blob: 7b529270d0d2cf6bfe2456c351690d4e2e54e48b [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 Axboeeddc7ef2019-12-13 21:18:10 -0700428 union {
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700429 unsigned mask;
430 };
Jens Axboe15b71ab2019-12-11 11:20:36 -0700431 struct filename *filename;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700432 struct statx __user *buffer;
Jens Axboec12cedf2020-01-08 17:41:21 -0700433 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600434 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700435};
436
Jens Axboe05f3fb32019-12-09 11:22:50 -0700437struct io_files_update {
438 struct file *file;
439 u64 arg;
440 u32 nr_args;
441 u32 offset;
442};
443
Jens Axboe4840e412019-12-25 22:03:45 -0700444struct io_fadvise {
445 struct file *file;
446 u64 offset;
447 u32 len;
448 u32 advice;
449};
450
Jens Axboec1ca7572019-12-25 22:18:28 -0700451struct io_madvise {
452 struct file *file;
453 u64 addr;
454 u32 len;
455 u32 advice;
456};
457
Jens Axboe3e4827b2020-01-08 15:18:09 -0700458struct io_epoll {
459 struct file *file;
460 int epfd;
461 int op;
462 int fd;
463 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700464};
465
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300466struct io_splice {
467 struct file *file_out;
468 struct file *file_in;
469 loff_t off_out;
470 loff_t off_in;
471 u64 len;
472 unsigned int flags;
473};
474
Jens Axboeddf0322d2020-02-23 16:41:33 -0700475struct io_provide_buf {
476 struct file *file;
477 __u64 addr;
478 __s32 len;
479 __u32 bgid;
480 __u16 nbufs;
481 __u16 bid;
482};
483
Jens Axboef499a022019-12-02 16:28:46 -0700484struct io_async_connect {
485 struct sockaddr_storage address;
486};
487
Jens Axboe03b12302019-12-02 18:50:25 -0700488struct io_async_msghdr {
489 struct iovec fast_iov[UIO_FASTIOV];
490 struct iovec *iov;
491 struct sockaddr __user *uaddr;
492 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700493 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700494};
495
Jens Axboef67676d2019-12-02 11:03:47 -0700496struct io_async_rw {
497 struct iovec fast_iov[UIO_FASTIOV];
498 struct iovec *iov;
499 ssize_t nr_segs;
500 ssize_t size;
501};
502
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700503struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700504 union {
505 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700506 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700507 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700508 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700509 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700510};
511
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300512enum {
513 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
514 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
515 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
516 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
517 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700518 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300519
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300520 REQ_F_LINK_HEAD_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300521 REQ_F_LINK_NEXT_BIT,
522 REQ_F_FAIL_LINK_BIT,
523 REQ_F_INFLIGHT_BIT,
524 REQ_F_CUR_POS_BIT,
525 REQ_F_NOWAIT_BIT,
526 REQ_F_IOPOLL_COMPLETED_BIT,
527 REQ_F_LINK_TIMEOUT_BIT,
528 REQ_F_TIMEOUT_BIT,
529 REQ_F_ISREG_BIT,
530 REQ_F_MUST_PUNT_BIT,
531 REQ_F_TIMEOUT_NOSEQ_BIT,
532 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300533 REQ_F_NEED_CLEANUP_BIT,
Jens Axboe2ca10252020-02-13 17:17:35 -0700534 REQ_F_OVERFLOW_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700535 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700536 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600537 REQ_F_NO_FILE_TABLE_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700538
539 /* not a real bit, just to check we're not overflowing the space */
540 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300541};
542
543enum {
544 /* ctx owns file */
545 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
546 /* drain existing IO first */
547 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
548 /* linked sqes */
549 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
550 /* doesn't sever on completion < 0 */
551 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
552 /* IOSQE_ASYNC */
553 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700554 /* IOSQE_BUFFER_SELECT */
555 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300556
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300557 /* head of a link */
558 REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300559 /* already grabbed next link */
560 REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT),
561 /* fail rest of links */
562 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
563 /* on inflight list */
564 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
565 /* read/write uses file position */
566 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
567 /* must not punt to workers */
568 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
569 /* polled IO has completed */
570 REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT),
571 /* has linked timeout */
572 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
573 /* timeout request */
574 REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT),
575 /* regular file */
576 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
577 /* must be punted even for NONBLOCK */
578 REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT),
579 /* no timeout sequence */
580 REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT),
581 /* completion under lock */
582 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300583 /* needs cleanup */
584 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboe2ca10252020-02-13 17:17:35 -0700585 /* in overflow list */
586 REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700587 /* already went through poll handler */
588 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700589 /* buffer already selected */
590 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600591 /* doesn't need file table for this request */
592 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700593};
594
595struct async_poll {
596 struct io_poll_iocb poll;
597 struct io_wq_work work;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300598};
599
Jens Axboe09bb8392019-03-13 12:39:28 -0600600/*
601 * NOTE! Each of the iocb union members has the file pointer
602 * as the first entry in their struct definition. So you can
603 * access the file pointer through any of the sub-structs,
604 * or directly as just 'ki_filp' in this struct.
605 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700606struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700607 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600608 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700609 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700610 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700611 struct io_accept accept;
612 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700613 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700614 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700615 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700616 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700617 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700618 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700619 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700620 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700621 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700622 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300623 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700624 struct io_provide_buf pbuf;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700625 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700626
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700627 struct io_async_ctx *io;
Pavel Begunkovc398ecb2020-04-09 08:17:59 +0300628 int cflags;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300629 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700630 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700631
632 struct io_ring_ctx *ctx;
Jens Axboed7718a92020-02-14 22:23:12 -0700633 struct list_head list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700634 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700635 refcount_t refs;
Jens Axboe3537b6a2020-04-03 11:19:06 -0600636 struct task_struct *task;
637 unsigned long fsize;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700638 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600639 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600640 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700641
Jens Axboed7718a92020-02-14 22:23:12 -0700642 struct list_head link_list;
643
Jens Axboefcb323c2019-10-24 12:39:47 -0600644 struct list_head inflight_entry;
645
Xiaoguang Wang05589552020-03-31 14:05:18 +0800646 struct percpu_ref *fixed_file_refs;
647
Jens Axboeb41e9852020-02-17 09:52:41 -0700648 union {
649 /*
650 * Only commands that never go async can use the below fields,
Jens Axboed7718a92020-02-14 22:23:12 -0700651 * obviously. Right now only IORING_OP_POLL_ADD uses them, and
652 * async armed poll handlers for regular commands. The latter
653 * restore the work, if needed.
Jens Axboeb41e9852020-02-17 09:52:41 -0700654 */
655 struct {
Jens Axboeb41e9852020-02-17 09:52:41 -0700656 struct callback_head task_work;
Jens Axboed7718a92020-02-14 22:23:12 -0700657 struct hlist_node hash_node;
658 struct async_poll *apoll;
Jens Axboeb41e9852020-02-17 09:52:41 -0700659 };
660 struct io_wq_work work;
661 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700662};
663
664#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700665#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700666
Jens Axboe9a56a232019-01-09 09:06:50 -0700667struct io_submit_state {
668 struct blk_plug plug;
669
670 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700671 * io_kiocb alloc cache
672 */
673 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300674 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700675
676 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700677 * File reference cache
678 */
679 struct file *file;
680 unsigned int fd;
681 unsigned int has_refs;
682 unsigned int used_refs;
683 unsigned int ios_left;
684};
685
Jens Axboed3656342019-12-18 09:50:26 -0700686struct io_op_def {
687 /* needs req->io allocated for deferral/async */
688 unsigned async_ctx : 1;
689 /* needs current->mm setup, does mm access */
690 unsigned needs_mm : 1;
691 /* needs req->file assigned */
692 unsigned needs_file : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700693 /* hash wq insertion if file is a regular file */
694 unsigned hash_reg_file : 1;
695 /* unbound wq insertion if file is a non-regular file */
696 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700697 /* opcode is not supported by this kernel */
698 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700699 /* needs file table */
700 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700701 /* needs ->fs */
702 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700703 /* set if opcode supports polled "wait" */
704 unsigned pollin : 1;
705 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700706 /* op supports buffer selection */
707 unsigned buffer_select : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700708};
709
710static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300711 [IORING_OP_NOP] = {},
712 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700713 .async_ctx = 1,
714 .needs_mm = 1,
715 .needs_file = 1,
716 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700717 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700718 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700719 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300720 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700721 .async_ctx = 1,
722 .needs_mm = 1,
723 .needs_file = 1,
724 .hash_reg_file = 1,
725 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700726 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700727 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300728 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700729 .needs_file = 1,
730 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300731 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700732 .needs_file = 1,
733 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700734 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700735 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300736 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700737 .needs_file = 1,
738 .hash_reg_file = 1,
739 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700740 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700741 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300742 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700743 .needs_file = 1,
744 .unbound_nonreg_file = 1,
745 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300746 [IORING_OP_POLL_REMOVE] = {},
747 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700748 .needs_file = 1,
749 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300750 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700751 .async_ctx = 1,
752 .needs_mm = 1,
753 .needs_file = 1,
754 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700755 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700756 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700757 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300758 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700759 .async_ctx = 1,
760 .needs_mm = 1,
761 .needs_file = 1,
762 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700763 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700764 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700765 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700766 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300767 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700768 .async_ctx = 1,
769 .needs_mm = 1,
770 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300771 [IORING_OP_TIMEOUT_REMOVE] = {},
772 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700773 .needs_mm = 1,
774 .needs_file = 1,
775 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700776 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700777 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700778 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300779 [IORING_OP_ASYNC_CANCEL] = {},
780 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700781 .async_ctx = 1,
782 .needs_mm = 1,
783 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300784 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700785 .async_ctx = 1,
786 .needs_mm = 1,
787 .needs_file = 1,
788 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700789 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700790 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300791 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700792 .needs_file = 1,
793 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300794 [IORING_OP_OPENAT] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700795 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700796 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700797 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300798 [IORING_OP_CLOSE] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700799 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700800 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300801 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700802 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700803 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700804 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300805 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700806 .needs_mm = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700807 .needs_fs = 1,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600808 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700809 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300810 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700811 .needs_mm = 1,
812 .needs_file = 1,
813 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700814 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700815 .buffer_select = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700816 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300817 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700818 .needs_mm = 1,
819 .needs_file = 1,
820 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700821 .pollout = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700822 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300823 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700824 .needs_file = 1,
825 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300826 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700827 .needs_mm = 1,
828 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300829 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700830 .needs_mm = 1,
831 .needs_file = 1,
832 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700833 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700834 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300835 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700836 .needs_mm = 1,
837 .needs_file = 1,
838 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700839 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700840 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700841 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300842 [IORING_OP_OPENAT2] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700843 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700844 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700845 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700846 [IORING_OP_EPOLL_CTL] = {
847 .unbound_nonreg_file = 1,
848 .file_table = 1,
849 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300850 [IORING_OP_SPLICE] = {
851 .needs_file = 1,
852 .hash_reg_file = 1,
853 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700854 },
855 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700856 [IORING_OP_REMOVE_BUFFERS] = {},
Jens Axboed3656342019-12-18 09:50:26 -0700857};
858
Jens Axboe561fb042019-10-24 07:25:42 -0600859static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700860static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800861static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700862static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700863static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
864static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700865static int __io_sqe_files_update(struct io_ring_ctx *ctx,
866 struct io_uring_files_update *ip,
867 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700868static int io_grab_files(struct io_kiocb *req);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300869static void io_cleanup_req(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700870static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
871 int fd, struct file **out_file, bool fixed);
872static void __io_queue_sqe(struct io_kiocb *req,
873 const struct io_uring_sqe *sqe);
Jens Axboede0617e2019-04-06 21:51:27 -0600874
Jens Axboe2b188cc2019-01-07 10:46:33 -0700875static struct kmem_cache *req_cachep;
876
877static const struct file_operations io_uring_fops;
878
879struct sock *io_uring_get_socket(struct file *file)
880{
881#if defined(CONFIG_UNIX)
882 if (file->f_op == &io_uring_fops) {
883 struct io_ring_ctx *ctx = file->private_data;
884
885 return ctx->ring_sock->sk;
886 }
887#endif
888 return NULL;
889}
890EXPORT_SYMBOL(io_uring_get_socket);
891
Jens Axboe4a38aed22020-05-14 17:21:15 -0600892static void io_file_put_work(struct work_struct *work);
893
Jens Axboe2b188cc2019-01-07 10:46:33 -0700894static void io_ring_ctx_ref_free(struct percpu_ref *ref)
895{
896 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
897
Jens Axboe0f158b42020-05-14 17:18:39 -0600898 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700899}
900
901static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
902{
903 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700904 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700905
906 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
907 if (!ctx)
908 return NULL;
909
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700910 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
911 if (!ctx->fallback_req)
912 goto err;
913
Jens Axboe78076bb2019-12-04 19:56:40 -0700914 /*
915 * Use 5 bits less than the max cq entries, that should give us around
916 * 32 entries per hash list if totally full and uniformly spread.
917 */
918 hash_bits = ilog2(p->cq_entries);
919 hash_bits -= 5;
920 if (hash_bits <= 0)
921 hash_bits = 1;
922 ctx->cancel_hash_bits = hash_bits;
923 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
924 GFP_KERNEL);
925 if (!ctx->cancel_hash)
926 goto err;
927 __hash_init(ctx->cancel_hash, 1U << hash_bits);
928
Roman Gushchin21482892019-05-07 10:01:48 -0700929 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700930 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
931 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700932
933 ctx->flags = p->flags;
934 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700935 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -0600936 init_completion(&ctx->ref_comp);
937 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -0700938 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -0700939 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700940 mutex_init(&ctx->uring_lock);
941 init_waitqueue_head(&ctx->wait);
942 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700943 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600944 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600945 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600946 init_waitqueue_head(&ctx->inflight_wait);
947 spin_lock_init(&ctx->inflight_lock);
948 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe4a38aed22020-05-14 17:21:15 -0600949 INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work);
950 init_llist_head(&ctx->file_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700951 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700952err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700953 if (ctx->fallback_req)
954 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -0700955 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700956 kfree(ctx);
957 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700958}
959
Bob Liu9d858b22019-11-13 18:06:25 +0800960static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600961{
Jackie Liua197f662019-11-08 08:09:12 -0700962 struct io_ring_ctx *ctx = req->ctx;
963
Pavel Begunkov31af27c2020-04-15 00:39:50 +0300964 return req->sequence != ctx->cached_cq_tail
965 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600966}
967
Bob Liu9d858b22019-11-13 18:06:25 +0800968static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600969{
Pavel Begunkov87987892020-01-18 01:22:30 +0300970 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +0800971 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600972
Bob Liu9d858b22019-11-13 18:06:25 +0800973 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600974}
975
976static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600977{
978 struct io_kiocb *req;
979
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600980 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800981 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600982 list_del_init(&req->list);
983 return req;
984 }
985
986 return NULL;
987}
988
Jens Axboe5262f562019-09-17 12:26:57 -0600989static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
990{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600991 struct io_kiocb *req;
992
993 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700994 if (req) {
995 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
996 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800997 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700998 list_del_init(&req->list);
999 return req;
1000 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001001 }
1002
1003 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -06001004}
1005
Jens Axboede0617e2019-04-06 21:51:27 -06001006static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001007{
Hristo Venev75b28af2019-08-26 17:23:46 +00001008 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001009
Pavel Begunkov07910152020-01-17 03:52:46 +03001010 /* order cqe stores with ring update */
1011 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001012
Pavel Begunkov07910152020-01-17 03:52:46 +03001013 if (wq_has_sleeper(&ctx->cq_wait)) {
1014 wake_up_interruptible(&ctx->cq_wait);
1015 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001016 }
1017}
1018
Jens Axboecccf0ee2020-01-27 16:34:48 -07001019static inline void io_req_work_grab_env(struct io_kiocb *req,
1020 const struct io_op_def *def)
Jens Axboe18d9be12019-09-10 09:13:05 -06001021{
Jens Axboecccf0ee2020-01-27 16:34:48 -07001022 if (!req->work.mm && def->needs_mm) {
1023 mmgrab(current->mm);
1024 req->work.mm = current->mm;
1025 }
1026 if (!req->work.creds)
1027 req->work.creds = get_current_cred();
Jens Axboeff002b32020-02-07 16:05:21 -07001028 if (!req->work.fs && def->needs_fs) {
1029 spin_lock(&current->fs->lock);
1030 if (!current->fs->in_exec) {
1031 req->work.fs = current->fs;
1032 req->work.fs->users++;
1033 } else {
1034 req->work.flags |= IO_WQ_WORK_CANCEL;
1035 }
1036 spin_unlock(&current->fs->lock);
1037 }
Jens Axboe6ab23142020-02-08 20:23:59 -07001038 if (!req->work.task_pid)
1039 req->work.task_pid = task_pid_vnr(current);
Jens Axboecccf0ee2020-01-27 16:34:48 -07001040}
1041
1042static inline void io_req_work_drop_env(struct io_kiocb *req)
1043{
1044 if (req->work.mm) {
1045 mmdrop(req->work.mm);
1046 req->work.mm = NULL;
1047 }
1048 if (req->work.creds) {
1049 put_cred(req->work.creds);
1050 req->work.creds = NULL;
1051 }
Jens Axboeff002b32020-02-07 16:05:21 -07001052 if (req->work.fs) {
1053 struct fs_struct *fs = req->work.fs;
1054
1055 spin_lock(&req->work.fs->lock);
1056 if (--fs->users)
1057 fs = NULL;
1058 spin_unlock(&req->work.fs->lock);
1059 if (fs)
1060 free_fs_struct(fs);
1061 }
Jens Axboe561fb042019-10-24 07:25:42 -06001062}
1063
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001064static inline void io_prep_async_work(struct io_kiocb *req,
Jens Axboe94ae5e72019-11-14 19:39:52 -07001065 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -06001066{
Jens Axboed3656342019-12-18 09:50:26 -07001067 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe54a91f32019-09-10 09:15:04 -06001068
Jens Axboed3656342019-12-18 09:50:26 -07001069 if (req->flags & REQ_F_ISREG) {
1070 if (def->hash_reg_file)
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001071 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001072 } else {
1073 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001074 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001075 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001076
1077 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -06001078
Jens Axboe94ae5e72019-11-14 19:39:52 -07001079 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001080}
1081
Jackie Liua197f662019-11-08 08:09:12 -07001082static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001083{
Jackie Liua197f662019-11-08 08:09:12 -07001084 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001085 struct io_kiocb *link;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001086
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001087 io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06001088
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001089 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1090 &req->work, req->flags);
1091 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001092
1093 if (link)
1094 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -06001095}
1096
Jens Axboe5262f562019-09-17 12:26:57 -06001097static void io_kill_timeout(struct io_kiocb *req)
1098{
1099 int ret;
1100
Jens Axboe2d283902019-12-04 11:08:05 -07001101 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001102 if (ret != -1) {
1103 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -06001104 list_del_init(&req->list);
Pavel Begunkovf0e20b82020-03-07 01:15:22 +03001105 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001106 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001107 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001108 }
1109}
1110
1111static void io_kill_timeouts(struct io_ring_ctx *ctx)
1112{
1113 struct io_kiocb *req, *tmp;
1114
1115 spin_lock_irq(&ctx->completion_lock);
1116 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
1117 io_kill_timeout(req);
1118 spin_unlock_irq(&ctx->completion_lock);
1119}
1120
Jens Axboede0617e2019-04-06 21:51:27 -06001121static void io_commit_cqring(struct io_ring_ctx *ctx)
1122{
1123 struct io_kiocb *req;
1124
Jens Axboe5262f562019-09-17 12:26:57 -06001125 while ((req = io_get_timeout_req(ctx)) != NULL)
1126 io_kill_timeout(req);
1127
Jens Axboede0617e2019-04-06 21:51:27 -06001128 __io_commit_cqring(ctx);
1129
Pavel Begunkov87987892020-01-18 01:22:30 +03001130 while ((req = io_get_deferred_req(ctx)) != NULL)
Jackie Liua197f662019-11-08 08:09:12 -07001131 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -06001132}
1133
Jens Axboe2b188cc2019-01-07 10:46:33 -07001134static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1135{
Hristo Venev75b28af2019-08-26 17:23:46 +00001136 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001137 unsigned tail;
1138
1139 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001140 /*
1141 * writes to the cq entry need to come after reading head; the
1142 * control dependency is enough as we're using WRITE_ONCE to
1143 * fill the cq entry
1144 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001145 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001146 return NULL;
1147
1148 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001149 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001150}
1151
Jens Axboef2842ab2020-01-08 11:04:00 -07001152static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1153{
Jens Axboef0b493e2020-02-01 21:30:11 -07001154 if (!ctx->cq_ev_fd)
1155 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001156 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1157 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001158 if (!ctx->eventfd_async)
1159 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001160 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001161}
1162
Jens Axboeb41e9852020-02-17 09:52:41 -07001163static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001164{
1165 if (waitqueue_active(&ctx->wait))
1166 wake_up(&ctx->wait);
1167 if (waitqueue_active(&ctx->sqo_wait))
1168 wake_up(&ctx->sqo_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001169 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001170 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001171}
1172
Jens Axboec4a2ed72019-11-21 21:01:26 -07001173/* Returns true if there are no backlogged entries after the flush */
1174static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001175{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001176 struct io_rings *rings = ctx->rings;
1177 struct io_uring_cqe *cqe;
1178 struct io_kiocb *req;
1179 unsigned long flags;
1180 LIST_HEAD(list);
1181
1182 if (!force) {
1183 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001184 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001185 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1186 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001187 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001188 }
1189
1190 spin_lock_irqsave(&ctx->completion_lock, flags);
1191
1192 /* if force is set, the ring is going away. always drop after that */
1193 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001194 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001195
Jens Axboec4a2ed72019-11-21 21:01:26 -07001196 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001197 while (!list_empty(&ctx->cq_overflow_list)) {
1198 cqe = io_get_cqring(ctx);
1199 if (!cqe && !force)
1200 break;
1201
1202 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1203 list);
1204 list_move(&req->list, &list);
Jens Axboe2ca10252020-02-13 17:17:35 -07001205 req->flags &= ~REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001206 if (cqe) {
1207 WRITE_ONCE(cqe->user_data, req->user_data);
1208 WRITE_ONCE(cqe->res, req->result);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001209 WRITE_ONCE(cqe->flags, req->cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001210 } else {
1211 WRITE_ONCE(ctx->rings->cq_overflow,
1212 atomic_inc_return(&ctx->cached_cq_overflow));
1213 }
1214 }
1215
1216 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001217 if (cqe) {
1218 clear_bit(0, &ctx->sq_check_overflow);
1219 clear_bit(0, &ctx->cq_check_overflow);
1220 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001221 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1222 io_cqring_ev_posted(ctx);
1223
1224 while (!list_empty(&list)) {
1225 req = list_first_entry(&list, struct io_kiocb, list);
1226 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001227 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001228 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001229
1230 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001231}
1232
Jens Axboebcda7ba2020-02-23 16:42:51 -07001233static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001234{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001235 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001236 struct io_uring_cqe *cqe;
1237
Jens Axboe78e19bb2019-11-06 15:21:34 -07001238 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001239
Jens Axboe2b188cc2019-01-07 10:46:33 -07001240 /*
1241 * If we can't get a cq entry, userspace overflowed the
1242 * submission (by quite a lot). Increment the overflow count in
1243 * the ring.
1244 */
1245 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001246 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001247 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001248 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001249 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001250 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001251 WRITE_ONCE(ctx->rings->cq_overflow,
1252 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001253 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001254 if (list_empty(&ctx->cq_overflow_list)) {
1255 set_bit(0, &ctx->sq_check_overflow);
1256 set_bit(0, &ctx->cq_check_overflow);
1257 }
Jens Axboe2ca10252020-02-13 17:17:35 -07001258 req->flags |= REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001259 refcount_inc(&req->refs);
1260 req->result = res;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001261 req->cflags = cflags;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001262 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001263 }
1264}
1265
Jens Axboebcda7ba2020-02-23 16:42:51 -07001266static void io_cqring_fill_event(struct io_kiocb *req, long res)
1267{
1268 __io_cqring_fill_event(req, res, 0);
1269}
1270
1271static void __io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001272{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001273 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001274 unsigned long flags;
1275
1276 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001277 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001278 io_commit_cqring(ctx);
1279 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1280
Jens Axboe8c838782019-03-12 15:48:16 -06001281 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001282}
1283
Jens Axboebcda7ba2020-02-23 16:42:51 -07001284static void io_cqring_add_event(struct io_kiocb *req, long res)
1285{
1286 __io_cqring_add_event(req, res, 0);
1287}
1288
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001289static inline bool io_is_fallback_req(struct io_kiocb *req)
1290{
1291 return req == (struct io_kiocb *)
1292 ((unsigned long) req->ctx->fallback_req & ~1UL);
1293}
1294
1295static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1296{
1297 struct io_kiocb *req;
1298
1299 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001300 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001301 return req;
1302
1303 return NULL;
1304}
1305
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001306static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1307 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001308{
Jens Axboefd6fab22019-03-14 16:30:06 -06001309 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001310 struct io_kiocb *req;
1311
Jens Axboe2579f912019-01-09 09:10:43 -07001312 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001313 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001314 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001315 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001316 } else if (!state->free_reqs) {
1317 size_t sz;
1318 int ret;
1319
1320 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001321 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1322
1323 /*
1324 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1325 * retry single alloc to be on the safe side.
1326 */
1327 if (unlikely(ret <= 0)) {
1328 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1329 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001330 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001331 ret = 1;
1332 }
Jens Axboe2579f912019-01-09 09:10:43 -07001333 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001334 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001335 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001336 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001337 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001338 }
1339
Jens Axboe2579f912019-01-09 09:10:43 -07001340 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001341fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001342 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001343}
1344
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001345static inline void io_put_file(struct io_kiocb *req, struct file *file,
1346 bool fixed)
1347{
1348 if (fixed)
Xiaoguang Wang05589552020-03-31 14:05:18 +08001349 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001350 else
1351 fput(file);
1352}
1353
Jens Axboec6ca97b302019-12-28 12:11:08 -07001354static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001355{
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001356 if (req->flags & REQ_F_NEED_CLEANUP)
1357 io_cleanup_req(req);
1358
YueHaibing96fd84d2020-01-07 22:22:44 +08001359 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001360 if (req->file)
1361 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboe3537b6a2020-04-03 11:19:06 -06001362 if (req->task)
1363 put_task_struct(req->task);
Jens Axboecccf0ee2020-01-27 16:34:48 -07001364
1365 io_req_work_drop_env(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001366}
1367
1368static void __io_free_req(struct io_kiocb *req)
1369{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001370 __io_req_aux_free(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06001371
Jens Axboefcb323c2019-10-24 12:39:47 -06001372 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001373 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001374 unsigned long flags;
1375
1376 spin_lock_irqsave(&ctx->inflight_lock, flags);
1377 list_del(&req->inflight_entry);
1378 if (waitqueue_active(&ctx->inflight_wait))
1379 wake_up(&ctx->inflight_wait);
1380 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1381 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001382
1383 percpu_ref_put(&req->ctx->refs);
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03001384 if (likely(!io_is_fallback_req(req)))
1385 kmem_cache_free(req_cachep, req);
1386 else
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001387 clear_bit_unlock(0, (unsigned long *) &req->ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -06001388}
1389
Jens Axboec6ca97b302019-12-28 12:11:08 -07001390struct req_batch {
1391 void *reqs[IO_IOPOLL_BATCH];
1392 int to_free;
1393 int need_iter;
1394};
1395
1396static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1397{
1398 if (!rb->to_free)
1399 return;
1400 if (rb->need_iter) {
1401 int i, inflight = 0;
1402 unsigned long flags;
1403
1404 for (i = 0; i < rb->to_free; i++) {
1405 struct io_kiocb *req = rb->reqs[i];
1406
Jens Axboe10fef4b2020-01-09 07:52:28 -07001407 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001408 req->file = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08001409 percpu_ref_put(req->fixed_file_refs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001410 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001411 if (req->flags & REQ_F_INFLIGHT)
1412 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001413 __io_req_aux_free(req);
1414 }
1415 if (!inflight)
1416 goto do_free;
1417
1418 spin_lock_irqsave(&ctx->inflight_lock, flags);
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_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001423 list_del(&req->inflight_entry);
1424 if (!--inflight)
1425 break;
1426 }
1427 }
1428 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1429
1430 if (waitqueue_active(&ctx->inflight_wait))
1431 wake_up(&ctx->inflight_wait);
1432 }
1433do_free:
1434 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
1435 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001436 rb->to_free = rb->need_iter = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001437}
1438
Jackie Liua197f662019-11-08 08:09:12 -07001439static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001440{
Jackie Liua197f662019-11-08 08:09:12 -07001441 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001442 int ret;
1443
Jens Axboe2d283902019-12-04 11:08:05 -07001444 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001445 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001446 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001447 io_commit_cqring(ctx);
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001448 req->flags &= ~REQ_F_LINK_HEAD;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001449 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001450 return true;
1451 }
1452
1453 return false;
1454}
1455
Jens Axboeba816ad2019-09-28 11:36:45 -06001456static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001457{
Jens Axboe2665abf2019-11-05 12:40:47 -07001458 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001459 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001460
Jens Axboe4d7dd462019-11-20 13:03:52 -07001461 /* Already got next link */
1462 if (req->flags & REQ_F_LINK_NEXT)
1463 return;
1464
Jens Axboe9e645e112019-05-10 16:07:28 -06001465 /*
1466 * The list should never be empty when we are called here. But could
1467 * potentially happen if the chain is messed up, check to be on the
1468 * safe side.
1469 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001470 while (!list_empty(&req->link_list)) {
1471 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1472 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001473
Pavel Begunkov44932332019-12-05 16:16:35 +03001474 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1475 (nxt->flags & REQ_F_TIMEOUT))) {
1476 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001477 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001478 req->flags &= ~REQ_F_LINK_TIMEOUT;
1479 continue;
1480 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001481
Pavel Begunkov44932332019-12-05 16:16:35 +03001482 list_del_init(&req->link_list);
1483 if (!list_empty(&nxt->link_list))
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001484 nxt->flags |= REQ_F_LINK_HEAD;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001485 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001486 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001487 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001488
Jens Axboe4d7dd462019-11-20 13:03:52 -07001489 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001490 if (wake_ev)
1491 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001492}
1493
1494/*
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001495 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
Jens Axboe9e645e112019-05-10 16:07:28 -06001496 */
1497static void io_fail_links(struct io_kiocb *req)
1498{
Jens Axboe2665abf2019-11-05 12:40:47 -07001499 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001500 unsigned long flags;
1501
1502 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001503
1504 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001505 struct io_kiocb *link = list_first_entry(&req->link_list,
1506 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001507
Pavel Begunkov44932332019-12-05 16:16:35 +03001508 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001509 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001510
1511 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001512 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001513 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001514 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001515 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001516 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001517 }
Jens Axboe5d960722019-11-19 15:31:28 -07001518 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001519 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001520
1521 io_commit_cqring(ctx);
1522 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1523 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001524}
1525
Jens Axboe4d7dd462019-11-20 13:03:52 -07001526static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001527{
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001528 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001529 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001530
Jens Axboe9e645e112019-05-10 16:07:28 -06001531 /*
1532 * If LINK is set, we have dependent requests in this chain. If we
1533 * didn't fail this request, queue the first one up, moving any other
1534 * dependencies to the next request. In case of failure, fail the rest
1535 * of the chain.
1536 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001537 if (req->flags & REQ_F_FAIL_LINK) {
1538 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001539 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1540 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001541 struct io_ring_ctx *ctx = req->ctx;
1542 unsigned long flags;
1543
1544 /*
1545 * If this is a timeout link, we could be racing with the
1546 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001547 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001548 */
1549 spin_lock_irqsave(&ctx->completion_lock, flags);
1550 io_req_link_next(req, nxt);
1551 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1552 } else {
1553 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001554 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001555}
Jens Axboe9e645e112019-05-10 16:07:28 -06001556
Jackie Liuc69f8db2019-11-09 11:00:08 +08001557static void io_free_req(struct io_kiocb *req)
1558{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001559 struct io_kiocb *nxt = NULL;
1560
1561 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001562 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001563
1564 if (nxt)
1565 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001566}
1567
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001568static void io_link_work_cb(struct io_wq_work **workptr)
1569{
Pavel Begunkov18a542f2020-03-23 00:23:29 +03001570 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
1571 struct io_kiocb *link;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001572
Pavel Begunkov18a542f2020-03-23 00:23:29 +03001573 link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001574 io_queue_linked_timeout(link);
1575 io_wq_submit_work(workptr);
1576}
1577
1578static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
1579{
1580 struct io_kiocb *link;
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001581 const struct io_op_def *def = &io_op_defs[nxt->opcode];
1582
1583 if ((nxt->flags & REQ_F_ISREG) && def->hash_reg_file)
1584 io_wq_hash_work(&nxt->work, file_inode(nxt->file));
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001585
1586 *workptr = &nxt->work;
1587 link = io_prep_linked_timeout(nxt);
Pavel Begunkov18a542f2020-03-23 00:23:29 +03001588 if (link)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001589 nxt->work.func = io_link_work_cb;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001590}
1591
Jens Axboeba816ad2019-09-28 11:36:45 -06001592/*
1593 * Drop reference to request, return next in chain (if there is one) if this
1594 * was the last reference to this request.
1595 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001596__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001597static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001598{
Jens Axboe2a44f462020-02-25 13:25:41 -07001599 if (refcount_dec_and_test(&req->refs)) {
1600 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001601 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001602 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001603}
1604
Jens Axboe2b188cc2019-01-07 10:46:33 -07001605static void io_put_req(struct io_kiocb *req)
1606{
Jens Axboedef596e2019-01-09 08:59:42 -07001607 if (refcount_dec_and_test(&req->refs))
1608 io_free_req(req);
1609}
1610
Pavel Begunkove9fd9392020-03-04 16:14:12 +03001611static void io_steal_work(struct io_kiocb *req,
1612 struct io_wq_work **workptr)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001613{
1614 /*
1615 * It's in an io-wq worker, so there always should be at least
1616 * one reference, which will be dropped in io_put_work() just
1617 * after the current handler returns.
1618 *
1619 * It also means, that if the counter dropped to 1, then there is
1620 * no asynchronous users left, so it's safe to steal the next work.
1621 */
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001622 if (refcount_read(&req->refs) == 1) {
1623 struct io_kiocb *nxt = NULL;
1624
1625 io_req_find_next(req, &nxt);
1626 if (nxt)
1627 io_wq_assign_next(workptr, nxt);
1628 }
1629}
1630
Jens Axboe978db572019-11-14 22:39:04 -07001631/*
1632 * Must only be used if we don't need to care about links, usually from
1633 * within the completion handling itself.
1634 */
1635static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001636{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001637 /* drop both submit and complete references */
1638 if (refcount_sub_and_test(2, &req->refs))
1639 __io_free_req(req);
1640}
1641
Jens Axboe978db572019-11-14 22:39:04 -07001642static void io_double_put_req(struct io_kiocb *req)
1643{
1644 /* drop both submit and complete references */
1645 if (refcount_sub_and_test(2, &req->refs))
1646 io_free_req(req);
1647}
1648
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001649static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001650{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001651 struct io_rings *rings = ctx->rings;
1652
Jens Axboead3eb2c2019-12-18 17:12:20 -07001653 if (test_bit(0, &ctx->cq_check_overflow)) {
1654 /*
1655 * noflush == true is from the waitqueue handler, just ensure
1656 * we wake up the task, and the next invocation will flush the
1657 * entries. We cannot safely to it from here.
1658 */
1659 if (noflush && !list_empty(&ctx->cq_overflow_list))
1660 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001661
Jens Axboead3eb2c2019-12-18 17:12:20 -07001662 io_cqring_overflow_flush(ctx, false);
1663 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001664
Jens Axboea3a0e432019-08-20 11:03:11 -06001665 /* See comment at the top of this file */
1666 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001667 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001668}
1669
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001670static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1671{
1672 struct io_rings *rings = ctx->rings;
1673
1674 /* make sure SQ entry isn't read before tail */
1675 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1676}
1677
Jens Axboe8237e042019-12-28 10:48:22 -07001678static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001679{
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001680 if ((req->flags & REQ_F_LINK_HEAD) || io_is_fallback_req(req))
Jens Axboec6ca97b302019-12-28 12:11:08 -07001681 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001682
Jens Axboec6ca97b302019-12-28 12:11:08 -07001683 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1684 rb->need_iter++;
1685
1686 rb->reqs[rb->to_free++] = req;
1687 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1688 io_free_req_many(req->ctx, rb);
1689 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001690}
1691
Jens Axboebcda7ba2020-02-23 16:42:51 -07001692static int io_put_kbuf(struct io_kiocb *req)
1693{
Jens Axboe4d954c22020-02-27 07:31:19 -07001694 struct io_buffer *kbuf;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001695 int cflags;
1696
Jens Axboe4d954c22020-02-27 07:31:19 -07001697 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001698 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
1699 cflags |= IORING_CQE_F_BUFFER;
1700 req->rw.addr = 0;
1701 kfree(kbuf);
1702 return cflags;
1703}
1704
Jens Axboedef596e2019-01-09 08:59:42 -07001705/*
1706 * Find and free completed poll iocbs
1707 */
1708static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1709 struct list_head *done)
1710{
Jens Axboe8237e042019-12-28 10:48:22 -07001711 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001712 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001713
Jens Axboec6ca97b302019-12-28 12:11:08 -07001714 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001715 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07001716 int cflags = 0;
1717
Jens Axboedef596e2019-01-09 08:59:42 -07001718 req = list_first_entry(done, struct io_kiocb, list);
1719 list_del(&req->list);
1720
Jens Axboebcda7ba2020-02-23 16:42:51 -07001721 if (req->flags & REQ_F_BUFFER_SELECTED)
1722 cflags = io_put_kbuf(req);
1723
1724 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07001725 (*nr_events)++;
1726
Jens Axboe8237e042019-12-28 10:48:22 -07001727 if (refcount_dec_and_test(&req->refs) &&
1728 !io_req_multi_free(&rb, req))
1729 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001730 }
Jens Axboedef596e2019-01-09 08:59:42 -07001731
Jens Axboe09bb8392019-03-13 12:39:28 -06001732 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08001733 if (ctx->flags & IORING_SETUP_SQPOLL)
1734 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001735 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001736}
1737
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001738static void io_iopoll_queue(struct list_head *again)
1739{
1740 struct io_kiocb *req;
1741
1742 do {
1743 req = list_first_entry(again, struct io_kiocb, list);
1744 list_del(&req->list);
1745 refcount_inc(&req->refs);
1746 io_queue_async_work(req);
1747 } while (!list_empty(again));
1748}
1749
Jens Axboedef596e2019-01-09 08:59:42 -07001750static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1751 long min)
1752{
1753 struct io_kiocb *req, *tmp;
1754 LIST_HEAD(done);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001755 LIST_HEAD(again);
Jens Axboedef596e2019-01-09 08:59:42 -07001756 bool spin;
1757 int ret;
1758
1759 /*
1760 * Only spin for completions if we don't have multiple devices hanging
1761 * off our complete list, and we're under the requested amount.
1762 */
1763 spin = !ctx->poll_multi_file && *nr_events < min;
1764
1765 ret = 0;
1766 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001767 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001768
1769 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001770 * Move completed and retryable entries to our local lists.
1771 * If we find a request that requires polling, break out
1772 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07001773 */
1774 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1775 list_move_tail(&req->list, &done);
1776 continue;
1777 }
1778 if (!list_empty(&done))
1779 break;
1780
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001781 if (req->result == -EAGAIN) {
1782 list_move_tail(&req->list, &again);
1783 continue;
1784 }
1785 if (!list_empty(&again))
1786 break;
1787
Jens Axboedef596e2019-01-09 08:59:42 -07001788 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1789 if (ret < 0)
1790 break;
1791
1792 if (ret && spin)
1793 spin = false;
1794 ret = 0;
1795 }
1796
1797 if (!list_empty(&done))
1798 io_iopoll_complete(ctx, nr_events, &done);
1799
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001800 if (!list_empty(&again))
1801 io_iopoll_queue(&again);
1802
Jens Axboedef596e2019-01-09 08:59:42 -07001803 return ret;
1804}
1805
1806/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001807 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001808 * non-spinning poll check - we'll still enter the driver poll loop, but only
1809 * as a non-spinning completion check.
1810 */
1811static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1812 long min)
1813{
Jens Axboe08f54392019-08-21 22:19:11 -06001814 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001815 int ret;
1816
1817 ret = io_do_iopoll(ctx, nr_events, min);
1818 if (ret < 0)
1819 return ret;
1820 if (!min || *nr_events >= min)
1821 return 0;
1822 }
1823
1824 return 1;
1825}
1826
1827/*
1828 * We can't just wait for polled events to come to us, we have to actively
1829 * find and complete them.
1830 */
1831static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1832{
1833 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1834 return;
1835
1836 mutex_lock(&ctx->uring_lock);
1837 while (!list_empty(&ctx->poll_list)) {
1838 unsigned int nr_events = 0;
1839
1840 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001841
1842 /*
1843 * Ensure we allow local-to-the-cpu processing to take place,
1844 * in this case we need to ensure that we reap all events.
1845 */
1846 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001847 }
1848 mutex_unlock(&ctx->uring_lock);
1849}
1850
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001851static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1852 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001853{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001854 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001855
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001856 /*
1857 * We disallow the app entering submit/complete with polling, but we
1858 * still need to lock the ring to prevent racing with polled issue
1859 * that got punted to a workqueue.
1860 */
1861 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001862 do {
1863 int tmin = 0;
1864
Jens Axboe500f9fb2019-08-19 12:15:59 -06001865 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001866 * Don't enter poll loop if we already have events pending.
1867 * If we do, we can potentially be spinning for commands that
1868 * already triggered a CQE (eg in error).
1869 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001870 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001871 break;
1872
1873 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001874 * If a submit got punted to a workqueue, we can have the
1875 * application entering polling for a command before it gets
1876 * issued. That app will hold the uring_lock for the duration
1877 * of the poll right here, so we need to take a breather every
1878 * now and then to ensure that the issue has a chance to add
1879 * the poll to the issued list. Otherwise we can spin here
1880 * forever, while the workqueue is stuck trying to acquire the
1881 * very same mutex.
1882 */
1883 if (!(++iters & 7)) {
1884 mutex_unlock(&ctx->uring_lock);
1885 mutex_lock(&ctx->uring_lock);
1886 }
1887
Jens Axboedef596e2019-01-09 08:59:42 -07001888 if (*nr_events < min)
1889 tmin = min - *nr_events;
1890
1891 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1892 if (ret <= 0)
1893 break;
1894 ret = 0;
1895 } while (min && !*nr_events && !need_resched());
1896
Jens Axboe500f9fb2019-08-19 12:15:59 -06001897 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001898 return ret;
1899}
1900
Jens Axboe491381ce2019-10-17 09:20:46 -06001901static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001902{
Jens Axboe491381ce2019-10-17 09:20:46 -06001903 /*
1904 * Tell lockdep we inherited freeze protection from submission
1905 * thread.
1906 */
1907 if (req->flags & REQ_F_ISREG) {
1908 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001909
Jens Axboe491381ce2019-10-17 09:20:46 -06001910 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001911 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001912 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001913}
1914
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001915static inline void req_set_fail_links(struct io_kiocb *req)
1916{
1917 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1918 req->flags |= REQ_F_FAIL_LINK;
1919}
1920
Jens Axboeba816ad2019-09-28 11:36:45 -06001921static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001922{
Jens Axboe9adbd452019-12-20 08:45:55 -07001923 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001924 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001925
Jens Axboe491381ce2019-10-17 09:20:46 -06001926 if (kiocb->ki_flags & IOCB_WRITE)
1927 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001928
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001929 if (res != req->result)
1930 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001931 if (req->flags & REQ_F_BUFFER_SELECTED)
1932 cflags = io_put_kbuf(req);
1933 __io_cqring_add_event(req, res, cflags);
Jens Axboeba816ad2019-09-28 11:36:45 -06001934}
1935
1936static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1937{
Jens Axboe9adbd452019-12-20 08:45:55 -07001938 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001939
1940 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001941 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001942}
1943
Jens Axboedef596e2019-01-09 08:59:42 -07001944static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1945{
Jens Axboe9adbd452019-12-20 08:45:55 -07001946 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001947
Jens Axboe491381ce2019-10-17 09:20:46 -06001948 if (kiocb->ki_flags & IOCB_WRITE)
1949 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001950
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001951 if (res != req->result)
1952 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001953 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001954 if (res != -EAGAIN)
1955 req->flags |= REQ_F_IOPOLL_COMPLETED;
1956}
1957
1958/*
1959 * After the iocb has been issued, it's safe to be found on the poll list.
1960 * Adding the kiocb to the list AFTER submission ensures that we don't
1961 * find it from a io_iopoll_getevents() thread before the issuer is done
1962 * accessing the kiocb cookie.
1963 */
1964static void io_iopoll_req_issued(struct io_kiocb *req)
1965{
1966 struct io_ring_ctx *ctx = req->ctx;
1967
1968 /*
1969 * Track whether we have multiple files in our lists. This will impact
1970 * how we do polling eventually, not spinning if we're on potentially
1971 * different devices.
1972 */
1973 if (list_empty(&ctx->poll_list)) {
1974 ctx->poll_multi_file = false;
1975 } else if (!ctx->poll_multi_file) {
1976 struct io_kiocb *list_req;
1977
1978 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1979 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001980 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001981 ctx->poll_multi_file = true;
1982 }
1983
1984 /*
1985 * For fast devices, IO may have already completed. If it has, add
1986 * it to the front so we find it first.
1987 */
1988 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1989 list_add(&req->list, &ctx->poll_list);
1990 else
1991 list_add_tail(&req->list, &ctx->poll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08001992
1993 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
1994 wq_has_sleeper(&ctx->sqo_wait))
1995 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07001996}
1997
Jens Axboe3d6770f2019-04-13 11:50:54 -06001998static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001999{
Jens Axboe3d6770f2019-04-13 11:50:54 -06002000 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07002001 int diff = state->has_refs - state->used_refs;
2002
2003 if (diff)
2004 fput_many(state->file, diff);
2005 state->file = NULL;
2006 }
2007}
2008
2009/*
2010 * Get as many references to a file as we have IOs left in this submission,
2011 * assuming most submissions are for one file, or at least that each file
2012 * has more than one submission.
2013 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002014static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002015{
2016 if (!state)
2017 return fget(fd);
2018
2019 if (state->file) {
2020 if (state->fd == fd) {
2021 state->used_refs++;
2022 state->ios_left--;
2023 return state->file;
2024 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06002025 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002026 }
2027 state->file = fget_many(fd, state->ios_left);
2028 if (!state->file)
2029 return NULL;
2030
2031 state->fd = fd;
2032 state->has_refs = state->ios_left;
2033 state->used_refs = 1;
2034 state->ios_left--;
2035 return state->file;
2036}
2037
Jens Axboe2b188cc2019-01-07 10:46:33 -07002038/*
2039 * If we tracked the file through the SCM inflight mechanism, we could support
2040 * any file. For now, just ensure that anything potentially problematic is done
2041 * inline.
2042 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002043static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002044{
2045 umode_t mode = file_inode(file)->i_mode;
2046
Jens Axboe10d59342019-12-09 20:16:22 -07002047 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002048 return true;
2049 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
2050 return true;
2051
Jens Axboeaf197f52020-04-28 13:15:06 -06002052 if (!(file->f_mode & FMODE_NOWAIT))
2053 return false;
2054
2055 if (rw == READ)
2056 return file->f_op->read_iter != NULL;
2057
2058 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002059}
2060
Jens Axboe3529d8c2019-12-19 18:24:38 -07002061static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2062 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002063{
Jens Axboedef596e2019-01-09 08:59:42 -07002064 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002065 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002066 unsigned ioprio;
2067 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002068
Jens Axboe491381ce2019-10-17 09:20:46 -06002069 if (S_ISREG(file_inode(req->file)->i_mode))
2070 req->flags |= REQ_F_ISREG;
2071
Jens Axboe2b188cc2019-01-07 10:46:33 -07002072 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002073 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2074 req->flags |= REQ_F_CUR_POS;
2075 kiocb->ki_pos = req->file->f_pos;
2076 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002077 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002078 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2079 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2080 if (unlikely(ret))
2081 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002082
2083 ioprio = READ_ONCE(sqe->ioprio);
2084 if (ioprio) {
2085 ret = ioprio_check_cap(ioprio);
2086 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002087 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002088
2089 kiocb->ki_ioprio = ioprio;
2090 } else
2091 kiocb->ki_ioprio = get_current_ioprio();
2092
Stefan Bühler8449eed2019-04-27 20:34:19 +02002093 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06002094 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
2095 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02002096 req->flags |= REQ_F_NOWAIT;
2097
2098 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002099 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02002100
Jens Axboedef596e2019-01-09 08:59:42 -07002101 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002102 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2103 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002104 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002105
Jens Axboedef596e2019-01-09 08:59:42 -07002106 kiocb->ki_flags |= IOCB_HIPRI;
2107 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06002108 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002109 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002110 if (kiocb->ki_flags & IOCB_HIPRI)
2111 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002112 kiocb->ki_complete = io_complete_rw;
2113 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002114
Jens Axboe3529d8c2019-12-19 18:24:38 -07002115 req->rw.addr = READ_ONCE(sqe->addr);
2116 req->rw.len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002117 /* we own ->private, reuse it for the buffer index / buffer ID */
Jens Axboe9adbd452019-12-20 08:45:55 -07002118 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002119 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002120 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002121}
2122
2123static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2124{
2125 switch (ret) {
2126 case -EIOCBQUEUED:
2127 break;
2128 case -ERESTARTSYS:
2129 case -ERESTARTNOINTR:
2130 case -ERESTARTNOHAND:
2131 case -ERESTART_RESTARTBLOCK:
2132 /*
2133 * We can't just restart the syscall, since previously
2134 * submitted sqes may already be in progress. Just fail this
2135 * IO with EINTR.
2136 */
2137 ret = -EINTR;
2138 /* fall through */
2139 default:
2140 kiocb->ki_complete(kiocb, ret, 0);
2141 }
2142}
2143
Pavel Begunkov014db002020-03-03 21:33:12 +03002144static void kiocb_done(struct kiocb *kiocb, ssize_t ret)
Jens Axboeba816ad2019-09-28 11:36:45 -06002145{
Jens Axboeba042912019-12-25 16:33:42 -07002146 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2147
2148 if (req->flags & REQ_F_CUR_POS)
2149 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002150 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Pavel Begunkov014db002020-03-03 21:33:12 +03002151 io_complete_rw(kiocb, ret, 0);
Jens Axboeba816ad2019-09-28 11:36:45 -06002152 else
2153 io_rw_done(kiocb, ret);
2154}
2155
Jens Axboe9adbd452019-12-20 08:45:55 -07002156static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002157 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002158{
Jens Axboe9adbd452019-12-20 08:45:55 -07002159 struct io_ring_ctx *ctx = req->ctx;
2160 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002161 struct io_mapped_ubuf *imu;
2162 unsigned index, buf_index;
2163 size_t offset;
2164 u64 buf_addr;
2165
2166 /* attempt to use fixed buffers without having provided iovecs */
2167 if (unlikely(!ctx->user_bufs))
2168 return -EFAULT;
2169
Jens Axboe9adbd452019-12-20 08:45:55 -07002170 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07002171 if (unlikely(buf_index >= ctx->nr_user_bufs))
2172 return -EFAULT;
2173
2174 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2175 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002176 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002177
2178 /* overflow */
2179 if (buf_addr + len < buf_addr)
2180 return -EFAULT;
2181 /* not inside the mapped region */
2182 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2183 return -EFAULT;
2184
2185 /*
2186 * May not be a start of buffer, set size appropriately
2187 * and advance us to the beginning.
2188 */
2189 offset = buf_addr - imu->ubuf;
2190 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002191
2192 if (offset) {
2193 /*
2194 * Don't use iov_iter_advance() here, as it's really slow for
2195 * using the latter parts of a big fixed buffer - it iterates
2196 * over each segment manually. We can cheat a bit here, because
2197 * we know that:
2198 *
2199 * 1) it's a BVEC iter, we set it up
2200 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2201 * first and last bvec
2202 *
2203 * So just find our index, and adjust the iterator afterwards.
2204 * If the offset is within the first bvec (or the whole first
2205 * bvec, just use iov_iter_advance(). This makes it easier
2206 * since we can just skip the first segment, which may not
2207 * be PAGE_SIZE aligned.
2208 */
2209 const struct bio_vec *bvec = imu->bvec;
2210
2211 if (offset <= bvec->bv_len) {
2212 iov_iter_advance(iter, offset);
2213 } else {
2214 unsigned long seg_skip;
2215
2216 /* skip first vec */
2217 offset -= bvec->bv_len;
2218 seg_skip = 1 + (offset >> PAGE_SHIFT);
2219
2220 iter->bvec = bvec + seg_skip;
2221 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002222 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002223 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002224 }
2225 }
2226
Jens Axboe5e559562019-11-13 16:12:46 -07002227 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002228}
2229
Jens Axboebcda7ba2020-02-23 16:42:51 -07002230static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2231{
2232 if (needs_lock)
2233 mutex_unlock(&ctx->uring_lock);
2234}
2235
2236static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2237{
2238 /*
2239 * "Normal" inline submissions always hold the uring_lock, since we
2240 * grab it from the system call. Same is true for the SQPOLL offload.
2241 * The only exception is when we've detached the request and issue it
2242 * from an async worker thread, grab the lock for that case.
2243 */
2244 if (needs_lock)
2245 mutex_lock(&ctx->uring_lock);
2246}
2247
2248static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2249 int bgid, struct io_buffer *kbuf,
2250 bool needs_lock)
2251{
2252 struct io_buffer *head;
2253
2254 if (req->flags & REQ_F_BUFFER_SELECTED)
2255 return kbuf;
2256
2257 io_ring_submit_lock(req->ctx, needs_lock);
2258
2259 lockdep_assert_held(&req->ctx->uring_lock);
2260
2261 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2262 if (head) {
2263 if (!list_empty(&head->list)) {
2264 kbuf = list_last_entry(&head->list, struct io_buffer,
2265 list);
2266 list_del(&kbuf->list);
2267 } else {
2268 kbuf = head;
2269 idr_remove(&req->ctx->io_buffer_idr, bgid);
2270 }
2271 if (*len > kbuf->len)
2272 *len = kbuf->len;
2273 } else {
2274 kbuf = ERR_PTR(-ENOBUFS);
2275 }
2276
2277 io_ring_submit_unlock(req->ctx, needs_lock);
2278
2279 return kbuf;
2280}
2281
Jens Axboe4d954c22020-02-27 07:31:19 -07002282static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2283 bool needs_lock)
2284{
2285 struct io_buffer *kbuf;
2286 int bgid;
2287
2288 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2289 bgid = (int) (unsigned long) req->rw.kiocb.private;
2290 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2291 if (IS_ERR(kbuf))
2292 return kbuf;
2293 req->rw.addr = (u64) (unsigned long) kbuf;
2294 req->flags |= REQ_F_BUFFER_SELECTED;
2295 return u64_to_user_ptr(kbuf->addr);
2296}
2297
2298#ifdef CONFIG_COMPAT
2299static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2300 bool needs_lock)
2301{
2302 struct compat_iovec __user *uiov;
2303 compat_ssize_t clen;
2304 void __user *buf;
2305 ssize_t len;
2306
2307 uiov = u64_to_user_ptr(req->rw.addr);
2308 if (!access_ok(uiov, sizeof(*uiov)))
2309 return -EFAULT;
2310 if (__get_user(clen, &uiov->iov_len))
2311 return -EFAULT;
2312 if (clen < 0)
2313 return -EINVAL;
2314
2315 len = clen;
2316 buf = io_rw_buffer_select(req, &len, needs_lock);
2317 if (IS_ERR(buf))
2318 return PTR_ERR(buf);
2319 iov[0].iov_base = buf;
2320 iov[0].iov_len = (compat_size_t) len;
2321 return 0;
2322}
2323#endif
2324
2325static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2326 bool needs_lock)
2327{
2328 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2329 void __user *buf;
2330 ssize_t len;
2331
2332 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2333 return -EFAULT;
2334
2335 len = iov[0].iov_len;
2336 if (len < 0)
2337 return -EINVAL;
2338 buf = io_rw_buffer_select(req, &len, needs_lock);
2339 if (IS_ERR(buf))
2340 return PTR_ERR(buf);
2341 iov[0].iov_base = buf;
2342 iov[0].iov_len = len;
2343 return 0;
2344}
2345
2346static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2347 bool needs_lock)
2348{
2349 if (req->flags & REQ_F_BUFFER_SELECTED)
2350 return 0;
2351 if (!req->rw.len)
2352 return 0;
2353 else if (req->rw.len > 1)
2354 return -EINVAL;
2355
2356#ifdef CONFIG_COMPAT
2357 if (req->ctx->compat)
2358 return io_compat_import(req, iov, needs_lock);
2359#endif
2360
2361 return __io_iov_buffer_select(req, iov, needs_lock);
2362}
2363
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002364static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
Jens Axboebcda7ba2020-02-23 16:42:51 -07002365 struct iovec **iovec, struct iov_iter *iter,
2366 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002367{
Jens Axboe9adbd452019-12-20 08:45:55 -07002368 void __user *buf = u64_to_user_ptr(req->rw.addr);
2369 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002370 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002371 u8 opcode;
2372
Jens Axboed625c6e2019-12-17 19:53:05 -07002373 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002374 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002375 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002376 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002377 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002378
Jens Axboebcda7ba2020-02-23 16:42:51 -07002379 /* buffer index only valid with fixed read/write, or buffer select */
2380 if (req->rw.kiocb.private && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002381 return -EINVAL;
2382
Jens Axboe3a6820f2019-12-22 15:19:35 -07002383 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002384 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002385 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
2386 if (IS_ERR(buf)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002387 *iovec = NULL;
Jens Axboe4d954c22020-02-27 07:31:19 -07002388 return PTR_ERR(buf);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002389 }
Jens Axboe3f9d6442020-03-11 12:27:04 -06002390 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002391 }
2392
Jens Axboe3a6820f2019-12-22 15:19:35 -07002393 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2394 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002395 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002396 }
2397
Jens Axboef67676d2019-12-02 11:03:47 -07002398 if (req->io) {
2399 struct io_async_rw *iorw = &req->io->rw;
2400
2401 *iovec = iorw->iov;
2402 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2403 if (iorw->iov == iorw->fast_iov)
2404 *iovec = NULL;
2405 return iorw->size;
2406 }
2407
Jens Axboe4d954c22020-02-27 07:31:19 -07002408 if (req->flags & REQ_F_BUFFER_SELECT) {
2409 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002410 if (!ret) {
2411 ret = (*iovec)->iov_len;
2412 iov_iter_init(iter, rw, *iovec, 1, ret);
2413 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002414 *iovec = NULL;
2415 return ret;
2416 }
2417
Jens Axboe2b188cc2019-01-07 10:46:33 -07002418#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002419 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002420 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2421 iovec, iter);
2422#endif
2423
2424 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2425}
2426
Jens Axboe32960612019-09-23 11:05:34 -06002427/*
2428 * For files that don't have ->read_iter() and ->write_iter(), handle them
2429 * by looping over ->read() or ->write() manually.
2430 */
2431static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2432 struct iov_iter *iter)
2433{
2434 ssize_t ret = 0;
2435
2436 /*
2437 * Don't support polled IO through this interface, and we can't
2438 * support non-blocking either. For the latter, this just causes
2439 * the kiocb to be handled from an async context.
2440 */
2441 if (kiocb->ki_flags & IOCB_HIPRI)
2442 return -EOPNOTSUPP;
2443 if (kiocb->ki_flags & IOCB_NOWAIT)
2444 return -EAGAIN;
2445
2446 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002447 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002448 ssize_t nr;
2449
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002450 if (!iov_iter_is_bvec(iter)) {
2451 iovec = iov_iter_iovec(iter);
2452 } else {
2453 /* fixed buffers import bvec */
2454 iovec.iov_base = kmap(iter->bvec->bv_page)
2455 + iter->iov_offset;
2456 iovec.iov_len = min(iter->count,
2457 iter->bvec->bv_len - iter->iov_offset);
2458 }
2459
Jens Axboe32960612019-09-23 11:05:34 -06002460 if (rw == READ) {
2461 nr = file->f_op->read(file, iovec.iov_base,
2462 iovec.iov_len, &kiocb->ki_pos);
2463 } else {
2464 nr = file->f_op->write(file, iovec.iov_base,
2465 iovec.iov_len, &kiocb->ki_pos);
2466 }
2467
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002468 if (iov_iter_is_bvec(iter))
2469 kunmap(iter->bvec->bv_page);
2470
Jens Axboe32960612019-09-23 11:05:34 -06002471 if (nr < 0) {
2472 if (!ret)
2473 ret = nr;
2474 break;
2475 }
2476 ret += nr;
2477 if (nr != iovec.iov_len)
2478 break;
2479 iov_iter_advance(iter, nr);
2480 }
2481
2482 return ret;
2483}
2484
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002485static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002486 struct iovec *iovec, struct iovec *fast_iov,
2487 struct iov_iter *iter)
2488{
2489 req->io->rw.nr_segs = iter->nr_segs;
2490 req->io->rw.size = io_size;
2491 req->io->rw.iov = iovec;
2492 if (!req->io->rw.iov) {
2493 req->io->rw.iov = req->io->rw.fast_iov;
Xiaoguang Wang45097da2020-04-08 22:29:58 +08002494 if (req->io->rw.iov != fast_iov)
2495 memcpy(req->io->rw.iov, fast_iov,
2496 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002497 } else {
2498 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002499 }
2500}
2501
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002502static inline int __io_alloc_async_ctx(struct io_kiocb *req)
2503{
2504 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
2505 return req->io == NULL;
2506}
2507
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002508static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002509{
Jens Axboed3656342019-12-18 09:50:26 -07002510 if (!io_op_defs[req->opcode].async_ctx)
2511 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002512
2513 return __io_alloc_async_ctx(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002514}
2515
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002516static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2517 struct iovec *iovec, struct iovec *fast_iov,
2518 struct iov_iter *iter)
2519{
Jens Axboe980ad262020-01-24 23:08:54 -07002520 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002521 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002522 if (!req->io) {
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002523 if (__io_alloc_async_ctx(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07002524 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002525
Jens Axboe5d204bc2020-01-31 12:06:52 -07002526 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2527 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002528 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002529}
2530
Jens Axboe3529d8c2019-12-19 18:24:38 -07002531static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2532 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002533{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002534 struct io_async_ctx *io;
2535 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002536 ssize_t ret;
2537
Jens Axboe3529d8c2019-12-19 18:24:38 -07002538 ret = io_prep_rw(req, sqe, force_nonblock);
2539 if (ret)
2540 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002541
Jens Axboe3529d8c2019-12-19 18:24:38 -07002542 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2543 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002544
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002545 /* either don't need iovec imported or already have it */
2546 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002547 return 0;
2548
2549 io = req->io;
2550 io->rw.iov = io->rw.fast_iov;
2551 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002552 ret = io_import_iovec(READ, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002553 req->io = io;
2554 if (ret < 0)
2555 return ret;
2556
2557 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2558 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002559}
2560
Pavel Begunkov014db002020-03-03 21:33:12 +03002561static int io_read(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002562{
2563 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002564 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002565 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002566 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002567 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002568
Jens Axboebcda7ba2020-02-23 16:42:51 -07002569 ret = io_import_iovec(READ, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07002570 if (ret < 0)
2571 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002572
Jens Axboefd6c2e42019-12-18 12:19:41 -07002573 /* Ensure we clear previously set non-block flag */
2574 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07002575 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002576
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002577 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002578 io_size = ret;
Pavel Begunkovdea3b492020-04-12 02:05:04 +03002579 if (req->flags & REQ_F_LINK_HEAD)
Jens Axboef67676d2019-12-02 11:03:47 -07002580 req->result = io_size;
2581
2582 /*
2583 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2584 * we know to async punt it even if it was opened O_NONBLOCK
2585 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002586 if (force_nonblock && !io_file_supports_async(req->file, READ))
Jens Axboef67676d2019-12-02 11:03:47 -07002587 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002588
Jens Axboe31b51512019-01-18 22:56:34 -07002589 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002590 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002591 if (!ret) {
2592 ssize_t ret2;
2593
Jens Axboe9adbd452019-12-20 08:45:55 -07002594 if (req->file->f_op->read_iter)
2595 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002596 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002597 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002598
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002599 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002600 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkov014db002020-03-03 21:33:12 +03002601 kiocb_done(kiocb, ret2);
Jens Axboef67676d2019-12-02 11:03:47 -07002602 } else {
2603copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002604 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002605 inline_vecs, &iter);
2606 if (ret)
2607 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002608 /* any defer here is final, must blocking retry */
Jens Axboe490e8962020-04-28 13:16:53 -06002609 if (!(req->flags & REQ_F_NOWAIT) &&
2610 !file_can_poll(req->file))
Jens Axboe29de5f62020-02-20 09:56:08 -07002611 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002612 return -EAGAIN;
2613 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002614 }
Jens Axboef67676d2019-12-02 11:03:47 -07002615out_free:
Pavel Begunkov1e950812020-02-06 19:51:16 +03002616 kfree(iovec);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002617 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002618 return ret;
2619}
2620
Jens Axboe3529d8c2019-12-19 18:24:38 -07002621static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2622 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002623{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002624 struct io_async_ctx *io;
2625 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002626 ssize_t ret;
2627
Jens Axboe3529d8c2019-12-19 18:24:38 -07002628 ret = io_prep_rw(req, sqe, force_nonblock);
2629 if (ret)
2630 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002631
Jens Axboe3529d8c2019-12-19 18:24:38 -07002632 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2633 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002634
Jens Axboe4ed734b2020-03-20 11:23:41 -06002635 req->fsize = rlimit(RLIMIT_FSIZE);
2636
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002637 /* either don't need iovec imported or already have it */
2638 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002639 return 0;
2640
2641 io = req->io;
2642 io->rw.iov = io->rw.fast_iov;
2643 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002644 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002645 req->io = io;
2646 if (ret < 0)
2647 return ret;
2648
2649 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2650 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002651}
2652
Pavel Begunkov014db002020-03-03 21:33:12 +03002653static int io_write(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002654{
2655 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002656 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002657 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002658 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002659 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002660
Jens Axboebcda7ba2020-02-23 16:42:51 -07002661 ret = io_import_iovec(WRITE, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07002662 if (ret < 0)
2663 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002664
Jens Axboefd6c2e42019-12-18 12:19:41 -07002665 /* Ensure we clear previously set non-block flag */
2666 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002667 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002668
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002669 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002670 io_size = ret;
Pavel Begunkovdea3b492020-04-12 02:05:04 +03002671 if (req->flags & REQ_F_LINK_HEAD)
Jens Axboef67676d2019-12-02 11:03:47 -07002672 req->result = io_size;
2673
2674 /*
2675 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2676 * we know to async punt it even if it was opened O_NONBLOCK
2677 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002678 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07002679 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07002680
Jens Axboe10d59342019-12-09 20:16:22 -07002681 /* file path doesn't support NOWAIT for non-direct_IO */
2682 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2683 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002684 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002685
Jens Axboe31b51512019-01-18 22:56:34 -07002686 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002687 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002688 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002689 ssize_t ret2;
2690
Jens Axboe2b188cc2019-01-07 10:46:33 -07002691 /*
2692 * Open-code file_start_write here to grab freeze protection,
2693 * which will be released by another thread in
2694 * io_complete_rw(). Fool lockdep by telling it the lock got
2695 * released so that it doesn't complain about the held lock when
2696 * we return to userspace.
2697 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002698 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002699 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002700 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002701 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002702 SB_FREEZE_WRITE);
2703 }
2704 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002705
Jens Axboe4ed734b2020-03-20 11:23:41 -06002706 if (!force_nonblock)
2707 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
2708
Jens Axboe9adbd452019-12-20 08:45:55 -07002709 if (req->file->f_op->write_iter)
2710 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002711 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002712 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboe4ed734b2020-03-20 11:23:41 -06002713
2714 if (!force_nonblock)
2715 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
2716
Jens Axboefaac9962020-02-07 15:45:22 -07002717 /*
Chucheng Luobff60352020-03-25 11:31:38 +08002718 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
Jens Axboefaac9962020-02-07 15:45:22 -07002719 * retry them without IOCB_NOWAIT.
2720 */
2721 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
2722 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07002723 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkov014db002020-03-03 21:33:12 +03002724 kiocb_done(kiocb, ret2);
Jens Axboef67676d2019-12-02 11:03:47 -07002725 } else {
2726copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002727 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002728 inline_vecs, &iter);
2729 if (ret)
2730 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002731 /* any defer here is final, must blocking retry */
Jens Axboe490e8962020-04-28 13:16:53 -06002732 if (!file_can_poll(req->file))
2733 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002734 return -EAGAIN;
2735 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002736 }
Jens Axboe31b51512019-01-18 22:56:34 -07002737out_free:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002738 req->flags &= ~REQ_F_NEED_CLEANUP;
Pavel Begunkov1e950812020-02-06 19:51:16 +03002739 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002740 return ret;
2741}
2742
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002743static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2744{
2745 struct io_splice* sp = &req->splice;
2746 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
2747 int ret;
2748
2749 if (req->flags & REQ_F_NEED_CLEANUP)
2750 return 0;
2751
2752 sp->file_in = NULL;
2753 sp->off_in = READ_ONCE(sqe->splice_off_in);
2754 sp->off_out = READ_ONCE(sqe->off);
2755 sp->len = READ_ONCE(sqe->len);
2756 sp->flags = READ_ONCE(sqe->splice_flags);
2757
2758 if (unlikely(sp->flags & ~valid_flags))
2759 return -EINVAL;
2760
2761 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
2762 (sp->flags & SPLICE_F_FD_IN_FIXED));
2763 if (ret)
2764 return ret;
2765 req->flags |= REQ_F_NEED_CLEANUP;
2766
2767 if (!S_ISREG(file_inode(sp->file_in)->i_mode))
2768 req->work.flags |= IO_WQ_WORK_UNBOUND;
2769
2770 return 0;
2771}
2772
Pavel Begunkov014db002020-03-03 21:33:12 +03002773static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002774{
2775 struct io_splice *sp = &req->splice;
2776 struct file *in = sp->file_in;
2777 struct file *out = sp->file_out;
2778 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
2779 loff_t *poff_in, *poff_out;
2780 long ret;
2781
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03002782 if (force_nonblock)
2783 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002784
2785 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
2786 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
2787 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
2788 if (force_nonblock && ret == -EAGAIN)
2789 return -EAGAIN;
2790
2791 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
2792 req->flags &= ~REQ_F_NEED_CLEANUP;
2793
2794 io_cqring_add_event(req, ret);
2795 if (ret != sp->len)
2796 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03002797 io_put_req(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002798 return 0;
2799}
2800
Jens Axboe2b188cc2019-01-07 10:46:33 -07002801/*
2802 * IORING_OP_NOP just posts a completion event, nothing else.
2803 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002804static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002805{
2806 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002807
Jens Axboedef596e2019-01-09 08:59:42 -07002808 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2809 return -EINVAL;
2810
Jens Axboe78e19bb2019-11-06 15:21:34 -07002811 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002812 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002813 return 0;
2814}
2815
Jens Axboe3529d8c2019-12-19 18:24:38 -07002816static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002817{
Jens Axboe6b063142019-01-10 22:13:58 -07002818 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002819
Jens Axboe09bb8392019-03-13 12:39:28 -06002820 if (!req->file)
2821 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002822
Jens Axboe6b063142019-01-10 22:13:58 -07002823 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002824 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002825 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002826 return -EINVAL;
2827
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002828 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2829 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2830 return -EINVAL;
2831
2832 req->sync.off = READ_ONCE(sqe->off);
2833 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002834 return 0;
2835}
2836
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002837static bool io_req_cancelled(struct io_kiocb *req)
2838{
2839 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2840 req_set_fail_links(req);
2841 io_cqring_add_event(req, -ECANCELED);
2842 io_put_req(req);
2843 return true;
2844 }
2845
2846 return false;
2847}
2848
Pavel Begunkov014db002020-03-03 21:33:12 +03002849static void __io_fsync(struct io_kiocb *req)
Jens Axboe78912932020-01-14 22:09:06 -07002850{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002851 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002852 int ret;
2853
Jens Axboe9adbd452019-12-20 08:45:55 -07002854 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002855 end > 0 ? end : LLONG_MAX,
2856 req->sync.flags & IORING_FSYNC_DATASYNC);
2857 if (ret < 0)
2858 req_set_fail_links(req);
2859 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002860 io_put_req(req);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002861}
2862
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002863static void io_fsync_finish(struct io_wq_work **workptr)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002864{
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002865 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002866
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002867 if (io_req_cancelled(req))
2868 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03002869 __io_fsync(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03002870 io_steal_work(req, workptr);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002871}
2872
Pavel Begunkov014db002020-03-03 21:33:12 +03002873static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002874{
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002875 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002876 if (force_nonblock) {
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002877 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002878 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002879 }
Pavel Begunkov014db002020-03-03 21:33:12 +03002880 __io_fsync(req);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002881 return 0;
2882}
2883
Pavel Begunkov014db002020-03-03 21:33:12 +03002884static void __io_fallocate(struct io_kiocb *req)
Jens Axboed63d1b52019-12-10 10:38:56 -07002885{
Jens Axboed63d1b52019-12-10 10:38:56 -07002886 int ret;
2887
Jens Axboe4ed734b2020-03-20 11:23:41 -06002888 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
Jens Axboed63d1b52019-12-10 10:38:56 -07002889 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2890 req->sync.len);
Jens Axboe4ed734b2020-03-20 11:23:41 -06002891 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
Jens Axboed63d1b52019-12-10 10:38:56 -07002892 if (ret < 0)
2893 req_set_fail_links(req);
2894 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002895 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002896}
2897
Jens Axboe2b188cc2019-01-07 10:46:33 -07002898static void io_fallocate_finish(struct io_wq_work **workptr)
2899{
2900 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Jens Axboed63d1b52019-12-10 10:38:56 -07002901
2902 if (io_req_cancelled(req))
2903 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03002904 __io_fallocate(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03002905 io_steal_work(req, workptr);
Jens Axboed63d1b52019-12-10 10:38:56 -07002906}
2907
2908static int io_fallocate_prep(struct io_kiocb *req,
2909 const struct io_uring_sqe *sqe)
2910{
2911 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2912 return -EINVAL;
2913
2914 req->sync.off = READ_ONCE(sqe->off);
2915 req->sync.len = READ_ONCE(sqe->addr);
2916 req->sync.mode = READ_ONCE(sqe->len);
Jens Axboe4ed734b2020-03-20 11:23:41 -06002917 req->fsize = rlimit(RLIMIT_FSIZE);
Jens Axboed63d1b52019-12-10 10:38:56 -07002918 return 0;
2919}
2920
Pavel Begunkov014db002020-03-03 21:33:12 +03002921static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07002922{
Jens Axboed63d1b52019-12-10 10:38:56 -07002923 /* fallocate always requiring blocking context */
2924 if (force_nonblock) {
Jens Axboed63d1b52019-12-10 10:38:56 -07002925 req->work.func = io_fallocate_finish;
2926 return -EAGAIN;
2927 }
2928
Pavel Begunkov014db002020-03-03 21:33:12 +03002929 __io_fallocate(req);
Jens Axboed63d1b52019-12-10 10:38:56 -07002930 return 0;
2931}
2932
Jens Axboe15b71ab2019-12-11 11:20:36 -07002933static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2934{
Jens Axboef8748882020-01-08 17:47:02 -07002935 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002936 int ret;
2937
2938 if (sqe->ioprio || sqe->buf_index)
2939 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03002940 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07002941 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002942 if (req->flags & REQ_F_NEED_CLEANUP)
2943 return 0;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002944
2945 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002946 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002947 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002948 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06002949 if (force_o_largefile())
2950 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002951
Jens Axboef8748882020-01-08 17:47:02 -07002952 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002953 if (IS_ERR(req->open.filename)) {
2954 ret = PTR_ERR(req->open.filename);
2955 req->open.filename = NULL;
2956 return ret;
2957 }
2958
Jens Axboe4022e7a2020-03-19 19:23:18 -06002959 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002960 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002961 return 0;
2962}
2963
Jens Axboecebdb982020-01-08 17:59:24 -07002964static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2965{
2966 struct open_how __user *how;
2967 const char __user *fname;
2968 size_t len;
2969 int ret;
2970
2971 if (sqe->ioprio || sqe->buf_index)
2972 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03002973 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07002974 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002975 if (req->flags & REQ_F_NEED_CLEANUP)
2976 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07002977
2978 req->open.dfd = READ_ONCE(sqe->fd);
2979 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2980 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2981 len = READ_ONCE(sqe->len);
2982
2983 if (len < OPEN_HOW_SIZE_VER0)
2984 return -EINVAL;
2985
2986 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2987 len);
2988 if (ret)
2989 return ret;
2990
2991 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2992 req->open.how.flags |= O_LARGEFILE;
2993
2994 req->open.filename = getname(fname);
2995 if (IS_ERR(req->open.filename)) {
2996 ret = PTR_ERR(req->open.filename);
2997 req->open.filename = NULL;
2998 return ret;
2999 }
3000
Jens Axboe4022e7a2020-03-19 19:23:18 -06003001 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003002 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboecebdb982020-01-08 17:59:24 -07003003 return 0;
3004}
3005
Pavel Begunkov014db002020-03-03 21:33:12 +03003006static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003007{
3008 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003009 struct file *file;
3010 int ret;
3011
Jens Axboef86cd202020-01-29 13:46:44 -07003012 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003013 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003014
Jens Axboecebdb982020-01-08 17:59:24 -07003015 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003016 if (ret)
3017 goto err;
3018
Jens Axboe4022e7a2020-03-19 19:23:18 -06003019 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003020 if (ret < 0)
3021 goto err;
3022
3023 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3024 if (IS_ERR(file)) {
3025 put_unused_fd(ret);
3026 ret = PTR_ERR(file);
3027 } else {
3028 fsnotify_open(file);
3029 fd_install(ret, file);
3030 }
3031err:
3032 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003033 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003034 if (ret < 0)
3035 req_set_fail_links(req);
3036 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003037 io_put_req(req);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003038 return 0;
3039}
3040
Pavel Begunkov014db002020-03-03 21:33:12 +03003041static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003042{
3043 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
Pavel Begunkov014db002020-03-03 21:33:12 +03003044 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003045}
3046
Jens Axboe067524e2020-03-02 16:32:28 -07003047static int io_remove_buffers_prep(struct io_kiocb *req,
3048 const struct io_uring_sqe *sqe)
3049{
3050 struct io_provide_buf *p = &req->pbuf;
3051 u64 tmp;
3052
3053 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3054 return -EINVAL;
3055
3056 tmp = READ_ONCE(sqe->fd);
3057 if (!tmp || tmp > USHRT_MAX)
3058 return -EINVAL;
3059
3060 memset(p, 0, sizeof(*p));
3061 p->nbufs = tmp;
3062 p->bgid = READ_ONCE(sqe->buf_group);
3063 return 0;
3064}
3065
3066static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3067 int bgid, unsigned nbufs)
3068{
3069 unsigned i = 0;
3070
3071 /* shouldn't happen */
3072 if (!nbufs)
3073 return 0;
3074
3075 /* the head kbuf is the list itself */
3076 while (!list_empty(&buf->list)) {
3077 struct io_buffer *nxt;
3078
3079 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3080 list_del(&nxt->list);
3081 kfree(nxt);
3082 if (++i == nbufs)
3083 return i;
3084 }
3085 i++;
3086 kfree(buf);
3087 idr_remove(&ctx->io_buffer_idr, bgid);
3088
3089 return i;
3090}
3091
3092static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock)
3093{
3094 struct io_provide_buf *p = &req->pbuf;
3095 struct io_ring_ctx *ctx = req->ctx;
3096 struct io_buffer *head;
3097 int ret = 0;
3098
3099 io_ring_submit_lock(ctx, !force_nonblock);
3100
3101 lockdep_assert_held(&ctx->uring_lock);
3102
3103 ret = -ENOENT;
3104 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3105 if (head)
3106 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3107
3108 io_ring_submit_lock(ctx, !force_nonblock);
3109 if (ret < 0)
3110 req_set_fail_links(req);
3111 io_cqring_add_event(req, ret);
3112 io_put_req(req);
3113 return 0;
3114}
3115
Jens Axboeddf0322d2020-02-23 16:41:33 -07003116static int io_provide_buffers_prep(struct io_kiocb *req,
3117 const struct io_uring_sqe *sqe)
3118{
3119 struct io_provide_buf *p = &req->pbuf;
3120 u64 tmp;
3121
3122 if (sqe->ioprio || sqe->rw_flags)
3123 return -EINVAL;
3124
3125 tmp = READ_ONCE(sqe->fd);
3126 if (!tmp || tmp > USHRT_MAX)
3127 return -E2BIG;
3128 p->nbufs = tmp;
3129 p->addr = READ_ONCE(sqe->addr);
3130 p->len = READ_ONCE(sqe->len);
3131
3132 if (!access_ok(u64_to_user_ptr(p->addr), p->len))
3133 return -EFAULT;
3134
3135 p->bgid = READ_ONCE(sqe->buf_group);
3136 tmp = READ_ONCE(sqe->off);
3137 if (tmp > USHRT_MAX)
3138 return -E2BIG;
3139 p->bid = tmp;
3140 return 0;
3141}
3142
3143static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3144{
3145 struct io_buffer *buf;
3146 u64 addr = pbuf->addr;
3147 int i, bid = pbuf->bid;
3148
3149 for (i = 0; i < pbuf->nbufs; i++) {
3150 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3151 if (!buf)
3152 break;
3153
3154 buf->addr = addr;
3155 buf->len = pbuf->len;
3156 buf->bid = bid;
3157 addr += pbuf->len;
3158 bid++;
3159 if (!*head) {
3160 INIT_LIST_HEAD(&buf->list);
3161 *head = buf;
3162 } else {
3163 list_add_tail(&buf->list, &(*head)->list);
3164 }
3165 }
3166
3167 return i ? i : -ENOMEM;
3168}
3169
Jens Axboeddf0322d2020-02-23 16:41:33 -07003170static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock)
3171{
3172 struct io_provide_buf *p = &req->pbuf;
3173 struct io_ring_ctx *ctx = req->ctx;
3174 struct io_buffer *head, *list;
3175 int ret = 0;
3176
3177 io_ring_submit_lock(ctx, !force_nonblock);
3178
3179 lockdep_assert_held(&ctx->uring_lock);
3180
3181 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3182
3183 ret = io_add_buffers(p, &head);
3184 if (ret < 0)
3185 goto out;
3186
3187 if (!list) {
3188 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3189 GFP_KERNEL);
3190 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003191 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003192 goto out;
3193 }
3194 }
3195out:
3196 io_ring_submit_unlock(ctx, !force_nonblock);
3197 if (ret < 0)
3198 req_set_fail_links(req);
3199 io_cqring_add_event(req, ret);
3200 io_put_req(req);
3201 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003202}
3203
Jens Axboe3e4827b2020-01-08 15:18:09 -07003204static int io_epoll_ctl_prep(struct io_kiocb *req,
3205 const struct io_uring_sqe *sqe)
3206{
3207#if defined(CONFIG_EPOLL)
3208 if (sqe->ioprio || sqe->buf_index)
3209 return -EINVAL;
3210
3211 req->epoll.epfd = READ_ONCE(sqe->fd);
3212 req->epoll.op = READ_ONCE(sqe->len);
3213 req->epoll.fd = READ_ONCE(sqe->off);
3214
3215 if (ep_op_has_event(req->epoll.op)) {
3216 struct epoll_event __user *ev;
3217
3218 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3219 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3220 return -EFAULT;
3221 }
3222
3223 return 0;
3224#else
3225 return -EOPNOTSUPP;
3226#endif
3227}
3228
Pavel Begunkov014db002020-03-03 21:33:12 +03003229static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003230{
3231#if defined(CONFIG_EPOLL)
3232 struct io_epoll *ie = &req->epoll;
3233 int ret;
3234
3235 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3236 if (force_nonblock && ret == -EAGAIN)
3237 return -EAGAIN;
3238
3239 if (ret < 0)
3240 req_set_fail_links(req);
3241 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003242 io_put_req(req);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003243 return 0;
3244#else
3245 return -EOPNOTSUPP;
3246#endif
3247}
3248
Jens Axboec1ca7572019-12-25 22:18:28 -07003249static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3250{
3251#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3252 if (sqe->ioprio || sqe->buf_index || sqe->off)
3253 return -EINVAL;
3254
3255 req->madvise.addr = READ_ONCE(sqe->addr);
3256 req->madvise.len = READ_ONCE(sqe->len);
3257 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3258 return 0;
3259#else
3260 return -EOPNOTSUPP;
3261#endif
3262}
3263
Pavel Begunkov014db002020-03-03 21:33:12 +03003264static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003265{
3266#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3267 struct io_madvise *ma = &req->madvise;
3268 int ret;
3269
3270 if (force_nonblock)
3271 return -EAGAIN;
3272
3273 ret = do_madvise(ma->addr, ma->len, ma->advice);
3274 if (ret < 0)
3275 req_set_fail_links(req);
3276 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003277 io_put_req(req);
Jens Axboec1ca7572019-12-25 22:18:28 -07003278 return 0;
3279#else
3280 return -EOPNOTSUPP;
3281#endif
3282}
3283
Jens Axboe4840e412019-12-25 22:03:45 -07003284static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3285{
3286 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3287 return -EINVAL;
3288
3289 req->fadvise.offset = READ_ONCE(sqe->off);
3290 req->fadvise.len = READ_ONCE(sqe->len);
3291 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3292 return 0;
3293}
3294
Pavel Begunkov014db002020-03-03 21:33:12 +03003295static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07003296{
3297 struct io_fadvise *fa = &req->fadvise;
3298 int ret;
3299
Jens Axboe3e694262020-02-01 09:22:49 -07003300 if (force_nonblock) {
3301 switch (fa->advice) {
3302 case POSIX_FADV_NORMAL:
3303 case POSIX_FADV_RANDOM:
3304 case POSIX_FADV_SEQUENTIAL:
3305 break;
3306 default:
3307 return -EAGAIN;
3308 }
3309 }
Jens Axboe4840e412019-12-25 22:03:45 -07003310
3311 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3312 if (ret < 0)
3313 req_set_fail_links(req);
3314 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003315 io_put_req(req);
Jens Axboe4840e412019-12-25 22:03:45 -07003316 return 0;
3317}
3318
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003319static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3320{
Jens Axboef8748882020-01-08 17:47:02 -07003321 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003322 unsigned lookup_flags;
3323 int ret;
3324
3325 if (sqe->ioprio || sqe->buf_index)
3326 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003327 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003328 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003329 if (req->flags & REQ_F_NEED_CLEANUP)
3330 return 0;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003331
3332 req->open.dfd = READ_ONCE(sqe->fd);
3333 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07003334 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003335 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07003336 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003337
Jens Axboec12cedf2020-01-08 17:41:21 -07003338 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003339 return -EINVAL;
3340
Jens Axboef8748882020-01-08 17:47:02 -07003341 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003342 if (IS_ERR(req->open.filename)) {
3343 ret = PTR_ERR(req->open.filename);
3344 req->open.filename = NULL;
3345 return ret;
3346 }
3347
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003348 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003349 return 0;
3350}
3351
Pavel Begunkov014db002020-03-03 21:33:12 +03003352static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003353{
3354 struct io_open *ctx = &req->open;
3355 unsigned lookup_flags;
3356 struct path path;
3357 struct kstat stat;
3358 int ret;
3359
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003360 if (force_nonblock) {
3361 /* only need file table for an actual valid fd */
3362 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
3363 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003364 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003365 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003366
Jens Axboec12cedf2020-01-08 17:41:21 -07003367 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003368 return -EINVAL;
3369
3370retry:
3371 /* filename_lookup() drops it, keep a reference */
3372 ctx->filename->refcnt++;
3373
3374 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
3375 NULL);
3376 if (ret)
3377 goto err;
3378
Jens Axboec12cedf2020-01-08 17:41:21 -07003379 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003380 path_put(&path);
3381 if (retry_estale(ret, lookup_flags)) {
3382 lookup_flags |= LOOKUP_REVAL;
3383 goto retry;
3384 }
3385 if (!ret)
3386 ret = cp_statx(&stat, ctx->buffer);
3387err:
3388 putname(ctx->filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003389 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003390 if (ret < 0)
3391 req_set_fail_links(req);
3392 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003393 io_put_req(req);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003394 return 0;
3395}
3396
Jens Axboeb5dba592019-12-11 14:02:38 -07003397static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3398{
3399 /*
3400 * If we queue this for async, it must not be cancellable. That would
3401 * leave the 'file' in an undeterminate state.
3402 */
3403 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
3404
3405 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
3406 sqe->rw_flags || sqe->buf_index)
3407 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003408 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003409 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07003410
3411 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboeb5dba592019-12-11 14:02:38 -07003412 return 0;
3413}
3414
Pavel Begunkova93b3332020-02-08 14:04:34 +03003415/* only called when __close_fd_get_file() is done */
Pavel Begunkov014db002020-03-03 21:33:12 +03003416static void __io_close_finish(struct io_kiocb *req)
Pavel Begunkova93b3332020-02-08 14:04:34 +03003417{
3418 int ret;
3419
3420 ret = filp_close(req->close.put_file, req->work.files);
3421 if (ret < 0)
3422 req_set_fail_links(req);
3423 io_cqring_add_event(req, ret);
3424 fput(req->close.put_file);
Pavel Begunkov014db002020-03-03 21:33:12 +03003425 io_put_req(req);
Pavel Begunkova93b3332020-02-08 14:04:34 +03003426}
3427
Jens Axboeb5dba592019-12-11 14:02:38 -07003428static void io_close_finish(struct io_wq_work **workptr)
3429{
3430 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Jens Axboeb5dba592019-12-11 14:02:38 -07003431
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03003432 /* not cancellable, don't do io_req_cancelled() */
Pavel Begunkov014db002020-03-03 21:33:12 +03003433 __io_close_finish(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03003434 io_steal_work(req, workptr);
Jens Axboeb5dba592019-12-11 14:02:38 -07003435}
3436
Pavel Begunkov014db002020-03-03 21:33:12 +03003437static int io_close(struct io_kiocb *req, bool force_nonblock)
Jens Axboeb5dba592019-12-11 14:02:38 -07003438{
3439 int ret;
3440
3441 req->close.put_file = NULL;
3442 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
Jens Axboe904fbcb2020-05-08 21:27:24 -06003443 if (ret < 0) {
3444 if (ret == -ENOENT)
3445 ret = -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07003446 return ret;
Jens Axboe904fbcb2020-05-08 21:27:24 -06003447 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003448
3449 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkova2100672020-03-02 23:45:16 +03003450 if (req->close.put_file->f_op->flush && force_nonblock) {
Pavel Begunkov594506f2020-03-03 21:33:11 +03003451 /* submission ref will be dropped, take it for async */
3452 refcount_inc(&req->refs);
3453
Pavel Begunkova2100672020-03-02 23:45:16 +03003454 req->work.func = io_close_finish;
3455 /*
3456 * Do manual async queue here to avoid grabbing files - we don't
3457 * need the files, and it'll cause io_close_finish() to close
3458 * the file again and cause a double CQE entry for this request
3459 */
3460 io_queue_async_work(req);
3461 return 0;
3462 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003463
3464 /*
3465 * No ->flush(), safely close from here and just punt the
3466 * fput() to async context.
3467 */
Pavel Begunkov014db002020-03-03 21:33:12 +03003468 __io_close_finish(req);
Jens Axboe1a417f42020-01-31 17:16:48 -07003469 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07003470}
3471
Jens Axboe3529d8c2019-12-19 18:24:38 -07003472static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003473{
3474 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003475
3476 if (!req->file)
3477 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003478
3479 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3480 return -EINVAL;
3481 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
3482 return -EINVAL;
3483
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003484 req->sync.off = READ_ONCE(sqe->off);
3485 req->sync.len = READ_ONCE(sqe->len);
3486 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003487 return 0;
3488}
3489
Pavel Begunkov014db002020-03-03 21:33:12 +03003490static void __io_sync_file_range(struct io_kiocb *req)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003491{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003492 int ret;
3493
Jens Axboe9adbd452019-12-20 08:45:55 -07003494 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003495 req->sync.flags);
3496 if (ret < 0)
3497 req_set_fail_links(req);
3498 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003499 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03003500}
3501
3502
3503static void io_sync_file_range_finish(struct io_wq_work **workptr)
3504{
3505 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03003506
3507 if (io_req_cancelled(req))
3508 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03003509 __io_sync_file_range(req);
Pavel Begunkov7759a0b2020-05-01 17:09:36 +03003510 io_steal_work(req, workptr);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003511}
3512
Pavel Begunkov014db002020-03-03 21:33:12 +03003513static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003514{
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003515 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003516 if (force_nonblock) {
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003517 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003518 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003519 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003520
Pavel Begunkov014db002020-03-03 21:33:12 +03003521 __io_sync_file_range(req);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003522 return 0;
3523}
3524
YueHaibing469956e2020-03-04 15:53:52 +08003525#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003526static int io_setup_async_msg(struct io_kiocb *req,
3527 struct io_async_msghdr *kmsg)
3528{
3529 if (req->io)
3530 return -EAGAIN;
3531 if (io_alloc_async_ctx(req)) {
3532 if (kmsg->iov != kmsg->fast_iov)
3533 kfree(kmsg->iov);
3534 return -ENOMEM;
3535 }
3536 req->flags |= REQ_F_NEED_CLEANUP;
3537 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
3538 return -EAGAIN;
3539}
3540
Jens Axboe3529d8c2019-12-19 18:24:38 -07003541static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06003542{
Jens Axboee47293f2019-12-20 08:58:21 -07003543 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003544 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003545 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003546
Jens Axboee47293f2019-12-20 08:58:21 -07003547 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3548 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07003549 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003550
Jens Axboed8768362020-02-27 14:17:49 -07003551#ifdef CONFIG_COMPAT
3552 if (req->ctx->compat)
3553 sr->msg_flags |= MSG_CMSG_COMPAT;
3554#endif
3555
Jens Axboefddafac2020-01-04 20:19:44 -07003556 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003557 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003558 /* iovec is already imported */
3559 if (req->flags & REQ_F_NEED_CLEANUP)
3560 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003561
Jens Axboed9688562019-12-09 19:35:20 -07003562 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003563 ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003564 &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003565 if (!ret)
3566 req->flags |= REQ_F_NEED_CLEANUP;
3567 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003568}
3569
Pavel Begunkov014db002020-03-03 21:33:12 +03003570static int io_sendmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003571{
Jens Axboe0b416c32019-12-15 10:57:46 -07003572 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07003573 struct socket *sock;
3574 int ret;
3575
3576 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3577 return -EINVAL;
3578
3579 sock = sock_from_file(req->file, &ret);
3580 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003581 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003582 unsigned flags;
3583
Jens Axboe03b12302019-12-02 18:50:25 -07003584 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003585 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003586 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003587 /* if iov is set, it's allocated already */
3588 if (!kmsg->iov)
3589 kmsg->iov = kmsg->fast_iov;
3590 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003591 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003592 struct io_sr_msg *sr = &req->sr_msg;
3593
Jens Axboe0b416c32019-12-15 10:57:46 -07003594 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003595 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003596
3597 io.msg.iov = io.msg.fast_iov;
3598 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3599 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003600 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003601 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003602 }
3603
Jens Axboee47293f2019-12-20 08:58:21 -07003604 flags = req->sr_msg.msg_flags;
3605 if (flags & MSG_DONTWAIT)
3606 req->flags |= REQ_F_NOWAIT;
3607 else if (force_nonblock)
3608 flags |= MSG_DONTWAIT;
3609
Jens Axboe0b416c32019-12-15 10:57:46 -07003610 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003611 if (force_nonblock && ret == -EAGAIN)
3612 return io_setup_async_msg(req, kmsg);
Jens Axboe03b12302019-12-02 18:50:25 -07003613 if (ret == -ERESTARTSYS)
3614 ret = -EINTR;
3615 }
3616
Pavel Begunkov1e950812020-02-06 19:51:16 +03003617 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003618 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003619 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe03b12302019-12-02 18:50:25 -07003620 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003621 if (ret < 0)
3622 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003623 io_put_req(req);
Jens Axboe03b12302019-12-02 18:50:25 -07003624 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003625}
3626
Pavel Begunkov014db002020-03-03 21:33:12 +03003627static int io_send(struct io_kiocb *req, bool force_nonblock)
Jens Axboefddafac2020-01-04 20:19:44 -07003628{
Jens Axboefddafac2020-01-04 20:19:44 -07003629 struct socket *sock;
3630 int ret;
3631
3632 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3633 return -EINVAL;
3634
3635 sock = sock_from_file(req->file, &ret);
3636 if (sock) {
3637 struct io_sr_msg *sr = &req->sr_msg;
3638 struct msghdr msg;
3639 struct iovec iov;
3640 unsigned flags;
3641
3642 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3643 &msg.msg_iter);
3644 if (ret)
3645 return ret;
3646
3647 msg.msg_name = NULL;
3648 msg.msg_control = NULL;
3649 msg.msg_controllen = 0;
3650 msg.msg_namelen = 0;
3651
3652 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 Axboe0b7b21e2020-01-31 08:34:59 -07003658 msg.msg_flags = flags;
3659 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003660 if (force_nonblock && ret == -EAGAIN)
3661 return -EAGAIN;
3662 if (ret == -ERESTARTSYS)
3663 ret = -EINTR;
3664 }
3665
3666 io_cqring_add_event(req, ret);
3667 if (ret < 0)
3668 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003669 io_put_req(req);
Jens Axboefddafac2020-01-04 20:19:44 -07003670 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003671}
3672
Jens Axboe52de1fe2020-02-27 10:15:42 -07003673static int __io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
3674{
3675 struct io_sr_msg *sr = &req->sr_msg;
3676 struct iovec __user *uiov;
3677 size_t iov_len;
3678 int ret;
3679
3680 ret = __copy_msghdr_from_user(&io->msg.msg, sr->msg, &io->msg.uaddr,
3681 &uiov, &iov_len);
3682 if (ret)
3683 return ret;
3684
3685 if (req->flags & REQ_F_BUFFER_SELECT) {
3686 if (iov_len > 1)
3687 return -EINVAL;
3688 if (copy_from_user(io->msg.iov, uiov, sizeof(*uiov)))
3689 return -EFAULT;
3690 sr->len = io->msg.iov[0].iov_len;
3691 iov_iter_init(&io->msg.msg.msg_iter, READ, io->msg.iov, 1,
3692 sr->len);
3693 io->msg.iov = NULL;
3694 } else {
3695 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
3696 &io->msg.iov, &io->msg.msg.msg_iter);
3697 if (ret > 0)
3698 ret = 0;
3699 }
3700
3701 return ret;
3702}
3703
3704#ifdef CONFIG_COMPAT
3705static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
3706 struct io_async_ctx *io)
3707{
3708 struct compat_msghdr __user *msg_compat;
3709 struct io_sr_msg *sr = &req->sr_msg;
3710 struct compat_iovec __user *uiov;
3711 compat_uptr_t ptr;
3712 compat_size_t len;
3713 int ret;
3714
3715 msg_compat = (struct compat_msghdr __user *) sr->msg;
3716 ret = __get_compat_msghdr(&io->msg.msg, msg_compat, &io->msg.uaddr,
3717 &ptr, &len);
3718 if (ret)
3719 return ret;
3720
3721 uiov = compat_ptr(ptr);
3722 if (req->flags & REQ_F_BUFFER_SELECT) {
3723 compat_ssize_t clen;
3724
3725 if (len > 1)
3726 return -EINVAL;
3727 if (!access_ok(uiov, sizeof(*uiov)))
3728 return -EFAULT;
3729 if (__get_user(clen, &uiov->iov_len))
3730 return -EFAULT;
3731 if (clen < 0)
3732 return -EINVAL;
3733 sr->len = io->msg.iov[0].iov_len;
3734 io->msg.iov = NULL;
3735 } else {
3736 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
3737 &io->msg.iov,
3738 &io->msg.msg.msg_iter);
3739 if (ret < 0)
3740 return ret;
3741 }
3742
3743 return 0;
3744}
Jens Axboe03b12302019-12-02 18:50:25 -07003745#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07003746
3747static int io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
3748{
3749 io->msg.iov = io->msg.fast_iov;
3750
3751#ifdef CONFIG_COMPAT
3752 if (req->ctx->compat)
3753 return __io_compat_recvmsg_copy_hdr(req, io);
3754#endif
3755
3756 return __io_recvmsg_copy_hdr(req, io);
3757}
3758
Jens Axboebcda7ba2020-02-23 16:42:51 -07003759static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
3760 int *cflags, bool needs_lock)
3761{
3762 struct io_sr_msg *sr = &req->sr_msg;
3763 struct io_buffer *kbuf;
3764
3765 if (!(req->flags & REQ_F_BUFFER_SELECT))
3766 return NULL;
3767
3768 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
3769 if (IS_ERR(kbuf))
3770 return kbuf;
3771
3772 sr->kbuf = kbuf;
3773 req->flags |= REQ_F_BUFFER_SELECTED;
3774
3775 *cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
3776 *cflags |= IORING_CQE_F_BUFFER;
3777 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07003778}
3779
Jens Axboe3529d8c2019-12-19 18:24:38 -07003780static int io_recvmsg_prep(struct io_kiocb *req,
3781 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003782{
Jens Axboee47293f2019-12-20 08:58:21 -07003783 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003784 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003785 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07003786
Jens Axboe3529d8c2019-12-19 18:24:38 -07003787 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3788 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003789 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003790 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003791
Jens Axboed8768362020-02-27 14:17:49 -07003792#ifdef CONFIG_COMPAT
3793 if (req->ctx->compat)
3794 sr->msg_flags |= MSG_CMSG_COMPAT;
3795#endif
3796
Jens Axboefddafac2020-01-04 20:19:44 -07003797 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003798 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003799 /* iovec is already imported */
3800 if (req->flags & REQ_F_NEED_CLEANUP)
3801 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003802
Jens Axboe52de1fe2020-02-27 10:15:42 -07003803 ret = io_recvmsg_copy_hdr(req, io);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003804 if (!ret)
3805 req->flags |= REQ_F_NEED_CLEANUP;
3806 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003807}
3808
Pavel Begunkov014db002020-03-03 21:33:12 +03003809static int io_recvmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003810{
Jens Axboe0b416c32019-12-15 10:57:46 -07003811 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003812 struct socket *sock;
Jens Axboe52de1fe2020-02-27 10:15:42 -07003813 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003814
3815 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3816 return -EINVAL;
3817
3818 sock = sock_from_file(req->file, &ret);
3819 if (sock) {
Jens Axboe52de1fe2020-02-27 10:15:42 -07003820 struct io_buffer *kbuf;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003821 struct io_async_ctx io;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003822 unsigned flags;
3823
Jens Axboe03b12302019-12-02 18:50:25 -07003824 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003825 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003826 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003827 /* if iov is set, it's allocated already */
3828 if (!kmsg->iov)
3829 kmsg->iov = kmsg->fast_iov;
3830 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003831 } else {
Jens Axboe0b416c32019-12-15 10:57:46 -07003832 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003833 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003834
Jens Axboe52de1fe2020-02-27 10:15:42 -07003835 ret = io_recvmsg_copy_hdr(req, &io);
Jens Axboe03b12302019-12-02 18:50:25 -07003836 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003837 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003838 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003839
Jens Axboe52de1fe2020-02-27 10:15:42 -07003840 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
3841 if (IS_ERR(kbuf)) {
3842 return PTR_ERR(kbuf);
3843 } else if (kbuf) {
3844 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3845 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
3846 1, req->sr_msg.len);
3847 }
3848
Jens Axboee47293f2019-12-20 08:58:21 -07003849 flags = req->sr_msg.msg_flags;
3850 if (flags & MSG_DONTWAIT)
3851 req->flags |= REQ_F_NOWAIT;
3852 else if (force_nonblock)
3853 flags |= MSG_DONTWAIT;
3854
3855 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3856 kmsg->uaddr, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003857 if (force_nonblock && ret == -EAGAIN)
3858 return io_setup_async_msg(req, kmsg);
Jens Axboe441cdbd2019-12-02 18:49:10 -07003859 if (ret == -ERESTARTSYS)
3860 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003861 }
3862
Pavel Begunkov1e950812020-02-06 19:51:16 +03003863 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003864 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003865 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe52de1fe2020-02-27 10:15:42 -07003866 __io_cqring_add_event(req, ret, cflags);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003867 if (ret < 0)
3868 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003869 io_put_req(req);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003870 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003871}
3872
Pavel Begunkov014db002020-03-03 21:33:12 +03003873static int io_recv(struct io_kiocb *req, bool force_nonblock)
Jens Axboefddafac2020-01-04 20:19:44 -07003874{
Jens Axboebcda7ba2020-02-23 16:42:51 -07003875 struct io_buffer *kbuf = NULL;
Jens Axboefddafac2020-01-04 20:19:44 -07003876 struct socket *sock;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003877 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003878
3879 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3880 return -EINVAL;
3881
3882 sock = sock_from_file(req->file, &ret);
3883 if (sock) {
3884 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003885 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07003886 struct msghdr msg;
3887 struct iovec iov;
3888 unsigned flags;
3889
Jens Axboebcda7ba2020-02-23 16:42:51 -07003890 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
3891 if (IS_ERR(kbuf))
3892 return PTR_ERR(kbuf);
3893 else if (kbuf)
3894 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07003895
Jens Axboebcda7ba2020-02-23 16:42:51 -07003896 ret = import_single_range(READ, buf, sr->len, &iov,
3897 &msg.msg_iter);
3898 if (ret) {
3899 kfree(kbuf);
3900 return ret;
3901 }
3902
3903 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07003904 msg.msg_name = NULL;
3905 msg.msg_control = NULL;
3906 msg.msg_controllen = 0;
3907 msg.msg_namelen = 0;
3908 msg.msg_iocb = NULL;
3909 msg.msg_flags = 0;
3910
3911 flags = req->sr_msg.msg_flags;
3912 if (flags & MSG_DONTWAIT)
3913 req->flags |= REQ_F_NOWAIT;
3914 else if (force_nonblock)
3915 flags |= MSG_DONTWAIT;
3916
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003917 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07003918 if (force_nonblock && ret == -EAGAIN)
3919 return -EAGAIN;
3920 if (ret == -ERESTARTSYS)
3921 ret = -EINTR;
3922 }
3923
Jens Axboebcda7ba2020-02-23 16:42:51 -07003924 kfree(kbuf);
3925 req->flags &= ~REQ_F_NEED_CLEANUP;
3926 __io_cqring_add_event(req, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07003927 if (ret < 0)
3928 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003929 io_put_req(req);
Jens Axboefddafac2020-01-04 20:19:44 -07003930 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003931}
3932
Jens Axboe3529d8c2019-12-19 18:24:38 -07003933static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003934{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003935 struct io_accept *accept = &req->accept;
3936
Jens Axboe17f2fe32019-10-17 14:42:58 -06003937 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3938 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003939 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003940 return -EINVAL;
3941
Jens Axboed55e5f52019-12-11 16:12:15 -07003942 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3943 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003944 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06003945 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003946 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003947}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003948
Pavel Begunkov014db002020-03-03 21:33:12 +03003949static int __io_accept(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003950{
3951 struct io_accept *accept = &req->accept;
3952 unsigned file_flags;
3953 int ret;
3954
3955 file_flags = force_nonblock ? O_NONBLOCK : 0;
3956 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06003957 accept->addr_len, accept->flags,
3958 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003959 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003960 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003961 if (ret == -ERESTARTSYS)
3962 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003963 if (ret < 0)
3964 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003965 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003966 io_put_req(req);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003967 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003968}
3969
3970static void io_accept_finish(struct io_wq_work **workptr)
3971{
3972 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003973
3974 if (io_req_cancelled(req))
3975 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03003976 __io_accept(req, false);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03003977 io_steal_work(req, workptr);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003978}
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003979
Pavel Begunkov014db002020-03-03 21:33:12 +03003980static int io_accept(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003981{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003982 int ret;
3983
Pavel Begunkov014db002020-03-03 21:33:12 +03003984 ret = __io_accept(req, force_nonblock);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003985 if (ret == -EAGAIN && force_nonblock) {
3986 req->work.func = io_accept_finish;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003987 return -EAGAIN;
3988 }
3989 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003990}
3991
Jens Axboe3529d8c2019-12-19 18:24:38 -07003992static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003993{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003994 struct io_connect *conn = &req->connect;
3995 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003996
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003997 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3998 return -EINVAL;
3999 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4000 return -EINVAL;
4001
Jens Axboe3529d8c2019-12-19 18:24:38 -07004002 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4003 conn->addr_len = READ_ONCE(sqe->addr2);
4004
4005 if (!io)
4006 return 0;
4007
4008 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004009 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004010}
4011
Pavel Begunkov014db002020-03-03 21:33:12 +03004012static int io_connect(struct io_kiocb *req, bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004013{
Jens Axboef499a022019-12-02 16:28:46 -07004014 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004015 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004016 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004017
Jens Axboef499a022019-12-02 16:28:46 -07004018 if (req->io) {
4019 io = req->io;
4020 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004021 ret = move_addr_to_kernel(req->connect.addr,
4022 req->connect.addr_len,
4023 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004024 if (ret)
4025 goto out;
4026 io = &__io;
4027 }
4028
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004029 file_flags = force_nonblock ? O_NONBLOCK : 0;
4030
4031 ret = __sys_connect_file(req->file, &io->connect.address,
4032 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004033 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004034 if (req->io)
4035 return -EAGAIN;
4036 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004037 ret = -ENOMEM;
4038 goto out;
4039 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004040 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004041 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004042 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004043 if (ret == -ERESTARTSYS)
4044 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004045out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004046 if (ret < 0)
4047 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004048 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03004049 io_put_req(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004050 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004051}
YueHaibing469956e2020-03-04 15:53:52 +08004052#else /* !CONFIG_NET */
4053static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4054{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004055 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004056}
4057
YueHaibing469956e2020-03-04 15:53:52 +08004058static int io_sendmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004059{
YueHaibing469956e2020-03-04 15:53:52 +08004060 return -EOPNOTSUPP;
4061}
4062
4063static int io_send(struct io_kiocb *req, bool force_nonblock)
4064{
4065 return -EOPNOTSUPP;
4066}
4067
4068static int io_recvmsg_prep(struct io_kiocb *req,
4069 const struct io_uring_sqe *sqe)
4070{
4071 return -EOPNOTSUPP;
4072}
4073
4074static int io_recvmsg(struct io_kiocb *req, bool force_nonblock)
4075{
4076 return -EOPNOTSUPP;
4077}
4078
4079static int io_recv(struct io_kiocb *req, bool force_nonblock)
4080{
4081 return -EOPNOTSUPP;
4082}
4083
4084static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4085{
4086 return -EOPNOTSUPP;
4087}
4088
4089static int io_accept(struct io_kiocb *req, bool force_nonblock)
4090{
4091 return -EOPNOTSUPP;
4092}
4093
4094static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4095{
4096 return -EOPNOTSUPP;
4097}
4098
4099static int io_connect(struct io_kiocb *req, bool force_nonblock)
4100{
4101 return -EOPNOTSUPP;
4102}
4103#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004104
Jens Axboed7718a92020-02-14 22:23:12 -07004105struct io_poll_table {
4106 struct poll_table_struct pt;
4107 struct io_kiocb *req;
4108 int error;
4109};
4110
Jens Axboed7718a92020-02-14 22:23:12 -07004111static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4112 __poll_t mask, task_work_func_t func)
4113{
4114 struct task_struct *tsk;
Jens Axboeaa96bf82020-04-03 11:26:26 -06004115 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004116
4117 /* for instances that support it check for an event match first: */
4118 if (mask && !(mask & poll->events))
4119 return 0;
4120
4121 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4122
4123 list_del_init(&poll->wait.entry);
4124
4125 tsk = req->task;
4126 req->result = mask;
4127 init_task_work(&req->task_work, func);
4128 /*
Jens Axboeaa96bf82020-04-03 11:26:26 -06004129 * If this fails, then the task is exiting. Punt to one of the io-wq
4130 * threads to ensure the work gets run, we can't always rely on exit
4131 * cancelation taking care of this.
Jens Axboed7718a92020-02-14 22:23:12 -07004132 */
Jens Axboeaa96bf82020-04-03 11:26:26 -06004133 ret = task_work_add(tsk, &req->task_work, true);
4134 if (unlikely(ret)) {
4135 tsk = io_wq_get_task(req->ctx->io_wq);
4136 task_work_add(tsk, &req->task_work, true);
4137 }
Jens Axboed7718a92020-02-14 22:23:12 -07004138 wake_up_process(tsk);
4139 return 1;
4140}
4141
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004142static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4143 __acquires(&req->ctx->completion_lock)
4144{
4145 struct io_ring_ctx *ctx = req->ctx;
4146
4147 if (!req->result && !READ_ONCE(poll->canceled)) {
4148 struct poll_table_struct pt = { ._key = poll->events };
4149
4150 req->result = vfs_poll(req->file, &pt) & poll->events;
4151 }
4152
4153 spin_lock_irq(&ctx->completion_lock);
4154 if (!req->result && !READ_ONCE(poll->canceled)) {
4155 add_wait_queue(poll->head, &poll->wait);
4156 return true;
4157 }
4158
4159 return false;
4160}
4161
Jens Axboe18bceab2020-05-15 11:56:54 -06004162static void io_poll_remove_double(struct io_kiocb *req)
4163{
4164 struct io_poll_iocb *poll = (struct io_poll_iocb *) req->io;
4165
4166 lockdep_assert_held(&req->ctx->completion_lock);
4167
4168 if (poll && poll->head) {
4169 struct wait_queue_head *head = poll->head;
4170
4171 spin_lock(&head->lock);
4172 list_del_init(&poll->wait.entry);
4173 if (poll->wait.private)
4174 refcount_dec(&req->refs);
4175 poll->head = NULL;
4176 spin_unlock(&head->lock);
4177 }
4178}
4179
4180static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4181{
4182 struct io_ring_ctx *ctx = req->ctx;
4183
4184 io_poll_remove_double(req);
4185 req->poll.done = true;
4186 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4187 io_commit_cqring(ctx);
4188}
4189
4190static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
4191{
4192 struct io_ring_ctx *ctx = req->ctx;
4193
4194 if (io_poll_rewait(req, &req->poll)) {
4195 spin_unlock_irq(&ctx->completion_lock);
4196 return;
4197 }
4198
4199 hash_del(&req->hash_node);
4200 io_poll_complete(req, req->result, 0);
4201 req->flags |= REQ_F_COMP_LOCKED;
4202 io_put_req_find_next(req, nxt);
4203 spin_unlock_irq(&ctx->completion_lock);
4204
4205 io_cqring_ev_posted(ctx);
4206}
4207
4208static void io_poll_task_func(struct callback_head *cb)
4209{
4210 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4211 struct io_kiocb *nxt = NULL;
4212
4213 io_poll_task_handler(req, &nxt);
4214 if (nxt) {
4215 struct io_ring_ctx *ctx = nxt->ctx;
4216
4217 mutex_lock(&ctx->uring_lock);
4218 __io_queue_sqe(nxt, NULL);
4219 mutex_unlock(&ctx->uring_lock);
4220 }
4221}
4222
4223static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4224 int sync, void *key)
4225{
4226 struct io_kiocb *req = wait->private;
4227 struct io_poll_iocb *poll = (struct io_poll_iocb *) req->io;
4228 __poll_t mask = key_to_poll(key);
4229
4230 /* for instances that support it check for an event match first: */
4231 if (mask && !(mask & poll->events))
4232 return 0;
4233
4234 if (req->poll.head) {
4235 bool done;
4236
4237 spin_lock(&req->poll.head->lock);
4238 done = list_empty(&req->poll.wait.entry);
4239 if (!done)
4240 list_del_init(&req->poll.wait.entry);
4241 spin_unlock(&req->poll.head->lock);
4242 if (!done)
4243 __io_async_wake(req, poll, mask, io_poll_task_func);
4244 }
4245 refcount_dec(&req->refs);
4246 return 1;
4247}
4248
4249static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4250 wait_queue_func_t wake_func)
4251{
4252 poll->head = NULL;
4253 poll->done = false;
4254 poll->canceled = false;
4255 poll->events = events;
4256 INIT_LIST_HEAD(&poll->wait.entry);
4257 init_waitqueue_func_entry(&poll->wait, wake_func);
4258}
4259
4260static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
4261 struct wait_queue_head *head)
4262{
4263 struct io_kiocb *req = pt->req;
4264
4265 /*
4266 * If poll->head is already set, it's because the file being polled
4267 * uses multiple waitqueues for poll handling (eg one for read, one
4268 * for write). Setup a separate io_poll_iocb if this happens.
4269 */
4270 if (unlikely(poll->head)) {
4271 /* already have a 2nd entry, fail a third attempt */
4272 if (req->io) {
4273 pt->error = -EINVAL;
4274 return;
4275 }
4276 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
4277 if (!poll) {
4278 pt->error = -ENOMEM;
4279 return;
4280 }
4281 io_init_poll_iocb(poll, req->poll.events, io_poll_double_wake);
4282 refcount_inc(&req->refs);
4283 poll->wait.private = req;
4284 req->io = (void *) poll;
4285 }
4286
4287 pt->error = 0;
4288 poll->head = head;
4289 add_wait_queue(head, &poll->wait);
4290}
4291
4292static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4293 struct poll_table_struct *p)
4294{
4295 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4296
4297 __io_queue_proc(&pt->req->apoll->poll, pt, head);
4298}
4299
Jens Axboed7718a92020-02-14 22:23:12 -07004300static void io_async_task_func(struct callback_head *cb)
4301{
4302 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4303 struct async_poll *apoll = req->apoll;
4304 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2bae0472020-04-13 11:16:34 -06004305 bool canceled;
Jens Axboed7718a92020-02-14 22:23:12 -07004306
4307 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4308
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004309 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07004310 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004311 return;
Jens Axboed7718a92020-02-14 22:23:12 -07004312 }
4313
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004314 if (hash_hashed(&req->hash_node))
4315 hash_del(&req->hash_node);
4316
Jens Axboe2bae0472020-04-13 11:16:34 -06004317 canceled = READ_ONCE(apoll->poll.canceled);
4318 if (canceled) {
4319 io_cqring_fill_event(req, -ECANCELED);
4320 io_commit_cqring(ctx);
4321 }
4322
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004323 spin_unlock_irq(&ctx->completion_lock);
4324
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004325 /* restore ->work in case we need to retry again */
4326 memcpy(&req->work, &apoll->work, sizeof(req->work));
4327
Jens Axboe2bae0472020-04-13 11:16:34 -06004328 if (canceled) {
4329 kfree(apoll);
4330 io_cqring_ev_posted(ctx);
4331 req_set_fail_links(req);
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004332 io_double_put_req(req);
Jens Axboe2bae0472020-04-13 11:16:34 -06004333 return;
4334 }
4335
Jens Axboed7718a92020-02-14 22:23:12 -07004336 __set_current_state(TASK_RUNNING);
4337 mutex_lock(&ctx->uring_lock);
4338 __io_queue_sqe(req, NULL);
4339 mutex_unlock(&ctx->uring_lock);
4340
4341 kfree(apoll);
4342}
4343
4344static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4345 void *key)
4346{
4347 struct io_kiocb *req = wait->private;
4348 struct io_poll_iocb *poll = &req->apoll->poll;
4349
4350 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4351 key_to_poll(key));
4352
4353 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4354}
4355
4356static void io_poll_req_insert(struct io_kiocb *req)
4357{
4358 struct io_ring_ctx *ctx = req->ctx;
4359 struct hlist_head *list;
4360
4361 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
4362 hlist_add_head(&req->hash_node, list);
4363}
4364
4365static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
4366 struct io_poll_iocb *poll,
4367 struct io_poll_table *ipt, __poll_t mask,
4368 wait_queue_func_t wake_func)
4369 __acquires(&ctx->completion_lock)
4370{
4371 struct io_ring_ctx *ctx = req->ctx;
4372 bool cancel = false;
4373
4374 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06004375 io_init_poll_iocb(poll, mask, wake_func);
4376 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07004377
4378 ipt->pt._key = mask;
4379 ipt->req = req;
4380 ipt->error = -EINVAL;
4381
Jens Axboed7718a92020-02-14 22:23:12 -07004382 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
4383
4384 spin_lock_irq(&ctx->completion_lock);
4385 if (likely(poll->head)) {
4386 spin_lock(&poll->head->lock);
4387 if (unlikely(list_empty(&poll->wait.entry))) {
4388 if (ipt->error)
4389 cancel = true;
4390 ipt->error = 0;
4391 mask = 0;
4392 }
4393 if (mask || ipt->error)
4394 list_del_init(&poll->wait.entry);
4395 else if (cancel)
4396 WRITE_ONCE(poll->canceled, true);
4397 else if (!poll->done) /* actually waiting for an event */
4398 io_poll_req_insert(req);
4399 spin_unlock(&poll->head->lock);
4400 }
4401
4402 return mask;
4403}
4404
4405static bool io_arm_poll_handler(struct io_kiocb *req)
4406{
4407 const struct io_op_def *def = &io_op_defs[req->opcode];
4408 struct io_ring_ctx *ctx = req->ctx;
4409 struct async_poll *apoll;
4410 struct io_poll_table ipt;
4411 __poll_t mask, ret;
Jens Axboe18bceab2020-05-15 11:56:54 -06004412 bool had_io;
Jens Axboed7718a92020-02-14 22:23:12 -07004413
4414 if (!req->file || !file_can_poll(req->file))
4415 return false;
4416 if (req->flags & (REQ_F_MUST_PUNT | REQ_F_POLLED))
4417 return false;
4418 if (!def->pollin && !def->pollout)
4419 return false;
4420
4421 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
4422 if (unlikely(!apoll))
4423 return false;
4424
4425 req->flags |= REQ_F_POLLED;
4426 memcpy(&apoll->work, &req->work, sizeof(req->work));
Jens Axboe18bceab2020-05-15 11:56:54 -06004427 had_io = req->io != NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07004428
Jens Axboe3537b6a2020-04-03 11:19:06 -06004429 get_task_struct(current);
Jens Axboed7718a92020-02-14 22:23:12 -07004430 req->task = current;
4431 req->apoll = apoll;
4432 INIT_HLIST_NODE(&req->hash_node);
4433
Nathan Chancellor8755d972020-03-02 16:01:19 -07004434 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07004435 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07004436 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07004437 if (def->pollout)
4438 mask |= POLLOUT | POLLWRNORM;
4439 mask |= POLLERR | POLLPRI;
4440
4441 ipt.pt._qproc = io_async_queue_proc;
4442
4443 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
4444 io_async_wake);
4445 if (ret) {
4446 ipt.error = 0;
Jens Axboe18bceab2020-05-15 11:56:54 -06004447 /* only remove double add if we did it here */
4448 if (!had_io)
4449 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004450 spin_unlock_irq(&ctx->completion_lock);
4451 memcpy(&req->work, &apoll->work, sizeof(req->work));
4452 kfree(apoll);
4453 return false;
4454 }
4455 spin_unlock_irq(&ctx->completion_lock);
4456 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
4457 apoll->poll.events);
4458 return true;
4459}
4460
4461static bool __io_poll_remove_one(struct io_kiocb *req,
4462 struct io_poll_iocb *poll)
4463{
Jens Axboeb41e9852020-02-17 09:52:41 -07004464 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004465
4466 spin_lock(&poll->head->lock);
4467 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07004468 if (!list_empty(&poll->wait.entry)) {
4469 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07004470 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004471 }
4472 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004473 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07004474 return do_complete;
4475}
4476
4477static bool io_poll_remove_one(struct io_kiocb *req)
4478{
4479 bool do_complete;
4480
4481 if (req->opcode == IORING_OP_POLL_ADD) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004482 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004483 do_complete = __io_poll_remove_one(req, &req->poll);
4484 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004485 struct async_poll *apoll = req->apoll;
4486
Jens Axboed7718a92020-02-14 22:23:12 -07004487 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004488 do_complete = __io_poll_remove_one(req, &apoll->poll);
4489 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07004490 io_put_req(req);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004491 /*
4492 * restore ->work because we will call
4493 * io_req_work_drop_env below when dropping the
4494 * final reference.
4495 */
4496 memcpy(&req->work, &apoll->work, sizeof(req->work));
4497 kfree(apoll);
4498 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08004499 }
4500
Jens Axboeb41e9852020-02-17 09:52:41 -07004501 if (do_complete) {
4502 io_cqring_fill_event(req, -ECANCELED);
4503 io_commit_cqring(req->ctx);
4504 req->flags |= REQ_F_COMP_LOCKED;
4505 io_put_req(req);
4506 }
4507
4508 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004509}
4510
4511static void io_poll_remove_all(struct io_ring_ctx *ctx)
4512{
Jens Axboe78076bb2019-12-04 19:56:40 -07004513 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004514 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004515 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004516
4517 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07004518 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
4519 struct hlist_head *list;
4520
4521 list = &ctx->cancel_hash[i];
4522 hlist_for_each_entry_safe(req, tmp, list, hash_node)
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004523 posted += io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004524 }
4525 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07004526
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004527 if (posted)
4528 io_cqring_ev_posted(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004529}
4530
Jens Axboe47f46762019-11-09 17:43:02 -07004531static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
4532{
Jens Axboe78076bb2019-12-04 19:56:40 -07004533 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07004534 struct io_kiocb *req;
4535
Jens Axboe78076bb2019-12-04 19:56:40 -07004536 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
4537 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07004538 if (sqe_addr != req->user_data)
4539 continue;
4540 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07004541 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07004542 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07004543 }
4544
4545 return -ENOENT;
4546}
4547
Jens Axboe3529d8c2019-12-19 18:24:38 -07004548static int io_poll_remove_prep(struct io_kiocb *req,
4549 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004550{
Jens Axboe221c5eb2019-01-17 09:41:58 -07004551 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4552 return -EINVAL;
4553 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
4554 sqe->poll_events)
4555 return -EINVAL;
4556
Jens Axboe0969e782019-12-17 18:40:57 -07004557 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07004558 return 0;
4559}
4560
4561/*
4562 * Find a running poll command that matches one specified in sqe->addr,
4563 * and remove it if found.
4564 */
4565static int io_poll_remove(struct io_kiocb *req)
4566{
4567 struct io_ring_ctx *ctx = req->ctx;
4568 u64 addr;
4569 int ret;
4570
Jens Axboe0969e782019-12-17 18:40:57 -07004571 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004572 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07004573 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004574 spin_unlock_irq(&ctx->completion_lock);
4575
Jens Axboe78e19bb2019-11-06 15:21:34 -07004576 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004577 if (ret < 0)
4578 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004579 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004580 return 0;
4581}
4582
Jens Axboe221c5eb2019-01-17 09:41:58 -07004583static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4584 void *key)
4585{
Jens Axboec2f2eb72020-02-10 09:07:05 -07004586 struct io_kiocb *req = wait->private;
4587 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004588
Jens Axboed7718a92020-02-14 22:23:12 -07004589 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004590}
4591
Jens Axboe221c5eb2019-01-17 09:41:58 -07004592static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
4593 struct poll_table_struct *p)
4594{
4595 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4596
Jens Axboed7718a92020-02-14 22:23:12 -07004597 __io_queue_proc(&pt->req->poll, pt, head);
Jens Axboeeac406c2019-11-14 12:09:58 -07004598}
4599
Jens Axboe3529d8c2019-12-19 18:24:38 -07004600static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004601{
4602 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004603 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004604
4605 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4606 return -EINVAL;
4607 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
4608 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06004609 if (!poll->file)
4610 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004611
Jens Axboe221c5eb2019-01-17 09:41:58 -07004612 events = READ_ONCE(sqe->poll_events);
4613 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboeb41e9852020-02-17 09:52:41 -07004614
Jens Axboe3537b6a2020-04-03 11:19:06 -06004615 get_task_struct(current);
Jens Axboeb41e9852020-02-17 09:52:41 -07004616 req->task = current;
Jens Axboe0969e782019-12-17 18:40:57 -07004617 return 0;
4618}
4619
Pavel Begunkov014db002020-03-03 21:33:12 +03004620static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07004621{
4622 struct io_poll_iocb *poll = &req->poll;
4623 struct io_ring_ctx *ctx = req->ctx;
4624 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07004625 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07004626
Jens Axboe78076bb2019-12-04 19:56:40 -07004627 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe36703242019-07-25 10:20:18 -06004628 INIT_LIST_HEAD(&req->list);
Jens Axboed7718a92020-02-14 22:23:12 -07004629 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06004630
Jens Axboed7718a92020-02-14 22:23:12 -07004631 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
4632 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004633
Jens Axboe8c838782019-03-12 15:48:16 -06004634 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06004635 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004636 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06004637 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07004638 spin_unlock_irq(&ctx->completion_lock);
4639
Jens Axboe8c838782019-03-12 15:48:16 -06004640 if (mask) {
4641 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03004642 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004643 }
Jens Axboe8c838782019-03-12 15:48:16 -06004644 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004645}
4646
Jens Axboe5262f562019-09-17 12:26:57 -06004647static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
4648{
Jens Axboead8a48a2019-11-15 08:49:11 -07004649 struct io_timeout_data *data = container_of(timer,
4650 struct io_timeout_data, timer);
4651 struct io_kiocb *req = data->req;
4652 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06004653 unsigned long flags;
4654
Jens Axboe5262f562019-09-17 12:26:57 -06004655 atomic_inc(&ctx->cq_timeouts);
4656
4657 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08004658 /*
Jens Axboe11365042019-10-16 09:08:32 -06004659 * We could be racing with timeout deletion. If the list is empty,
4660 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08004661 */
Jens Axboe842f9612019-10-29 12:34:10 -06004662 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06004663 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06004664
Jens Axboe11365042019-10-16 09:08:32 -06004665 /*
4666 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08004667 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06004668 * pointer will be increased, otherwise other timeout reqs may
4669 * return in advance without waiting for enough wait_nr.
4670 */
4671 prev = req;
4672 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
4673 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06004674 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06004675 }
Jens Axboe842f9612019-10-29 12:34:10 -06004676
Jens Axboe78e19bb2019-11-06 15:21:34 -07004677 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06004678 io_commit_cqring(ctx);
4679 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4680
4681 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004682 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004683 io_put_req(req);
4684 return HRTIMER_NORESTART;
4685}
4686
Jens Axboe47f46762019-11-09 17:43:02 -07004687static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
4688{
4689 struct io_kiocb *req;
4690 int ret = -ENOENT;
4691
4692 list_for_each_entry(req, &ctx->timeout_list, list) {
4693 if (user_data == req->user_data) {
4694 list_del_init(&req->list);
4695 ret = 0;
4696 break;
4697 }
4698 }
4699
4700 if (ret == -ENOENT)
4701 return ret;
4702
Jens Axboe2d283902019-12-04 11:08:05 -07004703 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07004704 if (ret == -1)
4705 return -EALREADY;
4706
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004707 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004708 io_cqring_fill_event(req, -ECANCELED);
4709 io_put_req(req);
4710 return 0;
4711}
4712
Jens Axboe3529d8c2019-12-19 18:24:38 -07004713static int io_timeout_remove_prep(struct io_kiocb *req,
4714 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07004715{
Jens Axboeb29472e2019-12-17 18:50:29 -07004716 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4717 return -EINVAL;
4718 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
4719 return -EINVAL;
4720
4721 req->timeout.addr = READ_ONCE(sqe->addr);
4722 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
4723 if (req->timeout.flags)
4724 return -EINVAL;
4725
Jens Axboeb29472e2019-12-17 18:50:29 -07004726 return 0;
4727}
4728
Jens Axboe11365042019-10-16 09:08:32 -06004729/*
4730 * Remove or update an existing timeout command
4731 */
Jens Axboefc4df992019-12-10 14:38:45 -07004732static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06004733{
4734 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07004735 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06004736
Jens Axboe11365042019-10-16 09:08:32 -06004737 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07004738 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06004739
Jens Axboe47f46762019-11-09 17:43:02 -07004740 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06004741 io_commit_cqring(ctx);
4742 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004743 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004744 if (ret < 0)
4745 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08004746 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06004747 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06004748}
4749
Jens Axboe3529d8c2019-12-19 18:24:38 -07004750static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07004751 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06004752{
Jens Axboead8a48a2019-11-15 08:49:11 -07004753 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06004754 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06004755
Jens Axboead8a48a2019-11-15 08:49:11 -07004756 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06004757 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07004758 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06004759 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07004760 if (sqe->off && is_timeout_link)
4761 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06004762 flags = READ_ONCE(sqe->timeout_flags);
4763 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06004764 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06004765
Jens Axboe26a61672019-12-20 09:02:01 -07004766 req->timeout.count = READ_ONCE(sqe->off);
4767
Jens Axboe3529d8c2019-12-19 18:24:38 -07004768 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07004769 return -ENOMEM;
4770
4771 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07004772 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07004773 req->flags |= REQ_F_TIMEOUT;
4774
4775 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06004776 return -EFAULT;
4777
Jens Axboe11365042019-10-16 09:08:32 -06004778 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07004779 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06004780 else
Jens Axboead8a48a2019-11-15 08:49:11 -07004781 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06004782
Jens Axboead8a48a2019-11-15 08:49:11 -07004783 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
4784 return 0;
4785}
4786
Jens Axboefc4df992019-12-10 14:38:45 -07004787static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07004788{
Jens Axboead8a48a2019-11-15 08:49:11 -07004789 struct io_ring_ctx *ctx = req->ctx;
4790 struct io_timeout_data *data;
4791 struct list_head *entry;
4792 unsigned span = 0;
Pavel Begunkovb55ce732020-04-15 00:39:49 +03004793 u32 count = req->timeout.count;
Pavel Begunkov22cad152020-04-15 00:39:48 +03004794 u32 seq = req->sequence;
Jens Axboead8a48a2019-11-15 08:49:11 -07004795
Jens Axboe2d283902019-12-04 11:08:05 -07004796 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004797
Jens Axboe5262f562019-09-17 12:26:57 -06004798 /*
4799 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07004800 * timeout event to be satisfied. If it isn't set, then this is
4801 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06004802 */
Jens Axboe93bd25b2019-11-11 23:34:31 -07004803 if (!count) {
4804 req->flags |= REQ_F_TIMEOUT_NOSEQ;
4805 spin_lock_irq(&ctx->completion_lock);
4806 entry = ctx->timeout_list.prev;
4807 goto add;
4808 }
Jens Axboe5262f562019-09-17 12:26:57 -06004809
Pavel Begunkov22cad152020-04-15 00:39:48 +03004810 req->sequence = seq + count;
Jens Axboe5262f562019-09-17 12:26:57 -06004811
4812 /*
4813 * Insertion sort, ensuring the first entry in the list is always
4814 * the one we need first.
4815 */
Jens Axboe5262f562019-09-17 12:26:57 -06004816 spin_lock_irq(&ctx->completion_lock);
4817 list_for_each_prev(entry, &ctx->timeout_list) {
4818 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
Pavel Begunkov22cad152020-04-15 00:39:48 +03004819 unsigned nxt_seq;
yangerkun5da0fb12019-10-15 21:59:29 +08004820 long long tmp, tmp_nxt;
Pavel Begunkovb55ce732020-04-15 00:39:49 +03004821 u32 nxt_offset = nxt->timeout.count;
Jens Axboe5262f562019-09-17 12:26:57 -06004822
Jens Axboe93bd25b2019-11-11 23:34:31 -07004823 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
4824 continue;
4825
yangerkun5da0fb12019-10-15 21:59:29 +08004826 /*
Pavel Begunkov22cad152020-04-15 00:39:48 +03004827 * Since seq + count can overflow, use type long
yangerkun5da0fb12019-10-15 21:59:29 +08004828 * long to store it.
4829 */
Pavel Begunkov22cad152020-04-15 00:39:48 +03004830 tmp = (long long)seq + count;
4831 nxt_seq = nxt->sequence - nxt_offset;
4832 tmp_nxt = (long long)nxt_seq + nxt_offset;
yangerkun5da0fb12019-10-15 21:59:29 +08004833
4834 /*
4835 * cached_sq_head may overflow, and it will never overflow twice
4836 * once there is some timeout req still be valid.
4837 */
Pavel Begunkov22cad152020-04-15 00:39:48 +03004838 if (seq < nxt_seq)
yangerkun8b07a652019-10-17 12:12:35 +08004839 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08004840
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004841 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06004842 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004843
4844 /*
4845 * Sequence of reqs after the insert one and itself should
4846 * be adjusted because each timeout req consumes a slot.
4847 */
4848 span++;
4849 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06004850 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004851 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004852add:
Jens Axboe5262f562019-09-17 12:26:57 -06004853 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07004854 data->timer.function = io_timeout_fn;
4855 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06004856 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004857 return 0;
4858}
4859
Jens Axboe62755e32019-10-28 21:49:21 -06004860static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06004861{
Jens Axboe62755e32019-10-28 21:49:21 -06004862 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06004863
Jens Axboe62755e32019-10-28 21:49:21 -06004864 return req->user_data == (unsigned long) data;
4865}
4866
Jens Axboee977d6d2019-11-05 12:39:45 -07004867static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06004868{
Jens Axboe62755e32019-10-28 21:49:21 -06004869 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06004870 int ret = 0;
4871
Jens Axboe62755e32019-10-28 21:49:21 -06004872 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
4873 switch (cancel_ret) {
4874 case IO_WQ_CANCEL_OK:
4875 ret = 0;
4876 break;
4877 case IO_WQ_CANCEL_RUNNING:
4878 ret = -EALREADY;
4879 break;
4880 case IO_WQ_CANCEL_NOTFOUND:
4881 ret = -ENOENT;
4882 break;
4883 }
4884
Jens Axboee977d6d2019-11-05 12:39:45 -07004885 return ret;
4886}
4887
Jens Axboe47f46762019-11-09 17:43:02 -07004888static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
4889 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03004890 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07004891{
4892 unsigned long flags;
4893 int ret;
4894
4895 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
4896 if (ret != -ENOENT) {
4897 spin_lock_irqsave(&ctx->completion_lock, flags);
4898 goto done;
4899 }
4900
4901 spin_lock_irqsave(&ctx->completion_lock, flags);
4902 ret = io_timeout_cancel(ctx, sqe_addr);
4903 if (ret != -ENOENT)
4904 goto done;
4905 ret = io_poll_cancel(ctx, sqe_addr);
4906done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004907 if (!ret)
4908 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07004909 io_cqring_fill_event(req, ret);
4910 io_commit_cqring(ctx);
4911 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4912 io_cqring_ev_posted(ctx);
4913
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004914 if (ret < 0)
4915 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03004916 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004917}
4918
Jens Axboe3529d8c2019-12-19 18:24:38 -07004919static int io_async_cancel_prep(struct io_kiocb *req,
4920 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07004921{
Jens Axboefbf23842019-12-17 18:45:56 -07004922 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07004923 return -EINVAL;
4924 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
4925 sqe->cancel_flags)
4926 return -EINVAL;
4927
Jens Axboefbf23842019-12-17 18:45:56 -07004928 req->cancel.addr = READ_ONCE(sqe->addr);
4929 return 0;
4930}
4931
Pavel Begunkov014db002020-03-03 21:33:12 +03004932static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07004933{
4934 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07004935
Pavel Begunkov014db002020-03-03 21:33:12 +03004936 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06004937 return 0;
4938}
4939
Jens Axboe05f3fb32019-12-09 11:22:50 -07004940static int io_files_update_prep(struct io_kiocb *req,
4941 const struct io_uring_sqe *sqe)
4942{
4943 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
4944 return -EINVAL;
4945
4946 req->files_update.offset = READ_ONCE(sqe->off);
4947 req->files_update.nr_args = READ_ONCE(sqe->len);
4948 if (!req->files_update.nr_args)
4949 return -EINVAL;
4950 req->files_update.arg = READ_ONCE(sqe->addr);
4951 return 0;
4952}
4953
4954static int io_files_update(struct io_kiocb *req, bool force_nonblock)
4955{
4956 struct io_ring_ctx *ctx = req->ctx;
4957 struct io_uring_files_update up;
4958 int ret;
4959
Jens Axboef86cd202020-01-29 13:46:44 -07004960 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004961 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004962
4963 up.offset = req->files_update.offset;
4964 up.fds = req->files_update.arg;
4965
4966 mutex_lock(&ctx->uring_lock);
4967 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
4968 mutex_unlock(&ctx->uring_lock);
4969
4970 if (ret < 0)
4971 req_set_fail_links(req);
4972 io_cqring_add_event(req, ret);
4973 io_put_req(req);
4974 return 0;
4975}
4976
Jens Axboe3529d8c2019-12-19 18:24:38 -07004977static int io_req_defer_prep(struct io_kiocb *req,
4978 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07004979{
Jens Axboee7815732019-12-17 19:45:06 -07004980 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07004981
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03004982 if (!sqe)
4983 return 0;
4984
Jens Axboef86cd202020-01-29 13:46:44 -07004985 if (io_op_defs[req->opcode].file_table) {
4986 ret = io_grab_files(req);
4987 if (unlikely(ret))
4988 return ret;
4989 }
4990
Jens Axboecccf0ee2020-01-27 16:34:48 -07004991 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
4992
Jens Axboed625c6e2019-12-17 19:53:05 -07004993 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07004994 case IORING_OP_NOP:
4995 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004996 case IORING_OP_READV:
4997 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004998 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004999 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005000 break;
5001 case IORING_OP_WRITEV:
5002 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005003 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005004 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005005 break;
Jens Axboe0969e782019-12-17 18:40:57 -07005006 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005007 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005008 break;
5009 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005010 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005011 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005012 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005013 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005014 break;
5015 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005016 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005017 break;
Jens Axboe03b12302019-12-02 18:50:25 -07005018 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005019 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005020 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005021 break;
5022 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005023 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005024 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005025 break;
Jens Axboef499a022019-12-02 16:28:46 -07005026 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005027 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005028 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005029 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005030 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005031 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07005032 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005033 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07005034 break;
Jens Axboefbf23842019-12-17 18:45:56 -07005035 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005036 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005037 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005038 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005039 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005040 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005041 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005042 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005043 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005044 case IORING_OP_FALLOCATE:
5045 ret = io_fallocate_prep(req, sqe);
5046 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005047 case IORING_OP_OPENAT:
5048 ret = io_openat_prep(req, sqe);
5049 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005050 case IORING_OP_CLOSE:
5051 ret = io_close_prep(req, sqe);
5052 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005053 case IORING_OP_FILES_UPDATE:
5054 ret = io_files_update_prep(req, sqe);
5055 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005056 case IORING_OP_STATX:
5057 ret = io_statx_prep(req, sqe);
5058 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005059 case IORING_OP_FADVISE:
5060 ret = io_fadvise_prep(req, sqe);
5061 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005062 case IORING_OP_MADVISE:
5063 ret = io_madvise_prep(req, sqe);
5064 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005065 case IORING_OP_OPENAT2:
5066 ret = io_openat2_prep(req, sqe);
5067 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005068 case IORING_OP_EPOLL_CTL:
5069 ret = io_epoll_ctl_prep(req, sqe);
5070 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005071 case IORING_OP_SPLICE:
5072 ret = io_splice_prep(req, sqe);
5073 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005074 case IORING_OP_PROVIDE_BUFFERS:
5075 ret = io_provide_buffers_prep(req, sqe);
5076 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005077 case IORING_OP_REMOVE_BUFFERS:
5078 ret = io_remove_buffers_prep(req, sqe);
5079 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005080 default:
Jens Axboee7815732019-12-17 19:45:06 -07005081 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5082 req->opcode);
5083 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005084 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005085 }
5086
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005087 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07005088}
5089
Jens Axboe3529d8c2019-12-19 18:24:38 -07005090static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06005091{
Jackie Liua197f662019-11-08 08:09:12 -07005092 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07005093 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06005094
Bob Liu9d858b22019-11-13 18:06:25 +08005095 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov4ee36312020-05-01 17:09:37 +03005096 if (!req_need_defer(req) && list_empty_careful(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06005097 return 0;
5098
Jens Axboe3529d8c2019-12-19 18:24:38 -07005099 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06005100 return -EAGAIN;
5101
Jens Axboe3529d8c2019-12-19 18:24:38 -07005102 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005103 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07005104 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07005105
Jens Axboede0617e2019-04-06 21:51:27 -06005106 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08005107 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06005108 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06005109 return 0;
5110 }
5111
Jens Axboe915967f2019-11-21 09:01:20 -07005112 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06005113 list_add_tail(&req->list, &ctx->defer_list);
5114 spin_unlock_irq(&ctx->completion_lock);
5115 return -EIOCBQUEUED;
5116}
5117
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005118static void io_cleanup_req(struct io_kiocb *req)
5119{
5120 struct io_async_ctx *io = req->io;
5121
5122 switch (req->opcode) {
5123 case IORING_OP_READV:
5124 case IORING_OP_READ_FIXED:
5125 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005126 if (req->flags & REQ_F_BUFFER_SELECTED)
5127 kfree((void *)(unsigned long)req->rw.addr);
5128 /* fallthrough */
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005129 case IORING_OP_WRITEV:
5130 case IORING_OP_WRITE_FIXED:
5131 case IORING_OP_WRITE:
5132 if (io->rw.iov != io->rw.fast_iov)
5133 kfree(io->rw.iov);
5134 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005135 case IORING_OP_RECVMSG:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005136 if (req->flags & REQ_F_BUFFER_SELECTED)
5137 kfree(req->sr_msg.kbuf);
5138 /* fallthrough */
5139 case IORING_OP_SENDMSG:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005140 if (io->msg.iov != io->msg.fast_iov)
5141 kfree(io->msg.iov);
5142 break;
Jens Axboebcda7ba2020-02-23 16:42:51 -07005143 case IORING_OP_RECV:
5144 if (req->flags & REQ_F_BUFFER_SELECTED)
5145 kfree(req->sr_msg.kbuf);
5146 break;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03005147 case IORING_OP_OPENAT:
5148 case IORING_OP_OPENAT2:
5149 case IORING_OP_STATX:
5150 putname(req->open.filename);
5151 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005152 case IORING_OP_SPLICE:
5153 io_put_file(req, req->splice.file_in,
5154 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5155 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005156 }
5157
5158 req->flags &= ~REQ_F_NEED_CLEANUP;
5159}
5160
Jens Axboe3529d8c2019-12-19 18:24:38 -07005161static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Pavel Begunkov014db002020-03-03 21:33:12 +03005162 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005163{
Jackie Liua197f662019-11-08 08:09:12 -07005164 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005165 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005166
Jens Axboed625c6e2019-12-17 19:53:05 -07005167 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005168 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005169 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005170 break;
5171 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005172 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005173 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005174 if (sqe) {
5175 ret = io_read_prep(req, sqe, force_nonblock);
5176 if (ret < 0)
5177 break;
5178 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005179 ret = io_read(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005180 break;
5181 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07005182 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005183 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005184 if (sqe) {
5185 ret = io_write_prep(req, sqe, force_nonblock);
5186 if (ret < 0)
5187 break;
5188 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005189 ret = io_write(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005190 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005191 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005192 if (sqe) {
5193 ret = io_prep_fsync(req, sqe);
5194 if (ret < 0)
5195 break;
5196 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005197 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005198 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005199 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005200 if (sqe) {
5201 ret = io_poll_add_prep(req, sqe);
5202 if (ret)
5203 break;
5204 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005205 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005206 break;
5207 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005208 if (sqe) {
5209 ret = io_poll_remove_prep(req, sqe);
5210 if (ret < 0)
5211 break;
5212 }
Jens Axboefc4df992019-12-10 14:38:45 -07005213 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005214 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005215 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005216 if (sqe) {
5217 ret = io_prep_sfr(req, sqe);
5218 if (ret < 0)
5219 break;
5220 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005221 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005222 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005223 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005224 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005225 if (sqe) {
5226 ret = io_sendmsg_prep(req, sqe);
5227 if (ret < 0)
5228 break;
5229 }
Jens Axboefddafac2020-01-04 20:19:44 -07005230 if (req->opcode == IORING_OP_SENDMSG)
Pavel Begunkov014db002020-03-03 21:33:12 +03005231 ret = io_sendmsg(req, force_nonblock);
Jens Axboefddafac2020-01-04 20:19:44 -07005232 else
Pavel Begunkov014db002020-03-03 21:33:12 +03005233 ret = io_send(req, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005234 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06005235 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005236 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005237 if (sqe) {
5238 ret = io_recvmsg_prep(req, sqe);
5239 if (ret)
5240 break;
5241 }
Jens Axboefddafac2020-01-04 20:19:44 -07005242 if (req->opcode == IORING_OP_RECVMSG)
Pavel Begunkov014db002020-03-03 21:33:12 +03005243 ret = io_recvmsg(req, force_nonblock);
Jens Axboefddafac2020-01-04 20:19:44 -07005244 else
Pavel Begunkov014db002020-03-03 21:33:12 +03005245 ret = io_recv(req, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06005246 break;
Jens Axboe5262f562019-09-17 12:26:57 -06005247 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005248 if (sqe) {
5249 ret = io_timeout_prep(req, sqe, false);
5250 if (ret)
5251 break;
5252 }
Jens Axboefc4df992019-12-10 14:38:45 -07005253 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005254 break;
Jens Axboe11365042019-10-16 09:08:32 -06005255 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005256 if (sqe) {
5257 ret = io_timeout_remove_prep(req, sqe);
5258 if (ret)
5259 break;
5260 }
Jens Axboefc4df992019-12-10 14:38:45 -07005261 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06005262 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06005263 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005264 if (sqe) {
5265 ret = io_accept_prep(req, sqe);
5266 if (ret)
5267 break;
5268 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005269 ret = io_accept(req, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005270 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005271 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005272 if (sqe) {
5273 ret = io_connect_prep(req, sqe);
5274 if (ret)
5275 break;
5276 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005277 ret = io_connect(req, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005278 break;
Jens Axboe62755e32019-10-28 21:49:21 -06005279 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005280 if (sqe) {
5281 ret = io_async_cancel_prep(req, sqe);
5282 if (ret)
5283 break;
5284 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005285 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005286 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005287 case IORING_OP_FALLOCATE:
5288 if (sqe) {
5289 ret = io_fallocate_prep(req, sqe);
5290 if (ret)
5291 break;
5292 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005293 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005294 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005295 case IORING_OP_OPENAT:
5296 if (sqe) {
5297 ret = io_openat_prep(req, sqe);
5298 if (ret)
5299 break;
5300 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005301 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005302 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005303 case IORING_OP_CLOSE:
5304 if (sqe) {
5305 ret = io_close_prep(req, sqe);
5306 if (ret)
5307 break;
5308 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005309 ret = io_close(req, force_nonblock);
Jens Axboeb5dba592019-12-11 14:02:38 -07005310 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005311 case IORING_OP_FILES_UPDATE:
5312 if (sqe) {
5313 ret = io_files_update_prep(req, sqe);
5314 if (ret)
5315 break;
5316 }
5317 ret = io_files_update(req, force_nonblock);
5318 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005319 case IORING_OP_STATX:
5320 if (sqe) {
5321 ret = io_statx_prep(req, sqe);
5322 if (ret)
5323 break;
5324 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005325 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005326 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005327 case IORING_OP_FADVISE:
5328 if (sqe) {
5329 ret = io_fadvise_prep(req, sqe);
5330 if (ret)
5331 break;
5332 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005333 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005334 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005335 case IORING_OP_MADVISE:
5336 if (sqe) {
5337 ret = io_madvise_prep(req, sqe);
5338 if (ret)
5339 break;
5340 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005341 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005342 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005343 case IORING_OP_OPENAT2:
5344 if (sqe) {
5345 ret = io_openat2_prep(req, sqe);
5346 if (ret)
5347 break;
5348 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005349 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005350 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005351 case IORING_OP_EPOLL_CTL:
5352 if (sqe) {
5353 ret = io_epoll_ctl_prep(req, sqe);
5354 if (ret)
5355 break;
5356 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005357 ret = io_epoll_ctl(req, force_nonblock);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005358 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005359 case IORING_OP_SPLICE:
5360 if (sqe) {
5361 ret = io_splice_prep(req, sqe);
5362 if (ret < 0)
5363 break;
5364 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005365 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005366 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005367 case IORING_OP_PROVIDE_BUFFERS:
5368 if (sqe) {
5369 ret = io_provide_buffers_prep(req, sqe);
5370 if (ret)
5371 break;
5372 }
5373 ret = io_provide_buffers(req, force_nonblock);
5374 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005375 case IORING_OP_REMOVE_BUFFERS:
5376 if (sqe) {
5377 ret = io_remove_buffers_prep(req, sqe);
5378 if (ret)
5379 break;
5380 }
5381 ret = io_remove_buffers(req, force_nonblock);
Jens Axboe31b51512019-01-18 22:56:34 -07005382 break;
5383 default:
5384 ret = -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07005385 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005386 }
5387
Jens Axboe31b51512019-01-18 22:56:34 -07005388 if (ret)
5389 return ret;
5390
5391 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07005392 const bool in_async = io_wq_current_is_worker();
5393
Jens Axboe9e645e112019-05-10 16:07:28 -06005394 if (req->result == -EAGAIN)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005395 return -EAGAIN;
5396
Jens Axboe11ba8202020-01-15 21:51:17 -07005397 /* workqueue context doesn't hold uring_lock, grab it now */
5398 if (in_async)
5399 mutex_lock(&ctx->uring_lock);
5400
Jens Axboe2b188cc2019-01-07 10:46:33 -07005401 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07005402
5403 if (in_async)
5404 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005405 }
5406
5407 return 0;
5408}
5409
Jens Axboe561fb042019-10-24 07:25:42 -06005410static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005411{
Jens Axboe561fb042019-10-24 07:25:42 -06005412 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005413 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06005414 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005415
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005416 /* if NO_CANCEL is set, we must still run the work */
5417 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
5418 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06005419 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005420 }
Jens Axboe31b51512019-01-18 22:56:34 -07005421
Jens Axboe561fb042019-10-24 07:25:42 -06005422 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06005423 do {
Pavel Begunkov014db002020-03-03 21:33:12 +03005424 ret = io_issue_sqe(req, NULL, false);
Jens Axboe561fb042019-10-24 07:25:42 -06005425 /*
5426 * We can get EAGAIN for polled IO even though we're
5427 * forcing a sync submission from here, since we can't
5428 * wait for request slots on the block side.
5429 */
5430 if (ret != -EAGAIN)
5431 break;
5432 cond_resched();
5433 } while (1);
5434 }
Jens Axboe31b51512019-01-18 22:56:34 -07005435
Jens Axboe561fb042019-10-24 07:25:42 -06005436 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005437 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07005438 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06005439 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07005440 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005441
Pavel Begunkove9fd9392020-03-04 16:14:12 +03005442 io_steal_work(req, workptr);
Jens Axboe31b51512019-01-18 22:56:34 -07005443}
Jens Axboe2b188cc2019-01-07 10:46:33 -07005444
Jens Axboe65e19f52019-10-26 07:20:21 -06005445static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
5446 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06005447{
Jens Axboe65e19f52019-10-26 07:20:21 -06005448 struct fixed_file_table *table;
5449
Jens Axboe05f3fb32019-12-09 11:22:50 -07005450 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08005451 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06005452}
5453
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005454static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
5455 int fd, struct file **out_file, bool fixed)
5456{
5457 struct io_ring_ctx *ctx = req->ctx;
5458 struct file *file;
5459
5460 if (fixed) {
5461 if (unlikely(!ctx->file_data ||
5462 (unsigned) fd >= ctx->nr_user_files))
5463 return -EBADF;
5464 fd = array_index_nospec(fd, ctx->nr_user_files);
5465 file = io_file_from_index(ctx, fd);
5466 if (!file)
5467 return -EBADF;
Xiaoguang Wang05589552020-03-31 14:05:18 +08005468 req->fixed_file_refs = ctx->file_data->cur_refs;
5469 percpu_ref_get(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005470 } else {
5471 trace_io_uring_file_get(ctx, fd);
5472 file = __io_file_get(state, fd);
5473 if (unlikely(!file))
5474 return -EBADF;
5475 }
5476
5477 *out_file = file;
5478 return 0;
5479}
5480
Jens Axboe3529d8c2019-12-19 18:24:38 -07005481static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Jens Axboe63ff8222020-05-07 14:56:15 -06005482 int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06005483{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005484 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06005485
Jens Axboe63ff8222020-05-07 14:56:15 -06005486 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005487 if (unlikely(!fixed && req->needs_fixed_file))
5488 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06005489
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005490 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06005491}
5492
Jackie Liua197f662019-11-08 08:09:12 -07005493static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005494{
Jens Axboefcb323c2019-10-24 12:39:47 -06005495 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07005496 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06005497
Jens Axboe5b0bbee2020-04-27 10:41:22 -06005498 if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE))
Jens Axboef86cd202020-01-29 13:46:44 -07005499 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005500 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07005501 return -EBADF;
5502
Jens Axboefcb323c2019-10-24 12:39:47 -06005503 rcu_read_lock();
5504 spin_lock_irq(&ctx->inflight_lock);
5505 /*
5506 * We use the f_ops->flush() handler to ensure that we can flush
5507 * out work accessing these files if the fd is closed. Check if
5508 * the fd has changed since we started down this path, and disallow
5509 * this operation if it has.
5510 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005511 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06005512 list_add(&req->inflight_entry, &ctx->inflight_list);
5513 req->flags |= REQ_F_INFLIGHT;
5514 req->work.files = current->files;
5515 ret = 0;
5516 }
5517 spin_unlock_irq(&ctx->inflight_lock);
5518 rcu_read_unlock();
5519
5520 return ret;
5521}
5522
Jens Axboe2665abf2019-11-05 12:40:47 -07005523static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
5524{
Jens Axboead8a48a2019-11-15 08:49:11 -07005525 struct io_timeout_data *data = container_of(timer,
5526 struct io_timeout_data, timer);
5527 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07005528 struct io_ring_ctx *ctx = req->ctx;
5529 struct io_kiocb *prev = NULL;
5530 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07005531
5532 spin_lock_irqsave(&ctx->completion_lock, flags);
5533
5534 /*
5535 * We don't expect the list to be empty, that will only happen if we
5536 * race with the completion of the linked work.
5537 */
Pavel Begunkov44932332019-12-05 16:16:35 +03005538 if (!list_empty(&req->link_list)) {
5539 prev = list_entry(req->link_list.prev, struct io_kiocb,
5540 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005541 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03005542 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005543 prev->flags &= ~REQ_F_LINK_TIMEOUT;
5544 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07005545 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005546 }
5547
5548 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5549
5550 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005551 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03005552 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07005553 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07005554 } else {
5555 io_cqring_add_event(req, -ETIME);
5556 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005557 }
Jens Axboe2665abf2019-11-05 12:40:47 -07005558 return HRTIMER_NORESTART;
5559}
5560
Jens Axboead8a48a2019-11-15 08:49:11 -07005561static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005562{
Jens Axboe76a46e02019-11-10 23:34:16 -07005563 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07005564
Jens Axboe76a46e02019-11-10 23:34:16 -07005565 /*
5566 * If the list is now empty, then our linked request finished before
5567 * we got a chance to setup the timer
5568 */
5569 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03005570 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07005571 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07005572
Jens Axboead8a48a2019-11-15 08:49:11 -07005573 data->timer.function = io_link_timeout_fn;
5574 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
5575 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07005576 }
Jens Axboe76a46e02019-11-10 23:34:16 -07005577 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07005578
Jens Axboe2665abf2019-11-05 12:40:47 -07005579 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07005580 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005581}
5582
Jens Axboead8a48a2019-11-15 08:49:11 -07005583static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005584{
5585 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005586
Pavel Begunkovdea3b492020-04-12 02:05:04 +03005587 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07005588 return NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005589 /* for polled retry, if flag is set, we already went through here */
5590 if (req->flags & REQ_F_POLLED)
5591 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005592
Pavel Begunkov44932332019-12-05 16:16:35 +03005593 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
5594 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07005595 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07005596 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005597
Jens Axboe76a46e02019-11-10 23:34:16 -07005598 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07005599 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07005600}
5601
Jens Axboe3529d8c2019-12-19 18:24:38 -07005602static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005603{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005604 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005605 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07005606 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005607 int ret;
5608
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005609again:
5610 linked_timeout = io_prep_linked_timeout(req);
5611
Jens Axboe193155c2020-02-22 23:22:19 -07005612 if (req->work.creds && req->work.creds != current_cred()) {
5613 if (old_creds)
5614 revert_creds(old_creds);
5615 if (old_creds == req->work.creds)
5616 old_creds = NULL; /* restored original creds */
5617 else
5618 old_creds = override_creds(req->work.creds);
5619 }
5620
Pavel Begunkov014db002020-03-03 21:33:12 +03005621 ret = io_issue_sqe(req, sqe, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06005622
5623 /*
5624 * We async punt it if the file wasn't marked NOWAIT, or if the file
5625 * doesn't support non-blocking read/write attempts
5626 */
5627 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
5628 (req->flags & REQ_F_MUST_PUNT))) {
Jens Axboed7718a92020-02-14 22:23:12 -07005629 if (io_arm_poll_handler(req)) {
5630 if (linked_timeout)
5631 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005632 goto exit;
Jens Axboed7718a92020-02-14 22:23:12 -07005633 }
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005634punt:
Jens Axboef86cd202020-01-29 13:46:44 -07005635 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005636 ret = io_grab_files(req);
5637 if (ret)
5638 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005639 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005640
5641 /*
5642 * Queued up for async execution, worker will release
5643 * submit reference when the iocb is actually submitted.
5644 */
5645 io_queue_async_work(req);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005646 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005647 }
Jens Axboee65ef562019-03-12 10:16:44 -06005648
Jens Axboefcb323c2019-10-24 12:39:47 -06005649err:
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005650 nxt = NULL;
Jens Axboee65ef562019-03-12 10:16:44 -06005651 /* drop submission reference */
Jens Axboe2a44f462020-02-25 13:25:41 -07005652 io_put_req_find_next(req, &nxt);
Jens Axboee65ef562019-03-12 10:16:44 -06005653
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005654 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07005655 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005656 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005657 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005658 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005659 }
5660
Jens Axboee65ef562019-03-12 10:16:44 -06005661 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06005662 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07005663 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005664 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06005665 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06005666 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005667 if (nxt) {
5668 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005669
5670 if (req->flags & REQ_F_FORCE_ASYNC)
5671 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005672 goto again;
5673 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005674exit:
Jens Axboe193155c2020-02-22 23:22:19 -07005675 if (old_creds)
5676 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005677}
5678
Jens Axboe3529d8c2019-12-19 18:24:38 -07005679static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005680{
5681 int ret;
5682
Jens Axboe3529d8c2019-12-19 18:24:38 -07005683 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005684 if (ret) {
5685 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03005686fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005687 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005688 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07005689 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005690 }
Pavel Begunkov25508782019-12-30 21:24:47 +03005691 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkov11185912020-01-22 23:09:35 +03005692 ret = io_req_defer_prep(req, sqe);
5693 if (unlikely(ret < 0))
5694 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07005695 /*
5696 * Never try inline submit of IOSQE_ASYNC is set, go straight
5697 * to async execution.
5698 */
5699 req->work.flags |= IO_WQ_WORK_CONCURRENT;
5700 io_queue_async_work(req);
5701 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005702 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07005703 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08005704}
5705
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005706static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005707{
Jens Axboe94ae5e72019-11-14 19:39:52 -07005708 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005709 io_cqring_add_event(req, -ECANCELED);
5710 io_double_put_req(req);
5711 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07005712 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005713}
5714
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005715static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Xiaoguang Wang7d01bd72020-05-08 21:19:30 +08005716 struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06005717{
Jackie Liua197f662019-11-08 08:09:12 -07005718 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005719 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06005720
Jens Axboe9e645e112019-05-10 16:07:28 -06005721 /*
5722 * If we already have a head request, queue this one for async
5723 * submittal once the head completes. If we don't have a head but
5724 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
5725 * submitted sync once the chain is complete. If none of those
5726 * conditions are true (normal request), then just queue it.
5727 */
5728 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03005729 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06005730
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03005731 /*
5732 * Taking sequential execution of a link, draining both sides
5733 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
5734 * requests in the link. So, it drains the head and the
5735 * next after the link request. The last one is done via
5736 * drain_next flag to persist the effect across calls.
5737 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005738 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03005739 head->flags |= REQ_F_IO_DRAIN;
5740 ctx->drain_next = 1;
5741 }
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005742 if (io_alloc_async_ctx(req))
5743 return -EAGAIN;
Jens Axboe9e645e112019-05-10 16:07:28 -06005744
Jens Axboe3529d8c2019-12-19 18:24:38 -07005745 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005746 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005747 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03005748 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005749 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07005750 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03005751 trace_io_uring_link(ctx, req, head);
5752 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06005753
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005754 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005755 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005756 io_queue_link_head(head);
5757 *link = NULL;
5758 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005759 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03005760 if (unlikely(ctx->drain_next)) {
5761 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005762 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03005763 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005764 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03005765 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03005766 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03005767
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005768 if (io_alloc_async_ctx(req))
5769 return -EAGAIN;
5770
Pavel Begunkov711be032020-01-17 03:57:59 +03005771 ret = io_req_defer_prep(req, sqe);
5772 if (ret)
5773 req->flags |= REQ_F_FAIL_LINK;
5774 *link = req;
5775 } else {
5776 io_queue_sqe(req, sqe);
5777 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005778 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005779
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005780 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06005781}
5782
Jens Axboe9a56a232019-01-09 09:06:50 -07005783/*
5784 * Batched submission is done, ensure local IO is flushed out.
5785 */
5786static void io_submit_state_end(struct io_submit_state *state)
5787{
5788 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06005789 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07005790 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03005791 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07005792}
5793
5794/*
5795 * Start submission side cache.
5796 */
5797static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08005798 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07005799{
5800 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07005801 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07005802 state->file = NULL;
5803 state->ios_left = max_ios;
5804}
5805
Jens Axboe2b188cc2019-01-07 10:46:33 -07005806static void io_commit_sqring(struct io_ring_ctx *ctx)
5807{
Hristo Venev75b28af2019-08-26 17:23:46 +00005808 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005809
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03005810 /*
5811 * Ensure any loads from the SQEs are done at this point,
5812 * since once we write the new head, the application could
5813 * write new data to them.
5814 */
5815 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005816}
5817
5818/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07005819 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07005820 * that is mapped by userspace. This means that care needs to be taken to
5821 * ensure that reads are stable, as we cannot rely on userspace always
5822 * being a good citizen. If members of the sqe are validated and then later
5823 * used, it's important that those reads are done through READ_ONCE() to
5824 * prevent a re-load down the line.
5825 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03005826static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005827{
Hristo Venev75b28af2019-08-26 17:23:46 +00005828 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005829 unsigned head;
5830
5831 /*
5832 * The cached sq head (or cq tail) serves two purposes:
5833 *
5834 * 1) allows us to batch the cost of updating the user visible
5835 * head updates.
5836 * 2) allows the kernel side to track the head on its own, even
5837 * though the application is the one updating it.
5838 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005839 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03005840 if (likely(head < ctx->sq_entries))
5841 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07005842
5843 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06005844 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005845 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03005846 return NULL;
5847}
5848
5849static inline void io_consume_sqe(struct io_ring_ctx *ctx)
5850{
5851 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005852}
5853
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005854#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
5855 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
5856 IOSQE_BUFFER_SELECT)
5857
5858static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
5859 const struct io_uring_sqe *sqe,
5860 struct io_submit_state *state, bool async)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005861{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005862 unsigned int sqe_flags;
Jens Axboe63ff8222020-05-07 14:56:15 -06005863 int id;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005864
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005865 /*
5866 * All io need record the previous position, if LINK vs DARIN,
5867 * it can be used to mark the position of the first IO in the
5868 * link list.
5869 */
Pavel Begunkov31af27c2020-04-15 00:39:50 +03005870 req->sequence = ctx->cached_sq_head - ctx->cached_sq_dropped;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005871 req->opcode = READ_ONCE(sqe->opcode);
5872 req->user_data = READ_ONCE(sqe->user_data);
5873 req->io = NULL;
5874 req->file = NULL;
5875 req->ctx = ctx;
5876 req->flags = 0;
5877 /* one is dropped after submission, the other at completion */
5878 refcount_set(&req->refs, 2);
5879 req->task = NULL;
5880 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005881 req->needs_fixed_file = async;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005882 INIT_IO_WORK(&req->work, io_wq_submit_work);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005883
5884 if (unlikely(req->opcode >= IORING_OP_LAST))
5885 return -EINVAL;
5886
5887 if (io_op_defs[req->opcode].needs_mm && !current->mm) {
5888 if (unlikely(!mmget_not_zero(ctx->sqo_mm)))
5889 return -EFAULT;
5890 use_mm(ctx->sqo_mm);
5891 }
5892
5893 sqe_flags = READ_ONCE(sqe->flags);
5894 /* enforce forwards compatibility on users */
5895 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
5896 return -EINVAL;
5897
5898 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
5899 !io_op_defs[req->opcode].buffer_select)
5900 return -EOPNOTSUPP;
5901
5902 id = READ_ONCE(sqe->personality);
5903 if (id) {
5904 req->work.creds = idr_find(&ctx->personality_idr, id);
5905 if (unlikely(!req->work.creds))
5906 return -EINVAL;
5907 get_cred(req->work.creds);
5908 }
5909
5910 /* same numerical values with corresponding REQ_F_*, safe to copy */
5911 req->flags |= sqe_flags & (IOSQE_IO_DRAIN | IOSQE_IO_HARDLINK |
5912 IOSQE_ASYNC | IOSQE_FIXED_FILE |
5913 IOSQE_BUFFER_SELECT | IOSQE_IO_LINK);
5914
Jens Axboe63ff8222020-05-07 14:56:15 -06005915 if (!io_op_defs[req->opcode].needs_file)
5916 return 0;
5917
5918 return io_req_set_file(state, req, READ_ONCE(sqe->fd));
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005919}
5920
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005921static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03005922 struct file *ring_file, int ring_fd, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07005923{
5924 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005925 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005926 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005927
Jens Axboec4a2ed72019-11-21 21:01:26 -07005928 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07005929 if (test_bit(0, &ctx->sq_check_overflow)) {
5930 if (!list_empty(&ctx->cq_overflow_list) &&
5931 !io_cqring_overflow_flush(ctx, false))
5932 return -EBUSY;
5933 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005934
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005935 /* make sure SQ entry isn't read before tail */
5936 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03005937
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005938 if (!percpu_ref_tryget_many(&ctx->refs, nr))
5939 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005940
5941 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08005942 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005943 statep = &state;
5944 }
5945
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005946 ctx->ring_fd = ring_fd;
5947 ctx->ring_file = ring_file;
5948
Jens Axboe6c271ce2019-01-10 11:22:30 -07005949 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005950 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03005951 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005952 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005953
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03005954 sqe = io_get_sqe(ctx);
5955 if (unlikely(!sqe)) {
5956 io_consume_sqe(ctx);
5957 break;
5958 }
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005959 req = io_alloc_req(ctx, statep);
Pavel Begunkov196be952019-11-07 01:41:06 +03005960 if (unlikely(!req)) {
5961 if (!submitted)
5962 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005963 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06005964 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005965
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005966 err = io_init_req(ctx, req, sqe, statep, async);
Pavel Begunkov709b3022020-04-08 08:58:43 +03005967 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07005968 /* will complete beyond this point, count as submitted */
5969 submitted++;
5970
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005971 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005972fail_req:
5973 io_cqring_add_event(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07005974 io_double_put_req(req);
5975 break;
5976 }
5977
Jens Axboe354420f2020-01-08 18:55:15 -07005978 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
5979 true, async);
Xiaoguang Wang7d01bd72020-05-08 21:19:30 +08005980 err = io_submit_sqe(req, sqe, &link);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005981 if (err)
5982 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005983 }
5984
Pavel Begunkov9466f432020-01-25 22:34:01 +03005985 if (unlikely(submitted != nr)) {
5986 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
5987
5988 percpu_ref_put_many(&ctx->refs, nr - ref_used);
5989 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005990 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005991 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005992 if (statep)
5993 io_submit_state_end(&state);
5994
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005995 /* Commit SQ ring head once we've consumed and submitted all SQEs */
5996 io_commit_sqring(ctx);
5997
Jens Axboe6c271ce2019-01-10 11:22:30 -07005998 return submitted;
5999}
6000
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03006001static inline void io_sq_thread_drop_mm(struct io_ring_ctx *ctx)
6002{
6003 struct mm_struct *mm = current->mm;
6004
6005 if (mm) {
6006 unuse_mm(mm);
6007 mmput(mm);
6008 }
6009}
6010
Jens Axboe6c271ce2019-01-10 11:22:30 -07006011static int io_sq_thread(void *data)
6012{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006013 struct io_ring_ctx *ctx = data;
Jens Axboe181e4482019-11-25 08:52:30 -07006014 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006015 mm_segment_t old_fs;
6016 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006017 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006018 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006019
Jens Axboe0f158b42020-05-14 17:18:39 -06006020 complete(&ctx->sq_thread_comp);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08006021
Jens Axboe6c271ce2019-01-10 11:22:30 -07006022 old_fs = get_fs();
6023 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07006024 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006025
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006026 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006027 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006028 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006029
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006030 if (!list_empty(&ctx->poll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006031 unsigned nr_events = 0;
6032
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006033 mutex_lock(&ctx->uring_lock);
6034 if (!list_empty(&ctx->poll_list))
6035 io_iopoll_getevents(ctx, &nr_events, 0);
6036 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07006037 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006038 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006039 }
6040
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006041 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006042
6043 /*
6044 * If submit got -EBUSY, flag us as needing the application
6045 * to enter the kernel to reap and flush events.
6046 */
6047 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006048 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006049 * Drop cur_mm before scheduling, we can't hold it for
6050 * long periods (or over schedule()). Do this before
6051 * adding ourselves to the waitqueue, as the unuse/drop
6052 * may sleep.
6053 */
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03006054 io_sq_thread_drop_mm(ctx);
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006055
6056 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07006057 * We're polling. If we're within the defined idle
6058 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07006059 * to sleep. The exception is if we got EBUSY doing
6060 * more IO, we should wait for the application to
6061 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07006062 */
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006063 if (!list_empty(&ctx->poll_list) ||
Jens Axboedf069d82020-02-04 16:48:34 -07006064 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6065 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboeb41e9852020-02-17 09:52:41 -07006066 if (current->task_works)
6067 task_work_run();
Jens Axboe9831a902019-09-19 09:48:55 -06006068 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006069 continue;
6070 }
6071
Jens Axboe6c271ce2019-01-10 11:22:30 -07006072 prepare_to_wait(&ctx->sqo_wait, &wait,
6073 TASK_INTERRUPTIBLE);
6074
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006075 /*
6076 * While doing polled IO, before going to sleep, we need
6077 * to check if there are new reqs added to poll_list, it
6078 * is because reqs may have been punted to io worker and
6079 * will be added to poll_list later, hence check the
6080 * poll_list again.
6081 */
6082 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6083 !list_empty_careful(&ctx->poll_list)) {
6084 finish_wait(&ctx->sqo_wait, &wait);
6085 continue;
6086 }
6087
Jens Axboe6c271ce2019-01-10 11:22:30 -07006088 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00006089 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02006090 /* make sure to read SQ tail after writing flags */
6091 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006092
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006093 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006094 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006095 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006096 finish_wait(&ctx->sqo_wait, &wait);
6097 break;
6098 }
Jens Axboeb41e9852020-02-17 09:52:41 -07006099 if (current->task_works) {
6100 task_work_run();
Hillf Danton10bea962020-04-01 17:19:33 +08006101 finish_wait(&ctx->sqo_wait, &wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07006102 continue;
6103 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006104 if (signal_pending(current))
6105 flush_signals(current);
6106 schedule();
6107 finish_wait(&ctx->sqo_wait, &wait);
6108
Hristo Venev75b28af2019-08-26 17:23:46 +00006109 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006110 continue;
6111 }
6112 finish_wait(&ctx->sqo_wait, &wait);
6113
Hristo Venev75b28af2019-08-26 17:23:46 +00006114 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006115 }
6116
Jens Axboe8a4955f2019-12-09 14:52:35 -07006117 mutex_lock(&ctx->uring_lock);
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03006118 ret = io_submit_sqes(ctx, to_submit, NULL, -1, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07006119 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006120 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006121 }
6122
Jens Axboeb41e9852020-02-17 09:52:41 -07006123 if (current->task_works)
6124 task_work_run();
6125
Jens Axboe6c271ce2019-01-10 11:22:30 -07006126 set_fs(old_fs);
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03006127 io_sq_thread_drop_mm(ctx);
Jens Axboe181e4482019-11-25 08:52:30 -07006128 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006129
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006130 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006131
Jens Axboe6c271ce2019-01-10 11:22:30 -07006132 return 0;
6133}
6134
Jens Axboebda52162019-09-24 13:47:15 -06006135struct io_wait_queue {
6136 struct wait_queue_entry wq;
6137 struct io_ring_ctx *ctx;
6138 unsigned to_wait;
6139 unsigned nr_timeouts;
6140};
6141
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006142static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06006143{
6144 struct io_ring_ctx *ctx = iowq->ctx;
6145
6146 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006147 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006148 * started waiting. For timeouts, we always want to return to userspace,
6149 * regardless of event count.
6150 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006151 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006152 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6153}
6154
6155static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6156 int wake_flags, void *key)
6157{
6158 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6159 wq);
6160
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006161 /* use noflush == true, as we can't safely rely on locking context */
6162 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06006163 return -1;
6164
6165 return autoremove_wake_function(curr, mode, wake_flags, key);
6166}
6167
Jens Axboe2b188cc2019-01-07 10:46:33 -07006168/*
6169 * Wait until events become available, if we don't already have some. The
6170 * application must reap them itself, as they reside on the shared cq ring.
6171 */
6172static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6173 const sigset_t __user *sig, size_t sigsz)
6174{
Jens Axboebda52162019-09-24 13:47:15 -06006175 struct io_wait_queue iowq = {
6176 .wq = {
6177 .private = current,
6178 .func = io_wake_function,
6179 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6180 },
6181 .ctx = ctx,
6182 .to_wait = min_events,
6183 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006184 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006185 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006186
Jens Axboeb41e9852020-02-17 09:52:41 -07006187 do {
6188 if (io_cqring_events(ctx, false) >= min_events)
6189 return 0;
6190 if (!current->task_works)
6191 break;
6192 task_work_run();
6193 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006194
6195 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006196#ifdef CONFIG_COMPAT
6197 if (in_compat_syscall())
6198 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006199 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006200 else
6201#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006202 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006203
Jens Axboe2b188cc2019-01-07 10:46:33 -07006204 if (ret)
6205 return ret;
6206 }
6207
Jens Axboebda52162019-09-24 13:47:15 -06006208 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006209 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006210 do {
6211 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6212 TASK_INTERRUPTIBLE);
Jens Axboeb41e9852020-02-17 09:52:41 -07006213 if (current->task_works)
6214 task_work_run();
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006215 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06006216 break;
6217 schedule();
6218 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006219 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06006220 break;
6221 }
6222 } while (1);
6223 finish_wait(&ctx->wait, &iowq.wq);
6224
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006225 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006226
Hristo Venev75b28af2019-08-26 17:23:46 +00006227 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006228}
6229
Jens Axboe6b063142019-01-10 22:13:58 -07006230static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6231{
6232#if defined(CONFIG_UNIX)
6233 if (ctx->ring_sock) {
6234 struct sock *sock = ctx->ring_sock->sk;
6235 struct sk_buff *skb;
6236
6237 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6238 kfree_skb(skb);
6239 }
6240#else
6241 int i;
6242
Jens Axboe65e19f52019-10-26 07:20:21 -06006243 for (i = 0; i < ctx->nr_user_files; i++) {
6244 struct file *file;
6245
6246 file = io_file_from_index(ctx, i);
6247 if (file)
6248 fput(file);
6249 }
Jens Axboe6b063142019-01-10 22:13:58 -07006250#endif
6251}
6252
Jens Axboe05f3fb32019-12-09 11:22:50 -07006253static void io_file_ref_kill(struct percpu_ref *ref)
6254{
6255 struct fixed_file_data *data;
6256
6257 data = container_of(ref, struct fixed_file_data, refs);
6258 complete(&data->done);
6259}
6260
Jens Axboe6b063142019-01-10 22:13:58 -07006261static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6262{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006263 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006264 struct fixed_file_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006265 unsigned nr_tables, i;
6266
Jens Axboe05f3fb32019-12-09 11:22:50 -07006267 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006268 return -ENXIO;
6269
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006270 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006271 if (!list_empty(&data->ref_list))
6272 ref_node = list_first_entry(&data->ref_list,
6273 struct fixed_file_ref_node, node);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006274 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006275 if (ref_node)
6276 percpu_ref_kill(&ref_node->refs);
6277
6278 percpu_ref_kill(&data->refs);
6279
6280 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06006281 flush_delayed_work(&ctx->file_put_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07006282 wait_for_completion(&data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006283
Jens Axboe6b063142019-01-10 22:13:58 -07006284 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06006285 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6286 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006287 kfree(data->table[i].files);
6288 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006289 percpu_ref_exit(&data->refs);
6290 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006291 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006292 ctx->nr_user_files = 0;
6293 return 0;
6294}
6295
Jens Axboe6c271ce2019-01-10 11:22:30 -07006296static void io_sq_thread_stop(struct io_ring_ctx *ctx)
6297{
6298 if (ctx->sqo_thread) {
Jens Axboe0f158b42020-05-14 17:18:39 -06006299 wait_for_completion(&ctx->sq_thread_comp);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006300 /*
6301 * The park is a bit of a work-around, without it we get
6302 * warning spews on shutdown with SQPOLL set and affinity
6303 * set to a single CPU.
6304 */
Jens Axboe06058632019-04-13 09:26:03 -06006305 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006306 kthread_stop(ctx->sqo_thread);
6307 ctx->sqo_thread = NULL;
6308 }
6309}
6310
Jens Axboe6b063142019-01-10 22:13:58 -07006311static void io_finish_async(struct io_ring_ctx *ctx)
6312{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006313 io_sq_thread_stop(ctx);
6314
Jens Axboe561fb042019-10-24 07:25:42 -06006315 if (ctx->io_wq) {
6316 io_wq_destroy(ctx->io_wq);
6317 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006318 }
6319}
6320
6321#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07006322/*
6323 * Ensure the UNIX gc is aware of our file set, so we are certain that
6324 * the io_uring can be safely unregistered on process exit, even if we have
6325 * loops in the file referencing.
6326 */
6327static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
6328{
6329 struct sock *sk = ctx->ring_sock->sk;
6330 struct scm_fp_list *fpl;
6331 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06006332 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07006333
Jens Axboe6b063142019-01-10 22:13:58 -07006334 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
6335 if (!fpl)
6336 return -ENOMEM;
6337
6338 skb = alloc_skb(0, GFP_KERNEL);
6339 if (!skb) {
6340 kfree(fpl);
6341 return -ENOMEM;
6342 }
6343
6344 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07006345
Jens Axboe08a45172019-10-03 08:11:03 -06006346 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07006347 fpl->user = get_uid(ctx->user);
6348 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006349 struct file *file = io_file_from_index(ctx, i + offset);
6350
6351 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06006352 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06006353 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06006354 unix_inflight(fpl->user, fpl->fp[nr_files]);
6355 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07006356 }
6357
Jens Axboe08a45172019-10-03 08:11:03 -06006358 if (nr_files) {
6359 fpl->max = SCM_MAX_FD;
6360 fpl->count = nr_files;
6361 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006362 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06006363 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
6364 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07006365
Jens Axboe08a45172019-10-03 08:11:03 -06006366 for (i = 0; i < nr_files; i++)
6367 fput(fpl->fp[i]);
6368 } else {
6369 kfree_skb(skb);
6370 kfree(fpl);
6371 }
Jens Axboe6b063142019-01-10 22:13:58 -07006372
6373 return 0;
6374}
6375
6376/*
6377 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
6378 * causes regular reference counting to break down. We rely on the UNIX
6379 * garbage collection to take care of this problem for us.
6380 */
6381static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6382{
6383 unsigned left, total;
6384 int ret = 0;
6385
6386 total = 0;
6387 left = ctx->nr_user_files;
6388 while (left) {
6389 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07006390
6391 ret = __io_sqe_files_scm(ctx, this_files, total);
6392 if (ret)
6393 break;
6394 left -= this_files;
6395 total += this_files;
6396 }
6397
6398 if (!ret)
6399 return 0;
6400
6401 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006402 struct file *file = io_file_from_index(ctx, total);
6403
6404 if (file)
6405 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07006406 total++;
6407 }
6408
6409 return ret;
6410}
6411#else
6412static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6413{
6414 return 0;
6415}
6416#endif
6417
Jens Axboe65e19f52019-10-26 07:20:21 -06006418static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
6419 unsigned nr_files)
6420{
6421 int i;
6422
6423 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006424 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006425 unsigned this_files;
6426
6427 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
6428 table->files = kcalloc(this_files, sizeof(struct file *),
6429 GFP_KERNEL);
6430 if (!table->files)
6431 break;
6432 nr_files -= this_files;
6433 }
6434
6435 if (i == nr_tables)
6436 return 0;
6437
6438 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006439 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006440 kfree(table->files);
6441 }
6442 return 1;
6443}
6444
Jens Axboe05f3fb32019-12-09 11:22:50 -07006445static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06006446{
6447#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06006448 struct sock *sock = ctx->ring_sock->sk;
6449 struct sk_buff_head list, *head = &sock->sk_receive_queue;
6450 struct sk_buff *skb;
6451 int i;
6452
6453 __skb_queue_head_init(&list);
6454
6455 /*
6456 * Find the skb that holds this file in its SCM_RIGHTS. When found,
6457 * remove this entry and rearrange the file array.
6458 */
6459 skb = skb_dequeue(head);
6460 while (skb) {
6461 struct scm_fp_list *fp;
6462
6463 fp = UNIXCB(skb).fp;
6464 for (i = 0; i < fp->count; i++) {
6465 int left;
6466
6467 if (fp->fp[i] != file)
6468 continue;
6469
6470 unix_notinflight(fp->user, fp->fp[i]);
6471 left = fp->count - 1 - i;
6472 if (left) {
6473 memmove(&fp->fp[i], &fp->fp[i + 1],
6474 left * sizeof(struct file *));
6475 }
6476 fp->count--;
6477 if (!fp->count) {
6478 kfree_skb(skb);
6479 skb = NULL;
6480 } else {
6481 __skb_queue_tail(&list, skb);
6482 }
6483 fput(file);
6484 file = NULL;
6485 break;
6486 }
6487
6488 if (!file)
6489 break;
6490
6491 __skb_queue_tail(&list, skb);
6492
6493 skb = skb_dequeue(head);
6494 }
6495
6496 if (skb_peek(&list)) {
6497 spin_lock_irq(&head->lock);
6498 while ((skb = __skb_dequeue(&list)) != NULL)
6499 __skb_queue_tail(head, skb);
6500 spin_unlock_irq(&head->lock);
6501 }
6502#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07006503 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06006504#endif
6505}
6506
Jens Axboe05f3fb32019-12-09 11:22:50 -07006507struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08006508 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006509 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006510};
6511
Jens Axboe4a38aed22020-05-14 17:21:15 -06006512static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006513{
Jens Axboe4a38aed22020-05-14 17:21:15 -06006514 struct fixed_file_data *file_data = ref_node->file_data;
6515 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006516 struct io_file_put *pfile, *tmp;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006517
Xiaoguang Wang05589552020-03-31 14:05:18 +08006518 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006519 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006520 io_ring_file_put(ctx, pfile->file);
6521 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006522 }
6523
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006524 spin_lock(&file_data->lock);
6525 list_del(&ref_node->node);
6526 spin_unlock(&file_data->lock);
Jens Axboe2faf8522020-02-04 19:54:55 -07006527
Xiaoguang Wang05589552020-03-31 14:05:18 +08006528 percpu_ref_exit(&ref_node->refs);
6529 kfree(ref_node);
6530 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006531}
6532
Jens Axboe4a38aed22020-05-14 17:21:15 -06006533static void io_file_put_work(struct work_struct *work)
6534{
6535 struct io_ring_ctx *ctx;
6536 struct llist_node *node;
6537
6538 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
6539 node = llist_del_all(&ctx->file_put_llist);
6540
6541 while (node) {
6542 struct fixed_file_ref_node *ref_node;
6543 struct llist_node *next = node->next;
6544
6545 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
6546 __io_file_put_work(ref_node);
6547 node = next;
6548 }
6549}
6550
Jens Axboe05f3fb32019-12-09 11:22:50 -07006551static void io_file_data_ref_zero(struct percpu_ref *ref)
6552{
Xiaoguang Wang05589552020-03-31 14:05:18 +08006553 struct fixed_file_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06006554 struct io_ring_ctx *ctx;
6555 bool first_add;
6556 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006557
Xiaoguang Wang05589552020-03-31 14:05:18 +08006558 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Jens Axboe4a38aed22020-05-14 17:21:15 -06006559 ctx = ref_node->file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006560
Jens Axboe4a38aed22020-05-14 17:21:15 -06006561 if (percpu_ref_is_dying(&ctx->file_data->refs))
6562 delay = 0;
6563
6564 first_add = llist_add(&ref_node->llist, &ctx->file_put_llist);
6565 if (!delay)
6566 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
6567 else if (first_add)
6568 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006569}
6570
6571static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
6572 struct io_ring_ctx *ctx)
6573{
6574 struct fixed_file_ref_node *ref_node;
6575
6576 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
6577 if (!ref_node)
6578 return ERR_PTR(-ENOMEM);
6579
6580 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
6581 0, GFP_KERNEL)) {
6582 kfree(ref_node);
6583 return ERR_PTR(-ENOMEM);
6584 }
6585 INIT_LIST_HEAD(&ref_node->node);
6586 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006587 ref_node->file_data = ctx->file_data;
6588 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006589}
6590
6591static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
6592{
6593 percpu_ref_exit(&ref_node->refs);
6594 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006595}
6596
6597static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
6598 unsigned nr_args)
6599{
6600 __s32 __user *fds = (__s32 __user *) arg;
6601 unsigned nr_tables;
6602 struct file *file;
6603 int fd, ret = 0;
6604 unsigned i;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006605 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006606
6607 if (ctx->file_data)
6608 return -EBUSY;
6609 if (!nr_args)
6610 return -EINVAL;
6611 if (nr_args > IORING_MAX_FIXED_FILES)
6612 return -EMFILE;
6613
6614 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
6615 if (!ctx->file_data)
6616 return -ENOMEM;
6617 ctx->file_data->ctx = ctx;
6618 init_completion(&ctx->file_data->done);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006619 INIT_LIST_HEAD(&ctx->file_data->ref_list);
Xiaoguang Wangf7fe9342020-04-07 20:02:31 +08006620 spin_lock_init(&ctx->file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006621
6622 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
6623 ctx->file_data->table = kcalloc(nr_tables,
6624 sizeof(struct fixed_file_table),
6625 GFP_KERNEL);
6626 if (!ctx->file_data->table) {
6627 kfree(ctx->file_data);
6628 ctx->file_data = NULL;
6629 return -ENOMEM;
6630 }
6631
Xiaoguang Wang05589552020-03-31 14:05:18 +08006632 if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill,
Jens Axboe05f3fb32019-12-09 11:22:50 -07006633 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
6634 kfree(ctx->file_data->table);
6635 kfree(ctx->file_data);
6636 ctx->file_data = NULL;
6637 return -ENOMEM;
6638 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006639
6640 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
6641 percpu_ref_exit(&ctx->file_data->refs);
6642 kfree(ctx->file_data->table);
6643 kfree(ctx->file_data);
6644 ctx->file_data = NULL;
6645 return -ENOMEM;
6646 }
6647
6648 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
6649 struct fixed_file_table *table;
6650 unsigned index;
6651
6652 ret = -EFAULT;
6653 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
6654 break;
6655 /* allow sparse sets */
6656 if (fd == -1) {
6657 ret = 0;
6658 continue;
6659 }
6660
6661 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6662 index = i & IORING_FILE_TABLE_MASK;
6663 file = fget(fd);
6664
6665 ret = -EBADF;
6666 if (!file)
6667 break;
6668
6669 /*
6670 * Don't allow io_uring instances to be registered. If UNIX
6671 * isn't enabled, then this causes a reference cycle and this
6672 * instance can never get freed. If UNIX is enabled we'll
6673 * handle it just fine, but there's still no point in allowing
6674 * a ring fd as it doesn't support regular read/write anyway.
6675 */
6676 if (file->f_op == &io_uring_fops) {
6677 fput(file);
6678 break;
6679 }
6680 ret = 0;
6681 table->files[index] = file;
6682 }
6683
6684 if (ret) {
6685 for (i = 0; i < ctx->nr_user_files; i++) {
6686 file = io_file_from_index(ctx, i);
6687 if (file)
6688 fput(file);
6689 }
6690 for (i = 0; i < nr_tables; i++)
6691 kfree(ctx->file_data->table[i].files);
6692
6693 kfree(ctx->file_data->table);
6694 kfree(ctx->file_data);
6695 ctx->file_data = NULL;
6696 ctx->nr_user_files = 0;
6697 return ret;
6698 }
6699
6700 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006701 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006702 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006703 return ret;
6704 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006705
Xiaoguang Wang05589552020-03-31 14:05:18 +08006706 ref_node = alloc_fixed_file_ref_node(ctx);
6707 if (IS_ERR(ref_node)) {
6708 io_sqe_files_unregister(ctx);
6709 return PTR_ERR(ref_node);
6710 }
6711
6712 ctx->file_data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006713 spin_lock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006714 list_add(&ref_node->node, &ctx->file_data->ref_list);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006715 spin_unlock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006716 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006717 return ret;
6718}
6719
Jens Axboec3a31e62019-10-03 13:59:56 -06006720static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
6721 int index)
6722{
6723#if defined(CONFIG_UNIX)
6724 struct sock *sock = ctx->ring_sock->sk;
6725 struct sk_buff_head *head = &sock->sk_receive_queue;
6726 struct sk_buff *skb;
6727
6728 /*
6729 * See if we can merge this file into an existing skb SCM_RIGHTS
6730 * file set. If there's no room, fall back to allocating a new skb
6731 * and filling it in.
6732 */
6733 spin_lock_irq(&head->lock);
6734 skb = skb_peek(head);
6735 if (skb) {
6736 struct scm_fp_list *fpl = UNIXCB(skb).fp;
6737
6738 if (fpl->count < SCM_MAX_FD) {
6739 __skb_unlink(skb, head);
6740 spin_unlock_irq(&head->lock);
6741 fpl->fp[fpl->count] = get_file(file);
6742 unix_inflight(fpl->user, fpl->fp[fpl->count]);
6743 fpl->count++;
6744 spin_lock_irq(&head->lock);
6745 __skb_queue_head(head, skb);
6746 } else {
6747 skb = NULL;
6748 }
6749 }
6750 spin_unlock_irq(&head->lock);
6751
6752 if (skb) {
6753 fput(file);
6754 return 0;
6755 }
6756
6757 return __io_sqe_files_scm(ctx, 1, index);
6758#else
6759 return 0;
6760#endif
6761}
6762
Hillf Dantona5318d32020-03-23 17:47:15 +08006763static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08006764 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006765{
Hillf Dantona5318d32020-03-23 17:47:15 +08006766 struct io_file_put *pfile;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006767 struct percpu_ref *refs = data->cur_refs;
6768 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006769
Jens Axboe05f3fb32019-12-09 11:22:50 -07006770 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08006771 if (!pfile)
6772 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006773
Xiaoguang Wang05589552020-03-31 14:05:18 +08006774 ref_node = container_of(refs, struct fixed_file_ref_node, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006775 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006776 list_add(&pfile->list, &ref_node->file_list);
6777
Hillf Dantona5318d32020-03-23 17:47:15 +08006778 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006779}
6780
6781static int __io_sqe_files_update(struct io_ring_ctx *ctx,
6782 struct io_uring_files_update *up,
6783 unsigned nr_args)
6784{
6785 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006786 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006787 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006788 __s32 __user *fds;
6789 int fd, i, err;
6790 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006791 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06006792
Jens Axboe05f3fb32019-12-09 11:22:50 -07006793 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06006794 return -EOVERFLOW;
6795 if (done > ctx->nr_user_files)
6796 return -EINVAL;
6797
Xiaoguang Wang05589552020-03-31 14:05:18 +08006798 ref_node = alloc_fixed_file_ref_node(ctx);
6799 if (IS_ERR(ref_node))
6800 return PTR_ERR(ref_node);
6801
Jens Axboec3a31e62019-10-03 13:59:56 -06006802 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006803 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06006804 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006805 struct fixed_file_table *table;
6806 unsigned index;
6807
Jens Axboec3a31e62019-10-03 13:59:56 -06006808 err = 0;
6809 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
6810 err = -EFAULT;
6811 break;
6812 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006813 i = array_index_nospec(up->offset, ctx->nr_user_files);
6814 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06006815 index = i & IORING_FILE_TABLE_MASK;
6816 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006817 file = io_file_from_index(ctx, index);
Hillf Dantona5318d32020-03-23 17:47:15 +08006818 err = io_queue_file_removal(data, file);
6819 if (err)
6820 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06006821 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006822 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06006823 }
6824 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06006825 file = fget(fd);
6826 if (!file) {
6827 err = -EBADF;
6828 break;
6829 }
6830 /*
6831 * Don't allow io_uring instances to be registered. If
6832 * UNIX isn't enabled, then this causes a reference
6833 * cycle and this instance can never get freed. If UNIX
6834 * is enabled we'll handle it just fine, but there's
6835 * still no point in allowing a ring fd as it doesn't
6836 * support regular read/write anyway.
6837 */
6838 if (file->f_op == &io_uring_fops) {
6839 fput(file);
6840 err = -EBADF;
6841 break;
6842 }
Jens Axboe65e19f52019-10-26 07:20:21 -06006843 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006844 err = io_sqe_file_register(ctx, file, i);
6845 if (err)
6846 break;
6847 }
6848 nr_args--;
6849 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006850 up->offset++;
6851 }
6852
Xiaoguang Wang05589552020-03-31 14:05:18 +08006853 if (needs_switch) {
6854 percpu_ref_kill(data->cur_refs);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006855 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006856 list_add(&ref_node->node, &data->ref_list);
6857 data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006858 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006859 percpu_ref_get(&ctx->file_data->refs);
6860 } else
6861 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06006862
6863 return done ? done : err;
6864}
Xiaoguang Wang05589552020-03-31 14:05:18 +08006865
Jens Axboe05f3fb32019-12-09 11:22:50 -07006866static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
6867 unsigned nr_args)
6868{
6869 struct io_uring_files_update up;
6870
6871 if (!ctx->file_data)
6872 return -ENXIO;
6873 if (!nr_args)
6874 return -EINVAL;
6875 if (copy_from_user(&up, arg, sizeof(up)))
6876 return -EFAULT;
6877 if (up.resv)
6878 return -EINVAL;
6879
6880 return __io_sqe_files_update(ctx, &up, nr_args);
6881}
Jens Axboec3a31e62019-10-03 13:59:56 -06006882
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006883static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07006884{
6885 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6886
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006887 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07006888 io_put_req(req);
6889}
6890
Pavel Begunkov24369c22020-01-28 03:15:48 +03006891static int io_init_wq_offload(struct io_ring_ctx *ctx,
6892 struct io_uring_params *p)
6893{
6894 struct io_wq_data data;
6895 struct fd f;
6896 struct io_ring_ctx *ctx_attach;
6897 unsigned int concurrency;
6898 int ret = 0;
6899
6900 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006901 data.free_work = io_free_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03006902
6903 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
6904 /* Do QD, or 4 * CPUS, whatever is smallest */
6905 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
6906
6907 ctx->io_wq = io_wq_create(concurrency, &data);
6908 if (IS_ERR(ctx->io_wq)) {
6909 ret = PTR_ERR(ctx->io_wq);
6910 ctx->io_wq = NULL;
6911 }
6912 return ret;
6913 }
6914
6915 f = fdget(p->wq_fd);
6916 if (!f.file)
6917 return -EBADF;
6918
6919 if (f.file->f_op != &io_uring_fops) {
6920 ret = -EINVAL;
6921 goto out_fput;
6922 }
6923
6924 ctx_attach = f.file->private_data;
6925 /* @io_wq is protected by holding the fd */
6926 if (!io_wq_get(ctx_attach->io_wq, &data)) {
6927 ret = -EINVAL;
6928 goto out_fput;
6929 }
6930
6931 ctx->io_wq = ctx_attach->io_wq;
6932out_fput:
6933 fdput(f);
6934 return ret;
6935}
6936
Jens Axboe6c271ce2019-01-10 11:22:30 -07006937static int io_sq_offload_start(struct io_ring_ctx *ctx,
6938 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006939{
6940 int ret;
6941
Jens Axboe6c271ce2019-01-10 11:22:30 -07006942 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006943 mmgrab(current->mm);
6944 ctx->sqo_mm = current->mm;
6945
Jens Axboe6c271ce2019-01-10 11:22:30 -07006946 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06006947 ret = -EPERM;
6948 if (!capable(CAP_SYS_ADMIN))
6949 goto err;
6950
Jens Axboe917257d2019-04-13 09:28:55 -06006951 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
6952 if (!ctx->sq_thread_idle)
6953 ctx->sq_thread_idle = HZ;
6954
Jens Axboe6c271ce2019-01-10 11:22:30 -07006955 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06006956 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006957
Jens Axboe917257d2019-04-13 09:28:55 -06006958 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06006959 if (cpu >= nr_cpu_ids)
6960 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08006961 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06006962 goto err;
6963
Jens Axboe6c271ce2019-01-10 11:22:30 -07006964 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
6965 ctx, cpu,
6966 "io_uring-sq");
6967 } else {
6968 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
6969 "io_uring-sq");
6970 }
6971 if (IS_ERR(ctx->sqo_thread)) {
6972 ret = PTR_ERR(ctx->sqo_thread);
6973 ctx->sqo_thread = NULL;
6974 goto err;
6975 }
6976 wake_up_process(ctx->sqo_thread);
6977 } else if (p->flags & IORING_SETUP_SQ_AFF) {
6978 /* Can't have SQ_AFF without SQPOLL */
6979 ret = -EINVAL;
6980 goto err;
6981 }
6982
Pavel Begunkov24369c22020-01-28 03:15:48 +03006983 ret = io_init_wq_offload(ctx, p);
6984 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006985 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006986
6987 return 0;
6988err:
Jens Axboe54a91f32019-09-10 09:15:04 -06006989 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006990 mmdrop(ctx->sqo_mm);
6991 ctx->sqo_mm = NULL;
6992 return ret;
6993}
6994
6995static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
6996{
6997 atomic_long_sub(nr_pages, &user->locked_vm);
6998}
6999
7000static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
7001{
7002 unsigned long page_limit, cur_pages, new_pages;
7003
7004 /* Don't allow more pages than we can safely lock */
7005 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
7006
7007 do {
7008 cur_pages = atomic_long_read(&user->locked_vm);
7009 new_pages = cur_pages + nr_pages;
7010 if (new_pages > page_limit)
7011 return -ENOMEM;
7012 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7013 new_pages) != cur_pages);
7014
7015 return 0;
7016}
7017
7018static void io_mem_free(void *ptr)
7019{
Mark Rutland52e04ef2019-04-30 17:30:21 +01007020 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007021
Mark Rutland52e04ef2019-04-30 17:30:21 +01007022 if (!ptr)
7023 return;
7024
7025 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007026 if (put_page_testzero(page))
7027 free_compound_page(page);
7028}
7029
7030static void *io_mem_alloc(size_t size)
7031{
7032 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
7033 __GFP_NORETRY;
7034
7035 return (void *) __get_free_pages(gfp_flags, get_order(size));
7036}
7037
Hristo Venev75b28af2019-08-26 17:23:46 +00007038static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
7039 size_t *sq_offset)
7040{
7041 struct io_rings *rings;
7042 size_t off, sq_array_size;
7043
7044 off = struct_size(rings, cqes, cq_entries);
7045 if (off == SIZE_MAX)
7046 return SIZE_MAX;
7047
7048#ifdef CONFIG_SMP
7049 off = ALIGN(off, SMP_CACHE_BYTES);
7050 if (off == 0)
7051 return SIZE_MAX;
7052#endif
7053
7054 sq_array_size = array_size(sizeof(u32), sq_entries);
7055 if (sq_array_size == SIZE_MAX)
7056 return SIZE_MAX;
7057
7058 if (check_add_overflow(off, sq_array_size, &off))
7059 return SIZE_MAX;
7060
7061 if (sq_offset)
7062 *sq_offset = off;
7063
7064 return off;
7065}
7066
Jens Axboe2b188cc2019-01-07 10:46:33 -07007067static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
7068{
Hristo Venev75b28af2019-08-26 17:23:46 +00007069 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007070
Hristo Venev75b28af2019-08-26 17:23:46 +00007071 pages = (size_t)1 << get_order(
7072 rings_size(sq_entries, cq_entries, NULL));
7073 pages += (size_t)1 << get_order(
7074 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07007075
Hristo Venev75b28af2019-08-26 17:23:46 +00007076 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007077}
7078
Jens Axboeedafcce2019-01-09 09:16:05 -07007079static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
7080{
7081 int i, j;
7082
7083 if (!ctx->user_bufs)
7084 return -ENXIO;
7085
7086 for (i = 0; i < ctx->nr_user_bufs; i++) {
7087 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7088
7089 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007090 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07007091
7092 if (ctx->account_mem)
7093 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007094 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007095 imu->nr_bvecs = 0;
7096 }
7097
7098 kfree(ctx->user_bufs);
7099 ctx->user_bufs = NULL;
7100 ctx->nr_user_bufs = 0;
7101 return 0;
7102}
7103
7104static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
7105 void __user *arg, unsigned index)
7106{
7107 struct iovec __user *src;
7108
7109#ifdef CONFIG_COMPAT
7110 if (ctx->compat) {
7111 struct compat_iovec __user *ciovs;
7112 struct compat_iovec ciov;
7113
7114 ciovs = (struct compat_iovec __user *) arg;
7115 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
7116 return -EFAULT;
7117
Jens Axboed55e5f52019-12-11 16:12:15 -07007118 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07007119 dst->iov_len = ciov.iov_len;
7120 return 0;
7121 }
7122#endif
7123 src = (struct iovec __user *) arg;
7124 if (copy_from_user(dst, &src[index], sizeof(*dst)))
7125 return -EFAULT;
7126 return 0;
7127}
7128
7129static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
7130 unsigned nr_args)
7131{
7132 struct vm_area_struct **vmas = NULL;
7133 struct page **pages = NULL;
7134 int i, j, got_pages = 0;
7135 int ret = -EINVAL;
7136
7137 if (ctx->user_bufs)
7138 return -EBUSY;
7139 if (!nr_args || nr_args > UIO_MAXIOV)
7140 return -EINVAL;
7141
7142 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
7143 GFP_KERNEL);
7144 if (!ctx->user_bufs)
7145 return -ENOMEM;
7146
7147 for (i = 0; i < nr_args; i++) {
7148 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7149 unsigned long off, start, end, ubuf;
7150 int pret, nr_pages;
7151 struct iovec iov;
7152 size_t size;
7153
7154 ret = io_copy_iov(ctx, &iov, arg, i);
7155 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03007156 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007157
7158 /*
7159 * Don't impose further limits on the size and buffer
7160 * constraints here, we'll -EINVAL later when IO is
7161 * submitted if they are wrong.
7162 */
7163 ret = -EFAULT;
7164 if (!iov.iov_base || !iov.iov_len)
7165 goto err;
7166
7167 /* arbitrary limit, but we need something */
7168 if (iov.iov_len > SZ_1G)
7169 goto err;
7170
7171 ubuf = (unsigned long) iov.iov_base;
7172 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
7173 start = ubuf >> PAGE_SHIFT;
7174 nr_pages = end - start;
7175
7176 if (ctx->account_mem) {
7177 ret = io_account_mem(ctx->user, nr_pages);
7178 if (ret)
7179 goto err;
7180 }
7181
7182 ret = 0;
7183 if (!pages || nr_pages > got_pages) {
7184 kfree(vmas);
7185 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007186 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07007187 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007188 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07007189 sizeof(struct vm_area_struct *),
7190 GFP_KERNEL);
7191 if (!pages || !vmas) {
7192 ret = -ENOMEM;
7193 if (ctx->account_mem)
7194 io_unaccount_mem(ctx->user, nr_pages);
7195 goto err;
7196 }
7197 got_pages = nr_pages;
7198 }
7199
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007200 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07007201 GFP_KERNEL);
7202 ret = -ENOMEM;
7203 if (!imu->bvec) {
7204 if (ctx->account_mem)
7205 io_unaccount_mem(ctx->user, nr_pages);
7206 goto err;
7207 }
7208
7209 ret = 0;
7210 down_read(&current->mm->mmap_sem);
John Hubbard2113b052020-01-30 22:13:13 -08007211 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07007212 FOLL_WRITE | FOLL_LONGTERM,
7213 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007214 if (pret == nr_pages) {
7215 /* don't support file backed memory */
7216 for (j = 0; j < nr_pages; j++) {
7217 struct vm_area_struct *vma = vmas[j];
7218
7219 if (vma->vm_file &&
7220 !is_file_hugepages(vma->vm_file)) {
7221 ret = -EOPNOTSUPP;
7222 break;
7223 }
7224 }
7225 } else {
7226 ret = pret < 0 ? pret : -EFAULT;
7227 }
7228 up_read(&current->mm->mmap_sem);
7229 if (ret) {
7230 /*
7231 * if we did partial map, or found file backed vmas,
7232 * release any pages we did get
7233 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07007234 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007235 unpin_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07007236 if (ctx->account_mem)
7237 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007238 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007239 goto err;
7240 }
7241
7242 off = ubuf & ~PAGE_MASK;
7243 size = iov.iov_len;
7244 for (j = 0; j < nr_pages; j++) {
7245 size_t vec_len;
7246
7247 vec_len = min_t(size_t, size, PAGE_SIZE - off);
7248 imu->bvec[j].bv_page = pages[j];
7249 imu->bvec[j].bv_len = vec_len;
7250 imu->bvec[j].bv_offset = off;
7251 off = 0;
7252 size -= vec_len;
7253 }
7254 /* store original address for later verification */
7255 imu->ubuf = ubuf;
7256 imu->len = iov.iov_len;
7257 imu->nr_bvecs = nr_pages;
7258
7259 ctx->nr_user_bufs++;
7260 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007261 kvfree(pages);
7262 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007263 return 0;
7264err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007265 kvfree(pages);
7266 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007267 io_sqe_buffer_unregister(ctx);
7268 return ret;
7269}
7270
Jens Axboe9b402842019-04-11 11:45:41 -06007271static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
7272{
7273 __s32 __user *fds = arg;
7274 int fd;
7275
7276 if (ctx->cq_ev_fd)
7277 return -EBUSY;
7278
7279 if (copy_from_user(&fd, fds, sizeof(*fds)))
7280 return -EFAULT;
7281
7282 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
7283 if (IS_ERR(ctx->cq_ev_fd)) {
7284 int ret = PTR_ERR(ctx->cq_ev_fd);
7285 ctx->cq_ev_fd = NULL;
7286 return ret;
7287 }
7288
7289 return 0;
7290}
7291
7292static int io_eventfd_unregister(struct io_ring_ctx *ctx)
7293{
7294 if (ctx->cq_ev_fd) {
7295 eventfd_ctx_put(ctx->cq_ev_fd);
7296 ctx->cq_ev_fd = NULL;
7297 return 0;
7298 }
7299
7300 return -ENXIO;
7301}
7302
Jens Axboe5a2e7452020-02-23 16:23:11 -07007303static int __io_destroy_buffers(int id, void *p, void *data)
7304{
7305 struct io_ring_ctx *ctx = data;
7306 struct io_buffer *buf = p;
7307
Jens Axboe067524e2020-03-02 16:32:28 -07007308 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007309 return 0;
7310}
7311
7312static void io_destroy_buffers(struct io_ring_ctx *ctx)
7313{
7314 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
7315 idr_destroy(&ctx->io_buffer_idr);
7316}
7317
Jens Axboe2b188cc2019-01-07 10:46:33 -07007318static void io_ring_ctx_free(struct io_ring_ctx *ctx)
7319{
Jens Axboe6b063142019-01-10 22:13:58 -07007320 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007321 if (ctx->sqo_mm)
7322 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07007323
7324 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07007325 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07007326 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06007327 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007328 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07007329 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07007330
Jens Axboe2b188cc2019-01-07 10:46:33 -07007331#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07007332 if (ctx->ring_sock) {
7333 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007334 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07007335 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007336#endif
7337
Hristo Venev75b28af2019-08-26 17:23:46 +00007338 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007339 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007340
7341 percpu_ref_exit(&ctx->refs);
7342 if (ctx->account_mem)
7343 io_unaccount_mem(ctx->user,
7344 ring_pages(ctx->sq_entries, ctx->cq_entries));
7345 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07007346 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07007347 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07007348 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007349 kfree(ctx);
7350}
7351
7352static __poll_t io_uring_poll(struct file *file, poll_table *wait)
7353{
7354 struct io_ring_ctx *ctx = file->private_data;
7355 __poll_t mask = 0;
7356
7357 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02007358 /*
7359 * synchronizes with barrier from wq_has_sleeper call in
7360 * io_commit_cqring
7361 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007362 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00007363 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
7364 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007365 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01007366 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007367 mask |= EPOLLIN | EPOLLRDNORM;
7368
7369 return mask;
7370}
7371
7372static int io_uring_fasync(int fd, struct file *file, int on)
7373{
7374 struct io_ring_ctx *ctx = file->private_data;
7375
7376 return fasync_helper(fd, file, on, &ctx->cq_fasync);
7377}
7378
Jens Axboe071698e2020-01-28 10:04:42 -07007379static int io_remove_personalities(int id, void *p, void *data)
7380{
7381 struct io_ring_ctx *ctx = data;
7382 const struct cred *cred;
7383
7384 cred = idr_remove(&ctx->personality_idr, id);
7385 if (cred)
7386 put_cred(cred);
7387 return 0;
7388}
7389
Jens Axboe85faa7b2020-04-09 18:14:00 -06007390static void io_ring_exit_work(struct work_struct *work)
7391{
7392 struct io_ring_ctx *ctx;
7393
7394 ctx = container_of(work, struct io_ring_ctx, exit_work);
7395 if (ctx->rings)
7396 io_cqring_overflow_flush(ctx, true);
7397
Jens Axboe0f158b42020-05-14 17:18:39 -06007398 wait_for_completion(&ctx->ref_comp);
Jens Axboe85faa7b2020-04-09 18:14:00 -06007399 io_ring_ctx_free(ctx);
7400}
7401
Jens Axboe2b188cc2019-01-07 10:46:33 -07007402static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
7403{
7404 mutex_lock(&ctx->uring_lock);
7405 percpu_ref_kill(&ctx->refs);
7406 mutex_unlock(&ctx->uring_lock);
7407
Jens Axboedf069d82020-02-04 16:48:34 -07007408 /*
7409 * Wait for sq thread to idle, if we have one. It won't spin on new
7410 * work after we've killed the ctx ref above. This is important to do
7411 * before we cancel existing commands, as the thread could otherwise
7412 * be queueing new work post that. If that's work we need to cancel,
7413 * it could cause shutdown to hang.
7414 */
7415 while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait))
Xiaoguang Wang3fd44c82020-05-01 08:52:56 +08007416 cond_resched();
Jens Axboedf069d82020-02-04 16:48:34 -07007417
Jens Axboe5262f562019-09-17 12:26:57 -06007418 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07007419 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06007420
7421 if (ctx->io_wq)
7422 io_wq_cancel_all(ctx->io_wq);
7423
Jens Axboedef596e2019-01-09 08:59:42 -07007424 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07007425 /* if we failed setting up the ctx, we might not have any rings */
7426 if (ctx->rings)
7427 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07007428 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe85faa7b2020-04-09 18:14:00 -06007429 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
7430 queue_work(system_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007431}
7432
7433static int io_uring_release(struct inode *inode, struct file *file)
7434{
7435 struct io_ring_ctx *ctx = file->private_data;
7436
7437 file->private_data = NULL;
7438 io_ring_ctx_wait_and_kill(ctx);
7439 return 0;
7440}
7441
Jens Axboefcb323c2019-10-24 12:39:47 -06007442static void io_uring_cancel_files(struct io_ring_ctx *ctx,
7443 struct files_struct *files)
7444{
Jens Axboefcb323c2019-10-24 12:39:47 -06007445 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007446 struct io_kiocb *cancel_req = NULL, *req;
7447 DEFINE_WAIT(wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007448
7449 spin_lock_irq(&ctx->inflight_lock);
7450 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07007451 if (req->work.files != files)
7452 continue;
7453 /* req is being completed, ignore */
7454 if (!refcount_inc_not_zero(&req->refs))
7455 continue;
7456 cancel_req = req;
7457 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06007458 }
Jens Axboe768134d2019-11-10 20:30:53 -07007459 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007460 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07007461 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06007462 spin_unlock_irq(&ctx->inflight_lock);
7463
Jens Axboe768134d2019-11-10 20:30:53 -07007464 /* We need to keep going until we don't find a matching req */
7465 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007466 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08007467
Jens Axboe2ca10252020-02-13 17:17:35 -07007468 if (cancel_req->flags & REQ_F_OVERFLOW) {
7469 spin_lock_irq(&ctx->completion_lock);
7470 list_del(&cancel_req->list);
7471 cancel_req->flags &= ~REQ_F_OVERFLOW;
7472 if (list_empty(&ctx->cq_overflow_list)) {
7473 clear_bit(0, &ctx->sq_check_overflow);
7474 clear_bit(0, &ctx->cq_check_overflow);
7475 }
7476 spin_unlock_irq(&ctx->completion_lock);
7477
7478 WRITE_ONCE(ctx->rings->cq_overflow,
7479 atomic_inc_return(&ctx->cached_cq_overflow));
7480
7481 /*
7482 * Put inflight ref and overflow ref. If that's
7483 * all we had, then we're done with this request.
7484 */
7485 if (refcount_sub_and_test(2, &cancel_req->refs)) {
7486 io_put_req(cancel_req);
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007487 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboe2ca10252020-02-13 17:17:35 -07007488 continue;
7489 }
7490 }
7491
Bob Liu2f6d9b92019-11-13 18:06:24 +08007492 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
7493 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06007494 schedule();
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007495 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007496 }
7497}
7498
7499static int io_uring_flush(struct file *file, void *data)
7500{
7501 struct io_ring_ctx *ctx = file->private_data;
7502
7503 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07007504
7505 /*
7506 * If the task is going away, cancel work it may have pending
7507 */
7508 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
7509 io_wq_cancel_pid(ctx->io_wq, task_pid_vnr(current));
7510
Jens Axboefcb323c2019-10-24 12:39:47 -06007511 return 0;
7512}
7513
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007514static void *io_uring_validate_mmap_request(struct file *file,
7515 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007516{
Jens Axboe2b188cc2019-01-07 10:46:33 -07007517 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007518 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007519 struct page *page;
7520 void *ptr;
7521
7522 switch (offset) {
7523 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00007524 case IORING_OFF_CQ_RING:
7525 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007526 break;
7527 case IORING_OFF_SQES:
7528 ptr = ctx->sq_sqes;
7529 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007530 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007531 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007532 }
7533
7534 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07007535 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007536 return ERR_PTR(-EINVAL);
7537
7538 return ptr;
7539}
7540
7541#ifdef CONFIG_MMU
7542
7543static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7544{
7545 size_t sz = vma->vm_end - vma->vm_start;
7546 unsigned long pfn;
7547 void *ptr;
7548
7549 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
7550 if (IS_ERR(ptr))
7551 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007552
7553 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
7554 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
7555}
7556
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007557#else /* !CONFIG_MMU */
7558
7559static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7560{
7561 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
7562}
7563
7564static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
7565{
7566 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
7567}
7568
7569static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
7570 unsigned long addr, unsigned long len,
7571 unsigned long pgoff, unsigned long flags)
7572{
7573 void *ptr;
7574
7575 ptr = io_uring_validate_mmap_request(file, pgoff, len);
7576 if (IS_ERR(ptr))
7577 return PTR_ERR(ptr);
7578
7579 return (unsigned long) ptr;
7580}
7581
7582#endif /* !CONFIG_MMU */
7583
Jens Axboe2b188cc2019-01-07 10:46:33 -07007584SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
7585 u32, min_complete, u32, flags, const sigset_t __user *, sig,
7586 size_t, sigsz)
7587{
7588 struct io_ring_ctx *ctx;
7589 long ret = -EBADF;
7590 int submitted = 0;
7591 struct fd f;
7592
Jens Axboeb41e9852020-02-17 09:52:41 -07007593 if (current->task_works)
7594 task_work_run();
7595
Jens Axboe6c271ce2019-01-10 11:22:30 -07007596 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007597 return -EINVAL;
7598
7599 f = fdget(fd);
7600 if (!f.file)
7601 return -EBADF;
7602
7603 ret = -EOPNOTSUPP;
7604 if (f.file->f_op != &io_uring_fops)
7605 goto out_fput;
7606
7607 ret = -ENXIO;
7608 ctx = f.file->private_data;
7609 if (!percpu_ref_tryget(&ctx->refs))
7610 goto out_fput;
7611
Jens Axboe6c271ce2019-01-10 11:22:30 -07007612 /*
7613 * For SQ polling, the thread will do all submissions and completions.
7614 * Just return the requested submit count, and wake the thread if
7615 * we were asked to.
7616 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007617 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007618 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07007619 if (!list_empty_careful(&ctx->cq_overflow_list))
7620 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007621 if (flags & IORING_ENTER_SQ_WAKEUP)
7622 wake_up(&ctx->sqo_wait);
7623 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007624 } else if (to_submit) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07007625 mutex_lock(&ctx->uring_lock);
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03007626 submitted = io_submit_sqes(ctx, to_submit, f.file, fd, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007627 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007628
7629 if (submitted != to_submit)
7630 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007631 }
7632 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07007633 unsigned nr_events = 0;
7634
Jens Axboe2b188cc2019-01-07 10:46:33 -07007635 min_complete = min(min_complete, ctx->cq_entries);
7636
Xiaoguang Wang32b22442020-03-11 09:26:09 +08007637 /*
7638 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
7639 * space applications don't need to do io completion events
7640 * polling again, they can rely on io_sq_thread to do polling
7641 * work, which can reduce cpu usage and uring_lock contention.
7642 */
7643 if (ctx->flags & IORING_SETUP_IOPOLL &&
7644 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Jens Axboedef596e2019-01-09 08:59:42 -07007645 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07007646 } else {
7647 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
7648 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007649 }
7650
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007651out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03007652 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007653out_fput:
7654 fdput(f);
7655 return submitted ? submitted : ret;
7656}
7657
Tobias Klauserbebdb652020-02-26 18:38:32 +01007658#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07007659static int io_uring_show_cred(int id, void *p, void *data)
7660{
7661 const struct cred *cred = p;
7662 struct seq_file *m = data;
7663 struct user_namespace *uns = seq_user_ns(m);
7664 struct group_info *gi;
7665 kernel_cap_t cap;
7666 unsigned __capi;
7667 int g;
7668
7669 seq_printf(m, "%5d\n", id);
7670 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
7671 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
7672 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
7673 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
7674 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
7675 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
7676 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
7677 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
7678 seq_puts(m, "\n\tGroups:\t");
7679 gi = cred->group_info;
7680 for (g = 0; g < gi->ngroups; g++) {
7681 seq_put_decimal_ull(m, g ? " " : "",
7682 from_kgid_munged(uns, gi->gid[g]));
7683 }
7684 seq_puts(m, "\n\tCapEff:\t");
7685 cap = cred->cap_effective;
7686 CAP_FOR_EACH_U32(__capi)
7687 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
7688 seq_putc(m, '\n');
7689 return 0;
7690}
7691
7692static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
7693{
7694 int i;
7695
7696 mutex_lock(&ctx->uring_lock);
7697 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
7698 for (i = 0; i < ctx->nr_user_files; i++) {
7699 struct fixed_file_table *table;
7700 struct file *f;
7701
7702 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7703 f = table->files[i & IORING_FILE_TABLE_MASK];
7704 if (f)
7705 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
7706 else
7707 seq_printf(m, "%5u: <none>\n", i);
7708 }
7709 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
7710 for (i = 0; i < ctx->nr_user_bufs; i++) {
7711 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
7712
7713 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
7714 (unsigned int) buf->len);
7715 }
7716 if (!idr_is_empty(&ctx->personality_idr)) {
7717 seq_printf(m, "Personalities:\n");
7718 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
7719 }
Jens Axboed7718a92020-02-14 22:23:12 -07007720 seq_printf(m, "PollList:\n");
7721 spin_lock_irq(&ctx->completion_lock);
7722 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
7723 struct hlist_head *list = &ctx->cancel_hash[i];
7724 struct io_kiocb *req;
7725
7726 hlist_for_each_entry(req, list, hash_node)
7727 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
7728 req->task->task_works != NULL);
7729 }
7730 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07007731 mutex_unlock(&ctx->uring_lock);
7732}
7733
7734static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
7735{
7736 struct io_ring_ctx *ctx = f->private_data;
7737
7738 if (percpu_ref_tryget(&ctx->refs)) {
7739 __io_uring_show_fdinfo(ctx, m);
7740 percpu_ref_put(&ctx->refs);
7741 }
7742}
Tobias Klauserbebdb652020-02-26 18:38:32 +01007743#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07007744
Jens Axboe2b188cc2019-01-07 10:46:33 -07007745static const struct file_operations io_uring_fops = {
7746 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06007747 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07007748 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007749#ifndef CONFIG_MMU
7750 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
7751 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
7752#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07007753 .poll = io_uring_poll,
7754 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01007755#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07007756 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01007757#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07007758};
7759
7760static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
7761 struct io_uring_params *p)
7762{
Hristo Venev75b28af2019-08-26 17:23:46 +00007763 struct io_rings *rings;
7764 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007765
Hristo Venev75b28af2019-08-26 17:23:46 +00007766 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
7767 if (size == SIZE_MAX)
7768 return -EOVERFLOW;
7769
7770 rings = io_mem_alloc(size);
7771 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007772 return -ENOMEM;
7773
Hristo Venev75b28af2019-08-26 17:23:46 +00007774 ctx->rings = rings;
7775 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
7776 rings->sq_ring_mask = p->sq_entries - 1;
7777 rings->cq_ring_mask = p->cq_entries - 1;
7778 rings->sq_ring_entries = p->sq_entries;
7779 rings->cq_ring_entries = p->cq_entries;
7780 ctx->sq_mask = rings->sq_ring_mask;
7781 ctx->cq_mask = rings->cq_ring_mask;
7782 ctx->sq_entries = rings->sq_ring_entries;
7783 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007784
7785 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07007786 if (size == SIZE_MAX) {
7787 io_mem_free(ctx->rings);
7788 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007789 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07007790 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007791
7792 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07007793 if (!ctx->sq_sqes) {
7794 io_mem_free(ctx->rings);
7795 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007796 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07007797 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007798
Jens Axboe2b188cc2019-01-07 10:46:33 -07007799 return 0;
7800}
7801
7802/*
7803 * Allocate an anonymous fd, this is what constitutes the application
7804 * visible backing of an io_uring instance. The application mmaps this
7805 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
7806 * we have to tie this fd to a socket for file garbage collection purposes.
7807 */
7808static int io_uring_get_fd(struct io_ring_ctx *ctx)
7809{
7810 struct file *file;
7811 int ret;
7812
7813#if defined(CONFIG_UNIX)
7814 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
7815 &ctx->ring_sock);
7816 if (ret)
7817 return ret;
7818#endif
7819
7820 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
7821 if (ret < 0)
7822 goto err;
7823
7824 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
7825 O_RDWR | O_CLOEXEC);
7826 if (IS_ERR(file)) {
7827 put_unused_fd(ret);
7828 ret = PTR_ERR(file);
7829 goto err;
7830 }
7831
7832#if defined(CONFIG_UNIX)
7833 ctx->ring_sock->file = file;
7834#endif
7835 fd_install(ret, file);
7836 return ret;
7837err:
7838#if defined(CONFIG_UNIX)
7839 sock_release(ctx->ring_sock);
7840 ctx->ring_sock = NULL;
7841#endif
7842 return ret;
7843}
7844
Xiaoguang Wang7f136572020-05-05 16:28:53 +08007845static int io_uring_create(unsigned entries, struct io_uring_params *p,
7846 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007847{
7848 struct user_struct *user = NULL;
7849 struct io_ring_ctx *ctx;
7850 bool account_mem;
7851 int ret;
7852
Jens Axboe8110c1a2019-12-28 15:39:54 -07007853 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007854 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07007855 if (entries > IORING_MAX_ENTRIES) {
7856 if (!(p->flags & IORING_SETUP_CLAMP))
7857 return -EINVAL;
7858 entries = IORING_MAX_ENTRIES;
7859 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007860
7861 /*
7862 * Use twice as many entries for the CQ ring. It's possible for the
7863 * application to drive a higher depth than the size of the SQ ring,
7864 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06007865 * some flexibility in overcommitting a bit. If the application has
7866 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
7867 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07007868 */
7869 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06007870 if (p->flags & IORING_SETUP_CQSIZE) {
7871 /*
7872 * If IORING_SETUP_CQSIZE is set, we do the same roundup
7873 * to a power-of-two, if it isn't already. We do NOT impose
7874 * any cq vs sq ring sizing.
7875 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07007876 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06007877 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07007878 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
7879 if (!(p->flags & IORING_SETUP_CLAMP))
7880 return -EINVAL;
7881 p->cq_entries = IORING_MAX_CQ_ENTRIES;
7882 }
Jens Axboe33a107f2019-10-04 12:10:03 -06007883 p->cq_entries = roundup_pow_of_two(p->cq_entries);
7884 } else {
7885 p->cq_entries = 2 * p->sq_entries;
7886 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007887
7888 user = get_uid(current_user());
7889 account_mem = !capable(CAP_IPC_LOCK);
7890
7891 if (account_mem) {
7892 ret = io_account_mem(user,
7893 ring_pages(p->sq_entries, p->cq_entries));
7894 if (ret) {
7895 free_uid(user);
7896 return ret;
7897 }
7898 }
7899
7900 ctx = io_ring_ctx_alloc(p);
7901 if (!ctx) {
7902 if (account_mem)
7903 io_unaccount_mem(user, ring_pages(p->sq_entries,
7904 p->cq_entries));
7905 free_uid(user);
7906 return -ENOMEM;
7907 }
7908 ctx->compat = in_compat_syscall();
7909 ctx->account_mem = account_mem;
7910 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07007911 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07007912
7913 ret = io_allocate_scq_urings(ctx, p);
7914 if (ret)
7915 goto err;
7916
Jens Axboe6c271ce2019-01-10 11:22:30 -07007917 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007918 if (ret)
7919 goto err;
7920
Jens Axboe2b188cc2019-01-07 10:46:33 -07007921 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007922 p->sq_off.head = offsetof(struct io_rings, sq.head);
7923 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
7924 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
7925 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
7926 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
7927 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
7928 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007929
7930 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007931 p->cq_off.head = offsetof(struct io_rings, cq.head);
7932 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
7933 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
7934 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
7935 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
7936 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02007937 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06007938
Xiaoguang Wang7f136572020-05-05 16:28:53 +08007939 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
7940 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
7941 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL;
7942
7943 if (copy_to_user(params, p, sizeof(*p))) {
7944 ret = -EFAULT;
7945 goto err;
7946 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06007947 /*
7948 * Install ring fd as the very last thing, so we don't risk someone
7949 * having closed it before we finish setup
7950 */
7951 ret = io_uring_get_fd(ctx);
7952 if (ret < 0)
7953 goto err;
7954
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007955 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007956 return ret;
7957err:
7958 io_ring_ctx_wait_and_kill(ctx);
7959 return ret;
7960}
7961
7962/*
7963 * Sets up an aio uring context, and returns the fd. Applications asks for a
7964 * ring size, we return the actual sq/cq ring sizes (among other things) in the
7965 * params structure passed in.
7966 */
7967static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
7968{
7969 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007970 int i;
7971
7972 if (copy_from_user(&p, params, sizeof(p)))
7973 return -EFAULT;
7974 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
7975 if (p.resv[i])
7976 return -EINVAL;
7977 }
7978
Jens Axboe6c271ce2019-01-10 11:22:30 -07007979 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07007980 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03007981 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007982 return -EINVAL;
7983
Xiaoguang Wang7f136572020-05-05 16:28:53 +08007984 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007985}
7986
7987SYSCALL_DEFINE2(io_uring_setup, u32, entries,
7988 struct io_uring_params __user *, params)
7989{
7990 return io_uring_setup(entries, params);
7991}
7992
Jens Axboe66f4af92020-01-16 15:36:52 -07007993static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
7994{
7995 struct io_uring_probe *p;
7996 size_t size;
7997 int i, ret;
7998
7999 size = struct_size(p, ops, nr_args);
8000 if (size == SIZE_MAX)
8001 return -EOVERFLOW;
8002 p = kzalloc(size, GFP_KERNEL);
8003 if (!p)
8004 return -ENOMEM;
8005
8006 ret = -EFAULT;
8007 if (copy_from_user(p, arg, size))
8008 goto out;
8009 ret = -EINVAL;
8010 if (memchr_inv(p, 0, size))
8011 goto out;
8012
8013 p->last_op = IORING_OP_LAST - 1;
8014 if (nr_args > IORING_OP_LAST)
8015 nr_args = IORING_OP_LAST;
8016
8017 for (i = 0; i < nr_args; i++) {
8018 p->ops[i].op = i;
8019 if (!io_op_defs[i].not_supported)
8020 p->ops[i].flags = IO_URING_OP_SUPPORTED;
8021 }
8022 p->ops_len = i;
8023
8024 ret = 0;
8025 if (copy_to_user(arg, p, size))
8026 ret = -EFAULT;
8027out:
8028 kfree(p);
8029 return ret;
8030}
8031
Jens Axboe071698e2020-01-28 10:04:42 -07008032static int io_register_personality(struct io_ring_ctx *ctx)
8033{
8034 const struct cred *creds = get_current_cred();
8035 int id;
8036
8037 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
8038 USHRT_MAX, GFP_KERNEL);
8039 if (id < 0)
8040 put_cred(creds);
8041 return id;
8042}
8043
8044static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
8045{
8046 const struct cred *old_creds;
8047
8048 old_creds = idr_remove(&ctx->personality_idr, id);
8049 if (old_creds) {
8050 put_cred(old_creds);
8051 return 0;
8052 }
8053
8054 return -EINVAL;
8055}
8056
8057static bool io_register_op_must_quiesce(int op)
8058{
8059 switch (op) {
8060 case IORING_UNREGISTER_FILES:
8061 case IORING_REGISTER_FILES_UPDATE:
8062 case IORING_REGISTER_PROBE:
8063 case IORING_REGISTER_PERSONALITY:
8064 case IORING_UNREGISTER_PERSONALITY:
8065 return false;
8066 default:
8067 return true;
8068 }
8069}
8070
Jens Axboeedafcce2019-01-09 09:16:05 -07008071static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
8072 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06008073 __releases(ctx->uring_lock)
8074 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07008075{
8076 int ret;
8077
Jens Axboe35fa71a2019-04-22 10:23:23 -06008078 /*
8079 * We're inside the ring mutex, if the ref is already dying, then
8080 * someone else killed the ctx or is already going through
8081 * io_uring_register().
8082 */
8083 if (percpu_ref_is_dying(&ctx->refs))
8084 return -ENXIO;
8085
Jens Axboe071698e2020-01-28 10:04:42 -07008086 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008087 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06008088
Jens Axboe05f3fb32019-12-09 11:22:50 -07008089 /*
8090 * Drop uring mutex before waiting for references to exit. If
8091 * another thread is currently inside io_uring_enter() it might
8092 * need to grab the uring_lock to make progress. If we hold it
8093 * here across the drain wait, then we can deadlock. It's safe
8094 * to drop the mutex here, since no new references will come in
8095 * after we've killed the percpu ref.
8096 */
8097 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f158b42020-05-14 17:18:39 -06008098 ret = wait_for_completion_interruptible(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008099 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07008100 if (ret) {
8101 percpu_ref_resurrect(&ctx->refs);
8102 ret = -EINTR;
8103 goto out;
8104 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008105 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008106
8107 switch (opcode) {
8108 case IORING_REGISTER_BUFFERS:
8109 ret = io_sqe_buffer_register(ctx, arg, nr_args);
8110 break;
8111 case IORING_UNREGISTER_BUFFERS:
8112 ret = -EINVAL;
8113 if (arg || nr_args)
8114 break;
8115 ret = io_sqe_buffer_unregister(ctx);
8116 break;
Jens Axboe6b063142019-01-10 22:13:58 -07008117 case IORING_REGISTER_FILES:
8118 ret = io_sqe_files_register(ctx, arg, nr_args);
8119 break;
8120 case IORING_UNREGISTER_FILES:
8121 ret = -EINVAL;
8122 if (arg || nr_args)
8123 break;
8124 ret = io_sqe_files_unregister(ctx);
8125 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06008126 case IORING_REGISTER_FILES_UPDATE:
8127 ret = io_sqe_files_update(ctx, arg, nr_args);
8128 break;
Jens Axboe9b402842019-04-11 11:45:41 -06008129 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07008130 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06008131 ret = -EINVAL;
8132 if (nr_args != 1)
8133 break;
8134 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07008135 if (ret)
8136 break;
8137 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
8138 ctx->eventfd_async = 1;
8139 else
8140 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06008141 break;
8142 case IORING_UNREGISTER_EVENTFD:
8143 ret = -EINVAL;
8144 if (arg || nr_args)
8145 break;
8146 ret = io_eventfd_unregister(ctx);
8147 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07008148 case IORING_REGISTER_PROBE:
8149 ret = -EINVAL;
8150 if (!arg || nr_args > 256)
8151 break;
8152 ret = io_probe(ctx, arg, nr_args);
8153 break;
Jens Axboe071698e2020-01-28 10:04:42 -07008154 case IORING_REGISTER_PERSONALITY:
8155 ret = -EINVAL;
8156 if (arg || nr_args)
8157 break;
8158 ret = io_register_personality(ctx);
8159 break;
8160 case IORING_UNREGISTER_PERSONALITY:
8161 ret = -EINVAL;
8162 if (arg)
8163 break;
8164 ret = io_unregister_personality(ctx, nr_args);
8165 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008166 default:
8167 ret = -EINVAL;
8168 break;
8169 }
8170
Jens Axboe071698e2020-01-28 10:04:42 -07008171 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008172 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07008173 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07008174out:
Jens Axboe0f158b42020-05-14 17:18:39 -06008175 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008176 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008177 return ret;
8178}
8179
8180SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
8181 void __user *, arg, unsigned int, nr_args)
8182{
8183 struct io_ring_ctx *ctx;
8184 long ret = -EBADF;
8185 struct fd f;
8186
8187 f = fdget(fd);
8188 if (!f.file)
8189 return -EBADF;
8190
8191 ret = -EOPNOTSUPP;
8192 if (f.file->f_op != &io_uring_fops)
8193 goto out_fput;
8194
8195 ctx = f.file->private_data;
8196
8197 mutex_lock(&ctx->uring_lock);
8198 ret = __io_uring_register(ctx, opcode, arg, nr_args);
8199 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02008200 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
8201 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07008202out_fput:
8203 fdput(f);
8204 return ret;
8205}
8206
Jens Axboe2b188cc2019-01-07 10:46:33 -07008207static int __init io_uring_init(void)
8208{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008209#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
8210 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
8211 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
8212} while (0)
8213
8214#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
8215 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
8216 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
8217 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
8218 BUILD_BUG_SQE_ELEM(1, __u8, flags);
8219 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
8220 BUILD_BUG_SQE_ELEM(4, __s32, fd);
8221 BUILD_BUG_SQE_ELEM(8, __u64, off);
8222 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
8223 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008224 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008225 BUILD_BUG_SQE_ELEM(24, __u32, len);
8226 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
8227 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
8228 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
8229 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
8230 BUILD_BUG_SQE_ELEM(28, __u16, poll_events);
8231 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
8232 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
8233 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
8234 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
8235 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
8236 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
8237 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
8238 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008239 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008240 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
8241 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
8242 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008243 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008244
Jens Axboed3656342019-12-18 09:50:26 -07008245 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07008246 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008247 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
8248 return 0;
8249};
8250__initcall(io_uring_init);