blob: 2d86bd7b27878fe42ae23e6658c5bc6103e4110a [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
Jens Axboe52de1fe2020-02-27 10:15:42 -070047#include <net/compat.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070048#include <linux/refcount.h>
49#include <linux/uio.h>
Pavel Begunkov6b47ee62020-01-18 20:22:41 +030050#include <linux/bits.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070051
52#include <linux/sched/signal.h>
53#include <linux/fs.h>
54#include <linux/file.h>
55#include <linux/fdtable.h>
56#include <linux/mm.h>
57#include <linux/mman.h>
58#include <linux/mmu_context.h>
59#include <linux/percpu.h>
60#include <linux/slab.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070061#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070062#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070063#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070064#include <linux/net.h>
65#include <net/sock.h>
66#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070067#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070068#include <linux/anon_inodes.h>
69#include <linux/sched/mm.h>
70#include <linux/uaccess.h>
71#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070072#include <linux/sizes.h>
73#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070074#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070075#include <linux/namei.h>
76#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070077#include <linux/fadvise.h>
Jens Axboe3e4827b2020-01-08 15:18:09 -070078#include <linux/eventpoll.h>
Jens Axboeff002b32020-02-07 16:05:21 -070079#include <linux/fs_struct.h>
Pavel Begunkov7d67af22020-02-24 11:32:45 +030080#include <linux/splice.h>
Jens Axboeb41e9852020-02-17 09:52:41 -070081#include <linux/task_work.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070082
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020083#define CREATE_TRACE_POINTS
84#include <trace/events/io_uring.h>
85
Jens Axboe2b188cc2019-01-07 10:46:33 -070086#include <uapi/linux/io_uring.h>
87
88#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060089#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070090
Daniel Xu5277dea2019-09-14 14:23:45 -070091#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060092#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060093
94/*
95 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
96 */
97#define IORING_FILE_TABLE_SHIFT 9
98#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
99#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
100#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700101
102struct io_uring {
103 u32 head ____cacheline_aligned_in_smp;
104 u32 tail ____cacheline_aligned_in_smp;
105};
106
Stefan Bühler1e84b972019-04-24 23:54:16 +0200107/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000108 * This data is shared with the application through the mmap at offsets
109 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200110 *
111 * The offsets to the member fields are published through struct
112 * io_sqring_offsets when calling io_uring_setup.
113 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000114struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200115 /*
116 * Head and tail offsets into the ring; the offsets need to be
117 * masked to get valid indices.
118 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000119 * The kernel controls head of the sq ring and the tail of the cq ring,
120 * and the application controls tail of the sq ring and the head of the
121 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200122 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000123 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200124 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000125 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200126 * ring_entries - 1)
127 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000128 u32 sq_ring_mask, cq_ring_mask;
129 /* Ring sizes (constant, power of 2) */
130 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200131 /*
132 * Number of invalid entries dropped by the kernel due to
133 * invalid index stored in array
134 *
135 * Written by the kernel, shouldn't be modified by the
136 * application (i.e. get number of "new events" by comparing to
137 * cached value).
138 *
139 * After a new SQ head value was read by the application this
140 * counter includes all submissions that were dropped reaching
141 * the new SQ head (and possibly more).
142 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000143 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200144 /*
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;
230 unsigned int account_mem: 1;
231 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 Begunkovb55ce732020-04-15 00:39:49 +0300397 u32 count;
Jens Axboeb29472e2019-12-17 18:50:29 -0700398};
399
Jens Axboe9adbd452019-12-20 08:45:55 -0700400struct io_rw {
401 /* NOTE: kiocb has the file as the first member, so don't do it here */
402 struct kiocb kiocb;
403 u64 addr;
404 u64 len;
405};
406
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700407struct io_connect {
408 struct file *file;
409 struct sockaddr __user *addr;
410 int addr_len;
411};
412
Jens Axboee47293f2019-12-20 08:58:21 -0700413struct io_sr_msg {
414 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700415 union {
416 struct user_msghdr __user *msg;
417 void __user *buf;
418 };
Jens Axboee47293f2019-12-20 08:58:21 -0700419 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700420 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700421 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700422 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700423};
424
Jens Axboe15b71ab2019-12-11 11:20:36 -0700425struct io_open {
426 struct file *file;
427 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700428 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700429 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600430 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700431};
432
Jens Axboe05f3fb32019-12-09 11:22:50 -0700433struct io_files_update {
434 struct file *file;
435 u64 arg;
436 u32 nr_args;
437 u32 offset;
438};
439
Jens Axboe4840e412019-12-25 22:03:45 -0700440struct io_fadvise {
441 struct file *file;
442 u64 offset;
443 u32 len;
444 u32 advice;
445};
446
Jens Axboec1ca7572019-12-25 22:18:28 -0700447struct io_madvise {
448 struct file *file;
449 u64 addr;
450 u32 len;
451 u32 advice;
452};
453
Jens Axboe3e4827b2020-01-08 15:18:09 -0700454struct io_epoll {
455 struct file *file;
456 int epfd;
457 int op;
458 int fd;
459 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700460};
461
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300462struct io_splice {
463 struct file *file_out;
464 struct file *file_in;
465 loff_t off_out;
466 loff_t off_in;
467 u64 len;
468 unsigned int flags;
469};
470
Jens Axboeddf0322d2020-02-23 16:41:33 -0700471struct io_provide_buf {
472 struct file *file;
473 __u64 addr;
474 __s32 len;
475 __u32 bgid;
476 __u16 nbufs;
477 __u16 bid;
478};
479
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700480struct io_statx {
481 struct file *file;
482 int dfd;
483 unsigned int mask;
484 unsigned int flags;
485 struct filename *filename;
486 struct statx __user *buffer;
487};
488
Jens Axboef499a022019-12-02 16:28:46 -0700489struct io_async_connect {
490 struct sockaddr_storage address;
491};
492
Jens Axboe03b12302019-12-02 18:50:25 -0700493struct io_async_msghdr {
494 struct iovec fast_iov[UIO_FASTIOV];
495 struct iovec *iov;
496 struct sockaddr __user *uaddr;
497 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700498 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700499};
500
Jens Axboef67676d2019-12-02 11:03:47 -0700501struct io_async_rw {
502 struct iovec fast_iov[UIO_FASTIOV];
503 struct iovec *iov;
504 ssize_t nr_segs;
505 ssize_t size;
506};
507
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700508struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700509 union {
510 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700511 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700512 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700513 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700514 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700515};
516
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300517enum {
518 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
519 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
520 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
521 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
522 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700523 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300524
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300525 REQ_F_LINK_HEAD_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300526 REQ_F_LINK_NEXT_BIT,
527 REQ_F_FAIL_LINK_BIT,
528 REQ_F_INFLIGHT_BIT,
529 REQ_F_CUR_POS_BIT,
530 REQ_F_NOWAIT_BIT,
531 REQ_F_IOPOLL_COMPLETED_BIT,
532 REQ_F_LINK_TIMEOUT_BIT,
533 REQ_F_TIMEOUT_BIT,
534 REQ_F_ISREG_BIT,
535 REQ_F_MUST_PUNT_BIT,
536 REQ_F_TIMEOUT_NOSEQ_BIT,
537 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300538 REQ_F_NEED_CLEANUP_BIT,
Jens Axboe2ca10252020-02-13 17:17:35 -0700539 REQ_F_OVERFLOW_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700540 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700541 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600542 REQ_F_NO_FILE_TABLE_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700543
544 /* not a real bit, just to check we're not overflowing the space */
545 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300546};
547
548enum {
549 /* ctx owns file */
550 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
551 /* drain existing IO first */
552 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
553 /* linked sqes */
554 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
555 /* doesn't sever on completion < 0 */
556 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
557 /* IOSQE_ASYNC */
558 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700559 /* IOSQE_BUFFER_SELECT */
560 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300561
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300562 /* head of a link */
563 REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300564 /* already grabbed next link */
565 REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT),
566 /* fail rest of links */
567 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
568 /* on inflight list */
569 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
570 /* read/write uses file position */
571 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
572 /* must not punt to workers */
573 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
574 /* polled IO has completed */
575 REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT),
576 /* has linked timeout */
577 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
578 /* timeout request */
579 REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT),
580 /* regular file */
581 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
582 /* must be punted even for NONBLOCK */
583 REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT),
584 /* no timeout sequence */
585 REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT),
586 /* completion under lock */
587 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300588 /* needs cleanup */
589 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboe2ca10252020-02-13 17:17:35 -0700590 /* in overflow list */
591 REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700592 /* already went through poll handler */
593 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700594 /* buffer already selected */
595 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600596 /* doesn't need file table for this request */
597 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700598};
599
600struct async_poll {
601 struct io_poll_iocb poll;
602 struct io_wq_work work;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300603};
604
Jens Axboe09bb8392019-03-13 12:39:28 -0600605/*
606 * NOTE! Each of the iocb union members has the file pointer
607 * as the first entry in their struct definition. So you can
608 * access the file pointer through any of the sub-structs,
609 * or directly as just 'ki_filp' in this struct.
610 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700611struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700612 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600613 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700614 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700615 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700616 struct io_accept accept;
617 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700618 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700619 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700620 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700621 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700622 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700623 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700624 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700625 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700626 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700627 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300628 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700629 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700630 struct io_statx statx;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700631 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700632
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700633 struct io_async_ctx *io;
Pavel Begunkovc398ecb2020-04-09 08:17:59 +0300634 int cflags;
Jens Axboed625c6e2019-12-17 19:53:05 -0700635 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700636
637 struct io_ring_ctx *ctx;
Jens Axboed7718a92020-02-14 22:23:12 -0700638 struct list_head list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700639 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700640 refcount_t refs;
Jens Axboe3537b6a2020-04-03 11:19:06 -0600641 struct task_struct *task;
642 unsigned long fsize;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700643 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600644 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600645 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700646
Jens Axboed7718a92020-02-14 22:23:12 -0700647 struct list_head link_list;
648
Jens Axboefcb323c2019-10-24 12:39:47 -0600649 struct list_head inflight_entry;
650
Xiaoguang Wang05589552020-03-31 14:05:18 +0800651 struct percpu_ref *fixed_file_refs;
652
Jens Axboeb41e9852020-02-17 09:52:41 -0700653 union {
654 /*
655 * Only commands that never go async can use the below fields,
Jens Axboed7718a92020-02-14 22:23:12 -0700656 * obviously. Right now only IORING_OP_POLL_ADD uses them, and
657 * async armed poll handlers for regular commands. The latter
658 * restore the work, if needed.
Jens Axboeb41e9852020-02-17 09:52:41 -0700659 */
660 struct {
Jens Axboeb41e9852020-02-17 09:52:41 -0700661 struct callback_head task_work;
Jens Axboed7718a92020-02-14 22:23:12 -0700662 struct hlist_node hash_node;
663 struct async_poll *apoll;
Jens Axboeb41e9852020-02-17 09:52:41 -0700664 };
665 struct io_wq_work work;
666 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700667};
668
669#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700670#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700671
Jens Axboe9a56a232019-01-09 09:06:50 -0700672struct io_submit_state {
673 struct blk_plug plug;
674
675 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700676 * io_kiocb alloc cache
677 */
678 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300679 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700680
681 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700682 * File reference cache
683 */
684 struct file *file;
685 unsigned int fd;
686 unsigned int has_refs;
687 unsigned int used_refs;
688 unsigned int ios_left;
689};
690
Jens Axboed3656342019-12-18 09:50:26 -0700691struct io_op_def {
692 /* needs req->io allocated for deferral/async */
693 unsigned async_ctx : 1;
694 /* needs current->mm setup, does mm access */
695 unsigned needs_mm : 1;
696 /* needs req->file assigned */
697 unsigned needs_file : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700698 /* hash wq insertion if file is a regular file */
699 unsigned hash_reg_file : 1;
700 /* unbound wq insertion if file is a non-regular file */
701 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700702 /* opcode is not supported by this kernel */
703 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700704 /* needs file table */
705 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700706 /* needs ->fs */
707 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700708 /* set if opcode supports polled "wait" */
709 unsigned pollin : 1;
710 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700711 /* op supports buffer selection */
712 unsigned buffer_select : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700713};
714
715static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300716 [IORING_OP_NOP] = {},
717 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700718 .async_ctx = 1,
719 .needs_mm = 1,
720 .needs_file = 1,
721 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700722 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700723 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700724 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300725 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700726 .async_ctx = 1,
727 .needs_mm = 1,
728 .needs_file = 1,
729 .hash_reg_file = 1,
730 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700731 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700732 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300733 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700734 .needs_file = 1,
735 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300736 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700737 .needs_file = 1,
738 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700739 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700740 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300741 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700742 .needs_file = 1,
743 .hash_reg_file = 1,
744 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700745 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700746 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300747 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700748 .needs_file = 1,
749 .unbound_nonreg_file = 1,
750 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300751 [IORING_OP_POLL_REMOVE] = {},
752 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700753 .needs_file = 1,
754 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300755 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700756 .async_ctx = 1,
757 .needs_mm = 1,
758 .needs_file = 1,
759 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700760 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700761 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700762 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300763 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700764 .async_ctx = 1,
765 .needs_mm = 1,
766 .needs_file = 1,
767 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700768 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700769 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700770 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700771 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300772 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700773 .async_ctx = 1,
774 .needs_mm = 1,
775 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300776 [IORING_OP_TIMEOUT_REMOVE] = {},
777 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700778 .needs_mm = 1,
779 .needs_file = 1,
780 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700781 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700782 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700783 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300784 [IORING_OP_ASYNC_CANCEL] = {},
785 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700786 .async_ctx = 1,
787 .needs_mm = 1,
788 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300789 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700790 .async_ctx = 1,
791 .needs_mm = 1,
792 .needs_file = 1,
793 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700794 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700795 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300796 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700797 .needs_file = 1,
798 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300799 [IORING_OP_OPENAT] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700800 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700801 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700802 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300803 [IORING_OP_CLOSE] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700804 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700805 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300806 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700807 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700808 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700809 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300810 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700811 .needs_mm = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700812 .needs_fs = 1,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600813 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700814 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300815 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700816 .needs_mm = 1,
817 .needs_file = 1,
818 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700819 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700820 .buffer_select = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700821 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300822 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700823 .needs_mm = 1,
824 .needs_file = 1,
825 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700826 .pollout = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700827 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300828 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700829 .needs_file = 1,
830 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300831 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700832 .needs_mm = 1,
833 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300834 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700835 .needs_mm = 1,
836 .needs_file = 1,
837 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700838 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700839 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300840 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700841 .needs_mm = 1,
842 .needs_file = 1,
843 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700844 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700845 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700846 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300847 [IORING_OP_OPENAT2] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700848 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700849 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700850 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700851 [IORING_OP_EPOLL_CTL] = {
852 .unbound_nonreg_file = 1,
853 .file_table = 1,
854 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300855 [IORING_OP_SPLICE] = {
856 .needs_file = 1,
857 .hash_reg_file = 1,
858 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700859 },
860 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700861 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300862 [IORING_OP_TEE] = {
863 .needs_file = 1,
864 .hash_reg_file = 1,
865 .unbound_nonreg_file = 1,
866 },
Jens Axboed3656342019-12-18 09:50:26 -0700867};
868
Jens Axboe561fb042019-10-24 07:25:42 -0600869static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700870static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800871static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700872static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700873static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
874static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700875static int __io_sqe_files_update(struct io_ring_ctx *ctx,
876 struct io_uring_files_update *ip,
877 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700878static int io_grab_files(struct io_kiocb *req);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300879static void io_cleanup_req(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700880static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
881 int fd, struct file **out_file, bool fixed);
882static void __io_queue_sqe(struct io_kiocb *req,
883 const struct io_uring_sqe *sqe);
Jens Axboede0617e2019-04-06 21:51:27 -0600884
Jens Axboe2b188cc2019-01-07 10:46:33 -0700885static struct kmem_cache *req_cachep;
886
887static const struct file_operations io_uring_fops;
888
889struct sock *io_uring_get_socket(struct file *file)
890{
891#if defined(CONFIG_UNIX)
892 if (file->f_op == &io_uring_fops) {
893 struct io_ring_ctx *ctx = file->private_data;
894
895 return ctx->ring_sock->sk;
896 }
897#endif
898 return NULL;
899}
900EXPORT_SYMBOL(io_uring_get_socket);
901
Jens Axboe4a38aed22020-05-14 17:21:15 -0600902static void io_file_put_work(struct work_struct *work);
903
Pavel Begunkov0cdaf762020-05-17 14:13:40 +0300904static inline bool io_async_submit(struct io_ring_ctx *ctx)
905{
906 return ctx->flags & IORING_SETUP_SQPOLL;
907}
908
Jens Axboe2b188cc2019-01-07 10:46:33 -0700909static void io_ring_ctx_ref_free(struct percpu_ref *ref)
910{
911 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
912
Jens Axboe0f158b42020-05-14 17:18:39 -0600913 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700914}
915
916static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
917{
918 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700919 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700920
921 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
922 if (!ctx)
923 return NULL;
924
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700925 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
926 if (!ctx->fallback_req)
927 goto err;
928
Jens Axboe78076bb2019-12-04 19:56:40 -0700929 /*
930 * Use 5 bits less than the max cq entries, that should give us around
931 * 32 entries per hash list if totally full and uniformly spread.
932 */
933 hash_bits = ilog2(p->cq_entries);
934 hash_bits -= 5;
935 if (hash_bits <= 0)
936 hash_bits = 1;
937 ctx->cancel_hash_bits = hash_bits;
938 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
939 GFP_KERNEL);
940 if (!ctx->cancel_hash)
941 goto err;
942 __hash_init(ctx->cancel_hash, 1U << hash_bits);
943
Roman Gushchin21482892019-05-07 10:01:48 -0700944 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700945 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
946 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700947
948 ctx->flags = p->flags;
949 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700950 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -0600951 init_completion(&ctx->ref_comp);
952 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -0700953 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -0700954 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700955 mutex_init(&ctx->uring_lock);
956 init_waitqueue_head(&ctx->wait);
957 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700958 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600959 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600960 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600961 init_waitqueue_head(&ctx->inflight_wait);
962 spin_lock_init(&ctx->inflight_lock);
963 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe4a38aed22020-05-14 17:21:15 -0600964 INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work);
965 init_llist_head(&ctx->file_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700966 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700967err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700968 if (ctx->fallback_req)
969 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -0700970 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700971 kfree(ctx);
972 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700973}
974
Bob Liu9d858b22019-11-13 18:06:25 +0800975static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600976{
Jackie Liua197f662019-11-08 08:09:12 -0700977 struct io_ring_ctx *ctx = req->ctx;
978
Pavel Begunkov31af27c2020-04-15 00:39:50 +0300979 return req->sequence != ctx->cached_cq_tail
980 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600981}
982
Bob Liu9d858b22019-11-13 18:06:25 +0800983static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600984{
Pavel Begunkov87987892020-01-18 01:22:30 +0300985 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +0800986 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600987
Bob Liu9d858b22019-11-13 18:06:25 +0800988 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600989}
990
Jens Axboe5262f562019-09-17 12:26:57 -0600991static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
992{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600993 struct io_kiocb *req;
994
995 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700996 if (req) {
997 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
998 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800999 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07001000 list_del_init(&req->list);
1001 return req;
1002 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001003 }
1004
1005 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -06001006}
1007
Jens Axboede0617e2019-04-06 21:51:27 -06001008static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001009{
Hristo Venev75b28af2019-08-26 17:23:46 +00001010 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001011
Pavel Begunkov07910152020-01-17 03:52:46 +03001012 /* order cqe stores with ring update */
1013 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001014
Pavel Begunkov07910152020-01-17 03:52:46 +03001015 if (wq_has_sleeper(&ctx->cq_wait)) {
1016 wake_up_interruptible(&ctx->cq_wait);
1017 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001018 }
1019}
1020
Jens Axboecccf0ee2020-01-27 16:34:48 -07001021static inline void io_req_work_grab_env(struct io_kiocb *req,
1022 const struct io_op_def *def)
Jens Axboe18d9be12019-09-10 09:13:05 -06001023{
Jens Axboecccf0ee2020-01-27 16:34:48 -07001024 if (!req->work.mm && def->needs_mm) {
1025 mmgrab(current->mm);
1026 req->work.mm = current->mm;
1027 }
1028 if (!req->work.creds)
1029 req->work.creds = get_current_cred();
Jens Axboeff002b32020-02-07 16:05:21 -07001030 if (!req->work.fs && def->needs_fs) {
1031 spin_lock(&current->fs->lock);
1032 if (!current->fs->in_exec) {
1033 req->work.fs = current->fs;
1034 req->work.fs->users++;
1035 } else {
1036 req->work.flags |= IO_WQ_WORK_CANCEL;
1037 }
1038 spin_unlock(&current->fs->lock);
1039 }
Jens Axboe6ab23142020-02-08 20:23:59 -07001040 if (!req->work.task_pid)
1041 req->work.task_pid = task_pid_vnr(current);
Jens Axboecccf0ee2020-01-27 16:34:48 -07001042}
1043
1044static inline void io_req_work_drop_env(struct io_kiocb *req)
1045{
1046 if (req->work.mm) {
1047 mmdrop(req->work.mm);
1048 req->work.mm = NULL;
1049 }
1050 if (req->work.creds) {
1051 put_cred(req->work.creds);
1052 req->work.creds = NULL;
1053 }
Jens Axboeff002b32020-02-07 16:05:21 -07001054 if (req->work.fs) {
1055 struct fs_struct *fs = req->work.fs;
1056
1057 spin_lock(&req->work.fs->lock);
1058 if (--fs->users)
1059 fs = NULL;
1060 spin_unlock(&req->work.fs->lock);
1061 if (fs)
1062 free_fs_struct(fs);
1063 }
Jens Axboe561fb042019-10-24 07:25:42 -06001064}
1065
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001066static inline void io_prep_async_work(struct io_kiocb *req,
Jens Axboe94ae5e72019-11-14 19:39:52 -07001067 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -06001068{
Jens Axboed3656342019-12-18 09:50:26 -07001069 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe54a91f32019-09-10 09:15:04 -06001070
Jens Axboed3656342019-12-18 09:50:26 -07001071 if (req->flags & REQ_F_ISREG) {
1072 if (def->hash_reg_file)
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001073 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001074 } else {
1075 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001076 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001077 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001078
1079 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -06001080
Jens Axboe94ae5e72019-11-14 19:39:52 -07001081 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001082}
1083
Jackie Liua197f662019-11-08 08:09:12 -07001084static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001085{
Jackie Liua197f662019-11-08 08:09:12 -07001086 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001087 struct io_kiocb *link;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001088
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001089 io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06001090
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001091 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1092 &req->work, req->flags);
1093 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001094
1095 if (link)
1096 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -06001097}
1098
Jens Axboe5262f562019-09-17 12:26:57 -06001099static void io_kill_timeout(struct io_kiocb *req)
1100{
1101 int ret;
1102
Jens Axboe2d283902019-12-04 11:08:05 -07001103 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001104 if (ret != -1) {
1105 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -06001106 list_del_init(&req->list);
Pavel Begunkovf0e20b82020-03-07 01:15:22 +03001107 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001108 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001109 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001110 }
1111}
1112
1113static void io_kill_timeouts(struct io_ring_ctx *ctx)
1114{
1115 struct io_kiocb *req, *tmp;
1116
1117 spin_lock_irq(&ctx->completion_lock);
1118 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
1119 io_kill_timeout(req);
1120 spin_unlock_irq(&ctx->completion_lock);
1121}
1122
Pavel Begunkov04518942020-05-26 20:34:05 +03001123static void __io_queue_deferred(struct io_ring_ctx *ctx)
1124{
1125 do {
1126 struct io_kiocb *req = list_first_entry(&ctx->defer_list,
1127 struct io_kiocb, list);
1128
1129 if (req_need_defer(req))
1130 break;
1131 list_del_init(&req->list);
1132 io_queue_async_work(req);
1133 } while (!list_empty(&ctx->defer_list));
1134}
1135
Jens Axboede0617e2019-04-06 21:51:27 -06001136static void io_commit_cqring(struct io_ring_ctx *ctx)
1137{
1138 struct io_kiocb *req;
1139
Jens Axboe5262f562019-09-17 12:26:57 -06001140 while ((req = io_get_timeout_req(ctx)) != NULL)
1141 io_kill_timeout(req);
1142
Jens Axboede0617e2019-04-06 21:51:27 -06001143 __io_commit_cqring(ctx);
1144
Pavel Begunkov04518942020-05-26 20:34:05 +03001145 if (unlikely(!list_empty(&ctx->defer_list)))
1146 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001147}
1148
Jens Axboe2b188cc2019-01-07 10:46:33 -07001149static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1150{
Hristo Venev75b28af2019-08-26 17:23:46 +00001151 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001152 unsigned tail;
1153
1154 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001155 /*
1156 * writes to the cq entry need to come after reading head; the
1157 * control dependency is enough as we're using WRITE_ONCE to
1158 * fill the cq entry
1159 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001160 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001161 return NULL;
1162
1163 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001164 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001165}
1166
Jens Axboef2842ab2020-01-08 11:04:00 -07001167static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1168{
Jens Axboef0b493e2020-02-01 21:30:11 -07001169 if (!ctx->cq_ev_fd)
1170 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001171 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1172 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001173 if (!ctx->eventfd_async)
1174 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001175 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001176}
1177
Jens Axboeb41e9852020-02-17 09:52:41 -07001178static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001179{
1180 if (waitqueue_active(&ctx->wait))
1181 wake_up(&ctx->wait);
1182 if (waitqueue_active(&ctx->sqo_wait))
1183 wake_up(&ctx->sqo_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001184 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001185 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001186}
1187
Jens Axboec4a2ed72019-11-21 21:01:26 -07001188/* Returns true if there are no backlogged entries after the flush */
1189static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001190{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001191 struct io_rings *rings = ctx->rings;
1192 struct io_uring_cqe *cqe;
1193 struct io_kiocb *req;
1194 unsigned long flags;
1195 LIST_HEAD(list);
1196
1197 if (!force) {
1198 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001199 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001200 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1201 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001202 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001203 }
1204
1205 spin_lock_irqsave(&ctx->completion_lock, flags);
1206
1207 /* if force is set, the ring is going away. always drop after that */
1208 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001209 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001210
Jens Axboec4a2ed72019-11-21 21:01:26 -07001211 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001212 while (!list_empty(&ctx->cq_overflow_list)) {
1213 cqe = io_get_cqring(ctx);
1214 if (!cqe && !force)
1215 break;
1216
1217 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1218 list);
1219 list_move(&req->list, &list);
Jens Axboe2ca10252020-02-13 17:17:35 -07001220 req->flags &= ~REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001221 if (cqe) {
1222 WRITE_ONCE(cqe->user_data, req->user_data);
1223 WRITE_ONCE(cqe->res, req->result);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001224 WRITE_ONCE(cqe->flags, req->cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001225 } else {
1226 WRITE_ONCE(ctx->rings->cq_overflow,
1227 atomic_inc_return(&ctx->cached_cq_overflow));
1228 }
1229 }
1230
1231 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001232 if (cqe) {
1233 clear_bit(0, &ctx->sq_check_overflow);
1234 clear_bit(0, &ctx->cq_check_overflow);
1235 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001236 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1237 io_cqring_ev_posted(ctx);
1238
1239 while (!list_empty(&list)) {
1240 req = list_first_entry(&list, struct io_kiocb, list);
1241 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001242 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001243 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001244
1245 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001246}
1247
Jens Axboebcda7ba2020-02-23 16:42:51 -07001248static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001249{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001250 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001251 struct io_uring_cqe *cqe;
1252
Jens Axboe78e19bb2019-11-06 15:21:34 -07001253 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001254
Jens Axboe2b188cc2019-01-07 10:46:33 -07001255 /*
1256 * If we can't get a cq entry, userspace overflowed the
1257 * submission (by quite a lot). Increment the overflow count in
1258 * the ring.
1259 */
1260 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001261 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001262 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001263 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001264 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001265 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001266 WRITE_ONCE(ctx->rings->cq_overflow,
1267 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001268 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001269 if (list_empty(&ctx->cq_overflow_list)) {
1270 set_bit(0, &ctx->sq_check_overflow);
1271 set_bit(0, &ctx->cq_check_overflow);
1272 }
Jens Axboe2ca10252020-02-13 17:17:35 -07001273 req->flags |= REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001274 refcount_inc(&req->refs);
1275 req->result = res;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001276 req->cflags = cflags;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001277 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001278 }
1279}
1280
Jens Axboebcda7ba2020-02-23 16:42:51 -07001281static void io_cqring_fill_event(struct io_kiocb *req, long res)
1282{
1283 __io_cqring_fill_event(req, res, 0);
1284}
1285
1286static void __io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001287{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001288 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001289 unsigned long flags;
1290
1291 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001292 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001293 io_commit_cqring(ctx);
1294 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1295
Jens Axboe8c838782019-03-12 15:48:16 -06001296 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001297}
1298
Jens Axboebcda7ba2020-02-23 16:42:51 -07001299static void io_cqring_add_event(struct io_kiocb *req, long res)
1300{
1301 __io_cqring_add_event(req, res, 0);
1302}
1303
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001304static inline bool io_is_fallback_req(struct io_kiocb *req)
1305{
1306 return req == (struct io_kiocb *)
1307 ((unsigned long) req->ctx->fallback_req & ~1UL);
1308}
1309
1310static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1311{
1312 struct io_kiocb *req;
1313
1314 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001315 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001316 return req;
1317
1318 return NULL;
1319}
1320
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001321static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1322 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001323{
Jens Axboefd6fab22019-03-14 16:30:06 -06001324 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001325 struct io_kiocb *req;
1326
Jens Axboe2579f912019-01-09 09:10:43 -07001327 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001328 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001329 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001330 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001331 } else if (!state->free_reqs) {
1332 size_t sz;
1333 int ret;
1334
1335 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001336 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1337
1338 /*
1339 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1340 * retry single alloc to be on the safe side.
1341 */
1342 if (unlikely(ret <= 0)) {
1343 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1344 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001345 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001346 ret = 1;
1347 }
Jens Axboe2579f912019-01-09 09:10:43 -07001348 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001349 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001350 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001351 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001352 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001353 }
1354
Jens Axboe2579f912019-01-09 09:10:43 -07001355 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001356fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001357 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001358}
1359
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001360static inline void io_put_file(struct io_kiocb *req, struct file *file,
1361 bool fixed)
1362{
1363 if (fixed)
Xiaoguang Wang05589552020-03-31 14:05:18 +08001364 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001365 else
1366 fput(file);
1367}
1368
Jens Axboec6ca97b302019-12-28 12:11:08 -07001369static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001370{
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001371 if (req->flags & REQ_F_NEED_CLEANUP)
1372 io_cleanup_req(req);
1373
YueHaibing96fd84d2020-01-07 22:22:44 +08001374 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001375 if (req->file)
1376 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboe3537b6a2020-04-03 11:19:06 -06001377 if (req->task)
1378 put_task_struct(req->task);
Jens Axboecccf0ee2020-01-27 16:34:48 -07001379
1380 io_req_work_drop_env(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001381}
1382
1383static void __io_free_req(struct io_kiocb *req)
1384{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001385 __io_req_aux_free(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06001386
Jens Axboefcb323c2019-10-24 12:39:47 -06001387 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001388 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001389 unsigned long flags;
1390
1391 spin_lock_irqsave(&ctx->inflight_lock, flags);
1392 list_del(&req->inflight_entry);
1393 if (waitqueue_active(&ctx->inflight_wait))
1394 wake_up(&ctx->inflight_wait);
1395 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1396 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001397
1398 percpu_ref_put(&req->ctx->refs);
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03001399 if (likely(!io_is_fallback_req(req)))
1400 kmem_cache_free(req_cachep, req);
1401 else
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001402 clear_bit_unlock(0, (unsigned long *) &req->ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -06001403}
1404
Jens Axboec6ca97b302019-12-28 12:11:08 -07001405struct req_batch {
1406 void *reqs[IO_IOPOLL_BATCH];
1407 int to_free;
1408 int need_iter;
1409};
1410
1411static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1412{
1413 if (!rb->to_free)
1414 return;
1415 if (rb->need_iter) {
1416 int i, inflight = 0;
1417 unsigned long flags;
1418
1419 for (i = 0; i < rb->to_free; i++) {
1420 struct io_kiocb *req = rb->reqs[i];
1421
Jens Axboe10fef4b2020-01-09 07:52:28 -07001422 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001423 req->file = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08001424 percpu_ref_put(req->fixed_file_refs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001425 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001426 if (req->flags & REQ_F_INFLIGHT)
1427 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001428 __io_req_aux_free(req);
1429 }
1430 if (!inflight)
1431 goto do_free;
1432
1433 spin_lock_irqsave(&ctx->inflight_lock, flags);
1434 for (i = 0; i < rb->to_free; i++) {
1435 struct io_kiocb *req = rb->reqs[i];
1436
Jens Axboe10fef4b2020-01-09 07:52:28 -07001437 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001438 list_del(&req->inflight_entry);
1439 if (!--inflight)
1440 break;
1441 }
1442 }
1443 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1444
1445 if (waitqueue_active(&ctx->inflight_wait))
1446 wake_up(&ctx->inflight_wait);
1447 }
1448do_free:
1449 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
1450 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001451 rb->to_free = rb->need_iter = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001452}
1453
Jackie Liua197f662019-11-08 08:09:12 -07001454static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001455{
Jackie Liua197f662019-11-08 08:09:12 -07001456 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001457 int ret;
1458
Jens Axboe2d283902019-12-04 11:08:05 -07001459 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001460 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001461 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001462 io_commit_cqring(ctx);
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001463 req->flags &= ~REQ_F_LINK_HEAD;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001464 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001465 return true;
1466 }
1467
1468 return false;
1469}
1470
Jens Axboeba816ad2019-09-28 11:36:45 -06001471static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001472{
Jens Axboe2665abf2019-11-05 12:40:47 -07001473 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001474 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001475
Jens Axboe4d7dd462019-11-20 13:03:52 -07001476 /* Already got next link */
1477 if (req->flags & REQ_F_LINK_NEXT)
1478 return;
1479
Jens Axboe9e645e112019-05-10 16:07:28 -06001480 /*
1481 * The list should never be empty when we are called here. But could
1482 * potentially happen if the chain is messed up, check to be on the
1483 * safe side.
1484 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001485 while (!list_empty(&req->link_list)) {
1486 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1487 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001488
Pavel Begunkov44932332019-12-05 16:16:35 +03001489 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1490 (nxt->flags & REQ_F_TIMEOUT))) {
1491 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001492 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001493 req->flags &= ~REQ_F_LINK_TIMEOUT;
1494 continue;
1495 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001496
Pavel Begunkov44932332019-12-05 16:16:35 +03001497 list_del_init(&req->link_list);
1498 if (!list_empty(&nxt->link_list))
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001499 nxt->flags |= REQ_F_LINK_HEAD;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001500 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001501 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001502 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001503
Jens Axboe4d7dd462019-11-20 13:03:52 -07001504 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001505 if (wake_ev)
1506 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001507}
1508
1509/*
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001510 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
Jens Axboe9e645e112019-05-10 16:07:28 -06001511 */
1512static void io_fail_links(struct io_kiocb *req)
1513{
Jens Axboe2665abf2019-11-05 12:40:47 -07001514 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001515 unsigned long flags;
1516
1517 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001518
1519 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001520 struct io_kiocb *link = list_first_entry(&req->link_list,
1521 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001522
Pavel Begunkov44932332019-12-05 16:16:35 +03001523 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001524 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001525
1526 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001527 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001528 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001529 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001530 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001531 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001532 }
Jens Axboe5d960722019-11-19 15:31:28 -07001533 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001534 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001535
1536 io_commit_cqring(ctx);
1537 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1538 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001539}
1540
Jens Axboe4d7dd462019-11-20 13:03:52 -07001541static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001542{
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001543 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001544 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001545
Jens Axboe9e645e112019-05-10 16:07:28 -06001546 /*
1547 * If LINK is set, we have dependent requests in this chain. If we
1548 * didn't fail this request, queue the first one up, moving any other
1549 * dependencies to the next request. In case of failure, fail the rest
1550 * of the chain.
1551 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001552 if (req->flags & REQ_F_FAIL_LINK) {
1553 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001554 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1555 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001556 struct io_ring_ctx *ctx = req->ctx;
1557 unsigned long flags;
1558
1559 /*
1560 * If this is a timeout link, we could be racing with the
1561 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001562 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001563 */
1564 spin_lock_irqsave(&ctx->completion_lock, flags);
1565 io_req_link_next(req, nxt);
1566 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1567 } else {
1568 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001569 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001570}
Jens Axboe9e645e112019-05-10 16:07:28 -06001571
Jackie Liuc69f8db2019-11-09 11:00:08 +08001572static void io_free_req(struct io_kiocb *req)
1573{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001574 struct io_kiocb *nxt = NULL;
1575
1576 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001577 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001578
1579 if (nxt)
1580 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001581}
1582
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001583static void io_link_work_cb(struct io_wq_work **workptr)
1584{
Pavel Begunkov18a542f2020-03-23 00:23:29 +03001585 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
1586 struct io_kiocb *link;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001587
Pavel Begunkov18a542f2020-03-23 00:23:29 +03001588 link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001589 io_queue_linked_timeout(link);
1590 io_wq_submit_work(workptr);
1591}
1592
1593static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
1594{
1595 struct io_kiocb *link;
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001596 const struct io_op_def *def = &io_op_defs[nxt->opcode];
1597
1598 if ((nxt->flags & REQ_F_ISREG) && def->hash_reg_file)
1599 io_wq_hash_work(&nxt->work, file_inode(nxt->file));
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001600
1601 *workptr = &nxt->work;
1602 link = io_prep_linked_timeout(nxt);
Pavel Begunkov18a542f2020-03-23 00:23:29 +03001603 if (link)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001604 nxt->work.func = io_link_work_cb;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001605}
1606
Jens Axboeba816ad2019-09-28 11:36:45 -06001607/*
1608 * Drop reference to request, return next in chain (if there is one) if this
1609 * was the last reference to this request.
1610 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001611__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001612static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001613{
Jens Axboe2a44f462020-02-25 13:25:41 -07001614 if (refcount_dec_and_test(&req->refs)) {
1615 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001616 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001617 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001618}
1619
Jens Axboe2b188cc2019-01-07 10:46:33 -07001620static void io_put_req(struct io_kiocb *req)
1621{
Jens Axboedef596e2019-01-09 08:59:42 -07001622 if (refcount_dec_and_test(&req->refs))
1623 io_free_req(req);
1624}
1625
Pavel Begunkove9fd9392020-03-04 16:14:12 +03001626static void io_steal_work(struct io_kiocb *req,
1627 struct io_wq_work **workptr)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001628{
1629 /*
1630 * It's in an io-wq worker, so there always should be at least
1631 * one reference, which will be dropped in io_put_work() just
1632 * after the current handler returns.
1633 *
1634 * It also means, that if the counter dropped to 1, then there is
1635 * no asynchronous users left, so it's safe to steal the next work.
1636 */
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001637 if (refcount_read(&req->refs) == 1) {
1638 struct io_kiocb *nxt = NULL;
1639
1640 io_req_find_next(req, &nxt);
1641 if (nxt)
1642 io_wq_assign_next(workptr, nxt);
1643 }
1644}
1645
Jens Axboe978db572019-11-14 22:39:04 -07001646/*
1647 * Must only be used if we don't need to care about links, usually from
1648 * within the completion handling itself.
1649 */
1650static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001651{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001652 /* drop both submit and complete references */
1653 if (refcount_sub_and_test(2, &req->refs))
1654 __io_free_req(req);
1655}
1656
Jens Axboe978db572019-11-14 22:39:04 -07001657static void io_double_put_req(struct io_kiocb *req)
1658{
1659 /* drop both submit and complete references */
1660 if (refcount_sub_and_test(2, &req->refs))
1661 io_free_req(req);
1662}
1663
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001664static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001665{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001666 struct io_rings *rings = ctx->rings;
1667
Jens Axboead3eb2c2019-12-18 17:12:20 -07001668 if (test_bit(0, &ctx->cq_check_overflow)) {
1669 /*
1670 * noflush == true is from the waitqueue handler, just ensure
1671 * we wake up the task, and the next invocation will flush the
1672 * entries. We cannot safely to it from here.
1673 */
1674 if (noflush && !list_empty(&ctx->cq_overflow_list))
1675 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001676
Jens Axboead3eb2c2019-12-18 17:12:20 -07001677 io_cqring_overflow_flush(ctx, false);
1678 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001679
Jens Axboea3a0e432019-08-20 11:03:11 -06001680 /* See comment at the top of this file */
1681 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001682 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001683}
1684
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001685static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1686{
1687 struct io_rings *rings = ctx->rings;
1688
1689 /* make sure SQ entry isn't read before tail */
1690 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1691}
1692
Jens Axboe8237e042019-12-28 10:48:22 -07001693static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001694{
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001695 if ((req->flags & REQ_F_LINK_HEAD) || io_is_fallback_req(req))
Jens Axboec6ca97b302019-12-28 12:11:08 -07001696 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001697
Jens Axboec6ca97b302019-12-28 12:11:08 -07001698 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1699 rb->need_iter++;
1700
1701 rb->reqs[rb->to_free++] = req;
1702 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1703 io_free_req_many(req->ctx, rb);
1704 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001705}
1706
Jens Axboebcda7ba2020-02-23 16:42:51 -07001707static int io_put_kbuf(struct io_kiocb *req)
1708{
Jens Axboe4d954c22020-02-27 07:31:19 -07001709 struct io_buffer *kbuf;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001710 int cflags;
1711
Jens Axboe4d954c22020-02-27 07:31:19 -07001712 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001713 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
1714 cflags |= IORING_CQE_F_BUFFER;
1715 req->rw.addr = 0;
1716 kfree(kbuf);
1717 return cflags;
1718}
1719
Jens Axboedef596e2019-01-09 08:59:42 -07001720/*
1721 * Find and free completed poll iocbs
1722 */
1723static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1724 struct list_head *done)
1725{
Jens Axboe8237e042019-12-28 10:48:22 -07001726 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001727 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001728
Jens Axboec6ca97b302019-12-28 12:11:08 -07001729 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001730 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07001731 int cflags = 0;
1732
Jens Axboedef596e2019-01-09 08:59:42 -07001733 req = list_first_entry(done, struct io_kiocb, list);
1734 list_del(&req->list);
1735
Jens Axboebcda7ba2020-02-23 16:42:51 -07001736 if (req->flags & REQ_F_BUFFER_SELECTED)
1737 cflags = io_put_kbuf(req);
1738
1739 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07001740 (*nr_events)++;
1741
Jens Axboe8237e042019-12-28 10:48:22 -07001742 if (refcount_dec_and_test(&req->refs) &&
1743 !io_req_multi_free(&rb, req))
1744 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001745 }
Jens Axboedef596e2019-01-09 08:59:42 -07001746
Jens Axboe09bb8392019-03-13 12:39:28 -06001747 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08001748 if (ctx->flags & IORING_SETUP_SQPOLL)
1749 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001750 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001751}
1752
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001753static void io_iopoll_queue(struct list_head *again)
1754{
1755 struct io_kiocb *req;
1756
1757 do {
1758 req = list_first_entry(again, struct io_kiocb, list);
1759 list_del(&req->list);
1760 refcount_inc(&req->refs);
1761 io_queue_async_work(req);
1762 } while (!list_empty(again));
1763}
1764
Jens Axboedef596e2019-01-09 08:59:42 -07001765static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1766 long min)
1767{
1768 struct io_kiocb *req, *tmp;
1769 LIST_HEAD(done);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001770 LIST_HEAD(again);
Jens Axboedef596e2019-01-09 08:59:42 -07001771 bool spin;
1772 int ret;
1773
1774 /*
1775 * Only spin for completions if we don't have multiple devices hanging
1776 * off our complete list, and we're under the requested amount.
1777 */
1778 spin = !ctx->poll_multi_file && *nr_events < min;
1779
1780 ret = 0;
1781 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001782 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001783
1784 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001785 * Move completed and retryable entries to our local lists.
1786 * If we find a request that requires polling, break out
1787 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07001788 */
1789 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1790 list_move_tail(&req->list, &done);
1791 continue;
1792 }
1793 if (!list_empty(&done))
1794 break;
1795
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001796 if (req->result == -EAGAIN) {
1797 list_move_tail(&req->list, &again);
1798 continue;
1799 }
1800 if (!list_empty(&again))
1801 break;
1802
Jens Axboedef596e2019-01-09 08:59:42 -07001803 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1804 if (ret < 0)
1805 break;
1806
1807 if (ret && spin)
1808 spin = false;
1809 ret = 0;
1810 }
1811
1812 if (!list_empty(&done))
1813 io_iopoll_complete(ctx, nr_events, &done);
1814
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001815 if (!list_empty(&again))
1816 io_iopoll_queue(&again);
1817
Jens Axboedef596e2019-01-09 08:59:42 -07001818 return ret;
1819}
1820
1821/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001822 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001823 * non-spinning poll check - we'll still enter the driver poll loop, but only
1824 * as a non-spinning completion check.
1825 */
1826static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1827 long min)
1828{
Jens Axboe08f54392019-08-21 22:19:11 -06001829 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001830 int ret;
1831
1832 ret = io_do_iopoll(ctx, nr_events, min);
1833 if (ret < 0)
1834 return ret;
1835 if (!min || *nr_events >= min)
1836 return 0;
1837 }
1838
1839 return 1;
1840}
1841
1842/*
1843 * We can't just wait for polled events to come to us, we have to actively
1844 * find and complete them.
1845 */
1846static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1847{
1848 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1849 return;
1850
1851 mutex_lock(&ctx->uring_lock);
1852 while (!list_empty(&ctx->poll_list)) {
1853 unsigned int nr_events = 0;
1854
1855 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001856
1857 /*
1858 * Ensure we allow local-to-the-cpu processing to take place,
1859 * in this case we need to ensure that we reap all events.
1860 */
1861 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001862 }
1863 mutex_unlock(&ctx->uring_lock);
1864}
1865
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001866static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1867 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001868{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001869 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001870
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001871 /*
1872 * We disallow the app entering submit/complete with polling, but we
1873 * still need to lock the ring to prevent racing with polled issue
1874 * that got punted to a workqueue.
1875 */
1876 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001877 do {
1878 int tmin = 0;
1879
Jens Axboe500f9fb2019-08-19 12:15:59 -06001880 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001881 * Don't enter poll loop if we already have events pending.
1882 * If we do, we can potentially be spinning for commands that
1883 * already triggered a CQE (eg in error).
1884 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001885 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001886 break;
1887
1888 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001889 * If a submit got punted to a workqueue, we can have the
1890 * application entering polling for a command before it gets
1891 * issued. That app will hold the uring_lock for the duration
1892 * of the poll right here, so we need to take a breather every
1893 * now and then to ensure that the issue has a chance to add
1894 * the poll to the issued list. Otherwise we can spin here
1895 * forever, while the workqueue is stuck trying to acquire the
1896 * very same mutex.
1897 */
1898 if (!(++iters & 7)) {
1899 mutex_unlock(&ctx->uring_lock);
1900 mutex_lock(&ctx->uring_lock);
1901 }
1902
Jens Axboedef596e2019-01-09 08:59:42 -07001903 if (*nr_events < min)
1904 tmin = min - *nr_events;
1905
1906 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1907 if (ret <= 0)
1908 break;
1909 ret = 0;
1910 } while (min && !*nr_events && !need_resched());
1911
Jens Axboe500f9fb2019-08-19 12:15:59 -06001912 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001913 return ret;
1914}
1915
Jens Axboe491381ce2019-10-17 09:20:46 -06001916static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001917{
Jens Axboe491381ce2019-10-17 09:20:46 -06001918 /*
1919 * Tell lockdep we inherited freeze protection from submission
1920 * thread.
1921 */
1922 if (req->flags & REQ_F_ISREG) {
1923 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001924
Jens Axboe491381ce2019-10-17 09:20:46 -06001925 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001926 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001927 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001928}
1929
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001930static inline void req_set_fail_links(struct io_kiocb *req)
1931{
1932 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1933 req->flags |= REQ_F_FAIL_LINK;
1934}
1935
Jens Axboeba816ad2019-09-28 11:36:45 -06001936static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001937{
Jens Axboe9adbd452019-12-20 08:45:55 -07001938 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001939 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001940
Jens Axboe491381ce2019-10-17 09:20:46 -06001941 if (kiocb->ki_flags & IOCB_WRITE)
1942 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001943
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001944 if (res != req->result)
1945 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001946 if (req->flags & REQ_F_BUFFER_SELECTED)
1947 cflags = io_put_kbuf(req);
1948 __io_cqring_add_event(req, res, cflags);
Jens Axboeba816ad2019-09-28 11:36:45 -06001949}
1950
1951static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1952{
Jens Axboe9adbd452019-12-20 08:45:55 -07001953 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001954
1955 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001956 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001957}
1958
Jens Axboedef596e2019-01-09 08:59:42 -07001959static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1960{
Jens Axboe9adbd452019-12-20 08:45:55 -07001961 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001962
Jens Axboe491381ce2019-10-17 09:20:46 -06001963 if (kiocb->ki_flags & IOCB_WRITE)
1964 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001965
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001966 if (res != req->result)
1967 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001968 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001969 if (res != -EAGAIN)
1970 req->flags |= REQ_F_IOPOLL_COMPLETED;
1971}
1972
1973/*
1974 * After the iocb has been issued, it's safe to be found on the poll list.
1975 * Adding the kiocb to the list AFTER submission ensures that we don't
1976 * find it from a io_iopoll_getevents() thread before the issuer is done
1977 * accessing the kiocb cookie.
1978 */
1979static void io_iopoll_req_issued(struct io_kiocb *req)
1980{
1981 struct io_ring_ctx *ctx = req->ctx;
1982
1983 /*
1984 * Track whether we have multiple files in our lists. This will impact
1985 * how we do polling eventually, not spinning if we're on potentially
1986 * different devices.
1987 */
1988 if (list_empty(&ctx->poll_list)) {
1989 ctx->poll_multi_file = false;
1990 } else if (!ctx->poll_multi_file) {
1991 struct io_kiocb *list_req;
1992
1993 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1994 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001995 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001996 ctx->poll_multi_file = true;
1997 }
1998
1999 /*
2000 * For fast devices, IO may have already completed. If it has, add
2001 * it to the front so we find it first.
2002 */
2003 if (req->flags & REQ_F_IOPOLL_COMPLETED)
2004 list_add(&req->list, &ctx->poll_list);
2005 else
2006 list_add_tail(&req->list, &ctx->poll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002007
2008 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2009 wq_has_sleeper(&ctx->sqo_wait))
2010 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002011}
2012
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002013static void __io_state_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07002014{
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002015 int diff = state->has_refs - state->used_refs;
Jens Axboe9a56a232019-01-09 09:06:50 -07002016
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002017 if (diff)
2018 fput_many(state->file, diff);
2019 state->file = NULL;
2020}
2021
2022static inline void io_state_file_put(struct io_submit_state *state)
2023{
2024 if (state->file)
2025 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002026}
2027
2028/*
2029 * Get as many references to a file as we have IOs left in this submission,
2030 * assuming most submissions are for one file, or at least that each file
2031 * has more than one submission.
2032 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002033static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002034{
2035 if (!state)
2036 return fget(fd);
2037
2038 if (state->file) {
2039 if (state->fd == fd) {
2040 state->used_refs++;
2041 state->ios_left--;
2042 return state->file;
2043 }
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002044 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002045 }
2046 state->file = fget_many(fd, state->ios_left);
2047 if (!state->file)
2048 return NULL;
2049
2050 state->fd = fd;
2051 state->has_refs = state->ios_left;
2052 state->used_refs = 1;
2053 state->ios_left--;
2054 return state->file;
2055}
2056
Jens Axboe2b188cc2019-01-07 10:46:33 -07002057/*
2058 * If we tracked the file through the SCM inflight mechanism, we could support
2059 * any file. For now, just ensure that anything potentially problematic is done
2060 * inline.
2061 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002062static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002063{
2064 umode_t mode = file_inode(file)->i_mode;
2065
Jens Axboe10d59342019-12-09 20:16:22 -07002066 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002067 return true;
2068 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
2069 return true;
2070
Jens Axboeaf197f52020-04-28 13:15:06 -06002071 if (!(file->f_mode & FMODE_NOWAIT))
2072 return false;
2073
2074 if (rw == READ)
2075 return file->f_op->read_iter != NULL;
2076
2077 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002078}
2079
Jens Axboe3529d8c2019-12-19 18:24:38 -07002080static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2081 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002082{
Jens Axboedef596e2019-01-09 08:59:42 -07002083 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002084 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002085 unsigned ioprio;
2086 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002087
Jens Axboe491381ce2019-10-17 09:20:46 -06002088 if (S_ISREG(file_inode(req->file)->i_mode))
2089 req->flags |= REQ_F_ISREG;
2090
Jens Axboe2b188cc2019-01-07 10:46:33 -07002091 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002092 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2093 req->flags |= REQ_F_CUR_POS;
2094 kiocb->ki_pos = req->file->f_pos;
2095 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002096 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002097 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2098 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2099 if (unlikely(ret))
2100 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002101
2102 ioprio = READ_ONCE(sqe->ioprio);
2103 if (ioprio) {
2104 ret = ioprio_check_cap(ioprio);
2105 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002106 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002107
2108 kiocb->ki_ioprio = ioprio;
2109 } else
2110 kiocb->ki_ioprio = get_current_ioprio();
2111
Stefan Bühler8449eed2019-04-27 20:34:19 +02002112 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06002113 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
2114 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02002115 req->flags |= REQ_F_NOWAIT;
2116
2117 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002118 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02002119
Jens Axboedef596e2019-01-09 08:59:42 -07002120 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002121 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2122 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002123 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002124
Jens Axboedef596e2019-01-09 08:59:42 -07002125 kiocb->ki_flags |= IOCB_HIPRI;
2126 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06002127 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002128 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002129 if (kiocb->ki_flags & IOCB_HIPRI)
2130 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002131 kiocb->ki_complete = io_complete_rw;
2132 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002133
Jens Axboe3529d8c2019-12-19 18:24:38 -07002134 req->rw.addr = READ_ONCE(sqe->addr);
2135 req->rw.len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002136 /* we own ->private, reuse it for the buffer index / buffer ID */
Jens Axboe9adbd452019-12-20 08:45:55 -07002137 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002138 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002139 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002140}
2141
2142static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2143{
2144 switch (ret) {
2145 case -EIOCBQUEUED:
2146 break;
2147 case -ERESTARTSYS:
2148 case -ERESTARTNOINTR:
2149 case -ERESTARTNOHAND:
2150 case -ERESTART_RESTARTBLOCK:
2151 /*
2152 * We can't just restart the syscall, since previously
2153 * submitted sqes may already be in progress. Just fail this
2154 * IO with EINTR.
2155 */
2156 ret = -EINTR;
2157 /* fall through */
2158 default:
2159 kiocb->ki_complete(kiocb, ret, 0);
2160 }
2161}
2162
Pavel Begunkov014db002020-03-03 21:33:12 +03002163static void kiocb_done(struct kiocb *kiocb, ssize_t ret)
Jens Axboeba816ad2019-09-28 11:36:45 -06002164{
Jens Axboeba042912019-12-25 16:33:42 -07002165 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2166
2167 if (req->flags & REQ_F_CUR_POS)
2168 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002169 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Pavel Begunkov014db002020-03-03 21:33:12 +03002170 io_complete_rw(kiocb, ret, 0);
Jens Axboeba816ad2019-09-28 11:36:45 -06002171 else
2172 io_rw_done(kiocb, ret);
2173}
2174
Jens Axboe9adbd452019-12-20 08:45:55 -07002175static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002176 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002177{
Jens Axboe9adbd452019-12-20 08:45:55 -07002178 struct io_ring_ctx *ctx = req->ctx;
2179 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002180 struct io_mapped_ubuf *imu;
2181 unsigned index, buf_index;
2182 size_t offset;
2183 u64 buf_addr;
2184
2185 /* attempt to use fixed buffers without having provided iovecs */
2186 if (unlikely(!ctx->user_bufs))
2187 return -EFAULT;
2188
Jens Axboe9adbd452019-12-20 08:45:55 -07002189 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07002190 if (unlikely(buf_index >= ctx->nr_user_bufs))
2191 return -EFAULT;
2192
2193 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2194 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002195 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002196
2197 /* overflow */
2198 if (buf_addr + len < buf_addr)
2199 return -EFAULT;
2200 /* not inside the mapped region */
2201 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2202 return -EFAULT;
2203
2204 /*
2205 * May not be a start of buffer, set size appropriately
2206 * and advance us to the beginning.
2207 */
2208 offset = buf_addr - imu->ubuf;
2209 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002210
2211 if (offset) {
2212 /*
2213 * Don't use iov_iter_advance() here, as it's really slow for
2214 * using the latter parts of a big fixed buffer - it iterates
2215 * over each segment manually. We can cheat a bit here, because
2216 * we know that:
2217 *
2218 * 1) it's a BVEC iter, we set it up
2219 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2220 * first and last bvec
2221 *
2222 * So just find our index, and adjust the iterator afterwards.
2223 * If the offset is within the first bvec (or the whole first
2224 * bvec, just use iov_iter_advance(). This makes it easier
2225 * since we can just skip the first segment, which may not
2226 * be PAGE_SIZE aligned.
2227 */
2228 const struct bio_vec *bvec = imu->bvec;
2229
2230 if (offset <= bvec->bv_len) {
2231 iov_iter_advance(iter, offset);
2232 } else {
2233 unsigned long seg_skip;
2234
2235 /* skip first vec */
2236 offset -= bvec->bv_len;
2237 seg_skip = 1 + (offset >> PAGE_SHIFT);
2238
2239 iter->bvec = bvec + seg_skip;
2240 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002241 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002242 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002243 }
2244 }
2245
Jens Axboe5e559562019-11-13 16:12:46 -07002246 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002247}
2248
Jens Axboebcda7ba2020-02-23 16:42:51 -07002249static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2250{
2251 if (needs_lock)
2252 mutex_unlock(&ctx->uring_lock);
2253}
2254
2255static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2256{
2257 /*
2258 * "Normal" inline submissions always hold the uring_lock, since we
2259 * grab it from the system call. Same is true for the SQPOLL offload.
2260 * The only exception is when we've detached the request and issue it
2261 * from an async worker thread, grab the lock for that case.
2262 */
2263 if (needs_lock)
2264 mutex_lock(&ctx->uring_lock);
2265}
2266
2267static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2268 int bgid, struct io_buffer *kbuf,
2269 bool needs_lock)
2270{
2271 struct io_buffer *head;
2272
2273 if (req->flags & REQ_F_BUFFER_SELECTED)
2274 return kbuf;
2275
2276 io_ring_submit_lock(req->ctx, needs_lock);
2277
2278 lockdep_assert_held(&req->ctx->uring_lock);
2279
2280 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2281 if (head) {
2282 if (!list_empty(&head->list)) {
2283 kbuf = list_last_entry(&head->list, struct io_buffer,
2284 list);
2285 list_del(&kbuf->list);
2286 } else {
2287 kbuf = head;
2288 idr_remove(&req->ctx->io_buffer_idr, bgid);
2289 }
2290 if (*len > kbuf->len)
2291 *len = kbuf->len;
2292 } else {
2293 kbuf = ERR_PTR(-ENOBUFS);
2294 }
2295
2296 io_ring_submit_unlock(req->ctx, needs_lock);
2297
2298 return kbuf;
2299}
2300
Jens Axboe4d954c22020-02-27 07:31:19 -07002301static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2302 bool needs_lock)
2303{
2304 struct io_buffer *kbuf;
2305 int bgid;
2306
2307 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2308 bgid = (int) (unsigned long) req->rw.kiocb.private;
2309 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2310 if (IS_ERR(kbuf))
2311 return kbuf;
2312 req->rw.addr = (u64) (unsigned long) kbuf;
2313 req->flags |= REQ_F_BUFFER_SELECTED;
2314 return u64_to_user_ptr(kbuf->addr);
2315}
2316
2317#ifdef CONFIG_COMPAT
2318static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2319 bool needs_lock)
2320{
2321 struct compat_iovec __user *uiov;
2322 compat_ssize_t clen;
2323 void __user *buf;
2324 ssize_t len;
2325
2326 uiov = u64_to_user_ptr(req->rw.addr);
2327 if (!access_ok(uiov, sizeof(*uiov)))
2328 return -EFAULT;
2329 if (__get_user(clen, &uiov->iov_len))
2330 return -EFAULT;
2331 if (clen < 0)
2332 return -EINVAL;
2333
2334 len = clen;
2335 buf = io_rw_buffer_select(req, &len, needs_lock);
2336 if (IS_ERR(buf))
2337 return PTR_ERR(buf);
2338 iov[0].iov_base = buf;
2339 iov[0].iov_len = (compat_size_t) len;
2340 return 0;
2341}
2342#endif
2343
2344static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2345 bool needs_lock)
2346{
2347 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2348 void __user *buf;
2349 ssize_t len;
2350
2351 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2352 return -EFAULT;
2353
2354 len = iov[0].iov_len;
2355 if (len < 0)
2356 return -EINVAL;
2357 buf = io_rw_buffer_select(req, &len, needs_lock);
2358 if (IS_ERR(buf))
2359 return PTR_ERR(buf);
2360 iov[0].iov_base = buf;
2361 iov[0].iov_len = len;
2362 return 0;
2363}
2364
2365static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2366 bool needs_lock)
2367{
2368 if (req->flags & REQ_F_BUFFER_SELECTED)
2369 return 0;
2370 if (!req->rw.len)
2371 return 0;
2372 else if (req->rw.len > 1)
2373 return -EINVAL;
2374
2375#ifdef CONFIG_COMPAT
2376 if (req->ctx->compat)
2377 return io_compat_import(req, iov, needs_lock);
2378#endif
2379
2380 return __io_iov_buffer_select(req, iov, needs_lock);
2381}
2382
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002383static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
Jens Axboebcda7ba2020-02-23 16:42:51 -07002384 struct iovec **iovec, struct iov_iter *iter,
2385 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002386{
Jens Axboe9adbd452019-12-20 08:45:55 -07002387 void __user *buf = u64_to_user_ptr(req->rw.addr);
2388 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002389 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002390 u8 opcode;
2391
Jens Axboed625c6e2019-12-17 19:53:05 -07002392 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002393 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002394 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002395 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002396 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002397
Jens Axboebcda7ba2020-02-23 16:42:51 -07002398 /* buffer index only valid with fixed read/write, or buffer select */
2399 if (req->rw.kiocb.private && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002400 return -EINVAL;
2401
Jens Axboe3a6820f2019-12-22 15:19:35 -07002402 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002403 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002404 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
2405 if (IS_ERR(buf)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002406 *iovec = NULL;
Jens Axboe4d954c22020-02-27 07:31:19 -07002407 return PTR_ERR(buf);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002408 }
Jens Axboe3f9d6442020-03-11 12:27:04 -06002409 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002410 }
2411
Jens Axboe3a6820f2019-12-22 15:19:35 -07002412 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2413 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002414 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002415 }
2416
Jens Axboef67676d2019-12-02 11:03:47 -07002417 if (req->io) {
2418 struct io_async_rw *iorw = &req->io->rw;
2419
2420 *iovec = iorw->iov;
2421 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2422 if (iorw->iov == iorw->fast_iov)
2423 *iovec = NULL;
2424 return iorw->size;
2425 }
2426
Jens Axboe4d954c22020-02-27 07:31:19 -07002427 if (req->flags & REQ_F_BUFFER_SELECT) {
2428 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002429 if (!ret) {
2430 ret = (*iovec)->iov_len;
2431 iov_iter_init(iter, rw, *iovec, 1, ret);
2432 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002433 *iovec = NULL;
2434 return ret;
2435 }
2436
Jens Axboe2b188cc2019-01-07 10:46:33 -07002437#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002438 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002439 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2440 iovec, iter);
2441#endif
2442
2443 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2444}
2445
Jens Axboe32960612019-09-23 11:05:34 -06002446/*
2447 * For files that don't have ->read_iter() and ->write_iter(), handle them
2448 * by looping over ->read() or ->write() manually.
2449 */
2450static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2451 struct iov_iter *iter)
2452{
2453 ssize_t ret = 0;
2454
2455 /*
2456 * Don't support polled IO through this interface, and we can't
2457 * support non-blocking either. For the latter, this just causes
2458 * the kiocb to be handled from an async context.
2459 */
2460 if (kiocb->ki_flags & IOCB_HIPRI)
2461 return -EOPNOTSUPP;
2462 if (kiocb->ki_flags & IOCB_NOWAIT)
2463 return -EAGAIN;
2464
2465 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002466 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002467 ssize_t nr;
2468
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002469 if (!iov_iter_is_bvec(iter)) {
2470 iovec = iov_iter_iovec(iter);
2471 } else {
2472 /* fixed buffers import bvec */
2473 iovec.iov_base = kmap(iter->bvec->bv_page)
2474 + iter->iov_offset;
2475 iovec.iov_len = min(iter->count,
2476 iter->bvec->bv_len - iter->iov_offset);
2477 }
2478
Jens Axboe32960612019-09-23 11:05:34 -06002479 if (rw == READ) {
2480 nr = file->f_op->read(file, iovec.iov_base,
2481 iovec.iov_len, &kiocb->ki_pos);
2482 } else {
2483 nr = file->f_op->write(file, iovec.iov_base,
2484 iovec.iov_len, &kiocb->ki_pos);
2485 }
2486
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002487 if (iov_iter_is_bvec(iter))
2488 kunmap(iter->bvec->bv_page);
2489
Jens Axboe32960612019-09-23 11:05:34 -06002490 if (nr < 0) {
2491 if (!ret)
2492 ret = nr;
2493 break;
2494 }
2495 ret += nr;
2496 if (nr != iovec.iov_len)
2497 break;
2498 iov_iter_advance(iter, nr);
2499 }
2500
2501 return ret;
2502}
2503
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002504static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002505 struct iovec *iovec, struct iovec *fast_iov,
2506 struct iov_iter *iter)
2507{
2508 req->io->rw.nr_segs = iter->nr_segs;
2509 req->io->rw.size = io_size;
2510 req->io->rw.iov = iovec;
2511 if (!req->io->rw.iov) {
2512 req->io->rw.iov = req->io->rw.fast_iov;
Xiaoguang Wang45097da2020-04-08 22:29:58 +08002513 if (req->io->rw.iov != fast_iov)
2514 memcpy(req->io->rw.iov, fast_iov,
2515 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002516 } else {
2517 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002518 }
2519}
2520
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002521static inline int __io_alloc_async_ctx(struct io_kiocb *req)
2522{
2523 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
2524 return req->io == NULL;
2525}
2526
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002527static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002528{
Jens Axboed3656342019-12-18 09:50:26 -07002529 if (!io_op_defs[req->opcode].async_ctx)
2530 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002531
2532 return __io_alloc_async_ctx(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002533}
2534
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002535static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2536 struct iovec *iovec, struct iovec *fast_iov,
2537 struct iov_iter *iter)
2538{
Jens Axboe980ad262020-01-24 23:08:54 -07002539 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002540 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002541 if (!req->io) {
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002542 if (__io_alloc_async_ctx(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07002543 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002544
Jens Axboe5d204bc2020-01-31 12:06:52 -07002545 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2546 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002547 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002548}
2549
Jens Axboe3529d8c2019-12-19 18:24:38 -07002550static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2551 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002552{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002553 struct io_async_ctx *io;
2554 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002555 ssize_t ret;
2556
Jens Axboe3529d8c2019-12-19 18:24:38 -07002557 ret = io_prep_rw(req, sqe, force_nonblock);
2558 if (ret)
2559 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002560
Jens Axboe3529d8c2019-12-19 18:24:38 -07002561 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2562 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002563
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002564 /* either don't need iovec imported or already have it */
2565 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002566 return 0;
2567
2568 io = req->io;
2569 io->rw.iov = io->rw.fast_iov;
2570 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002571 ret = io_import_iovec(READ, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002572 req->io = io;
2573 if (ret < 0)
2574 return ret;
2575
2576 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2577 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002578}
2579
Pavel Begunkov014db002020-03-03 21:33:12 +03002580static int io_read(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002581{
2582 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002583 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002584 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002585 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002586 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002587
Jens Axboebcda7ba2020-02-23 16:42:51 -07002588 ret = io_import_iovec(READ, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07002589 if (ret < 0)
2590 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002591
Jens Axboefd6c2e42019-12-18 12:19:41 -07002592 /* Ensure we clear previously set non-block flag */
2593 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07002594 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002595
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002596 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002597 io_size = ret;
Pavel Begunkovdea3b492020-04-12 02:05:04 +03002598 if (req->flags & REQ_F_LINK_HEAD)
Jens Axboef67676d2019-12-02 11:03:47 -07002599 req->result = io_size;
2600
2601 /*
2602 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2603 * we know to async punt it even if it was opened O_NONBLOCK
2604 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002605 if (force_nonblock && !io_file_supports_async(req->file, READ))
Jens Axboef67676d2019-12-02 11:03:47 -07002606 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002607
Jens Axboe31b51512019-01-18 22:56:34 -07002608 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002609 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002610 if (!ret) {
2611 ssize_t ret2;
2612
Jens Axboe9adbd452019-12-20 08:45:55 -07002613 if (req->file->f_op->read_iter)
2614 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002615 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002616 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002617
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002618 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002619 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkov014db002020-03-03 21:33:12 +03002620 kiocb_done(kiocb, ret2);
Jens Axboef67676d2019-12-02 11:03:47 -07002621 } else {
2622copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002623 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002624 inline_vecs, &iter);
2625 if (ret)
2626 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002627 /* any defer here is final, must blocking retry */
Jens Axboe490e8962020-04-28 13:16:53 -06002628 if (!(req->flags & REQ_F_NOWAIT) &&
2629 !file_can_poll(req->file))
Jens Axboe29de5f62020-02-20 09:56:08 -07002630 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002631 return -EAGAIN;
2632 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002633 }
Jens Axboef67676d2019-12-02 11:03:47 -07002634out_free:
Pavel Begunkov1e950812020-02-06 19:51:16 +03002635 kfree(iovec);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002636 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002637 return ret;
2638}
2639
Jens Axboe3529d8c2019-12-19 18:24:38 -07002640static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2641 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002642{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002643 struct io_async_ctx *io;
2644 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002645 ssize_t ret;
2646
Jens Axboe3529d8c2019-12-19 18:24:38 -07002647 ret = io_prep_rw(req, sqe, force_nonblock);
2648 if (ret)
2649 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002650
Jens Axboe3529d8c2019-12-19 18:24:38 -07002651 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2652 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002653
Jens Axboe4ed734b2020-03-20 11:23:41 -06002654 req->fsize = rlimit(RLIMIT_FSIZE);
2655
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002656 /* either don't need iovec imported or already have it */
2657 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002658 return 0;
2659
2660 io = req->io;
2661 io->rw.iov = io->rw.fast_iov;
2662 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002663 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002664 req->io = io;
2665 if (ret < 0)
2666 return ret;
2667
2668 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2669 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002670}
2671
Pavel Begunkov014db002020-03-03 21:33:12 +03002672static int io_write(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002673{
2674 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002675 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002676 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002677 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002678 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002679
Jens Axboebcda7ba2020-02-23 16:42:51 -07002680 ret = io_import_iovec(WRITE, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07002681 if (ret < 0)
2682 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002683
Jens Axboefd6c2e42019-12-18 12:19:41 -07002684 /* Ensure we clear previously set non-block flag */
2685 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002686 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002687
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002688 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002689 io_size = ret;
Pavel Begunkovdea3b492020-04-12 02:05:04 +03002690 if (req->flags & REQ_F_LINK_HEAD)
Jens Axboef67676d2019-12-02 11:03:47 -07002691 req->result = io_size;
2692
2693 /*
2694 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2695 * we know to async punt it even if it was opened O_NONBLOCK
2696 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002697 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07002698 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07002699
Jens Axboe10d59342019-12-09 20:16:22 -07002700 /* file path doesn't support NOWAIT for non-direct_IO */
2701 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2702 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002703 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002704
Jens Axboe31b51512019-01-18 22:56:34 -07002705 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002706 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002707 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002708 ssize_t ret2;
2709
Jens Axboe2b188cc2019-01-07 10:46:33 -07002710 /*
2711 * Open-code file_start_write here to grab freeze protection,
2712 * which will be released by another thread in
2713 * io_complete_rw(). Fool lockdep by telling it the lock got
2714 * released so that it doesn't complain about the held lock when
2715 * we return to userspace.
2716 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002717 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002718 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002719 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002720 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002721 SB_FREEZE_WRITE);
2722 }
2723 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002724
Jens Axboe4ed734b2020-03-20 11:23:41 -06002725 if (!force_nonblock)
2726 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
2727
Jens Axboe9adbd452019-12-20 08:45:55 -07002728 if (req->file->f_op->write_iter)
2729 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002730 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002731 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboe4ed734b2020-03-20 11:23:41 -06002732
2733 if (!force_nonblock)
2734 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
2735
Jens Axboefaac9962020-02-07 15:45:22 -07002736 /*
Chucheng Luobff60352020-03-25 11:31:38 +08002737 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
Jens Axboefaac9962020-02-07 15:45:22 -07002738 * retry them without IOCB_NOWAIT.
2739 */
2740 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
2741 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07002742 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkov014db002020-03-03 21:33:12 +03002743 kiocb_done(kiocb, ret2);
Jens Axboef67676d2019-12-02 11:03:47 -07002744 } else {
2745copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002746 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002747 inline_vecs, &iter);
2748 if (ret)
2749 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002750 /* any defer here is final, must blocking retry */
Jens Axboe490e8962020-04-28 13:16:53 -06002751 if (!file_can_poll(req->file))
2752 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002753 return -EAGAIN;
2754 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002755 }
Jens Axboe31b51512019-01-18 22:56:34 -07002756out_free:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002757 req->flags &= ~REQ_F_NEED_CLEANUP;
Pavel Begunkov1e950812020-02-06 19:51:16 +03002758 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002759 return ret;
2760}
2761
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03002762static int __io_splice_prep(struct io_kiocb *req,
2763 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002764{
2765 struct io_splice* sp = &req->splice;
2766 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
2767 int ret;
2768
2769 if (req->flags & REQ_F_NEED_CLEANUP)
2770 return 0;
2771
2772 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002773 sp->len = READ_ONCE(sqe->len);
2774 sp->flags = READ_ONCE(sqe->splice_flags);
2775
2776 if (unlikely(sp->flags & ~valid_flags))
2777 return -EINVAL;
2778
2779 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
2780 (sp->flags & SPLICE_F_FD_IN_FIXED));
2781 if (ret)
2782 return ret;
2783 req->flags |= REQ_F_NEED_CLEANUP;
2784
2785 if (!S_ISREG(file_inode(sp->file_in)->i_mode))
2786 req->work.flags |= IO_WQ_WORK_UNBOUND;
2787
2788 return 0;
2789}
2790
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03002791static int io_tee_prep(struct io_kiocb *req,
2792 const struct io_uring_sqe *sqe)
2793{
2794 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
2795 return -EINVAL;
2796 return __io_splice_prep(req, sqe);
2797}
2798
2799static int io_tee(struct io_kiocb *req, bool force_nonblock)
2800{
2801 struct io_splice *sp = &req->splice;
2802 struct file *in = sp->file_in;
2803 struct file *out = sp->file_out;
2804 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
2805 long ret = 0;
2806
2807 if (force_nonblock)
2808 return -EAGAIN;
2809 if (sp->len)
2810 ret = do_tee(in, out, sp->len, flags);
2811
2812 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
2813 req->flags &= ~REQ_F_NEED_CLEANUP;
2814
2815 io_cqring_add_event(req, ret);
2816 if (ret != sp->len)
2817 req_set_fail_links(req);
2818 io_put_req(req);
2819 return 0;
2820}
2821
2822static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2823{
2824 struct io_splice* sp = &req->splice;
2825
2826 sp->off_in = READ_ONCE(sqe->splice_off_in);
2827 sp->off_out = READ_ONCE(sqe->off);
2828 return __io_splice_prep(req, sqe);
2829}
2830
Pavel Begunkov014db002020-03-03 21:33:12 +03002831static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002832{
2833 struct io_splice *sp = &req->splice;
2834 struct file *in = sp->file_in;
2835 struct file *out = sp->file_out;
2836 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
2837 loff_t *poff_in, *poff_out;
2838 long ret;
2839
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03002840 if (force_nonblock)
2841 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002842
2843 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
2844 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
2845 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
2846 if (force_nonblock && ret == -EAGAIN)
2847 return -EAGAIN;
2848
2849 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
2850 req->flags &= ~REQ_F_NEED_CLEANUP;
2851
2852 io_cqring_add_event(req, ret);
2853 if (ret != sp->len)
2854 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03002855 io_put_req(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002856 return 0;
2857}
2858
Jens Axboe2b188cc2019-01-07 10:46:33 -07002859/*
2860 * IORING_OP_NOP just posts a completion event, nothing else.
2861 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002862static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002863{
2864 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002865
Jens Axboedef596e2019-01-09 08:59:42 -07002866 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2867 return -EINVAL;
2868
Jens Axboe78e19bb2019-11-06 15:21:34 -07002869 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002870 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002871 return 0;
2872}
2873
Jens Axboe3529d8c2019-12-19 18:24:38 -07002874static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002875{
Jens Axboe6b063142019-01-10 22:13:58 -07002876 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002877
Jens Axboe09bb8392019-03-13 12:39:28 -06002878 if (!req->file)
2879 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002880
Jens Axboe6b063142019-01-10 22:13:58 -07002881 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002882 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002883 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002884 return -EINVAL;
2885
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002886 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2887 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2888 return -EINVAL;
2889
2890 req->sync.off = READ_ONCE(sqe->off);
2891 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002892 return 0;
2893}
2894
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002895static bool io_req_cancelled(struct io_kiocb *req)
2896{
2897 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2898 req_set_fail_links(req);
2899 io_cqring_add_event(req, -ECANCELED);
2900 io_put_req(req);
2901 return true;
2902 }
2903
2904 return false;
2905}
2906
Pavel Begunkov014db002020-03-03 21:33:12 +03002907static void __io_fsync(struct io_kiocb *req)
Jens Axboe78912932020-01-14 22:09:06 -07002908{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002909 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002910 int ret;
2911
Jens Axboe9adbd452019-12-20 08:45:55 -07002912 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002913 end > 0 ? end : LLONG_MAX,
2914 req->sync.flags & IORING_FSYNC_DATASYNC);
2915 if (ret < 0)
2916 req_set_fail_links(req);
2917 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002918 io_put_req(req);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002919}
2920
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002921static void io_fsync_finish(struct io_wq_work **workptr)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002922{
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002923 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002924
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002925 if (io_req_cancelled(req))
2926 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03002927 __io_fsync(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03002928 io_steal_work(req, workptr);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002929}
2930
Pavel Begunkov014db002020-03-03 21:33:12 +03002931static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002932{
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002933 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002934 if (force_nonblock) {
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002935 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002936 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002937 }
Pavel Begunkov014db002020-03-03 21:33:12 +03002938 __io_fsync(req);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002939 return 0;
2940}
2941
Pavel Begunkov014db002020-03-03 21:33:12 +03002942static void __io_fallocate(struct io_kiocb *req)
Jens Axboed63d1b52019-12-10 10:38:56 -07002943{
Jens Axboed63d1b52019-12-10 10:38:56 -07002944 int ret;
2945
Jens Axboe4ed734b2020-03-20 11:23:41 -06002946 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
Jens Axboed63d1b52019-12-10 10:38:56 -07002947 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2948 req->sync.len);
Jens Axboe4ed734b2020-03-20 11:23:41 -06002949 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
Jens Axboed63d1b52019-12-10 10:38:56 -07002950 if (ret < 0)
2951 req_set_fail_links(req);
2952 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002953 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002954}
2955
Jens Axboe2b188cc2019-01-07 10:46:33 -07002956static void io_fallocate_finish(struct io_wq_work **workptr)
2957{
2958 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Jens Axboed63d1b52019-12-10 10:38:56 -07002959
2960 if (io_req_cancelled(req))
2961 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03002962 __io_fallocate(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03002963 io_steal_work(req, workptr);
Jens Axboed63d1b52019-12-10 10:38:56 -07002964}
2965
2966static int io_fallocate_prep(struct io_kiocb *req,
2967 const struct io_uring_sqe *sqe)
2968{
2969 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2970 return -EINVAL;
2971
2972 req->sync.off = READ_ONCE(sqe->off);
2973 req->sync.len = READ_ONCE(sqe->addr);
2974 req->sync.mode = READ_ONCE(sqe->len);
Jens Axboe4ed734b2020-03-20 11:23:41 -06002975 req->fsize = rlimit(RLIMIT_FSIZE);
Jens Axboed63d1b52019-12-10 10:38:56 -07002976 return 0;
2977}
2978
Pavel Begunkov014db002020-03-03 21:33:12 +03002979static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07002980{
Jens Axboed63d1b52019-12-10 10:38:56 -07002981 /* fallocate always requiring blocking context */
2982 if (force_nonblock) {
Jens Axboed63d1b52019-12-10 10:38:56 -07002983 req->work.func = io_fallocate_finish;
2984 return -EAGAIN;
2985 }
2986
Pavel Begunkov014db002020-03-03 21:33:12 +03002987 __io_fallocate(req);
Jens Axboed63d1b52019-12-10 10:38:56 -07002988 return 0;
2989}
2990
Jens Axboe15b71ab2019-12-11 11:20:36 -07002991static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2992{
Jens Axboef8748882020-01-08 17:47:02 -07002993 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002994 int ret;
2995
2996 if (sqe->ioprio || sqe->buf_index)
2997 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03002998 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07002999 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003000 if (req->flags & REQ_F_NEED_CLEANUP)
3001 return 0;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003002
3003 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07003004 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07003005 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07003006 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003007 if (force_o_largefile())
3008 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003009
Jens Axboef8748882020-01-08 17:47:02 -07003010 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003011 if (IS_ERR(req->open.filename)) {
3012 ret = PTR_ERR(req->open.filename);
3013 req->open.filename = NULL;
3014 return ret;
3015 }
3016
Jens Axboe4022e7a2020-03-19 19:23:18 -06003017 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003018 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003019 return 0;
3020}
3021
Jens Axboecebdb982020-01-08 17:59:24 -07003022static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3023{
3024 struct open_how __user *how;
3025 const char __user *fname;
3026 size_t len;
3027 int ret;
3028
3029 if (sqe->ioprio || sqe->buf_index)
3030 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003031 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003032 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003033 if (req->flags & REQ_F_NEED_CLEANUP)
3034 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07003035
3036 req->open.dfd = READ_ONCE(sqe->fd);
3037 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3038 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3039 len = READ_ONCE(sqe->len);
3040
3041 if (len < OPEN_HOW_SIZE_VER0)
3042 return -EINVAL;
3043
3044 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3045 len);
3046 if (ret)
3047 return ret;
3048
3049 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
3050 req->open.how.flags |= O_LARGEFILE;
3051
3052 req->open.filename = getname(fname);
3053 if (IS_ERR(req->open.filename)) {
3054 ret = PTR_ERR(req->open.filename);
3055 req->open.filename = NULL;
3056 return ret;
3057 }
3058
Jens Axboe4022e7a2020-03-19 19:23:18 -06003059 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003060 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboecebdb982020-01-08 17:59:24 -07003061 return 0;
3062}
3063
Pavel Begunkov014db002020-03-03 21:33:12 +03003064static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003065{
3066 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003067 struct file *file;
3068 int ret;
3069
Jens Axboef86cd202020-01-29 13:46:44 -07003070 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003071 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003072
Jens Axboecebdb982020-01-08 17:59:24 -07003073 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003074 if (ret)
3075 goto err;
3076
Jens Axboe4022e7a2020-03-19 19:23:18 -06003077 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003078 if (ret < 0)
3079 goto err;
3080
3081 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3082 if (IS_ERR(file)) {
3083 put_unused_fd(ret);
3084 ret = PTR_ERR(file);
3085 } else {
3086 fsnotify_open(file);
3087 fd_install(ret, file);
3088 }
3089err:
3090 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003091 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003092 if (ret < 0)
3093 req_set_fail_links(req);
3094 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003095 io_put_req(req);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003096 return 0;
3097}
3098
Pavel Begunkov014db002020-03-03 21:33:12 +03003099static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003100{
3101 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
Pavel Begunkov014db002020-03-03 21:33:12 +03003102 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003103}
3104
Jens Axboe067524e2020-03-02 16:32:28 -07003105static int io_remove_buffers_prep(struct io_kiocb *req,
3106 const struct io_uring_sqe *sqe)
3107{
3108 struct io_provide_buf *p = &req->pbuf;
3109 u64 tmp;
3110
3111 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3112 return -EINVAL;
3113
3114 tmp = READ_ONCE(sqe->fd);
3115 if (!tmp || tmp > USHRT_MAX)
3116 return -EINVAL;
3117
3118 memset(p, 0, sizeof(*p));
3119 p->nbufs = tmp;
3120 p->bgid = READ_ONCE(sqe->buf_group);
3121 return 0;
3122}
3123
3124static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3125 int bgid, unsigned nbufs)
3126{
3127 unsigned i = 0;
3128
3129 /* shouldn't happen */
3130 if (!nbufs)
3131 return 0;
3132
3133 /* the head kbuf is the list itself */
3134 while (!list_empty(&buf->list)) {
3135 struct io_buffer *nxt;
3136
3137 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3138 list_del(&nxt->list);
3139 kfree(nxt);
3140 if (++i == nbufs)
3141 return i;
3142 }
3143 i++;
3144 kfree(buf);
3145 idr_remove(&ctx->io_buffer_idr, bgid);
3146
3147 return i;
3148}
3149
3150static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock)
3151{
3152 struct io_provide_buf *p = &req->pbuf;
3153 struct io_ring_ctx *ctx = req->ctx;
3154 struct io_buffer *head;
3155 int ret = 0;
3156
3157 io_ring_submit_lock(ctx, !force_nonblock);
3158
3159 lockdep_assert_held(&ctx->uring_lock);
3160
3161 ret = -ENOENT;
3162 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3163 if (head)
3164 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3165
3166 io_ring_submit_lock(ctx, !force_nonblock);
3167 if (ret < 0)
3168 req_set_fail_links(req);
3169 io_cqring_add_event(req, ret);
3170 io_put_req(req);
3171 return 0;
3172}
3173
Jens Axboeddf0322d2020-02-23 16:41:33 -07003174static int io_provide_buffers_prep(struct io_kiocb *req,
3175 const struct io_uring_sqe *sqe)
3176{
3177 struct io_provide_buf *p = &req->pbuf;
3178 u64 tmp;
3179
3180 if (sqe->ioprio || sqe->rw_flags)
3181 return -EINVAL;
3182
3183 tmp = READ_ONCE(sqe->fd);
3184 if (!tmp || tmp > USHRT_MAX)
3185 return -E2BIG;
3186 p->nbufs = tmp;
3187 p->addr = READ_ONCE(sqe->addr);
3188 p->len = READ_ONCE(sqe->len);
3189
3190 if (!access_ok(u64_to_user_ptr(p->addr), p->len))
3191 return -EFAULT;
3192
3193 p->bgid = READ_ONCE(sqe->buf_group);
3194 tmp = READ_ONCE(sqe->off);
3195 if (tmp > USHRT_MAX)
3196 return -E2BIG;
3197 p->bid = tmp;
3198 return 0;
3199}
3200
3201static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3202{
3203 struct io_buffer *buf;
3204 u64 addr = pbuf->addr;
3205 int i, bid = pbuf->bid;
3206
3207 for (i = 0; i < pbuf->nbufs; i++) {
3208 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3209 if (!buf)
3210 break;
3211
3212 buf->addr = addr;
3213 buf->len = pbuf->len;
3214 buf->bid = bid;
3215 addr += pbuf->len;
3216 bid++;
3217 if (!*head) {
3218 INIT_LIST_HEAD(&buf->list);
3219 *head = buf;
3220 } else {
3221 list_add_tail(&buf->list, &(*head)->list);
3222 }
3223 }
3224
3225 return i ? i : -ENOMEM;
3226}
3227
Jens Axboeddf0322d2020-02-23 16:41:33 -07003228static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock)
3229{
3230 struct io_provide_buf *p = &req->pbuf;
3231 struct io_ring_ctx *ctx = req->ctx;
3232 struct io_buffer *head, *list;
3233 int ret = 0;
3234
3235 io_ring_submit_lock(ctx, !force_nonblock);
3236
3237 lockdep_assert_held(&ctx->uring_lock);
3238
3239 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3240
3241 ret = io_add_buffers(p, &head);
3242 if (ret < 0)
3243 goto out;
3244
3245 if (!list) {
3246 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3247 GFP_KERNEL);
3248 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003249 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003250 goto out;
3251 }
3252 }
3253out:
3254 io_ring_submit_unlock(ctx, !force_nonblock);
3255 if (ret < 0)
3256 req_set_fail_links(req);
3257 io_cqring_add_event(req, ret);
3258 io_put_req(req);
3259 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003260}
3261
Jens Axboe3e4827b2020-01-08 15:18:09 -07003262static int io_epoll_ctl_prep(struct io_kiocb *req,
3263 const struct io_uring_sqe *sqe)
3264{
3265#if defined(CONFIG_EPOLL)
3266 if (sqe->ioprio || sqe->buf_index)
3267 return -EINVAL;
3268
3269 req->epoll.epfd = READ_ONCE(sqe->fd);
3270 req->epoll.op = READ_ONCE(sqe->len);
3271 req->epoll.fd = READ_ONCE(sqe->off);
3272
3273 if (ep_op_has_event(req->epoll.op)) {
3274 struct epoll_event __user *ev;
3275
3276 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3277 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3278 return -EFAULT;
3279 }
3280
3281 return 0;
3282#else
3283 return -EOPNOTSUPP;
3284#endif
3285}
3286
Pavel Begunkov014db002020-03-03 21:33:12 +03003287static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003288{
3289#if defined(CONFIG_EPOLL)
3290 struct io_epoll *ie = &req->epoll;
3291 int ret;
3292
3293 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3294 if (force_nonblock && ret == -EAGAIN)
3295 return -EAGAIN;
3296
3297 if (ret < 0)
3298 req_set_fail_links(req);
3299 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003300 io_put_req(req);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003301 return 0;
3302#else
3303 return -EOPNOTSUPP;
3304#endif
3305}
3306
Jens Axboec1ca7572019-12-25 22:18:28 -07003307static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3308{
3309#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3310 if (sqe->ioprio || sqe->buf_index || sqe->off)
3311 return -EINVAL;
3312
3313 req->madvise.addr = READ_ONCE(sqe->addr);
3314 req->madvise.len = READ_ONCE(sqe->len);
3315 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3316 return 0;
3317#else
3318 return -EOPNOTSUPP;
3319#endif
3320}
3321
Pavel Begunkov014db002020-03-03 21:33:12 +03003322static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003323{
3324#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3325 struct io_madvise *ma = &req->madvise;
3326 int ret;
3327
3328 if (force_nonblock)
3329 return -EAGAIN;
3330
3331 ret = do_madvise(ma->addr, ma->len, ma->advice);
3332 if (ret < 0)
3333 req_set_fail_links(req);
3334 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003335 io_put_req(req);
Jens Axboec1ca7572019-12-25 22:18:28 -07003336 return 0;
3337#else
3338 return -EOPNOTSUPP;
3339#endif
3340}
3341
Jens Axboe4840e412019-12-25 22:03:45 -07003342static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3343{
3344 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3345 return -EINVAL;
3346
3347 req->fadvise.offset = READ_ONCE(sqe->off);
3348 req->fadvise.len = READ_ONCE(sqe->len);
3349 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3350 return 0;
3351}
3352
Pavel Begunkov014db002020-03-03 21:33:12 +03003353static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07003354{
3355 struct io_fadvise *fa = &req->fadvise;
3356 int ret;
3357
Jens Axboe3e694262020-02-01 09:22:49 -07003358 if (force_nonblock) {
3359 switch (fa->advice) {
3360 case POSIX_FADV_NORMAL:
3361 case POSIX_FADV_RANDOM:
3362 case POSIX_FADV_SEQUENTIAL:
3363 break;
3364 default:
3365 return -EAGAIN;
3366 }
3367 }
Jens Axboe4840e412019-12-25 22:03:45 -07003368
3369 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3370 if (ret < 0)
3371 req_set_fail_links(req);
3372 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003373 io_put_req(req);
Jens Axboe4840e412019-12-25 22:03:45 -07003374 return 0;
3375}
3376
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003377static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3378{
Jens Axboef8748882020-01-08 17:47:02 -07003379 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003380 unsigned lookup_flags;
3381 int ret;
3382
3383 if (sqe->ioprio || sqe->buf_index)
3384 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003385 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003386 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003387 if (req->flags & REQ_F_NEED_CLEANUP)
3388 return 0;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003389
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003390 req->statx.dfd = READ_ONCE(sqe->fd);
3391 req->statx.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07003392 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003393 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3394 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003395
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003396 if (vfs_stat_set_lookup_flags(&lookup_flags, req->statx.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003397 return -EINVAL;
3398
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003399 req->statx.filename = getname_flags(fname, lookup_flags, NULL);
3400 if (IS_ERR(req->statx.filename)) {
3401 ret = PTR_ERR(req->statx.filename);
3402 req->statx.filename = NULL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003403 return ret;
3404 }
3405
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003406 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003407 return 0;
3408}
3409
Pavel Begunkov014db002020-03-03 21:33:12 +03003410static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003411{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003412 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003413 unsigned lookup_flags;
3414 struct path path;
3415 struct kstat stat;
3416 int ret;
3417
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003418 if (force_nonblock) {
3419 /* only need file table for an actual valid fd */
3420 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
3421 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003422 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003423 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003424
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003425 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003426 return -EINVAL;
3427
3428retry:
3429 /* filename_lookup() drops it, keep a reference */
3430 ctx->filename->refcnt++;
3431
3432 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
3433 NULL);
3434 if (ret)
3435 goto err;
3436
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003437 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003438 path_put(&path);
3439 if (retry_estale(ret, lookup_flags)) {
3440 lookup_flags |= LOOKUP_REVAL;
3441 goto retry;
3442 }
3443 if (!ret)
3444 ret = cp_statx(&stat, ctx->buffer);
3445err:
3446 putname(ctx->filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003447 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003448 if (ret < 0)
3449 req_set_fail_links(req);
3450 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003451 io_put_req(req);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003452 return 0;
3453}
3454
Jens Axboeb5dba592019-12-11 14:02:38 -07003455static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3456{
3457 /*
3458 * If we queue this for async, it must not be cancellable. That would
3459 * leave the 'file' in an undeterminate state.
3460 */
3461 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
3462
3463 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
3464 sqe->rw_flags || sqe->buf_index)
3465 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003466 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003467 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07003468
3469 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboeb5dba592019-12-11 14:02:38 -07003470 return 0;
3471}
3472
Pavel Begunkova93b3332020-02-08 14:04:34 +03003473/* only called when __close_fd_get_file() is done */
Pavel Begunkov014db002020-03-03 21:33:12 +03003474static void __io_close_finish(struct io_kiocb *req)
Pavel Begunkova93b3332020-02-08 14:04:34 +03003475{
3476 int ret;
3477
3478 ret = filp_close(req->close.put_file, req->work.files);
3479 if (ret < 0)
3480 req_set_fail_links(req);
3481 io_cqring_add_event(req, ret);
3482 fput(req->close.put_file);
Pavel Begunkov014db002020-03-03 21:33:12 +03003483 io_put_req(req);
Pavel Begunkova93b3332020-02-08 14:04:34 +03003484}
3485
Jens Axboeb5dba592019-12-11 14:02:38 -07003486static void io_close_finish(struct io_wq_work **workptr)
3487{
3488 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Jens Axboeb5dba592019-12-11 14:02:38 -07003489
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03003490 /* not cancellable, don't do io_req_cancelled() */
Pavel Begunkov014db002020-03-03 21:33:12 +03003491 __io_close_finish(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03003492 io_steal_work(req, workptr);
Jens Axboeb5dba592019-12-11 14:02:38 -07003493}
3494
Pavel Begunkov014db002020-03-03 21:33:12 +03003495static int io_close(struct io_kiocb *req, bool force_nonblock)
Jens Axboeb5dba592019-12-11 14:02:38 -07003496{
3497 int ret;
3498
3499 req->close.put_file = NULL;
3500 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03003501 if (ret < 0)
3502 return (ret == -ENOENT) ? -EBADF : ret;
Jens Axboeb5dba592019-12-11 14:02:38 -07003503
3504 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkova2100672020-03-02 23:45:16 +03003505 if (req->close.put_file->f_op->flush && force_nonblock) {
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03003506 /* avoid grabbing files - we don't need the files */
3507 req->flags |= REQ_F_NO_FILE_TABLE | REQ_F_MUST_PUNT;
Pavel Begunkova2100672020-03-02 23:45:16 +03003508 req->work.func = io_close_finish;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03003509 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03003510 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003511
3512 /*
3513 * No ->flush(), safely close from here and just punt the
3514 * fput() to async context.
3515 */
Pavel Begunkov014db002020-03-03 21:33:12 +03003516 __io_close_finish(req);
Jens Axboe1a417f42020-01-31 17:16:48 -07003517 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07003518}
3519
Jens Axboe3529d8c2019-12-19 18:24:38 -07003520static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003521{
3522 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003523
3524 if (!req->file)
3525 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003526
3527 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3528 return -EINVAL;
3529 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
3530 return -EINVAL;
3531
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003532 req->sync.off = READ_ONCE(sqe->off);
3533 req->sync.len = READ_ONCE(sqe->len);
3534 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003535 return 0;
3536}
3537
Pavel Begunkov014db002020-03-03 21:33:12 +03003538static void __io_sync_file_range(struct io_kiocb *req)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003539{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003540 int ret;
3541
Jens Axboe9adbd452019-12-20 08:45:55 -07003542 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003543 req->sync.flags);
3544 if (ret < 0)
3545 req_set_fail_links(req);
3546 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003547 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03003548}
3549
3550
3551static void io_sync_file_range_finish(struct io_wq_work **workptr)
3552{
3553 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03003554
3555 if (io_req_cancelled(req))
3556 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03003557 __io_sync_file_range(req);
Pavel Begunkov7759a0b2020-05-01 17:09:36 +03003558 io_steal_work(req, workptr);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003559}
3560
Pavel Begunkov014db002020-03-03 21:33:12 +03003561static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003562{
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003563 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003564 if (force_nonblock) {
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003565 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003566 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003567 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003568
Pavel Begunkov014db002020-03-03 21:33:12 +03003569 __io_sync_file_range(req);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003570 return 0;
3571}
3572
YueHaibing469956e2020-03-04 15:53:52 +08003573#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003574static int io_setup_async_msg(struct io_kiocb *req,
3575 struct io_async_msghdr *kmsg)
3576{
3577 if (req->io)
3578 return -EAGAIN;
3579 if (io_alloc_async_ctx(req)) {
3580 if (kmsg->iov != kmsg->fast_iov)
3581 kfree(kmsg->iov);
3582 return -ENOMEM;
3583 }
3584 req->flags |= REQ_F_NEED_CLEANUP;
3585 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
3586 return -EAGAIN;
3587}
3588
Jens Axboe3529d8c2019-12-19 18:24:38 -07003589static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06003590{
Jens Axboee47293f2019-12-20 08:58:21 -07003591 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003592 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003593 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003594
Jens Axboee47293f2019-12-20 08:58:21 -07003595 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3596 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07003597 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003598
Jens Axboed8768362020-02-27 14:17:49 -07003599#ifdef CONFIG_COMPAT
3600 if (req->ctx->compat)
3601 sr->msg_flags |= MSG_CMSG_COMPAT;
3602#endif
3603
Jens Axboefddafac2020-01-04 20:19:44 -07003604 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003605 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003606 /* iovec is already imported */
3607 if (req->flags & REQ_F_NEED_CLEANUP)
3608 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003609
Jens Axboed9688562019-12-09 19:35:20 -07003610 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003611 ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003612 &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003613 if (!ret)
3614 req->flags |= REQ_F_NEED_CLEANUP;
3615 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003616}
3617
Pavel Begunkov014db002020-03-03 21:33:12 +03003618static int io_sendmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003619{
Jens Axboe0b416c32019-12-15 10:57:46 -07003620 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07003621 struct socket *sock;
3622 int ret;
3623
3624 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3625 return -EINVAL;
3626
3627 sock = sock_from_file(req->file, &ret);
3628 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003629 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003630 unsigned flags;
3631
Jens Axboe03b12302019-12-02 18:50:25 -07003632 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003633 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003634 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003635 /* if iov is set, it's allocated already */
3636 if (!kmsg->iov)
3637 kmsg->iov = kmsg->fast_iov;
3638 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003639 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003640 struct io_sr_msg *sr = &req->sr_msg;
3641
Jens Axboe0b416c32019-12-15 10:57:46 -07003642 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003643 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003644
3645 io.msg.iov = io.msg.fast_iov;
3646 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3647 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003648 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003649 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003650 }
3651
Jens Axboee47293f2019-12-20 08:58:21 -07003652 flags = req->sr_msg.msg_flags;
3653 if (flags & MSG_DONTWAIT)
3654 req->flags |= REQ_F_NOWAIT;
3655 else if (force_nonblock)
3656 flags |= MSG_DONTWAIT;
3657
Jens Axboe0b416c32019-12-15 10:57:46 -07003658 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003659 if (force_nonblock && ret == -EAGAIN)
3660 return io_setup_async_msg(req, kmsg);
Jens Axboe03b12302019-12-02 18:50:25 -07003661 if (ret == -ERESTARTSYS)
3662 ret = -EINTR;
3663 }
3664
Pavel Begunkov1e950812020-02-06 19:51:16 +03003665 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003666 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003667 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe03b12302019-12-02 18:50:25 -07003668 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003669 if (ret < 0)
3670 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003671 io_put_req(req);
Jens Axboe03b12302019-12-02 18:50:25 -07003672 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003673}
3674
Pavel Begunkov014db002020-03-03 21:33:12 +03003675static int io_send(struct io_kiocb *req, bool force_nonblock)
Jens Axboefddafac2020-01-04 20:19:44 -07003676{
Jens Axboefddafac2020-01-04 20:19:44 -07003677 struct socket *sock;
3678 int ret;
3679
3680 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3681 return -EINVAL;
3682
3683 sock = sock_from_file(req->file, &ret);
3684 if (sock) {
3685 struct io_sr_msg *sr = &req->sr_msg;
3686 struct msghdr msg;
3687 struct iovec iov;
3688 unsigned flags;
3689
3690 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3691 &msg.msg_iter);
3692 if (ret)
3693 return ret;
3694
3695 msg.msg_name = NULL;
3696 msg.msg_control = NULL;
3697 msg.msg_controllen = 0;
3698 msg.msg_namelen = 0;
3699
3700 flags = req->sr_msg.msg_flags;
3701 if (flags & MSG_DONTWAIT)
3702 req->flags |= REQ_F_NOWAIT;
3703 else if (force_nonblock)
3704 flags |= MSG_DONTWAIT;
3705
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003706 msg.msg_flags = flags;
3707 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003708 if (force_nonblock && ret == -EAGAIN)
3709 return -EAGAIN;
3710 if (ret == -ERESTARTSYS)
3711 ret = -EINTR;
3712 }
3713
3714 io_cqring_add_event(req, ret);
3715 if (ret < 0)
3716 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003717 io_put_req(req);
Jens Axboefddafac2020-01-04 20:19:44 -07003718 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003719}
3720
Jens Axboe52de1fe2020-02-27 10:15:42 -07003721static int __io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
3722{
3723 struct io_sr_msg *sr = &req->sr_msg;
3724 struct iovec __user *uiov;
3725 size_t iov_len;
3726 int ret;
3727
3728 ret = __copy_msghdr_from_user(&io->msg.msg, sr->msg, &io->msg.uaddr,
3729 &uiov, &iov_len);
3730 if (ret)
3731 return ret;
3732
3733 if (req->flags & REQ_F_BUFFER_SELECT) {
3734 if (iov_len > 1)
3735 return -EINVAL;
3736 if (copy_from_user(io->msg.iov, uiov, sizeof(*uiov)))
3737 return -EFAULT;
3738 sr->len = io->msg.iov[0].iov_len;
3739 iov_iter_init(&io->msg.msg.msg_iter, READ, io->msg.iov, 1,
3740 sr->len);
3741 io->msg.iov = NULL;
3742 } else {
3743 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
3744 &io->msg.iov, &io->msg.msg.msg_iter);
3745 if (ret > 0)
3746 ret = 0;
3747 }
3748
3749 return ret;
3750}
3751
3752#ifdef CONFIG_COMPAT
3753static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
3754 struct io_async_ctx *io)
3755{
3756 struct compat_msghdr __user *msg_compat;
3757 struct io_sr_msg *sr = &req->sr_msg;
3758 struct compat_iovec __user *uiov;
3759 compat_uptr_t ptr;
3760 compat_size_t len;
3761 int ret;
3762
3763 msg_compat = (struct compat_msghdr __user *) sr->msg;
3764 ret = __get_compat_msghdr(&io->msg.msg, msg_compat, &io->msg.uaddr,
3765 &ptr, &len);
3766 if (ret)
3767 return ret;
3768
3769 uiov = compat_ptr(ptr);
3770 if (req->flags & REQ_F_BUFFER_SELECT) {
3771 compat_ssize_t clen;
3772
3773 if (len > 1)
3774 return -EINVAL;
3775 if (!access_ok(uiov, sizeof(*uiov)))
3776 return -EFAULT;
3777 if (__get_user(clen, &uiov->iov_len))
3778 return -EFAULT;
3779 if (clen < 0)
3780 return -EINVAL;
3781 sr->len = io->msg.iov[0].iov_len;
3782 io->msg.iov = NULL;
3783 } else {
3784 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
3785 &io->msg.iov,
3786 &io->msg.msg.msg_iter);
3787 if (ret < 0)
3788 return ret;
3789 }
3790
3791 return 0;
3792}
Jens Axboe03b12302019-12-02 18:50:25 -07003793#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07003794
3795static int io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
3796{
3797 io->msg.iov = io->msg.fast_iov;
3798
3799#ifdef CONFIG_COMPAT
3800 if (req->ctx->compat)
3801 return __io_compat_recvmsg_copy_hdr(req, io);
3802#endif
3803
3804 return __io_recvmsg_copy_hdr(req, io);
3805}
3806
Jens Axboebcda7ba2020-02-23 16:42:51 -07003807static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
3808 int *cflags, bool needs_lock)
3809{
3810 struct io_sr_msg *sr = &req->sr_msg;
3811 struct io_buffer *kbuf;
3812
3813 if (!(req->flags & REQ_F_BUFFER_SELECT))
3814 return NULL;
3815
3816 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
3817 if (IS_ERR(kbuf))
3818 return kbuf;
3819
3820 sr->kbuf = kbuf;
3821 req->flags |= REQ_F_BUFFER_SELECTED;
3822
3823 *cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
3824 *cflags |= IORING_CQE_F_BUFFER;
3825 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07003826}
3827
Jens Axboe3529d8c2019-12-19 18:24:38 -07003828static int io_recvmsg_prep(struct io_kiocb *req,
3829 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003830{
Jens Axboee47293f2019-12-20 08:58:21 -07003831 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003832 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003833 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07003834
Jens Axboe3529d8c2019-12-19 18:24:38 -07003835 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3836 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003837 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003838 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003839
Jens Axboed8768362020-02-27 14:17:49 -07003840#ifdef CONFIG_COMPAT
3841 if (req->ctx->compat)
3842 sr->msg_flags |= MSG_CMSG_COMPAT;
3843#endif
3844
Jens Axboefddafac2020-01-04 20:19:44 -07003845 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003846 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003847 /* iovec is already imported */
3848 if (req->flags & REQ_F_NEED_CLEANUP)
3849 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003850
Jens Axboe52de1fe2020-02-27 10:15:42 -07003851 ret = io_recvmsg_copy_hdr(req, io);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003852 if (!ret)
3853 req->flags |= REQ_F_NEED_CLEANUP;
3854 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003855}
3856
Pavel Begunkov014db002020-03-03 21:33:12 +03003857static int io_recvmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003858{
Jens Axboe0b416c32019-12-15 10:57:46 -07003859 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003860 struct socket *sock;
Jens Axboe52de1fe2020-02-27 10:15:42 -07003861 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003862
3863 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3864 return -EINVAL;
3865
3866 sock = sock_from_file(req->file, &ret);
3867 if (sock) {
Jens Axboe52de1fe2020-02-27 10:15:42 -07003868 struct io_buffer *kbuf;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003869 struct io_async_ctx io;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003870 unsigned flags;
3871
Jens Axboe03b12302019-12-02 18:50:25 -07003872 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003873 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003874 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003875 /* if iov is set, it's allocated already */
3876 if (!kmsg->iov)
3877 kmsg->iov = kmsg->fast_iov;
3878 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003879 } else {
Jens Axboe0b416c32019-12-15 10:57:46 -07003880 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003881 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003882
Jens Axboe52de1fe2020-02-27 10:15:42 -07003883 ret = io_recvmsg_copy_hdr(req, &io);
Jens Axboe03b12302019-12-02 18:50:25 -07003884 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003885 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003886 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003887
Jens Axboe52de1fe2020-02-27 10:15:42 -07003888 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
3889 if (IS_ERR(kbuf)) {
3890 return PTR_ERR(kbuf);
3891 } else if (kbuf) {
3892 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3893 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
3894 1, req->sr_msg.len);
3895 }
3896
Jens Axboee47293f2019-12-20 08:58:21 -07003897 flags = req->sr_msg.msg_flags;
3898 if (flags & MSG_DONTWAIT)
3899 req->flags |= REQ_F_NOWAIT;
3900 else if (force_nonblock)
3901 flags |= MSG_DONTWAIT;
3902
3903 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3904 kmsg->uaddr, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003905 if (force_nonblock && ret == -EAGAIN)
3906 return io_setup_async_msg(req, kmsg);
Jens Axboe441cdbd2019-12-02 18:49:10 -07003907 if (ret == -ERESTARTSYS)
3908 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003909 }
3910
Pavel Begunkov1e950812020-02-06 19:51:16 +03003911 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003912 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003913 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe52de1fe2020-02-27 10:15:42 -07003914 __io_cqring_add_event(req, ret, cflags);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003915 if (ret < 0)
3916 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003917 io_put_req(req);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003918 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003919}
3920
Pavel Begunkov014db002020-03-03 21:33:12 +03003921static int io_recv(struct io_kiocb *req, bool force_nonblock)
Jens Axboefddafac2020-01-04 20:19:44 -07003922{
Jens Axboebcda7ba2020-02-23 16:42:51 -07003923 struct io_buffer *kbuf = NULL;
Jens Axboefddafac2020-01-04 20:19:44 -07003924 struct socket *sock;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003925 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003926
3927 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3928 return -EINVAL;
3929
3930 sock = sock_from_file(req->file, &ret);
3931 if (sock) {
3932 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003933 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07003934 struct msghdr msg;
3935 struct iovec iov;
3936 unsigned flags;
3937
Jens Axboebcda7ba2020-02-23 16:42:51 -07003938 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
3939 if (IS_ERR(kbuf))
3940 return PTR_ERR(kbuf);
3941 else if (kbuf)
3942 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07003943
Jens Axboebcda7ba2020-02-23 16:42:51 -07003944 ret = import_single_range(READ, buf, sr->len, &iov,
3945 &msg.msg_iter);
3946 if (ret) {
3947 kfree(kbuf);
3948 return ret;
3949 }
3950
3951 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07003952 msg.msg_name = NULL;
3953 msg.msg_control = NULL;
3954 msg.msg_controllen = 0;
3955 msg.msg_namelen = 0;
3956 msg.msg_iocb = NULL;
3957 msg.msg_flags = 0;
3958
3959 flags = req->sr_msg.msg_flags;
3960 if (flags & MSG_DONTWAIT)
3961 req->flags |= REQ_F_NOWAIT;
3962 else if (force_nonblock)
3963 flags |= MSG_DONTWAIT;
3964
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003965 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07003966 if (force_nonblock && ret == -EAGAIN)
3967 return -EAGAIN;
3968 if (ret == -ERESTARTSYS)
3969 ret = -EINTR;
3970 }
3971
Jens Axboebcda7ba2020-02-23 16:42:51 -07003972 kfree(kbuf);
3973 req->flags &= ~REQ_F_NEED_CLEANUP;
3974 __io_cqring_add_event(req, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07003975 if (ret < 0)
3976 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003977 io_put_req(req);
Jens Axboefddafac2020-01-04 20:19:44 -07003978 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003979}
3980
Jens Axboe3529d8c2019-12-19 18:24:38 -07003981static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003982{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003983 struct io_accept *accept = &req->accept;
3984
Jens Axboe17f2fe32019-10-17 14:42:58 -06003985 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3986 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003987 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003988 return -EINVAL;
3989
Jens Axboed55e5f52019-12-11 16:12:15 -07003990 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3991 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003992 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06003993 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003994 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003995}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003996
Pavel Begunkov014db002020-03-03 21:33:12 +03003997static int __io_accept(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003998{
3999 struct io_accept *accept = &req->accept;
4000 unsigned file_flags;
4001 int ret;
4002
4003 file_flags = force_nonblock ? O_NONBLOCK : 0;
4004 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004005 accept->addr_len, accept->flags,
4006 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004007 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004008 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07004009 if (ret == -ERESTARTSYS)
4010 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004011 if (ret < 0)
4012 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004013 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03004014 io_put_req(req);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004015 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004016}
4017
4018static void io_accept_finish(struct io_wq_work **workptr)
4019{
4020 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004021
4022 if (io_req_cancelled(req))
4023 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03004024 __io_accept(req, false);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03004025 io_steal_work(req, workptr);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004026}
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004027
Pavel Begunkov014db002020-03-03 21:33:12 +03004028static int io_accept(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004029{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004030 int ret;
4031
Pavel Begunkov014db002020-03-03 21:33:12 +03004032 ret = __io_accept(req, force_nonblock);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004033 if (ret == -EAGAIN && force_nonblock) {
4034 req->work.func = io_accept_finish;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004035 return -EAGAIN;
4036 }
4037 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06004038}
4039
Jens Axboe3529d8c2019-12-19 18:24:38 -07004040static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004041{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004042 struct io_connect *conn = &req->connect;
4043 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07004044
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004045 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4046 return -EINVAL;
4047 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4048 return -EINVAL;
4049
Jens Axboe3529d8c2019-12-19 18:24:38 -07004050 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4051 conn->addr_len = READ_ONCE(sqe->addr2);
4052
4053 if (!io)
4054 return 0;
4055
4056 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004057 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004058}
4059
Pavel Begunkov014db002020-03-03 21:33:12 +03004060static int io_connect(struct io_kiocb *req, bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004061{
Jens Axboef499a022019-12-02 16:28:46 -07004062 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004063 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004064 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004065
Jens Axboef499a022019-12-02 16:28:46 -07004066 if (req->io) {
4067 io = req->io;
4068 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004069 ret = move_addr_to_kernel(req->connect.addr,
4070 req->connect.addr_len,
4071 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004072 if (ret)
4073 goto out;
4074 io = &__io;
4075 }
4076
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004077 file_flags = force_nonblock ? O_NONBLOCK : 0;
4078
4079 ret = __sys_connect_file(req->file, &io->connect.address,
4080 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004081 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004082 if (req->io)
4083 return -EAGAIN;
4084 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004085 ret = -ENOMEM;
4086 goto out;
4087 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004088 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004089 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004090 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004091 if (ret == -ERESTARTSYS)
4092 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004093out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004094 if (ret < 0)
4095 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004096 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03004097 io_put_req(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004098 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004099}
YueHaibing469956e2020-03-04 15:53:52 +08004100#else /* !CONFIG_NET */
4101static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4102{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004103 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004104}
4105
YueHaibing469956e2020-03-04 15:53:52 +08004106static int io_sendmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004107{
YueHaibing469956e2020-03-04 15:53:52 +08004108 return -EOPNOTSUPP;
4109}
4110
4111static int io_send(struct io_kiocb *req, bool force_nonblock)
4112{
4113 return -EOPNOTSUPP;
4114}
4115
4116static int io_recvmsg_prep(struct io_kiocb *req,
4117 const struct io_uring_sqe *sqe)
4118{
4119 return -EOPNOTSUPP;
4120}
4121
4122static int io_recvmsg(struct io_kiocb *req, bool force_nonblock)
4123{
4124 return -EOPNOTSUPP;
4125}
4126
4127static int io_recv(struct io_kiocb *req, bool force_nonblock)
4128{
4129 return -EOPNOTSUPP;
4130}
4131
4132static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4133{
4134 return -EOPNOTSUPP;
4135}
4136
4137static int io_accept(struct io_kiocb *req, bool force_nonblock)
4138{
4139 return -EOPNOTSUPP;
4140}
4141
4142static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4143{
4144 return -EOPNOTSUPP;
4145}
4146
4147static int io_connect(struct io_kiocb *req, bool force_nonblock)
4148{
4149 return -EOPNOTSUPP;
4150}
4151#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004152
Jens Axboed7718a92020-02-14 22:23:12 -07004153struct io_poll_table {
4154 struct poll_table_struct pt;
4155 struct io_kiocb *req;
4156 int error;
4157};
4158
Jens Axboed7718a92020-02-14 22:23:12 -07004159static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4160 __poll_t mask, task_work_func_t func)
4161{
4162 struct task_struct *tsk;
Jens Axboeaa96bf82020-04-03 11:26:26 -06004163 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004164
4165 /* for instances that support it check for an event match first: */
4166 if (mask && !(mask & poll->events))
4167 return 0;
4168
4169 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4170
4171 list_del_init(&poll->wait.entry);
4172
4173 tsk = req->task;
4174 req->result = mask;
4175 init_task_work(&req->task_work, func);
4176 /*
Jens Axboeaa96bf82020-04-03 11:26:26 -06004177 * If this fails, then the task is exiting. Punt to one of the io-wq
4178 * threads to ensure the work gets run, we can't always rely on exit
4179 * cancelation taking care of this.
Jens Axboed7718a92020-02-14 22:23:12 -07004180 */
Jens Axboeaa96bf82020-04-03 11:26:26 -06004181 ret = task_work_add(tsk, &req->task_work, true);
4182 if (unlikely(ret)) {
4183 tsk = io_wq_get_task(req->ctx->io_wq);
4184 task_work_add(tsk, &req->task_work, true);
4185 }
Jens Axboed7718a92020-02-14 22:23:12 -07004186 wake_up_process(tsk);
4187 return 1;
4188}
4189
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004190static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4191 __acquires(&req->ctx->completion_lock)
4192{
4193 struct io_ring_ctx *ctx = req->ctx;
4194
4195 if (!req->result && !READ_ONCE(poll->canceled)) {
4196 struct poll_table_struct pt = { ._key = poll->events };
4197
4198 req->result = vfs_poll(req->file, &pt) & poll->events;
4199 }
4200
4201 spin_lock_irq(&ctx->completion_lock);
4202 if (!req->result && !READ_ONCE(poll->canceled)) {
4203 add_wait_queue(poll->head, &poll->wait);
4204 return true;
4205 }
4206
4207 return false;
4208}
4209
Jens Axboe18bceab2020-05-15 11:56:54 -06004210static void io_poll_remove_double(struct io_kiocb *req)
4211{
4212 struct io_poll_iocb *poll = (struct io_poll_iocb *) req->io;
4213
4214 lockdep_assert_held(&req->ctx->completion_lock);
4215
4216 if (poll && poll->head) {
4217 struct wait_queue_head *head = poll->head;
4218
4219 spin_lock(&head->lock);
4220 list_del_init(&poll->wait.entry);
4221 if (poll->wait.private)
4222 refcount_dec(&req->refs);
4223 poll->head = NULL;
4224 spin_unlock(&head->lock);
4225 }
4226}
4227
4228static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4229{
4230 struct io_ring_ctx *ctx = req->ctx;
4231
4232 io_poll_remove_double(req);
4233 req->poll.done = true;
4234 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4235 io_commit_cqring(ctx);
4236}
4237
4238static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
4239{
4240 struct io_ring_ctx *ctx = req->ctx;
4241
4242 if (io_poll_rewait(req, &req->poll)) {
4243 spin_unlock_irq(&ctx->completion_lock);
4244 return;
4245 }
4246
4247 hash_del(&req->hash_node);
4248 io_poll_complete(req, req->result, 0);
4249 req->flags |= REQ_F_COMP_LOCKED;
4250 io_put_req_find_next(req, nxt);
4251 spin_unlock_irq(&ctx->completion_lock);
4252
4253 io_cqring_ev_posted(ctx);
4254}
4255
4256static void io_poll_task_func(struct callback_head *cb)
4257{
4258 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4259 struct io_kiocb *nxt = NULL;
4260
4261 io_poll_task_handler(req, &nxt);
4262 if (nxt) {
4263 struct io_ring_ctx *ctx = nxt->ctx;
4264
4265 mutex_lock(&ctx->uring_lock);
4266 __io_queue_sqe(nxt, NULL);
4267 mutex_unlock(&ctx->uring_lock);
4268 }
4269}
4270
4271static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4272 int sync, void *key)
4273{
4274 struct io_kiocb *req = wait->private;
4275 struct io_poll_iocb *poll = (struct io_poll_iocb *) req->io;
4276 __poll_t mask = key_to_poll(key);
4277
4278 /* for instances that support it check for an event match first: */
4279 if (mask && !(mask & poll->events))
4280 return 0;
4281
4282 if (req->poll.head) {
4283 bool done;
4284
4285 spin_lock(&req->poll.head->lock);
4286 done = list_empty(&req->poll.wait.entry);
4287 if (!done)
4288 list_del_init(&req->poll.wait.entry);
4289 spin_unlock(&req->poll.head->lock);
4290 if (!done)
4291 __io_async_wake(req, poll, mask, io_poll_task_func);
4292 }
4293 refcount_dec(&req->refs);
4294 return 1;
4295}
4296
4297static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4298 wait_queue_func_t wake_func)
4299{
4300 poll->head = NULL;
4301 poll->done = false;
4302 poll->canceled = false;
4303 poll->events = events;
4304 INIT_LIST_HEAD(&poll->wait.entry);
4305 init_waitqueue_func_entry(&poll->wait, wake_func);
4306}
4307
4308static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
4309 struct wait_queue_head *head)
4310{
4311 struct io_kiocb *req = pt->req;
4312
4313 /*
4314 * If poll->head is already set, it's because the file being polled
4315 * uses multiple waitqueues for poll handling (eg one for read, one
4316 * for write). Setup a separate io_poll_iocb if this happens.
4317 */
4318 if (unlikely(poll->head)) {
4319 /* already have a 2nd entry, fail a third attempt */
4320 if (req->io) {
4321 pt->error = -EINVAL;
4322 return;
4323 }
4324 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
4325 if (!poll) {
4326 pt->error = -ENOMEM;
4327 return;
4328 }
4329 io_init_poll_iocb(poll, req->poll.events, io_poll_double_wake);
4330 refcount_inc(&req->refs);
4331 poll->wait.private = req;
4332 req->io = (void *) poll;
4333 }
4334
4335 pt->error = 0;
4336 poll->head = head;
4337 add_wait_queue(head, &poll->wait);
4338}
4339
4340static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4341 struct poll_table_struct *p)
4342{
4343 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4344
4345 __io_queue_proc(&pt->req->apoll->poll, pt, head);
4346}
4347
Jens Axboed7718a92020-02-14 22:23:12 -07004348static void io_async_task_func(struct callback_head *cb)
4349{
4350 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4351 struct async_poll *apoll = req->apoll;
4352 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe31067252020-05-17 17:43:31 -06004353 bool canceled = false;
Jens Axboed7718a92020-02-14 22:23:12 -07004354
4355 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4356
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004357 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07004358 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004359 return;
Jens Axboed7718a92020-02-14 22:23:12 -07004360 }
4361
Jens Axboe31067252020-05-17 17:43:31 -06004362 /* If req is still hashed, it cannot have been canceled. Don't check. */
4363 if (hash_hashed(&req->hash_node)) {
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004364 hash_del(&req->hash_node);
Jens Axboe31067252020-05-17 17:43:31 -06004365 } else {
4366 canceled = READ_ONCE(apoll->poll.canceled);
4367 if (canceled) {
4368 io_cqring_fill_event(req, -ECANCELED);
4369 io_commit_cqring(ctx);
4370 }
Jens Axboe2bae0472020-04-13 11:16:34 -06004371 }
4372
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004373 spin_unlock_irq(&ctx->completion_lock);
4374
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004375 /* restore ->work in case we need to retry again */
4376 memcpy(&req->work, &apoll->work, sizeof(req->work));
Jens Axboe31067252020-05-17 17:43:31 -06004377 kfree(apoll);
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004378
Jens Axboe31067252020-05-17 17:43:31 -06004379 if (!canceled) {
4380 __set_current_state(TASK_RUNNING);
4381 mutex_lock(&ctx->uring_lock);
4382 __io_queue_sqe(req, NULL);
4383 mutex_unlock(&ctx->uring_lock);
4384 } else {
Jens Axboe2bae0472020-04-13 11:16:34 -06004385 io_cqring_ev_posted(ctx);
4386 req_set_fail_links(req);
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004387 io_double_put_req(req);
Jens Axboe2bae0472020-04-13 11:16:34 -06004388 }
Jens Axboed7718a92020-02-14 22:23:12 -07004389}
4390
4391static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4392 void *key)
4393{
4394 struct io_kiocb *req = wait->private;
4395 struct io_poll_iocb *poll = &req->apoll->poll;
4396
4397 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4398 key_to_poll(key));
4399
4400 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4401}
4402
4403static void io_poll_req_insert(struct io_kiocb *req)
4404{
4405 struct io_ring_ctx *ctx = req->ctx;
4406 struct hlist_head *list;
4407
4408 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
4409 hlist_add_head(&req->hash_node, list);
4410}
4411
4412static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
4413 struct io_poll_iocb *poll,
4414 struct io_poll_table *ipt, __poll_t mask,
4415 wait_queue_func_t wake_func)
4416 __acquires(&ctx->completion_lock)
4417{
4418 struct io_ring_ctx *ctx = req->ctx;
4419 bool cancel = false;
4420
4421 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06004422 io_init_poll_iocb(poll, mask, wake_func);
4423 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07004424
4425 ipt->pt._key = mask;
4426 ipt->req = req;
4427 ipt->error = -EINVAL;
4428
Jens Axboed7718a92020-02-14 22:23:12 -07004429 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
4430
4431 spin_lock_irq(&ctx->completion_lock);
4432 if (likely(poll->head)) {
4433 spin_lock(&poll->head->lock);
4434 if (unlikely(list_empty(&poll->wait.entry))) {
4435 if (ipt->error)
4436 cancel = true;
4437 ipt->error = 0;
4438 mask = 0;
4439 }
4440 if (mask || ipt->error)
4441 list_del_init(&poll->wait.entry);
4442 else if (cancel)
4443 WRITE_ONCE(poll->canceled, true);
4444 else if (!poll->done) /* actually waiting for an event */
4445 io_poll_req_insert(req);
4446 spin_unlock(&poll->head->lock);
4447 }
4448
4449 return mask;
4450}
4451
4452static bool io_arm_poll_handler(struct io_kiocb *req)
4453{
4454 const struct io_op_def *def = &io_op_defs[req->opcode];
4455 struct io_ring_ctx *ctx = req->ctx;
4456 struct async_poll *apoll;
4457 struct io_poll_table ipt;
4458 __poll_t mask, ret;
Jens Axboe18bceab2020-05-15 11:56:54 -06004459 bool had_io;
Jens Axboed7718a92020-02-14 22:23:12 -07004460
4461 if (!req->file || !file_can_poll(req->file))
4462 return false;
4463 if (req->flags & (REQ_F_MUST_PUNT | REQ_F_POLLED))
4464 return false;
4465 if (!def->pollin && !def->pollout)
4466 return false;
4467
4468 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
4469 if (unlikely(!apoll))
4470 return false;
4471
4472 req->flags |= REQ_F_POLLED;
4473 memcpy(&apoll->work, &req->work, sizeof(req->work));
Jens Axboe18bceab2020-05-15 11:56:54 -06004474 had_io = req->io != NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07004475
Jens Axboe3537b6a2020-04-03 11:19:06 -06004476 get_task_struct(current);
Jens Axboed7718a92020-02-14 22:23:12 -07004477 req->task = current;
4478 req->apoll = apoll;
4479 INIT_HLIST_NODE(&req->hash_node);
4480
Nathan Chancellor8755d972020-03-02 16:01:19 -07004481 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07004482 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07004483 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07004484 if (def->pollout)
4485 mask |= POLLOUT | POLLWRNORM;
4486 mask |= POLLERR | POLLPRI;
4487
4488 ipt.pt._qproc = io_async_queue_proc;
4489
4490 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
4491 io_async_wake);
4492 if (ret) {
4493 ipt.error = 0;
Jens Axboe18bceab2020-05-15 11:56:54 -06004494 /* only remove double add if we did it here */
4495 if (!had_io)
4496 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004497 spin_unlock_irq(&ctx->completion_lock);
4498 memcpy(&req->work, &apoll->work, sizeof(req->work));
4499 kfree(apoll);
4500 return false;
4501 }
4502 spin_unlock_irq(&ctx->completion_lock);
4503 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
4504 apoll->poll.events);
4505 return true;
4506}
4507
4508static bool __io_poll_remove_one(struct io_kiocb *req,
4509 struct io_poll_iocb *poll)
4510{
Jens Axboeb41e9852020-02-17 09:52:41 -07004511 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004512
4513 spin_lock(&poll->head->lock);
4514 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07004515 if (!list_empty(&poll->wait.entry)) {
4516 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07004517 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004518 }
4519 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004520 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07004521 return do_complete;
4522}
4523
4524static bool io_poll_remove_one(struct io_kiocb *req)
4525{
4526 bool do_complete;
4527
4528 if (req->opcode == IORING_OP_POLL_ADD) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004529 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004530 do_complete = __io_poll_remove_one(req, &req->poll);
4531 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004532 struct async_poll *apoll = req->apoll;
4533
Jens Axboed7718a92020-02-14 22:23:12 -07004534 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004535 do_complete = __io_poll_remove_one(req, &apoll->poll);
4536 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07004537 io_put_req(req);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004538 /*
4539 * restore ->work because we will call
4540 * io_req_work_drop_env below when dropping the
4541 * final reference.
4542 */
4543 memcpy(&req->work, &apoll->work, sizeof(req->work));
4544 kfree(apoll);
4545 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08004546 }
4547
Jens Axboeb41e9852020-02-17 09:52:41 -07004548 if (do_complete) {
4549 io_cqring_fill_event(req, -ECANCELED);
4550 io_commit_cqring(req->ctx);
4551 req->flags |= REQ_F_COMP_LOCKED;
4552 io_put_req(req);
4553 }
4554
4555 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004556}
4557
4558static void io_poll_remove_all(struct io_ring_ctx *ctx)
4559{
Jens Axboe78076bb2019-12-04 19:56:40 -07004560 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004561 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004562 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004563
4564 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07004565 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
4566 struct hlist_head *list;
4567
4568 list = &ctx->cancel_hash[i];
4569 hlist_for_each_entry_safe(req, tmp, list, hash_node)
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004570 posted += io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004571 }
4572 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07004573
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004574 if (posted)
4575 io_cqring_ev_posted(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004576}
4577
Jens Axboe47f46762019-11-09 17:43:02 -07004578static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
4579{
Jens Axboe78076bb2019-12-04 19:56:40 -07004580 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07004581 struct io_kiocb *req;
4582
Jens Axboe78076bb2019-12-04 19:56:40 -07004583 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
4584 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07004585 if (sqe_addr != req->user_data)
4586 continue;
4587 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07004588 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07004589 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07004590 }
4591
4592 return -ENOENT;
4593}
4594
Jens Axboe3529d8c2019-12-19 18:24:38 -07004595static int io_poll_remove_prep(struct io_kiocb *req,
4596 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004597{
Jens Axboe221c5eb2019-01-17 09:41:58 -07004598 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4599 return -EINVAL;
4600 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
4601 sqe->poll_events)
4602 return -EINVAL;
4603
Jens Axboe0969e782019-12-17 18:40:57 -07004604 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07004605 return 0;
4606}
4607
4608/*
4609 * Find a running poll command that matches one specified in sqe->addr,
4610 * and remove it if found.
4611 */
4612static int io_poll_remove(struct io_kiocb *req)
4613{
4614 struct io_ring_ctx *ctx = req->ctx;
4615 u64 addr;
4616 int ret;
4617
Jens Axboe0969e782019-12-17 18:40:57 -07004618 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004619 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07004620 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004621 spin_unlock_irq(&ctx->completion_lock);
4622
Jens Axboe78e19bb2019-11-06 15:21:34 -07004623 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004624 if (ret < 0)
4625 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004626 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004627 return 0;
4628}
4629
Jens Axboe221c5eb2019-01-17 09:41:58 -07004630static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4631 void *key)
4632{
Jens Axboec2f2eb72020-02-10 09:07:05 -07004633 struct io_kiocb *req = wait->private;
4634 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004635
Jens Axboed7718a92020-02-14 22:23:12 -07004636 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004637}
4638
Jens Axboe221c5eb2019-01-17 09:41:58 -07004639static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
4640 struct poll_table_struct *p)
4641{
4642 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4643
Jens Axboed7718a92020-02-14 22:23:12 -07004644 __io_queue_proc(&pt->req->poll, pt, head);
Jens Axboeeac406c2019-11-14 12:09:58 -07004645}
4646
Jens Axboe3529d8c2019-12-19 18:24:38 -07004647static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004648{
4649 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004650 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004651
4652 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4653 return -EINVAL;
4654 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
4655 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06004656 if (!poll->file)
4657 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004658
Jens Axboe221c5eb2019-01-17 09:41:58 -07004659 events = READ_ONCE(sqe->poll_events);
4660 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboeb41e9852020-02-17 09:52:41 -07004661
Jens Axboe3537b6a2020-04-03 11:19:06 -06004662 get_task_struct(current);
Jens Axboeb41e9852020-02-17 09:52:41 -07004663 req->task = current;
Jens Axboe0969e782019-12-17 18:40:57 -07004664 return 0;
4665}
4666
Pavel Begunkov014db002020-03-03 21:33:12 +03004667static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07004668{
4669 struct io_poll_iocb *poll = &req->poll;
4670 struct io_ring_ctx *ctx = req->ctx;
4671 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07004672 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07004673
Jens Axboe78076bb2019-12-04 19:56:40 -07004674 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe36703242019-07-25 10:20:18 -06004675 INIT_LIST_HEAD(&req->list);
Jens Axboed7718a92020-02-14 22:23:12 -07004676 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06004677
Jens Axboed7718a92020-02-14 22:23:12 -07004678 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
4679 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004680
Jens Axboe8c838782019-03-12 15:48:16 -06004681 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06004682 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004683 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06004684 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07004685 spin_unlock_irq(&ctx->completion_lock);
4686
Jens Axboe8c838782019-03-12 15:48:16 -06004687 if (mask) {
4688 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03004689 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004690 }
Jens Axboe8c838782019-03-12 15:48:16 -06004691 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004692}
4693
Jens Axboe5262f562019-09-17 12:26:57 -06004694static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
4695{
Jens Axboead8a48a2019-11-15 08:49:11 -07004696 struct io_timeout_data *data = container_of(timer,
4697 struct io_timeout_data, timer);
4698 struct io_kiocb *req = data->req;
4699 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06004700 unsigned long flags;
4701
Jens Axboe5262f562019-09-17 12:26:57 -06004702 atomic_inc(&ctx->cq_timeouts);
4703
4704 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08004705 /*
Jens Axboe11365042019-10-16 09:08:32 -06004706 * We could be racing with timeout deletion. If the list is empty,
4707 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08004708 */
Jens Axboe842f9612019-10-29 12:34:10 -06004709 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06004710 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06004711
Jens Axboe11365042019-10-16 09:08:32 -06004712 /*
4713 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08004714 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06004715 * pointer will be increased, otherwise other timeout reqs may
4716 * return in advance without waiting for enough wait_nr.
4717 */
4718 prev = req;
4719 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
4720 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06004721 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06004722 }
Jens Axboe842f9612019-10-29 12:34:10 -06004723
Jens Axboe78e19bb2019-11-06 15:21:34 -07004724 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06004725 io_commit_cqring(ctx);
4726 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4727
4728 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004729 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004730 io_put_req(req);
4731 return HRTIMER_NORESTART;
4732}
4733
Jens Axboe47f46762019-11-09 17:43:02 -07004734static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
4735{
4736 struct io_kiocb *req;
4737 int ret = -ENOENT;
4738
4739 list_for_each_entry(req, &ctx->timeout_list, list) {
4740 if (user_data == req->user_data) {
4741 list_del_init(&req->list);
4742 ret = 0;
4743 break;
4744 }
4745 }
4746
4747 if (ret == -ENOENT)
4748 return ret;
4749
Jens Axboe2d283902019-12-04 11:08:05 -07004750 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07004751 if (ret == -1)
4752 return -EALREADY;
4753
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004754 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004755 io_cqring_fill_event(req, -ECANCELED);
4756 io_put_req(req);
4757 return 0;
4758}
4759
Jens Axboe3529d8c2019-12-19 18:24:38 -07004760static int io_timeout_remove_prep(struct io_kiocb *req,
4761 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07004762{
Jens Axboeb29472e2019-12-17 18:50:29 -07004763 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4764 return -EINVAL;
4765 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
4766 return -EINVAL;
4767
4768 req->timeout.addr = READ_ONCE(sqe->addr);
4769 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
4770 if (req->timeout.flags)
4771 return -EINVAL;
4772
Jens Axboeb29472e2019-12-17 18:50:29 -07004773 return 0;
4774}
4775
Jens Axboe11365042019-10-16 09:08:32 -06004776/*
4777 * Remove or update an existing timeout command
4778 */
Jens Axboefc4df992019-12-10 14:38:45 -07004779static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06004780{
4781 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07004782 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06004783
Jens Axboe11365042019-10-16 09:08:32 -06004784 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07004785 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06004786
Jens Axboe47f46762019-11-09 17:43:02 -07004787 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06004788 io_commit_cqring(ctx);
4789 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004790 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004791 if (ret < 0)
4792 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08004793 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06004794 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06004795}
4796
Jens Axboe3529d8c2019-12-19 18:24:38 -07004797static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07004798 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06004799{
Jens Axboead8a48a2019-11-15 08:49:11 -07004800 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06004801 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03004802 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06004803
Jens Axboead8a48a2019-11-15 08:49:11 -07004804 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06004805 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07004806 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06004807 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03004808 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07004809 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06004810 flags = READ_ONCE(sqe->timeout_flags);
4811 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06004812 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06004813
Pavel Begunkov56080b02020-05-26 20:34:04 +03004814 req->timeout.count = off;
Jens Axboe26a61672019-12-20 09:02:01 -07004815
Jens Axboe3529d8c2019-12-19 18:24:38 -07004816 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07004817 return -ENOMEM;
4818
4819 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07004820 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07004821 req->flags |= REQ_F_TIMEOUT;
4822
4823 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06004824 return -EFAULT;
4825
Jens Axboe11365042019-10-16 09:08:32 -06004826 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07004827 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06004828 else
Jens Axboead8a48a2019-11-15 08:49:11 -07004829 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06004830
Jens Axboead8a48a2019-11-15 08:49:11 -07004831 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
4832 return 0;
4833}
4834
Jens Axboefc4df992019-12-10 14:38:45 -07004835static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07004836{
Jens Axboead8a48a2019-11-15 08:49:11 -07004837 struct io_ring_ctx *ctx = req->ctx;
4838 struct io_timeout_data *data;
4839 struct list_head *entry;
4840 unsigned span = 0;
Pavel Begunkovb55ce732020-04-15 00:39:49 +03004841 u32 count = req->timeout.count;
Pavel Begunkov22cad152020-04-15 00:39:48 +03004842 u32 seq = req->sequence;
Jens Axboead8a48a2019-11-15 08:49:11 -07004843
Jens Axboe2d283902019-12-04 11:08:05 -07004844 data = &req->io->timeout;
Pavel Begunkov733f5c92020-05-26 20:34:03 +03004845 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07004846
Jens Axboe5262f562019-09-17 12:26:57 -06004847 /*
4848 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07004849 * timeout event to be satisfied. If it isn't set, then this is
4850 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06004851 */
Jens Axboe93bd25b2019-11-11 23:34:31 -07004852 if (!count) {
4853 req->flags |= REQ_F_TIMEOUT_NOSEQ;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004854 entry = ctx->timeout_list.prev;
4855 goto add;
4856 }
Jens Axboe5262f562019-09-17 12:26:57 -06004857
Pavel Begunkov22cad152020-04-15 00:39:48 +03004858 req->sequence = seq + count;
Jens Axboe5262f562019-09-17 12:26:57 -06004859
4860 /*
4861 * Insertion sort, ensuring the first entry in the list is always
4862 * the one we need first.
4863 */
Jens Axboe5262f562019-09-17 12:26:57 -06004864 list_for_each_prev(entry, &ctx->timeout_list) {
4865 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
Pavel Begunkov22cad152020-04-15 00:39:48 +03004866 unsigned nxt_seq;
yangerkun5da0fb12019-10-15 21:59:29 +08004867 long long tmp, tmp_nxt;
Pavel Begunkovb55ce732020-04-15 00:39:49 +03004868 u32 nxt_offset = nxt->timeout.count;
Jens Axboe5262f562019-09-17 12:26:57 -06004869
Jens Axboe93bd25b2019-11-11 23:34:31 -07004870 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
4871 continue;
4872
yangerkun5da0fb12019-10-15 21:59:29 +08004873 /*
Pavel Begunkov22cad152020-04-15 00:39:48 +03004874 * Since seq + count can overflow, use type long
yangerkun5da0fb12019-10-15 21:59:29 +08004875 * long to store it.
4876 */
Pavel Begunkov22cad152020-04-15 00:39:48 +03004877 tmp = (long long)seq + count;
4878 nxt_seq = nxt->sequence - nxt_offset;
4879 tmp_nxt = (long long)nxt_seq + nxt_offset;
yangerkun5da0fb12019-10-15 21:59:29 +08004880
4881 /*
4882 * cached_sq_head may overflow, and it will never overflow twice
4883 * once there is some timeout req still be valid.
4884 */
Pavel Begunkov22cad152020-04-15 00:39:48 +03004885 if (seq < nxt_seq)
yangerkun8b07a652019-10-17 12:12:35 +08004886 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08004887
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004888 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06004889 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004890
4891 /*
4892 * Sequence of reqs after the insert one and itself should
4893 * be adjusted because each timeout req consumes a slot.
4894 */
4895 span++;
4896 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06004897 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004898 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004899add:
Jens Axboe5262f562019-09-17 12:26:57 -06004900 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07004901 data->timer.function = io_timeout_fn;
4902 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06004903 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004904 return 0;
4905}
4906
Jens Axboe62755e32019-10-28 21:49:21 -06004907static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06004908{
Jens Axboe62755e32019-10-28 21:49:21 -06004909 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06004910
Jens Axboe62755e32019-10-28 21:49:21 -06004911 return req->user_data == (unsigned long) data;
4912}
4913
Jens Axboee977d6d2019-11-05 12:39:45 -07004914static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06004915{
Jens Axboe62755e32019-10-28 21:49:21 -06004916 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06004917 int ret = 0;
4918
Jens Axboe62755e32019-10-28 21:49:21 -06004919 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
4920 switch (cancel_ret) {
4921 case IO_WQ_CANCEL_OK:
4922 ret = 0;
4923 break;
4924 case IO_WQ_CANCEL_RUNNING:
4925 ret = -EALREADY;
4926 break;
4927 case IO_WQ_CANCEL_NOTFOUND:
4928 ret = -ENOENT;
4929 break;
4930 }
4931
Jens Axboee977d6d2019-11-05 12:39:45 -07004932 return ret;
4933}
4934
Jens Axboe47f46762019-11-09 17:43:02 -07004935static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
4936 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03004937 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07004938{
4939 unsigned long flags;
4940 int ret;
4941
4942 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
4943 if (ret != -ENOENT) {
4944 spin_lock_irqsave(&ctx->completion_lock, flags);
4945 goto done;
4946 }
4947
4948 spin_lock_irqsave(&ctx->completion_lock, flags);
4949 ret = io_timeout_cancel(ctx, sqe_addr);
4950 if (ret != -ENOENT)
4951 goto done;
4952 ret = io_poll_cancel(ctx, sqe_addr);
4953done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004954 if (!ret)
4955 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07004956 io_cqring_fill_event(req, ret);
4957 io_commit_cqring(ctx);
4958 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4959 io_cqring_ev_posted(ctx);
4960
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004961 if (ret < 0)
4962 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03004963 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004964}
4965
Jens Axboe3529d8c2019-12-19 18:24:38 -07004966static int io_async_cancel_prep(struct io_kiocb *req,
4967 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07004968{
Jens Axboefbf23842019-12-17 18:45:56 -07004969 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07004970 return -EINVAL;
4971 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
4972 sqe->cancel_flags)
4973 return -EINVAL;
4974
Jens Axboefbf23842019-12-17 18:45:56 -07004975 req->cancel.addr = READ_ONCE(sqe->addr);
4976 return 0;
4977}
4978
Pavel Begunkov014db002020-03-03 21:33:12 +03004979static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07004980{
4981 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07004982
Pavel Begunkov014db002020-03-03 21:33:12 +03004983 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06004984 return 0;
4985}
4986
Jens Axboe05f3fb32019-12-09 11:22:50 -07004987static int io_files_update_prep(struct io_kiocb *req,
4988 const struct io_uring_sqe *sqe)
4989{
4990 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
4991 return -EINVAL;
4992
4993 req->files_update.offset = READ_ONCE(sqe->off);
4994 req->files_update.nr_args = READ_ONCE(sqe->len);
4995 if (!req->files_update.nr_args)
4996 return -EINVAL;
4997 req->files_update.arg = READ_ONCE(sqe->addr);
4998 return 0;
4999}
5000
5001static int io_files_update(struct io_kiocb *req, bool force_nonblock)
5002{
5003 struct io_ring_ctx *ctx = req->ctx;
5004 struct io_uring_files_update up;
5005 int ret;
5006
Jens Axboef86cd202020-01-29 13:46:44 -07005007 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005008 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005009
5010 up.offset = req->files_update.offset;
5011 up.fds = req->files_update.arg;
5012
5013 mutex_lock(&ctx->uring_lock);
5014 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
5015 mutex_unlock(&ctx->uring_lock);
5016
5017 if (ret < 0)
5018 req_set_fail_links(req);
5019 io_cqring_add_event(req, ret);
5020 io_put_req(req);
5021 return 0;
5022}
5023
Jens Axboe3529d8c2019-12-19 18:24:38 -07005024static int io_req_defer_prep(struct io_kiocb *req,
5025 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005026{
Jens Axboee7815732019-12-17 19:45:06 -07005027 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005028
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03005029 if (!sqe)
5030 return 0;
5031
Jens Axboef86cd202020-01-29 13:46:44 -07005032 if (io_op_defs[req->opcode].file_table) {
5033 ret = io_grab_files(req);
5034 if (unlikely(ret))
5035 return ret;
5036 }
5037
Jens Axboecccf0ee2020-01-27 16:34:48 -07005038 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
5039
Jens Axboed625c6e2019-12-17 19:53:05 -07005040 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005041 case IORING_OP_NOP:
5042 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005043 case IORING_OP_READV:
5044 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005045 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005046 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005047 break;
5048 case IORING_OP_WRITEV:
5049 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005050 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005051 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005052 break;
Jens Axboe0969e782019-12-17 18:40:57 -07005053 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005054 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005055 break;
5056 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005057 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005058 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005059 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005060 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005061 break;
5062 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005063 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005064 break;
Jens Axboe03b12302019-12-02 18:50:25 -07005065 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005066 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005067 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005068 break;
5069 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005070 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005071 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005072 break;
Jens Axboef499a022019-12-02 16:28:46 -07005073 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005074 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005075 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005076 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005077 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005078 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07005079 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005080 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07005081 break;
Jens Axboefbf23842019-12-17 18:45:56 -07005082 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005083 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005084 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005085 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005086 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005087 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005088 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005089 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005090 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005091 case IORING_OP_FALLOCATE:
5092 ret = io_fallocate_prep(req, sqe);
5093 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005094 case IORING_OP_OPENAT:
5095 ret = io_openat_prep(req, sqe);
5096 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005097 case IORING_OP_CLOSE:
5098 ret = io_close_prep(req, sqe);
5099 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005100 case IORING_OP_FILES_UPDATE:
5101 ret = io_files_update_prep(req, sqe);
5102 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005103 case IORING_OP_STATX:
5104 ret = io_statx_prep(req, sqe);
5105 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005106 case IORING_OP_FADVISE:
5107 ret = io_fadvise_prep(req, sqe);
5108 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005109 case IORING_OP_MADVISE:
5110 ret = io_madvise_prep(req, sqe);
5111 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005112 case IORING_OP_OPENAT2:
5113 ret = io_openat2_prep(req, sqe);
5114 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005115 case IORING_OP_EPOLL_CTL:
5116 ret = io_epoll_ctl_prep(req, sqe);
5117 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005118 case IORING_OP_SPLICE:
5119 ret = io_splice_prep(req, sqe);
5120 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005121 case IORING_OP_PROVIDE_BUFFERS:
5122 ret = io_provide_buffers_prep(req, sqe);
5123 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005124 case IORING_OP_REMOVE_BUFFERS:
5125 ret = io_remove_buffers_prep(req, sqe);
5126 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005127 case IORING_OP_TEE:
5128 ret = io_tee_prep(req, sqe);
5129 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005130 default:
Jens Axboee7815732019-12-17 19:45:06 -07005131 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5132 req->opcode);
5133 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005134 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005135 }
5136
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005137 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07005138}
5139
Jens Axboe3529d8c2019-12-19 18:24:38 -07005140static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06005141{
Jackie Liua197f662019-11-08 08:09:12 -07005142 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07005143 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06005144
Bob Liu9d858b22019-11-13 18:06:25 +08005145 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov4ee36312020-05-01 17:09:37 +03005146 if (!req_need_defer(req) && list_empty_careful(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06005147 return 0;
5148
Jens Axboe3529d8c2019-12-19 18:24:38 -07005149 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06005150 return -EAGAIN;
5151
Jens Axboe3529d8c2019-12-19 18:24:38 -07005152 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005153 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07005154 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07005155
Jens Axboede0617e2019-04-06 21:51:27 -06005156 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08005157 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06005158 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06005159 return 0;
5160 }
5161
Jens Axboe915967f2019-11-21 09:01:20 -07005162 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06005163 list_add_tail(&req->list, &ctx->defer_list);
5164 spin_unlock_irq(&ctx->completion_lock);
5165 return -EIOCBQUEUED;
5166}
5167
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005168static void io_cleanup_req(struct io_kiocb *req)
5169{
5170 struct io_async_ctx *io = req->io;
5171
5172 switch (req->opcode) {
5173 case IORING_OP_READV:
5174 case IORING_OP_READ_FIXED:
5175 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005176 if (req->flags & REQ_F_BUFFER_SELECTED)
5177 kfree((void *)(unsigned long)req->rw.addr);
5178 /* fallthrough */
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005179 case IORING_OP_WRITEV:
5180 case IORING_OP_WRITE_FIXED:
5181 case IORING_OP_WRITE:
5182 if (io->rw.iov != io->rw.fast_iov)
5183 kfree(io->rw.iov);
5184 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005185 case IORING_OP_RECVMSG:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005186 if (req->flags & REQ_F_BUFFER_SELECTED)
5187 kfree(req->sr_msg.kbuf);
5188 /* fallthrough */
5189 case IORING_OP_SENDMSG:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005190 if (io->msg.iov != io->msg.fast_iov)
5191 kfree(io->msg.iov);
5192 break;
Jens Axboebcda7ba2020-02-23 16:42:51 -07005193 case IORING_OP_RECV:
5194 if (req->flags & REQ_F_BUFFER_SELECTED)
5195 kfree(req->sr_msg.kbuf);
5196 break;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03005197 case IORING_OP_OPENAT:
5198 case IORING_OP_OPENAT2:
5199 case IORING_OP_STATX:
5200 putname(req->open.filename);
5201 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005202 case IORING_OP_SPLICE:
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005203 case IORING_OP_TEE:
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005204 io_put_file(req, req->splice.file_in,
5205 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5206 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005207 }
5208
5209 req->flags &= ~REQ_F_NEED_CLEANUP;
5210}
5211
Jens Axboe3529d8c2019-12-19 18:24:38 -07005212static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Pavel Begunkov014db002020-03-03 21:33:12 +03005213 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005214{
Jackie Liua197f662019-11-08 08:09:12 -07005215 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005216 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005217
Jens Axboed625c6e2019-12-17 19:53:05 -07005218 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005219 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005220 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005221 break;
5222 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005223 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005224 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005225 if (sqe) {
5226 ret = io_read_prep(req, sqe, force_nonblock);
5227 if (ret < 0)
5228 break;
5229 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005230 ret = io_read(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005231 break;
5232 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07005233 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005234 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005235 if (sqe) {
5236 ret = io_write_prep(req, sqe, force_nonblock);
5237 if (ret < 0)
5238 break;
5239 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005240 ret = io_write(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005241 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005242 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005243 if (sqe) {
5244 ret = io_prep_fsync(req, sqe);
5245 if (ret < 0)
5246 break;
5247 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005248 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005249 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005250 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005251 if (sqe) {
5252 ret = io_poll_add_prep(req, sqe);
5253 if (ret)
5254 break;
5255 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005256 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005257 break;
5258 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005259 if (sqe) {
5260 ret = io_poll_remove_prep(req, sqe);
5261 if (ret < 0)
5262 break;
5263 }
Jens Axboefc4df992019-12-10 14:38:45 -07005264 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005265 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005266 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005267 if (sqe) {
5268 ret = io_prep_sfr(req, sqe);
5269 if (ret < 0)
5270 break;
5271 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005272 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005273 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005274 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005275 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005276 if (sqe) {
5277 ret = io_sendmsg_prep(req, sqe);
5278 if (ret < 0)
5279 break;
5280 }
Jens Axboefddafac2020-01-04 20:19:44 -07005281 if (req->opcode == IORING_OP_SENDMSG)
Pavel Begunkov014db002020-03-03 21:33:12 +03005282 ret = io_sendmsg(req, force_nonblock);
Jens Axboefddafac2020-01-04 20:19:44 -07005283 else
Pavel Begunkov014db002020-03-03 21:33:12 +03005284 ret = io_send(req, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005285 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06005286 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005287 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005288 if (sqe) {
5289 ret = io_recvmsg_prep(req, sqe);
5290 if (ret)
5291 break;
5292 }
Jens Axboefddafac2020-01-04 20:19:44 -07005293 if (req->opcode == IORING_OP_RECVMSG)
Pavel Begunkov014db002020-03-03 21:33:12 +03005294 ret = io_recvmsg(req, force_nonblock);
Jens Axboefddafac2020-01-04 20:19:44 -07005295 else
Pavel Begunkov014db002020-03-03 21:33:12 +03005296 ret = io_recv(req, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06005297 break;
Jens Axboe5262f562019-09-17 12:26:57 -06005298 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005299 if (sqe) {
5300 ret = io_timeout_prep(req, sqe, false);
5301 if (ret)
5302 break;
5303 }
Jens Axboefc4df992019-12-10 14:38:45 -07005304 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005305 break;
Jens Axboe11365042019-10-16 09:08:32 -06005306 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005307 if (sqe) {
5308 ret = io_timeout_remove_prep(req, sqe);
5309 if (ret)
5310 break;
5311 }
Jens Axboefc4df992019-12-10 14:38:45 -07005312 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06005313 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06005314 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005315 if (sqe) {
5316 ret = io_accept_prep(req, sqe);
5317 if (ret)
5318 break;
5319 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005320 ret = io_accept(req, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005321 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005322 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005323 if (sqe) {
5324 ret = io_connect_prep(req, sqe);
5325 if (ret)
5326 break;
5327 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005328 ret = io_connect(req, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005329 break;
Jens Axboe62755e32019-10-28 21:49:21 -06005330 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005331 if (sqe) {
5332 ret = io_async_cancel_prep(req, sqe);
5333 if (ret)
5334 break;
5335 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005336 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005337 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005338 case IORING_OP_FALLOCATE:
5339 if (sqe) {
5340 ret = io_fallocate_prep(req, sqe);
5341 if (ret)
5342 break;
5343 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005344 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005345 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005346 case IORING_OP_OPENAT:
5347 if (sqe) {
5348 ret = io_openat_prep(req, sqe);
5349 if (ret)
5350 break;
5351 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005352 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005353 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005354 case IORING_OP_CLOSE:
5355 if (sqe) {
5356 ret = io_close_prep(req, sqe);
5357 if (ret)
5358 break;
5359 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005360 ret = io_close(req, force_nonblock);
Jens Axboeb5dba592019-12-11 14:02:38 -07005361 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005362 case IORING_OP_FILES_UPDATE:
5363 if (sqe) {
5364 ret = io_files_update_prep(req, sqe);
5365 if (ret)
5366 break;
5367 }
5368 ret = io_files_update(req, force_nonblock);
5369 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005370 case IORING_OP_STATX:
5371 if (sqe) {
5372 ret = io_statx_prep(req, sqe);
5373 if (ret)
5374 break;
5375 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005376 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005377 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005378 case IORING_OP_FADVISE:
5379 if (sqe) {
5380 ret = io_fadvise_prep(req, sqe);
5381 if (ret)
5382 break;
5383 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005384 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005385 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005386 case IORING_OP_MADVISE:
5387 if (sqe) {
5388 ret = io_madvise_prep(req, sqe);
5389 if (ret)
5390 break;
5391 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005392 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005393 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005394 case IORING_OP_OPENAT2:
5395 if (sqe) {
5396 ret = io_openat2_prep(req, sqe);
5397 if (ret)
5398 break;
5399 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005400 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005401 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005402 case IORING_OP_EPOLL_CTL:
5403 if (sqe) {
5404 ret = io_epoll_ctl_prep(req, sqe);
5405 if (ret)
5406 break;
5407 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005408 ret = io_epoll_ctl(req, force_nonblock);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005409 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005410 case IORING_OP_SPLICE:
5411 if (sqe) {
5412 ret = io_splice_prep(req, sqe);
5413 if (ret < 0)
5414 break;
5415 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005416 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005417 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005418 case IORING_OP_PROVIDE_BUFFERS:
5419 if (sqe) {
5420 ret = io_provide_buffers_prep(req, sqe);
5421 if (ret)
5422 break;
5423 }
5424 ret = io_provide_buffers(req, force_nonblock);
5425 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005426 case IORING_OP_REMOVE_BUFFERS:
5427 if (sqe) {
5428 ret = io_remove_buffers_prep(req, sqe);
5429 if (ret)
5430 break;
5431 }
5432 ret = io_remove_buffers(req, force_nonblock);
Jens Axboe31b51512019-01-18 22:56:34 -07005433 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005434 case IORING_OP_TEE:
5435 if (sqe) {
5436 ret = io_tee_prep(req, sqe);
5437 if (ret < 0)
5438 break;
5439 }
5440 ret = io_tee(req, force_nonblock);
5441 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005442 default:
5443 ret = -EINVAL;
5444 break;
5445 }
5446
5447 if (ret)
5448 return ret;
5449
5450 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07005451 const bool in_async = io_wq_current_is_worker();
5452
Jens Axboe9e645e112019-05-10 16:07:28 -06005453 if (req->result == -EAGAIN)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005454 return -EAGAIN;
5455
Jens Axboe11ba8202020-01-15 21:51:17 -07005456 /* workqueue context doesn't hold uring_lock, grab it now */
5457 if (in_async)
5458 mutex_lock(&ctx->uring_lock);
5459
Jens Axboe2b188cc2019-01-07 10:46:33 -07005460 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07005461
5462 if (in_async)
5463 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07005464 }
5465
5466 return 0;
5467}
5468
Jens Axboe561fb042019-10-24 07:25:42 -06005469static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboedef596e2019-01-09 08:59:42 -07005470{
Jens Axboe561fb042019-10-24 07:25:42 -06005471 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005472 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06005473 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005474
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005475 /* if NO_CANCEL is set, we must still run the work */
5476 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
5477 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06005478 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005479 }
Jens Axboe31b51512019-01-18 22:56:34 -07005480
Jens Axboe561fb042019-10-24 07:25:42 -06005481 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06005482 do {
Pavel Begunkov014db002020-03-03 21:33:12 +03005483 ret = io_issue_sqe(req, NULL, false);
Jens Axboe561fb042019-10-24 07:25:42 -06005484 /*
5485 * We can get EAGAIN for polled IO even though we're
5486 * forcing a sync submission from here, since we can't
5487 * wait for request slots on the block side.
5488 */
5489 if (ret != -EAGAIN)
5490 break;
5491 cond_resched();
5492 } while (1);
5493 }
Jens Axboe31b51512019-01-18 22:56:34 -07005494
Jens Axboe561fb042019-10-24 07:25:42 -06005495 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005496 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07005497 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06005498 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07005499 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005500
Pavel Begunkove9fd9392020-03-04 16:14:12 +03005501 io_steal_work(req, workptr);
Jens Axboe31b51512019-01-18 22:56:34 -07005502}
Jens Axboe2b188cc2019-01-07 10:46:33 -07005503
Jens Axboe65e19f52019-10-26 07:20:21 -06005504static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
5505 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06005506{
Jens Axboe65e19f52019-10-26 07:20:21 -06005507 struct fixed_file_table *table;
5508
Jens Axboe05f3fb32019-12-09 11:22:50 -07005509 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08005510 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06005511}
5512
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005513static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
5514 int fd, struct file **out_file, bool fixed)
5515{
5516 struct io_ring_ctx *ctx = req->ctx;
5517 struct file *file;
5518
5519 if (fixed) {
5520 if (unlikely(!ctx->file_data ||
5521 (unsigned) fd >= ctx->nr_user_files))
5522 return -EBADF;
5523 fd = array_index_nospec(fd, ctx->nr_user_files);
5524 file = io_file_from_index(ctx, fd);
5525 if (!file)
5526 return -EBADF;
Xiaoguang Wang05589552020-03-31 14:05:18 +08005527 req->fixed_file_refs = ctx->file_data->cur_refs;
5528 percpu_ref_get(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005529 } else {
5530 trace_io_uring_file_get(ctx, fd);
5531 file = __io_file_get(state, fd);
5532 if (unlikely(!file))
5533 return -EBADF;
5534 }
5535
5536 *out_file = file;
5537 return 0;
5538}
5539
Jens Axboe3529d8c2019-12-19 18:24:38 -07005540static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Jens Axboe63ff8222020-05-07 14:56:15 -06005541 int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06005542{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005543 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06005544
Jens Axboe63ff8222020-05-07 14:56:15 -06005545 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03005546 if (unlikely(!fixed && io_async_submit(req->ctx)))
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005547 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06005548
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005549 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06005550}
5551
Jackie Liua197f662019-11-08 08:09:12 -07005552static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005553{
Jens Axboefcb323c2019-10-24 12:39:47 -06005554 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07005555 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06005556
Jens Axboe5b0bbee2020-04-27 10:41:22 -06005557 if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE))
Jens Axboef86cd202020-01-29 13:46:44 -07005558 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005559 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07005560 return -EBADF;
5561
Jens Axboefcb323c2019-10-24 12:39:47 -06005562 rcu_read_lock();
5563 spin_lock_irq(&ctx->inflight_lock);
5564 /*
5565 * We use the f_ops->flush() handler to ensure that we can flush
5566 * out work accessing these files if the fd is closed. Check if
5567 * the fd has changed since we started down this path, and disallow
5568 * this operation if it has.
5569 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005570 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06005571 list_add(&req->inflight_entry, &ctx->inflight_list);
5572 req->flags |= REQ_F_INFLIGHT;
5573 req->work.files = current->files;
5574 ret = 0;
5575 }
5576 spin_unlock_irq(&ctx->inflight_lock);
5577 rcu_read_unlock();
5578
5579 return ret;
5580}
5581
Jens Axboe2665abf2019-11-05 12:40:47 -07005582static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
5583{
Jens Axboead8a48a2019-11-15 08:49:11 -07005584 struct io_timeout_data *data = container_of(timer,
5585 struct io_timeout_data, timer);
5586 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07005587 struct io_ring_ctx *ctx = req->ctx;
5588 struct io_kiocb *prev = NULL;
5589 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07005590
5591 spin_lock_irqsave(&ctx->completion_lock, flags);
5592
5593 /*
5594 * We don't expect the list to be empty, that will only happen if we
5595 * race with the completion of the linked work.
5596 */
Pavel Begunkov44932332019-12-05 16:16:35 +03005597 if (!list_empty(&req->link_list)) {
5598 prev = list_entry(req->link_list.prev, struct io_kiocb,
5599 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005600 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03005601 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005602 prev->flags &= ~REQ_F_LINK_TIMEOUT;
5603 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07005604 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005605 }
5606
5607 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5608
5609 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005610 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03005611 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07005612 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07005613 } else {
5614 io_cqring_add_event(req, -ETIME);
5615 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005616 }
Jens Axboe2665abf2019-11-05 12:40:47 -07005617 return HRTIMER_NORESTART;
5618}
5619
Jens Axboead8a48a2019-11-15 08:49:11 -07005620static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005621{
Jens Axboe76a46e02019-11-10 23:34:16 -07005622 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07005623
Jens Axboe76a46e02019-11-10 23:34:16 -07005624 /*
5625 * If the list is now empty, then our linked request finished before
5626 * we got a chance to setup the timer
5627 */
5628 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03005629 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07005630 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07005631
Jens Axboead8a48a2019-11-15 08:49:11 -07005632 data->timer.function = io_link_timeout_fn;
5633 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
5634 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07005635 }
Jens Axboe76a46e02019-11-10 23:34:16 -07005636 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07005637
Jens Axboe2665abf2019-11-05 12:40:47 -07005638 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07005639 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005640}
5641
Jens Axboead8a48a2019-11-15 08:49:11 -07005642static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005643{
5644 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005645
Pavel Begunkovdea3b492020-04-12 02:05:04 +03005646 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07005647 return NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005648 /* for polled retry, if flag is set, we already went through here */
5649 if (req->flags & REQ_F_POLLED)
5650 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005651
Pavel Begunkov44932332019-12-05 16:16:35 +03005652 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
5653 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07005654 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07005655 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005656
Jens Axboe76a46e02019-11-10 23:34:16 -07005657 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07005658 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07005659}
5660
Jens Axboe3529d8c2019-12-19 18:24:38 -07005661static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005662{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005663 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005664 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07005665 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005666 int ret;
5667
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005668again:
5669 linked_timeout = io_prep_linked_timeout(req);
5670
Jens Axboe193155c2020-02-22 23:22:19 -07005671 if (req->work.creds && req->work.creds != current_cred()) {
5672 if (old_creds)
5673 revert_creds(old_creds);
5674 if (old_creds == req->work.creds)
5675 old_creds = NULL; /* restored original creds */
5676 else
5677 old_creds = override_creds(req->work.creds);
5678 }
5679
Pavel Begunkov014db002020-03-03 21:33:12 +03005680 ret = io_issue_sqe(req, sqe, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06005681
5682 /*
5683 * We async punt it if the file wasn't marked NOWAIT, or if the file
5684 * doesn't support non-blocking read/write attempts
5685 */
5686 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
5687 (req->flags & REQ_F_MUST_PUNT))) {
Jens Axboed7718a92020-02-14 22:23:12 -07005688 if (io_arm_poll_handler(req)) {
5689 if (linked_timeout)
5690 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005691 goto exit;
Jens Axboed7718a92020-02-14 22:23:12 -07005692 }
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005693punt:
Jens Axboef86cd202020-01-29 13:46:44 -07005694 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005695 ret = io_grab_files(req);
5696 if (ret)
5697 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005698 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005699
5700 /*
5701 * Queued up for async execution, worker will release
5702 * submit reference when the iocb is actually submitted.
5703 */
5704 io_queue_async_work(req);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005705 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005706 }
Jens Axboee65ef562019-03-12 10:16:44 -06005707
Jens Axboefcb323c2019-10-24 12:39:47 -06005708err:
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005709 nxt = NULL;
Jens Axboee65ef562019-03-12 10:16:44 -06005710 /* drop submission reference */
Jens Axboe2a44f462020-02-25 13:25:41 -07005711 io_put_req_find_next(req, &nxt);
Jens Axboee65ef562019-03-12 10:16:44 -06005712
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005713 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07005714 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005715 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005716 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005717 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005718 }
5719
Jens Axboee65ef562019-03-12 10:16:44 -06005720 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06005721 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07005722 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005723 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06005724 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06005725 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005726 if (nxt) {
5727 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005728
5729 if (req->flags & REQ_F_FORCE_ASYNC)
5730 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005731 goto again;
5732 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005733exit:
Jens Axboe193155c2020-02-22 23:22:19 -07005734 if (old_creds)
5735 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005736}
5737
Jens Axboe3529d8c2019-12-19 18:24:38 -07005738static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005739{
5740 int ret;
5741
Jens Axboe3529d8c2019-12-19 18:24:38 -07005742 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005743 if (ret) {
5744 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03005745fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005746 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005747 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07005748 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005749 }
Pavel Begunkov25508782019-12-30 21:24:47 +03005750 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkov11185912020-01-22 23:09:35 +03005751 ret = io_req_defer_prep(req, sqe);
5752 if (unlikely(ret < 0))
5753 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07005754 /*
5755 * Never try inline submit of IOSQE_ASYNC is set, go straight
5756 * to async execution.
5757 */
5758 req->work.flags |= IO_WQ_WORK_CONCURRENT;
5759 io_queue_async_work(req);
5760 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005761 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07005762 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08005763}
5764
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005765static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005766{
Jens Axboe94ae5e72019-11-14 19:39:52 -07005767 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005768 io_cqring_add_event(req, -ECANCELED);
5769 io_double_put_req(req);
5770 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07005771 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005772}
5773
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005774static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Xiaoguang Wang7d01bd72020-05-08 21:19:30 +08005775 struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06005776{
Jackie Liua197f662019-11-08 08:09:12 -07005777 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005778 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06005779
Jens Axboe9e645e112019-05-10 16:07:28 -06005780 /*
5781 * If we already have a head request, queue this one for async
5782 * submittal once the head completes. If we don't have a head but
5783 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
5784 * submitted sync once the chain is complete. If none of those
5785 * conditions are true (normal request), then just queue it.
5786 */
5787 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03005788 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06005789
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03005790 /*
5791 * Taking sequential execution of a link, draining both sides
5792 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
5793 * requests in the link. So, it drains the head and the
5794 * next after the link request. The last one is done via
5795 * drain_next flag to persist the effect across calls.
5796 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005797 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03005798 head->flags |= REQ_F_IO_DRAIN;
5799 ctx->drain_next = 1;
5800 }
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005801 if (io_alloc_async_ctx(req))
5802 return -EAGAIN;
Jens Axboe9e645e112019-05-10 16:07:28 -06005803
Jens Axboe3529d8c2019-12-19 18:24:38 -07005804 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005805 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005806 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03005807 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005808 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07005809 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03005810 trace_io_uring_link(ctx, req, head);
5811 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06005812
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005813 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005814 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005815 io_queue_link_head(head);
5816 *link = NULL;
5817 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005818 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03005819 if (unlikely(ctx->drain_next)) {
5820 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005821 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03005822 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005823 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03005824 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03005825 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03005826
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005827 if (io_alloc_async_ctx(req))
5828 return -EAGAIN;
5829
Pavel Begunkov711be032020-01-17 03:57:59 +03005830 ret = io_req_defer_prep(req, sqe);
5831 if (ret)
5832 req->flags |= REQ_F_FAIL_LINK;
5833 *link = req;
5834 } else {
5835 io_queue_sqe(req, sqe);
5836 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005837 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005838
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005839 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06005840}
5841
Jens Axboe9a56a232019-01-09 09:06:50 -07005842/*
5843 * Batched submission is done, ensure local IO is flushed out.
5844 */
5845static void io_submit_state_end(struct io_submit_state *state)
5846{
5847 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03005848 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07005849 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03005850 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07005851}
5852
5853/*
5854 * Start submission side cache.
5855 */
5856static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08005857 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07005858{
5859 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07005860 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07005861 state->file = NULL;
5862 state->ios_left = max_ios;
5863}
5864
Jens Axboe2b188cc2019-01-07 10:46:33 -07005865static void io_commit_sqring(struct io_ring_ctx *ctx)
5866{
Hristo Venev75b28af2019-08-26 17:23:46 +00005867 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005868
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03005869 /*
5870 * Ensure any loads from the SQEs are done at this point,
5871 * since once we write the new head, the application could
5872 * write new data to them.
5873 */
5874 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005875}
5876
5877/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07005878 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07005879 * that is mapped by userspace. This means that care needs to be taken to
5880 * ensure that reads are stable, as we cannot rely on userspace always
5881 * being a good citizen. If members of the sqe are validated and then later
5882 * used, it's important that those reads are done through READ_ONCE() to
5883 * prevent a re-load down the line.
5884 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03005885static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005886{
Hristo Venev75b28af2019-08-26 17:23:46 +00005887 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005888 unsigned head;
5889
5890 /*
5891 * The cached sq head (or cq tail) serves two purposes:
5892 *
5893 * 1) allows us to batch the cost of updating the user visible
5894 * head updates.
5895 * 2) allows the kernel side to track the head on its own, even
5896 * though the application is the one updating it.
5897 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005898 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03005899 if (likely(head < ctx->sq_entries))
5900 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07005901
5902 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06005903 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005904 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03005905 return NULL;
5906}
5907
5908static inline void io_consume_sqe(struct io_ring_ctx *ctx)
5909{
5910 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005911}
5912
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005913#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
5914 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
5915 IOSQE_BUFFER_SELECT)
5916
5917static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
5918 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03005919 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005920{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005921 unsigned int sqe_flags;
Jens Axboe63ff8222020-05-07 14:56:15 -06005922 int id;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005923
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005924 /*
5925 * All io need record the previous position, if LINK vs DARIN,
5926 * it can be used to mark the position of the first IO in the
5927 * link list.
5928 */
Pavel Begunkov31af27c2020-04-15 00:39:50 +03005929 req->sequence = ctx->cached_sq_head - ctx->cached_sq_dropped;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005930 req->opcode = READ_ONCE(sqe->opcode);
5931 req->user_data = READ_ONCE(sqe->user_data);
5932 req->io = NULL;
5933 req->file = NULL;
5934 req->ctx = ctx;
5935 req->flags = 0;
5936 /* one is dropped after submission, the other at completion */
5937 refcount_set(&req->refs, 2);
5938 req->task = NULL;
5939 req->result = 0;
5940 INIT_IO_WORK(&req->work, io_wq_submit_work);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005941
5942 if (unlikely(req->opcode >= IORING_OP_LAST))
5943 return -EINVAL;
5944
5945 if (io_op_defs[req->opcode].needs_mm && !current->mm) {
5946 if (unlikely(!mmget_not_zero(ctx->sqo_mm)))
5947 return -EFAULT;
5948 use_mm(ctx->sqo_mm);
5949 }
5950
5951 sqe_flags = READ_ONCE(sqe->flags);
5952 /* enforce forwards compatibility on users */
5953 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
5954 return -EINVAL;
5955
5956 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
5957 !io_op_defs[req->opcode].buffer_select)
5958 return -EOPNOTSUPP;
5959
5960 id = READ_ONCE(sqe->personality);
5961 if (id) {
5962 req->work.creds = idr_find(&ctx->personality_idr, id);
5963 if (unlikely(!req->work.creds))
5964 return -EINVAL;
5965 get_cred(req->work.creds);
5966 }
5967
5968 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03005969 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005970
Jens Axboe63ff8222020-05-07 14:56:15 -06005971 if (!io_op_defs[req->opcode].needs_file)
5972 return 0;
5973
5974 return io_req_set_file(state, req, READ_ONCE(sqe->fd));
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005975}
5976
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005977static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03005978 struct file *ring_file, int ring_fd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07005979{
5980 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005981 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005982 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005983
Jens Axboec4a2ed72019-11-21 21:01:26 -07005984 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07005985 if (test_bit(0, &ctx->sq_check_overflow)) {
5986 if (!list_empty(&ctx->cq_overflow_list) &&
5987 !io_cqring_overflow_flush(ctx, false))
5988 return -EBUSY;
5989 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005990
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005991 /* make sure SQ entry isn't read before tail */
5992 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03005993
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005994 if (!percpu_ref_tryget_many(&ctx->refs, nr))
5995 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005996
5997 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08005998 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005999 statep = &state;
6000 }
6001
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006002 ctx->ring_fd = ring_fd;
6003 ctx->ring_file = ring_file;
6004
Jens Axboe6c271ce2019-01-10 11:22:30 -07006005 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006006 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006007 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006008 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006009
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006010 sqe = io_get_sqe(ctx);
6011 if (unlikely(!sqe)) {
6012 io_consume_sqe(ctx);
6013 break;
6014 }
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006015 req = io_alloc_req(ctx, statep);
Pavel Begunkov196be952019-11-07 01:41:06 +03006016 if (unlikely(!req)) {
6017 if (!submitted)
6018 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006019 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006020 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006021
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006022 err = io_init_req(ctx, req, sqe, statep);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006023 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006024 /* will complete beyond this point, count as submitted */
6025 submitted++;
6026
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006027 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006028fail_req:
6029 io_cqring_add_event(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006030 io_double_put_req(req);
6031 break;
6032 }
6033
Jens Axboe354420f2020-01-08 18:55:15 -07006034 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006035 true, io_async_submit(ctx));
Xiaoguang Wang7d01bd72020-05-08 21:19:30 +08006036 err = io_submit_sqe(req, sqe, &link);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006037 if (err)
6038 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006039 }
6040
Pavel Begunkov9466f432020-01-25 22:34:01 +03006041 if (unlikely(submitted != nr)) {
6042 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
6043
6044 percpu_ref_put_many(&ctx->refs, nr - ref_used);
6045 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006046 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006047 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006048 if (statep)
6049 io_submit_state_end(&state);
6050
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006051 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6052 io_commit_sqring(ctx);
6053
Jens Axboe6c271ce2019-01-10 11:22:30 -07006054 return submitted;
6055}
6056
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03006057static inline void io_sq_thread_drop_mm(struct io_ring_ctx *ctx)
6058{
6059 struct mm_struct *mm = current->mm;
6060
6061 if (mm) {
6062 unuse_mm(mm);
6063 mmput(mm);
6064 }
6065}
6066
Jens Axboe6c271ce2019-01-10 11:22:30 -07006067static int io_sq_thread(void *data)
6068{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006069 struct io_ring_ctx *ctx = data;
Jens Axboe181e4482019-11-25 08:52:30 -07006070 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006071 mm_segment_t old_fs;
6072 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006073 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006074 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006075
Jens Axboe0f158b42020-05-14 17:18:39 -06006076 complete(&ctx->sq_thread_comp);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08006077
Jens Axboe6c271ce2019-01-10 11:22:30 -07006078 old_fs = get_fs();
6079 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07006080 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006081
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006082 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006083 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006084 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006085
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006086 if (!list_empty(&ctx->poll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006087 unsigned nr_events = 0;
6088
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006089 mutex_lock(&ctx->uring_lock);
6090 if (!list_empty(&ctx->poll_list))
6091 io_iopoll_getevents(ctx, &nr_events, 0);
6092 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07006093 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006094 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006095 }
6096
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006097 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006098
6099 /*
6100 * If submit got -EBUSY, flag us as needing the application
6101 * to enter the kernel to reap and flush events.
6102 */
6103 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006104 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006105 * Drop cur_mm before scheduling, we can't hold it for
6106 * long periods (or over schedule()). Do this before
6107 * adding ourselves to the waitqueue, as the unuse/drop
6108 * may sleep.
6109 */
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03006110 io_sq_thread_drop_mm(ctx);
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006111
6112 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07006113 * We're polling. If we're within the defined idle
6114 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07006115 * to sleep. The exception is if we got EBUSY doing
6116 * more IO, we should wait for the application to
6117 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07006118 */
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006119 if (!list_empty(&ctx->poll_list) ||
Jens Axboedf069d82020-02-04 16:48:34 -07006120 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6121 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboeb41e9852020-02-17 09:52:41 -07006122 if (current->task_works)
6123 task_work_run();
Jens Axboe9831a902019-09-19 09:48:55 -06006124 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006125 continue;
6126 }
6127
Jens Axboe6c271ce2019-01-10 11:22:30 -07006128 prepare_to_wait(&ctx->sqo_wait, &wait,
6129 TASK_INTERRUPTIBLE);
6130
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006131 /*
6132 * While doing polled IO, before going to sleep, we need
6133 * to check if there are new reqs added to poll_list, it
6134 * is because reqs may have been punted to io worker and
6135 * will be added to poll_list later, hence check the
6136 * poll_list again.
6137 */
6138 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6139 !list_empty_careful(&ctx->poll_list)) {
6140 finish_wait(&ctx->sqo_wait, &wait);
6141 continue;
6142 }
6143
Jens Axboe6c271ce2019-01-10 11:22:30 -07006144 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00006145 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02006146 /* make sure to read SQ tail after writing flags */
6147 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006148
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006149 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006150 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006151 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006152 finish_wait(&ctx->sqo_wait, &wait);
6153 break;
6154 }
Jens Axboeb41e9852020-02-17 09:52:41 -07006155 if (current->task_works) {
6156 task_work_run();
Hillf Danton10bea962020-04-01 17:19:33 +08006157 finish_wait(&ctx->sqo_wait, &wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07006158 continue;
6159 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006160 if (signal_pending(current))
6161 flush_signals(current);
6162 schedule();
6163 finish_wait(&ctx->sqo_wait, &wait);
6164
Hristo Venev75b28af2019-08-26 17:23:46 +00006165 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006166 continue;
6167 }
6168 finish_wait(&ctx->sqo_wait, &wait);
6169
Hristo Venev75b28af2019-08-26 17:23:46 +00006170 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006171 }
6172
Jens Axboe8a4955f2019-12-09 14:52:35 -07006173 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang6b668c92020-05-20 15:35:03 +08006174 if (likely(!percpu_ref_is_dying(&ctx->refs)))
6175 ret = io_submit_sqes(ctx, to_submit, NULL, -1);
Jens Axboe8a4955f2019-12-09 14:52:35 -07006176 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006177 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006178 }
6179
Jens Axboeb41e9852020-02-17 09:52:41 -07006180 if (current->task_works)
6181 task_work_run();
6182
Jens Axboe6c271ce2019-01-10 11:22:30 -07006183 set_fs(old_fs);
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03006184 io_sq_thread_drop_mm(ctx);
Jens Axboe181e4482019-11-25 08:52:30 -07006185 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006186
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006187 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006188
Jens Axboe6c271ce2019-01-10 11:22:30 -07006189 return 0;
6190}
6191
Jens Axboebda52162019-09-24 13:47:15 -06006192struct io_wait_queue {
6193 struct wait_queue_entry wq;
6194 struct io_ring_ctx *ctx;
6195 unsigned to_wait;
6196 unsigned nr_timeouts;
6197};
6198
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006199static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06006200{
6201 struct io_ring_ctx *ctx = iowq->ctx;
6202
6203 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006204 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006205 * started waiting. For timeouts, we always want to return to userspace,
6206 * regardless of event count.
6207 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006208 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006209 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6210}
6211
6212static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6213 int wake_flags, void *key)
6214{
6215 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6216 wq);
6217
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006218 /* use noflush == true, as we can't safely rely on locking context */
6219 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06006220 return -1;
6221
6222 return autoremove_wake_function(curr, mode, wake_flags, key);
6223}
6224
Jens Axboe2b188cc2019-01-07 10:46:33 -07006225/*
6226 * Wait until events become available, if we don't already have some. The
6227 * application must reap them itself, as they reside on the shared cq ring.
6228 */
6229static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6230 const sigset_t __user *sig, size_t sigsz)
6231{
Jens Axboebda52162019-09-24 13:47:15 -06006232 struct io_wait_queue iowq = {
6233 .wq = {
6234 .private = current,
6235 .func = io_wake_function,
6236 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6237 },
6238 .ctx = ctx,
6239 .to_wait = min_events,
6240 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006241 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006242 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006243
Jens Axboeb41e9852020-02-17 09:52:41 -07006244 do {
6245 if (io_cqring_events(ctx, false) >= min_events)
6246 return 0;
6247 if (!current->task_works)
6248 break;
6249 task_work_run();
6250 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006251
6252 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006253#ifdef CONFIG_COMPAT
6254 if (in_compat_syscall())
6255 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006256 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006257 else
6258#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006259 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006260
Jens Axboe2b188cc2019-01-07 10:46:33 -07006261 if (ret)
6262 return ret;
6263 }
6264
Jens Axboebda52162019-09-24 13:47:15 -06006265 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006266 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006267 do {
6268 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6269 TASK_INTERRUPTIBLE);
Jens Axboeb41e9852020-02-17 09:52:41 -07006270 if (current->task_works)
6271 task_work_run();
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006272 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06006273 break;
6274 schedule();
6275 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006276 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06006277 break;
6278 }
6279 } while (1);
6280 finish_wait(&ctx->wait, &iowq.wq);
6281
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006282 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006283
Hristo Venev75b28af2019-08-26 17:23:46 +00006284 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006285}
6286
Jens Axboe6b063142019-01-10 22:13:58 -07006287static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6288{
6289#if defined(CONFIG_UNIX)
6290 if (ctx->ring_sock) {
6291 struct sock *sock = ctx->ring_sock->sk;
6292 struct sk_buff *skb;
6293
6294 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6295 kfree_skb(skb);
6296 }
6297#else
6298 int i;
6299
Jens Axboe65e19f52019-10-26 07:20:21 -06006300 for (i = 0; i < ctx->nr_user_files; i++) {
6301 struct file *file;
6302
6303 file = io_file_from_index(ctx, i);
6304 if (file)
6305 fput(file);
6306 }
Jens Axboe6b063142019-01-10 22:13:58 -07006307#endif
6308}
6309
Jens Axboe05f3fb32019-12-09 11:22:50 -07006310static void io_file_ref_kill(struct percpu_ref *ref)
6311{
6312 struct fixed_file_data *data;
6313
6314 data = container_of(ref, struct fixed_file_data, refs);
6315 complete(&data->done);
6316}
6317
Jens Axboe6b063142019-01-10 22:13:58 -07006318static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6319{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006320 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006321 struct fixed_file_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006322 unsigned nr_tables, i;
6323
Jens Axboe05f3fb32019-12-09 11:22:50 -07006324 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006325 return -ENXIO;
6326
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006327 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006328 if (!list_empty(&data->ref_list))
6329 ref_node = list_first_entry(&data->ref_list,
6330 struct fixed_file_ref_node, node);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006331 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006332 if (ref_node)
6333 percpu_ref_kill(&ref_node->refs);
6334
6335 percpu_ref_kill(&data->refs);
6336
6337 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06006338 flush_delayed_work(&ctx->file_put_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07006339 wait_for_completion(&data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006340
Jens Axboe6b063142019-01-10 22:13:58 -07006341 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06006342 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6343 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006344 kfree(data->table[i].files);
6345 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006346 percpu_ref_exit(&data->refs);
6347 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006348 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006349 ctx->nr_user_files = 0;
6350 return 0;
6351}
6352
Jens Axboe6c271ce2019-01-10 11:22:30 -07006353static void io_sq_thread_stop(struct io_ring_ctx *ctx)
6354{
6355 if (ctx->sqo_thread) {
Jens Axboe0f158b42020-05-14 17:18:39 -06006356 wait_for_completion(&ctx->sq_thread_comp);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006357 /*
6358 * The park is a bit of a work-around, without it we get
6359 * warning spews on shutdown with SQPOLL set and affinity
6360 * set to a single CPU.
6361 */
Jens Axboe06058632019-04-13 09:26:03 -06006362 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006363 kthread_stop(ctx->sqo_thread);
6364 ctx->sqo_thread = NULL;
6365 }
6366}
6367
Jens Axboe6b063142019-01-10 22:13:58 -07006368static void io_finish_async(struct io_ring_ctx *ctx)
6369{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006370 io_sq_thread_stop(ctx);
6371
Jens Axboe561fb042019-10-24 07:25:42 -06006372 if (ctx->io_wq) {
6373 io_wq_destroy(ctx->io_wq);
6374 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006375 }
6376}
6377
6378#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07006379/*
6380 * Ensure the UNIX gc is aware of our file set, so we are certain that
6381 * the io_uring can be safely unregistered on process exit, even if we have
6382 * loops in the file referencing.
6383 */
6384static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
6385{
6386 struct sock *sk = ctx->ring_sock->sk;
6387 struct scm_fp_list *fpl;
6388 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06006389 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07006390
Jens Axboe6b063142019-01-10 22:13:58 -07006391 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
6392 if (!fpl)
6393 return -ENOMEM;
6394
6395 skb = alloc_skb(0, GFP_KERNEL);
6396 if (!skb) {
6397 kfree(fpl);
6398 return -ENOMEM;
6399 }
6400
6401 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07006402
Jens Axboe08a45172019-10-03 08:11:03 -06006403 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07006404 fpl->user = get_uid(ctx->user);
6405 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006406 struct file *file = io_file_from_index(ctx, i + offset);
6407
6408 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06006409 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06006410 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06006411 unix_inflight(fpl->user, fpl->fp[nr_files]);
6412 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07006413 }
6414
Jens Axboe08a45172019-10-03 08:11:03 -06006415 if (nr_files) {
6416 fpl->max = SCM_MAX_FD;
6417 fpl->count = nr_files;
6418 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006419 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06006420 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
6421 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07006422
Jens Axboe08a45172019-10-03 08:11:03 -06006423 for (i = 0; i < nr_files; i++)
6424 fput(fpl->fp[i]);
6425 } else {
6426 kfree_skb(skb);
6427 kfree(fpl);
6428 }
Jens Axboe6b063142019-01-10 22:13:58 -07006429
6430 return 0;
6431}
6432
6433/*
6434 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
6435 * causes regular reference counting to break down. We rely on the UNIX
6436 * garbage collection to take care of this problem for us.
6437 */
6438static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6439{
6440 unsigned left, total;
6441 int ret = 0;
6442
6443 total = 0;
6444 left = ctx->nr_user_files;
6445 while (left) {
6446 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07006447
6448 ret = __io_sqe_files_scm(ctx, this_files, total);
6449 if (ret)
6450 break;
6451 left -= this_files;
6452 total += this_files;
6453 }
6454
6455 if (!ret)
6456 return 0;
6457
6458 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006459 struct file *file = io_file_from_index(ctx, total);
6460
6461 if (file)
6462 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07006463 total++;
6464 }
6465
6466 return ret;
6467}
6468#else
6469static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6470{
6471 return 0;
6472}
6473#endif
6474
Jens Axboe65e19f52019-10-26 07:20:21 -06006475static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
6476 unsigned nr_files)
6477{
6478 int i;
6479
6480 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006481 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006482 unsigned this_files;
6483
6484 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
6485 table->files = kcalloc(this_files, sizeof(struct file *),
6486 GFP_KERNEL);
6487 if (!table->files)
6488 break;
6489 nr_files -= this_files;
6490 }
6491
6492 if (i == nr_tables)
6493 return 0;
6494
6495 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006496 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006497 kfree(table->files);
6498 }
6499 return 1;
6500}
6501
Jens Axboe05f3fb32019-12-09 11:22:50 -07006502static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06006503{
6504#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06006505 struct sock *sock = ctx->ring_sock->sk;
6506 struct sk_buff_head list, *head = &sock->sk_receive_queue;
6507 struct sk_buff *skb;
6508 int i;
6509
6510 __skb_queue_head_init(&list);
6511
6512 /*
6513 * Find the skb that holds this file in its SCM_RIGHTS. When found,
6514 * remove this entry and rearrange the file array.
6515 */
6516 skb = skb_dequeue(head);
6517 while (skb) {
6518 struct scm_fp_list *fp;
6519
6520 fp = UNIXCB(skb).fp;
6521 for (i = 0; i < fp->count; i++) {
6522 int left;
6523
6524 if (fp->fp[i] != file)
6525 continue;
6526
6527 unix_notinflight(fp->user, fp->fp[i]);
6528 left = fp->count - 1 - i;
6529 if (left) {
6530 memmove(&fp->fp[i], &fp->fp[i + 1],
6531 left * sizeof(struct file *));
6532 }
6533 fp->count--;
6534 if (!fp->count) {
6535 kfree_skb(skb);
6536 skb = NULL;
6537 } else {
6538 __skb_queue_tail(&list, skb);
6539 }
6540 fput(file);
6541 file = NULL;
6542 break;
6543 }
6544
6545 if (!file)
6546 break;
6547
6548 __skb_queue_tail(&list, skb);
6549
6550 skb = skb_dequeue(head);
6551 }
6552
6553 if (skb_peek(&list)) {
6554 spin_lock_irq(&head->lock);
6555 while ((skb = __skb_dequeue(&list)) != NULL)
6556 __skb_queue_tail(head, skb);
6557 spin_unlock_irq(&head->lock);
6558 }
6559#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07006560 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06006561#endif
6562}
6563
Jens Axboe05f3fb32019-12-09 11:22:50 -07006564struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08006565 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006566 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006567};
6568
Jens Axboe4a38aed22020-05-14 17:21:15 -06006569static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006570{
Jens Axboe4a38aed22020-05-14 17:21:15 -06006571 struct fixed_file_data *file_data = ref_node->file_data;
6572 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006573 struct io_file_put *pfile, *tmp;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006574
Xiaoguang Wang05589552020-03-31 14:05:18 +08006575 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006576 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006577 io_ring_file_put(ctx, pfile->file);
6578 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006579 }
6580
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006581 spin_lock(&file_data->lock);
6582 list_del(&ref_node->node);
6583 spin_unlock(&file_data->lock);
Jens Axboe2faf8522020-02-04 19:54:55 -07006584
Xiaoguang Wang05589552020-03-31 14:05:18 +08006585 percpu_ref_exit(&ref_node->refs);
6586 kfree(ref_node);
6587 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006588}
6589
Jens Axboe4a38aed22020-05-14 17:21:15 -06006590static void io_file_put_work(struct work_struct *work)
6591{
6592 struct io_ring_ctx *ctx;
6593 struct llist_node *node;
6594
6595 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
6596 node = llist_del_all(&ctx->file_put_llist);
6597
6598 while (node) {
6599 struct fixed_file_ref_node *ref_node;
6600 struct llist_node *next = node->next;
6601
6602 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
6603 __io_file_put_work(ref_node);
6604 node = next;
6605 }
6606}
6607
Jens Axboe05f3fb32019-12-09 11:22:50 -07006608static void io_file_data_ref_zero(struct percpu_ref *ref)
6609{
Xiaoguang Wang05589552020-03-31 14:05:18 +08006610 struct fixed_file_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06006611 struct io_ring_ctx *ctx;
6612 bool first_add;
6613 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006614
Xiaoguang Wang05589552020-03-31 14:05:18 +08006615 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Jens Axboe4a38aed22020-05-14 17:21:15 -06006616 ctx = ref_node->file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006617
Jens Axboe4a38aed22020-05-14 17:21:15 -06006618 if (percpu_ref_is_dying(&ctx->file_data->refs))
6619 delay = 0;
6620
6621 first_add = llist_add(&ref_node->llist, &ctx->file_put_llist);
6622 if (!delay)
6623 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
6624 else if (first_add)
6625 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006626}
6627
6628static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
6629 struct io_ring_ctx *ctx)
6630{
6631 struct fixed_file_ref_node *ref_node;
6632
6633 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
6634 if (!ref_node)
6635 return ERR_PTR(-ENOMEM);
6636
6637 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
6638 0, GFP_KERNEL)) {
6639 kfree(ref_node);
6640 return ERR_PTR(-ENOMEM);
6641 }
6642 INIT_LIST_HEAD(&ref_node->node);
6643 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006644 ref_node->file_data = ctx->file_data;
6645 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006646}
6647
6648static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
6649{
6650 percpu_ref_exit(&ref_node->refs);
6651 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006652}
6653
6654static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
6655 unsigned nr_args)
6656{
6657 __s32 __user *fds = (__s32 __user *) arg;
6658 unsigned nr_tables;
6659 struct file *file;
6660 int fd, ret = 0;
6661 unsigned i;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006662 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006663
6664 if (ctx->file_data)
6665 return -EBUSY;
6666 if (!nr_args)
6667 return -EINVAL;
6668 if (nr_args > IORING_MAX_FIXED_FILES)
6669 return -EMFILE;
6670
6671 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
6672 if (!ctx->file_data)
6673 return -ENOMEM;
6674 ctx->file_data->ctx = ctx;
6675 init_completion(&ctx->file_data->done);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006676 INIT_LIST_HEAD(&ctx->file_data->ref_list);
Xiaoguang Wangf7fe9342020-04-07 20:02:31 +08006677 spin_lock_init(&ctx->file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006678
6679 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
6680 ctx->file_data->table = kcalloc(nr_tables,
6681 sizeof(struct fixed_file_table),
6682 GFP_KERNEL);
6683 if (!ctx->file_data->table) {
6684 kfree(ctx->file_data);
6685 ctx->file_data = NULL;
6686 return -ENOMEM;
6687 }
6688
Xiaoguang Wang05589552020-03-31 14:05:18 +08006689 if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill,
Jens Axboe05f3fb32019-12-09 11:22:50 -07006690 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
6691 kfree(ctx->file_data->table);
6692 kfree(ctx->file_data);
6693 ctx->file_data = NULL;
6694 return -ENOMEM;
6695 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006696
6697 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
6698 percpu_ref_exit(&ctx->file_data->refs);
6699 kfree(ctx->file_data->table);
6700 kfree(ctx->file_data);
6701 ctx->file_data = NULL;
6702 return -ENOMEM;
6703 }
6704
6705 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
6706 struct fixed_file_table *table;
6707 unsigned index;
6708
6709 ret = -EFAULT;
6710 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
6711 break;
6712 /* allow sparse sets */
6713 if (fd == -1) {
6714 ret = 0;
6715 continue;
6716 }
6717
6718 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6719 index = i & IORING_FILE_TABLE_MASK;
6720 file = fget(fd);
6721
6722 ret = -EBADF;
6723 if (!file)
6724 break;
6725
6726 /*
6727 * Don't allow io_uring instances to be registered. If UNIX
6728 * isn't enabled, then this causes a reference cycle and this
6729 * instance can never get freed. If UNIX is enabled we'll
6730 * handle it just fine, but there's still no point in allowing
6731 * a ring fd as it doesn't support regular read/write anyway.
6732 */
6733 if (file->f_op == &io_uring_fops) {
6734 fput(file);
6735 break;
6736 }
6737 ret = 0;
6738 table->files[index] = file;
6739 }
6740
6741 if (ret) {
6742 for (i = 0; i < ctx->nr_user_files; i++) {
6743 file = io_file_from_index(ctx, i);
6744 if (file)
6745 fput(file);
6746 }
6747 for (i = 0; i < nr_tables; i++)
6748 kfree(ctx->file_data->table[i].files);
6749
6750 kfree(ctx->file_data->table);
6751 kfree(ctx->file_data);
6752 ctx->file_data = NULL;
6753 ctx->nr_user_files = 0;
6754 return ret;
6755 }
6756
6757 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006758 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006759 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006760 return ret;
6761 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006762
Xiaoguang Wang05589552020-03-31 14:05:18 +08006763 ref_node = alloc_fixed_file_ref_node(ctx);
6764 if (IS_ERR(ref_node)) {
6765 io_sqe_files_unregister(ctx);
6766 return PTR_ERR(ref_node);
6767 }
6768
6769 ctx->file_data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006770 spin_lock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006771 list_add(&ref_node->node, &ctx->file_data->ref_list);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006772 spin_unlock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006773 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006774 return ret;
6775}
6776
Jens Axboec3a31e62019-10-03 13:59:56 -06006777static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
6778 int index)
6779{
6780#if defined(CONFIG_UNIX)
6781 struct sock *sock = ctx->ring_sock->sk;
6782 struct sk_buff_head *head = &sock->sk_receive_queue;
6783 struct sk_buff *skb;
6784
6785 /*
6786 * See if we can merge this file into an existing skb SCM_RIGHTS
6787 * file set. If there's no room, fall back to allocating a new skb
6788 * and filling it in.
6789 */
6790 spin_lock_irq(&head->lock);
6791 skb = skb_peek(head);
6792 if (skb) {
6793 struct scm_fp_list *fpl = UNIXCB(skb).fp;
6794
6795 if (fpl->count < SCM_MAX_FD) {
6796 __skb_unlink(skb, head);
6797 spin_unlock_irq(&head->lock);
6798 fpl->fp[fpl->count] = get_file(file);
6799 unix_inflight(fpl->user, fpl->fp[fpl->count]);
6800 fpl->count++;
6801 spin_lock_irq(&head->lock);
6802 __skb_queue_head(head, skb);
6803 } else {
6804 skb = NULL;
6805 }
6806 }
6807 spin_unlock_irq(&head->lock);
6808
6809 if (skb) {
6810 fput(file);
6811 return 0;
6812 }
6813
6814 return __io_sqe_files_scm(ctx, 1, index);
6815#else
6816 return 0;
6817#endif
6818}
6819
Hillf Dantona5318d32020-03-23 17:47:15 +08006820static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08006821 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006822{
Hillf Dantona5318d32020-03-23 17:47:15 +08006823 struct io_file_put *pfile;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006824 struct percpu_ref *refs = data->cur_refs;
6825 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006826
Jens Axboe05f3fb32019-12-09 11:22:50 -07006827 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08006828 if (!pfile)
6829 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006830
Xiaoguang Wang05589552020-03-31 14:05:18 +08006831 ref_node = container_of(refs, struct fixed_file_ref_node, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006832 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006833 list_add(&pfile->list, &ref_node->file_list);
6834
Hillf Dantona5318d32020-03-23 17:47:15 +08006835 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006836}
6837
6838static int __io_sqe_files_update(struct io_ring_ctx *ctx,
6839 struct io_uring_files_update *up,
6840 unsigned nr_args)
6841{
6842 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006843 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006844 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006845 __s32 __user *fds;
6846 int fd, i, err;
6847 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006848 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06006849
Jens Axboe05f3fb32019-12-09 11:22:50 -07006850 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06006851 return -EOVERFLOW;
6852 if (done > ctx->nr_user_files)
6853 return -EINVAL;
6854
Xiaoguang Wang05589552020-03-31 14:05:18 +08006855 ref_node = alloc_fixed_file_ref_node(ctx);
6856 if (IS_ERR(ref_node))
6857 return PTR_ERR(ref_node);
6858
Jens Axboec3a31e62019-10-03 13:59:56 -06006859 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006860 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06006861 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006862 struct fixed_file_table *table;
6863 unsigned index;
6864
Jens Axboec3a31e62019-10-03 13:59:56 -06006865 err = 0;
6866 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
6867 err = -EFAULT;
6868 break;
6869 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006870 i = array_index_nospec(up->offset, ctx->nr_user_files);
6871 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06006872 index = i & IORING_FILE_TABLE_MASK;
6873 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006874 file = io_file_from_index(ctx, index);
Hillf Dantona5318d32020-03-23 17:47:15 +08006875 err = io_queue_file_removal(data, file);
6876 if (err)
6877 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06006878 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006879 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06006880 }
6881 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06006882 file = fget(fd);
6883 if (!file) {
6884 err = -EBADF;
6885 break;
6886 }
6887 /*
6888 * Don't allow io_uring instances to be registered. If
6889 * UNIX isn't enabled, then this causes a reference
6890 * cycle and this instance can never get freed. If UNIX
6891 * is enabled we'll handle it just fine, but there's
6892 * still no point in allowing a ring fd as it doesn't
6893 * support regular read/write anyway.
6894 */
6895 if (file->f_op == &io_uring_fops) {
6896 fput(file);
6897 err = -EBADF;
6898 break;
6899 }
Jens Axboe65e19f52019-10-26 07:20:21 -06006900 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006901 err = io_sqe_file_register(ctx, file, i);
6902 if (err)
6903 break;
6904 }
6905 nr_args--;
6906 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006907 up->offset++;
6908 }
6909
Xiaoguang Wang05589552020-03-31 14:05:18 +08006910 if (needs_switch) {
6911 percpu_ref_kill(data->cur_refs);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006912 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006913 list_add(&ref_node->node, &data->ref_list);
6914 data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006915 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006916 percpu_ref_get(&ctx->file_data->refs);
6917 } else
6918 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06006919
6920 return done ? done : err;
6921}
Xiaoguang Wang05589552020-03-31 14:05:18 +08006922
Jens Axboe05f3fb32019-12-09 11:22:50 -07006923static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
6924 unsigned nr_args)
6925{
6926 struct io_uring_files_update up;
6927
6928 if (!ctx->file_data)
6929 return -ENXIO;
6930 if (!nr_args)
6931 return -EINVAL;
6932 if (copy_from_user(&up, arg, sizeof(up)))
6933 return -EFAULT;
6934 if (up.resv)
6935 return -EINVAL;
6936
6937 return __io_sqe_files_update(ctx, &up, nr_args);
6938}
Jens Axboec3a31e62019-10-03 13:59:56 -06006939
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006940static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07006941{
6942 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6943
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006944 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07006945 io_put_req(req);
6946}
6947
Pavel Begunkov24369c22020-01-28 03:15:48 +03006948static int io_init_wq_offload(struct io_ring_ctx *ctx,
6949 struct io_uring_params *p)
6950{
6951 struct io_wq_data data;
6952 struct fd f;
6953 struct io_ring_ctx *ctx_attach;
6954 unsigned int concurrency;
6955 int ret = 0;
6956
6957 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006958 data.free_work = io_free_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03006959
6960 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
6961 /* Do QD, or 4 * CPUS, whatever is smallest */
6962 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
6963
6964 ctx->io_wq = io_wq_create(concurrency, &data);
6965 if (IS_ERR(ctx->io_wq)) {
6966 ret = PTR_ERR(ctx->io_wq);
6967 ctx->io_wq = NULL;
6968 }
6969 return ret;
6970 }
6971
6972 f = fdget(p->wq_fd);
6973 if (!f.file)
6974 return -EBADF;
6975
6976 if (f.file->f_op != &io_uring_fops) {
6977 ret = -EINVAL;
6978 goto out_fput;
6979 }
6980
6981 ctx_attach = f.file->private_data;
6982 /* @io_wq is protected by holding the fd */
6983 if (!io_wq_get(ctx_attach->io_wq, &data)) {
6984 ret = -EINVAL;
6985 goto out_fput;
6986 }
6987
6988 ctx->io_wq = ctx_attach->io_wq;
6989out_fput:
6990 fdput(f);
6991 return ret;
6992}
6993
Jens Axboe6c271ce2019-01-10 11:22:30 -07006994static int io_sq_offload_start(struct io_ring_ctx *ctx,
6995 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006996{
6997 int ret;
6998
Jens Axboe6c271ce2019-01-10 11:22:30 -07006999 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007000 mmgrab(current->mm);
7001 ctx->sqo_mm = current->mm;
7002
Jens Axboe6c271ce2019-01-10 11:22:30 -07007003 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06007004 ret = -EPERM;
7005 if (!capable(CAP_SYS_ADMIN))
7006 goto err;
7007
Jens Axboe917257d2019-04-13 09:28:55 -06007008 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7009 if (!ctx->sq_thread_idle)
7010 ctx->sq_thread_idle = HZ;
7011
Jens Axboe6c271ce2019-01-10 11:22:30 -07007012 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007013 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007014
Jens Axboe917257d2019-04-13 09:28:55 -06007015 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007016 if (cpu >= nr_cpu_ids)
7017 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007018 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007019 goto err;
7020
Jens Axboe6c271ce2019-01-10 11:22:30 -07007021 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
7022 ctx, cpu,
7023 "io_uring-sq");
7024 } else {
7025 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
7026 "io_uring-sq");
7027 }
7028 if (IS_ERR(ctx->sqo_thread)) {
7029 ret = PTR_ERR(ctx->sqo_thread);
7030 ctx->sqo_thread = NULL;
7031 goto err;
7032 }
7033 wake_up_process(ctx->sqo_thread);
7034 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7035 /* Can't have SQ_AFF without SQPOLL */
7036 ret = -EINVAL;
7037 goto err;
7038 }
7039
Pavel Begunkov24369c22020-01-28 03:15:48 +03007040 ret = io_init_wq_offload(ctx, p);
7041 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007042 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007043
7044 return 0;
7045err:
Jens Axboe54a91f32019-09-10 09:15:04 -06007046 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007047 mmdrop(ctx->sqo_mm);
7048 ctx->sqo_mm = NULL;
7049 return ret;
7050}
7051
7052static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
7053{
7054 atomic_long_sub(nr_pages, &user->locked_vm);
7055}
7056
7057static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
7058{
7059 unsigned long page_limit, cur_pages, new_pages;
7060
7061 /* Don't allow more pages than we can safely lock */
7062 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
7063
7064 do {
7065 cur_pages = atomic_long_read(&user->locked_vm);
7066 new_pages = cur_pages + nr_pages;
7067 if (new_pages > page_limit)
7068 return -ENOMEM;
7069 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7070 new_pages) != cur_pages);
7071
7072 return 0;
7073}
7074
7075static void io_mem_free(void *ptr)
7076{
Mark Rutland52e04ef2019-04-30 17:30:21 +01007077 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007078
Mark Rutland52e04ef2019-04-30 17:30:21 +01007079 if (!ptr)
7080 return;
7081
7082 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007083 if (put_page_testzero(page))
7084 free_compound_page(page);
7085}
7086
7087static void *io_mem_alloc(size_t size)
7088{
7089 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
7090 __GFP_NORETRY;
7091
7092 return (void *) __get_free_pages(gfp_flags, get_order(size));
7093}
7094
Hristo Venev75b28af2019-08-26 17:23:46 +00007095static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
7096 size_t *sq_offset)
7097{
7098 struct io_rings *rings;
7099 size_t off, sq_array_size;
7100
7101 off = struct_size(rings, cqes, cq_entries);
7102 if (off == SIZE_MAX)
7103 return SIZE_MAX;
7104
7105#ifdef CONFIG_SMP
7106 off = ALIGN(off, SMP_CACHE_BYTES);
7107 if (off == 0)
7108 return SIZE_MAX;
7109#endif
7110
7111 sq_array_size = array_size(sizeof(u32), sq_entries);
7112 if (sq_array_size == SIZE_MAX)
7113 return SIZE_MAX;
7114
7115 if (check_add_overflow(off, sq_array_size, &off))
7116 return SIZE_MAX;
7117
7118 if (sq_offset)
7119 *sq_offset = off;
7120
7121 return off;
7122}
7123
Jens Axboe2b188cc2019-01-07 10:46:33 -07007124static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
7125{
Hristo Venev75b28af2019-08-26 17:23:46 +00007126 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007127
Hristo Venev75b28af2019-08-26 17:23:46 +00007128 pages = (size_t)1 << get_order(
7129 rings_size(sq_entries, cq_entries, NULL));
7130 pages += (size_t)1 << get_order(
7131 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07007132
Hristo Venev75b28af2019-08-26 17:23:46 +00007133 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007134}
7135
Jens Axboeedafcce2019-01-09 09:16:05 -07007136static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
7137{
7138 int i, j;
7139
7140 if (!ctx->user_bufs)
7141 return -ENXIO;
7142
7143 for (i = 0; i < ctx->nr_user_bufs; i++) {
7144 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7145
7146 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007147 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07007148
7149 if (ctx->account_mem)
7150 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007151 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007152 imu->nr_bvecs = 0;
7153 }
7154
7155 kfree(ctx->user_bufs);
7156 ctx->user_bufs = NULL;
7157 ctx->nr_user_bufs = 0;
7158 return 0;
7159}
7160
7161static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
7162 void __user *arg, unsigned index)
7163{
7164 struct iovec __user *src;
7165
7166#ifdef CONFIG_COMPAT
7167 if (ctx->compat) {
7168 struct compat_iovec __user *ciovs;
7169 struct compat_iovec ciov;
7170
7171 ciovs = (struct compat_iovec __user *) arg;
7172 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
7173 return -EFAULT;
7174
Jens Axboed55e5f52019-12-11 16:12:15 -07007175 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07007176 dst->iov_len = ciov.iov_len;
7177 return 0;
7178 }
7179#endif
7180 src = (struct iovec __user *) arg;
7181 if (copy_from_user(dst, &src[index], sizeof(*dst)))
7182 return -EFAULT;
7183 return 0;
7184}
7185
7186static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
7187 unsigned nr_args)
7188{
7189 struct vm_area_struct **vmas = NULL;
7190 struct page **pages = NULL;
7191 int i, j, got_pages = 0;
7192 int ret = -EINVAL;
7193
7194 if (ctx->user_bufs)
7195 return -EBUSY;
7196 if (!nr_args || nr_args > UIO_MAXIOV)
7197 return -EINVAL;
7198
7199 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
7200 GFP_KERNEL);
7201 if (!ctx->user_bufs)
7202 return -ENOMEM;
7203
7204 for (i = 0; i < nr_args; i++) {
7205 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7206 unsigned long off, start, end, ubuf;
7207 int pret, nr_pages;
7208 struct iovec iov;
7209 size_t size;
7210
7211 ret = io_copy_iov(ctx, &iov, arg, i);
7212 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03007213 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007214
7215 /*
7216 * Don't impose further limits on the size and buffer
7217 * constraints here, we'll -EINVAL later when IO is
7218 * submitted if they are wrong.
7219 */
7220 ret = -EFAULT;
7221 if (!iov.iov_base || !iov.iov_len)
7222 goto err;
7223
7224 /* arbitrary limit, but we need something */
7225 if (iov.iov_len > SZ_1G)
7226 goto err;
7227
7228 ubuf = (unsigned long) iov.iov_base;
7229 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
7230 start = ubuf >> PAGE_SHIFT;
7231 nr_pages = end - start;
7232
7233 if (ctx->account_mem) {
7234 ret = io_account_mem(ctx->user, nr_pages);
7235 if (ret)
7236 goto err;
7237 }
7238
7239 ret = 0;
7240 if (!pages || nr_pages > got_pages) {
7241 kfree(vmas);
7242 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007243 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07007244 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007245 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07007246 sizeof(struct vm_area_struct *),
7247 GFP_KERNEL);
7248 if (!pages || !vmas) {
7249 ret = -ENOMEM;
7250 if (ctx->account_mem)
7251 io_unaccount_mem(ctx->user, nr_pages);
7252 goto err;
7253 }
7254 got_pages = nr_pages;
7255 }
7256
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007257 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07007258 GFP_KERNEL);
7259 ret = -ENOMEM;
7260 if (!imu->bvec) {
7261 if (ctx->account_mem)
7262 io_unaccount_mem(ctx->user, nr_pages);
7263 goto err;
7264 }
7265
7266 ret = 0;
7267 down_read(&current->mm->mmap_sem);
John Hubbard2113b052020-01-30 22:13:13 -08007268 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07007269 FOLL_WRITE | FOLL_LONGTERM,
7270 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007271 if (pret == nr_pages) {
7272 /* don't support file backed memory */
7273 for (j = 0; j < nr_pages; j++) {
7274 struct vm_area_struct *vma = vmas[j];
7275
7276 if (vma->vm_file &&
7277 !is_file_hugepages(vma->vm_file)) {
7278 ret = -EOPNOTSUPP;
7279 break;
7280 }
7281 }
7282 } else {
7283 ret = pret < 0 ? pret : -EFAULT;
7284 }
7285 up_read(&current->mm->mmap_sem);
7286 if (ret) {
7287 /*
7288 * if we did partial map, or found file backed vmas,
7289 * release any pages we did get
7290 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07007291 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007292 unpin_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07007293 if (ctx->account_mem)
7294 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007295 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007296 goto err;
7297 }
7298
7299 off = ubuf & ~PAGE_MASK;
7300 size = iov.iov_len;
7301 for (j = 0; j < nr_pages; j++) {
7302 size_t vec_len;
7303
7304 vec_len = min_t(size_t, size, PAGE_SIZE - off);
7305 imu->bvec[j].bv_page = pages[j];
7306 imu->bvec[j].bv_len = vec_len;
7307 imu->bvec[j].bv_offset = off;
7308 off = 0;
7309 size -= vec_len;
7310 }
7311 /* store original address for later verification */
7312 imu->ubuf = ubuf;
7313 imu->len = iov.iov_len;
7314 imu->nr_bvecs = nr_pages;
7315
7316 ctx->nr_user_bufs++;
7317 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007318 kvfree(pages);
7319 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007320 return 0;
7321err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007322 kvfree(pages);
7323 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007324 io_sqe_buffer_unregister(ctx);
7325 return ret;
7326}
7327
Jens Axboe9b402842019-04-11 11:45:41 -06007328static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
7329{
7330 __s32 __user *fds = arg;
7331 int fd;
7332
7333 if (ctx->cq_ev_fd)
7334 return -EBUSY;
7335
7336 if (copy_from_user(&fd, fds, sizeof(*fds)))
7337 return -EFAULT;
7338
7339 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
7340 if (IS_ERR(ctx->cq_ev_fd)) {
7341 int ret = PTR_ERR(ctx->cq_ev_fd);
7342 ctx->cq_ev_fd = NULL;
7343 return ret;
7344 }
7345
7346 return 0;
7347}
7348
7349static int io_eventfd_unregister(struct io_ring_ctx *ctx)
7350{
7351 if (ctx->cq_ev_fd) {
7352 eventfd_ctx_put(ctx->cq_ev_fd);
7353 ctx->cq_ev_fd = NULL;
7354 return 0;
7355 }
7356
7357 return -ENXIO;
7358}
7359
Jens Axboe5a2e7452020-02-23 16:23:11 -07007360static int __io_destroy_buffers(int id, void *p, void *data)
7361{
7362 struct io_ring_ctx *ctx = data;
7363 struct io_buffer *buf = p;
7364
Jens Axboe067524e2020-03-02 16:32:28 -07007365 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007366 return 0;
7367}
7368
7369static void io_destroy_buffers(struct io_ring_ctx *ctx)
7370{
7371 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
7372 idr_destroy(&ctx->io_buffer_idr);
7373}
7374
Jens Axboe2b188cc2019-01-07 10:46:33 -07007375static void io_ring_ctx_free(struct io_ring_ctx *ctx)
7376{
Jens Axboe6b063142019-01-10 22:13:58 -07007377 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007378 if (ctx->sqo_mm)
7379 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07007380
7381 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07007382 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07007383 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06007384 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007385 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07007386 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07007387
Jens Axboe2b188cc2019-01-07 10:46:33 -07007388#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07007389 if (ctx->ring_sock) {
7390 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007391 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07007392 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007393#endif
7394
Hristo Venev75b28af2019-08-26 17:23:46 +00007395 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007396 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007397
7398 percpu_ref_exit(&ctx->refs);
7399 if (ctx->account_mem)
7400 io_unaccount_mem(ctx->user,
7401 ring_pages(ctx->sq_entries, ctx->cq_entries));
7402 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07007403 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07007404 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07007405 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007406 kfree(ctx);
7407}
7408
7409static __poll_t io_uring_poll(struct file *file, poll_table *wait)
7410{
7411 struct io_ring_ctx *ctx = file->private_data;
7412 __poll_t mask = 0;
7413
7414 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02007415 /*
7416 * synchronizes with barrier from wq_has_sleeper call in
7417 * io_commit_cqring
7418 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007419 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00007420 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
7421 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007422 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01007423 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007424 mask |= EPOLLIN | EPOLLRDNORM;
7425
7426 return mask;
7427}
7428
7429static int io_uring_fasync(int fd, struct file *file, int on)
7430{
7431 struct io_ring_ctx *ctx = file->private_data;
7432
7433 return fasync_helper(fd, file, on, &ctx->cq_fasync);
7434}
7435
Jens Axboe071698e2020-01-28 10:04:42 -07007436static int io_remove_personalities(int id, void *p, void *data)
7437{
7438 struct io_ring_ctx *ctx = data;
7439 const struct cred *cred;
7440
7441 cred = idr_remove(&ctx->personality_idr, id);
7442 if (cred)
7443 put_cred(cred);
7444 return 0;
7445}
7446
Jens Axboe85faa7b2020-04-09 18:14:00 -06007447static void io_ring_exit_work(struct work_struct *work)
7448{
7449 struct io_ring_ctx *ctx;
7450
7451 ctx = container_of(work, struct io_ring_ctx, exit_work);
7452 if (ctx->rings)
7453 io_cqring_overflow_flush(ctx, true);
7454
Jens Axboe0f158b42020-05-14 17:18:39 -06007455 wait_for_completion(&ctx->ref_comp);
Jens Axboe85faa7b2020-04-09 18:14:00 -06007456 io_ring_ctx_free(ctx);
7457}
7458
Jens Axboe2b188cc2019-01-07 10:46:33 -07007459static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
7460{
7461 mutex_lock(&ctx->uring_lock);
7462 percpu_ref_kill(&ctx->refs);
7463 mutex_unlock(&ctx->uring_lock);
7464
Jens Axboe5262f562019-09-17 12:26:57 -06007465 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07007466 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06007467
7468 if (ctx->io_wq)
7469 io_wq_cancel_all(ctx->io_wq);
7470
Jens Axboedef596e2019-01-09 08:59:42 -07007471 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07007472 /* if we failed setting up the ctx, we might not have any rings */
7473 if (ctx->rings)
7474 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07007475 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe85faa7b2020-04-09 18:14:00 -06007476 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
7477 queue_work(system_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007478}
7479
7480static int io_uring_release(struct inode *inode, struct file *file)
7481{
7482 struct io_ring_ctx *ctx = file->private_data;
7483
7484 file->private_data = NULL;
7485 io_ring_ctx_wait_and_kill(ctx);
7486 return 0;
7487}
7488
Jens Axboefcb323c2019-10-24 12:39:47 -06007489static void io_uring_cancel_files(struct io_ring_ctx *ctx,
7490 struct files_struct *files)
7491{
Jens Axboefcb323c2019-10-24 12:39:47 -06007492 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007493 struct io_kiocb *cancel_req = NULL, *req;
7494 DEFINE_WAIT(wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007495
7496 spin_lock_irq(&ctx->inflight_lock);
7497 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07007498 if (req->work.files != files)
7499 continue;
7500 /* req is being completed, ignore */
7501 if (!refcount_inc_not_zero(&req->refs))
7502 continue;
7503 cancel_req = req;
7504 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06007505 }
Jens Axboe768134d2019-11-10 20:30:53 -07007506 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007507 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07007508 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06007509 spin_unlock_irq(&ctx->inflight_lock);
7510
Jens Axboe768134d2019-11-10 20:30:53 -07007511 /* We need to keep going until we don't find a matching req */
7512 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007513 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08007514
Jens Axboe2ca10252020-02-13 17:17:35 -07007515 if (cancel_req->flags & REQ_F_OVERFLOW) {
7516 spin_lock_irq(&ctx->completion_lock);
7517 list_del(&cancel_req->list);
7518 cancel_req->flags &= ~REQ_F_OVERFLOW;
7519 if (list_empty(&ctx->cq_overflow_list)) {
7520 clear_bit(0, &ctx->sq_check_overflow);
7521 clear_bit(0, &ctx->cq_check_overflow);
7522 }
7523 spin_unlock_irq(&ctx->completion_lock);
7524
7525 WRITE_ONCE(ctx->rings->cq_overflow,
7526 atomic_inc_return(&ctx->cached_cq_overflow));
7527
7528 /*
7529 * Put inflight ref and overflow ref. If that's
7530 * all we had, then we're done with this request.
7531 */
7532 if (refcount_sub_and_test(2, &cancel_req->refs)) {
Pavel Begunkov4518a3c2020-05-26 20:34:02 +03007533 io_free_req(cancel_req);
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007534 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboe2ca10252020-02-13 17:17:35 -07007535 continue;
7536 }
7537 }
7538
Bob Liu2f6d9b92019-11-13 18:06:24 +08007539 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
7540 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06007541 schedule();
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007542 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007543 }
7544}
7545
7546static int io_uring_flush(struct file *file, void *data)
7547{
7548 struct io_ring_ctx *ctx = file->private_data;
7549
7550 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07007551
7552 /*
7553 * If the task is going away, cancel work it may have pending
7554 */
7555 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
7556 io_wq_cancel_pid(ctx->io_wq, task_pid_vnr(current));
7557
Jens Axboefcb323c2019-10-24 12:39:47 -06007558 return 0;
7559}
7560
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007561static void *io_uring_validate_mmap_request(struct file *file,
7562 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007563{
Jens Axboe2b188cc2019-01-07 10:46:33 -07007564 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007565 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007566 struct page *page;
7567 void *ptr;
7568
7569 switch (offset) {
7570 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00007571 case IORING_OFF_CQ_RING:
7572 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007573 break;
7574 case IORING_OFF_SQES:
7575 ptr = ctx->sq_sqes;
7576 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007577 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007578 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007579 }
7580
7581 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07007582 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007583 return ERR_PTR(-EINVAL);
7584
7585 return ptr;
7586}
7587
7588#ifdef CONFIG_MMU
7589
7590static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7591{
7592 size_t sz = vma->vm_end - vma->vm_start;
7593 unsigned long pfn;
7594 void *ptr;
7595
7596 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
7597 if (IS_ERR(ptr))
7598 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007599
7600 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
7601 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
7602}
7603
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007604#else /* !CONFIG_MMU */
7605
7606static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7607{
7608 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
7609}
7610
7611static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
7612{
7613 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
7614}
7615
7616static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
7617 unsigned long addr, unsigned long len,
7618 unsigned long pgoff, unsigned long flags)
7619{
7620 void *ptr;
7621
7622 ptr = io_uring_validate_mmap_request(file, pgoff, len);
7623 if (IS_ERR(ptr))
7624 return PTR_ERR(ptr);
7625
7626 return (unsigned long) ptr;
7627}
7628
7629#endif /* !CONFIG_MMU */
7630
Jens Axboe2b188cc2019-01-07 10:46:33 -07007631SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
7632 u32, min_complete, u32, flags, const sigset_t __user *, sig,
7633 size_t, sigsz)
7634{
7635 struct io_ring_ctx *ctx;
7636 long ret = -EBADF;
7637 int submitted = 0;
7638 struct fd f;
7639
Jens Axboeb41e9852020-02-17 09:52:41 -07007640 if (current->task_works)
7641 task_work_run();
7642
Jens Axboe6c271ce2019-01-10 11:22:30 -07007643 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007644 return -EINVAL;
7645
7646 f = fdget(fd);
7647 if (!f.file)
7648 return -EBADF;
7649
7650 ret = -EOPNOTSUPP;
7651 if (f.file->f_op != &io_uring_fops)
7652 goto out_fput;
7653
7654 ret = -ENXIO;
7655 ctx = f.file->private_data;
7656 if (!percpu_ref_tryget(&ctx->refs))
7657 goto out_fput;
7658
Jens Axboe6c271ce2019-01-10 11:22:30 -07007659 /*
7660 * For SQ polling, the thread will do all submissions and completions.
7661 * Just return the requested submit count, and wake the thread if
7662 * we were asked to.
7663 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007664 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007665 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07007666 if (!list_empty_careful(&ctx->cq_overflow_list))
7667 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007668 if (flags & IORING_ENTER_SQ_WAKEUP)
7669 wake_up(&ctx->sqo_wait);
7670 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007671 } else if (to_submit) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07007672 mutex_lock(&ctx->uring_lock);
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03007673 submitted = io_submit_sqes(ctx, to_submit, f.file, fd);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007674 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007675
7676 if (submitted != to_submit)
7677 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007678 }
7679 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07007680 unsigned nr_events = 0;
7681
Jens Axboe2b188cc2019-01-07 10:46:33 -07007682 min_complete = min(min_complete, ctx->cq_entries);
7683
Xiaoguang Wang32b22442020-03-11 09:26:09 +08007684 /*
7685 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
7686 * space applications don't need to do io completion events
7687 * polling again, they can rely on io_sq_thread to do polling
7688 * work, which can reduce cpu usage and uring_lock contention.
7689 */
7690 if (ctx->flags & IORING_SETUP_IOPOLL &&
7691 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Jens Axboedef596e2019-01-09 08:59:42 -07007692 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07007693 } else {
7694 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
7695 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007696 }
7697
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007698out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03007699 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007700out_fput:
7701 fdput(f);
7702 return submitted ? submitted : ret;
7703}
7704
Tobias Klauserbebdb652020-02-26 18:38:32 +01007705#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07007706static int io_uring_show_cred(int id, void *p, void *data)
7707{
7708 const struct cred *cred = p;
7709 struct seq_file *m = data;
7710 struct user_namespace *uns = seq_user_ns(m);
7711 struct group_info *gi;
7712 kernel_cap_t cap;
7713 unsigned __capi;
7714 int g;
7715
7716 seq_printf(m, "%5d\n", id);
7717 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
7718 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
7719 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
7720 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
7721 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
7722 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
7723 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
7724 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
7725 seq_puts(m, "\n\tGroups:\t");
7726 gi = cred->group_info;
7727 for (g = 0; g < gi->ngroups; g++) {
7728 seq_put_decimal_ull(m, g ? " " : "",
7729 from_kgid_munged(uns, gi->gid[g]));
7730 }
7731 seq_puts(m, "\n\tCapEff:\t");
7732 cap = cred->cap_effective;
7733 CAP_FOR_EACH_U32(__capi)
7734 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
7735 seq_putc(m, '\n');
7736 return 0;
7737}
7738
7739static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
7740{
7741 int i;
7742
7743 mutex_lock(&ctx->uring_lock);
7744 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
7745 for (i = 0; i < ctx->nr_user_files; i++) {
7746 struct fixed_file_table *table;
7747 struct file *f;
7748
7749 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7750 f = table->files[i & IORING_FILE_TABLE_MASK];
7751 if (f)
7752 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
7753 else
7754 seq_printf(m, "%5u: <none>\n", i);
7755 }
7756 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
7757 for (i = 0; i < ctx->nr_user_bufs; i++) {
7758 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
7759
7760 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
7761 (unsigned int) buf->len);
7762 }
7763 if (!idr_is_empty(&ctx->personality_idr)) {
7764 seq_printf(m, "Personalities:\n");
7765 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
7766 }
Jens Axboed7718a92020-02-14 22:23:12 -07007767 seq_printf(m, "PollList:\n");
7768 spin_lock_irq(&ctx->completion_lock);
7769 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
7770 struct hlist_head *list = &ctx->cancel_hash[i];
7771 struct io_kiocb *req;
7772
7773 hlist_for_each_entry(req, list, hash_node)
7774 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
7775 req->task->task_works != NULL);
7776 }
7777 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07007778 mutex_unlock(&ctx->uring_lock);
7779}
7780
7781static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
7782{
7783 struct io_ring_ctx *ctx = f->private_data;
7784
7785 if (percpu_ref_tryget(&ctx->refs)) {
7786 __io_uring_show_fdinfo(ctx, m);
7787 percpu_ref_put(&ctx->refs);
7788 }
7789}
Tobias Klauserbebdb652020-02-26 18:38:32 +01007790#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07007791
Jens Axboe2b188cc2019-01-07 10:46:33 -07007792static const struct file_operations io_uring_fops = {
7793 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06007794 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07007795 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007796#ifndef CONFIG_MMU
7797 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
7798 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
7799#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07007800 .poll = io_uring_poll,
7801 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01007802#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07007803 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01007804#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07007805};
7806
7807static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
7808 struct io_uring_params *p)
7809{
Hristo Venev75b28af2019-08-26 17:23:46 +00007810 struct io_rings *rings;
7811 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007812
Hristo Venev75b28af2019-08-26 17:23:46 +00007813 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
7814 if (size == SIZE_MAX)
7815 return -EOVERFLOW;
7816
7817 rings = io_mem_alloc(size);
7818 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007819 return -ENOMEM;
7820
Hristo Venev75b28af2019-08-26 17:23:46 +00007821 ctx->rings = rings;
7822 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
7823 rings->sq_ring_mask = p->sq_entries - 1;
7824 rings->cq_ring_mask = p->cq_entries - 1;
7825 rings->sq_ring_entries = p->sq_entries;
7826 rings->cq_ring_entries = p->cq_entries;
7827 ctx->sq_mask = rings->sq_ring_mask;
7828 ctx->cq_mask = rings->cq_ring_mask;
7829 ctx->sq_entries = rings->sq_ring_entries;
7830 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007831
7832 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07007833 if (size == SIZE_MAX) {
7834 io_mem_free(ctx->rings);
7835 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007836 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07007837 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007838
7839 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07007840 if (!ctx->sq_sqes) {
7841 io_mem_free(ctx->rings);
7842 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007843 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07007844 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007845
Jens Axboe2b188cc2019-01-07 10:46:33 -07007846 return 0;
7847}
7848
7849/*
7850 * Allocate an anonymous fd, this is what constitutes the application
7851 * visible backing of an io_uring instance. The application mmaps this
7852 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
7853 * we have to tie this fd to a socket for file garbage collection purposes.
7854 */
7855static int io_uring_get_fd(struct io_ring_ctx *ctx)
7856{
7857 struct file *file;
7858 int ret;
7859
7860#if defined(CONFIG_UNIX)
7861 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
7862 &ctx->ring_sock);
7863 if (ret)
7864 return ret;
7865#endif
7866
7867 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
7868 if (ret < 0)
7869 goto err;
7870
7871 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
7872 O_RDWR | O_CLOEXEC);
7873 if (IS_ERR(file)) {
7874 put_unused_fd(ret);
7875 ret = PTR_ERR(file);
7876 goto err;
7877 }
7878
7879#if defined(CONFIG_UNIX)
7880 ctx->ring_sock->file = file;
7881#endif
7882 fd_install(ret, file);
7883 return ret;
7884err:
7885#if defined(CONFIG_UNIX)
7886 sock_release(ctx->ring_sock);
7887 ctx->ring_sock = NULL;
7888#endif
7889 return ret;
7890}
7891
Xiaoguang Wang7f136572020-05-05 16:28:53 +08007892static int io_uring_create(unsigned entries, struct io_uring_params *p,
7893 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007894{
7895 struct user_struct *user = NULL;
7896 struct io_ring_ctx *ctx;
7897 bool account_mem;
7898 int ret;
7899
Jens Axboe8110c1a2019-12-28 15:39:54 -07007900 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007901 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07007902 if (entries > IORING_MAX_ENTRIES) {
7903 if (!(p->flags & IORING_SETUP_CLAMP))
7904 return -EINVAL;
7905 entries = IORING_MAX_ENTRIES;
7906 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007907
7908 /*
7909 * Use twice as many entries for the CQ ring. It's possible for the
7910 * application to drive a higher depth than the size of the SQ ring,
7911 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06007912 * some flexibility in overcommitting a bit. If the application has
7913 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
7914 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07007915 */
7916 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06007917 if (p->flags & IORING_SETUP_CQSIZE) {
7918 /*
7919 * If IORING_SETUP_CQSIZE is set, we do the same roundup
7920 * to a power-of-two, if it isn't already. We do NOT impose
7921 * any cq vs sq ring sizing.
7922 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07007923 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06007924 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07007925 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
7926 if (!(p->flags & IORING_SETUP_CLAMP))
7927 return -EINVAL;
7928 p->cq_entries = IORING_MAX_CQ_ENTRIES;
7929 }
Jens Axboe33a107f2019-10-04 12:10:03 -06007930 p->cq_entries = roundup_pow_of_two(p->cq_entries);
7931 } else {
7932 p->cq_entries = 2 * p->sq_entries;
7933 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007934
7935 user = get_uid(current_user());
7936 account_mem = !capable(CAP_IPC_LOCK);
7937
7938 if (account_mem) {
7939 ret = io_account_mem(user,
7940 ring_pages(p->sq_entries, p->cq_entries));
7941 if (ret) {
7942 free_uid(user);
7943 return ret;
7944 }
7945 }
7946
7947 ctx = io_ring_ctx_alloc(p);
7948 if (!ctx) {
7949 if (account_mem)
7950 io_unaccount_mem(user, ring_pages(p->sq_entries,
7951 p->cq_entries));
7952 free_uid(user);
7953 return -ENOMEM;
7954 }
7955 ctx->compat = in_compat_syscall();
7956 ctx->account_mem = account_mem;
7957 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07007958 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07007959
7960 ret = io_allocate_scq_urings(ctx, p);
7961 if (ret)
7962 goto err;
7963
Jens Axboe6c271ce2019-01-10 11:22:30 -07007964 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007965 if (ret)
7966 goto err;
7967
Jens Axboe2b188cc2019-01-07 10:46:33 -07007968 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007969 p->sq_off.head = offsetof(struct io_rings, sq.head);
7970 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
7971 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
7972 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
7973 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
7974 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
7975 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007976
7977 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007978 p->cq_off.head = offsetof(struct io_rings, cq.head);
7979 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
7980 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
7981 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
7982 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
7983 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02007984 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06007985
Xiaoguang Wang7f136572020-05-05 16:28:53 +08007986 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
7987 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
7988 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL;
7989
7990 if (copy_to_user(params, p, sizeof(*p))) {
7991 ret = -EFAULT;
7992 goto err;
7993 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06007994 /*
7995 * Install ring fd as the very last thing, so we don't risk someone
7996 * having closed it before we finish setup
7997 */
7998 ret = io_uring_get_fd(ctx);
7999 if (ret < 0)
8000 goto err;
8001
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02008002 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008003 return ret;
8004err:
8005 io_ring_ctx_wait_and_kill(ctx);
8006 return ret;
8007}
8008
8009/*
8010 * Sets up an aio uring context, and returns the fd. Applications asks for a
8011 * ring size, we return the actual sq/cq ring sizes (among other things) in the
8012 * params structure passed in.
8013 */
8014static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
8015{
8016 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008017 int i;
8018
8019 if (copy_from_user(&p, params, sizeof(p)))
8020 return -EFAULT;
8021 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
8022 if (p.resv[i])
8023 return -EINVAL;
8024 }
8025
Jens Axboe6c271ce2019-01-10 11:22:30 -07008026 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07008027 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03008028 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008029 return -EINVAL;
8030
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008031 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008032}
8033
8034SYSCALL_DEFINE2(io_uring_setup, u32, entries,
8035 struct io_uring_params __user *, params)
8036{
8037 return io_uring_setup(entries, params);
8038}
8039
Jens Axboe66f4af92020-01-16 15:36:52 -07008040static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
8041{
8042 struct io_uring_probe *p;
8043 size_t size;
8044 int i, ret;
8045
8046 size = struct_size(p, ops, nr_args);
8047 if (size == SIZE_MAX)
8048 return -EOVERFLOW;
8049 p = kzalloc(size, GFP_KERNEL);
8050 if (!p)
8051 return -ENOMEM;
8052
8053 ret = -EFAULT;
8054 if (copy_from_user(p, arg, size))
8055 goto out;
8056 ret = -EINVAL;
8057 if (memchr_inv(p, 0, size))
8058 goto out;
8059
8060 p->last_op = IORING_OP_LAST - 1;
8061 if (nr_args > IORING_OP_LAST)
8062 nr_args = IORING_OP_LAST;
8063
8064 for (i = 0; i < nr_args; i++) {
8065 p->ops[i].op = i;
8066 if (!io_op_defs[i].not_supported)
8067 p->ops[i].flags = IO_URING_OP_SUPPORTED;
8068 }
8069 p->ops_len = i;
8070
8071 ret = 0;
8072 if (copy_to_user(arg, p, size))
8073 ret = -EFAULT;
8074out:
8075 kfree(p);
8076 return ret;
8077}
8078
Jens Axboe071698e2020-01-28 10:04:42 -07008079static int io_register_personality(struct io_ring_ctx *ctx)
8080{
8081 const struct cred *creds = get_current_cred();
8082 int id;
8083
8084 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
8085 USHRT_MAX, GFP_KERNEL);
8086 if (id < 0)
8087 put_cred(creds);
8088 return id;
8089}
8090
8091static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
8092{
8093 const struct cred *old_creds;
8094
8095 old_creds = idr_remove(&ctx->personality_idr, id);
8096 if (old_creds) {
8097 put_cred(old_creds);
8098 return 0;
8099 }
8100
8101 return -EINVAL;
8102}
8103
8104static bool io_register_op_must_quiesce(int op)
8105{
8106 switch (op) {
8107 case IORING_UNREGISTER_FILES:
8108 case IORING_REGISTER_FILES_UPDATE:
8109 case IORING_REGISTER_PROBE:
8110 case IORING_REGISTER_PERSONALITY:
8111 case IORING_UNREGISTER_PERSONALITY:
8112 return false;
8113 default:
8114 return true;
8115 }
8116}
8117
Jens Axboeedafcce2019-01-09 09:16:05 -07008118static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
8119 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06008120 __releases(ctx->uring_lock)
8121 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07008122{
8123 int ret;
8124
Jens Axboe35fa71a2019-04-22 10:23:23 -06008125 /*
8126 * We're inside the ring mutex, if the ref is already dying, then
8127 * someone else killed the ctx or is already going through
8128 * io_uring_register().
8129 */
8130 if (percpu_ref_is_dying(&ctx->refs))
8131 return -ENXIO;
8132
Jens Axboe071698e2020-01-28 10:04:42 -07008133 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008134 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06008135
Jens Axboe05f3fb32019-12-09 11:22:50 -07008136 /*
8137 * Drop uring mutex before waiting for references to exit. If
8138 * another thread is currently inside io_uring_enter() it might
8139 * need to grab the uring_lock to make progress. If we hold it
8140 * here across the drain wait, then we can deadlock. It's safe
8141 * to drop the mutex here, since no new references will come in
8142 * after we've killed the percpu ref.
8143 */
8144 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f158b42020-05-14 17:18:39 -06008145 ret = wait_for_completion_interruptible(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008146 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07008147 if (ret) {
8148 percpu_ref_resurrect(&ctx->refs);
8149 ret = -EINTR;
8150 goto out;
8151 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008152 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008153
8154 switch (opcode) {
8155 case IORING_REGISTER_BUFFERS:
8156 ret = io_sqe_buffer_register(ctx, arg, nr_args);
8157 break;
8158 case IORING_UNREGISTER_BUFFERS:
8159 ret = -EINVAL;
8160 if (arg || nr_args)
8161 break;
8162 ret = io_sqe_buffer_unregister(ctx);
8163 break;
Jens Axboe6b063142019-01-10 22:13:58 -07008164 case IORING_REGISTER_FILES:
8165 ret = io_sqe_files_register(ctx, arg, nr_args);
8166 break;
8167 case IORING_UNREGISTER_FILES:
8168 ret = -EINVAL;
8169 if (arg || nr_args)
8170 break;
8171 ret = io_sqe_files_unregister(ctx);
8172 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06008173 case IORING_REGISTER_FILES_UPDATE:
8174 ret = io_sqe_files_update(ctx, arg, nr_args);
8175 break;
Jens Axboe9b402842019-04-11 11:45:41 -06008176 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07008177 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06008178 ret = -EINVAL;
8179 if (nr_args != 1)
8180 break;
8181 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07008182 if (ret)
8183 break;
8184 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
8185 ctx->eventfd_async = 1;
8186 else
8187 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06008188 break;
8189 case IORING_UNREGISTER_EVENTFD:
8190 ret = -EINVAL;
8191 if (arg || nr_args)
8192 break;
8193 ret = io_eventfd_unregister(ctx);
8194 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07008195 case IORING_REGISTER_PROBE:
8196 ret = -EINVAL;
8197 if (!arg || nr_args > 256)
8198 break;
8199 ret = io_probe(ctx, arg, nr_args);
8200 break;
Jens Axboe071698e2020-01-28 10:04:42 -07008201 case IORING_REGISTER_PERSONALITY:
8202 ret = -EINVAL;
8203 if (arg || nr_args)
8204 break;
8205 ret = io_register_personality(ctx);
8206 break;
8207 case IORING_UNREGISTER_PERSONALITY:
8208 ret = -EINVAL;
8209 if (arg)
8210 break;
8211 ret = io_unregister_personality(ctx, nr_args);
8212 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008213 default:
8214 ret = -EINVAL;
8215 break;
8216 }
8217
Jens Axboe071698e2020-01-28 10:04:42 -07008218 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008219 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07008220 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07008221out:
Jens Axboe0f158b42020-05-14 17:18:39 -06008222 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008223 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008224 return ret;
8225}
8226
8227SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
8228 void __user *, arg, unsigned int, nr_args)
8229{
8230 struct io_ring_ctx *ctx;
8231 long ret = -EBADF;
8232 struct fd f;
8233
8234 f = fdget(fd);
8235 if (!f.file)
8236 return -EBADF;
8237
8238 ret = -EOPNOTSUPP;
8239 if (f.file->f_op != &io_uring_fops)
8240 goto out_fput;
8241
8242 ctx = f.file->private_data;
8243
8244 mutex_lock(&ctx->uring_lock);
8245 ret = __io_uring_register(ctx, opcode, arg, nr_args);
8246 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02008247 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
8248 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07008249out_fput:
8250 fdput(f);
8251 return ret;
8252}
8253
Jens Axboe2b188cc2019-01-07 10:46:33 -07008254static int __init io_uring_init(void)
8255{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008256#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
8257 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
8258 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
8259} while (0)
8260
8261#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
8262 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
8263 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
8264 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
8265 BUILD_BUG_SQE_ELEM(1, __u8, flags);
8266 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
8267 BUILD_BUG_SQE_ELEM(4, __s32, fd);
8268 BUILD_BUG_SQE_ELEM(8, __u64, off);
8269 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
8270 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008271 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008272 BUILD_BUG_SQE_ELEM(24, __u32, len);
8273 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
8274 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
8275 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
8276 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
8277 BUILD_BUG_SQE_ELEM(28, __u16, poll_events);
8278 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
8279 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
8280 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
8281 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
8282 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
8283 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
8284 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
8285 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008286 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008287 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
8288 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
8289 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008290 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008291
Jens Axboed3656342019-12-18 09:50:26 -07008292 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07008293 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008294 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
8295 return 0;
8296};
8297__initcall(io_uring_init);