blob: f131105fa12a21ffce4f6245997f25d5b1a38cbe [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;
357};
358
359struct io_sync {
360 struct file *file;
361 loff_t len;
362 loff_t off;
363 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700364 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700365};
366
Jens Axboefbf23842019-12-17 18:45:56 -0700367struct io_cancel {
368 struct file *file;
369 u64 addr;
370};
371
Jens Axboeb29472e2019-12-17 18:50:29 -0700372struct io_timeout {
373 struct file *file;
374 u64 addr;
375 int flags;
Jens Axboe26a61672019-12-20 09:02:01 -0700376 unsigned count;
Jens Axboeb29472e2019-12-17 18:50:29 -0700377};
378
Jens Axboe9adbd452019-12-20 08:45:55 -0700379struct io_rw {
380 /* NOTE: kiocb has the file as the first member, so don't do it here */
381 struct kiocb kiocb;
382 u64 addr;
383 u64 len;
384};
385
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700386struct io_connect {
387 struct file *file;
388 struct sockaddr __user *addr;
389 int addr_len;
390};
391
Jens Axboee47293f2019-12-20 08:58:21 -0700392struct io_sr_msg {
393 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700394 union {
395 struct user_msghdr __user *msg;
396 void __user *buf;
397 };
Jens Axboee47293f2019-12-20 08:58:21 -0700398 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700399 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700400 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700401 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700402};
403
Jens Axboe15b71ab2019-12-11 11:20:36 -0700404struct io_open {
405 struct file *file;
406 int dfd;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700407 union {
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700408 unsigned mask;
409 };
Jens Axboe15b71ab2019-12-11 11:20:36 -0700410 struct filename *filename;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700411 struct statx __user *buffer;
Jens Axboec12cedf2020-01-08 17:41:21 -0700412 struct open_how how;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700413};
414
Jens Axboe05f3fb32019-12-09 11:22:50 -0700415struct io_files_update {
416 struct file *file;
417 u64 arg;
418 u32 nr_args;
419 u32 offset;
420};
421
Jens Axboe4840e412019-12-25 22:03:45 -0700422struct io_fadvise {
423 struct file *file;
424 u64 offset;
425 u32 len;
426 u32 advice;
427};
428
Jens Axboec1ca7572019-12-25 22:18:28 -0700429struct io_madvise {
430 struct file *file;
431 u64 addr;
432 u32 len;
433 u32 advice;
434};
435
Jens Axboe3e4827b2020-01-08 15:18:09 -0700436struct io_epoll {
437 struct file *file;
438 int epfd;
439 int op;
440 int fd;
441 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700442};
443
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300444struct io_splice {
445 struct file *file_out;
446 struct file *file_in;
447 loff_t off_out;
448 loff_t off_in;
449 u64 len;
450 unsigned int flags;
451};
452
Jens Axboeddf0322d2020-02-23 16:41:33 -0700453struct io_provide_buf {
454 struct file *file;
455 __u64 addr;
456 __s32 len;
457 __u32 bgid;
458 __u16 nbufs;
459 __u16 bid;
460};
461
Jens Axboef499a022019-12-02 16:28:46 -0700462struct io_async_connect {
463 struct sockaddr_storage address;
464};
465
Jens Axboe03b12302019-12-02 18:50:25 -0700466struct io_async_msghdr {
467 struct iovec fast_iov[UIO_FASTIOV];
468 struct iovec *iov;
469 struct sockaddr __user *uaddr;
470 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700471 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700472};
473
Jens Axboef67676d2019-12-02 11:03:47 -0700474struct io_async_rw {
475 struct iovec fast_iov[UIO_FASTIOV];
476 struct iovec *iov;
477 ssize_t nr_segs;
478 ssize_t size;
479};
480
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700481struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700482 union {
483 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700484 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700485 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700486 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700487 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700488};
489
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300490enum {
491 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
492 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
493 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
494 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
495 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700496 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300497
498 REQ_F_LINK_NEXT_BIT,
499 REQ_F_FAIL_LINK_BIT,
500 REQ_F_INFLIGHT_BIT,
501 REQ_F_CUR_POS_BIT,
502 REQ_F_NOWAIT_BIT,
503 REQ_F_IOPOLL_COMPLETED_BIT,
504 REQ_F_LINK_TIMEOUT_BIT,
505 REQ_F_TIMEOUT_BIT,
506 REQ_F_ISREG_BIT,
507 REQ_F_MUST_PUNT_BIT,
508 REQ_F_TIMEOUT_NOSEQ_BIT,
509 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300510 REQ_F_NEED_CLEANUP_BIT,
Jens Axboe2ca10252020-02-13 17:17:35 -0700511 REQ_F_OVERFLOW_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700512 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700513 REQ_F_BUFFER_SELECTED_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300514};
515
516enum {
517 /* ctx owns file */
518 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
519 /* drain existing IO first */
520 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
521 /* linked sqes */
522 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
523 /* doesn't sever on completion < 0 */
524 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
525 /* IOSQE_ASYNC */
526 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700527 /* IOSQE_BUFFER_SELECT */
528 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300529
530 /* already grabbed next link */
531 REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT),
532 /* fail rest of links */
533 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
534 /* on inflight list */
535 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
536 /* read/write uses file position */
537 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
538 /* must not punt to workers */
539 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
540 /* polled IO has completed */
541 REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT),
542 /* has linked timeout */
543 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
544 /* timeout request */
545 REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT),
546 /* regular file */
547 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
548 /* must be punted even for NONBLOCK */
549 REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT),
550 /* no timeout sequence */
551 REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT),
552 /* completion under lock */
553 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300554 /* needs cleanup */
555 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboe2ca10252020-02-13 17:17:35 -0700556 /* in overflow list */
557 REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700558 /* already went through poll handler */
559 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700560 /* buffer already selected */
561 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700562};
563
564struct async_poll {
565 struct io_poll_iocb poll;
566 struct io_wq_work work;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300567};
568
Jens Axboe09bb8392019-03-13 12:39:28 -0600569/*
570 * NOTE! Each of the iocb union members has the file pointer
571 * as the first entry in their struct definition. So you can
572 * access the file pointer through any of the sub-structs,
573 * or directly as just 'ki_filp' in this struct.
574 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700575struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700576 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600577 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700578 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700579 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700580 struct io_accept accept;
581 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700582 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700583 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700584 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700585 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700586 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700587 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700588 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700589 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700590 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700591 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300592 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700593 struct io_provide_buf pbuf;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700594 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700595
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700596 struct io_async_ctx *io;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300597 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700598 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700599
600 struct io_ring_ctx *ctx;
Jens Axboed7718a92020-02-14 22:23:12 -0700601 struct list_head list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700602 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700603 refcount_t refs;
Jens Axboed7718a92020-02-14 22:23:12 -0700604 struct task_struct *task;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700605 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600606 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600607 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700608
Jens Axboed7718a92020-02-14 22:23:12 -0700609 struct list_head link_list;
610
Jens Axboefcb323c2019-10-24 12:39:47 -0600611 struct list_head inflight_entry;
612
Jens Axboeb41e9852020-02-17 09:52:41 -0700613 union {
614 /*
615 * Only commands that never go async can use the below fields,
Jens Axboed7718a92020-02-14 22:23:12 -0700616 * obviously. Right now only IORING_OP_POLL_ADD uses them, and
617 * async armed poll handlers for regular commands. The latter
618 * restore the work, if needed.
Jens Axboeb41e9852020-02-17 09:52:41 -0700619 */
620 struct {
Jens Axboeb41e9852020-02-17 09:52:41 -0700621 struct callback_head task_work;
Jens Axboed7718a92020-02-14 22:23:12 -0700622 struct hlist_node hash_node;
623 struct async_poll *apoll;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700624 int cflags;
Jens Axboeb41e9852020-02-17 09:52:41 -0700625 };
626 struct io_wq_work work;
627 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700628};
629
630#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700631#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700632
Jens Axboe9a56a232019-01-09 09:06:50 -0700633struct io_submit_state {
634 struct blk_plug plug;
635
636 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700637 * io_kiocb alloc cache
638 */
639 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300640 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700641
642 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700643 * File reference cache
644 */
645 struct file *file;
646 unsigned int fd;
647 unsigned int has_refs;
648 unsigned int used_refs;
649 unsigned int ios_left;
650};
651
Jens Axboed3656342019-12-18 09:50:26 -0700652struct io_op_def {
653 /* needs req->io allocated for deferral/async */
654 unsigned async_ctx : 1;
655 /* needs current->mm setup, does mm access */
656 unsigned needs_mm : 1;
657 /* needs req->file assigned */
658 unsigned needs_file : 1;
659 /* needs req->file assigned IFF fd is >= 0 */
660 unsigned fd_non_neg : 1;
661 /* hash wq insertion if file is a regular file */
662 unsigned hash_reg_file : 1;
663 /* unbound wq insertion if file is a non-regular file */
664 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700665 /* opcode is not supported by this kernel */
666 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700667 /* needs file table */
668 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700669 /* needs ->fs */
670 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700671 /* set if opcode supports polled "wait" */
672 unsigned pollin : 1;
673 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700674 /* op supports buffer selection */
675 unsigned buffer_select : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700676};
677
678static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300679 [IORING_OP_NOP] = {},
680 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700681 .async_ctx = 1,
682 .needs_mm = 1,
683 .needs_file = 1,
684 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700685 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700686 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700687 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300688 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700689 .async_ctx = 1,
690 .needs_mm = 1,
691 .needs_file = 1,
692 .hash_reg_file = 1,
693 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700694 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700695 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300696 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700697 .needs_file = 1,
698 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300699 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700700 .needs_file = 1,
701 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700702 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700703 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300704 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700705 .needs_file = 1,
706 .hash_reg_file = 1,
707 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700708 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700709 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300710 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700711 .needs_file = 1,
712 .unbound_nonreg_file = 1,
713 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300714 [IORING_OP_POLL_REMOVE] = {},
715 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700716 .needs_file = 1,
717 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300718 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700719 .async_ctx = 1,
720 .needs_mm = 1,
721 .needs_file = 1,
722 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700723 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700724 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700725 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300726 [IORING_OP_RECVMSG] = {
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 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700733 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700734 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300735 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700736 .async_ctx = 1,
737 .needs_mm = 1,
738 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300739 [IORING_OP_TIMEOUT_REMOVE] = {},
740 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700741 .needs_mm = 1,
742 .needs_file = 1,
743 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700744 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700745 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700746 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300747 [IORING_OP_ASYNC_CANCEL] = {},
748 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700749 .async_ctx = 1,
750 .needs_mm = 1,
751 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300752 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700753 .async_ctx = 1,
754 .needs_mm = 1,
755 .needs_file = 1,
756 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700757 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700758 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300759 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700760 .needs_file = 1,
761 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300762 [IORING_OP_OPENAT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700763 .needs_file = 1,
764 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700765 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700766 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700767 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300768 [IORING_OP_CLOSE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700769 .needs_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700770 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700771 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300772 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700773 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700774 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700775 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300776 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700777 .needs_mm = 1,
778 .needs_file = 1,
779 .fd_non_neg = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700780 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700781 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300782 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700783 .needs_mm = 1,
784 .needs_file = 1,
785 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700786 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700787 .buffer_select = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700788 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300789 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700790 .needs_mm = 1,
791 .needs_file = 1,
792 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700793 .pollout = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700794 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300795 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700796 .needs_file = 1,
797 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300798 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700799 .needs_mm = 1,
800 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300801 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700802 .needs_mm = 1,
803 .needs_file = 1,
804 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700805 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700806 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300807 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700808 .needs_mm = 1,
809 .needs_file = 1,
810 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700811 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700812 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700813 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300814 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -0700815 .needs_file = 1,
816 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700817 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700818 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700819 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700820 [IORING_OP_EPOLL_CTL] = {
821 .unbound_nonreg_file = 1,
822 .file_table = 1,
823 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300824 [IORING_OP_SPLICE] = {
825 .needs_file = 1,
826 .hash_reg_file = 1,
827 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700828 },
829 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700830 [IORING_OP_REMOVE_BUFFERS] = {},
Jens Axboed3656342019-12-18 09:50:26 -0700831};
832
Jens Axboe561fb042019-10-24 07:25:42 -0600833static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700834static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800835static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700836static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700837static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
838static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700839static int __io_sqe_files_update(struct io_ring_ctx *ctx,
840 struct io_uring_files_update *ip,
841 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700842static int io_grab_files(struct io_kiocb *req);
Jens Axboe2faf8522020-02-04 19:54:55 -0700843static void io_ring_file_ref_flush(struct fixed_file_data *data);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300844static void io_cleanup_req(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700845static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
846 int fd, struct file **out_file, bool fixed);
847static void __io_queue_sqe(struct io_kiocb *req,
848 const struct io_uring_sqe *sqe);
Jens Axboede0617e2019-04-06 21:51:27 -0600849
Jens Axboe2b188cc2019-01-07 10:46:33 -0700850static struct kmem_cache *req_cachep;
851
852static const struct file_operations io_uring_fops;
853
854struct sock *io_uring_get_socket(struct file *file)
855{
856#if defined(CONFIG_UNIX)
857 if (file->f_op == &io_uring_fops) {
858 struct io_ring_ctx *ctx = file->private_data;
859
860 return ctx->ring_sock->sk;
861 }
862#endif
863 return NULL;
864}
865EXPORT_SYMBOL(io_uring_get_socket);
866
867static void io_ring_ctx_ref_free(struct percpu_ref *ref)
868{
869 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
870
Jens Axboe206aefd2019-11-07 18:27:42 -0700871 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700872}
873
874static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
875{
876 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700877 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700878
879 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
880 if (!ctx)
881 return NULL;
882
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700883 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
884 if (!ctx->fallback_req)
885 goto err;
886
Jens Axboe206aefd2019-11-07 18:27:42 -0700887 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
888 if (!ctx->completions)
889 goto err;
890
Jens Axboe78076bb2019-12-04 19:56:40 -0700891 /*
892 * Use 5 bits less than the max cq entries, that should give us around
893 * 32 entries per hash list if totally full and uniformly spread.
894 */
895 hash_bits = ilog2(p->cq_entries);
896 hash_bits -= 5;
897 if (hash_bits <= 0)
898 hash_bits = 1;
899 ctx->cancel_hash_bits = hash_bits;
900 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
901 GFP_KERNEL);
902 if (!ctx->cancel_hash)
903 goto err;
904 __hash_init(ctx->cancel_hash, 1U << hash_bits);
905
Roman Gushchin21482892019-05-07 10:01:48 -0700906 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700907 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
908 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700909
910 ctx->flags = p->flags;
911 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700912 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700913 init_completion(&ctx->completions[0]);
914 init_completion(&ctx->completions[1]);
Jens Axboe5a2e7452020-02-23 16:23:11 -0700915 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -0700916 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700917 mutex_init(&ctx->uring_lock);
918 init_waitqueue_head(&ctx->wait);
919 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700920 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600921 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600922 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600923 init_waitqueue_head(&ctx->inflight_wait);
924 spin_lock_init(&ctx->inflight_lock);
925 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700926 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700927err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700928 if (ctx->fallback_req)
929 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700930 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700931 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700932 kfree(ctx);
933 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700934}
935
Bob Liu9d858b22019-11-13 18:06:25 +0800936static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600937{
Jackie Liua197f662019-11-08 08:09:12 -0700938 struct io_ring_ctx *ctx = req->ctx;
939
Jens Axboe498ccd92019-10-25 10:04:25 -0600940 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
941 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600942}
943
Bob Liu9d858b22019-11-13 18:06:25 +0800944static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600945{
Pavel Begunkov87987892020-01-18 01:22:30 +0300946 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +0800947 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600948
Bob Liu9d858b22019-11-13 18:06:25 +0800949 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600950}
951
952static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600953{
954 struct io_kiocb *req;
955
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600956 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800957 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600958 list_del_init(&req->list);
959 return req;
960 }
961
962 return NULL;
963}
964
Jens Axboe5262f562019-09-17 12:26:57 -0600965static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
966{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600967 struct io_kiocb *req;
968
969 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700970 if (req) {
971 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
972 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800973 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700974 list_del_init(&req->list);
975 return req;
976 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600977 }
978
979 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600980}
981
Jens Axboede0617e2019-04-06 21:51:27 -0600982static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700983{
Hristo Venev75b28af2019-08-26 17:23:46 +0000984 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700985
Pavel Begunkov07910152020-01-17 03:52:46 +0300986 /* order cqe stores with ring update */
987 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700988
Pavel Begunkov07910152020-01-17 03:52:46 +0300989 if (wq_has_sleeper(&ctx->cq_wait)) {
990 wake_up_interruptible(&ctx->cq_wait);
991 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700992 }
993}
994
Jens Axboecccf0ee2020-01-27 16:34:48 -0700995static inline void io_req_work_grab_env(struct io_kiocb *req,
996 const struct io_op_def *def)
Jens Axboe18d9be12019-09-10 09:13:05 -0600997{
Jens Axboecccf0ee2020-01-27 16:34:48 -0700998 if (!req->work.mm && def->needs_mm) {
999 mmgrab(current->mm);
1000 req->work.mm = current->mm;
1001 }
1002 if (!req->work.creds)
1003 req->work.creds = get_current_cred();
Jens Axboeff002b32020-02-07 16:05:21 -07001004 if (!req->work.fs && def->needs_fs) {
1005 spin_lock(&current->fs->lock);
1006 if (!current->fs->in_exec) {
1007 req->work.fs = current->fs;
1008 req->work.fs->users++;
1009 } else {
1010 req->work.flags |= IO_WQ_WORK_CANCEL;
1011 }
1012 spin_unlock(&current->fs->lock);
1013 }
Jens Axboe6ab23142020-02-08 20:23:59 -07001014 if (!req->work.task_pid)
1015 req->work.task_pid = task_pid_vnr(current);
Jens Axboecccf0ee2020-01-27 16:34:48 -07001016}
1017
1018static inline void io_req_work_drop_env(struct io_kiocb *req)
1019{
1020 if (req->work.mm) {
1021 mmdrop(req->work.mm);
1022 req->work.mm = NULL;
1023 }
1024 if (req->work.creds) {
1025 put_cred(req->work.creds);
1026 req->work.creds = NULL;
1027 }
Jens Axboeff002b32020-02-07 16:05:21 -07001028 if (req->work.fs) {
1029 struct fs_struct *fs = req->work.fs;
1030
1031 spin_lock(&req->work.fs->lock);
1032 if (--fs->users)
1033 fs = NULL;
1034 spin_unlock(&req->work.fs->lock);
1035 if (fs)
1036 free_fs_struct(fs);
1037 }
Jens Axboe561fb042019-10-24 07:25:42 -06001038}
1039
Jens Axboe94ae5e72019-11-14 19:39:52 -07001040static inline bool io_prep_async_work(struct io_kiocb *req,
1041 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -06001042{
Jens Axboed3656342019-12-18 09:50:26 -07001043 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -06001044 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -06001045
Jens Axboed3656342019-12-18 09:50:26 -07001046 if (req->flags & REQ_F_ISREG) {
1047 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001048 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -07001049 } else {
1050 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001051 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001052 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001053
1054 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -06001055
Jens Axboe94ae5e72019-11-14 19:39:52 -07001056 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001057 return do_hashed;
1058}
1059
Jackie Liua197f662019-11-08 08:09:12 -07001060static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001061{
Jackie Liua197f662019-11-08 08:09:12 -07001062 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001063 struct io_kiocb *link;
1064 bool do_hashed;
1065
1066 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06001067
1068 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
1069 req->flags);
1070 if (!do_hashed) {
1071 io_wq_enqueue(ctx->io_wq, &req->work);
1072 } else {
1073 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
1074 file_inode(req->file));
1075 }
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);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001089 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001090 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001091 }
1092}
1093
1094static void io_kill_timeouts(struct io_ring_ctx *ctx)
1095{
1096 struct io_kiocb *req, *tmp;
1097
1098 spin_lock_irq(&ctx->completion_lock);
1099 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
1100 io_kill_timeout(req);
1101 spin_unlock_irq(&ctx->completion_lock);
1102}
1103
Jens Axboede0617e2019-04-06 21:51:27 -06001104static void io_commit_cqring(struct io_ring_ctx *ctx)
1105{
1106 struct io_kiocb *req;
1107
Jens Axboe5262f562019-09-17 12:26:57 -06001108 while ((req = io_get_timeout_req(ctx)) != NULL)
1109 io_kill_timeout(req);
1110
Jens Axboede0617e2019-04-06 21:51:27 -06001111 __io_commit_cqring(ctx);
1112
Pavel Begunkov87987892020-01-18 01:22:30 +03001113 while ((req = io_get_deferred_req(ctx)) != NULL)
Jackie Liua197f662019-11-08 08:09:12 -07001114 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -06001115}
1116
Jens Axboe2b188cc2019-01-07 10:46:33 -07001117static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1118{
Hristo Venev75b28af2019-08-26 17:23:46 +00001119 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001120 unsigned tail;
1121
1122 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001123 /*
1124 * writes to the cq entry need to come after reading head; the
1125 * control dependency is enough as we're using WRITE_ONCE to
1126 * fill the cq entry
1127 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001128 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001129 return NULL;
1130
1131 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001132 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001133}
1134
Jens Axboef2842ab2020-01-08 11:04:00 -07001135static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1136{
Jens Axboef0b493e2020-02-01 21:30:11 -07001137 if (!ctx->cq_ev_fd)
1138 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001139 if (!ctx->eventfd_async)
1140 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001141 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001142}
1143
Jens Axboeb41e9852020-02-17 09:52:41 -07001144static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001145{
1146 if (waitqueue_active(&ctx->wait))
1147 wake_up(&ctx->wait);
1148 if (waitqueue_active(&ctx->sqo_wait))
1149 wake_up(&ctx->sqo_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001150 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001151 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001152}
1153
Jens Axboec4a2ed72019-11-21 21:01:26 -07001154/* Returns true if there are no backlogged entries after the flush */
1155static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001156{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001157 struct io_rings *rings = ctx->rings;
1158 struct io_uring_cqe *cqe;
1159 struct io_kiocb *req;
1160 unsigned long flags;
1161 LIST_HEAD(list);
1162
1163 if (!force) {
1164 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001165 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001166 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1167 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001168 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001169 }
1170
1171 spin_lock_irqsave(&ctx->completion_lock, flags);
1172
1173 /* if force is set, the ring is going away. always drop after that */
1174 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001175 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001176
Jens Axboec4a2ed72019-11-21 21:01:26 -07001177 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001178 while (!list_empty(&ctx->cq_overflow_list)) {
1179 cqe = io_get_cqring(ctx);
1180 if (!cqe && !force)
1181 break;
1182
1183 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1184 list);
1185 list_move(&req->list, &list);
Jens Axboe2ca10252020-02-13 17:17:35 -07001186 req->flags &= ~REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001187 if (cqe) {
1188 WRITE_ONCE(cqe->user_data, req->user_data);
1189 WRITE_ONCE(cqe->res, req->result);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001190 WRITE_ONCE(cqe->flags, req->cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001191 } else {
1192 WRITE_ONCE(ctx->rings->cq_overflow,
1193 atomic_inc_return(&ctx->cached_cq_overflow));
1194 }
1195 }
1196
1197 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001198 if (cqe) {
1199 clear_bit(0, &ctx->sq_check_overflow);
1200 clear_bit(0, &ctx->cq_check_overflow);
1201 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001202 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1203 io_cqring_ev_posted(ctx);
1204
1205 while (!list_empty(&list)) {
1206 req = list_first_entry(&list, struct io_kiocb, list);
1207 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001208 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001209 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001210
1211 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001212}
1213
Jens Axboebcda7ba2020-02-23 16:42:51 -07001214static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001215{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001216 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001217 struct io_uring_cqe *cqe;
1218
Jens Axboe78e19bb2019-11-06 15:21:34 -07001219 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001220
Jens Axboe2b188cc2019-01-07 10:46:33 -07001221 /*
1222 * If we can't get a cq entry, userspace overflowed the
1223 * submission (by quite a lot). Increment the overflow count in
1224 * the ring.
1225 */
1226 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001227 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001228 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001229 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001230 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001231 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001232 WRITE_ONCE(ctx->rings->cq_overflow,
1233 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001234 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001235 if (list_empty(&ctx->cq_overflow_list)) {
1236 set_bit(0, &ctx->sq_check_overflow);
1237 set_bit(0, &ctx->cq_check_overflow);
1238 }
Jens Axboe2ca10252020-02-13 17:17:35 -07001239 req->flags |= REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001240 refcount_inc(&req->refs);
1241 req->result = res;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001242 req->cflags = cflags;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001243 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001244 }
1245}
1246
Jens Axboebcda7ba2020-02-23 16:42:51 -07001247static void io_cqring_fill_event(struct io_kiocb *req, long res)
1248{
1249 __io_cqring_fill_event(req, res, 0);
1250}
1251
1252static void __io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001253{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001254 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001255 unsigned long flags;
1256
1257 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001258 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001259 io_commit_cqring(ctx);
1260 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1261
Jens Axboe8c838782019-03-12 15:48:16 -06001262 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001263}
1264
Jens Axboebcda7ba2020-02-23 16:42:51 -07001265static void io_cqring_add_event(struct io_kiocb *req, long res)
1266{
1267 __io_cqring_add_event(req, res, 0);
1268}
1269
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001270static inline bool io_is_fallback_req(struct io_kiocb *req)
1271{
1272 return req == (struct io_kiocb *)
1273 ((unsigned long) req->ctx->fallback_req & ~1UL);
1274}
1275
1276static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1277{
1278 struct io_kiocb *req;
1279
1280 req = ctx->fallback_req;
1281 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1282 return req;
1283
1284 return NULL;
1285}
1286
Jens Axboe2579f912019-01-09 09:10:43 -07001287static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1288 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001289{
Jens Axboefd6fab22019-03-14 16:30:06 -06001290 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001291 struct io_kiocb *req;
1292
Jens Axboe2579f912019-01-09 09:10:43 -07001293 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001294 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001295 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001296 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001297 } else if (!state->free_reqs) {
1298 size_t sz;
1299 int ret;
1300
1301 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001302 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1303
1304 /*
1305 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1306 * retry single alloc to be on the safe side.
1307 */
1308 if (unlikely(ret <= 0)) {
1309 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1310 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001311 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001312 ret = 1;
1313 }
Jens Axboe2579f912019-01-09 09:10:43 -07001314 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001315 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001316 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001317 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001318 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001319 }
1320
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001321got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001322 req->io = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001323 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001324 req->ctx = ctx;
1325 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001326 /* one is dropped after submission, the other at completion */
1327 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001328 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001329 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001330 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001331fallback:
1332 req = io_get_fallback_req(ctx);
1333 if (req)
1334 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001335 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001336 return NULL;
1337}
1338
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001339static inline void io_put_file(struct io_kiocb *req, struct file *file,
1340 bool fixed)
1341{
1342 if (fixed)
1343 percpu_ref_put(&req->ctx->file_data->refs);
1344 else
1345 fput(file);
1346}
1347
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001348static void __io_req_do_free(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07001349{
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001350 if (likely(!io_is_fallback_req(req)))
1351 kmem_cache_free(req_cachep, req);
1352 else
1353 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
1354}
1355
Jens Axboec6ca97b302019-12-28 12:11:08 -07001356static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001357{
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001358 if (req->flags & REQ_F_NEED_CLEANUP)
1359 io_cleanup_req(req);
1360
YueHaibing96fd84d2020-01-07 22:22:44 +08001361 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001362 if (req->file)
1363 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboecccf0ee2020-01-27 16:34:48 -07001364
1365 io_req_work_drop_env(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001366}
1367
1368static void __io_free_req(struct io_kiocb *req)
1369{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001370 __io_req_aux_free(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06001371
Jens Axboefcb323c2019-10-24 12:39:47 -06001372 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001373 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001374 unsigned long flags;
1375
1376 spin_lock_irqsave(&ctx->inflight_lock, flags);
1377 list_del(&req->inflight_entry);
1378 if (waitqueue_active(&ctx->inflight_wait))
1379 wake_up(&ctx->inflight_wait);
1380 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1381 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001382
1383 percpu_ref_put(&req->ctx->refs);
1384 __io_req_do_free(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001385}
1386
Jens Axboec6ca97b302019-12-28 12:11:08 -07001387struct req_batch {
1388 void *reqs[IO_IOPOLL_BATCH];
1389 int to_free;
1390 int need_iter;
1391};
1392
1393static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1394{
Jens Axboe10fef4b2020-01-09 07:52:28 -07001395 int fixed_refs = rb->to_free;
1396
Jens Axboec6ca97b302019-12-28 12:11:08 -07001397 if (!rb->to_free)
1398 return;
1399 if (rb->need_iter) {
1400 int i, inflight = 0;
1401 unsigned long flags;
1402
Jens Axboe10fef4b2020-01-09 07:52:28 -07001403 fixed_refs = 0;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001404 for (i = 0; i < rb->to_free; i++) {
1405 struct io_kiocb *req = rb->reqs[i];
1406
Jens Axboe10fef4b2020-01-09 07:52:28 -07001407 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001408 req->file = NULL;
Jens Axboe10fef4b2020-01-09 07:52:28 -07001409 fixed_refs++;
1410 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001411 if (req->flags & REQ_F_INFLIGHT)
1412 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001413 __io_req_aux_free(req);
1414 }
1415 if (!inflight)
1416 goto do_free;
1417
1418 spin_lock_irqsave(&ctx->inflight_lock, flags);
1419 for (i = 0; i < rb->to_free; i++) {
1420 struct io_kiocb *req = rb->reqs[i];
1421
Jens Axboe10fef4b2020-01-09 07:52:28 -07001422 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001423 list_del(&req->inflight_entry);
1424 if (!--inflight)
1425 break;
1426 }
1427 }
1428 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1429
1430 if (waitqueue_active(&ctx->inflight_wait))
1431 wake_up(&ctx->inflight_wait);
1432 }
1433do_free:
1434 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001435 if (fixed_refs)
1436 percpu_ref_put_many(&ctx->file_data->refs, fixed_refs);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001437 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001438 rb->to_free = rb->need_iter = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001439}
1440
Jackie Liua197f662019-11-08 08:09:12 -07001441static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001442{
Jackie Liua197f662019-11-08 08:09:12 -07001443 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001444 int ret;
1445
Jens Axboe2d283902019-12-04 11:08:05 -07001446 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001447 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001448 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001449 io_commit_cqring(ctx);
1450 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001451 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001452 return true;
1453 }
1454
1455 return false;
1456}
1457
Jens Axboeba816ad2019-09-28 11:36:45 -06001458static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001459{
Jens Axboe2665abf2019-11-05 12:40:47 -07001460 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001461 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001462
Jens Axboe4d7dd462019-11-20 13:03:52 -07001463 /* Already got next link */
1464 if (req->flags & REQ_F_LINK_NEXT)
1465 return;
1466
Jens Axboe9e645e112019-05-10 16:07:28 -06001467 /*
1468 * The list should never be empty when we are called here. But could
1469 * potentially happen if the chain is messed up, check to be on the
1470 * safe side.
1471 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001472 while (!list_empty(&req->link_list)) {
1473 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1474 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001475
Pavel Begunkov44932332019-12-05 16:16:35 +03001476 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1477 (nxt->flags & REQ_F_TIMEOUT))) {
1478 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001479 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001480 req->flags &= ~REQ_F_LINK_TIMEOUT;
1481 continue;
1482 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001483
Pavel Begunkov44932332019-12-05 16:16:35 +03001484 list_del_init(&req->link_list);
1485 if (!list_empty(&nxt->link_list))
1486 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001487 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001488 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001489 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001490
Jens Axboe4d7dd462019-11-20 13:03:52 -07001491 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001492 if (wake_ev)
1493 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001494}
1495
1496/*
1497 * Called if REQ_F_LINK is set, and we fail the head request
1498 */
1499static void io_fail_links(struct io_kiocb *req)
1500{
Jens Axboe2665abf2019-11-05 12:40:47 -07001501 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001502 unsigned long flags;
1503
1504 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001505
1506 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001507 struct io_kiocb *link = list_first_entry(&req->link_list,
1508 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001509
Pavel Begunkov44932332019-12-05 16:16:35 +03001510 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001511 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001512
1513 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001514 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001515 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001516 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001517 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001518 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001519 }
Jens Axboe5d960722019-11-19 15:31:28 -07001520 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001521 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001522
1523 io_commit_cqring(ctx);
1524 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1525 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001526}
1527
Jens Axboe4d7dd462019-11-20 13:03:52 -07001528static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001529{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001530 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001531 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001532
Jens Axboe9e645e112019-05-10 16:07:28 -06001533 /*
1534 * If LINK is set, we have dependent requests in this chain. If we
1535 * didn't fail this request, queue the first one up, moving any other
1536 * dependencies to the next request. In case of failure, fail the rest
1537 * of the chain.
1538 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001539 if (req->flags & REQ_F_FAIL_LINK) {
1540 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001541 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1542 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001543 struct io_ring_ctx *ctx = req->ctx;
1544 unsigned long flags;
1545
1546 /*
1547 * If this is a timeout link, we could be racing with the
1548 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001549 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001550 */
1551 spin_lock_irqsave(&ctx->completion_lock, flags);
1552 io_req_link_next(req, nxt);
1553 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1554 } else {
1555 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001556 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001557}
Jens Axboe9e645e112019-05-10 16:07:28 -06001558
Jackie Liuc69f8db2019-11-09 11:00:08 +08001559static void io_free_req(struct io_kiocb *req)
1560{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001561 struct io_kiocb *nxt = NULL;
1562
1563 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001564 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001565
1566 if (nxt)
1567 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001568}
1569
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001570static void io_link_work_cb(struct io_wq_work **workptr)
1571{
1572 struct io_wq_work *work = *workptr;
1573 struct io_kiocb *link = work->data;
1574
1575 io_queue_linked_timeout(link);
1576 io_wq_submit_work(workptr);
1577}
1578
1579static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
1580{
1581 struct io_kiocb *link;
1582
1583 *workptr = &nxt->work;
1584 link = io_prep_linked_timeout(nxt);
1585 if (link) {
1586 nxt->work.func = io_link_work_cb;
1587 nxt->work.data = link;
1588 }
1589}
1590
Jens Axboeba816ad2019-09-28 11:36:45 -06001591/*
1592 * Drop reference to request, return next in chain (if there is one) if this
1593 * was the last reference to this request.
1594 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001595__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001596static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001597{
Jens Axboe2a44f462020-02-25 13:25:41 -07001598 if (refcount_dec_and_test(&req->refs)) {
1599 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001600 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001601 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001602}
1603
Jens Axboe2b188cc2019-01-07 10:46:33 -07001604static void io_put_req(struct io_kiocb *req)
1605{
Jens Axboedef596e2019-01-09 08:59:42 -07001606 if (refcount_dec_and_test(&req->refs))
1607 io_free_req(req);
1608}
1609
Pavel Begunkove9fd9392020-03-04 16:14:12 +03001610static void io_steal_work(struct io_kiocb *req,
1611 struct io_wq_work **workptr)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001612{
1613 /*
1614 * It's in an io-wq worker, so there always should be at least
1615 * one reference, which will be dropped in io_put_work() just
1616 * after the current handler returns.
1617 *
1618 * It also means, that if the counter dropped to 1, then there is
1619 * no asynchronous users left, so it's safe to steal the next work.
1620 */
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001621 if (refcount_read(&req->refs) == 1) {
1622 struct io_kiocb *nxt = NULL;
1623
1624 io_req_find_next(req, &nxt);
1625 if (nxt)
1626 io_wq_assign_next(workptr, nxt);
1627 }
1628}
1629
Jens Axboe978db572019-11-14 22:39:04 -07001630/*
1631 * Must only be used if we don't need to care about links, usually from
1632 * within the completion handling itself.
1633 */
1634static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001635{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001636 /* drop both submit and complete references */
1637 if (refcount_sub_and_test(2, &req->refs))
1638 __io_free_req(req);
1639}
1640
Jens Axboe978db572019-11-14 22:39:04 -07001641static void io_double_put_req(struct io_kiocb *req)
1642{
1643 /* drop both submit and complete references */
1644 if (refcount_sub_and_test(2, &req->refs))
1645 io_free_req(req);
1646}
1647
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001648static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001649{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001650 struct io_rings *rings = ctx->rings;
1651
Jens Axboead3eb2c2019-12-18 17:12:20 -07001652 if (test_bit(0, &ctx->cq_check_overflow)) {
1653 /*
1654 * noflush == true is from the waitqueue handler, just ensure
1655 * we wake up the task, and the next invocation will flush the
1656 * entries. We cannot safely to it from here.
1657 */
1658 if (noflush && !list_empty(&ctx->cq_overflow_list))
1659 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001660
Jens Axboead3eb2c2019-12-18 17:12:20 -07001661 io_cqring_overflow_flush(ctx, false);
1662 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001663
Jens Axboea3a0e432019-08-20 11:03:11 -06001664 /* See comment at the top of this file */
1665 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001666 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001667}
1668
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001669static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1670{
1671 struct io_rings *rings = ctx->rings;
1672
1673 /* make sure SQ entry isn't read before tail */
1674 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1675}
1676
Jens Axboe8237e042019-12-28 10:48:22 -07001677static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001678{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001679 if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req))
1680 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001681
Jens Axboec6ca97b302019-12-28 12:11:08 -07001682 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1683 rb->need_iter++;
1684
1685 rb->reqs[rb->to_free++] = req;
1686 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1687 io_free_req_many(req->ctx, rb);
1688 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001689}
1690
Jens Axboebcda7ba2020-02-23 16:42:51 -07001691static int io_put_kbuf(struct io_kiocb *req)
1692{
Jens Axboe4d954c22020-02-27 07:31:19 -07001693 struct io_buffer *kbuf;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001694 int cflags;
1695
Jens Axboe4d954c22020-02-27 07:31:19 -07001696 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001697 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
1698 cflags |= IORING_CQE_F_BUFFER;
1699 req->rw.addr = 0;
1700 kfree(kbuf);
1701 return cflags;
1702}
1703
Jens Axboedef596e2019-01-09 08:59:42 -07001704/*
1705 * Find and free completed poll iocbs
1706 */
1707static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1708 struct list_head *done)
1709{
Jens Axboe8237e042019-12-28 10:48:22 -07001710 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001711 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001712
Jens Axboec6ca97b302019-12-28 12:11:08 -07001713 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001714 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07001715 int cflags = 0;
1716
Jens Axboedef596e2019-01-09 08:59:42 -07001717 req = list_first_entry(done, struct io_kiocb, list);
1718 list_del(&req->list);
1719
Jens Axboebcda7ba2020-02-23 16:42:51 -07001720 if (req->flags & REQ_F_BUFFER_SELECTED)
1721 cflags = io_put_kbuf(req);
1722
1723 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07001724 (*nr_events)++;
1725
Jens Axboe8237e042019-12-28 10:48:22 -07001726 if (refcount_dec_and_test(&req->refs) &&
1727 !io_req_multi_free(&rb, req))
1728 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001729 }
Jens Axboedef596e2019-01-09 08:59:42 -07001730
Jens Axboe09bb8392019-03-13 12:39:28 -06001731 io_commit_cqring(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001732 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001733}
1734
1735static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1736 long min)
1737{
1738 struct io_kiocb *req, *tmp;
1739 LIST_HEAD(done);
1740 bool spin;
1741 int ret;
1742
1743 /*
1744 * Only spin for completions if we don't have multiple devices hanging
1745 * off our complete list, and we're under the requested amount.
1746 */
1747 spin = !ctx->poll_multi_file && *nr_events < min;
1748
1749 ret = 0;
1750 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001751 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001752
1753 /*
1754 * Move completed entries to our local list. If we find a
1755 * request that requires polling, break out and complete
1756 * the done list first, if we have entries there.
1757 */
1758 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1759 list_move_tail(&req->list, &done);
1760 continue;
1761 }
1762 if (!list_empty(&done))
1763 break;
1764
1765 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1766 if (ret < 0)
1767 break;
1768
1769 if (ret && spin)
1770 spin = false;
1771 ret = 0;
1772 }
1773
1774 if (!list_empty(&done))
1775 io_iopoll_complete(ctx, nr_events, &done);
1776
1777 return ret;
1778}
1779
1780/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001781 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001782 * non-spinning poll check - we'll still enter the driver poll loop, but only
1783 * as a non-spinning completion check.
1784 */
1785static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1786 long min)
1787{
Jens Axboe08f54392019-08-21 22:19:11 -06001788 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001789 int ret;
1790
1791 ret = io_do_iopoll(ctx, nr_events, min);
1792 if (ret < 0)
1793 return ret;
1794 if (!min || *nr_events >= min)
1795 return 0;
1796 }
1797
1798 return 1;
1799}
1800
1801/*
1802 * We can't just wait for polled events to come to us, we have to actively
1803 * find and complete them.
1804 */
1805static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1806{
1807 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1808 return;
1809
1810 mutex_lock(&ctx->uring_lock);
1811 while (!list_empty(&ctx->poll_list)) {
1812 unsigned int nr_events = 0;
1813
1814 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001815
1816 /*
1817 * Ensure we allow local-to-the-cpu processing to take place,
1818 * in this case we need to ensure that we reap all events.
1819 */
1820 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001821 }
1822 mutex_unlock(&ctx->uring_lock);
1823}
1824
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001825static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1826 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001827{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001828 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001829
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001830 /*
1831 * We disallow the app entering submit/complete with polling, but we
1832 * still need to lock the ring to prevent racing with polled issue
1833 * that got punted to a workqueue.
1834 */
1835 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001836 do {
1837 int tmin = 0;
1838
Jens Axboe500f9fb2019-08-19 12:15:59 -06001839 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001840 * Don't enter poll loop if we already have events pending.
1841 * If we do, we can potentially be spinning for commands that
1842 * already triggered a CQE (eg in error).
1843 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001844 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001845 break;
1846
1847 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001848 * If a submit got punted to a workqueue, we can have the
1849 * application entering polling for a command before it gets
1850 * issued. That app will hold the uring_lock for the duration
1851 * of the poll right here, so we need to take a breather every
1852 * now and then to ensure that the issue has a chance to add
1853 * the poll to the issued list. Otherwise we can spin here
1854 * forever, while the workqueue is stuck trying to acquire the
1855 * very same mutex.
1856 */
1857 if (!(++iters & 7)) {
1858 mutex_unlock(&ctx->uring_lock);
1859 mutex_lock(&ctx->uring_lock);
1860 }
1861
Jens Axboedef596e2019-01-09 08:59:42 -07001862 if (*nr_events < min)
1863 tmin = min - *nr_events;
1864
1865 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1866 if (ret <= 0)
1867 break;
1868 ret = 0;
1869 } while (min && !*nr_events && !need_resched());
1870
Jens Axboe500f9fb2019-08-19 12:15:59 -06001871 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001872 return ret;
1873}
1874
Jens Axboe491381ce2019-10-17 09:20:46 -06001875static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001876{
Jens Axboe491381ce2019-10-17 09:20:46 -06001877 /*
1878 * Tell lockdep we inherited freeze protection from submission
1879 * thread.
1880 */
1881 if (req->flags & REQ_F_ISREG) {
1882 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001883
Jens Axboe491381ce2019-10-17 09:20:46 -06001884 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001885 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001886 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001887}
1888
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001889static inline void req_set_fail_links(struct io_kiocb *req)
1890{
1891 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1892 req->flags |= REQ_F_FAIL_LINK;
1893}
1894
Jens Axboeba816ad2019-09-28 11:36:45 -06001895static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001896{
Jens Axboe9adbd452019-12-20 08:45:55 -07001897 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001898 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001899
Jens Axboe491381ce2019-10-17 09:20:46 -06001900 if (kiocb->ki_flags & IOCB_WRITE)
1901 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001902
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001903 if (res != req->result)
1904 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001905 if (req->flags & REQ_F_BUFFER_SELECTED)
1906 cflags = io_put_kbuf(req);
1907 __io_cqring_add_event(req, res, cflags);
Jens Axboeba816ad2019-09-28 11:36:45 -06001908}
1909
1910static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1911{
Jens Axboe9adbd452019-12-20 08:45:55 -07001912 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001913
1914 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001915 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001916}
1917
Jens Axboedef596e2019-01-09 08:59:42 -07001918static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1919{
Jens Axboe9adbd452019-12-20 08:45:55 -07001920 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001921
Jens Axboe491381ce2019-10-17 09:20:46 -06001922 if (kiocb->ki_flags & IOCB_WRITE)
1923 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001924
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001925 if (res != req->result)
1926 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001927 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001928 if (res != -EAGAIN)
1929 req->flags |= REQ_F_IOPOLL_COMPLETED;
1930}
1931
1932/*
1933 * After the iocb has been issued, it's safe to be found on the poll list.
1934 * Adding the kiocb to the list AFTER submission ensures that we don't
1935 * find it from a io_iopoll_getevents() thread before the issuer is done
1936 * accessing the kiocb cookie.
1937 */
1938static void io_iopoll_req_issued(struct io_kiocb *req)
1939{
1940 struct io_ring_ctx *ctx = req->ctx;
1941
1942 /*
1943 * Track whether we have multiple files in our lists. This will impact
1944 * how we do polling eventually, not spinning if we're on potentially
1945 * different devices.
1946 */
1947 if (list_empty(&ctx->poll_list)) {
1948 ctx->poll_multi_file = false;
1949 } else if (!ctx->poll_multi_file) {
1950 struct io_kiocb *list_req;
1951
1952 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1953 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001954 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001955 ctx->poll_multi_file = true;
1956 }
1957
1958 /*
1959 * For fast devices, IO may have already completed. If it has, add
1960 * it to the front so we find it first.
1961 */
1962 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1963 list_add(&req->list, &ctx->poll_list);
1964 else
1965 list_add_tail(&req->list, &ctx->poll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08001966
1967 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
1968 wq_has_sleeper(&ctx->sqo_wait))
1969 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07001970}
1971
Jens Axboe3d6770f2019-04-13 11:50:54 -06001972static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001973{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001974 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001975 int diff = state->has_refs - state->used_refs;
1976
1977 if (diff)
1978 fput_many(state->file, diff);
1979 state->file = NULL;
1980 }
1981}
1982
1983/*
1984 * Get as many references to a file as we have IOs left in this submission,
1985 * assuming most submissions are for one file, or at least that each file
1986 * has more than one submission.
1987 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001988static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07001989{
1990 if (!state)
1991 return fget(fd);
1992
1993 if (state->file) {
1994 if (state->fd == fd) {
1995 state->used_refs++;
1996 state->ios_left--;
1997 return state->file;
1998 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001999 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002000 }
2001 state->file = fget_many(fd, state->ios_left);
2002 if (!state->file)
2003 return NULL;
2004
2005 state->fd = fd;
2006 state->has_refs = state->ios_left;
2007 state->used_refs = 1;
2008 state->ios_left--;
2009 return state->file;
2010}
2011
Jens Axboe2b188cc2019-01-07 10:46:33 -07002012/*
2013 * If we tracked the file through the SCM inflight mechanism, we could support
2014 * any file. For now, just ensure that anything potentially problematic is done
2015 * inline.
2016 */
2017static bool io_file_supports_async(struct file *file)
2018{
2019 umode_t mode = file_inode(file)->i_mode;
2020
Jens Axboe10d59342019-12-09 20:16:22 -07002021 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002022 return true;
2023 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
2024 return true;
2025
2026 return false;
2027}
2028
Jens Axboe3529d8c2019-12-19 18:24:38 -07002029static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2030 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002031{
Jens Axboedef596e2019-01-09 08:59:42 -07002032 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002033 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002034 unsigned ioprio;
2035 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002036
Jens Axboe491381ce2019-10-17 09:20:46 -06002037 if (S_ISREG(file_inode(req->file)->i_mode))
2038 req->flags |= REQ_F_ISREG;
2039
Jens Axboe2b188cc2019-01-07 10:46:33 -07002040 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002041 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2042 req->flags |= REQ_F_CUR_POS;
2043 kiocb->ki_pos = req->file->f_pos;
2044 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002045 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002046 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2047 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2048 if (unlikely(ret))
2049 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002050
2051 ioprio = READ_ONCE(sqe->ioprio);
2052 if (ioprio) {
2053 ret = ioprio_check_cap(ioprio);
2054 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002055 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002056
2057 kiocb->ki_ioprio = ioprio;
2058 } else
2059 kiocb->ki_ioprio = get_current_ioprio();
2060
Stefan Bühler8449eed2019-04-27 20:34:19 +02002061 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06002062 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
2063 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02002064 req->flags |= REQ_F_NOWAIT;
2065
2066 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002067 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02002068
Jens Axboedef596e2019-01-09 08:59:42 -07002069 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002070 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2071 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002072 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002073
Jens Axboedef596e2019-01-09 08:59:42 -07002074 kiocb->ki_flags |= IOCB_HIPRI;
2075 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06002076 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002077 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002078 if (kiocb->ki_flags & IOCB_HIPRI)
2079 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002080 kiocb->ki_complete = io_complete_rw;
2081 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002082
Jens Axboe3529d8c2019-12-19 18:24:38 -07002083 req->rw.addr = READ_ONCE(sqe->addr);
2084 req->rw.len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002085 /* we own ->private, reuse it for the buffer index / buffer ID */
Jens Axboe9adbd452019-12-20 08:45:55 -07002086 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002087 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002088 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002089}
2090
2091static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2092{
2093 switch (ret) {
2094 case -EIOCBQUEUED:
2095 break;
2096 case -ERESTARTSYS:
2097 case -ERESTARTNOINTR:
2098 case -ERESTARTNOHAND:
2099 case -ERESTART_RESTARTBLOCK:
2100 /*
2101 * We can't just restart the syscall, since previously
2102 * submitted sqes may already be in progress. Just fail this
2103 * IO with EINTR.
2104 */
2105 ret = -EINTR;
2106 /* fall through */
2107 default:
2108 kiocb->ki_complete(kiocb, ret, 0);
2109 }
2110}
2111
Pavel Begunkov014db002020-03-03 21:33:12 +03002112static void kiocb_done(struct kiocb *kiocb, ssize_t ret)
Jens Axboeba816ad2019-09-28 11:36:45 -06002113{
Jens Axboeba042912019-12-25 16:33:42 -07002114 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2115
2116 if (req->flags & REQ_F_CUR_POS)
2117 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002118 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Pavel Begunkov014db002020-03-03 21:33:12 +03002119 io_complete_rw(kiocb, ret, 0);
Jens Axboeba816ad2019-09-28 11:36:45 -06002120 else
2121 io_rw_done(kiocb, ret);
2122}
2123
Jens Axboe9adbd452019-12-20 08:45:55 -07002124static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002125 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002126{
Jens Axboe9adbd452019-12-20 08:45:55 -07002127 struct io_ring_ctx *ctx = req->ctx;
2128 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002129 struct io_mapped_ubuf *imu;
2130 unsigned index, buf_index;
2131 size_t offset;
2132 u64 buf_addr;
2133
2134 /* attempt to use fixed buffers without having provided iovecs */
2135 if (unlikely(!ctx->user_bufs))
2136 return -EFAULT;
2137
Jens Axboe9adbd452019-12-20 08:45:55 -07002138 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07002139 if (unlikely(buf_index >= ctx->nr_user_bufs))
2140 return -EFAULT;
2141
2142 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2143 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002144 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002145
2146 /* overflow */
2147 if (buf_addr + len < buf_addr)
2148 return -EFAULT;
2149 /* not inside the mapped region */
2150 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2151 return -EFAULT;
2152
2153 /*
2154 * May not be a start of buffer, set size appropriately
2155 * and advance us to the beginning.
2156 */
2157 offset = buf_addr - imu->ubuf;
2158 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002159
2160 if (offset) {
2161 /*
2162 * Don't use iov_iter_advance() here, as it's really slow for
2163 * using the latter parts of a big fixed buffer - it iterates
2164 * over each segment manually. We can cheat a bit here, because
2165 * we know that:
2166 *
2167 * 1) it's a BVEC iter, we set it up
2168 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2169 * first and last bvec
2170 *
2171 * So just find our index, and adjust the iterator afterwards.
2172 * If the offset is within the first bvec (or the whole first
2173 * bvec, just use iov_iter_advance(). This makes it easier
2174 * since we can just skip the first segment, which may not
2175 * be PAGE_SIZE aligned.
2176 */
2177 const struct bio_vec *bvec = imu->bvec;
2178
2179 if (offset <= bvec->bv_len) {
2180 iov_iter_advance(iter, offset);
2181 } else {
2182 unsigned long seg_skip;
2183
2184 /* skip first vec */
2185 offset -= bvec->bv_len;
2186 seg_skip = 1 + (offset >> PAGE_SHIFT);
2187
2188 iter->bvec = bvec + seg_skip;
2189 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002190 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002191 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002192 }
2193 }
2194
Jens Axboe5e559562019-11-13 16:12:46 -07002195 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002196}
2197
Jens Axboebcda7ba2020-02-23 16:42:51 -07002198static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2199{
2200 if (needs_lock)
2201 mutex_unlock(&ctx->uring_lock);
2202}
2203
2204static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2205{
2206 /*
2207 * "Normal" inline submissions always hold the uring_lock, since we
2208 * grab it from the system call. Same is true for the SQPOLL offload.
2209 * The only exception is when we've detached the request and issue it
2210 * from an async worker thread, grab the lock for that case.
2211 */
2212 if (needs_lock)
2213 mutex_lock(&ctx->uring_lock);
2214}
2215
2216static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2217 int bgid, struct io_buffer *kbuf,
2218 bool needs_lock)
2219{
2220 struct io_buffer *head;
2221
2222 if (req->flags & REQ_F_BUFFER_SELECTED)
2223 return kbuf;
2224
2225 io_ring_submit_lock(req->ctx, needs_lock);
2226
2227 lockdep_assert_held(&req->ctx->uring_lock);
2228
2229 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2230 if (head) {
2231 if (!list_empty(&head->list)) {
2232 kbuf = list_last_entry(&head->list, struct io_buffer,
2233 list);
2234 list_del(&kbuf->list);
2235 } else {
2236 kbuf = head;
2237 idr_remove(&req->ctx->io_buffer_idr, bgid);
2238 }
2239 if (*len > kbuf->len)
2240 *len = kbuf->len;
2241 } else {
2242 kbuf = ERR_PTR(-ENOBUFS);
2243 }
2244
2245 io_ring_submit_unlock(req->ctx, needs_lock);
2246
2247 return kbuf;
2248}
2249
Jens Axboe4d954c22020-02-27 07:31:19 -07002250static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2251 bool needs_lock)
2252{
2253 struct io_buffer *kbuf;
2254 int bgid;
2255
2256 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2257 bgid = (int) (unsigned long) req->rw.kiocb.private;
2258 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2259 if (IS_ERR(kbuf))
2260 return kbuf;
2261 req->rw.addr = (u64) (unsigned long) kbuf;
2262 req->flags |= REQ_F_BUFFER_SELECTED;
2263 return u64_to_user_ptr(kbuf->addr);
2264}
2265
2266#ifdef CONFIG_COMPAT
2267static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2268 bool needs_lock)
2269{
2270 struct compat_iovec __user *uiov;
2271 compat_ssize_t clen;
2272 void __user *buf;
2273 ssize_t len;
2274
2275 uiov = u64_to_user_ptr(req->rw.addr);
2276 if (!access_ok(uiov, sizeof(*uiov)))
2277 return -EFAULT;
2278 if (__get_user(clen, &uiov->iov_len))
2279 return -EFAULT;
2280 if (clen < 0)
2281 return -EINVAL;
2282
2283 len = clen;
2284 buf = io_rw_buffer_select(req, &len, needs_lock);
2285 if (IS_ERR(buf))
2286 return PTR_ERR(buf);
2287 iov[0].iov_base = buf;
2288 iov[0].iov_len = (compat_size_t) len;
2289 return 0;
2290}
2291#endif
2292
2293static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2294 bool needs_lock)
2295{
2296 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2297 void __user *buf;
2298 ssize_t len;
2299
2300 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2301 return -EFAULT;
2302
2303 len = iov[0].iov_len;
2304 if (len < 0)
2305 return -EINVAL;
2306 buf = io_rw_buffer_select(req, &len, needs_lock);
2307 if (IS_ERR(buf))
2308 return PTR_ERR(buf);
2309 iov[0].iov_base = buf;
2310 iov[0].iov_len = len;
2311 return 0;
2312}
2313
2314static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2315 bool needs_lock)
2316{
2317 if (req->flags & REQ_F_BUFFER_SELECTED)
2318 return 0;
2319 if (!req->rw.len)
2320 return 0;
2321 else if (req->rw.len > 1)
2322 return -EINVAL;
2323
2324#ifdef CONFIG_COMPAT
2325 if (req->ctx->compat)
2326 return io_compat_import(req, iov, needs_lock);
2327#endif
2328
2329 return __io_iov_buffer_select(req, iov, needs_lock);
2330}
2331
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002332static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
Jens Axboebcda7ba2020-02-23 16:42:51 -07002333 struct iovec **iovec, struct iov_iter *iter,
2334 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002335{
Jens Axboe9adbd452019-12-20 08:45:55 -07002336 void __user *buf = u64_to_user_ptr(req->rw.addr);
2337 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002338 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002339 u8 opcode;
2340
Jens Axboed625c6e2019-12-17 19:53:05 -07002341 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002342 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002343 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002344 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002345 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002346
Jens Axboebcda7ba2020-02-23 16:42:51 -07002347 /* buffer index only valid with fixed read/write, or buffer select */
2348 if (req->rw.kiocb.private && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002349 return -EINVAL;
2350
Jens Axboe3a6820f2019-12-22 15:19:35 -07002351 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002352 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002353 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
2354 if (IS_ERR(buf)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002355 *iovec = NULL;
Jens Axboe4d954c22020-02-27 07:31:19 -07002356 return PTR_ERR(buf);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002357 }
Jens Axboebcda7ba2020-02-23 16:42:51 -07002358 }
2359
Jens Axboe3a6820f2019-12-22 15:19:35 -07002360 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2361 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002362 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002363 }
2364
Jens Axboef67676d2019-12-02 11:03:47 -07002365 if (req->io) {
2366 struct io_async_rw *iorw = &req->io->rw;
2367
2368 *iovec = iorw->iov;
2369 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2370 if (iorw->iov == iorw->fast_iov)
2371 *iovec = NULL;
2372 return iorw->size;
2373 }
2374
Jens Axboe4d954c22020-02-27 07:31:19 -07002375 if (req->flags & REQ_F_BUFFER_SELECT) {
2376 ret = io_iov_buffer_select(req, *iovec, needs_lock);
2377 if (!ret)
2378 iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len);
2379 *iovec = NULL;
2380 return ret;
2381 }
2382
Jens Axboe2b188cc2019-01-07 10:46:33 -07002383#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002384 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002385 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2386 iovec, iter);
2387#endif
2388
2389 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2390}
2391
Jens Axboe32960612019-09-23 11:05:34 -06002392/*
2393 * For files that don't have ->read_iter() and ->write_iter(), handle them
2394 * by looping over ->read() or ->write() manually.
2395 */
2396static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2397 struct iov_iter *iter)
2398{
2399 ssize_t ret = 0;
2400
2401 /*
2402 * Don't support polled IO through this interface, and we can't
2403 * support non-blocking either. For the latter, this just causes
2404 * the kiocb to be handled from an async context.
2405 */
2406 if (kiocb->ki_flags & IOCB_HIPRI)
2407 return -EOPNOTSUPP;
2408 if (kiocb->ki_flags & IOCB_NOWAIT)
2409 return -EAGAIN;
2410
2411 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002412 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002413 ssize_t nr;
2414
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002415 if (!iov_iter_is_bvec(iter)) {
2416 iovec = iov_iter_iovec(iter);
2417 } else {
2418 /* fixed buffers import bvec */
2419 iovec.iov_base = kmap(iter->bvec->bv_page)
2420 + iter->iov_offset;
2421 iovec.iov_len = min(iter->count,
2422 iter->bvec->bv_len - iter->iov_offset);
2423 }
2424
Jens Axboe32960612019-09-23 11:05:34 -06002425 if (rw == READ) {
2426 nr = file->f_op->read(file, iovec.iov_base,
2427 iovec.iov_len, &kiocb->ki_pos);
2428 } else {
2429 nr = file->f_op->write(file, iovec.iov_base,
2430 iovec.iov_len, &kiocb->ki_pos);
2431 }
2432
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002433 if (iov_iter_is_bvec(iter))
2434 kunmap(iter->bvec->bv_page);
2435
Jens Axboe32960612019-09-23 11:05:34 -06002436 if (nr < 0) {
2437 if (!ret)
2438 ret = nr;
2439 break;
2440 }
2441 ret += nr;
2442 if (nr != iovec.iov_len)
2443 break;
2444 iov_iter_advance(iter, nr);
2445 }
2446
2447 return ret;
2448}
2449
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002450static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002451 struct iovec *iovec, struct iovec *fast_iov,
2452 struct iov_iter *iter)
2453{
2454 req->io->rw.nr_segs = iter->nr_segs;
2455 req->io->rw.size = io_size;
2456 req->io->rw.iov = iovec;
2457 if (!req->io->rw.iov) {
2458 req->io->rw.iov = req->io->rw.fast_iov;
2459 memcpy(req->io->rw.iov, fast_iov,
2460 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002461 } else {
2462 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002463 }
2464}
2465
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002466static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002467{
Jens Axboed3656342019-12-18 09:50:26 -07002468 if (!io_op_defs[req->opcode].async_ctx)
2469 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002470 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002471 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002472}
2473
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002474static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2475 struct iovec *iovec, struct iovec *fast_iov,
2476 struct iov_iter *iter)
2477{
Jens Axboe980ad262020-01-24 23:08:54 -07002478 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002479 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002480 if (!req->io) {
2481 if (io_alloc_async_ctx(req))
2482 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002483
Jens Axboe5d204bc2020-01-31 12:06:52 -07002484 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2485 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002486 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002487}
2488
Jens Axboe3529d8c2019-12-19 18:24:38 -07002489static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2490 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002491{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002492 struct io_async_ctx *io;
2493 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002494 ssize_t ret;
2495
Jens Axboe3529d8c2019-12-19 18:24:38 -07002496 ret = io_prep_rw(req, sqe, force_nonblock);
2497 if (ret)
2498 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002499
Jens Axboe3529d8c2019-12-19 18:24:38 -07002500 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2501 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002502
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002503 /* either don't need iovec imported or already have it */
2504 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002505 return 0;
2506
2507 io = req->io;
2508 io->rw.iov = io->rw.fast_iov;
2509 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002510 ret = io_import_iovec(READ, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002511 req->io = io;
2512 if (ret < 0)
2513 return ret;
2514
2515 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2516 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002517}
2518
Pavel Begunkov014db002020-03-03 21:33:12 +03002519static int io_read(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002520{
2521 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002522 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002523 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002524 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002525 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002526
Jens Axboebcda7ba2020-02-23 16:42:51 -07002527 ret = io_import_iovec(READ, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07002528 if (ret < 0)
2529 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002530
Jens Axboefd6c2e42019-12-18 12:19:41 -07002531 /* Ensure we clear previously set non-block flag */
2532 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07002533 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002534
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002535 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002536 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002537 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002538 req->result = io_size;
2539
2540 /*
2541 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2542 * we know to async punt it even if it was opened O_NONBLOCK
2543 */
Jens Axboe29de5f62020-02-20 09:56:08 -07002544 if (force_nonblock && !io_file_supports_async(req->file))
Jens Axboef67676d2019-12-02 11:03:47 -07002545 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002546
Jens Axboe31b51512019-01-18 22:56:34 -07002547 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002548 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002549 if (!ret) {
2550 ssize_t ret2;
2551
Jens Axboe9adbd452019-12-20 08:45:55 -07002552 if (req->file->f_op->read_iter)
2553 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002554 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002555 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002556
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002557 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002558 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkov014db002020-03-03 21:33:12 +03002559 kiocb_done(kiocb, ret2);
Jens Axboef67676d2019-12-02 11:03:47 -07002560 } else {
2561copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002562 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002563 inline_vecs, &iter);
2564 if (ret)
2565 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002566 /* any defer here is final, must blocking retry */
2567 if (!(req->flags & REQ_F_NOWAIT))
2568 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002569 return -EAGAIN;
2570 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002571 }
Jens Axboef67676d2019-12-02 11:03:47 -07002572out_free:
Pavel Begunkov1e950812020-02-06 19:51:16 +03002573 kfree(iovec);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002574 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002575 return ret;
2576}
2577
Jens Axboe3529d8c2019-12-19 18:24:38 -07002578static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2579 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002580{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002581 struct io_async_ctx *io;
2582 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002583 ssize_t ret;
2584
Jens Axboe3529d8c2019-12-19 18:24:38 -07002585 ret = io_prep_rw(req, sqe, force_nonblock);
2586 if (ret)
2587 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002588
Jens Axboe3529d8c2019-12-19 18:24:38 -07002589 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2590 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002591
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002592 /* either don't need iovec imported or already have it */
2593 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002594 return 0;
2595
2596 io = req->io;
2597 io->rw.iov = io->rw.fast_iov;
2598 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002599 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002600 req->io = io;
2601 if (ret < 0)
2602 return ret;
2603
2604 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2605 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002606}
2607
Pavel Begunkov014db002020-03-03 21:33:12 +03002608static int io_write(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002609{
2610 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002611 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002612 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002613 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002614 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002615
Jens Axboebcda7ba2020-02-23 16:42:51 -07002616 ret = io_import_iovec(WRITE, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07002617 if (ret < 0)
2618 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002619
Jens Axboefd6c2e42019-12-18 12:19:41 -07002620 /* Ensure we clear previously set non-block flag */
2621 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002622 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002623
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002624 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002625 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002626 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002627 req->result = io_size;
2628
2629 /*
2630 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2631 * we know to async punt it even if it was opened O_NONBLOCK
2632 */
Jens Axboe29de5f62020-02-20 09:56:08 -07002633 if (force_nonblock && !io_file_supports_async(req->file))
Jens Axboef67676d2019-12-02 11:03:47 -07002634 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07002635
Jens Axboe10d59342019-12-09 20:16:22 -07002636 /* file path doesn't support NOWAIT for non-direct_IO */
2637 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2638 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002639 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002640
Jens Axboe31b51512019-01-18 22:56:34 -07002641 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002642 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002643 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002644 ssize_t ret2;
2645
Jens Axboe2b188cc2019-01-07 10:46:33 -07002646 /*
2647 * Open-code file_start_write here to grab freeze protection,
2648 * which will be released by another thread in
2649 * io_complete_rw(). Fool lockdep by telling it the lock got
2650 * released so that it doesn't complain about the held lock when
2651 * we return to userspace.
2652 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002653 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002654 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002655 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002656 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002657 SB_FREEZE_WRITE);
2658 }
2659 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002660
Jens Axboe9adbd452019-12-20 08:45:55 -07002661 if (req->file->f_op->write_iter)
2662 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002663 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002664 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboefaac9962020-02-07 15:45:22 -07002665 /*
2666 * Raw bdev writes will -EOPNOTSUPP for IOCB_NOWAIT. Just
2667 * retry them without IOCB_NOWAIT.
2668 */
2669 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
2670 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07002671 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkov014db002020-03-03 21:33:12 +03002672 kiocb_done(kiocb, ret2);
Jens Axboef67676d2019-12-02 11:03:47 -07002673 } else {
2674copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002675 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002676 inline_vecs, &iter);
2677 if (ret)
2678 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002679 /* any defer here is final, must blocking retry */
2680 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002681 return -EAGAIN;
2682 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002683 }
Jens Axboe31b51512019-01-18 22:56:34 -07002684out_free:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002685 req->flags &= ~REQ_F_NEED_CLEANUP;
Pavel Begunkov1e950812020-02-06 19:51:16 +03002686 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002687 return ret;
2688}
2689
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002690static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2691{
2692 struct io_splice* sp = &req->splice;
2693 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
2694 int ret;
2695
2696 if (req->flags & REQ_F_NEED_CLEANUP)
2697 return 0;
2698
2699 sp->file_in = NULL;
2700 sp->off_in = READ_ONCE(sqe->splice_off_in);
2701 sp->off_out = READ_ONCE(sqe->off);
2702 sp->len = READ_ONCE(sqe->len);
2703 sp->flags = READ_ONCE(sqe->splice_flags);
2704
2705 if (unlikely(sp->flags & ~valid_flags))
2706 return -EINVAL;
2707
2708 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
2709 (sp->flags & SPLICE_F_FD_IN_FIXED));
2710 if (ret)
2711 return ret;
2712 req->flags |= REQ_F_NEED_CLEANUP;
2713
2714 if (!S_ISREG(file_inode(sp->file_in)->i_mode))
2715 req->work.flags |= IO_WQ_WORK_UNBOUND;
2716
2717 return 0;
2718}
2719
2720static bool io_splice_punt(struct file *file)
2721{
2722 if (get_pipe_info(file))
2723 return false;
2724 if (!io_file_supports_async(file))
2725 return true;
2726 return !(file->f_mode & O_NONBLOCK);
2727}
2728
Pavel Begunkov014db002020-03-03 21:33:12 +03002729static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002730{
2731 struct io_splice *sp = &req->splice;
2732 struct file *in = sp->file_in;
2733 struct file *out = sp->file_out;
2734 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
2735 loff_t *poff_in, *poff_out;
2736 long ret;
2737
2738 if (force_nonblock) {
2739 if (io_splice_punt(in) || io_splice_punt(out))
2740 return -EAGAIN;
2741 flags |= SPLICE_F_NONBLOCK;
2742 }
2743
2744 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
2745 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
2746 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
2747 if (force_nonblock && ret == -EAGAIN)
2748 return -EAGAIN;
2749
2750 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
2751 req->flags &= ~REQ_F_NEED_CLEANUP;
2752
2753 io_cqring_add_event(req, ret);
2754 if (ret != sp->len)
2755 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03002756 io_put_req(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002757 return 0;
2758}
2759
Jens Axboe2b188cc2019-01-07 10:46:33 -07002760/*
2761 * IORING_OP_NOP just posts a completion event, nothing else.
2762 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002763static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002764{
2765 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002766
Jens Axboedef596e2019-01-09 08:59:42 -07002767 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2768 return -EINVAL;
2769
Jens Axboe78e19bb2019-11-06 15:21:34 -07002770 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002771 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002772 return 0;
2773}
2774
Jens Axboe3529d8c2019-12-19 18:24:38 -07002775static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002776{
Jens Axboe6b063142019-01-10 22:13:58 -07002777 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002778
Jens Axboe09bb8392019-03-13 12:39:28 -06002779 if (!req->file)
2780 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002781
Jens Axboe6b063142019-01-10 22:13:58 -07002782 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002783 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002784 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002785 return -EINVAL;
2786
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002787 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2788 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2789 return -EINVAL;
2790
2791 req->sync.off = READ_ONCE(sqe->off);
2792 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002793 return 0;
2794}
2795
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002796static bool io_req_cancelled(struct io_kiocb *req)
2797{
2798 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2799 req_set_fail_links(req);
2800 io_cqring_add_event(req, -ECANCELED);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03002801 io_put_req(req);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002802 return true;
2803 }
2804
2805 return false;
2806}
2807
Pavel Begunkov014db002020-03-03 21:33:12 +03002808static void __io_fsync(struct io_kiocb *req)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002809{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002810 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002811 int ret;
2812
Jens Axboe9adbd452019-12-20 08:45:55 -07002813 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002814 end > 0 ? end : LLONG_MAX,
2815 req->sync.flags & IORING_FSYNC_DATASYNC);
2816 if (ret < 0)
2817 req_set_fail_links(req);
2818 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002819 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002820}
2821
2822static void io_fsync_finish(struct io_wq_work **workptr)
2823{
2824 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002825
2826 if (io_req_cancelled(req))
2827 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03002828 __io_fsync(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03002829 io_steal_work(req, workptr);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002830}
2831
Pavel Begunkov014db002020-03-03 21:33:12 +03002832static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002833{
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002834 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002835 if (force_nonblock) {
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002836 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002837 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002838 }
Pavel Begunkov014db002020-03-03 21:33:12 +03002839 __io_fsync(req);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002840 return 0;
2841}
2842
Pavel Begunkov014db002020-03-03 21:33:12 +03002843static void __io_fallocate(struct io_kiocb *req)
Jens Axboed63d1b52019-12-10 10:38:56 -07002844{
Jens Axboed63d1b52019-12-10 10:38:56 -07002845 int ret;
2846
2847 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2848 req->sync.len);
2849 if (ret < 0)
2850 req_set_fail_links(req);
2851 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002852 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002853}
2854
2855static void io_fallocate_finish(struct io_wq_work **workptr)
2856{
2857 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002858
Pavel Begunkov594506f2020-03-03 21:33:11 +03002859 if (io_req_cancelled(req))
2860 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03002861 __io_fallocate(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03002862 io_steal_work(req, workptr);
Jens Axboed63d1b52019-12-10 10:38:56 -07002863}
2864
2865static int io_fallocate_prep(struct io_kiocb *req,
2866 const struct io_uring_sqe *sqe)
2867{
2868 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2869 return -EINVAL;
2870
2871 req->sync.off = READ_ONCE(sqe->off);
2872 req->sync.len = READ_ONCE(sqe->addr);
2873 req->sync.mode = READ_ONCE(sqe->len);
2874 return 0;
2875}
2876
Pavel Begunkov014db002020-03-03 21:33:12 +03002877static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07002878{
Jens Axboed63d1b52019-12-10 10:38:56 -07002879 /* fallocate always requiring blocking context */
2880 if (force_nonblock) {
Jens Axboed63d1b52019-12-10 10:38:56 -07002881 req->work.func = io_fallocate_finish;
2882 return -EAGAIN;
2883 }
2884
Pavel Begunkov014db002020-03-03 21:33:12 +03002885 __io_fallocate(req);
Jens Axboed63d1b52019-12-10 10:38:56 -07002886 return 0;
2887}
2888
Jens Axboe15b71ab2019-12-11 11:20:36 -07002889static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2890{
Jens Axboef8748882020-01-08 17:47:02 -07002891 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002892 int ret;
2893
2894 if (sqe->ioprio || sqe->buf_index)
2895 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002896 if (sqe->flags & IOSQE_FIXED_FILE)
2897 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002898 if (req->flags & REQ_F_NEED_CLEANUP)
2899 return 0;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002900
2901 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002902 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002903 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002904 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002905
Jens Axboef8748882020-01-08 17:47:02 -07002906 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002907 if (IS_ERR(req->open.filename)) {
2908 ret = PTR_ERR(req->open.filename);
2909 req->open.filename = NULL;
2910 return ret;
2911 }
2912
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002913 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002914 return 0;
2915}
2916
Jens Axboecebdb982020-01-08 17:59:24 -07002917static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2918{
2919 struct open_how __user *how;
2920 const char __user *fname;
2921 size_t len;
2922 int ret;
2923
2924 if (sqe->ioprio || sqe->buf_index)
2925 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002926 if (sqe->flags & IOSQE_FIXED_FILE)
2927 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002928 if (req->flags & REQ_F_NEED_CLEANUP)
2929 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07002930
2931 req->open.dfd = READ_ONCE(sqe->fd);
2932 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2933 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2934 len = READ_ONCE(sqe->len);
2935
2936 if (len < OPEN_HOW_SIZE_VER0)
2937 return -EINVAL;
2938
2939 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2940 len);
2941 if (ret)
2942 return ret;
2943
2944 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2945 req->open.how.flags |= O_LARGEFILE;
2946
2947 req->open.filename = getname(fname);
2948 if (IS_ERR(req->open.filename)) {
2949 ret = PTR_ERR(req->open.filename);
2950 req->open.filename = NULL;
2951 return ret;
2952 }
2953
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002954 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboecebdb982020-01-08 17:59:24 -07002955 return 0;
2956}
2957
Pavel Begunkov014db002020-03-03 21:33:12 +03002958static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002959{
2960 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002961 struct file *file;
2962 int ret;
2963
Jens Axboef86cd202020-01-29 13:46:44 -07002964 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002965 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002966
Jens Axboecebdb982020-01-08 17:59:24 -07002967 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002968 if (ret)
2969 goto err;
2970
Jens Axboecebdb982020-01-08 17:59:24 -07002971 ret = get_unused_fd_flags(req->open.how.flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002972 if (ret < 0)
2973 goto err;
2974
2975 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2976 if (IS_ERR(file)) {
2977 put_unused_fd(ret);
2978 ret = PTR_ERR(file);
2979 } else {
2980 fsnotify_open(file);
2981 fd_install(ret, file);
2982 }
2983err:
2984 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002985 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002986 if (ret < 0)
2987 req_set_fail_links(req);
2988 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002989 io_put_req(req);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002990 return 0;
2991}
2992
Pavel Begunkov014db002020-03-03 21:33:12 +03002993static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07002994{
2995 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
Pavel Begunkov014db002020-03-03 21:33:12 +03002996 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07002997}
2998
Jens Axboe067524e2020-03-02 16:32:28 -07002999static int io_remove_buffers_prep(struct io_kiocb *req,
3000 const struct io_uring_sqe *sqe)
3001{
3002 struct io_provide_buf *p = &req->pbuf;
3003 u64 tmp;
3004
3005 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3006 return -EINVAL;
3007
3008 tmp = READ_ONCE(sqe->fd);
3009 if (!tmp || tmp > USHRT_MAX)
3010 return -EINVAL;
3011
3012 memset(p, 0, sizeof(*p));
3013 p->nbufs = tmp;
3014 p->bgid = READ_ONCE(sqe->buf_group);
3015 return 0;
3016}
3017
3018static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3019 int bgid, unsigned nbufs)
3020{
3021 unsigned i = 0;
3022
3023 /* shouldn't happen */
3024 if (!nbufs)
3025 return 0;
3026
3027 /* the head kbuf is the list itself */
3028 while (!list_empty(&buf->list)) {
3029 struct io_buffer *nxt;
3030
3031 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3032 list_del(&nxt->list);
3033 kfree(nxt);
3034 if (++i == nbufs)
3035 return i;
3036 }
3037 i++;
3038 kfree(buf);
3039 idr_remove(&ctx->io_buffer_idr, bgid);
3040
3041 return i;
3042}
3043
3044static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock)
3045{
3046 struct io_provide_buf *p = &req->pbuf;
3047 struct io_ring_ctx *ctx = req->ctx;
3048 struct io_buffer *head;
3049 int ret = 0;
3050
3051 io_ring_submit_lock(ctx, !force_nonblock);
3052
3053 lockdep_assert_held(&ctx->uring_lock);
3054
3055 ret = -ENOENT;
3056 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3057 if (head)
3058 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3059
3060 io_ring_submit_lock(ctx, !force_nonblock);
3061 if (ret < 0)
3062 req_set_fail_links(req);
3063 io_cqring_add_event(req, ret);
3064 io_put_req(req);
3065 return 0;
3066}
3067
Jens Axboeddf0322d2020-02-23 16:41:33 -07003068static int io_provide_buffers_prep(struct io_kiocb *req,
3069 const struct io_uring_sqe *sqe)
3070{
3071 struct io_provide_buf *p = &req->pbuf;
3072 u64 tmp;
3073
3074 if (sqe->ioprio || sqe->rw_flags)
3075 return -EINVAL;
3076
3077 tmp = READ_ONCE(sqe->fd);
3078 if (!tmp || tmp > USHRT_MAX)
3079 return -E2BIG;
3080 p->nbufs = tmp;
3081 p->addr = READ_ONCE(sqe->addr);
3082 p->len = READ_ONCE(sqe->len);
3083
3084 if (!access_ok(u64_to_user_ptr(p->addr), p->len))
3085 return -EFAULT;
3086
3087 p->bgid = READ_ONCE(sqe->buf_group);
3088 tmp = READ_ONCE(sqe->off);
3089 if (tmp > USHRT_MAX)
3090 return -E2BIG;
3091 p->bid = tmp;
3092 return 0;
3093}
3094
3095static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3096{
3097 struct io_buffer *buf;
3098 u64 addr = pbuf->addr;
3099 int i, bid = pbuf->bid;
3100
3101 for (i = 0; i < pbuf->nbufs; i++) {
3102 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3103 if (!buf)
3104 break;
3105
3106 buf->addr = addr;
3107 buf->len = pbuf->len;
3108 buf->bid = bid;
3109 addr += pbuf->len;
3110 bid++;
3111 if (!*head) {
3112 INIT_LIST_HEAD(&buf->list);
3113 *head = buf;
3114 } else {
3115 list_add_tail(&buf->list, &(*head)->list);
3116 }
3117 }
3118
3119 return i ? i : -ENOMEM;
3120}
3121
Jens Axboeddf0322d2020-02-23 16:41:33 -07003122static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock)
3123{
3124 struct io_provide_buf *p = &req->pbuf;
3125 struct io_ring_ctx *ctx = req->ctx;
3126 struct io_buffer *head, *list;
3127 int ret = 0;
3128
3129 io_ring_submit_lock(ctx, !force_nonblock);
3130
3131 lockdep_assert_held(&ctx->uring_lock);
3132
3133 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3134
3135 ret = io_add_buffers(p, &head);
3136 if (ret < 0)
3137 goto out;
3138
3139 if (!list) {
3140 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3141 GFP_KERNEL);
3142 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003143 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003144 goto out;
3145 }
3146 }
3147out:
3148 io_ring_submit_unlock(ctx, !force_nonblock);
3149 if (ret < 0)
3150 req_set_fail_links(req);
3151 io_cqring_add_event(req, ret);
3152 io_put_req(req);
3153 return 0;
3154}
3155
Jens Axboe3e4827b2020-01-08 15:18:09 -07003156static int io_epoll_ctl_prep(struct io_kiocb *req,
3157 const struct io_uring_sqe *sqe)
3158{
3159#if defined(CONFIG_EPOLL)
3160 if (sqe->ioprio || sqe->buf_index)
3161 return -EINVAL;
3162
3163 req->epoll.epfd = READ_ONCE(sqe->fd);
3164 req->epoll.op = READ_ONCE(sqe->len);
3165 req->epoll.fd = READ_ONCE(sqe->off);
3166
3167 if (ep_op_has_event(req->epoll.op)) {
3168 struct epoll_event __user *ev;
3169
3170 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3171 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3172 return -EFAULT;
3173 }
3174
3175 return 0;
3176#else
3177 return -EOPNOTSUPP;
3178#endif
3179}
3180
Pavel Begunkov014db002020-03-03 21:33:12 +03003181static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003182{
3183#if defined(CONFIG_EPOLL)
3184 struct io_epoll *ie = &req->epoll;
3185 int ret;
3186
3187 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3188 if (force_nonblock && ret == -EAGAIN)
3189 return -EAGAIN;
3190
3191 if (ret < 0)
3192 req_set_fail_links(req);
3193 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003194 io_put_req(req);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003195 return 0;
3196#else
3197 return -EOPNOTSUPP;
3198#endif
3199}
3200
Jens Axboec1ca7572019-12-25 22:18:28 -07003201static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3202{
3203#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3204 if (sqe->ioprio || sqe->buf_index || sqe->off)
3205 return -EINVAL;
3206
3207 req->madvise.addr = READ_ONCE(sqe->addr);
3208 req->madvise.len = READ_ONCE(sqe->len);
3209 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3210 return 0;
3211#else
3212 return -EOPNOTSUPP;
3213#endif
3214}
3215
Pavel Begunkov014db002020-03-03 21:33:12 +03003216static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003217{
3218#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3219 struct io_madvise *ma = &req->madvise;
3220 int ret;
3221
3222 if (force_nonblock)
3223 return -EAGAIN;
3224
3225 ret = do_madvise(ma->addr, ma->len, ma->advice);
3226 if (ret < 0)
3227 req_set_fail_links(req);
3228 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003229 io_put_req(req);
Jens Axboec1ca7572019-12-25 22:18:28 -07003230 return 0;
3231#else
3232 return -EOPNOTSUPP;
3233#endif
3234}
3235
Jens Axboe4840e412019-12-25 22:03:45 -07003236static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3237{
3238 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3239 return -EINVAL;
3240
3241 req->fadvise.offset = READ_ONCE(sqe->off);
3242 req->fadvise.len = READ_ONCE(sqe->len);
3243 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3244 return 0;
3245}
3246
Pavel Begunkov014db002020-03-03 21:33:12 +03003247static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07003248{
3249 struct io_fadvise *fa = &req->fadvise;
3250 int ret;
3251
Jens Axboe3e694262020-02-01 09:22:49 -07003252 if (force_nonblock) {
3253 switch (fa->advice) {
3254 case POSIX_FADV_NORMAL:
3255 case POSIX_FADV_RANDOM:
3256 case POSIX_FADV_SEQUENTIAL:
3257 break;
3258 default:
3259 return -EAGAIN;
3260 }
3261 }
Jens Axboe4840e412019-12-25 22:03:45 -07003262
3263 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3264 if (ret < 0)
3265 req_set_fail_links(req);
3266 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003267 io_put_req(req);
Jens Axboe4840e412019-12-25 22:03:45 -07003268 return 0;
3269}
3270
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003271static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3272{
Jens Axboef8748882020-01-08 17:47:02 -07003273 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003274 unsigned lookup_flags;
3275 int ret;
3276
3277 if (sqe->ioprio || sqe->buf_index)
3278 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07003279 if (sqe->flags & IOSQE_FIXED_FILE)
3280 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003281 if (req->flags & REQ_F_NEED_CLEANUP)
3282 return 0;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003283
3284 req->open.dfd = READ_ONCE(sqe->fd);
3285 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07003286 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003287 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07003288 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003289
Jens Axboec12cedf2020-01-08 17:41:21 -07003290 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003291 return -EINVAL;
3292
Jens Axboef8748882020-01-08 17:47:02 -07003293 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003294 if (IS_ERR(req->open.filename)) {
3295 ret = PTR_ERR(req->open.filename);
3296 req->open.filename = NULL;
3297 return ret;
3298 }
3299
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003300 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003301 return 0;
3302}
3303
Pavel Begunkov014db002020-03-03 21:33:12 +03003304static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003305{
3306 struct io_open *ctx = &req->open;
3307 unsigned lookup_flags;
3308 struct path path;
3309 struct kstat stat;
3310 int ret;
3311
3312 if (force_nonblock)
3313 return -EAGAIN;
3314
Jens Axboec12cedf2020-01-08 17:41:21 -07003315 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003316 return -EINVAL;
3317
3318retry:
3319 /* filename_lookup() drops it, keep a reference */
3320 ctx->filename->refcnt++;
3321
3322 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
3323 NULL);
3324 if (ret)
3325 goto err;
3326
Jens Axboec12cedf2020-01-08 17:41:21 -07003327 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003328 path_put(&path);
3329 if (retry_estale(ret, lookup_flags)) {
3330 lookup_flags |= LOOKUP_REVAL;
3331 goto retry;
3332 }
3333 if (!ret)
3334 ret = cp_statx(&stat, ctx->buffer);
3335err:
3336 putname(ctx->filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003337 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003338 if (ret < 0)
3339 req_set_fail_links(req);
3340 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003341 io_put_req(req);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003342 return 0;
3343}
3344
Jens Axboeb5dba592019-12-11 14:02:38 -07003345static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3346{
3347 /*
3348 * If we queue this for async, it must not be cancellable. That would
3349 * leave the 'file' in an undeterminate state.
3350 */
3351 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
3352
3353 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
3354 sqe->rw_flags || sqe->buf_index)
3355 return -EINVAL;
3356 if (sqe->flags & IOSQE_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003357 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07003358
3359 req->close.fd = READ_ONCE(sqe->fd);
3360 if (req->file->f_op == &io_uring_fops ||
Pavel Begunkovb14cca02020-01-17 04:45:59 +03003361 req->close.fd == req->ctx->ring_fd)
Jens Axboeb5dba592019-12-11 14:02:38 -07003362 return -EBADF;
3363
3364 return 0;
3365}
3366
Pavel Begunkova93b3332020-02-08 14:04:34 +03003367/* only called when __close_fd_get_file() is done */
Pavel Begunkov014db002020-03-03 21:33:12 +03003368static void __io_close_finish(struct io_kiocb *req)
Pavel Begunkova93b3332020-02-08 14:04:34 +03003369{
3370 int ret;
3371
3372 ret = filp_close(req->close.put_file, req->work.files);
3373 if (ret < 0)
3374 req_set_fail_links(req);
3375 io_cqring_add_event(req, ret);
3376 fput(req->close.put_file);
Pavel Begunkov014db002020-03-03 21:33:12 +03003377 io_put_req(req);
Pavel Begunkova93b3332020-02-08 14:04:34 +03003378}
3379
Jens Axboeb5dba592019-12-11 14:02:38 -07003380static void io_close_finish(struct io_wq_work **workptr)
3381{
3382 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Jens Axboeb5dba592019-12-11 14:02:38 -07003383
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03003384 /* not cancellable, don't do io_req_cancelled() */
Pavel Begunkov014db002020-03-03 21:33:12 +03003385 __io_close_finish(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03003386 io_steal_work(req, workptr);
Jens Axboeb5dba592019-12-11 14:02:38 -07003387}
3388
Pavel Begunkov014db002020-03-03 21:33:12 +03003389static int io_close(struct io_kiocb *req, bool force_nonblock)
Jens Axboeb5dba592019-12-11 14:02:38 -07003390{
3391 int ret;
3392
3393 req->close.put_file = NULL;
3394 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
3395 if (ret < 0)
3396 return ret;
3397
3398 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkova2100672020-03-02 23:45:16 +03003399 if (req->close.put_file->f_op->flush && force_nonblock) {
Pavel Begunkov594506f2020-03-03 21:33:11 +03003400 /* submission ref will be dropped, take it for async */
3401 refcount_inc(&req->refs);
3402
Pavel Begunkova2100672020-03-02 23:45:16 +03003403 req->work.func = io_close_finish;
3404 /*
3405 * Do manual async queue here to avoid grabbing files - we don't
3406 * need the files, and it'll cause io_close_finish() to close
3407 * the file again and cause a double CQE entry for this request
3408 */
3409 io_queue_async_work(req);
3410 return 0;
3411 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003412
3413 /*
3414 * No ->flush(), safely close from here and just punt the
3415 * fput() to async context.
3416 */
Pavel Begunkov014db002020-03-03 21:33:12 +03003417 __io_close_finish(req);
Pavel Begunkova93b3332020-02-08 14:04:34 +03003418 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07003419}
3420
Jens Axboe3529d8c2019-12-19 18:24:38 -07003421static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003422{
3423 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003424
3425 if (!req->file)
3426 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003427
3428 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3429 return -EINVAL;
3430 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
3431 return -EINVAL;
3432
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003433 req->sync.off = READ_ONCE(sqe->off);
3434 req->sync.len = READ_ONCE(sqe->len);
3435 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003436 return 0;
3437}
3438
Pavel Begunkov014db002020-03-03 21:33:12 +03003439static void __io_sync_file_range(struct io_kiocb *req)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003440{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003441 int ret;
3442
Jens Axboe9adbd452019-12-20 08:45:55 -07003443 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003444 req->sync.flags);
3445 if (ret < 0)
3446 req_set_fail_links(req);
3447 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003448 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03003449}
3450
3451
3452static void io_sync_file_range_finish(struct io_wq_work **workptr)
3453{
3454 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3455 struct io_kiocb *nxt = NULL;
3456
3457 if (io_req_cancelled(req))
3458 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03003459 __io_sync_file_range(req);
Pavel Begunkov594506f2020-03-03 21:33:11 +03003460 io_put_req(req); /* put submission ref */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003461 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003462 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003463}
3464
Pavel Begunkov014db002020-03-03 21:33:12 +03003465static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003466{
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003467 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003468 if (force_nonblock) {
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003469 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003470 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003471 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003472
Pavel Begunkov014db002020-03-03 21:33:12 +03003473 __io_sync_file_range(req);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003474 return 0;
3475}
3476
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003477static int io_setup_async_msg(struct io_kiocb *req,
3478 struct io_async_msghdr *kmsg)
3479{
3480 if (req->io)
3481 return -EAGAIN;
3482 if (io_alloc_async_ctx(req)) {
3483 if (kmsg->iov != kmsg->fast_iov)
3484 kfree(kmsg->iov);
3485 return -ENOMEM;
3486 }
3487 req->flags |= REQ_F_NEED_CLEANUP;
3488 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
3489 return -EAGAIN;
3490}
3491
Jens Axboe3529d8c2019-12-19 18:24:38 -07003492static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06003493{
Jens Axboe03b12302019-12-02 18:50:25 -07003494#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003495 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003496 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003497 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003498
Jens Axboee47293f2019-12-20 08:58:21 -07003499 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3500 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07003501 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003502
Jens Axboed8768362020-02-27 14:17:49 -07003503#ifdef CONFIG_COMPAT
3504 if (req->ctx->compat)
3505 sr->msg_flags |= MSG_CMSG_COMPAT;
3506#endif
3507
Jens Axboefddafac2020-01-04 20:19:44 -07003508 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003509 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003510 /* iovec is already imported */
3511 if (req->flags & REQ_F_NEED_CLEANUP)
3512 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003513
Jens Axboed9688562019-12-09 19:35:20 -07003514 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003515 ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003516 &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003517 if (!ret)
3518 req->flags |= REQ_F_NEED_CLEANUP;
3519 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003520#else
Jens Axboee47293f2019-12-20 08:58:21 -07003521 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003522#endif
3523}
3524
Pavel Begunkov014db002020-03-03 21:33:12 +03003525static int io_sendmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003526{
3527#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003528 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07003529 struct socket *sock;
3530 int ret;
3531
3532 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3533 return -EINVAL;
3534
3535 sock = sock_from_file(req->file, &ret);
3536 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003537 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003538 unsigned flags;
3539
Jens Axboe03b12302019-12-02 18:50:25 -07003540 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003541 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003542 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003543 /* if iov is set, it's allocated already */
3544 if (!kmsg->iov)
3545 kmsg->iov = kmsg->fast_iov;
3546 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003547 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003548 struct io_sr_msg *sr = &req->sr_msg;
3549
Jens Axboe0b416c32019-12-15 10:57:46 -07003550 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003551 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003552
3553 io.msg.iov = io.msg.fast_iov;
3554 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3555 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003556 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003557 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003558 }
3559
Jens Axboee47293f2019-12-20 08:58:21 -07003560 flags = req->sr_msg.msg_flags;
3561 if (flags & MSG_DONTWAIT)
3562 req->flags |= REQ_F_NOWAIT;
3563 else if (force_nonblock)
3564 flags |= MSG_DONTWAIT;
3565
Jens Axboe0b416c32019-12-15 10:57:46 -07003566 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003567 if (force_nonblock && ret == -EAGAIN)
3568 return io_setup_async_msg(req, kmsg);
Jens Axboe03b12302019-12-02 18:50:25 -07003569 if (ret == -ERESTARTSYS)
3570 ret = -EINTR;
3571 }
3572
Pavel Begunkov1e950812020-02-06 19:51:16 +03003573 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003574 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003575 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe03b12302019-12-02 18:50:25 -07003576 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003577 if (ret < 0)
3578 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003579 io_put_req(req);
Jens Axboe03b12302019-12-02 18:50:25 -07003580 return 0;
3581#else
3582 return -EOPNOTSUPP;
3583#endif
3584}
3585
Pavel Begunkov014db002020-03-03 21:33:12 +03003586static int io_send(struct io_kiocb *req, bool force_nonblock)
Jens Axboefddafac2020-01-04 20:19:44 -07003587{
3588#if defined(CONFIG_NET)
3589 struct socket *sock;
3590 int ret;
3591
3592 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3593 return -EINVAL;
3594
3595 sock = sock_from_file(req->file, &ret);
3596 if (sock) {
3597 struct io_sr_msg *sr = &req->sr_msg;
3598 struct msghdr msg;
3599 struct iovec iov;
3600 unsigned flags;
3601
3602 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3603 &msg.msg_iter);
3604 if (ret)
3605 return ret;
3606
3607 msg.msg_name = NULL;
3608 msg.msg_control = NULL;
3609 msg.msg_controllen = 0;
3610 msg.msg_namelen = 0;
3611
3612 flags = req->sr_msg.msg_flags;
3613 if (flags & MSG_DONTWAIT)
3614 req->flags |= REQ_F_NOWAIT;
3615 else if (force_nonblock)
3616 flags |= MSG_DONTWAIT;
3617
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003618 msg.msg_flags = flags;
3619 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003620 if (force_nonblock && ret == -EAGAIN)
3621 return -EAGAIN;
3622 if (ret == -ERESTARTSYS)
3623 ret = -EINTR;
3624 }
3625
3626 io_cqring_add_event(req, ret);
3627 if (ret < 0)
3628 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003629 io_put_req(req);
Jens Axboefddafac2020-01-04 20:19:44 -07003630 return 0;
3631#else
3632 return -EOPNOTSUPP;
3633#endif
3634}
3635
Jens Axboe52de1fe2020-02-27 10:15:42 -07003636static int __io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
3637{
3638 struct io_sr_msg *sr = &req->sr_msg;
3639 struct iovec __user *uiov;
3640 size_t iov_len;
3641 int ret;
3642
3643 ret = __copy_msghdr_from_user(&io->msg.msg, sr->msg, &io->msg.uaddr,
3644 &uiov, &iov_len);
3645 if (ret)
3646 return ret;
3647
3648 if (req->flags & REQ_F_BUFFER_SELECT) {
3649 if (iov_len > 1)
3650 return -EINVAL;
3651 if (copy_from_user(io->msg.iov, uiov, sizeof(*uiov)))
3652 return -EFAULT;
3653 sr->len = io->msg.iov[0].iov_len;
3654 iov_iter_init(&io->msg.msg.msg_iter, READ, io->msg.iov, 1,
3655 sr->len);
3656 io->msg.iov = NULL;
3657 } else {
3658 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
3659 &io->msg.iov, &io->msg.msg.msg_iter);
3660 if (ret > 0)
3661 ret = 0;
3662 }
3663
3664 return ret;
3665}
3666
3667#ifdef CONFIG_COMPAT
3668static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
3669 struct io_async_ctx *io)
3670{
3671 struct compat_msghdr __user *msg_compat;
3672 struct io_sr_msg *sr = &req->sr_msg;
3673 struct compat_iovec __user *uiov;
3674 compat_uptr_t ptr;
3675 compat_size_t len;
3676 int ret;
3677
3678 msg_compat = (struct compat_msghdr __user *) sr->msg;
3679 ret = __get_compat_msghdr(&io->msg.msg, msg_compat, &io->msg.uaddr,
3680 &ptr, &len);
3681 if (ret)
3682 return ret;
3683
3684 uiov = compat_ptr(ptr);
3685 if (req->flags & REQ_F_BUFFER_SELECT) {
3686 compat_ssize_t clen;
3687
3688 if (len > 1)
3689 return -EINVAL;
3690 if (!access_ok(uiov, sizeof(*uiov)))
3691 return -EFAULT;
3692 if (__get_user(clen, &uiov->iov_len))
3693 return -EFAULT;
3694 if (clen < 0)
3695 return -EINVAL;
3696 sr->len = io->msg.iov[0].iov_len;
3697 io->msg.iov = NULL;
3698 } else {
3699 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
3700 &io->msg.iov,
3701 &io->msg.msg.msg_iter);
3702 if (ret < 0)
3703 return ret;
3704 }
3705
3706 return 0;
3707}
3708#endif
3709
3710static int io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
3711{
3712 io->msg.iov = io->msg.fast_iov;
3713
3714#ifdef CONFIG_COMPAT
3715 if (req->ctx->compat)
3716 return __io_compat_recvmsg_copy_hdr(req, io);
3717#endif
3718
3719 return __io_recvmsg_copy_hdr(req, io);
3720}
3721
Jens Axboebcda7ba2020-02-23 16:42:51 -07003722static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
3723 int *cflags, bool needs_lock)
3724{
3725 struct io_sr_msg *sr = &req->sr_msg;
3726 struct io_buffer *kbuf;
3727
3728 if (!(req->flags & REQ_F_BUFFER_SELECT))
3729 return NULL;
3730
3731 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
3732 if (IS_ERR(kbuf))
3733 return kbuf;
3734
3735 sr->kbuf = kbuf;
3736 req->flags |= REQ_F_BUFFER_SELECTED;
3737
3738 *cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
3739 *cflags |= IORING_CQE_F_BUFFER;
3740 return kbuf;
3741}
3742
Jens Axboe3529d8c2019-12-19 18:24:38 -07003743static int io_recvmsg_prep(struct io_kiocb *req,
3744 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003745{
3746#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003747 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003748 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003749 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07003750
Jens Axboe3529d8c2019-12-19 18:24:38 -07003751 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3752 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003753 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003754 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003755
Jens Axboed8768362020-02-27 14:17:49 -07003756#ifdef CONFIG_COMPAT
3757 if (req->ctx->compat)
3758 sr->msg_flags |= MSG_CMSG_COMPAT;
3759#endif
3760
Jens Axboefddafac2020-01-04 20:19:44 -07003761 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003762 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003763 /* iovec is already imported */
3764 if (req->flags & REQ_F_NEED_CLEANUP)
3765 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003766
Jens Axboe52de1fe2020-02-27 10:15:42 -07003767 ret = io_recvmsg_copy_hdr(req, io);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003768 if (!ret)
3769 req->flags |= REQ_F_NEED_CLEANUP;
3770 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003771#else
Jens Axboee47293f2019-12-20 08:58:21 -07003772 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003773#endif
3774}
3775
Pavel Begunkov014db002020-03-03 21:33:12 +03003776static int io_recvmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003777{
3778#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003779 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003780 struct socket *sock;
Jens Axboe52de1fe2020-02-27 10:15:42 -07003781 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003782
3783 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3784 return -EINVAL;
3785
3786 sock = sock_from_file(req->file, &ret);
3787 if (sock) {
Jens Axboe52de1fe2020-02-27 10:15:42 -07003788 struct io_buffer *kbuf;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003789 struct io_async_ctx io;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003790 unsigned flags;
3791
Jens Axboe03b12302019-12-02 18:50:25 -07003792 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003793 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003794 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003795 /* if iov is set, it's allocated already */
3796 if (!kmsg->iov)
3797 kmsg->iov = kmsg->fast_iov;
3798 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003799 } else {
Jens Axboe0b416c32019-12-15 10:57:46 -07003800 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003801 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003802
Jens Axboe52de1fe2020-02-27 10:15:42 -07003803 ret = io_recvmsg_copy_hdr(req, &io);
Jens Axboe03b12302019-12-02 18:50:25 -07003804 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003805 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003806 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003807
Jens Axboe52de1fe2020-02-27 10:15:42 -07003808 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
3809 if (IS_ERR(kbuf)) {
3810 return PTR_ERR(kbuf);
3811 } else if (kbuf) {
3812 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3813 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
3814 1, req->sr_msg.len);
3815 }
3816
Jens Axboee47293f2019-12-20 08:58:21 -07003817 flags = req->sr_msg.msg_flags;
3818 if (flags & MSG_DONTWAIT)
3819 req->flags |= REQ_F_NOWAIT;
3820 else if (force_nonblock)
3821 flags |= MSG_DONTWAIT;
3822
3823 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3824 kmsg->uaddr, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003825 if (force_nonblock && ret == -EAGAIN)
3826 return io_setup_async_msg(req, kmsg);
Jens Axboe441cdbd2019-12-02 18:49:10 -07003827 if (ret == -ERESTARTSYS)
3828 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003829 }
3830
Pavel Begunkov1e950812020-02-06 19:51:16 +03003831 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003832 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003833 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe52de1fe2020-02-27 10:15:42 -07003834 __io_cqring_add_event(req, ret, cflags);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003835 if (ret < 0)
3836 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003837 io_put_req(req);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003838 return 0;
3839#else
3840 return -EOPNOTSUPP;
3841#endif
3842}
3843
Pavel Begunkov014db002020-03-03 21:33:12 +03003844static int io_recv(struct io_kiocb *req, bool force_nonblock)
Jens Axboefddafac2020-01-04 20:19:44 -07003845{
3846#if defined(CONFIG_NET)
Jens Axboebcda7ba2020-02-23 16:42:51 -07003847 struct io_buffer *kbuf = NULL;
Jens Axboefddafac2020-01-04 20:19:44 -07003848 struct socket *sock;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003849 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003850
3851 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3852 return -EINVAL;
3853
3854 sock = sock_from_file(req->file, &ret);
3855 if (sock) {
3856 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003857 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07003858 struct msghdr msg;
3859 struct iovec iov;
3860 unsigned flags;
3861
Jens Axboebcda7ba2020-02-23 16:42:51 -07003862 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
3863 if (IS_ERR(kbuf))
3864 return PTR_ERR(kbuf);
3865 else if (kbuf)
3866 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07003867
Jens Axboebcda7ba2020-02-23 16:42:51 -07003868 ret = import_single_range(READ, buf, sr->len, &iov,
3869 &msg.msg_iter);
3870 if (ret) {
3871 kfree(kbuf);
3872 return ret;
3873 }
3874
3875 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07003876 msg.msg_name = NULL;
3877 msg.msg_control = NULL;
3878 msg.msg_controllen = 0;
3879 msg.msg_namelen = 0;
3880 msg.msg_iocb = NULL;
3881 msg.msg_flags = 0;
3882
3883 flags = req->sr_msg.msg_flags;
3884 if (flags & MSG_DONTWAIT)
3885 req->flags |= REQ_F_NOWAIT;
3886 else if (force_nonblock)
3887 flags |= MSG_DONTWAIT;
3888
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003889 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07003890 if (force_nonblock && ret == -EAGAIN)
3891 return -EAGAIN;
3892 if (ret == -ERESTARTSYS)
3893 ret = -EINTR;
3894 }
3895
Jens Axboebcda7ba2020-02-23 16:42:51 -07003896 kfree(kbuf);
3897 req->flags &= ~REQ_F_NEED_CLEANUP;
3898 __io_cqring_add_event(req, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07003899 if (ret < 0)
3900 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003901 io_put_req(req);
Jens Axboefddafac2020-01-04 20:19:44 -07003902 return 0;
3903#else
3904 return -EOPNOTSUPP;
3905#endif
3906}
3907
3908
Jens Axboe3529d8c2019-12-19 18:24:38 -07003909static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003910{
3911#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003912 struct io_accept *accept = &req->accept;
3913
Jens Axboe17f2fe32019-10-17 14:42:58 -06003914 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3915 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003916 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003917 return -EINVAL;
3918
Jens Axboed55e5f52019-12-11 16:12:15 -07003919 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3920 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003921 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003922 return 0;
3923#else
3924 return -EOPNOTSUPP;
3925#endif
3926}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003927
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003928#if defined(CONFIG_NET)
Pavel Begunkov014db002020-03-03 21:33:12 +03003929static int __io_accept(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003930{
3931 struct io_accept *accept = &req->accept;
3932 unsigned file_flags;
3933 int ret;
3934
3935 file_flags = force_nonblock ? O_NONBLOCK : 0;
3936 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3937 accept->addr_len, accept->flags);
3938 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003939 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003940 if (ret == -ERESTARTSYS)
3941 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003942 if (ret < 0)
3943 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003944 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003945 io_put_req(req);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003946 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003947}
3948
3949static void io_accept_finish(struct io_wq_work **workptr)
3950{
3951 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003952
3953 if (io_req_cancelled(req))
3954 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03003955 __io_accept(req, false);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03003956 io_steal_work(req, workptr);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003957}
3958#endif
3959
Pavel Begunkov014db002020-03-03 21:33:12 +03003960static int io_accept(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003961{
3962#if defined(CONFIG_NET)
3963 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#else
3972 return -EOPNOTSUPP;
3973#endif
3974}
3975
Jens Axboe3529d8c2019-12-19 18:24:38 -07003976static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003977{
3978#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003979 struct io_connect *conn = &req->connect;
3980 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003981
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003982 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3983 return -EINVAL;
3984 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3985 return -EINVAL;
3986
Jens Axboe3529d8c2019-12-19 18:24:38 -07003987 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3988 conn->addr_len = READ_ONCE(sqe->addr2);
3989
3990 if (!io)
3991 return 0;
3992
3993 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003994 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003995#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003996 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07003997#endif
3998}
3999
Pavel Begunkov014db002020-03-03 21:33:12 +03004000static int io_connect(struct io_kiocb *req, bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004001{
4002#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07004003 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004004 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004005 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004006
Jens Axboef499a022019-12-02 16:28:46 -07004007 if (req->io) {
4008 io = req->io;
4009 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004010 ret = move_addr_to_kernel(req->connect.addr,
4011 req->connect.addr_len,
4012 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004013 if (ret)
4014 goto out;
4015 io = &__io;
4016 }
4017
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004018 file_flags = force_nonblock ? O_NONBLOCK : 0;
4019
4020 ret = __sys_connect_file(req->file, &io->connect.address,
4021 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004022 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004023 if (req->io)
4024 return -EAGAIN;
4025 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004026 ret = -ENOMEM;
4027 goto out;
4028 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004029 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004030 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004031 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004032 if (ret == -ERESTARTSYS)
4033 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004034out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004035 if (ret < 0)
4036 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004037 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03004038 io_put_req(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004039 return 0;
4040#else
4041 return -EOPNOTSUPP;
4042#endif
4043}
4044
Jens Axboed7718a92020-02-14 22:23:12 -07004045struct io_poll_table {
4046 struct poll_table_struct pt;
4047 struct io_kiocb *req;
4048 int error;
4049};
4050
4051static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
4052 struct wait_queue_head *head)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004053{
Jens Axboed7718a92020-02-14 22:23:12 -07004054 if (unlikely(poll->head)) {
4055 pt->error = -EINVAL;
4056 return;
4057 }
4058
4059 pt->error = 0;
4060 poll->head = head;
4061 add_wait_queue(head, &poll->wait);
4062}
4063
4064static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4065 struct poll_table_struct *p)
4066{
4067 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4068
4069 __io_queue_proc(&pt->req->apoll->poll, pt, head);
4070}
4071
4072static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4073 __poll_t mask, task_work_func_t func)
4074{
4075 struct task_struct *tsk;
4076
4077 /* for instances that support it check for an event match first: */
4078 if (mask && !(mask & poll->events))
4079 return 0;
4080
4081 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4082
4083 list_del_init(&poll->wait.entry);
4084
4085 tsk = req->task;
4086 req->result = mask;
4087 init_task_work(&req->task_work, func);
4088 /*
4089 * If this fails, then the task is exiting. If that is the case, then
4090 * the exit check will ultimately cancel these work items. Hence we
4091 * don't need to check here and handle it specifically.
4092 */
4093 task_work_add(tsk, &req->task_work, true);
4094 wake_up_process(tsk);
4095 return 1;
4096}
4097
4098static void io_async_task_func(struct callback_head *cb)
4099{
4100 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4101 struct async_poll *apoll = req->apoll;
4102 struct io_ring_ctx *ctx = req->ctx;
4103
4104 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4105
4106 WARN_ON_ONCE(!list_empty(&req->apoll->poll.wait.entry));
4107
4108 if (hash_hashed(&req->hash_node)) {
4109 spin_lock_irq(&ctx->completion_lock);
4110 hash_del(&req->hash_node);
4111 spin_unlock_irq(&ctx->completion_lock);
4112 }
4113
4114 /* restore ->work in case we need to retry again */
4115 memcpy(&req->work, &apoll->work, sizeof(req->work));
4116
4117 __set_current_state(TASK_RUNNING);
4118 mutex_lock(&ctx->uring_lock);
4119 __io_queue_sqe(req, NULL);
4120 mutex_unlock(&ctx->uring_lock);
4121
4122 kfree(apoll);
4123}
4124
4125static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4126 void *key)
4127{
4128 struct io_kiocb *req = wait->private;
4129 struct io_poll_iocb *poll = &req->apoll->poll;
4130
4131 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4132 key_to_poll(key));
4133
4134 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4135}
4136
4137static void io_poll_req_insert(struct io_kiocb *req)
4138{
4139 struct io_ring_ctx *ctx = req->ctx;
4140 struct hlist_head *list;
4141
4142 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
4143 hlist_add_head(&req->hash_node, list);
4144}
4145
4146static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
4147 struct io_poll_iocb *poll,
4148 struct io_poll_table *ipt, __poll_t mask,
4149 wait_queue_func_t wake_func)
4150 __acquires(&ctx->completion_lock)
4151{
4152 struct io_ring_ctx *ctx = req->ctx;
4153 bool cancel = false;
4154
4155 poll->file = req->file;
4156 poll->head = NULL;
4157 poll->done = poll->canceled = false;
4158 poll->events = mask;
4159
4160 ipt->pt._key = mask;
4161 ipt->req = req;
4162 ipt->error = -EINVAL;
4163
4164 INIT_LIST_HEAD(&poll->wait.entry);
4165 init_waitqueue_func_entry(&poll->wait, wake_func);
4166 poll->wait.private = req;
4167
4168 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
4169
4170 spin_lock_irq(&ctx->completion_lock);
4171 if (likely(poll->head)) {
4172 spin_lock(&poll->head->lock);
4173 if (unlikely(list_empty(&poll->wait.entry))) {
4174 if (ipt->error)
4175 cancel = true;
4176 ipt->error = 0;
4177 mask = 0;
4178 }
4179 if (mask || ipt->error)
4180 list_del_init(&poll->wait.entry);
4181 else if (cancel)
4182 WRITE_ONCE(poll->canceled, true);
4183 else if (!poll->done) /* actually waiting for an event */
4184 io_poll_req_insert(req);
4185 spin_unlock(&poll->head->lock);
4186 }
4187
4188 return mask;
4189}
4190
4191static bool io_arm_poll_handler(struct io_kiocb *req)
4192{
4193 const struct io_op_def *def = &io_op_defs[req->opcode];
4194 struct io_ring_ctx *ctx = req->ctx;
4195 struct async_poll *apoll;
4196 struct io_poll_table ipt;
4197 __poll_t mask, ret;
4198
4199 if (!req->file || !file_can_poll(req->file))
4200 return false;
4201 if (req->flags & (REQ_F_MUST_PUNT | REQ_F_POLLED))
4202 return false;
4203 if (!def->pollin && !def->pollout)
4204 return false;
4205
4206 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
4207 if (unlikely(!apoll))
4208 return false;
4209
4210 req->flags |= REQ_F_POLLED;
4211 memcpy(&apoll->work, &req->work, sizeof(req->work));
4212
4213 /*
4214 * Don't need a reference here, as we're adding it to the task
4215 * task_works list. If the task exits, the list is pruned.
4216 */
4217 req->task = current;
4218 req->apoll = apoll;
4219 INIT_HLIST_NODE(&req->hash_node);
4220
Nathan Chancellor8755d972020-03-02 16:01:19 -07004221 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07004222 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07004223 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07004224 if (def->pollout)
4225 mask |= POLLOUT | POLLWRNORM;
4226 mask |= POLLERR | POLLPRI;
4227
4228 ipt.pt._qproc = io_async_queue_proc;
4229
4230 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
4231 io_async_wake);
4232 if (ret) {
4233 ipt.error = 0;
4234 apoll->poll.done = true;
4235 spin_unlock_irq(&ctx->completion_lock);
4236 memcpy(&req->work, &apoll->work, sizeof(req->work));
4237 kfree(apoll);
4238 return false;
4239 }
4240 spin_unlock_irq(&ctx->completion_lock);
4241 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
4242 apoll->poll.events);
4243 return true;
4244}
4245
4246static bool __io_poll_remove_one(struct io_kiocb *req,
4247 struct io_poll_iocb *poll)
4248{
Jens Axboeb41e9852020-02-17 09:52:41 -07004249 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004250
4251 spin_lock(&poll->head->lock);
4252 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07004253 if (!list_empty(&poll->wait.entry)) {
4254 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07004255 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004256 }
4257 spin_unlock(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07004258 return do_complete;
4259}
4260
4261static bool io_poll_remove_one(struct io_kiocb *req)
4262{
4263 bool do_complete;
4264
4265 if (req->opcode == IORING_OP_POLL_ADD) {
4266 do_complete = __io_poll_remove_one(req, &req->poll);
4267 } else {
4268 /* non-poll requests have submit ref still */
4269 do_complete = __io_poll_remove_one(req, &req->apoll->poll);
4270 if (do_complete)
4271 io_put_req(req);
4272 }
4273
Jens Axboe78076bb2019-12-04 19:56:40 -07004274 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07004275
Jens Axboeb41e9852020-02-17 09:52:41 -07004276 if (do_complete) {
4277 io_cqring_fill_event(req, -ECANCELED);
4278 io_commit_cqring(req->ctx);
4279 req->flags |= REQ_F_COMP_LOCKED;
4280 io_put_req(req);
4281 }
4282
4283 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004284}
4285
4286static void io_poll_remove_all(struct io_ring_ctx *ctx)
4287{
Jens Axboe78076bb2019-12-04 19:56:40 -07004288 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004289 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07004290 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004291
4292 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07004293 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
4294 struct hlist_head *list;
4295
4296 list = &ctx->cancel_hash[i];
4297 hlist_for_each_entry_safe(req, tmp, list, hash_node)
4298 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004299 }
4300 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07004301
4302 io_cqring_ev_posted(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004303}
4304
Jens Axboe47f46762019-11-09 17:43:02 -07004305static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
4306{
Jens Axboe78076bb2019-12-04 19:56:40 -07004307 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07004308 struct io_kiocb *req;
4309
Jens Axboe78076bb2019-12-04 19:56:40 -07004310 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
4311 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07004312 if (sqe_addr != req->user_data)
4313 continue;
4314 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07004315 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07004316 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07004317 }
4318
4319 return -ENOENT;
4320}
4321
Jens Axboe3529d8c2019-12-19 18:24:38 -07004322static int io_poll_remove_prep(struct io_kiocb *req,
4323 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004324{
Jens Axboe221c5eb2019-01-17 09:41:58 -07004325 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4326 return -EINVAL;
4327 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
4328 sqe->poll_events)
4329 return -EINVAL;
4330
Jens Axboe0969e782019-12-17 18:40:57 -07004331 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07004332 return 0;
4333}
4334
4335/*
4336 * Find a running poll command that matches one specified in sqe->addr,
4337 * and remove it if found.
4338 */
4339static int io_poll_remove(struct io_kiocb *req)
4340{
4341 struct io_ring_ctx *ctx = req->ctx;
4342 u64 addr;
4343 int ret;
4344
Jens Axboe0969e782019-12-17 18:40:57 -07004345 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004346 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07004347 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004348 spin_unlock_irq(&ctx->completion_lock);
4349
Jens Axboe78e19bb2019-11-06 15:21:34 -07004350 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004351 if (ret < 0)
4352 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004353 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004354 return 0;
4355}
4356
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004357static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004358{
Jackie Liua197f662019-11-08 08:09:12 -07004359 struct io_ring_ctx *ctx = req->ctx;
4360
Jens Axboe8c838782019-03-12 15:48:16 -06004361 req->poll.done = true;
Pavel Begunkovb0a20342020-02-28 10:36:35 +03004362 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06004363 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004364}
4365
Jens Axboeb41e9852020-02-17 09:52:41 -07004366static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004367{
Jens Axboe221c5eb2019-01-17 09:41:58 -07004368 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004369
Jens Axboe221c5eb2019-01-17 09:41:58 -07004370 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07004371 hash_del(&req->hash_node);
Jens Axboeb41e9852020-02-17 09:52:41 -07004372 io_poll_complete(req, req->result, 0);
4373 req->flags |= REQ_F_COMP_LOCKED;
4374 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004375 spin_unlock_irq(&ctx->completion_lock);
4376
Jens Axboe8c838782019-03-12 15:48:16 -06004377 io_cqring_ev_posted(ctx);
Jens Axboeb41e9852020-02-17 09:52:41 -07004378}
Jens Axboe89723d02019-11-05 15:32:58 -07004379
Jens Axboeb41e9852020-02-17 09:52:41 -07004380static void io_poll_task_func(struct callback_head *cb)
4381{
4382 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4383 struct io_kiocb *nxt = NULL;
4384
4385 io_poll_task_handler(req, &nxt);
Jens Axboed7718a92020-02-14 22:23:12 -07004386 if (nxt) {
4387 struct io_ring_ctx *ctx = nxt->ctx;
4388
4389 mutex_lock(&ctx->uring_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07004390 __io_queue_sqe(nxt, NULL);
Jens Axboed7718a92020-02-14 22:23:12 -07004391 mutex_unlock(&ctx->uring_lock);
4392 }
Jens Axboef0b493e2020-02-01 21:30:11 -07004393}
4394
Jens Axboe221c5eb2019-01-17 09:41:58 -07004395static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4396 void *key)
4397{
Jens Axboec2f2eb72020-02-10 09:07:05 -07004398 struct io_kiocb *req = wait->private;
4399 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004400
Jens Axboed7718a92020-02-14 22:23:12 -07004401 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004402}
4403
Jens Axboe221c5eb2019-01-17 09:41:58 -07004404static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
4405 struct poll_table_struct *p)
4406{
4407 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4408
Jens Axboed7718a92020-02-14 22:23:12 -07004409 __io_queue_proc(&pt->req->poll, pt, head);
Jens Axboeeac406c2019-11-14 12:09:58 -07004410}
4411
Jens Axboe3529d8c2019-12-19 18:24:38 -07004412static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004413{
4414 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004415 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004416
4417 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4418 return -EINVAL;
4419 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
4420 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06004421 if (!poll->file)
4422 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004423
Jens Axboe221c5eb2019-01-17 09:41:58 -07004424 events = READ_ONCE(sqe->poll_events);
4425 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboeb41e9852020-02-17 09:52:41 -07004426
Jens Axboed7718a92020-02-14 22:23:12 -07004427 /*
4428 * Don't need a reference here, as we're adding it to the task
4429 * task_works list. If the task exits, the list is pruned.
4430 */
Jens Axboeb41e9852020-02-17 09:52:41 -07004431 req->task = current;
Jens Axboe0969e782019-12-17 18:40:57 -07004432 return 0;
4433}
4434
Pavel Begunkov014db002020-03-03 21:33:12 +03004435static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07004436{
4437 struct io_poll_iocb *poll = &req->poll;
4438 struct io_ring_ctx *ctx = req->ctx;
4439 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07004440 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07004441
Jens Axboe78076bb2019-12-04 19:56:40 -07004442 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe36703242019-07-25 10:20:18 -06004443 INIT_LIST_HEAD(&req->list);
Jens Axboed7718a92020-02-14 22:23:12 -07004444 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06004445
Jens Axboed7718a92020-02-14 22:23:12 -07004446 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
4447 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004448
Jens Axboe8c838782019-03-12 15:48:16 -06004449 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06004450 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004451 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06004452 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07004453 spin_unlock_irq(&ctx->completion_lock);
4454
Jens Axboe8c838782019-03-12 15:48:16 -06004455 if (mask) {
4456 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03004457 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004458 }
Jens Axboe8c838782019-03-12 15:48:16 -06004459 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004460}
4461
Jens Axboe5262f562019-09-17 12:26:57 -06004462static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
4463{
Jens Axboead8a48a2019-11-15 08:49:11 -07004464 struct io_timeout_data *data = container_of(timer,
4465 struct io_timeout_data, timer);
4466 struct io_kiocb *req = data->req;
4467 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06004468 unsigned long flags;
4469
Jens Axboe5262f562019-09-17 12:26:57 -06004470 atomic_inc(&ctx->cq_timeouts);
4471
4472 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08004473 /*
Jens Axboe11365042019-10-16 09:08:32 -06004474 * We could be racing with timeout deletion. If the list is empty,
4475 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08004476 */
Jens Axboe842f9612019-10-29 12:34:10 -06004477 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06004478 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06004479
Jens Axboe11365042019-10-16 09:08:32 -06004480 /*
4481 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08004482 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06004483 * pointer will be increased, otherwise other timeout reqs may
4484 * return in advance without waiting for enough wait_nr.
4485 */
4486 prev = req;
4487 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
4488 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06004489 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06004490 }
Jens Axboe842f9612019-10-29 12:34:10 -06004491
Jens Axboe78e19bb2019-11-06 15:21:34 -07004492 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06004493 io_commit_cqring(ctx);
4494 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4495
4496 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004497 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004498 io_put_req(req);
4499 return HRTIMER_NORESTART;
4500}
4501
Jens Axboe47f46762019-11-09 17:43:02 -07004502static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
4503{
4504 struct io_kiocb *req;
4505 int ret = -ENOENT;
4506
4507 list_for_each_entry(req, &ctx->timeout_list, list) {
4508 if (user_data == req->user_data) {
4509 list_del_init(&req->list);
4510 ret = 0;
4511 break;
4512 }
4513 }
4514
4515 if (ret == -ENOENT)
4516 return ret;
4517
Jens Axboe2d283902019-12-04 11:08:05 -07004518 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07004519 if (ret == -1)
4520 return -EALREADY;
4521
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004522 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004523 io_cqring_fill_event(req, -ECANCELED);
4524 io_put_req(req);
4525 return 0;
4526}
4527
Jens Axboe3529d8c2019-12-19 18:24:38 -07004528static int io_timeout_remove_prep(struct io_kiocb *req,
4529 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07004530{
Jens Axboeb29472e2019-12-17 18:50:29 -07004531 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4532 return -EINVAL;
4533 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
4534 return -EINVAL;
4535
4536 req->timeout.addr = READ_ONCE(sqe->addr);
4537 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
4538 if (req->timeout.flags)
4539 return -EINVAL;
4540
Jens Axboeb29472e2019-12-17 18:50:29 -07004541 return 0;
4542}
4543
Jens Axboe11365042019-10-16 09:08:32 -06004544/*
4545 * Remove or update an existing timeout command
4546 */
Jens Axboefc4df992019-12-10 14:38:45 -07004547static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06004548{
4549 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07004550 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06004551
Jens Axboe11365042019-10-16 09:08:32 -06004552 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07004553 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06004554
Jens Axboe47f46762019-11-09 17:43:02 -07004555 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06004556 io_commit_cqring(ctx);
4557 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004558 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004559 if (ret < 0)
4560 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08004561 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06004562 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06004563}
4564
Jens Axboe3529d8c2019-12-19 18:24:38 -07004565static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07004566 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06004567{
Jens Axboead8a48a2019-11-15 08:49:11 -07004568 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06004569 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06004570
Jens Axboead8a48a2019-11-15 08:49:11 -07004571 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06004572 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07004573 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06004574 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07004575 if (sqe->off && is_timeout_link)
4576 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06004577 flags = READ_ONCE(sqe->timeout_flags);
4578 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06004579 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06004580
Jens Axboe26a61672019-12-20 09:02:01 -07004581 req->timeout.count = READ_ONCE(sqe->off);
4582
Jens Axboe3529d8c2019-12-19 18:24:38 -07004583 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07004584 return -ENOMEM;
4585
4586 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07004587 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07004588 req->flags |= REQ_F_TIMEOUT;
4589
4590 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06004591 return -EFAULT;
4592
Jens Axboe11365042019-10-16 09:08:32 -06004593 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07004594 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06004595 else
Jens Axboead8a48a2019-11-15 08:49:11 -07004596 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06004597
Jens Axboead8a48a2019-11-15 08:49:11 -07004598 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
4599 return 0;
4600}
4601
Jens Axboefc4df992019-12-10 14:38:45 -07004602static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07004603{
4604 unsigned count;
4605 struct io_ring_ctx *ctx = req->ctx;
4606 struct io_timeout_data *data;
4607 struct list_head *entry;
4608 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07004609
Jens Axboe2d283902019-12-04 11:08:05 -07004610 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004611
Jens Axboe5262f562019-09-17 12:26:57 -06004612 /*
4613 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07004614 * timeout event to be satisfied. If it isn't set, then this is
4615 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06004616 */
Jens Axboe26a61672019-12-20 09:02:01 -07004617 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004618 if (!count) {
4619 req->flags |= REQ_F_TIMEOUT_NOSEQ;
4620 spin_lock_irq(&ctx->completion_lock);
4621 entry = ctx->timeout_list.prev;
4622 goto add;
4623 }
Jens Axboe5262f562019-09-17 12:26:57 -06004624
4625 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07004626 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06004627
4628 /*
4629 * Insertion sort, ensuring the first entry in the list is always
4630 * the one we need first.
4631 */
Jens Axboe5262f562019-09-17 12:26:57 -06004632 spin_lock_irq(&ctx->completion_lock);
4633 list_for_each_prev(entry, &ctx->timeout_list) {
4634 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08004635 unsigned nxt_sq_head;
4636 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07004637 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06004638
Jens Axboe93bd25b2019-11-11 23:34:31 -07004639 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
4640 continue;
4641
yangerkun5da0fb12019-10-15 21:59:29 +08004642 /*
4643 * Since cached_sq_head + count - 1 can overflow, use type long
4644 * long to store it.
4645 */
4646 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03004647 nxt_sq_head = nxt->sequence - nxt_offset + 1;
4648 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08004649
4650 /*
4651 * cached_sq_head may overflow, and it will never overflow twice
4652 * once there is some timeout req still be valid.
4653 */
4654 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08004655 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08004656
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004657 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06004658 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004659
4660 /*
4661 * Sequence of reqs after the insert one and itself should
4662 * be adjusted because each timeout req consumes a slot.
4663 */
4664 span++;
4665 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06004666 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004667 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004668add:
Jens Axboe5262f562019-09-17 12:26:57 -06004669 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07004670 data->timer.function = io_timeout_fn;
4671 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06004672 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004673 return 0;
4674}
4675
Jens Axboe62755e32019-10-28 21:49:21 -06004676static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06004677{
Jens Axboe62755e32019-10-28 21:49:21 -06004678 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06004679
Jens Axboe62755e32019-10-28 21:49:21 -06004680 return req->user_data == (unsigned long) data;
4681}
4682
Jens Axboee977d6d2019-11-05 12:39:45 -07004683static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06004684{
Jens Axboe62755e32019-10-28 21:49:21 -06004685 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06004686 int ret = 0;
4687
Jens Axboe62755e32019-10-28 21:49:21 -06004688 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
4689 switch (cancel_ret) {
4690 case IO_WQ_CANCEL_OK:
4691 ret = 0;
4692 break;
4693 case IO_WQ_CANCEL_RUNNING:
4694 ret = -EALREADY;
4695 break;
4696 case IO_WQ_CANCEL_NOTFOUND:
4697 ret = -ENOENT;
4698 break;
4699 }
4700
Jens Axboee977d6d2019-11-05 12:39:45 -07004701 return ret;
4702}
4703
Jens Axboe47f46762019-11-09 17:43:02 -07004704static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
4705 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03004706 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07004707{
4708 unsigned long flags;
4709 int ret;
4710
4711 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
4712 if (ret != -ENOENT) {
4713 spin_lock_irqsave(&ctx->completion_lock, flags);
4714 goto done;
4715 }
4716
4717 spin_lock_irqsave(&ctx->completion_lock, flags);
4718 ret = io_timeout_cancel(ctx, sqe_addr);
4719 if (ret != -ENOENT)
4720 goto done;
4721 ret = io_poll_cancel(ctx, sqe_addr);
4722done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004723 if (!ret)
4724 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07004725 io_cqring_fill_event(req, ret);
4726 io_commit_cqring(ctx);
4727 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4728 io_cqring_ev_posted(ctx);
4729
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004730 if (ret < 0)
4731 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03004732 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004733}
4734
Jens Axboe3529d8c2019-12-19 18:24:38 -07004735static int io_async_cancel_prep(struct io_kiocb *req,
4736 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07004737{
Jens Axboefbf23842019-12-17 18:45:56 -07004738 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07004739 return -EINVAL;
4740 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
4741 sqe->cancel_flags)
4742 return -EINVAL;
4743
Jens Axboefbf23842019-12-17 18:45:56 -07004744 req->cancel.addr = READ_ONCE(sqe->addr);
4745 return 0;
4746}
4747
Pavel Begunkov014db002020-03-03 21:33:12 +03004748static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07004749{
4750 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07004751
Pavel Begunkov014db002020-03-03 21:33:12 +03004752 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06004753 return 0;
4754}
4755
Jens Axboe05f3fb32019-12-09 11:22:50 -07004756static int io_files_update_prep(struct io_kiocb *req,
4757 const struct io_uring_sqe *sqe)
4758{
4759 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
4760 return -EINVAL;
4761
4762 req->files_update.offset = READ_ONCE(sqe->off);
4763 req->files_update.nr_args = READ_ONCE(sqe->len);
4764 if (!req->files_update.nr_args)
4765 return -EINVAL;
4766 req->files_update.arg = READ_ONCE(sqe->addr);
4767 return 0;
4768}
4769
4770static int io_files_update(struct io_kiocb *req, bool force_nonblock)
4771{
4772 struct io_ring_ctx *ctx = req->ctx;
4773 struct io_uring_files_update up;
4774 int ret;
4775
Jens Axboef86cd202020-01-29 13:46:44 -07004776 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004777 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004778
4779 up.offset = req->files_update.offset;
4780 up.fds = req->files_update.arg;
4781
4782 mutex_lock(&ctx->uring_lock);
4783 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
4784 mutex_unlock(&ctx->uring_lock);
4785
4786 if (ret < 0)
4787 req_set_fail_links(req);
4788 io_cqring_add_event(req, ret);
4789 io_put_req(req);
4790 return 0;
4791}
4792
Jens Axboe3529d8c2019-12-19 18:24:38 -07004793static int io_req_defer_prep(struct io_kiocb *req,
4794 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07004795{
Jens Axboee7815732019-12-17 19:45:06 -07004796 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07004797
Jens Axboef86cd202020-01-29 13:46:44 -07004798 if (io_op_defs[req->opcode].file_table) {
4799 ret = io_grab_files(req);
4800 if (unlikely(ret))
4801 return ret;
4802 }
4803
Jens Axboecccf0ee2020-01-27 16:34:48 -07004804 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
4805
Jens Axboed625c6e2019-12-17 19:53:05 -07004806 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07004807 case IORING_OP_NOP:
4808 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004809 case IORING_OP_READV:
4810 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004811 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004812 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004813 break;
4814 case IORING_OP_WRITEV:
4815 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004816 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004817 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004818 break;
Jens Axboe0969e782019-12-17 18:40:57 -07004819 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004820 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004821 break;
4822 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004823 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004824 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004825 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004826 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004827 break;
4828 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004829 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004830 break;
Jens Axboe03b12302019-12-02 18:50:25 -07004831 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004832 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004833 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004834 break;
4835 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004836 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004837 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004838 break;
Jens Axboef499a022019-12-02 16:28:46 -07004839 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004840 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004841 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004842 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004843 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004844 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004845 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004846 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004847 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004848 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004849 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004850 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004851 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004852 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004853 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004854 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004855 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004856 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004857 case IORING_OP_FALLOCATE:
4858 ret = io_fallocate_prep(req, sqe);
4859 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004860 case IORING_OP_OPENAT:
4861 ret = io_openat_prep(req, sqe);
4862 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004863 case IORING_OP_CLOSE:
4864 ret = io_close_prep(req, sqe);
4865 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004866 case IORING_OP_FILES_UPDATE:
4867 ret = io_files_update_prep(req, sqe);
4868 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004869 case IORING_OP_STATX:
4870 ret = io_statx_prep(req, sqe);
4871 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004872 case IORING_OP_FADVISE:
4873 ret = io_fadvise_prep(req, sqe);
4874 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004875 case IORING_OP_MADVISE:
4876 ret = io_madvise_prep(req, sqe);
4877 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004878 case IORING_OP_OPENAT2:
4879 ret = io_openat2_prep(req, sqe);
4880 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004881 case IORING_OP_EPOLL_CTL:
4882 ret = io_epoll_ctl_prep(req, sqe);
4883 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004884 case IORING_OP_SPLICE:
4885 ret = io_splice_prep(req, sqe);
4886 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004887 case IORING_OP_PROVIDE_BUFFERS:
4888 ret = io_provide_buffers_prep(req, sqe);
4889 break;
Jens Axboe067524e2020-03-02 16:32:28 -07004890 case IORING_OP_REMOVE_BUFFERS:
4891 ret = io_remove_buffers_prep(req, sqe);
4892 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004893 default:
Jens Axboee7815732019-12-17 19:45:06 -07004894 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
4895 req->opcode);
4896 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004897 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004898 }
4899
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004900 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07004901}
4902
Jens Axboe3529d8c2019-12-19 18:24:38 -07004903static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06004904{
Jackie Liua197f662019-11-08 08:09:12 -07004905 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07004906 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06004907
Bob Liu9d858b22019-11-13 18:06:25 +08004908 /* Still need defer if there is pending req in defer list. */
4909 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06004910 return 0;
4911
Jens Axboe3529d8c2019-12-19 18:24:38 -07004912 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06004913 return -EAGAIN;
4914
Jens Axboe3529d8c2019-12-19 18:24:38 -07004915 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004916 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004917 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004918
Jens Axboede0617e2019-04-06 21:51:27 -06004919 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004920 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004921 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004922 return 0;
4923 }
4924
Jens Axboe915967f2019-11-21 09:01:20 -07004925 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004926 list_add_tail(&req->list, &ctx->defer_list);
4927 spin_unlock_irq(&ctx->completion_lock);
4928 return -EIOCBQUEUED;
4929}
4930
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004931static void io_cleanup_req(struct io_kiocb *req)
4932{
4933 struct io_async_ctx *io = req->io;
4934
4935 switch (req->opcode) {
4936 case IORING_OP_READV:
4937 case IORING_OP_READ_FIXED:
4938 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07004939 if (req->flags & REQ_F_BUFFER_SELECTED)
4940 kfree((void *)(unsigned long)req->rw.addr);
4941 /* fallthrough */
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004942 case IORING_OP_WRITEV:
4943 case IORING_OP_WRITE_FIXED:
4944 case IORING_OP_WRITE:
4945 if (io->rw.iov != io->rw.fast_iov)
4946 kfree(io->rw.iov);
4947 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004948 case IORING_OP_RECVMSG:
Jens Axboe52de1fe2020-02-27 10:15:42 -07004949 if (req->flags & REQ_F_BUFFER_SELECTED)
4950 kfree(req->sr_msg.kbuf);
4951 /* fallthrough */
4952 case IORING_OP_SENDMSG:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004953 if (io->msg.iov != io->msg.fast_iov)
4954 kfree(io->msg.iov);
4955 break;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004956 case IORING_OP_RECV:
4957 if (req->flags & REQ_F_BUFFER_SELECTED)
4958 kfree(req->sr_msg.kbuf);
4959 break;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004960 case IORING_OP_OPENAT:
4961 case IORING_OP_OPENAT2:
4962 case IORING_OP_STATX:
4963 putname(req->open.filename);
4964 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004965 case IORING_OP_SPLICE:
4966 io_put_file(req, req->splice.file_in,
4967 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
4968 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004969 }
4970
4971 req->flags &= ~REQ_F_NEED_CLEANUP;
4972}
4973
Jens Axboe3529d8c2019-12-19 18:24:38 -07004974static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Pavel Begunkov014db002020-03-03 21:33:12 +03004975 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004976{
Jackie Liua197f662019-11-08 08:09:12 -07004977 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07004978 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004979
Jens Axboed625c6e2019-12-17 19:53:05 -07004980 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004981 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004982 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004983 break;
4984 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004985 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004986 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004987 if (sqe) {
4988 ret = io_read_prep(req, sqe, force_nonblock);
4989 if (ret < 0)
4990 break;
4991 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004992 ret = io_read(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004993 break;
4994 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07004995 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004996 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004997 if (sqe) {
4998 ret = io_write_prep(req, sqe, force_nonblock);
4999 if (ret < 0)
5000 break;
5001 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005002 ret = io_write(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005003 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005004 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005005 if (sqe) {
5006 ret = io_prep_fsync(req, sqe);
5007 if (ret < 0)
5008 break;
5009 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005010 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005011 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005012 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005013 if (sqe) {
5014 ret = io_poll_add_prep(req, sqe);
5015 if (ret)
5016 break;
5017 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005018 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005019 break;
5020 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005021 if (sqe) {
5022 ret = io_poll_remove_prep(req, sqe);
5023 if (ret < 0)
5024 break;
5025 }
Jens Axboefc4df992019-12-10 14:38:45 -07005026 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005027 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005028 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005029 if (sqe) {
5030 ret = io_prep_sfr(req, sqe);
5031 if (ret < 0)
5032 break;
5033 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005034 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005035 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005036 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005037 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005038 if (sqe) {
5039 ret = io_sendmsg_prep(req, sqe);
5040 if (ret < 0)
5041 break;
5042 }
Jens Axboefddafac2020-01-04 20:19:44 -07005043 if (req->opcode == IORING_OP_SENDMSG)
Pavel Begunkov014db002020-03-03 21:33:12 +03005044 ret = io_sendmsg(req, force_nonblock);
Jens Axboefddafac2020-01-04 20:19:44 -07005045 else
Pavel Begunkov014db002020-03-03 21:33:12 +03005046 ret = io_send(req, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005047 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06005048 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005049 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005050 if (sqe) {
5051 ret = io_recvmsg_prep(req, sqe);
5052 if (ret)
5053 break;
5054 }
Jens Axboefddafac2020-01-04 20:19:44 -07005055 if (req->opcode == IORING_OP_RECVMSG)
Pavel Begunkov014db002020-03-03 21:33:12 +03005056 ret = io_recvmsg(req, force_nonblock);
Jens Axboefddafac2020-01-04 20:19:44 -07005057 else
Pavel Begunkov014db002020-03-03 21:33:12 +03005058 ret = io_recv(req, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06005059 break;
Jens Axboe5262f562019-09-17 12:26:57 -06005060 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005061 if (sqe) {
5062 ret = io_timeout_prep(req, sqe, false);
5063 if (ret)
5064 break;
5065 }
Jens Axboefc4df992019-12-10 14:38:45 -07005066 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005067 break;
Jens Axboe11365042019-10-16 09:08:32 -06005068 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005069 if (sqe) {
5070 ret = io_timeout_remove_prep(req, sqe);
5071 if (ret)
5072 break;
5073 }
Jens Axboefc4df992019-12-10 14:38:45 -07005074 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06005075 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06005076 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005077 if (sqe) {
5078 ret = io_accept_prep(req, sqe);
5079 if (ret)
5080 break;
5081 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005082 ret = io_accept(req, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005083 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005084 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005085 if (sqe) {
5086 ret = io_connect_prep(req, sqe);
5087 if (ret)
5088 break;
5089 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005090 ret = io_connect(req, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005091 break;
Jens Axboe62755e32019-10-28 21:49:21 -06005092 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005093 if (sqe) {
5094 ret = io_async_cancel_prep(req, sqe);
5095 if (ret)
5096 break;
5097 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005098 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005099 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005100 case IORING_OP_FALLOCATE:
5101 if (sqe) {
5102 ret = io_fallocate_prep(req, sqe);
5103 if (ret)
5104 break;
5105 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005106 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005107 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005108 case IORING_OP_OPENAT:
5109 if (sqe) {
5110 ret = io_openat_prep(req, sqe);
5111 if (ret)
5112 break;
5113 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005114 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005115 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005116 case IORING_OP_CLOSE:
5117 if (sqe) {
5118 ret = io_close_prep(req, sqe);
5119 if (ret)
5120 break;
5121 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005122 ret = io_close(req, force_nonblock);
Jens Axboeb5dba592019-12-11 14:02:38 -07005123 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005124 case IORING_OP_FILES_UPDATE:
5125 if (sqe) {
5126 ret = io_files_update_prep(req, sqe);
5127 if (ret)
5128 break;
5129 }
5130 ret = io_files_update(req, force_nonblock);
5131 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005132 case IORING_OP_STATX:
5133 if (sqe) {
5134 ret = io_statx_prep(req, sqe);
5135 if (ret)
5136 break;
5137 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005138 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005139 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005140 case IORING_OP_FADVISE:
5141 if (sqe) {
5142 ret = io_fadvise_prep(req, sqe);
5143 if (ret)
5144 break;
5145 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005146 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005147 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005148 case IORING_OP_MADVISE:
5149 if (sqe) {
5150 ret = io_madvise_prep(req, sqe);
5151 if (ret)
5152 break;
5153 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005154 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005155 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005156 case IORING_OP_OPENAT2:
5157 if (sqe) {
5158 ret = io_openat2_prep(req, sqe);
5159 if (ret)
5160 break;
5161 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005162 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005163 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005164 case IORING_OP_EPOLL_CTL:
5165 if (sqe) {
5166 ret = io_epoll_ctl_prep(req, sqe);
5167 if (ret)
5168 break;
5169 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005170 ret = io_epoll_ctl(req, force_nonblock);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005171 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005172 case IORING_OP_SPLICE:
5173 if (sqe) {
5174 ret = io_splice_prep(req, sqe);
5175 if (ret < 0)
5176 break;
5177 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005178 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005179 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005180 case IORING_OP_PROVIDE_BUFFERS:
5181 if (sqe) {
5182 ret = io_provide_buffers_prep(req, sqe);
5183 if (ret)
5184 break;
5185 }
5186 ret = io_provide_buffers(req, force_nonblock);
5187 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005188 case IORING_OP_REMOVE_BUFFERS:
5189 if (sqe) {
5190 ret = io_remove_buffers_prep(req, sqe);
5191 if (ret)
5192 break;
5193 }
5194 ret = io_remove_buffers(req, force_nonblock);
5195 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005196 default:
5197 ret = -EINVAL;
5198 break;
5199 }
5200
Jens Axboedef596e2019-01-09 08:59:42 -07005201 if (ret)
5202 return ret;
5203
5204 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07005205 const bool in_async = io_wq_current_is_worker();
5206
Jens Axboe9e645e112019-05-10 16:07:28 -06005207 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07005208 return -EAGAIN;
5209
Jens Axboe11ba8202020-01-15 21:51:17 -07005210 /* workqueue context doesn't hold uring_lock, grab it now */
5211 if (in_async)
5212 mutex_lock(&ctx->uring_lock);
5213
Jens Axboedef596e2019-01-09 08:59:42 -07005214 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07005215
5216 if (in_async)
5217 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07005218 }
5219
5220 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005221}
5222
Jens Axboe561fb042019-10-24 07:25:42 -06005223static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07005224{
Jens Axboe561fb042019-10-24 07:25:42 -06005225 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005226 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06005227 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005228
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005229 /* if NO_CANCEL is set, we must still run the work */
5230 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
5231 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06005232 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005233 }
Jens Axboe31b51512019-01-18 22:56:34 -07005234
Jens Axboe561fb042019-10-24 07:25:42 -06005235 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06005236 do {
Pavel Begunkov014db002020-03-03 21:33:12 +03005237 ret = io_issue_sqe(req, NULL, false);
Jens Axboe561fb042019-10-24 07:25:42 -06005238 /*
5239 * We can get EAGAIN for polled IO even though we're
5240 * forcing a sync submission from here, since we can't
5241 * wait for request slots on the block side.
5242 */
5243 if (ret != -EAGAIN)
5244 break;
5245 cond_resched();
5246 } while (1);
5247 }
Jens Axboe31b51512019-01-18 22:56:34 -07005248
Jens Axboe561fb042019-10-24 07:25:42 -06005249 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005250 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07005251 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06005252 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07005253 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005254
Pavel Begunkove9fd9392020-03-04 16:14:12 +03005255 io_steal_work(req, workptr);
Jens Axboe31b51512019-01-18 22:56:34 -07005256}
Jens Axboe2b188cc2019-01-07 10:46:33 -07005257
Jens Axboe15b71ab2019-12-11 11:20:36 -07005258static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe9e3aa612019-12-11 15:55:43 -07005259{
Jens Axboed3656342019-12-18 09:50:26 -07005260 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07005261 return 0;
Jens Axboe0b5faf62020-02-06 21:42:51 -07005262 if ((fd == -1 || fd == AT_FDCWD) && io_op_defs[req->opcode].fd_non_neg)
Jens Axboed3656342019-12-18 09:50:26 -07005263 return 0;
5264 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06005265}
5266
Jens Axboe65e19f52019-10-26 07:20:21 -06005267static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
5268 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06005269{
Jens Axboe65e19f52019-10-26 07:20:21 -06005270 struct fixed_file_table *table;
5271
Jens Axboe05f3fb32019-12-09 11:22:50 -07005272 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
5273 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06005274}
5275
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005276static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
5277 int fd, struct file **out_file, bool fixed)
5278{
5279 struct io_ring_ctx *ctx = req->ctx;
5280 struct file *file;
5281
5282 if (fixed) {
5283 if (unlikely(!ctx->file_data ||
5284 (unsigned) fd >= ctx->nr_user_files))
5285 return -EBADF;
5286 fd = array_index_nospec(fd, ctx->nr_user_files);
5287 file = io_file_from_index(ctx, fd);
5288 if (!file)
5289 return -EBADF;
5290 percpu_ref_get(&ctx->file_data->refs);
5291 } else {
5292 trace_io_uring_file_get(ctx, fd);
5293 file = __io_file_get(state, fd);
5294 if (unlikely(!file))
5295 return -EBADF;
5296 }
5297
5298 *out_file = file;
5299 return 0;
5300}
5301
Jens Axboe3529d8c2019-12-19 18:24:38 -07005302static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
5303 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06005304{
5305 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07005306 int fd;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005307 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06005308
Jens Axboe3529d8c2019-12-19 18:24:38 -07005309 flags = READ_ONCE(sqe->flags);
5310 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06005311
Jens Axboed3656342019-12-18 09:50:26 -07005312 if (!io_req_needs_file(req, fd))
5313 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06005314
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005315 fixed = (flags & IOSQE_FIXED_FILE);
5316 if (unlikely(!fixed && req->needs_fixed_file))
5317 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06005318
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005319 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06005320}
5321
Jackie Liua197f662019-11-08 08:09:12 -07005322static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005323{
Jens Axboefcb323c2019-10-24 12:39:47 -06005324 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07005325 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06005326
Jens Axboef86cd202020-01-29 13:46:44 -07005327 if (req->work.files)
5328 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005329 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07005330 return -EBADF;
5331
Jens Axboefcb323c2019-10-24 12:39:47 -06005332 rcu_read_lock();
5333 spin_lock_irq(&ctx->inflight_lock);
5334 /*
5335 * We use the f_ops->flush() handler to ensure that we can flush
5336 * out work accessing these files if the fd is closed. Check if
5337 * the fd has changed since we started down this path, and disallow
5338 * this operation if it has.
5339 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005340 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06005341 list_add(&req->inflight_entry, &ctx->inflight_list);
5342 req->flags |= REQ_F_INFLIGHT;
5343 req->work.files = current->files;
5344 ret = 0;
5345 }
5346 spin_unlock_irq(&ctx->inflight_lock);
5347 rcu_read_unlock();
5348
5349 return ret;
5350}
5351
Jens Axboe2665abf2019-11-05 12:40:47 -07005352static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
5353{
Jens Axboead8a48a2019-11-15 08:49:11 -07005354 struct io_timeout_data *data = container_of(timer,
5355 struct io_timeout_data, timer);
5356 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07005357 struct io_ring_ctx *ctx = req->ctx;
5358 struct io_kiocb *prev = NULL;
5359 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07005360
5361 spin_lock_irqsave(&ctx->completion_lock, flags);
5362
5363 /*
5364 * We don't expect the list to be empty, that will only happen if we
5365 * race with the completion of the linked work.
5366 */
Pavel Begunkov44932332019-12-05 16:16:35 +03005367 if (!list_empty(&req->link_list)) {
5368 prev = list_entry(req->link_list.prev, struct io_kiocb,
5369 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005370 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03005371 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005372 prev->flags &= ~REQ_F_LINK_TIMEOUT;
5373 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07005374 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005375 }
5376
5377 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5378
5379 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005380 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03005381 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07005382 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07005383 } else {
5384 io_cqring_add_event(req, -ETIME);
5385 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005386 }
Jens Axboe2665abf2019-11-05 12:40:47 -07005387 return HRTIMER_NORESTART;
5388}
5389
Jens Axboead8a48a2019-11-15 08:49:11 -07005390static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005391{
Jens Axboe76a46e02019-11-10 23:34:16 -07005392 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07005393
Jens Axboe76a46e02019-11-10 23:34:16 -07005394 /*
5395 * If the list is now empty, then our linked request finished before
5396 * we got a chance to setup the timer
5397 */
5398 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03005399 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07005400 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07005401
Jens Axboead8a48a2019-11-15 08:49:11 -07005402 data->timer.function = io_link_timeout_fn;
5403 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
5404 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07005405 }
Jens Axboe76a46e02019-11-10 23:34:16 -07005406 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07005407
Jens Axboe2665abf2019-11-05 12:40:47 -07005408 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07005409 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005410}
5411
Jens Axboead8a48a2019-11-15 08:49:11 -07005412static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005413{
5414 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005415
Jens Axboe2665abf2019-11-05 12:40:47 -07005416 if (!(req->flags & REQ_F_LINK))
5417 return NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005418 /* for polled retry, if flag is set, we already went through here */
5419 if (req->flags & REQ_F_POLLED)
5420 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005421
Pavel Begunkov44932332019-12-05 16:16:35 +03005422 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
5423 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07005424 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07005425 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005426
Jens Axboe76a46e02019-11-10 23:34:16 -07005427 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07005428 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07005429}
5430
Jens Axboe3529d8c2019-12-19 18:24:38 -07005431static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005432{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005433 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005434 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07005435 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005436 int ret;
5437
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005438again:
5439 linked_timeout = io_prep_linked_timeout(req);
5440
Jens Axboe193155c2020-02-22 23:22:19 -07005441 if (req->work.creds && req->work.creds != current_cred()) {
5442 if (old_creds)
5443 revert_creds(old_creds);
5444 if (old_creds == req->work.creds)
5445 old_creds = NULL; /* restored original creds */
5446 else
5447 old_creds = override_creds(req->work.creds);
5448 }
5449
Pavel Begunkov014db002020-03-03 21:33:12 +03005450 ret = io_issue_sqe(req, sqe, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06005451
5452 /*
5453 * We async punt it if the file wasn't marked NOWAIT, or if the file
5454 * doesn't support non-blocking read/write attempts
5455 */
5456 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
5457 (req->flags & REQ_F_MUST_PUNT))) {
Jens Axboed7718a92020-02-14 22:23:12 -07005458 if (io_arm_poll_handler(req)) {
5459 if (linked_timeout)
5460 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005461 goto exit;
Jens Axboed7718a92020-02-14 22:23:12 -07005462 }
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005463punt:
Jens Axboef86cd202020-01-29 13:46:44 -07005464 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005465 ret = io_grab_files(req);
5466 if (ret)
5467 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005468 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005469
5470 /*
5471 * Queued up for async execution, worker will release
5472 * submit reference when the iocb is actually submitted.
5473 */
5474 io_queue_async_work(req);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005475 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005476 }
Jens Axboee65ef562019-03-12 10:16:44 -06005477
Jens Axboefcb323c2019-10-24 12:39:47 -06005478err:
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005479 nxt = NULL;
Jens Axboee65ef562019-03-12 10:16:44 -06005480 /* drop submission reference */
Jens Axboe2a44f462020-02-25 13:25:41 -07005481 io_put_req_find_next(req, &nxt);
Jens Axboee65ef562019-03-12 10:16:44 -06005482
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005483 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07005484 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005485 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005486 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005487 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005488 }
5489
Jens Axboee65ef562019-03-12 10:16:44 -06005490 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06005491 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07005492 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005493 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06005494 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06005495 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005496 if (nxt) {
5497 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005498
5499 if (req->flags & REQ_F_FORCE_ASYNC)
5500 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005501 goto again;
5502 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005503exit:
Jens Axboe193155c2020-02-22 23:22:19 -07005504 if (old_creds)
5505 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005506}
5507
Jens Axboe3529d8c2019-12-19 18:24:38 -07005508static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005509{
5510 int ret;
5511
Jens Axboe3529d8c2019-12-19 18:24:38 -07005512 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005513 if (ret) {
5514 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03005515fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005516 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005517 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07005518 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005519 }
Pavel Begunkov25508782019-12-30 21:24:47 +03005520 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkov11185912020-01-22 23:09:35 +03005521 ret = io_req_defer_prep(req, sqe);
5522 if (unlikely(ret < 0))
5523 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07005524 /*
5525 * Never try inline submit of IOSQE_ASYNC is set, go straight
5526 * to async execution.
5527 */
5528 req->work.flags |= IO_WQ_WORK_CONCURRENT;
5529 io_queue_async_work(req);
5530 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005531 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07005532 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08005533}
5534
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005535static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005536{
Jens Axboe94ae5e72019-11-14 19:39:52 -07005537 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005538 io_cqring_add_event(req, -ECANCELED);
5539 io_double_put_req(req);
5540 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07005541 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005542}
5543
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005544#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboebcda7ba2020-02-23 16:42:51 -07005545 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
5546 IOSQE_BUFFER_SELECT)
Jens Axboe9e645e112019-05-10 16:07:28 -06005547
Jens Axboe3529d8c2019-12-19 18:24:38 -07005548static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
5549 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06005550{
Jackie Liua197f662019-11-08 08:09:12 -07005551 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005552 unsigned int sqe_flags;
Jens Axboe75c6a032020-01-28 10:15:23 -07005553 int ret, id;
Jens Axboe9e645e112019-05-10 16:07:28 -06005554
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005555 sqe_flags = READ_ONCE(sqe->flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06005556
5557 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005558 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06005559 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03005560 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06005561 }
5562
Jens Axboebcda7ba2020-02-23 16:42:51 -07005563 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
5564 !io_op_defs[req->opcode].buffer_select) {
5565 ret = -EOPNOTSUPP;
5566 goto err_req;
5567 }
5568
Jens Axboe75c6a032020-01-28 10:15:23 -07005569 id = READ_ONCE(sqe->personality);
5570 if (id) {
Jens Axboe193155c2020-02-22 23:22:19 -07005571 req->work.creds = idr_find(&ctx->personality_idr, id);
5572 if (unlikely(!req->work.creds)) {
Jens Axboe75c6a032020-01-28 10:15:23 -07005573 ret = -EINVAL;
5574 goto err_req;
5575 }
Jens Axboe193155c2020-02-22 23:22:19 -07005576 get_cred(req->work.creds);
Jens Axboe75c6a032020-01-28 10:15:23 -07005577 }
5578
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03005579 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005580 req->flags |= sqe_flags & (IOSQE_IO_DRAIN | IOSQE_IO_HARDLINK |
Jens Axboebcda7ba2020-02-23 16:42:51 -07005581 IOSQE_ASYNC | IOSQE_FIXED_FILE |
5582 IOSQE_BUFFER_SELECT);
Jens Axboe9e645e112019-05-10 16:07:28 -06005583
Jens Axboe3529d8c2019-12-19 18:24:38 -07005584 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06005585 if (unlikely(ret)) {
5586err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005587 io_cqring_add_event(req, ret);
5588 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005589 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06005590 }
5591
Jens Axboe9e645e112019-05-10 16:07:28 -06005592 /*
5593 * If we already have a head request, queue this one for async
5594 * submittal once the head completes. If we don't have a head but
5595 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
5596 * submitted sync once the chain is complete. If none of those
5597 * conditions are true (normal request), then just queue it.
5598 */
5599 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03005600 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06005601
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03005602 /*
5603 * Taking sequential execution of a link, draining both sides
5604 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
5605 * requests in the link. So, it drains the head and the
5606 * next after the link request. The last one is done via
5607 * drain_next flag to persist the effect across calls.
5608 */
Pavel Begunkov711be032020-01-17 03:57:59 +03005609 if (sqe_flags & IOSQE_IO_DRAIN) {
5610 head->flags |= REQ_F_IO_DRAIN;
5611 ctx->drain_next = 1;
5612 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005613 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06005614 ret = -EAGAIN;
5615 goto err_req;
5616 }
5617
Jens Axboe3529d8c2019-12-19 18:24:38 -07005618 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005619 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005620 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03005621 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07005622 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07005623 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03005624 trace_io_uring_link(ctx, req, head);
5625 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06005626
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005627 /* last request of a link, enqueue the link */
5628 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
5629 io_queue_link_head(head);
5630 *link = NULL;
5631 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005632 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03005633 if (unlikely(ctx->drain_next)) {
5634 req->flags |= REQ_F_IO_DRAIN;
5635 req->ctx->drain_next = 0;
5636 }
5637 if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
5638 req->flags |= REQ_F_LINK;
Pavel Begunkov711be032020-01-17 03:57:59 +03005639 INIT_LIST_HEAD(&req->link_list);
5640 ret = io_req_defer_prep(req, sqe);
5641 if (ret)
5642 req->flags |= REQ_F_FAIL_LINK;
5643 *link = req;
5644 } else {
5645 io_queue_sqe(req, sqe);
5646 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005647 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005648
5649 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06005650}
5651
Jens Axboe9a56a232019-01-09 09:06:50 -07005652/*
5653 * Batched submission is done, ensure local IO is flushed out.
5654 */
5655static void io_submit_state_end(struct io_submit_state *state)
5656{
5657 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06005658 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07005659 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03005660 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07005661}
5662
5663/*
5664 * Start submission side cache.
5665 */
5666static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08005667 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07005668{
5669 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07005670 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07005671 state->file = NULL;
5672 state->ios_left = max_ios;
5673}
5674
Jens Axboe2b188cc2019-01-07 10:46:33 -07005675static void io_commit_sqring(struct io_ring_ctx *ctx)
5676{
Hristo Venev75b28af2019-08-26 17:23:46 +00005677 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005678
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03005679 /*
5680 * Ensure any loads from the SQEs are done at this point,
5681 * since once we write the new head, the application could
5682 * write new data to them.
5683 */
5684 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005685}
5686
5687/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07005688 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07005689 * that is mapped by userspace. This means that care needs to be taken to
5690 * ensure that reads are stable, as we cannot rely on userspace always
5691 * being a good citizen. If members of the sqe are validated and then later
5692 * used, it's important that those reads are done through READ_ONCE() to
5693 * prevent a re-load down the line.
5694 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07005695static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
5696 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005697{
Hristo Venev75b28af2019-08-26 17:23:46 +00005698 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005699 unsigned head;
5700
5701 /*
5702 * The cached sq head (or cq tail) serves two purposes:
5703 *
5704 * 1) allows us to batch the cost of updating the user visible
5705 * head updates.
5706 * 2) allows the kernel side to track the head on its own, even
5707 * though the application is the one updating it.
5708 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005709 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03005710 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03005711 /*
5712 * All io need record the previous position, if LINK vs DARIN,
5713 * it can be used to mark the position of the first IO in the
5714 * link list.
5715 */
5716 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07005717 *sqe_ptr = &ctx->sq_sqes[head];
5718 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
5719 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005720 ctx->cached_sq_head++;
5721 return true;
5722 }
5723
5724 /* drop invalid entries */
5725 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06005726 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005727 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005728 return false;
5729}
5730
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005731static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005732 struct file *ring_file, int ring_fd,
5733 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07005734{
5735 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005736 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005737 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005738 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005739
Jens Axboec4a2ed72019-11-21 21:01:26 -07005740 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07005741 if (test_bit(0, &ctx->sq_check_overflow)) {
5742 if (!list_empty(&ctx->cq_overflow_list) &&
5743 !io_cqring_overflow_flush(ctx, false))
5744 return -EBUSY;
5745 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005746
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005747 /* make sure SQ entry isn't read before tail */
5748 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03005749
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005750 if (!percpu_ref_tryget_many(&ctx->refs, nr))
5751 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005752
5753 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08005754 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005755 statep = &state;
5756 }
5757
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005758 ctx->ring_fd = ring_fd;
5759 ctx->ring_file = ring_file;
5760
Jens Axboe6c271ce2019-01-10 11:22:30 -07005761 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005762 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03005763 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005764 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005765
Pavel Begunkov196be952019-11-07 01:41:06 +03005766 req = io_get_req(ctx, statep);
5767 if (unlikely(!req)) {
5768 if (!submitted)
5769 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005770 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06005771 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07005772 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005773 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03005774 break;
5775 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005776
Jens Axboed3656342019-12-18 09:50:26 -07005777 /* will complete beyond this point, count as submitted */
5778 submitted++;
5779
5780 if (unlikely(req->opcode >= IORING_OP_LAST)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005781 err = -EINVAL;
5782fail_req:
5783 io_cqring_add_event(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07005784 io_double_put_req(req);
5785 break;
5786 }
5787
5788 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005789 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005790 if (unlikely(mm_fault)) {
5791 err = -EFAULT;
5792 goto fail_req;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005793 }
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005794 use_mm(ctx->sqo_mm);
5795 *mm = ctx->sqo_mm;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005796 }
5797
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03005798 req->needs_fixed_file = async;
Jens Axboe354420f2020-01-08 18:55:15 -07005799 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
5800 true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07005801 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005802 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005803 }
5804
Pavel Begunkov9466f432020-01-25 22:34:01 +03005805 if (unlikely(submitted != nr)) {
5806 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
5807
5808 percpu_ref_put_many(&ctx->refs, nr - ref_used);
5809 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005810 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005811 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005812 if (statep)
5813 io_submit_state_end(&state);
5814
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005815 /* Commit SQ ring head once we've consumed and submitted all SQEs */
5816 io_commit_sqring(ctx);
5817
Jens Axboe6c271ce2019-01-10 11:22:30 -07005818 return submitted;
5819}
5820
5821static int io_sq_thread(void *data)
5822{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005823 struct io_ring_ctx *ctx = data;
5824 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07005825 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005826 mm_segment_t old_fs;
5827 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005828 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005829 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005830
Jens Axboe206aefd2019-11-07 18:27:42 -07005831 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08005832
Jens Axboe6c271ce2019-01-10 11:22:30 -07005833 old_fs = get_fs();
5834 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07005835 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005836
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005837 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005838 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005839 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005840
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005841 if (!list_empty(&ctx->poll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005842 unsigned nr_events = 0;
5843
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005844 mutex_lock(&ctx->uring_lock);
5845 if (!list_empty(&ctx->poll_list))
5846 io_iopoll_getevents(ctx, &nr_events, 0);
5847 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07005848 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005849 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005850 }
5851
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005852 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005853
5854 /*
5855 * If submit got -EBUSY, flag us as needing the application
5856 * to enter the kernel to reap and flush events.
5857 */
5858 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005859 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01005860 * Drop cur_mm before scheduling, we can't hold it for
5861 * long periods (or over schedule()). Do this before
5862 * adding ourselves to the waitqueue, as the unuse/drop
5863 * may sleep.
5864 */
5865 if (cur_mm) {
5866 unuse_mm(cur_mm);
5867 mmput(cur_mm);
5868 cur_mm = NULL;
5869 }
5870
5871 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07005872 * We're polling. If we're within the defined idle
5873 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07005874 * to sleep. The exception is if we got EBUSY doing
5875 * more IO, we should wait for the application to
5876 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07005877 */
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005878 if (!list_empty(&ctx->poll_list) ||
Jens Axboedf069d82020-02-04 16:48:34 -07005879 (!time_after(jiffies, timeout) && ret != -EBUSY &&
5880 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005881 if (current->task_works)
5882 task_work_run();
Jens Axboe9831a902019-09-19 09:48:55 -06005883 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005884 continue;
5885 }
5886
Jens Axboe6c271ce2019-01-10 11:22:30 -07005887 prepare_to_wait(&ctx->sqo_wait, &wait,
5888 TASK_INTERRUPTIBLE);
5889
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005890 /*
5891 * While doing polled IO, before going to sleep, we need
5892 * to check if there are new reqs added to poll_list, it
5893 * is because reqs may have been punted to io worker and
5894 * will be added to poll_list later, hence check the
5895 * poll_list again.
5896 */
5897 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
5898 !list_empty_careful(&ctx->poll_list)) {
5899 finish_wait(&ctx->sqo_wait, &wait);
5900 continue;
5901 }
5902
Jens Axboe6c271ce2019-01-10 11:22:30 -07005903 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00005904 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02005905 /* make sure to read SQ tail after writing flags */
5906 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005907
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005908 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005909 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005910 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005911 finish_wait(&ctx->sqo_wait, &wait);
5912 break;
5913 }
Jens Axboeb41e9852020-02-17 09:52:41 -07005914 if (current->task_works) {
5915 task_work_run();
5916 continue;
5917 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005918 if (signal_pending(current))
5919 flush_signals(current);
5920 schedule();
5921 finish_wait(&ctx->sqo_wait, &wait);
5922
Hristo Venev75b28af2019-08-26 17:23:46 +00005923 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005924 continue;
5925 }
5926 finish_wait(&ctx->sqo_wait, &wait);
5927
Hristo Venev75b28af2019-08-26 17:23:46 +00005928 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005929 }
5930
Jens Axboe8a4955f2019-12-09 14:52:35 -07005931 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005932 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07005933 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005934 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005935 }
5936
Jens Axboeb41e9852020-02-17 09:52:41 -07005937 if (current->task_works)
5938 task_work_run();
5939
Jens Axboe6c271ce2019-01-10 11:22:30 -07005940 set_fs(old_fs);
5941 if (cur_mm) {
5942 unuse_mm(cur_mm);
5943 mmput(cur_mm);
5944 }
Jens Axboe181e4482019-11-25 08:52:30 -07005945 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06005946
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005947 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06005948
Jens Axboe6c271ce2019-01-10 11:22:30 -07005949 return 0;
5950}
5951
Jens Axboebda52162019-09-24 13:47:15 -06005952struct io_wait_queue {
5953 struct wait_queue_entry wq;
5954 struct io_ring_ctx *ctx;
5955 unsigned to_wait;
5956 unsigned nr_timeouts;
5957};
5958
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005959static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06005960{
5961 struct io_ring_ctx *ctx = iowq->ctx;
5962
5963 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08005964 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06005965 * started waiting. For timeouts, we always want to return to userspace,
5966 * regardless of event count.
5967 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005968 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06005969 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
5970}
5971
5972static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
5973 int wake_flags, void *key)
5974{
5975 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
5976 wq);
5977
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005978 /* use noflush == true, as we can't safely rely on locking context */
5979 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06005980 return -1;
5981
5982 return autoremove_wake_function(curr, mode, wake_flags, key);
5983}
5984
Jens Axboe2b188cc2019-01-07 10:46:33 -07005985/*
5986 * Wait until events become available, if we don't already have some. The
5987 * application must reap them itself, as they reside on the shared cq ring.
5988 */
5989static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
5990 const sigset_t __user *sig, size_t sigsz)
5991{
Jens Axboebda52162019-09-24 13:47:15 -06005992 struct io_wait_queue iowq = {
5993 .wq = {
5994 .private = current,
5995 .func = io_wake_function,
5996 .entry = LIST_HEAD_INIT(iowq.wq.entry),
5997 },
5998 .ctx = ctx,
5999 .to_wait = min_events,
6000 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006001 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006002 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006003
Jens Axboeb41e9852020-02-17 09:52:41 -07006004 do {
6005 if (io_cqring_events(ctx, false) >= min_events)
6006 return 0;
6007 if (!current->task_works)
6008 break;
6009 task_work_run();
6010 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006011
6012 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006013#ifdef CONFIG_COMPAT
6014 if (in_compat_syscall())
6015 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006016 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006017 else
6018#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006019 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006020
Jens Axboe2b188cc2019-01-07 10:46:33 -07006021 if (ret)
6022 return ret;
6023 }
6024
Jens Axboebda52162019-09-24 13:47:15 -06006025 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006026 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006027 do {
6028 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6029 TASK_INTERRUPTIBLE);
Jens Axboeb41e9852020-02-17 09:52:41 -07006030 if (current->task_works)
6031 task_work_run();
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006032 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06006033 break;
6034 schedule();
6035 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006036 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06006037 break;
6038 }
6039 } while (1);
6040 finish_wait(&ctx->wait, &iowq.wq);
6041
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006042 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006043
Hristo Venev75b28af2019-08-26 17:23:46 +00006044 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006045}
6046
Jens Axboe6b063142019-01-10 22:13:58 -07006047static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6048{
6049#if defined(CONFIG_UNIX)
6050 if (ctx->ring_sock) {
6051 struct sock *sock = ctx->ring_sock->sk;
6052 struct sk_buff *skb;
6053
6054 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6055 kfree_skb(skb);
6056 }
6057#else
6058 int i;
6059
Jens Axboe65e19f52019-10-26 07:20:21 -06006060 for (i = 0; i < ctx->nr_user_files; i++) {
6061 struct file *file;
6062
6063 file = io_file_from_index(ctx, i);
6064 if (file)
6065 fput(file);
6066 }
Jens Axboe6b063142019-01-10 22:13:58 -07006067#endif
6068}
6069
Jens Axboe05f3fb32019-12-09 11:22:50 -07006070static void io_file_ref_kill(struct percpu_ref *ref)
6071{
6072 struct fixed_file_data *data;
6073
6074 data = container_of(ref, struct fixed_file_data, refs);
6075 complete(&data->done);
6076}
6077
Jens Axboe6b063142019-01-10 22:13:58 -07006078static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6079{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006080 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06006081 unsigned nr_tables, i;
6082
Jens Axboe05f3fb32019-12-09 11:22:50 -07006083 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006084 return -ENXIO;
6085
Jens Axboe05f3fb32019-12-09 11:22:50 -07006086 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
Jens Axboee46a7952020-01-17 11:15:34 -07006087 flush_work(&data->ref_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07006088 wait_for_completion(&data->done);
6089 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006090 percpu_ref_exit(&data->refs);
6091
Jens Axboe6b063142019-01-10 22:13:58 -07006092 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06006093 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6094 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006095 kfree(data->table[i].files);
6096 kfree(data->table);
6097 kfree(data);
6098 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006099 ctx->nr_user_files = 0;
6100 return 0;
6101}
6102
Jens Axboe6c271ce2019-01-10 11:22:30 -07006103static void io_sq_thread_stop(struct io_ring_ctx *ctx)
6104{
6105 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07006106 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006107 /*
6108 * The park is a bit of a work-around, without it we get
6109 * warning spews on shutdown with SQPOLL set and affinity
6110 * set to a single CPU.
6111 */
Jens Axboe06058632019-04-13 09:26:03 -06006112 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006113 kthread_stop(ctx->sqo_thread);
6114 ctx->sqo_thread = NULL;
6115 }
6116}
6117
Jens Axboe6b063142019-01-10 22:13:58 -07006118static void io_finish_async(struct io_ring_ctx *ctx)
6119{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006120 io_sq_thread_stop(ctx);
6121
Jens Axboe561fb042019-10-24 07:25:42 -06006122 if (ctx->io_wq) {
6123 io_wq_destroy(ctx->io_wq);
6124 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006125 }
6126}
6127
6128#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07006129/*
6130 * Ensure the UNIX gc is aware of our file set, so we are certain that
6131 * the io_uring can be safely unregistered on process exit, even if we have
6132 * loops in the file referencing.
6133 */
6134static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
6135{
6136 struct sock *sk = ctx->ring_sock->sk;
6137 struct scm_fp_list *fpl;
6138 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06006139 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07006140
6141 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
6142 unsigned long inflight = ctx->user->unix_inflight + nr;
6143
6144 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
6145 return -EMFILE;
6146 }
6147
6148 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
6149 if (!fpl)
6150 return -ENOMEM;
6151
6152 skb = alloc_skb(0, GFP_KERNEL);
6153 if (!skb) {
6154 kfree(fpl);
6155 return -ENOMEM;
6156 }
6157
6158 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07006159
Jens Axboe08a45172019-10-03 08:11:03 -06006160 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07006161 fpl->user = get_uid(ctx->user);
6162 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006163 struct file *file = io_file_from_index(ctx, i + offset);
6164
6165 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06006166 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06006167 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06006168 unix_inflight(fpl->user, fpl->fp[nr_files]);
6169 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07006170 }
6171
Jens Axboe08a45172019-10-03 08:11:03 -06006172 if (nr_files) {
6173 fpl->max = SCM_MAX_FD;
6174 fpl->count = nr_files;
6175 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006176 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06006177 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
6178 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07006179
Jens Axboe08a45172019-10-03 08:11:03 -06006180 for (i = 0; i < nr_files; i++)
6181 fput(fpl->fp[i]);
6182 } else {
6183 kfree_skb(skb);
6184 kfree(fpl);
6185 }
Jens Axboe6b063142019-01-10 22:13:58 -07006186
6187 return 0;
6188}
6189
6190/*
6191 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
6192 * causes regular reference counting to break down. We rely on the UNIX
6193 * garbage collection to take care of this problem for us.
6194 */
6195static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6196{
6197 unsigned left, total;
6198 int ret = 0;
6199
6200 total = 0;
6201 left = ctx->nr_user_files;
6202 while (left) {
6203 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07006204
6205 ret = __io_sqe_files_scm(ctx, this_files, total);
6206 if (ret)
6207 break;
6208 left -= this_files;
6209 total += this_files;
6210 }
6211
6212 if (!ret)
6213 return 0;
6214
6215 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006216 struct file *file = io_file_from_index(ctx, total);
6217
6218 if (file)
6219 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07006220 total++;
6221 }
6222
6223 return ret;
6224}
6225#else
6226static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6227{
6228 return 0;
6229}
6230#endif
6231
Jens Axboe65e19f52019-10-26 07:20:21 -06006232static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
6233 unsigned nr_files)
6234{
6235 int i;
6236
6237 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006238 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006239 unsigned this_files;
6240
6241 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
6242 table->files = kcalloc(this_files, sizeof(struct file *),
6243 GFP_KERNEL);
6244 if (!table->files)
6245 break;
6246 nr_files -= this_files;
6247 }
6248
6249 if (i == nr_tables)
6250 return 0;
6251
6252 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006253 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006254 kfree(table->files);
6255 }
6256 return 1;
6257}
6258
Jens Axboe05f3fb32019-12-09 11:22:50 -07006259static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06006260{
6261#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06006262 struct sock *sock = ctx->ring_sock->sk;
6263 struct sk_buff_head list, *head = &sock->sk_receive_queue;
6264 struct sk_buff *skb;
6265 int i;
6266
6267 __skb_queue_head_init(&list);
6268
6269 /*
6270 * Find the skb that holds this file in its SCM_RIGHTS. When found,
6271 * remove this entry and rearrange the file array.
6272 */
6273 skb = skb_dequeue(head);
6274 while (skb) {
6275 struct scm_fp_list *fp;
6276
6277 fp = UNIXCB(skb).fp;
6278 for (i = 0; i < fp->count; i++) {
6279 int left;
6280
6281 if (fp->fp[i] != file)
6282 continue;
6283
6284 unix_notinflight(fp->user, fp->fp[i]);
6285 left = fp->count - 1 - i;
6286 if (left) {
6287 memmove(&fp->fp[i], &fp->fp[i + 1],
6288 left * sizeof(struct file *));
6289 }
6290 fp->count--;
6291 if (!fp->count) {
6292 kfree_skb(skb);
6293 skb = NULL;
6294 } else {
6295 __skb_queue_tail(&list, skb);
6296 }
6297 fput(file);
6298 file = NULL;
6299 break;
6300 }
6301
6302 if (!file)
6303 break;
6304
6305 __skb_queue_tail(&list, skb);
6306
6307 skb = skb_dequeue(head);
6308 }
6309
6310 if (skb_peek(&list)) {
6311 spin_lock_irq(&head->lock);
6312 while ((skb = __skb_dequeue(&list)) != NULL)
6313 __skb_queue_tail(head, skb);
6314 spin_unlock_irq(&head->lock);
6315 }
6316#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07006317 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06006318#endif
6319}
6320
Jens Axboe05f3fb32019-12-09 11:22:50 -07006321struct io_file_put {
6322 struct llist_node llist;
6323 struct file *file;
6324 struct completion *done;
6325};
6326
Jens Axboe2faf8522020-02-04 19:54:55 -07006327static void io_ring_file_ref_flush(struct fixed_file_data *data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006328{
6329 struct io_file_put *pfile, *tmp;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006330 struct llist_node *node;
6331
Jens Axboe05f3fb32019-12-09 11:22:50 -07006332 while ((node = llist_del_all(&data->put_llist)) != NULL) {
6333 llist_for_each_entry_safe(pfile, tmp, node, llist) {
6334 io_ring_file_put(data->ctx, pfile->file);
6335 if (pfile->done)
6336 complete(pfile->done);
6337 else
6338 kfree(pfile);
6339 }
6340 }
Jens Axboe2faf8522020-02-04 19:54:55 -07006341}
Jens Axboe05f3fb32019-12-09 11:22:50 -07006342
Jens Axboe2faf8522020-02-04 19:54:55 -07006343static void io_ring_file_ref_switch(struct work_struct *work)
6344{
6345 struct fixed_file_data *data;
6346
6347 data = container_of(work, struct fixed_file_data, ref_work);
6348 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006349 percpu_ref_switch_to_percpu(&data->refs);
6350}
6351
6352static void io_file_data_ref_zero(struct percpu_ref *ref)
6353{
6354 struct fixed_file_data *data;
6355
6356 data = container_of(ref, struct fixed_file_data, refs);
6357
Jens Axboe2faf8522020-02-04 19:54:55 -07006358 /*
6359 * We can't safely switch from inside this context, punt to wq. If
6360 * the table ref is going away, the table is being unregistered.
6361 * Don't queue up the async work for that case, the caller will
6362 * handle it.
6363 */
6364 if (!percpu_ref_is_dying(&data->refs))
6365 queue_work(system_wq, &data->ref_work);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006366}
6367
6368static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
6369 unsigned nr_args)
6370{
6371 __s32 __user *fds = (__s32 __user *) arg;
6372 unsigned nr_tables;
6373 struct file *file;
6374 int fd, ret = 0;
6375 unsigned i;
6376
6377 if (ctx->file_data)
6378 return -EBUSY;
6379 if (!nr_args)
6380 return -EINVAL;
6381 if (nr_args > IORING_MAX_FIXED_FILES)
6382 return -EMFILE;
6383
6384 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
6385 if (!ctx->file_data)
6386 return -ENOMEM;
6387 ctx->file_data->ctx = ctx;
6388 init_completion(&ctx->file_data->done);
6389
6390 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
6391 ctx->file_data->table = kcalloc(nr_tables,
6392 sizeof(struct fixed_file_table),
6393 GFP_KERNEL);
6394 if (!ctx->file_data->table) {
6395 kfree(ctx->file_data);
6396 ctx->file_data = NULL;
6397 return -ENOMEM;
6398 }
6399
6400 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
6401 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
6402 kfree(ctx->file_data->table);
6403 kfree(ctx->file_data);
6404 ctx->file_data = NULL;
6405 return -ENOMEM;
6406 }
6407 ctx->file_data->put_llist.first = NULL;
6408 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
6409
6410 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
6411 percpu_ref_exit(&ctx->file_data->refs);
6412 kfree(ctx->file_data->table);
6413 kfree(ctx->file_data);
6414 ctx->file_data = NULL;
6415 return -ENOMEM;
6416 }
6417
6418 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
6419 struct fixed_file_table *table;
6420 unsigned index;
6421
6422 ret = -EFAULT;
6423 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
6424 break;
6425 /* allow sparse sets */
6426 if (fd == -1) {
6427 ret = 0;
6428 continue;
6429 }
6430
6431 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6432 index = i & IORING_FILE_TABLE_MASK;
6433 file = fget(fd);
6434
6435 ret = -EBADF;
6436 if (!file)
6437 break;
6438
6439 /*
6440 * Don't allow io_uring instances to be registered. If UNIX
6441 * isn't enabled, then this causes a reference cycle and this
6442 * instance can never get freed. If UNIX is enabled we'll
6443 * handle it just fine, but there's still no point in allowing
6444 * a ring fd as it doesn't support regular read/write anyway.
6445 */
6446 if (file->f_op == &io_uring_fops) {
6447 fput(file);
6448 break;
6449 }
6450 ret = 0;
6451 table->files[index] = file;
6452 }
6453
6454 if (ret) {
6455 for (i = 0; i < ctx->nr_user_files; i++) {
6456 file = io_file_from_index(ctx, i);
6457 if (file)
6458 fput(file);
6459 }
6460 for (i = 0; i < nr_tables; i++)
6461 kfree(ctx->file_data->table[i].files);
6462
6463 kfree(ctx->file_data->table);
6464 kfree(ctx->file_data);
6465 ctx->file_data = NULL;
6466 ctx->nr_user_files = 0;
6467 return ret;
6468 }
6469
6470 ret = io_sqe_files_scm(ctx);
6471 if (ret)
6472 io_sqe_files_unregister(ctx);
6473
6474 return ret;
6475}
6476
Jens Axboec3a31e62019-10-03 13:59:56 -06006477static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
6478 int index)
6479{
6480#if defined(CONFIG_UNIX)
6481 struct sock *sock = ctx->ring_sock->sk;
6482 struct sk_buff_head *head = &sock->sk_receive_queue;
6483 struct sk_buff *skb;
6484
6485 /*
6486 * See if we can merge this file into an existing skb SCM_RIGHTS
6487 * file set. If there's no room, fall back to allocating a new skb
6488 * and filling it in.
6489 */
6490 spin_lock_irq(&head->lock);
6491 skb = skb_peek(head);
6492 if (skb) {
6493 struct scm_fp_list *fpl = UNIXCB(skb).fp;
6494
6495 if (fpl->count < SCM_MAX_FD) {
6496 __skb_unlink(skb, head);
6497 spin_unlock_irq(&head->lock);
6498 fpl->fp[fpl->count] = get_file(file);
6499 unix_inflight(fpl->user, fpl->fp[fpl->count]);
6500 fpl->count++;
6501 spin_lock_irq(&head->lock);
6502 __skb_queue_head(head, skb);
6503 } else {
6504 skb = NULL;
6505 }
6506 }
6507 spin_unlock_irq(&head->lock);
6508
6509 if (skb) {
6510 fput(file);
6511 return 0;
6512 }
6513
6514 return __io_sqe_files_scm(ctx, 1, index);
6515#else
6516 return 0;
6517#endif
6518}
6519
Jens Axboe05f3fb32019-12-09 11:22:50 -07006520static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06006521{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006522 struct fixed_file_data *data;
6523
Jens Axboedd3db2a2020-02-26 10:23:43 -07006524 /*
6525 * Juggle reference to ensure we hit zero, if needed, so we can
6526 * switch back to percpu mode
6527 */
Jens Axboe05f3fb32019-12-09 11:22:50 -07006528 data = container_of(ref, struct fixed_file_data, refs);
Jens Axboedd3db2a2020-02-26 10:23:43 -07006529 percpu_ref_put(&data->refs);
6530 percpu_ref_get(&data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006531}
6532
6533static bool io_queue_file_removal(struct fixed_file_data *data,
6534 struct file *file)
6535{
6536 struct io_file_put *pfile, pfile_stack;
6537 DECLARE_COMPLETION_ONSTACK(done);
6538
6539 /*
6540 * If we fail allocating the struct we need for doing async reomval
6541 * of this file, just punt to sync and wait for it.
6542 */
6543 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
6544 if (!pfile) {
6545 pfile = &pfile_stack;
6546 pfile->done = &done;
6547 }
6548
6549 pfile->file = file;
6550 llist_add(&pfile->llist, &data->put_llist);
6551
6552 if (pfile == &pfile_stack) {
Jens Axboedd3db2a2020-02-26 10:23:43 -07006553 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006554 wait_for_completion(&done);
6555 flush_work(&data->ref_work);
6556 return false;
6557 }
6558
6559 return true;
6560}
6561
6562static int __io_sqe_files_update(struct io_ring_ctx *ctx,
6563 struct io_uring_files_update *up,
6564 unsigned nr_args)
6565{
6566 struct fixed_file_data *data = ctx->file_data;
6567 bool ref_switch = false;
6568 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006569 __s32 __user *fds;
6570 int fd, i, err;
6571 __u32 done;
6572
Jens Axboe05f3fb32019-12-09 11:22:50 -07006573 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06006574 return -EOVERFLOW;
6575 if (done > ctx->nr_user_files)
6576 return -EINVAL;
6577
6578 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006579 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06006580 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006581 struct fixed_file_table *table;
6582 unsigned index;
6583
Jens Axboec3a31e62019-10-03 13:59:56 -06006584 err = 0;
6585 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
6586 err = -EFAULT;
6587 break;
6588 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006589 i = array_index_nospec(up->offset, ctx->nr_user_files);
6590 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06006591 index = i & IORING_FILE_TABLE_MASK;
6592 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006593 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06006594 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006595 if (io_queue_file_removal(data, file))
6596 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06006597 }
6598 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06006599 file = fget(fd);
6600 if (!file) {
6601 err = -EBADF;
6602 break;
6603 }
6604 /*
6605 * Don't allow io_uring instances to be registered. If
6606 * UNIX isn't enabled, then this causes a reference
6607 * cycle and this instance can never get freed. If UNIX
6608 * is enabled we'll handle it just fine, but there's
6609 * still no point in allowing a ring fd as it doesn't
6610 * support regular read/write anyway.
6611 */
6612 if (file->f_op == &io_uring_fops) {
6613 fput(file);
6614 err = -EBADF;
6615 break;
6616 }
Jens Axboe65e19f52019-10-26 07:20:21 -06006617 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006618 err = io_sqe_file_register(ctx, file, i);
6619 if (err)
6620 break;
6621 }
6622 nr_args--;
6623 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006624 up->offset++;
6625 }
6626
Jens Axboedd3db2a2020-02-26 10:23:43 -07006627 if (ref_switch)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006628 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06006629
6630 return done ? done : err;
6631}
Jens Axboe05f3fb32019-12-09 11:22:50 -07006632static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
6633 unsigned nr_args)
6634{
6635 struct io_uring_files_update up;
6636
6637 if (!ctx->file_data)
6638 return -ENXIO;
6639 if (!nr_args)
6640 return -EINVAL;
6641 if (copy_from_user(&up, arg, sizeof(up)))
6642 return -EFAULT;
6643 if (up.resv)
6644 return -EINVAL;
6645
6646 return __io_sqe_files_update(ctx, &up, nr_args);
6647}
Jens Axboec3a31e62019-10-03 13:59:56 -06006648
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006649static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07006650{
6651 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6652
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006653 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07006654 io_put_req(req);
6655}
6656
Pavel Begunkov24369c22020-01-28 03:15:48 +03006657static int io_init_wq_offload(struct io_ring_ctx *ctx,
6658 struct io_uring_params *p)
6659{
6660 struct io_wq_data data;
6661 struct fd f;
6662 struct io_ring_ctx *ctx_attach;
6663 unsigned int concurrency;
6664 int ret = 0;
6665
6666 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006667 data.free_work = io_free_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03006668
6669 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
6670 /* Do QD, or 4 * CPUS, whatever is smallest */
6671 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
6672
6673 ctx->io_wq = io_wq_create(concurrency, &data);
6674 if (IS_ERR(ctx->io_wq)) {
6675 ret = PTR_ERR(ctx->io_wq);
6676 ctx->io_wq = NULL;
6677 }
6678 return ret;
6679 }
6680
6681 f = fdget(p->wq_fd);
6682 if (!f.file)
6683 return -EBADF;
6684
6685 if (f.file->f_op != &io_uring_fops) {
6686 ret = -EINVAL;
6687 goto out_fput;
6688 }
6689
6690 ctx_attach = f.file->private_data;
6691 /* @io_wq is protected by holding the fd */
6692 if (!io_wq_get(ctx_attach->io_wq, &data)) {
6693 ret = -EINVAL;
6694 goto out_fput;
6695 }
6696
6697 ctx->io_wq = ctx_attach->io_wq;
6698out_fput:
6699 fdput(f);
6700 return ret;
6701}
6702
Jens Axboe6c271ce2019-01-10 11:22:30 -07006703static int io_sq_offload_start(struct io_ring_ctx *ctx,
6704 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006705{
6706 int ret;
6707
Jens Axboe6c271ce2019-01-10 11:22:30 -07006708 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006709 mmgrab(current->mm);
6710 ctx->sqo_mm = current->mm;
6711
Jens Axboe6c271ce2019-01-10 11:22:30 -07006712 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06006713 ret = -EPERM;
6714 if (!capable(CAP_SYS_ADMIN))
6715 goto err;
6716
Jens Axboe917257d2019-04-13 09:28:55 -06006717 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
6718 if (!ctx->sq_thread_idle)
6719 ctx->sq_thread_idle = HZ;
6720
Jens Axboe6c271ce2019-01-10 11:22:30 -07006721 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06006722 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006723
Jens Axboe917257d2019-04-13 09:28:55 -06006724 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06006725 if (cpu >= nr_cpu_ids)
6726 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08006727 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06006728 goto err;
6729
Jens Axboe6c271ce2019-01-10 11:22:30 -07006730 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
6731 ctx, cpu,
6732 "io_uring-sq");
6733 } else {
6734 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
6735 "io_uring-sq");
6736 }
6737 if (IS_ERR(ctx->sqo_thread)) {
6738 ret = PTR_ERR(ctx->sqo_thread);
6739 ctx->sqo_thread = NULL;
6740 goto err;
6741 }
6742 wake_up_process(ctx->sqo_thread);
6743 } else if (p->flags & IORING_SETUP_SQ_AFF) {
6744 /* Can't have SQ_AFF without SQPOLL */
6745 ret = -EINVAL;
6746 goto err;
6747 }
6748
Pavel Begunkov24369c22020-01-28 03:15:48 +03006749 ret = io_init_wq_offload(ctx, p);
6750 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006751 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006752
6753 return 0;
6754err:
Jens Axboe54a91f32019-09-10 09:15:04 -06006755 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006756 mmdrop(ctx->sqo_mm);
6757 ctx->sqo_mm = NULL;
6758 return ret;
6759}
6760
6761static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
6762{
6763 atomic_long_sub(nr_pages, &user->locked_vm);
6764}
6765
6766static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
6767{
6768 unsigned long page_limit, cur_pages, new_pages;
6769
6770 /* Don't allow more pages than we can safely lock */
6771 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
6772
6773 do {
6774 cur_pages = atomic_long_read(&user->locked_vm);
6775 new_pages = cur_pages + nr_pages;
6776 if (new_pages > page_limit)
6777 return -ENOMEM;
6778 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
6779 new_pages) != cur_pages);
6780
6781 return 0;
6782}
6783
6784static void io_mem_free(void *ptr)
6785{
Mark Rutland52e04ef2019-04-30 17:30:21 +01006786 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006787
Mark Rutland52e04ef2019-04-30 17:30:21 +01006788 if (!ptr)
6789 return;
6790
6791 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006792 if (put_page_testzero(page))
6793 free_compound_page(page);
6794}
6795
6796static void *io_mem_alloc(size_t size)
6797{
6798 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
6799 __GFP_NORETRY;
6800
6801 return (void *) __get_free_pages(gfp_flags, get_order(size));
6802}
6803
Hristo Venev75b28af2019-08-26 17:23:46 +00006804static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
6805 size_t *sq_offset)
6806{
6807 struct io_rings *rings;
6808 size_t off, sq_array_size;
6809
6810 off = struct_size(rings, cqes, cq_entries);
6811 if (off == SIZE_MAX)
6812 return SIZE_MAX;
6813
6814#ifdef CONFIG_SMP
6815 off = ALIGN(off, SMP_CACHE_BYTES);
6816 if (off == 0)
6817 return SIZE_MAX;
6818#endif
6819
6820 sq_array_size = array_size(sizeof(u32), sq_entries);
6821 if (sq_array_size == SIZE_MAX)
6822 return SIZE_MAX;
6823
6824 if (check_add_overflow(off, sq_array_size, &off))
6825 return SIZE_MAX;
6826
6827 if (sq_offset)
6828 *sq_offset = off;
6829
6830 return off;
6831}
6832
Jens Axboe2b188cc2019-01-07 10:46:33 -07006833static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
6834{
Hristo Venev75b28af2019-08-26 17:23:46 +00006835 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006836
Hristo Venev75b28af2019-08-26 17:23:46 +00006837 pages = (size_t)1 << get_order(
6838 rings_size(sq_entries, cq_entries, NULL));
6839 pages += (size_t)1 << get_order(
6840 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07006841
Hristo Venev75b28af2019-08-26 17:23:46 +00006842 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006843}
6844
Jens Axboeedafcce2019-01-09 09:16:05 -07006845static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
6846{
6847 int i, j;
6848
6849 if (!ctx->user_bufs)
6850 return -ENXIO;
6851
6852 for (i = 0; i < ctx->nr_user_bufs; i++) {
6853 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6854
6855 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006856 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07006857
6858 if (ctx->account_mem)
6859 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006860 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006861 imu->nr_bvecs = 0;
6862 }
6863
6864 kfree(ctx->user_bufs);
6865 ctx->user_bufs = NULL;
6866 ctx->nr_user_bufs = 0;
6867 return 0;
6868}
6869
6870static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
6871 void __user *arg, unsigned index)
6872{
6873 struct iovec __user *src;
6874
6875#ifdef CONFIG_COMPAT
6876 if (ctx->compat) {
6877 struct compat_iovec __user *ciovs;
6878 struct compat_iovec ciov;
6879
6880 ciovs = (struct compat_iovec __user *) arg;
6881 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
6882 return -EFAULT;
6883
Jens Axboed55e5f52019-12-11 16:12:15 -07006884 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07006885 dst->iov_len = ciov.iov_len;
6886 return 0;
6887 }
6888#endif
6889 src = (struct iovec __user *) arg;
6890 if (copy_from_user(dst, &src[index], sizeof(*dst)))
6891 return -EFAULT;
6892 return 0;
6893}
6894
6895static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
6896 unsigned nr_args)
6897{
6898 struct vm_area_struct **vmas = NULL;
6899 struct page **pages = NULL;
6900 int i, j, got_pages = 0;
6901 int ret = -EINVAL;
6902
6903 if (ctx->user_bufs)
6904 return -EBUSY;
6905 if (!nr_args || nr_args > UIO_MAXIOV)
6906 return -EINVAL;
6907
6908 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
6909 GFP_KERNEL);
6910 if (!ctx->user_bufs)
6911 return -ENOMEM;
6912
6913 for (i = 0; i < nr_args; i++) {
6914 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6915 unsigned long off, start, end, ubuf;
6916 int pret, nr_pages;
6917 struct iovec iov;
6918 size_t size;
6919
6920 ret = io_copy_iov(ctx, &iov, arg, i);
6921 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03006922 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07006923
6924 /*
6925 * Don't impose further limits on the size and buffer
6926 * constraints here, we'll -EINVAL later when IO is
6927 * submitted if they are wrong.
6928 */
6929 ret = -EFAULT;
6930 if (!iov.iov_base || !iov.iov_len)
6931 goto err;
6932
6933 /* arbitrary limit, but we need something */
6934 if (iov.iov_len > SZ_1G)
6935 goto err;
6936
6937 ubuf = (unsigned long) iov.iov_base;
6938 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
6939 start = ubuf >> PAGE_SHIFT;
6940 nr_pages = end - start;
6941
6942 if (ctx->account_mem) {
6943 ret = io_account_mem(ctx->user, nr_pages);
6944 if (ret)
6945 goto err;
6946 }
6947
6948 ret = 0;
6949 if (!pages || nr_pages > got_pages) {
6950 kfree(vmas);
6951 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006952 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07006953 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006954 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07006955 sizeof(struct vm_area_struct *),
6956 GFP_KERNEL);
6957 if (!pages || !vmas) {
6958 ret = -ENOMEM;
6959 if (ctx->account_mem)
6960 io_unaccount_mem(ctx->user, nr_pages);
6961 goto err;
6962 }
6963 got_pages = nr_pages;
6964 }
6965
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006966 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07006967 GFP_KERNEL);
6968 ret = -ENOMEM;
6969 if (!imu->bvec) {
6970 if (ctx->account_mem)
6971 io_unaccount_mem(ctx->user, nr_pages);
6972 goto err;
6973 }
6974
6975 ret = 0;
6976 down_read(&current->mm->mmap_sem);
John Hubbard2113b052020-01-30 22:13:13 -08006977 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07006978 FOLL_WRITE | FOLL_LONGTERM,
6979 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006980 if (pret == nr_pages) {
6981 /* don't support file backed memory */
6982 for (j = 0; j < nr_pages; j++) {
6983 struct vm_area_struct *vma = vmas[j];
6984
6985 if (vma->vm_file &&
6986 !is_file_hugepages(vma->vm_file)) {
6987 ret = -EOPNOTSUPP;
6988 break;
6989 }
6990 }
6991 } else {
6992 ret = pret < 0 ? pret : -EFAULT;
6993 }
6994 up_read(&current->mm->mmap_sem);
6995 if (ret) {
6996 /*
6997 * if we did partial map, or found file backed vmas,
6998 * release any pages we did get
6999 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07007000 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007001 unpin_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07007002 if (ctx->account_mem)
7003 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007004 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007005 goto err;
7006 }
7007
7008 off = ubuf & ~PAGE_MASK;
7009 size = iov.iov_len;
7010 for (j = 0; j < nr_pages; j++) {
7011 size_t vec_len;
7012
7013 vec_len = min_t(size_t, size, PAGE_SIZE - off);
7014 imu->bvec[j].bv_page = pages[j];
7015 imu->bvec[j].bv_len = vec_len;
7016 imu->bvec[j].bv_offset = off;
7017 off = 0;
7018 size -= vec_len;
7019 }
7020 /* store original address for later verification */
7021 imu->ubuf = ubuf;
7022 imu->len = iov.iov_len;
7023 imu->nr_bvecs = nr_pages;
7024
7025 ctx->nr_user_bufs++;
7026 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007027 kvfree(pages);
7028 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007029 return 0;
7030err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007031 kvfree(pages);
7032 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007033 io_sqe_buffer_unregister(ctx);
7034 return ret;
7035}
7036
Jens Axboe9b402842019-04-11 11:45:41 -06007037static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
7038{
7039 __s32 __user *fds = arg;
7040 int fd;
7041
7042 if (ctx->cq_ev_fd)
7043 return -EBUSY;
7044
7045 if (copy_from_user(&fd, fds, sizeof(*fds)))
7046 return -EFAULT;
7047
7048 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
7049 if (IS_ERR(ctx->cq_ev_fd)) {
7050 int ret = PTR_ERR(ctx->cq_ev_fd);
7051 ctx->cq_ev_fd = NULL;
7052 return ret;
7053 }
7054
7055 return 0;
7056}
7057
7058static int io_eventfd_unregister(struct io_ring_ctx *ctx)
7059{
7060 if (ctx->cq_ev_fd) {
7061 eventfd_ctx_put(ctx->cq_ev_fd);
7062 ctx->cq_ev_fd = NULL;
7063 return 0;
7064 }
7065
7066 return -ENXIO;
7067}
7068
Jens Axboe5a2e7452020-02-23 16:23:11 -07007069static int __io_destroy_buffers(int id, void *p, void *data)
7070{
7071 struct io_ring_ctx *ctx = data;
7072 struct io_buffer *buf = p;
7073
Jens Axboe067524e2020-03-02 16:32:28 -07007074 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007075 return 0;
7076}
7077
7078static void io_destroy_buffers(struct io_ring_ctx *ctx)
7079{
7080 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
7081 idr_destroy(&ctx->io_buffer_idr);
7082}
7083
Jens Axboe2b188cc2019-01-07 10:46:33 -07007084static void io_ring_ctx_free(struct io_ring_ctx *ctx)
7085{
Jens Axboe6b063142019-01-10 22:13:58 -07007086 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007087 if (ctx->sqo_mm)
7088 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07007089
7090 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07007091 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07007092 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06007093 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007094 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07007095 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07007096
Jens Axboe2b188cc2019-01-07 10:46:33 -07007097#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07007098 if (ctx->ring_sock) {
7099 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007100 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07007101 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007102#endif
7103
Hristo Venev75b28af2019-08-26 17:23:46 +00007104 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007105 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007106
7107 percpu_ref_exit(&ctx->refs);
7108 if (ctx->account_mem)
7109 io_unaccount_mem(ctx->user,
7110 ring_pages(ctx->sq_entries, ctx->cq_entries));
7111 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07007112 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07007113 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07007114 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07007115 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007116 kfree(ctx);
7117}
7118
7119static __poll_t io_uring_poll(struct file *file, poll_table *wait)
7120{
7121 struct io_ring_ctx *ctx = file->private_data;
7122 __poll_t mask = 0;
7123
7124 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02007125 /*
7126 * synchronizes with barrier from wq_has_sleeper call in
7127 * io_commit_cqring
7128 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007129 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00007130 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
7131 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007132 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01007133 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007134 mask |= EPOLLIN | EPOLLRDNORM;
7135
7136 return mask;
7137}
7138
7139static int io_uring_fasync(int fd, struct file *file, int on)
7140{
7141 struct io_ring_ctx *ctx = file->private_data;
7142
7143 return fasync_helper(fd, file, on, &ctx->cq_fasync);
7144}
7145
Jens Axboe071698e2020-01-28 10:04:42 -07007146static int io_remove_personalities(int id, void *p, void *data)
7147{
7148 struct io_ring_ctx *ctx = data;
7149 const struct cred *cred;
7150
7151 cred = idr_remove(&ctx->personality_idr, id);
7152 if (cred)
7153 put_cred(cred);
7154 return 0;
7155}
7156
Jens Axboe2b188cc2019-01-07 10:46:33 -07007157static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
7158{
7159 mutex_lock(&ctx->uring_lock);
7160 percpu_ref_kill(&ctx->refs);
7161 mutex_unlock(&ctx->uring_lock);
7162
Jens Axboedf069d82020-02-04 16:48:34 -07007163 /*
7164 * Wait for sq thread to idle, if we have one. It won't spin on new
7165 * work after we've killed the ctx ref above. This is important to do
7166 * before we cancel existing commands, as the thread could otherwise
7167 * be queueing new work post that. If that's work we need to cancel,
7168 * it could cause shutdown to hang.
7169 */
7170 while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait))
7171 cpu_relax();
7172
Jens Axboe5262f562019-09-17 12:26:57 -06007173 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07007174 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06007175
7176 if (ctx->io_wq)
7177 io_wq_cancel_all(ctx->io_wq);
7178
Jens Axboedef596e2019-01-09 08:59:42 -07007179 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07007180 /* if we failed setting up the ctx, we might not have any rings */
7181 if (ctx->rings)
7182 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07007183 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe206aefd2019-11-07 18:27:42 -07007184 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007185 io_ring_ctx_free(ctx);
7186}
7187
7188static int io_uring_release(struct inode *inode, struct file *file)
7189{
7190 struct io_ring_ctx *ctx = file->private_data;
7191
7192 file->private_data = NULL;
7193 io_ring_ctx_wait_and_kill(ctx);
7194 return 0;
7195}
7196
Jens Axboefcb323c2019-10-24 12:39:47 -06007197static void io_uring_cancel_files(struct io_ring_ctx *ctx,
7198 struct files_struct *files)
7199{
7200 struct io_kiocb *req;
7201 DEFINE_WAIT(wait);
7202
7203 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07007204 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06007205
7206 spin_lock_irq(&ctx->inflight_lock);
7207 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07007208 if (req->work.files != files)
7209 continue;
7210 /* req is being completed, ignore */
7211 if (!refcount_inc_not_zero(&req->refs))
7212 continue;
7213 cancel_req = req;
7214 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06007215 }
Jens Axboe768134d2019-11-10 20:30:53 -07007216 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007217 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07007218 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06007219 spin_unlock_irq(&ctx->inflight_lock);
7220
Jens Axboe768134d2019-11-10 20:30:53 -07007221 /* We need to keep going until we don't find a matching req */
7222 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007223 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08007224
Jens Axboe2ca10252020-02-13 17:17:35 -07007225 if (cancel_req->flags & REQ_F_OVERFLOW) {
7226 spin_lock_irq(&ctx->completion_lock);
7227 list_del(&cancel_req->list);
7228 cancel_req->flags &= ~REQ_F_OVERFLOW;
7229 if (list_empty(&ctx->cq_overflow_list)) {
7230 clear_bit(0, &ctx->sq_check_overflow);
7231 clear_bit(0, &ctx->cq_check_overflow);
7232 }
7233 spin_unlock_irq(&ctx->completion_lock);
7234
7235 WRITE_ONCE(ctx->rings->cq_overflow,
7236 atomic_inc_return(&ctx->cached_cq_overflow));
7237
7238 /*
7239 * Put inflight ref and overflow ref. If that's
7240 * all we had, then we're done with this request.
7241 */
7242 if (refcount_sub_and_test(2, &cancel_req->refs)) {
7243 io_put_req(cancel_req);
7244 continue;
7245 }
7246 }
7247
Bob Liu2f6d9b92019-11-13 18:06:24 +08007248 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
7249 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06007250 schedule();
7251 }
Jens Axboe768134d2019-11-10 20:30:53 -07007252 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007253}
7254
7255static int io_uring_flush(struct file *file, void *data)
7256{
7257 struct io_ring_ctx *ctx = file->private_data;
7258
7259 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07007260
7261 /*
7262 * If the task is going away, cancel work it may have pending
7263 */
7264 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
7265 io_wq_cancel_pid(ctx->io_wq, task_pid_vnr(current));
7266
Jens Axboefcb323c2019-10-24 12:39:47 -06007267 return 0;
7268}
7269
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007270static void *io_uring_validate_mmap_request(struct file *file,
7271 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007272{
Jens Axboe2b188cc2019-01-07 10:46:33 -07007273 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007274 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007275 struct page *page;
7276 void *ptr;
7277
7278 switch (offset) {
7279 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00007280 case IORING_OFF_CQ_RING:
7281 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007282 break;
7283 case IORING_OFF_SQES:
7284 ptr = ctx->sq_sqes;
7285 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007286 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007287 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007288 }
7289
7290 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07007291 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007292 return ERR_PTR(-EINVAL);
7293
7294 return ptr;
7295}
7296
7297#ifdef CONFIG_MMU
7298
7299static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7300{
7301 size_t sz = vma->vm_end - vma->vm_start;
7302 unsigned long pfn;
7303 void *ptr;
7304
7305 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
7306 if (IS_ERR(ptr))
7307 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007308
7309 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
7310 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
7311}
7312
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007313#else /* !CONFIG_MMU */
7314
7315static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7316{
7317 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
7318}
7319
7320static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
7321{
7322 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
7323}
7324
7325static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
7326 unsigned long addr, unsigned long len,
7327 unsigned long pgoff, unsigned long flags)
7328{
7329 void *ptr;
7330
7331 ptr = io_uring_validate_mmap_request(file, pgoff, len);
7332 if (IS_ERR(ptr))
7333 return PTR_ERR(ptr);
7334
7335 return (unsigned long) ptr;
7336}
7337
7338#endif /* !CONFIG_MMU */
7339
Jens Axboe2b188cc2019-01-07 10:46:33 -07007340SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
7341 u32, min_complete, u32, flags, const sigset_t __user *, sig,
7342 size_t, sigsz)
7343{
7344 struct io_ring_ctx *ctx;
7345 long ret = -EBADF;
7346 int submitted = 0;
7347 struct fd f;
7348
Jens Axboeb41e9852020-02-17 09:52:41 -07007349 if (current->task_works)
7350 task_work_run();
7351
Jens Axboe6c271ce2019-01-10 11:22:30 -07007352 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007353 return -EINVAL;
7354
7355 f = fdget(fd);
7356 if (!f.file)
7357 return -EBADF;
7358
7359 ret = -EOPNOTSUPP;
7360 if (f.file->f_op != &io_uring_fops)
7361 goto out_fput;
7362
7363 ret = -ENXIO;
7364 ctx = f.file->private_data;
7365 if (!percpu_ref_tryget(&ctx->refs))
7366 goto out_fput;
7367
Jens Axboe6c271ce2019-01-10 11:22:30 -07007368 /*
7369 * For SQ polling, the thread will do all submissions and completions.
7370 * Just return the requested submit count, and wake the thread if
7371 * we were asked to.
7372 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007373 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007374 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07007375 if (!list_empty_careful(&ctx->cq_overflow_list))
7376 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007377 if (flags & IORING_ENTER_SQ_WAKEUP)
7378 wake_up(&ctx->sqo_wait);
7379 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007380 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03007381 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007382
7383 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03007384 /* already have mm, so io_submit_sqes() won't try to grab it */
7385 cur_mm = ctx->sqo_mm;
7386 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
7387 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007388 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007389
7390 if (submitted != to_submit)
7391 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007392 }
7393 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07007394 unsigned nr_events = 0;
7395
Jens Axboe2b188cc2019-01-07 10:46:33 -07007396 min_complete = min(min_complete, ctx->cq_entries);
7397
Jens Axboedef596e2019-01-09 08:59:42 -07007398 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07007399 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07007400 } else {
7401 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
7402 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007403 }
7404
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007405out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03007406 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007407out_fput:
7408 fdput(f);
7409 return submitted ? submitted : ret;
7410}
7411
Tobias Klauserbebdb652020-02-26 18:38:32 +01007412#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07007413static int io_uring_show_cred(int id, void *p, void *data)
7414{
7415 const struct cred *cred = p;
7416 struct seq_file *m = data;
7417 struct user_namespace *uns = seq_user_ns(m);
7418 struct group_info *gi;
7419 kernel_cap_t cap;
7420 unsigned __capi;
7421 int g;
7422
7423 seq_printf(m, "%5d\n", id);
7424 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
7425 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
7426 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
7427 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
7428 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
7429 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
7430 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
7431 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
7432 seq_puts(m, "\n\tGroups:\t");
7433 gi = cred->group_info;
7434 for (g = 0; g < gi->ngroups; g++) {
7435 seq_put_decimal_ull(m, g ? " " : "",
7436 from_kgid_munged(uns, gi->gid[g]));
7437 }
7438 seq_puts(m, "\n\tCapEff:\t");
7439 cap = cred->cap_effective;
7440 CAP_FOR_EACH_U32(__capi)
7441 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
7442 seq_putc(m, '\n');
7443 return 0;
7444}
7445
7446static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
7447{
7448 int i;
7449
7450 mutex_lock(&ctx->uring_lock);
7451 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
7452 for (i = 0; i < ctx->nr_user_files; i++) {
7453 struct fixed_file_table *table;
7454 struct file *f;
7455
7456 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7457 f = table->files[i & IORING_FILE_TABLE_MASK];
7458 if (f)
7459 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
7460 else
7461 seq_printf(m, "%5u: <none>\n", i);
7462 }
7463 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
7464 for (i = 0; i < ctx->nr_user_bufs; i++) {
7465 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
7466
7467 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
7468 (unsigned int) buf->len);
7469 }
7470 if (!idr_is_empty(&ctx->personality_idr)) {
7471 seq_printf(m, "Personalities:\n");
7472 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
7473 }
Jens Axboed7718a92020-02-14 22:23:12 -07007474 seq_printf(m, "PollList:\n");
7475 spin_lock_irq(&ctx->completion_lock);
7476 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
7477 struct hlist_head *list = &ctx->cancel_hash[i];
7478 struct io_kiocb *req;
7479
7480 hlist_for_each_entry(req, list, hash_node)
7481 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
7482 req->task->task_works != NULL);
7483 }
7484 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07007485 mutex_unlock(&ctx->uring_lock);
7486}
7487
7488static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
7489{
7490 struct io_ring_ctx *ctx = f->private_data;
7491
7492 if (percpu_ref_tryget(&ctx->refs)) {
7493 __io_uring_show_fdinfo(ctx, m);
7494 percpu_ref_put(&ctx->refs);
7495 }
7496}
Tobias Klauserbebdb652020-02-26 18:38:32 +01007497#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07007498
Jens Axboe2b188cc2019-01-07 10:46:33 -07007499static const struct file_operations io_uring_fops = {
7500 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06007501 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07007502 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007503#ifndef CONFIG_MMU
7504 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
7505 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
7506#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07007507 .poll = io_uring_poll,
7508 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01007509#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07007510 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01007511#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07007512};
7513
7514static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
7515 struct io_uring_params *p)
7516{
Hristo Venev75b28af2019-08-26 17:23:46 +00007517 struct io_rings *rings;
7518 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007519
Hristo Venev75b28af2019-08-26 17:23:46 +00007520 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
7521 if (size == SIZE_MAX)
7522 return -EOVERFLOW;
7523
7524 rings = io_mem_alloc(size);
7525 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007526 return -ENOMEM;
7527
Hristo Venev75b28af2019-08-26 17:23:46 +00007528 ctx->rings = rings;
7529 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
7530 rings->sq_ring_mask = p->sq_entries - 1;
7531 rings->cq_ring_mask = p->cq_entries - 1;
7532 rings->sq_ring_entries = p->sq_entries;
7533 rings->cq_ring_entries = p->cq_entries;
7534 ctx->sq_mask = rings->sq_ring_mask;
7535 ctx->cq_mask = rings->cq_ring_mask;
7536 ctx->sq_entries = rings->sq_ring_entries;
7537 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007538
7539 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07007540 if (size == SIZE_MAX) {
7541 io_mem_free(ctx->rings);
7542 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007543 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07007544 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007545
7546 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07007547 if (!ctx->sq_sqes) {
7548 io_mem_free(ctx->rings);
7549 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007550 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07007551 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007552
Jens Axboe2b188cc2019-01-07 10:46:33 -07007553 return 0;
7554}
7555
7556/*
7557 * Allocate an anonymous fd, this is what constitutes the application
7558 * visible backing of an io_uring instance. The application mmaps this
7559 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
7560 * we have to tie this fd to a socket for file garbage collection purposes.
7561 */
7562static int io_uring_get_fd(struct io_ring_ctx *ctx)
7563{
7564 struct file *file;
7565 int ret;
7566
7567#if defined(CONFIG_UNIX)
7568 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
7569 &ctx->ring_sock);
7570 if (ret)
7571 return ret;
7572#endif
7573
7574 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
7575 if (ret < 0)
7576 goto err;
7577
7578 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
7579 O_RDWR | O_CLOEXEC);
7580 if (IS_ERR(file)) {
7581 put_unused_fd(ret);
7582 ret = PTR_ERR(file);
7583 goto err;
7584 }
7585
7586#if defined(CONFIG_UNIX)
7587 ctx->ring_sock->file = file;
7588#endif
7589 fd_install(ret, file);
7590 return ret;
7591err:
7592#if defined(CONFIG_UNIX)
7593 sock_release(ctx->ring_sock);
7594 ctx->ring_sock = NULL;
7595#endif
7596 return ret;
7597}
7598
7599static int io_uring_create(unsigned entries, struct io_uring_params *p)
7600{
7601 struct user_struct *user = NULL;
7602 struct io_ring_ctx *ctx;
7603 bool account_mem;
7604 int ret;
7605
Jens Axboe8110c1a2019-12-28 15:39:54 -07007606 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007607 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07007608 if (entries > IORING_MAX_ENTRIES) {
7609 if (!(p->flags & IORING_SETUP_CLAMP))
7610 return -EINVAL;
7611 entries = IORING_MAX_ENTRIES;
7612 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007613
7614 /*
7615 * Use twice as many entries for the CQ ring. It's possible for the
7616 * application to drive a higher depth than the size of the SQ ring,
7617 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06007618 * some flexibility in overcommitting a bit. If the application has
7619 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
7620 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07007621 */
7622 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06007623 if (p->flags & IORING_SETUP_CQSIZE) {
7624 /*
7625 * If IORING_SETUP_CQSIZE is set, we do the same roundup
7626 * to a power-of-two, if it isn't already. We do NOT impose
7627 * any cq vs sq ring sizing.
7628 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07007629 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06007630 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07007631 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
7632 if (!(p->flags & IORING_SETUP_CLAMP))
7633 return -EINVAL;
7634 p->cq_entries = IORING_MAX_CQ_ENTRIES;
7635 }
Jens Axboe33a107f2019-10-04 12:10:03 -06007636 p->cq_entries = roundup_pow_of_two(p->cq_entries);
7637 } else {
7638 p->cq_entries = 2 * p->sq_entries;
7639 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007640
7641 user = get_uid(current_user());
7642 account_mem = !capable(CAP_IPC_LOCK);
7643
7644 if (account_mem) {
7645 ret = io_account_mem(user,
7646 ring_pages(p->sq_entries, p->cq_entries));
7647 if (ret) {
7648 free_uid(user);
7649 return ret;
7650 }
7651 }
7652
7653 ctx = io_ring_ctx_alloc(p);
7654 if (!ctx) {
7655 if (account_mem)
7656 io_unaccount_mem(user, ring_pages(p->sq_entries,
7657 p->cq_entries));
7658 free_uid(user);
7659 return -ENOMEM;
7660 }
7661 ctx->compat = in_compat_syscall();
7662 ctx->account_mem = account_mem;
7663 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07007664 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07007665
7666 ret = io_allocate_scq_urings(ctx, p);
7667 if (ret)
7668 goto err;
7669
Jens Axboe6c271ce2019-01-10 11:22:30 -07007670 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007671 if (ret)
7672 goto err;
7673
Jens Axboe2b188cc2019-01-07 10:46:33 -07007674 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007675 p->sq_off.head = offsetof(struct io_rings, sq.head);
7676 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
7677 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
7678 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
7679 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
7680 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
7681 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007682
7683 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007684 p->cq_off.head = offsetof(struct io_rings, cq.head);
7685 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
7686 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
7687 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
7688 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
7689 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06007690
Jens Axboe044c1ab2019-10-28 09:15:33 -06007691 /*
7692 * Install ring fd as the very last thing, so we don't risk someone
7693 * having closed it before we finish setup
7694 */
7695 ret = io_uring_get_fd(ctx);
7696 if (ret < 0)
7697 goto err;
7698
Jens Axboeda8c9692019-12-02 18:51:26 -07007699 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboecccf0ee2020-01-27 16:34:48 -07007700 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jens Axboed7718a92020-02-14 22:23:12 -07007701 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007702 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007703 return ret;
7704err:
7705 io_ring_ctx_wait_and_kill(ctx);
7706 return ret;
7707}
7708
7709/*
7710 * Sets up an aio uring context, and returns the fd. Applications asks for a
7711 * ring size, we return the actual sq/cq ring sizes (among other things) in the
7712 * params structure passed in.
7713 */
7714static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
7715{
7716 struct io_uring_params p;
7717 long ret;
7718 int i;
7719
7720 if (copy_from_user(&p, params, sizeof(p)))
7721 return -EFAULT;
7722 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
7723 if (p.resv[i])
7724 return -EINVAL;
7725 }
7726
Jens Axboe6c271ce2019-01-10 11:22:30 -07007727 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07007728 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03007729 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007730 return -EINVAL;
7731
7732 ret = io_uring_create(entries, &p);
7733 if (ret < 0)
7734 return ret;
7735
7736 if (copy_to_user(params, &p, sizeof(p)))
7737 return -EFAULT;
7738
7739 return ret;
7740}
7741
7742SYSCALL_DEFINE2(io_uring_setup, u32, entries,
7743 struct io_uring_params __user *, params)
7744{
7745 return io_uring_setup(entries, params);
7746}
7747
Jens Axboe66f4af92020-01-16 15:36:52 -07007748static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
7749{
7750 struct io_uring_probe *p;
7751 size_t size;
7752 int i, ret;
7753
7754 size = struct_size(p, ops, nr_args);
7755 if (size == SIZE_MAX)
7756 return -EOVERFLOW;
7757 p = kzalloc(size, GFP_KERNEL);
7758 if (!p)
7759 return -ENOMEM;
7760
7761 ret = -EFAULT;
7762 if (copy_from_user(p, arg, size))
7763 goto out;
7764 ret = -EINVAL;
7765 if (memchr_inv(p, 0, size))
7766 goto out;
7767
7768 p->last_op = IORING_OP_LAST - 1;
7769 if (nr_args > IORING_OP_LAST)
7770 nr_args = IORING_OP_LAST;
7771
7772 for (i = 0; i < nr_args; i++) {
7773 p->ops[i].op = i;
7774 if (!io_op_defs[i].not_supported)
7775 p->ops[i].flags = IO_URING_OP_SUPPORTED;
7776 }
7777 p->ops_len = i;
7778
7779 ret = 0;
7780 if (copy_to_user(arg, p, size))
7781 ret = -EFAULT;
7782out:
7783 kfree(p);
7784 return ret;
7785}
7786
Jens Axboe071698e2020-01-28 10:04:42 -07007787static int io_register_personality(struct io_ring_ctx *ctx)
7788{
7789 const struct cred *creds = get_current_cred();
7790 int id;
7791
7792 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
7793 USHRT_MAX, GFP_KERNEL);
7794 if (id < 0)
7795 put_cred(creds);
7796 return id;
7797}
7798
7799static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
7800{
7801 const struct cred *old_creds;
7802
7803 old_creds = idr_remove(&ctx->personality_idr, id);
7804 if (old_creds) {
7805 put_cred(old_creds);
7806 return 0;
7807 }
7808
7809 return -EINVAL;
7810}
7811
7812static bool io_register_op_must_quiesce(int op)
7813{
7814 switch (op) {
7815 case IORING_UNREGISTER_FILES:
7816 case IORING_REGISTER_FILES_UPDATE:
7817 case IORING_REGISTER_PROBE:
7818 case IORING_REGISTER_PERSONALITY:
7819 case IORING_UNREGISTER_PERSONALITY:
7820 return false;
7821 default:
7822 return true;
7823 }
7824}
7825
Jens Axboeedafcce2019-01-09 09:16:05 -07007826static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
7827 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06007828 __releases(ctx->uring_lock)
7829 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07007830{
7831 int ret;
7832
Jens Axboe35fa71a2019-04-22 10:23:23 -06007833 /*
7834 * We're inside the ring mutex, if the ref is already dying, then
7835 * someone else killed the ctx or is already going through
7836 * io_uring_register().
7837 */
7838 if (percpu_ref_is_dying(&ctx->refs))
7839 return -ENXIO;
7840
Jens Axboe071698e2020-01-28 10:04:42 -07007841 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007842 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06007843
Jens Axboe05f3fb32019-12-09 11:22:50 -07007844 /*
7845 * Drop uring mutex before waiting for references to exit. If
7846 * another thread is currently inside io_uring_enter() it might
7847 * need to grab the uring_lock to make progress. If we hold it
7848 * here across the drain wait, then we can deadlock. It's safe
7849 * to drop the mutex here, since no new references will come in
7850 * after we've killed the percpu ref.
7851 */
7852 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007853 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007854 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007855 if (ret) {
7856 percpu_ref_resurrect(&ctx->refs);
7857 ret = -EINTR;
7858 goto out;
7859 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007860 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007861
7862 switch (opcode) {
7863 case IORING_REGISTER_BUFFERS:
7864 ret = io_sqe_buffer_register(ctx, arg, nr_args);
7865 break;
7866 case IORING_UNREGISTER_BUFFERS:
7867 ret = -EINVAL;
7868 if (arg || nr_args)
7869 break;
7870 ret = io_sqe_buffer_unregister(ctx);
7871 break;
Jens Axboe6b063142019-01-10 22:13:58 -07007872 case IORING_REGISTER_FILES:
7873 ret = io_sqe_files_register(ctx, arg, nr_args);
7874 break;
7875 case IORING_UNREGISTER_FILES:
7876 ret = -EINVAL;
7877 if (arg || nr_args)
7878 break;
7879 ret = io_sqe_files_unregister(ctx);
7880 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06007881 case IORING_REGISTER_FILES_UPDATE:
7882 ret = io_sqe_files_update(ctx, arg, nr_args);
7883 break;
Jens Axboe9b402842019-04-11 11:45:41 -06007884 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07007885 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06007886 ret = -EINVAL;
7887 if (nr_args != 1)
7888 break;
7889 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07007890 if (ret)
7891 break;
7892 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
7893 ctx->eventfd_async = 1;
7894 else
7895 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06007896 break;
7897 case IORING_UNREGISTER_EVENTFD:
7898 ret = -EINVAL;
7899 if (arg || nr_args)
7900 break;
7901 ret = io_eventfd_unregister(ctx);
7902 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07007903 case IORING_REGISTER_PROBE:
7904 ret = -EINVAL;
7905 if (!arg || nr_args > 256)
7906 break;
7907 ret = io_probe(ctx, arg, nr_args);
7908 break;
Jens Axboe071698e2020-01-28 10:04:42 -07007909 case IORING_REGISTER_PERSONALITY:
7910 ret = -EINVAL;
7911 if (arg || nr_args)
7912 break;
7913 ret = io_register_personality(ctx);
7914 break;
7915 case IORING_UNREGISTER_PERSONALITY:
7916 ret = -EINVAL;
7917 if (arg)
7918 break;
7919 ret = io_unregister_personality(ctx, nr_args);
7920 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07007921 default:
7922 ret = -EINVAL;
7923 break;
7924 }
7925
Jens Axboe071698e2020-01-28 10:04:42 -07007926 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007927 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07007928 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07007929out:
7930 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007931 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007932 return ret;
7933}
7934
7935SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
7936 void __user *, arg, unsigned int, nr_args)
7937{
7938 struct io_ring_ctx *ctx;
7939 long ret = -EBADF;
7940 struct fd f;
7941
7942 f = fdget(fd);
7943 if (!f.file)
7944 return -EBADF;
7945
7946 ret = -EOPNOTSUPP;
7947 if (f.file->f_op != &io_uring_fops)
7948 goto out_fput;
7949
7950 ctx = f.file->private_data;
7951
7952 mutex_lock(&ctx->uring_lock);
7953 ret = __io_uring_register(ctx, opcode, arg, nr_args);
7954 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007955 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
7956 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07007957out_fput:
7958 fdput(f);
7959 return ret;
7960}
7961
Jens Axboe2b188cc2019-01-07 10:46:33 -07007962static int __init io_uring_init(void)
7963{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007964#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
7965 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
7966 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
7967} while (0)
7968
7969#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
7970 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
7971 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
7972 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
7973 BUILD_BUG_SQE_ELEM(1, __u8, flags);
7974 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
7975 BUILD_BUG_SQE_ELEM(4, __s32, fd);
7976 BUILD_BUG_SQE_ELEM(8, __u64, off);
7977 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
7978 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03007979 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007980 BUILD_BUG_SQE_ELEM(24, __u32, len);
7981 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
7982 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
7983 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
7984 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
7985 BUILD_BUG_SQE_ELEM(28, __u16, poll_events);
7986 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
7987 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
7988 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
7989 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
7990 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
7991 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
7992 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
7993 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03007994 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007995 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
7996 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
7997 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03007998 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007999
Jens Axboed3656342019-12-18 09:50:26 -07008000 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008001 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
8002 return 0;
8003};
8004__initcall(io_uring_init);