blob: af8d1d64f858cd836655e2b9c1938490357e376d [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 Axboebcf5a062020-05-22 09:24:42 -060081#include <linux/pagemap.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070082
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020083#define CREATE_TRACE_POINTS
84#include <trace/events/io_uring.h>
85
Jens Axboe2b188cc2019-01-07 10:46:33 -070086#include <uapi/linux/io_uring.h>
87
88#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060089#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070090
Daniel Xu5277dea2019-09-14 14:23:45 -070091#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060092#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060093
94/*
95 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
96 */
97#define IORING_FILE_TABLE_SHIFT 9
98#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
99#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
100#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700101
102struct io_uring {
103 u32 head ____cacheline_aligned_in_smp;
104 u32 tail ____cacheline_aligned_in_smp;
105};
106
Stefan Bühler1e84b972019-04-24 23:54:16 +0200107/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000108 * This data is shared with the application through the mmap at offsets
109 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200110 *
111 * The offsets to the member fields are published through struct
112 * io_sqring_offsets when calling io_uring_setup.
113 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000114struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200115 /*
116 * Head and tail offsets into the ring; the offsets need to be
117 * masked to get valid indices.
118 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000119 * The kernel controls head of the sq ring and the tail of the cq ring,
120 * and the application controls tail of the sq ring and the head of the
121 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200122 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000123 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200124 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000125 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200126 * ring_entries - 1)
127 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000128 u32 sq_ring_mask, cq_ring_mask;
129 /* Ring sizes (constant, power of 2) */
130 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200131 /*
132 * Number of invalid entries dropped by the kernel due to
133 * invalid index stored in array
134 *
135 * Written by the kernel, shouldn't be modified by the
136 * application (i.e. get number of "new events" by comparing to
137 * cached value).
138 *
139 * After a new SQ head value was read by the application this
140 * counter includes all submissions that were dropped reaching
141 * the new SQ head (and possibly more).
142 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000143 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200144 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200145 * Runtime SQ flags
Stefan Bühler1e84b972019-04-24 23:54:16 +0200146 *
147 * Written by the kernel, shouldn't be modified by the
148 * application.
149 *
150 * The application needs a full memory barrier before checking
151 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
152 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000153 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200154 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200155 * Runtime CQ flags
156 *
157 * Written by the application, shouldn't be modified by the
158 * kernel.
159 */
160 u32 cq_flags;
161 /*
Stefan Bühler1e84b972019-04-24 23:54:16 +0200162 * Number of completion events lost because the queue was full;
163 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800164 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200165 * the completion queue.
166 *
167 * Written by the kernel, shouldn't be modified by the
168 * application (i.e. get number of "new events" by comparing to
169 * cached value).
170 *
171 * As completion events come in out of order this counter is not
172 * ordered with any other data.
173 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000174 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200175 /*
176 * Ring buffer of completion events.
177 *
178 * The kernel writes completion events fresh every time they are
179 * produced, so the application is allowed to modify pending
180 * entries.
181 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000182 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700183};
184
Jens Axboeedafcce2019-01-09 09:16:05 -0700185struct io_mapped_ubuf {
186 u64 ubuf;
187 size_t len;
188 struct bio_vec *bvec;
189 unsigned int nr_bvecs;
190};
191
Jens Axboe65e19f52019-10-26 07:20:21 -0600192struct fixed_file_table {
193 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700194};
195
Xiaoguang Wang05589552020-03-31 14:05:18 +0800196struct fixed_file_ref_node {
197 struct percpu_ref refs;
198 struct list_head node;
199 struct list_head file_list;
200 struct fixed_file_data *file_data;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600201 struct llist_node llist;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800202};
203
Jens Axboe05f3fb32019-12-09 11:22:50 -0700204struct fixed_file_data {
205 struct fixed_file_table *table;
206 struct io_ring_ctx *ctx;
207
Xiaoguang Wang05589552020-03-31 14:05:18 +0800208 struct percpu_ref *cur_refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700209 struct percpu_ref refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700210 struct completion done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800211 struct list_head ref_list;
212 spinlock_t lock;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700213};
214
Jens Axboe5a2e7452020-02-23 16:23:11 -0700215struct io_buffer {
216 struct list_head list;
217 __u64 addr;
218 __s32 len;
219 __u16 bid;
220};
221
Jens Axboe2b188cc2019-01-07 10:46:33 -0700222struct io_ring_ctx {
223 struct {
224 struct percpu_ref refs;
225 } ____cacheline_aligned_in_smp;
226
227 struct {
228 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800229 unsigned int compat: 1;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -0700230 unsigned int limit_mem: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800231 unsigned int cq_overflow_flushed: 1;
232 unsigned int drain_next: 1;
233 unsigned int eventfd_async: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700234
Hristo Venev75b28af2019-08-26 17:23:46 +0000235 /*
236 * Ring buffer of indices into array of io_uring_sqe, which is
237 * mmapped by the application using the IORING_OFF_SQES offset.
238 *
239 * This indirection could e.g. be used to assign fixed
240 * io_uring_sqe entries to operations and only submit them to
241 * the queue when needed.
242 *
243 * The kernel modifies neither the indices array nor the entries
244 * array.
245 */
246 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700247 unsigned cached_sq_head;
248 unsigned sq_entries;
249 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700250 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600251 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700252 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700253 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600254
255 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600256 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700257 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700258
Jens Axboefcb323c2019-10-24 12:39:47 -0600259 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700260 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700261 } ____cacheline_aligned_in_smp;
262
Hristo Venev75b28af2019-08-26 17:23:46 +0000263 struct io_rings *rings;
264
Jens Axboe2b188cc2019-01-07 10:46:33 -0700265 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600266 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700267 struct task_struct *sqo_thread; /* if using sq thread polling */
268 struct mm_struct *sqo_mm;
269 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700270
Jens Axboe6b063142019-01-10 22:13:58 -0700271 /*
272 * If used, fixed file set. Writers must ensure that ->refs is dead,
273 * readers must ensure that ->refs is alive as long as the file* is
274 * used. Only updated through io_uring_register(2).
275 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700276 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700277 unsigned nr_user_files;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300278 int ring_fd;
279 struct file *ring_file;
Jens Axboe6b063142019-01-10 22:13:58 -0700280
Jens Axboeedafcce2019-01-09 09:16:05 -0700281 /* if used, fixed mapped user buffers */
282 unsigned nr_user_bufs;
283 struct io_mapped_ubuf *user_bufs;
284
Jens Axboe2b188cc2019-01-07 10:46:33 -0700285 struct user_struct *user;
286
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700287 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700288
Jens Axboe0f158b42020-05-14 17:18:39 -0600289 struct completion ref_comp;
290 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700291
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700292 /* if all else fails... */
293 struct io_kiocb *fallback_req;
294
Jens Axboe206aefd2019-11-07 18:27:42 -0700295#if defined(CONFIG_UNIX)
296 struct socket *ring_sock;
297#endif
298
Jens Axboe5a2e7452020-02-23 16:23:11 -0700299 struct idr io_buffer_idr;
300
Jens Axboe071698e2020-01-28 10:04:42 -0700301 struct idr personality_idr;
302
Jens Axboe206aefd2019-11-07 18:27:42 -0700303 struct {
304 unsigned cached_cq_tail;
305 unsigned cq_entries;
306 unsigned cq_mask;
307 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700308 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700309 struct wait_queue_head cq_wait;
310 struct fasync_struct *cq_fasync;
311 struct eventfd_ctx *cq_ev_fd;
312 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700313
314 struct {
315 struct mutex uring_lock;
316 wait_queue_head_t wait;
317 } ____cacheline_aligned_in_smp;
318
319 struct {
320 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700321
Jens Axboedef596e2019-01-09 08:59:42 -0700322 /*
323 * ->poll_list is protected by the ctx->uring_lock for
324 * io_uring instances that don't use IORING_SETUP_SQPOLL.
325 * For SQPOLL, only the single threaded io_sq_thread() will
326 * manipulate the list, hence no extra locking is needed there.
327 */
328 struct list_head poll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700329 struct hlist_head *cancel_hash;
330 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700331 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600332
333 spinlock_t inflight_lock;
334 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700335 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600336
Jens Axboe4a38aed22020-05-14 17:21:15 -0600337 struct delayed_work file_put_work;
338 struct llist_head file_put_llist;
339
Jens Axboe85faa7b2020-04-09 18:14:00 -0600340 struct work_struct exit_work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700341};
342
Jens Axboe09bb8392019-03-13 12:39:28 -0600343/*
344 * First field must be the file pointer in all the
345 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
346 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700347struct io_poll_iocb {
348 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700349 union {
350 struct wait_queue_head *head;
351 u64 addr;
352 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700353 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600354 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700355 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700356 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700357};
358
Jens Axboeb5dba592019-12-11 14:02:38 -0700359struct io_close {
360 struct file *file;
361 struct file *put_file;
362 int fd;
363};
364
Jens Axboead8a48a2019-11-15 08:49:11 -0700365struct io_timeout_data {
366 struct io_kiocb *req;
367 struct hrtimer timer;
368 struct timespec64 ts;
369 enum hrtimer_mode mode;
370};
371
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700372struct io_accept {
373 struct file *file;
374 struct sockaddr __user *addr;
375 int __user *addr_len;
376 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600377 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700378};
379
380struct io_sync {
381 struct file *file;
382 loff_t len;
383 loff_t off;
384 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700385 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700386};
387
Jens Axboefbf23842019-12-17 18:45:56 -0700388struct io_cancel {
389 struct file *file;
390 u64 addr;
391};
392
Jens Axboeb29472e2019-12-17 18:50:29 -0700393struct io_timeout {
394 struct file *file;
395 u64 addr;
396 int flags;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300397 u32 off;
398 u32 target_seq;
Jens Axboeb29472e2019-12-17 18:50:29 -0700399};
400
Jens Axboe9adbd452019-12-20 08:45:55 -0700401struct io_rw {
402 /* NOTE: kiocb has the file as the first member, so don't do it here */
403 struct kiocb kiocb;
404 u64 addr;
405 u64 len;
406};
407
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700408struct io_connect {
409 struct file *file;
410 struct sockaddr __user *addr;
411 int addr_len;
412};
413
Jens Axboee47293f2019-12-20 08:58:21 -0700414struct io_sr_msg {
415 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700416 union {
417 struct user_msghdr __user *msg;
418 void __user *buf;
419 };
Jens Axboee47293f2019-12-20 08:58:21 -0700420 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700421 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700422 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700423 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700424};
425
Jens Axboe15b71ab2019-12-11 11:20:36 -0700426struct io_open {
427 struct file *file;
428 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700429 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700430 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600431 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700432};
433
Jens Axboe05f3fb32019-12-09 11:22:50 -0700434struct io_files_update {
435 struct file *file;
436 u64 arg;
437 u32 nr_args;
438 u32 offset;
439};
440
Jens Axboe4840e412019-12-25 22:03:45 -0700441struct io_fadvise {
442 struct file *file;
443 u64 offset;
444 u32 len;
445 u32 advice;
446};
447
Jens Axboec1ca7572019-12-25 22:18:28 -0700448struct io_madvise {
449 struct file *file;
450 u64 addr;
451 u32 len;
452 u32 advice;
453};
454
Jens Axboe3e4827b2020-01-08 15:18:09 -0700455struct io_epoll {
456 struct file *file;
457 int epfd;
458 int op;
459 int fd;
460 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700461};
462
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300463struct io_splice {
464 struct file *file_out;
465 struct file *file_in;
466 loff_t off_out;
467 loff_t off_in;
468 u64 len;
469 unsigned int flags;
470};
471
Jens Axboeddf0322d2020-02-23 16:41:33 -0700472struct io_provide_buf {
473 struct file *file;
474 __u64 addr;
475 __s32 len;
476 __u32 bgid;
477 __u16 nbufs;
478 __u16 bid;
479};
480
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700481struct io_statx {
482 struct file *file;
483 int dfd;
484 unsigned int mask;
485 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700486 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700487 struct statx __user *buffer;
488};
489
Jens Axboef499a022019-12-02 16:28:46 -0700490struct io_async_connect {
491 struct sockaddr_storage address;
492};
493
Jens Axboe03b12302019-12-02 18:50:25 -0700494struct io_async_msghdr {
495 struct iovec fast_iov[UIO_FASTIOV];
496 struct iovec *iov;
497 struct sockaddr __user *uaddr;
498 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700499 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700500};
501
Jens Axboef67676d2019-12-02 11:03:47 -0700502struct io_async_rw {
503 struct iovec fast_iov[UIO_FASTIOV];
504 struct iovec *iov;
505 ssize_t nr_segs;
506 ssize_t size;
Jens Axboebcf5a062020-05-22 09:24:42 -0600507 struct wait_page_queue wpq;
508 struct callback_head task_work;
Jens Axboef67676d2019-12-02 11:03:47 -0700509};
510
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700511struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700512 union {
513 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700514 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700515 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700516 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700517 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700518};
519
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300520enum {
521 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
522 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
523 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
524 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
525 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700526 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300527
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300528 REQ_F_LINK_HEAD_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300529 REQ_F_LINK_NEXT_BIT,
530 REQ_F_FAIL_LINK_BIT,
531 REQ_F_INFLIGHT_BIT,
532 REQ_F_CUR_POS_BIT,
533 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300534 REQ_F_LINK_TIMEOUT_BIT,
535 REQ_F_TIMEOUT_BIT,
536 REQ_F_ISREG_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300537 REQ_F_TIMEOUT_NOSEQ_BIT,
538 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300539 REQ_F_NEED_CLEANUP_BIT,
Jens Axboe2ca10252020-02-13 17:17:35 -0700540 REQ_F_OVERFLOW_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700541 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700542 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600543 REQ_F_NO_FILE_TABLE_BIT,
Pavel Begunkovd4c81f32020-06-08 21:08:19 +0300544 REQ_F_QUEUE_TIMEOUT_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800545 REQ_F_WORK_INITIALIZED_BIT,
Pavel Begunkov4dd28242020-06-15 10:33:13 +0300546 REQ_F_TASK_PINNED_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700547
548 /* not a real bit, just to check we're not overflowing the space */
549 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300550};
551
552enum {
553 /* ctx owns file */
554 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
555 /* drain existing IO first */
556 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
557 /* linked sqes */
558 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
559 /* doesn't sever on completion < 0 */
560 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
561 /* IOSQE_ASYNC */
562 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700563 /* IOSQE_BUFFER_SELECT */
564 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300565
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300566 /* head of a link */
567 REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300568 /* already grabbed next link */
569 REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT),
570 /* fail rest of links */
571 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
572 /* on inflight list */
573 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
574 /* read/write uses file position */
575 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
576 /* must not punt to workers */
577 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300578 /* has linked timeout */
579 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
580 /* timeout request */
581 REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT),
582 /* regular file */
583 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300584 /* no timeout sequence */
585 REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT),
586 /* completion under lock */
587 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300588 /* needs cleanup */
589 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboe2ca10252020-02-13 17:17:35 -0700590 /* in overflow list */
591 REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700592 /* already went through poll handler */
593 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700594 /* buffer already selected */
595 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600596 /* doesn't need file table for this request */
597 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Pavel Begunkovd4c81f32020-06-08 21:08:19 +0300598 /* needs to queue linked timeout */
599 REQ_F_QUEUE_TIMEOUT = BIT(REQ_F_QUEUE_TIMEOUT_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800600 /* io_wq_work is initialized */
601 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Pavel Begunkov4dd28242020-06-15 10:33:13 +0300602 /* req->task is refcounted */
603 REQ_F_TASK_PINNED = BIT(REQ_F_TASK_PINNED_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700604};
605
606struct async_poll {
607 struct io_poll_iocb poll;
608 struct io_wq_work work;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300609};
610
Jens Axboe09bb8392019-03-13 12:39:28 -0600611/*
612 * NOTE! Each of the iocb union members has the file pointer
613 * as the first entry in their struct definition. So you can
614 * access the file pointer through any of the sub-structs,
615 * or directly as just 'ki_filp' in this struct.
616 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700617struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700618 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600619 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700620 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700621 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700622 struct io_accept accept;
623 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700624 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700625 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700626 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700627 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700628 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700629 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700630 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700631 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700632 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700633 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300634 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700635 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700636 struct io_statx statx;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700637 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700638
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700639 struct io_async_ctx *io;
Pavel Begunkovc398ecb2020-04-09 08:17:59 +0300640 int cflags;
Jens Axboed625c6e2019-12-17 19:53:05 -0700641 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800642 /* polled IO has completed */
643 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700644
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700645 u16 buf_index;
646
Jens Axboe2b188cc2019-01-07 10:46:33 -0700647 struct io_ring_ctx *ctx;
Jens Axboed7718a92020-02-14 22:23:12 -0700648 struct list_head list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700649 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700650 refcount_t refs;
Jens Axboe3537b6a2020-04-03 11:19:06 -0600651 struct task_struct *task;
652 unsigned long fsize;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700653 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600654 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600655 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700656
Jens Axboed7718a92020-02-14 22:23:12 -0700657 struct list_head link_list;
658
Jens Axboefcb323c2019-10-24 12:39:47 -0600659 struct list_head inflight_entry;
660
Xiaoguang Wang05589552020-03-31 14:05:18 +0800661 struct percpu_ref *fixed_file_refs;
662
Jens Axboeb41e9852020-02-17 09:52:41 -0700663 union {
664 /*
665 * Only commands that never go async can use the below fields,
Jens Axboed7718a92020-02-14 22:23:12 -0700666 * obviously. Right now only IORING_OP_POLL_ADD uses them, and
667 * async armed poll handlers for regular commands. The latter
668 * restore the work, if needed.
Jens Axboeb41e9852020-02-17 09:52:41 -0700669 */
670 struct {
Jens Axboed7718a92020-02-14 22:23:12 -0700671 struct hlist_node hash_node;
672 struct async_poll *apoll;
Jens Axboeb41e9852020-02-17 09:52:41 -0700673 };
674 struct io_wq_work work;
675 };
Pavel Begunkov8ef77762020-06-27 14:04:59 +0300676 struct callback_head task_work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700677};
678
Jens Axboedef596e2019-01-09 08:59:42 -0700679#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700680
Jens Axboe013538b2020-06-22 09:29:15 -0600681struct io_comp_state {
682 unsigned int nr;
683 struct list_head list;
684 struct io_ring_ctx *ctx;
685};
686
Jens Axboe9a56a232019-01-09 09:06:50 -0700687struct io_submit_state {
688 struct blk_plug plug;
689
690 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700691 * io_kiocb alloc cache
692 */
693 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300694 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700695
696 /*
Jens Axboe013538b2020-06-22 09:29:15 -0600697 * Batch completion logic
698 */
699 struct io_comp_state comp;
700
701 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700702 * File reference cache
703 */
704 struct file *file;
705 unsigned int fd;
706 unsigned int has_refs;
707 unsigned int used_refs;
708 unsigned int ios_left;
709};
710
Jens Axboed3656342019-12-18 09:50:26 -0700711struct io_op_def {
712 /* needs req->io allocated for deferral/async */
713 unsigned async_ctx : 1;
714 /* needs current->mm setup, does mm access */
715 unsigned needs_mm : 1;
716 /* needs req->file assigned */
717 unsigned needs_file : 1;
Jens Axboefd2206e2020-06-02 16:40:47 -0600718 /* don't fail if file grab fails */
719 unsigned needs_file_no_error : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700720 /* hash wq insertion if file is a regular file */
721 unsigned hash_reg_file : 1;
722 /* unbound wq insertion if file is a non-regular file */
723 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700724 /* opcode is not supported by this kernel */
725 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700726 /* needs file table */
727 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700728 /* needs ->fs */
729 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700730 /* set if opcode supports polled "wait" */
731 unsigned pollin : 1;
732 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700733 /* op supports buffer selection */
734 unsigned buffer_select : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700735};
736
737static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300738 [IORING_OP_NOP] = {},
739 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700740 .async_ctx = 1,
741 .needs_mm = 1,
742 .needs_file = 1,
743 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700744 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700745 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700746 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300747 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700748 .async_ctx = 1,
749 .needs_mm = 1,
750 .needs_file = 1,
751 .hash_reg_file = 1,
752 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700753 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700754 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300755 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700756 .needs_file = 1,
757 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300758 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700759 .needs_file = 1,
760 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700761 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700762 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300763 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700764 .needs_file = 1,
765 .hash_reg_file = 1,
766 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700767 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700768 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300769 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700770 .needs_file = 1,
771 .unbound_nonreg_file = 1,
772 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300773 [IORING_OP_POLL_REMOVE] = {},
774 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700775 .needs_file = 1,
776 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300777 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700778 .async_ctx = 1,
779 .needs_mm = 1,
780 .needs_file = 1,
781 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700782 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700783 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700784 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300785 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700786 .async_ctx = 1,
787 .needs_mm = 1,
788 .needs_file = 1,
789 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700790 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700791 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700792 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700793 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300794 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700795 .async_ctx = 1,
796 .needs_mm = 1,
797 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300798 [IORING_OP_TIMEOUT_REMOVE] = {},
799 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700800 .needs_mm = 1,
801 .needs_file = 1,
802 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700803 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700804 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700805 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300806 [IORING_OP_ASYNC_CANCEL] = {},
807 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700808 .async_ctx = 1,
809 .needs_mm = 1,
810 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300811 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700812 .async_ctx = 1,
813 .needs_mm = 1,
814 .needs_file = 1,
815 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700816 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700817 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300818 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700819 .needs_file = 1,
820 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300821 [IORING_OP_OPENAT] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700822 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700823 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700824 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300825 [IORING_OP_CLOSE] = {
Jens Axboefd2206e2020-06-02 16:40:47 -0600826 .needs_file = 1,
827 .needs_file_no_error = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700828 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700829 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300830 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700831 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700832 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700833 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300834 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700835 .needs_mm = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700836 .needs_fs = 1,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600837 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700838 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300839 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700840 .needs_mm = 1,
841 .needs_file = 1,
842 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700843 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700844 .buffer_select = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700845 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300846 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700847 .needs_mm = 1,
848 .needs_file = 1,
849 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700850 .pollout = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700851 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300852 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700853 .needs_file = 1,
854 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300855 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700856 .needs_mm = 1,
857 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300858 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700859 .needs_mm = 1,
860 .needs_file = 1,
861 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700862 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700863 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300864 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700865 .needs_mm = 1,
866 .needs_file = 1,
867 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700868 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700869 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700870 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300871 [IORING_OP_OPENAT2] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700872 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700873 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700874 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700875 [IORING_OP_EPOLL_CTL] = {
876 .unbound_nonreg_file = 1,
877 .file_table = 1,
878 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300879 [IORING_OP_SPLICE] = {
880 .needs_file = 1,
881 .hash_reg_file = 1,
882 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700883 },
884 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700885 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300886 [IORING_OP_TEE] = {
887 .needs_file = 1,
888 .hash_reg_file = 1,
889 .unbound_nonreg_file = 1,
890 },
Jens Axboed3656342019-12-18 09:50:26 -0700891};
892
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -0700893enum io_mem_account {
894 ACCT_LOCKED,
895 ACCT_PINNED,
896};
897
Jens Axboe78e19bb2019-11-06 15:21:34 -0700898static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800899static void io_put_req(struct io_kiocb *req);
Jens Axboec40f6372020-06-25 15:39:59 -0600900static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700901static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700902static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
903static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700904static int __io_sqe_files_update(struct io_ring_ctx *ctx,
905 struct io_uring_files_update *ip,
906 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700907static int io_grab_files(struct io_kiocb *req);
Jens Axboe2237d762020-06-26 13:44:16 -0600908static void io_complete_rw_common(struct kiocb *kiocb, long res,
909 struct io_comp_state *cs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300910static void io_cleanup_req(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700911static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
912 int fd, struct file **out_file, bool fixed);
913static void __io_queue_sqe(struct io_kiocb *req,
Jens Axboef13fad72020-06-22 09:34:30 -0600914 const struct io_uring_sqe *sqe,
915 struct io_comp_state *cs);
Jens Axboede0617e2019-04-06 21:51:27 -0600916
Jens Axboeb63534c2020-06-04 11:28:00 -0600917static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
918 struct iovec **iovec, struct iov_iter *iter,
919 bool needs_lock);
920static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
921 struct iovec *iovec, struct iovec *fast_iov,
922 struct iov_iter *iter);
923
Jens Axboe2b188cc2019-01-07 10:46:33 -0700924static struct kmem_cache *req_cachep;
925
926static const struct file_operations io_uring_fops;
927
928struct sock *io_uring_get_socket(struct file *file)
929{
930#if defined(CONFIG_UNIX)
931 if (file->f_op == &io_uring_fops) {
932 struct io_ring_ctx *ctx = file->private_data;
933
934 return ctx->ring_sock->sk;
935 }
936#endif
937 return NULL;
938}
939EXPORT_SYMBOL(io_uring_get_socket);
940
Pavel Begunkov4dd28242020-06-15 10:33:13 +0300941static void io_get_req_task(struct io_kiocb *req)
942{
943 if (req->flags & REQ_F_TASK_PINNED)
944 return;
945 get_task_struct(req->task);
946 req->flags |= REQ_F_TASK_PINNED;
947}
948
949/* not idempotent -- it doesn't clear REQ_F_TASK_PINNED */
950static void __io_put_req_task(struct io_kiocb *req)
951{
952 if (req->flags & REQ_F_TASK_PINNED)
953 put_task_struct(req->task);
954}
955
Jens Axboec40f6372020-06-25 15:39:59 -0600956static void io_sq_thread_drop_mm(struct io_ring_ctx *ctx)
957{
958 struct mm_struct *mm = current->mm;
959
960 if (mm) {
961 kthread_unuse_mm(mm);
962 mmput(mm);
963 }
964}
965
966static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
967{
968 if (!current->mm) {
969 if (unlikely(!mmget_not_zero(ctx->sqo_mm)))
970 return -EFAULT;
971 kthread_use_mm(ctx->sqo_mm);
972 }
973
974 return 0;
975}
976
977static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx,
978 struct io_kiocb *req)
979{
980 if (!io_op_defs[req->opcode].needs_mm)
981 return 0;
982 return __io_sq_thread_acquire_mm(ctx);
983}
984
985static inline void req_set_fail_links(struct io_kiocb *req)
986{
987 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
988 req->flags |= REQ_F_FAIL_LINK;
989}
990
Jens Axboe4a38aed22020-05-14 17:21:15 -0600991static void io_file_put_work(struct work_struct *work);
992
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800993/*
994 * Note: must call io_req_init_async() for the first time you
995 * touch any members of io_wq_work.
996 */
997static inline void io_req_init_async(struct io_kiocb *req)
998{
999 if (req->flags & REQ_F_WORK_INITIALIZED)
1000 return;
1001
1002 memset(&req->work, 0, sizeof(req->work));
1003 req->flags |= REQ_F_WORK_INITIALIZED;
1004}
1005
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03001006static inline bool io_async_submit(struct io_ring_ctx *ctx)
1007{
1008 return ctx->flags & IORING_SETUP_SQPOLL;
1009}
1010
Jens Axboe2b188cc2019-01-07 10:46:33 -07001011static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1012{
1013 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1014
Jens Axboe0f158b42020-05-14 17:18:39 -06001015 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001016}
1017
1018static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1019{
1020 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001021 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001022
1023 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1024 if (!ctx)
1025 return NULL;
1026
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001027 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
1028 if (!ctx->fallback_req)
1029 goto err;
1030
Jens Axboe78076bb2019-12-04 19:56:40 -07001031 /*
1032 * Use 5 bits less than the max cq entries, that should give us around
1033 * 32 entries per hash list if totally full and uniformly spread.
1034 */
1035 hash_bits = ilog2(p->cq_entries);
1036 hash_bits -= 5;
1037 if (hash_bits <= 0)
1038 hash_bits = 1;
1039 ctx->cancel_hash_bits = hash_bits;
1040 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1041 GFP_KERNEL);
1042 if (!ctx->cancel_hash)
1043 goto err;
1044 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1045
Roman Gushchin21482892019-05-07 10:01:48 -07001046 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001047 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1048 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001049
1050 ctx->flags = p->flags;
Jens Axboe583863e2020-05-17 09:20:00 -06001051 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001052 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001053 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001054 init_completion(&ctx->ref_comp);
1055 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001056 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001057 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001058 mutex_init(&ctx->uring_lock);
1059 init_waitqueue_head(&ctx->wait);
1060 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001061 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001062 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001063 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001064 init_waitqueue_head(&ctx->inflight_wait);
1065 spin_lock_init(&ctx->inflight_lock);
1066 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe4a38aed22020-05-14 17:21:15 -06001067 INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work);
1068 init_llist_head(&ctx->file_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001069 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001070err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001071 if (ctx->fallback_req)
1072 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -07001073 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001074 kfree(ctx);
1075 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001076}
1077
Bob Liu9d858b22019-11-13 18:06:25 +08001078static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -06001079{
Jackie Liua197f662019-11-08 08:09:12 -07001080 struct io_ring_ctx *ctx = req->ctx;
1081
Pavel Begunkov31af27c2020-04-15 00:39:50 +03001082 return req->sequence != ctx->cached_cq_tail
1083 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -06001084}
1085
Bob Liu9d858b22019-11-13 18:06:25 +08001086static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001087{
Pavel Begunkov87987892020-01-18 01:22:30 +03001088 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +08001089 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001090
Bob Liu9d858b22019-11-13 18:06:25 +08001091 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001092}
1093
Jens Axboede0617e2019-04-06 21:51:27 -06001094static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001095{
Hristo Venev75b28af2019-08-26 17:23:46 +00001096 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001097
Pavel Begunkov07910152020-01-17 03:52:46 +03001098 /* order cqe stores with ring update */
1099 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001100
Pavel Begunkov07910152020-01-17 03:52:46 +03001101 if (wq_has_sleeper(&ctx->cq_wait)) {
1102 wake_up_interruptible(&ctx->cq_wait);
1103 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001104 }
1105}
1106
Jens Axboecccf0ee2020-01-27 16:34:48 -07001107static inline void io_req_work_grab_env(struct io_kiocb *req,
1108 const struct io_op_def *def)
Jens Axboe18d9be12019-09-10 09:13:05 -06001109{
Jens Axboecccf0ee2020-01-27 16:34:48 -07001110 if (!req->work.mm && def->needs_mm) {
1111 mmgrab(current->mm);
1112 req->work.mm = current->mm;
1113 }
1114 if (!req->work.creds)
1115 req->work.creds = get_current_cred();
Jens Axboeff002b32020-02-07 16:05:21 -07001116 if (!req->work.fs && def->needs_fs) {
1117 spin_lock(&current->fs->lock);
1118 if (!current->fs->in_exec) {
1119 req->work.fs = current->fs;
1120 req->work.fs->users++;
1121 } else {
1122 req->work.flags |= IO_WQ_WORK_CANCEL;
1123 }
1124 spin_unlock(&current->fs->lock);
1125 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001126}
1127
1128static inline void io_req_work_drop_env(struct io_kiocb *req)
1129{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001130 if (!(req->flags & REQ_F_WORK_INITIALIZED))
1131 return;
1132
Jens Axboecccf0ee2020-01-27 16:34:48 -07001133 if (req->work.mm) {
1134 mmdrop(req->work.mm);
1135 req->work.mm = NULL;
1136 }
1137 if (req->work.creds) {
1138 put_cred(req->work.creds);
1139 req->work.creds = NULL;
1140 }
Jens Axboeff002b32020-02-07 16:05:21 -07001141 if (req->work.fs) {
1142 struct fs_struct *fs = req->work.fs;
1143
1144 spin_lock(&req->work.fs->lock);
1145 if (--fs->users)
1146 fs = NULL;
1147 spin_unlock(&req->work.fs->lock);
1148 if (fs)
1149 free_fs_struct(fs);
1150 }
Jens Axboe561fb042019-10-24 07:25:42 -06001151}
1152
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001153static inline void io_prep_async_work(struct io_kiocb *req,
Jens Axboe94ae5e72019-11-14 19:39:52 -07001154 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -06001155{
Jens Axboed3656342019-12-18 09:50:26 -07001156 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe54a91f32019-09-10 09:15:04 -06001157
Jens Axboed3656342019-12-18 09:50:26 -07001158 if (req->flags & REQ_F_ISREG) {
1159 if (def->hash_reg_file)
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001160 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001161 } else {
1162 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001163 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001164 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001165
Pavel Begunkov59960b92020-06-15 16:36:30 +03001166 io_req_init_async(req);
Jens Axboecccf0ee2020-01-27 16:34:48 -07001167 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -06001168
Jens Axboe94ae5e72019-11-14 19:39:52 -07001169 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001170}
1171
Jackie Liua197f662019-11-08 08:09:12 -07001172static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001173{
Jackie Liua197f662019-11-08 08:09:12 -07001174 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001175 struct io_kiocb *link;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001176
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001177 io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06001178
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001179 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1180 &req->work, req->flags);
1181 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001182
1183 if (link)
1184 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -06001185}
1186
Jens Axboe5262f562019-09-17 12:26:57 -06001187static void io_kill_timeout(struct io_kiocb *req)
1188{
1189 int ret;
1190
Jens Axboe2d283902019-12-04 11:08:05 -07001191 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001192 if (ret != -1) {
1193 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -06001194 list_del_init(&req->list);
Pavel Begunkovf0e20b82020-03-07 01:15:22 +03001195 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001196 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001197 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001198 }
1199}
1200
1201static void io_kill_timeouts(struct io_ring_ctx *ctx)
1202{
1203 struct io_kiocb *req, *tmp;
1204
1205 spin_lock_irq(&ctx->completion_lock);
1206 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
1207 io_kill_timeout(req);
1208 spin_unlock_irq(&ctx->completion_lock);
1209}
1210
Pavel Begunkov04518942020-05-26 20:34:05 +03001211static void __io_queue_deferred(struct io_ring_ctx *ctx)
1212{
1213 do {
1214 struct io_kiocb *req = list_first_entry(&ctx->defer_list,
1215 struct io_kiocb, list);
1216
1217 if (req_need_defer(req))
1218 break;
1219 list_del_init(&req->list);
1220 io_queue_async_work(req);
1221 } while (!list_empty(&ctx->defer_list));
1222}
1223
Pavel Begunkov360428f2020-05-30 14:54:17 +03001224static void io_flush_timeouts(struct io_ring_ctx *ctx)
1225{
1226 while (!list_empty(&ctx->timeout_list)) {
1227 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
1228 struct io_kiocb, list);
1229
1230 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
1231 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001232 if (req->timeout.target_seq != ctx->cached_cq_tail
1233 - atomic_read(&ctx->cq_timeouts))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001234 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001235
Pavel Begunkov360428f2020-05-30 14:54:17 +03001236 list_del_init(&req->list);
1237 io_kill_timeout(req);
1238 }
1239}
1240
Jens Axboede0617e2019-04-06 21:51:27 -06001241static void io_commit_cqring(struct io_ring_ctx *ctx)
1242{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001243 io_flush_timeouts(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001244 __io_commit_cqring(ctx);
1245
Pavel Begunkov04518942020-05-26 20:34:05 +03001246 if (unlikely(!list_empty(&ctx->defer_list)))
1247 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001248}
1249
Jens Axboe2b188cc2019-01-07 10:46:33 -07001250static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1251{
Hristo Venev75b28af2019-08-26 17:23:46 +00001252 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001253 unsigned tail;
1254
1255 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001256 /*
1257 * writes to the cq entry need to come after reading head; the
1258 * control dependency is enough as we're using WRITE_ONCE to
1259 * fill the cq entry
1260 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001261 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001262 return NULL;
1263
1264 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001265 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001266}
1267
Jens Axboef2842ab2020-01-08 11:04:00 -07001268static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1269{
Jens Axboef0b493e2020-02-01 21:30:11 -07001270 if (!ctx->cq_ev_fd)
1271 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001272 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1273 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001274 if (!ctx->eventfd_async)
1275 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001276 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001277}
1278
Jens Axboeb41e9852020-02-17 09:52:41 -07001279static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001280{
1281 if (waitqueue_active(&ctx->wait))
1282 wake_up(&ctx->wait);
1283 if (waitqueue_active(&ctx->sqo_wait))
1284 wake_up(&ctx->sqo_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001285 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001286 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001287}
1288
Jens Axboec4a2ed72019-11-21 21:01:26 -07001289/* Returns true if there are no backlogged entries after the flush */
1290static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001291{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001292 struct io_rings *rings = ctx->rings;
1293 struct io_uring_cqe *cqe;
1294 struct io_kiocb *req;
1295 unsigned long flags;
1296 LIST_HEAD(list);
1297
1298 if (!force) {
1299 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001300 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001301 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1302 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001303 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001304 }
1305
1306 spin_lock_irqsave(&ctx->completion_lock, flags);
1307
1308 /* if force is set, the ring is going away. always drop after that */
1309 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001310 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001311
Jens Axboec4a2ed72019-11-21 21:01:26 -07001312 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001313 while (!list_empty(&ctx->cq_overflow_list)) {
1314 cqe = io_get_cqring(ctx);
1315 if (!cqe && !force)
1316 break;
1317
1318 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1319 list);
1320 list_move(&req->list, &list);
Jens Axboe2ca10252020-02-13 17:17:35 -07001321 req->flags &= ~REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001322 if (cqe) {
1323 WRITE_ONCE(cqe->user_data, req->user_data);
1324 WRITE_ONCE(cqe->res, req->result);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001325 WRITE_ONCE(cqe->flags, req->cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001326 } else {
1327 WRITE_ONCE(ctx->rings->cq_overflow,
1328 atomic_inc_return(&ctx->cached_cq_overflow));
1329 }
1330 }
1331
1332 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001333 if (cqe) {
1334 clear_bit(0, &ctx->sq_check_overflow);
1335 clear_bit(0, &ctx->cq_check_overflow);
1336 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001337 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1338 io_cqring_ev_posted(ctx);
1339
1340 while (!list_empty(&list)) {
1341 req = list_first_entry(&list, struct io_kiocb, list);
1342 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001343 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001344 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001345
1346 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001347}
1348
Jens Axboebcda7ba2020-02-23 16:42:51 -07001349static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001350{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001351 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001352 struct io_uring_cqe *cqe;
1353
Jens Axboe78e19bb2019-11-06 15:21:34 -07001354 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001355
Jens Axboe2b188cc2019-01-07 10:46:33 -07001356 /*
1357 * If we can't get a cq entry, userspace overflowed the
1358 * submission (by quite a lot). Increment the overflow count in
1359 * the ring.
1360 */
1361 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001362 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001363 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001364 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001365 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001366 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001367 WRITE_ONCE(ctx->rings->cq_overflow,
1368 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001369 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001370 if (list_empty(&ctx->cq_overflow_list)) {
1371 set_bit(0, &ctx->sq_check_overflow);
1372 set_bit(0, &ctx->cq_check_overflow);
1373 }
Jens Axboe2ca10252020-02-13 17:17:35 -07001374 req->flags |= REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001375 refcount_inc(&req->refs);
1376 req->result = res;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001377 req->cflags = cflags;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001378 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001379 }
1380}
1381
Jens Axboebcda7ba2020-02-23 16:42:51 -07001382static void io_cqring_fill_event(struct io_kiocb *req, long res)
1383{
1384 __io_cqring_fill_event(req, res, 0);
1385}
1386
Jens Axboee1e16092020-06-22 09:17:17 -06001387static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001388{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001389 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001390 unsigned long flags;
1391
1392 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001393 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001394 io_commit_cqring(ctx);
1395 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1396
Jens Axboe8c838782019-03-12 15:48:16 -06001397 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001398}
1399
Jens Axboe229a7b62020-06-22 10:13:11 -06001400static void io_submit_flush_completions(struct io_comp_state *cs)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001401{
Jens Axboe229a7b62020-06-22 10:13:11 -06001402 struct io_ring_ctx *ctx = cs->ctx;
1403
1404 spin_lock_irq(&ctx->completion_lock);
1405 while (!list_empty(&cs->list)) {
1406 struct io_kiocb *req;
1407
1408 req = list_first_entry(&cs->list, struct io_kiocb, list);
1409 list_del(&req->list);
1410 io_cqring_fill_event(req, req->result);
1411 if (!(req->flags & REQ_F_LINK_HEAD)) {
1412 req->flags |= REQ_F_COMP_LOCKED;
1413 io_put_req(req);
1414 } else {
1415 spin_unlock_irq(&ctx->completion_lock);
1416 io_put_req(req);
1417 spin_lock_irq(&ctx->completion_lock);
1418 }
1419 }
1420 io_commit_cqring(ctx);
1421 spin_unlock_irq(&ctx->completion_lock);
1422
1423 io_cqring_ev_posted(ctx);
1424 cs->nr = 0;
1425}
1426
1427static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags,
1428 struct io_comp_state *cs)
1429{
1430 if (!cs) {
1431 io_cqring_add_event(req, res, cflags);
1432 io_put_req(req);
1433 } else {
1434 req->result = res;
1435 list_add_tail(&req->list, &cs->list);
1436 if (++cs->nr >= 32)
1437 io_submit_flush_completions(cs);
1438 }
Jens Axboee1e16092020-06-22 09:17:17 -06001439}
1440
1441static void io_req_complete(struct io_kiocb *req, long res)
1442{
Jens Axboe229a7b62020-06-22 10:13:11 -06001443 __io_req_complete(req, res, 0, NULL);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001444}
1445
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001446static inline bool io_is_fallback_req(struct io_kiocb *req)
1447{
1448 return req == (struct io_kiocb *)
1449 ((unsigned long) req->ctx->fallback_req & ~1UL);
1450}
1451
1452static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1453{
1454 struct io_kiocb *req;
1455
1456 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001457 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001458 return req;
1459
1460 return NULL;
1461}
1462
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001463static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1464 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001465{
Jens Axboefd6fab22019-03-14 16:30:06 -06001466 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001467 struct io_kiocb *req;
1468
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001469 if (!state->free_reqs) {
Jens Axboe2579f912019-01-09 09:10:43 -07001470 size_t sz;
1471 int ret;
1472
1473 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001474 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1475
1476 /*
1477 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1478 * retry single alloc to be on the safe side.
1479 */
1480 if (unlikely(ret <= 0)) {
1481 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1482 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001483 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001484 ret = 1;
1485 }
Jens Axboe2579f912019-01-09 09:10:43 -07001486 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001487 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001488 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001489 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001490 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001491 }
1492
Jens Axboe2579f912019-01-09 09:10:43 -07001493 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001494fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001495 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001496}
1497
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001498static inline void io_put_file(struct io_kiocb *req, struct file *file,
1499 bool fixed)
1500{
1501 if (fixed)
Xiaoguang Wang05589552020-03-31 14:05:18 +08001502 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001503 else
1504 fput(file);
1505}
1506
Pavel Begunkove6543a82020-06-28 12:52:30 +03001507static void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001508{
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001509 if (req->flags & REQ_F_NEED_CLEANUP)
1510 io_cleanup_req(req);
1511
YueHaibing96fd84d2020-01-07 22:22:44 +08001512 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001513 if (req->file)
1514 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Pavel Begunkov4dd28242020-06-15 10:33:13 +03001515 __io_put_req_task(req);
Jens Axboecccf0ee2020-01-27 16:34:48 -07001516 io_req_work_drop_env(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06001517
Jens Axboefcb323c2019-10-24 12:39:47 -06001518 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001519 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001520 unsigned long flags;
1521
1522 spin_lock_irqsave(&ctx->inflight_lock, flags);
1523 list_del(&req->inflight_entry);
1524 if (waitqueue_active(&ctx->inflight_wait))
1525 wake_up(&ctx->inflight_wait);
1526 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1527 }
Pavel Begunkove6543a82020-06-28 12:52:30 +03001528}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001529
Pavel Begunkove6543a82020-06-28 12:52:30 +03001530static void __io_free_req(struct io_kiocb *req)
1531{
1532 io_dismantle_req(req);
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001533 percpu_ref_put(&req->ctx->refs);
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03001534 if (likely(!io_is_fallback_req(req)))
1535 kmem_cache_free(req_cachep, req);
1536 else
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001537 clear_bit_unlock(0, (unsigned long *) &req->ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -06001538}
1539
Jens Axboec6ca97b302019-12-28 12:11:08 -07001540struct req_batch {
1541 void *reqs[IO_IOPOLL_BATCH];
1542 int to_free;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001543};
1544
1545static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1546{
1547 if (!rb->to_free)
1548 return;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001549
Jens Axboec6ca97b302019-12-28 12:11:08 -07001550 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
1551 percpu_ref_put_many(&ctx->refs, rb->to_free);
Pavel Begunkov2757a232020-06-28 12:52:31 +03001552 rb->to_free = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001553}
1554
Jackie Liua197f662019-11-08 08:09:12 -07001555static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001556{
Jackie Liua197f662019-11-08 08:09:12 -07001557 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001558 int ret;
1559
Jens Axboe2d283902019-12-04 11:08:05 -07001560 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001561 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001562 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001563 io_commit_cqring(ctx);
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001564 req->flags &= ~REQ_F_LINK_HEAD;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001565 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001566 return true;
1567 }
1568
1569 return false;
1570}
1571
Jens Axboeba816ad2019-09-28 11:36:45 -06001572static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001573{
Jens Axboe2665abf2019-11-05 12:40:47 -07001574 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001575 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001576
Jens Axboe4d7dd462019-11-20 13:03:52 -07001577 /* Already got next link */
1578 if (req->flags & REQ_F_LINK_NEXT)
1579 return;
1580
Jens Axboe9e645e112019-05-10 16:07:28 -06001581 /*
1582 * The list should never be empty when we are called here. But could
1583 * potentially happen if the chain is messed up, check to be on the
1584 * safe side.
1585 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001586 while (!list_empty(&req->link_list)) {
1587 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1588 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001589
Pavel Begunkov44932332019-12-05 16:16:35 +03001590 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1591 (nxt->flags & REQ_F_TIMEOUT))) {
1592 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001593 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001594 req->flags &= ~REQ_F_LINK_TIMEOUT;
1595 continue;
1596 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001597
Pavel Begunkov44932332019-12-05 16:16:35 +03001598 list_del_init(&req->link_list);
1599 if (!list_empty(&nxt->link_list))
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001600 nxt->flags |= REQ_F_LINK_HEAD;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001601 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001602 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001603 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001604
Jens Axboe4d7dd462019-11-20 13:03:52 -07001605 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001606 if (wake_ev)
1607 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001608}
1609
1610/*
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001611 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
Jens Axboe9e645e112019-05-10 16:07:28 -06001612 */
1613static void io_fail_links(struct io_kiocb *req)
1614{
Jens Axboe2665abf2019-11-05 12:40:47 -07001615 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001616 unsigned long flags;
1617
1618 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001619
1620 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001621 struct io_kiocb *link = list_first_entry(&req->link_list,
1622 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001623
Pavel Begunkov44932332019-12-05 16:16:35 +03001624 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001625 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001626
1627 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001628 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001629 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001630 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001631 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001632 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001633 }
Jens Axboe5d960722019-11-19 15:31:28 -07001634 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001635 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001636
1637 io_commit_cqring(ctx);
1638 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1639 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001640}
1641
Jens Axboe4d7dd462019-11-20 13:03:52 -07001642static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001643{
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001644 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001645 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001646
Jens Axboe9e645e112019-05-10 16:07:28 -06001647 /*
1648 * If LINK is set, we have dependent requests in this chain. If we
1649 * didn't fail this request, queue the first one up, moving any other
1650 * dependencies to the next request. In case of failure, fail the rest
1651 * of the chain.
1652 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001653 if (req->flags & REQ_F_FAIL_LINK) {
1654 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001655 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1656 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001657 struct io_ring_ctx *ctx = req->ctx;
1658 unsigned long flags;
1659
1660 /*
1661 * If this is a timeout link, we could be racing with the
1662 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001663 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001664 */
1665 spin_lock_irqsave(&ctx->completion_lock, flags);
1666 io_req_link_next(req, nxt);
1667 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1668 } else {
1669 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001670 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001671}
Jens Axboe9e645e112019-05-10 16:07:28 -06001672
Jens Axboec40f6372020-06-25 15:39:59 -06001673static void __io_req_task_cancel(struct io_kiocb *req, int error)
1674{
1675 struct io_ring_ctx *ctx = req->ctx;
1676
1677 spin_lock_irq(&ctx->completion_lock);
1678 io_cqring_fill_event(req, error);
1679 io_commit_cqring(ctx);
1680 spin_unlock_irq(&ctx->completion_lock);
1681
1682 io_cqring_ev_posted(ctx);
1683 req_set_fail_links(req);
1684 io_double_put_req(req);
1685}
1686
1687static void io_req_task_cancel(struct callback_head *cb)
1688{
1689 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
1690
1691 __io_req_task_cancel(req, -ECANCELED);
1692}
1693
1694static void __io_req_task_submit(struct io_kiocb *req)
1695{
1696 struct io_ring_ctx *ctx = req->ctx;
1697
1698 __set_current_state(TASK_RUNNING);
1699 if (!__io_sq_thread_acquire_mm(ctx)) {
1700 mutex_lock(&ctx->uring_lock);
1701 __io_queue_sqe(req, NULL, NULL);
1702 mutex_unlock(&ctx->uring_lock);
1703 } else {
1704 __io_req_task_cancel(req, -EFAULT);
1705 }
1706}
1707
1708static void io_req_task_submit(struct callback_head *cb)
1709{
1710 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
1711
1712 __io_req_task_submit(req);
1713}
1714
1715static void io_req_task_queue(struct io_kiocb *req)
1716{
1717 struct task_struct *tsk = req->task;
1718 int ret;
1719
1720 init_task_work(&req->task_work, io_req_task_submit);
1721
1722 ret = task_work_add(tsk, &req->task_work, true);
1723 if (unlikely(ret)) {
1724 init_task_work(&req->task_work, io_req_task_cancel);
1725 tsk = io_wq_get_task(req->ctx->io_wq);
1726 task_work_add(tsk, &req->task_work, true);
1727 }
1728 wake_up_process(tsk);
1729}
1730
Pavel Begunkovc3524382020-06-28 12:52:32 +03001731static void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08001732{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001733 struct io_kiocb *nxt = NULL;
1734
1735 io_req_find_next(req, &nxt);
Pavel Begunkov906a8c32020-06-27 14:04:55 +03001736 if (nxt)
1737 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001738}
1739
Pavel Begunkovc3524382020-06-28 12:52:32 +03001740static void io_free_req(struct io_kiocb *req)
1741{
1742 io_queue_next(req);
1743 __io_free_req(req);
1744}
1745
Jens Axboeba816ad2019-09-28 11:36:45 -06001746/*
1747 * Drop reference to request, return next in chain (if there is one) if this
1748 * was the last reference to this request.
1749 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001750__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001751static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001752{
Jens Axboe2a44f462020-02-25 13:25:41 -07001753 if (refcount_dec_and_test(&req->refs)) {
1754 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001755 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001756 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001757}
1758
Jens Axboe2b188cc2019-01-07 10:46:33 -07001759static void io_put_req(struct io_kiocb *req)
1760{
Jens Axboedef596e2019-01-09 08:59:42 -07001761 if (refcount_dec_and_test(&req->refs))
1762 io_free_req(req);
1763}
1764
Pavel Begunkovf4db7182020-06-25 18:20:54 +03001765static struct io_wq_work *io_steal_work(struct io_kiocb *req)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001766{
Pavel Begunkov1bcb8c5d2020-06-27 14:04:56 +03001767 struct io_kiocb *nxt = NULL;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001768
Pavel Begunkovf4db7182020-06-25 18:20:54 +03001769 /*
1770 * A ref is owned by io-wq in which context we're. So, if that's the
1771 * last one, it's safe to steal next work. False negatives are Ok,
1772 * it just will be re-punted async in io_put_work()
1773 */
1774 if (refcount_read(&req->refs) != 1)
1775 return NULL;
1776
1777 io_req_find_next(req, &nxt);
1778 if (!nxt)
1779 return NULL;
1780
1781 if ((nxt->flags & REQ_F_ISREG) && io_op_defs[nxt->opcode].hash_reg_file)
1782 io_wq_hash_work(&nxt->work, file_inode(nxt->file));
1783
Pavel Begunkov1bcb8c5d2020-06-27 14:04:56 +03001784 io_req_task_queue(nxt);
1785 /*
1786 * If we're going to return actual work, here should be timeout prep:
1787 *
1788 * link = io_prep_linked_timeout(nxt);
1789 * if (link)
1790 * nxt->flags |= REQ_F_QUEUE_TIMEOUT;
1791 */
1792 return NULL;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001793}
1794
Jens Axboe978db572019-11-14 22:39:04 -07001795/*
1796 * Must only be used if we don't need to care about links, usually from
1797 * within the completion handling itself.
1798 */
1799static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001800{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001801 /* drop both submit and complete references */
1802 if (refcount_sub_and_test(2, &req->refs))
1803 __io_free_req(req);
1804}
1805
Jens Axboe978db572019-11-14 22:39:04 -07001806static void io_double_put_req(struct io_kiocb *req)
1807{
1808 /* drop both submit and complete references */
1809 if (refcount_sub_and_test(2, &req->refs))
1810 io_free_req(req);
1811}
1812
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001813static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001814{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001815 struct io_rings *rings = ctx->rings;
1816
Jens Axboead3eb2c2019-12-18 17:12:20 -07001817 if (test_bit(0, &ctx->cq_check_overflow)) {
1818 /*
1819 * noflush == true is from the waitqueue handler, just ensure
1820 * we wake up the task, and the next invocation will flush the
1821 * entries. We cannot safely to it from here.
1822 */
1823 if (noflush && !list_empty(&ctx->cq_overflow_list))
1824 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001825
Jens Axboead3eb2c2019-12-18 17:12:20 -07001826 io_cqring_overflow_flush(ctx, false);
1827 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001828
Jens Axboea3a0e432019-08-20 11:03:11 -06001829 /* See comment at the top of this file */
1830 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001831 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001832}
1833
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001834static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1835{
1836 struct io_rings *rings = ctx->rings;
1837
1838 /* make sure SQ entry isn't read before tail */
1839 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1840}
1841
Pavel Begunkovc3524382020-06-28 12:52:32 +03001842static inline void io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001843{
Pavel Begunkovc3524382020-06-28 12:52:32 +03001844 if (unlikely(io_is_fallback_req(req))) {
1845 io_free_req(req);
1846 return;
1847 }
1848 if (req->flags & REQ_F_LINK_HEAD)
1849 io_queue_next(req);
Jens Axboee94f1412019-12-19 12:06:02 -07001850
Pavel Begunkov2757a232020-06-28 12:52:31 +03001851 io_dismantle_req(req);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001852 rb->reqs[rb->to_free++] = req;
1853 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1854 io_free_req_many(req->ctx, rb);
Jens Axboee94f1412019-12-19 12:06:02 -07001855}
1856
Jens Axboebcda7ba2020-02-23 16:42:51 -07001857static int io_put_kbuf(struct io_kiocb *req)
1858{
Jens Axboe4d954c22020-02-27 07:31:19 -07001859 struct io_buffer *kbuf;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001860 int cflags;
1861
Jens Axboe4d954c22020-02-27 07:31:19 -07001862 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001863 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
1864 cflags |= IORING_CQE_F_BUFFER;
1865 req->rw.addr = 0;
1866 kfree(kbuf);
1867 return cflags;
1868}
1869
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001870static void io_iopoll_queue(struct list_head *again)
1871{
1872 struct io_kiocb *req;
1873
1874 do {
1875 req = list_first_entry(again, struct io_kiocb, list);
1876 list_del(&req->list);
Pavel Begunkovd60b5fb2020-06-25 12:37:11 +03001877
1878 /* shouldn't happen unless io_uring is dying, cancel reqs */
1879 if (unlikely(!current->mm)) {
Jens Axboe2237d762020-06-26 13:44:16 -06001880 io_complete_rw_common(&req->rw.kiocb, -EAGAIN, NULL);
Pavel Begunkovd60b5fb2020-06-25 12:37:11 +03001881 continue;
1882 }
1883
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001884 refcount_inc(&req->refs);
1885 io_queue_async_work(req);
1886 } while (!list_empty(again));
1887}
1888
Jens Axboedef596e2019-01-09 08:59:42 -07001889/*
1890 * Find and free completed poll iocbs
1891 */
1892static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1893 struct list_head *done)
1894{
Jens Axboe8237e042019-12-28 10:48:22 -07001895 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001896 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001897 LIST_HEAD(again);
1898
1899 /* order with ->result store in io_complete_rw_iopoll() */
1900 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07001901
Pavel Begunkov2757a232020-06-28 12:52:31 +03001902 rb.to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001903 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07001904 int cflags = 0;
1905
Jens Axboedef596e2019-01-09 08:59:42 -07001906 req = list_first_entry(done, struct io_kiocb, list);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001907 if (READ_ONCE(req->result) == -EAGAIN) {
1908 req->iopoll_completed = 0;
1909 list_move_tail(&req->list, &again);
1910 continue;
1911 }
Jens Axboedef596e2019-01-09 08:59:42 -07001912 list_del(&req->list);
1913
Jens Axboebcda7ba2020-02-23 16:42:51 -07001914 if (req->flags & REQ_F_BUFFER_SELECTED)
1915 cflags = io_put_kbuf(req);
1916
1917 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07001918 (*nr_events)++;
1919
Pavel Begunkovc3524382020-06-28 12:52:32 +03001920 if (refcount_dec_and_test(&req->refs))
1921 io_req_multi_free(&rb, req);
Jens Axboedef596e2019-01-09 08:59:42 -07001922 }
Jens Axboedef596e2019-01-09 08:59:42 -07001923
Jens Axboe09bb8392019-03-13 12:39:28 -06001924 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08001925 if (ctx->flags & IORING_SETUP_SQPOLL)
1926 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001927 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001928
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001929 if (!list_empty(&again))
1930 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001931}
1932
Jens Axboedef596e2019-01-09 08:59:42 -07001933static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1934 long min)
1935{
1936 struct io_kiocb *req, *tmp;
1937 LIST_HEAD(done);
1938 bool spin;
1939 int ret;
1940
1941 /*
1942 * Only spin for completions if we don't have multiple devices hanging
1943 * off our complete list, and we're under the requested amount.
1944 */
1945 spin = !ctx->poll_multi_file && *nr_events < min;
1946
1947 ret = 0;
1948 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001949 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001950
1951 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001952 * Move completed and retryable entries to our local lists.
1953 * If we find a request that requires polling, break out
1954 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07001955 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08001956 if (READ_ONCE(req->iopoll_completed)) {
Jens Axboedef596e2019-01-09 08:59:42 -07001957 list_move_tail(&req->list, &done);
1958 continue;
1959 }
1960 if (!list_empty(&done))
1961 break;
1962
1963 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1964 if (ret < 0)
1965 break;
1966
1967 if (ret && spin)
1968 spin = false;
1969 ret = 0;
1970 }
1971
1972 if (!list_empty(&done))
1973 io_iopoll_complete(ctx, nr_events, &done);
1974
1975 return ret;
1976}
1977
1978/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001979 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001980 * non-spinning poll check - we'll still enter the driver poll loop, but only
1981 * as a non-spinning completion check.
1982 */
1983static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1984 long min)
1985{
Jens Axboe08f54392019-08-21 22:19:11 -06001986 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001987 int ret;
1988
1989 ret = io_do_iopoll(ctx, nr_events, min);
1990 if (ret < 0)
1991 return ret;
1992 if (!min || *nr_events >= min)
1993 return 0;
1994 }
1995
1996 return 1;
1997}
1998
1999/*
2000 * We can't just wait for polled events to come to us, we have to actively
2001 * find and complete them.
2002 */
2003static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
2004{
2005 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2006 return;
2007
2008 mutex_lock(&ctx->uring_lock);
2009 while (!list_empty(&ctx->poll_list)) {
2010 unsigned int nr_events = 0;
2011
2012 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06002013
2014 /*
2015 * Ensure we allow local-to-the-cpu processing to take place,
2016 * in this case we need to ensure that we reap all events.
2017 */
2018 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07002019 }
2020 mutex_unlock(&ctx->uring_lock);
2021}
2022
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002023static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
2024 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002025{
Jens Axboe2b2ed972019-10-25 10:06:15 -06002026 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002027
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002028 /*
2029 * We disallow the app entering submit/complete with polling, but we
2030 * still need to lock the ring to prevent racing with polled issue
2031 * that got punted to a workqueue.
2032 */
2033 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002034 do {
2035 int tmin = 0;
2036
Jens Axboe500f9fb2019-08-19 12:15:59 -06002037 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002038 * Don't enter poll loop if we already have events pending.
2039 * If we do, we can potentially be spinning for commands that
2040 * already triggered a CQE (eg in error).
2041 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002042 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06002043 break;
2044
2045 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002046 * If a submit got punted to a workqueue, we can have the
2047 * application entering polling for a command before it gets
2048 * issued. That app will hold the uring_lock for the duration
2049 * of the poll right here, so we need to take a breather every
2050 * now and then to ensure that the issue has a chance to add
2051 * the poll to the issued list. Otherwise we can spin here
2052 * forever, while the workqueue is stuck trying to acquire the
2053 * very same mutex.
2054 */
2055 if (!(++iters & 7)) {
2056 mutex_unlock(&ctx->uring_lock);
2057 mutex_lock(&ctx->uring_lock);
2058 }
2059
Jens Axboedef596e2019-01-09 08:59:42 -07002060 if (*nr_events < min)
2061 tmin = min - *nr_events;
2062
2063 ret = io_iopoll_getevents(ctx, nr_events, tmin);
2064 if (ret <= 0)
2065 break;
2066 ret = 0;
2067 } while (min && !*nr_events && !need_resched());
2068
Jens Axboe500f9fb2019-08-19 12:15:59 -06002069 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002070 return ret;
2071}
2072
Jens Axboe491381ce2019-10-17 09:20:46 -06002073static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002074{
Jens Axboe491381ce2019-10-17 09:20:46 -06002075 /*
2076 * Tell lockdep we inherited freeze protection from submission
2077 * thread.
2078 */
2079 if (req->flags & REQ_F_ISREG) {
2080 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002081
Jens Axboe491381ce2019-10-17 09:20:46 -06002082 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002083 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002084 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002085}
2086
Jens Axboea1d7c392020-06-22 11:09:46 -06002087static void io_complete_rw_common(struct kiocb *kiocb, long res,
2088 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002089{
Jens Axboe9adbd452019-12-20 08:45:55 -07002090 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002091 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002092
Jens Axboe491381ce2019-10-17 09:20:46 -06002093 if (kiocb->ki_flags & IOCB_WRITE)
2094 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002095
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002096 if (res != req->result)
2097 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002098 if (req->flags & REQ_F_BUFFER_SELECTED)
2099 cflags = io_put_kbuf(req);
Jens Axboea1d7c392020-06-22 11:09:46 -06002100 __io_req_complete(req, res, cflags, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002101}
2102
Jens Axboeb63534c2020-06-04 11:28:00 -06002103#ifdef CONFIG_BLOCK
2104static bool io_resubmit_prep(struct io_kiocb *req, int error)
2105{
2106 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2107 ssize_t ret = -ECANCELED;
2108 struct iov_iter iter;
2109 int rw;
2110
2111 if (error) {
2112 ret = error;
2113 goto end_req;
2114 }
2115
2116 switch (req->opcode) {
2117 case IORING_OP_READV:
2118 case IORING_OP_READ_FIXED:
2119 case IORING_OP_READ:
2120 rw = READ;
2121 break;
2122 case IORING_OP_WRITEV:
2123 case IORING_OP_WRITE_FIXED:
2124 case IORING_OP_WRITE:
2125 rw = WRITE;
2126 break;
2127 default:
2128 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2129 req->opcode);
2130 goto end_req;
2131 }
2132
2133 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2134 if (ret < 0)
2135 goto end_req;
2136 ret = io_setup_async_rw(req, ret, iovec, inline_vecs, &iter);
2137 if (!ret)
2138 return true;
2139 kfree(iovec);
2140end_req:
Jens Axboeb63534c2020-06-04 11:28:00 -06002141 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06002142 io_req_complete(req, ret);
Jens Axboeb63534c2020-06-04 11:28:00 -06002143 return false;
2144}
2145
2146static void io_rw_resubmit(struct callback_head *cb)
2147{
2148 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2149 struct io_ring_ctx *ctx = req->ctx;
2150 int err;
2151
2152 __set_current_state(TASK_RUNNING);
2153
2154 err = io_sq_thread_acquire_mm(ctx, req);
2155
2156 if (io_resubmit_prep(req, err)) {
2157 refcount_inc(&req->refs);
2158 io_queue_async_work(req);
2159 }
2160}
2161#endif
2162
2163static bool io_rw_reissue(struct io_kiocb *req, long res)
2164{
2165#ifdef CONFIG_BLOCK
2166 struct task_struct *tsk;
2167 int ret;
2168
2169 if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker())
2170 return false;
2171
2172 tsk = req->task;
2173 init_task_work(&req->task_work, io_rw_resubmit);
2174 ret = task_work_add(tsk, &req->task_work, true);
2175 if (!ret)
2176 return true;
2177#endif
2178 return false;
2179}
2180
Jens Axboea1d7c392020-06-22 11:09:46 -06002181static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2182 struct io_comp_state *cs)
2183{
2184 if (!io_rw_reissue(req, res))
2185 io_complete_rw_common(&req->rw.kiocb, res, cs);
2186}
2187
Jens Axboeba816ad2019-09-28 11:36:45 -06002188static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2189{
Jens Axboe9adbd452019-12-20 08:45:55 -07002190 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002191
Jens Axboea1d7c392020-06-22 11:09:46 -06002192 __io_complete_rw(req, res, res2, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002193}
2194
Jens Axboedef596e2019-01-09 08:59:42 -07002195static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2196{
Jens Axboe9adbd452019-12-20 08:45:55 -07002197 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002198
Jens Axboe491381ce2019-10-17 09:20:46 -06002199 if (kiocb->ki_flags & IOCB_WRITE)
2200 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002201
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002202 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002203 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002204
2205 WRITE_ONCE(req->result, res);
2206 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002207 smp_wmb();
2208 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002209}
2210
2211/*
2212 * After the iocb has been issued, it's safe to be found on the poll list.
2213 * Adding the kiocb to the list AFTER submission ensures that we don't
2214 * find it from a io_iopoll_getevents() thread before the issuer is done
2215 * accessing the kiocb cookie.
2216 */
2217static void io_iopoll_req_issued(struct io_kiocb *req)
2218{
2219 struct io_ring_ctx *ctx = req->ctx;
2220
2221 /*
2222 * Track whether we have multiple files in our lists. This will impact
2223 * how we do polling eventually, not spinning if we're on potentially
2224 * different devices.
2225 */
2226 if (list_empty(&ctx->poll_list)) {
2227 ctx->poll_multi_file = false;
2228 } else if (!ctx->poll_multi_file) {
2229 struct io_kiocb *list_req;
2230
2231 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
2232 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07002233 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002234 ctx->poll_multi_file = true;
2235 }
2236
2237 /*
2238 * For fast devices, IO may have already completed. If it has, add
2239 * it to the front so we find it first.
2240 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002241 if (READ_ONCE(req->iopoll_completed))
Jens Axboedef596e2019-01-09 08:59:42 -07002242 list_add(&req->list, &ctx->poll_list);
2243 else
2244 list_add_tail(&req->list, &ctx->poll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002245
2246 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2247 wq_has_sleeper(&ctx->sqo_wait))
2248 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002249}
2250
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002251static void __io_state_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07002252{
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002253 int diff = state->has_refs - state->used_refs;
Jens Axboe9a56a232019-01-09 09:06:50 -07002254
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002255 if (diff)
2256 fput_many(state->file, diff);
2257 state->file = NULL;
2258}
2259
2260static inline void io_state_file_put(struct io_submit_state *state)
2261{
2262 if (state->file)
2263 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002264}
2265
2266/*
2267 * Get as many references to a file as we have IOs left in this submission,
2268 * assuming most submissions are for one file, or at least that each file
2269 * has more than one submission.
2270 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002271static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002272{
2273 if (!state)
2274 return fget(fd);
2275
2276 if (state->file) {
2277 if (state->fd == fd) {
2278 state->used_refs++;
2279 state->ios_left--;
2280 return state->file;
2281 }
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002282 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002283 }
2284 state->file = fget_many(fd, state->ios_left);
2285 if (!state->file)
2286 return NULL;
2287
2288 state->fd = fd;
2289 state->has_refs = state->ios_left;
2290 state->used_refs = 1;
2291 state->ios_left--;
2292 return state->file;
2293}
2294
Jens Axboe4503b762020-06-01 10:00:27 -06002295static bool io_bdev_nowait(struct block_device *bdev)
2296{
2297#ifdef CONFIG_BLOCK
2298 return !bdev || queue_is_mq(bdev_get_queue(bdev));
2299#else
2300 return true;
2301#endif
2302}
2303
Jens Axboe2b188cc2019-01-07 10:46:33 -07002304/*
2305 * If we tracked the file through the SCM inflight mechanism, we could support
2306 * any file. For now, just ensure that anything potentially problematic is done
2307 * inline.
2308 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002309static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002310{
2311 umode_t mode = file_inode(file)->i_mode;
2312
Jens Axboe4503b762020-06-01 10:00:27 -06002313 if (S_ISBLK(mode)) {
2314 if (io_bdev_nowait(file->f_inode->i_bdev))
2315 return true;
2316 return false;
2317 }
2318 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002319 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002320 if (S_ISREG(mode)) {
2321 if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
2322 file->f_op != &io_uring_fops)
2323 return true;
2324 return false;
2325 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002326
Jens Axboec5b85622020-06-09 19:23:05 -06002327 /* any ->read/write should understand O_NONBLOCK */
2328 if (file->f_flags & O_NONBLOCK)
2329 return true;
2330
Jens Axboeaf197f52020-04-28 13:15:06 -06002331 if (!(file->f_mode & FMODE_NOWAIT))
2332 return false;
2333
2334 if (rw == READ)
2335 return file->f_op->read_iter != NULL;
2336
2337 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002338}
2339
Jens Axboe3529d8c2019-12-19 18:24:38 -07002340static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2341 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002342{
Jens Axboedef596e2019-01-09 08:59:42 -07002343 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002344 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002345 unsigned ioprio;
2346 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002347
Jens Axboe491381ce2019-10-17 09:20:46 -06002348 if (S_ISREG(file_inode(req->file)->i_mode))
2349 req->flags |= REQ_F_ISREG;
2350
Jens Axboe2b188cc2019-01-07 10:46:33 -07002351 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002352 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2353 req->flags |= REQ_F_CUR_POS;
2354 kiocb->ki_pos = req->file->f_pos;
2355 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002356 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002357 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2358 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2359 if (unlikely(ret))
2360 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002361
2362 ioprio = READ_ONCE(sqe->ioprio);
2363 if (ioprio) {
2364 ret = ioprio_check_cap(ioprio);
2365 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002366 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002367
2368 kiocb->ki_ioprio = ioprio;
2369 } else
2370 kiocb->ki_ioprio = get_current_ioprio();
2371
Stefan Bühler8449eed2019-04-27 20:34:19 +02002372 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboec5b85622020-06-09 19:23:05 -06002373 if (kiocb->ki_flags & IOCB_NOWAIT)
Stefan Bühler8449eed2019-04-27 20:34:19 +02002374 req->flags |= REQ_F_NOWAIT;
2375
Jens Axboeb63534c2020-06-04 11:28:00 -06002376 if (kiocb->ki_flags & IOCB_DIRECT)
2377 io_get_req_task(req);
2378
Stefan Bühler8449eed2019-04-27 20:34:19 +02002379 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002380 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02002381
Jens Axboedef596e2019-01-09 08:59:42 -07002382 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002383 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2384 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002385 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002386
Jens Axboedef596e2019-01-09 08:59:42 -07002387 kiocb->ki_flags |= IOCB_HIPRI;
2388 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06002389 req->result = 0;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002390 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002391 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002392 if (kiocb->ki_flags & IOCB_HIPRI)
2393 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002394 kiocb->ki_complete = io_complete_rw;
2395 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002396
Jens Axboe3529d8c2019-12-19 18:24:38 -07002397 req->rw.addr = READ_ONCE(sqe->addr);
2398 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002399 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002400 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002401}
2402
2403static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2404{
2405 switch (ret) {
2406 case -EIOCBQUEUED:
2407 break;
2408 case -ERESTARTSYS:
2409 case -ERESTARTNOINTR:
2410 case -ERESTARTNOHAND:
2411 case -ERESTART_RESTARTBLOCK:
2412 /*
2413 * We can't just restart the syscall, since previously
2414 * submitted sqes may already be in progress. Just fail this
2415 * IO with EINTR.
2416 */
2417 ret = -EINTR;
2418 /* fall through */
2419 default:
2420 kiocb->ki_complete(kiocb, ret, 0);
2421 }
2422}
2423
Jens Axboea1d7c392020-06-22 11:09:46 -06002424static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
2425 struct io_comp_state *cs)
Jens Axboeba816ad2019-09-28 11:36:45 -06002426{
Jens Axboeba042912019-12-25 16:33:42 -07002427 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2428
2429 if (req->flags & REQ_F_CUR_POS)
2430 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002431 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboea1d7c392020-06-22 11:09:46 -06002432 __io_complete_rw(req, ret, 0, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002433 else
2434 io_rw_done(kiocb, ret);
2435}
2436
Jens Axboe9adbd452019-12-20 08:45:55 -07002437static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002438 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002439{
Jens Axboe9adbd452019-12-20 08:45:55 -07002440 struct io_ring_ctx *ctx = req->ctx;
2441 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002442 struct io_mapped_ubuf *imu;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002443 u16 index, buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002444 size_t offset;
2445 u64 buf_addr;
2446
2447 /* attempt to use fixed buffers without having provided iovecs */
2448 if (unlikely(!ctx->user_bufs))
2449 return -EFAULT;
2450
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002451 buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002452 if (unlikely(buf_index >= ctx->nr_user_bufs))
2453 return -EFAULT;
2454
2455 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2456 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002457 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002458
2459 /* overflow */
2460 if (buf_addr + len < buf_addr)
2461 return -EFAULT;
2462 /* not inside the mapped region */
2463 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2464 return -EFAULT;
2465
2466 /*
2467 * May not be a start of buffer, set size appropriately
2468 * and advance us to the beginning.
2469 */
2470 offset = buf_addr - imu->ubuf;
2471 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002472
2473 if (offset) {
2474 /*
2475 * Don't use iov_iter_advance() here, as it's really slow for
2476 * using the latter parts of a big fixed buffer - it iterates
2477 * over each segment manually. We can cheat a bit here, because
2478 * we know that:
2479 *
2480 * 1) it's a BVEC iter, we set it up
2481 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2482 * first and last bvec
2483 *
2484 * So just find our index, and adjust the iterator afterwards.
2485 * If the offset is within the first bvec (or the whole first
2486 * bvec, just use iov_iter_advance(). This makes it easier
2487 * since we can just skip the first segment, which may not
2488 * be PAGE_SIZE aligned.
2489 */
2490 const struct bio_vec *bvec = imu->bvec;
2491
2492 if (offset <= bvec->bv_len) {
2493 iov_iter_advance(iter, offset);
2494 } else {
2495 unsigned long seg_skip;
2496
2497 /* skip first vec */
2498 offset -= bvec->bv_len;
2499 seg_skip = 1 + (offset >> PAGE_SHIFT);
2500
2501 iter->bvec = bvec + seg_skip;
2502 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002503 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002504 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002505 }
2506 }
2507
Jens Axboe5e559562019-11-13 16:12:46 -07002508 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002509}
2510
Jens Axboebcda7ba2020-02-23 16:42:51 -07002511static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2512{
2513 if (needs_lock)
2514 mutex_unlock(&ctx->uring_lock);
2515}
2516
2517static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2518{
2519 /*
2520 * "Normal" inline submissions always hold the uring_lock, since we
2521 * grab it from the system call. Same is true for the SQPOLL offload.
2522 * The only exception is when we've detached the request and issue it
2523 * from an async worker thread, grab the lock for that case.
2524 */
2525 if (needs_lock)
2526 mutex_lock(&ctx->uring_lock);
2527}
2528
2529static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2530 int bgid, struct io_buffer *kbuf,
2531 bool needs_lock)
2532{
2533 struct io_buffer *head;
2534
2535 if (req->flags & REQ_F_BUFFER_SELECTED)
2536 return kbuf;
2537
2538 io_ring_submit_lock(req->ctx, needs_lock);
2539
2540 lockdep_assert_held(&req->ctx->uring_lock);
2541
2542 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2543 if (head) {
2544 if (!list_empty(&head->list)) {
2545 kbuf = list_last_entry(&head->list, struct io_buffer,
2546 list);
2547 list_del(&kbuf->list);
2548 } else {
2549 kbuf = head;
2550 idr_remove(&req->ctx->io_buffer_idr, bgid);
2551 }
2552 if (*len > kbuf->len)
2553 *len = kbuf->len;
2554 } else {
2555 kbuf = ERR_PTR(-ENOBUFS);
2556 }
2557
2558 io_ring_submit_unlock(req->ctx, needs_lock);
2559
2560 return kbuf;
2561}
2562
Jens Axboe4d954c22020-02-27 07:31:19 -07002563static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2564 bool needs_lock)
2565{
2566 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002567 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07002568
2569 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002570 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07002571 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2572 if (IS_ERR(kbuf))
2573 return kbuf;
2574 req->rw.addr = (u64) (unsigned long) kbuf;
2575 req->flags |= REQ_F_BUFFER_SELECTED;
2576 return u64_to_user_ptr(kbuf->addr);
2577}
2578
2579#ifdef CONFIG_COMPAT
2580static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2581 bool needs_lock)
2582{
2583 struct compat_iovec __user *uiov;
2584 compat_ssize_t clen;
2585 void __user *buf;
2586 ssize_t len;
2587
2588 uiov = u64_to_user_ptr(req->rw.addr);
2589 if (!access_ok(uiov, sizeof(*uiov)))
2590 return -EFAULT;
2591 if (__get_user(clen, &uiov->iov_len))
2592 return -EFAULT;
2593 if (clen < 0)
2594 return -EINVAL;
2595
2596 len = clen;
2597 buf = io_rw_buffer_select(req, &len, needs_lock);
2598 if (IS_ERR(buf))
2599 return PTR_ERR(buf);
2600 iov[0].iov_base = buf;
2601 iov[0].iov_len = (compat_size_t) len;
2602 return 0;
2603}
2604#endif
2605
2606static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2607 bool needs_lock)
2608{
2609 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2610 void __user *buf;
2611 ssize_t len;
2612
2613 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2614 return -EFAULT;
2615
2616 len = iov[0].iov_len;
2617 if (len < 0)
2618 return -EINVAL;
2619 buf = io_rw_buffer_select(req, &len, needs_lock);
2620 if (IS_ERR(buf))
2621 return PTR_ERR(buf);
2622 iov[0].iov_base = buf;
2623 iov[0].iov_len = len;
2624 return 0;
2625}
2626
2627static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2628 bool needs_lock)
2629{
Jens Axboedddb3e22020-06-04 11:27:01 -06002630 if (req->flags & REQ_F_BUFFER_SELECTED) {
2631 struct io_buffer *kbuf;
2632
2633 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2634 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
2635 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002636 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06002637 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002638 if (!req->rw.len)
2639 return 0;
2640 else if (req->rw.len > 1)
2641 return -EINVAL;
2642
2643#ifdef CONFIG_COMPAT
2644 if (req->ctx->compat)
2645 return io_compat_import(req, iov, needs_lock);
2646#endif
2647
2648 return __io_iov_buffer_select(req, iov, needs_lock);
2649}
2650
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002651static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
Jens Axboebcda7ba2020-02-23 16:42:51 -07002652 struct iovec **iovec, struct iov_iter *iter,
2653 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002654{
Jens Axboe9adbd452019-12-20 08:45:55 -07002655 void __user *buf = u64_to_user_ptr(req->rw.addr);
2656 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002657 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002658 u8 opcode;
2659
Jens Axboed625c6e2019-12-17 19:53:05 -07002660 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002661 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002662 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002663 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002664 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002665
Jens Axboebcda7ba2020-02-23 16:42:51 -07002666 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002667 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002668 return -EINVAL;
2669
Jens Axboe3a6820f2019-12-22 15:19:35 -07002670 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002671 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002672 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
2673 if (IS_ERR(buf)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002674 *iovec = NULL;
Jens Axboe4d954c22020-02-27 07:31:19 -07002675 return PTR_ERR(buf);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002676 }
Jens Axboe3f9d6442020-03-11 12:27:04 -06002677 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002678 }
2679
Jens Axboe3a6820f2019-12-22 15:19:35 -07002680 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2681 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002682 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002683 }
2684
Jens Axboef67676d2019-12-02 11:03:47 -07002685 if (req->io) {
2686 struct io_async_rw *iorw = &req->io->rw;
2687
2688 *iovec = iorw->iov;
2689 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2690 if (iorw->iov == iorw->fast_iov)
2691 *iovec = NULL;
2692 return iorw->size;
2693 }
2694
Jens Axboe4d954c22020-02-27 07:31:19 -07002695 if (req->flags & REQ_F_BUFFER_SELECT) {
2696 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002697 if (!ret) {
2698 ret = (*iovec)->iov_len;
2699 iov_iter_init(iter, rw, *iovec, 1, ret);
2700 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002701 *iovec = NULL;
2702 return ret;
2703 }
2704
Jens Axboe2b188cc2019-01-07 10:46:33 -07002705#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002706 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002707 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2708 iovec, iter);
2709#endif
2710
2711 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2712}
2713
Jens Axboe32960612019-09-23 11:05:34 -06002714/*
2715 * For files that don't have ->read_iter() and ->write_iter(), handle them
2716 * by looping over ->read() or ->write() manually.
2717 */
2718static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2719 struct iov_iter *iter)
2720{
2721 ssize_t ret = 0;
2722
2723 /*
2724 * Don't support polled IO through this interface, and we can't
2725 * support non-blocking either. For the latter, this just causes
2726 * the kiocb to be handled from an async context.
2727 */
2728 if (kiocb->ki_flags & IOCB_HIPRI)
2729 return -EOPNOTSUPP;
2730 if (kiocb->ki_flags & IOCB_NOWAIT)
2731 return -EAGAIN;
2732
2733 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002734 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002735 ssize_t nr;
2736
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002737 if (!iov_iter_is_bvec(iter)) {
2738 iovec = iov_iter_iovec(iter);
2739 } else {
2740 /* fixed buffers import bvec */
2741 iovec.iov_base = kmap(iter->bvec->bv_page)
2742 + iter->iov_offset;
2743 iovec.iov_len = min(iter->count,
2744 iter->bvec->bv_len - iter->iov_offset);
2745 }
2746
Jens Axboe32960612019-09-23 11:05:34 -06002747 if (rw == READ) {
2748 nr = file->f_op->read(file, iovec.iov_base,
2749 iovec.iov_len, &kiocb->ki_pos);
2750 } else {
2751 nr = file->f_op->write(file, iovec.iov_base,
2752 iovec.iov_len, &kiocb->ki_pos);
2753 }
2754
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002755 if (iov_iter_is_bvec(iter))
2756 kunmap(iter->bvec->bv_page);
2757
Jens Axboe32960612019-09-23 11:05:34 -06002758 if (nr < 0) {
2759 if (!ret)
2760 ret = nr;
2761 break;
2762 }
2763 ret += nr;
2764 if (nr != iovec.iov_len)
2765 break;
2766 iov_iter_advance(iter, nr);
2767 }
2768
2769 return ret;
2770}
2771
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002772static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002773 struct iovec *iovec, struct iovec *fast_iov,
2774 struct iov_iter *iter)
2775{
2776 req->io->rw.nr_segs = iter->nr_segs;
2777 req->io->rw.size = io_size;
2778 req->io->rw.iov = iovec;
2779 if (!req->io->rw.iov) {
2780 req->io->rw.iov = req->io->rw.fast_iov;
Xiaoguang Wang45097da2020-04-08 22:29:58 +08002781 if (req->io->rw.iov != fast_iov)
2782 memcpy(req->io->rw.iov, fast_iov,
2783 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002784 } else {
2785 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002786 }
2787}
2788
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002789static inline int __io_alloc_async_ctx(struct io_kiocb *req)
2790{
2791 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
2792 return req->io == NULL;
2793}
2794
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002795static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002796{
Jens Axboed3656342019-12-18 09:50:26 -07002797 if (!io_op_defs[req->opcode].async_ctx)
2798 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002799
2800 return __io_alloc_async_ctx(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002801}
2802
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002803static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2804 struct iovec *iovec, struct iovec *fast_iov,
2805 struct iov_iter *iter)
2806{
Jens Axboe980ad262020-01-24 23:08:54 -07002807 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002808 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002809 if (!req->io) {
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002810 if (__io_alloc_async_ctx(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07002811 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002812
Jens Axboe5d204bc2020-01-31 12:06:52 -07002813 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2814 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002815 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002816}
2817
Jens Axboe3529d8c2019-12-19 18:24:38 -07002818static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2819 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002820{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002821 struct io_async_ctx *io;
2822 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002823 ssize_t ret;
2824
Jens Axboe3529d8c2019-12-19 18:24:38 -07002825 ret = io_prep_rw(req, sqe, force_nonblock);
2826 if (ret)
2827 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002828
Jens Axboe3529d8c2019-12-19 18:24:38 -07002829 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2830 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002831
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002832 /* either don't need iovec imported or already have it */
2833 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002834 return 0;
2835
2836 io = req->io;
2837 io->rw.iov = io->rw.fast_iov;
2838 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002839 ret = io_import_iovec(READ, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002840 req->io = io;
2841 if (ret < 0)
2842 return ret;
2843
2844 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2845 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002846}
2847
Jens Axboebcf5a062020-05-22 09:24:42 -06002848static void io_async_buf_cancel(struct callback_head *cb)
2849{
2850 struct io_async_rw *rw;
2851 struct io_kiocb *req;
2852
2853 rw = container_of(cb, struct io_async_rw, task_work);
2854 req = rw->wpq.wait.private;
Jens Axboec40f6372020-06-25 15:39:59 -06002855 __io_req_task_cancel(req, -ECANCELED);
Jens Axboebcf5a062020-05-22 09:24:42 -06002856}
2857
2858static void io_async_buf_retry(struct callback_head *cb)
2859{
2860 struct io_async_rw *rw;
Jens Axboebcf5a062020-05-22 09:24:42 -06002861 struct io_kiocb *req;
2862
2863 rw = container_of(cb, struct io_async_rw, task_work);
2864 req = rw->wpq.wait.private;
Jens Axboebcf5a062020-05-22 09:24:42 -06002865
Jens Axboec40f6372020-06-25 15:39:59 -06002866 __io_req_task_submit(req);
Jens Axboebcf5a062020-05-22 09:24:42 -06002867}
2868
2869static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
2870 int sync, void *arg)
2871{
2872 struct wait_page_queue *wpq;
2873 struct io_kiocb *req = wait->private;
2874 struct io_async_rw *rw = &req->io->rw;
2875 struct wait_page_key *key = arg;
2876 struct task_struct *tsk;
2877 int ret;
2878
2879 wpq = container_of(wait, struct wait_page_queue, wait);
2880
2881 ret = wake_page_match(wpq, key);
2882 if (ret != 1)
2883 return ret;
2884
2885 list_del_init(&wait->entry);
2886
2887 init_task_work(&rw->task_work, io_async_buf_retry);
2888 /* submit ref gets dropped, acquire a new one */
2889 refcount_inc(&req->refs);
2890 tsk = req->task;
2891 ret = task_work_add(tsk, &rw->task_work, true);
2892 if (unlikely(ret)) {
2893 /* queue just for cancelation */
2894 init_task_work(&rw->task_work, io_async_buf_cancel);
2895 tsk = io_wq_get_task(req->ctx->io_wq);
2896 task_work_add(tsk, &rw->task_work, true);
2897 }
2898 wake_up_process(tsk);
2899 return 1;
2900}
2901
2902static bool io_rw_should_retry(struct io_kiocb *req)
2903{
2904 struct kiocb *kiocb = &req->rw.kiocb;
2905 int ret;
2906
2907 /* never retry for NOWAIT, we just complete with -EAGAIN */
2908 if (req->flags & REQ_F_NOWAIT)
2909 return false;
2910
2911 /* already tried, or we're doing O_DIRECT */
2912 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_WAITQ))
2913 return false;
2914 /*
2915 * just use poll if we can, and don't attempt if the fs doesn't
2916 * support callback based unlocks
2917 */
2918 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
2919 return false;
2920
2921 /*
2922 * If request type doesn't require req->io to defer in general,
2923 * we need to allocate it here
2924 */
2925 if (!req->io && __io_alloc_async_ctx(req))
2926 return false;
2927
2928 ret = kiocb_wait_page_queue_init(kiocb, &req->io->rw.wpq,
2929 io_async_buf_func, req);
2930 if (!ret) {
2931 io_get_req_task(req);
2932 return true;
2933 }
2934
2935 return false;
2936}
2937
2938static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
2939{
2940 if (req->file->f_op->read_iter)
2941 return call_read_iter(req->file, &req->rw.kiocb, iter);
2942 return loop_rw_iter(READ, req->file, &req->rw.kiocb, iter);
2943}
2944
Jens Axboea1d7c392020-06-22 11:09:46 -06002945static int io_read(struct io_kiocb *req, bool force_nonblock,
2946 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002947{
2948 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002949 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002950 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002951 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002952 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002953
Jens Axboebcda7ba2020-02-23 16:42:51 -07002954 ret = io_import_iovec(READ, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07002955 if (ret < 0)
2956 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002957
Jens Axboefd6c2e42019-12-18 12:19:41 -07002958 /* Ensure we clear previously set non-block flag */
2959 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07002960 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002961
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002962 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002963 io_size = ret;
Pavel Begunkovdea3b492020-04-12 02:05:04 +03002964 if (req->flags & REQ_F_LINK_HEAD)
Jens Axboef67676d2019-12-02 11:03:47 -07002965 req->result = io_size;
2966
Pavel Begunkov24c74672020-06-21 13:09:51 +03002967 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06002968 if (force_nonblock && !io_file_supports_async(req->file, READ))
Jens Axboef67676d2019-12-02 11:03:47 -07002969 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002970
Jens Axboe31b51512019-01-18 22:56:34 -07002971 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002972 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002973 if (!ret) {
Jens Axboeb63534c2020-06-04 11:28:00 -06002974 unsigned long nr_segs = iter.nr_segs;
Jens Axboe4503b762020-06-01 10:00:27 -06002975 ssize_t ret2 = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002976
Jens Axboebcf5a062020-05-22 09:24:42 -06002977 ret2 = io_iter_do_read(req, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002978
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002979 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe4503b762020-06-01 10:00:27 -06002980 if (!force_nonblock || (ret2 != -EAGAIN && ret2 != -EIO)) {
Jens Axboea1d7c392020-06-22 11:09:46 -06002981 kiocb_done(kiocb, ret2, cs);
Jens Axboef67676d2019-12-02 11:03:47 -07002982 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06002983 iter.count = iov_count;
2984 iter.nr_segs = nr_segs;
Jens Axboef67676d2019-12-02 11:03:47 -07002985copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002986 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002987 inline_vecs, &iter);
2988 if (ret)
2989 goto out_free;
Jens Axboebcf5a062020-05-22 09:24:42 -06002990 /* if we can retry, do so with the callbacks armed */
2991 if (io_rw_should_retry(req)) {
2992 ret2 = io_iter_do_read(req, &iter);
2993 if (ret2 == -EIOCBQUEUED) {
2994 goto out_free;
2995 } else if (ret2 != -EAGAIN) {
Jens Axboea1d7c392020-06-22 11:09:46 -06002996 kiocb_done(kiocb, ret2, cs);
Jens Axboebcf5a062020-05-22 09:24:42 -06002997 goto out_free;
2998 }
2999 }
3000 kiocb->ki_flags &= ~IOCB_WAITQ;
Jens Axboef67676d2019-12-02 11:03:47 -07003001 return -EAGAIN;
3002 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003003 }
Jens Axboef67676d2019-12-02 11:03:47 -07003004out_free:
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003005 if (!(req->flags & REQ_F_NEED_CLEANUP))
3006 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003007 return ret;
3008}
3009
Jens Axboe3529d8c2019-12-19 18:24:38 -07003010static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3011 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07003012{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003013 struct io_async_ctx *io;
3014 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07003015 ssize_t ret;
3016
Jens Axboe3529d8c2019-12-19 18:24:38 -07003017 ret = io_prep_rw(req, sqe, force_nonblock);
3018 if (ret)
3019 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003020
Jens Axboe3529d8c2019-12-19 18:24:38 -07003021 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3022 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003023
Jens Axboe4ed734b2020-03-20 11:23:41 -06003024 req->fsize = rlimit(RLIMIT_FSIZE);
3025
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003026 /* either don't need iovec imported or already have it */
3027 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003028 return 0;
3029
3030 io = req->io;
3031 io->rw.iov = io->rw.fast_iov;
3032 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003033 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003034 req->io = io;
3035 if (ret < 0)
3036 return ret;
3037
3038 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
3039 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003040}
3041
Jens Axboea1d7c392020-06-22 11:09:46 -06003042static int io_write(struct io_kiocb *req, bool force_nonblock,
3043 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003044{
3045 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003046 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003047 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07003048 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07003049 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003050
Jens Axboebcda7ba2020-02-23 16:42:51 -07003051 ret = io_import_iovec(WRITE, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003052 if (ret < 0)
3053 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003054
Jens Axboefd6c2e42019-12-18 12:19:41 -07003055 /* Ensure we clear previously set non-block flag */
3056 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07003057 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003058
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08003059 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003060 io_size = ret;
Pavel Begunkovdea3b492020-04-12 02:05:04 +03003061 if (req->flags & REQ_F_LINK_HEAD)
Jens Axboef67676d2019-12-02 11:03:47 -07003062 req->result = io_size;
3063
Pavel Begunkov24c74672020-06-21 13:09:51 +03003064 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003065 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003066 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003067
Jens Axboe10d59342019-12-09 20:16:22 -07003068 /* file path doesn't support NOWAIT for non-direct_IO */
3069 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3070 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003071 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003072
Jens Axboe31b51512019-01-18 22:56:34 -07003073 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07003074 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003075 if (!ret) {
Jens Axboeb63534c2020-06-04 11:28:00 -06003076 unsigned long nr_segs = iter.nr_segs;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003077 ssize_t ret2;
3078
Jens Axboe2b188cc2019-01-07 10:46:33 -07003079 /*
3080 * Open-code file_start_write here to grab freeze protection,
3081 * which will be released by another thread in
3082 * io_complete_rw(). Fool lockdep by telling it the lock got
3083 * released so that it doesn't complain about the held lock when
3084 * we return to userspace.
3085 */
Jens Axboe491381ce2019-10-17 09:20:46 -06003086 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07003087 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07003088 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07003089 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07003090 SB_FREEZE_WRITE);
3091 }
3092 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003093
Jens Axboe4ed734b2020-03-20 11:23:41 -06003094 if (!force_nonblock)
3095 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
3096
Jens Axboe9adbd452019-12-20 08:45:55 -07003097 if (req->file->f_op->write_iter)
3098 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06003099 else
Jens Axboe9adbd452019-12-20 08:45:55 -07003100 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboe4ed734b2020-03-20 11:23:41 -06003101
3102 if (!force_nonblock)
3103 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
3104
Jens Axboefaac9962020-02-07 15:45:22 -07003105 /*
Chucheng Luobff60352020-03-25 11:31:38 +08003106 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
Jens Axboefaac9962020-02-07 15:45:22 -07003107 * retry them without IOCB_NOWAIT.
3108 */
3109 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3110 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07003111 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboea1d7c392020-06-22 11:09:46 -06003112 kiocb_done(kiocb, ret2, cs);
Jens Axboef67676d2019-12-02 11:03:47 -07003113 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06003114 iter.count = iov_count;
3115 iter.nr_segs = nr_segs;
Jens Axboef67676d2019-12-02 11:03:47 -07003116copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003117 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07003118 inline_vecs, &iter);
3119 if (ret)
3120 goto out_free;
3121 return -EAGAIN;
3122 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003123 }
Jens Axboe31b51512019-01-18 22:56:34 -07003124out_free:
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003125 if (!(req->flags & REQ_F_NEED_CLEANUP))
3126 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003127 return ret;
3128}
3129
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003130static int __io_splice_prep(struct io_kiocb *req,
3131 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003132{
3133 struct io_splice* sp = &req->splice;
3134 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
3135 int ret;
3136
3137 if (req->flags & REQ_F_NEED_CLEANUP)
3138 return 0;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003139 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3140 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003141
3142 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003143 sp->len = READ_ONCE(sqe->len);
3144 sp->flags = READ_ONCE(sqe->splice_flags);
3145
3146 if (unlikely(sp->flags & ~valid_flags))
3147 return -EINVAL;
3148
3149 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
3150 (sp->flags & SPLICE_F_FD_IN_FIXED));
3151 if (ret)
3152 return ret;
3153 req->flags |= REQ_F_NEED_CLEANUP;
3154
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003155 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3156 /*
3157 * Splice operation will be punted aync, and here need to
3158 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3159 */
3160 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003161 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003162 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003163
3164 return 0;
3165}
3166
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003167static int io_tee_prep(struct io_kiocb *req,
3168 const struct io_uring_sqe *sqe)
3169{
3170 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3171 return -EINVAL;
3172 return __io_splice_prep(req, sqe);
3173}
3174
3175static int io_tee(struct io_kiocb *req, bool force_nonblock)
3176{
3177 struct io_splice *sp = &req->splice;
3178 struct file *in = sp->file_in;
3179 struct file *out = sp->file_out;
3180 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3181 long ret = 0;
3182
3183 if (force_nonblock)
3184 return -EAGAIN;
3185 if (sp->len)
3186 ret = do_tee(in, out, sp->len, flags);
3187
3188 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3189 req->flags &= ~REQ_F_NEED_CLEANUP;
3190
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003191 if (ret != sp->len)
3192 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003193 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003194 return 0;
3195}
3196
3197static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3198{
3199 struct io_splice* sp = &req->splice;
3200
3201 sp->off_in = READ_ONCE(sqe->splice_off_in);
3202 sp->off_out = READ_ONCE(sqe->off);
3203 return __io_splice_prep(req, sqe);
3204}
3205
Pavel Begunkov014db002020-03-03 21:33:12 +03003206static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003207{
3208 struct io_splice *sp = &req->splice;
3209 struct file *in = sp->file_in;
3210 struct file *out = sp->file_out;
3211 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3212 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003213 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003214
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003215 if (force_nonblock)
3216 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003217
3218 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3219 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003220
Jens Axboe948a7742020-05-17 14:21:38 -06003221 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003222 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003223
3224 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3225 req->flags &= ~REQ_F_NEED_CLEANUP;
3226
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003227 if (ret != sp->len)
3228 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003229 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003230 return 0;
3231}
3232
Jens Axboe2b188cc2019-01-07 10:46:33 -07003233/*
3234 * IORING_OP_NOP just posts a completion event, nothing else.
3235 */
Jens Axboe229a7b62020-06-22 10:13:11 -06003236static int io_nop(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003237{
3238 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003239
Jens Axboedef596e2019-01-09 08:59:42 -07003240 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3241 return -EINVAL;
3242
Jens Axboe229a7b62020-06-22 10:13:11 -06003243 __io_req_complete(req, 0, 0, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003244 return 0;
3245}
3246
Jens Axboe3529d8c2019-12-19 18:24:38 -07003247static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003248{
Jens Axboe6b063142019-01-10 22:13:58 -07003249 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003250
Jens Axboe09bb8392019-03-13 12:39:28 -06003251 if (!req->file)
3252 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003253
Jens Axboe6b063142019-01-10 22:13:58 -07003254 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003255 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003256 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003257 return -EINVAL;
3258
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003259 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3260 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3261 return -EINVAL;
3262
3263 req->sync.off = READ_ONCE(sqe->off);
3264 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003265 return 0;
3266}
3267
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003268static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Jens Axboe78912932020-01-14 22:09:06 -07003269{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003270 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003271 int ret;
3272
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003273 /* fsync always requires a blocking context */
3274 if (force_nonblock)
3275 return -EAGAIN;
3276
Jens Axboe9adbd452019-12-20 08:45:55 -07003277 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003278 end > 0 ? end : LLONG_MAX,
3279 req->sync.flags & IORING_FSYNC_DATASYNC);
3280 if (ret < 0)
3281 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003282 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003283 return 0;
3284}
3285
Jens Axboed63d1b52019-12-10 10:38:56 -07003286static int io_fallocate_prep(struct io_kiocb *req,
3287 const struct io_uring_sqe *sqe)
3288{
3289 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3290 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003291 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3292 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003293
3294 req->sync.off = READ_ONCE(sqe->off);
3295 req->sync.len = READ_ONCE(sqe->addr);
3296 req->sync.mode = READ_ONCE(sqe->len);
Jens Axboe4ed734b2020-03-20 11:23:41 -06003297 req->fsize = rlimit(RLIMIT_FSIZE);
Jens Axboed63d1b52019-12-10 10:38:56 -07003298 return 0;
3299}
3300
Pavel Begunkov014db002020-03-03 21:33:12 +03003301static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07003302{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003303 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003304
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003305 /* fallocate always requiring blocking context */
3306 if (force_nonblock)
3307 return -EAGAIN;
3308
3309 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
3310 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3311 req->sync.len);
3312 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
3313 if (ret < 0)
3314 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003315 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003316 return 0;
3317}
3318
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003319static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003320{
Jens Axboef8748882020-01-08 17:47:02 -07003321 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003322 int ret;
3323
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003324 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003325 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003326 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003327 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003328 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003329 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003330
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003331 /* open.how should be already initialised */
3332 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003333 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003334
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003335 req->open.dfd = READ_ONCE(sqe->fd);
3336 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003337 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003338 if (IS_ERR(req->open.filename)) {
3339 ret = PTR_ERR(req->open.filename);
3340 req->open.filename = NULL;
3341 return ret;
3342 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003343 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003344 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003345 return 0;
3346}
3347
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003348static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3349{
3350 u64 flags, mode;
3351
3352 if (req->flags & REQ_F_NEED_CLEANUP)
3353 return 0;
3354 mode = READ_ONCE(sqe->len);
3355 flags = READ_ONCE(sqe->open_flags);
3356 req->open.how = build_open_how(flags, mode);
3357 return __io_openat_prep(req, sqe);
3358}
3359
Jens Axboecebdb982020-01-08 17:59:24 -07003360static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3361{
3362 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003363 size_t len;
3364 int ret;
3365
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003366 if (req->flags & REQ_F_NEED_CLEANUP)
3367 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07003368 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3369 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003370 if (len < OPEN_HOW_SIZE_VER0)
3371 return -EINVAL;
3372
3373 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3374 len);
3375 if (ret)
3376 return ret;
3377
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003378 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003379}
3380
Pavel Begunkov014db002020-03-03 21:33:12 +03003381static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003382{
3383 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003384 struct file *file;
3385 int ret;
3386
Jens Axboef86cd202020-01-29 13:46:44 -07003387 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003388 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003389
Jens Axboecebdb982020-01-08 17:59:24 -07003390 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003391 if (ret)
3392 goto err;
3393
Jens Axboe4022e7a2020-03-19 19:23:18 -06003394 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003395 if (ret < 0)
3396 goto err;
3397
3398 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3399 if (IS_ERR(file)) {
3400 put_unused_fd(ret);
3401 ret = PTR_ERR(file);
3402 } else {
3403 fsnotify_open(file);
3404 fd_install(ret, file);
3405 }
3406err:
3407 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003408 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003409 if (ret < 0)
3410 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003411 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003412 return 0;
3413}
3414
Pavel Begunkov014db002020-03-03 21:33:12 +03003415static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003416{
Pavel Begunkov014db002020-03-03 21:33:12 +03003417 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003418}
3419
Jens Axboe067524e2020-03-02 16:32:28 -07003420static int io_remove_buffers_prep(struct io_kiocb *req,
3421 const struct io_uring_sqe *sqe)
3422{
3423 struct io_provide_buf *p = &req->pbuf;
3424 u64 tmp;
3425
3426 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3427 return -EINVAL;
3428
3429 tmp = READ_ONCE(sqe->fd);
3430 if (!tmp || tmp > USHRT_MAX)
3431 return -EINVAL;
3432
3433 memset(p, 0, sizeof(*p));
3434 p->nbufs = tmp;
3435 p->bgid = READ_ONCE(sqe->buf_group);
3436 return 0;
3437}
3438
3439static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3440 int bgid, unsigned nbufs)
3441{
3442 unsigned i = 0;
3443
3444 /* shouldn't happen */
3445 if (!nbufs)
3446 return 0;
3447
3448 /* the head kbuf is the list itself */
3449 while (!list_empty(&buf->list)) {
3450 struct io_buffer *nxt;
3451
3452 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3453 list_del(&nxt->list);
3454 kfree(nxt);
3455 if (++i == nbufs)
3456 return i;
3457 }
3458 i++;
3459 kfree(buf);
3460 idr_remove(&ctx->io_buffer_idr, bgid);
3461
3462 return i;
3463}
3464
Jens Axboe229a7b62020-06-22 10:13:11 -06003465static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock,
3466 struct io_comp_state *cs)
Jens Axboe067524e2020-03-02 16:32:28 -07003467{
3468 struct io_provide_buf *p = &req->pbuf;
3469 struct io_ring_ctx *ctx = req->ctx;
3470 struct io_buffer *head;
3471 int ret = 0;
3472
3473 io_ring_submit_lock(ctx, !force_nonblock);
3474
3475 lockdep_assert_held(&ctx->uring_lock);
3476
3477 ret = -ENOENT;
3478 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3479 if (head)
3480 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3481
3482 io_ring_submit_lock(ctx, !force_nonblock);
3483 if (ret < 0)
3484 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003485 __io_req_complete(req, ret, 0, cs);
Jens Axboe067524e2020-03-02 16:32:28 -07003486 return 0;
3487}
3488
Jens Axboeddf0322d2020-02-23 16:41:33 -07003489static int io_provide_buffers_prep(struct io_kiocb *req,
3490 const struct io_uring_sqe *sqe)
3491{
3492 struct io_provide_buf *p = &req->pbuf;
3493 u64 tmp;
3494
3495 if (sqe->ioprio || sqe->rw_flags)
3496 return -EINVAL;
3497
3498 tmp = READ_ONCE(sqe->fd);
3499 if (!tmp || tmp > USHRT_MAX)
3500 return -E2BIG;
3501 p->nbufs = tmp;
3502 p->addr = READ_ONCE(sqe->addr);
3503 p->len = READ_ONCE(sqe->len);
3504
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07003505 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07003506 return -EFAULT;
3507
3508 p->bgid = READ_ONCE(sqe->buf_group);
3509 tmp = READ_ONCE(sqe->off);
3510 if (tmp > USHRT_MAX)
3511 return -E2BIG;
3512 p->bid = tmp;
3513 return 0;
3514}
3515
3516static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3517{
3518 struct io_buffer *buf;
3519 u64 addr = pbuf->addr;
3520 int i, bid = pbuf->bid;
3521
3522 for (i = 0; i < pbuf->nbufs; i++) {
3523 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3524 if (!buf)
3525 break;
3526
3527 buf->addr = addr;
3528 buf->len = pbuf->len;
3529 buf->bid = bid;
3530 addr += pbuf->len;
3531 bid++;
3532 if (!*head) {
3533 INIT_LIST_HEAD(&buf->list);
3534 *head = buf;
3535 } else {
3536 list_add_tail(&buf->list, &(*head)->list);
3537 }
3538 }
3539
3540 return i ? i : -ENOMEM;
3541}
3542
Jens Axboe229a7b62020-06-22 10:13:11 -06003543static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock,
3544 struct io_comp_state *cs)
Jens Axboeddf0322d2020-02-23 16:41:33 -07003545{
3546 struct io_provide_buf *p = &req->pbuf;
3547 struct io_ring_ctx *ctx = req->ctx;
3548 struct io_buffer *head, *list;
3549 int ret = 0;
3550
3551 io_ring_submit_lock(ctx, !force_nonblock);
3552
3553 lockdep_assert_held(&ctx->uring_lock);
3554
3555 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3556
3557 ret = io_add_buffers(p, &head);
3558 if (ret < 0)
3559 goto out;
3560
3561 if (!list) {
3562 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3563 GFP_KERNEL);
3564 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003565 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003566 goto out;
3567 }
3568 }
3569out:
3570 io_ring_submit_unlock(ctx, !force_nonblock);
3571 if (ret < 0)
3572 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003573 __io_req_complete(req, ret, 0, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003574 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003575}
3576
Jens Axboe3e4827b2020-01-08 15:18:09 -07003577static int io_epoll_ctl_prep(struct io_kiocb *req,
3578 const struct io_uring_sqe *sqe)
3579{
3580#if defined(CONFIG_EPOLL)
3581 if (sqe->ioprio || sqe->buf_index)
3582 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003583 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3584 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07003585
3586 req->epoll.epfd = READ_ONCE(sqe->fd);
3587 req->epoll.op = READ_ONCE(sqe->len);
3588 req->epoll.fd = READ_ONCE(sqe->off);
3589
3590 if (ep_op_has_event(req->epoll.op)) {
3591 struct epoll_event __user *ev;
3592
3593 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3594 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3595 return -EFAULT;
3596 }
3597
3598 return 0;
3599#else
3600 return -EOPNOTSUPP;
3601#endif
3602}
3603
Jens Axboe229a7b62020-06-22 10:13:11 -06003604static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock,
3605 struct io_comp_state *cs)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003606{
3607#if defined(CONFIG_EPOLL)
3608 struct io_epoll *ie = &req->epoll;
3609 int ret;
3610
3611 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3612 if (force_nonblock && ret == -EAGAIN)
3613 return -EAGAIN;
3614
3615 if (ret < 0)
3616 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003617 __io_req_complete(req, ret, 0, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003618 return 0;
3619#else
3620 return -EOPNOTSUPP;
3621#endif
3622}
3623
Jens Axboec1ca7572019-12-25 22:18:28 -07003624static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3625{
3626#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3627 if (sqe->ioprio || sqe->buf_index || sqe->off)
3628 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003629 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3630 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07003631
3632 req->madvise.addr = READ_ONCE(sqe->addr);
3633 req->madvise.len = READ_ONCE(sqe->len);
3634 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3635 return 0;
3636#else
3637 return -EOPNOTSUPP;
3638#endif
3639}
3640
Pavel Begunkov014db002020-03-03 21:33:12 +03003641static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003642{
3643#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3644 struct io_madvise *ma = &req->madvise;
3645 int ret;
3646
3647 if (force_nonblock)
3648 return -EAGAIN;
3649
3650 ret = do_madvise(ma->addr, ma->len, ma->advice);
3651 if (ret < 0)
3652 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003653 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07003654 return 0;
3655#else
3656 return -EOPNOTSUPP;
3657#endif
3658}
3659
Jens Axboe4840e412019-12-25 22:03:45 -07003660static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3661{
3662 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3663 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003664 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3665 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07003666
3667 req->fadvise.offset = READ_ONCE(sqe->off);
3668 req->fadvise.len = READ_ONCE(sqe->len);
3669 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3670 return 0;
3671}
3672
Pavel Begunkov014db002020-03-03 21:33:12 +03003673static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07003674{
3675 struct io_fadvise *fa = &req->fadvise;
3676 int ret;
3677
Jens Axboe3e694262020-02-01 09:22:49 -07003678 if (force_nonblock) {
3679 switch (fa->advice) {
3680 case POSIX_FADV_NORMAL:
3681 case POSIX_FADV_RANDOM:
3682 case POSIX_FADV_SEQUENTIAL:
3683 break;
3684 default:
3685 return -EAGAIN;
3686 }
3687 }
Jens Axboe4840e412019-12-25 22:03:45 -07003688
3689 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3690 if (ret < 0)
3691 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003692 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07003693 return 0;
3694}
3695
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003696static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3697{
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003698 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3699 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003700 if (sqe->ioprio || sqe->buf_index)
3701 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003702 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003703 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003704
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003705 req->statx.dfd = READ_ONCE(sqe->fd);
3706 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003707 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003708 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3709 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003710
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003711 return 0;
3712}
3713
Pavel Begunkov014db002020-03-03 21:33:12 +03003714static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003715{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003716 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003717 int ret;
3718
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003719 if (force_nonblock) {
3720 /* only need file table for an actual valid fd */
3721 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
3722 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003723 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003724 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003725
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003726 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
3727 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003728
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003729 if (ret < 0)
3730 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003731 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003732 return 0;
3733}
3734
Jens Axboeb5dba592019-12-11 14:02:38 -07003735static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3736{
3737 /*
3738 * If we queue this for async, it must not be cancellable. That would
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003739 * leave the 'file' in an undeterminate state, and here need to modify
3740 * io_wq_work.flags, so initialize io_wq_work firstly.
Jens Axboeb5dba592019-12-11 14:02:38 -07003741 */
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003742 io_req_init_async(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07003743 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
3744
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003745 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3746 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07003747 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
3748 sqe->rw_flags || sqe->buf_index)
3749 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003750 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003751 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07003752
3753 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06003754 if ((req->file && req->file->f_op == &io_uring_fops) ||
3755 req->close.fd == req->ctx->ring_fd)
3756 return -EBADF;
3757
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003758 req->close.put_file = NULL;
Jens Axboeb5dba592019-12-11 14:02:38 -07003759 return 0;
3760}
3761
Jens Axboe229a7b62020-06-22 10:13:11 -06003762static int io_close(struct io_kiocb *req, bool force_nonblock,
3763 struct io_comp_state *cs)
Jens Axboeb5dba592019-12-11 14:02:38 -07003764{
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003765 struct io_close *close = &req->close;
Jens Axboeb5dba592019-12-11 14:02:38 -07003766 int ret;
3767
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003768 /* might be already done during nonblock submission */
3769 if (!close->put_file) {
3770 ret = __close_fd_get_file(close->fd, &close->put_file);
3771 if (ret < 0)
3772 return (ret == -ENOENT) ? -EBADF : ret;
3773 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003774
3775 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003776 if (close->put_file->f_op->flush && force_nonblock) {
Pavel Begunkov24c74672020-06-21 13:09:51 +03003777 /* was never set, but play safe */
3778 req->flags &= ~REQ_F_NOWAIT;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03003779 /* avoid grabbing files - we don't need the files */
Pavel Begunkov24c74672020-06-21 13:09:51 +03003780 req->flags |= REQ_F_NO_FILE_TABLE;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03003781 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03003782 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003783
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003784 /* No ->flush() or already async, safely close from here */
3785 ret = filp_close(close->put_file, req->work.files);
3786 if (ret < 0)
3787 req_set_fail_links(req);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003788 fput(close->put_file);
3789 close->put_file = NULL;
Jens Axboe229a7b62020-06-22 10:13:11 -06003790 __io_req_complete(req, ret, 0, cs);
Jens Axboe1a417f42020-01-31 17:16:48 -07003791 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07003792}
3793
Jens Axboe3529d8c2019-12-19 18:24:38 -07003794static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003795{
3796 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003797
3798 if (!req->file)
3799 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003800
3801 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3802 return -EINVAL;
3803 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
3804 return -EINVAL;
3805
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003806 req->sync.off = READ_ONCE(sqe->off);
3807 req->sync.len = READ_ONCE(sqe->len);
3808 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003809 return 0;
3810}
3811
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003812static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003813{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003814 int ret;
3815
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003816 /* sync_file_range always requires a blocking context */
3817 if (force_nonblock)
3818 return -EAGAIN;
3819
Jens Axboe9adbd452019-12-20 08:45:55 -07003820 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003821 req->sync.flags);
3822 if (ret < 0)
3823 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003824 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003825 return 0;
3826}
3827
YueHaibing469956e2020-03-04 15:53:52 +08003828#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003829static int io_setup_async_msg(struct io_kiocb *req,
3830 struct io_async_msghdr *kmsg)
3831{
3832 if (req->io)
3833 return -EAGAIN;
3834 if (io_alloc_async_ctx(req)) {
3835 if (kmsg->iov != kmsg->fast_iov)
3836 kfree(kmsg->iov);
3837 return -ENOMEM;
3838 }
3839 req->flags |= REQ_F_NEED_CLEANUP;
3840 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
3841 return -EAGAIN;
3842}
3843
Jens Axboe3529d8c2019-12-19 18:24:38 -07003844static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06003845{
Jens Axboee47293f2019-12-20 08:58:21 -07003846 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003847 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003848 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003849
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03003850 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3851 return -EINVAL;
3852
Jens Axboee47293f2019-12-20 08:58:21 -07003853 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3854 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07003855 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003856
Jens Axboed8768362020-02-27 14:17:49 -07003857#ifdef CONFIG_COMPAT
3858 if (req->ctx->compat)
3859 sr->msg_flags |= MSG_CMSG_COMPAT;
3860#endif
3861
Jens Axboefddafac2020-01-04 20:19:44 -07003862 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003863 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003864 /* iovec is already imported */
3865 if (req->flags & REQ_F_NEED_CLEANUP)
3866 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003867
Jens Axboed9688562019-12-09 19:35:20 -07003868 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003869 ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003870 &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003871 if (!ret)
3872 req->flags |= REQ_F_NEED_CLEANUP;
3873 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003874}
3875
Jens Axboe229a7b62020-06-22 10:13:11 -06003876static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
3877 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07003878{
Jens Axboe0b416c32019-12-15 10:57:46 -07003879 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07003880 struct socket *sock;
3881 int ret;
3882
Jens Axboe03b12302019-12-02 18:50:25 -07003883 sock = sock_from_file(req->file, &ret);
3884 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003885 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003886 unsigned flags;
3887
Jens Axboe03b12302019-12-02 18:50:25 -07003888 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003889 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003890 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003891 /* if iov is set, it's allocated already */
3892 if (!kmsg->iov)
3893 kmsg->iov = kmsg->fast_iov;
3894 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003895 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003896 struct io_sr_msg *sr = &req->sr_msg;
3897
Jens Axboe0b416c32019-12-15 10:57:46 -07003898 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003899 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003900
3901 io.msg.iov = io.msg.fast_iov;
3902 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3903 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003904 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003905 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003906 }
3907
Jens Axboee47293f2019-12-20 08:58:21 -07003908 flags = req->sr_msg.msg_flags;
3909 if (flags & MSG_DONTWAIT)
3910 req->flags |= REQ_F_NOWAIT;
3911 else if (force_nonblock)
3912 flags |= MSG_DONTWAIT;
3913
Jens Axboe0b416c32019-12-15 10:57:46 -07003914 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003915 if (force_nonblock && ret == -EAGAIN)
3916 return io_setup_async_msg(req, kmsg);
Jens Axboe03b12302019-12-02 18:50:25 -07003917 if (ret == -ERESTARTSYS)
3918 ret = -EINTR;
3919 }
3920
Pavel Begunkov1e950812020-02-06 19:51:16 +03003921 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003922 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003923 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003924 if (ret < 0)
3925 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003926 __io_req_complete(req, ret, 0, cs);
Jens Axboe03b12302019-12-02 18:50:25 -07003927 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003928}
3929
Jens Axboe229a7b62020-06-22 10:13:11 -06003930static int io_send(struct io_kiocb *req, bool force_nonblock,
3931 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07003932{
Jens Axboefddafac2020-01-04 20:19:44 -07003933 struct socket *sock;
3934 int ret;
3935
Jens Axboefddafac2020-01-04 20:19:44 -07003936 sock = sock_from_file(req->file, &ret);
3937 if (sock) {
3938 struct io_sr_msg *sr = &req->sr_msg;
3939 struct msghdr msg;
3940 struct iovec iov;
3941 unsigned flags;
3942
3943 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3944 &msg.msg_iter);
3945 if (ret)
3946 return ret;
3947
3948 msg.msg_name = NULL;
3949 msg.msg_control = NULL;
3950 msg.msg_controllen = 0;
3951 msg.msg_namelen = 0;
3952
3953 flags = req->sr_msg.msg_flags;
3954 if (flags & MSG_DONTWAIT)
3955 req->flags |= REQ_F_NOWAIT;
3956 else if (force_nonblock)
3957 flags |= MSG_DONTWAIT;
3958
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003959 msg.msg_flags = flags;
3960 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003961 if (force_nonblock && ret == -EAGAIN)
3962 return -EAGAIN;
3963 if (ret == -ERESTARTSYS)
3964 ret = -EINTR;
3965 }
3966
Jens Axboefddafac2020-01-04 20:19:44 -07003967 if (ret < 0)
3968 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003969 __io_req_complete(req, ret, 0, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07003970 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003971}
3972
Jens Axboe52de1fe2020-02-27 10:15:42 -07003973static int __io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
3974{
3975 struct io_sr_msg *sr = &req->sr_msg;
3976 struct iovec __user *uiov;
3977 size_t iov_len;
3978 int ret;
3979
3980 ret = __copy_msghdr_from_user(&io->msg.msg, sr->msg, &io->msg.uaddr,
3981 &uiov, &iov_len);
3982 if (ret)
3983 return ret;
3984
3985 if (req->flags & REQ_F_BUFFER_SELECT) {
3986 if (iov_len > 1)
3987 return -EINVAL;
3988 if (copy_from_user(io->msg.iov, uiov, sizeof(*uiov)))
3989 return -EFAULT;
3990 sr->len = io->msg.iov[0].iov_len;
3991 iov_iter_init(&io->msg.msg.msg_iter, READ, io->msg.iov, 1,
3992 sr->len);
3993 io->msg.iov = NULL;
3994 } else {
3995 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
3996 &io->msg.iov, &io->msg.msg.msg_iter);
3997 if (ret > 0)
3998 ret = 0;
3999 }
4000
4001 return ret;
4002}
4003
4004#ifdef CONFIG_COMPAT
4005static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
4006 struct io_async_ctx *io)
4007{
4008 struct compat_msghdr __user *msg_compat;
4009 struct io_sr_msg *sr = &req->sr_msg;
4010 struct compat_iovec __user *uiov;
4011 compat_uptr_t ptr;
4012 compat_size_t len;
4013 int ret;
4014
4015 msg_compat = (struct compat_msghdr __user *) sr->msg;
4016 ret = __get_compat_msghdr(&io->msg.msg, msg_compat, &io->msg.uaddr,
4017 &ptr, &len);
4018 if (ret)
4019 return ret;
4020
4021 uiov = compat_ptr(ptr);
4022 if (req->flags & REQ_F_BUFFER_SELECT) {
4023 compat_ssize_t clen;
4024
4025 if (len > 1)
4026 return -EINVAL;
4027 if (!access_ok(uiov, sizeof(*uiov)))
4028 return -EFAULT;
4029 if (__get_user(clen, &uiov->iov_len))
4030 return -EFAULT;
4031 if (clen < 0)
4032 return -EINVAL;
4033 sr->len = io->msg.iov[0].iov_len;
4034 io->msg.iov = NULL;
4035 } else {
4036 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
4037 &io->msg.iov,
4038 &io->msg.msg.msg_iter);
4039 if (ret < 0)
4040 return ret;
4041 }
4042
4043 return 0;
4044}
Jens Axboe03b12302019-12-02 18:50:25 -07004045#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004046
4047static int io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
4048{
4049 io->msg.iov = io->msg.fast_iov;
4050
4051#ifdef CONFIG_COMPAT
4052 if (req->ctx->compat)
4053 return __io_compat_recvmsg_copy_hdr(req, io);
4054#endif
4055
4056 return __io_recvmsg_copy_hdr(req, io);
4057}
4058
Jens Axboebcda7ba2020-02-23 16:42:51 -07004059static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
4060 int *cflags, bool needs_lock)
4061{
4062 struct io_sr_msg *sr = &req->sr_msg;
4063 struct io_buffer *kbuf;
4064
4065 if (!(req->flags & REQ_F_BUFFER_SELECT))
4066 return NULL;
4067
4068 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4069 if (IS_ERR(kbuf))
4070 return kbuf;
4071
4072 sr->kbuf = kbuf;
4073 req->flags |= REQ_F_BUFFER_SELECTED;
4074
4075 *cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
4076 *cflags |= IORING_CQE_F_BUFFER;
4077 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004078}
4079
Jens Axboe3529d8c2019-12-19 18:24:38 -07004080static int io_recvmsg_prep(struct io_kiocb *req,
4081 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004082{
Jens Axboee47293f2019-12-20 08:58:21 -07004083 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004084 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004085 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004086
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004087 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4088 return -EINVAL;
4089
Jens Axboe3529d8c2019-12-19 18:24:38 -07004090 sr->msg_flags = READ_ONCE(sqe->msg_flags);
4091 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004092 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004093 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004094
Jens Axboed8768362020-02-27 14:17:49 -07004095#ifdef CONFIG_COMPAT
4096 if (req->ctx->compat)
4097 sr->msg_flags |= MSG_CMSG_COMPAT;
4098#endif
4099
Jens Axboefddafac2020-01-04 20:19:44 -07004100 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07004101 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03004102 /* iovec is already imported */
4103 if (req->flags & REQ_F_NEED_CLEANUP)
4104 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004105
Jens Axboe52de1fe2020-02-27 10:15:42 -07004106 ret = io_recvmsg_copy_hdr(req, io);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004107 if (!ret)
4108 req->flags |= REQ_F_NEED_CLEANUP;
4109 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004110}
4111
Jens Axboe229a7b62020-06-22 10:13:11 -06004112static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4113 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004114{
Jens Axboe0b416c32019-12-15 10:57:46 -07004115 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004116 struct socket *sock;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004117 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004118
Jens Axboe0fa03c62019-04-19 13:34:07 -06004119 sock = sock_from_file(req->file, &ret);
4120 if (sock) {
Jens Axboe52de1fe2020-02-27 10:15:42 -07004121 struct io_buffer *kbuf;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004122 struct io_async_ctx io;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004123 unsigned flags;
4124
Jens Axboe03b12302019-12-02 18:50:25 -07004125 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07004126 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07004127 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07004128 /* if iov is set, it's allocated already */
4129 if (!kmsg->iov)
4130 kmsg->iov = kmsg->fast_iov;
4131 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004132 } else {
Jens Axboe0b416c32019-12-15 10:57:46 -07004133 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07004134 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004135
Jens Axboe52de1fe2020-02-27 10:15:42 -07004136 ret = io_recvmsg_copy_hdr(req, &io);
Jens Axboe03b12302019-12-02 18:50:25 -07004137 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004138 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004139 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06004140
Jens Axboe52de1fe2020-02-27 10:15:42 -07004141 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
4142 if (IS_ERR(kbuf)) {
4143 return PTR_ERR(kbuf);
4144 } else if (kbuf) {
4145 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
4146 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
4147 1, req->sr_msg.len);
4148 }
4149
Jens Axboee47293f2019-12-20 08:58:21 -07004150 flags = req->sr_msg.msg_flags;
4151 if (flags & MSG_DONTWAIT)
4152 req->flags |= REQ_F_NOWAIT;
4153 else if (force_nonblock)
4154 flags |= MSG_DONTWAIT;
4155
4156 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
4157 kmsg->uaddr, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004158 if (force_nonblock && ret == -EAGAIN)
4159 return io_setup_async_msg(req, kmsg);
Jens Axboe441cdbd2019-12-02 18:49:10 -07004160 if (ret == -ERESTARTSYS)
4161 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004162 }
4163
Pavel Begunkov1e950812020-02-06 19:51:16 +03004164 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07004165 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004166 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004167 if (ret < 0)
4168 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004169 __io_req_complete(req, ret, cflags, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004170 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004171}
4172
Jens Axboe229a7b62020-06-22 10:13:11 -06004173static int io_recv(struct io_kiocb *req, bool force_nonblock,
4174 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07004175{
Jens Axboebcda7ba2020-02-23 16:42:51 -07004176 struct io_buffer *kbuf = NULL;
Jens Axboefddafac2020-01-04 20:19:44 -07004177 struct socket *sock;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004178 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004179
Jens Axboefddafac2020-01-04 20:19:44 -07004180 sock = sock_from_file(req->file, &ret);
4181 if (sock) {
4182 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004183 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004184 struct msghdr msg;
4185 struct iovec iov;
4186 unsigned flags;
4187
Jens Axboebcda7ba2020-02-23 16:42:51 -07004188 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
4189 if (IS_ERR(kbuf))
4190 return PTR_ERR(kbuf);
4191 else if (kbuf)
4192 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004193
Jens Axboebcda7ba2020-02-23 16:42:51 -07004194 ret = import_single_range(READ, buf, sr->len, &iov,
4195 &msg.msg_iter);
4196 if (ret) {
4197 kfree(kbuf);
4198 return ret;
4199 }
4200
4201 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004202 msg.msg_name = NULL;
4203 msg.msg_control = NULL;
4204 msg.msg_controllen = 0;
4205 msg.msg_namelen = 0;
4206 msg.msg_iocb = NULL;
4207 msg.msg_flags = 0;
4208
4209 flags = req->sr_msg.msg_flags;
4210 if (flags & MSG_DONTWAIT)
4211 req->flags |= REQ_F_NOWAIT;
4212 else if (force_nonblock)
4213 flags |= MSG_DONTWAIT;
4214
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004215 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07004216 if (force_nonblock && ret == -EAGAIN)
4217 return -EAGAIN;
4218 if (ret == -ERESTARTSYS)
4219 ret = -EINTR;
4220 }
4221
Jens Axboebcda7ba2020-02-23 16:42:51 -07004222 kfree(kbuf);
4223 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004224 if (ret < 0)
4225 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004226 __io_req_complete(req, ret, cflags, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004227 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004228}
4229
Jens Axboe3529d8c2019-12-19 18:24:38 -07004230static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004231{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004232 struct io_accept *accept = &req->accept;
4233
Jens Axboe17f2fe32019-10-17 14:42:58 -06004234 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4235 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004236 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004237 return -EINVAL;
4238
Jens Axboed55e5f52019-12-11 16:12:15 -07004239 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4240 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004241 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004242 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004243 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004244}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004245
Jens Axboe229a7b62020-06-22 10:13:11 -06004246static int io_accept(struct io_kiocb *req, bool force_nonblock,
4247 struct io_comp_state *cs)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004248{
4249 struct io_accept *accept = &req->accept;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004250 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004251 int ret;
4252
Jiufei Xuee697dee2020-06-10 13:41:59 +08004253 if (req->file->f_flags & O_NONBLOCK)
4254 req->flags |= REQ_F_NOWAIT;
4255
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004256 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004257 accept->addr_len, accept->flags,
4258 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004259 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004260 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004261 if (ret < 0) {
4262 if (ret == -ERESTARTSYS)
4263 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004264 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004265 }
Jens Axboe229a7b62020-06-22 10:13:11 -06004266 __io_req_complete(req, ret, 0, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004267 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004268}
4269
Jens Axboe3529d8c2019-12-19 18:24:38 -07004270static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004271{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004272 struct io_connect *conn = &req->connect;
4273 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07004274
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004275 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4276 return -EINVAL;
4277 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4278 return -EINVAL;
4279
Jens Axboe3529d8c2019-12-19 18:24:38 -07004280 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4281 conn->addr_len = READ_ONCE(sqe->addr2);
4282
4283 if (!io)
4284 return 0;
4285
4286 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004287 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004288}
4289
Jens Axboe229a7b62020-06-22 10:13:11 -06004290static int io_connect(struct io_kiocb *req, bool force_nonblock,
4291 struct io_comp_state *cs)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004292{
Jens Axboef499a022019-12-02 16:28:46 -07004293 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004294 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004295 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004296
Jens Axboef499a022019-12-02 16:28:46 -07004297 if (req->io) {
4298 io = req->io;
4299 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004300 ret = move_addr_to_kernel(req->connect.addr,
4301 req->connect.addr_len,
4302 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004303 if (ret)
4304 goto out;
4305 io = &__io;
4306 }
4307
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004308 file_flags = force_nonblock ? O_NONBLOCK : 0;
4309
4310 ret = __sys_connect_file(req->file, &io->connect.address,
4311 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004312 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004313 if (req->io)
4314 return -EAGAIN;
4315 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004316 ret = -ENOMEM;
4317 goto out;
4318 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004319 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004320 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004321 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004322 if (ret == -ERESTARTSYS)
4323 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004324out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004325 if (ret < 0)
4326 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004327 __io_req_complete(req, ret, 0, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004328 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004329}
YueHaibing469956e2020-03-04 15:53:52 +08004330#else /* !CONFIG_NET */
4331static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4332{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004333 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004334}
4335
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004336static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4337 struct io_comp_state *cs)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004338{
YueHaibing469956e2020-03-04 15:53:52 +08004339 return -EOPNOTSUPP;
4340}
4341
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004342static int io_send(struct io_kiocb *req, bool force_nonblock,
4343 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004344{
4345 return -EOPNOTSUPP;
4346}
4347
4348static int io_recvmsg_prep(struct io_kiocb *req,
4349 const struct io_uring_sqe *sqe)
4350{
4351 return -EOPNOTSUPP;
4352}
4353
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004354static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4355 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004356{
4357 return -EOPNOTSUPP;
4358}
4359
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004360static int io_recv(struct io_kiocb *req, bool force_nonblock,
4361 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004362{
4363 return -EOPNOTSUPP;
4364}
4365
4366static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4367{
4368 return -EOPNOTSUPP;
4369}
4370
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004371static int io_accept(struct io_kiocb *req, bool force_nonblock,
4372 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004373{
4374 return -EOPNOTSUPP;
4375}
4376
4377static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4378{
4379 return -EOPNOTSUPP;
4380}
4381
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004382static int io_connect(struct io_kiocb *req, bool force_nonblock,
4383 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004384{
4385 return -EOPNOTSUPP;
4386}
4387#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004388
Jens Axboed7718a92020-02-14 22:23:12 -07004389struct io_poll_table {
4390 struct poll_table_struct pt;
4391 struct io_kiocb *req;
4392 int error;
4393};
4394
Jens Axboed7718a92020-02-14 22:23:12 -07004395static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4396 __poll_t mask, task_work_func_t func)
4397{
4398 struct task_struct *tsk;
Jens Axboeaa96bf82020-04-03 11:26:26 -06004399 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004400
4401 /* for instances that support it check for an event match first: */
4402 if (mask && !(mask & poll->events))
4403 return 0;
4404
4405 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4406
4407 list_del_init(&poll->wait.entry);
4408
4409 tsk = req->task;
4410 req->result = mask;
4411 init_task_work(&req->task_work, func);
4412 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004413 * If this fails, then the task is exiting. When a task exits, the
4414 * work gets canceled, so just cancel this request as well instead
4415 * of executing it. We can't safely execute it anyway, as we may not
4416 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004417 */
Jens Axboeaa96bf82020-04-03 11:26:26 -06004418 ret = task_work_add(tsk, &req->task_work, true);
4419 if (unlikely(ret)) {
Jens Axboee3aabf92020-05-18 11:04:17 -06004420 WRITE_ONCE(poll->canceled, true);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004421 tsk = io_wq_get_task(req->ctx->io_wq);
4422 task_work_add(tsk, &req->task_work, true);
4423 }
Jens Axboed7718a92020-02-14 22:23:12 -07004424 wake_up_process(tsk);
4425 return 1;
4426}
4427
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004428static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4429 __acquires(&req->ctx->completion_lock)
4430{
4431 struct io_ring_ctx *ctx = req->ctx;
4432
4433 if (!req->result && !READ_ONCE(poll->canceled)) {
4434 struct poll_table_struct pt = { ._key = poll->events };
4435
4436 req->result = vfs_poll(req->file, &pt) & poll->events;
4437 }
4438
4439 spin_lock_irq(&ctx->completion_lock);
4440 if (!req->result && !READ_ONCE(poll->canceled)) {
4441 add_wait_queue(poll->head, &poll->wait);
4442 return true;
4443 }
4444
4445 return false;
4446}
4447
Jens Axboe18bceab2020-05-15 11:56:54 -06004448static void io_poll_remove_double(struct io_kiocb *req)
4449{
4450 struct io_poll_iocb *poll = (struct io_poll_iocb *) req->io;
4451
4452 lockdep_assert_held(&req->ctx->completion_lock);
4453
4454 if (poll && poll->head) {
4455 struct wait_queue_head *head = poll->head;
4456
4457 spin_lock(&head->lock);
4458 list_del_init(&poll->wait.entry);
4459 if (poll->wait.private)
4460 refcount_dec(&req->refs);
4461 poll->head = NULL;
4462 spin_unlock(&head->lock);
4463 }
4464}
4465
4466static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4467{
4468 struct io_ring_ctx *ctx = req->ctx;
4469
4470 io_poll_remove_double(req);
4471 req->poll.done = true;
4472 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4473 io_commit_cqring(ctx);
4474}
4475
4476static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
4477{
4478 struct io_ring_ctx *ctx = req->ctx;
4479
4480 if (io_poll_rewait(req, &req->poll)) {
4481 spin_unlock_irq(&ctx->completion_lock);
4482 return;
4483 }
4484
4485 hash_del(&req->hash_node);
4486 io_poll_complete(req, req->result, 0);
4487 req->flags |= REQ_F_COMP_LOCKED;
4488 io_put_req_find_next(req, nxt);
4489 spin_unlock_irq(&ctx->completion_lock);
4490
4491 io_cqring_ev_posted(ctx);
4492}
4493
4494static void io_poll_task_func(struct callback_head *cb)
4495{
4496 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4497 struct io_kiocb *nxt = NULL;
4498
4499 io_poll_task_handler(req, &nxt);
4500 if (nxt) {
4501 struct io_ring_ctx *ctx = nxt->ctx;
4502
4503 mutex_lock(&ctx->uring_lock);
Jens Axboef13fad72020-06-22 09:34:30 -06004504 __io_queue_sqe(nxt, NULL, NULL);
Jens Axboe18bceab2020-05-15 11:56:54 -06004505 mutex_unlock(&ctx->uring_lock);
4506 }
4507}
4508
4509static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4510 int sync, void *key)
4511{
4512 struct io_kiocb *req = wait->private;
4513 struct io_poll_iocb *poll = (struct io_poll_iocb *) req->io;
4514 __poll_t mask = key_to_poll(key);
4515
4516 /* for instances that support it check for an event match first: */
4517 if (mask && !(mask & poll->events))
4518 return 0;
4519
4520 if (req->poll.head) {
4521 bool done;
4522
4523 spin_lock(&req->poll.head->lock);
4524 done = list_empty(&req->poll.wait.entry);
4525 if (!done)
4526 list_del_init(&req->poll.wait.entry);
4527 spin_unlock(&req->poll.head->lock);
4528 if (!done)
4529 __io_async_wake(req, poll, mask, io_poll_task_func);
4530 }
4531 refcount_dec(&req->refs);
4532 return 1;
4533}
4534
4535static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4536 wait_queue_func_t wake_func)
4537{
4538 poll->head = NULL;
4539 poll->done = false;
4540 poll->canceled = false;
4541 poll->events = events;
4542 INIT_LIST_HEAD(&poll->wait.entry);
4543 init_waitqueue_func_entry(&poll->wait, wake_func);
4544}
4545
4546static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
4547 struct wait_queue_head *head)
4548{
4549 struct io_kiocb *req = pt->req;
4550
4551 /*
4552 * If poll->head is already set, it's because the file being polled
4553 * uses multiple waitqueues for poll handling (eg one for read, one
4554 * for write). Setup a separate io_poll_iocb if this happens.
4555 */
4556 if (unlikely(poll->head)) {
4557 /* already have a 2nd entry, fail a third attempt */
4558 if (req->io) {
4559 pt->error = -EINVAL;
4560 return;
4561 }
4562 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
4563 if (!poll) {
4564 pt->error = -ENOMEM;
4565 return;
4566 }
4567 io_init_poll_iocb(poll, req->poll.events, io_poll_double_wake);
4568 refcount_inc(&req->refs);
4569 poll->wait.private = req;
4570 req->io = (void *) poll;
4571 }
4572
4573 pt->error = 0;
4574 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004575
4576 if (poll->events & EPOLLEXCLUSIVE)
4577 add_wait_queue_exclusive(head, &poll->wait);
4578 else
4579 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06004580}
4581
4582static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4583 struct poll_table_struct *p)
4584{
4585 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4586
4587 __io_queue_proc(&pt->req->apoll->poll, pt, head);
4588}
4589
Jens Axboed7718a92020-02-14 22:23:12 -07004590static void io_async_task_func(struct callback_head *cb)
4591{
4592 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4593 struct async_poll *apoll = req->apoll;
4594 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe31067252020-05-17 17:43:31 -06004595 bool canceled = false;
Jens Axboed7718a92020-02-14 22:23:12 -07004596
4597 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4598
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004599 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07004600 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004601 return;
Jens Axboed7718a92020-02-14 22:23:12 -07004602 }
4603
Jens Axboe31067252020-05-17 17:43:31 -06004604 /* If req is still hashed, it cannot have been canceled. Don't check. */
4605 if (hash_hashed(&req->hash_node)) {
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004606 hash_del(&req->hash_node);
Jens Axboe31067252020-05-17 17:43:31 -06004607 } else {
4608 canceled = READ_ONCE(apoll->poll.canceled);
4609 if (canceled) {
4610 io_cqring_fill_event(req, -ECANCELED);
4611 io_commit_cqring(ctx);
4612 }
Jens Axboe2bae0472020-04-13 11:16:34 -06004613 }
4614
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004615 spin_unlock_irq(&ctx->completion_lock);
4616
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004617 /* restore ->work in case we need to retry again */
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004618 if (req->flags & REQ_F_WORK_INITIALIZED)
4619 memcpy(&req->work, &apoll->work, sizeof(req->work));
Jens Axboe31067252020-05-17 17:43:31 -06004620 kfree(apoll);
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004621
Jens Axboe31067252020-05-17 17:43:31 -06004622 if (!canceled) {
4623 __set_current_state(TASK_RUNNING);
Jens Axboe9d8426a2020-06-16 18:42:49 -06004624 if (io_sq_thread_acquire_mm(ctx, req)) {
Jens Axboee1e16092020-06-22 09:17:17 -06004625 io_cqring_add_event(req, -EFAULT, 0);
Jens Axboe9d8426a2020-06-16 18:42:49 -06004626 goto end_req;
4627 }
Jens Axboe31067252020-05-17 17:43:31 -06004628 mutex_lock(&ctx->uring_lock);
Jens Axboef13fad72020-06-22 09:34:30 -06004629 __io_queue_sqe(req, NULL, NULL);
Jens Axboe31067252020-05-17 17:43:31 -06004630 mutex_unlock(&ctx->uring_lock);
4631 } else {
Jens Axboe2bae0472020-04-13 11:16:34 -06004632 io_cqring_ev_posted(ctx);
Jens Axboe9d8426a2020-06-16 18:42:49 -06004633end_req:
Jens Axboe2bae0472020-04-13 11:16:34 -06004634 req_set_fail_links(req);
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004635 io_double_put_req(req);
Jens Axboe2bae0472020-04-13 11:16:34 -06004636 }
Jens Axboed7718a92020-02-14 22:23:12 -07004637}
4638
4639static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4640 void *key)
4641{
4642 struct io_kiocb *req = wait->private;
4643 struct io_poll_iocb *poll = &req->apoll->poll;
4644
4645 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4646 key_to_poll(key));
4647
4648 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4649}
4650
4651static void io_poll_req_insert(struct io_kiocb *req)
4652{
4653 struct io_ring_ctx *ctx = req->ctx;
4654 struct hlist_head *list;
4655
4656 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
4657 hlist_add_head(&req->hash_node, list);
4658}
4659
4660static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
4661 struct io_poll_iocb *poll,
4662 struct io_poll_table *ipt, __poll_t mask,
4663 wait_queue_func_t wake_func)
4664 __acquires(&ctx->completion_lock)
4665{
4666 struct io_ring_ctx *ctx = req->ctx;
4667 bool cancel = false;
4668
Jens Axboe18bceab2020-05-15 11:56:54 -06004669 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03004670 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06004671 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07004672
4673 ipt->pt._key = mask;
4674 ipt->req = req;
4675 ipt->error = -EINVAL;
4676
Jens Axboed7718a92020-02-14 22:23:12 -07004677 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
4678
4679 spin_lock_irq(&ctx->completion_lock);
4680 if (likely(poll->head)) {
4681 spin_lock(&poll->head->lock);
4682 if (unlikely(list_empty(&poll->wait.entry))) {
4683 if (ipt->error)
4684 cancel = true;
4685 ipt->error = 0;
4686 mask = 0;
4687 }
4688 if (mask || ipt->error)
4689 list_del_init(&poll->wait.entry);
4690 else if (cancel)
4691 WRITE_ONCE(poll->canceled, true);
4692 else if (!poll->done) /* actually waiting for an event */
4693 io_poll_req_insert(req);
4694 spin_unlock(&poll->head->lock);
4695 }
4696
4697 return mask;
4698}
4699
4700static bool io_arm_poll_handler(struct io_kiocb *req)
4701{
4702 const struct io_op_def *def = &io_op_defs[req->opcode];
4703 struct io_ring_ctx *ctx = req->ctx;
4704 struct async_poll *apoll;
4705 struct io_poll_table ipt;
4706 __poll_t mask, ret;
Jens Axboe18bceab2020-05-15 11:56:54 -06004707 bool had_io;
Jens Axboed7718a92020-02-14 22:23:12 -07004708
4709 if (!req->file || !file_can_poll(req->file))
4710 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03004711 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07004712 return false;
4713 if (!def->pollin && !def->pollout)
4714 return false;
4715
4716 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
4717 if (unlikely(!apoll))
4718 return false;
4719
4720 req->flags |= REQ_F_POLLED;
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004721 if (req->flags & REQ_F_WORK_INITIALIZED)
4722 memcpy(&apoll->work, &req->work, sizeof(req->work));
Jens Axboe18bceab2020-05-15 11:56:54 -06004723 had_io = req->io != NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07004724
Pavel Begunkov4dd28242020-06-15 10:33:13 +03004725 io_get_req_task(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004726 req->apoll = apoll;
4727 INIT_HLIST_NODE(&req->hash_node);
4728
Nathan Chancellor8755d972020-03-02 16:01:19 -07004729 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07004730 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07004731 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07004732 if (def->pollout)
4733 mask |= POLLOUT | POLLWRNORM;
4734 mask |= POLLERR | POLLPRI;
4735
4736 ipt.pt._qproc = io_async_queue_proc;
4737
4738 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
4739 io_async_wake);
4740 if (ret) {
4741 ipt.error = 0;
Jens Axboe18bceab2020-05-15 11:56:54 -06004742 /* only remove double add if we did it here */
4743 if (!had_io)
4744 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004745 spin_unlock_irq(&ctx->completion_lock);
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004746 if (req->flags & REQ_F_WORK_INITIALIZED)
4747 memcpy(&req->work, &apoll->work, sizeof(req->work));
Jens Axboed7718a92020-02-14 22:23:12 -07004748 kfree(apoll);
4749 return false;
4750 }
4751 spin_unlock_irq(&ctx->completion_lock);
4752 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
4753 apoll->poll.events);
4754 return true;
4755}
4756
4757static bool __io_poll_remove_one(struct io_kiocb *req,
4758 struct io_poll_iocb *poll)
4759{
Jens Axboeb41e9852020-02-17 09:52:41 -07004760 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004761
4762 spin_lock(&poll->head->lock);
4763 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07004764 if (!list_empty(&poll->wait.entry)) {
4765 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07004766 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004767 }
4768 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004769 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07004770 return do_complete;
4771}
4772
4773static bool io_poll_remove_one(struct io_kiocb *req)
4774{
4775 bool do_complete;
4776
4777 if (req->opcode == IORING_OP_POLL_ADD) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004778 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004779 do_complete = __io_poll_remove_one(req, &req->poll);
4780 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004781 struct async_poll *apoll = req->apoll;
4782
Jens Axboed7718a92020-02-14 22:23:12 -07004783 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004784 do_complete = __io_poll_remove_one(req, &apoll->poll);
4785 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07004786 io_put_req(req);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004787 /*
4788 * restore ->work because we will call
4789 * io_req_work_drop_env below when dropping the
4790 * final reference.
4791 */
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004792 if (req->flags & REQ_F_WORK_INITIALIZED)
4793 memcpy(&req->work, &apoll->work,
4794 sizeof(req->work));
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004795 kfree(apoll);
4796 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08004797 }
4798
Jens Axboeb41e9852020-02-17 09:52:41 -07004799 if (do_complete) {
4800 io_cqring_fill_event(req, -ECANCELED);
4801 io_commit_cqring(req->ctx);
4802 req->flags |= REQ_F_COMP_LOCKED;
4803 io_put_req(req);
4804 }
4805
4806 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004807}
4808
4809static void io_poll_remove_all(struct io_ring_ctx *ctx)
4810{
Jens Axboe78076bb2019-12-04 19:56:40 -07004811 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004812 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004813 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004814
4815 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07004816 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
4817 struct hlist_head *list;
4818
4819 list = &ctx->cancel_hash[i];
4820 hlist_for_each_entry_safe(req, tmp, list, hash_node)
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004821 posted += io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004822 }
4823 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07004824
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004825 if (posted)
4826 io_cqring_ev_posted(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004827}
4828
Jens Axboe47f46762019-11-09 17:43:02 -07004829static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
4830{
Jens Axboe78076bb2019-12-04 19:56:40 -07004831 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07004832 struct io_kiocb *req;
4833
Jens Axboe78076bb2019-12-04 19:56:40 -07004834 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
4835 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07004836 if (sqe_addr != req->user_data)
4837 continue;
4838 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07004839 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07004840 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07004841 }
4842
4843 return -ENOENT;
4844}
4845
Jens Axboe3529d8c2019-12-19 18:24:38 -07004846static int io_poll_remove_prep(struct io_kiocb *req,
4847 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004848{
Jens Axboe221c5eb2019-01-17 09:41:58 -07004849 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4850 return -EINVAL;
4851 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
4852 sqe->poll_events)
4853 return -EINVAL;
4854
Jens Axboe0969e782019-12-17 18:40:57 -07004855 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07004856 return 0;
4857}
4858
4859/*
4860 * Find a running poll command that matches one specified in sqe->addr,
4861 * and remove it if found.
4862 */
4863static int io_poll_remove(struct io_kiocb *req)
4864{
4865 struct io_ring_ctx *ctx = req->ctx;
4866 u64 addr;
4867 int ret;
4868
Jens Axboe0969e782019-12-17 18:40:57 -07004869 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004870 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07004871 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004872 spin_unlock_irq(&ctx->completion_lock);
4873
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004874 if (ret < 0)
4875 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004876 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004877 return 0;
4878}
4879
Jens Axboe221c5eb2019-01-17 09:41:58 -07004880static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4881 void *key)
4882{
Jens Axboec2f2eb72020-02-10 09:07:05 -07004883 struct io_kiocb *req = wait->private;
4884 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004885
Jens Axboed7718a92020-02-14 22:23:12 -07004886 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004887}
4888
Jens Axboe221c5eb2019-01-17 09:41:58 -07004889static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
4890 struct poll_table_struct *p)
4891{
4892 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4893
Jens Axboed7718a92020-02-14 22:23:12 -07004894 __io_queue_proc(&pt->req->poll, pt, head);
Jens Axboeeac406c2019-11-14 12:09:58 -07004895}
4896
Jens Axboe3529d8c2019-12-19 18:24:38 -07004897static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004898{
4899 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08004900 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004901
4902 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4903 return -EINVAL;
4904 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
4905 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06004906 if (!poll->file)
4907 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004908
Jiufei Xue5769a352020-06-17 17:53:55 +08004909 events = READ_ONCE(sqe->poll32_events);
4910#ifdef __BIG_ENDIAN
4911 events = swahw32(events);
4912#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004913 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
4914 (events & EPOLLEXCLUSIVE);
Jens Axboeb41e9852020-02-17 09:52:41 -07004915
Pavel Begunkov4dd28242020-06-15 10:33:13 +03004916 io_get_req_task(req);
Jens Axboe0969e782019-12-17 18:40:57 -07004917 return 0;
4918}
4919
Pavel Begunkov014db002020-03-03 21:33:12 +03004920static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07004921{
4922 struct io_poll_iocb *poll = &req->poll;
4923 struct io_ring_ctx *ctx = req->ctx;
4924 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07004925 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07004926
Jens Axboe78076bb2019-12-04 19:56:40 -07004927 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe36703242019-07-25 10:20:18 -06004928 INIT_LIST_HEAD(&req->list);
Jens Axboed7718a92020-02-14 22:23:12 -07004929 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06004930
Jens Axboed7718a92020-02-14 22:23:12 -07004931 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
4932 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004933
Jens Axboe8c838782019-03-12 15:48:16 -06004934 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06004935 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004936 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06004937 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07004938 spin_unlock_irq(&ctx->completion_lock);
4939
Jens Axboe8c838782019-03-12 15:48:16 -06004940 if (mask) {
4941 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03004942 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004943 }
Jens Axboe8c838782019-03-12 15:48:16 -06004944 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004945}
4946
Jens Axboe5262f562019-09-17 12:26:57 -06004947static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
4948{
Jens Axboead8a48a2019-11-15 08:49:11 -07004949 struct io_timeout_data *data = container_of(timer,
4950 struct io_timeout_data, timer);
4951 struct io_kiocb *req = data->req;
4952 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06004953 unsigned long flags;
4954
Jens Axboe5262f562019-09-17 12:26:57 -06004955 atomic_inc(&ctx->cq_timeouts);
4956
4957 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08004958 /*
Jens Axboe11365042019-10-16 09:08:32 -06004959 * We could be racing with timeout deletion. If the list is empty,
4960 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08004961 */
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03004962 if (!list_empty(&req->list))
Jens Axboe11365042019-10-16 09:08:32 -06004963 list_del_init(&req->list);
Jens Axboe842f9612019-10-29 12:34:10 -06004964
Jens Axboe78e19bb2019-11-06 15:21:34 -07004965 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06004966 io_commit_cqring(ctx);
4967 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4968
4969 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004970 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004971 io_put_req(req);
4972 return HRTIMER_NORESTART;
4973}
4974
Jens Axboe47f46762019-11-09 17:43:02 -07004975static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
4976{
4977 struct io_kiocb *req;
4978 int ret = -ENOENT;
4979
4980 list_for_each_entry(req, &ctx->timeout_list, list) {
4981 if (user_data == req->user_data) {
4982 list_del_init(&req->list);
4983 ret = 0;
4984 break;
4985 }
4986 }
4987
4988 if (ret == -ENOENT)
4989 return ret;
4990
Jens Axboe2d283902019-12-04 11:08:05 -07004991 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07004992 if (ret == -1)
4993 return -EALREADY;
4994
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004995 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004996 io_cqring_fill_event(req, -ECANCELED);
4997 io_put_req(req);
4998 return 0;
4999}
5000
Jens Axboe3529d8c2019-12-19 18:24:38 -07005001static int io_timeout_remove_prep(struct io_kiocb *req,
5002 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005003{
Jens Axboeb29472e2019-12-17 18:50:29 -07005004 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5005 return -EINVAL;
5006 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
5007 return -EINVAL;
5008
5009 req->timeout.addr = READ_ONCE(sqe->addr);
5010 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
5011 if (req->timeout.flags)
5012 return -EINVAL;
5013
Jens Axboeb29472e2019-12-17 18:50:29 -07005014 return 0;
5015}
5016
Jens Axboe11365042019-10-16 09:08:32 -06005017/*
5018 * Remove or update an existing timeout command
5019 */
Jens Axboefc4df992019-12-10 14:38:45 -07005020static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06005021{
5022 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005023 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005024
Jens Axboe11365042019-10-16 09:08:32 -06005025 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07005026 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06005027
Jens Axboe47f46762019-11-09 17:43:02 -07005028 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005029 io_commit_cqring(ctx);
5030 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005031 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005032 if (ret < 0)
5033 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005034 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005035 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005036}
5037
Jens Axboe3529d8c2019-12-19 18:24:38 -07005038static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005039 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005040{
Jens Axboead8a48a2019-11-15 08:49:11 -07005041 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005042 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005043 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005044
Jens Axboead8a48a2019-11-15 08:49:11 -07005045 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005046 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005047 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005048 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005049 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005050 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005051 flags = READ_ONCE(sqe->timeout_flags);
5052 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005053 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005054
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005055 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005056
Jens Axboe3529d8c2019-12-19 18:24:38 -07005057 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005058 return -ENOMEM;
5059
5060 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005061 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005062 req->flags |= REQ_F_TIMEOUT;
5063
5064 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005065 return -EFAULT;
5066
Jens Axboe11365042019-10-16 09:08:32 -06005067 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07005068 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06005069 else
Jens Axboead8a48a2019-11-15 08:49:11 -07005070 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06005071
Jens Axboead8a48a2019-11-15 08:49:11 -07005072 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5073 return 0;
5074}
5075
Jens Axboefc4df992019-12-10 14:38:45 -07005076static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07005077{
Jens Axboead8a48a2019-11-15 08:49:11 -07005078 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005079 struct io_timeout_data *data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005080 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005081 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005082
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005083 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005084
Jens Axboe5262f562019-09-17 12:26:57 -06005085 /*
5086 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005087 * timeout event to be satisfied. If it isn't set, then this is
5088 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005089 */
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005090 if (!off) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005091 req->flags |= REQ_F_TIMEOUT_NOSEQ;
Jens Axboe93bd25b2019-11-11 23:34:31 -07005092 entry = ctx->timeout_list.prev;
5093 goto add;
5094 }
Jens Axboe5262f562019-09-17 12:26:57 -06005095
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005096 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5097 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005098
5099 /*
5100 * Insertion sort, ensuring the first entry in the list is always
5101 * the one we need first.
5102 */
Jens Axboe5262f562019-09-17 12:26:57 -06005103 list_for_each_prev(entry, &ctx->timeout_list) {
5104 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
Jens Axboe5262f562019-09-17 12:26:57 -06005105
Jens Axboe93bd25b2019-11-11 23:34:31 -07005106 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
5107 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005108 /* nxt.seq is behind @tail, otherwise would've been completed */
5109 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005110 break;
5111 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005112add:
Jens Axboe5262f562019-09-17 12:26:57 -06005113 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005114 data->timer.function = io_timeout_fn;
5115 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005116 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005117 return 0;
5118}
5119
Jens Axboe62755e32019-10-28 21:49:21 -06005120static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005121{
Jens Axboe62755e32019-10-28 21:49:21 -06005122 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005123
Jens Axboe62755e32019-10-28 21:49:21 -06005124 return req->user_data == (unsigned long) data;
5125}
5126
Jens Axboee977d6d2019-11-05 12:39:45 -07005127static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005128{
Jens Axboe62755e32019-10-28 21:49:21 -06005129 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005130 int ret = 0;
5131
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005132 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005133 switch (cancel_ret) {
5134 case IO_WQ_CANCEL_OK:
5135 ret = 0;
5136 break;
5137 case IO_WQ_CANCEL_RUNNING:
5138 ret = -EALREADY;
5139 break;
5140 case IO_WQ_CANCEL_NOTFOUND:
5141 ret = -ENOENT;
5142 break;
5143 }
5144
Jens Axboee977d6d2019-11-05 12:39:45 -07005145 return ret;
5146}
5147
Jens Axboe47f46762019-11-09 17:43:02 -07005148static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5149 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005150 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005151{
5152 unsigned long flags;
5153 int ret;
5154
5155 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5156 if (ret != -ENOENT) {
5157 spin_lock_irqsave(&ctx->completion_lock, flags);
5158 goto done;
5159 }
5160
5161 spin_lock_irqsave(&ctx->completion_lock, flags);
5162 ret = io_timeout_cancel(ctx, sqe_addr);
5163 if (ret != -ENOENT)
5164 goto done;
5165 ret = io_poll_cancel(ctx, sqe_addr);
5166done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005167 if (!ret)
5168 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005169 io_cqring_fill_event(req, ret);
5170 io_commit_cqring(ctx);
5171 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5172 io_cqring_ev_posted(ctx);
5173
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005174 if (ret < 0)
5175 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005176 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005177}
5178
Jens Axboe3529d8c2019-12-19 18:24:38 -07005179static int io_async_cancel_prep(struct io_kiocb *req,
5180 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005181{
Jens Axboefbf23842019-12-17 18:45:56 -07005182 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005183 return -EINVAL;
5184 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
5185 sqe->cancel_flags)
5186 return -EINVAL;
5187
Jens Axboefbf23842019-12-17 18:45:56 -07005188 req->cancel.addr = READ_ONCE(sqe->addr);
5189 return 0;
5190}
5191
Pavel Begunkov014db002020-03-03 21:33:12 +03005192static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07005193{
5194 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005195
Pavel Begunkov014db002020-03-03 21:33:12 +03005196 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005197 return 0;
5198}
5199
Jens Axboe05f3fb32019-12-09 11:22:50 -07005200static int io_files_update_prep(struct io_kiocb *req,
5201 const struct io_uring_sqe *sqe)
5202{
5203 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
5204 return -EINVAL;
5205
5206 req->files_update.offset = READ_ONCE(sqe->off);
5207 req->files_update.nr_args = READ_ONCE(sqe->len);
5208 if (!req->files_update.nr_args)
5209 return -EINVAL;
5210 req->files_update.arg = READ_ONCE(sqe->addr);
5211 return 0;
5212}
5213
Jens Axboe229a7b62020-06-22 10:13:11 -06005214static int io_files_update(struct io_kiocb *req, bool force_nonblock,
5215 struct io_comp_state *cs)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005216{
5217 struct io_ring_ctx *ctx = req->ctx;
5218 struct io_uring_files_update up;
5219 int ret;
5220
Jens Axboef86cd202020-01-29 13:46:44 -07005221 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005222 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005223
5224 up.offset = req->files_update.offset;
5225 up.fds = req->files_update.arg;
5226
5227 mutex_lock(&ctx->uring_lock);
5228 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
5229 mutex_unlock(&ctx->uring_lock);
5230
5231 if (ret < 0)
5232 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005233 __io_req_complete(req, ret, 0, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005234 return 0;
5235}
5236
Jens Axboe3529d8c2019-12-19 18:24:38 -07005237static int io_req_defer_prep(struct io_kiocb *req,
Jens Axboec40f6372020-06-25 15:39:59 -06005238 const struct io_uring_sqe *sqe, bool for_async)
Jens Axboef67676d2019-12-02 11:03:47 -07005239{
Jens Axboee7815732019-12-17 19:45:06 -07005240 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005241
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03005242 if (!sqe)
5243 return 0;
5244
Pavel Begunkov710c2bf2020-06-27 14:04:58 +03005245 if (io_op_defs[req->opcode].file_table) {
5246 io_req_init_async(req);
5247 ret = io_grab_files(req);
5248 if (unlikely(ret))
5249 return ret;
5250 }
5251
Jens Axboec40f6372020-06-25 15:39:59 -06005252 if (for_async || (req->flags & REQ_F_WORK_INITIALIZED)) {
5253 io_req_init_async(req);
Jens Axboec40f6372020-06-25 15:39:59 -06005254 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
Jens Axboef86cd202020-01-29 13:46:44 -07005255 }
5256
Jens Axboed625c6e2019-12-17 19:53:05 -07005257 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005258 case IORING_OP_NOP:
5259 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005260 case IORING_OP_READV:
5261 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005262 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005263 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005264 break;
5265 case IORING_OP_WRITEV:
5266 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005267 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005268 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005269 break;
Jens Axboe0969e782019-12-17 18:40:57 -07005270 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005271 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005272 break;
5273 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005274 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005275 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005276 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005277 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005278 break;
5279 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005280 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005281 break;
Jens Axboe03b12302019-12-02 18:50:25 -07005282 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005283 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005284 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005285 break;
5286 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005287 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005288 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005289 break;
Jens Axboef499a022019-12-02 16:28:46 -07005290 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005291 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005292 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005293 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005294 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005295 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07005296 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005297 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07005298 break;
Jens Axboefbf23842019-12-17 18:45:56 -07005299 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005300 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005301 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005302 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005303 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005304 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005305 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005306 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005307 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005308 case IORING_OP_FALLOCATE:
5309 ret = io_fallocate_prep(req, sqe);
5310 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005311 case IORING_OP_OPENAT:
5312 ret = io_openat_prep(req, sqe);
5313 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005314 case IORING_OP_CLOSE:
5315 ret = io_close_prep(req, sqe);
5316 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005317 case IORING_OP_FILES_UPDATE:
5318 ret = io_files_update_prep(req, sqe);
5319 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005320 case IORING_OP_STATX:
5321 ret = io_statx_prep(req, sqe);
5322 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005323 case IORING_OP_FADVISE:
5324 ret = io_fadvise_prep(req, sqe);
5325 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005326 case IORING_OP_MADVISE:
5327 ret = io_madvise_prep(req, sqe);
5328 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005329 case IORING_OP_OPENAT2:
5330 ret = io_openat2_prep(req, sqe);
5331 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005332 case IORING_OP_EPOLL_CTL:
5333 ret = io_epoll_ctl_prep(req, sqe);
5334 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005335 case IORING_OP_SPLICE:
5336 ret = io_splice_prep(req, sqe);
5337 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005338 case IORING_OP_PROVIDE_BUFFERS:
5339 ret = io_provide_buffers_prep(req, sqe);
5340 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005341 case IORING_OP_REMOVE_BUFFERS:
5342 ret = io_remove_buffers_prep(req, sqe);
5343 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005344 case IORING_OP_TEE:
5345 ret = io_tee_prep(req, sqe);
5346 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005347 default:
Jens Axboee7815732019-12-17 19:45:06 -07005348 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5349 req->opcode);
5350 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005351 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005352 }
5353
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005354 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07005355}
5356
Jens Axboe3529d8c2019-12-19 18:24:38 -07005357static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06005358{
Jackie Liua197f662019-11-08 08:09:12 -07005359 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07005360 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06005361
Bob Liu9d858b22019-11-13 18:06:25 +08005362 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov4ee36312020-05-01 17:09:37 +03005363 if (!req_need_defer(req) && list_empty_careful(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06005364 return 0;
5365
Pavel Begunkov650b5482020-05-17 14:02:11 +03005366 if (!req->io) {
5367 if (io_alloc_async_ctx(req))
5368 return -EAGAIN;
Jens Axboec40f6372020-06-25 15:39:59 -06005369 ret = io_req_defer_prep(req, sqe, true);
Pavel Begunkov650b5482020-05-17 14:02:11 +03005370 if (ret < 0)
5371 return ret;
5372 }
Jens Axboe2d283902019-12-04 11:08:05 -07005373
Jens Axboede0617e2019-04-06 21:51:27 -06005374 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08005375 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06005376 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06005377 return 0;
5378 }
5379
Jens Axboe915967f2019-11-21 09:01:20 -07005380 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06005381 list_add_tail(&req->list, &ctx->defer_list);
5382 spin_unlock_irq(&ctx->completion_lock);
5383 return -EIOCBQUEUED;
5384}
5385
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005386static void io_cleanup_req(struct io_kiocb *req)
5387{
5388 struct io_async_ctx *io = req->io;
5389
5390 switch (req->opcode) {
5391 case IORING_OP_READV:
5392 case IORING_OP_READ_FIXED:
5393 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005394 if (req->flags & REQ_F_BUFFER_SELECTED)
5395 kfree((void *)(unsigned long)req->rw.addr);
5396 /* fallthrough */
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005397 case IORING_OP_WRITEV:
5398 case IORING_OP_WRITE_FIXED:
5399 case IORING_OP_WRITE:
5400 if (io->rw.iov != io->rw.fast_iov)
5401 kfree(io->rw.iov);
5402 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005403 case IORING_OP_RECVMSG:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005404 if (req->flags & REQ_F_BUFFER_SELECTED)
5405 kfree(req->sr_msg.kbuf);
5406 /* fallthrough */
5407 case IORING_OP_SENDMSG:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005408 if (io->msg.iov != io->msg.fast_iov)
5409 kfree(io->msg.iov);
5410 break;
Jens Axboebcda7ba2020-02-23 16:42:51 -07005411 case IORING_OP_RECV:
5412 if (req->flags & REQ_F_BUFFER_SELECTED)
5413 kfree(req->sr_msg.kbuf);
5414 break;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03005415 case IORING_OP_OPENAT:
5416 case IORING_OP_OPENAT2:
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03005417 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005418 case IORING_OP_SPLICE:
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005419 case IORING_OP_TEE:
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005420 io_put_file(req, req->splice.file_in,
5421 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5422 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005423 }
5424
5425 req->flags &= ~REQ_F_NEED_CLEANUP;
5426}
5427
Jens Axboe3529d8c2019-12-19 18:24:38 -07005428static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06005429 bool force_nonblock, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005430{
Jackie Liua197f662019-11-08 08:09:12 -07005431 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005432 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005433
Jens Axboed625c6e2019-12-17 19:53:05 -07005434 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005435 case IORING_OP_NOP:
Jens Axboe229a7b62020-06-22 10:13:11 -06005436 ret = io_nop(req, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005437 break;
5438 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005439 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005440 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005441 if (sqe) {
5442 ret = io_read_prep(req, sqe, force_nonblock);
5443 if (ret < 0)
5444 break;
5445 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005446 ret = io_read(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005447 break;
5448 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07005449 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005450 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005451 if (sqe) {
5452 ret = io_write_prep(req, sqe, force_nonblock);
5453 if (ret < 0)
5454 break;
5455 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005456 ret = io_write(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005457 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005458 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005459 if (sqe) {
5460 ret = io_prep_fsync(req, sqe);
5461 if (ret < 0)
5462 break;
5463 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005464 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005465 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005466 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005467 if (sqe) {
5468 ret = io_poll_add_prep(req, sqe);
5469 if (ret)
5470 break;
5471 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005472 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005473 break;
5474 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005475 if (sqe) {
5476 ret = io_poll_remove_prep(req, sqe);
5477 if (ret < 0)
5478 break;
5479 }
Jens Axboefc4df992019-12-10 14:38:45 -07005480 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005481 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005482 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005483 if (sqe) {
5484 ret = io_prep_sfr(req, sqe);
5485 if (ret < 0)
5486 break;
5487 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005488 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005489 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005490 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005491 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005492 if (sqe) {
5493 ret = io_sendmsg_prep(req, sqe);
5494 if (ret < 0)
5495 break;
5496 }
Jens Axboefddafac2020-01-04 20:19:44 -07005497 if (req->opcode == IORING_OP_SENDMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005498 ret = io_sendmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005499 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005500 ret = io_send(req, force_nonblock, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005501 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06005502 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005503 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005504 if (sqe) {
5505 ret = io_recvmsg_prep(req, sqe);
5506 if (ret)
5507 break;
5508 }
Jens Axboefddafac2020-01-04 20:19:44 -07005509 if (req->opcode == IORING_OP_RECVMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005510 ret = io_recvmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005511 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005512 ret = io_recv(req, force_nonblock, cs);
Jens Axboeaa1fa282019-04-19 13:38:09 -06005513 break;
Jens Axboe5262f562019-09-17 12:26:57 -06005514 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005515 if (sqe) {
5516 ret = io_timeout_prep(req, sqe, false);
5517 if (ret)
5518 break;
5519 }
Jens Axboefc4df992019-12-10 14:38:45 -07005520 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005521 break;
Jens Axboe11365042019-10-16 09:08:32 -06005522 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005523 if (sqe) {
5524 ret = io_timeout_remove_prep(req, sqe);
5525 if (ret)
5526 break;
5527 }
Jens Axboefc4df992019-12-10 14:38:45 -07005528 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06005529 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06005530 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005531 if (sqe) {
5532 ret = io_accept_prep(req, sqe);
5533 if (ret)
5534 break;
5535 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005536 ret = io_accept(req, force_nonblock, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005537 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005538 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005539 if (sqe) {
5540 ret = io_connect_prep(req, sqe);
5541 if (ret)
5542 break;
5543 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005544 ret = io_connect(req, force_nonblock, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005545 break;
Jens Axboe62755e32019-10-28 21:49:21 -06005546 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005547 if (sqe) {
5548 ret = io_async_cancel_prep(req, sqe);
5549 if (ret)
5550 break;
5551 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005552 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005553 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005554 case IORING_OP_FALLOCATE:
5555 if (sqe) {
5556 ret = io_fallocate_prep(req, sqe);
5557 if (ret)
5558 break;
5559 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005560 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005561 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005562 case IORING_OP_OPENAT:
5563 if (sqe) {
5564 ret = io_openat_prep(req, sqe);
5565 if (ret)
5566 break;
5567 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005568 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005569 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005570 case IORING_OP_CLOSE:
5571 if (sqe) {
5572 ret = io_close_prep(req, sqe);
5573 if (ret)
5574 break;
5575 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005576 ret = io_close(req, force_nonblock, cs);
Jens Axboeb5dba592019-12-11 14:02:38 -07005577 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005578 case IORING_OP_FILES_UPDATE:
5579 if (sqe) {
5580 ret = io_files_update_prep(req, sqe);
5581 if (ret)
5582 break;
5583 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005584 ret = io_files_update(req, force_nonblock, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005585 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005586 case IORING_OP_STATX:
5587 if (sqe) {
5588 ret = io_statx_prep(req, sqe);
5589 if (ret)
5590 break;
5591 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005592 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005593 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005594 case IORING_OP_FADVISE:
5595 if (sqe) {
5596 ret = io_fadvise_prep(req, sqe);
5597 if (ret)
5598 break;
5599 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005600 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005601 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005602 case IORING_OP_MADVISE:
5603 if (sqe) {
5604 ret = io_madvise_prep(req, sqe);
5605 if (ret)
5606 break;
5607 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005608 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005609 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005610 case IORING_OP_OPENAT2:
5611 if (sqe) {
5612 ret = io_openat2_prep(req, sqe);
5613 if (ret)
5614 break;
5615 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005616 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005617 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005618 case IORING_OP_EPOLL_CTL:
5619 if (sqe) {
5620 ret = io_epoll_ctl_prep(req, sqe);
5621 if (ret)
5622 break;
5623 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005624 ret = io_epoll_ctl(req, force_nonblock, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005625 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005626 case IORING_OP_SPLICE:
5627 if (sqe) {
5628 ret = io_splice_prep(req, sqe);
5629 if (ret < 0)
5630 break;
5631 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005632 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005633 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005634 case IORING_OP_PROVIDE_BUFFERS:
5635 if (sqe) {
5636 ret = io_provide_buffers_prep(req, sqe);
5637 if (ret)
5638 break;
5639 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005640 ret = io_provide_buffers(req, force_nonblock, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07005641 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005642 case IORING_OP_REMOVE_BUFFERS:
5643 if (sqe) {
5644 ret = io_remove_buffers_prep(req, sqe);
5645 if (ret)
5646 break;
5647 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005648 ret = io_remove_buffers(req, force_nonblock, cs);
Jens Axboe31b51512019-01-18 22:56:34 -07005649 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005650 case IORING_OP_TEE:
5651 if (sqe) {
5652 ret = io_tee_prep(req, sqe);
5653 if (ret < 0)
5654 break;
5655 }
5656 ret = io_tee(req, force_nonblock);
5657 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005658 default:
5659 ret = -EINVAL;
5660 break;
5661 }
5662
5663 if (ret)
5664 return ret;
5665
Jens Axboeb5325762020-05-19 21:20:27 -06005666 /* If the op doesn't have a file, we're not polling for it */
5667 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07005668 const bool in_async = io_wq_current_is_worker();
5669
Jens Axboe11ba8202020-01-15 21:51:17 -07005670 /* workqueue context doesn't hold uring_lock, grab it now */
5671 if (in_async)
5672 mutex_lock(&ctx->uring_lock);
5673
Jens Axboe2b188cc2019-01-07 10:46:33 -07005674 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07005675
5676 if (in_async)
5677 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07005678 }
5679
5680 return 0;
5681}
5682
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03005683static void io_arm_async_linked_timeout(struct io_kiocb *req)
5684{
5685 struct io_kiocb *link;
5686
5687 /* link head's timeout is queued in io_queue_async_work() */
5688 if (!(req->flags & REQ_F_QUEUE_TIMEOUT))
5689 return;
5690
5691 link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
5692 io_queue_linked_timeout(link);
5693}
5694
Pavel Begunkovf4db7182020-06-25 18:20:54 +03005695static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work)
Jens Axboedef596e2019-01-09 08:59:42 -07005696{
Jens Axboe2b188cc2019-01-07 10:46:33 -07005697 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06005698 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005699
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03005700 io_arm_async_linked_timeout(req);
5701
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005702 /* if NO_CANCEL is set, we must still run the work */
5703 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
5704 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06005705 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005706 }
Jens Axboe31b51512019-01-18 22:56:34 -07005707
Jens Axboe561fb042019-10-24 07:25:42 -06005708 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06005709 do {
Jens Axboef13fad72020-06-22 09:34:30 -06005710 ret = io_issue_sqe(req, NULL, false, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06005711 /*
5712 * We can get EAGAIN for polled IO even though we're
5713 * forcing a sync submission from here, since we can't
5714 * wait for request slots on the block side.
5715 */
5716 if (ret != -EAGAIN)
5717 break;
5718 cond_resched();
5719 } while (1);
5720 }
Jens Axboe31b51512019-01-18 22:56:34 -07005721
Jens Axboe561fb042019-10-24 07:25:42 -06005722 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005723 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005724 io_req_complete(req, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005725 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005726
Pavel Begunkovf4db7182020-06-25 18:20:54 +03005727 return io_steal_work(req);
Jens Axboe31b51512019-01-18 22:56:34 -07005728}
Jens Axboe2b188cc2019-01-07 10:46:33 -07005729
Jens Axboe65e19f52019-10-26 07:20:21 -06005730static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
5731 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06005732{
Jens Axboe65e19f52019-10-26 07:20:21 -06005733 struct fixed_file_table *table;
5734
Jens Axboe05f3fb32019-12-09 11:22:50 -07005735 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08005736 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06005737}
5738
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005739static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
5740 int fd, struct file **out_file, bool fixed)
5741{
5742 struct io_ring_ctx *ctx = req->ctx;
5743 struct file *file;
5744
5745 if (fixed) {
5746 if (unlikely(!ctx->file_data ||
5747 (unsigned) fd >= ctx->nr_user_files))
5748 return -EBADF;
5749 fd = array_index_nospec(fd, ctx->nr_user_files);
5750 file = io_file_from_index(ctx, fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06005751 if (file) {
5752 req->fixed_file_refs = ctx->file_data->cur_refs;
5753 percpu_ref_get(req->fixed_file_refs);
5754 }
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005755 } else {
5756 trace_io_uring_file_get(ctx, fd);
5757 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005758 }
5759
Jens Axboefd2206e2020-06-02 16:40:47 -06005760 if (file || io_op_defs[req->opcode].needs_file_no_error) {
5761 *out_file = file;
5762 return 0;
5763 }
5764 return -EBADF;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005765}
5766
Jens Axboe3529d8c2019-12-19 18:24:38 -07005767static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Jens Axboe63ff8222020-05-07 14:56:15 -06005768 int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06005769{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005770 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06005771
Jens Axboe63ff8222020-05-07 14:56:15 -06005772 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03005773 if (unlikely(!fixed && io_async_submit(req->ctx)))
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005774 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06005775
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005776 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06005777}
5778
Jackie Liua197f662019-11-08 08:09:12 -07005779static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005780{
Jens Axboefcb323c2019-10-24 12:39:47 -06005781 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07005782 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06005783
Jens Axboe5b0bbee2020-04-27 10:41:22 -06005784 if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE))
Jens Axboef86cd202020-01-29 13:46:44 -07005785 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005786 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07005787 return -EBADF;
5788
Jens Axboefcb323c2019-10-24 12:39:47 -06005789 rcu_read_lock();
5790 spin_lock_irq(&ctx->inflight_lock);
5791 /*
5792 * We use the f_ops->flush() handler to ensure that we can flush
5793 * out work accessing these files if the fd is closed. Check if
5794 * the fd has changed since we started down this path, and disallow
5795 * this operation if it has.
5796 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005797 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06005798 list_add(&req->inflight_entry, &ctx->inflight_list);
5799 req->flags |= REQ_F_INFLIGHT;
5800 req->work.files = current->files;
5801 ret = 0;
5802 }
5803 spin_unlock_irq(&ctx->inflight_lock);
5804 rcu_read_unlock();
5805
5806 return ret;
5807}
5808
Jens Axboe2665abf2019-11-05 12:40:47 -07005809static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
5810{
Jens Axboead8a48a2019-11-15 08:49:11 -07005811 struct io_timeout_data *data = container_of(timer,
5812 struct io_timeout_data, timer);
5813 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07005814 struct io_ring_ctx *ctx = req->ctx;
5815 struct io_kiocb *prev = NULL;
5816 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07005817
5818 spin_lock_irqsave(&ctx->completion_lock, flags);
5819
5820 /*
5821 * We don't expect the list to be empty, that will only happen if we
5822 * race with the completion of the linked work.
5823 */
Pavel Begunkov44932332019-12-05 16:16:35 +03005824 if (!list_empty(&req->link_list)) {
5825 prev = list_entry(req->link_list.prev, struct io_kiocb,
5826 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005827 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03005828 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005829 prev->flags &= ~REQ_F_LINK_TIMEOUT;
5830 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07005831 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005832 }
5833
5834 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5835
5836 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005837 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03005838 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07005839 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07005840 } else {
Jens Axboee1e16092020-06-22 09:17:17 -06005841 io_req_complete(req, -ETIME);
Jens Axboe2665abf2019-11-05 12:40:47 -07005842 }
Jens Axboe2665abf2019-11-05 12:40:47 -07005843 return HRTIMER_NORESTART;
5844}
5845
Jens Axboead8a48a2019-11-15 08:49:11 -07005846static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005847{
Jens Axboe76a46e02019-11-10 23:34:16 -07005848 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07005849
Jens Axboe76a46e02019-11-10 23:34:16 -07005850 /*
5851 * If the list is now empty, then our linked request finished before
5852 * we got a chance to setup the timer
5853 */
5854 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03005855 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07005856 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07005857
Jens Axboead8a48a2019-11-15 08:49:11 -07005858 data->timer.function = io_link_timeout_fn;
5859 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
5860 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07005861 }
Jens Axboe76a46e02019-11-10 23:34:16 -07005862 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07005863
Jens Axboe2665abf2019-11-05 12:40:47 -07005864 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07005865 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005866}
5867
Jens Axboead8a48a2019-11-15 08:49:11 -07005868static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005869{
5870 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005871
Pavel Begunkovdea3b492020-04-12 02:05:04 +03005872 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07005873 return NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005874 /* for polled retry, if flag is set, we already went through here */
5875 if (req->flags & REQ_F_POLLED)
5876 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005877
Pavel Begunkov44932332019-12-05 16:16:35 +03005878 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
5879 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07005880 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07005881 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005882
Jens Axboe76a46e02019-11-10 23:34:16 -07005883 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07005884 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07005885}
5886
Jens Axboef13fad72020-06-22 09:34:30 -06005887static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
5888 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005889{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005890 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005891 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07005892 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005893 int ret;
5894
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005895again:
5896 linked_timeout = io_prep_linked_timeout(req);
5897
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08005898 if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds &&
5899 req->work.creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07005900 if (old_creds)
5901 revert_creds(old_creds);
5902 if (old_creds == req->work.creds)
5903 old_creds = NULL; /* restored original creds */
5904 else
5905 old_creds = override_creds(req->work.creds);
5906 }
5907
Jens Axboef13fad72020-06-22 09:34:30 -06005908 ret = io_issue_sqe(req, sqe, true, cs);
Jens Axboe491381ce2019-10-17 09:20:46 -06005909
5910 /*
5911 * We async punt it if the file wasn't marked NOWAIT, or if the file
5912 * doesn't support non-blocking read/write attempts
5913 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03005914 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Jens Axboed7718a92020-02-14 22:23:12 -07005915 if (io_arm_poll_handler(req)) {
5916 if (linked_timeout)
5917 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005918 goto exit;
Jens Axboed7718a92020-02-14 22:23:12 -07005919 }
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005920punt:
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08005921 io_req_init_async(req);
5922
Jens Axboef86cd202020-01-29 13:46:44 -07005923 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005924 ret = io_grab_files(req);
5925 if (ret)
5926 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005927 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005928
5929 /*
5930 * Queued up for async execution, worker will release
5931 * submit reference when the iocb is actually submitted.
5932 */
5933 io_queue_async_work(req);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005934 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005935 }
Jens Axboee65ef562019-03-12 10:16:44 -06005936
Jens Axboefcb323c2019-10-24 12:39:47 -06005937err:
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005938 nxt = NULL;
Jens Axboee65ef562019-03-12 10:16:44 -06005939 /* drop submission reference */
Jens Axboe2a44f462020-02-25 13:25:41 -07005940 io_put_req_find_next(req, &nxt);
Jens Axboee65ef562019-03-12 10:16:44 -06005941
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005942 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07005943 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005944 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005945 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005946 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005947 }
5948
Jens Axboee65ef562019-03-12 10:16:44 -06005949 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06005950 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005951 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005952 io_req_complete(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06005953 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005954 if (nxt) {
5955 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005956
5957 if (req->flags & REQ_F_FORCE_ASYNC)
5958 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005959 goto again;
5960 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005961exit:
Jens Axboe193155c2020-02-22 23:22:19 -07005962 if (old_creds)
5963 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005964}
5965
Jens Axboef13fad72020-06-22 09:34:30 -06005966static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
5967 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005968{
5969 int ret;
5970
Jens Axboe3529d8c2019-12-19 18:24:38 -07005971 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005972 if (ret) {
5973 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03005974fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005975 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005976 io_put_req(req);
5977 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005978 }
Pavel Begunkov25508782019-12-30 21:24:47 +03005979 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03005980 if (!req->io) {
5981 ret = -EAGAIN;
5982 if (io_alloc_async_ctx(req))
5983 goto fail_req;
Jens Axboec40f6372020-06-25 15:39:59 -06005984 ret = io_req_defer_prep(req, sqe, true);
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03005985 if (unlikely(ret < 0))
5986 goto fail_req;
5987 }
5988
Jens Axboece35a472019-12-17 08:04:44 -07005989 /*
5990 * Never try inline submit of IOSQE_ASYNC is set, go straight
5991 * to async execution.
5992 */
5993 req->work.flags |= IO_WQ_WORK_CONCURRENT;
5994 io_queue_async_work(req);
5995 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06005996 __io_queue_sqe(req, sqe, cs);
Jens Axboece35a472019-12-17 08:04:44 -07005997 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08005998}
5999
Jens Axboef13fad72020-06-22 09:34:30 -06006000static inline void io_queue_link_head(struct io_kiocb *req,
6001 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006002{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006003 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006004 io_put_req(req);
6005 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006006 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006007 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006008}
6009
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006010static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06006011 struct io_kiocb **link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006012{
Jackie Liua197f662019-11-08 08:09:12 -07006013 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006014 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006015
Jens Axboe9e645e112019-05-10 16:07:28 -06006016 /*
6017 * If we already have a head request, queue this one for async
6018 * submittal once the head completes. If we don't have a head but
6019 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6020 * submitted sync once the chain is complete. If none of those
6021 * conditions are true (normal request), then just queue it.
6022 */
6023 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03006024 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006025
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006026 /*
6027 * Taking sequential execution of a link, draining both sides
6028 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6029 * requests in the link. So, it drains the head and the
6030 * next after the link request. The last one is done via
6031 * drain_next flag to persist the effect across calls.
6032 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006033 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006034 head->flags |= REQ_F_IO_DRAIN;
6035 ctx->drain_next = 1;
6036 }
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006037 if (io_alloc_async_ctx(req))
6038 return -EAGAIN;
Jens Axboe9e645e112019-05-10 16:07:28 -06006039
Jens Axboec40f6372020-06-25 15:39:59 -06006040 ret = io_req_defer_prep(req, sqe, false);
Jens Axboe2d283902019-12-04 11:08:05 -07006041 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006042 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006043 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006044 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006045 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006046 trace_io_uring_link(ctx, req, head);
Jens Axboec40f6372020-06-25 15:39:59 -06006047 io_get_req_task(req);
Pavel Begunkov9d763772019-12-17 02:22:07 +03006048 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06006049
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006050 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006051 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006052 io_queue_link_head(head, cs);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006053 *link = NULL;
6054 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006055 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006056 if (unlikely(ctx->drain_next)) {
6057 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006058 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006059 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006060 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006061 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03006062 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03006063
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006064 if (io_alloc_async_ctx(req))
6065 return -EAGAIN;
6066
Pavel Begunkova6d45dd2020-06-27 14:04:57 +03006067 ret = io_req_defer_prep(req, sqe, false);
Pavel Begunkov711be032020-01-17 03:57:59 +03006068 if (ret)
6069 req->flags |= REQ_F_FAIL_LINK;
6070 *link = req;
6071 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006072 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006073 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006074 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006075
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006076 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006077}
6078
Jens Axboe9a56a232019-01-09 09:06:50 -07006079/*
6080 * Batched submission is done, ensure local IO is flushed out.
6081 */
6082static void io_submit_state_end(struct io_submit_state *state)
6083{
Jens Axboef13fad72020-06-22 09:34:30 -06006084 if (!list_empty(&state->comp.list))
6085 io_submit_flush_completions(&state->comp);
Jens Axboe9a56a232019-01-09 09:06:50 -07006086 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006087 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07006088 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006089 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07006090}
6091
6092/*
6093 * Start submission side cache.
6094 */
6095static void io_submit_state_start(struct io_submit_state *state,
Jens Axboe013538b2020-06-22 09:29:15 -06006096 struct io_ring_ctx *ctx, unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006097{
6098 blk_start_plug(&state->plug);
Jens Axboeb63534c2020-06-04 11:28:00 -06006099#ifdef CONFIG_BLOCK
6100 state->plug.nowait = true;
6101#endif
Jens Axboe013538b2020-06-22 09:29:15 -06006102 state->comp.nr = 0;
6103 INIT_LIST_HEAD(&state->comp.list);
6104 state->comp.ctx = ctx;
Jens Axboe2579f912019-01-09 09:10:43 -07006105 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07006106 state->file = NULL;
6107 state->ios_left = max_ios;
6108}
6109
Jens Axboe2b188cc2019-01-07 10:46:33 -07006110static void io_commit_sqring(struct io_ring_ctx *ctx)
6111{
Hristo Venev75b28af2019-08-26 17:23:46 +00006112 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006113
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006114 /*
6115 * Ensure any loads from the SQEs are done at this point,
6116 * since once we write the new head, the application could
6117 * write new data to them.
6118 */
6119 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006120}
6121
6122/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006123 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006124 * that is mapped by userspace. This means that care needs to be taken to
6125 * ensure that reads are stable, as we cannot rely on userspace always
6126 * being a good citizen. If members of the sqe are validated and then later
6127 * used, it's important that those reads are done through READ_ONCE() to
6128 * prevent a re-load down the line.
6129 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006130static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006131{
Hristo Venev75b28af2019-08-26 17:23:46 +00006132 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006133 unsigned head;
6134
6135 /*
6136 * The cached sq head (or cq tail) serves two purposes:
6137 *
6138 * 1) allows us to batch the cost of updating the user visible
6139 * head updates.
6140 * 2) allows the kernel side to track the head on its own, even
6141 * though the application is the one updating it.
6142 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006143 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006144 if (likely(head < ctx->sq_entries))
6145 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006146
6147 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006148 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006149 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006150 return NULL;
6151}
6152
6153static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6154{
6155 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006156}
6157
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006158#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6159 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6160 IOSQE_BUFFER_SELECT)
6161
6162static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6163 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006164 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006165{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006166 unsigned int sqe_flags;
Jens Axboe63ff8222020-05-07 14:56:15 -06006167 int id;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006168
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006169 /*
6170 * All io need record the previous position, if LINK vs DARIN,
6171 * it can be used to mark the position of the first IO in the
6172 * link list.
6173 */
Pavel Begunkov31af27c2020-04-15 00:39:50 +03006174 req->sequence = ctx->cached_sq_head - ctx->cached_sq_dropped;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006175 req->opcode = READ_ONCE(sqe->opcode);
6176 req->user_data = READ_ONCE(sqe->user_data);
6177 req->io = NULL;
6178 req->file = NULL;
6179 req->ctx = ctx;
6180 req->flags = 0;
6181 /* one is dropped after submission, the other at completion */
6182 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006183 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006184 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006185
6186 if (unlikely(req->opcode >= IORING_OP_LAST))
6187 return -EINVAL;
6188
Jens Axboe9d8426a2020-06-16 18:42:49 -06006189 if (unlikely(io_sq_thread_acquire_mm(ctx, req)))
6190 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006191
6192 sqe_flags = READ_ONCE(sqe->flags);
6193 /* enforce forwards compatibility on users */
6194 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6195 return -EINVAL;
6196
6197 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6198 !io_op_defs[req->opcode].buffer_select)
6199 return -EOPNOTSUPP;
6200
6201 id = READ_ONCE(sqe->personality);
6202 if (id) {
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006203 io_req_init_async(req);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006204 req->work.creds = idr_find(&ctx->personality_idr, id);
6205 if (unlikely(!req->work.creds))
6206 return -EINVAL;
6207 get_cred(req->work.creds);
6208 }
6209
6210 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006211 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006212
Jens Axboe63ff8222020-05-07 14:56:15 -06006213 if (!io_op_defs[req->opcode].needs_file)
6214 return 0;
6215
6216 return io_req_set_file(state, req, READ_ONCE(sqe->fd));
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006217}
6218
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006219static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006220 struct file *ring_file, int ring_fd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006221{
Jens Axboeac8691c2020-06-01 08:30:41 -06006222 struct io_submit_state state;
Jens Axboe9e645e112019-05-10 16:07:28 -06006223 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06006224 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006225
Jens Axboec4a2ed72019-11-21 21:01:26 -07006226 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006227 if (test_bit(0, &ctx->sq_check_overflow)) {
6228 if (!list_empty(&ctx->cq_overflow_list) &&
6229 !io_cqring_overflow_flush(ctx, false))
6230 return -EBUSY;
6231 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006232
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006233 /* make sure SQ entry isn't read before tail */
6234 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006235
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006236 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6237 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006238
Jens Axboe013538b2020-06-22 09:29:15 -06006239 io_submit_state_start(&state, ctx, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006240
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006241 ctx->ring_fd = ring_fd;
6242 ctx->ring_file = ring_file;
6243
Jens Axboe6c271ce2019-01-10 11:22:30 -07006244 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006245 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006246 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006247 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006248
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006249 sqe = io_get_sqe(ctx);
6250 if (unlikely(!sqe)) {
6251 io_consume_sqe(ctx);
6252 break;
6253 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006254 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006255 if (unlikely(!req)) {
6256 if (!submitted)
6257 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006258 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006259 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006260
Jens Axboeac8691c2020-06-01 08:30:41 -06006261 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006262 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006263 /* will complete beyond this point, count as submitted */
6264 submitted++;
6265
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006266 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006267fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006268 io_put_req(req);
6269 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006270 break;
6271 }
6272
Jens Axboe354420f2020-01-08 18:55:15 -07006273 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006274 true, io_async_submit(ctx));
Jens Axboef13fad72020-06-22 09:34:30 -06006275 err = io_submit_sqe(req, sqe, &link, &state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006276 if (err)
6277 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006278 }
6279
Pavel Begunkov9466f432020-01-25 22:34:01 +03006280 if (unlikely(submitted != nr)) {
6281 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
6282
6283 percpu_ref_put_many(&ctx->refs, nr - ref_used);
6284 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006285 if (link)
Jens Axboef13fad72020-06-22 09:34:30 -06006286 io_queue_link_head(link, &state.comp);
Jens Axboeac8691c2020-06-01 08:30:41 -06006287 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006288
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006289 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6290 io_commit_sqring(ctx);
6291
Jens Axboe6c271ce2019-01-10 11:22:30 -07006292 return submitted;
6293}
6294
6295static int io_sq_thread(void *data)
6296{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006297 struct io_ring_ctx *ctx = data;
Jens Axboe181e4482019-11-25 08:52:30 -07006298 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006299 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006300 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006301 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006302
Jens Axboe0f158b42020-05-14 17:18:39 -06006303 complete(&ctx->sq_thread_comp);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08006304
Jens Axboe181e4482019-11-25 08:52:30 -07006305 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006306
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006307 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006308 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006309 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006310
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006311 if (!list_empty(&ctx->poll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006312 unsigned nr_events = 0;
6313
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006314 mutex_lock(&ctx->uring_lock);
6315 if (!list_empty(&ctx->poll_list))
6316 io_iopoll_getevents(ctx, &nr_events, 0);
6317 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07006318 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006319 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006320 }
6321
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006322 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006323
6324 /*
6325 * If submit got -EBUSY, flag us as needing the application
6326 * to enter the kernel to reap and flush events.
6327 */
Xuan Zhuob772f072020-06-23 19:34:06 +08006328 if (!to_submit || ret == -EBUSY || need_resched()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006329 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006330 * Drop cur_mm before scheduling, we can't hold it for
6331 * long periods (or over schedule()). Do this before
6332 * adding ourselves to the waitqueue, as the unuse/drop
6333 * may sleep.
6334 */
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03006335 io_sq_thread_drop_mm(ctx);
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006336
6337 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07006338 * We're polling. If we're within the defined idle
6339 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07006340 * to sleep. The exception is if we got EBUSY doing
6341 * more IO, we should wait for the application to
6342 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07006343 */
Xuan Zhuob772f072020-06-23 19:34:06 +08006344 if (!list_empty(&ctx->poll_list) || need_resched() ||
Jens Axboedf069d82020-02-04 16:48:34 -07006345 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6346 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboeb41e9852020-02-17 09:52:41 -07006347 if (current->task_works)
6348 task_work_run();
Jens Axboe9831a902019-09-19 09:48:55 -06006349 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006350 continue;
6351 }
6352
Jens Axboe6c271ce2019-01-10 11:22:30 -07006353 prepare_to_wait(&ctx->sqo_wait, &wait,
6354 TASK_INTERRUPTIBLE);
6355
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006356 /*
6357 * While doing polled IO, before going to sleep, we need
6358 * to check if there are new reqs added to poll_list, it
6359 * is because reqs may have been punted to io worker and
6360 * will be added to poll_list later, hence check the
6361 * poll_list again.
6362 */
6363 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6364 !list_empty_careful(&ctx->poll_list)) {
6365 finish_wait(&ctx->sqo_wait, &wait);
6366 continue;
6367 }
6368
Jens Axboe6c271ce2019-01-10 11:22:30 -07006369 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00006370 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02006371 /* make sure to read SQ tail after writing flags */
6372 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006373
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006374 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006375 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006376 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006377 finish_wait(&ctx->sqo_wait, &wait);
6378 break;
6379 }
Jens Axboeb41e9852020-02-17 09:52:41 -07006380 if (current->task_works) {
6381 task_work_run();
Hillf Danton10bea962020-04-01 17:19:33 +08006382 finish_wait(&ctx->sqo_wait, &wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07006383 continue;
6384 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006385 if (signal_pending(current))
6386 flush_signals(current);
6387 schedule();
6388 finish_wait(&ctx->sqo_wait, &wait);
6389
Hristo Venev75b28af2019-08-26 17:23:46 +00006390 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Xiaoguang Wangd4ae2712020-05-20 21:24:35 +08006391 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006392 continue;
6393 }
6394 finish_wait(&ctx->sqo_wait, &wait);
6395
Hristo Venev75b28af2019-08-26 17:23:46 +00006396 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006397 }
6398
Jens Axboe8a4955f2019-12-09 14:52:35 -07006399 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang6b668c92020-05-20 15:35:03 +08006400 if (likely(!percpu_ref_is_dying(&ctx->refs)))
6401 ret = io_submit_sqes(ctx, to_submit, NULL, -1);
Jens Axboe8a4955f2019-12-09 14:52:35 -07006402 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006403 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006404 }
6405
Jens Axboeb41e9852020-02-17 09:52:41 -07006406 if (current->task_works)
6407 task_work_run();
6408
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03006409 io_sq_thread_drop_mm(ctx);
Jens Axboe181e4482019-11-25 08:52:30 -07006410 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006411
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006412 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006413
Jens Axboe6c271ce2019-01-10 11:22:30 -07006414 return 0;
6415}
6416
Jens Axboebda52162019-09-24 13:47:15 -06006417struct io_wait_queue {
6418 struct wait_queue_entry wq;
6419 struct io_ring_ctx *ctx;
6420 unsigned to_wait;
6421 unsigned nr_timeouts;
6422};
6423
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006424static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06006425{
6426 struct io_ring_ctx *ctx = iowq->ctx;
6427
6428 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006429 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006430 * started waiting. For timeouts, we always want to return to userspace,
6431 * regardless of event count.
6432 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006433 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006434 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6435}
6436
6437static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6438 int wake_flags, void *key)
6439{
6440 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6441 wq);
6442
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006443 /* use noflush == true, as we can't safely rely on locking context */
6444 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06006445 return -1;
6446
6447 return autoremove_wake_function(curr, mode, wake_flags, key);
6448}
6449
Jens Axboe2b188cc2019-01-07 10:46:33 -07006450/*
6451 * Wait until events become available, if we don't already have some. The
6452 * application must reap them itself, as they reside on the shared cq ring.
6453 */
6454static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6455 const sigset_t __user *sig, size_t sigsz)
6456{
Jens Axboebda52162019-09-24 13:47:15 -06006457 struct io_wait_queue iowq = {
6458 .wq = {
6459 .private = current,
6460 .func = io_wake_function,
6461 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6462 },
6463 .ctx = ctx,
6464 .to_wait = min_events,
6465 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006466 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006467 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006468
Jens Axboeb41e9852020-02-17 09:52:41 -07006469 do {
6470 if (io_cqring_events(ctx, false) >= min_events)
6471 return 0;
6472 if (!current->task_works)
6473 break;
6474 task_work_run();
6475 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006476
6477 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006478#ifdef CONFIG_COMPAT
6479 if (in_compat_syscall())
6480 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006481 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006482 else
6483#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006484 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006485
Jens Axboe2b188cc2019-01-07 10:46:33 -07006486 if (ret)
6487 return ret;
6488 }
6489
Jens Axboebda52162019-09-24 13:47:15 -06006490 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006491 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006492 do {
6493 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6494 TASK_INTERRUPTIBLE);
Jens Axboeb41e9852020-02-17 09:52:41 -07006495 if (current->task_works)
6496 task_work_run();
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006497 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06006498 break;
6499 schedule();
6500 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006501 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06006502 break;
6503 }
6504 } while (1);
6505 finish_wait(&ctx->wait, &iowq.wq);
6506
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006507 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006508
Hristo Venev75b28af2019-08-26 17:23:46 +00006509 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006510}
6511
Jens Axboe6b063142019-01-10 22:13:58 -07006512static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6513{
6514#if defined(CONFIG_UNIX)
6515 if (ctx->ring_sock) {
6516 struct sock *sock = ctx->ring_sock->sk;
6517 struct sk_buff *skb;
6518
6519 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6520 kfree_skb(skb);
6521 }
6522#else
6523 int i;
6524
Jens Axboe65e19f52019-10-26 07:20:21 -06006525 for (i = 0; i < ctx->nr_user_files; i++) {
6526 struct file *file;
6527
6528 file = io_file_from_index(ctx, i);
6529 if (file)
6530 fput(file);
6531 }
Jens Axboe6b063142019-01-10 22:13:58 -07006532#endif
6533}
6534
Jens Axboe05f3fb32019-12-09 11:22:50 -07006535static void io_file_ref_kill(struct percpu_ref *ref)
6536{
6537 struct fixed_file_data *data;
6538
6539 data = container_of(ref, struct fixed_file_data, refs);
6540 complete(&data->done);
6541}
6542
Jens Axboe6b063142019-01-10 22:13:58 -07006543static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6544{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006545 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006546 struct fixed_file_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006547 unsigned nr_tables, i;
6548
Jens Axboe05f3fb32019-12-09 11:22:50 -07006549 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006550 return -ENXIO;
6551
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006552 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006553 if (!list_empty(&data->ref_list))
6554 ref_node = list_first_entry(&data->ref_list,
6555 struct fixed_file_ref_node, node);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006556 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006557 if (ref_node)
6558 percpu_ref_kill(&ref_node->refs);
6559
6560 percpu_ref_kill(&data->refs);
6561
6562 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06006563 flush_delayed_work(&ctx->file_put_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07006564 wait_for_completion(&data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006565
Jens Axboe6b063142019-01-10 22:13:58 -07006566 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06006567 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6568 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006569 kfree(data->table[i].files);
6570 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006571 percpu_ref_exit(&data->refs);
6572 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006573 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006574 ctx->nr_user_files = 0;
6575 return 0;
6576}
6577
Jens Axboe6c271ce2019-01-10 11:22:30 -07006578static void io_sq_thread_stop(struct io_ring_ctx *ctx)
6579{
6580 if (ctx->sqo_thread) {
Jens Axboe0f158b42020-05-14 17:18:39 -06006581 wait_for_completion(&ctx->sq_thread_comp);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006582 /*
6583 * The park is a bit of a work-around, without it we get
6584 * warning spews on shutdown with SQPOLL set and affinity
6585 * set to a single CPU.
6586 */
Jens Axboe06058632019-04-13 09:26:03 -06006587 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006588 kthread_stop(ctx->sqo_thread);
6589 ctx->sqo_thread = NULL;
6590 }
6591}
6592
Jens Axboe6b063142019-01-10 22:13:58 -07006593static void io_finish_async(struct io_ring_ctx *ctx)
6594{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006595 io_sq_thread_stop(ctx);
6596
Jens Axboe561fb042019-10-24 07:25:42 -06006597 if (ctx->io_wq) {
6598 io_wq_destroy(ctx->io_wq);
6599 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006600 }
6601}
6602
6603#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07006604/*
6605 * Ensure the UNIX gc is aware of our file set, so we are certain that
6606 * the io_uring can be safely unregistered on process exit, even if we have
6607 * loops in the file referencing.
6608 */
6609static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
6610{
6611 struct sock *sk = ctx->ring_sock->sk;
6612 struct scm_fp_list *fpl;
6613 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06006614 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07006615
Jens Axboe6b063142019-01-10 22:13:58 -07006616 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
6617 if (!fpl)
6618 return -ENOMEM;
6619
6620 skb = alloc_skb(0, GFP_KERNEL);
6621 if (!skb) {
6622 kfree(fpl);
6623 return -ENOMEM;
6624 }
6625
6626 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07006627
Jens Axboe08a45172019-10-03 08:11:03 -06006628 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07006629 fpl->user = get_uid(ctx->user);
6630 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006631 struct file *file = io_file_from_index(ctx, i + offset);
6632
6633 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06006634 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06006635 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06006636 unix_inflight(fpl->user, fpl->fp[nr_files]);
6637 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07006638 }
6639
Jens Axboe08a45172019-10-03 08:11:03 -06006640 if (nr_files) {
6641 fpl->max = SCM_MAX_FD;
6642 fpl->count = nr_files;
6643 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006644 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06006645 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
6646 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07006647
Jens Axboe08a45172019-10-03 08:11:03 -06006648 for (i = 0; i < nr_files; i++)
6649 fput(fpl->fp[i]);
6650 } else {
6651 kfree_skb(skb);
6652 kfree(fpl);
6653 }
Jens Axboe6b063142019-01-10 22:13:58 -07006654
6655 return 0;
6656}
6657
6658/*
6659 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
6660 * causes regular reference counting to break down. We rely on the UNIX
6661 * garbage collection to take care of this problem for us.
6662 */
6663static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6664{
6665 unsigned left, total;
6666 int ret = 0;
6667
6668 total = 0;
6669 left = ctx->nr_user_files;
6670 while (left) {
6671 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07006672
6673 ret = __io_sqe_files_scm(ctx, this_files, total);
6674 if (ret)
6675 break;
6676 left -= this_files;
6677 total += this_files;
6678 }
6679
6680 if (!ret)
6681 return 0;
6682
6683 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006684 struct file *file = io_file_from_index(ctx, total);
6685
6686 if (file)
6687 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07006688 total++;
6689 }
6690
6691 return ret;
6692}
6693#else
6694static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6695{
6696 return 0;
6697}
6698#endif
6699
Jens Axboe65e19f52019-10-26 07:20:21 -06006700static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
6701 unsigned nr_files)
6702{
6703 int i;
6704
6705 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006706 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006707 unsigned this_files;
6708
6709 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
6710 table->files = kcalloc(this_files, sizeof(struct file *),
6711 GFP_KERNEL);
6712 if (!table->files)
6713 break;
6714 nr_files -= this_files;
6715 }
6716
6717 if (i == nr_tables)
6718 return 0;
6719
6720 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006721 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006722 kfree(table->files);
6723 }
6724 return 1;
6725}
6726
Jens Axboe05f3fb32019-12-09 11:22:50 -07006727static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06006728{
6729#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06006730 struct sock *sock = ctx->ring_sock->sk;
6731 struct sk_buff_head list, *head = &sock->sk_receive_queue;
6732 struct sk_buff *skb;
6733 int i;
6734
6735 __skb_queue_head_init(&list);
6736
6737 /*
6738 * Find the skb that holds this file in its SCM_RIGHTS. When found,
6739 * remove this entry and rearrange the file array.
6740 */
6741 skb = skb_dequeue(head);
6742 while (skb) {
6743 struct scm_fp_list *fp;
6744
6745 fp = UNIXCB(skb).fp;
6746 for (i = 0; i < fp->count; i++) {
6747 int left;
6748
6749 if (fp->fp[i] != file)
6750 continue;
6751
6752 unix_notinflight(fp->user, fp->fp[i]);
6753 left = fp->count - 1 - i;
6754 if (left) {
6755 memmove(&fp->fp[i], &fp->fp[i + 1],
6756 left * sizeof(struct file *));
6757 }
6758 fp->count--;
6759 if (!fp->count) {
6760 kfree_skb(skb);
6761 skb = NULL;
6762 } else {
6763 __skb_queue_tail(&list, skb);
6764 }
6765 fput(file);
6766 file = NULL;
6767 break;
6768 }
6769
6770 if (!file)
6771 break;
6772
6773 __skb_queue_tail(&list, skb);
6774
6775 skb = skb_dequeue(head);
6776 }
6777
6778 if (skb_peek(&list)) {
6779 spin_lock_irq(&head->lock);
6780 while ((skb = __skb_dequeue(&list)) != NULL)
6781 __skb_queue_tail(head, skb);
6782 spin_unlock_irq(&head->lock);
6783 }
6784#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07006785 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06006786#endif
6787}
6788
Jens Axboe05f3fb32019-12-09 11:22:50 -07006789struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08006790 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006791 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006792};
6793
Jens Axboe4a38aed22020-05-14 17:21:15 -06006794static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006795{
Jens Axboe4a38aed22020-05-14 17:21:15 -06006796 struct fixed_file_data *file_data = ref_node->file_data;
6797 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006798 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006799
6800 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006801 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006802 io_ring_file_put(ctx, pfile->file);
6803 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006804 }
6805
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006806 spin_lock(&file_data->lock);
6807 list_del(&ref_node->node);
6808 spin_unlock(&file_data->lock);
Jens Axboe2faf8522020-02-04 19:54:55 -07006809
Xiaoguang Wang05589552020-03-31 14:05:18 +08006810 percpu_ref_exit(&ref_node->refs);
6811 kfree(ref_node);
6812 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006813}
6814
Jens Axboe4a38aed22020-05-14 17:21:15 -06006815static void io_file_put_work(struct work_struct *work)
6816{
6817 struct io_ring_ctx *ctx;
6818 struct llist_node *node;
6819
6820 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
6821 node = llist_del_all(&ctx->file_put_llist);
6822
6823 while (node) {
6824 struct fixed_file_ref_node *ref_node;
6825 struct llist_node *next = node->next;
6826
6827 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
6828 __io_file_put_work(ref_node);
6829 node = next;
6830 }
6831}
6832
Jens Axboe05f3fb32019-12-09 11:22:50 -07006833static void io_file_data_ref_zero(struct percpu_ref *ref)
6834{
Xiaoguang Wang05589552020-03-31 14:05:18 +08006835 struct fixed_file_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06006836 struct io_ring_ctx *ctx;
6837 bool first_add;
6838 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006839
Xiaoguang Wang05589552020-03-31 14:05:18 +08006840 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Jens Axboe4a38aed22020-05-14 17:21:15 -06006841 ctx = ref_node->file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006842
Jens Axboe4a38aed22020-05-14 17:21:15 -06006843 if (percpu_ref_is_dying(&ctx->file_data->refs))
6844 delay = 0;
6845
6846 first_add = llist_add(&ref_node->llist, &ctx->file_put_llist);
6847 if (!delay)
6848 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
6849 else if (first_add)
6850 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006851}
6852
6853static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
6854 struct io_ring_ctx *ctx)
6855{
6856 struct fixed_file_ref_node *ref_node;
6857
6858 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
6859 if (!ref_node)
6860 return ERR_PTR(-ENOMEM);
6861
6862 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
6863 0, GFP_KERNEL)) {
6864 kfree(ref_node);
6865 return ERR_PTR(-ENOMEM);
6866 }
6867 INIT_LIST_HEAD(&ref_node->node);
6868 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006869 ref_node->file_data = ctx->file_data;
6870 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006871}
6872
6873static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
6874{
6875 percpu_ref_exit(&ref_node->refs);
6876 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006877}
6878
6879static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
6880 unsigned nr_args)
6881{
6882 __s32 __user *fds = (__s32 __user *) arg;
6883 unsigned nr_tables;
6884 struct file *file;
6885 int fd, ret = 0;
6886 unsigned i;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006887 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006888
6889 if (ctx->file_data)
6890 return -EBUSY;
6891 if (!nr_args)
6892 return -EINVAL;
6893 if (nr_args > IORING_MAX_FIXED_FILES)
6894 return -EMFILE;
6895
6896 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
6897 if (!ctx->file_data)
6898 return -ENOMEM;
6899 ctx->file_data->ctx = ctx;
6900 init_completion(&ctx->file_data->done);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006901 INIT_LIST_HEAD(&ctx->file_data->ref_list);
Xiaoguang Wangf7fe9342020-04-07 20:02:31 +08006902 spin_lock_init(&ctx->file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006903
6904 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
6905 ctx->file_data->table = kcalloc(nr_tables,
6906 sizeof(struct fixed_file_table),
6907 GFP_KERNEL);
6908 if (!ctx->file_data->table) {
6909 kfree(ctx->file_data);
6910 ctx->file_data = NULL;
6911 return -ENOMEM;
6912 }
6913
Xiaoguang Wang05589552020-03-31 14:05:18 +08006914 if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill,
Jens Axboe05f3fb32019-12-09 11:22:50 -07006915 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
6916 kfree(ctx->file_data->table);
6917 kfree(ctx->file_data);
6918 ctx->file_data = NULL;
6919 return -ENOMEM;
6920 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006921
6922 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
6923 percpu_ref_exit(&ctx->file_data->refs);
6924 kfree(ctx->file_data->table);
6925 kfree(ctx->file_data);
6926 ctx->file_data = NULL;
6927 return -ENOMEM;
6928 }
6929
6930 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
6931 struct fixed_file_table *table;
6932 unsigned index;
6933
6934 ret = -EFAULT;
6935 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
6936 break;
6937 /* allow sparse sets */
6938 if (fd == -1) {
6939 ret = 0;
6940 continue;
6941 }
6942
6943 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6944 index = i & IORING_FILE_TABLE_MASK;
6945 file = fget(fd);
6946
6947 ret = -EBADF;
6948 if (!file)
6949 break;
6950
6951 /*
6952 * Don't allow io_uring instances to be registered. If UNIX
6953 * isn't enabled, then this causes a reference cycle and this
6954 * instance can never get freed. If UNIX is enabled we'll
6955 * handle it just fine, but there's still no point in allowing
6956 * a ring fd as it doesn't support regular read/write anyway.
6957 */
6958 if (file->f_op == &io_uring_fops) {
6959 fput(file);
6960 break;
6961 }
6962 ret = 0;
6963 table->files[index] = file;
6964 }
6965
6966 if (ret) {
6967 for (i = 0; i < ctx->nr_user_files; i++) {
6968 file = io_file_from_index(ctx, i);
6969 if (file)
6970 fput(file);
6971 }
6972 for (i = 0; i < nr_tables; i++)
6973 kfree(ctx->file_data->table[i].files);
6974
6975 kfree(ctx->file_data->table);
6976 kfree(ctx->file_data);
6977 ctx->file_data = NULL;
6978 ctx->nr_user_files = 0;
6979 return ret;
6980 }
6981
6982 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006983 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006984 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006985 return ret;
6986 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006987
Xiaoguang Wang05589552020-03-31 14:05:18 +08006988 ref_node = alloc_fixed_file_ref_node(ctx);
6989 if (IS_ERR(ref_node)) {
6990 io_sqe_files_unregister(ctx);
6991 return PTR_ERR(ref_node);
6992 }
6993
6994 ctx->file_data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006995 spin_lock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006996 list_add(&ref_node->node, &ctx->file_data->ref_list);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006997 spin_unlock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006998 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006999 return ret;
7000}
7001
Jens Axboec3a31e62019-10-03 13:59:56 -06007002static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7003 int index)
7004{
7005#if defined(CONFIG_UNIX)
7006 struct sock *sock = ctx->ring_sock->sk;
7007 struct sk_buff_head *head = &sock->sk_receive_queue;
7008 struct sk_buff *skb;
7009
7010 /*
7011 * See if we can merge this file into an existing skb SCM_RIGHTS
7012 * file set. If there's no room, fall back to allocating a new skb
7013 * and filling it in.
7014 */
7015 spin_lock_irq(&head->lock);
7016 skb = skb_peek(head);
7017 if (skb) {
7018 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7019
7020 if (fpl->count < SCM_MAX_FD) {
7021 __skb_unlink(skb, head);
7022 spin_unlock_irq(&head->lock);
7023 fpl->fp[fpl->count] = get_file(file);
7024 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7025 fpl->count++;
7026 spin_lock_irq(&head->lock);
7027 __skb_queue_head(head, skb);
7028 } else {
7029 skb = NULL;
7030 }
7031 }
7032 spin_unlock_irq(&head->lock);
7033
7034 if (skb) {
7035 fput(file);
7036 return 0;
7037 }
7038
7039 return __io_sqe_files_scm(ctx, 1, index);
7040#else
7041 return 0;
7042#endif
7043}
7044
Hillf Dantona5318d32020-03-23 17:47:15 +08007045static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007046 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007047{
Hillf Dantona5318d32020-03-23 17:47:15 +08007048 struct io_file_put *pfile;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007049 struct percpu_ref *refs = data->cur_refs;
7050 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007051
Jens Axboe05f3fb32019-12-09 11:22:50 -07007052 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08007053 if (!pfile)
7054 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007055
Xiaoguang Wang05589552020-03-31 14:05:18 +08007056 ref_node = container_of(refs, struct fixed_file_ref_node, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007057 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007058 list_add(&pfile->list, &ref_node->file_list);
7059
Hillf Dantona5318d32020-03-23 17:47:15 +08007060 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007061}
7062
7063static int __io_sqe_files_update(struct io_ring_ctx *ctx,
7064 struct io_uring_files_update *up,
7065 unsigned nr_args)
7066{
7067 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007068 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007069 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007070 __s32 __user *fds;
7071 int fd, i, err;
7072 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007073 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007074
Jens Axboe05f3fb32019-12-09 11:22:50 -07007075 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007076 return -EOVERFLOW;
7077 if (done > ctx->nr_user_files)
7078 return -EINVAL;
7079
Xiaoguang Wang05589552020-03-31 14:05:18 +08007080 ref_node = alloc_fixed_file_ref_node(ctx);
7081 if (IS_ERR(ref_node))
7082 return PTR_ERR(ref_node);
7083
Jens Axboec3a31e62019-10-03 13:59:56 -06007084 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007085 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06007086 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007087 struct fixed_file_table *table;
7088 unsigned index;
7089
Jens Axboec3a31e62019-10-03 13:59:56 -06007090 err = 0;
7091 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7092 err = -EFAULT;
7093 break;
7094 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007095 i = array_index_nospec(up->offset, ctx->nr_user_files);
7096 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06007097 index = i & IORING_FILE_TABLE_MASK;
7098 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007099 file = io_file_from_index(ctx, index);
Hillf Dantona5318d32020-03-23 17:47:15 +08007100 err = io_queue_file_removal(data, file);
7101 if (err)
7102 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06007103 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007104 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007105 }
7106 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007107 file = fget(fd);
7108 if (!file) {
7109 err = -EBADF;
7110 break;
7111 }
7112 /*
7113 * Don't allow io_uring instances to be registered. If
7114 * UNIX isn't enabled, then this causes a reference
7115 * cycle and this instance can never get freed. If UNIX
7116 * is enabled we'll handle it just fine, but there's
7117 * still no point in allowing a ring fd as it doesn't
7118 * support regular read/write anyway.
7119 */
7120 if (file->f_op == &io_uring_fops) {
7121 fput(file);
7122 err = -EBADF;
7123 break;
7124 }
Jens Axboe65e19f52019-10-26 07:20:21 -06007125 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007126 err = io_sqe_file_register(ctx, file, i);
7127 if (err)
7128 break;
7129 }
7130 nr_args--;
7131 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007132 up->offset++;
7133 }
7134
Xiaoguang Wang05589552020-03-31 14:05:18 +08007135 if (needs_switch) {
7136 percpu_ref_kill(data->cur_refs);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007137 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007138 list_add(&ref_node->node, &data->ref_list);
7139 data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007140 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007141 percpu_ref_get(&ctx->file_data->refs);
7142 } else
7143 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007144
7145 return done ? done : err;
7146}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007147
Jens Axboe05f3fb32019-12-09 11:22:50 -07007148static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7149 unsigned nr_args)
7150{
7151 struct io_uring_files_update up;
7152
7153 if (!ctx->file_data)
7154 return -ENXIO;
7155 if (!nr_args)
7156 return -EINVAL;
7157 if (copy_from_user(&up, arg, sizeof(up)))
7158 return -EFAULT;
7159 if (up.resv)
7160 return -EINVAL;
7161
7162 return __io_sqe_files_update(ctx, &up, nr_args);
7163}
Jens Axboec3a31e62019-10-03 13:59:56 -06007164
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007165static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007166{
7167 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7168
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007169 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07007170 io_put_req(req);
7171}
7172
Pavel Begunkov24369c22020-01-28 03:15:48 +03007173static int io_init_wq_offload(struct io_ring_ctx *ctx,
7174 struct io_uring_params *p)
7175{
7176 struct io_wq_data data;
7177 struct fd f;
7178 struct io_ring_ctx *ctx_attach;
7179 unsigned int concurrency;
7180 int ret = 0;
7181
7182 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007183 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03007184 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007185
7186 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
7187 /* Do QD, or 4 * CPUS, whatever is smallest */
7188 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
7189
7190 ctx->io_wq = io_wq_create(concurrency, &data);
7191 if (IS_ERR(ctx->io_wq)) {
7192 ret = PTR_ERR(ctx->io_wq);
7193 ctx->io_wq = NULL;
7194 }
7195 return ret;
7196 }
7197
7198 f = fdget(p->wq_fd);
7199 if (!f.file)
7200 return -EBADF;
7201
7202 if (f.file->f_op != &io_uring_fops) {
7203 ret = -EINVAL;
7204 goto out_fput;
7205 }
7206
7207 ctx_attach = f.file->private_data;
7208 /* @io_wq is protected by holding the fd */
7209 if (!io_wq_get(ctx_attach->io_wq, &data)) {
7210 ret = -EINVAL;
7211 goto out_fput;
7212 }
7213
7214 ctx->io_wq = ctx_attach->io_wq;
7215out_fput:
7216 fdput(f);
7217 return ret;
7218}
7219
Jens Axboe6c271ce2019-01-10 11:22:30 -07007220static int io_sq_offload_start(struct io_ring_ctx *ctx,
7221 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007222{
7223 int ret;
7224
7225 mmgrab(current->mm);
7226 ctx->sqo_mm = current->mm;
7227
Jens Axboe6c271ce2019-01-10 11:22:30 -07007228 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06007229 ret = -EPERM;
7230 if (!capable(CAP_SYS_ADMIN))
7231 goto err;
7232
Jens Axboe917257d2019-04-13 09:28:55 -06007233 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7234 if (!ctx->sq_thread_idle)
7235 ctx->sq_thread_idle = HZ;
7236
Jens Axboe6c271ce2019-01-10 11:22:30 -07007237 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007238 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007239
Jens Axboe917257d2019-04-13 09:28:55 -06007240 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007241 if (cpu >= nr_cpu_ids)
7242 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007243 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007244 goto err;
7245
Jens Axboe6c271ce2019-01-10 11:22:30 -07007246 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
7247 ctx, cpu,
7248 "io_uring-sq");
7249 } else {
7250 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
7251 "io_uring-sq");
7252 }
7253 if (IS_ERR(ctx->sqo_thread)) {
7254 ret = PTR_ERR(ctx->sqo_thread);
7255 ctx->sqo_thread = NULL;
7256 goto err;
7257 }
7258 wake_up_process(ctx->sqo_thread);
7259 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7260 /* Can't have SQ_AFF without SQPOLL */
7261 ret = -EINVAL;
7262 goto err;
7263 }
7264
Pavel Begunkov24369c22020-01-28 03:15:48 +03007265 ret = io_init_wq_offload(ctx, p);
7266 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007267 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007268
7269 return 0;
7270err:
Jens Axboe54a91f32019-09-10 09:15:04 -06007271 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007272 mmdrop(ctx->sqo_mm);
7273 ctx->sqo_mm = NULL;
7274 return ret;
7275}
7276
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007277static inline void __io_unaccount_mem(struct user_struct *user,
7278 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007279{
7280 atomic_long_sub(nr_pages, &user->locked_vm);
7281}
7282
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007283static inline int __io_account_mem(struct user_struct *user,
7284 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007285{
7286 unsigned long page_limit, cur_pages, new_pages;
7287
7288 /* Don't allow more pages than we can safely lock */
7289 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
7290
7291 do {
7292 cur_pages = atomic_long_read(&user->locked_vm);
7293 new_pages = cur_pages + nr_pages;
7294 if (new_pages > page_limit)
7295 return -ENOMEM;
7296 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7297 new_pages) != cur_pages);
7298
7299 return 0;
7300}
7301
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007302static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7303 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007304{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07007305 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007306 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007307
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007308 if (ctx->sqo_mm) {
7309 if (acct == ACCT_LOCKED)
7310 ctx->sqo_mm->locked_vm -= nr_pages;
7311 else if (acct == ACCT_PINNED)
7312 atomic64_sub(nr_pages, &ctx->sqo_mm->pinned_vm);
7313 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007314}
7315
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007316static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7317 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007318{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007319 int ret;
7320
7321 if (ctx->limit_mem) {
7322 ret = __io_account_mem(ctx->user, nr_pages);
7323 if (ret)
7324 return ret;
7325 }
7326
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007327 if (ctx->sqo_mm) {
7328 if (acct == ACCT_LOCKED)
7329 ctx->sqo_mm->locked_vm += nr_pages;
7330 else if (acct == ACCT_PINNED)
7331 atomic64_add(nr_pages, &ctx->sqo_mm->pinned_vm);
7332 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007333
7334 return 0;
7335}
7336
Jens Axboe2b188cc2019-01-07 10:46:33 -07007337static void io_mem_free(void *ptr)
7338{
Mark Rutland52e04ef2019-04-30 17:30:21 +01007339 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007340
Mark Rutland52e04ef2019-04-30 17:30:21 +01007341 if (!ptr)
7342 return;
7343
7344 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007345 if (put_page_testzero(page))
7346 free_compound_page(page);
7347}
7348
7349static void *io_mem_alloc(size_t size)
7350{
7351 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
7352 __GFP_NORETRY;
7353
7354 return (void *) __get_free_pages(gfp_flags, get_order(size));
7355}
7356
Hristo Venev75b28af2019-08-26 17:23:46 +00007357static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
7358 size_t *sq_offset)
7359{
7360 struct io_rings *rings;
7361 size_t off, sq_array_size;
7362
7363 off = struct_size(rings, cqes, cq_entries);
7364 if (off == SIZE_MAX)
7365 return SIZE_MAX;
7366
7367#ifdef CONFIG_SMP
7368 off = ALIGN(off, SMP_CACHE_BYTES);
7369 if (off == 0)
7370 return SIZE_MAX;
7371#endif
7372
7373 sq_array_size = array_size(sizeof(u32), sq_entries);
7374 if (sq_array_size == SIZE_MAX)
7375 return SIZE_MAX;
7376
7377 if (check_add_overflow(off, sq_array_size, &off))
7378 return SIZE_MAX;
7379
7380 if (sq_offset)
7381 *sq_offset = off;
7382
7383 return off;
7384}
7385
Jens Axboe2b188cc2019-01-07 10:46:33 -07007386static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
7387{
Hristo Venev75b28af2019-08-26 17:23:46 +00007388 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007389
Hristo Venev75b28af2019-08-26 17:23:46 +00007390 pages = (size_t)1 << get_order(
7391 rings_size(sq_entries, cq_entries, NULL));
7392 pages += (size_t)1 << get_order(
7393 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07007394
Hristo Venev75b28af2019-08-26 17:23:46 +00007395 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007396}
7397
Jens Axboeedafcce2019-01-09 09:16:05 -07007398static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
7399{
7400 int i, j;
7401
7402 if (!ctx->user_bufs)
7403 return -ENXIO;
7404
7405 for (i = 0; i < ctx->nr_user_bufs; i++) {
7406 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7407
7408 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007409 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07007410
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007411 io_unaccount_mem(ctx, imu->nr_bvecs, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007412 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007413 imu->nr_bvecs = 0;
7414 }
7415
7416 kfree(ctx->user_bufs);
7417 ctx->user_bufs = NULL;
7418 ctx->nr_user_bufs = 0;
7419 return 0;
7420}
7421
7422static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
7423 void __user *arg, unsigned index)
7424{
7425 struct iovec __user *src;
7426
7427#ifdef CONFIG_COMPAT
7428 if (ctx->compat) {
7429 struct compat_iovec __user *ciovs;
7430 struct compat_iovec ciov;
7431
7432 ciovs = (struct compat_iovec __user *) arg;
7433 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
7434 return -EFAULT;
7435
Jens Axboed55e5f52019-12-11 16:12:15 -07007436 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07007437 dst->iov_len = ciov.iov_len;
7438 return 0;
7439 }
7440#endif
7441 src = (struct iovec __user *) arg;
7442 if (copy_from_user(dst, &src[index], sizeof(*dst)))
7443 return -EFAULT;
7444 return 0;
7445}
7446
7447static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
7448 unsigned nr_args)
7449{
7450 struct vm_area_struct **vmas = NULL;
7451 struct page **pages = NULL;
7452 int i, j, got_pages = 0;
7453 int ret = -EINVAL;
7454
7455 if (ctx->user_bufs)
7456 return -EBUSY;
7457 if (!nr_args || nr_args > UIO_MAXIOV)
7458 return -EINVAL;
7459
7460 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
7461 GFP_KERNEL);
7462 if (!ctx->user_bufs)
7463 return -ENOMEM;
7464
7465 for (i = 0; i < nr_args; i++) {
7466 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7467 unsigned long off, start, end, ubuf;
7468 int pret, nr_pages;
7469 struct iovec iov;
7470 size_t size;
7471
7472 ret = io_copy_iov(ctx, &iov, arg, i);
7473 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03007474 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007475
7476 /*
7477 * Don't impose further limits on the size and buffer
7478 * constraints here, we'll -EINVAL later when IO is
7479 * submitted if they are wrong.
7480 */
7481 ret = -EFAULT;
7482 if (!iov.iov_base || !iov.iov_len)
7483 goto err;
7484
7485 /* arbitrary limit, but we need something */
7486 if (iov.iov_len > SZ_1G)
7487 goto err;
7488
7489 ubuf = (unsigned long) iov.iov_base;
7490 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
7491 start = ubuf >> PAGE_SHIFT;
7492 nr_pages = end - start;
7493
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007494 ret = io_account_mem(ctx, nr_pages, ACCT_PINNED);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007495 if (ret)
7496 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007497
7498 ret = 0;
7499 if (!pages || nr_pages > got_pages) {
Denis Efremova8c73c12020-06-05 12:32:03 +03007500 kvfree(vmas);
7501 kvfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007502 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07007503 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007504 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07007505 sizeof(struct vm_area_struct *),
7506 GFP_KERNEL);
7507 if (!pages || !vmas) {
7508 ret = -ENOMEM;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007509 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07007510 goto err;
7511 }
7512 got_pages = nr_pages;
7513 }
7514
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007515 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07007516 GFP_KERNEL);
7517 ret = -ENOMEM;
7518 if (!imu->bvec) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007519 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07007520 goto err;
7521 }
7522
7523 ret = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007524 mmap_read_lock(current->mm);
John Hubbard2113b052020-01-30 22:13:13 -08007525 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07007526 FOLL_WRITE | FOLL_LONGTERM,
7527 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007528 if (pret == nr_pages) {
7529 /* don't support file backed memory */
7530 for (j = 0; j < nr_pages; j++) {
7531 struct vm_area_struct *vma = vmas[j];
7532
7533 if (vma->vm_file &&
7534 !is_file_hugepages(vma->vm_file)) {
7535 ret = -EOPNOTSUPP;
7536 break;
7537 }
7538 }
7539 } else {
7540 ret = pret < 0 ? pret : -EFAULT;
7541 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007542 mmap_read_unlock(current->mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07007543 if (ret) {
7544 /*
7545 * if we did partial map, or found file backed vmas,
7546 * release any pages we did get
7547 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07007548 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007549 unpin_user_pages(pages, pret);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007550 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007551 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007552 goto err;
7553 }
7554
7555 off = ubuf & ~PAGE_MASK;
7556 size = iov.iov_len;
7557 for (j = 0; j < nr_pages; j++) {
7558 size_t vec_len;
7559
7560 vec_len = min_t(size_t, size, PAGE_SIZE - off);
7561 imu->bvec[j].bv_page = pages[j];
7562 imu->bvec[j].bv_len = vec_len;
7563 imu->bvec[j].bv_offset = off;
7564 off = 0;
7565 size -= vec_len;
7566 }
7567 /* store original address for later verification */
7568 imu->ubuf = ubuf;
7569 imu->len = iov.iov_len;
7570 imu->nr_bvecs = nr_pages;
7571
7572 ctx->nr_user_bufs++;
7573 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007574 kvfree(pages);
7575 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007576 return 0;
7577err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007578 kvfree(pages);
7579 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007580 io_sqe_buffer_unregister(ctx);
7581 return ret;
7582}
7583
Jens Axboe9b402842019-04-11 11:45:41 -06007584static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
7585{
7586 __s32 __user *fds = arg;
7587 int fd;
7588
7589 if (ctx->cq_ev_fd)
7590 return -EBUSY;
7591
7592 if (copy_from_user(&fd, fds, sizeof(*fds)))
7593 return -EFAULT;
7594
7595 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
7596 if (IS_ERR(ctx->cq_ev_fd)) {
7597 int ret = PTR_ERR(ctx->cq_ev_fd);
7598 ctx->cq_ev_fd = NULL;
7599 return ret;
7600 }
7601
7602 return 0;
7603}
7604
7605static int io_eventfd_unregister(struct io_ring_ctx *ctx)
7606{
7607 if (ctx->cq_ev_fd) {
7608 eventfd_ctx_put(ctx->cq_ev_fd);
7609 ctx->cq_ev_fd = NULL;
7610 return 0;
7611 }
7612
7613 return -ENXIO;
7614}
7615
Jens Axboe5a2e7452020-02-23 16:23:11 -07007616static int __io_destroy_buffers(int id, void *p, void *data)
7617{
7618 struct io_ring_ctx *ctx = data;
7619 struct io_buffer *buf = p;
7620
Jens Axboe067524e2020-03-02 16:32:28 -07007621 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007622 return 0;
7623}
7624
7625static void io_destroy_buffers(struct io_ring_ctx *ctx)
7626{
7627 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
7628 idr_destroy(&ctx->io_buffer_idr);
7629}
7630
Jens Axboe2b188cc2019-01-07 10:46:33 -07007631static void io_ring_ctx_free(struct io_ring_ctx *ctx)
7632{
Jens Axboe6b063142019-01-10 22:13:58 -07007633 io_finish_async(ctx);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007634 if (ctx->sqo_mm) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07007635 mmdrop(ctx->sqo_mm);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007636 ctx->sqo_mm = NULL;
7637 }
Jens Axboedef596e2019-01-09 08:59:42 -07007638
7639 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07007640 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07007641 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06007642 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007643 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07007644 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07007645
Jens Axboe2b188cc2019-01-07 10:46:33 -07007646#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07007647 if (ctx->ring_sock) {
7648 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007649 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07007650 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007651#endif
7652
Hristo Venev75b28af2019-08-26 17:23:46 +00007653 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007654 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007655
7656 percpu_ref_exit(&ctx->refs);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007657 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
7658 ACCT_LOCKED);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007659 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07007660 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07007661 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07007662 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007663 kfree(ctx);
7664}
7665
7666static __poll_t io_uring_poll(struct file *file, poll_table *wait)
7667{
7668 struct io_ring_ctx *ctx = file->private_data;
7669 __poll_t mask = 0;
7670
7671 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02007672 /*
7673 * synchronizes with barrier from wq_has_sleeper call in
7674 * io_commit_cqring
7675 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007676 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00007677 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
7678 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007679 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01007680 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007681 mask |= EPOLLIN | EPOLLRDNORM;
7682
7683 return mask;
7684}
7685
7686static int io_uring_fasync(int fd, struct file *file, int on)
7687{
7688 struct io_ring_ctx *ctx = file->private_data;
7689
7690 return fasync_helper(fd, file, on, &ctx->cq_fasync);
7691}
7692
Jens Axboe071698e2020-01-28 10:04:42 -07007693static int io_remove_personalities(int id, void *p, void *data)
7694{
7695 struct io_ring_ctx *ctx = data;
7696 const struct cred *cred;
7697
7698 cred = idr_remove(&ctx->personality_idr, id);
7699 if (cred)
7700 put_cred(cred);
7701 return 0;
7702}
7703
Jens Axboe85faa7b2020-04-09 18:14:00 -06007704static void io_ring_exit_work(struct work_struct *work)
7705{
7706 struct io_ring_ctx *ctx;
7707
7708 ctx = container_of(work, struct io_ring_ctx, exit_work);
7709 if (ctx->rings)
7710 io_cqring_overflow_flush(ctx, true);
7711
Jens Axboe56952e92020-06-17 15:00:04 -06007712 /*
7713 * If we're doing polled IO and end up having requests being
7714 * submitted async (out-of-line), then completions can come in while
7715 * we're waiting for refs to drop. We need to reap these manually,
7716 * as nobody else will be looking for them.
7717 */
7718 while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)) {
7719 io_iopoll_reap_events(ctx);
7720 if (ctx->rings)
7721 io_cqring_overflow_flush(ctx, true);
7722 }
Jens Axboe85faa7b2020-04-09 18:14:00 -06007723 io_ring_ctx_free(ctx);
7724}
7725
Jens Axboe2b188cc2019-01-07 10:46:33 -07007726static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
7727{
7728 mutex_lock(&ctx->uring_lock);
7729 percpu_ref_kill(&ctx->refs);
7730 mutex_unlock(&ctx->uring_lock);
7731
Jens Axboe5262f562019-09-17 12:26:57 -06007732 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07007733 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06007734
7735 if (ctx->io_wq)
7736 io_wq_cancel_all(ctx->io_wq);
7737
Jens Axboedef596e2019-01-09 08:59:42 -07007738 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07007739 /* if we failed setting up the ctx, we might not have any rings */
7740 if (ctx->rings)
7741 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07007742 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe85faa7b2020-04-09 18:14:00 -06007743 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
7744 queue_work(system_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007745}
7746
7747static int io_uring_release(struct inode *inode, struct file *file)
7748{
7749 struct io_ring_ctx *ctx = file->private_data;
7750
7751 file->private_data = NULL;
7752 io_ring_ctx_wait_and_kill(ctx);
7753 return 0;
7754}
7755
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03007756static bool io_wq_files_match(struct io_wq_work *work, void *data)
7757{
7758 struct files_struct *files = data;
7759
7760 return work->files == files;
7761}
7762
Jens Axboefcb323c2019-10-24 12:39:47 -06007763static void io_uring_cancel_files(struct io_ring_ctx *ctx,
7764 struct files_struct *files)
7765{
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03007766 if (list_empty_careful(&ctx->inflight_list))
7767 return;
7768
7769 /* cancel all at once, should be faster than doing it one by one*/
7770 io_wq_cancel_cb(ctx->io_wq, io_wq_files_match, files, true);
7771
Jens Axboefcb323c2019-10-24 12:39:47 -06007772 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007773 struct io_kiocb *cancel_req = NULL, *req;
7774 DEFINE_WAIT(wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007775
7776 spin_lock_irq(&ctx->inflight_lock);
7777 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07007778 if (req->work.files != files)
7779 continue;
7780 /* req is being completed, ignore */
7781 if (!refcount_inc_not_zero(&req->refs))
7782 continue;
7783 cancel_req = req;
7784 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06007785 }
Jens Axboe768134d2019-11-10 20:30:53 -07007786 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007787 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07007788 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06007789 spin_unlock_irq(&ctx->inflight_lock);
7790
Jens Axboe768134d2019-11-10 20:30:53 -07007791 /* We need to keep going until we don't find a matching req */
7792 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007793 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08007794
Jens Axboe2ca10252020-02-13 17:17:35 -07007795 if (cancel_req->flags & REQ_F_OVERFLOW) {
7796 spin_lock_irq(&ctx->completion_lock);
7797 list_del(&cancel_req->list);
7798 cancel_req->flags &= ~REQ_F_OVERFLOW;
7799 if (list_empty(&ctx->cq_overflow_list)) {
7800 clear_bit(0, &ctx->sq_check_overflow);
7801 clear_bit(0, &ctx->cq_check_overflow);
7802 }
7803 spin_unlock_irq(&ctx->completion_lock);
7804
7805 WRITE_ONCE(ctx->rings->cq_overflow,
7806 atomic_inc_return(&ctx->cached_cq_overflow));
7807
7808 /*
7809 * Put inflight ref and overflow ref. If that's
7810 * all we had, then we're done with this request.
7811 */
7812 if (refcount_sub_and_test(2, &cancel_req->refs)) {
Pavel Begunkov4518a3c2020-05-26 20:34:02 +03007813 io_free_req(cancel_req);
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007814 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboe2ca10252020-02-13 17:17:35 -07007815 continue;
7816 }
Pavel Begunkov7b53d592020-05-30 14:19:15 +03007817 } else {
7818 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
7819 io_put_req(cancel_req);
Jens Axboe2ca10252020-02-13 17:17:35 -07007820 }
7821
Jens Axboefcb323c2019-10-24 12:39:47 -06007822 schedule();
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007823 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007824 }
7825}
7826
Pavel Begunkov801dd572020-06-15 10:33:14 +03007827static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Pavel Begunkov44e728b2020-06-15 10:24:04 +03007828{
Pavel Begunkov801dd572020-06-15 10:33:14 +03007829 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7830 struct task_struct *task = data;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03007831
Pavel Begunkov801dd572020-06-15 10:33:14 +03007832 return req->task == task;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03007833}
7834
Jens Axboefcb323c2019-10-24 12:39:47 -06007835static int io_uring_flush(struct file *file, void *data)
7836{
7837 struct io_ring_ctx *ctx = file->private_data;
7838
7839 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07007840
7841 /*
7842 * If the task is going away, cancel work it may have pending
7843 */
Pavel Begunkov801dd572020-06-15 10:33:14 +03007844 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
7845 io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, current, true);
Jens Axboe6ab23142020-02-08 20:23:59 -07007846
Jens Axboefcb323c2019-10-24 12:39:47 -06007847 return 0;
7848}
7849
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007850static void *io_uring_validate_mmap_request(struct file *file,
7851 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007852{
Jens Axboe2b188cc2019-01-07 10:46:33 -07007853 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007854 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007855 struct page *page;
7856 void *ptr;
7857
7858 switch (offset) {
7859 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00007860 case IORING_OFF_CQ_RING:
7861 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007862 break;
7863 case IORING_OFF_SQES:
7864 ptr = ctx->sq_sqes;
7865 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007866 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007867 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007868 }
7869
7870 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07007871 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007872 return ERR_PTR(-EINVAL);
7873
7874 return ptr;
7875}
7876
7877#ifdef CONFIG_MMU
7878
7879static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7880{
7881 size_t sz = vma->vm_end - vma->vm_start;
7882 unsigned long pfn;
7883 void *ptr;
7884
7885 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
7886 if (IS_ERR(ptr))
7887 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007888
7889 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
7890 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
7891}
7892
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007893#else /* !CONFIG_MMU */
7894
7895static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7896{
7897 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
7898}
7899
7900static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
7901{
7902 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
7903}
7904
7905static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
7906 unsigned long addr, unsigned long len,
7907 unsigned long pgoff, unsigned long flags)
7908{
7909 void *ptr;
7910
7911 ptr = io_uring_validate_mmap_request(file, pgoff, len);
7912 if (IS_ERR(ptr))
7913 return PTR_ERR(ptr);
7914
7915 return (unsigned long) ptr;
7916}
7917
7918#endif /* !CONFIG_MMU */
7919
Jens Axboe2b188cc2019-01-07 10:46:33 -07007920SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
7921 u32, min_complete, u32, flags, const sigset_t __user *, sig,
7922 size_t, sigsz)
7923{
7924 struct io_ring_ctx *ctx;
7925 long ret = -EBADF;
7926 int submitted = 0;
7927 struct fd f;
7928
Jens Axboeb41e9852020-02-17 09:52:41 -07007929 if (current->task_works)
7930 task_work_run();
7931
Jens Axboe6c271ce2019-01-10 11:22:30 -07007932 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007933 return -EINVAL;
7934
7935 f = fdget(fd);
7936 if (!f.file)
7937 return -EBADF;
7938
7939 ret = -EOPNOTSUPP;
7940 if (f.file->f_op != &io_uring_fops)
7941 goto out_fput;
7942
7943 ret = -ENXIO;
7944 ctx = f.file->private_data;
7945 if (!percpu_ref_tryget(&ctx->refs))
7946 goto out_fput;
7947
Jens Axboe6c271ce2019-01-10 11:22:30 -07007948 /*
7949 * For SQ polling, the thread will do all submissions and completions.
7950 * Just return the requested submit count, and wake the thread if
7951 * we were asked to.
7952 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007953 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007954 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07007955 if (!list_empty_careful(&ctx->cq_overflow_list))
7956 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007957 if (flags & IORING_ENTER_SQ_WAKEUP)
7958 wake_up(&ctx->sqo_wait);
7959 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007960 } else if (to_submit) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07007961 mutex_lock(&ctx->uring_lock);
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03007962 submitted = io_submit_sqes(ctx, to_submit, f.file, fd);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007963 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007964
7965 if (submitted != to_submit)
7966 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007967 }
7968 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07007969 unsigned nr_events = 0;
7970
Jens Axboe2b188cc2019-01-07 10:46:33 -07007971 min_complete = min(min_complete, ctx->cq_entries);
7972
Xiaoguang Wang32b22442020-03-11 09:26:09 +08007973 /*
7974 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
7975 * space applications don't need to do io completion events
7976 * polling again, they can rely on io_sq_thread to do polling
7977 * work, which can reduce cpu usage and uring_lock contention.
7978 */
7979 if (ctx->flags & IORING_SETUP_IOPOLL &&
7980 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Jens Axboedef596e2019-01-09 08:59:42 -07007981 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07007982 } else {
7983 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
7984 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007985 }
7986
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007987out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03007988 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007989out_fput:
7990 fdput(f);
7991 return submitted ? submitted : ret;
7992}
7993
Tobias Klauserbebdb652020-02-26 18:38:32 +01007994#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07007995static int io_uring_show_cred(int id, void *p, void *data)
7996{
7997 const struct cred *cred = p;
7998 struct seq_file *m = data;
7999 struct user_namespace *uns = seq_user_ns(m);
8000 struct group_info *gi;
8001 kernel_cap_t cap;
8002 unsigned __capi;
8003 int g;
8004
8005 seq_printf(m, "%5d\n", id);
8006 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
8007 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
8008 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
8009 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
8010 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
8011 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
8012 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
8013 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
8014 seq_puts(m, "\n\tGroups:\t");
8015 gi = cred->group_info;
8016 for (g = 0; g < gi->ngroups; g++) {
8017 seq_put_decimal_ull(m, g ? " " : "",
8018 from_kgid_munged(uns, gi->gid[g]));
8019 }
8020 seq_puts(m, "\n\tCapEff:\t");
8021 cap = cred->cap_effective;
8022 CAP_FOR_EACH_U32(__capi)
8023 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
8024 seq_putc(m, '\n');
8025 return 0;
8026}
8027
8028static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
8029{
8030 int i;
8031
8032 mutex_lock(&ctx->uring_lock);
8033 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
8034 for (i = 0; i < ctx->nr_user_files; i++) {
8035 struct fixed_file_table *table;
8036 struct file *f;
8037
8038 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
8039 f = table->files[i & IORING_FILE_TABLE_MASK];
8040 if (f)
8041 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
8042 else
8043 seq_printf(m, "%5u: <none>\n", i);
8044 }
8045 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
8046 for (i = 0; i < ctx->nr_user_bufs; i++) {
8047 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
8048
8049 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
8050 (unsigned int) buf->len);
8051 }
8052 if (!idr_is_empty(&ctx->personality_idr)) {
8053 seq_printf(m, "Personalities:\n");
8054 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
8055 }
Jens Axboed7718a92020-02-14 22:23:12 -07008056 seq_printf(m, "PollList:\n");
8057 spin_lock_irq(&ctx->completion_lock);
8058 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8059 struct hlist_head *list = &ctx->cancel_hash[i];
8060 struct io_kiocb *req;
8061
8062 hlist_for_each_entry(req, list, hash_node)
8063 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
8064 req->task->task_works != NULL);
8065 }
8066 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07008067 mutex_unlock(&ctx->uring_lock);
8068}
8069
8070static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
8071{
8072 struct io_ring_ctx *ctx = f->private_data;
8073
8074 if (percpu_ref_tryget(&ctx->refs)) {
8075 __io_uring_show_fdinfo(ctx, m);
8076 percpu_ref_put(&ctx->refs);
8077 }
8078}
Tobias Klauserbebdb652020-02-26 18:38:32 +01008079#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07008080
Jens Axboe2b188cc2019-01-07 10:46:33 -07008081static const struct file_operations io_uring_fops = {
8082 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06008083 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008084 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008085#ifndef CONFIG_MMU
8086 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
8087 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
8088#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008089 .poll = io_uring_poll,
8090 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008091#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008092 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008093#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008094};
8095
8096static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
8097 struct io_uring_params *p)
8098{
Hristo Venev75b28af2019-08-26 17:23:46 +00008099 struct io_rings *rings;
8100 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008101
Hristo Venev75b28af2019-08-26 17:23:46 +00008102 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
8103 if (size == SIZE_MAX)
8104 return -EOVERFLOW;
8105
8106 rings = io_mem_alloc(size);
8107 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008108 return -ENOMEM;
8109
Hristo Venev75b28af2019-08-26 17:23:46 +00008110 ctx->rings = rings;
8111 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
8112 rings->sq_ring_mask = p->sq_entries - 1;
8113 rings->cq_ring_mask = p->cq_entries - 1;
8114 rings->sq_ring_entries = p->sq_entries;
8115 rings->cq_ring_entries = p->cq_entries;
8116 ctx->sq_mask = rings->sq_ring_mask;
8117 ctx->cq_mask = rings->cq_ring_mask;
8118 ctx->sq_entries = rings->sq_ring_entries;
8119 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008120
8121 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07008122 if (size == SIZE_MAX) {
8123 io_mem_free(ctx->rings);
8124 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008125 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07008126 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008127
8128 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07008129 if (!ctx->sq_sqes) {
8130 io_mem_free(ctx->rings);
8131 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008132 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07008133 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008134
Jens Axboe2b188cc2019-01-07 10:46:33 -07008135 return 0;
8136}
8137
8138/*
8139 * Allocate an anonymous fd, this is what constitutes the application
8140 * visible backing of an io_uring instance. The application mmaps this
8141 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
8142 * we have to tie this fd to a socket for file garbage collection purposes.
8143 */
8144static int io_uring_get_fd(struct io_ring_ctx *ctx)
8145{
8146 struct file *file;
8147 int ret;
8148
8149#if defined(CONFIG_UNIX)
8150 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
8151 &ctx->ring_sock);
8152 if (ret)
8153 return ret;
8154#endif
8155
8156 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
8157 if (ret < 0)
8158 goto err;
8159
8160 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
8161 O_RDWR | O_CLOEXEC);
8162 if (IS_ERR(file)) {
8163 put_unused_fd(ret);
8164 ret = PTR_ERR(file);
8165 goto err;
8166 }
8167
8168#if defined(CONFIG_UNIX)
8169 ctx->ring_sock->file = file;
8170#endif
8171 fd_install(ret, file);
8172 return ret;
8173err:
8174#if defined(CONFIG_UNIX)
8175 sock_release(ctx->ring_sock);
8176 ctx->ring_sock = NULL;
8177#endif
8178 return ret;
8179}
8180
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008181static int io_uring_create(unsigned entries, struct io_uring_params *p,
8182 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008183{
8184 struct user_struct *user = NULL;
8185 struct io_ring_ctx *ctx;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008186 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008187 int ret;
8188
Jens Axboe8110c1a2019-12-28 15:39:54 -07008189 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008190 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07008191 if (entries > IORING_MAX_ENTRIES) {
8192 if (!(p->flags & IORING_SETUP_CLAMP))
8193 return -EINVAL;
8194 entries = IORING_MAX_ENTRIES;
8195 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008196
8197 /*
8198 * Use twice as many entries for the CQ ring. It's possible for the
8199 * application to drive a higher depth than the size of the SQ ring,
8200 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06008201 * some flexibility in overcommitting a bit. If the application has
8202 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
8203 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07008204 */
8205 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06008206 if (p->flags & IORING_SETUP_CQSIZE) {
8207 /*
8208 * If IORING_SETUP_CQSIZE is set, we do the same roundup
8209 * to a power-of-two, if it isn't already. We do NOT impose
8210 * any cq vs sq ring sizing.
8211 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07008212 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06008213 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07008214 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
8215 if (!(p->flags & IORING_SETUP_CLAMP))
8216 return -EINVAL;
8217 p->cq_entries = IORING_MAX_CQ_ENTRIES;
8218 }
Jens Axboe33a107f2019-10-04 12:10:03 -06008219 p->cq_entries = roundup_pow_of_two(p->cq_entries);
8220 } else {
8221 p->cq_entries = 2 * p->sq_entries;
8222 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008223
8224 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008225 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008226
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008227 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008228 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008229 ring_pages(p->sq_entries, p->cq_entries));
8230 if (ret) {
8231 free_uid(user);
8232 return ret;
8233 }
8234 }
8235
8236 ctx = io_ring_ctx_alloc(p);
8237 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008238 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008239 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008240 p->cq_entries));
8241 free_uid(user);
8242 return -ENOMEM;
8243 }
8244 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07008245 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07008246 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07008247
8248 ret = io_allocate_scq_urings(ctx, p);
8249 if (ret)
8250 goto err;
8251
Jens Axboe6c271ce2019-01-10 11:22:30 -07008252 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008253 if (ret)
8254 goto err;
8255
Jens Axboe2b188cc2019-01-07 10:46:33 -07008256 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00008257 p->sq_off.head = offsetof(struct io_rings, sq.head);
8258 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
8259 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
8260 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
8261 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
8262 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
8263 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008264
8265 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00008266 p->cq_off.head = offsetof(struct io_rings, cq.head);
8267 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
8268 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
8269 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
8270 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
8271 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02008272 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06008273
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008274 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
8275 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08008276 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
8277 IORING_FEAT_POLL_32BITS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008278
8279 if (copy_to_user(params, p, sizeof(*p))) {
8280 ret = -EFAULT;
8281 goto err;
8282 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06008283 /*
8284 * Install ring fd as the very last thing, so we don't risk someone
8285 * having closed it before we finish setup
8286 */
8287 ret = io_uring_get_fd(ctx);
8288 if (ret < 0)
8289 goto err;
8290
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02008291 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008292 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
8293 ACCT_LOCKED);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008294 ctx->limit_mem = limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008295 return ret;
8296err:
8297 io_ring_ctx_wait_and_kill(ctx);
8298 return ret;
8299}
8300
8301/*
8302 * Sets up an aio uring context, and returns the fd. Applications asks for a
8303 * ring size, we return the actual sq/cq ring sizes (among other things) in the
8304 * params structure passed in.
8305 */
8306static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
8307{
8308 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008309 int i;
8310
8311 if (copy_from_user(&p, params, sizeof(p)))
8312 return -EFAULT;
8313 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
8314 if (p.resv[i])
8315 return -EINVAL;
8316 }
8317
Jens Axboe6c271ce2019-01-10 11:22:30 -07008318 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07008319 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03008320 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008321 return -EINVAL;
8322
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008323 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008324}
8325
8326SYSCALL_DEFINE2(io_uring_setup, u32, entries,
8327 struct io_uring_params __user *, params)
8328{
8329 return io_uring_setup(entries, params);
8330}
8331
Jens Axboe66f4af92020-01-16 15:36:52 -07008332static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
8333{
8334 struct io_uring_probe *p;
8335 size_t size;
8336 int i, ret;
8337
8338 size = struct_size(p, ops, nr_args);
8339 if (size == SIZE_MAX)
8340 return -EOVERFLOW;
8341 p = kzalloc(size, GFP_KERNEL);
8342 if (!p)
8343 return -ENOMEM;
8344
8345 ret = -EFAULT;
8346 if (copy_from_user(p, arg, size))
8347 goto out;
8348 ret = -EINVAL;
8349 if (memchr_inv(p, 0, size))
8350 goto out;
8351
8352 p->last_op = IORING_OP_LAST - 1;
8353 if (nr_args > IORING_OP_LAST)
8354 nr_args = IORING_OP_LAST;
8355
8356 for (i = 0; i < nr_args; i++) {
8357 p->ops[i].op = i;
8358 if (!io_op_defs[i].not_supported)
8359 p->ops[i].flags = IO_URING_OP_SUPPORTED;
8360 }
8361 p->ops_len = i;
8362
8363 ret = 0;
8364 if (copy_to_user(arg, p, size))
8365 ret = -EFAULT;
8366out:
8367 kfree(p);
8368 return ret;
8369}
8370
Jens Axboe071698e2020-01-28 10:04:42 -07008371static int io_register_personality(struct io_ring_ctx *ctx)
8372{
8373 const struct cred *creds = get_current_cred();
8374 int id;
8375
8376 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
8377 USHRT_MAX, GFP_KERNEL);
8378 if (id < 0)
8379 put_cred(creds);
8380 return id;
8381}
8382
8383static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
8384{
8385 const struct cred *old_creds;
8386
8387 old_creds = idr_remove(&ctx->personality_idr, id);
8388 if (old_creds) {
8389 put_cred(old_creds);
8390 return 0;
8391 }
8392
8393 return -EINVAL;
8394}
8395
8396static bool io_register_op_must_quiesce(int op)
8397{
8398 switch (op) {
8399 case IORING_UNREGISTER_FILES:
8400 case IORING_REGISTER_FILES_UPDATE:
8401 case IORING_REGISTER_PROBE:
8402 case IORING_REGISTER_PERSONALITY:
8403 case IORING_UNREGISTER_PERSONALITY:
8404 return false;
8405 default:
8406 return true;
8407 }
8408}
8409
Jens Axboeedafcce2019-01-09 09:16:05 -07008410static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
8411 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06008412 __releases(ctx->uring_lock)
8413 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07008414{
8415 int ret;
8416
Jens Axboe35fa71a2019-04-22 10:23:23 -06008417 /*
8418 * We're inside the ring mutex, if the ref is already dying, then
8419 * someone else killed the ctx or is already going through
8420 * io_uring_register().
8421 */
8422 if (percpu_ref_is_dying(&ctx->refs))
8423 return -ENXIO;
8424
Jens Axboe071698e2020-01-28 10:04:42 -07008425 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008426 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06008427
Jens Axboe05f3fb32019-12-09 11:22:50 -07008428 /*
8429 * Drop uring mutex before waiting for references to exit. If
8430 * another thread is currently inside io_uring_enter() it might
8431 * need to grab the uring_lock to make progress. If we hold it
8432 * here across the drain wait, then we can deadlock. It's safe
8433 * to drop the mutex here, since no new references will come in
8434 * after we've killed the percpu ref.
8435 */
8436 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f158b42020-05-14 17:18:39 -06008437 ret = wait_for_completion_interruptible(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008438 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07008439 if (ret) {
8440 percpu_ref_resurrect(&ctx->refs);
8441 ret = -EINTR;
8442 goto out;
8443 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008444 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008445
8446 switch (opcode) {
8447 case IORING_REGISTER_BUFFERS:
8448 ret = io_sqe_buffer_register(ctx, arg, nr_args);
8449 break;
8450 case IORING_UNREGISTER_BUFFERS:
8451 ret = -EINVAL;
8452 if (arg || nr_args)
8453 break;
8454 ret = io_sqe_buffer_unregister(ctx);
8455 break;
Jens Axboe6b063142019-01-10 22:13:58 -07008456 case IORING_REGISTER_FILES:
8457 ret = io_sqe_files_register(ctx, arg, nr_args);
8458 break;
8459 case IORING_UNREGISTER_FILES:
8460 ret = -EINVAL;
8461 if (arg || nr_args)
8462 break;
8463 ret = io_sqe_files_unregister(ctx);
8464 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06008465 case IORING_REGISTER_FILES_UPDATE:
8466 ret = io_sqe_files_update(ctx, arg, nr_args);
8467 break;
Jens Axboe9b402842019-04-11 11:45:41 -06008468 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07008469 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06008470 ret = -EINVAL;
8471 if (nr_args != 1)
8472 break;
8473 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07008474 if (ret)
8475 break;
8476 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
8477 ctx->eventfd_async = 1;
8478 else
8479 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06008480 break;
8481 case IORING_UNREGISTER_EVENTFD:
8482 ret = -EINVAL;
8483 if (arg || nr_args)
8484 break;
8485 ret = io_eventfd_unregister(ctx);
8486 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07008487 case IORING_REGISTER_PROBE:
8488 ret = -EINVAL;
8489 if (!arg || nr_args > 256)
8490 break;
8491 ret = io_probe(ctx, arg, nr_args);
8492 break;
Jens Axboe071698e2020-01-28 10:04:42 -07008493 case IORING_REGISTER_PERSONALITY:
8494 ret = -EINVAL;
8495 if (arg || nr_args)
8496 break;
8497 ret = io_register_personality(ctx);
8498 break;
8499 case IORING_UNREGISTER_PERSONALITY:
8500 ret = -EINVAL;
8501 if (arg)
8502 break;
8503 ret = io_unregister_personality(ctx, nr_args);
8504 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008505 default:
8506 ret = -EINVAL;
8507 break;
8508 }
8509
Jens Axboe071698e2020-01-28 10:04:42 -07008510 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008511 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07008512 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07008513out:
Jens Axboe0f158b42020-05-14 17:18:39 -06008514 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008515 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008516 return ret;
8517}
8518
8519SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
8520 void __user *, arg, unsigned int, nr_args)
8521{
8522 struct io_ring_ctx *ctx;
8523 long ret = -EBADF;
8524 struct fd f;
8525
8526 f = fdget(fd);
8527 if (!f.file)
8528 return -EBADF;
8529
8530 ret = -EOPNOTSUPP;
8531 if (f.file->f_op != &io_uring_fops)
8532 goto out_fput;
8533
8534 ctx = f.file->private_data;
8535
8536 mutex_lock(&ctx->uring_lock);
8537 ret = __io_uring_register(ctx, opcode, arg, nr_args);
8538 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02008539 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
8540 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07008541out_fput:
8542 fdput(f);
8543 return ret;
8544}
8545
Jens Axboe2b188cc2019-01-07 10:46:33 -07008546static int __init io_uring_init(void)
8547{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008548#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
8549 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
8550 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
8551} while (0)
8552
8553#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
8554 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
8555 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
8556 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
8557 BUILD_BUG_SQE_ELEM(1, __u8, flags);
8558 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
8559 BUILD_BUG_SQE_ELEM(4, __s32, fd);
8560 BUILD_BUG_SQE_ELEM(8, __u64, off);
8561 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
8562 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008563 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008564 BUILD_BUG_SQE_ELEM(24, __u32, len);
8565 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
8566 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
8567 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
8568 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +08008569 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
8570 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008571 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
8572 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
8573 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
8574 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
8575 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
8576 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
8577 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
8578 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008579 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008580 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
8581 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
8582 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008583 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008584
Jens Axboed3656342019-12-18 09:50:26 -07008585 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07008586 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008587 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
8588 return 0;
8589};
8590__initcall(io_uring_init);