blob: 358f97be9c7ba3cea4b70790cf3b19a1753531d7 [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 /*
145 * Runtime flags
146 *
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 /*
155 * Number of completion events lost because the queue was full;
156 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800157 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200158 * the completion queue.
159 *
160 * Written by the kernel, shouldn't be modified by the
161 * application (i.e. get number of "new events" by comparing to
162 * cached value).
163 *
164 * As completion events come in out of order this counter is not
165 * ordered with any other data.
166 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000167 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200168 /*
169 * Ring buffer of completion events.
170 *
171 * The kernel writes completion events fresh every time they are
172 * produced, so the application is allowed to modify pending
173 * entries.
174 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000175 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700176};
177
Jens Axboeedafcce2019-01-09 09:16:05 -0700178struct io_mapped_ubuf {
179 u64 ubuf;
180 size_t len;
181 struct bio_vec *bvec;
182 unsigned int nr_bvecs;
183};
184
Jens Axboe65e19f52019-10-26 07:20:21 -0600185struct fixed_file_table {
186 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700187};
188
Jens Axboe05f3fb32019-12-09 11:22:50 -0700189struct fixed_file_data {
190 struct fixed_file_table *table;
191 struct io_ring_ctx *ctx;
192
193 struct percpu_ref refs;
194 struct llist_head put_llist;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700195 struct work_struct ref_work;
196 struct completion done;
197};
198
Jens Axboe5a2e7452020-02-23 16:23:11 -0700199struct io_buffer {
200 struct list_head list;
201 __u64 addr;
202 __s32 len;
203 __u16 bid;
204};
205
Jens Axboe2b188cc2019-01-07 10:46:33 -0700206struct io_ring_ctx {
207 struct {
208 struct percpu_ref refs;
209 } ____cacheline_aligned_in_smp;
210
211 struct {
212 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800213 unsigned int compat: 1;
214 unsigned int account_mem: 1;
215 unsigned int cq_overflow_flushed: 1;
216 unsigned int drain_next: 1;
217 unsigned int eventfd_async: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700218
Hristo Venev75b28af2019-08-26 17:23:46 +0000219 /*
220 * Ring buffer of indices into array of io_uring_sqe, which is
221 * mmapped by the application using the IORING_OFF_SQES offset.
222 *
223 * This indirection could e.g. be used to assign fixed
224 * io_uring_sqe entries to operations and only submit them to
225 * the queue when needed.
226 *
227 * The kernel modifies neither the indices array nor the entries
228 * array.
229 */
230 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700231 unsigned cached_sq_head;
232 unsigned sq_entries;
233 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700234 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600235 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700236 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700237 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600238
239 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600240 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700241 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700242
Jens Axboefcb323c2019-10-24 12:39:47 -0600243 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700244 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700245 } ____cacheline_aligned_in_smp;
246
Hristo Venev75b28af2019-08-26 17:23:46 +0000247 struct io_rings *rings;
248
Jens Axboe2b188cc2019-01-07 10:46:33 -0700249 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600250 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700251 struct task_struct *sqo_thread; /* if using sq thread polling */
252 struct mm_struct *sqo_mm;
253 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700254
Jens Axboe6b063142019-01-10 22:13:58 -0700255 /*
256 * If used, fixed file set. Writers must ensure that ->refs is dead,
257 * readers must ensure that ->refs is alive as long as the file* is
258 * used. Only updated through io_uring_register(2).
259 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700260 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700261 unsigned nr_user_files;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300262 int ring_fd;
263 struct file *ring_file;
Jens Axboe6b063142019-01-10 22:13:58 -0700264
Jens Axboeedafcce2019-01-09 09:16:05 -0700265 /* if used, fixed mapped user buffers */
266 unsigned nr_user_bufs;
267 struct io_mapped_ubuf *user_bufs;
268
Jens Axboe2b188cc2019-01-07 10:46:33 -0700269 struct user_struct *user;
270
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700271 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700272
Jens Axboe206aefd2019-11-07 18:27:42 -0700273 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
274 struct completion *completions;
275
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700276 /* if all else fails... */
277 struct io_kiocb *fallback_req;
278
Jens Axboe206aefd2019-11-07 18:27:42 -0700279#if defined(CONFIG_UNIX)
280 struct socket *ring_sock;
281#endif
282
Jens Axboe5a2e7452020-02-23 16:23:11 -0700283 struct idr io_buffer_idr;
284
Jens Axboe071698e2020-01-28 10:04:42 -0700285 struct idr personality_idr;
286
Jens Axboe206aefd2019-11-07 18:27:42 -0700287 struct {
288 unsigned cached_cq_tail;
289 unsigned cq_entries;
290 unsigned cq_mask;
291 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700292 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700293 struct wait_queue_head cq_wait;
294 struct fasync_struct *cq_fasync;
295 struct eventfd_ctx *cq_ev_fd;
296 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700297
298 struct {
299 struct mutex uring_lock;
300 wait_queue_head_t wait;
301 } ____cacheline_aligned_in_smp;
302
303 struct {
304 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700305
Jens Axboedef596e2019-01-09 08:59:42 -0700306 /*
307 * ->poll_list is protected by the ctx->uring_lock for
308 * io_uring instances that don't use IORING_SETUP_SQPOLL.
309 * For SQPOLL, only the single threaded io_sq_thread() will
310 * manipulate the list, hence no extra locking is needed there.
311 */
312 struct list_head poll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700313 struct hlist_head *cancel_hash;
314 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700315 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600316
317 spinlock_t inflight_lock;
318 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700319 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700320};
321
Jens Axboe09bb8392019-03-13 12:39:28 -0600322/*
323 * First field must be the file pointer in all the
324 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
325 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700326struct io_poll_iocb {
327 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700328 union {
329 struct wait_queue_head *head;
330 u64 addr;
331 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700332 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600333 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700334 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700335 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700336};
337
Jens Axboeb5dba592019-12-11 14:02:38 -0700338struct io_close {
339 struct file *file;
340 struct file *put_file;
341 int fd;
342};
343
Jens Axboead8a48a2019-11-15 08:49:11 -0700344struct io_timeout_data {
345 struct io_kiocb *req;
346 struct hrtimer timer;
347 struct timespec64 ts;
348 enum hrtimer_mode mode;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +0300349 u32 seq_offset;
Jens Axboead8a48a2019-11-15 08:49:11 -0700350};
351
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700352struct io_accept {
353 struct file *file;
354 struct sockaddr __user *addr;
355 int __user *addr_len;
356 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600357 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700358};
359
360struct io_sync {
361 struct file *file;
362 loff_t len;
363 loff_t off;
364 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700365 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700366};
367
Jens Axboefbf23842019-12-17 18:45:56 -0700368struct io_cancel {
369 struct file *file;
370 u64 addr;
371};
372
Jens Axboeb29472e2019-12-17 18:50:29 -0700373struct io_timeout {
374 struct file *file;
375 u64 addr;
376 int flags;
Jens Axboe26a61672019-12-20 09:02:01 -0700377 unsigned count;
Jens Axboeb29472e2019-12-17 18:50:29 -0700378};
379
Jens Axboe9adbd452019-12-20 08:45:55 -0700380struct io_rw {
381 /* NOTE: kiocb has the file as the first member, so don't do it here */
382 struct kiocb kiocb;
383 u64 addr;
384 u64 len;
385};
386
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700387struct io_connect {
388 struct file *file;
389 struct sockaddr __user *addr;
390 int addr_len;
391};
392
Jens Axboee47293f2019-12-20 08:58:21 -0700393struct io_sr_msg {
394 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700395 union {
396 struct user_msghdr __user *msg;
397 void __user *buf;
398 };
Jens Axboee47293f2019-12-20 08:58:21 -0700399 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700400 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700401 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700402 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700403};
404
Jens Axboe15b71ab2019-12-11 11:20:36 -0700405struct io_open {
406 struct file *file;
407 int dfd;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700408 union {
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700409 unsigned mask;
410 };
Jens Axboe15b71ab2019-12-11 11:20:36 -0700411 struct filename *filename;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700412 struct statx __user *buffer;
Jens Axboec12cedf2020-01-08 17:41:21 -0700413 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600414 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700415};
416
Jens Axboe05f3fb32019-12-09 11:22:50 -0700417struct io_files_update {
418 struct file *file;
419 u64 arg;
420 u32 nr_args;
421 u32 offset;
422};
423
Jens Axboe4840e412019-12-25 22:03:45 -0700424struct io_fadvise {
425 struct file *file;
426 u64 offset;
427 u32 len;
428 u32 advice;
429};
430
Jens Axboec1ca7572019-12-25 22:18:28 -0700431struct io_madvise {
432 struct file *file;
433 u64 addr;
434 u32 len;
435 u32 advice;
436};
437
Jens Axboe3e4827b2020-01-08 15:18:09 -0700438struct io_epoll {
439 struct file *file;
440 int epfd;
441 int op;
442 int fd;
443 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700444};
445
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300446struct io_splice {
447 struct file *file_out;
448 struct file *file_in;
449 loff_t off_out;
450 loff_t off_in;
451 u64 len;
452 unsigned int flags;
453};
454
Jens Axboeddf0322d2020-02-23 16:41:33 -0700455struct io_provide_buf {
456 struct file *file;
457 __u64 addr;
458 __s32 len;
459 __u32 bgid;
460 __u16 nbufs;
461 __u16 bid;
462};
463
Jens Axboef499a022019-12-02 16:28:46 -0700464struct io_async_connect {
465 struct sockaddr_storage address;
466};
467
Jens Axboe03b12302019-12-02 18:50:25 -0700468struct io_async_msghdr {
469 struct iovec fast_iov[UIO_FASTIOV];
470 struct iovec *iov;
471 struct sockaddr __user *uaddr;
472 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700473 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700474};
475
Jens Axboef67676d2019-12-02 11:03:47 -0700476struct io_async_rw {
477 struct iovec fast_iov[UIO_FASTIOV];
478 struct iovec *iov;
479 ssize_t nr_segs;
480 ssize_t size;
481};
482
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700483struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700484 union {
485 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700486 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700487 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700488 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700489 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700490};
491
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300492enum {
493 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
494 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
495 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
496 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
497 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700498 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300499
500 REQ_F_LINK_NEXT_BIT,
501 REQ_F_FAIL_LINK_BIT,
502 REQ_F_INFLIGHT_BIT,
503 REQ_F_CUR_POS_BIT,
504 REQ_F_NOWAIT_BIT,
505 REQ_F_IOPOLL_COMPLETED_BIT,
506 REQ_F_LINK_TIMEOUT_BIT,
507 REQ_F_TIMEOUT_BIT,
508 REQ_F_ISREG_BIT,
509 REQ_F_MUST_PUNT_BIT,
510 REQ_F_TIMEOUT_NOSEQ_BIT,
511 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300512 REQ_F_NEED_CLEANUP_BIT,
Jens Axboe2ca10252020-02-13 17:17:35 -0700513 REQ_F_OVERFLOW_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700514 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700515 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700516
517 /* not a real bit, just to check we're not overflowing the space */
518 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300519};
520
521enum {
522 /* ctx owns file */
523 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
524 /* drain existing IO first */
525 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
526 /* linked sqes */
527 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
528 /* doesn't sever on completion < 0 */
529 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
530 /* IOSQE_ASYNC */
531 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700532 /* IOSQE_BUFFER_SELECT */
533 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300534
535 /* already grabbed next link */
536 REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT),
537 /* fail rest of links */
538 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
539 /* on inflight list */
540 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
541 /* read/write uses file position */
542 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
543 /* must not punt to workers */
544 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
545 /* polled IO has completed */
546 REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT),
547 /* has linked timeout */
548 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
549 /* timeout request */
550 REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT),
551 /* regular file */
552 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
553 /* must be punted even for NONBLOCK */
554 REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT),
555 /* no timeout sequence */
556 REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT),
557 /* completion under lock */
558 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300559 /* needs cleanup */
560 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboe2ca10252020-02-13 17:17:35 -0700561 /* in overflow list */
562 REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700563 /* already went through poll handler */
564 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700565 /* buffer already selected */
566 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700567};
568
569struct async_poll {
570 struct io_poll_iocb poll;
571 struct io_wq_work work;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300572};
573
Jens Axboe09bb8392019-03-13 12:39:28 -0600574/*
575 * NOTE! Each of the iocb union members has the file pointer
576 * as the first entry in their struct definition. So you can
577 * access the file pointer through any of the sub-structs,
578 * or directly as just 'ki_filp' in this struct.
579 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700580struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700581 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600582 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700583 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700584 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700585 struct io_accept accept;
586 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700587 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700588 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700589 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700590 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700591 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700592 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700593 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700594 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700595 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700596 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300597 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700598 struct io_provide_buf pbuf;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700599 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700600
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700601 struct io_async_ctx *io;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300602 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700603 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700604
605 struct io_ring_ctx *ctx;
Jens Axboed7718a92020-02-14 22:23:12 -0700606 struct list_head list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700607 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700608 refcount_t refs;
Jens Axboe4ed734b2020-03-20 11:23:41 -0600609 union {
610 struct task_struct *task;
611 unsigned long fsize;
612 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700613 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600614 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600615 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700616
Jens Axboed7718a92020-02-14 22:23:12 -0700617 struct list_head link_list;
618
Jens Axboefcb323c2019-10-24 12:39:47 -0600619 struct list_head inflight_entry;
620
Jens Axboeb41e9852020-02-17 09:52:41 -0700621 union {
622 /*
623 * Only commands that never go async can use the below fields,
Jens Axboed7718a92020-02-14 22:23:12 -0700624 * obviously. Right now only IORING_OP_POLL_ADD uses them, and
625 * async armed poll handlers for regular commands. The latter
626 * restore the work, if needed.
Jens Axboeb41e9852020-02-17 09:52:41 -0700627 */
628 struct {
Jens Axboeb41e9852020-02-17 09:52:41 -0700629 struct callback_head task_work;
Jens Axboed7718a92020-02-14 22:23:12 -0700630 struct hlist_node hash_node;
631 struct async_poll *apoll;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700632 int cflags;
Jens Axboeb41e9852020-02-17 09:52:41 -0700633 };
634 struct io_wq_work work;
635 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700636};
637
638#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700639#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700640
Jens Axboe9a56a232019-01-09 09:06:50 -0700641struct io_submit_state {
642 struct blk_plug plug;
643
644 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700645 * io_kiocb alloc cache
646 */
647 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300648 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700649
650 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700651 * File reference cache
652 */
653 struct file *file;
654 unsigned int fd;
655 unsigned int has_refs;
656 unsigned int used_refs;
657 unsigned int ios_left;
658};
659
Jens Axboed3656342019-12-18 09:50:26 -0700660struct io_op_def {
661 /* needs req->io allocated for deferral/async */
662 unsigned async_ctx : 1;
663 /* needs current->mm setup, does mm access */
664 unsigned needs_mm : 1;
665 /* needs req->file assigned */
666 unsigned needs_file : 1;
667 /* needs req->file assigned IFF fd is >= 0 */
668 unsigned fd_non_neg : 1;
669 /* hash wq insertion if file is a regular file */
670 unsigned hash_reg_file : 1;
671 /* unbound wq insertion if file is a non-regular file */
672 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700673 /* opcode is not supported by this kernel */
674 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700675 /* needs file table */
676 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700677 /* needs ->fs */
678 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700679 /* set if opcode supports polled "wait" */
680 unsigned pollin : 1;
681 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700682 /* op supports buffer selection */
683 unsigned buffer_select : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700684};
685
686static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300687 [IORING_OP_NOP] = {},
688 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700689 .async_ctx = 1,
690 .needs_mm = 1,
691 .needs_file = 1,
692 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700693 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700694 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700695 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300696 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700697 .async_ctx = 1,
698 .needs_mm = 1,
699 .needs_file = 1,
700 .hash_reg_file = 1,
701 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700702 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700703 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300704 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700705 .needs_file = 1,
706 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300707 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700708 .needs_file = 1,
709 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700710 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700711 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300712 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700713 .needs_file = 1,
714 .hash_reg_file = 1,
715 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700716 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700717 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300718 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700719 .needs_file = 1,
720 .unbound_nonreg_file = 1,
721 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300722 [IORING_OP_POLL_REMOVE] = {},
723 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700724 .needs_file = 1,
725 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300726 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700727 .async_ctx = 1,
728 .needs_mm = 1,
729 .needs_file = 1,
730 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700731 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700732 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700733 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300734 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700735 .async_ctx = 1,
736 .needs_mm = 1,
737 .needs_file = 1,
738 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700739 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700740 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700741 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700742 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300743 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700744 .async_ctx = 1,
745 .needs_mm = 1,
746 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300747 [IORING_OP_TIMEOUT_REMOVE] = {},
748 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700749 .needs_mm = 1,
750 .needs_file = 1,
751 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700752 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700753 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700754 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300755 [IORING_OP_ASYNC_CANCEL] = {},
756 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700757 .async_ctx = 1,
758 .needs_mm = 1,
759 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300760 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700761 .async_ctx = 1,
762 .needs_mm = 1,
763 .needs_file = 1,
764 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700765 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700766 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300767 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700768 .needs_file = 1,
769 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300770 [IORING_OP_OPENAT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700771 .needs_file = 1,
772 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700773 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700774 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700775 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300776 [IORING_OP_CLOSE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700777 .needs_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700778 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700779 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300780 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700781 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700782 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700783 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300784 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700785 .needs_mm = 1,
786 .needs_file = 1,
787 .fd_non_neg = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700788 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700789 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300790 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700791 .needs_mm = 1,
792 .needs_file = 1,
793 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700794 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700795 .buffer_select = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700796 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300797 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700798 .needs_mm = 1,
799 .needs_file = 1,
800 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700801 .pollout = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700802 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300803 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700804 .needs_file = 1,
805 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300806 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700807 .needs_mm = 1,
808 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300809 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700810 .needs_mm = 1,
811 .needs_file = 1,
812 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700813 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700814 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300815 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700816 .needs_mm = 1,
817 .needs_file = 1,
818 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700819 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700820 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700821 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300822 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -0700823 .needs_file = 1,
824 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700825 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700826 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700827 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700828 [IORING_OP_EPOLL_CTL] = {
829 .unbound_nonreg_file = 1,
830 .file_table = 1,
831 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300832 [IORING_OP_SPLICE] = {
833 .needs_file = 1,
834 .hash_reg_file = 1,
835 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700836 },
837 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700838 [IORING_OP_REMOVE_BUFFERS] = {},
Jens Axboed3656342019-12-18 09:50:26 -0700839};
840
Jens Axboe561fb042019-10-24 07:25:42 -0600841static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700842static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800843static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700844static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700845static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
846static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700847static int __io_sqe_files_update(struct io_ring_ctx *ctx,
848 struct io_uring_files_update *ip,
849 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700850static int io_grab_files(struct io_kiocb *req);
Jens Axboe2faf8522020-02-04 19:54:55 -0700851static void io_ring_file_ref_flush(struct fixed_file_data *data);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300852static void io_cleanup_req(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700853static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
854 int fd, struct file **out_file, bool fixed);
855static void __io_queue_sqe(struct io_kiocb *req,
856 const struct io_uring_sqe *sqe);
Jens Axboede0617e2019-04-06 21:51:27 -0600857
Jens Axboe2b188cc2019-01-07 10:46:33 -0700858static struct kmem_cache *req_cachep;
859
860static const struct file_operations io_uring_fops;
861
862struct sock *io_uring_get_socket(struct file *file)
863{
864#if defined(CONFIG_UNIX)
865 if (file->f_op == &io_uring_fops) {
866 struct io_ring_ctx *ctx = file->private_data;
867
868 return ctx->ring_sock->sk;
869 }
870#endif
871 return NULL;
872}
873EXPORT_SYMBOL(io_uring_get_socket);
874
875static void io_ring_ctx_ref_free(struct percpu_ref *ref)
876{
877 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
878
Jens Axboe206aefd2019-11-07 18:27:42 -0700879 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700880}
881
882static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
883{
884 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700885 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700886
887 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
888 if (!ctx)
889 return NULL;
890
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700891 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
892 if (!ctx->fallback_req)
893 goto err;
894
Jens Axboe206aefd2019-11-07 18:27:42 -0700895 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
896 if (!ctx->completions)
897 goto err;
898
Jens Axboe78076bb2019-12-04 19:56:40 -0700899 /*
900 * Use 5 bits less than the max cq entries, that should give us around
901 * 32 entries per hash list if totally full and uniformly spread.
902 */
903 hash_bits = ilog2(p->cq_entries);
904 hash_bits -= 5;
905 if (hash_bits <= 0)
906 hash_bits = 1;
907 ctx->cancel_hash_bits = hash_bits;
908 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
909 GFP_KERNEL);
910 if (!ctx->cancel_hash)
911 goto err;
912 __hash_init(ctx->cancel_hash, 1U << hash_bits);
913
Roman Gushchin21482892019-05-07 10:01:48 -0700914 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700915 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
916 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700917
918 ctx->flags = p->flags;
919 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700920 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700921 init_completion(&ctx->completions[0]);
922 init_completion(&ctx->completions[1]);
Jens Axboe5a2e7452020-02-23 16:23:11 -0700923 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -0700924 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700925 mutex_init(&ctx->uring_lock);
926 init_waitqueue_head(&ctx->wait);
927 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700928 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600929 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600930 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600931 init_waitqueue_head(&ctx->inflight_wait);
932 spin_lock_init(&ctx->inflight_lock);
933 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700934 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700935err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700936 if (ctx->fallback_req)
937 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700938 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700939 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700940 kfree(ctx);
941 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700942}
943
Bob Liu9d858b22019-11-13 18:06:25 +0800944static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600945{
Jackie Liua197f662019-11-08 08:09:12 -0700946 struct io_ring_ctx *ctx = req->ctx;
947
Jens Axboe498ccd92019-10-25 10:04:25 -0600948 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
949 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600950}
951
Bob Liu9d858b22019-11-13 18:06:25 +0800952static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600953{
Pavel Begunkov87987892020-01-18 01:22:30 +0300954 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +0800955 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600956
Bob Liu9d858b22019-11-13 18:06:25 +0800957 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600958}
959
960static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600961{
962 struct io_kiocb *req;
963
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600964 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800965 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600966 list_del_init(&req->list);
967 return req;
968 }
969
970 return NULL;
971}
972
Jens Axboe5262f562019-09-17 12:26:57 -0600973static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
974{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600975 struct io_kiocb *req;
976
977 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700978 if (req) {
979 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
980 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800981 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700982 list_del_init(&req->list);
983 return req;
984 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600985 }
986
987 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600988}
989
Jens Axboede0617e2019-04-06 21:51:27 -0600990static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700991{
Hristo Venev75b28af2019-08-26 17:23:46 +0000992 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700993
Pavel Begunkov07910152020-01-17 03:52:46 +0300994 /* order cqe stores with ring update */
995 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700996
Pavel Begunkov07910152020-01-17 03:52:46 +0300997 if (wq_has_sleeper(&ctx->cq_wait)) {
998 wake_up_interruptible(&ctx->cq_wait);
999 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001000 }
1001}
1002
Jens Axboecccf0ee2020-01-27 16:34:48 -07001003static inline void io_req_work_grab_env(struct io_kiocb *req,
1004 const struct io_op_def *def)
Jens Axboe18d9be12019-09-10 09:13:05 -06001005{
Jens Axboecccf0ee2020-01-27 16:34:48 -07001006 if (!req->work.mm && def->needs_mm) {
1007 mmgrab(current->mm);
1008 req->work.mm = current->mm;
1009 }
1010 if (!req->work.creds)
1011 req->work.creds = get_current_cred();
Jens Axboeff002b32020-02-07 16:05:21 -07001012 if (!req->work.fs && def->needs_fs) {
1013 spin_lock(&current->fs->lock);
1014 if (!current->fs->in_exec) {
1015 req->work.fs = current->fs;
1016 req->work.fs->users++;
1017 } else {
1018 req->work.flags |= IO_WQ_WORK_CANCEL;
1019 }
1020 spin_unlock(&current->fs->lock);
1021 }
Jens Axboe6ab23142020-02-08 20:23:59 -07001022 if (!req->work.task_pid)
1023 req->work.task_pid = task_pid_vnr(current);
Jens Axboecccf0ee2020-01-27 16:34:48 -07001024}
1025
1026static inline void io_req_work_drop_env(struct io_kiocb *req)
1027{
1028 if (req->work.mm) {
1029 mmdrop(req->work.mm);
1030 req->work.mm = NULL;
1031 }
1032 if (req->work.creds) {
1033 put_cred(req->work.creds);
1034 req->work.creds = NULL;
1035 }
Jens Axboeff002b32020-02-07 16:05:21 -07001036 if (req->work.fs) {
1037 struct fs_struct *fs = req->work.fs;
1038
1039 spin_lock(&req->work.fs->lock);
1040 if (--fs->users)
1041 fs = NULL;
1042 spin_unlock(&req->work.fs->lock);
1043 if (fs)
1044 free_fs_struct(fs);
1045 }
Jens Axboe561fb042019-10-24 07:25:42 -06001046}
1047
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001048static inline void io_prep_async_work(struct io_kiocb *req,
Jens Axboe94ae5e72019-11-14 19:39:52 -07001049 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -06001050{
Jens Axboed3656342019-12-18 09:50:26 -07001051 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe54a91f32019-09-10 09:15:04 -06001052
Jens Axboed3656342019-12-18 09:50:26 -07001053 if (req->flags & REQ_F_ISREG) {
1054 if (def->hash_reg_file)
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001055 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001056 } else {
1057 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001058 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001059 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001060
1061 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -06001062
Jens Axboe94ae5e72019-11-14 19:39:52 -07001063 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001064}
1065
Jackie Liua197f662019-11-08 08:09:12 -07001066static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001067{
Jackie Liua197f662019-11-08 08:09:12 -07001068 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001069 struct io_kiocb *link;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001070
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001071 io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06001072
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001073 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1074 &req->work, req->flags);
1075 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001076
1077 if (link)
1078 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -06001079}
1080
Jens Axboe5262f562019-09-17 12:26:57 -06001081static void io_kill_timeout(struct io_kiocb *req)
1082{
1083 int ret;
1084
Jens Axboe2d283902019-12-04 11:08:05 -07001085 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001086 if (ret != -1) {
1087 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -06001088 list_del_init(&req->list);
Pavel Begunkovf0e20b82020-03-07 01:15:22 +03001089 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001090 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001091 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001092 }
1093}
1094
1095static void io_kill_timeouts(struct io_ring_ctx *ctx)
1096{
1097 struct io_kiocb *req, *tmp;
1098
1099 spin_lock_irq(&ctx->completion_lock);
1100 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
1101 io_kill_timeout(req);
1102 spin_unlock_irq(&ctx->completion_lock);
1103}
1104
Jens Axboede0617e2019-04-06 21:51:27 -06001105static void io_commit_cqring(struct io_ring_ctx *ctx)
1106{
1107 struct io_kiocb *req;
1108
Jens Axboe5262f562019-09-17 12:26:57 -06001109 while ((req = io_get_timeout_req(ctx)) != NULL)
1110 io_kill_timeout(req);
1111
Jens Axboede0617e2019-04-06 21:51:27 -06001112 __io_commit_cqring(ctx);
1113
Pavel Begunkov87987892020-01-18 01:22:30 +03001114 while ((req = io_get_deferred_req(ctx)) != NULL)
Jackie Liua197f662019-11-08 08:09:12 -07001115 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -06001116}
1117
Jens Axboe2b188cc2019-01-07 10:46:33 -07001118static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1119{
Hristo Venev75b28af2019-08-26 17:23:46 +00001120 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001121 unsigned tail;
1122
1123 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001124 /*
1125 * writes to the cq entry need to come after reading head; the
1126 * control dependency is enough as we're using WRITE_ONCE to
1127 * fill the cq entry
1128 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001129 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001130 return NULL;
1131
1132 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001133 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001134}
1135
Jens Axboef2842ab2020-01-08 11:04:00 -07001136static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1137{
Jens Axboef0b493e2020-02-01 21:30:11 -07001138 if (!ctx->cq_ev_fd)
1139 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001140 if (!ctx->eventfd_async)
1141 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001142 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001143}
1144
Jens Axboeb41e9852020-02-17 09:52:41 -07001145static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001146{
1147 if (waitqueue_active(&ctx->wait))
1148 wake_up(&ctx->wait);
1149 if (waitqueue_active(&ctx->sqo_wait))
1150 wake_up(&ctx->sqo_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001151 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001152 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001153}
1154
Jens Axboec4a2ed72019-11-21 21:01:26 -07001155/* Returns true if there are no backlogged entries after the flush */
1156static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001157{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001158 struct io_rings *rings = ctx->rings;
1159 struct io_uring_cqe *cqe;
1160 struct io_kiocb *req;
1161 unsigned long flags;
1162 LIST_HEAD(list);
1163
1164 if (!force) {
1165 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001166 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001167 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1168 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001169 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001170 }
1171
1172 spin_lock_irqsave(&ctx->completion_lock, flags);
1173
1174 /* if force is set, the ring is going away. always drop after that */
1175 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001176 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001177
Jens Axboec4a2ed72019-11-21 21:01:26 -07001178 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001179 while (!list_empty(&ctx->cq_overflow_list)) {
1180 cqe = io_get_cqring(ctx);
1181 if (!cqe && !force)
1182 break;
1183
1184 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1185 list);
1186 list_move(&req->list, &list);
Jens Axboe2ca10252020-02-13 17:17:35 -07001187 req->flags &= ~REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001188 if (cqe) {
1189 WRITE_ONCE(cqe->user_data, req->user_data);
1190 WRITE_ONCE(cqe->res, req->result);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001191 WRITE_ONCE(cqe->flags, req->cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001192 } else {
1193 WRITE_ONCE(ctx->rings->cq_overflow,
1194 atomic_inc_return(&ctx->cached_cq_overflow));
1195 }
1196 }
1197
1198 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001199 if (cqe) {
1200 clear_bit(0, &ctx->sq_check_overflow);
1201 clear_bit(0, &ctx->cq_check_overflow);
1202 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001203 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1204 io_cqring_ev_posted(ctx);
1205
1206 while (!list_empty(&list)) {
1207 req = list_first_entry(&list, struct io_kiocb, list);
1208 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001209 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001210 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001211
1212 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001213}
1214
Jens Axboebcda7ba2020-02-23 16:42:51 -07001215static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001216{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001217 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001218 struct io_uring_cqe *cqe;
1219
Jens Axboe78e19bb2019-11-06 15:21:34 -07001220 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001221
Jens Axboe2b188cc2019-01-07 10:46:33 -07001222 /*
1223 * If we can't get a cq entry, userspace overflowed the
1224 * submission (by quite a lot). Increment the overflow count in
1225 * the ring.
1226 */
1227 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001228 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001229 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001230 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001231 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001232 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001233 WRITE_ONCE(ctx->rings->cq_overflow,
1234 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001235 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001236 if (list_empty(&ctx->cq_overflow_list)) {
1237 set_bit(0, &ctx->sq_check_overflow);
1238 set_bit(0, &ctx->cq_check_overflow);
1239 }
Jens Axboe2ca10252020-02-13 17:17:35 -07001240 req->flags |= REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001241 refcount_inc(&req->refs);
1242 req->result = res;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001243 req->cflags = cflags;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001244 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001245 }
1246}
1247
Jens Axboebcda7ba2020-02-23 16:42:51 -07001248static void io_cqring_fill_event(struct io_kiocb *req, long res)
1249{
1250 __io_cqring_fill_event(req, res, 0);
1251}
1252
1253static void __io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001254{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001255 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001256 unsigned long flags;
1257
1258 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001259 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001260 io_commit_cqring(ctx);
1261 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1262
Jens Axboe8c838782019-03-12 15:48:16 -06001263 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001264}
1265
Jens Axboebcda7ba2020-02-23 16:42:51 -07001266static void io_cqring_add_event(struct io_kiocb *req, long res)
1267{
1268 __io_cqring_add_event(req, res, 0);
1269}
1270
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001271static inline bool io_is_fallback_req(struct io_kiocb *req)
1272{
1273 return req == (struct io_kiocb *)
1274 ((unsigned long) req->ctx->fallback_req & ~1UL);
1275}
1276
1277static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1278{
1279 struct io_kiocb *req;
1280
1281 req = ctx->fallback_req;
1282 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1283 return req;
1284
1285 return NULL;
1286}
1287
Jens Axboe2579f912019-01-09 09:10:43 -07001288static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1289 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001290{
Jens Axboefd6fab22019-03-14 16:30:06 -06001291 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001292 struct io_kiocb *req;
1293
Jens Axboe2579f912019-01-09 09:10:43 -07001294 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001295 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001296 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001297 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001298 } else if (!state->free_reqs) {
1299 size_t sz;
1300 int ret;
1301
1302 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001303 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1304
1305 /*
1306 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1307 * retry single alloc to be on the safe side.
1308 */
1309 if (unlikely(ret <= 0)) {
1310 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1311 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001312 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001313 ret = 1;
1314 }
Jens Axboe2579f912019-01-09 09:10:43 -07001315 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001316 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001317 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001318 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001319 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001320 }
1321
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001322got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001323 req->io = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001324 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001325 req->ctx = ctx;
1326 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001327 /* one is dropped after submission, the other at completion */
1328 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001329 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001330 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001331 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001332fallback:
1333 req = io_get_fallback_req(ctx);
1334 if (req)
1335 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001336 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001337 return NULL;
1338}
1339
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001340static inline void io_put_file(struct io_kiocb *req, struct file *file,
1341 bool fixed)
1342{
1343 if (fixed)
1344 percpu_ref_put(&req->ctx->file_data->refs);
1345 else
1346 fput(file);
1347}
1348
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001349static void __io_req_do_free(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07001350{
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001351 if (likely(!io_is_fallback_req(req)))
1352 kmem_cache_free(req_cachep, req);
1353 else
1354 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
1355}
1356
Jens Axboec6ca97b302019-12-28 12:11:08 -07001357static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001358{
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001359 if (req->flags & REQ_F_NEED_CLEANUP)
1360 io_cleanup_req(req);
1361
YueHaibing96fd84d2020-01-07 22:22:44 +08001362 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001363 if (req->file)
1364 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboecccf0ee2020-01-27 16:34:48 -07001365
1366 io_req_work_drop_env(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001367}
1368
1369static void __io_free_req(struct io_kiocb *req)
1370{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001371 __io_req_aux_free(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06001372
Jens Axboefcb323c2019-10-24 12:39:47 -06001373 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001374 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001375 unsigned long flags;
1376
1377 spin_lock_irqsave(&ctx->inflight_lock, flags);
1378 list_del(&req->inflight_entry);
1379 if (waitqueue_active(&ctx->inflight_wait))
1380 wake_up(&ctx->inflight_wait);
1381 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1382 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001383
1384 percpu_ref_put(&req->ctx->refs);
1385 __io_req_do_free(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001386}
1387
Jens Axboec6ca97b302019-12-28 12:11:08 -07001388struct req_batch {
1389 void *reqs[IO_IOPOLL_BATCH];
1390 int to_free;
1391 int need_iter;
1392};
1393
1394static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1395{
Jens Axboe10fef4b2020-01-09 07:52:28 -07001396 int fixed_refs = rb->to_free;
1397
Jens Axboec6ca97b302019-12-28 12:11:08 -07001398 if (!rb->to_free)
1399 return;
1400 if (rb->need_iter) {
1401 int i, inflight = 0;
1402 unsigned long flags;
1403
Jens Axboe10fef4b2020-01-09 07:52:28 -07001404 fixed_refs = 0;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001405 for (i = 0; i < rb->to_free; i++) {
1406 struct io_kiocb *req = rb->reqs[i];
1407
Jens Axboe10fef4b2020-01-09 07:52:28 -07001408 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001409 req->file = NULL;
Jens Axboe10fef4b2020-01-09 07:52:28 -07001410 fixed_refs++;
1411 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001412 if (req->flags & REQ_F_INFLIGHT)
1413 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001414 __io_req_aux_free(req);
1415 }
1416 if (!inflight)
1417 goto do_free;
1418
1419 spin_lock_irqsave(&ctx->inflight_lock, flags);
1420 for (i = 0; i < rb->to_free; i++) {
1421 struct io_kiocb *req = rb->reqs[i];
1422
Jens Axboe10fef4b2020-01-09 07:52:28 -07001423 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001424 list_del(&req->inflight_entry);
1425 if (!--inflight)
1426 break;
1427 }
1428 }
1429 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1430
1431 if (waitqueue_active(&ctx->inflight_wait))
1432 wake_up(&ctx->inflight_wait);
1433 }
1434do_free:
1435 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001436 if (fixed_refs)
1437 percpu_ref_put_many(&ctx->file_data->refs, fixed_refs);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001438 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001439 rb->to_free = rb->need_iter = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001440}
1441
Jackie Liua197f662019-11-08 08:09:12 -07001442static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001443{
Jackie Liua197f662019-11-08 08:09:12 -07001444 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001445 int ret;
1446
Jens Axboe2d283902019-12-04 11:08:05 -07001447 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001448 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001449 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001450 io_commit_cqring(ctx);
1451 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001452 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001453 return true;
1454 }
1455
1456 return false;
1457}
1458
Jens Axboeba816ad2019-09-28 11:36:45 -06001459static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001460{
Jens Axboe2665abf2019-11-05 12:40:47 -07001461 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001462 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001463
Jens Axboe4d7dd462019-11-20 13:03:52 -07001464 /* Already got next link */
1465 if (req->flags & REQ_F_LINK_NEXT)
1466 return;
1467
Jens Axboe9e645e112019-05-10 16:07:28 -06001468 /*
1469 * The list should never be empty when we are called here. But could
1470 * potentially happen if the chain is messed up, check to be on the
1471 * safe side.
1472 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001473 while (!list_empty(&req->link_list)) {
1474 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1475 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001476
Pavel Begunkov44932332019-12-05 16:16:35 +03001477 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1478 (nxt->flags & REQ_F_TIMEOUT))) {
1479 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001480 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001481 req->flags &= ~REQ_F_LINK_TIMEOUT;
1482 continue;
1483 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001484
Pavel Begunkov44932332019-12-05 16:16:35 +03001485 list_del_init(&req->link_list);
1486 if (!list_empty(&nxt->link_list))
1487 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001488 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001489 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001490 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001491
Jens Axboe4d7dd462019-11-20 13:03:52 -07001492 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001493 if (wake_ev)
1494 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001495}
1496
1497/*
1498 * Called if REQ_F_LINK is set, and we fail the head request
1499 */
1500static void io_fail_links(struct io_kiocb *req)
1501{
Jens Axboe2665abf2019-11-05 12:40:47 -07001502 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001503 unsigned long flags;
1504
1505 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001506
1507 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001508 struct io_kiocb *link = list_first_entry(&req->link_list,
1509 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001510
Pavel Begunkov44932332019-12-05 16:16:35 +03001511 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001512 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001513
1514 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001515 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001516 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001517 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001518 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001519 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001520 }
Jens Axboe5d960722019-11-19 15:31:28 -07001521 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001522 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001523
1524 io_commit_cqring(ctx);
1525 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1526 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001527}
1528
Jens Axboe4d7dd462019-11-20 13:03:52 -07001529static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001530{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001531 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001532 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001533
Jens Axboe9e645e112019-05-10 16:07:28 -06001534 /*
1535 * If LINK is set, we have dependent requests in this chain. If we
1536 * didn't fail this request, queue the first one up, moving any other
1537 * dependencies to the next request. In case of failure, fail the rest
1538 * of the chain.
1539 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001540 if (req->flags & REQ_F_FAIL_LINK) {
1541 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001542 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1543 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001544 struct io_ring_ctx *ctx = req->ctx;
1545 unsigned long flags;
1546
1547 /*
1548 * If this is a timeout link, we could be racing with the
1549 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001550 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001551 */
1552 spin_lock_irqsave(&ctx->completion_lock, flags);
1553 io_req_link_next(req, nxt);
1554 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1555 } else {
1556 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001557 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001558}
Jens Axboe9e645e112019-05-10 16:07:28 -06001559
Jackie Liuc69f8db2019-11-09 11:00:08 +08001560static void io_free_req(struct io_kiocb *req)
1561{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001562 struct io_kiocb *nxt = NULL;
1563
1564 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001565 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001566
1567 if (nxt)
1568 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001569}
1570
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001571static void io_link_work_cb(struct io_wq_work **workptr)
1572{
Pavel Begunkov18a542f2020-03-23 00:23:29 +03001573 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
1574 struct io_kiocb *link;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001575
Pavel Begunkov18a542f2020-03-23 00:23:29 +03001576 link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001577 io_queue_linked_timeout(link);
1578 io_wq_submit_work(workptr);
1579}
1580
1581static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
1582{
1583 struct io_kiocb *link;
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001584 const struct io_op_def *def = &io_op_defs[nxt->opcode];
1585
1586 if ((nxt->flags & REQ_F_ISREG) && def->hash_reg_file)
1587 io_wq_hash_work(&nxt->work, file_inode(nxt->file));
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001588
1589 *workptr = &nxt->work;
1590 link = io_prep_linked_timeout(nxt);
Pavel Begunkov18a542f2020-03-23 00:23:29 +03001591 if (link)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001592 nxt->work.func = io_link_work_cb;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001593}
1594
Jens Axboeba816ad2019-09-28 11:36:45 -06001595/*
1596 * Drop reference to request, return next in chain (if there is one) if this
1597 * was the last reference to this request.
1598 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001599__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001600static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001601{
Jens Axboe2a44f462020-02-25 13:25:41 -07001602 if (refcount_dec_and_test(&req->refs)) {
1603 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001604 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001605 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001606}
1607
Jens Axboe2b188cc2019-01-07 10:46:33 -07001608static void io_put_req(struct io_kiocb *req)
1609{
Jens Axboedef596e2019-01-09 08:59:42 -07001610 if (refcount_dec_and_test(&req->refs))
1611 io_free_req(req);
1612}
1613
Pavel Begunkove9fd9392020-03-04 16:14:12 +03001614static void io_steal_work(struct io_kiocb *req,
1615 struct io_wq_work **workptr)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001616{
1617 /*
1618 * It's in an io-wq worker, so there always should be at least
1619 * one reference, which will be dropped in io_put_work() just
1620 * after the current handler returns.
1621 *
1622 * It also means, that if the counter dropped to 1, then there is
1623 * no asynchronous users left, so it's safe to steal the next work.
1624 */
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001625 if (refcount_read(&req->refs) == 1) {
1626 struct io_kiocb *nxt = NULL;
1627
1628 io_req_find_next(req, &nxt);
1629 if (nxt)
1630 io_wq_assign_next(workptr, nxt);
1631 }
1632}
1633
Jens Axboe978db572019-11-14 22:39:04 -07001634/*
1635 * Must only be used if we don't need to care about links, usually from
1636 * within the completion handling itself.
1637 */
1638static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001639{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001640 /* drop both submit and complete references */
1641 if (refcount_sub_and_test(2, &req->refs))
1642 __io_free_req(req);
1643}
1644
Jens Axboe978db572019-11-14 22:39:04 -07001645static void io_double_put_req(struct io_kiocb *req)
1646{
1647 /* drop both submit and complete references */
1648 if (refcount_sub_and_test(2, &req->refs))
1649 io_free_req(req);
1650}
1651
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001652static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001653{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001654 struct io_rings *rings = ctx->rings;
1655
Jens Axboead3eb2c2019-12-18 17:12:20 -07001656 if (test_bit(0, &ctx->cq_check_overflow)) {
1657 /*
1658 * noflush == true is from the waitqueue handler, just ensure
1659 * we wake up the task, and the next invocation will flush the
1660 * entries. We cannot safely to it from here.
1661 */
1662 if (noflush && !list_empty(&ctx->cq_overflow_list))
1663 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001664
Jens Axboead3eb2c2019-12-18 17:12:20 -07001665 io_cqring_overflow_flush(ctx, false);
1666 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001667
Jens Axboea3a0e432019-08-20 11:03:11 -06001668 /* See comment at the top of this file */
1669 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001670 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001671}
1672
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001673static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1674{
1675 struct io_rings *rings = ctx->rings;
1676
1677 /* make sure SQ entry isn't read before tail */
1678 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1679}
1680
Jens Axboe8237e042019-12-28 10:48:22 -07001681static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001682{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001683 if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req))
1684 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001685
Jens Axboec6ca97b302019-12-28 12:11:08 -07001686 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1687 rb->need_iter++;
1688
1689 rb->reqs[rb->to_free++] = req;
1690 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1691 io_free_req_many(req->ctx, rb);
1692 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001693}
1694
Jens Axboebcda7ba2020-02-23 16:42:51 -07001695static int io_put_kbuf(struct io_kiocb *req)
1696{
Jens Axboe4d954c22020-02-27 07:31:19 -07001697 struct io_buffer *kbuf;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001698 int cflags;
1699
Jens Axboe4d954c22020-02-27 07:31:19 -07001700 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001701 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
1702 cflags |= IORING_CQE_F_BUFFER;
1703 req->rw.addr = 0;
1704 kfree(kbuf);
1705 return cflags;
1706}
1707
Jens Axboedef596e2019-01-09 08:59:42 -07001708/*
1709 * Find and free completed poll iocbs
1710 */
1711static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1712 struct list_head *done)
1713{
Jens Axboe8237e042019-12-28 10:48:22 -07001714 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001715 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001716
Jens Axboec6ca97b302019-12-28 12:11:08 -07001717 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001718 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07001719 int cflags = 0;
1720
Jens Axboedef596e2019-01-09 08:59:42 -07001721 req = list_first_entry(done, struct io_kiocb, list);
1722 list_del(&req->list);
1723
Jens Axboebcda7ba2020-02-23 16:42:51 -07001724 if (req->flags & REQ_F_BUFFER_SELECTED)
1725 cflags = io_put_kbuf(req);
1726
1727 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07001728 (*nr_events)++;
1729
Jens Axboe8237e042019-12-28 10:48:22 -07001730 if (refcount_dec_and_test(&req->refs) &&
1731 !io_req_multi_free(&rb, req))
1732 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001733 }
Jens Axboedef596e2019-01-09 08:59:42 -07001734
Jens Axboe09bb8392019-03-13 12:39:28 -06001735 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08001736 if (ctx->flags & IORING_SETUP_SQPOLL)
1737 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001738 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001739}
1740
1741static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1742 long min)
1743{
1744 struct io_kiocb *req, *tmp;
1745 LIST_HEAD(done);
1746 bool spin;
1747 int ret;
1748
1749 /*
1750 * Only spin for completions if we don't have multiple devices hanging
1751 * off our complete list, and we're under the requested amount.
1752 */
1753 spin = !ctx->poll_multi_file && *nr_events < min;
1754
1755 ret = 0;
1756 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001757 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001758
1759 /*
1760 * Move completed entries to our local list. If we find a
1761 * request that requires polling, break out and complete
1762 * the done list first, if we have entries there.
1763 */
1764 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1765 list_move_tail(&req->list, &done);
1766 continue;
1767 }
1768 if (!list_empty(&done))
1769 break;
1770
1771 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1772 if (ret < 0)
1773 break;
1774
1775 if (ret && spin)
1776 spin = false;
1777 ret = 0;
1778 }
1779
1780 if (!list_empty(&done))
1781 io_iopoll_complete(ctx, nr_events, &done);
1782
1783 return ret;
1784}
1785
1786/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001787 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001788 * non-spinning poll check - we'll still enter the driver poll loop, but only
1789 * as a non-spinning completion check.
1790 */
1791static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1792 long min)
1793{
Jens Axboe08f54392019-08-21 22:19:11 -06001794 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001795 int ret;
1796
1797 ret = io_do_iopoll(ctx, nr_events, min);
1798 if (ret < 0)
1799 return ret;
1800 if (!min || *nr_events >= min)
1801 return 0;
1802 }
1803
1804 return 1;
1805}
1806
1807/*
1808 * We can't just wait for polled events to come to us, we have to actively
1809 * find and complete them.
1810 */
1811static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1812{
1813 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1814 return;
1815
1816 mutex_lock(&ctx->uring_lock);
1817 while (!list_empty(&ctx->poll_list)) {
1818 unsigned int nr_events = 0;
1819
1820 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001821
1822 /*
1823 * Ensure we allow local-to-the-cpu processing to take place,
1824 * in this case we need to ensure that we reap all events.
1825 */
1826 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001827 }
1828 mutex_unlock(&ctx->uring_lock);
1829}
1830
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001831static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1832 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001833{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001834 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001835
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001836 /*
1837 * We disallow the app entering submit/complete with polling, but we
1838 * still need to lock the ring to prevent racing with polled issue
1839 * that got punted to a workqueue.
1840 */
1841 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001842 do {
1843 int tmin = 0;
1844
Jens Axboe500f9fb2019-08-19 12:15:59 -06001845 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001846 * Don't enter poll loop if we already have events pending.
1847 * If we do, we can potentially be spinning for commands that
1848 * already triggered a CQE (eg in error).
1849 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001850 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001851 break;
1852
1853 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001854 * If a submit got punted to a workqueue, we can have the
1855 * application entering polling for a command before it gets
1856 * issued. That app will hold the uring_lock for the duration
1857 * of the poll right here, so we need to take a breather every
1858 * now and then to ensure that the issue has a chance to add
1859 * the poll to the issued list. Otherwise we can spin here
1860 * forever, while the workqueue is stuck trying to acquire the
1861 * very same mutex.
1862 */
1863 if (!(++iters & 7)) {
1864 mutex_unlock(&ctx->uring_lock);
1865 mutex_lock(&ctx->uring_lock);
1866 }
1867
Jens Axboedef596e2019-01-09 08:59:42 -07001868 if (*nr_events < min)
1869 tmin = min - *nr_events;
1870
1871 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1872 if (ret <= 0)
1873 break;
1874 ret = 0;
1875 } while (min && !*nr_events && !need_resched());
1876
Jens Axboe500f9fb2019-08-19 12:15:59 -06001877 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001878 return ret;
1879}
1880
Jens Axboe491381ce2019-10-17 09:20:46 -06001881static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001882{
Jens Axboe491381ce2019-10-17 09:20:46 -06001883 /*
1884 * Tell lockdep we inherited freeze protection from submission
1885 * thread.
1886 */
1887 if (req->flags & REQ_F_ISREG) {
1888 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001889
Jens Axboe491381ce2019-10-17 09:20:46 -06001890 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001891 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001892 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001893}
1894
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001895static inline void req_set_fail_links(struct io_kiocb *req)
1896{
1897 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1898 req->flags |= REQ_F_FAIL_LINK;
1899}
1900
Jens Axboeba816ad2019-09-28 11:36:45 -06001901static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001902{
Jens Axboe9adbd452019-12-20 08:45:55 -07001903 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001904 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001905
Jens Axboe491381ce2019-10-17 09:20:46 -06001906 if (kiocb->ki_flags & IOCB_WRITE)
1907 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001908
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001909 if (res != req->result)
1910 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001911 if (req->flags & REQ_F_BUFFER_SELECTED)
1912 cflags = io_put_kbuf(req);
1913 __io_cqring_add_event(req, res, cflags);
Jens Axboeba816ad2019-09-28 11:36:45 -06001914}
1915
1916static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1917{
Jens Axboe9adbd452019-12-20 08:45:55 -07001918 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001919
1920 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001921 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001922}
1923
Jens Axboedef596e2019-01-09 08:59:42 -07001924static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1925{
Jens Axboe9adbd452019-12-20 08:45:55 -07001926 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001927
Jens Axboe491381ce2019-10-17 09:20:46 -06001928 if (kiocb->ki_flags & IOCB_WRITE)
1929 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001930
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001931 if (res != req->result)
1932 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001933 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001934 if (res != -EAGAIN)
1935 req->flags |= REQ_F_IOPOLL_COMPLETED;
1936}
1937
1938/*
1939 * After the iocb has been issued, it's safe to be found on the poll list.
1940 * Adding the kiocb to the list AFTER submission ensures that we don't
1941 * find it from a io_iopoll_getevents() thread before the issuer is done
1942 * accessing the kiocb cookie.
1943 */
1944static void io_iopoll_req_issued(struct io_kiocb *req)
1945{
1946 struct io_ring_ctx *ctx = req->ctx;
1947
1948 /*
1949 * Track whether we have multiple files in our lists. This will impact
1950 * how we do polling eventually, not spinning if we're on potentially
1951 * different devices.
1952 */
1953 if (list_empty(&ctx->poll_list)) {
1954 ctx->poll_multi_file = false;
1955 } else if (!ctx->poll_multi_file) {
1956 struct io_kiocb *list_req;
1957
1958 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1959 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001960 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001961 ctx->poll_multi_file = true;
1962 }
1963
1964 /*
1965 * For fast devices, IO may have already completed. If it has, add
1966 * it to the front so we find it first.
1967 */
1968 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1969 list_add(&req->list, &ctx->poll_list);
1970 else
1971 list_add_tail(&req->list, &ctx->poll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08001972
1973 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
1974 wq_has_sleeper(&ctx->sqo_wait))
1975 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07001976}
1977
Jens Axboe3d6770f2019-04-13 11:50:54 -06001978static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001979{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001980 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001981 int diff = state->has_refs - state->used_refs;
1982
1983 if (diff)
1984 fput_many(state->file, diff);
1985 state->file = NULL;
1986 }
1987}
1988
1989/*
1990 * Get as many references to a file as we have IOs left in this submission,
1991 * assuming most submissions are for one file, or at least that each file
1992 * has more than one submission.
1993 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001994static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07001995{
1996 if (!state)
1997 return fget(fd);
1998
1999 if (state->file) {
2000 if (state->fd == fd) {
2001 state->used_refs++;
2002 state->ios_left--;
2003 return state->file;
2004 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06002005 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002006 }
2007 state->file = fget_many(fd, state->ios_left);
2008 if (!state->file)
2009 return NULL;
2010
2011 state->fd = fd;
2012 state->has_refs = state->ios_left;
2013 state->used_refs = 1;
2014 state->ios_left--;
2015 return state->file;
2016}
2017
Jens Axboe2b188cc2019-01-07 10:46:33 -07002018/*
2019 * If we tracked the file through the SCM inflight mechanism, we could support
2020 * any file. For now, just ensure that anything potentially problematic is done
2021 * inline.
2022 */
2023static bool io_file_supports_async(struct file *file)
2024{
2025 umode_t mode = file_inode(file)->i_mode;
2026
Jens Axboe10d59342019-12-09 20:16:22 -07002027 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002028 return true;
2029 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
2030 return true;
2031
2032 return false;
2033}
2034
Jens Axboe3529d8c2019-12-19 18:24:38 -07002035static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2036 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002037{
Jens Axboedef596e2019-01-09 08:59:42 -07002038 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002039 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002040 unsigned ioprio;
2041 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002042
Jens Axboe491381ce2019-10-17 09:20:46 -06002043 if (S_ISREG(file_inode(req->file)->i_mode))
2044 req->flags |= REQ_F_ISREG;
2045
Jens Axboe2b188cc2019-01-07 10:46:33 -07002046 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002047 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2048 req->flags |= REQ_F_CUR_POS;
2049 kiocb->ki_pos = req->file->f_pos;
2050 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002051 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002052 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2053 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2054 if (unlikely(ret))
2055 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002056
2057 ioprio = READ_ONCE(sqe->ioprio);
2058 if (ioprio) {
2059 ret = ioprio_check_cap(ioprio);
2060 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002061 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002062
2063 kiocb->ki_ioprio = ioprio;
2064 } else
2065 kiocb->ki_ioprio = get_current_ioprio();
2066
Stefan Bühler8449eed2019-04-27 20:34:19 +02002067 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06002068 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
2069 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02002070 req->flags |= REQ_F_NOWAIT;
2071
2072 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002073 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02002074
Jens Axboedef596e2019-01-09 08:59:42 -07002075 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002076 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2077 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002078 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002079
Jens Axboedef596e2019-01-09 08:59:42 -07002080 kiocb->ki_flags |= IOCB_HIPRI;
2081 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06002082 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002083 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002084 if (kiocb->ki_flags & IOCB_HIPRI)
2085 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002086 kiocb->ki_complete = io_complete_rw;
2087 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002088
Jens Axboe3529d8c2019-12-19 18:24:38 -07002089 req->rw.addr = READ_ONCE(sqe->addr);
2090 req->rw.len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002091 /* we own ->private, reuse it for the buffer index / buffer ID */
Jens Axboe9adbd452019-12-20 08:45:55 -07002092 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002093 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002094 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002095}
2096
2097static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2098{
2099 switch (ret) {
2100 case -EIOCBQUEUED:
2101 break;
2102 case -ERESTARTSYS:
2103 case -ERESTARTNOINTR:
2104 case -ERESTARTNOHAND:
2105 case -ERESTART_RESTARTBLOCK:
2106 /*
2107 * We can't just restart the syscall, since previously
2108 * submitted sqes may already be in progress. Just fail this
2109 * IO with EINTR.
2110 */
2111 ret = -EINTR;
2112 /* fall through */
2113 default:
2114 kiocb->ki_complete(kiocb, ret, 0);
2115 }
2116}
2117
Pavel Begunkov014db002020-03-03 21:33:12 +03002118static void kiocb_done(struct kiocb *kiocb, ssize_t ret)
Jens Axboeba816ad2019-09-28 11:36:45 -06002119{
Jens Axboeba042912019-12-25 16:33:42 -07002120 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2121
2122 if (req->flags & REQ_F_CUR_POS)
2123 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002124 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Pavel Begunkov014db002020-03-03 21:33:12 +03002125 io_complete_rw(kiocb, ret, 0);
Jens Axboeba816ad2019-09-28 11:36:45 -06002126 else
2127 io_rw_done(kiocb, ret);
2128}
2129
Jens Axboe9adbd452019-12-20 08:45:55 -07002130static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002131 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002132{
Jens Axboe9adbd452019-12-20 08:45:55 -07002133 struct io_ring_ctx *ctx = req->ctx;
2134 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002135 struct io_mapped_ubuf *imu;
2136 unsigned index, buf_index;
2137 size_t offset;
2138 u64 buf_addr;
2139
2140 /* attempt to use fixed buffers without having provided iovecs */
2141 if (unlikely(!ctx->user_bufs))
2142 return -EFAULT;
2143
Jens Axboe9adbd452019-12-20 08:45:55 -07002144 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07002145 if (unlikely(buf_index >= ctx->nr_user_bufs))
2146 return -EFAULT;
2147
2148 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2149 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002150 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002151
2152 /* overflow */
2153 if (buf_addr + len < buf_addr)
2154 return -EFAULT;
2155 /* not inside the mapped region */
2156 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2157 return -EFAULT;
2158
2159 /*
2160 * May not be a start of buffer, set size appropriately
2161 * and advance us to the beginning.
2162 */
2163 offset = buf_addr - imu->ubuf;
2164 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002165
2166 if (offset) {
2167 /*
2168 * Don't use iov_iter_advance() here, as it's really slow for
2169 * using the latter parts of a big fixed buffer - it iterates
2170 * over each segment manually. We can cheat a bit here, because
2171 * we know that:
2172 *
2173 * 1) it's a BVEC iter, we set it up
2174 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2175 * first and last bvec
2176 *
2177 * So just find our index, and adjust the iterator afterwards.
2178 * If the offset is within the first bvec (or the whole first
2179 * bvec, just use iov_iter_advance(). This makes it easier
2180 * since we can just skip the first segment, which may not
2181 * be PAGE_SIZE aligned.
2182 */
2183 const struct bio_vec *bvec = imu->bvec;
2184
2185 if (offset <= bvec->bv_len) {
2186 iov_iter_advance(iter, offset);
2187 } else {
2188 unsigned long seg_skip;
2189
2190 /* skip first vec */
2191 offset -= bvec->bv_len;
2192 seg_skip = 1 + (offset >> PAGE_SHIFT);
2193
2194 iter->bvec = bvec + seg_skip;
2195 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002196 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002197 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002198 }
2199 }
2200
Jens Axboe5e559562019-11-13 16:12:46 -07002201 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002202}
2203
Jens Axboebcda7ba2020-02-23 16:42:51 -07002204static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2205{
2206 if (needs_lock)
2207 mutex_unlock(&ctx->uring_lock);
2208}
2209
2210static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2211{
2212 /*
2213 * "Normal" inline submissions always hold the uring_lock, since we
2214 * grab it from the system call. Same is true for the SQPOLL offload.
2215 * The only exception is when we've detached the request and issue it
2216 * from an async worker thread, grab the lock for that case.
2217 */
2218 if (needs_lock)
2219 mutex_lock(&ctx->uring_lock);
2220}
2221
2222static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2223 int bgid, struct io_buffer *kbuf,
2224 bool needs_lock)
2225{
2226 struct io_buffer *head;
2227
2228 if (req->flags & REQ_F_BUFFER_SELECTED)
2229 return kbuf;
2230
2231 io_ring_submit_lock(req->ctx, needs_lock);
2232
2233 lockdep_assert_held(&req->ctx->uring_lock);
2234
2235 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2236 if (head) {
2237 if (!list_empty(&head->list)) {
2238 kbuf = list_last_entry(&head->list, struct io_buffer,
2239 list);
2240 list_del(&kbuf->list);
2241 } else {
2242 kbuf = head;
2243 idr_remove(&req->ctx->io_buffer_idr, bgid);
2244 }
2245 if (*len > kbuf->len)
2246 *len = kbuf->len;
2247 } else {
2248 kbuf = ERR_PTR(-ENOBUFS);
2249 }
2250
2251 io_ring_submit_unlock(req->ctx, needs_lock);
2252
2253 return kbuf;
2254}
2255
Jens Axboe4d954c22020-02-27 07:31:19 -07002256static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2257 bool needs_lock)
2258{
2259 struct io_buffer *kbuf;
2260 int bgid;
2261
2262 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2263 bgid = (int) (unsigned long) req->rw.kiocb.private;
2264 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2265 if (IS_ERR(kbuf))
2266 return kbuf;
2267 req->rw.addr = (u64) (unsigned long) kbuf;
2268 req->flags |= REQ_F_BUFFER_SELECTED;
2269 return u64_to_user_ptr(kbuf->addr);
2270}
2271
2272#ifdef CONFIG_COMPAT
2273static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2274 bool needs_lock)
2275{
2276 struct compat_iovec __user *uiov;
2277 compat_ssize_t clen;
2278 void __user *buf;
2279 ssize_t len;
2280
2281 uiov = u64_to_user_ptr(req->rw.addr);
2282 if (!access_ok(uiov, sizeof(*uiov)))
2283 return -EFAULT;
2284 if (__get_user(clen, &uiov->iov_len))
2285 return -EFAULT;
2286 if (clen < 0)
2287 return -EINVAL;
2288
2289 len = clen;
2290 buf = io_rw_buffer_select(req, &len, needs_lock);
2291 if (IS_ERR(buf))
2292 return PTR_ERR(buf);
2293 iov[0].iov_base = buf;
2294 iov[0].iov_len = (compat_size_t) len;
2295 return 0;
2296}
2297#endif
2298
2299static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2300 bool needs_lock)
2301{
2302 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2303 void __user *buf;
2304 ssize_t len;
2305
2306 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2307 return -EFAULT;
2308
2309 len = iov[0].iov_len;
2310 if (len < 0)
2311 return -EINVAL;
2312 buf = io_rw_buffer_select(req, &len, needs_lock);
2313 if (IS_ERR(buf))
2314 return PTR_ERR(buf);
2315 iov[0].iov_base = buf;
2316 iov[0].iov_len = len;
2317 return 0;
2318}
2319
2320static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2321 bool needs_lock)
2322{
2323 if (req->flags & REQ_F_BUFFER_SELECTED)
2324 return 0;
2325 if (!req->rw.len)
2326 return 0;
2327 else if (req->rw.len > 1)
2328 return -EINVAL;
2329
2330#ifdef CONFIG_COMPAT
2331 if (req->ctx->compat)
2332 return io_compat_import(req, iov, needs_lock);
2333#endif
2334
2335 return __io_iov_buffer_select(req, iov, needs_lock);
2336}
2337
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002338static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
Jens Axboebcda7ba2020-02-23 16:42:51 -07002339 struct iovec **iovec, struct iov_iter *iter,
2340 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002341{
Jens Axboe9adbd452019-12-20 08:45:55 -07002342 void __user *buf = u64_to_user_ptr(req->rw.addr);
2343 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002344 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002345 u8 opcode;
2346
Jens Axboed625c6e2019-12-17 19:53:05 -07002347 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002348 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002349 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002350 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002351 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002352
Jens Axboebcda7ba2020-02-23 16:42:51 -07002353 /* buffer index only valid with fixed read/write, or buffer select */
2354 if (req->rw.kiocb.private && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002355 return -EINVAL;
2356
Jens Axboe3a6820f2019-12-22 15:19:35 -07002357 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002358 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002359 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
2360 if (IS_ERR(buf)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002361 *iovec = NULL;
Jens Axboe4d954c22020-02-27 07:31:19 -07002362 return PTR_ERR(buf);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002363 }
Jens Axboe3f9d6442020-03-11 12:27:04 -06002364 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002365 }
2366
Jens Axboe3a6820f2019-12-22 15:19:35 -07002367 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2368 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002369 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002370 }
2371
Jens Axboef67676d2019-12-02 11:03:47 -07002372 if (req->io) {
2373 struct io_async_rw *iorw = &req->io->rw;
2374
2375 *iovec = iorw->iov;
2376 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2377 if (iorw->iov == iorw->fast_iov)
2378 *iovec = NULL;
2379 return iorw->size;
2380 }
2381
Jens Axboe4d954c22020-02-27 07:31:19 -07002382 if (req->flags & REQ_F_BUFFER_SELECT) {
2383 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002384 if (!ret) {
2385 ret = (*iovec)->iov_len;
2386 iov_iter_init(iter, rw, *iovec, 1, ret);
2387 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002388 *iovec = NULL;
2389 return ret;
2390 }
2391
Jens Axboe2b188cc2019-01-07 10:46:33 -07002392#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002393 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002394 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2395 iovec, iter);
2396#endif
2397
2398 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2399}
2400
Jens Axboe32960612019-09-23 11:05:34 -06002401/*
2402 * For files that don't have ->read_iter() and ->write_iter(), handle them
2403 * by looping over ->read() or ->write() manually.
2404 */
2405static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2406 struct iov_iter *iter)
2407{
2408 ssize_t ret = 0;
2409
2410 /*
2411 * Don't support polled IO through this interface, and we can't
2412 * support non-blocking either. For the latter, this just causes
2413 * the kiocb to be handled from an async context.
2414 */
2415 if (kiocb->ki_flags & IOCB_HIPRI)
2416 return -EOPNOTSUPP;
2417 if (kiocb->ki_flags & IOCB_NOWAIT)
2418 return -EAGAIN;
2419
2420 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002421 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002422 ssize_t nr;
2423
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002424 if (!iov_iter_is_bvec(iter)) {
2425 iovec = iov_iter_iovec(iter);
2426 } else {
2427 /* fixed buffers import bvec */
2428 iovec.iov_base = kmap(iter->bvec->bv_page)
2429 + iter->iov_offset;
2430 iovec.iov_len = min(iter->count,
2431 iter->bvec->bv_len - iter->iov_offset);
2432 }
2433
Jens Axboe32960612019-09-23 11:05:34 -06002434 if (rw == READ) {
2435 nr = file->f_op->read(file, iovec.iov_base,
2436 iovec.iov_len, &kiocb->ki_pos);
2437 } else {
2438 nr = file->f_op->write(file, iovec.iov_base,
2439 iovec.iov_len, &kiocb->ki_pos);
2440 }
2441
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002442 if (iov_iter_is_bvec(iter))
2443 kunmap(iter->bvec->bv_page);
2444
Jens Axboe32960612019-09-23 11:05:34 -06002445 if (nr < 0) {
2446 if (!ret)
2447 ret = nr;
2448 break;
2449 }
2450 ret += nr;
2451 if (nr != iovec.iov_len)
2452 break;
2453 iov_iter_advance(iter, nr);
2454 }
2455
2456 return ret;
2457}
2458
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002459static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002460 struct iovec *iovec, struct iovec *fast_iov,
2461 struct iov_iter *iter)
2462{
2463 req->io->rw.nr_segs = iter->nr_segs;
2464 req->io->rw.size = io_size;
2465 req->io->rw.iov = iovec;
2466 if (!req->io->rw.iov) {
2467 req->io->rw.iov = req->io->rw.fast_iov;
2468 memcpy(req->io->rw.iov, fast_iov,
2469 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002470 } else {
2471 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002472 }
2473}
2474
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002475static inline int __io_alloc_async_ctx(struct io_kiocb *req)
2476{
2477 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
2478 return req->io == NULL;
2479}
2480
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002481static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002482{
Jens Axboed3656342019-12-18 09:50:26 -07002483 if (!io_op_defs[req->opcode].async_ctx)
2484 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002485
2486 return __io_alloc_async_ctx(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002487}
2488
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002489static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2490 struct iovec *iovec, struct iovec *fast_iov,
2491 struct iov_iter *iter)
2492{
Jens Axboe980ad262020-01-24 23:08:54 -07002493 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002494 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002495 if (!req->io) {
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002496 if (__io_alloc_async_ctx(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07002497 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002498
Jens Axboe5d204bc2020-01-31 12:06:52 -07002499 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2500 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002501 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002502}
2503
Jens Axboe3529d8c2019-12-19 18:24:38 -07002504static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2505 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002506{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002507 struct io_async_ctx *io;
2508 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002509 ssize_t ret;
2510
Jens Axboe3529d8c2019-12-19 18:24:38 -07002511 ret = io_prep_rw(req, sqe, force_nonblock);
2512 if (ret)
2513 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002514
Jens Axboe3529d8c2019-12-19 18:24:38 -07002515 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2516 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002517
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002518 /* either don't need iovec imported or already have it */
2519 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002520 return 0;
2521
2522 io = req->io;
2523 io->rw.iov = io->rw.fast_iov;
2524 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002525 ret = io_import_iovec(READ, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002526 req->io = io;
2527 if (ret < 0)
2528 return ret;
2529
2530 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2531 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002532}
2533
Pavel Begunkov014db002020-03-03 21:33:12 +03002534static int io_read(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002535{
2536 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002537 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002538 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002539 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002540 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002541
Jens Axboebcda7ba2020-02-23 16:42:51 -07002542 ret = io_import_iovec(READ, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07002543 if (ret < 0)
2544 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002545
Jens Axboefd6c2e42019-12-18 12:19:41 -07002546 /* Ensure we clear previously set non-block flag */
2547 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07002548 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002549
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002550 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002551 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002552 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002553 req->result = io_size;
2554
2555 /*
2556 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2557 * we know to async punt it even if it was opened O_NONBLOCK
2558 */
Jens Axboe29de5f62020-02-20 09:56:08 -07002559 if (force_nonblock && !io_file_supports_async(req->file))
Jens Axboef67676d2019-12-02 11:03:47 -07002560 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002561
Jens Axboe31b51512019-01-18 22:56:34 -07002562 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002563 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002564 if (!ret) {
2565 ssize_t ret2;
2566
Jens Axboe9adbd452019-12-20 08:45:55 -07002567 if (req->file->f_op->read_iter)
2568 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002569 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002570 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002571
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002572 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002573 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkov014db002020-03-03 21:33:12 +03002574 kiocb_done(kiocb, ret2);
Jens Axboef67676d2019-12-02 11:03:47 -07002575 } else {
2576copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002577 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002578 inline_vecs, &iter);
2579 if (ret)
2580 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002581 /* any defer here is final, must blocking retry */
2582 if (!(req->flags & REQ_F_NOWAIT))
2583 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002584 return -EAGAIN;
2585 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002586 }
Jens Axboef67676d2019-12-02 11:03:47 -07002587out_free:
Pavel Begunkov1e950812020-02-06 19:51:16 +03002588 kfree(iovec);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002589 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002590 return ret;
2591}
2592
Jens Axboe3529d8c2019-12-19 18:24:38 -07002593static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2594 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002595{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002596 struct io_async_ctx *io;
2597 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002598 ssize_t ret;
2599
Jens Axboe3529d8c2019-12-19 18:24:38 -07002600 ret = io_prep_rw(req, sqe, force_nonblock);
2601 if (ret)
2602 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002603
Jens Axboe3529d8c2019-12-19 18:24:38 -07002604 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2605 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002606
Jens Axboe4ed734b2020-03-20 11:23:41 -06002607 req->fsize = rlimit(RLIMIT_FSIZE);
2608
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002609 /* either don't need iovec imported or already have it */
2610 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002611 return 0;
2612
2613 io = req->io;
2614 io->rw.iov = io->rw.fast_iov;
2615 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002616 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002617 req->io = io;
2618 if (ret < 0)
2619 return ret;
2620
2621 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2622 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002623}
2624
Pavel Begunkov014db002020-03-03 21:33:12 +03002625static int io_write(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002626{
2627 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002628 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002629 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002630 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002631 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002632
Jens Axboebcda7ba2020-02-23 16:42:51 -07002633 ret = io_import_iovec(WRITE, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07002634 if (ret < 0)
2635 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002636
Jens Axboefd6c2e42019-12-18 12:19:41 -07002637 /* Ensure we clear previously set non-block flag */
2638 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002639 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002640
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002641 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002642 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002643 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002644 req->result = io_size;
2645
2646 /*
2647 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2648 * we know to async punt it even if it was opened O_NONBLOCK
2649 */
Jens Axboe29de5f62020-02-20 09:56:08 -07002650 if (force_nonblock && !io_file_supports_async(req->file))
Jens Axboef67676d2019-12-02 11:03:47 -07002651 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07002652
Jens Axboe10d59342019-12-09 20:16:22 -07002653 /* file path doesn't support NOWAIT for non-direct_IO */
2654 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2655 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002656 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002657
Jens Axboe31b51512019-01-18 22:56:34 -07002658 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002659 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002660 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002661 ssize_t ret2;
2662
Jens Axboe2b188cc2019-01-07 10:46:33 -07002663 /*
2664 * Open-code file_start_write here to grab freeze protection,
2665 * which will be released by another thread in
2666 * io_complete_rw(). Fool lockdep by telling it the lock got
2667 * released so that it doesn't complain about the held lock when
2668 * we return to userspace.
2669 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002670 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002671 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002672 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002673 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002674 SB_FREEZE_WRITE);
2675 }
2676 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002677
Jens Axboe4ed734b2020-03-20 11:23:41 -06002678 if (!force_nonblock)
2679 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
2680
Jens Axboe9adbd452019-12-20 08:45:55 -07002681 if (req->file->f_op->write_iter)
2682 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002683 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002684 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboe4ed734b2020-03-20 11:23:41 -06002685
2686 if (!force_nonblock)
2687 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
2688
Jens Axboefaac9962020-02-07 15:45:22 -07002689 /*
Chucheng Luobff60352020-03-25 11:31:38 +08002690 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
Jens Axboefaac9962020-02-07 15:45:22 -07002691 * retry them without IOCB_NOWAIT.
2692 */
2693 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
2694 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07002695 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkov014db002020-03-03 21:33:12 +03002696 kiocb_done(kiocb, ret2);
Jens Axboef67676d2019-12-02 11:03:47 -07002697 } else {
2698copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002699 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002700 inline_vecs, &iter);
2701 if (ret)
2702 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002703 /* any defer here is final, must blocking retry */
2704 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002705 return -EAGAIN;
2706 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002707 }
Jens Axboe31b51512019-01-18 22:56:34 -07002708out_free:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002709 req->flags &= ~REQ_F_NEED_CLEANUP;
Pavel Begunkov1e950812020-02-06 19:51:16 +03002710 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002711 return ret;
2712}
2713
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002714static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2715{
2716 struct io_splice* sp = &req->splice;
2717 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
2718 int ret;
2719
2720 if (req->flags & REQ_F_NEED_CLEANUP)
2721 return 0;
2722
2723 sp->file_in = NULL;
2724 sp->off_in = READ_ONCE(sqe->splice_off_in);
2725 sp->off_out = READ_ONCE(sqe->off);
2726 sp->len = READ_ONCE(sqe->len);
2727 sp->flags = READ_ONCE(sqe->splice_flags);
2728
2729 if (unlikely(sp->flags & ~valid_flags))
2730 return -EINVAL;
2731
2732 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
2733 (sp->flags & SPLICE_F_FD_IN_FIXED));
2734 if (ret)
2735 return ret;
2736 req->flags |= REQ_F_NEED_CLEANUP;
2737
2738 if (!S_ISREG(file_inode(sp->file_in)->i_mode))
2739 req->work.flags |= IO_WQ_WORK_UNBOUND;
2740
2741 return 0;
2742}
2743
2744static bool io_splice_punt(struct file *file)
2745{
2746 if (get_pipe_info(file))
2747 return false;
2748 if (!io_file_supports_async(file))
2749 return true;
2750 return !(file->f_mode & O_NONBLOCK);
2751}
2752
Pavel Begunkov014db002020-03-03 21:33:12 +03002753static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002754{
2755 struct io_splice *sp = &req->splice;
2756 struct file *in = sp->file_in;
2757 struct file *out = sp->file_out;
2758 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
2759 loff_t *poff_in, *poff_out;
2760 long ret;
2761
2762 if (force_nonblock) {
2763 if (io_splice_punt(in) || io_splice_punt(out))
2764 return -EAGAIN;
2765 flags |= SPLICE_F_NONBLOCK;
2766 }
2767
2768 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
2769 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
2770 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
2771 if (force_nonblock && ret == -EAGAIN)
2772 return -EAGAIN;
2773
2774 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
2775 req->flags &= ~REQ_F_NEED_CLEANUP;
2776
2777 io_cqring_add_event(req, ret);
2778 if (ret != sp->len)
2779 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03002780 io_put_req(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002781 return 0;
2782}
2783
Jens Axboe2b188cc2019-01-07 10:46:33 -07002784/*
2785 * IORING_OP_NOP just posts a completion event, nothing else.
2786 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002787static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002788{
2789 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002790
Jens Axboedef596e2019-01-09 08:59:42 -07002791 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2792 return -EINVAL;
2793
Jens Axboe78e19bb2019-11-06 15:21:34 -07002794 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002795 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002796 return 0;
2797}
2798
Jens Axboe3529d8c2019-12-19 18:24:38 -07002799static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002800{
Jens Axboe6b063142019-01-10 22:13:58 -07002801 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002802
Jens Axboe09bb8392019-03-13 12:39:28 -06002803 if (!req->file)
2804 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002805
Jens Axboe6b063142019-01-10 22:13:58 -07002806 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002807 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002808 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002809 return -EINVAL;
2810
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002811 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2812 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2813 return -EINVAL;
2814
2815 req->sync.off = READ_ONCE(sqe->off);
2816 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002817 return 0;
2818}
2819
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002820static bool io_req_cancelled(struct io_kiocb *req)
2821{
2822 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2823 req_set_fail_links(req);
2824 io_cqring_add_event(req, -ECANCELED);
2825 io_put_req(req);
2826 return true;
2827 }
2828
2829 return false;
2830}
2831
Pavel Begunkov014db002020-03-03 21:33:12 +03002832static void __io_fsync(struct io_kiocb *req)
Jens Axboe78912932020-01-14 22:09:06 -07002833{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002834 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002835 int ret;
2836
Jens Axboe9adbd452019-12-20 08:45:55 -07002837 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002838 end > 0 ? end : LLONG_MAX,
2839 req->sync.flags & IORING_FSYNC_DATASYNC);
2840 if (ret < 0)
2841 req_set_fail_links(req);
2842 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002843 io_put_req(req);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002844}
2845
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002846static void io_fsync_finish(struct io_wq_work **workptr)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002847{
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002848 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002849
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002850 if (io_req_cancelled(req))
2851 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03002852 __io_fsync(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03002853 io_steal_work(req, workptr);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002854}
2855
Pavel Begunkov014db002020-03-03 21:33:12 +03002856static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002857{
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002858 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002859 if (force_nonblock) {
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002860 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002861 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002862 }
Pavel Begunkov014db002020-03-03 21:33:12 +03002863 __io_fsync(req);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002864 return 0;
2865}
2866
Pavel Begunkov014db002020-03-03 21:33:12 +03002867static void __io_fallocate(struct io_kiocb *req)
Jens Axboed63d1b52019-12-10 10:38:56 -07002868{
Jens Axboed63d1b52019-12-10 10:38:56 -07002869 int ret;
2870
Jens Axboe4ed734b2020-03-20 11:23:41 -06002871 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
Jens Axboed63d1b52019-12-10 10:38:56 -07002872 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2873 req->sync.len);
Jens Axboe4ed734b2020-03-20 11:23:41 -06002874 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
Jens Axboed63d1b52019-12-10 10:38:56 -07002875 if (ret < 0)
2876 req_set_fail_links(req);
2877 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002878 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002879}
2880
Jens Axboe2b188cc2019-01-07 10:46:33 -07002881static void io_fallocate_finish(struct io_wq_work **workptr)
2882{
2883 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Jens Axboed63d1b52019-12-10 10:38:56 -07002884
2885 if (io_req_cancelled(req))
2886 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03002887 __io_fallocate(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03002888 io_steal_work(req, workptr);
Jens Axboed63d1b52019-12-10 10:38:56 -07002889}
2890
2891static int io_fallocate_prep(struct io_kiocb *req,
2892 const struct io_uring_sqe *sqe)
2893{
2894 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2895 return -EINVAL;
2896
2897 req->sync.off = READ_ONCE(sqe->off);
2898 req->sync.len = READ_ONCE(sqe->addr);
2899 req->sync.mode = READ_ONCE(sqe->len);
Jens Axboe4ed734b2020-03-20 11:23:41 -06002900 req->fsize = rlimit(RLIMIT_FSIZE);
Jens Axboed63d1b52019-12-10 10:38:56 -07002901 return 0;
2902}
2903
Pavel Begunkov014db002020-03-03 21:33:12 +03002904static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07002905{
Jens Axboed63d1b52019-12-10 10:38:56 -07002906 /* fallocate always requiring blocking context */
2907 if (force_nonblock) {
Jens Axboed63d1b52019-12-10 10:38:56 -07002908 req->work.func = io_fallocate_finish;
2909 return -EAGAIN;
2910 }
2911
Pavel Begunkov014db002020-03-03 21:33:12 +03002912 __io_fallocate(req);
Jens Axboed63d1b52019-12-10 10:38:56 -07002913 return 0;
2914}
2915
Jens Axboe15b71ab2019-12-11 11:20:36 -07002916static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2917{
Jens Axboef8748882020-01-08 17:47:02 -07002918 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002919 int ret;
2920
2921 if (sqe->ioprio || sqe->buf_index)
2922 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002923 if (sqe->flags & IOSQE_FIXED_FILE)
2924 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002925 if (req->flags & REQ_F_NEED_CLEANUP)
2926 return 0;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002927
2928 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002929 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002930 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002931 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002932
Jens Axboef8748882020-01-08 17:47:02 -07002933 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002934 if (IS_ERR(req->open.filename)) {
2935 ret = PTR_ERR(req->open.filename);
2936 req->open.filename = NULL;
2937 return ret;
2938 }
2939
Jens Axboe4022e7a2020-03-19 19:23:18 -06002940 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002941 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002942 return 0;
2943}
2944
Jens Axboecebdb982020-01-08 17:59:24 -07002945static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2946{
2947 struct open_how __user *how;
2948 const char __user *fname;
2949 size_t len;
2950 int ret;
2951
2952 if (sqe->ioprio || sqe->buf_index)
2953 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002954 if (sqe->flags & IOSQE_FIXED_FILE)
2955 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002956 if (req->flags & REQ_F_NEED_CLEANUP)
2957 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07002958
2959 req->open.dfd = READ_ONCE(sqe->fd);
2960 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2961 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2962 len = READ_ONCE(sqe->len);
2963
2964 if (len < OPEN_HOW_SIZE_VER0)
2965 return -EINVAL;
2966
2967 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2968 len);
2969 if (ret)
2970 return ret;
2971
2972 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2973 req->open.how.flags |= O_LARGEFILE;
2974
2975 req->open.filename = getname(fname);
2976 if (IS_ERR(req->open.filename)) {
2977 ret = PTR_ERR(req->open.filename);
2978 req->open.filename = NULL;
2979 return ret;
2980 }
2981
Jens Axboe4022e7a2020-03-19 19:23:18 -06002982 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002983 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboecebdb982020-01-08 17:59:24 -07002984 return 0;
2985}
2986
Pavel Begunkov014db002020-03-03 21:33:12 +03002987static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002988{
2989 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002990 struct file *file;
2991 int ret;
2992
Jens Axboef86cd202020-01-29 13:46:44 -07002993 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002994 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002995
Jens Axboecebdb982020-01-08 17:59:24 -07002996 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002997 if (ret)
2998 goto err;
2999
Jens Axboe4022e7a2020-03-19 19:23:18 -06003000 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003001 if (ret < 0)
3002 goto err;
3003
3004 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3005 if (IS_ERR(file)) {
3006 put_unused_fd(ret);
3007 ret = PTR_ERR(file);
3008 } else {
3009 fsnotify_open(file);
3010 fd_install(ret, file);
3011 }
3012err:
3013 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003014 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003015 if (ret < 0)
3016 req_set_fail_links(req);
3017 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003018 io_put_req(req);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003019 return 0;
3020}
3021
Pavel Begunkov014db002020-03-03 21:33:12 +03003022static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003023{
3024 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
Pavel Begunkov014db002020-03-03 21:33:12 +03003025 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003026}
3027
Jens Axboe067524e2020-03-02 16:32:28 -07003028static int io_remove_buffers_prep(struct io_kiocb *req,
3029 const struct io_uring_sqe *sqe)
3030{
3031 struct io_provide_buf *p = &req->pbuf;
3032 u64 tmp;
3033
3034 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3035 return -EINVAL;
3036
3037 tmp = READ_ONCE(sqe->fd);
3038 if (!tmp || tmp > USHRT_MAX)
3039 return -EINVAL;
3040
3041 memset(p, 0, sizeof(*p));
3042 p->nbufs = tmp;
3043 p->bgid = READ_ONCE(sqe->buf_group);
3044 return 0;
3045}
3046
3047static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3048 int bgid, unsigned nbufs)
3049{
3050 unsigned i = 0;
3051
3052 /* shouldn't happen */
3053 if (!nbufs)
3054 return 0;
3055
3056 /* the head kbuf is the list itself */
3057 while (!list_empty(&buf->list)) {
3058 struct io_buffer *nxt;
3059
3060 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3061 list_del(&nxt->list);
3062 kfree(nxt);
3063 if (++i == nbufs)
3064 return i;
3065 }
3066 i++;
3067 kfree(buf);
3068 idr_remove(&ctx->io_buffer_idr, bgid);
3069
3070 return i;
3071}
3072
3073static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock)
3074{
3075 struct io_provide_buf *p = &req->pbuf;
3076 struct io_ring_ctx *ctx = req->ctx;
3077 struct io_buffer *head;
3078 int ret = 0;
3079
3080 io_ring_submit_lock(ctx, !force_nonblock);
3081
3082 lockdep_assert_held(&ctx->uring_lock);
3083
3084 ret = -ENOENT;
3085 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3086 if (head)
3087 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3088
3089 io_ring_submit_lock(ctx, !force_nonblock);
3090 if (ret < 0)
3091 req_set_fail_links(req);
3092 io_cqring_add_event(req, ret);
3093 io_put_req(req);
3094 return 0;
3095}
3096
Jens Axboeddf0322d2020-02-23 16:41:33 -07003097static int io_provide_buffers_prep(struct io_kiocb *req,
3098 const struct io_uring_sqe *sqe)
3099{
3100 struct io_provide_buf *p = &req->pbuf;
3101 u64 tmp;
3102
3103 if (sqe->ioprio || sqe->rw_flags)
3104 return -EINVAL;
3105
3106 tmp = READ_ONCE(sqe->fd);
3107 if (!tmp || tmp > USHRT_MAX)
3108 return -E2BIG;
3109 p->nbufs = tmp;
3110 p->addr = READ_ONCE(sqe->addr);
3111 p->len = READ_ONCE(sqe->len);
3112
3113 if (!access_ok(u64_to_user_ptr(p->addr), p->len))
3114 return -EFAULT;
3115
3116 p->bgid = READ_ONCE(sqe->buf_group);
3117 tmp = READ_ONCE(sqe->off);
3118 if (tmp > USHRT_MAX)
3119 return -E2BIG;
3120 p->bid = tmp;
3121 return 0;
3122}
3123
3124static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3125{
3126 struct io_buffer *buf;
3127 u64 addr = pbuf->addr;
3128 int i, bid = pbuf->bid;
3129
3130 for (i = 0; i < pbuf->nbufs; i++) {
3131 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3132 if (!buf)
3133 break;
3134
3135 buf->addr = addr;
3136 buf->len = pbuf->len;
3137 buf->bid = bid;
3138 addr += pbuf->len;
3139 bid++;
3140 if (!*head) {
3141 INIT_LIST_HEAD(&buf->list);
3142 *head = buf;
3143 } else {
3144 list_add_tail(&buf->list, &(*head)->list);
3145 }
3146 }
3147
3148 return i ? i : -ENOMEM;
3149}
3150
Jens Axboeddf0322d2020-02-23 16:41:33 -07003151static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock)
3152{
3153 struct io_provide_buf *p = &req->pbuf;
3154 struct io_ring_ctx *ctx = req->ctx;
3155 struct io_buffer *head, *list;
3156 int ret = 0;
3157
3158 io_ring_submit_lock(ctx, !force_nonblock);
3159
3160 lockdep_assert_held(&ctx->uring_lock);
3161
3162 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3163
3164 ret = io_add_buffers(p, &head);
3165 if (ret < 0)
3166 goto out;
3167
3168 if (!list) {
3169 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3170 GFP_KERNEL);
3171 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003172 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003173 goto out;
3174 }
3175 }
3176out:
3177 io_ring_submit_unlock(ctx, !force_nonblock);
3178 if (ret < 0)
3179 req_set_fail_links(req);
3180 io_cqring_add_event(req, ret);
3181 io_put_req(req);
3182 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003183}
3184
Jens Axboe3e4827b2020-01-08 15:18:09 -07003185static int io_epoll_ctl_prep(struct io_kiocb *req,
3186 const struct io_uring_sqe *sqe)
3187{
3188#if defined(CONFIG_EPOLL)
3189 if (sqe->ioprio || sqe->buf_index)
3190 return -EINVAL;
3191
3192 req->epoll.epfd = READ_ONCE(sqe->fd);
3193 req->epoll.op = READ_ONCE(sqe->len);
3194 req->epoll.fd = READ_ONCE(sqe->off);
3195
3196 if (ep_op_has_event(req->epoll.op)) {
3197 struct epoll_event __user *ev;
3198
3199 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3200 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3201 return -EFAULT;
3202 }
3203
3204 return 0;
3205#else
3206 return -EOPNOTSUPP;
3207#endif
3208}
3209
Pavel Begunkov014db002020-03-03 21:33:12 +03003210static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003211{
3212#if defined(CONFIG_EPOLL)
3213 struct io_epoll *ie = &req->epoll;
3214 int ret;
3215
3216 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3217 if (force_nonblock && ret == -EAGAIN)
3218 return -EAGAIN;
3219
3220 if (ret < 0)
3221 req_set_fail_links(req);
3222 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003223 io_put_req(req);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003224 return 0;
3225#else
3226 return -EOPNOTSUPP;
3227#endif
3228}
3229
Jens Axboec1ca7572019-12-25 22:18:28 -07003230static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3231{
3232#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3233 if (sqe->ioprio || sqe->buf_index || sqe->off)
3234 return -EINVAL;
3235
3236 req->madvise.addr = READ_ONCE(sqe->addr);
3237 req->madvise.len = READ_ONCE(sqe->len);
3238 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3239 return 0;
3240#else
3241 return -EOPNOTSUPP;
3242#endif
3243}
3244
Pavel Begunkov014db002020-03-03 21:33:12 +03003245static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003246{
3247#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3248 struct io_madvise *ma = &req->madvise;
3249 int ret;
3250
3251 if (force_nonblock)
3252 return -EAGAIN;
3253
3254 ret = do_madvise(ma->addr, ma->len, ma->advice);
3255 if (ret < 0)
3256 req_set_fail_links(req);
3257 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003258 io_put_req(req);
Jens Axboec1ca7572019-12-25 22:18:28 -07003259 return 0;
3260#else
3261 return -EOPNOTSUPP;
3262#endif
3263}
3264
Jens Axboe4840e412019-12-25 22:03:45 -07003265static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3266{
3267 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3268 return -EINVAL;
3269
3270 req->fadvise.offset = READ_ONCE(sqe->off);
3271 req->fadvise.len = READ_ONCE(sqe->len);
3272 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3273 return 0;
3274}
3275
Pavel Begunkov014db002020-03-03 21:33:12 +03003276static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07003277{
3278 struct io_fadvise *fa = &req->fadvise;
3279 int ret;
3280
Jens Axboe3e694262020-02-01 09:22:49 -07003281 if (force_nonblock) {
3282 switch (fa->advice) {
3283 case POSIX_FADV_NORMAL:
3284 case POSIX_FADV_RANDOM:
3285 case POSIX_FADV_SEQUENTIAL:
3286 break;
3287 default:
3288 return -EAGAIN;
3289 }
3290 }
Jens Axboe4840e412019-12-25 22:03:45 -07003291
3292 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3293 if (ret < 0)
3294 req_set_fail_links(req);
3295 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003296 io_put_req(req);
Jens Axboe4840e412019-12-25 22:03:45 -07003297 return 0;
3298}
3299
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003300static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3301{
Jens Axboef8748882020-01-08 17:47:02 -07003302 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003303 unsigned lookup_flags;
3304 int ret;
3305
3306 if (sqe->ioprio || sqe->buf_index)
3307 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07003308 if (sqe->flags & IOSQE_FIXED_FILE)
3309 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003310 if (req->flags & REQ_F_NEED_CLEANUP)
3311 return 0;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003312
3313 req->open.dfd = READ_ONCE(sqe->fd);
3314 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07003315 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003316 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07003317 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003318
Jens Axboec12cedf2020-01-08 17:41:21 -07003319 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003320 return -EINVAL;
3321
Jens Axboef8748882020-01-08 17:47:02 -07003322 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003323 if (IS_ERR(req->open.filename)) {
3324 ret = PTR_ERR(req->open.filename);
3325 req->open.filename = NULL;
3326 return ret;
3327 }
3328
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003329 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003330 return 0;
3331}
3332
Pavel Begunkov014db002020-03-03 21:33:12 +03003333static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003334{
3335 struct io_open *ctx = &req->open;
3336 unsigned lookup_flags;
3337 struct path path;
3338 struct kstat stat;
3339 int ret;
3340
3341 if (force_nonblock)
3342 return -EAGAIN;
3343
Jens Axboec12cedf2020-01-08 17:41:21 -07003344 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003345 return -EINVAL;
3346
3347retry:
3348 /* filename_lookup() drops it, keep a reference */
3349 ctx->filename->refcnt++;
3350
3351 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
3352 NULL);
3353 if (ret)
3354 goto err;
3355
Jens Axboec12cedf2020-01-08 17:41:21 -07003356 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003357 path_put(&path);
3358 if (retry_estale(ret, lookup_flags)) {
3359 lookup_flags |= LOOKUP_REVAL;
3360 goto retry;
3361 }
3362 if (!ret)
3363 ret = cp_statx(&stat, ctx->buffer);
3364err:
3365 putname(ctx->filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003366 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003367 if (ret < 0)
3368 req_set_fail_links(req);
3369 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003370 io_put_req(req);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003371 return 0;
3372}
3373
Jens Axboeb5dba592019-12-11 14:02:38 -07003374static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3375{
3376 /*
3377 * If we queue this for async, it must not be cancellable. That would
3378 * leave the 'file' in an undeterminate state.
3379 */
3380 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
3381
3382 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
3383 sqe->rw_flags || sqe->buf_index)
3384 return -EINVAL;
3385 if (sqe->flags & IOSQE_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003386 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07003387
3388 req->close.fd = READ_ONCE(sqe->fd);
3389 if (req->file->f_op == &io_uring_fops ||
Pavel Begunkovb14cca02020-01-17 04:45:59 +03003390 req->close.fd == req->ctx->ring_fd)
Jens Axboeb5dba592019-12-11 14:02:38 -07003391 return -EBADF;
3392
3393 return 0;
3394}
3395
Pavel Begunkova93b3332020-02-08 14:04:34 +03003396/* only called when __close_fd_get_file() is done */
Pavel Begunkov014db002020-03-03 21:33:12 +03003397static void __io_close_finish(struct io_kiocb *req)
Pavel Begunkova93b3332020-02-08 14:04:34 +03003398{
3399 int ret;
3400
3401 ret = filp_close(req->close.put_file, req->work.files);
3402 if (ret < 0)
3403 req_set_fail_links(req);
3404 io_cqring_add_event(req, ret);
3405 fput(req->close.put_file);
Pavel Begunkov014db002020-03-03 21:33:12 +03003406 io_put_req(req);
Pavel Begunkova93b3332020-02-08 14:04:34 +03003407}
3408
Jens Axboeb5dba592019-12-11 14:02:38 -07003409static void io_close_finish(struct io_wq_work **workptr)
3410{
3411 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Jens Axboeb5dba592019-12-11 14:02:38 -07003412
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03003413 /* not cancellable, don't do io_req_cancelled() */
Pavel Begunkov014db002020-03-03 21:33:12 +03003414 __io_close_finish(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03003415 io_steal_work(req, workptr);
Jens Axboeb5dba592019-12-11 14:02:38 -07003416}
3417
Pavel Begunkov014db002020-03-03 21:33:12 +03003418static int io_close(struct io_kiocb *req, bool force_nonblock)
Jens Axboeb5dba592019-12-11 14:02:38 -07003419{
3420 int ret;
3421
3422 req->close.put_file = NULL;
3423 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
3424 if (ret < 0)
3425 return ret;
3426
3427 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkova2100672020-03-02 23:45:16 +03003428 if (req->close.put_file->f_op->flush && force_nonblock) {
Pavel Begunkov594506f2020-03-03 21:33:11 +03003429 /* submission ref will be dropped, take it for async */
3430 refcount_inc(&req->refs);
3431
Pavel Begunkova2100672020-03-02 23:45:16 +03003432 req->work.func = io_close_finish;
3433 /*
3434 * Do manual async queue here to avoid grabbing files - we don't
3435 * need the files, and it'll cause io_close_finish() to close
3436 * the file again and cause a double CQE entry for this request
3437 */
3438 io_queue_async_work(req);
3439 return 0;
3440 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003441
3442 /*
3443 * No ->flush(), safely close from here and just punt the
3444 * fput() to async context.
3445 */
Pavel Begunkov014db002020-03-03 21:33:12 +03003446 __io_close_finish(req);
Jens Axboe1a417f42020-01-31 17:16:48 -07003447 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07003448}
3449
Jens Axboe3529d8c2019-12-19 18:24:38 -07003450static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003451{
3452 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003453
3454 if (!req->file)
3455 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003456
3457 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3458 return -EINVAL;
3459 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
3460 return -EINVAL;
3461
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003462 req->sync.off = READ_ONCE(sqe->off);
3463 req->sync.len = READ_ONCE(sqe->len);
3464 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003465 return 0;
3466}
3467
Pavel Begunkov014db002020-03-03 21:33:12 +03003468static void __io_sync_file_range(struct io_kiocb *req)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003469{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003470 int ret;
3471
Jens Axboe9adbd452019-12-20 08:45:55 -07003472 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003473 req->sync.flags);
3474 if (ret < 0)
3475 req_set_fail_links(req);
3476 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003477 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03003478}
3479
3480
3481static void io_sync_file_range_finish(struct io_wq_work **workptr)
3482{
3483 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3484 struct io_kiocb *nxt = NULL;
3485
3486 if (io_req_cancelled(req))
3487 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03003488 __io_sync_file_range(req);
Pavel Begunkov594506f2020-03-03 21:33:11 +03003489 io_put_req(req); /* put submission ref */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003490 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003491 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003492}
3493
Pavel Begunkov014db002020-03-03 21:33:12 +03003494static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003495{
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003496 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003497 if (force_nonblock) {
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003498 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003499 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003500 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003501
Pavel Begunkov014db002020-03-03 21:33:12 +03003502 __io_sync_file_range(req);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003503 return 0;
3504}
3505
YueHaibing469956e2020-03-04 15:53:52 +08003506#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003507static int io_setup_async_msg(struct io_kiocb *req,
3508 struct io_async_msghdr *kmsg)
3509{
3510 if (req->io)
3511 return -EAGAIN;
3512 if (io_alloc_async_ctx(req)) {
3513 if (kmsg->iov != kmsg->fast_iov)
3514 kfree(kmsg->iov);
3515 return -ENOMEM;
3516 }
3517 req->flags |= REQ_F_NEED_CLEANUP;
3518 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
3519 return -EAGAIN;
3520}
3521
Jens Axboe3529d8c2019-12-19 18:24:38 -07003522static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06003523{
Jens Axboee47293f2019-12-20 08:58:21 -07003524 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003525 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003526 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003527
Jens Axboee47293f2019-12-20 08:58:21 -07003528 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3529 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07003530 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003531
Jens Axboed8768362020-02-27 14:17:49 -07003532#ifdef CONFIG_COMPAT
3533 if (req->ctx->compat)
3534 sr->msg_flags |= MSG_CMSG_COMPAT;
3535#endif
3536
Jens Axboefddafac2020-01-04 20:19:44 -07003537 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003538 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003539 /* iovec is already imported */
3540 if (req->flags & REQ_F_NEED_CLEANUP)
3541 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003542
Jens Axboed9688562019-12-09 19:35:20 -07003543 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003544 ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003545 &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003546 if (!ret)
3547 req->flags |= REQ_F_NEED_CLEANUP;
3548 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003549}
3550
Pavel Begunkov014db002020-03-03 21:33:12 +03003551static int io_sendmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003552{
Jens Axboe0b416c32019-12-15 10:57:46 -07003553 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07003554 struct socket *sock;
3555 int ret;
3556
3557 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3558 return -EINVAL;
3559
3560 sock = sock_from_file(req->file, &ret);
3561 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003562 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003563 unsigned flags;
3564
Jens Axboe03b12302019-12-02 18:50:25 -07003565 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003566 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003567 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003568 /* if iov is set, it's allocated already */
3569 if (!kmsg->iov)
3570 kmsg->iov = kmsg->fast_iov;
3571 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003572 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003573 struct io_sr_msg *sr = &req->sr_msg;
3574
Jens Axboe0b416c32019-12-15 10:57:46 -07003575 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003576 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003577
3578 io.msg.iov = io.msg.fast_iov;
3579 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3580 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003581 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003582 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003583 }
3584
Jens Axboee47293f2019-12-20 08:58:21 -07003585 flags = req->sr_msg.msg_flags;
3586 if (flags & MSG_DONTWAIT)
3587 req->flags |= REQ_F_NOWAIT;
3588 else if (force_nonblock)
3589 flags |= MSG_DONTWAIT;
3590
Jens Axboe0b416c32019-12-15 10:57:46 -07003591 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003592 if (force_nonblock && ret == -EAGAIN)
3593 return io_setup_async_msg(req, kmsg);
Jens Axboe03b12302019-12-02 18:50:25 -07003594 if (ret == -ERESTARTSYS)
3595 ret = -EINTR;
3596 }
3597
Pavel Begunkov1e950812020-02-06 19:51:16 +03003598 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003599 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003600 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe03b12302019-12-02 18:50:25 -07003601 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003602 if (ret < 0)
3603 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003604 io_put_req(req);
Jens Axboe03b12302019-12-02 18:50:25 -07003605 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003606}
3607
Pavel Begunkov014db002020-03-03 21:33:12 +03003608static int io_send(struct io_kiocb *req, bool force_nonblock)
Jens Axboefddafac2020-01-04 20:19:44 -07003609{
Jens Axboefddafac2020-01-04 20:19:44 -07003610 struct socket *sock;
3611 int ret;
3612
3613 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3614 return -EINVAL;
3615
3616 sock = sock_from_file(req->file, &ret);
3617 if (sock) {
3618 struct io_sr_msg *sr = &req->sr_msg;
3619 struct msghdr msg;
3620 struct iovec iov;
3621 unsigned flags;
3622
3623 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3624 &msg.msg_iter);
3625 if (ret)
3626 return ret;
3627
3628 msg.msg_name = NULL;
3629 msg.msg_control = NULL;
3630 msg.msg_controllen = 0;
3631 msg.msg_namelen = 0;
3632
3633 flags = req->sr_msg.msg_flags;
3634 if (flags & MSG_DONTWAIT)
3635 req->flags |= REQ_F_NOWAIT;
3636 else if (force_nonblock)
3637 flags |= MSG_DONTWAIT;
3638
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003639 msg.msg_flags = flags;
3640 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003641 if (force_nonblock && ret == -EAGAIN)
3642 return -EAGAIN;
3643 if (ret == -ERESTARTSYS)
3644 ret = -EINTR;
3645 }
3646
3647 io_cqring_add_event(req, ret);
3648 if (ret < 0)
3649 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003650 io_put_req(req);
Jens Axboefddafac2020-01-04 20:19:44 -07003651 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003652}
3653
Jens Axboe52de1fe2020-02-27 10:15:42 -07003654static int __io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
3655{
3656 struct io_sr_msg *sr = &req->sr_msg;
3657 struct iovec __user *uiov;
3658 size_t iov_len;
3659 int ret;
3660
3661 ret = __copy_msghdr_from_user(&io->msg.msg, sr->msg, &io->msg.uaddr,
3662 &uiov, &iov_len);
3663 if (ret)
3664 return ret;
3665
3666 if (req->flags & REQ_F_BUFFER_SELECT) {
3667 if (iov_len > 1)
3668 return -EINVAL;
3669 if (copy_from_user(io->msg.iov, uiov, sizeof(*uiov)))
3670 return -EFAULT;
3671 sr->len = io->msg.iov[0].iov_len;
3672 iov_iter_init(&io->msg.msg.msg_iter, READ, io->msg.iov, 1,
3673 sr->len);
3674 io->msg.iov = NULL;
3675 } else {
3676 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
3677 &io->msg.iov, &io->msg.msg.msg_iter);
3678 if (ret > 0)
3679 ret = 0;
3680 }
3681
3682 return ret;
3683}
3684
3685#ifdef CONFIG_COMPAT
3686static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
3687 struct io_async_ctx *io)
3688{
3689 struct compat_msghdr __user *msg_compat;
3690 struct io_sr_msg *sr = &req->sr_msg;
3691 struct compat_iovec __user *uiov;
3692 compat_uptr_t ptr;
3693 compat_size_t len;
3694 int ret;
3695
3696 msg_compat = (struct compat_msghdr __user *) sr->msg;
3697 ret = __get_compat_msghdr(&io->msg.msg, msg_compat, &io->msg.uaddr,
3698 &ptr, &len);
3699 if (ret)
3700 return ret;
3701
3702 uiov = compat_ptr(ptr);
3703 if (req->flags & REQ_F_BUFFER_SELECT) {
3704 compat_ssize_t clen;
3705
3706 if (len > 1)
3707 return -EINVAL;
3708 if (!access_ok(uiov, sizeof(*uiov)))
3709 return -EFAULT;
3710 if (__get_user(clen, &uiov->iov_len))
3711 return -EFAULT;
3712 if (clen < 0)
3713 return -EINVAL;
3714 sr->len = io->msg.iov[0].iov_len;
3715 io->msg.iov = NULL;
3716 } else {
3717 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
3718 &io->msg.iov,
3719 &io->msg.msg.msg_iter);
3720 if (ret < 0)
3721 return ret;
3722 }
3723
3724 return 0;
3725}
Jens Axboe03b12302019-12-02 18:50:25 -07003726#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07003727
3728static int io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
3729{
3730 io->msg.iov = io->msg.fast_iov;
3731
3732#ifdef CONFIG_COMPAT
3733 if (req->ctx->compat)
3734 return __io_compat_recvmsg_copy_hdr(req, io);
3735#endif
3736
3737 return __io_recvmsg_copy_hdr(req, io);
3738}
3739
Jens Axboebcda7ba2020-02-23 16:42:51 -07003740static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
3741 int *cflags, bool needs_lock)
3742{
3743 struct io_sr_msg *sr = &req->sr_msg;
3744 struct io_buffer *kbuf;
3745
3746 if (!(req->flags & REQ_F_BUFFER_SELECT))
3747 return NULL;
3748
3749 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
3750 if (IS_ERR(kbuf))
3751 return kbuf;
3752
3753 sr->kbuf = kbuf;
3754 req->flags |= REQ_F_BUFFER_SELECTED;
3755
3756 *cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
3757 *cflags |= IORING_CQE_F_BUFFER;
3758 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07003759}
3760
Jens Axboe3529d8c2019-12-19 18:24:38 -07003761static int io_recvmsg_prep(struct io_kiocb *req,
3762 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003763{
Jens Axboee47293f2019-12-20 08:58:21 -07003764 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003765 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003766 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07003767
Jens Axboe3529d8c2019-12-19 18:24:38 -07003768 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3769 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003770 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003771 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003772
Jens Axboed8768362020-02-27 14:17:49 -07003773#ifdef CONFIG_COMPAT
3774 if (req->ctx->compat)
3775 sr->msg_flags |= MSG_CMSG_COMPAT;
3776#endif
3777
Jens Axboefddafac2020-01-04 20:19:44 -07003778 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003779 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003780 /* iovec is already imported */
3781 if (req->flags & REQ_F_NEED_CLEANUP)
3782 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003783
Jens Axboe52de1fe2020-02-27 10:15:42 -07003784 ret = io_recvmsg_copy_hdr(req, io);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003785 if (!ret)
3786 req->flags |= REQ_F_NEED_CLEANUP;
3787 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003788}
3789
Pavel Begunkov014db002020-03-03 21:33:12 +03003790static int io_recvmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003791{
Jens Axboe0b416c32019-12-15 10:57:46 -07003792 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003793 struct socket *sock;
Jens Axboe52de1fe2020-02-27 10:15:42 -07003794 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003795
3796 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3797 return -EINVAL;
3798
3799 sock = sock_from_file(req->file, &ret);
3800 if (sock) {
Jens Axboe52de1fe2020-02-27 10:15:42 -07003801 struct io_buffer *kbuf;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003802 struct io_async_ctx io;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003803 unsigned flags;
3804
Jens Axboe03b12302019-12-02 18:50:25 -07003805 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003806 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003807 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003808 /* if iov is set, it's allocated already */
3809 if (!kmsg->iov)
3810 kmsg->iov = kmsg->fast_iov;
3811 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003812 } else {
Jens Axboe0b416c32019-12-15 10:57:46 -07003813 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003814 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003815
Jens Axboe52de1fe2020-02-27 10:15:42 -07003816 ret = io_recvmsg_copy_hdr(req, &io);
Jens Axboe03b12302019-12-02 18:50:25 -07003817 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003818 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003819 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003820
Jens Axboe52de1fe2020-02-27 10:15:42 -07003821 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
3822 if (IS_ERR(kbuf)) {
3823 return PTR_ERR(kbuf);
3824 } else if (kbuf) {
3825 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3826 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
3827 1, req->sr_msg.len);
3828 }
3829
Jens Axboee47293f2019-12-20 08:58:21 -07003830 flags = req->sr_msg.msg_flags;
3831 if (flags & MSG_DONTWAIT)
3832 req->flags |= REQ_F_NOWAIT;
3833 else if (force_nonblock)
3834 flags |= MSG_DONTWAIT;
3835
3836 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3837 kmsg->uaddr, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003838 if (force_nonblock && ret == -EAGAIN)
3839 return io_setup_async_msg(req, kmsg);
Jens Axboe441cdbd2019-12-02 18:49:10 -07003840 if (ret == -ERESTARTSYS)
3841 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003842 }
3843
Pavel Begunkov1e950812020-02-06 19:51:16 +03003844 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003845 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003846 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe52de1fe2020-02-27 10:15:42 -07003847 __io_cqring_add_event(req, ret, cflags);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003848 if (ret < 0)
3849 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003850 io_put_req(req);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003851 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003852}
3853
Pavel Begunkov014db002020-03-03 21:33:12 +03003854static int io_recv(struct io_kiocb *req, bool force_nonblock)
Jens Axboefddafac2020-01-04 20:19:44 -07003855{
Jens Axboebcda7ba2020-02-23 16:42:51 -07003856 struct io_buffer *kbuf = NULL;
Jens Axboefddafac2020-01-04 20:19:44 -07003857 struct socket *sock;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003858 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003859
3860 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3861 return -EINVAL;
3862
3863 sock = sock_from_file(req->file, &ret);
3864 if (sock) {
3865 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003866 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07003867 struct msghdr msg;
3868 struct iovec iov;
3869 unsigned flags;
3870
Jens Axboebcda7ba2020-02-23 16:42:51 -07003871 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
3872 if (IS_ERR(kbuf))
3873 return PTR_ERR(kbuf);
3874 else if (kbuf)
3875 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07003876
Jens Axboebcda7ba2020-02-23 16:42:51 -07003877 ret = import_single_range(READ, buf, sr->len, &iov,
3878 &msg.msg_iter);
3879 if (ret) {
3880 kfree(kbuf);
3881 return ret;
3882 }
3883
3884 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07003885 msg.msg_name = NULL;
3886 msg.msg_control = NULL;
3887 msg.msg_controllen = 0;
3888 msg.msg_namelen = 0;
3889 msg.msg_iocb = NULL;
3890 msg.msg_flags = 0;
3891
3892 flags = req->sr_msg.msg_flags;
3893 if (flags & MSG_DONTWAIT)
3894 req->flags |= REQ_F_NOWAIT;
3895 else if (force_nonblock)
3896 flags |= MSG_DONTWAIT;
3897
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003898 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07003899 if (force_nonblock && ret == -EAGAIN)
3900 return -EAGAIN;
3901 if (ret == -ERESTARTSYS)
3902 ret = -EINTR;
3903 }
3904
Jens Axboebcda7ba2020-02-23 16:42:51 -07003905 kfree(kbuf);
3906 req->flags &= ~REQ_F_NEED_CLEANUP;
3907 __io_cqring_add_event(req, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07003908 if (ret < 0)
3909 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003910 io_put_req(req);
Jens Axboefddafac2020-01-04 20:19:44 -07003911 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003912}
3913
Jens Axboe3529d8c2019-12-19 18:24:38 -07003914static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003915{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003916 struct io_accept *accept = &req->accept;
3917
Jens Axboe17f2fe32019-10-17 14:42:58 -06003918 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3919 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003920 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003921 return -EINVAL;
3922
Jens Axboed55e5f52019-12-11 16:12:15 -07003923 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3924 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003925 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06003926 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003927 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003928}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003929
Pavel Begunkov014db002020-03-03 21:33:12 +03003930static int __io_accept(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003931{
3932 struct io_accept *accept = &req->accept;
3933 unsigned file_flags;
3934 int ret;
3935
3936 file_flags = force_nonblock ? O_NONBLOCK : 0;
3937 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06003938 accept->addr_len, accept->flags,
3939 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003940 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003941 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003942 if (ret == -ERESTARTSYS)
3943 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003944 if (ret < 0)
3945 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003946 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003947 io_put_req(req);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003948 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003949}
3950
3951static void io_accept_finish(struct io_wq_work **workptr)
3952{
3953 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003954
3955 if (io_req_cancelled(req))
3956 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03003957 __io_accept(req, false);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03003958 io_steal_work(req, workptr);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003959}
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003960
Pavel Begunkov014db002020-03-03 21:33:12 +03003961static int io_accept(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003962{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003963 int ret;
3964
Pavel Begunkov014db002020-03-03 21:33:12 +03003965 ret = __io_accept(req, force_nonblock);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003966 if (ret == -EAGAIN && force_nonblock) {
3967 req->work.func = io_accept_finish;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003968 return -EAGAIN;
3969 }
3970 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003971}
3972
Jens Axboe3529d8c2019-12-19 18:24:38 -07003973static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003974{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003975 struct io_connect *conn = &req->connect;
3976 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003977
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003978 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3979 return -EINVAL;
3980 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3981 return -EINVAL;
3982
Jens Axboe3529d8c2019-12-19 18:24:38 -07003983 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3984 conn->addr_len = READ_ONCE(sqe->addr2);
3985
3986 if (!io)
3987 return 0;
3988
3989 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003990 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003991}
3992
Pavel Begunkov014db002020-03-03 21:33:12 +03003993static int io_connect(struct io_kiocb *req, bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003994{
Jens Axboef499a022019-12-02 16:28:46 -07003995 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003996 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003997 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003998
Jens Axboef499a022019-12-02 16:28:46 -07003999 if (req->io) {
4000 io = req->io;
4001 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004002 ret = move_addr_to_kernel(req->connect.addr,
4003 req->connect.addr_len,
4004 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004005 if (ret)
4006 goto out;
4007 io = &__io;
4008 }
4009
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004010 file_flags = force_nonblock ? O_NONBLOCK : 0;
4011
4012 ret = __sys_connect_file(req->file, &io->connect.address,
4013 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004014 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004015 if (req->io)
4016 return -EAGAIN;
4017 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004018 ret = -ENOMEM;
4019 goto out;
4020 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004021 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004022 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004023 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004024 if (ret == -ERESTARTSYS)
4025 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004026out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004027 if (ret < 0)
4028 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004029 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03004030 io_put_req(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004031 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004032}
YueHaibing469956e2020-03-04 15:53:52 +08004033#else /* !CONFIG_NET */
4034static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4035{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004036 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004037}
4038
YueHaibing469956e2020-03-04 15:53:52 +08004039static int io_sendmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004040{
YueHaibing469956e2020-03-04 15:53:52 +08004041 return -EOPNOTSUPP;
4042}
4043
4044static int io_send(struct io_kiocb *req, bool force_nonblock)
4045{
4046 return -EOPNOTSUPP;
4047}
4048
4049static int io_recvmsg_prep(struct io_kiocb *req,
4050 const struct io_uring_sqe *sqe)
4051{
4052 return -EOPNOTSUPP;
4053}
4054
4055static int io_recvmsg(struct io_kiocb *req, bool force_nonblock)
4056{
4057 return -EOPNOTSUPP;
4058}
4059
4060static int io_recv(struct io_kiocb *req, bool force_nonblock)
4061{
4062 return -EOPNOTSUPP;
4063}
4064
4065static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4066{
4067 return -EOPNOTSUPP;
4068}
4069
4070static int io_accept(struct io_kiocb *req, bool force_nonblock)
4071{
4072 return -EOPNOTSUPP;
4073}
4074
4075static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4076{
4077 return -EOPNOTSUPP;
4078}
4079
4080static int io_connect(struct io_kiocb *req, bool force_nonblock)
4081{
4082 return -EOPNOTSUPP;
4083}
4084#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004085
Jens Axboed7718a92020-02-14 22:23:12 -07004086struct io_poll_table {
4087 struct poll_table_struct pt;
4088 struct io_kiocb *req;
4089 int error;
4090};
4091
4092static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
4093 struct wait_queue_head *head)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004094{
Jens Axboed7718a92020-02-14 22:23:12 -07004095 if (unlikely(poll->head)) {
4096 pt->error = -EINVAL;
4097 return;
4098 }
4099
4100 pt->error = 0;
4101 poll->head = head;
4102 add_wait_queue(head, &poll->wait);
4103}
4104
4105static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4106 struct poll_table_struct *p)
4107{
4108 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4109
4110 __io_queue_proc(&pt->req->apoll->poll, pt, head);
4111}
4112
4113static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4114 __poll_t mask, task_work_func_t func)
4115{
4116 struct task_struct *tsk;
4117
4118 /* for instances that support it check for an event match first: */
4119 if (mask && !(mask & poll->events))
4120 return 0;
4121
4122 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4123
4124 list_del_init(&poll->wait.entry);
4125
4126 tsk = req->task;
4127 req->result = mask;
4128 init_task_work(&req->task_work, func);
4129 /*
4130 * If this fails, then the task is exiting. If that is the case, then
4131 * the exit check will ultimately cancel these work items. Hence we
4132 * don't need to check here and handle it specifically.
4133 */
4134 task_work_add(tsk, &req->task_work, true);
4135 wake_up_process(tsk);
4136 return 1;
4137}
4138
4139static void io_async_task_func(struct callback_head *cb)
4140{
4141 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4142 struct async_poll *apoll = req->apoll;
4143 struct io_ring_ctx *ctx = req->ctx;
4144
4145 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4146
4147 WARN_ON_ONCE(!list_empty(&req->apoll->poll.wait.entry));
4148
4149 if (hash_hashed(&req->hash_node)) {
4150 spin_lock_irq(&ctx->completion_lock);
4151 hash_del(&req->hash_node);
4152 spin_unlock_irq(&ctx->completion_lock);
4153 }
4154
4155 /* restore ->work in case we need to retry again */
4156 memcpy(&req->work, &apoll->work, sizeof(req->work));
4157
4158 __set_current_state(TASK_RUNNING);
4159 mutex_lock(&ctx->uring_lock);
4160 __io_queue_sqe(req, NULL);
4161 mutex_unlock(&ctx->uring_lock);
4162
4163 kfree(apoll);
4164}
4165
4166static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4167 void *key)
4168{
4169 struct io_kiocb *req = wait->private;
4170 struct io_poll_iocb *poll = &req->apoll->poll;
4171
4172 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4173 key_to_poll(key));
4174
4175 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4176}
4177
4178static void io_poll_req_insert(struct io_kiocb *req)
4179{
4180 struct io_ring_ctx *ctx = req->ctx;
4181 struct hlist_head *list;
4182
4183 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
4184 hlist_add_head(&req->hash_node, list);
4185}
4186
4187static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
4188 struct io_poll_iocb *poll,
4189 struct io_poll_table *ipt, __poll_t mask,
4190 wait_queue_func_t wake_func)
4191 __acquires(&ctx->completion_lock)
4192{
4193 struct io_ring_ctx *ctx = req->ctx;
4194 bool cancel = false;
4195
4196 poll->file = req->file;
4197 poll->head = NULL;
4198 poll->done = poll->canceled = false;
4199 poll->events = mask;
4200
4201 ipt->pt._key = mask;
4202 ipt->req = req;
4203 ipt->error = -EINVAL;
4204
4205 INIT_LIST_HEAD(&poll->wait.entry);
4206 init_waitqueue_func_entry(&poll->wait, wake_func);
4207 poll->wait.private = req;
4208
4209 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
4210
4211 spin_lock_irq(&ctx->completion_lock);
4212 if (likely(poll->head)) {
4213 spin_lock(&poll->head->lock);
4214 if (unlikely(list_empty(&poll->wait.entry))) {
4215 if (ipt->error)
4216 cancel = true;
4217 ipt->error = 0;
4218 mask = 0;
4219 }
4220 if (mask || ipt->error)
4221 list_del_init(&poll->wait.entry);
4222 else if (cancel)
4223 WRITE_ONCE(poll->canceled, true);
4224 else if (!poll->done) /* actually waiting for an event */
4225 io_poll_req_insert(req);
4226 spin_unlock(&poll->head->lock);
4227 }
4228
4229 return mask;
4230}
4231
4232static bool io_arm_poll_handler(struct io_kiocb *req)
4233{
4234 const struct io_op_def *def = &io_op_defs[req->opcode];
4235 struct io_ring_ctx *ctx = req->ctx;
4236 struct async_poll *apoll;
4237 struct io_poll_table ipt;
4238 __poll_t mask, ret;
4239
4240 if (!req->file || !file_can_poll(req->file))
4241 return false;
4242 if (req->flags & (REQ_F_MUST_PUNT | REQ_F_POLLED))
4243 return false;
4244 if (!def->pollin && !def->pollout)
4245 return false;
4246
4247 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
4248 if (unlikely(!apoll))
4249 return false;
4250
4251 req->flags |= REQ_F_POLLED;
4252 memcpy(&apoll->work, &req->work, sizeof(req->work));
4253
4254 /*
4255 * Don't need a reference here, as we're adding it to the task
4256 * task_works list. If the task exits, the list is pruned.
4257 */
4258 req->task = current;
4259 req->apoll = apoll;
4260 INIT_HLIST_NODE(&req->hash_node);
4261
Nathan Chancellor8755d972020-03-02 16:01:19 -07004262 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07004263 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07004264 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07004265 if (def->pollout)
4266 mask |= POLLOUT | POLLWRNORM;
4267 mask |= POLLERR | POLLPRI;
4268
4269 ipt.pt._qproc = io_async_queue_proc;
4270
4271 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
4272 io_async_wake);
4273 if (ret) {
4274 ipt.error = 0;
4275 apoll->poll.done = true;
4276 spin_unlock_irq(&ctx->completion_lock);
4277 memcpy(&req->work, &apoll->work, sizeof(req->work));
4278 kfree(apoll);
4279 return false;
4280 }
4281 spin_unlock_irq(&ctx->completion_lock);
4282 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
4283 apoll->poll.events);
4284 return true;
4285}
4286
4287static bool __io_poll_remove_one(struct io_kiocb *req,
4288 struct io_poll_iocb *poll)
4289{
Jens Axboeb41e9852020-02-17 09:52:41 -07004290 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004291
4292 spin_lock(&poll->head->lock);
4293 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07004294 if (!list_empty(&poll->wait.entry)) {
4295 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07004296 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004297 }
4298 spin_unlock(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07004299 return do_complete;
4300}
4301
4302static bool io_poll_remove_one(struct io_kiocb *req)
4303{
4304 bool do_complete;
4305
4306 if (req->opcode == IORING_OP_POLL_ADD) {
4307 do_complete = __io_poll_remove_one(req, &req->poll);
4308 } else {
4309 /* non-poll requests have submit ref still */
4310 do_complete = __io_poll_remove_one(req, &req->apoll->poll);
4311 if (do_complete)
4312 io_put_req(req);
4313 }
4314
Jens Axboe78076bb2019-12-04 19:56:40 -07004315 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07004316
Jens Axboeb41e9852020-02-17 09:52:41 -07004317 if (do_complete) {
4318 io_cqring_fill_event(req, -ECANCELED);
4319 io_commit_cqring(req->ctx);
4320 req->flags |= REQ_F_COMP_LOCKED;
4321 io_put_req(req);
4322 }
4323
4324 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004325}
4326
4327static void io_poll_remove_all(struct io_ring_ctx *ctx)
4328{
Jens Axboe78076bb2019-12-04 19:56:40 -07004329 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004330 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07004331 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004332
4333 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07004334 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
4335 struct hlist_head *list;
4336
4337 list = &ctx->cancel_hash[i];
4338 hlist_for_each_entry_safe(req, tmp, list, hash_node)
4339 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004340 }
4341 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07004342
4343 io_cqring_ev_posted(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004344}
4345
Jens Axboe47f46762019-11-09 17:43:02 -07004346static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
4347{
Jens Axboe78076bb2019-12-04 19:56:40 -07004348 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07004349 struct io_kiocb *req;
4350
Jens Axboe78076bb2019-12-04 19:56:40 -07004351 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
4352 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07004353 if (sqe_addr != req->user_data)
4354 continue;
4355 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07004356 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07004357 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07004358 }
4359
4360 return -ENOENT;
4361}
4362
Jens Axboe3529d8c2019-12-19 18:24:38 -07004363static int io_poll_remove_prep(struct io_kiocb *req,
4364 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004365{
Jens Axboe221c5eb2019-01-17 09:41:58 -07004366 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4367 return -EINVAL;
4368 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
4369 sqe->poll_events)
4370 return -EINVAL;
4371
Jens Axboe0969e782019-12-17 18:40:57 -07004372 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07004373 return 0;
4374}
4375
4376/*
4377 * Find a running poll command that matches one specified in sqe->addr,
4378 * and remove it if found.
4379 */
4380static int io_poll_remove(struct io_kiocb *req)
4381{
4382 struct io_ring_ctx *ctx = req->ctx;
4383 u64 addr;
4384 int ret;
4385
Jens Axboe0969e782019-12-17 18:40:57 -07004386 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004387 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07004388 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004389 spin_unlock_irq(&ctx->completion_lock);
4390
Jens Axboe78e19bb2019-11-06 15:21:34 -07004391 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004392 if (ret < 0)
4393 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004394 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004395 return 0;
4396}
4397
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004398static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004399{
Jackie Liua197f662019-11-08 08:09:12 -07004400 struct io_ring_ctx *ctx = req->ctx;
4401
Jens Axboe8c838782019-03-12 15:48:16 -06004402 req->poll.done = true;
Pavel Begunkovb0a20342020-02-28 10:36:35 +03004403 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06004404 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004405}
4406
Jens Axboeb41e9852020-02-17 09:52:41 -07004407static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004408{
Jens Axboe221c5eb2019-01-17 09:41:58 -07004409 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004410
Jens Axboe221c5eb2019-01-17 09:41:58 -07004411 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07004412 hash_del(&req->hash_node);
Jens Axboeb41e9852020-02-17 09:52:41 -07004413 io_poll_complete(req, req->result, 0);
4414 req->flags |= REQ_F_COMP_LOCKED;
4415 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004416 spin_unlock_irq(&ctx->completion_lock);
4417
Jens Axboe8c838782019-03-12 15:48:16 -06004418 io_cqring_ev_posted(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004419}
4420
Jens Axboeb41e9852020-02-17 09:52:41 -07004421static void io_poll_task_func(struct callback_head *cb)
Jens Axboee94f1412019-12-19 12:06:02 -07004422{
Jens Axboeb41e9852020-02-17 09:52:41 -07004423 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4424 struct io_kiocb *nxt = NULL;
Jens Axboee94f1412019-12-19 12:06:02 -07004425
Jens Axboeb41e9852020-02-17 09:52:41 -07004426 io_poll_task_handler(req, &nxt);
Jens Axboed7718a92020-02-14 22:23:12 -07004427 if (nxt) {
4428 struct io_ring_ctx *ctx = nxt->ctx;
Jens Axboee94f1412019-12-19 12:06:02 -07004429
Jens Axboed7718a92020-02-14 22:23:12 -07004430 mutex_lock(&ctx->uring_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07004431 __io_queue_sqe(nxt, NULL);
Jens Axboed7718a92020-02-14 22:23:12 -07004432 mutex_unlock(&ctx->uring_lock);
Jens Axboee94f1412019-12-19 12:06:02 -07004433 }
Jens Axboef0b493e2020-02-01 21:30:11 -07004434}
4435
Jens Axboe221c5eb2019-01-17 09:41:58 -07004436static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4437 void *key)
4438{
Jens Axboec2f2eb72020-02-10 09:07:05 -07004439 struct io_kiocb *req = wait->private;
4440 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004441
Jens Axboed7718a92020-02-14 22:23:12 -07004442 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004443}
4444
Jens Axboe221c5eb2019-01-17 09:41:58 -07004445static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
4446 struct poll_table_struct *p)
4447{
4448 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4449
Jens Axboed7718a92020-02-14 22:23:12 -07004450 __io_queue_proc(&pt->req->poll, pt, head);
Jens Axboeeac406c2019-11-14 12:09:58 -07004451}
4452
Jens Axboe3529d8c2019-12-19 18:24:38 -07004453static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004454{
4455 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004456 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004457
4458 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4459 return -EINVAL;
4460 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
4461 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06004462 if (!poll->file)
4463 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004464
Jens Axboe221c5eb2019-01-17 09:41:58 -07004465 events = READ_ONCE(sqe->poll_events);
4466 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboeb41e9852020-02-17 09:52:41 -07004467
Jens Axboed7718a92020-02-14 22:23:12 -07004468 /*
4469 * Don't need a reference here, as we're adding it to the task
4470 * task_works list. If the task exits, the list is pruned.
4471 */
Jens Axboeb41e9852020-02-17 09:52:41 -07004472 req->task = current;
Jens Axboe0969e782019-12-17 18:40:57 -07004473 return 0;
4474}
4475
Pavel Begunkov014db002020-03-03 21:33:12 +03004476static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07004477{
4478 struct io_poll_iocb *poll = &req->poll;
4479 struct io_ring_ctx *ctx = req->ctx;
4480 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07004481 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07004482
Jens Axboe78076bb2019-12-04 19:56:40 -07004483 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe36703242019-07-25 10:20:18 -06004484 INIT_LIST_HEAD(&req->list);
Jens Axboed7718a92020-02-14 22:23:12 -07004485 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06004486
Jens Axboed7718a92020-02-14 22:23:12 -07004487 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
4488 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004489
Jens Axboe8c838782019-03-12 15:48:16 -06004490 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06004491 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004492 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06004493 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07004494 spin_unlock_irq(&ctx->completion_lock);
4495
Jens Axboe8c838782019-03-12 15:48:16 -06004496 if (mask) {
4497 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03004498 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004499 }
Jens Axboe8c838782019-03-12 15:48:16 -06004500 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004501}
4502
Jens Axboe5262f562019-09-17 12:26:57 -06004503static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
4504{
Jens Axboead8a48a2019-11-15 08:49:11 -07004505 struct io_timeout_data *data = container_of(timer,
4506 struct io_timeout_data, timer);
4507 struct io_kiocb *req = data->req;
4508 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06004509 unsigned long flags;
4510
Jens Axboe5262f562019-09-17 12:26:57 -06004511 atomic_inc(&ctx->cq_timeouts);
4512
4513 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08004514 /*
Jens Axboe11365042019-10-16 09:08:32 -06004515 * We could be racing with timeout deletion. If the list is empty,
4516 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08004517 */
Jens Axboe842f9612019-10-29 12:34:10 -06004518 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06004519 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06004520
Jens Axboe11365042019-10-16 09:08:32 -06004521 /*
4522 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08004523 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06004524 * pointer will be increased, otherwise other timeout reqs may
4525 * return in advance without waiting for enough wait_nr.
4526 */
4527 prev = req;
4528 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
4529 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06004530 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06004531 }
Jens Axboe842f9612019-10-29 12:34:10 -06004532
Jens Axboe78e19bb2019-11-06 15:21:34 -07004533 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06004534 io_commit_cqring(ctx);
4535 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4536
4537 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004538 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004539 io_put_req(req);
4540 return HRTIMER_NORESTART;
4541}
4542
Jens Axboe47f46762019-11-09 17:43:02 -07004543static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
4544{
4545 struct io_kiocb *req;
4546 int ret = -ENOENT;
4547
4548 list_for_each_entry(req, &ctx->timeout_list, list) {
4549 if (user_data == req->user_data) {
4550 list_del_init(&req->list);
4551 ret = 0;
4552 break;
4553 }
4554 }
4555
4556 if (ret == -ENOENT)
4557 return ret;
4558
Jens Axboe2d283902019-12-04 11:08:05 -07004559 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07004560 if (ret == -1)
4561 return -EALREADY;
4562
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004563 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004564 io_cqring_fill_event(req, -ECANCELED);
4565 io_put_req(req);
4566 return 0;
4567}
4568
Jens Axboe3529d8c2019-12-19 18:24:38 -07004569static int io_timeout_remove_prep(struct io_kiocb *req,
4570 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07004571{
Jens Axboeb29472e2019-12-17 18:50:29 -07004572 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4573 return -EINVAL;
4574 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
4575 return -EINVAL;
4576
4577 req->timeout.addr = READ_ONCE(sqe->addr);
4578 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
4579 if (req->timeout.flags)
4580 return -EINVAL;
4581
Jens Axboeb29472e2019-12-17 18:50:29 -07004582 return 0;
4583}
4584
Jens Axboe11365042019-10-16 09:08:32 -06004585/*
4586 * Remove or update an existing timeout command
4587 */
Jens Axboefc4df992019-12-10 14:38:45 -07004588static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06004589{
4590 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07004591 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06004592
Jens Axboe11365042019-10-16 09:08:32 -06004593 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07004594 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06004595
Jens Axboe47f46762019-11-09 17:43:02 -07004596 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06004597 io_commit_cqring(ctx);
4598 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004599 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004600 if (ret < 0)
4601 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08004602 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06004603 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06004604}
4605
Jens Axboe3529d8c2019-12-19 18:24:38 -07004606static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07004607 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06004608{
Jens Axboead8a48a2019-11-15 08:49:11 -07004609 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06004610 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06004611
Jens Axboead8a48a2019-11-15 08:49:11 -07004612 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06004613 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07004614 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06004615 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07004616 if (sqe->off && is_timeout_link)
4617 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06004618 flags = READ_ONCE(sqe->timeout_flags);
4619 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06004620 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06004621
Jens Axboe26a61672019-12-20 09:02:01 -07004622 req->timeout.count = READ_ONCE(sqe->off);
4623
Jens Axboe3529d8c2019-12-19 18:24:38 -07004624 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07004625 return -ENOMEM;
4626
4627 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07004628 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07004629 req->flags |= REQ_F_TIMEOUT;
4630
4631 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06004632 return -EFAULT;
4633
Jens Axboe11365042019-10-16 09:08:32 -06004634 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07004635 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06004636 else
Jens Axboead8a48a2019-11-15 08:49:11 -07004637 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06004638
Jens Axboead8a48a2019-11-15 08:49:11 -07004639 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
4640 return 0;
4641}
4642
Jens Axboefc4df992019-12-10 14:38:45 -07004643static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07004644{
4645 unsigned count;
4646 struct io_ring_ctx *ctx = req->ctx;
4647 struct io_timeout_data *data;
4648 struct list_head *entry;
4649 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07004650
Jens Axboe2d283902019-12-04 11:08:05 -07004651 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004652
Jens Axboe5262f562019-09-17 12:26:57 -06004653 /*
4654 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07004655 * timeout event to be satisfied. If it isn't set, then this is
4656 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06004657 */
Jens Axboe26a61672019-12-20 09:02:01 -07004658 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004659 if (!count) {
4660 req->flags |= REQ_F_TIMEOUT_NOSEQ;
4661 spin_lock_irq(&ctx->completion_lock);
4662 entry = ctx->timeout_list.prev;
4663 goto add;
4664 }
Jens Axboe5262f562019-09-17 12:26:57 -06004665
4666 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07004667 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06004668
4669 /*
4670 * Insertion sort, ensuring the first entry in the list is always
4671 * the one we need first.
4672 */
Jens Axboe5262f562019-09-17 12:26:57 -06004673 spin_lock_irq(&ctx->completion_lock);
4674 list_for_each_prev(entry, &ctx->timeout_list) {
4675 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08004676 unsigned nxt_sq_head;
4677 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07004678 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06004679
Jens Axboe93bd25b2019-11-11 23:34:31 -07004680 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
4681 continue;
4682
yangerkun5da0fb12019-10-15 21:59:29 +08004683 /*
4684 * Since cached_sq_head + count - 1 can overflow, use type long
4685 * long to store it.
4686 */
4687 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03004688 nxt_sq_head = nxt->sequence - nxt_offset + 1;
4689 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08004690
4691 /*
4692 * cached_sq_head may overflow, and it will never overflow twice
4693 * once there is some timeout req still be valid.
4694 */
4695 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08004696 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08004697
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004698 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06004699 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004700
4701 /*
4702 * Sequence of reqs after the insert one and itself should
4703 * be adjusted because each timeout req consumes a slot.
4704 */
4705 span++;
4706 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06004707 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004708 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004709add:
Jens Axboe5262f562019-09-17 12:26:57 -06004710 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07004711 data->timer.function = io_timeout_fn;
4712 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06004713 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004714 return 0;
4715}
4716
Jens Axboe62755e32019-10-28 21:49:21 -06004717static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06004718{
Jens Axboe62755e32019-10-28 21:49:21 -06004719 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06004720
Jens Axboe62755e32019-10-28 21:49:21 -06004721 return req->user_data == (unsigned long) data;
4722}
4723
Jens Axboee977d6d2019-11-05 12:39:45 -07004724static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06004725{
Jens Axboe62755e32019-10-28 21:49:21 -06004726 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06004727 int ret = 0;
4728
Jens Axboe62755e32019-10-28 21:49:21 -06004729 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
4730 switch (cancel_ret) {
4731 case IO_WQ_CANCEL_OK:
4732 ret = 0;
4733 break;
4734 case IO_WQ_CANCEL_RUNNING:
4735 ret = -EALREADY;
4736 break;
4737 case IO_WQ_CANCEL_NOTFOUND:
4738 ret = -ENOENT;
4739 break;
4740 }
4741
Jens Axboee977d6d2019-11-05 12:39:45 -07004742 return ret;
4743}
4744
Jens Axboe47f46762019-11-09 17:43:02 -07004745static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
4746 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03004747 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07004748{
4749 unsigned long flags;
4750 int ret;
4751
4752 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
4753 if (ret != -ENOENT) {
4754 spin_lock_irqsave(&ctx->completion_lock, flags);
4755 goto done;
4756 }
4757
4758 spin_lock_irqsave(&ctx->completion_lock, flags);
4759 ret = io_timeout_cancel(ctx, sqe_addr);
4760 if (ret != -ENOENT)
4761 goto done;
4762 ret = io_poll_cancel(ctx, sqe_addr);
4763done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004764 if (!ret)
4765 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07004766 io_cqring_fill_event(req, ret);
4767 io_commit_cqring(ctx);
4768 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4769 io_cqring_ev_posted(ctx);
4770
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004771 if (ret < 0)
4772 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03004773 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004774}
4775
Jens Axboe3529d8c2019-12-19 18:24:38 -07004776static int io_async_cancel_prep(struct io_kiocb *req,
4777 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07004778{
Jens Axboefbf23842019-12-17 18:45:56 -07004779 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07004780 return -EINVAL;
4781 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
4782 sqe->cancel_flags)
4783 return -EINVAL;
4784
Jens Axboefbf23842019-12-17 18:45:56 -07004785 req->cancel.addr = READ_ONCE(sqe->addr);
4786 return 0;
4787}
4788
Pavel Begunkov014db002020-03-03 21:33:12 +03004789static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07004790{
4791 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07004792
Pavel Begunkov014db002020-03-03 21:33:12 +03004793 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06004794 return 0;
4795}
4796
Jens Axboe05f3fb32019-12-09 11:22:50 -07004797static int io_files_update_prep(struct io_kiocb *req,
4798 const struct io_uring_sqe *sqe)
4799{
4800 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
4801 return -EINVAL;
4802
4803 req->files_update.offset = READ_ONCE(sqe->off);
4804 req->files_update.nr_args = READ_ONCE(sqe->len);
4805 if (!req->files_update.nr_args)
4806 return -EINVAL;
4807 req->files_update.arg = READ_ONCE(sqe->addr);
4808 return 0;
4809}
4810
4811static int io_files_update(struct io_kiocb *req, bool force_nonblock)
4812{
4813 struct io_ring_ctx *ctx = req->ctx;
4814 struct io_uring_files_update up;
4815 int ret;
4816
Jens Axboef86cd202020-01-29 13:46:44 -07004817 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004818 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004819
4820 up.offset = req->files_update.offset;
4821 up.fds = req->files_update.arg;
4822
4823 mutex_lock(&ctx->uring_lock);
4824 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
4825 mutex_unlock(&ctx->uring_lock);
4826
4827 if (ret < 0)
4828 req_set_fail_links(req);
4829 io_cqring_add_event(req, ret);
4830 io_put_req(req);
4831 return 0;
4832}
4833
Jens Axboe3529d8c2019-12-19 18:24:38 -07004834static int io_req_defer_prep(struct io_kiocb *req,
4835 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07004836{
Jens Axboee7815732019-12-17 19:45:06 -07004837 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07004838
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03004839 if (!sqe)
4840 return 0;
4841
Jens Axboef86cd202020-01-29 13:46:44 -07004842 if (io_op_defs[req->opcode].file_table) {
4843 ret = io_grab_files(req);
4844 if (unlikely(ret))
4845 return ret;
4846 }
4847
Jens Axboecccf0ee2020-01-27 16:34:48 -07004848 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
4849
Jens Axboed625c6e2019-12-17 19:53:05 -07004850 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07004851 case IORING_OP_NOP:
4852 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004853 case IORING_OP_READV:
4854 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004855 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004856 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004857 break;
4858 case IORING_OP_WRITEV:
4859 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004860 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004861 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004862 break;
Jens Axboe0969e782019-12-17 18:40:57 -07004863 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004864 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004865 break;
4866 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004867 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004868 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004869 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004870 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004871 break;
4872 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004873 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004874 break;
Jens Axboe03b12302019-12-02 18:50:25 -07004875 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004876 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004877 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004878 break;
4879 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004880 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004881 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004882 break;
Jens Axboef499a022019-12-02 16:28:46 -07004883 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004884 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004885 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004886 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004887 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004888 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004889 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004890 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004891 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004892 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004893 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004894 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004895 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004896 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004897 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004898 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004899 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004900 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004901 case IORING_OP_FALLOCATE:
4902 ret = io_fallocate_prep(req, sqe);
4903 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004904 case IORING_OP_OPENAT:
4905 ret = io_openat_prep(req, sqe);
4906 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004907 case IORING_OP_CLOSE:
4908 ret = io_close_prep(req, sqe);
4909 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004910 case IORING_OP_FILES_UPDATE:
4911 ret = io_files_update_prep(req, sqe);
4912 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004913 case IORING_OP_STATX:
4914 ret = io_statx_prep(req, sqe);
4915 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004916 case IORING_OP_FADVISE:
4917 ret = io_fadvise_prep(req, sqe);
4918 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004919 case IORING_OP_MADVISE:
4920 ret = io_madvise_prep(req, sqe);
4921 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004922 case IORING_OP_OPENAT2:
4923 ret = io_openat2_prep(req, sqe);
4924 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004925 case IORING_OP_EPOLL_CTL:
4926 ret = io_epoll_ctl_prep(req, sqe);
4927 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004928 case IORING_OP_SPLICE:
4929 ret = io_splice_prep(req, sqe);
4930 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004931 case IORING_OP_PROVIDE_BUFFERS:
4932 ret = io_provide_buffers_prep(req, sqe);
4933 break;
Jens Axboe067524e2020-03-02 16:32:28 -07004934 case IORING_OP_REMOVE_BUFFERS:
4935 ret = io_remove_buffers_prep(req, sqe);
4936 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004937 default:
Jens Axboee7815732019-12-17 19:45:06 -07004938 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
4939 req->opcode);
4940 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004941 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004942 }
4943
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004944 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07004945}
4946
Jens Axboe3529d8c2019-12-19 18:24:38 -07004947static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06004948{
Jackie Liua197f662019-11-08 08:09:12 -07004949 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07004950 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06004951
Bob Liu9d858b22019-11-13 18:06:25 +08004952 /* Still need defer if there is pending req in defer list. */
4953 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06004954 return 0;
4955
Jens Axboe3529d8c2019-12-19 18:24:38 -07004956 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06004957 return -EAGAIN;
4958
Jens Axboe3529d8c2019-12-19 18:24:38 -07004959 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004960 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004961 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004962
Jens Axboede0617e2019-04-06 21:51:27 -06004963 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004964 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004965 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004966 return 0;
4967 }
4968
Jens Axboe915967f2019-11-21 09:01:20 -07004969 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004970 list_add_tail(&req->list, &ctx->defer_list);
4971 spin_unlock_irq(&ctx->completion_lock);
4972 return -EIOCBQUEUED;
4973}
4974
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004975static void io_cleanup_req(struct io_kiocb *req)
4976{
4977 struct io_async_ctx *io = req->io;
4978
4979 switch (req->opcode) {
4980 case IORING_OP_READV:
4981 case IORING_OP_READ_FIXED:
4982 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07004983 if (req->flags & REQ_F_BUFFER_SELECTED)
4984 kfree((void *)(unsigned long)req->rw.addr);
4985 /* fallthrough */
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004986 case IORING_OP_WRITEV:
4987 case IORING_OP_WRITE_FIXED:
4988 case IORING_OP_WRITE:
4989 if (io->rw.iov != io->rw.fast_iov)
4990 kfree(io->rw.iov);
4991 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004992 case IORING_OP_RECVMSG:
Jens Axboe52de1fe2020-02-27 10:15:42 -07004993 if (req->flags & REQ_F_BUFFER_SELECTED)
4994 kfree(req->sr_msg.kbuf);
4995 /* fallthrough */
4996 case IORING_OP_SENDMSG:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004997 if (io->msg.iov != io->msg.fast_iov)
4998 kfree(io->msg.iov);
4999 break;
Jens Axboebcda7ba2020-02-23 16:42:51 -07005000 case IORING_OP_RECV:
5001 if (req->flags & REQ_F_BUFFER_SELECTED)
5002 kfree(req->sr_msg.kbuf);
5003 break;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03005004 case IORING_OP_OPENAT:
5005 case IORING_OP_OPENAT2:
5006 case IORING_OP_STATX:
5007 putname(req->open.filename);
5008 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005009 case IORING_OP_SPLICE:
5010 io_put_file(req, req->splice.file_in,
5011 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5012 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005013 }
5014
5015 req->flags &= ~REQ_F_NEED_CLEANUP;
5016}
5017
Jens Axboe3529d8c2019-12-19 18:24:38 -07005018static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Pavel Begunkov014db002020-03-03 21:33:12 +03005019 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005020{
Jackie Liua197f662019-11-08 08:09:12 -07005021 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005022 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005023
Jens Axboed625c6e2019-12-17 19:53:05 -07005024 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005025 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005026 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005027 break;
5028 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005029 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005030 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005031 if (sqe) {
5032 ret = io_read_prep(req, sqe, force_nonblock);
5033 if (ret < 0)
5034 break;
5035 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005036 ret = io_read(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005037 break;
5038 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07005039 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005040 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005041 if (sqe) {
5042 ret = io_write_prep(req, sqe, force_nonblock);
5043 if (ret < 0)
5044 break;
5045 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005046 ret = io_write(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005047 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005048 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005049 if (sqe) {
5050 ret = io_prep_fsync(req, sqe);
5051 if (ret < 0)
5052 break;
5053 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005054 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005055 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005056 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005057 if (sqe) {
5058 ret = io_poll_add_prep(req, sqe);
5059 if (ret)
5060 break;
5061 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005062 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005063 break;
5064 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005065 if (sqe) {
5066 ret = io_poll_remove_prep(req, sqe);
5067 if (ret < 0)
5068 break;
5069 }
Jens Axboefc4df992019-12-10 14:38:45 -07005070 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005071 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005072 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005073 if (sqe) {
5074 ret = io_prep_sfr(req, sqe);
5075 if (ret < 0)
5076 break;
5077 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005078 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005079 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005080 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005081 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005082 if (sqe) {
5083 ret = io_sendmsg_prep(req, sqe);
5084 if (ret < 0)
5085 break;
5086 }
Jens Axboefddafac2020-01-04 20:19:44 -07005087 if (req->opcode == IORING_OP_SENDMSG)
Pavel Begunkov014db002020-03-03 21:33:12 +03005088 ret = io_sendmsg(req, force_nonblock);
Jens Axboefddafac2020-01-04 20:19:44 -07005089 else
Pavel Begunkov014db002020-03-03 21:33:12 +03005090 ret = io_send(req, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005091 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06005092 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005093 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005094 if (sqe) {
5095 ret = io_recvmsg_prep(req, sqe);
5096 if (ret)
5097 break;
5098 }
Jens Axboefddafac2020-01-04 20:19:44 -07005099 if (req->opcode == IORING_OP_RECVMSG)
Pavel Begunkov014db002020-03-03 21:33:12 +03005100 ret = io_recvmsg(req, force_nonblock);
Jens Axboefddafac2020-01-04 20:19:44 -07005101 else
Pavel Begunkov014db002020-03-03 21:33:12 +03005102 ret = io_recv(req, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06005103 break;
Jens Axboe5262f562019-09-17 12:26:57 -06005104 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005105 if (sqe) {
5106 ret = io_timeout_prep(req, sqe, false);
5107 if (ret)
5108 break;
5109 }
Jens Axboefc4df992019-12-10 14:38:45 -07005110 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005111 break;
Jens Axboe11365042019-10-16 09:08:32 -06005112 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005113 if (sqe) {
5114 ret = io_timeout_remove_prep(req, sqe);
5115 if (ret)
5116 break;
5117 }
Jens Axboefc4df992019-12-10 14:38:45 -07005118 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06005119 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06005120 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005121 if (sqe) {
5122 ret = io_accept_prep(req, sqe);
5123 if (ret)
5124 break;
5125 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005126 ret = io_accept(req, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005127 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005128 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005129 if (sqe) {
5130 ret = io_connect_prep(req, sqe);
5131 if (ret)
5132 break;
5133 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005134 ret = io_connect(req, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005135 break;
Jens Axboe62755e32019-10-28 21:49:21 -06005136 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005137 if (sqe) {
5138 ret = io_async_cancel_prep(req, sqe);
5139 if (ret)
5140 break;
5141 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005142 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005143 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005144 case IORING_OP_FALLOCATE:
5145 if (sqe) {
5146 ret = io_fallocate_prep(req, sqe);
5147 if (ret)
5148 break;
5149 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005150 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005151 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005152 case IORING_OP_OPENAT:
5153 if (sqe) {
5154 ret = io_openat_prep(req, sqe);
5155 if (ret)
5156 break;
5157 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005158 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005159 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005160 case IORING_OP_CLOSE:
5161 if (sqe) {
5162 ret = io_close_prep(req, sqe);
5163 if (ret)
5164 break;
5165 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005166 ret = io_close(req, force_nonblock);
Jens Axboeb5dba592019-12-11 14:02:38 -07005167 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005168 case IORING_OP_FILES_UPDATE:
5169 if (sqe) {
5170 ret = io_files_update_prep(req, sqe);
5171 if (ret)
5172 break;
5173 }
5174 ret = io_files_update(req, force_nonblock);
5175 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005176 case IORING_OP_STATX:
5177 if (sqe) {
5178 ret = io_statx_prep(req, sqe);
5179 if (ret)
5180 break;
5181 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005182 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005183 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005184 case IORING_OP_FADVISE:
5185 if (sqe) {
5186 ret = io_fadvise_prep(req, sqe);
5187 if (ret)
5188 break;
5189 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005190 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005191 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005192 case IORING_OP_MADVISE:
5193 if (sqe) {
5194 ret = io_madvise_prep(req, sqe);
5195 if (ret)
5196 break;
5197 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005198 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005199 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005200 case IORING_OP_OPENAT2:
5201 if (sqe) {
5202 ret = io_openat2_prep(req, sqe);
5203 if (ret)
5204 break;
5205 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005206 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005207 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005208 case IORING_OP_EPOLL_CTL:
5209 if (sqe) {
5210 ret = io_epoll_ctl_prep(req, sqe);
5211 if (ret)
5212 break;
5213 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005214 ret = io_epoll_ctl(req, force_nonblock);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005215 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005216 case IORING_OP_SPLICE:
5217 if (sqe) {
5218 ret = io_splice_prep(req, sqe);
5219 if (ret < 0)
5220 break;
5221 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005222 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005223 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005224 case IORING_OP_PROVIDE_BUFFERS:
5225 if (sqe) {
5226 ret = io_provide_buffers_prep(req, sqe);
5227 if (ret)
5228 break;
5229 }
5230 ret = io_provide_buffers(req, force_nonblock);
5231 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005232 case IORING_OP_REMOVE_BUFFERS:
5233 if (sqe) {
5234 ret = io_remove_buffers_prep(req, sqe);
5235 if (ret)
5236 break;
5237 }
5238 ret = io_remove_buffers(req, force_nonblock);
Jens Axboe31b51512019-01-18 22:56:34 -07005239 break;
5240 default:
5241 ret = -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07005242 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005243 }
5244
Jens Axboe31b51512019-01-18 22:56:34 -07005245 if (ret)
5246 return ret;
5247
5248 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07005249 const bool in_async = io_wq_current_is_worker();
5250
Jens Axboe9e645e112019-05-10 16:07:28 -06005251 if (req->result == -EAGAIN)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005252 return -EAGAIN;
5253
Jens Axboe11ba8202020-01-15 21:51:17 -07005254 /* workqueue context doesn't hold uring_lock, grab it now */
5255 if (in_async)
5256 mutex_lock(&ctx->uring_lock);
5257
Jens Axboe2b188cc2019-01-07 10:46:33 -07005258 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07005259
5260 if (in_async)
5261 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005262 }
5263
5264 return 0;
5265}
5266
Jens Axboe561fb042019-10-24 07:25:42 -06005267static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005268{
Jens Axboe561fb042019-10-24 07:25:42 -06005269 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005270 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06005271 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005272
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005273 /* if NO_CANCEL is set, we must still run the work */
5274 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
5275 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06005276 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005277 }
Jens Axboe31b51512019-01-18 22:56:34 -07005278
Jens Axboe561fb042019-10-24 07:25:42 -06005279 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06005280 do {
Pavel Begunkov014db002020-03-03 21:33:12 +03005281 ret = io_issue_sqe(req, NULL, false);
Jens Axboe561fb042019-10-24 07:25:42 -06005282 /*
5283 * We can get EAGAIN for polled IO even though we're
5284 * forcing a sync submission from here, since we can't
5285 * wait for request slots on the block side.
5286 */
5287 if (ret != -EAGAIN)
5288 break;
5289 cond_resched();
5290 } while (1);
5291 }
Jens Axboe31b51512019-01-18 22:56:34 -07005292
Jens Axboe561fb042019-10-24 07:25:42 -06005293 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005294 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07005295 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06005296 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07005297 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005298
Pavel Begunkove9fd9392020-03-04 16:14:12 +03005299 io_steal_work(req, workptr);
Jens Axboe31b51512019-01-18 22:56:34 -07005300}
Jens Axboe2b188cc2019-01-07 10:46:33 -07005301
Jens Axboe15b71ab2019-12-11 11:20:36 -07005302static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe9e3aa612019-12-11 15:55:43 -07005303{
Jens Axboed3656342019-12-18 09:50:26 -07005304 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07005305 return 0;
Jens Axboe0b5faf62020-02-06 21:42:51 -07005306 if ((fd == -1 || fd == AT_FDCWD) && io_op_defs[req->opcode].fd_non_neg)
Jens Axboed3656342019-12-18 09:50:26 -07005307 return 0;
5308 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06005309}
5310
Jens Axboe65e19f52019-10-26 07:20:21 -06005311static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
5312 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06005313{
Jens Axboe65e19f52019-10-26 07:20:21 -06005314 struct fixed_file_table *table;
5315
Jens Axboe05f3fb32019-12-09 11:22:50 -07005316 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
5317 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06005318}
5319
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005320static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
5321 int fd, struct file **out_file, bool fixed)
5322{
5323 struct io_ring_ctx *ctx = req->ctx;
5324 struct file *file;
5325
5326 if (fixed) {
5327 if (unlikely(!ctx->file_data ||
5328 (unsigned) fd >= ctx->nr_user_files))
5329 return -EBADF;
5330 fd = array_index_nospec(fd, ctx->nr_user_files);
5331 file = io_file_from_index(ctx, fd);
5332 if (!file)
5333 return -EBADF;
5334 percpu_ref_get(&ctx->file_data->refs);
5335 } else {
5336 trace_io_uring_file_get(ctx, fd);
5337 file = __io_file_get(state, fd);
5338 if (unlikely(!file))
5339 return -EBADF;
5340 }
5341
5342 *out_file = file;
5343 return 0;
5344}
5345
Jens Axboe3529d8c2019-12-19 18:24:38 -07005346static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
5347 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06005348{
5349 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07005350 int fd;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005351 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06005352
Jens Axboe3529d8c2019-12-19 18:24:38 -07005353 flags = READ_ONCE(sqe->flags);
5354 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06005355
Jens Axboed3656342019-12-18 09:50:26 -07005356 if (!io_req_needs_file(req, fd))
5357 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06005358
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005359 fixed = (flags & IOSQE_FIXED_FILE);
5360 if (unlikely(!fixed && req->needs_fixed_file))
5361 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06005362
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005363 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06005364}
5365
Jackie Liua197f662019-11-08 08:09:12 -07005366static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005367{
Jens Axboefcb323c2019-10-24 12:39:47 -06005368 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07005369 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06005370
Jens Axboef86cd202020-01-29 13:46:44 -07005371 if (req->work.files)
5372 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005373 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07005374 return -EBADF;
5375
Jens Axboefcb323c2019-10-24 12:39:47 -06005376 rcu_read_lock();
5377 spin_lock_irq(&ctx->inflight_lock);
5378 /*
5379 * We use the f_ops->flush() handler to ensure that we can flush
5380 * out work accessing these files if the fd is closed. Check if
5381 * the fd has changed since we started down this path, and disallow
5382 * this operation if it has.
5383 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005384 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06005385 list_add(&req->inflight_entry, &ctx->inflight_list);
5386 req->flags |= REQ_F_INFLIGHT;
5387 req->work.files = current->files;
5388 ret = 0;
5389 }
5390 spin_unlock_irq(&ctx->inflight_lock);
5391 rcu_read_unlock();
5392
5393 return ret;
5394}
5395
Jens Axboe2665abf2019-11-05 12:40:47 -07005396static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
5397{
Jens Axboead8a48a2019-11-15 08:49:11 -07005398 struct io_timeout_data *data = container_of(timer,
5399 struct io_timeout_data, timer);
5400 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07005401 struct io_ring_ctx *ctx = req->ctx;
5402 struct io_kiocb *prev = NULL;
5403 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07005404
5405 spin_lock_irqsave(&ctx->completion_lock, flags);
5406
5407 /*
5408 * We don't expect the list to be empty, that will only happen if we
5409 * race with the completion of the linked work.
5410 */
Pavel Begunkov44932332019-12-05 16:16:35 +03005411 if (!list_empty(&req->link_list)) {
5412 prev = list_entry(req->link_list.prev, struct io_kiocb,
5413 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005414 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03005415 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005416 prev->flags &= ~REQ_F_LINK_TIMEOUT;
5417 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07005418 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005419 }
5420
5421 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5422
5423 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005424 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03005425 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07005426 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07005427 } else {
5428 io_cqring_add_event(req, -ETIME);
5429 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005430 }
Jens Axboe2665abf2019-11-05 12:40:47 -07005431 return HRTIMER_NORESTART;
5432}
5433
Jens Axboead8a48a2019-11-15 08:49:11 -07005434static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005435{
Jens Axboe76a46e02019-11-10 23:34:16 -07005436 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07005437
Jens Axboe76a46e02019-11-10 23:34:16 -07005438 /*
5439 * If the list is now empty, then our linked request finished before
5440 * we got a chance to setup the timer
5441 */
5442 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03005443 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07005444 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07005445
Jens Axboead8a48a2019-11-15 08:49:11 -07005446 data->timer.function = io_link_timeout_fn;
5447 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
5448 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07005449 }
Jens Axboe76a46e02019-11-10 23:34:16 -07005450 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07005451
Jens Axboe2665abf2019-11-05 12:40:47 -07005452 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07005453 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005454}
5455
Jens Axboead8a48a2019-11-15 08:49:11 -07005456static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005457{
5458 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005459
Jens Axboe2665abf2019-11-05 12:40:47 -07005460 if (!(req->flags & REQ_F_LINK))
5461 return NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005462 /* for polled retry, if flag is set, we already went through here */
5463 if (req->flags & REQ_F_POLLED)
5464 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005465
Pavel Begunkov44932332019-12-05 16:16:35 +03005466 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
5467 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07005468 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07005469 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005470
Jens Axboe76a46e02019-11-10 23:34:16 -07005471 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07005472 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07005473}
5474
Jens Axboe3529d8c2019-12-19 18:24:38 -07005475static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005476{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005477 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005478 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07005479 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005480 int ret;
5481
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005482again:
5483 linked_timeout = io_prep_linked_timeout(req);
5484
Jens Axboe193155c2020-02-22 23:22:19 -07005485 if (req->work.creds && req->work.creds != current_cred()) {
5486 if (old_creds)
5487 revert_creds(old_creds);
5488 if (old_creds == req->work.creds)
5489 old_creds = NULL; /* restored original creds */
5490 else
5491 old_creds = override_creds(req->work.creds);
5492 }
5493
Pavel Begunkov014db002020-03-03 21:33:12 +03005494 ret = io_issue_sqe(req, sqe, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06005495
5496 /*
5497 * We async punt it if the file wasn't marked NOWAIT, or if the file
5498 * doesn't support non-blocking read/write attempts
5499 */
5500 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
5501 (req->flags & REQ_F_MUST_PUNT))) {
Jens Axboed7718a92020-02-14 22:23:12 -07005502 if (io_arm_poll_handler(req)) {
5503 if (linked_timeout)
5504 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005505 goto exit;
Jens Axboed7718a92020-02-14 22:23:12 -07005506 }
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005507punt:
Jens Axboef86cd202020-01-29 13:46:44 -07005508 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005509 ret = io_grab_files(req);
5510 if (ret)
5511 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005512 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005513
5514 /*
5515 * Queued up for async execution, worker will release
5516 * submit reference when the iocb is actually submitted.
5517 */
5518 io_queue_async_work(req);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005519 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005520 }
Jens Axboee65ef562019-03-12 10:16:44 -06005521
Jens Axboefcb323c2019-10-24 12:39:47 -06005522err:
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005523 nxt = NULL;
Jens Axboee65ef562019-03-12 10:16:44 -06005524 /* drop submission reference */
Jens Axboe2a44f462020-02-25 13:25:41 -07005525 io_put_req_find_next(req, &nxt);
Jens Axboee65ef562019-03-12 10:16:44 -06005526
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005527 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07005528 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005529 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005530 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005531 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005532 }
5533
Jens Axboee65ef562019-03-12 10:16:44 -06005534 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06005535 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07005536 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005537 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06005538 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06005539 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005540 if (nxt) {
5541 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005542
5543 if (req->flags & REQ_F_FORCE_ASYNC)
5544 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005545 goto again;
5546 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005547exit:
Jens Axboe193155c2020-02-22 23:22:19 -07005548 if (old_creds)
5549 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005550}
5551
Jens Axboe3529d8c2019-12-19 18:24:38 -07005552static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005553{
5554 int ret;
5555
Jens Axboe3529d8c2019-12-19 18:24:38 -07005556 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005557 if (ret) {
5558 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03005559fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005560 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005561 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07005562 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005563 }
Pavel Begunkov25508782019-12-30 21:24:47 +03005564 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkov11185912020-01-22 23:09:35 +03005565 ret = io_req_defer_prep(req, sqe);
5566 if (unlikely(ret < 0))
5567 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07005568 /*
5569 * Never try inline submit of IOSQE_ASYNC is set, go straight
5570 * to async execution.
5571 */
5572 req->work.flags |= IO_WQ_WORK_CONCURRENT;
5573 io_queue_async_work(req);
5574 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005575 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07005576 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08005577}
5578
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005579static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005580{
Jens Axboe94ae5e72019-11-14 19:39:52 -07005581 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005582 io_cqring_add_event(req, -ECANCELED);
5583 io_double_put_req(req);
5584 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07005585 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005586}
5587
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005588#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboebcda7ba2020-02-23 16:42:51 -07005589 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
5590 IOSQE_BUFFER_SELECT)
Jens Axboe9e645e112019-05-10 16:07:28 -06005591
Jens Axboe3529d8c2019-12-19 18:24:38 -07005592static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
5593 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06005594{
Jackie Liua197f662019-11-08 08:09:12 -07005595 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005596 unsigned int sqe_flags;
Jens Axboe75c6a032020-01-28 10:15:23 -07005597 int ret, id;
Jens Axboe9e645e112019-05-10 16:07:28 -06005598
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005599 sqe_flags = READ_ONCE(sqe->flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06005600
5601 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005602 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06005603 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03005604 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06005605 }
5606
Jens Axboebcda7ba2020-02-23 16:42:51 -07005607 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
5608 !io_op_defs[req->opcode].buffer_select) {
5609 ret = -EOPNOTSUPP;
5610 goto err_req;
5611 }
5612
Jens Axboe75c6a032020-01-28 10:15:23 -07005613 id = READ_ONCE(sqe->personality);
5614 if (id) {
Jens Axboe193155c2020-02-22 23:22:19 -07005615 req->work.creds = idr_find(&ctx->personality_idr, id);
5616 if (unlikely(!req->work.creds)) {
Jens Axboe75c6a032020-01-28 10:15:23 -07005617 ret = -EINVAL;
5618 goto err_req;
5619 }
Jens Axboe193155c2020-02-22 23:22:19 -07005620 get_cred(req->work.creds);
Jens Axboe75c6a032020-01-28 10:15:23 -07005621 }
5622
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03005623 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005624 req->flags |= sqe_flags & (IOSQE_IO_DRAIN | IOSQE_IO_HARDLINK |
Jens Axboebcda7ba2020-02-23 16:42:51 -07005625 IOSQE_ASYNC | IOSQE_FIXED_FILE |
5626 IOSQE_BUFFER_SELECT);
Jens Axboe9e645e112019-05-10 16:07:28 -06005627
Jens Axboe3529d8c2019-12-19 18:24:38 -07005628 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06005629 if (unlikely(ret)) {
5630err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005631 io_cqring_add_event(req, ret);
5632 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005633 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06005634 }
5635
Jens Axboe9e645e112019-05-10 16:07:28 -06005636 /*
5637 * If we already have a head request, queue this one for async
5638 * submittal once the head completes. If we don't have a head but
5639 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
5640 * submitted sync once the chain is complete. If none of those
5641 * conditions are true (normal request), then just queue it.
5642 */
5643 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03005644 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06005645
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03005646 /*
5647 * Taking sequential execution of a link, draining both sides
5648 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
5649 * requests in the link. So, it drains the head and the
5650 * next after the link request. The last one is done via
5651 * drain_next flag to persist the effect across calls.
5652 */
Pavel Begunkov711be032020-01-17 03:57:59 +03005653 if (sqe_flags & IOSQE_IO_DRAIN) {
5654 head->flags |= REQ_F_IO_DRAIN;
5655 ctx->drain_next = 1;
5656 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005657 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06005658 ret = -EAGAIN;
5659 goto err_req;
5660 }
5661
Jens Axboe3529d8c2019-12-19 18:24:38 -07005662 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005663 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005664 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03005665 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07005666 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07005667 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03005668 trace_io_uring_link(ctx, req, head);
5669 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06005670
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005671 /* last request of a link, enqueue the link */
5672 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
5673 io_queue_link_head(head);
5674 *link = NULL;
5675 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005676 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03005677 if (unlikely(ctx->drain_next)) {
5678 req->flags |= REQ_F_IO_DRAIN;
5679 req->ctx->drain_next = 0;
5680 }
5681 if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
5682 req->flags |= REQ_F_LINK;
Pavel Begunkov711be032020-01-17 03:57:59 +03005683 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03005684
5685 if (io_alloc_async_ctx(req)) {
5686 ret = -EAGAIN;
5687 goto err_req;
5688 }
Pavel Begunkov711be032020-01-17 03:57:59 +03005689 ret = io_req_defer_prep(req, sqe);
5690 if (ret)
5691 req->flags |= REQ_F_FAIL_LINK;
5692 *link = req;
5693 } else {
5694 io_queue_sqe(req, sqe);
5695 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005696 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005697
5698 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06005699}
5700
Jens Axboe9a56a232019-01-09 09:06:50 -07005701/*
5702 * Batched submission is done, ensure local IO is flushed out.
5703 */
5704static void io_submit_state_end(struct io_submit_state *state)
5705{
5706 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06005707 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07005708 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03005709 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07005710}
5711
5712/*
5713 * Start submission side cache.
5714 */
5715static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08005716 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07005717{
5718 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07005719 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07005720 state->file = NULL;
5721 state->ios_left = max_ios;
5722}
5723
Jens Axboe2b188cc2019-01-07 10:46:33 -07005724static void io_commit_sqring(struct io_ring_ctx *ctx)
5725{
Hristo Venev75b28af2019-08-26 17:23:46 +00005726 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005727
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03005728 /*
5729 * Ensure any loads from the SQEs are done at this point,
5730 * since once we write the new head, the application could
5731 * write new data to them.
5732 */
5733 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005734}
5735
5736/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07005737 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07005738 * that is mapped by userspace. This means that care needs to be taken to
5739 * ensure that reads are stable, as we cannot rely on userspace always
5740 * being a good citizen. If members of the sqe are validated and then later
5741 * used, it's important that those reads are done through READ_ONCE() to
5742 * prevent a re-load down the line.
5743 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07005744static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
5745 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005746{
Hristo Venev75b28af2019-08-26 17:23:46 +00005747 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005748 unsigned head;
5749
5750 /*
5751 * The cached sq head (or cq tail) serves two purposes:
5752 *
5753 * 1) allows us to batch the cost of updating the user visible
5754 * head updates.
5755 * 2) allows the kernel side to track the head on its own, even
5756 * though the application is the one updating it.
5757 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005758 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03005759 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03005760 /*
5761 * All io need record the previous position, if LINK vs DARIN,
5762 * it can be used to mark the position of the first IO in the
5763 * link list.
5764 */
5765 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07005766 *sqe_ptr = &ctx->sq_sqes[head];
5767 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
5768 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005769 ctx->cached_sq_head++;
5770 return true;
5771 }
5772
5773 /* drop invalid entries */
5774 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06005775 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005776 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005777 return false;
5778}
5779
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005780static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005781 struct file *ring_file, int ring_fd,
5782 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07005783{
5784 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005785 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005786 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005787 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005788
Jens Axboec4a2ed72019-11-21 21:01:26 -07005789 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07005790 if (test_bit(0, &ctx->sq_check_overflow)) {
5791 if (!list_empty(&ctx->cq_overflow_list) &&
5792 !io_cqring_overflow_flush(ctx, false))
5793 return -EBUSY;
5794 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005795
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005796 /* make sure SQ entry isn't read before tail */
5797 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03005798
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005799 if (!percpu_ref_tryget_many(&ctx->refs, nr))
5800 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005801
5802 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08005803 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005804 statep = &state;
5805 }
5806
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005807 ctx->ring_fd = ring_fd;
5808 ctx->ring_file = ring_file;
5809
Jens Axboe6c271ce2019-01-10 11:22:30 -07005810 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005811 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03005812 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005813 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005814
Pavel Begunkov196be952019-11-07 01:41:06 +03005815 req = io_get_req(ctx, statep);
5816 if (unlikely(!req)) {
5817 if (!submitted)
5818 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005819 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06005820 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07005821 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005822 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03005823 break;
5824 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005825
Jens Axboed3656342019-12-18 09:50:26 -07005826 /* will complete beyond this point, count as submitted */
5827 submitted++;
5828
5829 if (unlikely(req->opcode >= IORING_OP_LAST)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005830 err = -EINVAL;
5831fail_req:
5832 io_cqring_add_event(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07005833 io_double_put_req(req);
5834 break;
5835 }
5836
5837 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005838 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005839 if (unlikely(mm_fault)) {
5840 err = -EFAULT;
5841 goto fail_req;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005842 }
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005843 use_mm(ctx->sqo_mm);
5844 *mm = ctx->sqo_mm;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005845 }
5846
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03005847 req->needs_fixed_file = async;
Jens Axboe354420f2020-01-08 18:55:15 -07005848 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
5849 true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07005850 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005851 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005852 }
5853
Pavel Begunkov9466f432020-01-25 22:34:01 +03005854 if (unlikely(submitted != nr)) {
5855 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
5856
5857 percpu_ref_put_many(&ctx->refs, nr - ref_used);
5858 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005859 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005860 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005861 if (statep)
5862 io_submit_state_end(&state);
5863
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005864 /* Commit SQ ring head once we've consumed and submitted all SQEs */
5865 io_commit_sqring(ctx);
5866
Jens Axboe6c271ce2019-01-10 11:22:30 -07005867 return submitted;
5868}
5869
5870static int io_sq_thread(void *data)
5871{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005872 struct io_ring_ctx *ctx = data;
5873 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07005874 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005875 mm_segment_t old_fs;
5876 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005877 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005878 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005879
Jens Axboe206aefd2019-11-07 18:27:42 -07005880 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08005881
Jens Axboe6c271ce2019-01-10 11:22:30 -07005882 old_fs = get_fs();
5883 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07005884 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005885
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005886 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005887 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005888 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005889
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005890 if (!list_empty(&ctx->poll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005891 unsigned nr_events = 0;
5892
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005893 mutex_lock(&ctx->uring_lock);
5894 if (!list_empty(&ctx->poll_list))
5895 io_iopoll_getevents(ctx, &nr_events, 0);
5896 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07005897 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005898 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005899 }
5900
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005901 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005902
5903 /*
5904 * If submit got -EBUSY, flag us as needing the application
5905 * to enter the kernel to reap and flush events.
5906 */
5907 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005908 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01005909 * Drop cur_mm before scheduling, we can't hold it for
5910 * long periods (or over schedule()). Do this before
5911 * adding ourselves to the waitqueue, as the unuse/drop
5912 * may sleep.
5913 */
5914 if (cur_mm) {
5915 unuse_mm(cur_mm);
5916 mmput(cur_mm);
5917 cur_mm = NULL;
5918 }
5919
5920 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07005921 * We're polling. If we're within the defined idle
5922 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07005923 * to sleep. The exception is if we got EBUSY doing
5924 * more IO, we should wait for the application to
5925 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07005926 */
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005927 if (!list_empty(&ctx->poll_list) ||
Jens Axboedf069d82020-02-04 16:48:34 -07005928 (!time_after(jiffies, timeout) && ret != -EBUSY &&
5929 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005930 if (current->task_works)
5931 task_work_run();
Jens Axboe9831a902019-09-19 09:48:55 -06005932 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005933 continue;
5934 }
5935
Jens Axboe6c271ce2019-01-10 11:22:30 -07005936 prepare_to_wait(&ctx->sqo_wait, &wait,
5937 TASK_INTERRUPTIBLE);
5938
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005939 /*
5940 * While doing polled IO, before going to sleep, we need
5941 * to check if there are new reqs added to poll_list, it
5942 * is because reqs may have been punted to io worker and
5943 * will be added to poll_list later, hence check the
5944 * poll_list again.
5945 */
5946 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
5947 !list_empty_careful(&ctx->poll_list)) {
5948 finish_wait(&ctx->sqo_wait, &wait);
5949 continue;
5950 }
5951
Jens Axboe6c271ce2019-01-10 11:22:30 -07005952 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00005953 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02005954 /* make sure to read SQ tail after writing flags */
5955 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005956
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005957 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005958 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005959 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005960 finish_wait(&ctx->sqo_wait, &wait);
5961 break;
5962 }
Jens Axboeb41e9852020-02-17 09:52:41 -07005963 if (current->task_works) {
5964 task_work_run();
5965 continue;
5966 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005967 if (signal_pending(current))
5968 flush_signals(current);
5969 schedule();
5970 finish_wait(&ctx->sqo_wait, &wait);
5971
Hristo Venev75b28af2019-08-26 17:23:46 +00005972 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005973 continue;
5974 }
5975 finish_wait(&ctx->sqo_wait, &wait);
5976
Hristo Venev75b28af2019-08-26 17:23:46 +00005977 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005978 }
5979
Jens Axboe8a4955f2019-12-09 14:52:35 -07005980 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005981 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07005982 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005983 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005984 }
5985
Jens Axboeb41e9852020-02-17 09:52:41 -07005986 if (current->task_works)
5987 task_work_run();
5988
Jens Axboe6c271ce2019-01-10 11:22:30 -07005989 set_fs(old_fs);
5990 if (cur_mm) {
5991 unuse_mm(cur_mm);
5992 mmput(cur_mm);
5993 }
Jens Axboe181e4482019-11-25 08:52:30 -07005994 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06005995
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005996 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06005997
Jens Axboe6c271ce2019-01-10 11:22:30 -07005998 return 0;
5999}
6000
Jens Axboebda52162019-09-24 13:47:15 -06006001struct io_wait_queue {
6002 struct wait_queue_entry wq;
6003 struct io_ring_ctx *ctx;
6004 unsigned to_wait;
6005 unsigned nr_timeouts;
6006};
6007
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006008static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06006009{
6010 struct io_ring_ctx *ctx = iowq->ctx;
6011
6012 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006013 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006014 * started waiting. For timeouts, we always want to return to userspace,
6015 * regardless of event count.
6016 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006017 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006018 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6019}
6020
6021static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6022 int wake_flags, void *key)
6023{
6024 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6025 wq);
6026
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006027 /* use noflush == true, as we can't safely rely on locking context */
6028 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06006029 return -1;
6030
6031 return autoremove_wake_function(curr, mode, wake_flags, key);
6032}
6033
Jens Axboe2b188cc2019-01-07 10:46:33 -07006034/*
6035 * Wait until events become available, if we don't already have some. The
6036 * application must reap them itself, as they reside on the shared cq ring.
6037 */
6038static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6039 const sigset_t __user *sig, size_t sigsz)
6040{
Jens Axboebda52162019-09-24 13:47:15 -06006041 struct io_wait_queue iowq = {
6042 .wq = {
6043 .private = current,
6044 .func = io_wake_function,
6045 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6046 },
6047 .ctx = ctx,
6048 .to_wait = min_events,
6049 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006050 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006051 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006052
Jens Axboeb41e9852020-02-17 09:52:41 -07006053 do {
6054 if (io_cqring_events(ctx, false) >= min_events)
6055 return 0;
6056 if (!current->task_works)
6057 break;
6058 task_work_run();
6059 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006060
6061 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006062#ifdef CONFIG_COMPAT
6063 if (in_compat_syscall())
6064 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006065 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006066 else
6067#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006068 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006069
Jens Axboe2b188cc2019-01-07 10:46:33 -07006070 if (ret)
6071 return ret;
6072 }
6073
Jens Axboebda52162019-09-24 13:47:15 -06006074 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006075 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006076 do {
6077 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6078 TASK_INTERRUPTIBLE);
Jens Axboeb41e9852020-02-17 09:52:41 -07006079 if (current->task_works)
6080 task_work_run();
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006081 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06006082 break;
6083 schedule();
6084 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006085 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06006086 break;
6087 }
6088 } while (1);
6089 finish_wait(&ctx->wait, &iowq.wq);
6090
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006091 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006092
Hristo Venev75b28af2019-08-26 17:23:46 +00006093 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006094}
6095
Jens Axboe6b063142019-01-10 22:13:58 -07006096static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6097{
6098#if defined(CONFIG_UNIX)
6099 if (ctx->ring_sock) {
6100 struct sock *sock = ctx->ring_sock->sk;
6101 struct sk_buff *skb;
6102
6103 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6104 kfree_skb(skb);
6105 }
6106#else
6107 int i;
6108
Jens Axboe65e19f52019-10-26 07:20:21 -06006109 for (i = 0; i < ctx->nr_user_files; i++) {
6110 struct file *file;
6111
6112 file = io_file_from_index(ctx, i);
6113 if (file)
6114 fput(file);
6115 }
Jens Axboe6b063142019-01-10 22:13:58 -07006116#endif
6117}
6118
Jens Axboe05f3fb32019-12-09 11:22:50 -07006119static void io_file_ref_kill(struct percpu_ref *ref)
6120{
6121 struct fixed_file_data *data;
6122
6123 data = container_of(ref, struct fixed_file_data, refs);
6124 complete(&data->done);
6125}
6126
Jens Axboe805b13a2020-03-08 20:07:28 -06006127static void io_file_ref_exit_and_free(struct work_struct *work)
Jens Axboec1e21482020-03-04 07:25:50 -07006128{
Jens Axboe805b13a2020-03-08 20:07:28 -06006129 struct fixed_file_data *data;
6130
6131 data = container_of(work, struct fixed_file_data, ref_work);
6132
6133 /*
6134 * Ensure any percpu-ref atomic switch callback has run, it could have
6135 * been in progress when the files were being unregistered. Once
6136 * that's done, we can safely exit and free the ref and containing
6137 * data structure.
6138 */
6139 rcu_barrier();
Jens Axboec1e21482020-03-04 07:25:50 -07006140 percpu_ref_exit(&data->refs);
6141 kfree(data);
6142}
6143
Jens Axboe6b063142019-01-10 22:13:58 -07006144static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6145{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006146 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06006147 unsigned nr_tables, i;
6148
Jens Axboe05f3fb32019-12-09 11:22:50 -07006149 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006150 return -ENXIO;
6151
Jens Axboe05f3fb32019-12-09 11:22:50 -07006152 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
Jens Axboee46a7952020-01-17 11:15:34 -07006153 flush_work(&data->ref_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07006154 wait_for_completion(&data->done);
6155 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006156
Jens Axboe6b063142019-01-10 22:13:58 -07006157 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06006158 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6159 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006160 kfree(data->table[i].files);
6161 kfree(data->table);
Jens Axboe805b13a2020-03-08 20:07:28 -06006162 INIT_WORK(&data->ref_work, io_file_ref_exit_and_free);
6163 queue_work(system_wq, &data->ref_work);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006164 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006165 ctx->nr_user_files = 0;
6166 return 0;
6167}
6168
Jens Axboe6c271ce2019-01-10 11:22:30 -07006169static void io_sq_thread_stop(struct io_ring_ctx *ctx)
6170{
6171 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07006172 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006173 /*
6174 * The park is a bit of a work-around, without it we get
6175 * warning spews on shutdown with SQPOLL set and affinity
6176 * set to a single CPU.
6177 */
Jens Axboe06058632019-04-13 09:26:03 -06006178 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006179 kthread_stop(ctx->sqo_thread);
6180 ctx->sqo_thread = NULL;
6181 }
6182}
6183
Jens Axboe6b063142019-01-10 22:13:58 -07006184static void io_finish_async(struct io_ring_ctx *ctx)
6185{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006186 io_sq_thread_stop(ctx);
6187
Jens Axboe561fb042019-10-24 07:25:42 -06006188 if (ctx->io_wq) {
6189 io_wq_destroy(ctx->io_wq);
6190 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006191 }
6192}
6193
6194#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07006195/*
6196 * Ensure the UNIX gc is aware of our file set, so we are certain that
6197 * the io_uring can be safely unregistered on process exit, even if we have
6198 * loops in the file referencing.
6199 */
6200static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
6201{
6202 struct sock *sk = ctx->ring_sock->sk;
6203 struct scm_fp_list *fpl;
6204 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06006205 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07006206
6207 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
6208 unsigned long inflight = ctx->user->unix_inflight + nr;
6209
6210 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
6211 return -EMFILE;
6212 }
6213
6214 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
6215 if (!fpl)
6216 return -ENOMEM;
6217
6218 skb = alloc_skb(0, GFP_KERNEL);
6219 if (!skb) {
6220 kfree(fpl);
6221 return -ENOMEM;
6222 }
6223
6224 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07006225
Jens Axboe08a45172019-10-03 08:11:03 -06006226 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07006227 fpl->user = get_uid(ctx->user);
6228 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006229 struct file *file = io_file_from_index(ctx, i + offset);
6230
6231 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06006232 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06006233 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06006234 unix_inflight(fpl->user, fpl->fp[nr_files]);
6235 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07006236 }
6237
Jens Axboe08a45172019-10-03 08:11:03 -06006238 if (nr_files) {
6239 fpl->max = SCM_MAX_FD;
6240 fpl->count = nr_files;
6241 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006242 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06006243 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
6244 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07006245
Jens Axboe08a45172019-10-03 08:11:03 -06006246 for (i = 0; i < nr_files; i++)
6247 fput(fpl->fp[i]);
6248 } else {
6249 kfree_skb(skb);
6250 kfree(fpl);
6251 }
Jens Axboe6b063142019-01-10 22:13:58 -07006252
6253 return 0;
6254}
6255
6256/*
6257 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
6258 * causes regular reference counting to break down. We rely on the UNIX
6259 * garbage collection to take care of this problem for us.
6260 */
6261static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6262{
6263 unsigned left, total;
6264 int ret = 0;
6265
6266 total = 0;
6267 left = ctx->nr_user_files;
6268 while (left) {
6269 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07006270
6271 ret = __io_sqe_files_scm(ctx, this_files, total);
6272 if (ret)
6273 break;
6274 left -= this_files;
6275 total += this_files;
6276 }
6277
6278 if (!ret)
6279 return 0;
6280
6281 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006282 struct file *file = io_file_from_index(ctx, total);
6283
6284 if (file)
6285 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07006286 total++;
6287 }
6288
6289 return ret;
6290}
6291#else
6292static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6293{
6294 return 0;
6295}
6296#endif
6297
Jens Axboe65e19f52019-10-26 07:20:21 -06006298static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
6299 unsigned nr_files)
6300{
6301 int i;
6302
6303 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006304 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006305 unsigned this_files;
6306
6307 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
6308 table->files = kcalloc(this_files, sizeof(struct file *),
6309 GFP_KERNEL);
6310 if (!table->files)
6311 break;
6312 nr_files -= this_files;
6313 }
6314
6315 if (i == nr_tables)
6316 return 0;
6317
6318 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006319 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006320 kfree(table->files);
6321 }
6322 return 1;
6323}
6324
Jens Axboe05f3fb32019-12-09 11:22:50 -07006325static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06006326{
6327#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06006328 struct sock *sock = ctx->ring_sock->sk;
6329 struct sk_buff_head list, *head = &sock->sk_receive_queue;
6330 struct sk_buff *skb;
6331 int i;
6332
6333 __skb_queue_head_init(&list);
6334
6335 /*
6336 * Find the skb that holds this file in its SCM_RIGHTS. When found,
6337 * remove this entry and rearrange the file array.
6338 */
6339 skb = skb_dequeue(head);
6340 while (skb) {
6341 struct scm_fp_list *fp;
6342
6343 fp = UNIXCB(skb).fp;
6344 for (i = 0; i < fp->count; i++) {
6345 int left;
6346
6347 if (fp->fp[i] != file)
6348 continue;
6349
6350 unix_notinflight(fp->user, fp->fp[i]);
6351 left = fp->count - 1 - i;
6352 if (left) {
6353 memmove(&fp->fp[i], &fp->fp[i + 1],
6354 left * sizeof(struct file *));
6355 }
6356 fp->count--;
6357 if (!fp->count) {
6358 kfree_skb(skb);
6359 skb = NULL;
6360 } else {
6361 __skb_queue_tail(&list, skb);
6362 }
6363 fput(file);
6364 file = NULL;
6365 break;
6366 }
6367
6368 if (!file)
6369 break;
6370
6371 __skb_queue_tail(&list, skb);
6372
6373 skb = skb_dequeue(head);
6374 }
6375
6376 if (skb_peek(&list)) {
6377 spin_lock_irq(&head->lock);
6378 while ((skb = __skb_dequeue(&list)) != NULL)
6379 __skb_queue_tail(head, skb);
6380 spin_unlock_irq(&head->lock);
6381 }
6382#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07006383 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06006384#endif
6385}
6386
Jens Axboe05f3fb32019-12-09 11:22:50 -07006387struct io_file_put {
6388 struct llist_node llist;
6389 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006390};
6391
Jens Axboe2faf8522020-02-04 19:54:55 -07006392static void io_ring_file_ref_flush(struct fixed_file_data *data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006393{
6394 struct io_file_put *pfile, *tmp;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006395 struct llist_node *node;
6396
Jens Axboe05f3fb32019-12-09 11:22:50 -07006397 while ((node = llist_del_all(&data->put_llist)) != NULL) {
6398 llist_for_each_entry_safe(pfile, tmp, node, llist) {
6399 io_ring_file_put(data->ctx, pfile->file);
Hillf Dantona5318d32020-03-23 17:47:15 +08006400 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006401 }
6402 }
Jens Axboe2faf8522020-02-04 19:54:55 -07006403}
Jens Axboe05f3fb32019-12-09 11:22:50 -07006404
Jens Axboe2faf8522020-02-04 19:54:55 -07006405static void io_ring_file_ref_switch(struct work_struct *work)
6406{
6407 struct fixed_file_data *data;
6408
6409 data = container_of(work, struct fixed_file_data, ref_work);
6410 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006411 percpu_ref_switch_to_percpu(&data->refs);
6412}
6413
6414static void io_file_data_ref_zero(struct percpu_ref *ref)
6415{
6416 struct fixed_file_data *data;
6417
6418 data = container_of(ref, struct fixed_file_data, refs);
6419
Jens Axboe2faf8522020-02-04 19:54:55 -07006420 /*
6421 * We can't safely switch from inside this context, punt to wq. If
6422 * the table ref is going away, the table is being unregistered.
6423 * Don't queue up the async work for that case, the caller will
6424 * handle it.
6425 */
6426 if (!percpu_ref_is_dying(&data->refs))
6427 queue_work(system_wq, &data->ref_work);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006428}
6429
6430static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
6431 unsigned nr_args)
6432{
6433 __s32 __user *fds = (__s32 __user *) arg;
6434 unsigned nr_tables;
6435 struct file *file;
6436 int fd, ret = 0;
6437 unsigned i;
6438
6439 if (ctx->file_data)
6440 return -EBUSY;
6441 if (!nr_args)
6442 return -EINVAL;
6443 if (nr_args > IORING_MAX_FIXED_FILES)
6444 return -EMFILE;
6445
6446 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
6447 if (!ctx->file_data)
6448 return -ENOMEM;
6449 ctx->file_data->ctx = ctx;
6450 init_completion(&ctx->file_data->done);
6451
6452 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
6453 ctx->file_data->table = kcalloc(nr_tables,
6454 sizeof(struct fixed_file_table),
6455 GFP_KERNEL);
6456 if (!ctx->file_data->table) {
6457 kfree(ctx->file_data);
6458 ctx->file_data = NULL;
6459 return -ENOMEM;
6460 }
6461
6462 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
6463 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
6464 kfree(ctx->file_data->table);
6465 kfree(ctx->file_data);
6466 ctx->file_data = NULL;
6467 return -ENOMEM;
6468 }
6469 ctx->file_data->put_llist.first = NULL;
6470 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
6471
6472 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
6473 percpu_ref_exit(&ctx->file_data->refs);
6474 kfree(ctx->file_data->table);
6475 kfree(ctx->file_data);
6476 ctx->file_data = NULL;
6477 return -ENOMEM;
6478 }
6479
6480 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
6481 struct fixed_file_table *table;
6482 unsigned index;
6483
6484 ret = -EFAULT;
6485 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
6486 break;
6487 /* allow sparse sets */
6488 if (fd == -1) {
6489 ret = 0;
6490 continue;
6491 }
6492
6493 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6494 index = i & IORING_FILE_TABLE_MASK;
6495 file = fget(fd);
6496
6497 ret = -EBADF;
6498 if (!file)
6499 break;
6500
6501 /*
6502 * Don't allow io_uring instances to be registered. If UNIX
6503 * isn't enabled, then this causes a reference cycle and this
6504 * instance can never get freed. If UNIX is enabled we'll
6505 * handle it just fine, but there's still no point in allowing
6506 * a ring fd as it doesn't support regular read/write anyway.
6507 */
6508 if (file->f_op == &io_uring_fops) {
6509 fput(file);
6510 break;
6511 }
6512 ret = 0;
6513 table->files[index] = file;
6514 }
6515
6516 if (ret) {
6517 for (i = 0; i < ctx->nr_user_files; i++) {
6518 file = io_file_from_index(ctx, i);
6519 if (file)
6520 fput(file);
6521 }
6522 for (i = 0; i < nr_tables; i++)
6523 kfree(ctx->file_data->table[i].files);
6524
6525 kfree(ctx->file_data->table);
6526 kfree(ctx->file_data);
6527 ctx->file_data = NULL;
6528 ctx->nr_user_files = 0;
6529 return ret;
6530 }
6531
6532 ret = io_sqe_files_scm(ctx);
6533 if (ret)
6534 io_sqe_files_unregister(ctx);
6535
6536 return ret;
6537}
6538
Jens Axboec3a31e62019-10-03 13:59:56 -06006539static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
6540 int index)
6541{
6542#if defined(CONFIG_UNIX)
6543 struct sock *sock = ctx->ring_sock->sk;
6544 struct sk_buff_head *head = &sock->sk_receive_queue;
6545 struct sk_buff *skb;
6546
6547 /*
6548 * See if we can merge this file into an existing skb SCM_RIGHTS
6549 * file set. If there's no room, fall back to allocating a new skb
6550 * and filling it in.
6551 */
6552 spin_lock_irq(&head->lock);
6553 skb = skb_peek(head);
6554 if (skb) {
6555 struct scm_fp_list *fpl = UNIXCB(skb).fp;
6556
6557 if (fpl->count < SCM_MAX_FD) {
6558 __skb_unlink(skb, head);
6559 spin_unlock_irq(&head->lock);
6560 fpl->fp[fpl->count] = get_file(file);
6561 unix_inflight(fpl->user, fpl->fp[fpl->count]);
6562 fpl->count++;
6563 spin_lock_irq(&head->lock);
6564 __skb_queue_head(head, skb);
6565 } else {
6566 skb = NULL;
6567 }
6568 }
6569 spin_unlock_irq(&head->lock);
6570
6571 if (skb) {
6572 fput(file);
6573 return 0;
6574 }
6575
6576 return __io_sqe_files_scm(ctx, 1, index);
6577#else
6578 return 0;
6579#endif
6580}
6581
Jens Axboe05f3fb32019-12-09 11:22:50 -07006582static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06006583{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006584 struct fixed_file_data *data;
6585
Jens Axboedd3db2a2020-02-26 10:23:43 -07006586 /*
6587 * Juggle reference to ensure we hit zero, if needed, so we can
6588 * switch back to percpu mode
6589 */
Jens Axboe05f3fb32019-12-09 11:22:50 -07006590 data = container_of(ref, struct fixed_file_data, refs);
Jens Axboedd3db2a2020-02-26 10:23:43 -07006591 percpu_ref_put(&data->refs);
6592 percpu_ref_get(&data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006593}
6594
Hillf Dantona5318d32020-03-23 17:47:15 +08006595static int io_queue_file_removal(struct fixed_file_data *data,
Jens Axboe05f3fb32019-12-09 11:22:50 -07006596 struct file *file)
6597{
Hillf Dantona5318d32020-03-23 17:47:15 +08006598 struct io_file_put *pfile;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006599
Jens Axboe05f3fb32019-12-09 11:22:50 -07006600 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08006601 if (!pfile)
6602 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006603
6604 pfile->file = file;
6605 llist_add(&pfile->llist, &data->put_llist);
Hillf Dantona5318d32020-03-23 17:47:15 +08006606 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006607}
6608
6609static int __io_sqe_files_update(struct io_ring_ctx *ctx,
6610 struct io_uring_files_update *up,
6611 unsigned nr_args)
6612{
6613 struct fixed_file_data *data = ctx->file_data;
6614 bool ref_switch = false;
6615 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006616 __s32 __user *fds;
6617 int fd, i, err;
6618 __u32 done;
6619
Jens Axboe05f3fb32019-12-09 11:22:50 -07006620 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06006621 return -EOVERFLOW;
6622 if (done > ctx->nr_user_files)
6623 return -EINVAL;
6624
6625 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006626 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06006627 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006628 struct fixed_file_table *table;
6629 unsigned index;
6630
Jens Axboec3a31e62019-10-03 13:59:56 -06006631 err = 0;
6632 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
6633 err = -EFAULT;
6634 break;
6635 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006636 i = array_index_nospec(up->offset, ctx->nr_user_files);
6637 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06006638 index = i & IORING_FILE_TABLE_MASK;
6639 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006640 file = io_file_from_index(ctx, index);
Hillf Dantona5318d32020-03-23 17:47:15 +08006641 err = io_queue_file_removal(data, file);
6642 if (err)
6643 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06006644 table->files[index] = NULL;
Hillf Dantona5318d32020-03-23 17:47:15 +08006645 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06006646 }
6647 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06006648 file = fget(fd);
6649 if (!file) {
6650 err = -EBADF;
6651 break;
6652 }
6653 /*
6654 * Don't allow io_uring instances to be registered. If
6655 * UNIX isn't enabled, then this causes a reference
6656 * cycle and this instance can never get freed. If UNIX
6657 * is enabled we'll handle it just fine, but there's
6658 * still no point in allowing a ring fd as it doesn't
6659 * support regular read/write anyway.
6660 */
6661 if (file->f_op == &io_uring_fops) {
6662 fput(file);
6663 err = -EBADF;
6664 break;
6665 }
Jens Axboe65e19f52019-10-26 07:20:21 -06006666 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006667 err = io_sqe_file_register(ctx, file, i);
6668 if (err)
6669 break;
6670 }
6671 nr_args--;
6672 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006673 up->offset++;
6674 }
6675
Jens Axboedd3db2a2020-02-26 10:23:43 -07006676 if (ref_switch)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006677 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06006678
6679 return done ? done : err;
6680}
Jens Axboe05f3fb32019-12-09 11:22:50 -07006681static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
6682 unsigned nr_args)
6683{
6684 struct io_uring_files_update up;
6685
6686 if (!ctx->file_data)
6687 return -ENXIO;
6688 if (!nr_args)
6689 return -EINVAL;
6690 if (copy_from_user(&up, arg, sizeof(up)))
6691 return -EFAULT;
6692 if (up.resv)
6693 return -EINVAL;
6694
6695 return __io_sqe_files_update(ctx, &up, nr_args);
6696}
Jens Axboec3a31e62019-10-03 13:59:56 -06006697
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006698static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07006699{
6700 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6701
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006702 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07006703 io_put_req(req);
6704}
6705
Pavel Begunkov24369c22020-01-28 03:15:48 +03006706static int io_init_wq_offload(struct io_ring_ctx *ctx,
6707 struct io_uring_params *p)
6708{
6709 struct io_wq_data data;
6710 struct fd f;
6711 struct io_ring_ctx *ctx_attach;
6712 unsigned int concurrency;
6713 int ret = 0;
6714
6715 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006716 data.free_work = io_free_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03006717
6718 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
6719 /* Do QD, or 4 * CPUS, whatever is smallest */
6720 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
6721
6722 ctx->io_wq = io_wq_create(concurrency, &data);
6723 if (IS_ERR(ctx->io_wq)) {
6724 ret = PTR_ERR(ctx->io_wq);
6725 ctx->io_wq = NULL;
6726 }
6727 return ret;
6728 }
6729
6730 f = fdget(p->wq_fd);
6731 if (!f.file)
6732 return -EBADF;
6733
6734 if (f.file->f_op != &io_uring_fops) {
6735 ret = -EINVAL;
6736 goto out_fput;
6737 }
6738
6739 ctx_attach = f.file->private_data;
6740 /* @io_wq is protected by holding the fd */
6741 if (!io_wq_get(ctx_attach->io_wq, &data)) {
6742 ret = -EINVAL;
6743 goto out_fput;
6744 }
6745
6746 ctx->io_wq = ctx_attach->io_wq;
6747out_fput:
6748 fdput(f);
6749 return ret;
6750}
6751
Jens Axboe6c271ce2019-01-10 11:22:30 -07006752static int io_sq_offload_start(struct io_ring_ctx *ctx,
6753 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006754{
6755 int ret;
6756
Jens Axboe6c271ce2019-01-10 11:22:30 -07006757 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006758 mmgrab(current->mm);
6759 ctx->sqo_mm = current->mm;
6760
Jens Axboe6c271ce2019-01-10 11:22:30 -07006761 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06006762 ret = -EPERM;
6763 if (!capable(CAP_SYS_ADMIN))
6764 goto err;
6765
Jens Axboe917257d2019-04-13 09:28:55 -06006766 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
6767 if (!ctx->sq_thread_idle)
6768 ctx->sq_thread_idle = HZ;
6769
Jens Axboe6c271ce2019-01-10 11:22:30 -07006770 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06006771 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006772
Jens Axboe917257d2019-04-13 09:28:55 -06006773 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06006774 if (cpu >= nr_cpu_ids)
6775 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08006776 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06006777 goto err;
6778
Jens Axboe6c271ce2019-01-10 11:22:30 -07006779 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
6780 ctx, cpu,
6781 "io_uring-sq");
6782 } else {
6783 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
6784 "io_uring-sq");
6785 }
6786 if (IS_ERR(ctx->sqo_thread)) {
6787 ret = PTR_ERR(ctx->sqo_thread);
6788 ctx->sqo_thread = NULL;
6789 goto err;
6790 }
6791 wake_up_process(ctx->sqo_thread);
6792 } else if (p->flags & IORING_SETUP_SQ_AFF) {
6793 /* Can't have SQ_AFF without SQPOLL */
6794 ret = -EINVAL;
6795 goto err;
6796 }
6797
Pavel Begunkov24369c22020-01-28 03:15:48 +03006798 ret = io_init_wq_offload(ctx, p);
6799 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006800 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006801
6802 return 0;
6803err:
Jens Axboe54a91f32019-09-10 09:15:04 -06006804 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006805 mmdrop(ctx->sqo_mm);
6806 ctx->sqo_mm = NULL;
6807 return ret;
6808}
6809
6810static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
6811{
6812 atomic_long_sub(nr_pages, &user->locked_vm);
6813}
6814
6815static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
6816{
6817 unsigned long page_limit, cur_pages, new_pages;
6818
6819 /* Don't allow more pages than we can safely lock */
6820 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
6821
6822 do {
6823 cur_pages = atomic_long_read(&user->locked_vm);
6824 new_pages = cur_pages + nr_pages;
6825 if (new_pages > page_limit)
6826 return -ENOMEM;
6827 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
6828 new_pages) != cur_pages);
6829
6830 return 0;
6831}
6832
6833static void io_mem_free(void *ptr)
6834{
Mark Rutland52e04ef2019-04-30 17:30:21 +01006835 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006836
Mark Rutland52e04ef2019-04-30 17:30:21 +01006837 if (!ptr)
6838 return;
6839
6840 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006841 if (put_page_testzero(page))
6842 free_compound_page(page);
6843}
6844
6845static void *io_mem_alloc(size_t size)
6846{
6847 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
6848 __GFP_NORETRY;
6849
6850 return (void *) __get_free_pages(gfp_flags, get_order(size));
6851}
6852
Hristo Venev75b28af2019-08-26 17:23:46 +00006853static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
6854 size_t *sq_offset)
6855{
6856 struct io_rings *rings;
6857 size_t off, sq_array_size;
6858
6859 off = struct_size(rings, cqes, cq_entries);
6860 if (off == SIZE_MAX)
6861 return SIZE_MAX;
6862
6863#ifdef CONFIG_SMP
6864 off = ALIGN(off, SMP_CACHE_BYTES);
6865 if (off == 0)
6866 return SIZE_MAX;
6867#endif
6868
6869 sq_array_size = array_size(sizeof(u32), sq_entries);
6870 if (sq_array_size == SIZE_MAX)
6871 return SIZE_MAX;
6872
6873 if (check_add_overflow(off, sq_array_size, &off))
6874 return SIZE_MAX;
6875
6876 if (sq_offset)
6877 *sq_offset = off;
6878
6879 return off;
6880}
6881
Jens Axboe2b188cc2019-01-07 10:46:33 -07006882static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
6883{
Hristo Venev75b28af2019-08-26 17:23:46 +00006884 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006885
Hristo Venev75b28af2019-08-26 17:23:46 +00006886 pages = (size_t)1 << get_order(
6887 rings_size(sq_entries, cq_entries, NULL));
6888 pages += (size_t)1 << get_order(
6889 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07006890
Hristo Venev75b28af2019-08-26 17:23:46 +00006891 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006892}
6893
Jens Axboeedafcce2019-01-09 09:16:05 -07006894static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
6895{
6896 int i, j;
6897
6898 if (!ctx->user_bufs)
6899 return -ENXIO;
6900
6901 for (i = 0; i < ctx->nr_user_bufs; i++) {
6902 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6903
6904 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006905 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07006906
6907 if (ctx->account_mem)
6908 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006909 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006910 imu->nr_bvecs = 0;
6911 }
6912
6913 kfree(ctx->user_bufs);
6914 ctx->user_bufs = NULL;
6915 ctx->nr_user_bufs = 0;
6916 return 0;
6917}
6918
6919static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
6920 void __user *arg, unsigned index)
6921{
6922 struct iovec __user *src;
6923
6924#ifdef CONFIG_COMPAT
6925 if (ctx->compat) {
6926 struct compat_iovec __user *ciovs;
6927 struct compat_iovec ciov;
6928
6929 ciovs = (struct compat_iovec __user *) arg;
6930 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
6931 return -EFAULT;
6932
Jens Axboed55e5f52019-12-11 16:12:15 -07006933 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07006934 dst->iov_len = ciov.iov_len;
6935 return 0;
6936 }
6937#endif
6938 src = (struct iovec __user *) arg;
6939 if (copy_from_user(dst, &src[index], sizeof(*dst)))
6940 return -EFAULT;
6941 return 0;
6942}
6943
6944static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
6945 unsigned nr_args)
6946{
6947 struct vm_area_struct **vmas = NULL;
6948 struct page **pages = NULL;
6949 int i, j, got_pages = 0;
6950 int ret = -EINVAL;
6951
6952 if (ctx->user_bufs)
6953 return -EBUSY;
6954 if (!nr_args || nr_args > UIO_MAXIOV)
6955 return -EINVAL;
6956
6957 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
6958 GFP_KERNEL);
6959 if (!ctx->user_bufs)
6960 return -ENOMEM;
6961
6962 for (i = 0; i < nr_args; i++) {
6963 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6964 unsigned long off, start, end, ubuf;
6965 int pret, nr_pages;
6966 struct iovec iov;
6967 size_t size;
6968
6969 ret = io_copy_iov(ctx, &iov, arg, i);
6970 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03006971 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07006972
6973 /*
6974 * Don't impose further limits on the size and buffer
6975 * constraints here, we'll -EINVAL later when IO is
6976 * submitted if they are wrong.
6977 */
6978 ret = -EFAULT;
6979 if (!iov.iov_base || !iov.iov_len)
6980 goto err;
6981
6982 /* arbitrary limit, but we need something */
6983 if (iov.iov_len > SZ_1G)
6984 goto err;
6985
6986 ubuf = (unsigned long) iov.iov_base;
6987 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
6988 start = ubuf >> PAGE_SHIFT;
6989 nr_pages = end - start;
6990
6991 if (ctx->account_mem) {
6992 ret = io_account_mem(ctx->user, nr_pages);
6993 if (ret)
6994 goto err;
6995 }
6996
6997 ret = 0;
6998 if (!pages || nr_pages > got_pages) {
6999 kfree(vmas);
7000 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007001 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07007002 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007003 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07007004 sizeof(struct vm_area_struct *),
7005 GFP_KERNEL);
7006 if (!pages || !vmas) {
7007 ret = -ENOMEM;
7008 if (ctx->account_mem)
7009 io_unaccount_mem(ctx->user, nr_pages);
7010 goto err;
7011 }
7012 got_pages = nr_pages;
7013 }
7014
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007015 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07007016 GFP_KERNEL);
7017 ret = -ENOMEM;
7018 if (!imu->bvec) {
7019 if (ctx->account_mem)
7020 io_unaccount_mem(ctx->user, nr_pages);
7021 goto err;
7022 }
7023
7024 ret = 0;
7025 down_read(&current->mm->mmap_sem);
John Hubbard2113b052020-01-30 22:13:13 -08007026 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07007027 FOLL_WRITE | FOLL_LONGTERM,
7028 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007029 if (pret == nr_pages) {
7030 /* don't support file backed memory */
7031 for (j = 0; j < nr_pages; j++) {
7032 struct vm_area_struct *vma = vmas[j];
7033
7034 if (vma->vm_file &&
7035 !is_file_hugepages(vma->vm_file)) {
7036 ret = -EOPNOTSUPP;
7037 break;
7038 }
7039 }
7040 } else {
7041 ret = pret < 0 ? pret : -EFAULT;
7042 }
7043 up_read(&current->mm->mmap_sem);
7044 if (ret) {
7045 /*
7046 * if we did partial map, or found file backed vmas,
7047 * release any pages we did get
7048 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07007049 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007050 unpin_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07007051 if (ctx->account_mem)
7052 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007053 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007054 goto err;
7055 }
7056
7057 off = ubuf & ~PAGE_MASK;
7058 size = iov.iov_len;
7059 for (j = 0; j < nr_pages; j++) {
7060 size_t vec_len;
7061
7062 vec_len = min_t(size_t, size, PAGE_SIZE - off);
7063 imu->bvec[j].bv_page = pages[j];
7064 imu->bvec[j].bv_len = vec_len;
7065 imu->bvec[j].bv_offset = off;
7066 off = 0;
7067 size -= vec_len;
7068 }
7069 /* store original address for later verification */
7070 imu->ubuf = ubuf;
7071 imu->len = iov.iov_len;
7072 imu->nr_bvecs = nr_pages;
7073
7074 ctx->nr_user_bufs++;
7075 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007076 kvfree(pages);
7077 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007078 return 0;
7079err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007080 kvfree(pages);
7081 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007082 io_sqe_buffer_unregister(ctx);
7083 return ret;
7084}
7085
Jens Axboe9b402842019-04-11 11:45:41 -06007086static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
7087{
7088 __s32 __user *fds = arg;
7089 int fd;
7090
7091 if (ctx->cq_ev_fd)
7092 return -EBUSY;
7093
7094 if (copy_from_user(&fd, fds, sizeof(*fds)))
7095 return -EFAULT;
7096
7097 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
7098 if (IS_ERR(ctx->cq_ev_fd)) {
7099 int ret = PTR_ERR(ctx->cq_ev_fd);
7100 ctx->cq_ev_fd = NULL;
7101 return ret;
7102 }
7103
7104 return 0;
7105}
7106
7107static int io_eventfd_unregister(struct io_ring_ctx *ctx)
7108{
7109 if (ctx->cq_ev_fd) {
7110 eventfd_ctx_put(ctx->cq_ev_fd);
7111 ctx->cq_ev_fd = NULL;
7112 return 0;
7113 }
7114
7115 return -ENXIO;
7116}
7117
Jens Axboe5a2e7452020-02-23 16:23:11 -07007118static int __io_destroy_buffers(int id, void *p, void *data)
7119{
7120 struct io_ring_ctx *ctx = data;
7121 struct io_buffer *buf = p;
7122
Jens Axboe067524e2020-03-02 16:32:28 -07007123 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007124 return 0;
7125}
7126
7127static void io_destroy_buffers(struct io_ring_ctx *ctx)
7128{
7129 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
7130 idr_destroy(&ctx->io_buffer_idr);
7131}
7132
Jens Axboe2b188cc2019-01-07 10:46:33 -07007133static void io_ring_ctx_free(struct io_ring_ctx *ctx)
7134{
Jens Axboe6b063142019-01-10 22:13:58 -07007135 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007136 if (ctx->sqo_mm)
7137 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07007138
7139 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07007140 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07007141 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06007142 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007143 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07007144 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07007145
Jens Axboe2b188cc2019-01-07 10:46:33 -07007146#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07007147 if (ctx->ring_sock) {
7148 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007149 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07007150 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007151#endif
7152
Hristo Venev75b28af2019-08-26 17:23:46 +00007153 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007154 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007155
7156 percpu_ref_exit(&ctx->refs);
7157 if (ctx->account_mem)
7158 io_unaccount_mem(ctx->user,
7159 ring_pages(ctx->sq_entries, ctx->cq_entries));
7160 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07007161 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07007162 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07007163 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07007164 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007165 kfree(ctx);
7166}
7167
7168static __poll_t io_uring_poll(struct file *file, poll_table *wait)
7169{
7170 struct io_ring_ctx *ctx = file->private_data;
7171 __poll_t mask = 0;
7172
7173 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02007174 /*
7175 * synchronizes with barrier from wq_has_sleeper call in
7176 * io_commit_cqring
7177 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007178 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00007179 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
7180 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007181 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01007182 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007183 mask |= EPOLLIN | EPOLLRDNORM;
7184
7185 return mask;
7186}
7187
7188static int io_uring_fasync(int fd, struct file *file, int on)
7189{
7190 struct io_ring_ctx *ctx = file->private_data;
7191
7192 return fasync_helper(fd, file, on, &ctx->cq_fasync);
7193}
7194
Jens Axboe071698e2020-01-28 10:04:42 -07007195static int io_remove_personalities(int id, void *p, void *data)
7196{
7197 struct io_ring_ctx *ctx = data;
7198 const struct cred *cred;
7199
7200 cred = idr_remove(&ctx->personality_idr, id);
7201 if (cred)
7202 put_cred(cred);
7203 return 0;
7204}
7205
Jens Axboe2b188cc2019-01-07 10:46:33 -07007206static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
7207{
7208 mutex_lock(&ctx->uring_lock);
7209 percpu_ref_kill(&ctx->refs);
7210 mutex_unlock(&ctx->uring_lock);
7211
Jens Axboedf069d82020-02-04 16:48:34 -07007212 /*
7213 * Wait for sq thread to idle, if we have one. It won't spin on new
7214 * work after we've killed the ctx ref above. This is important to do
7215 * before we cancel existing commands, as the thread could otherwise
7216 * be queueing new work post that. If that's work we need to cancel,
7217 * it could cause shutdown to hang.
7218 */
7219 while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait))
7220 cpu_relax();
7221
Jens Axboe5262f562019-09-17 12:26:57 -06007222 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07007223 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06007224
7225 if (ctx->io_wq)
7226 io_wq_cancel_all(ctx->io_wq);
7227
Jens Axboedef596e2019-01-09 08:59:42 -07007228 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07007229 /* if we failed setting up the ctx, we might not have any rings */
7230 if (ctx->rings)
7231 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07007232 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe206aefd2019-11-07 18:27:42 -07007233 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007234 io_ring_ctx_free(ctx);
7235}
7236
7237static int io_uring_release(struct inode *inode, struct file *file)
7238{
7239 struct io_ring_ctx *ctx = file->private_data;
7240
7241 file->private_data = NULL;
7242 io_ring_ctx_wait_and_kill(ctx);
7243 return 0;
7244}
7245
Jens Axboefcb323c2019-10-24 12:39:47 -06007246static void io_uring_cancel_files(struct io_ring_ctx *ctx,
7247 struct files_struct *files)
7248{
7249 struct io_kiocb *req;
7250 DEFINE_WAIT(wait);
7251
7252 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07007253 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06007254
7255 spin_lock_irq(&ctx->inflight_lock);
7256 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07007257 if (req->work.files != files)
7258 continue;
7259 /* req is being completed, ignore */
7260 if (!refcount_inc_not_zero(&req->refs))
7261 continue;
7262 cancel_req = req;
7263 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06007264 }
Jens Axboe768134d2019-11-10 20:30:53 -07007265 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007266 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07007267 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06007268 spin_unlock_irq(&ctx->inflight_lock);
7269
Jens Axboe768134d2019-11-10 20:30:53 -07007270 /* We need to keep going until we don't find a matching req */
7271 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007272 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08007273
Jens Axboe2ca10252020-02-13 17:17:35 -07007274 if (cancel_req->flags & REQ_F_OVERFLOW) {
7275 spin_lock_irq(&ctx->completion_lock);
7276 list_del(&cancel_req->list);
7277 cancel_req->flags &= ~REQ_F_OVERFLOW;
7278 if (list_empty(&ctx->cq_overflow_list)) {
7279 clear_bit(0, &ctx->sq_check_overflow);
7280 clear_bit(0, &ctx->cq_check_overflow);
7281 }
7282 spin_unlock_irq(&ctx->completion_lock);
7283
7284 WRITE_ONCE(ctx->rings->cq_overflow,
7285 atomic_inc_return(&ctx->cached_cq_overflow));
7286
7287 /*
7288 * Put inflight ref and overflow ref. If that's
7289 * all we had, then we're done with this request.
7290 */
7291 if (refcount_sub_and_test(2, &cancel_req->refs)) {
7292 io_put_req(cancel_req);
7293 continue;
7294 }
7295 }
7296
Bob Liu2f6d9b92019-11-13 18:06:24 +08007297 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
7298 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06007299 schedule();
7300 }
Jens Axboe768134d2019-11-10 20:30:53 -07007301 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007302}
7303
7304static int io_uring_flush(struct file *file, void *data)
7305{
7306 struct io_ring_ctx *ctx = file->private_data;
7307
7308 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07007309
7310 /*
7311 * If the task is going away, cancel work it may have pending
7312 */
7313 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
7314 io_wq_cancel_pid(ctx->io_wq, task_pid_vnr(current));
7315
Jens Axboefcb323c2019-10-24 12:39:47 -06007316 return 0;
7317}
7318
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007319static void *io_uring_validate_mmap_request(struct file *file,
7320 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007321{
Jens Axboe2b188cc2019-01-07 10:46:33 -07007322 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007323 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007324 struct page *page;
7325 void *ptr;
7326
7327 switch (offset) {
7328 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00007329 case IORING_OFF_CQ_RING:
7330 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007331 break;
7332 case IORING_OFF_SQES:
7333 ptr = ctx->sq_sqes;
7334 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007335 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007336 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007337 }
7338
7339 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07007340 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007341 return ERR_PTR(-EINVAL);
7342
7343 return ptr;
7344}
7345
7346#ifdef CONFIG_MMU
7347
7348static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7349{
7350 size_t sz = vma->vm_end - vma->vm_start;
7351 unsigned long pfn;
7352 void *ptr;
7353
7354 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
7355 if (IS_ERR(ptr))
7356 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007357
7358 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
7359 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
7360}
7361
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007362#else /* !CONFIG_MMU */
7363
7364static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7365{
7366 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
7367}
7368
7369static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
7370{
7371 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
7372}
7373
7374static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
7375 unsigned long addr, unsigned long len,
7376 unsigned long pgoff, unsigned long flags)
7377{
7378 void *ptr;
7379
7380 ptr = io_uring_validate_mmap_request(file, pgoff, len);
7381 if (IS_ERR(ptr))
7382 return PTR_ERR(ptr);
7383
7384 return (unsigned long) ptr;
7385}
7386
7387#endif /* !CONFIG_MMU */
7388
Jens Axboe2b188cc2019-01-07 10:46:33 -07007389SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
7390 u32, min_complete, u32, flags, const sigset_t __user *, sig,
7391 size_t, sigsz)
7392{
7393 struct io_ring_ctx *ctx;
7394 long ret = -EBADF;
7395 int submitted = 0;
7396 struct fd f;
7397
Jens Axboeb41e9852020-02-17 09:52:41 -07007398 if (current->task_works)
7399 task_work_run();
7400
Jens Axboe6c271ce2019-01-10 11:22:30 -07007401 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007402 return -EINVAL;
7403
7404 f = fdget(fd);
7405 if (!f.file)
7406 return -EBADF;
7407
7408 ret = -EOPNOTSUPP;
7409 if (f.file->f_op != &io_uring_fops)
7410 goto out_fput;
7411
7412 ret = -ENXIO;
7413 ctx = f.file->private_data;
7414 if (!percpu_ref_tryget(&ctx->refs))
7415 goto out_fput;
7416
Jens Axboe6c271ce2019-01-10 11:22:30 -07007417 /*
7418 * For SQ polling, the thread will do all submissions and completions.
7419 * Just return the requested submit count, and wake the thread if
7420 * we were asked to.
7421 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007422 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007423 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07007424 if (!list_empty_careful(&ctx->cq_overflow_list))
7425 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007426 if (flags & IORING_ENTER_SQ_WAKEUP)
7427 wake_up(&ctx->sqo_wait);
7428 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007429 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03007430 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007431
7432 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03007433 /* already have mm, so io_submit_sqes() won't try to grab it */
7434 cur_mm = ctx->sqo_mm;
7435 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
7436 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007437 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007438
7439 if (submitted != to_submit)
7440 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007441 }
7442 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07007443 unsigned nr_events = 0;
7444
Jens Axboe2b188cc2019-01-07 10:46:33 -07007445 min_complete = min(min_complete, ctx->cq_entries);
7446
Xiaoguang Wang32b22442020-03-11 09:26:09 +08007447 /*
7448 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
7449 * space applications don't need to do io completion events
7450 * polling again, they can rely on io_sq_thread to do polling
7451 * work, which can reduce cpu usage and uring_lock contention.
7452 */
7453 if (ctx->flags & IORING_SETUP_IOPOLL &&
7454 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Jens Axboedef596e2019-01-09 08:59:42 -07007455 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07007456 } else {
7457 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
7458 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007459 }
7460
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007461out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03007462 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007463out_fput:
7464 fdput(f);
7465 return submitted ? submitted : ret;
7466}
7467
Tobias Klauserbebdb652020-02-26 18:38:32 +01007468#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07007469static int io_uring_show_cred(int id, void *p, void *data)
7470{
7471 const struct cred *cred = p;
7472 struct seq_file *m = data;
7473 struct user_namespace *uns = seq_user_ns(m);
7474 struct group_info *gi;
7475 kernel_cap_t cap;
7476 unsigned __capi;
7477 int g;
7478
7479 seq_printf(m, "%5d\n", id);
7480 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
7481 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
7482 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
7483 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
7484 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
7485 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
7486 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
7487 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
7488 seq_puts(m, "\n\tGroups:\t");
7489 gi = cred->group_info;
7490 for (g = 0; g < gi->ngroups; g++) {
7491 seq_put_decimal_ull(m, g ? " " : "",
7492 from_kgid_munged(uns, gi->gid[g]));
7493 }
7494 seq_puts(m, "\n\tCapEff:\t");
7495 cap = cred->cap_effective;
7496 CAP_FOR_EACH_U32(__capi)
7497 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
7498 seq_putc(m, '\n');
7499 return 0;
7500}
7501
7502static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
7503{
7504 int i;
7505
7506 mutex_lock(&ctx->uring_lock);
7507 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
7508 for (i = 0; i < ctx->nr_user_files; i++) {
7509 struct fixed_file_table *table;
7510 struct file *f;
7511
7512 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7513 f = table->files[i & IORING_FILE_TABLE_MASK];
7514 if (f)
7515 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
7516 else
7517 seq_printf(m, "%5u: <none>\n", i);
7518 }
7519 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
7520 for (i = 0; i < ctx->nr_user_bufs; i++) {
7521 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
7522
7523 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
7524 (unsigned int) buf->len);
7525 }
7526 if (!idr_is_empty(&ctx->personality_idr)) {
7527 seq_printf(m, "Personalities:\n");
7528 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
7529 }
Jens Axboed7718a92020-02-14 22:23:12 -07007530 seq_printf(m, "PollList:\n");
7531 spin_lock_irq(&ctx->completion_lock);
7532 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
7533 struct hlist_head *list = &ctx->cancel_hash[i];
7534 struct io_kiocb *req;
7535
7536 hlist_for_each_entry(req, list, hash_node)
7537 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
7538 req->task->task_works != NULL);
7539 }
7540 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07007541 mutex_unlock(&ctx->uring_lock);
7542}
7543
7544static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
7545{
7546 struct io_ring_ctx *ctx = f->private_data;
7547
7548 if (percpu_ref_tryget(&ctx->refs)) {
7549 __io_uring_show_fdinfo(ctx, m);
7550 percpu_ref_put(&ctx->refs);
7551 }
7552}
Tobias Klauserbebdb652020-02-26 18:38:32 +01007553#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07007554
Jens Axboe2b188cc2019-01-07 10:46:33 -07007555static const struct file_operations io_uring_fops = {
7556 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06007557 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07007558 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007559#ifndef CONFIG_MMU
7560 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
7561 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
7562#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07007563 .poll = io_uring_poll,
7564 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01007565#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07007566 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01007567#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07007568};
7569
7570static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
7571 struct io_uring_params *p)
7572{
Hristo Venev75b28af2019-08-26 17:23:46 +00007573 struct io_rings *rings;
7574 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007575
Hristo Venev75b28af2019-08-26 17:23:46 +00007576 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
7577 if (size == SIZE_MAX)
7578 return -EOVERFLOW;
7579
7580 rings = io_mem_alloc(size);
7581 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007582 return -ENOMEM;
7583
Hristo Venev75b28af2019-08-26 17:23:46 +00007584 ctx->rings = rings;
7585 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
7586 rings->sq_ring_mask = p->sq_entries - 1;
7587 rings->cq_ring_mask = p->cq_entries - 1;
7588 rings->sq_ring_entries = p->sq_entries;
7589 rings->cq_ring_entries = p->cq_entries;
7590 ctx->sq_mask = rings->sq_ring_mask;
7591 ctx->cq_mask = rings->cq_ring_mask;
7592 ctx->sq_entries = rings->sq_ring_entries;
7593 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007594
7595 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07007596 if (size == SIZE_MAX) {
7597 io_mem_free(ctx->rings);
7598 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007599 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07007600 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007601
7602 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07007603 if (!ctx->sq_sqes) {
7604 io_mem_free(ctx->rings);
7605 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007606 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07007607 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007608
Jens Axboe2b188cc2019-01-07 10:46:33 -07007609 return 0;
7610}
7611
7612/*
7613 * Allocate an anonymous fd, this is what constitutes the application
7614 * visible backing of an io_uring instance. The application mmaps this
7615 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
7616 * we have to tie this fd to a socket for file garbage collection purposes.
7617 */
7618static int io_uring_get_fd(struct io_ring_ctx *ctx)
7619{
7620 struct file *file;
7621 int ret;
7622
7623#if defined(CONFIG_UNIX)
7624 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
7625 &ctx->ring_sock);
7626 if (ret)
7627 return ret;
7628#endif
7629
7630 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
7631 if (ret < 0)
7632 goto err;
7633
7634 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
7635 O_RDWR | O_CLOEXEC);
7636 if (IS_ERR(file)) {
7637 put_unused_fd(ret);
7638 ret = PTR_ERR(file);
7639 goto err;
7640 }
7641
7642#if defined(CONFIG_UNIX)
7643 ctx->ring_sock->file = file;
7644#endif
7645 fd_install(ret, file);
7646 return ret;
7647err:
7648#if defined(CONFIG_UNIX)
7649 sock_release(ctx->ring_sock);
7650 ctx->ring_sock = NULL;
7651#endif
7652 return ret;
7653}
7654
7655static int io_uring_create(unsigned entries, struct io_uring_params *p)
7656{
7657 struct user_struct *user = NULL;
7658 struct io_ring_ctx *ctx;
7659 bool account_mem;
7660 int ret;
7661
Jens Axboe8110c1a2019-12-28 15:39:54 -07007662 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007663 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07007664 if (entries > IORING_MAX_ENTRIES) {
7665 if (!(p->flags & IORING_SETUP_CLAMP))
7666 return -EINVAL;
7667 entries = IORING_MAX_ENTRIES;
7668 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007669
7670 /*
7671 * Use twice as many entries for the CQ ring. It's possible for the
7672 * application to drive a higher depth than the size of the SQ ring,
7673 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06007674 * some flexibility in overcommitting a bit. If the application has
7675 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
7676 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07007677 */
7678 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06007679 if (p->flags & IORING_SETUP_CQSIZE) {
7680 /*
7681 * If IORING_SETUP_CQSIZE is set, we do the same roundup
7682 * to a power-of-two, if it isn't already. We do NOT impose
7683 * any cq vs sq ring sizing.
7684 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07007685 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06007686 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07007687 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
7688 if (!(p->flags & IORING_SETUP_CLAMP))
7689 return -EINVAL;
7690 p->cq_entries = IORING_MAX_CQ_ENTRIES;
7691 }
Jens Axboe33a107f2019-10-04 12:10:03 -06007692 p->cq_entries = roundup_pow_of_two(p->cq_entries);
7693 } else {
7694 p->cq_entries = 2 * p->sq_entries;
7695 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007696
7697 user = get_uid(current_user());
7698 account_mem = !capable(CAP_IPC_LOCK);
7699
7700 if (account_mem) {
7701 ret = io_account_mem(user,
7702 ring_pages(p->sq_entries, p->cq_entries));
7703 if (ret) {
7704 free_uid(user);
7705 return ret;
7706 }
7707 }
7708
7709 ctx = io_ring_ctx_alloc(p);
7710 if (!ctx) {
7711 if (account_mem)
7712 io_unaccount_mem(user, ring_pages(p->sq_entries,
7713 p->cq_entries));
7714 free_uid(user);
7715 return -ENOMEM;
7716 }
7717 ctx->compat = in_compat_syscall();
7718 ctx->account_mem = account_mem;
7719 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07007720 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07007721
7722 ret = io_allocate_scq_urings(ctx, p);
7723 if (ret)
7724 goto err;
7725
Jens Axboe6c271ce2019-01-10 11:22:30 -07007726 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007727 if (ret)
7728 goto err;
7729
Jens Axboe2b188cc2019-01-07 10:46:33 -07007730 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007731 p->sq_off.head = offsetof(struct io_rings, sq.head);
7732 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
7733 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
7734 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
7735 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
7736 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
7737 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007738
7739 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007740 p->cq_off.head = offsetof(struct io_rings, cq.head);
7741 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
7742 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
7743 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
7744 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
7745 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06007746
Jens Axboe044c1ab2019-10-28 09:15:33 -06007747 /*
7748 * Install ring fd as the very last thing, so we don't risk someone
7749 * having closed it before we finish setup
7750 */
7751 ret = io_uring_get_fd(ctx);
7752 if (ret < 0)
7753 goto err;
7754
Jens Axboeda8c9692019-12-02 18:51:26 -07007755 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboecccf0ee2020-01-27 16:34:48 -07007756 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jens Axboed7718a92020-02-14 22:23:12 -07007757 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007758 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007759 return ret;
7760err:
7761 io_ring_ctx_wait_and_kill(ctx);
7762 return ret;
7763}
7764
7765/*
7766 * Sets up an aio uring context, and returns the fd. Applications asks for a
7767 * ring size, we return the actual sq/cq ring sizes (among other things) in the
7768 * params structure passed in.
7769 */
7770static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
7771{
7772 struct io_uring_params p;
7773 long ret;
7774 int i;
7775
7776 if (copy_from_user(&p, params, sizeof(p)))
7777 return -EFAULT;
7778 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
7779 if (p.resv[i])
7780 return -EINVAL;
7781 }
7782
Jens Axboe6c271ce2019-01-10 11:22:30 -07007783 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07007784 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03007785 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007786 return -EINVAL;
7787
7788 ret = io_uring_create(entries, &p);
7789 if (ret < 0)
7790 return ret;
7791
7792 if (copy_to_user(params, &p, sizeof(p)))
7793 return -EFAULT;
7794
7795 return ret;
7796}
7797
7798SYSCALL_DEFINE2(io_uring_setup, u32, entries,
7799 struct io_uring_params __user *, params)
7800{
7801 return io_uring_setup(entries, params);
7802}
7803
Jens Axboe66f4af92020-01-16 15:36:52 -07007804static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
7805{
7806 struct io_uring_probe *p;
7807 size_t size;
7808 int i, ret;
7809
7810 size = struct_size(p, ops, nr_args);
7811 if (size == SIZE_MAX)
7812 return -EOVERFLOW;
7813 p = kzalloc(size, GFP_KERNEL);
7814 if (!p)
7815 return -ENOMEM;
7816
7817 ret = -EFAULT;
7818 if (copy_from_user(p, arg, size))
7819 goto out;
7820 ret = -EINVAL;
7821 if (memchr_inv(p, 0, size))
7822 goto out;
7823
7824 p->last_op = IORING_OP_LAST - 1;
7825 if (nr_args > IORING_OP_LAST)
7826 nr_args = IORING_OP_LAST;
7827
7828 for (i = 0; i < nr_args; i++) {
7829 p->ops[i].op = i;
7830 if (!io_op_defs[i].not_supported)
7831 p->ops[i].flags = IO_URING_OP_SUPPORTED;
7832 }
7833 p->ops_len = i;
7834
7835 ret = 0;
7836 if (copy_to_user(arg, p, size))
7837 ret = -EFAULT;
7838out:
7839 kfree(p);
7840 return ret;
7841}
7842
Jens Axboe071698e2020-01-28 10:04:42 -07007843static int io_register_personality(struct io_ring_ctx *ctx)
7844{
7845 const struct cred *creds = get_current_cred();
7846 int id;
7847
7848 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
7849 USHRT_MAX, GFP_KERNEL);
7850 if (id < 0)
7851 put_cred(creds);
7852 return id;
7853}
7854
7855static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
7856{
7857 const struct cred *old_creds;
7858
7859 old_creds = idr_remove(&ctx->personality_idr, id);
7860 if (old_creds) {
7861 put_cred(old_creds);
7862 return 0;
7863 }
7864
7865 return -EINVAL;
7866}
7867
7868static bool io_register_op_must_quiesce(int op)
7869{
7870 switch (op) {
7871 case IORING_UNREGISTER_FILES:
7872 case IORING_REGISTER_FILES_UPDATE:
7873 case IORING_REGISTER_PROBE:
7874 case IORING_REGISTER_PERSONALITY:
7875 case IORING_UNREGISTER_PERSONALITY:
7876 return false;
7877 default:
7878 return true;
7879 }
7880}
7881
Jens Axboeedafcce2019-01-09 09:16:05 -07007882static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
7883 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06007884 __releases(ctx->uring_lock)
7885 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07007886{
7887 int ret;
7888
Jens Axboe35fa71a2019-04-22 10:23:23 -06007889 /*
7890 * We're inside the ring mutex, if the ref is already dying, then
7891 * someone else killed the ctx or is already going through
7892 * io_uring_register().
7893 */
7894 if (percpu_ref_is_dying(&ctx->refs))
7895 return -ENXIO;
7896
Jens Axboe071698e2020-01-28 10:04:42 -07007897 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007898 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06007899
Jens Axboe05f3fb32019-12-09 11:22:50 -07007900 /*
7901 * Drop uring mutex before waiting for references to exit. If
7902 * another thread is currently inside io_uring_enter() it might
7903 * need to grab the uring_lock to make progress. If we hold it
7904 * here across the drain wait, then we can deadlock. It's safe
7905 * to drop the mutex here, since no new references will come in
7906 * after we've killed the percpu ref.
7907 */
7908 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007909 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007910 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007911 if (ret) {
7912 percpu_ref_resurrect(&ctx->refs);
7913 ret = -EINTR;
7914 goto out;
7915 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007916 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007917
7918 switch (opcode) {
7919 case IORING_REGISTER_BUFFERS:
7920 ret = io_sqe_buffer_register(ctx, arg, nr_args);
7921 break;
7922 case IORING_UNREGISTER_BUFFERS:
7923 ret = -EINVAL;
7924 if (arg || nr_args)
7925 break;
7926 ret = io_sqe_buffer_unregister(ctx);
7927 break;
Jens Axboe6b063142019-01-10 22:13:58 -07007928 case IORING_REGISTER_FILES:
7929 ret = io_sqe_files_register(ctx, arg, nr_args);
7930 break;
7931 case IORING_UNREGISTER_FILES:
7932 ret = -EINVAL;
7933 if (arg || nr_args)
7934 break;
7935 ret = io_sqe_files_unregister(ctx);
7936 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06007937 case IORING_REGISTER_FILES_UPDATE:
7938 ret = io_sqe_files_update(ctx, arg, nr_args);
7939 break;
Jens Axboe9b402842019-04-11 11:45:41 -06007940 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07007941 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06007942 ret = -EINVAL;
7943 if (nr_args != 1)
7944 break;
7945 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07007946 if (ret)
7947 break;
7948 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
7949 ctx->eventfd_async = 1;
7950 else
7951 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06007952 break;
7953 case IORING_UNREGISTER_EVENTFD:
7954 ret = -EINVAL;
7955 if (arg || nr_args)
7956 break;
7957 ret = io_eventfd_unregister(ctx);
7958 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07007959 case IORING_REGISTER_PROBE:
7960 ret = -EINVAL;
7961 if (!arg || nr_args > 256)
7962 break;
7963 ret = io_probe(ctx, arg, nr_args);
7964 break;
Jens Axboe071698e2020-01-28 10:04:42 -07007965 case IORING_REGISTER_PERSONALITY:
7966 ret = -EINVAL;
7967 if (arg || nr_args)
7968 break;
7969 ret = io_register_personality(ctx);
7970 break;
7971 case IORING_UNREGISTER_PERSONALITY:
7972 ret = -EINVAL;
7973 if (arg)
7974 break;
7975 ret = io_unregister_personality(ctx, nr_args);
7976 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07007977 default:
7978 ret = -EINVAL;
7979 break;
7980 }
7981
Jens Axboe071698e2020-01-28 10:04:42 -07007982 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007983 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07007984 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07007985out:
7986 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007987 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007988 return ret;
7989}
7990
7991SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
7992 void __user *, arg, unsigned int, nr_args)
7993{
7994 struct io_ring_ctx *ctx;
7995 long ret = -EBADF;
7996 struct fd f;
7997
7998 f = fdget(fd);
7999 if (!f.file)
8000 return -EBADF;
8001
8002 ret = -EOPNOTSUPP;
8003 if (f.file->f_op != &io_uring_fops)
8004 goto out_fput;
8005
8006 ctx = f.file->private_data;
8007
8008 mutex_lock(&ctx->uring_lock);
8009 ret = __io_uring_register(ctx, opcode, arg, nr_args);
8010 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02008011 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
8012 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07008013out_fput:
8014 fdput(f);
8015 return ret;
8016}
8017
Jens Axboe2b188cc2019-01-07 10:46:33 -07008018static int __init io_uring_init(void)
8019{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008020#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
8021 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
8022 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
8023} while (0)
8024
8025#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
8026 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
8027 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
8028 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
8029 BUILD_BUG_SQE_ELEM(1, __u8, flags);
8030 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
8031 BUILD_BUG_SQE_ELEM(4, __s32, fd);
8032 BUILD_BUG_SQE_ELEM(8, __u64, off);
8033 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
8034 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008035 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008036 BUILD_BUG_SQE_ELEM(24, __u32, len);
8037 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
8038 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
8039 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
8040 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
8041 BUILD_BUG_SQE_ELEM(28, __u16, poll_events);
8042 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
8043 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
8044 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
8045 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
8046 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
8047 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
8048 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
8049 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008050 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008051 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
8052 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
8053 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008054 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008055
Jens Axboed3656342019-12-18 09:50:26 -07008056 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07008057 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008058 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
8059 return 0;
8060};
8061__initcall(io_uring_init);