blob: 381d50becd04a19d55f8953032238d93e839054b [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
Xiaoguang Wang05589552020-03-31 14:05:18 +0800189struct fixed_file_ref_node {
190 struct percpu_ref refs;
191 struct list_head node;
192 struct list_head file_list;
193 struct fixed_file_data *file_data;
194 struct work_struct work;
195};
196
Jens Axboe05f3fb32019-12-09 11:22:50 -0700197struct fixed_file_data {
198 struct fixed_file_table *table;
199 struct io_ring_ctx *ctx;
200
Xiaoguang Wang05589552020-03-31 14:05:18 +0800201 struct percpu_ref *cur_refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700202 struct percpu_ref refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700203 struct completion done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800204 struct list_head ref_list;
205 spinlock_t lock;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700206};
207
Jens Axboe5a2e7452020-02-23 16:23:11 -0700208struct io_buffer {
209 struct list_head list;
210 __u64 addr;
211 __s32 len;
212 __u16 bid;
213};
214
Jens Axboe2b188cc2019-01-07 10:46:33 -0700215struct io_ring_ctx {
216 struct {
217 struct percpu_ref refs;
218 } ____cacheline_aligned_in_smp;
219
220 struct {
221 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800222 unsigned int compat: 1;
223 unsigned int account_mem: 1;
224 unsigned int cq_overflow_flushed: 1;
225 unsigned int drain_next: 1;
226 unsigned int eventfd_async: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700227
Hristo Venev75b28af2019-08-26 17:23:46 +0000228 /*
229 * Ring buffer of indices into array of io_uring_sqe, which is
230 * mmapped by the application using the IORING_OFF_SQES offset.
231 *
232 * This indirection could e.g. be used to assign fixed
233 * io_uring_sqe entries to operations and only submit them to
234 * the queue when needed.
235 *
236 * The kernel modifies neither the indices array nor the entries
237 * array.
238 */
239 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700240 unsigned cached_sq_head;
241 unsigned sq_entries;
242 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700243 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600244 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700245 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700246 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600247
248 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600249 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700250 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700251
Jens Axboefcb323c2019-10-24 12:39:47 -0600252 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700253 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700254 } ____cacheline_aligned_in_smp;
255
Hristo Venev75b28af2019-08-26 17:23:46 +0000256 struct io_rings *rings;
257
Jens Axboe2b188cc2019-01-07 10:46:33 -0700258 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600259 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700260 struct task_struct *sqo_thread; /* if using sq thread polling */
261 struct mm_struct *sqo_mm;
262 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700263
Jens Axboe6b063142019-01-10 22:13:58 -0700264 /*
265 * If used, fixed file set. Writers must ensure that ->refs is dead,
266 * readers must ensure that ->refs is alive as long as the file* is
267 * used. Only updated through io_uring_register(2).
268 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700269 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700270 unsigned nr_user_files;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300271 int ring_fd;
272 struct file *ring_file;
Jens Axboe6b063142019-01-10 22:13:58 -0700273
Jens Axboeedafcce2019-01-09 09:16:05 -0700274 /* if used, fixed mapped user buffers */
275 unsigned nr_user_bufs;
276 struct io_mapped_ubuf *user_bufs;
277
Jens Axboe2b188cc2019-01-07 10:46:33 -0700278 struct user_struct *user;
279
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700280 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700281
Jens Axboe206aefd2019-11-07 18:27:42 -0700282 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
283 struct completion *completions;
284
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700285 /* if all else fails... */
286 struct io_kiocb *fallback_req;
287
Jens Axboe206aefd2019-11-07 18:27:42 -0700288#if defined(CONFIG_UNIX)
289 struct socket *ring_sock;
290#endif
291
Jens Axboe5a2e7452020-02-23 16:23:11 -0700292 struct idr io_buffer_idr;
293
Jens Axboe071698e2020-01-28 10:04:42 -0700294 struct idr personality_idr;
295
Jens Axboe206aefd2019-11-07 18:27:42 -0700296 struct {
297 unsigned cached_cq_tail;
298 unsigned cq_entries;
299 unsigned cq_mask;
300 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700301 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700302 struct wait_queue_head cq_wait;
303 struct fasync_struct *cq_fasync;
304 struct eventfd_ctx *cq_ev_fd;
305 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700306
307 struct {
308 struct mutex uring_lock;
309 wait_queue_head_t wait;
310 } ____cacheline_aligned_in_smp;
311
312 struct {
313 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700314
Jens Axboedef596e2019-01-09 08:59:42 -0700315 /*
316 * ->poll_list is protected by the ctx->uring_lock for
317 * io_uring instances that don't use IORING_SETUP_SQPOLL.
318 * For SQPOLL, only the single threaded io_sq_thread() will
319 * manipulate the list, hence no extra locking is needed there.
320 */
321 struct list_head poll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700322 struct hlist_head *cancel_hash;
323 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700324 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600325
326 spinlock_t inflight_lock;
327 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700328 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600329
330 struct work_struct exit_work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700331};
332
Jens Axboe09bb8392019-03-13 12:39:28 -0600333/*
334 * First field must be the file pointer in all the
335 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
336 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700337struct io_poll_iocb {
338 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700339 union {
340 struct wait_queue_head *head;
341 u64 addr;
342 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700343 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600344 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700345 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700346 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700347};
348
Jens Axboeb5dba592019-12-11 14:02:38 -0700349struct io_close {
350 struct file *file;
351 struct file *put_file;
352 int fd;
353};
354
Jens Axboead8a48a2019-11-15 08:49:11 -0700355struct io_timeout_data {
356 struct io_kiocb *req;
357 struct hrtimer timer;
358 struct timespec64 ts;
359 enum hrtimer_mode mode;
360};
361
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700362struct io_accept {
363 struct file *file;
364 struct sockaddr __user *addr;
365 int __user *addr_len;
366 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600367 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700368};
369
370struct io_sync {
371 struct file *file;
372 loff_t len;
373 loff_t off;
374 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700375 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700376};
377
Jens Axboefbf23842019-12-17 18:45:56 -0700378struct io_cancel {
379 struct file *file;
380 u64 addr;
381};
382
Jens Axboeb29472e2019-12-17 18:50:29 -0700383struct io_timeout {
384 struct file *file;
385 u64 addr;
386 int flags;
Pavel Begunkovb55ce732020-04-15 00:39:49 +0300387 u32 count;
Jens Axboeb29472e2019-12-17 18:50:29 -0700388};
389
Jens Axboe9adbd452019-12-20 08:45:55 -0700390struct io_rw {
391 /* NOTE: kiocb has the file as the first member, so don't do it here */
392 struct kiocb kiocb;
393 u64 addr;
394 u64 len;
395};
396
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700397struct io_connect {
398 struct file *file;
399 struct sockaddr __user *addr;
400 int addr_len;
401};
402
Jens Axboee47293f2019-12-20 08:58:21 -0700403struct io_sr_msg {
404 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700405 union {
406 struct user_msghdr __user *msg;
407 void __user *buf;
408 };
Jens Axboee47293f2019-12-20 08:58:21 -0700409 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700410 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700411 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700412 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700413};
414
Jens Axboe15b71ab2019-12-11 11:20:36 -0700415struct io_open {
416 struct file *file;
417 int dfd;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700418 union {
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700419 unsigned mask;
420 };
Jens Axboe15b71ab2019-12-11 11:20:36 -0700421 struct filename *filename;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700422 struct statx __user *buffer;
Jens Axboec12cedf2020-01-08 17:41:21 -0700423 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600424 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700425};
426
Jens Axboe05f3fb32019-12-09 11:22:50 -0700427struct io_files_update {
428 struct file *file;
429 u64 arg;
430 u32 nr_args;
431 u32 offset;
432};
433
Jens Axboe4840e412019-12-25 22:03:45 -0700434struct io_fadvise {
435 struct file *file;
436 u64 offset;
437 u32 len;
438 u32 advice;
439};
440
Jens Axboec1ca7572019-12-25 22:18:28 -0700441struct io_madvise {
442 struct file *file;
443 u64 addr;
444 u32 len;
445 u32 advice;
446};
447
Jens Axboe3e4827b2020-01-08 15:18:09 -0700448struct io_epoll {
449 struct file *file;
450 int epfd;
451 int op;
452 int fd;
453 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700454};
455
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300456struct io_splice {
457 struct file *file_out;
458 struct file *file_in;
459 loff_t off_out;
460 loff_t off_in;
461 u64 len;
462 unsigned int flags;
463};
464
Jens Axboeddf0322d2020-02-23 16:41:33 -0700465struct io_provide_buf {
466 struct file *file;
467 __u64 addr;
468 __s32 len;
469 __u32 bgid;
470 __u16 nbufs;
471 __u16 bid;
472};
473
Jens Axboef499a022019-12-02 16:28:46 -0700474struct io_async_connect {
475 struct sockaddr_storage address;
476};
477
Jens Axboe03b12302019-12-02 18:50:25 -0700478struct io_async_msghdr {
479 struct iovec fast_iov[UIO_FASTIOV];
480 struct iovec *iov;
481 struct sockaddr __user *uaddr;
482 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700483 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700484};
485
Jens Axboef67676d2019-12-02 11:03:47 -0700486struct io_async_rw {
487 struct iovec fast_iov[UIO_FASTIOV];
488 struct iovec *iov;
489 ssize_t nr_segs;
490 ssize_t size;
491};
492
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700493struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700494 union {
495 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700496 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700497 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700498 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700499 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700500};
501
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300502enum {
503 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
504 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
505 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
506 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
507 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700508 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300509
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300510 REQ_F_LINK_HEAD_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300511 REQ_F_LINK_NEXT_BIT,
512 REQ_F_FAIL_LINK_BIT,
513 REQ_F_INFLIGHT_BIT,
514 REQ_F_CUR_POS_BIT,
515 REQ_F_NOWAIT_BIT,
516 REQ_F_IOPOLL_COMPLETED_BIT,
517 REQ_F_LINK_TIMEOUT_BIT,
518 REQ_F_TIMEOUT_BIT,
519 REQ_F_ISREG_BIT,
520 REQ_F_MUST_PUNT_BIT,
521 REQ_F_TIMEOUT_NOSEQ_BIT,
522 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300523 REQ_F_NEED_CLEANUP_BIT,
Jens Axboe2ca10252020-02-13 17:17:35 -0700524 REQ_F_OVERFLOW_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700525 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700526 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700527
528 /* not a real bit, just to check we're not overflowing the space */
529 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300530};
531
532enum {
533 /* ctx owns file */
534 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
535 /* drain existing IO first */
536 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
537 /* linked sqes */
538 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
539 /* doesn't sever on completion < 0 */
540 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
541 /* IOSQE_ASYNC */
542 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700543 /* IOSQE_BUFFER_SELECT */
544 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300545
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300546 /* head of a link */
547 REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300548 /* already grabbed next link */
549 REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT),
550 /* fail rest of links */
551 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
552 /* on inflight list */
553 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
554 /* read/write uses file position */
555 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
556 /* must not punt to workers */
557 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
558 /* polled IO has completed */
559 REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT),
560 /* has linked timeout */
561 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
562 /* timeout request */
563 REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT),
564 /* regular file */
565 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
566 /* must be punted even for NONBLOCK */
567 REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT),
568 /* no timeout sequence */
569 REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT),
570 /* completion under lock */
571 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300572 /* needs cleanup */
573 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboe2ca10252020-02-13 17:17:35 -0700574 /* in overflow list */
575 REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700576 /* already went through poll handler */
577 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700578 /* buffer already selected */
579 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700580};
581
582struct async_poll {
583 struct io_poll_iocb poll;
584 struct io_wq_work work;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300585};
586
Jens Axboe09bb8392019-03-13 12:39:28 -0600587/*
588 * NOTE! Each of the iocb union members has the file pointer
589 * as the first entry in their struct definition. So you can
590 * access the file pointer through any of the sub-structs,
591 * or directly as just 'ki_filp' in this struct.
592 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700593struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700594 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600595 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700596 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700597 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700598 struct io_accept accept;
599 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700600 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700601 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700602 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700603 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700604 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700605 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700606 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700607 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700608 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700609 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300610 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700611 struct io_provide_buf pbuf;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700612 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700613
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700614 struct io_async_ctx *io;
Pavel Begunkovc398ecb2020-04-09 08:17:59 +0300615 int cflags;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300616 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700617 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700618
619 struct io_ring_ctx *ctx;
Jens Axboed7718a92020-02-14 22:23:12 -0700620 struct list_head list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700621 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700622 refcount_t refs;
Jens Axboe3537b6a2020-04-03 11:19:06 -0600623 struct task_struct *task;
624 unsigned long fsize;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700625 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600626 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600627 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700628
Jens Axboed7718a92020-02-14 22:23:12 -0700629 struct list_head link_list;
630
Jens Axboefcb323c2019-10-24 12:39:47 -0600631 struct list_head inflight_entry;
632
Xiaoguang Wang05589552020-03-31 14:05:18 +0800633 struct percpu_ref *fixed_file_refs;
634
Jens Axboeb41e9852020-02-17 09:52:41 -0700635 union {
636 /*
637 * Only commands that never go async can use the below fields,
Jens Axboed7718a92020-02-14 22:23:12 -0700638 * obviously. Right now only IORING_OP_POLL_ADD uses them, and
639 * async armed poll handlers for regular commands. The latter
640 * restore the work, if needed.
Jens Axboeb41e9852020-02-17 09:52:41 -0700641 */
642 struct {
Jens Axboeb41e9852020-02-17 09:52:41 -0700643 struct callback_head task_work;
Jens Axboed7718a92020-02-14 22:23:12 -0700644 struct hlist_node hash_node;
645 struct async_poll *apoll;
Jens Axboeb41e9852020-02-17 09:52:41 -0700646 };
647 struct io_wq_work work;
648 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700649};
650
651#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700652#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700653
Jens Axboe9a56a232019-01-09 09:06:50 -0700654struct io_submit_state {
655 struct blk_plug plug;
656
657 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700658 * io_kiocb alloc cache
659 */
660 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300661 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700662
663 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700664 * File reference cache
665 */
666 struct file *file;
667 unsigned int fd;
668 unsigned int has_refs;
669 unsigned int used_refs;
670 unsigned int ios_left;
671};
672
Jens Axboed3656342019-12-18 09:50:26 -0700673struct io_op_def {
674 /* needs req->io allocated for deferral/async */
675 unsigned async_ctx : 1;
676 /* needs current->mm setup, does mm access */
677 unsigned needs_mm : 1;
678 /* needs req->file assigned */
679 unsigned needs_file : 1;
680 /* needs req->file assigned IFF fd is >= 0 */
681 unsigned fd_non_neg : 1;
682 /* hash wq insertion if file is a regular file */
683 unsigned hash_reg_file : 1;
684 /* unbound wq insertion if file is a non-regular file */
685 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700686 /* opcode is not supported by this kernel */
687 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700688 /* needs file table */
689 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700690 /* needs ->fs */
691 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700692 /* set if opcode supports polled "wait" */
693 unsigned pollin : 1;
694 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700695 /* op supports buffer selection */
696 unsigned buffer_select : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700697};
698
699static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300700 [IORING_OP_NOP] = {},
701 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700702 .async_ctx = 1,
703 .needs_mm = 1,
704 .needs_file = 1,
705 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700706 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700707 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700708 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300709 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700710 .async_ctx = 1,
711 .needs_mm = 1,
712 .needs_file = 1,
713 .hash_reg_file = 1,
714 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700715 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700716 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300717 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700718 .needs_file = 1,
719 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300720 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700721 .needs_file = 1,
722 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700723 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700724 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300725 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700726 .needs_file = 1,
727 .hash_reg_file = 1,
728 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700729 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700730 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300731 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700732 .needs_file = 1,
733 .unbound_nonreg_file = 1,
734 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300735 [IORING_OP_POLL_REMOVE] = {},
736 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700737 .needs_file = 1,
738 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300739 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700740 .async_ctx = 1,
741 .needs_mm = 1,
742 .needs_file = 1,
743 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700744 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700745 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700746 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300747 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700748 .async_ctx = 1,
749 .needs_mm = 1,
750 .needs_file = 1,
751 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700752 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700753 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700754 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700755 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300756 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700757 .async_ctx = 1,
758 .needs_mm = 1,
759 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300760 [IORING_OP_TIMEOUT_REMOVE] = {},
761 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700762 .needs_mm = 1,
763 .needs_file = 1,
764 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700765 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700766 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700767 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300768 [IORING_OP_ASYNC_CANCEL] = {},
769 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700770 .async_ctx = 1,
771 .needs_mm = 1,
772 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300773 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700774 .async_ctx = 1,
775 .needs_mm = 1,
776 .needs_file = 1,
777 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700778 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700779 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300780 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700781 .needs_file = 1,
782 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300783 [IORING_OP_OPENAT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700784 .needs_file = 1,
785 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700786 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700787 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700788 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300789 [IORING_OP_CLOSE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700790 .needs_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700791 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700792 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300793 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700794 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700795 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700796 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300797 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700798 .needs_mm = 1,
799 .needs_file = 1,
800 .fd_non_neg = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700801 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700802 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300803 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700804 .needs_mm = 1,
805 .needs_file = 1,
806 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700807 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700808 .buffer_select = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700809 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300810 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700811 .needs_mm = 1,
812 .needs_file = 1,
813 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700814 .pollout = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700815 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300816 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700817 .needs_file = 1,
818 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300819 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700820 .needs_mm = 1,
821 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300822 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700823 .needs_mm = 1,
824 .needs_file = 1,
825 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700826 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700827 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300828 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700829 .needs_mm = 1,
830 .needs_file = 1,
831 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700832 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700833 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700834 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300835 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -0700836 .needs_file = 1,
837 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700838 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700839 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700840 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700841 [IORING_OP_EPOLL_CTL] = {
842 .unbound_nonreg_file = 1,
843 .file_table = 1,
844 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300845 [IORING_OP_SPLICE] = {
846 .needs_file = 1,
847 .hash_reg_file = 1,
848 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700849 },
850 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700851 [IORING_OP_REMOVE_BUFFERS] = {},
Jens Axboed3656342019-12-18 09:50:26 -0700852};
853
Jens Axboe561fb042019-10-24 07:25:42 -0600854static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700855static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800856static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700857static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700858static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
859static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700860static int __io_sqe_files_update(struct io_ring_ctx *ctx,
861 struct io_uring_files_update *ip,
862 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700863static int io_grab_files(struct io_kiocb *req);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300864static void io_cleanup_req(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700865static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
866 int fd, struct file **out_file, bool fixed);
867static void __io_queue_sqe(struct io_kiocb *req,
868 const struct io_uring_sqe *sqe);
Jens Axboede0617e2019-04-06 21:51:27 -0600869
Jens Axboe2b188cc2019-01-07 10:46:33 -0700870static struct kmem_cache *req_cachep;
871
872static const struct file_operations io_uring_fops;
873
874struct sock *io_uring_get_socket(struct file *file)
875{
876#if defined(CONFIG_UNIX)
877 if (file->f_op == &io_uring_fops) {
878 struct io_ring_ctx *ctx = file->private_data;
879
880 return ctx->ring_sock->sk;
881 }
882#endif
883 return NULL;
884}
885EXPORT_SYMBOL(io_uring_get_socket);
886
887static void io_ring_ctx_ref_free(struct percpu_ref *ref)
888{
889 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
890
Jens Axboe206aefd2019-11-07 18:27:42 -0700891 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700892}
893
894static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
895{
896 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700897 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700898
899 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
900 if (!ctx)
901 return NULL;
902
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700903 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
904 if (!ctx->fallback_req)
905 goto err;
906
Jens Axboe206aefd2019-11-07 18:27:42 -0700907 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
908 if (!ctx->completions)
909 goto err;
910
Jens Axboe78076bb2019-12-04 19:56:40 -0700911 /*
912 * Use 5 bits less than the max cq entries, that should give us around
913 * 32 entries per hash list if totally full and uniformly spread.
914 */
915 hash_bits = ilog2(p->cq_entries);
916 hash_bits -= 5;
917 if (hash_bits <= 0)
918 hash_bits = 1;
919 ctx->cancel_hash_bits = hash_bits;
920 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
921 GFP_KERNEL);
922 if (!ctx->cancel_hash)
923 goto err;
924 __hash_init(ctx->cancel_hash, 1U << hash_bits);
925
Roman Gushchin21482892019-05-07 10:01:48 -0700926 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700927 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
928 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700929
930 ctx->flags = p->flags;
931 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700932 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700933 init_completion(&ctx->completions[0]);
934 init_completion(&ctx->completions[1]);
Jens Axboe5a2e7452020-02-23 16:23:11 -0700935 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -0700936 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700937 mutex_init(&ctx->uring_lock);
938 init_waitqueue_head(&ctx->wait);
939 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700940 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600941 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600942 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600943 init_waitqueue_head(&ctx->inflight_wait);
944 spin_lock_init(&ctx->inflight_lock);
945 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700946 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700947err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700948 if (ctx->fallback_req)
949 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700950 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700951 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700952 kfree(ctx);
953 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700954}
955
Bob Liu9d858b22019-11-13 18:06:25 +0800956static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600957{
Jackie Liua197f662019-11-08 08:09:12 -0700958 struct io_ring_ctx *ctx = req->ctx;
959
Pavel Begunkov31af27c2020-04-15 00:39:50 +0300960 return req->sequence != ctx->cached_cq_tail
961 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600962}
963
Bob Liu9d858b22019-11-13 18:06:25 +0800964static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600965{
Pavel Begunkov87987892020-01-18 01:22:30 +0300966 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +0800967 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600968
Bob Liu9d858b22019-11-13 18:06:25 +0800969 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600970}
971
972static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600973{
974 struct io_kiocb *req;
975
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600976 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800977 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600978 list_del_init(&req->list);
979 return req;
980 }
981
982 return NULL;
983}
984
Jens Axboe5262f562019-09-17 12:26:57 -0600985static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
986{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600987 struct io_kiocb *req;
988
989 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700990 if (req) {
991 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
992 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800993 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700994 list_del_init(&req->list);
995 return req;
996 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600997 }
998
999 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -06001000}
1001
Jens Axboede0617e2019-04-06 21:51:27 -06001002static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001003{
Hristo Venev75b28af2019-08-26 17:23:46 +00001004 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001005
Pavel Begunkov07910152020-01-17 03:52:46 +03001006 /* order cqe stores with ring update */
1007 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001008
Pavel Begunkov07910152020-01-17 03:52:46 +03001009 if (wq_has_sleeper(&ctx->cq_wait)) {
1010 wake_up_interruptible(&ctx->cq_wait);
1011 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001012 }
1013}
1014
Jens Axboecccf0ee2020-01-27 16:34:48 -07001015static inline void io_req_work_grab_env(struct io_kiocb *req,
1016 const struct io_op_def *def)
Jens Axboe18d9be12019-09-10 09:13:05 -06001017{
Jens Axboecccf0ee2020-01-27 16:34:48 -07001018 if (!req->work.mm && def->needs_mm) {
1019 mmgrab(current->mm);
1020 req->work.mm = current->mm;
1021 }
1022 if (!req->work.creds)
1023 req->work.creds = get_current_cred();
Jens Axboeff002b32020-02-07 16:05:21 -07001024 if (!req->work.fs && def->needs_fs) {
1025 spin_lock(&current->fs->lock);
1026 if (!current->fs->in_exec) {
1027 req->work.fs = current->fs;
1028 req->work.fs->users++;
1029 } else {
1030 req->work.flags |= IO_WQ_WORK_CANCEL;
1031 }
1032 spin_unlock(&current->fs->lock);
1033 }
Jens Axboe6ab23142020-02-08 20:23:59 -07001034 if (!req->work.task_pid)
1035 req->work.task_pid = task_pid_vnr(current);
Jens Axboecccf0ee2020-01-27 16:34:48 -07001036}
1037
1038static inline void io_req_work_drop_env(struct io_kiocb *req)
1039{
1040 if (req->work.mm) {
1041 mmdrop(req->work.mm);
1042 req->work.mm = NULL;
1043 }
1044 if (req->work.creds) {
1045 put_cred(req->work.creds);
1046 req->work.creds = NULL;
1047 }
Jens Axboeff002b32020-02-07 16:05:21 -07001048 if (req->work.fs) {
1049 struct fs_struct *fs = req->work.fs;
1050
1051 spin_lock(&req->work.fs->lock);
1052 if (--fs->users)
1053 fs = NULL;
1054 spin_unlock(&req->work.fs->lock);
1055 if (fs)
1056 free_fs_struct(fs);
1057 }
Jens Axboe561fb042019-10-24 07:25:42 -06001058}
1059
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001060static inline void io_prep_async_work(struct io_kiocb *req,
Jens Axboe94ae5e72019-11-14 19:39:52 -07001061 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -06001062{
Jens Axboed3656342019-12-18 09:50:26 -07001063 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe54a91f32019-09-10 09:15:04 -06001064
Jens Axboed3656342019-12-18 09:50:26 -07001065 if (req->flags & REQ_F_ISREG) {
1066 if (def->hash_reg_file)
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001067 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001068 } else {
1069 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001070 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001071 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001072
1073 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -06001074
Jens Axboe94ae5e72019-11-14 19:39:52 -07001075 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001076}
1077
Jackie Liua197f662019-11-08 08:09:12 -07001078static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001079{
Jackie Liua197f662019-11-08 08:09:12 -07001080 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001081 struct io_kiocb *link;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001082
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001083 io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06001084
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001085 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1086 &req->work, req->flags);
1087 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001088
1089 if (link)
1090 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -06001091}
1092
Jens Axboe5262f562019-09-17 12:26:57 -06001093static void io_kill_timeout(struct io_kiocb *req)
1094{
1095 int ret;
1096
Jens Axboe2d283902019-12-04 11:08:05 -07001097 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001098 if (ret != -1) {
1099 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -06001100 list_del_init(&req->list);
Pavel Begunkovf0e20b82020-03-07 01:15:22 +03001101 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001102 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001103 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001104 }
1105}
1106
1107static void io_kill_timeouts(struct io_ring_ctx *ctx)
1108{
1109 struct io_kiocb *req, *tmp;
1110
1111 spin_lock_irq(&ctx->completion_lock);
1112 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
1113 io_kill_timeout(req);
1114 spin_unlock_irq(&ctx->completion_lock);
1115}
1116
Jens Axboede0617e2019-04-06 21:51:27 -06001117static void io_commit_cqring(struct io_ring_ctx *ctx)
1118{
1119 struct io_kiocb *req;
1120
Jens Axboe5262f562019-09-17 12:26:57 -06001121 while ((req = io_get_timeout_req(ctx)) != NULL)
1122 io_kill_timeout(req);
1123
Jens Axboede0617e2019-04-06 21:51:27 -06001124 __io_commit_cqring(ctx);
1125
Pavel Begunkov87987892020-01-18 01:22:30 +03001126 while ((req = io_get_deferred_req(ctx)) != NULL)
Jackie Liua197f662019-11-08 08:09:12 -07001127 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -06001128}
1129
Jens Axboe2b188cc2019-01-07 10:46:33 -07001130static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1131{
Hristo Venev75b28af2019-08-26 17:23:46 +00001132 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001133 unsigned tail;
1134
1135 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001136 /*
1137 * writes to the cq entry need to come after reading head; the
1138 * control dependency is enough as we're using WRITE_ONCE to
1139 * fill the cq entry
1140 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001141 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001142 return NULL;
1143
1144 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001145 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001146}
1147
Jens Axboef2842ab2020-01-08 11:04:00 -07001148static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1149{
Jens Axboef0b493e2020-02-01 21:30:11 -07001150 if (!ctx->cq_ev_fd)
1151 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001152 if (!ctx->eventfd_async)
1153 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001154 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001155}
1156
Jens Axboeb41e9852020-02-17 09:52:41 -07001157static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001158{
1159 if (waitqueue_active(&ctx->wait))
1160 wake_up(&ctx->wait);
1161 if (waitqueue_active(&ctx->sqo_wait))
1162 wake_up(&ctx->sqo_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001163 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001164 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001165}
1166
Jens Axboec4a2ed72019-11-21 21:01:26 -07001167/* Returns true if there are no backlogged entries after the flush */
1168static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001169{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001170 struct io_rings *rings = ctx->rings;
1171 struct io_uring_cqe *cqe;
1172 struct io_kiocb *req;
1173 unsigned long flags;
1174 LIST_HEAD(list);
1175
1176 if (!force) {
1177 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001178 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001179 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1180 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001181 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001182 }
1183
1184 spin_lock_irqsave(&ctx->completion_lock, flags);
1185
1186 /* if force is set, the ring is going away. always drop after that */
1187 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001188 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001189
Jens Axboec4a2ed72019-11-21 21:01:26 -07001190 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001191 while (!list_empty(&ctx->cq_overflow_list)) {
1192 cqe = io_get_cqring(ctx);
1193 if (!cqe && !force)
1194 break;
1195
1196 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1197 list);
1198 list_move(&req->list, &list);
Jens Axboe2ca10252020-02-13 17:17:35 -07001199 req->flags &= ~REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001200 if (cqe) {
1201 WRITE_ONCE(cqe->user_data, req->user_data);
1202 WRITE_ONCE(cqe->res, req->result);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001203 WRITE_ONCE(cqe->flags, req->cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001204 } else {
1205 WRITE_ONCE(ctx->rings->cq_overflow,
1206 atomic_inc_return(&ctx->cached_cq_overflow));
1207 }
1208 }
1209
1210 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001211 if (cqe) {
1212 clear_bit(0, &ctx->sq_check_overflow);
1213 clear_bit(0, &ctx->cq_check_overflow);
1214 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001215 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1216 io_cqring_ev_posted(ctx);
1217
1218 while (!list_empty(&list)) {
1219 req = list_first_entry(&list, struct io_kiocb, list);
1220 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001221 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001222 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001223
1224 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001225}
1226
Jens Axboebcda7ba2020-02-23 16:42:51 -07001227static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001228{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001229 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001230 struct io_uring_cqe *cqe;
1231
Jens Axboe78e19bb2019-11-06 15:21:34 -07001232 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001233
Jens Axboe2b188cc2019-01-07 10:46:33 -07001234 /*
1235 * If we can't get a cq entry, userspace overflowed the
1236 * submission (by quite a lot). Increment the overflow count in
1237 * the ring.
1238 */
1239 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001240 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001241 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001242 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001243 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001244 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001245 WRITE_ONCE(ctx->rings->cq_overflow,
1246 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001247 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001248 if (list_empty(&ctx->cq_overflow_list)) {
1249 set_bit(0, &ctx->sq_check_overflow);
1250 set_bit(0, &ctx->cq_check_overflow);
1251 }
Jens Axboe2ca10252020-02-13 17:17:35 -07001252 req->flags |= REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001253 refcount_inc(&req->refs);
1254 req->result = res;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001255 req->cflags = cflags;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001256 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001257 }
1258}
1259
Jens Axboebcda7ba2020-02-23 16:42:51 -07001260static void io_cqring_fill_event(struct io_kiocb *req, long res)
1261{
1262 __io_cqring_fill_event(req, res, 0);
1263}
1264
1265static void __io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001266{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001267 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001268 unsigned long flags;
1269
1270 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001271 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001272 io_commit_cqring(ctx);
1273 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1274
Jens Axboe8c838782019-03-12 15:48:16 -06001275 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001276}
1277
Jens Axboebcda7ba2020-02-23 16:42:51 -07001278static void io_cqring_add_event(struct io_kiocb *req, long res)
1279{
1280 __io_cqring_add_event(req, res, 0);
1281}
1282
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001283static inline bool io_is_fallback_req(struct io_kiocb *req)
1284{
1285 return req == (struct io_kiocb *)
1286 ((unsigned long) req->ctx->fallback_req & ~1UL);
1287}
1288
1289static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1290{
1291 struct io_kiocb *req;
1292
1293 req = ctx->fallback_req;
1294 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1295 return req;
1296
1297 return NULL;
1298}
1299
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001300static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1301 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001302{
Jens Axboefd6fab22019-03-14 16:30:06 -06001303 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001304 struct io_kiocb *req;
1305
Jens Axboe2579f912019-01-09 09:10:43 -07001306 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001307 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001308 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001309 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001310 } else if (!state->free_reqs) {
1311 size_t sz;
1312 int ret;
1313
1314 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001315 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1316
1317 /*
1318 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1319 * retry single alloc to be on the safe side.
1320 */
1321 if (unlikely(ret <= 0)) {
1322 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1323 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001324 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001325 ret = 1;
1326 }
Jens Axboe2579f912019-01-09 09:10:43 -07001327 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001328 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001329 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001330 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001331 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001332 }
1333
Jens Axboe2579f912019-01-09 09:10:43 -07001334 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001335fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001336 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001337}
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)
Xiaoguang Wang05589552020-03-31 14:05:18 +08001343 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001344 else
1345 fput(file);
1346}
1347
Jens Axboec6ca97b302019-12-28 12:11:08 -07001348static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001349{
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001350 if (req->flags & REQ_F_NEED_CLEANUP)
1351 io_cleanup_req(req);
1352
YueHaibing96fd84d2020-01-07 22:22:44 +08001353 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001354 if (req->file)
1355 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboe3537b6a2020-04-03 11:19:06 -06001356 if (req->task)
1357 put_task_struct(req->task);
Jens Axboecccf0ee2020-01-27 16:34:48 -07001358
1359 io_req_work_drop_env(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001360}
1361
1362static void __io_free_req(struct io_kiocb *req)
1363{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001364 __io_req_aux_free(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06001365
Jens Axboefcb323c2019-10-24 12:39:47 -06001366 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001367 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001368 unsigned long flags;
1369
1370 spin_lock_irqsave(&ctx->inflight_lock, flags);
1371 list_del(&req->inflight_entry);
1372 if (waitqueue_active(&ctx->inflight_wait))
1373 wake_up(&ctx->inflight_wait);
1374 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1375 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001376
1377 percpu_ref_put(&req->ctx->refs);
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03001378 if (likely(!io_is_fallback_req(req)))
1379 kmem_cache_free(req_cachep, req);
1380 else
1381 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -06001382}
1383
Jens Axboec6ca97b302019-12-28 12:11:08 -07001384struct req_batch {
1385 void *reqs[IO_IOPOLL_BATCH];
1386 int to_free;
1387 int need_iter;
1388};
1389
1390static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1391{
1392 if (!rb->to_free)
1393 return;
1394 if (rb->need_iter) {
1395 int i, inflight = 0;
1396 unsigned long flags;
1397
1398 for (i = 0; i < rb->to_free; i++) {
1399 struct io_kiocb *req = rb->reqs[i];
1400
Jens Axboe10fef4b2020-01-09 07:52:28 -07001401 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001402 req->file = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08001403 percpu_ref_put(req->fixed_file_refs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001404 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001405 if (req->flags & REQ_F_INFLIGHT)
1406 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001407 __io_req_aux_free(req);
1408 }
1409 if (!inflight)
1410 goto do_free;
1411
1412 spin_lock_irqsave(&ctx->inflight_lock, flags);
1413 for (i = 0; i < rb->to_free; i++) {
1414 struct io_kiocb *req = rb->reqs[i];
1415
Jens Axboe10fef4b2020-01-09 07:52:28 -07001416 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001417 list_del(&req->inflight_entry);
1418 if (!--inflight)
1419 break;
1420 }
1421 }
1422 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1423
1424 if (waitqueue_active(&ctx->inflight_wait))
1425 wake_up(&ctx->inflight_wait);
1426 }
1427do_free:
1428 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
1429 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001430 rb->to_free = rb->need_iter = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001431}
1432
Jackie Liua197f662019-11-08 08:09:12 -07001433static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001434{
Jackie Liua197f662019-11-08 08:09:12 -07001435 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001436 int ret;
1437
Jens Axboe2d283902019-12-04 11:08:05 -07001438 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001439 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001440 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001441 io_commit_cqring(ctx);
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001442 req->flags &= ~REQ_F_LINK_HEAD;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001443 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001444 return true;
1445 }
1446
1447 return false;
1448}
1449
Jens Axboeba816ad2019-09-28 11:36:45 -06001450static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001451{
Jens Axboe2665abf2019-11-05 12:40:47 -07001452 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001453 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001454
Jens Axboe4d7dd462019-11-20 13:03:52 -07001455 /* Already got next link */
1456 if (req->flags & REQ_F_LINK_NEXT)
1457 return;
1458
Jens Axboe9e645e112019-05-10 16:07:28 -06001459 /*
1460 * The list should never be empty when we are called here. But could
1461 * potentially happen if the chain is messed up, check to be on the
1462 * safe side.
1463 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001464 while (!list_empty(&req->link_list)) {
1465 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1466 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001467
Pavel Begunkov44932332019-12-05 16:16:35 +03001468 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1469 (nxt->flags & REQ_F_TIMEOUT))) {
1470 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001471 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001472 req->flags &= ~REQ_F_LINK_TIMEOUT;
1473 continue;
1474 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001475
Pavel Begunkov44932332019-12-05 16:16:35 +03001476 list_del_init(&req->link_list);
1477 if (!list_empty(&nxt->link_list))
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001478 nxt->flags |= REQ_F_LINK_HEAD;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001479 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001480 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001481 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001482
Jens Axboe4d7dd462019-11-20 13:03:52 -07001483 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001484 if (wake_ev)
1485 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001486}
1487
1488/*
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001489 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
Jens Axboe9e645e112019-05-10 16:07:28 -06001490 */
1491static void io_fail_links(struct io_kiocb *req)
1492{
Jens Axboe2665abf2019-11-05 12:40:47 -07001493 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001494 unsigned long flags;
1495
1496 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001497
1498 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001499 struct io_kiocb *link = list_first_entry(&req->link_list,
1500 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001501
Pavel Begunkov44932332019-12-05 16:16:35 +03001502 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001503 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001504
1505 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001506 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001507 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001508 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001509 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001510 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001511 }
Jens Axboe5d960722019-11-19 15:31:28 -07001512 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001513 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001514
1515 io_commit_cqring(ctx);
1516 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1517 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001518}
1519
Jens Axboe4d7dd462019-11-20 13:03:52 -07001520static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001521{
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001522 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001523 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001524
Jens Axboe9e645e112019-05-10 16:07:28 -06001525 /*
1526 * If LINK is set, we have dependent requests in this chain. If we
1527 * didn't fail this request, queue the first one up, moving any other
1528 * dependencies to the next request. In case of failure, fail the rest
1529 * of the chain.
1530 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001531 if (req->flags & REQ_F_FAIL_LINK) {
1532 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001533 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1534 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001535 struct io_ring_ctx *ctx = req->ctx;
1536 unsigned long flags;
1537
1538 /*
1539 * If this is a timeout link, we could be racing with the
1540 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001541 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001542 */
1543 spin_lock_irqsave(&ctx->completion_lock, flags);
1544 io_req_link_next(req, nxt);
1545 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1546 } else {
1547 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001548 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001549}
Jens Axboe9e645e112019-05-10 16:07:28 -06001550
Jackie Liuc69f8db2019-11-09 11:00:08 +08001551static void io_free_req(struct io_kiocb *req)
1552{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001553 struct io_kiocb *nxt = NULL;
1554
1555 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001556 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001557
1558 if (nxt)
1559 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001560}
1561
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001562static void io_link_work_cb(struct io_wq_work **workptr)
1563{
Pavel Begunkov18a542f2020-03-23 00:23:29 +03001564 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
1565 struct io_kiocb *link;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001566
Pavel Begunkov18a542f2020-03-23 00:23:29 +03001567 link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001568 io_queue_linked_timeout(link);
1569 io_wq_submit_work(workptr);
1570}
1571
1572static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
1573{
1574 struct io_kiocb *link;
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001575 const struct io_op_def *def = &io_op_defs[nxt->opcode];
1576
1577 if ((nxt->flags & REQ_F_ISREG) && def->hash_reg_file)
1578 io_wq_hash_work(&nxt->work, file_inode(nxt->file));
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001579
1580 *workptr = &nxt->work;
1581 link = io_prep_linked_timeout(nxt);
Pavel Begunkov18a542f2020-03-23 00:23:29 +03001582 if (link)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001583 nxt->work.func = io_link_work_cb;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001584}
1585
Jens Axboeba816ad2019-09-28 11:36:45 -06001586/*
1587 * Drop reference to request, return next in chain (if there is one) if this
1588 * was the last reference to this request.
1589 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001590__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001591static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001592{
Jens Axboe2a44f462020-02-25 13:25:41 -07001593 if (refcount_dec_and_test(&req->refs)) {
1594 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001595 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001596 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001597}
1598
Jens Axboe2b188cc2019-01-07 10:46:33 -07001599static void io_put_req(struct io_kiocb *req)
1600{
Jens Axboedef596e2019-01-09 08:59:42 -07001601 if (refcount_dec_and_test(&req->refs))
1602 io_free_req(req);
1603}
1604
Pavel Begunkove9fd9392020-03-04 16:14:12 +03001605static void io_steal_work(struct io_kiocb *req,
1606 struct io_wq_work **workptr)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001607{
1608 /*
1609 * It's in an io-wq worker, so there always should be at least
1610 * one reference, which will be dropped in io_put_work() just
1611 * after the current handler returns.
1612 *
1613 * It also means, that if the counter dropped to 1, then there is
1614 * no asynchronous users left, so it's safe to steal the next work.
1615 */
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001616 if (refcount_read(&req->refs) == 1) {
1617 struct io_kiocb *nxt = NULL;
1618
1619 io_req_find_next(req, &nxt);
1620 if (nxt)
1621 io_wq_assign_next(workptr, nxt);
1622 }
1623}
1624
Jens Axboe978db572019-11-14 22:39:04 -07001625/*
1626 * Must only be used if we don't need to care about links, usually from
1627 * within the completion handling itself.
1628 */
1629static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001630{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001631 /* drop both submit and complete references */
1632 if (refcount_sub_and_test(2, &req->refs))
1633 __io_free_req(req);
1634}
1635
Jens Axboe978db572019-11-14 22:39:04 -07001636static void io_double_put_req(struct io_kiocb *req)
1637{
1638 /* drop both submit and complete references */
1639 if (refcount_sub_and_test(2, &req->refs))
1640 io_free_req(req);
1641}
1642
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001643static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001644{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001645 struct io_rings *rings = ctx->rings;
1646
Jens Axboead3eb2c2019-12-18 17:12:20 -07001647 if (test_bit(0, &ctx->cq_check_overflow)) {
1648 /*
1649 * noflush == true is from the waitqueue handler, just ensure
1650 * we wake up the task, and the next invocation will flush the
1651 * entries. We cannot safely to it from here.
1652 */
1653 if (noflush && !list_empty(&ctx->cq_overflow_list))
1654 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001655
Jens Axboead3eb2c2019-12-18 17:12:20 -07001656 io_cqring_overflow_flush(ctx, false);
1657 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001658
Jens Axboea3a0e432019-08-20 11:03:11 -06001659 /* See comment at the top of this file */
1660 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001661 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001662}
1663
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001664static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1665{
1666 struct io_rings *rings = ctx->rings;
1667
1668 /* make sure SQ entry isn't read before tail */
1669 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1670}
1671
Jens Axboe8237e042019-12-28 10:48:22 -07001672static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001673{
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001674 if ((req->flags & REQ_F_LINK_HEAD) || io_is_fallback_req(req))
Jens Axboec6ca97b302019-12-28 12:11:08 -07001675 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001676
Jens Axboec6ca97b302019-12-28 12:11:08 -07001677 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1678 rb->need_iter++;
1679
1680 rb->reqs[rb->to_free++] = req;
1681 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1682 io_free_req_many(req->ctx, rb);
1683 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001684}
1685
Jens Axboebcda7ba2020-02-23 16:42:51 -07001686static int io_put_kbuf(struct io_kiocb *req)
1687{
Jens Axboe4d954c22020-02-27 07:31:19 -07001688 struct io_buffer *kbuf;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001689 int cflags;
1690
Jens Axboe4d954c22020-02-27 07:31:19 -07001691 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001692 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
1693 cflags |= IORING_CQE_F_BUFFER;
1694 req->rw.addr = 0;
1695 kfree(kbuf);
1696 return cflags;
1697}
1698
Jens Axboedef596e2019-01-09 08:59:42 -07001699/*
1700 * Find and free completed poll iocbs
1701 */
1702static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1703 struct list_head *done)
1704{
Jens Axboe8237e042019-12-28 10:48:22 -07001705 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001706 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001707
Jens Axboec6ca97b302019-12-28 12:11:08 -07001708 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001709 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07001710 int cflags = 0;
1711
Jens Axboedef596e2019-01-09 08:59:42 -07001712 req = list_first_entry(done, struct io_kiocb, list);
1713 list_del(&req->list);
1714
Jens Axboebcda7ba2020-02-23 16:42:51 -07001715 if (req->flags & REQ_F_BUFFER_SELECTED)
1716 cflags = io_put_kbuf(req);
1717
1718 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07001719 (*nr_events)++;
1720
Jens Axboe8237e042019-12-28 10:48:22 -07001721 if (refcount_dec_and_test(&req->refs) &&
1722 !io_req_multi_free(&rb, req))
1723 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001724 }
Jens Axboedef596e2019-01-09 08:59:42 -07001725
Jens Axboe09bb8392019-03-13 12:39:28 -06001726 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08001727 if (ctx->flags & IORING_SETUP_SQPOLL)
1728 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001729 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001730}
1731
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001732static void io_iopoll_queue(struct list_head *again)
1733{
1734 struct io_kiocb *req;
1735
1736 do {
1737 req = list_first_entry(again, struct io_kiocb, list);
1738 list_del(&req->list);
1739 refcount_inc(&req->refs);
1740 io_queue_async_work(req);
1741 } while (!list_empty(again));
1742}
1743
Jens Axboedef596e2019-01-09 08:59:42 -07001744static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1745 long min)
1746{
1747 struct io_kiocb *req, *tmp;
1748 LIST_HEAD(done);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001749 LIST_HEAD(again);
Jens Axboedef596e2019-01-09 08:59:42 -07001750 bool spin;
1751 int ret;
1752
1753 /*
1754 * Only spin for completions if we don't have multiple devices hanging
1755 * off our complete list, and we're under the requested amount.
1756 */
1757 spin = !ctx->poll_multi_file && *nr_events < min;
1758
1759 ret = 0;
1760 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001761 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001762
1763 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001764 * Move completed and retryable entries to our local lists.
1765 * If we find a request that requires polling, break out
1766 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07001767 */
1768 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1769 list_move_tail(&req->list, &done);
1770 continue;
1771 }
1772 if (!list_empty(&done))
1773 break;
1774
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001775 if (req->result == -EAGAIN) {
1776 list_move_tail(&req->list, &again);
1777 continue;
1778 }
1779 if (!list_empty(&again))
1780 break;
1781
Jens Axboedef596e2019-01-09 08:59:42 -07001782 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1783 if (ret < 0)
1784 break;
1785
1786 if (ret && spin)
1787 spin = false;
1788 ret = 0;
1789 }
1790
1791 if (!list_empty(&done))
1792 io_iopoll_complete(ctx, nr_events, &done);
1793
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001794 if (!list_empty(&again))
1795 io_iopoll_queue(&again);
1796
Jens Axboedef596e2019-01-09 08:59:42 -07001797 return ret;
1798}
1799
1800/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001801 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001802 * non-spinning poll check - we'll still enter the driver poll loop, but only
1803 * as a non-spinning completion check.
1804 */
1805static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1806 long min)
1807{
Jens Axboe08f54392019-08-21 22:19:11 -06001808 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001809 int ret;
1810
1811 ret = io_do_iopoll(ctx, nr_events, min);
1812 if (ret < 0)
1813 return ret;
1814 if (!min || *nr_events >= min)
1815 return 0;
1816 }
1817
1818 return 1;
1819}
1820
1821/*
1822 * We can't just wait for polled events to come to us, we have to actively
1823 * find and complete them.
1824 */
1825static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1826{
1827 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1828 return;
1829
1830 mutex_lock(&ctx->uring_lock);
1831 while (!list_empty(&ctx->poll_list)) {
1832 unsigned int nr_events = 0;
1833
1834 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001835
1836 /*
1837 * Ensure we allow local-to-the-cpu processing to take place,
1838 * in this case we need to ensure that we reap all events.
1839 */
1840 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001841 }
1842 mutex_unlock(&ctx->uring_lock);
1843}
1844
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001845static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1846 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001847{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001848 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001849
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001850 /*
1851 * We disallow the app entering submit/complete with polling, but we
1852 * still need to lock the ring to prevent racing with polled issue
1853 * that got punted to a workqueue.
1854 */
1855 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001856 do {
1857 int tmin = 0;
1858
Jens Axboe500f9fb2019-08-19 12:15:59 -06001859 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001860 * Don't enter poll loop if we already have events pending.
1861 * If we do, we can potentially be spinning for commands that
1862 * already triggered a CQE (eg in error).
1863 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001864 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001865 break;
1866
1867 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001868 * If a submit got punted to a workqueue, we can have the
1869 * application entering polling for a command before it gets
1870 * issued. That app will hold the uring_lock for the duration
1871 * of the poll right here, so we need to take a breather every
1872 * now and then to ensure that the issue has a chance to add
1873 * the poll to the issued list. Otherwise we can spin here
1874 * forever, while the workqueue is stuck trying to acquire the
1875 * very same mutex.
1876 */
1877 if (!(++iters & 7)) {
1878 mutex_unlock(&ctx->uring_lock);
1879 mutex_lock(&ctx->uring_lock);
1880 }
1881
Jens Axboedef596e2019-01-09 08:59:42 -07001882 if (*nr_events < min)
1883 tmin = min - *nr_events;
1884
1885 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1886 if (ret <= 0)
1887 break;
1888 ret = 0;
1889 } while (min && !*nr_events && !need_resched());
1890
Jens Axboe500f9fb2019-08-19 12:15:59 -06001891 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001892 return ret;
1893}
1894
Jens Axboe491381ce2019-10-17 09:20:46 -06001895static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001896{
Jens Axboe491381ce2019-10-17 09:20:46 -06001897 /*
1898 * Tell lockdep we inherited freeze protection from submission
1899 * thread.
1900 */
1901 if (req->flags & REQ_F_ISREG) {
1902 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001903
Jens Axboe491381ce2019-10-17 09:20:46 -06001904 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001905 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001906 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001907}
1908
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001909static inline void req_set_fail_links(struct io_kiocb *req)
1910{
1911 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1912 req->flags |= REQ_F_FAIL_LINK;
1913}
1914
Jens Axboeba816ad2019-09-28 11:36:45 -06001915static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001916{
Jens Axboe9adbd452019-12-20 08:45:55 -07001917 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001918 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001919
Jens Axboe491381ce2019-10-17 09:20:46 -06001920 if (kiocb->ki_flags & IOCB_WRITE)
1921 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001922
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001923 if (res != req->result)
1924 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001925 if (req->flags & REQ_F_BUFFER_SELECTED)
1926 cflags = io_put_kbuf(req);
1927 __io_cqring_add_event(req, res, cflags);
Jens Axboeba816ad2019-09-28 11:36:45 -06001928}
1929
1930static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1931{
Jens Axboe9adbd452019-12-20 08:45:55 -07001932 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001933
1934 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001935 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001936}
1937
Jens Axboedef596e2019-01-09 08:59:42 -07001938static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1939{
Jens Axboe9adbd452019-12-20 08:45:55 -07001940 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001941
Jens Axboe491381ce2019-10-17 09:20:46 -06001942 if (kiocb->ki_flags & IOCB_WRITE)
1943 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001944
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001945 if (res != req->result)
1946 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001947 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001948 if (res != -EAGAIN)
1949 req->flags |= REQ_F_IOPOLL_COMPLETED;
1950}
1951
1952/*
1953 * After the iocb has been issued, it's safe to be found on the poll list.
1954 * Adding the kiocb to the list AFTER submission ensures that we don't
1955 * find it from a io_iopoll_getevents() thread before the issuer is done
1956 * accessing the kiocb cookie.
1957 */
1958static void io_iopoll_req_issued(struct io_kiocb *req)
1959{
1960 struct io_ring_ctx *ctx = req->ctx;
1961
1962 /*
1963 * Track whether we have multiple files in our lists. This will impact
1964 * how we do polling eventually, not spinning if we're on potentially
1965 * different devices.
1966 */
1967 if (list_empty(&ctx->poll_list)) {
1968 ctx->poll_multi_file = false;
1969 } else if (!ctx->poll_multi_file) {
1970 struct io_kiocb *list_req;
1971
1972 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1973 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001974 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001975 ctx->poll_multi_file = true;
1976 }
1977
1978 /*
1979 * For fast devices, IO may have already completed. If it has, add
1980 * it to the front so we find it first.
1981 */
1982 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1983 list_add(&req->list, &ctx->poll_list);
1984 else
1985 list_add_tail(&req->list, &ctx->poll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08001986
1987 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
1988 wq_has_sleeper(&ctx->sqo_wait))
1989 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07001990}
1991
Jens Axboe3d6770f2019-04-13 11:50:54 -06001992static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001993{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001994 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001995 int diff = state->has_refs - state->used_refs;
1996
1997 if (diff)
1998 fput_many(state->file, diff);
1999 state->file = NULL;
2000 }
2001}
2002
2003/*
2004 * Get as many references to a file as we have IOs left in this submission,
2005 * assuming most submissions are for one file, or at least that each file
2006 * has more than one submission.
2007 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002008static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002009{
2010 if (!state)
2011 return fget(fd);
2012
2013 if (state->file) {
2014 if (state->fd == fd) {
2015 state->used_refs++;
2016 state->ios_left--;
2017 return state->file;
2018 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06002019 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002020 }
2021 state->file = fget_many(fd, state->ios_left);
2022 if (!state->file)
2023 return NULL;
2024
2025 state->fd = fd;
2026 state->has_refs = state->ios_left;
2027 state->used_refs = 1;
2028 state->ios_left--;
2029 return state->file;
2030}
2031
Jens Axboe2b188cc2019-01-07 10:46:33 -07002032/*
2033 * If we tracked the file through the SCM inflight mechanism, we could support
2034 * any file. For now, just ensure that anything potentially problematic is done
2035 * inline.
2036 */
2037static bool io_file_supports_async(struct file *file)
2038{
2039 umode_t mode = file_inode(file)->i_mode;
2040
Jens Axboe10d59342019-12-09 20:16:22 -07002041 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002042 return true;
2043 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
2044 return true;
2045
2046 return false;
2047}
2048
Jens Axboe3529d8c2019-12-19 18:24:38 -07002049static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2050 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002051{
Jens Axboedef596e2019-01-09 08:59:42 -07002052 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002053 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002054 unsigned ioprio;
2055 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002056
Jens Axboe491381ce2019-10-17 09:20:46 -06002057 if (S_ISREG(file_inode(req->file)->i_mode))
2058 req->flags |= REQ_F_ISREG;
2059
Jens Axboe2b188cc2019-01-07 10:46:33 -07002060 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002061 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2062 req->flags |= REQ_F_CUR_POS;
2063 kiocb->ki_pos = req->file->f_pos;
2064 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002065 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002066 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2067 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2068 if (unlikely(ret))
2069 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002070
2071 ioprio = READ_ONCE(sqe->ioprio);
2072 if (ioprio) {
2073 ret = ioprio_check_cap(ioprio);
2074 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002075 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002076
2077 kiocb->ki_ioprio = ioprio;
2078 } else
2079 kiocb->ki_ioprio = get_current_ioprio();
2080
Stefan Bühler8449eed2019-04-27 20:34:19 +02002081 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06002082 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
2083 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02002084 req->flags |= REQ_F_NOWAIT;
2085
2086 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002087 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02002088
Jens Axboedef596e2019-01-09 08:59:42 -07002089 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002090 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2091 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002092 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002093
Jens Axboedef596e2019-01-09 08:59:42 -07002094 kiocb->ki_flags |= IOCB_HIPRI;
2095 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06002096 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002097 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002098 if (kiocb->ki_flags & IOCB_HIPRI)
2099 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002100 kiocb->ki_complete = io_complete_rw;
2101 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002102
Jens Axboe3529d8c2019-12-19 18:24:38 -07002103 req->rw.addr = READ_ONCE(sqe->addr);
2104 req->rw.len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002105 /* we own ->private, reuse it for the buffer index / buffer ID */
Jens Axboe9adbd452019-12-20 08:45:55 -07002106 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002107 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002108 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002109}
2110
2111static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2112{
2113 switch (ret) {
2114 case -EIOCBQUEUED:
2115 break;
2116 case -ERESTARTSYS:
2117 case -ERESTARTNOINTR:
2118 case -ERESTARTNOHAND:
2119 case -ERESTART_RESTARTBLOCK:
2120 /*
2121 * We can't just restart the syscall, since previously
2122 * submitted sqes may already be in progress. Just fail this
2123 * IO with EINTR.
2124 */
2125 ret = -EINTR;
2126 /* fall through */
2127 default:
2128 kiocb->ki_complete(kiocb, ret, 0);
2129 }
2130}
2131
Pavel Begunkov014db002020-03-03 21:33:12 +03002132static void kiocb_done(struct kiocb *kiocb, ssize_t ret)
Jens Axboeba816ad2019-09-28 11:36:45 -06002133{
Jens Axboeba042912019-12-25 16:33:42 -07002134 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2135
2136 if (req->flags & REQ_F_CUR_POS)
2137 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002138 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Pavel Begunkov014db002020-03-03 21:33:12 +03002139 io_complete_rw(kiocb, ret, 0);
Jens Axboeba816ad2019-09-28 11:36:45 -06002140 else
2141 io_rw_done(kiocb, ret);
2142}
2143
Jens Axboe9adbd452019-12-20 08:45:55 -07002144static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002145 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002146{
Jens Axboe9adbd452019-12-20 08:45:55 -07002147 struct io_ring_ctx *ctx = req->ctx;
2148 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002149 struct io_mapped_ubuf *imu;
2150 unsigned index, buf_index;
2151 size_t offset;
2152 u64 buf_addr;
2153
2154 /* attempt to use fixed buffers without having provided iovecs */
2155 if (unlikely(!ctx->user_bufs))
2156 return -EFAULT;
2157
Jens Axboe9adbd452019-12-20 08:45:55 -07002158 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07002159 if (unlikely(buf_index >= ctx->nr_user_bufs))
2160 return -EFAULT;
2161
2162 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2163 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002164 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002165
2166 /* overflow */
2167 if (buf_addr + len < buf_addr)
2168 return -EFAULT;
2169 /* not inside the mapped region */
2170 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2171 return -EFAULT;
2172
2173 /*
2174 * May not be a start of buffer, set size appropriately
2175 * and advance us to the beginning.
2176 */
2177 offset = buf_addr - imu->ubuf;
2178 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002179
2180 if (offset) {
2181 /*
2182 * Don't use iov_iter_advance() here, as it's really slow for
2183 * using the latter parts of a big fixed buffer - it iterates
2184 * over each segment manually. We can cheat a bit here, because
2185 * we know that:
2186 *
2187 * 1) it's a BVEC iter, we set it up
2188 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2189 * first and last bvec
2190 *
2191 * So just find our index, and adjust the iterator afterwards.
2192 * If the offset is within the first bvec (or the whole first
2193 * bvec, just use iov_iter_advance(). This makes it easier
2194 * since we can just skip the first segment, which may not
2195 * be PAGE_SIZE aligned.
2196 */
2197 const struct bio_vec *bvec = imu->bvec;
2198
2199 if (offset <= bvec->bv_len) {
2200 iov_iter_advance(iter, offset);
2201 } else {
2202 unsigned long seg_skip;
2203
2204 /* skip first vec */
2205 offset -= bvec->bv_len;
2206 seg_skip = 1 + (offset >> PAGE_SHIFT);
2207
2208 iter->bvec = bvec + seg_skip;
2209 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002210 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002211 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002212 }
2213 }
2214
Jens Axboe5e559562019-11-13 16:12:46 -07002215 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002216}
2217
Jens Axboebcda7ba2020-02-23 16:42:51 -07002218static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2219{
2220 if (needs_lock)
2221 mutex_unlock(&ctx->uring_lock);
2222}
2223
2224static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2225{
2226 /*
2227 * "Normal" inline submissions always hold the uring_lock, since we
2228 * grab it from the system call. Same is true for the SQPOLL offload.
2229 * The only exception is when we've detached the request and issue it
2230 * from an async worker thread, grab the lock for that case.
2231 */
2232 if (needs_lock)
2233 mutex_lock(&ctx->uring_lock);
2234}
2235
2236static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2237 int bgid, struct io_buffer *kbuf,
2238 bool needs_lock)
2239{
2240 struct io_buffer *head;
2241
2242 if (req->flags & REQ_F_BUFFER_SELECTED)
2243 return kbuf;
2244
2245 io_ring_submit_lock(req->ctx, needs_lock);
2246
2247 lockdep_assert_held(&req->ctx->uring_lock);
2248
2249 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2250 if (head) {
2251 if (!list_empty(&head->list)) {
2252 kbuf = list_last_entry(&head->list, struct io_buffer,
2253 list);
2254 list_del(&kbuf->list);
2255 } else {
2256 kbuf = head;
2257 idr_remove(&req->ctx->io_buffer_idr, bgid);
2258 }
2259 if (*len > kbuf->len)
2260 *len = kbuf->len;
2261 } else {
2262 kbuf = ERR_PTR(-ENOBUFS);
2263 }
2264
2265 io_ring_submit_unlock(req->ctx, needs_lock);
2266
2267 return kbuf;
2268}
2269
Jens Axboe4d954c22020-02-27 07:31:19 -07002270static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2271 bool needs_lock)
2272{
2273 struct io_buffer *kbuf;
2274 int bgid;
2275
2276 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2277 bgid = (int) (unsigned long) req->rw.kiocb.private;
2278 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2279 if (IS_ERR(kbuf))
2280 return kbuf;
2281 req->rw.addr = (u64) (unsigned long) kbuf;
2282 req->flags |= REQ_F_BUFFER_SELECTED;
2283 return u64_to_user_ptr(kbuf->addr);
2284}
2285
2286#ifdef CONFIG_COMPAT
2287static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2288 bool needs_lock)
2289{
2290 struct compat_iovec __user *uiov;
2291 compat_ssize_t clen;
2292 void __user *buf;
2293 ssize_t len;
2294
2295 uiov = u64_to_user_ptr(req->rw.addr);
2296 if (!access_ok(uiov, sizeof(*uiov)))
2297 return -EFAULT;
2298 if (__get_user(clen, &uiov->iov_len))
2299 return -EFAULT;
2300 if (clen < 0)
2301 return -EINVAL;
2302
2303 len = clen;
2304 buf = io_rw_buffer_select(req, &len, needs_lock);
2305 if (IS_ERR(buf))
2306 return PTR_ERR(buf);
2307 iov[0].iov_base = buf;
2308 iov[0].iov_len = (compat_size_t) len;
2309 return 0;
2310}
2311#endif
2312
2313static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2314 bool needs_lock)
2315{
2316 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2317 void __user *buf;
2318 ssize_t len;
2319
2320 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2321 return -EFAULT;
2322
2323 len = iov[0].iov_len;
2324 if (len < 0)
2325 return -EINVAL;
2326 buf = io_rw_buffer_select(req, &len, needs_lock);
2327 if (IS_ERR(buf))
2328 return PTR_ERR(buf);
2329 iov[0].iov_base = buf;
2330 iov[0].iov_len = len;
2331 return 0;
2332}
2333
2334static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2335 bool needs_lock)
2336{
2337 if (req->flags & REQ_F_BUFFER_SELECTED)
2338 return 0;
2339 if (!req->rw.len)
2340 return 0;
2341 else if (req->rw.len > 1)
2342 return -EINVAL;
2343
2344#ifdef CONFIG_COMPAT
2345 if (req->ctx->compat)
2346 return io_compat_import(req, iov, needs_lock);
2347#endif
2348
2349 return __io_iov_buffer_select(req, iov, needs_lock);
2350}
2351
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002352static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
Jens Axboebcda7ba2020-02-23 16:42:51 -07002353 struct iovec **iovec, struct iov_iter *iter,
2354 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002355{
Jens Axboe9adbd452019-12-20 08:45:55 -07002356 void __user *buf = u64_to_user_ptr(req->rw.addr);
2357 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002358 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002359 u8 opcode;
2360
Jens Axboed625c6e2019-12-17 19:53:05 -07002361 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002362 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002363 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002364 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002365 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002366
Jens Axboebcda7ba2020-02-23 16:42:51 -07002367 /* buffer index only valid with fixed read/write, or buffer select */
2368 if (req->rw.kiocb.private && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002369 return -EINVAL;
2370
Jens Axboe3a6820f2019-12-22 15:19:35 -07002371 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002372 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002373 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
2374 if (IS_ERR(buf)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002375 *iovec = NULL;
Jens Axboe4d954c22020-02-27 07:31:19 -07002376 return PTR_ERR(buf);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002377 }
Jens Axboe3f9d6442020-03-11 12:27:04 -06002378 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002379 }
2380
Jens Axboe3a6820f2019-12-22 15:19:35 -07002381 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2382 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002383 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002384 }
2385
Jens Axboef67676d2019-12-02 11:03:47 -07002386 if (req->io) {
2387 struct io_async_rw *iorw = &req->io->rw;
2388
2389 *iovec = iorw->iov;
2390 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2391 if (iorw->iov == iorw->fast_iov)
2392 *iovec = NULL;
2393 return iorw->size;
2394 }
2395
Jens Axboe4d954c22020-02-27 07:31:19 -07002396 if (req->flags & REQ_F_BUFFER_SELECT) {
2397 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002398 if (!ret) {
2399 ret = (*iovec)->iov_len;
2400 iov_iter_init(iter, rw, *iovec, 1, ret);
2401 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002402 *iovec = NULL;
2403 return ret;
2404 }
2405
Jens Axboe2b188cc2019-01-07 10:46:33 -07002406#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002407 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002408 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2409 iovec, iter);
2410#endif
2411
2412 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2413}
2414
Jens Axboe32960612019-09-23 11:05:34 -06002415/*
2416 * For files that don't have ->read_iter() and ->write_iter(), handle them
2417 * by looping over ->read() or ->write() manually.
2418 */
2419static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2420 struct iov_iter *iter)
2421{
2422 ssize_t ret = 0;
2423
2424 /*
2425 * Don't support polled IO through this interface, and we can't
2426 * support non-blocking either. For the latter, this just causes
2427 * the kiocb to be handled from an async context.
2428 */
2429 if (kiocb->ki_flags & IOCB_HIPRI)
2430 return -EOPNOTSUPP;
2431 if (kiocb->ki_flags & IOCB_NOWAIT)
2432 return -EAGAIN;
2433
2434 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002435 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002436 ssize_t nr;
2437
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002438 if (!iov_iter_is_bvec(iter)) {
2439 iovec = iov_iter_iovec(iter);
2440 } else {
2441 /* fixed buffers import bvec */
2442 iovec.iov_base = kmap(iter->bvec->bv_page)
2443 + iter->iov_offset;
2444 iovec.iov_len = min(iter->count,
2445 iter->bvec->bv_len - iter->iov_offset);
2446 }
2447
Jens Axboe32960612019-09-23 11:05:34 -06002448 if (rw == READ) {
2449 nr = file->f_op->read(file, iovec.iov_base,
2450 iovec.iov_len, &kiocb->ki_pos);
2451 } else {
2452 nr = file->f_op->write(file, iovec.iov_base,
2453 iovec.iov_len, &kiocb->ki_pos);
2454 }
2455
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002456 if (iov_iter_is_bvec(iter))
2457 kunmap(iter->bvec->bv_page);
2458
Jens Axboe32960612019-09-23 11:05:34 -06002459 if (nr < 0) {
2460 if (!ret)
2461 ret = nr;
2462 break;
2463 }
2464 ret += nr;
2465 if (nr != iovec.iov_len)
2466 break;
2467 iov_iter_advance(iter, nr);
2468 }
2469
2470 return ret;
2471}
2472
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002473static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002474 struct iovec *iovec, struct iovec *fast_iov,
2475 struct iov_iter *iter)
2476{
2477 req->io->rw.nr_segs = iter->nr_segs;
2478 req->io->rw.size = io_size;
2479 req->io->rw.iov = iovec;
2480 if (!req->io->rw.iov) {
2481 req->io->rw.iov = req->io->rw.fast_iov;
Xiaoguang Wang45097da2020-04-08 22:29:58 +08002482 if (req->io->rw.iov != fast_iov)
2483 memcpy(req->io->rw.iov, fast_iov,
2484 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002485 } else {
2486 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002487 }
2488}
2489
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002490static inline int __io_alloc_async_ctx(struct io_kiocb *req)
2491{
2492 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
2493 return req->io == NULL;
2494}
2495
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002496static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002497{
Jens Axboed3656342019-12-18 09:50:26 -07002498 if (!io_op_defs[req->opcode].async_ctx)
2499 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002500
2501 return __io_alloc_async_ctx(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002502}
2503
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002504static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2505 struct iovec *iovec, struct iovec *fast_iov,
2506 struct iov_iter *iter)
2507{
Jens Axboe980ad262020-01-24 23:08:54 -07002508 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002509 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002510 if (!req->io) {
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002511 if (__io_alloc_async_ctx(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07002512 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002513
Jens Axboe5d204bc2020-01-31 12:06:52 -07002514 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2515 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002516 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002517}
2518
Jens Axboe3529d8c2019-12-19 18:24:38 -07002519static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2520 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002521{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002522 struct io_async_ctx *io;
2523 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002524 ssize_t ret;
2525
Jens Axboe3529d8c2019-12-19 18:24:38 -07002526 ret = io_prep_rw(req, sqe, force_nonblock);
2527 if (ret)
2528 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002529
Jens Axboe3529d8c2019-12-19 18:24:38 -07002530 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2531 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002532
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002533 /* either don't need iovec imported or already have it */
2534 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002535 return 0;
2536
2537 io = req->io;
2538 io->rw.iov = io->rw.fast_iov;
2539 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002540 ret = io_import_iovec(READ, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002541 req->io = io;
2542 if (ret < 0)
2543 return ret;
2544
2545 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2546 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002547}
2548
Pavel Begunkov014db002020-03-03 21:33:12 +03002549static int io_read(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002550{
2551 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002552 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002553 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002554 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002555 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002556
Jens Axboebcda7ba2020-02-23 16:42:51 -07002557 ret = io_import_iovec(READ, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07002558 if (ret < 0)
2559 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002560
Jens Axboefd6c2e42019-12-18 12:19:41 -07002561 /* Ensure we clear previously set non-block flag */
2562 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07002563 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002564
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002565 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002566 io_size = ret;
Pavel Begunkovdea3b492020-04-12 02:05:04 +03002567 if (req->flags & REQ_F_LINK_HEAD)
Jens Axboef67676d2019-12-02 11:03:47 -07002568 req->result = io_size;
2569
2570 /*
2571 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2572 * we know to async punt it even if it was opened O_NONBLOCK
2573 */
Jens Axboe29de5f62020-02-20 09:56:08 -07002574 if (force_nonblock && !io_file_supports_async(req->file))
Jens Axboef67676d2019-12-02 11:03:47 -07002575 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002576
Jens Axboe31b51512019-01-18 22:56:34 -07002577 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002578 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002579 if (!ret) {
2580 ssize_t ret2;
2581
Jens Axboe9adbd452019-12-20 08:45:55 -07002582 if (req->file->f_op->read_iter)
2583 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002584 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002585 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002586
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002587 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002588 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkov014db002020-03-03 21:33:12 +03002589 kiocb_done(kiocb, ret2);
Jens Axboef67676d2019-12-02 11:03:47 -07002590 } else {
2591copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002592 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002593 inline_vecs, &iter);
2594 if (ret)
2595 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002596 /* any defer here is final, must blocking retry */
2597 if (!(req->flags & REQ_F_NOWAIT))
2598 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002599 return -EAGAIN;
2600 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002601 }
Jens Axboef67676d2019-12-02 11:03:47 -07002602out_free:
Pavel Begunkov1e950812020-02-06 19:51:16 +03002603 kfree(iovec);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002604 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002605 return ret;
2606}
2607
Jens Axboe3529d8c2019-12-19 18:24:38 -07002608static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2609 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002610{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002611 struct io_async_ctx *io;
2612 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002613 ssize_t ret;
2614
Jens Axboe3529d8c2019-12-19 18:24:38 -07002615 ret = io_prep_rw(req, sqe, force_nonblock);
2616 if (ret)
2617 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002618
Jens Axboe3529d8c2019-12-19 18:24:38 -07002619 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2620 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002621
Jens Axboe4ed734b2020-03-20 11:23:41 -06002622 req->fsize = rlimit(RLIMIT_FSIZE);
2623
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002624 /* either don't need iovec imported or already have it */
2625 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002626 return 0;
2627
2628 io = req->io;
2629 io->rw.iov = io->rw.fast_iov;
2630 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002631 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002632 req->io = io;
2633 if (ret < 0)
2634 return ret;
2635
2636 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2637 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002638}
2639
Pavel Begunkov014db002020-03-03 21:33:12 +03002640static int io_write(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002641{
2642 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002643 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002644 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002645 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002646 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002647
Jens Axboebcda7ba2020-02-23 16:42:51 -07002648 ret = io_import_iovec(WRITE, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07002649 if (ret < 0)
2650 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002651
Jens Axboefd6c2e42019-12-18 12:19:41 -07002652 /* Ensure we clear previously set non-block flag */
2653 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002654 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002655
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002656 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002657 io_size = ret;
Pavel Begunkovdea3b492020-04-12 02:05:04 +03002658 if (req->flags & REQ_F_LINK_HEAD)
Jens Axboef67676d2019-12-02 11:03:47 -07002659 req->result = io_size;
2660
2661 /*
2662 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2663 * we know to async punt it even if it was opened O_NONBLOCK
2664 */
Jens Axboe29de5f62020-02-20 09:56:08 -07002665 if (force_nonblock && !io_file_supports_async(req->file))
Jens Axboef67676d2019-12-02 11:03:47 -07002666 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07002667
Jens Axboe10d59342019-12-09 20:16:22 -07002668 /* file path doesn't support NOWAIT for non-direct_IO */
2669 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2670 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002671 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002672
Jens Axboe31b51512019-01-18 22:56:34 -07002673 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002674 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002675 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002676 ssize_t ret2;
2677
Jens Axboe2b188cc2019-01-07 10:46:33 -07002678 /*
2679 * Open-code file_start_write here to grab freeze protection,
2680 * which will be released by another thread in
2681 * io_complete_rw(). Fool lockdep by telling it the lock got
2682 * released so that it doesn't complain about the held lock when
2683 * we return to userspace.
2684 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002685 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002686 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002687 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002688 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002689 SB_FREEZE_WRITE);
2690 }
2691 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002692
Jens Axboe4ed734b2020-03-20 11:23:41 -06002693 if (!force_nonblock)
2694 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
2695
Jens Axboe9adbd452019-12-20 08:45:55 -07002696 if (req->file->f_op->write_iter)
2697 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002698 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002699 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboe4ed734b2020-03-20 11:23:41 -06002700
2701 if (!force_nonblock)
2702 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
2703
Jens Axboefaac9962020-02-07 15:45:22 -07002704 /*
Chucheng Luobff60352020-03-25 11:31:38 +08002705 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
Jens Axboefaac9962020-02-07 15:45:22 -07002706 * retry them without IOCB_NOWAIT.
2707 */
2708 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
2709 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07002710 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkov014db002020-03-03 21:33:12 +03002711 kiocb_done(kiocb, ret2);
Jens Axboef67676d2019-12-02 11:03:47 -07002712 } else {
2713copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002714 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002715 inline_vecs, &iter);
2716 if (ret)
2717 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002718 /* any defer here is final, must blocking retry */
2719 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002720 return -EAGAIN;
2721 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002722 }
Jens Axboe31b51512019-01-18 22:56:34 -07002723out_free:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002724 req->flags &= ~REQ_F_NEED_CLEANUP;
Pavel Begunkov1e950812020-02-06 19:51:16 +03002725 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002726 return ret;
2727}
2728
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002729static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2730{
2731 struct io_splice* sp = &req->splice;
2732 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
2733 int ret;
2734
2735 if (req->flags & REQ_F_NEED_CLEANUP)
2736 return 0;
2737
2738 sp->file_in = NULL;
2739 sp->off_in = READ_ONCE(sqe->splice_off_in);
2740 sp->off_out = READ_ONCE(sqe->off);
2741 sp->len = READ_ONCE(sqe->len);
2742 sp->flags = READ_ONCE(sqe->splice_flags);
2743
2744 if (unlikely(sp->flags & ~valid_flags))
2745 return -EINVAL;
2746
2747 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
2748 (sp->flags & SPLICE_F_FD_IN_FIXED));
2749 if (ret)
2750 return ret;
2751 req->flags |= REQ_F_NEED_CLEANUP;
2752
2753 if (!S_ISREG(file_inode(sp->file_in)->i_mode))
2754 req->work.flags |= IO_WQ_WORK_UNBOUND;
2755
2756 return 0;
2757}
2758
2759static bool io_splice_punt(struct file *file)
2760{
2761 if (get_pipe_info(file))
2762 return false;
2763 if (!io_file_supports_async(file))
2764 return true;
Jens Axboe88357582020-04-12 21:12:49 -06002765 return !(file->f_flags & O_NONBLOCK);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002766}
2767
Pavel Begunkov014db002020-03-03 21:33:12 +03002768static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002769{
2770 struct io_splice *sp = &req->splice;
2771 struct file *in = sp->file_in;
2772 struct file *out = sp->file_out;
2773 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
2774 loff_t *poff_in, *poff_out;
2775 long ret;
2776
2777 if (force_nonblock) {
2778 if (io_splice_punt(in) || io_splice_punt(out))
2779 return -EAGAIN;
2780 flags |= SPLICE_F_NONBLOCK;
2781 }
2782
2783 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
2784 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
2785 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
2786 if (force_nonblock && ret == -EAGAIN)
2787 return -EAGAIN;
2788
2789 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
2790 req->flags &= ~REQ_F_NEED_CLEANUP;
2791
2792 io_cqring_add_event(req, ret);
2793 if (ret != sp->len)
2794 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03002795 io_put_req(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002796 return 0;
2797}
2798
Jens Axboe2b188cc2019-01-07 10:46:33 -07002799/*
2800 * IORING_OP_NOP just posts a completion event, nothing else.
2801 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002802static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002803{
2804 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002805
Jens Axboedef596e2019-01-09 08:59:42 -07002806 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2807 return -EINVAL;
2808
Jens Axboe78e19bb2019-11-06 15:21:34 -07002809 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002810 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002811 return 0;
2812}
2813
Jens Axboe3529d8c2019-12-19 18:24:38 -07002814static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002815{
Jens Axboe6b063142019-01-10 22:13:58 -07002816 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002817
Jens Axboe09bb8392019-03-13 12:39:28 -06002818 if (!req->file)
2819 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002820
Jens Axboe6b063142019-01-10 22:13:58 -07002821 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002822 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002823 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002824 return -EINVAL;
2825
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002826 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2827 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2828 return -EINVAL;
2829
2830 req->sync.off = READ_ONCE(sqe->off);
2831 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002832 return 0;
2833}
2834
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002835static bool io_req_cancelled(struct io_kiocb *req)
2836{
2837 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2838 req_set_fail_links(req);
2839 io_cqring_add_event(req, -ECANCELED);
2840 io_put_req(req);
2841 return true;
2842 }
2843
2844 return false;
2845}
2846
Pavel Begunkov014db002020-03-03 21:33:12 +03002847static void __io_fsync(struct io_kiocb *req)
Jens Axboe78912932020-01-14 22:09:06 -07002848{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002849 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002850 int ret;
2851
Jens Axboe9adbd452019-12-20 08:45:55 -07002852 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002853 end > 0 ? end : LLONG_MAX,
2854 req->sync.flags & IORING_FSYNC_DATASYNC);
2855 if (ret < 0)
2856 req_set_fail_links(req);
2857 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002858 io_put_req(req);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002859}
2860
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002861static void io_fsync_finish(struct io_wq_work **workptr)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002862{
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002863 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002864
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002865 if (io_req_cancelled(req))
2866 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03002867 __io_fsync(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03002868 io_steal_work(req, workptr);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002869}
2870
Pavel Begunkov014db002020-03-03 21:33:12 +03002871static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002872{
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002873 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002874 if (force_nonblock) {
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002875 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002876 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002877 }
Pavel Begunkov014db002020-03-03 21:33:12 +03002878 __io_fsync(req);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002879 return 0;
2880}
2881
Pavel Begunkov014db002020-03-03 21:33:12 +03002882static void __io_fallocate(struct io_kiocb *req)
Jens Axboed63d1b52019-12-10 10:38:56 -07002883{
Jens Axboed63d1b52019-12-10 10:38:56 -07002884 int ret;
2885
Jens Axboe4ed734b2020-03-20 11:23:41 -06002886 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
Jens Axboed63d1b52019-12-10 10:38:56 -07002887 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2888 req->sync.len);
Jens Axboe4ed734b2020-03-20 11:23:41 -06002889 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
Jens Axboed63d1b52019-12-10 10:38:56 -07002890 if (ret < 0)
2891 req_set_fail_links(req);
2892 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002893 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002894}
2895
Jens Axboe2b188cc2019-01-07 10:46:33 -07002896static void io_fallocate_finish(struct io_wq_work **workptr)
2897{
2898 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Jens Axboed63d1b52019-12-10 10:38:56 -07002899
2900 if (io_req_cancelled(req))
2901 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03002902 __io_fallocate(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03002903 io_steal_work(req, workptr);
Jens Axboed63d1b52019-12-10 10:38:56 -07002904}
2905
2906static int io_fallocate_prep(struct io_kiocb *req,
2907 const struct io_uring_sqe *sqe)
2908{
2909 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2910 return -EINVAL;
2911
2912 req->sync.off = READ_ONCE(sqe->off);
2913 req->sync.len = READ_ONCE(sqe->addr);
2914 req->sync.mode = READ_ONCE(sqe->len);
Jens Axboe4ed734b2020-03-20 11:23:41 -06002915 req->fsize = rlimit(RLIMIT_FSIZE);
Jens Axboed63d1b52019-12-10 10:38:56 -07002916 return 0;
2917}
2918
Pavel Begunkov014db002020-03-03 21:33:12 +03002919static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07002920{
Jens Axboed63d1b52019-12-10 10:38:56 -07002921 /* fallocate always requiring blocking context */
2922 if (force_nonblock) {
Jens Axboed63d1b52019-12-10 10:38:56 -07002923 req->work.func = io_fallocate_finish;
2924 return -EAGAIN;
2925 }
2926
Pavel Begunkov014db002020-03-03 21:33:12 +03002927 __io_fallocate(req);
Jens Axboed63d1b52019-12-10 10:38:56 -07002928 return 0;
2929}
2930
Jens Axboe15b71ab2019-12-11 11:20:36 -07002931static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2932{
Jens Axboef8748882020-01-08 17:47:02 -07002933 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002934 int ret;
2935
2936 if (sqe->ioprio || sqe->buf_index)
2937 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03002938 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07002939 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002940 if (req->flags & REQ_F_NEED_CLEANUP)
2941 return 0;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002942
2943 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002944 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002945 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002946 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06002947 if (force_o_largefile())
2948 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002949
Jens Axboef8748882020-01-08 17:47:02 -07002950 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002951 if (IS_ERR(req->open.filename)) {
2952 ret = PTR_ERR(req->open.filename);
2953 req->open.filename = NULL;
2954 return ret;
2955 }
2956
Jens Axboe4022e7a2020-03-19 19:23:18 -06002957 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002958 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002959 return 0;
2960}
2961
Jens Axboecebdb982020-01-08 17:59:24 -07002962static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2963{
2964 struct open_how __user *how;
2965 const char __user *fname;
2966 size_t len;
2967 int ret;
2968
2969 if (sqe->ioprio || sqe->buf_index)
2970 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03002971 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07002972 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002973 if (req->flags & REQ_F_NEED_CLEANUP)
2974 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07002975
2976 req->open.dfd = READ_ONCE(sqe->fd);
2977 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2978 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2979 len = READ_ONCE(sqe->len);
2980
2981 if (len < OPEN_HOW_SIZE_VER0)
2982 return -EINVAL;
2983
2984 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2985 len);
2986 if (ret)
2987 return ret;
2988
2989 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2990 req->open.how.flags |= O_LARGEFILE;
2991
2992 req->open.filename = getname(fname);
2993 if (IS_ERR(req->open.filename)) {
2994 ret = PTR_ERR(req->open.filename);
2995 req->open.filename = NULL;
2996 return ret;
2997 }
2998
Jens Axboe4022e7a2020-03-19 19:23:18 -06002999 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003000 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboecebdb982020-01-08 17:59:24 -07003001 return 0;
3002}
3003
Pavel Begunkov014db002020-03-03 21:33:12 +03003004static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003005{
3006 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003007 struct file *file;
3008 int ret;
3009
Jens Axboef86cd202020-01-29 13:46:44 -07003010 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003011 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003012
Jens Axboecebdb982020-01-08 17:59:24 -07003013 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003014 if (ret)
3015 goto err;
3016
Jens Axboe4022e7a2020-03-19 19:23:18 -06003017 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003018 if (ret < 0)
3019 goto err;
3020
3021 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3022 if (IS_ERR(file)) {
3023 put_unused_fd(ret);
3024 ret = PTR_ERR(file);
3025 } else {
3026 fsnotify_open(file);
3027 fd_install(ret, file);
3028 }
3029err:
3030 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003031 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003032 if (ret < 0)
3033 req_set_fail_links(req);
3034 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003035 io_put_req(req);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003036 return 0;
3037}
3038
Pavel Begunkov014db002020-03-03 21:33:12 +03003039static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003040{
3041 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
Pavel Begunkov014db002020-03-03 21:33:12 +03003042 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003043}
3044
Jens Axboe067524e2020-03-02 16:32:28 -07003045static int io_remove_buffers_prep(struct io_kiocb *req,
3046 const struct io_uring_sqe *sqe)
3047{
3048 struct io_provide_buf *p = &req->pbuf;
3049 u64 tmp;
3050
3051 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3052 return -EINVAL;
3053
3054 tmp = READ_ONCE(sqe->fd);
3055 if (!tmp || tmp > USHRT_MAX)
3056 return -EINVAL;
3057
3058 memset(p, 0, sizeof(*p));
3059 p->nbufs = tmp;
3060 p->bgid = READ_ONCE(sqe->buf_group);
3061 return 0;
3062}
3063
3064static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3065 int bgid, unsigned nbufs)
3066{
3067 unsigned i = 0;
3068
3069 /* shouldn't happen */
3070 if (!nbufs)
3071 return 0;
3072
3073 /* the head kbuf is the list itself */
3074 while (!list_empty(&buf->list)) {
3075 struct io_buffer *nxt;
3076
3077 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3078 list_del(&nxt->list);
3079 kfree(nxt);
3080 if (++i == nbufs)
3081 return i;
3082 }
3083 i++;
3084 kfree(buf);
3085 idr_remove(&ctx->io_buffer_idr, bgid);
3086
3087 return i;
3088}
3089
3090static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock)
3091{
3092 struct io_provide_buf *p = &req->pbuf;
3093 struct io_ring_ctx *ctx = req->ctx;
3094 struct io_buffer *head;
3095 int ret = 0;
3096
3097 io_ring_submit_lock(ctx, !force_nonblock);
3098
3099 lockdep_assert_held(&ctx->uring_lock);
3100
3101 ret = -ENOENT;
3102 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3103 if (head)
3104 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3105
3106 io_ring_submit_lock(ctx, !force_nonblock);
3107 if (ret < 0)
3108 req_set_fail_links(req);
3109 io_cqring_add_event(req, ret);
3110 io_put_req(req);
3111 return 0;
3112}
3113
Jens Axboeddf0322d2020-02-23 16:41:33 -07003114static int io_provide_buffers_prep(struct io_kiocb *req,
3115 const struct io_uring_sqe *sqe)
3116{
3117 struct io_provide_buf *p = &req->pbuf;
3118 u64 tmp;
3119
3120 if (sqe->ioprio || sqe->rw_flags)
3121 return -EINVAL;
3122
3123 tmp = READ_ONCE(sqe->fd);
3124 if (!tmp || tmp > USHRT_MAX)
3125 return -E2BIG;
3126 p->nbufs = tmp;
3127 p->addr = READ_ONCE(sqe->addr);
3128 p->len = READ_ONCE(sqe->len);
3129
3130 if (!access_ok(u64_to_user_ptr(p->addr), p->len))
3131 return -EFAULT;
3132
3133 p->bgid = READ_ONCE(sqe->buf_group);
3134 tmp = READ_ONCE(sqe->off);
3135 if (tmp > USHRT_MAX)
3136 return -E2BIG;
3137 p->bid = tmp;
3138 return 0;
3139}
3140
3141static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3142{
3143 struct io_buffer *buf;
3144 u64 addr = pbuf->addr;
3145 int i, bid = pbuf->bid;
3146
3147 for (i = 0; i < pbuf->nbufs; i++) {
3148 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3149 if (!buf)
3150 break;
3151
3152 buf->addr = addr;
3153 buf->len = pbuf->len;
3154 buf->bid = bid;
3155 addr += pbuf->len;
3156 bid++;
3157 if (!*head) {
3158 INIT_LIST_HEAD(&buf->list);
3159 *head = buf;
3160 } else {
3161 list_add_tail(&buf->list, &(*head)->list);
3162 }
3163 }
3164
3165 return i ? i : -ENOMEM;
3166}
3167
Jens Axboeddf0322d2020-02-23 16:41:33 -07003168static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock)
3169{
3170 struct io_provide_buf *p = &req->pbuf;
3171 struct io_ring_ctx *ctx = req->ctx;
3172 struct io_buffer *head, *list;
3173 int ret = 0;
3174
3175 io_ring_submit_lock(ctx, !force_nonblock);
3176
3177 lockdep_assert_held(&ctx->uring_lock);
3178
3179 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3180
3181 ret = io_add_buffers(p, &head);
3182 if (ret < 0)
3183 goto out;
3184
3185 if (!list) {
3186 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3187 GFP_KERNEL);
3188 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003189 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003190 goto out;
3191 }
3192 }
3193out:
3194 io_ring_submit_unlock(ctx, !force_nonblock);
3195 if (ret < 0)
3196 req_set_fail_links(req);
3197 io_cqring_add_event(req, ret);
3198 io_put_req(req);
3199 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003200}
3201
Jens Axboe3e4827b2020-01-08 15:18:09 -07003202static int io_epoll_ctl_prep(struct io_kiocb *req,
3203 const struct io_uring_sqe *sqe)
3204{
3205#if defined(CONFIG_EPOLL)
3206 if (sqe->ioprio || sqe->buf_index)
3207 return -EINVAL;
3208
3209 req->epoll.epfd = READ_ONCE(sqe->fd);
3210 req->epoll.op = READ_ONCE(sqe->len);
3211 req->epoll.fd = READ_ONCE(sqe->off);
3212
3213 if (ep_op_has_event(req->epoll.op)) {
3214 struct epoll_event __user *ev;
3215
3216 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3217 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3218 return -EFAULT;
3219 }
3220
3221 return 0;
3222#else
3223 return -EOPNOTSUPP;
3224#endif
3225}
3226
Pavel Begunkov014db002020-03-03 21:33:12 +03003227static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003228{
3229#if defined(CONFIG_EPOLL)
3230 struct io_epoll *ie = &req->epoll;
3231 int ret;
3232
3233 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3234 if (force_nonblock && ret == -EAGAIN)
3235 return -EAGAIN;
3236
3237 if (ret < 0)
3238 req_set_fail_links(req);
3239 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003240 io_put_req(req);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003241 return 0;
3242#else
3243 return -EOPNOTSUPP;
3244#endif
3245}
3246
Jens Axboec1ca7572019-12-25 22:18:28 -07003247static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3248{
3249#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3250 if (sqe->ioprio || sqe->buf_index || sqe->off)
3251 return -EINVAL;
3252
3253 req->madvise.addr = READ_ONCE(sqe->addr);
3254 req->madvise.len = READ_ONCE(sqe->len);
3255 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3256 return 0;
3257#else
3258 return -EOPNOTSUPP;
3259#endif
3260}
3261
Pavel Begunkov014db002020-03-03 21:33:12 +03003262static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003263{
3264#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3265 struct io_madvise *ma = &req->madvise;
3266 int ret;
3267
3268 if (force_nonblock)
3269 return -EAGAIN;
3270
3271 ret = do_madvise(ma->addr, ma->len, ma->advice);
3272 if (ret < 0)
3273 req_set_fail_links(req);
3274 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003275 io_put_req(req);
Jens Axboec1ca7572019-12-25 22:18:28 -07003276 return 0;
3277#else
3278 return -EOPNOTSUPP;
3279#endif
3280}
3281
Jens Axboe4840e412019-12-25 22:03:45 -07003282static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3283{
3284 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3285 return -EINVAL;
3286
3287 req->fadvise.offset = READ_ONCE(sqe->off);
3288 req->fadvise.len = READ_ONCE(sqe->len);
3289 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3290 return 0;
3291}
3292
Pavel Begunkov014db002020-03-03 21:33:12 +03003293static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07003294{
3295 struct io_fadvise *fa = &req->fadvise;
3296 int ret;
3297
Jens Axboe3e694262020-02-01 09:22:49 -07003298 if (force_nonblock) {
3299 switch (fa->advice) {
3300 case POSIX_FADV_NORMAL:
3301 case POSIX_FADV_RANDOM:
3302 case POSIX_FADV_SEQUENTIAL:
3303 break;
3304 default:
3305 return -EAGAIN;
3306 }
3307 }
Jens Axboe4840e412019-12-25 22:03:45 -07003308
3309 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3310 if (ret < 0)
3311 req_set_fail_links(req);
3312 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003313 io_put_req(req);
Jens Axboe4840e412019-12-25 22:03:45 -07003314 return 0;
3315}
3316
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003317static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3318{
Jens Axboef8748882020-01-08 17:47:02 -07003319 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003320 unsigned lookup_flags;
3321 int ret;
3322
3323 if (sqe->ioprio || sqe->buf_index)
3324 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003325 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003326 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003327 if (req->flags & REQ_F_NEED_CLEANUP)
3328 return 0;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003329
3330 req->open.dfd = READ_ONCE(sqe->fd);
3331 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07003332 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003333 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07003334 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003335
Jens Axboec12cedf2020-01-08 17:41:21 -07003336 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003337 return -EINVAL;
3338
Jens Axboef8748882020-01-08 17:47:02 -07003339 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003340 if (IS_ERR(req->open.filename)) {
3341 ret = PTR_ERR(req->open.filename);
3342 req->open.filename = NULL;
3343 return ret;
3344 }
3345
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003346 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003347 return 0;
3348}
3349
Pavel Begunkov014db002020-03-03 21:33:12 +03003350static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003351{
3352 struct io_open *ctx = &req->open;
3353 unsigned lookup_flags;
3354 struct path path;
3355 struct kstat stat;
3356 int ret;
3357
3358 if (force_nonblock)
3359 return -EAGAIN;
3360
Jens Axboec12cedf2020-01-08 17:41:21 -07003361 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003362 return -EINVAL;
3363
3364retry:
3365 /* filename_lookup() drops it, keep a reference */
3366 ctx->filename->refcnt++;
3367
3368 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
3369 NULL);
3370 if (ret)
3371 goto err;
3372
Jens Axboec12cedf2020-01-08 17:41:21 -07003373 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003374 path_put(&path);
3375 if (retry_estale(ret, lookup_flags)) {
3376 lookup_flags |= LOOKUP_REVAL;
3377 goto retry;
3378 }
3379 if (!ret)
3380 ret = cp_statx(&stat, ctx->buffer);
3381err:
3382 putname(ctx->filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003383 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003384 if (ret < 0)
3385 req_set_fail_links(req);
3386 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003387 io_put_req(req);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003388 return 0;
3389}
3390
Jens Axboeb5dba592019-12-11 14:02:38 -07003391static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3392{
3393 /*
3394 * If we queue this for async, it must not be cancellable. That would
3395 * leave the 'file' in an undeterminate state.
3396 */
3397 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
3398
3399 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
3400 sqe->rw_flags || sqe->buf_index)
3401 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003402 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003403 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07003404
3405 req->close.fd = READ_ONCE(sqe->fd);
3406 if (req->file->f_op == &io_uring_fops ||
Pavel Begunkovb14cca02020-01-17 04:45:59 +03003407 req->close.fd == req->ctx->ring_fd)
Jens Axboeb5dba592019-12-11 14:02:38 -07003408 return -EBADF;
3409
3410 return 0;
3411}
3412
Pavel Begunkova93b3332020-02-08 14:04:34 +03003413/* only called when __close_fd_get_file() is done */
Pavel Begunkov014db002020-03-03 21:33:12 +03003414static void __io_close_finish(struct io_kiocb *req)
Pavel Begunkova93b3332020-02-08 14:04:34 +03003415{
3416 int ret;
3417
3418 ret = filp_close(req->close.put_file, req->work.files);
3419 if (ret < 0)
3420 req_set_fail_links(req);
3421 io_cqring_add_event(req, ret);
3422 fput(req->close.put_file);
Pavel Begunkov014db002020-03-03 21:33:12 +03003423 io_put_req(req);
Pavel Begunkova93b3332020-02-08 14:04:34 +03003424}
3425
Jens Axboeb5dba592019-12-11 14:02:38 -07003426static void io_close_finish(struct io_wq_work **workptr)
3427{
3428 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Jens Axboeb5dba592019-12-11 14:02:38 -07003429
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03003430 /* not cancellable, don't do io_req_cancelled() */
Pavel Begunkov014db002020-03-03 21:33:12 +03003431 __io_close_finish(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03003432 io_steal_work(req, workptr);
Jens Axboeb5dba592019-12-11 14:02:38 -07003433}
3434
Pavel Begunkov014db002020-03-03 21:33:12 +03003435static int io_close(struct io_kiocb *req, bool force_nonblock)
Jens Axboeb5dba592019-12-11 14:02:38 -07003436{
3437 int ret;
3438
3439 req->close.put_file = NULL;
3440 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
3441 if (ret < 0)
3442 return ret;
3443
3444 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkova2100672020-03-02 23:45:16 +03003445 if (req->close.put_file->f_op->flush && force_nonblock) {
Pavel Begunkov594506f2020-03-03 21:33:11 +03003446 /* submission ref will be dropped, take it for async */
3447 refcount_inc(&req->refs);
3448
Pavel Begunkova2100672020-03-02 23:45:16 +03003449 req->work.func = io_close_finish;
3450 /*
3451 * Do manual async queue here to avoid grabbing files - we don't
3452 * need the files, and it'll cause io_close_finish() to close
3453 * the file again and cause a double CQE entry for this request
3454 */
3455 io_queue_async_work(req);
3456 return 0;
3457 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003458
3459 /*
3460 * No ->flush(), safely close from here and just punt the
3461 * fput() to async context.
3462 */
Pavel Begunkov014db002020-03-03 21:33:12 +03003463 __io_close_finish(req);
Jens Axboe1a417f42020-01-31 17:16:48 -07003464 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07003465}
3466
Jens Axboe3529d8c2019-12-19 18:24:38 -07003467static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003468{
3469 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003470
3471 if (!req->file)
3472 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003473
3474 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3475 return -EINVAL;
3476 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
3477 return -EINVAL;
3478
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003479 req->sync.off = READ_ONCE(sqe->off);
3480 req->sync.len = READ_ONCE(sqe->len);
3481 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003482 return 0;
3483}
3484
Pavel Begunkov014db002020-03-03 21:33:12 +03003485static void __io_sync_file_range(struct io_kiocb *req)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003486{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003487 int ret;
3488
Jens Axboe9adbd452019-12-20 08:45:55 -07003489 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003490 req->sync.flags);
3491 if (ret < 0)
3492 req_set_fail_links(req);
3493 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003494 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03003495}
3496
3497
3498static void io_sync_file_range_finish(struct io_wq_work **workptr)
3499{
3500 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03003501
3502 if (io_req_cancelled(req))
3503 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03003504 __io_sync_file_range(req);
Pavel Begunkov594506f2020-03-03 21:33:11 +03003505 io_put_req(req); /* put submission ref */
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003506}
3507
Pavel Begunkov014db002020-03-03 21:33:12 +03003508static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003509{
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003510 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003511 if (force_nonblock) {
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003512 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003513 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003514 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003515
Pavel Begunkov014db002020-03-03 21:33:12 +03003516 __io_sync_file_range(req);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003517 return 0;
3518}
3519
YueHaibing469956e2020-03-04 15:53:52 +08003520#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003521static int io_setup_async_msg(struct io_kiocb *req,
3522 struct io_async_msghdr *kmsg)
3523{
3524 if (req->io)
3525 return -EAGAIN;
3526 if (io_alloc_async_ctx(req)) {
3527 if (kmsg->iov != kmsg->fast_iov)
3528 kfree(kmsg->iov);
3529 return -ENOMEM;
3530 }
3531 req->flags |= REQ_F_NEED_CLEANUP;
3532 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
3533 return -EAGAIN;
3534}
3535
Jens Axboe3529d8c2019-12-19 18:24:38 -07003536static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06003537{
Jens Axboee47293f2019-12-20 08:58:21 -07003538 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003539 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003540 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003541
Jens Axboee47293f2019-12-20 08:58:21 -07003542 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3543 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07003544 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003545
Jens Axboed8768362020-02-27 14:17:49 -07003546#ifdef CONFIG_COMPAT
3547 if (req->ctx->compat)
3548 sr->msg_flags |= MSG_CMSG_COMPAT;
3549#endif
3550
Jens Axboefddafac2020-01-04 20:19:44 -07003551 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003552 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003553 /* iovec is already imported */
3554 if (req->flags & REQ_F_NEED_CLEANUP)
3555 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003556
Jens Axboed9688562019-12-09 19:35:20 -07003557 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003558 ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003559 &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003560 if (!ret)
3561 req->flags |= REQ_F_NEED_CLEANUP;
3562 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003563}
3564
Pavel Begunkov014db002020-03-03 21:33:12 +03003565static int io_sendmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003566{
Jens Axboe0b416c32019-12-15 10:57:46 -07003567 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07003568 struct socket *sock;
3569 int ret;
3570
3571 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3572 return -EINVAL;
3573
3574 sock = sock_from_file(req->file, &ret);
3575 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003576 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003577 unsigned flags;
3578
Jens Axboe03b12302019-12-02 18:50:25 -07003579 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003580 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003581 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003582 /* if iov is set, it's allocated already */
3583 if (!kmsg->iov)
3584 kmsg->iov = kmsg->fast_iov;
3585 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003586 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003587 struct io_sr_msg *sr = &req->sr_msg;
3588
Jens Axboe0b416c32019-12-15 10:57:46 -07003589 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003590 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003591
3592 io.msg.iov = io.msg.fast_iov;
3593 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3594 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003595 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003596 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003597 }
3598
Jens Axboee47293f2019-12-20 08:58:21 -07003599 flags = req->sr_msg.msg_flags;
3600 if (flags & MSG_DONTWAIT)
3601 req->flags |= REQ_F_NOWAIT;
3602 else if (force_nonblock)
3603 flags |= MSG_DONTWAIT;
3604
Jens Axboe0b416c32019-12-15 10:57:46 -07003605 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003606 if (force_nonblock && ret == -EAGAIN)
3607 return io_setup_async_msg(req, kmsg);
Jens Axboe03b12302019-12-02 18:50:25 -07003608 if (ret == -ERESTARTSYS)
3609 ret = -EINTR;
3610 }
3611
Pavel Begunkov1e950812020-02-06 19:51:16 +03003612 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003613 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003614 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe03b12302019-12-02 18:50:25 -07003615 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003616 if (ret < 0)
3617 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003618 io_put_req(req);
Jens Axboe03b12302019-12-02 18:50:25 -07003619 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003620}
3621
Pavel Begunkov014db002020-03-03 21:33:12 +03003622static int io_send(struct io_kiocb *req, bool force_nonblock)
Jens Axboefddafac2020-01-04 20:19:44 -07003623{
Jens Axboefddafac2020-01-04 20:19:44 -07003624 struct socket *sock;
3625 int ret;
3626
3627 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3628 return -EINVAL;
3629
3630 sock = sock_from_file(req->file, &ret);
3631 if (sock) {
3632 struct io_sr_msg *sr = &req->sr_msg;
3633 struct msghdr msg;
3634 struct iovec iov;
3635 unsigned flags;
3636
3637 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3638 &msg.msg_iter);
3639 if (ret)
3640 return ret;
3641
3642 msg.msg_name = NULL;
3643 msg.msg_control = NULL;
3644 msg.msg_controllen = 0;
3645 msg.msg_namelen = 0;
3646
3647 flags = req->sr_msg.msg_flags;
3648 if (flags & MSG_DONTWAIT)
3649 req->flags |= REQ_F_NOWAIT;
3650 else if (force_nonblock)
3651 flags |= MSG_DONTWAIT;
3652
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003653 msg.msg_flags = flags;
3654 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003655 if (force_nonblock && ret == -EAGAIN)
3656 return -EAGAIN;
3657 if (ret == -ERESTARTSYS)
3658 ret = -EINTR;
3659 }
3660
3661 io_cqring_add_event(req, ret);
3662 if (ret < 0)
3663 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003664 io_put_req(req);
Jens Axboefddafac2020-01-04 20:19:44 -07003665 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003666}
3667
Jens Axboe52de1fe2020-02-27 10:15:42 -07003668static int __io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
3669{
3670 struct io_sr_msg *sr = &req->sr_msg;
3671 struct iovec __user *uiov;
3672 size_t iov_len;
3673 int ret;
3674
3675 ret = __copy_msghdr_from_user(&io->msg.msg, sr->msg, &io->msg.uaddr,
3676 &uiov, &iov_len);
3677 if (ret)
3678 return ret;
3679
3680 if (req->flags & REQ_F_BUFFER_SELECT) {
3681 if (iov_len > 1)
3682 return -EINVAL;
3683 if (copy_from_user(io->msg.iov, uiov, sizeof(*uiov)))
3684 return -EFAULT;
3685 sr->len = io->msg.iov[0].iov_len;
3686 iov_iter_init(&io->msg.msg.msg_iter, READ, io->msg.iov, 1,
3687 sr->len);
3688 io->msg.iov = NULL;
3689 } else {
3690 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
3691 &io->msg.iov, &io->msg.msg.msg_iter);
3692 if (ret > 0)
3693 ret = 0;
3694 }
3695
3696 return ret;
3697}
3698
3699#ifdef CONFIG_COMPAT
3700static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
3701 struct io_async_ctx *io)
3702{
3703 struct compat_msghdr __user *msg_compat;
3704 struct io_sr_msg *sr = &req->sr_msg;
3705 struct compat_iovec __user *uiov;
3706 compat_uptr_t ptr;
3707 compat_size_t len;
3708 int ret;
3709
3710 msg_compat = (struct compat_msghdr __user *) sr->msg;
3711 ret = __get_compat_msghdr(&io->msg.msg, msg_compat, &io->msg.uaddr,
3712 &ptr, &len);
3713 if (ret)
3714 return ret;
3715
3716 uiov = compat_ptr(ptr);
3717 if (req->flags & REQ_F_BUFFER_SELECT) {
3718 compat_ssize_t clen;
3719
3720 if (len > 1)
3721 return -EINVAL;
3722 if (!access_ok(uiov, sizeof(*uiov)))
3723 return -EFAULT;
3724 if (__get_user(clen, &uiov->iov_len))
3725 return -EFAULT;
3726 if (clen < 0)
3727 return -EINVAL;
3728 sr->len = io->msg.iov[0].iov_len;
3729 io->msg.iov = NULL;
3730 } else {
3731 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
3732 &io->msg.iov,
3733 &io->msg.msg.msg_iter);
3734 if (ret < 0)
3735 return ret;
3736 }
3737
3738 return 0;
3739}
Jens Axboe03b12302019-12-02 18:50:25 -07003740#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07003741
3742static int io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
3743{
3744 io->msg.iov = io->msg.fast_iov;
3745
3746#ifdef CONFIG_COMPAT
3747 if (req->ctx->compat)
3748 return __io_compat_recvmsg_copy_hdr(req, io);
3749#endif
3750
3751 return __io_recvmsg_copy_hdr(req, io);
3752}
3753
Jens Axboebcda7ba2020-02-23 16:42:51 -07003754static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
3755 int *cflags, bool needs_lock)
3756{
3757 struct io_sr_msg *sr = &req->sr_msg;
3758 struct io_buffer *kbuf;
3759
3760 if (!(req->flags & REQ_F_BUFFER_SELECT))
3761 return NULL;
3762
3763 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
3764 if (IS_ERR(kbuf))
3765 return kbuf;
3766
3767 sr->kbuf = kbuf;
3768 req->flags |= REQ_F_BUFFER_SELECTED;
3769
3770 *cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
3771 *cflags |= IORING_CQE_F_BUFFER;
3772 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07003773}
3774
Jens Axboe3529d8c2019-12-19 18:24:38 -07003775static int io_recvmsg_prep(struct io_kiocb *req,
3776 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003777{
Jens Axboee47293f2019-12-20 08:58:21 -07003778 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003779 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003780 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07003781
Jens Axboe3529d8c2019-12-19 18:24:38 -07003782 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3783 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003784 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003785 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003786
Jens Axboed8768362020-02-27 14:17:49 -07003787#ifdef CONFIG_COMPAT
3788 if (req->ctx->compat)
3789 sr->msg_flags |= MSG_CMSG_COMPAT;
3790#endif
3791
Jens Axboefddafac2020-01-04 20:19:44 -07003792 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003793 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003794 /* iovec is already imported */
3795 if (req->flags & REQ_F_NEED_CLEANUP)
3796 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003797
Jens Axboe52de1fe2020-02-27 10:15:42 -07003798 ret = io_recvmsg_copy_hdr(req, io);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003799 if (!ret)
3800 req->flags |= REQ_F_NEED_CLEANUP;
3801 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003802}
3803
Pavel Begunkov014db002020-03-03 21:33:12 +03003804static int io_recvmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003805{
Jens Axboe0b416c32019-12-15 10:57:46 -07003806 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003807 struct socket *sock;
Jens Axboe52de1fe2020-02-27 10:15:42 -07003808 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003809
3810 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3811 return -EINVAL;
3812
3813 sock = sock_from_file(req->file, &ret);
3814 if (sock) {
Jens Axboe52de1fe2020-02-27 10:15:42 -07003815 struct io_buffer *kbuf;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003816 struct io_async_ctx io;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003817 unsigned flags;
3818
Jens Axboe03b12302019-12-02 18:50:25 -07003819 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003820 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003821 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003822 /* if iov is set, it's allocated already */
3823 if (!kmsg->iov)
3824 kmsg->iov = kmsg->fast_iov;
3825 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003826 } else {
Jens Axboe0b416c32019-12-15 10:57:46 -07003827 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003828 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003829
Jens Axboe52de1fe2020-02-27 10:15:42 -07003830 ret = io_recvmsg_copy_hdr(req, &io);
Jens Axboe03b12302019-12-02 18:50:25 -07003831 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003832 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003833 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003834
Jens Axboe52de1fe2020-02-27 10:15:42 -07003835 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
3836 if (IS_ERR(kbuf)) {
3837 return PTR_ERR(kbuf);
3838 } else if (kbuf) {
3839 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3840 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
3841 1, req->sr_msg.len);
3842 }
3843
Jens Axboee47293f2019-12-20 08:58:21 -07003844 flags = req->sr_msg.msg_flags;
3845 if (flags & MSG_DONTWAIT)
3846 req->flags |= REQ_F_NOWAIT;
3847 else if (force_nonblock)
3848 flags |= MSG_DONTWAIT;
3849
3850 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3851 kmsg->uaddr, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003852 if (force_nonblock && ret == -EAGAIN)
3853 return io_setup_async_msg(req, kmsg);
Jens Axboe441cdbd2019-12-02 18:49:10 -07003854 if (ret == -ERESTARTSYS)
3855 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003856 }
3857
Pavel Begunkov1e950812020-02-06 19:51:16 +03003858 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003859 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003860 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe52de1fe2020-02-27 10:15:42 -07003861 __io_cqring_add_event(req, ret, cflags);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003862 if (ret < 0)
3863 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003864 io_put_req(req);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003865 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003866}
3867
Pavel Begunkov014db002020-03-03 21:33:12 +03003868static int io_recv(struct io_kiocb *req, bool force_nonblock)
Jens Axboefddafac2020-01-04 20:19:44 -07003869{
Jens Axboebcda7ba2020-02-23 16:42:51 -07003870 struct io_buffer *kbuf = NULL;
Jens Axboefddafac2020-01-04 20:19:44 -07003871 struct socket *sock;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003872 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003873
3874 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3875 return -EINVAL;
3876
3877 sock = sock_from_file(req->file, &ret);
3878 if (sock) {
3879 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003880 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07003881 struct msghdr msg;
3882 struct iovec iov;
3883 unsigned flags;
3884
Jens Axboebcda7ba2020-02-23 16:42:51 -07003885 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
3886 if (IS_ERR(kbuf))
3887 return PTR_ERR(kbuf);
3888 else if (kbuf)
3889 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07003890
Jens Axboebcda7ba2020-02-23 16:42:51 -07003891 ret = import_single_range(READ, buf, sr->len, &iov,
3892 &msg.msg_iter);
3893 if (ret) {
3894 kfree(kbuf);
3895 return ret;
3896 }
3897
3898 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07003899 msg.msg_name = NULL;
3900 msg.msg_control = NULL;
3901 msg.msg_controllen = 0;
3902 msg.msg_namelen = 0;
3903 msg.msg_iocb = NULL;
3904 msg.msg_flags = 0;
3905
3906 flags = req->sr_msg.msg_flags;
3907 if (flags & MSG_DONTWAIT)
3908 req->flags |= REQ_F_NOWAIT;
3909 else if (force_nonblock)
3910 flags |= MSG_DONTWAIT;
3911
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003912 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07003913 if (force_nonblock && ret == -EAGAIN)
3914 return -EAGAIN;
3915 if (ret == -ERESTARTSYS)
3916 ret = -EINTR;
3917 }
3918
Jens Axboebcda7ba2020-02-23 16:42:51 -07003919 kfree(kbuf);
3920 req->flags &= ~REQ_F_NEED_CLEANUP;
3921 __io_cqring_add_event(req, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07003922 if (ret < 0)
3923 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003924 io_put_req(req);
Jens Axboefddafac2020-01-04 20:19:44 -07003925 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003926}
3927
Jens Axboe3529d8c2019-12-19 18:24:38 -07003928static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003929{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003930 struct io_accept *accept = &req->accept;
3931
Jens Axboe17f2fe32019-10-17 14:42:58 -06003932 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3933 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003934 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003935 return -EINVAL;
3936
Jens Axboed55e5f52019-12-11 16:12:15 -07003937 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3938 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003939 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06003940 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003941 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003942}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003943
Pavel Begunkov014db002020-03-03 21:33:12 +03003944static int __io_accept(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003945{
3946 struct io_accept *accept = &req->accept;
3947 unsigned file_flags;
3948 int ret;
3949
3950 file_flags = force_nonblock ? O_NONBLOCK : 0;
3951 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06003952 accept->addr_len, accept->flags,
3953 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003954 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003955 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003956 if (ret == -ERESTARTSYS)
3957 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003958 if (ret < 0)
3959 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003960 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003961 io_put_req(req);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003962 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003963}
3964
3965static void io_accept_finish(struct io_wq_work **workptr)
3966{
3967 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003968
3969 if (io_req_cancelled(req))
3970 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03003971 __io_accept(req, false);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03003972 io_steal_work(req, workptr);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003973}
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003974
Pavel Begunkov014db002020-03-03 21:33:12 +03003975static int io_accept(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003976{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003977 int ret;
3978
Pavel Begunkov014db002020-03-03 21:33:12 +03003979 ret = __io_accept(req, force_nonblock);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003980 if (ret == -EAGAIN && force_nonblock) {
3981 req->work.func = io_accept_finish;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003982 return -EAGAIN;
3983 }
3984 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003985}
3986
Jens Axboe3529d8c2019-12-19 18:24:38 -07003987static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003988{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003989 struct io_connect *conn = &req->connect;
3990 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003991
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003992 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3993 return -EINVAL;
3994 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3995 return -EINVAL;
3996
Jens Axboe3529d8c2019-12-19 18:24:38 -07003997 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3998 conn->addr_len = READ_ONCE(sqe->addr2);
3999
4000 if (!io)
4001 return 0;
4002
4003 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004004 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004005}
4006
Pavel Begunkov014db002020-03-03 21:33:12 +03004007static int io_connect(struct io_kiocb *req, bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004008{
Jens Axboef499a022019-12-02 16:28:46 -07004009 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004010 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004011 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004012
Jens Axboef499a022019-12-02 16:28:46 -07004013 if (req->io) {
4014 io = req->io;
4015 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004016 ret = move_addr_to_kernel(req->connect.addr,
4017 req->connect.addr_len,
4018 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004019 if (ret)
4020 goto out;
4021 io = &__io;
4022 }
4023
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004024 file_flags = force_nonblock ? O_NONBLOCK : 0;
4025
4026 ret = __sys_connect_file(req->file, &io->connect.address,
4027 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004028 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004029 if (req->io)
4030 return -EAGAIN;
4031 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004032 ret = -ENOMEM;
4033 goto out;
4034 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004035 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004036 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004037 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004038 if (ret == -ERESTARTSYS)
4039 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004040out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004041 if (ret < 0)
4042 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004043 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03004044 io_put_req(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004045 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004046}
YueHaibing469956e2020-03-04 15:53:52 +08004047#else /* !CONFIG_NET */
4048static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4049{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004050 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004051}
4052
YueHaibing469956e2020-03-04 15:53:52 +08004053static int io_sendmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004054{
YueHaibing469956e2020-03-04 15:53:52 +08004055 return -EOPNOTSUPP;
4056}
4057
4058static int io_send(struct io_kiocb *req, bool force_nonblock)
4059{
4060 return -EOPNOTSUPP;
4061}
4062
4063static int io_recvmsg_prep(struct io_kiocb *req,
4064 const struct io_uring_sqe *sqe)
4065{
4066 return -EOPNOTSUPP;
4067}
4068
4069static int io_recvmsg(struct io_kiocb *req, bool force_nonblock)
4070{
4071 return -EOPNOTSUPP;
4072}
4073
4074static int io_recv(struct io_kiocb *req, bool force_nonblock)
4075{
4076 return -EOPNOTSUPP;
4077}
4078
4079static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4080{
4081 return -EOPNOTSUPP;
4082}
4083
4084static int io_accept(struct io_kiocb *req, bool force_nonblock)
4085{
4086 return -EOPNOTSUPP;
4087}
4088
4089static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4090{
4091 return -EOPNOTSUPP;
4092}
4093
4094static int io_connect(struct io_kiocb *req, bool force_nonblock)
4095{
4096 return -EOPNOTSUPP;
4097}
4098#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004099
Jens Axboed7718a92020-02-14 22:23:12 -07004100struct io_poll_table {
4101 struct poll_table_struct pt;
4102 struct io_kiocb *req;
4103 int error;
4104};
4105
4106static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
4107 struct wait_queue_head *head)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004108{
Jens Axboed7718a92020-02-14 22:23:12 -07004109 if (unlikely(poll->head)) {
4110 pt->error = -EINVAL;
4111 return;
4112 }
4113
4114 pt->error = 0;
4115 poll->head = head;
4116 add_wait_queue(head, &poll->wait);
4117}
4118
4119static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4120 struct poll_table_struct *p)
4121{
4122 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4123
4124 __io_queue_proc(&pt->req->apoll->poll, pt, head);
4125}
4126
4127static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4128 __poll_t mask, task_work_func_t func)
4129{
4130 struct task_struct *tsk;
Jens Axboeaa96bf82020-04-03 11:26:26 -06004131 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004132
4133 /* for instances that support it check for an event match first: */
4134 if (mask && !(mask & poll->events))
4135 return 0;
4136
4137 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4138
4139 list_del_init(&poll->wait.entry);
4140
4141 tsk = req->task;
4142 req->result = mask;
4143 init_task_work(&req->task_work, func);
4144 /*
Jens Axboeaa96bf82020-04-03 11:26:26 -06004145 * If this fails, then the task is exiting. Punt to one of the io-wq
4146 * threads to ensure the work gets run, we can't always rely on exit
4147 * cancelation taking care of this.
Jens Axboed7718a92020-02-14 22:23:12 -07004148 */
Jens Axboeaa96bf82020-04-03 11:26:26 -06004149 ret = task_work_add(tsk, &req->task_work, true);
4150 if (unlikely(ret)) {
4151 tsk = io_wq_get_task(req->ctx->io_wq);
4152 task_work_add(tsk, &req->task_work, true);
4153 }
Jens Axboed7718a92020-02-14 22:23:12 -07004154 wake_up_process(tsk);
4155 return 1;
4156}
4157
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004158static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4159 __acquires(&req->ctx->completion_lock)
4160{
4161 struct io_ring_ctx *ctx = req->ctx;
4162
4163 if (!req->result && !READ_ONCE(poll->canceled)) {
4164 struct poll_table_struct pt = { ._key = poll->events };
4165
4166 req->result = vfs_poll(req->file, &pt) & poll->events;
4167 }
4168
4169 spin_lock_irq(&ctx->completion_lock);
4170 if (!req->result && !READ_ONCE(poll->canceled)) {
4171 add_wait_queue(poll->head, &poll->wait);
4172 return true;
4173 }
4174
4175 return false;
4176}
4177
Jens Axboed7718a92020-02-14 22:23:12 -07004178static void io_async_task_func(struct callback_head *cb)
4179{
4180 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4181 struct async_poll *apoll = req->apoll;
4182 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2bae0472020-04-13 11:16:34 -06004183 bool canceled;
Jens Axboed7718a92020-02-14 22:23:12 -07004184
4185 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4186
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004187 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07004188 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004189 return;
Jens Axboed7718a92020-02-14 22:23:12 -07004190 }
4191
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004192 if (hash_hashed(&req->hash_node))
4193 hash_del(&req->hash_node);
4194
Jens Axboe2bae0472020-04-13 11:16:34 -06004195 canceled = READ_ONCE(apoll->poll.canceled);
4196 if (canceled) {
4197 io_cqring_fill_event(req, -ECANCELED);
4198 io_commit_cqring(ctx);
4199 }
4200
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004201 spin_unlock_irq(&ctx->completion_lock);
4202
Jens Axboe2bae0472020-04-13 11:16:34 -06004203 if (canceled) {
4204 kfree(apoll);
4205 io_cqring_ev_posted(ctx);
4206 req_set_fail_links(req);
4207 io_put_req(req);
4208 return;
4209 }
4210
Jens Axboed7718a92020-02-14 22:23:12 -07004211 /* restore ->work in case we need to retry again */
4212 memcpy(&req->work, &apoll->work, sizeof(req->work));
4213
4214 __set_current_state(TASK_RUNNING);
4215 mutex_lock(&ctx->uring_lock);
4216 __io_queue_sqe(req, NULL);
4217 mutex_unlock(&ctx->uring_lock);
4218
4219 kfree(apoll);
4220}
4221
4222static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4223 void *key)
4224{
4225 struct io_kiocb *req = wait->private;
4226 struct io_poll_iocb *poll = &req->apoll->poll;
4227
4228 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4229 key_to_poll(key));
4230
4231 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4232}
4233
4234static void io_poll_req_insert(struct io_kiocb *req)
4235{
4236 struct io_ring_ctx *ctx = req->ctx;
4237 struct hlist_head *list;
4238
4239 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
4240 hlist_add_head(&req->hash_node, list);
4241}
4242
4243static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
4244 struct io_poll_iocb *poll,
4245 struct io_poll_table *ipt, __poll_t mask,
4246 wait_queue_func_t wake_func)
4247 __acquires(&ctx->completion_lock)
4248{
4249 struct io_ring_ctx *ctx = req->ctx;
4250 bool cancel = false;
4251
4252 poll->file = req->file;
4253 poll->head = NULL;
4254 poll->done = poll->canceled = false;
4255 poll->events = mask;
4256
4257 ipt->pt._key = mask;
4258 ipt->req = req;
4259 ipt->error = -EINVAL;
4260
4261 INIT_LIST_HEAD(&poll->wait.entry);
4262 init_waitqueue_func_entry(&poll->wait, wake_func);
4263 poll->wait.private = req;
4264
4265 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
4266
4267 spin_lock_irq(&ctx->completion_lock);
4268 if (likely(poll->head)) {
4269 spin_lock(&poll->head->lock);
4270 if (unlikely(list_empty(&poll->wait.entry))) {
4271 if (ipt->error)
4272 cancel = true;
4273 ipt->error = 0;
4274 mask = 0;
4275 }
4276 if (mask || ipt->error)
4277 list_del_init(&poll->wait.entry);
4278 else if (cancel)
4279 WRITE_ONCE(poll->canceled, true);
4280 else if (!poll->done) /* actually waiting for an event */
4281 io_poll_req_insert(req);
4282 spin_unlock(&poll->head->lock);
4283 }
4284
4285 return mask;
4286}
4287
4288static bool io_arm_poll_handler(struct io_kiocb *req)
4289{
4290 const struct io_op_def *def = &io_op_defs[req->opcode];
4291 struct io_ring_ctx *ctx = req->ctx;
4292 struct async_poll *apoll;
4293 struct io_poll_table ipt;
4294 __poll_t mask, ret;
4295
4296 if (!req->file || !file_can_poll(req->file))
4297 return false;
4298 if (req->flags & (REQ_F_MUST_PUNT | REQ_F_POLLED))
4299 return false;
4300 if (!def->pollin && !def->pollout)
4301 return false;
4302
4303 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
4304 if (unlikely(!apoll))
4305 return false;
4306
4307 req->flags |= REQ_F_POLLED;
4308 memcpy(&apoll->work, &req->work, sizeof(req->work));
4309
Jens Axboe3537b6a2020-04-03 11:19:06 -06004310 get_task_struct(current);
Jens Axboed7718a92020-02-14 22:23:12 -07004311 req->task = current;
4312 req->apoll = apoll;
4313 INIT_HLIST_NODE(&req->hash_node);
4314
Nathan Chancellor8755d972020-03-02 16:01:19 -07004315 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07004316 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07004317 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07004318 if (def->pollout)
4319 mask |= POLLOUT | POLLWRNORM;
4320 mask |= POLLERR | POLLPRI;
4321
4322 ipt.pt._qproc = io_async_queue_proc;
4323
4324 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
4325 io_async_wake);
4326 if (ret) {
4327 ipt.error = 0;
4328 apoll->poll.done = true;
4329 spin_unlock_irq(&ctx->completion_lock);
4330 memcpy(&req->work, &apoll->work, sizeof(req->work));
4331 kfree(apoll);
4332 return false;
4333 }
4334 spin_unlock_irq(&ctx->completion_lock);
4335 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
4336 apoll->poll.events);
4337 return true;
4338}
4339
4340static bool __io_poll_remove_one(struct io_kiocb *req,
4341 struct io_poll_iocb *poll)
4342{
Jens Axboeb41e9852020-02-17 09:52:41 -07004343 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004344
4345 spin_lock(&poll->head->lock);
4346 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07004347 if (!list_empty(&poll->wait.entry)) {
4348 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07004349 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004350 }
4351 spin_unlock(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07004352 return do_complete;
4353}
4354
4355static bool io_poll_remove_one(struct io_kiocb *req)
4356{
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08004357 struct async_poll *apoll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07004358 bool do_complete;
4359
4360 if (req->opcode == IORING_OP_POLL_ADD) {
4361 do_complete = __io_poll_remove_one(req, &req->poll);
4362 } else {
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08004363 apoll = req->apoll;
Jens Axboed7718a92020-02-14 22:23:12 -07004364 /* non-poll requests have submit ref still */
4365 do_complete = __io_poll_remove_one(req, &req->apoll->poll);
4366 if (do_complete)
4367 io_put_req(req);
4368 }
4369
Jens Axboe78076bb2019-12-04 19:56:40 -07004370 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07004371
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08004372 if (apoll) {
4373 /*
4374 * restore ->work because we need to call io_req_work_drop_env.
4375 */
4376 memcpy(&req->work, &apoll->work, sizeof(req->work));
4377 kfree(apoll);
4378 }
4379
Jens Axboeb41e9852020-02-17 09:52:41 -07004380 if (do_complete) {
4381 io_cqring_fill_event(req, -ECANCELED);
4382 io_commit_cqring(req->ctx);
4383 req->flags |= REQ_F_COMP_LOCKED;
4384 io_put_req(req);
4385 }
4386
4387 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004388}
4389
4390static void io_poll_remove_all(struct io_ring_ctx *ctx)
4391{
Jens Axboe78076bb2019-12-04 19:56:40 -07004392 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004393 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004394 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004395
4396 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07004397 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
4398 struct hlist_head *list;
4399
4400 list = &ctx->cancel_hash[i];
4401 hlist_for_each_entry_safe(req, tmp, list, hash_node)
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004402 posted += io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004403 }
4404 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07004405
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004406 if (posted)
4407 io_cqring_ev_posted(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004408}
4409
Jens Axboe47f46762019-11-09 17:43:02 -07004410static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
4411{
Jens Axboe78076bb2019-12-04 19:56:40 -07004412 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07004413 struct io_kiocb *req;
4414
Jens Axboe78076bb2019-12-04 19:56:40 -07004415 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
4416 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07004417 if (sqe_addr != req->user_data)
4418 continue;
4419 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07004420 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07004421 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07004422 }
4423
4424 return -ENOENT;
4425}
4426
Jens Axboe3529d8c2019-12-19 18:24:38 -07004427static int io_poll_remove_prep(struct io_kiocb *req,
4428 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004429{
Jens Axboe221c5eb2019-01-17 09:41:58 -07004430 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4431 return -EINVAL;
4432 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
4433 sqe->poll_events)
4434 return -EINVAL;
4435
Jens Axboe0969e782019-12-17 18:40:57 -07004436 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07004437 return 0;
4438}
4439
4440/*
4441 * Find a running poll command that matches one specified in sqe->addr,
4442 * and remove it if found.
4443 */
4444static int io_poll_remove(struct io_kiocb *req)
4445{
4446 struct io_ring_ctx *ctx = req->ctx;
4447 u64 addr;
4448 int ret;
4449
Jens Axboe0969e782019-12-17 18:40:57 -07004450 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004451 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07004452 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004453 spin_unlock_irq(&ctx->completion_lock);
4454
Jens Axboe78e19bb2019-11-06 15:21:34 -07004455 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004456 if (ret < 0)
4457 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004458 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004459 return 0;
4460}
4461
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004462static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004463{
Jackie Liua197f662019-11-08 08:09:12 -07004464 struct io_ring_ctx *ctx = req->ctx;
4465
Jens Axboe8c838782019-03-12 15:48:16 -06004466 req->poll.done = true;
Pavel Begunkovb0a20342020-02-28 10:36:35 +03004467 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06004468 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004469}
4470
Jens Axboeb41e9852020-02-17 09:52:41 -07004471static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004472{
Jens Axboe221c5eb2019-01-17 09:41:58 -07004473 struct io_ring_ctx *ctx = req->ctx;
Jens Axboea6ba6322020-04-03 11:10:14 -06004474 struct io_poll_iocb *poll = &req->poll;
4475
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004476 if (io_poll_rewait(req, poll)) {
Jens Axboea6ba6322020-04-03 11:10:14 -06004477 spin_unlock_irq(&ctx->completion_lock);
4478 return;
4479 }
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004480
Jens Axboe78076bb2019-12-04 19:56:40 -07004481 hash_del(&req->hash_node);
Jens Axboeb41e9852020-02-17 09:52:41 -07004482 io_poll_complete(req, req->result, 0);
4483 req->flags |= REQ_F_COMP_LOCKED;
4484 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004485 spin_unlock_irq(&ctx->completion_lock);
4486
Jens Axboe8c838782019-03-12 15:48:16 -06004487 io_cqring_ev_posted(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004488}
4489
Jens Axboeb41e9852020-02-17 09:52:41 -07004490static void io_poll_task_func(struct callback_head *cb)
Jens Axboee94f1412019-12-19 12:06:02 -07004491{
Jens Axboeb41e9852020-02-17 09:52:41 -07004492 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4493 struct io_kiocb *nxt = NULL;
Jens Axboee94f1412019-12-19 12:06:02 -07004494
Jens Axboeb41e9852020-02-17 09:52:41 -07004495 io_poll_task_handler(req, &nxt);
Jens Axboed7718a92020-02-14 22:23:12 -07004496 if (nxt) {
4497 struct io_ring_ctx *ctx = nxt->ctx;
Jens Axboee94f1412019-12-19 12:06:02 -07004498
Jens Axboed7718a92020-02-14 22:23:12 -07004499 mutex_lock(&ctx->uring_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07004500 __io_queue_sqe(nxt, NULL);
Jens Axboed7718a92020-02-14 22:23:12 -07004501 mutex_unlock(&ctx->uring_lock);
Jens Axboee94f1412019-12-19 12:06:02 -07004502 }
Jens Axboef0b493e2020-02-01 21:30:11 -07004503}
4504
Jens Axboe221c5eb2019-01-17 09:41:58 -07004505static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4506 void *key)
4507{
Jens Axboec2f2eb72020-02-10 09:07:05 -07004508 struct io_kiocb *req = wait->private;
4509 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004510
Jens Axboed7718a92020-02-14 22:23:12 -07004511 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004512}
4513
Jens Axboe221c5eb2019-01-17 09:41:58 -07004514static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
4515 struct poll_table_struct *p)
4516{
4517 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4518
Jens Axboed7718a92020-02-14 22:23:12 -07004519 __io_queue_proc(&pt->req->poll, pt, head);
Jens Axboeeac406c2019-11-14 12:09:58 -07004520}
4521
Jens Axboe3529d8c2019-12-19 18:24:38 -07004522static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004523{
4524 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004525 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004526
4527 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4528 return -EINVAL;
4529 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
4530 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06004531 if (!poll->file)
4532 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004533
Jens Axboe221c5eb2019-01-17 09:41:58 -07004534 events = READ_ONCE(sqe->poll_events);
4535 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboeb41e9852020-02-17 09:52:41 -07004536
Jens Axboe3537b6a2020-04-03 11:19:06 -06004537 get_task_struct(current);
Jens Axboeb41e9852020-02-17 09:52:41 -07004538 req->task = current;
Jens Axboe0969e782019-12-17 18:40:57 -07004539 return 0;
4540}
4541
Pavel Begunkov014db002020-03-03 21:33:12 +03004542static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07004543{
4544 struct io_poll_iocb *poll = &req->poll;
4545 struct io_ring_ctx *ctx = req->ctx;
4546 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07004547 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07004548
Jens Axboe78076bb2019-12-04 19:56:40 -07004549 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe36703242019-07-25 10:20:18 -06004550 INIT_LIST_HEAD(&req->list);
Jens Axboed7718a92020-02-14 22:23:12 -07004551 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06004552
Jens Axboed7718a92020-02-14 22:23:12 -07004553 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
4554 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004555
Jens Axboe8c838782019-03-12 15:48:16 -06004556 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06004557 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004558 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06004559 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07004560 spin_unlock_irq(&ctx->completion_lock);
4561
Jens Axboe8c838782019-03-12 15:48:16 -06004562 if (mask) {
4563 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03004564 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004565 }
Jens Axboe8c838782019-03-12 15:48:16 -06004566 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004567}
4568
Jens Axboe5262f562019-09-17 12:26:57 -06004569static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
4570{
Jens Axboead8a48a2019-11-15 08:49:11 -07004571 struct io_timeout_data *data = container_of(timer,
4572 struct io_timeout_data, timer);
4573 struct io_kiocb *req = data->req;
4574 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06004575 unsigned long flags;
4576
Jens Axboe5262f562019-09-17 12:26:57 -06004577 atomic_inc(&ctx->cq_timeouts);
4578
4579 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08004580 /*
Jens Axboe11365042019-10-16 09:08:32 -06004581 * We could be racing with timeout deletion. If the list is empty,
4582 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08004583 */
Jens Axboe842f9612019-10-29 12:34:10 -06004584 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06004585 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06004586
Jens Axboe11365042019-10-16 09:08:32 -06004587 /*
4588 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08004589 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06004590 * pointer will be increased, otherwise other timeout reqs may
4591 * return in advance without waiting for enough wait_nr.
4592 */
4593 prev = req;
4594 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
4595 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06004596 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06004597 }
Jens Axboe842f9612019-10-29 12:34:10 -06004598
Jens Axboe78e19bb2019-11-06 15:21:34 -07004599 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06004600 io_commit_cqring(ctx);
4601 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4602
4603 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004604 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004605 io_put_req(req);
4606 return HRTIMER_NORESTART;
4607}
4608
Jens Axboe47f46762019-11-09 17:43:02 -07004609static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
4610{
4611 struct io_kiocb *req;
4612 int ret = -ENOENT;
4613
4614 list_for_each_entry(req, &ctx->timeout_list, list) {
4615 if (user_data == req->user_data) {
4616 list_del_init(&req->list);
4617 ret = 0;
4618 break;
4619 }
4620 }
4621
4622 if (ret == -ENOENT)
4623 return ret;
4624
Jens Axboe2d283902019-12-04 11:08:05 -07004625 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07004626 if (ret == -1)
4627 return -EALREADY;
4628
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004629 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004630 io_cqring_fill_event(req, -ECANCELED);
4631 io_put_req(req);
4632 return 0;
4633}
4634
Jens Axboe3529d8c2019-12-19 18:24:38 -07004635static int io_timeout_remove_prep(struct io_kiocb *req,
4636 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07004637{
Jens Axboeb29472e2019-12-17 18:50:29 -07004638 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4639 return -EINVAL;
4640 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
4641 return -EINVAL;
4642
4643 req->timeout.addr = READ_ONCE(sqe->addr);
4644 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
4645 if (req->timeout.flags)
4646 return -EINVAL;
4647
Jens Axboeb29472e2019-12-17 18:50:29 -07004648 return 0;
4649}
4650
Jens Axboe11365042019-10-16 09:08:32 -06004651/*
4652 * Remove or update an existing timeout command
4653 */
Jens Axboefc4df992019-12-10 14:38:45 -07004654static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06004655{
4656 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07004657 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06004658
Jens Axboe11365042019-10-16 09:08:32 -06004659 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07004660 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06004661
Jens Axboe47f46762019-11-09 17:43:02 -07004662 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06004663 io_commit_cqring(ctx);
4664 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004665 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004666 if (ret < 0)
4667 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08004668 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06004669 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06004670}
4671
Jens Axboe3529d8c2019-12-19 18:24:38 -07004672static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07004673 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06004674{
Jens Axboead8a48a2019-11-15 08:49:11 -07004675 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06004676 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06004677
Jens Axboead8a48a2019-11-15 08:49:11 -07004678 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06004679 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07004680 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06004681 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07004682 if (sqe->off && is_timeout_link)
4683 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06004684 flags = READ_ONCE(sqe->timeout_flags);
4685 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06004686 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06004687
Jens Axboe26a61672019-12-20 09:02:01 -07004688 req->timeout.count = READ_ONCE(sqe->off);
4689
Jens Axboe3529d8c2019-12-19 18:24:38 -07004690 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07004691 return -ENOMEM;
4692
4693 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07004694 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07004695 req->flags |= REQ_F_TIMEOUT;
4696
4697 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06004698 return -EFAULT;
4699
Jens Axboe11365042019-10-16 09:08:32 -06004700 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07004701 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06004702 else
Jens Axboead8a48a2019-11-15 08:49:11 -07004703 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06004704
Jens Axboead8a48a2019-11-15 08:49:11 -07004705 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
4706 return 0;
4707}
4708
Jens Axboefc4df992019-12-10 14:38:45 -07004709static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07004710{
Jens Axboead8a48a2019-11-15 08:49:11 -07004711 struct io_ring_ctx *ctx = req->ctx;
4712 struct io_timeout_data *data;
4713 struct list_head *entry;
4714 unsigned span = 0;
Pavel Begunkovb55ce732020-04-15 00:39:49 +03004715 u32 count = req->timeout.count;
Pavel Begunkov22cad152020-04-15 00:39:48 +03004716 u32 seq = req->sequence;
Jens Axboead8a48a2019-11-15 08:49:11 -07004717
Jens Axboe2d283902019-12-04 11:08:05 -07004718 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004719
Jens Axboe5262f562019-09-17 12:26:57 -06004720 /*
4721 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07004722 * timeout event to be satisfied. If it isn't set, then this is
4723 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06004724 */
Jens Axboe93bd25b2019-11-11 23:34:31 -07004725 if (!count) {
4726 req->flags |= REQ_F_TIMEOUT_NOSEQ;
4727 spin_lock_irq(&ctx->completion_lock);
4728 entry = ctx->timeout_list.prev;
4729 goto add;
4730 }
Jens Axboe5262f562019-09-17 12:26:57 -06004731
Pavel Begunkov22cad152020-04-15 00:39:48 +03004732 req->sequence = seq + count;
Jens Axboe5262f562019-09-17 12:26:57 -06004733
4734 /*
4735 * Insertion sort, ensuring the first entry in the list is always
4736 * the one we need first.
4737 */
Jens Axboe5262f562019-09-17 12:26:57 -06004738 spin_lock_irq(&ctx->completion_lock);
4739 list_for_each_prev(entry, &ctx->timeout_list) {
4740 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
Pavel Begunkov22cad152020-04-15 00:39:48 +03004741 unsigned nxt_seq;
yangerkun5da0fb12019-10-15 21:59:29 +08004742 long long tmp, tmp_nxt;
Pavel Begunkovb55ce732020-04-15 00:39:49 +03004743 u32 nxt_offset = nxt->timeout.count;
Jens Axboe5262f562019-09-17 12:26:57 -06004744
Jens Axboe93bd25b2019-11-11 23:34:31 -07004745 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
4746 continue;
4747
yangerkun5da0fb12019-10-15 21:59:29 +08004748 /*
Pavel Begunkov22cad152020-04-15 00:39:48 +03004749 * Since seq + count can overflow, use type long
yangerkun5da0fb12019-10-15 21:59:29 +08004750 * long to store it.
4751 */
Pavel Begunkov22cad152020-04-15 00:39:48 +03004752 tmp = (long long)seq + count;
4753 nxt_seq = nxt->sequence - nxt_offset;
4754 tmp_nxt = (long long)nxt_seq + nxt_offset;
yangerkun5da0fb12019-10-15 21:59:29 +08004755
4756 /*
4757 * cached_sq_head may overflow, and it will never overflow twice
4758 * once there is some timeout req still be valid.
4759 */
Pavel Begunkov22cad152020-04-15 00:39:48 +03004760 if (seq < nxt_seq)
yangerkun8b07a652019-10-17 12:12:35 +08004761 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08004762
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004763 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06004764 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004765
4766 /*
4767 * Sequence of reqs after the insert one and itself should
4768 * be adjusted because each timeout req consumes a slot.
4769 */
4770 span++;
4771 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06004772 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004773 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004774add:
Jens Axboe5262f562019-09-17 12:26:57 -06004775 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07004776 data->timer.function = io_timeout_fn;
4777 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06004778 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004779 return 0;
4780}
4781
Jens Axboe62755e32019-10-28 21:49:21 -06004782static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06004783{
Jens Axboe62755e32019-10-28 21:49:21 -06004784 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06004785
Jens Axboe62755e32019-10-28 21:49:21 -06004786 return req->user_data == (unsigned long) data;
4787}
4788
Jens Axboee977d6d2019-11-05 12:39:45 -07004789static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06004790{
Jens Axboe62755e32019-10-28 21:49:21 -06004791 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06004792 int ret = 0;
4793
Jens Axboe62755e32019-10-28 21:49:21 -06004794 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
4795 switch (cancel_ret) {
4796 case IO_WQ_CANCEL_OK:
4797 ret = 0;
4798 break;
4799 case IO_WQ_CANCEL_RUNNING:
4800 ret = -EALREADY;
4801 break;
4802 case IO_WQ_CANCEL_NOTFOUND:
4803 ret = -ENOENT;
4804 break;
4805 }
4806
Jens Axboee977d6d2019-11-05 12:39:45 -07004807 return ret;
4808}
4809
Jens Axboe47f46762019-11-09 17:43:02 -07004810static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
4811 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03004812 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07004813{
4814 unsigned long flags;
4815 int ret;
4816
4817 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
4818 if (ret != -ENOENT) {
4819 spin_lock_irqsave(&ctx->completion_lock, flags);
4820 goto done;
4821 }
4822
4823 spin_lock_irqsave(&ctx->completion_lock, flags);
4824 ret = io_timeout_cancel(ctx, sqe_addr);
4825 if (ret != -ENOENT)
4826 goto done;
4827 ret = io_poll_cancel(ctx, sqe_addr);
4828done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004829 if (!ret)
4830 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07004831 io_cqring_fill_event(req, ret);
4832 io_commit_cqring(ctx);
4833 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4834 io_cqring_ev_posted(ctx);
4835
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004836 if (ret < 0)
4837 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03004838 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004839}
4840
Jens Axboe3529d8c2019-12-19 18:24:38 -07004841static int io_async_cancel_prep(struct io_kiocb *req,
4842 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07004843{
Jens Axboefbf23842019-12-17 18:45:56 -07004844 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07004845 return -EINVAL;
4846 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
4847 sqe->cancel_flags)
4848 return -EINVAL;
4849
Jens Axboefbf23842019-12-17 18:45:56 -07004850 req->cancel.addr = READ_ONCE(sqe->addr);
4851 return 0;
4852}
4853
Pavel Begunkov014db002020-03-03 21:33:12 +03004854static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07004855{
4856 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07004857
Pavel Begunkov014db002020-03-03 21:33:12 +03004858 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06004859 return 0;
4860}
4861
Jens Axboe05f3fb32019-12-09 11:22:50 -07004862static int io_files_update_prep(struct io_kiocb *req,
4863 const struct io_uring_sqe *sqe)
4864{
4865 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
4866 return -EINVAL;
4867
4868 req->files_update.offset = READ_ONCE(sqe->off);
4869 req->files_update.nr_args = READ_ONCE(sqe->len);
4870 if (!req->files_update.nr_args)
4871 return -EINVAL;
4872 req->files_update.arg = READ_ONCE(sqe->addr);
4873 return 0;
4874}
4875
4876static int io_files_update(struct io_kiocb *req, bool force_nonblock)
4877{
4878 struct io_ring_ctx *ctx = req->ctx;
4879 struct io_uring_files_update up;
4880 int ret;
4881
Jens Axboef86cd202020-01-29 13:46:44 -07004882 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004883 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004884
4885 up.offset = req->files_update.offset;
4886 up.fds = req->files_update.arg;
4887
4888 mutex_lock(&ctx->uring_lock);
4889 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
4890 mutex_unlock(&ctx->uring_lock);
4891
4892 if (ret < 0)
4893 req_set_fail_links(req);
4894 io_cqring_add_event(req, ret);
4895 io_put_req(req);
4896 return 0;
4897}
4898
Jens Axboe3529d8c2019-12-19 18:24:38 -07004899static int io_req_defer_prep(struct io_kiocb *req,
4900 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07004901{
Jens Axboee7815732019-12-17 19:45:06 -07004902 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07004903
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03004904 if (!sqe)
4905 return 0;
4906
Jens Axboef86cd202020-01-29 13:46:44 -07004907 if (io_op_defs[req->opcode].file_table) {
4908 ret = io_grab_files(req);
4909 if (unlikely(ret))
4910 return ret;
4911 }
4912
Jens Axboecccf0ee2020-01-27 16:34:48 -07004913 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
4914
Jens Axboed625c6e2019-12-17 19:53:05 -07004915 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07004916 case IORING_OP_NOP:
4917 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004918 case IORING_OP_READV:
4919 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004920 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004921 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004922 break;
4923 case IORING_OP_WRITEV:
4924 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004925 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004926 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004927 break;
Jens Axboe0969e782019-12-17 18:40:57 -07004928 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004929 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004930 break;
4931 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004932 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004933 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004934 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004935 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004936 break;
4937 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004938 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004939 break;
Jens Axboe03b12302019-12-02 18:50:25 -07004940 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004941 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004942 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004943 break;
4944 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004945 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004946 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004947 break;
Jens Axboef499a022019-12-02 16:28:46 -07004948 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004949 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004950 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004951 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004952 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004953 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004954 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004955 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004956 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004957 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004958 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004959 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004960 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004961 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004962 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004963 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004964 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004965 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004966 case IORING_OP_FALLOCATE:
4967 ret = io_fallocate_prep(req, sqe);
4968 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004969 case IORING_OP_OPENAT:
4970 ret = io_openat_prep(req, sqe);
4971 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004972 case IORING_OP_CLOSE:
4973 ret = io_close_prep(req, sqe);
4974 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004975 case IORING_OP_FILES_UPDATE:
4976 ret = io_files_update_prep(req, sqe);
4977 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004978 case IORING_OP_STATX:
4979 ret = io_statx_prep(req, sqe);
4980 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004981 case IORING_OP_FADVISE:
4982 ret = io_fadvise_prep(req, sqe);
4983 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004984 case IORING_OP_MADVISE:
4985 ret = io_madvise_prep(req, sqe);
4986 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004987 case IORING_OP_OPENAT2:
4988 ret = io_openat2_prep(req, sqe);
4989 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004990 case IORING_OP_EPOLL_CTL:
4991 ret = io_epoll_ctl_prep(req, sqe);
4992 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004993 case IORING_OP_SPLICE:
4994 ret = io_splice_prep(req, sqe);
4995 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004996 case IORING_OP_PROVIDE_BUFFERS:
4997 ret = io_provide_buffers_prep(req, sqe);
4998 break;
Jens Axboe067524e2020-03-02 16:32:28 -07004999 case IORING_OP_REMOVE_BUFFERS:
5000 ret = io_remove_buffers_prep(req, sqe);
5001 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005002 default:
Jens Axboee7815732019-12-17 19:45:06 -07005003 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5004 req->opcode);
5005 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005006 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005007 }
5008
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005009 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07005010}
5011
Jens Axboe3529d8c2019-12-19 18:24:38 -07005012static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06005013{
Jackie Liua197f662019-11-08 08:09:12 -07005014 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07005015 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06005016
Bob Liu9d858b22019-11-13 18:06:25 +08005017 /* Still need defer if there is pending req in defer list. */
5018 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06005019 return 0;
5020
Jens Axboe3529d8c2019-12-19 18:24:38 -07005021 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06005022 return -EAGAIN;
5023
Jens Axboe3529d8c2019-12-19 18:24:38 -07005024 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005025 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07005026 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07005027
Jens Axboede0617e2019-04-06 21:51:27 -06005028 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08005029 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06005030 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06005031 return 0;
5032 }
5033
Jens Axboe915967f2019-11-21 09:01:20 -07005034 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06005035 list_add_tail(&req->list, &ctx->defer_list);
5036 spin_unlock_irq(&ctx->completion_lock);
5037 return -EIOCBQUEUED;
5038}
5039
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005040static void io_cleanup_req(struct io_kiocb *req)
5041{
5042 struct io_async_ctx *io = req->io;
5043
5044 switch (req->opcode) {
5045 case IORING_OP_READV:
5046 case IORING_OP_READ_FIXED:
5047 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005048 if (req->flags & REQ_F_BUFFER_SELECTED)
5049 kfree((void *)(unsigned long)req->rw.addr);
5050 /* fallthrough */
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005051 case IORING_OP_WRITEV:
5052 case IORING_OP_WRITE_FIXED:
5053 case IORING_OP_WRITE:
5054 if (io->rw.iov != io->rw.fast_iov)
5055 kfree(io->rw.iov);
5056 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005057 case IORING_OP_RECVMSG:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005058 if (req->flags & REQ_F_BUFFER_SELECTED)
5059 kfree(req->sr_msg.kbuf);
5060 /* fallthrough */
5061 case IORING_OP_SENDMSG:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005062 if (io->msg.iov != io->msg.fast_iov)
5063 kfree(io->msg.iov);
5064 break;
Jens Axboebcda7ba2020-02-23 16:42:51 -07005065 case IORING_OP_RECV:
5066 if (req->flags & REQ_F_BUFFER_SELECTED)
5067 kfree(req->sr_msg.kbuf);
5068 break;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03005069 case IORING_OP_OPENAT:
5070 case IORING_OP_OPENAT2:
5071 case IORING_OP_STATX:
5072 putname(req->open.filename);
5073 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005074 case IORING_OP_SPLICE:
5075 io_put_file(req, req->splice.file_in,
5076 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5077 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005078 }
5079
5080 req->flags &= ~REQ_F_NEED_CLEANUP;
5081}
5082
Jens Axboe3529d8c2019-12-19 18:24:38 -07005083static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Pavel Begunkov014db002020-03-03 21:33:12 +03005084 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005085{
Jackie Liua197f662019-11-08 08:09:12 -07005086 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005087 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005088
Jens Axboed625c6e2019-12-17 19:53:05 -07005089 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005090 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005091 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005092 break;
5093 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005094 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005095 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005096 if (sqe) {
5097 ret = io_read_prep(req, sqe, force_nonblock);
5098 if (ret < 0)
5099 break;
5100 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005101 ret = io_read(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005102 break;
5103 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07005104 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005105 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005106 if (sqe) {
5107 ret = io_write_prep(req, sqe, force_nonblock);
5108 if (ret < 0)
5109 break;
5110 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005111 ret = io_write(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005112 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005113 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005114 if (sqe) {
5115 ret = io_prep_fsync(req, sqe);
5116 if (ret < 0)
5117 break;
5118 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005119 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005120 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005121 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005122 if (sqe) {
5123 ret = io_poll_add_prep(req, sqe);
5124 if (ret)
5125 break;
5126 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005127 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005128 break;
5129 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005130 if (sqe) {
5131 ret = io_poll_remove_prep(req, sqe);
5132 if (ret < 0)
5133 break;
5134 }
Jens Axboefc4df992019-12-10 14:38:45 -07005135 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005136 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005137 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005138 if (sqe) {
5139 ret = io_prep_sfr(req, sqe);
5140 if (ret < 0)
5141 break;
5142 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005143 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005144 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005145 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005146 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005147 if (sqe) {
5148 ret = io_sendmsg_prep(req, sqe);
5149 if (ret < 0)
5150 break;
5151 }
Jens Axboefddafac2020-01-04 20:19:44 -07005152 if (req->opcode == IORING_OP_SENDMSG)
Pavel Begunkov014db002020-03-03 21:33:12 +03005153 ret = io_sendmsg(req, force_nonblock);
Jens Axboefddafac2020-01-04 20:19:44 -07005154 else
Pavel Begunkov014db002020-03-03 21:33:12 +03005155 ret = io_send(req, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005156 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06005157 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005158 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005159 if (sqe) {
5160 ret = io_recvmsg_prep(req, sqe);
5161 if (ret)
5162 break;
5163 }
Jens Axboefddafac2020-01-04 20:19:44 -07005164 if (req->opcode == IORING_OP_RECVMSG)
Pavel Begunkov014db002020-03-03 21:33:12 +03005165 ret = io_recvmsg(req, force_nonblock);
Jens Axboefddafac2020-01-04 20:19:44 -07005166 else
Pavel Begunkov014db002020-03-03 21:33:12 +03005167 ret = io_recv(req, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06005168 break;
Jens Axboe5262f562019-09-17 12:26:57 -06005169 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005170 if (sqe) {
5171 ret = io_timeout_prep(req, sqe, false);
5172 if (ret)
5173 break;
5174 }
Jens Axboefc4df992019-12-10 14:38:45 -07005175 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005176 break;
Jens Axboe11365042019-10-16 09:08:32 -06005177 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005178 if (sqe) {
5179 ret = io_timeout_remove_prep(req, sqe);
5180 if (ret)
5181 break;
5182 }
Jens Axboefc4df992019-12-10 14:38:45 -07005183 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06005184 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06005185 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005186 if (sqe) {
5187 ret = io_accept_prep(req, sqe);
5188 if (ret)
5189 break;
5190 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005191 ret = io_accept(req, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005192 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005193 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005194 if (sqe) {
5195 ret = io_connect_prep(req, sqe);
5196 if (ret)
5197 break;
5198 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005199 ret = io_connect(req, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005200 break;
Jens Axboe62755e32019-10-28 21:49:21 -06005201 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005202 if (sqe) {
5203 ret = io_async_cancel_prep(req, sqe);
5204 if (ret)
5205 break;
5206 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005207 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005208 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005209 case IORING_OP_FALLOCATE:
5210 if (sqe) {
5211 ret = io_fallocate_prep(req, sqe);
5212 if (ret)
5213 break;
5214 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005215 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005216 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005217 case IORING_OP_OPENAT:
5218 if (sqe) {
5219 ret = io_openat_prep(req, sqe);
5220 if (ret)
5221 break;
5222 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005223 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005224 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005225 case IORING_OP_CLOSE:
5226 if (sqe) {
5227 ret = io_close_prep(req, sqe);
5228 if (ret)
5229 break;
5230 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005231 ret = io_close(req, force_nonblock);
Jens Axboeb5dba592019-12-11 14:02:38 -07005232 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005233 case IORING_OP_FILES_UPDATE:
5234 if (sqe) {
5235 ret = io_files_update_prep(req, sqe);
5236 if (ret)
5237 break;
5238 }
5239 ret = io_files_update(req, force_nonblock);
5240 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005241 case IORING_OP_STATX:
5242 if (sqe) {
5243 ret = io_statx_prep(req, sqe);
5244 if (ret)
5245 break;
5246 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005247 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005248 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005249 case IORING_OP_FADVISE:
5250 if (sqe) {
5251 ret = io_fadvise_prep(req, sqe);
5252 if (ret)
5253 break;
5254 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005255 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005256 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005257 case IORING_OP_MADVISE:
5258 if (sqe) {
5259 ret = io_madvise_prep(req, sqe);
5260 if (ret)
5261 break;
5262 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005263 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005264 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005265 case IORING_OP_OPENAT2:
5266 if (sqe) {
5267 ret = io_openat2_prep(req, sqe);
5268 if (ret)
5269 break;
5270 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005271 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005272 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005273 case IORING_OP_EPOLL_CTL:
5274 if (sqe) {
5275 ret = io_epoll_ctl_prep(req, sqe);
5276 if (ret)
5277 break;
5278 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005279 ret = io_epoll_ctl(req, force_nonblock);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005280 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005281 case IORING_OP_SPLICE:
5282 if (sqe) {
5283 ret = io_splice_prep(req, sqe);
5284 if (ret < 0)
5285 break;
5286 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005287 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005288 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005289 case IORING_OP_PROVIDE_BUFFERS:
5290 if (sqe) {
5291 ret = io_provide_buffers_prep(req, sqe);
5292 if (ret)
5293 break;
5294 }
5295 ret = io_provide_buffers(req, force_nonblock);
5296 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005297 case IORING_OP_REMOVE_BUFFERS:
5298 if (sqe) {
5299 ret = io_remove_buffers_prep(req, sqe);
5300 if (ret)
5301 break;
5302 }
5303 ret = io_remove_buffers(req, force_nonblock);
Jens Axboe31b51512019-01-18 22:56:34 -07005304 break;
5305 default:
5306 ret = -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07005307 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005308 }
5309
Jens Axboe31b51512019-01-18 22:56:34 -07005310 if (ret)
5311 return ret;
5312
5313 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07005314 const bool in_async = io_wq_current_is_worker();
5315
Jens Axboe9e645e112019-05-10 16:07:28 -06005316 if (req->result == -EAGAIN)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005317 return -EAGAIN;
5318
Jens Axboe11ba8202020-01-15 21:51:17 -07005319 /* workqueue context doesn't hold uring_lock, grab it now */
5320 if (in_async)
5321 mutex_lock(&ctx->uring_lock);
5322
Jens Axboe2b188cc2019-01-07 10:46:33 -07005323 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07005324
5325 if (in_async)
5326 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005327 }
5328
5329 return 0;
5330}
5331
Jens Axboe561fb042019-10-24 07:25:42 -06005332static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005333{
Jens Axboe561fb042019-10-24 07:25:42 -06005334 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005335 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06005336 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005337
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005338 /* if NO_CANCEL is set, we must still run the work */
5339 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
5340 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06005341 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005342 }
Jens Axboe31b51512019-01-18 22:56:34 -07005343
Jens Axboe561fb042019-10-24 07:25:42 -06005344 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06005345 do {
Pavel Begunkov014db002020-03-03 21:33:12 +03005346 ret = io_issue_sqe(req, NULL, false);
Jens Axboe561fb042019-10-24 07:25:42 -06005347 /*
5348 * We can get EAGAIN for polled IO even though we're
5349 * forcing a sync submission from here, since we can't
5350 * wait for request slots on the block side.
5351 */
5352 if (ret != -EAGAIN)
5353 break;
5354 cond_resched();
5355 } while (1);
5356 }
Jens Axboe31b51512019-01-18 22:56:34 -07005357
Jens Axboe561fb042019-10-24 07:25:42 -06005358 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005359 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07005360 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06005361 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07005362 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005363
Pavel Begunkove9fd9392020-03-04 16:14:12 +03005364 io_steal_work(req, workptr);
Jens Axboe31b51512019-01-18 22:56:34 -07005365}
Jens Axboe2b188cc2019-01-07 10:46:33 -07005366
Jens Axboe15b71ab2019-12-11 11:20:36 -07005367static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe9e3aa612019-12-11 15:55:43 -07005368{
Jens Axboed3656342019-12-18 09:50:26 -07005369 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07005370 return 0;
Jens Axboe0b5faf62020-02-06 21:42:51 -07005371 if ((fd == -1 || fd == AT_FDCWD) && io_op_defs[req->opcode].fd_non_neg)
Jens Axboed3656342019-12-18 09:50:26 -07005372 return 0;
5373 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06005374}
5375
Jens Axboe65e19f52019-10-26 07:20:21 -06005376static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
5377 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06005378{
Jens Axboe65e19f52019-10-26 07:20:21 -06005379 struct fixed_file_table *table;
5380
Jens Axboe05f3fb32019-12-09 11:22:50 -07005381 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
5382 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06005383}
5384
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005385static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
5386 int fd, struct file **out_file, bool fixed)
5387{
5388 struct io_ring_ctx *ctx = req->ctx;
5389 struct file *file;
5390
5391 if (fixed) {
5392 if (unlikely(!ctx->file_data ||
5393 (unsigned) fd >= ctx->nr_user_files))
5394 return -EBADF;
5395 fd = array_index_nospec(fd, ctx->nr_user_files);
5396 file = io_file_from_index(ctx, fd);
5397 if (!file)
5398 return -EBADF;
Xiaoguang Wang05589552020-03-31 14:05:18 +08005399 req->fixed_file_refs = ctx->file_data->cur_refs;
5400 percpu_ref_get(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005401 } else {
5402 trace_io_uring_file_get(ctx, fd);
5403 file = __io_file_get(state, fd);
5404 if (unlikely(!file))
5405 return -EBADF;
5406 }
5407
5408 *out_file = file;
5409 return 0;
5410}
5411
Jens Axboe3529d8c2019-12-19 18:24:38 -07005412static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Pavel Begunkov9c280f92020-04-08 08:58:46 +03005413 int fd, unsigned int flags)
Jens Axboe09bb8392019-03-13 12:39:28 -06005414{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005415 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06005416
Jens Axboed3656342019-12-18 09:50:26 -07005417 if (!io_req_needs_file(req, fd))
5418 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06005419
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005420 fixed = (flags & IOSQE_FIXED_FILE);
5421 if (unlikely(!fixed && req->needs_fixed_file))
5422 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06005423
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005424 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06005425}
5426
Jackie Liua197f662019-11-08 08:09:12 -07005427static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005428{
Jens Axboefcb323c2019-10-24 12:39:47 -06005429 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07005430 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06005431
Jens Axboef86cd202020-01-29 13:46:44 -07005432 if (req->work.files)
5433 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005434 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07005435 return -EBADF;
5436
Jens Axboefcb323c2019-10-24 12:39:47 -06005437 rcu_read_lock();
5438 spin_lock_irq(&ctx->inflight_lock);
5439 /*
5440 * We use the f_ops->flush() handler to ensure that we can flush
5441 * out work accessing these files if the fd is closed. Check if
5442 * the fd has changed since we started down this path, and disallow
5443 * this operation if it has.
5444 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005445 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06005446 list_add(&req->inflight_entry, &ctx->inflight_list);
5447 req->flags |= REQ_F_INFLIGHT;
5448 req->work.files = current->files;
5449 ret = 0;
5450 }
5451 spin_unlock_irq(&ctx->inflight_lock);
5452 rcu_read_unlock();
5453
5454 return ret;
5455}
5456
Jens Axboe2665abf2019-11-05 12:40:47 -07005457static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
5458{
Jens Axboead8a48a2019-11-15 08:49:11 -07005459 struct io_timeout_data *data = container_of(timer,
5460 struct io_timeout_data, timer);
5461 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07005462 struct io_ring_ctx *ctx = req->ctx;
5463 struct io_kiocb *prev = NULL;
5464 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07005465
5466 spin_lock_irqsave(&ctx->completion_lock, flags);
5467
5468 /*
5469 * We don't expect the list to be empty, that will only happen if we
5470 * race with the completion of the linked work.
5471 */
Pavel Begunkov44932332019-12-05 16:16:35 +03005472 if (!list_empty(&req->link_list)) {
5473 prev = list_entry(req->link_list.prev, struct io_kiocb,
5474 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005475 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03005476 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005477 prev->flags &= ~REQ_F_LINK_TIMEOUT;
5478 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07005479 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005480 }
5481
5482 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5483
5484 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005485 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03005486 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07005487 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07005488 } else {
5489 io_cqring_add_event(req, -ETIME);
5490 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005491 }
Jens Axboe2665abf2019-11-05 12:40:47 -07005492 return HRTIMER_NORESTART;
5493}
5494
Jens Axboead8a48a2019-11-15 08:49:11 -07005495static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005496{
Jens Axboe76a46e02019-11-10 23:34:16 -07005497 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07005498
Jens Axboe76a46e02019-11-10 23:34:16 -07005499 /*
5500 * If the list is now empty, then our linked request finished before
5501 * we got a chance to setup the timer
5502 */
5503 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03005504 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07005505 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07005506
Jens Axboead8a48a2019-11-15 08:49:11 -07005507 data->timer.function = io_link_timeout_fn;
5508 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
5509 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07005510 }
Jens Axboe76a46e02019-11-10 23:34:16 -07005511 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07005512
Jens Axboe2665abf2019-11-05 12:40:47 -07005513 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07005514 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005515}
5516
Jens Axboead8a48a2019-11-15 08:49:11 -07005517static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005518{
5519 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005520
Pavel Begunkovdea3b492020-04-12 02:05:04 +03005521 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07005522 return NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005523 /* for polled retry, if flag is set, we already went through here */
5524 if (req->flags & REQ_F_POLLED)
5525 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005526
Pavel Begunkov44932332019-12-05 16:16:35 +03005527 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
5528 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07005529 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07005530 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005531
Jens Axboe76a46e02019-11-10 23:34:16 -07005532 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07005533 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07005534}
5535
Jens Axboe3529d8c2019-12-19 18:24:38 -07005536static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005537{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005538 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005539 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07005540 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005541 int ret;
5542
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005543again:
5544 linked_timeout = io_prep_linked_timeout(req);
5545
Jens Axboe193155c2020-02-22 23:22:19 -07005546 if (req->work.creds && req->work.creds != current_cred()) {
5547 if (old_creds)
5548 revert_creds(old_creds);
5549 if (old_creds == req->work.creds)
5550 old_creds = NULL; /* restored original creds */
5551 else
5552 old_creds = override_creds(req->work.creds);
5553 }
5554
Pavel Begunkov014db002020-03-03 21:33:12 +03005555 ret = io_issue_sqe(req, sqe, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06005556
5557 /*
5558 * We async punt it if the file wasn't marked NOWAIT, or if the file
5559 * doesn't support non-blocking read/write attempts
5560 */
5561 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
5562 (req->flags & REQ_F_MUST_PUNT))) {
Jens Axboed7718a92020-02-14 22:23:12 -07005563 if (io_arm_poll_handler(req)) {
5564 if (linked_timeout)
5565 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005566 goto exit;
Jens Axboed7718a92020-02-14 22:23:12 -07005567 }
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005568punt:
Jens Axboef86cd202020-01-29 13:46:44 -07005569 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005570 ret = io_grab_files(req);
5571 if (ret)
5572 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005573 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005574
5575 /*
5576 * Queued up for async execution, worker will release
5577 * submit reference when the iocb is actually submitted.
5578 */
5579 io_queue_async_work(req);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005580 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005581 }
Jens Axboee65ef562019-03-12 10:16:44 -06005582
Jens Axboefcb323c2019-10-24 12:39:47 -06005583err:
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005584 nxt = NULL;
Jens Axboee65ef562019-03-12 10:16:44 -06005585 /* drop submission reference */
Jens Axboe2a44f462020-02-25 13:25:41 -07005586 io_put_req_find_next(req, &nxt);
Jens Axboee65ef562019-03-12 10:16:44 -06005587
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005588 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07005589 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005590 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005591 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005592 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005593 }
5594
Jens Axboee65ef562019-03-12 10:16:44 -06005595 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06005596 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07005597 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005598 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06005599 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06005600 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005601 if (nxt) {
5602 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005603
5604 if (req->flags & REQ_F_FORCE_ASYNC)
5605 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005606 goto again;
5607 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005608exit:
Jens Axboe193155c2020-02-22 23:22:19 -07005609 if (old_creds)
5610 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005611}
5612
Jens Axboe3529d8c2019-12-19 18:24:38 -07005613static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005614{
5615 int ret;
5616
Jens Axboe3529d8c2019-12-19 18:24:38 -07005617 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005618 if (ret) {
5619 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03005620fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005621 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005622 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07005623 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005624 }
Pavel Begunkov25508782019-12-30 21:24:47 +03005625 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkov11185912020-01-22 23:09:35 +03005626 ret = io_req_defer_prep(req, sqe);
5627 if (unlikely(ret < 0))
5628 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07005629 /*
5630 * Never try inline submit of IOSQE_ASYNC is set, go straight
5631 * to async execution.
5632 */
5633 req->work.flags |= IO_WQ_WORK_CONCURRENT;
5634 io_queue_async_work(req);
5635 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005636 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07005637 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08005638}
5639
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005640static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005641{
Jens Axboe94ae5e72019-11-14 19:39:52 -07005642 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005643 io_cqring_add_event(req, -ECANCELED);
5644 io_double_put_req(req);
5645 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07005646 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005647}
5648
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005649static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe3529d8c2019-12-19 18:24:38 -07005650 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06005651{
Jackie Liua197f662019-11-08 08:09:12 -07005652 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005653 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06005654
Jens Axboe9e645e112019-05-10 16:07:28 -06005655 /*
5656 * If we already have a head request, queue this one for async
5657 * submittal once the head completes. If we don't have a head but
5658 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
5659 * submitted sync once the chain is complete. If none of those
5660 * conditions are true (normal request), then just queue it.
5661 */
5662 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03005663 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06005664
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03005665 /*
5666 * Taking sequential execution of a link, draining both sides
5667 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
5668 * requests in the link. So, it drains the head and the
5669 * next after the link request. The last one is done via
5670 * drain_next flag to persist the effect across calls.
5671 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005672 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03005673 head->flags |= REQ_F_IO_DRAIN;
5674 ctx->drain_next = 1;
5675 }
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005676 if (io_alloc_async_ctx(req))
5677 return -EAGAIN;
Jens Axboe9e645e112019-05-10 16:07:28 -06005678
Jens Axboe3529d8c2019-12-19 18:24:38 -07005679 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005680 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005681 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03005682 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005683 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07005684 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03005685 trace_io_uring_link(ctx, req, head);
5686 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06005687
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005688 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005689 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005690 io_queue_link_head(head);
5691 *link = NULL;
5692 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005693 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03005694 if (unlikely(ctx->drain_next)) {
5695 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005696 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03005697 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005698 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03005699 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03005700 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03005701
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005702 if (io_alloc_async_ctx(req))
5703 return -EAGAIN;
5704
Pavel Begunkov711be032020-01-17 03:57:59 +03005705 ret = io_req_defer_prep(req, sqe);
5706 if (ret)
5707 req->flags |= REQ_F_FAIL_LINK;
5708 *link = req;
5709 } else {
5710 io_queue_sqe(req, sqe);
5711 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005712 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005713
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005714 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06005715}
5716
Jens Axboe9a56a232019-01-09 09:06:50 -07005717/*
5718 * Batched submission is done, ensure local IO is flushed out.
5719 */
5720static void io_submit_state_end(struct io_submit_state *state)
5721{
5722 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06005723 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07005724 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03005725 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07005726}
5727
5728/*
5729 * Start submission side cache.
5730 */
5731static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08005732 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07005733{
5734 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07005735 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07005736 state->file = NULL;
5737 state->ios_left = max_ios;
5738}
5739
Jens Axboe2b188cc2019-01-07 10:46:33 -07005740static void io_commit_sqring(struct io_ring_ctx *ctx)
5741{
Hristo Venev75b28af2019-08-26 17:23:46 +00005742 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005743
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03005744 /*
5745 * Ensure any loads from the SQEs are done at this point,
5746 * since once we write the new head, the application could
5747 * write new data to them.
5748 */
5749 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005750}
5751
5752/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07005753 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07005754 * that is mapped by userspace. This means that care needs to be taken to
5755 * ensure that reads are stable, as we cannot rely on userspace always
5756 * being a good citizen. If members of the sqe are validated and then later
5757 * used, it's important that those reads are done through READ_ONCE() to
5758 * prevent a re-load down the line.
5759 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03005760static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005761{
Hristo Venev75b28af2019-08-26 17:23:46 +00005762 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005763 unsigned head;
5764
5765 /*
5766 * The cached sq head (or cq tail) serves two purposes:
5767 *
5768 * 1) allows us to batch the cost of updating the user visible
5769 * head updates.
5770 * 2) allows the kernel side to track the head on its own, even
5771 * though the application is the one updating it.
5772 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005773 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03005774 if (likely(head < ctx->sq_entries))
5775 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07005776
5777 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06005778 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005779 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03005780 return NULL;
5781}
5782
5783static inline void io_consume_sqe(struct io_ring_ctx *ctx)
5784{
5785 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005786}
5787
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005788#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
5789 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
5790 IOSQE_BUFFER_SELECT)
5791
5792static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
5793 const struct io_uring_sqe *sqe,
5794 struct io_submit_state *state, bool async)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005795{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005796 unsigned int sqe_flags;
5797 int id, fd;
5798
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005799 /*
5800 * All io need record the previous position, if LINK vs DARIN,
5801 * it can be used to mark the position of the first IO in the
5802 * link list.
5803 */
Pavel Begunkov31af27c2020-04-15 00:39:50 +03005804 req->sequence = ctx->cached_sq_head - ctx->cached_sq_dropped;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005805 req->opcode = READ_ONCE(sqe->opcode);
5806 req->user_data = READ_ONCE(sqe->user_data);
5807 req->io = NULL;
5808 req->file = NULL;
5809 req->ctx = ctx;
5810 req->flags = 0;
5811 /* one is dropped after submission, the other at completion */
5812 refcount_set(&req->refs, 2);
5813 req->task = NULL;
5814 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005815 req->needs_fixed_file = async;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005816 INIT_IO_WORK(&req->work, io_wq_submit_work);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005817
5818 if (unlikely(req->opcode >= IORING_OP_LAST))
5819 return -EINVAL;
5820
5821 if (io_op_defs[req->opcode].needs_mm && !current->mm) {
5822 if (unlikely(!mmget_not_zero(ctx->sqo_mm)))
5823 return -EFAULT;
5824 use_mm(ctx->sqo_mm);
5825 }
5826
5827 sqe_flags = READ_ONCE(sqe->flags);
5828 /* enforce forwards compatibility on users */
5829 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
5830 return -EINVAL;
5831
5832 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
5833 !io_op_defs[req->opcode].buffer_select)
5834 return -EOPNOTSUPP;
5835
5836 id = READ_ONCE(sqe->personality);
5837 if (id) {
5838 req->work.creds = idr_find(&ctx->personality_idr, id);
5839 if (unlikely(!req->work.creds))
5840 return -EINVAL;
5841 get_cred(req->work.creds);
5842 }
5843
5844 /* same numerical values with corresponding REQ_F_*, safe to copy */
5845 req->flags |= sqe_flags & (IOSQE_IO_DRAIN | IOSQE_IO_HARDLINK |
5846 IOSQE_ASYNC | IOSQE_FIXED_FILE |
5847 IOSQE_BUFFER_SELECT | IOSQE_IO_LINK);
5848
5849 fd = READ_ONCE(sqe->fd);
5850 return io_req_set_file(state, req, fd, sqe_flags);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005851}
5852
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005853static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03005854 struct file *ring_file, int ring_fd, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07005855{
5856 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005857 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005858 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005859
Jens Axboec4a2ed72019-11-21 21:01:26 -07005860 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07005861 if (test_bit(0, &ctx->sq_check_overflow)) {
5862 if (!list_empty(&ctx->cq_overflow_list) &&
5863 !io_cqring_overflow_flush(ctx, false))
5864 return -EBUSY;
5865 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005866
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005867 /* make sure SQ entry isn't read before tail */
5868 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03005869
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005870 if (!percpu_ref_tryget_many(&ctx->refs, nr))
5871 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005872
5873 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08005874 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005875 statep = &state;
5876 }
5877
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005878 ctx->ring_fd = ring_fd;
5879 ctx->ring_file = ring_file;
5880
Jens Axboe6c271ce2019-01-10 11:22:30 -07005881 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005882 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03005883 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005884 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005885
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03005886 sqe = io_get_sqe(ctx);
5887 if (unlikely(!sqe)) {
5888 io_consume_sqe(ctx);
5889 break;
5890 }
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005891 req = io_alloc_req(ctx, statep);
Pavel Begunkov196be952019-11-07 01:41:06 +03005892 if (unlikely(!req)) {
5893 if (!submitted)
5894 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005895 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06005896 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005897
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005898 err = io_init_req(ctx, req, sqe, statep, async);
Pavel Begunkov709b3022020-04-08 08:58:43 +03005899 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07005900 /* will complete beyond this point, count as submitted */
5901 submitted++;
5902
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005903 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005904fail_req:
5905 io_cqring_add_event(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07005906 io_double_put_req(req);
5907 break;
5908 }
5909
Jens Axboe354420f2020-01-08 18:55:15 -07005910 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
5911 true, async);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005912 err = io_submit_sqe(req, sqe, statep, &link);
5913 if (err)
5914 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005915 }
5916
Pavel Begunkov9466f432020-01-25 22:34:01 +03005917 if (unlikely(submitted != nr)) {
5918 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
5919
5920 percpu_ref_put_many(&ctx->refs, nr - ref_used);
5921 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005922 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005923 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005924 if (statep)
5925 io_submit_state_end(&state);
5926
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005927 /* Commit SQ ring head once we've consumed and submitted all SQEs */
5928 io_commit_sqring(ctx);
5929
Jens Axboe6c271ce2019-01-10 11:22:30 -07005930 return submitted;
5931}
5932
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03005933static inline void io_sq_thread_drop_mm(struct io_ring_ctx *ctx)
5934{
5935 struct mm_struct *mm = current->mm;
5936
5937 if (mm) {
5938 unuse_mm(mm);
5939 mmput(mm);
5940 }
5941}
5942
Jens Axboe6c271ce2019-01-10 11:22:30 -07005943static int io_sq_thread(void *data)
5944{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005945 struct io_ring_ctx *ctx = data;
Jens Axboe181e4482019-11-25 08:52:30 -07005946 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005947 mm_segment_t old_fs;
5948 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005949 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005950 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005951
Jens Axboe206aefd2019-11-07 18:27:42 -07005952 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08005953
Jens Axboe6c271ce2019-01-10 11:22:30 -07005954 old_fs = get_fs();
5955 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07005956 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005957
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005958 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005959 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005960 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005961
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005962 if (!list_empty(&ctx->poll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005963 unsigned nr_events = 0;
5964
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005965 mutex_lock(&ctx->uring_lock);
5966 if (!list_empty(&ctx->poll_list))
5967 io_iopoll_getevents(ctx, &nr_events, 0);
5968 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07005969 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005970 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005971 }
5972
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005973 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005974
5975 /*
5976 * If submit got -EBUSY, flag us as needing the application
5977 * to enter the kernel to reap and flush events.
5978 */
5979 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005980 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01005981 * Drop cur_mm before scheduling, we can't hold it for
5982 * long periods (or over schedule()). Do this before
5983 * adding ourselves to the waitqueue, as the unuse/drop
5984 * may sleep.
5985 */
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03005986 io_sq_thread_drop_mm(ctx);
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01005987
5988 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07005989 * We're polling. If we're within the defined idle
5990 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07005991 * to sleep. The exception is if we got EBUSY doing
5992 * more IO, we should wait for the application to
5993 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07005994 */
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005995 if (!list_empty(&ctx->poll_list) ||
Jens Axboedf069d82020-02-04 16:48:34 -07005996 (!time_after(jiffies, timeout) && ret != -EBUSY &&
5997 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005998 if (current->task_works)
5999 task_work_run();
Jens Axboe9831a902019-09-19 09:48:55 -06006000 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006001 continue;
6002 }
6003
Jens Axboe6c271ce2019-01-10 11:22:30 -07006004 prepare_to_wait(&ctx->sqo_wait, &wait,
6005 TASK_INTERRUPTIBLE);
6006
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006007 /*
6008 * While doing polled IO, before going to sleep, we need
6009 * to check if there are new reqs added to poll_list, it
6010 * is because reqs may have been punted to io worker and
6011 * will be added to poll_list later, hence check the
6012 * poll_list again.
6013 */
6014 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6015 !list_empty_careful(&ctx->poll_list)) {
6016 finish_wait(&ctx->sqo_wait, &wait);
6017 continue;
6018 }
6019
Jens Axboe6c271ce2019-01-10 11:22:30 -07006020 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00006021 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02006022 /* make sure to read SQ tail after writing flags */
6023 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006024
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006025 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006026 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006027 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006028 finish_wait(&ctx->sqo_wait, &wait);
6029 break;
6030 }
Jens Axboeb41e9852020-02-17 09:52:41 -07006031 if (current->task_works) {
6032 task_work_run();
Hillf Danton10bea962020-04-01 17:19:33 +08006033 finish_wait(&ctx->sqo_wait, &wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07006034 continue;
6035 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006036 if (signal_pending(current))
6037 flush_signals(current);
6038 schedule();
6039 finish_wait(&ctx->sqo_wait, &wait);
6040
Hristo Venev75b28af2019-08-26 17:23:46 +00006041 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006042 continue;
6043 }
6044 finish_wait(&ctx->sqo_wait, &wait);
6045
Hristo Venev75b28af2019-08-26 17:23:46 +00006046 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006047 }
6048
Jens Axboe8a4955f2019-12-09 14:52:35 -07006049 mutex_lock(&ctx->uring_lock);
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03006050 ret = io_submit_sqes(ctx, to_submit, NULL, -1, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07006051 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006052 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006053 }
6054
Jens Axboeb41e9852020-02-17 09:52:41 -07006055 if (current->task_works)
6056 task_work_run();
6057
Jens Axboe6c271ce2019-01-10 11:22:30 -07006058 set_fs(old_fs);
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03006059 io_sq_thread_drop_mm(ctx);
Jens Axboe181e4482019-11-25 08:52:30 -07006060 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006061
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006062 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006063
Jens Axboe6c271ce2019-01-10 11:22:30 -07006064 return 0;
6065}
6066
Jens Axboebda52162019-09-24 13:47:15 -06006067struct io_wait_queue {
6068 struct wait_queue_entry wq;
6069 struct io_ring_ctx *ctx;
6070 unsigned to_wait;
6071 unsigned nr_timeouts;
6072};
6073
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006074static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06006075{
6076 struct io_ring_ctx *ctx = iowq->ctx;
6077
6078 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006079 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006080 * started waiting. For timeouts, we always want to return to userspace,
6081 * regardless of event count.
6082 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006083 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006084 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6085}
6086
6087static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6088 int wake_flags, void *key)
6089{
6090 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6091 wq);
6092
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006093 /* use noflush == true, as we can't safely rely on locking context */
6094 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06006095 return -1;
6096
6097 return autoremove_wake_function(curr, mode, wake_flags, key);
6098}
6099
Jens Axboe2b188cc2019-01-07 10:46:33 -07006100/*
6101 * Wait until events become available, if we don't already have some. The
6102 * application must reap them itself, as they reside on the shared cq ring.
6103 */
6104static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6105 const sigset_t __user *sig, size_t sigsz)
6106{
Jens Axboebda52162019-09-24 13:47:15 -06006107 struct io_wait_queue iowq = {
6108 .wq = {
6109 .private = current,
6110 .func = io_wake_function,
6111 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6112 },
6113 .ctx = ctx,
6114 .to_wait = min_events,
6115 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006116 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006117 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006118
Jens Axboeb41e9852020-02-17 09:52:41 -07006119 do {
6120 if (io_cqring_events(ctx, false) >= min_events)
6121 return 0;
6122 if (!current->task_works)
6123 break;
6124 task_work_run();
6125 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006126
6127 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006128#ifdef CONFIG_COMPAT
6129 if (in_compat_syscall())
6130 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006131 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006132 else
6133#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006134 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006135
Jens Axboe2b188cc2019-01-07 10:46:33 -07006136 if (ret)
6137 return ret;
6138 }
6139
Jens Axboebda52162019-09-24 13:47:15 -06006140 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006141 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006142 do {
6143 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6144 TASK_INTERRUPTIBLE);
Jens Axboeb41e9852020-02-17 09:52:41 -07006145 if (current->task_works)
6146 task_work_run();
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006147 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06006148 break;
6149 schedule();
6150 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006151 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06006152 break;
6153 }
6154 } while (1);
6155 finish_wait(&ctx->wait, &iowq.wq);
6156
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006157 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006158
Hristo Venev75b28af2019-08-26 17:23:46 +00006159 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006160}
6161
Jens Axboe6b063142019-01-10 22:13:58 -07006162static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6163{
6164#if defined(CONFIG_UNIX)
6165 if (ctx->ring_sock) {
6166 struct sock *sock = ctx->ring_sock->sk;
6167 struct sk_buff *skb;
6168
6169 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6170 kfree_skb(skb);
6171 }
6172#else
6173 int i;
6174
Jens Axboe65e19f52019-10-26 07:20:21 -06006175 for (i = 0; i < ctx->nr_user_files; i++) {
6176 struct file *file;
6177
6178 file = io_file_from_index(ctx, i);
6179 if (file)
6180 fput(file);
6181 }
Jens Axboe6b063142019-01-10 22:13:58 -07006182#endif
6183}
6184
Jens Axboe05f3fb32019-12-09 11:22:50 -07006185static void io_file_ref_kill(struct percpu_ref *ref)
6186{
6187 struct fixed_file_data *data;
6188
6189 data = container_of(ref, struct fixed_file_data, refs);
6190 complete(&data->done);
6191}
6192
Jens Axboe6b063142019-01-10 22:13:58 -07006193static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6194{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006195 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006196 struct fixed_file_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006197 unsigned nr_tables, i;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006198 unsigned long flags;
Jens Axboe65e19f52019-10-26 07:20:21 -06006199
Jens Axboe05f3fb32019-12-09 11:22:50 -07006200 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006201 return -ENXIO;
6202
Xiaoguang Wang05589552020-03-31 14:05:18 +08006203 spin_lock_irqsave(&data->lock, flags);
6204 if (!list_empty(&data->ref_list))
6205 ref_node = list_first_entry(&data->ref_list,
6206 struct fixed_file_ref_node, node);
6207 spin_unlock_irqrestore(&data->lock, flags);
6208 if (ref_node)
6209 percpu_ref_kill(&ref_node->refs);
6210
6211 percpu_ref_kill(&data->refs);
6212
6213 /* wait for all refs nodes to complete */
Jens Axboe2faf8522020-02-04 19:54:55 -07006214 wait_for_completion(&data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006215
Jens Axboe6b063142019-01-10 22:13:58 -07006216 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06006217 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6218 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006219 kfree(data->table[i].files);
6220 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006221 percpu_ref_exit(&data->refs);
6222 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006223 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006224 ctx->nr_user_files = 0;
6225 return 0;
6226}
6227
Jens Axboe6c271ce2019-01-10 11:22:30 -07006228static void io_sq_thread_stop(struct io_ring_ctx *ctx)
6229{
6230 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07006231 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006232 /*
6233 * The park is a bit of a work-around, without it we get
6234 * warning spews on shutdown with SQPOLL set and affinity
6235 * set to a single CPU.
6236 */
Jens Axboe06058632019-04-13 09:26:03 -06006237 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006238 kthread_stop(ctx->sqo_thread);
6239 ctx->sqo_thread = NULL;
6240 }
6241}
6242
Jens Axboe6b063142019-01-10 22:13:58 -07006243static void io_finish_async(struct io_ring_ctx *ctx)
6244{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006245 io_sq_thread_stop(ctx);
6246
Jens Axboe561fb042019-10-24 07:25:42 -06006247 if (ctx->io_wq) {
6248 io_wq_destroy(ctx->io_wq);
6249 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006250 }
6251}
6252
6253#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07006254/*
6255 * Ensure the UNIX gc is aware of our file set, so we are certain that
6256 * the io_uring can be safely unregistered on process exit, even if we have
6257 * loops in the file referencing.
6258 */
6259static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
6260{
6261 struct sock *sk = ctx->ring_sock->sk;
6262 struct scm_fp_list *fpl;
6263 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06006264 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07006265
Jens Axboe6b063142019-01-10 22:13:58 -07006266 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
6267 if (!fpl)
6268 return -ENOMEM;
6269
6270 skb = alloc_skb(0, GFP_KERNEL);
6271 if (!skb) {
6272 kfree(fpl);
6273 return -ENOMEM;
6274 }
6275
6276 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07006277
Jens Axboe08a45172019-10-03 08:11:03 -06006278 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07006279 fpl->user = get_uid(ctx->user);
6280 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006281 struct file *file = io_file_from_index(ctx, i + offset);
6282
6283 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06006284 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06006285 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06006286 unix_inflight(fpl->user, fpl->fp[nr_files]);
6287 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07006288 }
6289
Jens Axboe08a45172019-10-03 08:11:03 -06006290 if (nr_files) {
6291 fpl->max = SCM_MAX_FD;
6292 fpl->count = nr_files;
6293 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006294 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06006295 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
6296 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07006297
Jens Axboe08a45172019-10-03 08:11:03 -06006298 for (i = 0; i < nr_files; i++)
6299 fput(fpl->fp[i]);
6300 } else {
6301 kfree_skb(skb);
6302 kfree(fpl);
6303 }
Jens Axboe6b063142019-01-10 22:13:58 -07006304
6305 return 0;
6306}
6307
6308/*
6309 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
6310 * causes regular reference counting to break down. We rely on the UNIX
6311 * garbage collection to take care of this problem for us.
6312 */
6313static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6314{
6315 unsigned left, total;
6316 int ret = 0;
6317
6318 total = 0;
6319 left = ctx->nr_user_files;
6320 while (left) {
6321 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07006322
6323 ret = __io_sqe_files_scm(ctx, this_files, total);
6324 if (ret)
6325 break;
6326 left -= this_files;
6327 total += this_files;
6328 }
6329
6330 if (!ret)
6331 return 0;
6332
6333 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006334 struct file *file = io_file_from_index(ctx, total);
6335
6336 if (file)
6337 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07006338 total++;
6339 }
6340
6341 return ret;
6342}
6343#else
6344static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6345{
6346 return 0;
6347}
6348#endif
6349
Jens Axboe65e19f52019-10-26 07:20:21 -06006350static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
6351 unsigned nr_files)
6352{
6353 int i;
6354
6355 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006356 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006357 unsigned this_files;
6358
6359 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
6360 table->files = kcalloc(this_files, sizeof(struct file *),
6361 GFP_KERNEL);
6362 if (!table->files)
6363 break;
6364 nr_files -= this_files;
6365 }
6366
6367 if (i == nr_tables)
6368 return 0;
6369
6370 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006371 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006372 kfree(table->files);
6373 }
6374 return 1;
6375}
6376
Jens Axboe05f3fb32019-12-09 11:22:50 -07006377static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06006378{
6379#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06006380 struct sock *sock = ctx->ring_sock->sk;
6381 struct sk_buff_head list, *head = &sock->sk_receive_queue;
6382 struct sk_buff *skb;
6383 int i;
6384
6385 __skb_queue_head_init(&list);
6386
6387 /*
6388 * Find the skb that holds this file in its SCM_RIGHTS. When found,
6389 * remove this entry and rearrange the file array.
6390 */
6391 skb = skb_dequeue(head);
6392 while (skb) {
6393 struct scm_fp_list *fp;
6394
6395 fp = UNIXCB(skb).fp;
6396 for (i = 0; i < fp->count; i++) {
6397 int left;
6398
6399 if (fp->fp[i] != file)
6400 continue;
6401
6402 unix_notinflight(fp->user, fp->fp[i]);
6403 left = fp->count - 1 - i;
6404 if (left) {
6405 memmove(&fp->fp[i], &fp->fp[i + 1],
6406 left * sizeof(struct file *));
6407 }
6408 fp->count--;
6409 if (!fp->count) {
6410 kfree_skb(skb);
6411 skb = NULL;
6412 } else {
6413 __skb_queue_tail(&list, skb);
6414 }
6415 fput(file);
6416 file = NULL;
6417 break;
6418 }
6419
6420 if (!file)
6421 break;
6422
6423 __skb_queue_tail(&list, skb);
6424
6425 skb = skb_dequeue(head);
6426 }
6427
6428 if (skb_peek(&list)) {
6429 spin_lock_irq(&head->lock);
6430 while ((skb = __skb_dequeue(&list)) != NULL)
6431 __skb_queue_tail(head, skb);
6432 spin_unlock_irq(&head->lock);
6433 }
6434#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07006435 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06006436#endif
6437}
6438
Jens Axboe05f3fb32019-12-09 11:22:50 -07006439struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08006440 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006441 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006442};
6443
Xiaoguang Wang05589552020-03-31 14:05:18 +08006444static void io_file_put_work(struct work_struct *work)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006445{
Xiaoguang Wang05589552020-03-31 14:05:18 +08006446 struct fixed_file_ref_node *ref_node;
6447 struct fixed_file_data *file_data;
6448 struct io_ring_ctx *ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006449 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006450 unsigned long flags;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006451
Xiaoguang Wang05589552020-03-31 14:05:18 +08006452 ref_node = container_of(work, struct fixed_file_ref_node, work);
6453 file_data = ref_node->file_data;
6454 ctx = file_data->ctx;
6455
6456 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
6457 list_del_init(&pfile->list);
6458 io_ring_file_put(ctx, pfile->file);
6459 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006460 }
6461
Xiaoguang Wang05589552020-03-31 14:05:18 +08006462 spin_lock_irqsave(&file_data->lock, flags);
6463 list_del_init(&ref_node->node);
6464 spin_unlock_irqrestore(&file_data->lock, flags);
Jens Axboe2faf8522020-02-04 19:54:55 -07006465
Xiaoguang Wang05589552020-03-31 14:05:18 +08006466 percpu_ref_exit(&ref_node->refs);
6467 kfree(ref_node);
6468 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006469}
6470
6471static void io_file_data_ref_zero(struct percpu_ref *ref)
6472{
Xiaoguang Wang05589552020-03-31 14:05:18 +08006473 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006474
Xiaoguang Wang05589552020-03-31 14:05:18 +08006475 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006476
Xiaoguang Wang05589552020-03-31 14:05:18 +08006477 queue_work(system_wq, &ref_node->work);
6478}
6479
6480static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
6481 struct io_ring_ctx *ctx)
6482{
6483 struct fixed_file_ref_node *ref_node;
6484
6485 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
6486 if (!ref_node)
6487 return ERR_PTR(-ENOMEM);
6488
6489 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
6490 0, GFP_KERNEL)) {
6491 kfree(ref_node);
6492 return ERR_PTR(-ENOMEM);
6493 }
6494 INIT_LIST_HEAD(&ref_node->node);
6495 INIT_LIST_HEAD(&ref_node->file_list);
6496 INIT_WORK(&ref_node->work, io_file_put_work);
6497 ref_node->file_data = ctx->file_data;
6498 return ref_node;
6499
6500}
6501
6502static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
6503{
6504 percpu_ref_exit(&ref_node->refs);
6505 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006506}
6507
6508static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
6509 unsigned nr_args)
6510{
6511 __s32 __user *fds = (__s32 __user *) arg;
6512 unsigned nr_tables;
6513 struct file *file;
6514 int fd, ret = 0;
6515 unsigned i;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006516 struct fixed_file_ref_node *ref_node;
6517 unsigned long flags;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006518
6519 if (ctx->file_data)
6520 return -EBUSY;
6521 if (!nr_args)
6522 return -EINVAL;
6523 if (nr_args > IORING_MAX_FIXED_FILES)
6524 return -EMFILE;
6525
6526 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
6527 if (!ctx->file_data)
6528 return -ENOMEM;
6529 ctx->file_data->ctx = ctx;
6530 init_completion(&ctx->file_data->done);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006531 INIT_LIST_HEAD(&ctx->file_data->ref_list);
Xiaoguang Wangf7fe9342020-04-07 20:02:31 +08006532 spin_lock_init(&ctx->file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006533
6534 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
6535 ctx->file_data->table = kcalloc(nr_tables,
6536 sizeof(struct fixed_file_table),
6537 GFP_KERNEL);
6538 if (!ctx->file_data->table) {
6539 kfree(ctx->file_data);
6540 ctx->file_data = NULL;
6541 return -ENOMEM;
6542 }
6543
Xiaoguang Wang05589552020-03-31 14:05:18 +08006544 if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill,
Jens Axboe05f3fb32019-12-09 11:22:50 -07006545 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
6546 kfree(ctx->file_data->table);
6547 kfree(ctx->file_data);
6548 ctx->file_data = NULL;
6549 return -ENOMEM;
6550 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006551
6552 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
6553 percpu_ref_exit(&ctx->file_data->refs);
6554 kfree(ctx->file_data->table);
6555 kfree(ctx->file_data);
6556 ctx->file_data = NULL;
6557 return -ENOMEM;
6558 }
6559
6560 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
6561 struct fixed_file_table *table;
6562 unsigned index;
6563
6564 ret = -EFAULT;
6565 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
6566 break;
6567 /* allow sparse sets */
6568 if (fd == -1) {
6569 ret = 0;
6570 continue;
6571 }
6572
6573 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6574 index = i & IORING_FILE_TABLE_MASK;
6575 file = fget(fd);
6576
6577 ret = -EBADF;
6578 if (!file)
6579 break;
6580
6581 /*
6582 * Don't allow io_uring instances to be registered. If UNIX
6583 * isn't enabled, then this causes a reference cycle and this
6584 * instance can never get freed. If UNIX is enabled we'll
6585 * handle it just fine, but there's still no point in allowing
6586 * a ring fd as it doesn't support regular read/write anyway.
6587 */
6588 if (file->f_op == &io_uring_fops) {
6589 fput(file);
6590 break;
6591 }
6592 ret = 0;
6593 table->files[index] = file;
6594 }
6595
6596 if (ret) {
6597 for (i = 0; i < ctx->nr_user_files; i++) {
6598 file = io_file_from_index(ctx, i);
6599 if (file)
6600 fput(file);
6601 }
6602 for (i = 0; i < nr_tables; i++)
6603 kfree(ctx->file_data->table[i].files);
6604
6605 kfree(ctx->file_data->table);
6606 kfree(ctx->file_data);
6607 ctx->file_data = NULL;
6608 ctx->nr_user_files = 0;
6609 return ret;
6610 }
6611
6612 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006613 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006614 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006615 return ret;
6616 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006617
Xiaoguang Wang05589552020-03-31 14:05:18 +08006618 ref_node = alloc_fixed_file_ref_node(ctx);
6619 if (IS_ERR(ref_node)) {
6620 io_sqe_files_unregister(ctx);
6621 return PTR_ERR(ref_node);
6622 }
6623
6624 ctx->file_data->cur_refs = &ref_node->refs;
6625 spin_lock_irqsave(&ctx->file_data->lock, flags);
6626 list_add(&ref_node->node, &ctx->file_data->ref_list);
6627 spin_unlock_irqrestore(&ctx->file_data->lock, flags);
6628 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006629 return ret;
6630}
6631
Jens Axboec3a31e62019-10-03 13:59:56 -06006632static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
6633 int index)
6634{
6635#if defined(CONFIG_UNIX)
6636 struct sock *sock = ctx->ring_sock->sk;
6637 struct sk_buff_head *head = &sock->sk_receive_queue;
6638 struct sk_buff *skb;
6639
6640 /*
6641 * See if we can merge this file into an existing skb SCM_RIGHTS
6642 * file set. If there's no room, fall back to allocating a new skb
6643 * and filling it in.
6644 */
6645 spin_lock_irq(&head->lock);
6646 skb = skb_peek(head);
6647 if (skb) {
6648 struct scm_fp_list *fpl = UNIXCB(skb).fp;
6649
6650 if (fpl->count < SCM_MAX_FD) {
6651 __skb_unlink(skb, head);
6652 spin_unlock_irq(&head->lock);
6653 fpl->fp[fpl->count] = get_file(file);
6654 unix_inflight(fpl->user, fpl->fp[fpl->count]);
6655 fpl->count++;
6656 spin_lock_irq(&head->lock);
6657 __skb_queue_head(head, skb);
6658 } else {
6659 skb = NULL;
6660 }
6661 }
6662 spin_unlock_irq(&head->lock);
6663
6664 if (skb) {
6665 fput(file);
6666 return 0;
6667 }
6668
6669 return __io_sqe_files_scm(ctx, 1, index);
6670#else
6671 return 0;
6672#endif
6673}
6674
Hillf Dantona5318d32020-03-23 17:47:15 +08006675static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08006676 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006677{
Hillf Dantona5318d32020-03-23 17:47:15 +08006678 struct io_file_put *pfile;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006679 struct percpu_ref *refs = data->cur_refs;
6680 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006681
Jens Axboe05f3fb32019-12-09 11:22:50 -07006682 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08006683 if (!pfile)
6684 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006685
Xiaoguang Wang05589552020-03-31 14:05:18 +08006686 ref_node = container_of(refs, struct fixed_file_ref_node, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006687 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006688 list_add(&pfile->list, &ref_node->file_list);
6689
Hillf Dantona5318d32020-03-23 17:47:15 +08006690 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006691}
6692
6693static int __io_sqe_files_update(struct io_ring_ctx *ctx,
6694 struct io_uring_files_update *up,
6695 unsigned nr_args)
6696{
6697 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006698 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006699 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006700 __s32 __user *fds;
6701 int fd, i, err;
6702 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006703 unsigned long flags;
6704 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06006705
Jens Axboe05f3fb32019-12-09 11:22:50 -07006706 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06006707 return -EOVERFLOW;
6708 if (done > ctx->nr_user_files)
6709 return -EINVAL;
6710
Xiaoguang Wang05589552020-03-31 14:05:18 +08006711 ref_node = alloc_fixed_file_ref_node(ctx);
6712 if (IS_ERR(ref_node))
6713 return PTR_ERR(ref_node);
6714
Jens Axboec3a31e62019-10-03 13:59:56 -06006715 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006716 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06006717 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006718 struct fixed_file_table *table;
6719 unsigned index;
6720
Jens Axboec3a31e62019-10-03 13:59:56 -06006721 err = 0;
6722 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
6723 err = -EFAULT;
6724 break;
6725 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006726 i = array_index_nospec(up->offset, ctx->nr_user_files);
6727 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06006728 index = i & IORING_FILE_TABLE_MASK;
6729 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006730 file = io_file_from_index(ctx, index);
Hillf Dantona5318d32020-03-23 17:47:15 +08006731 err = io_queue_file_removal(data, file);
6732 if (err)
6733 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06006734 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006735 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06006736 }
6737 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06006738 file = fget(fd);
6739 if (!file) {
6740 err = -EBADF;
6741 break;
6742 }
6743 /*
6744 * Don't allow io_uring instances to be registered. If
6745 * UNIX isn't enabled, then this causes a reference
6746 * cycle and this instance can never get freed. If UNIX
6747 * is enabled we'll handle it just fine, but there's
6748 * still no point in allowing a ring fd as it doesn't
6749 * support regular read/write anyway.
6750 */
6751 if (file->f_op == &io_uring_fops) {
6752 fput(file);
6753 err = -EBADF;
6754 break;
6755 }
Jens Axboe65e19f52019-10-26 07:20:21 -06006756 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006757 err = io_sqe_file_register(ctx, file, i);
6758 if (err)
6759 break;
6760 }
6761 nr_args--;
6762 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006763 up->offset++;
6764 }
6765
Xiaoguang Wang05589552020-03-31 14:05:18 +08006766 if (needs_switch) {
6767 percpu_ref_kill(data->cur_refs);
6768 spin_lock_irqsave(&data->lock, flags);
6769 list_add(&ref_node->node, &data->ref_list);
6770 data->cur_refs = &ref_node->refs;
6771 spin_unlock_irqrestore(&data->lock, flags);
6772 percpu_ref_get(&ctx->file_data->refs);
6773 } else
6774 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06006775
6776 return done ? done : err;
6777}
Xiaoguang Wang05589552020-03-31 14:05:18 +08006778
Jens Axboe05f3fb32019-12-09 11:22:50 -07006779static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
6780 unsigned nr_args)
6781{
6782 struct io_uring_files_update up;
6783
6784 if (!ctx->file_data)
6785 return -ENXIO;
6786 if (!nr_args)
6787 return -EINVAL;
6788 if (copy_from_user(&up, arg, sizeof(up)))
6789 return -EFAULT;
6790 if (up.resv)
6791 return -EINVAL;
6792
6793 return __io_sqe_files_update(ctx, &up, nr_args);
6794}
Jens Axboec3a31e62019-10-03 13:59:56 -06006795
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006796static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07006797{
6798 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6799
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006800 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07006801 io_put_req(req);
6802}
6803
Pavel Begunkov24369c22020-01-28 03:15:48 +03006804static int io_init_wq_offload(struct io_ring_ctx *ctx,
6805 struct io_uring_params *p)
6806{
6807 struct io_wq_data data;
6808 struct fd f;
6809 struct io_ring_ctx *ctx_attach;
6810 unsigned int concurrency;
6811 int ret = 0;
6812
6813 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006814 data.free_work = io_free_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03006815
6816 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
6817 /* Do QD, or 4 * CPUS, whatever is smallest */
6818 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
6819
6820 ctx->io_wq = io_wq_create(concurrency, &data);
6821 if (IS_ERR(ctx->io_wq)) {
6822 ret = PTR_ERR(ctx->io_wq);
6823 ctx->io_wq = NULL;
6824 }
6825 return ret;
6826 }
6827
6828 f = fdget(p->wq_fd);
6829 if (!f.file)
6830 return -EBADF;
6831
6832 if (f.file->f_op != &io_uring_fops) {
6833 ret = -EINVAL;
6834 goto out_fput;
6835 }
6836
6837 ctx_attach = f.file->private_data;
6838 /* @io_wq is protected by holding the fd */
6839 if (!io_wq_get(ctx_attach->io_wq, &data)) {
6840 ret = -EINVAL;
6841 goto out_fput;
6842 }
6843
6844 ctx->io_wq = ctx_attach->io_wq;
6845out_fput:
6846 fdput(f);
6847 return ret;
6848}
6849
Jens Axboe6c271ce2019-01-10 11:22:30 -07006850static int io_sq_offload_start(struct io_ring_ctx *ctx,
6851 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006852{
6853 int ret;
6854
Jens Axboe6c271ce2019-01-10 11:22:30 -07006855 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006856 mmgrab(current->mm);
6857 ctx->sqo_mm = current->mm;
6858
Jens Axboe6c271ce2019-01-10 11:22:30 -07006859 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06006860 ret = -EPERM;
6861 if (!capable(CAP_SYS_ADMIN))
6862 goto err;
6863
Jens Axboe917257d2019-04-13 09:28:55 -06006864 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
6865 if (!ctx->sq_thread_idle)
6866 ctx->sq_thread_idle = HZ;
6867
Jens Axboe6c271ce2019-01-10 11:22:30 -07006868 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06006869 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006870
Jens Axboe917257d2019-04-13 09:28:55 -06006871 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06006872 if (cpu >= nr_cpu_ids)
6873 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08006874 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06006875 goto err;
6876
Jens Axboe6c271ce2019-01-10 11:22:30 -07006877 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
6878 ctx, cpu,
6879 "io_uring-sq");
6880 } else {
6881 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
6882 "io_uring-sq");
6883 }
6884 if (IS_ERR(ctx->sqo_thread)) {
6885 ret = PTR_ERR(ctx->sqo_thread);
6886 ctx->sqo_thread = NULL;
6887 goto err;
6888 }
6889 wake_up_process(ctx->sqo_thread);
6890 } else if (p->flags & IORING_SETUP_SQ_AFF) {
6891 /* Can't have SQ_AFF without SQPOLL */
6892 ret = -EINVAL;
6893 goto err;
6894 }
6895
Pavel Begunkov24369c22020-01-28 03:15:48 +03006896 ret = io_init_wq_offload(ctx, p);
6897 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006898 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006899
6900 return 0;
6901err:
Jens Axboe54a91f32019-09-10 09:15:04 -06006902 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006903 mmdrop(ctx->sqo_mm);
6904 ctx->sqo_mm = NULL;
6905 return ret;
6906}
6907
6908static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
6909{
6910 atomic_long_sub(nr_pages, &user->locked_vm);
6911}
6912
6913static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
6914{
6915 unsigned long page_limit, cur_pages, new_pages;
6916
6917 /* Don't allow more pages than we can safely lock */
6918 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
6919
6920 do {
6921 cur_pages = atomic_long_read(&user->locked_vm);
6922 new_pages = cur_pages + nr_pages;
6923 if (new_pages > page_limit)
6924 return -ENOMEM;
6925 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
6926 new_pages) != cur_pages);
6927
6928 return 0;
6929}
6930
6931static void io_mem_free(void *ptr)
6932{
Mark Rutland52e04ef2019-04-30 17:30:21 +01006933 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006934
Mark Rutland52e04ef2019-04-30 17:30:21 +01006935 if (!ptr)
6936 return;
6937
6938 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006939 if (put_page_testzero(page))
6940 free_compound_page(page);
6941}
6942
6943static void *io_mem_alloc(size_t size)
6944{
6945 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
6946 __GFP_NORETRY;
6947
6948 return (void *) __get_free_pages(gfp_flags, get_order(size));
6949}
6950
Hristo Venev75b28af2019-08-26 17:23:46 +00006951static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
6952 size_t *sq_offset)
6953{
6954 struct io_rings *rings;
6955 size_t off, sq_array_size;
6956
6957 off = struct_size(rings, cqes, cq_entries);
6958 if (off == SIZE_MAX)
6959 return SIZE_MAX;
6960
6961#ifdef CONFIG_SMP
6962 off = ALIGN(off, SMP_CACHE_BYTES);
6963 if (off == 0)
6964 return SIZE_MAX;
6965#endif
6966
6967 sq_array_size = array_size(sizeof(u32), sq_entries);
6968 if (sq_array_size == SIZE_MAX)
6969 return SIZE_MAX;
6970
6971 if (check_add_overflow(off, sq_array_size, &off))
6972 return SIZE_MAX;
6973
6974 if (sq_offset)
6975 *sq_offset = off;
6976
6977 return off;
6978}
6979
Jens Axboe2b188cc2019-01-07 10:46:33 -07006980static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
6981{
Hristo Venev75b28af2019-08-26 17:23:46 +00006982 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006983
Hristo Venev75b28af2019-08-26 17:23:46 +00006984 pages = (size_t)1 << get_order(
6985 rings_size(sq_entries, cq_entries, NULL));
6986 pages += (size_t)1 << get_order(
6987 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07006988
Hristo Venev75b28af2019-08-26 17:23:46 +00006989 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006990}
6991
Jens Axboeedafcce2019-01-09 09:16:05 -07006992static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
6993{
6994 int i, j;
6995
6996 if (!ctx->user_bufs)
6997 return -ENXIO;
6998
6999 for (i = 0; i < ctx->nr_user_bufs; i++) {
7000 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7001
7002 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007003 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07007004
7005 if (ctx->account_mem)
7006 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007007 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007008 imu->nr_bvecs = 0;
7009 }
7010
7011 kfree(ctx->user_bufs);
7012 ctx->user_bufs = NULL;
7013 ctx->nr_user_bufs = 0;
7014 return 0;
7015}
7016
7017static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
7018 void __user *arg, unsigned index)
7019{
7020 struct iovec __user *src;
7021
7022#ifdef CONFIG_COMPAT
7023 if (ctx->compat) {
7024 struct compat_iovec __user *ciovs;
7025 struct compat_iovec ciov;
7026
7027 ciovs = (struct compat_iovec __user *) arg;
7028 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
7029 return -EFAULT;
7030
Jens Axboed55e5f52019-12-11 16:12:15 -07007031 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07007032 dst->iov_len = ciov.iov_len;
7033 return 0;
7034 }
7035#endif
7036 src = (struct iovec __user *) arg;
7037 if (copy_from_user(dst, &src[index], sizeof(*dst)))
7038 return -EFAULT;
7039 return 0;
7040}
7041
7042static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
7043 unsigned nr_args)
7044{
7045 struct vm_area_struct **vmas = NULL;
7046 struct page **pages = NULL;
7047 int i, j, got_pages = 0;
7048 int ret = -EINVAL;
7049
7050 if (ctx->user_bufs)
7051 return -EBUSY;
7052 if (!nr_args || nr_args > UIO_MAXIOV)
7053 return -EINVAL;
7054
7055 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
7056 GFP_KERNEL);
7057 if (!ctx->user_bufs)
7058 return -ENOMEM;
7059
7060 for (i = 0; i < nr_args; i++) {
7061 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7062 unsigned long off, start, end, ubuf;
7063 int pret, nr_pages;
7064 struct iovec iov;
7065 size_t size;
7066
7067 ret = io_copy_iov(ctx, &iov, arg, i);
7068 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03007069 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007070
7071 /*
7072 * Don't impose further limits on the size and buffer
7073 * constraints here, we'll -EINVAL later when IO is
7074 * submitted if they are wrong.
7075 */
7076 ret = -EFAULT;
7077 if (!iov.iov_base || !iov.iov_len)
7078 goto err;
7079
7080 /* arbitrary limit, but we need something */
7081 if (iov.iov_len > SZ_1G)
7082 goto err;
7083
7084 ubuf = (unsigned long) iov.iov_base;
7085 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
7086 start = ubuf >> PAGE_SHIFT;
7087 nr_pages = end - start;
7088
7089 if (ctx->account_mem) {
7090 ret = io_account_mem(ctx->user, nr_pages);
7091 if (ret)
7092 goto err;
7093 }
7094
7095 ret = 0;
7096 if (!pages || nr_pages > got_pages) {
7097 kfree(vmas);
7098 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007099 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07007100 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007101 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07007102 sizeof(struct vm_area_struct *),
7103 GFP_KERNEL);
7104 if (!pages || !vmas) {
7105 ret = -ENOMEM;
7106 if (ctx->account_mem)
7107 io_unaccount_mem(ctx->user, nr_pages);
7108 goto err;
7109 }
7110 got_pages = nr_pages;
7111 }
7112
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007113 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07007114 GFP_KERNEL);
7115 ret = -ENOMEM;
7116 if (!imu->bvec) {
7117 if (ctx->account_mem)
7118 io_unaccount_mem(ctx->user, nr_pages);
7119 goto err;
7120 }
7121
7122 ret = 0;
7123 down_read(&current->mm->mmap_sem);
John Hubbard2113b052020-01-30 22:13:13 -08007124 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07007125 FOLL_WRITE | FOLL_LONGTERM,
7126 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007127 if (pret == nr_pages) {
7128 /* don't support file backed memory */
7129 for (j = 0; j < nr_pages; j++) {
7130 struct vm_area_struct *vma = vmas[j];
7131
7132 if (vma->vm_file &&
7133 !is_file_hugepages(vma->vm_file)) {
7134 ret = -EOPNOTSUPP;
7135 break;
7136 }
7137 }
7138 } else {
7139 ret = pret < 0 ? pret : -EFAULT;
7140 }
7141 up_read(&current->mm->mmap_sem);
7142 if (ret) {
7143 /*
7144 * if we did partial map, or found file backed vmas,
7145 * release any pages we did get
7146 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07007147 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007148 unpin_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07007149 if (ctx->account_mem)
7150 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007151 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007152 goto err;
7153 }
7154
7155 off = ubuf & ~PAGE_MASK;
7156 size = iov.iov_len;
7157 for (j = 0; j < nr_pages; j++) {
7158 size_t vec_len;
7159
7160 vec_len = min_t(size_t, size, PAGE_SIZE - off);
7161 imu->bvec[j].bv_page = pages[j];
7162 imu->bvec[j].bv_len = vec_len;
7163 imu->bvec[j].bv_offset = off;
7164 off = 0;
7165 size -= vec_len;
7166 }
7167 /* store original address for later verification */
7168 imu->ubuf = ubuf;
7169 imu->len = iov.iov_len;
7170 imu->nr_bvecs = nr_pages;
7171
7172 ctx->nr_user_bufs++;
7173 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007174 kvfree(pages);
7175 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007176 return 0;
7177err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007178 kvfree(pages);
7179 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007180 io_sqe_buffer_unregister(ctx);
7181 return ret;
7182}
7183
Jens Axboe9b402842019-04-11 11:45:41 -06007184static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
7185{
7186 __s32 __user *fds = arg;
7187 int fd;
7188
7189 if (ctx->cq_ev_fd)
7190 return -EBUSY;
7191
7192 if (copy_from_user(&fd, fds, sizeof(*fds)))
7193 return -EFAULT;
7194
7195 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
7196 if (IS_ERR(ctx->cq_ev_fd)) {
7197 int ret = PTR_ERR(ctx->cq_ev_fd);
7198 ctx->cq_ev_fd = NULL;
7199 return ret;
7200 }
7201
7202 return 0;
7203}
7204
7205static int io_eventfd_unregister(struct io_ring_ctx *ctx)
7206{
7207 if (ctx->cq_ev_fd) {
7208 eventfd_ctx_put(ctx->cq_ev_fd);
7209 ctx->cq_ev_fd = NULL;
7210 return 0;
7211 }
7212
7213 return -ENXIO;
7214}
7215
Jens Axboe5a2e7452020-02-23 16:23:11 -07007216static int __io_destroy_buffers(int id, void *p, void *data)
7217{
7218 struct io_ring_ctx *ctx = data;
7219 struct io_buffer *buf = p;
7220
Jens Axboe067524e2020-03-02 16:32:28 -07007221 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007222 return 0;
7223}
7224
7225static void io_destroy_buffers(struct io_ring_ctx *ctx)
7226{
7227 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
7228 idr_destroy(&ctx->io_buffer_idr);
7229}
7230
Jens Axboe2b188cc2019-01-07 10:46:33 -07007231static void io_ring_ctx_free(struct io_ring_ctx *ctx)
7232{
Jens Axboe6b063142019-01-10 22:13:58 -07007233 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007234 if (ctx->sqo_mm)
7235 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07007236
7237 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07007238 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07007239 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06007240 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007241 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07007242 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07007243
Jens Axboe2b188cc2019-01-07 10:46:33 -07007244#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07007245 if (ctx->ring_sock) {
7246 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007247 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07007248 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007249#endif
7250
Hristo Venev75b28af2019-08-26 17:23:46 +00007251 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007252 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007253
7254 percpu_ref_exit(&ctx->refs);
7255 if (ctx->account_mem)
7256 io_unaccount_mem(ctx->user,
7257 ring_pages(ctx->sq_entries, ctx->cq_entries));
7258 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07007259 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07007260 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07007261 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07007262 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007263 kfree(ctx);
7264}
7265
7266static __poll_t io_uring_poll(struct file *file, poll_table *wait)
7267{
7268 struct io_ring_ctx *ctx = file->private_data;
7269 __poll_t mask = 0;
7270
7271 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02007272 /*
7273 * synchronizes with barrier from wq_has_sleeper call in
7274 * io_commit_cqring
7275 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007276 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00007277 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
7278 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007279 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01007280 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007281 mask |= EPOLLIN | EPOLLRDNORM;
7282
7283 return mask;
7284}
7285
7286static int io_uring_fasync(int fd, struct file *file, int on)
7287{
7288 struct io_ring_ctx *ctx = file->private_data;
7289
7290 return fasync_helper(fd, file, on, &ctx->cq_fasync);
7291}
7292
Jens Axboe071698e2020-01-28 10:04:42 -07007293static int io_remove_personalities(int id, void *p, void *data)
7294{
7295 struct io_ring_ctx *ctx = data;
7296 const struct cred *cred;
7297
7298 cred = idr_remove(&ctx->personality_idr, id);
7299 if (cred)
7300 put_cred(cred);
7301 return 0;
7302}
7303
Jens Axboe85faa7b2020-04-09 18:14:00 -06007304static void io_ring_exit_work(struct work_struct *work)
7305{
7306 struct io_ring_ctx *ctx;
7307
7308 ctx = container_of(work, struct io_ring_ctx, exit_work);
7309 if (ctx->rings)
7310 io_cqring_overflow_flush(ctx, true);
7311
7312 wait_for_completion(&ctx->completions[0]);
7313 io_ring_ctx_free(ctx);
7314}
7315
Jens Axboe2b188cc2019-01-07 10:46:33 -07007316static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
7317{
7318 mutex_lock(&ctx->uring_lock);
7319 percpu_ref_kill(&ctx->refs);
7320 mutex_unlock(&ctx->uring_lock);
7321
Jens Axboedf069d82020-02-04 16:48:34 -07007322 /*
7323 * Wait for sq thread to idle, if we have one. It won't spin on new
7324 * work after we've killed the ctx ref above. This is important to do
7325 * before we cancel existing commands, as the thread could otherwise
7326 * be queueing new work post that. If that's work we need to cancel,
7327 * it could cause shutdown to hang.
7328 */
7329 while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait))
7330 cpu_relax();
7331
Jens Axboe5262f562019-09-17 12:26:57 -06007332 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07007333 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06007334
7335 if (ctx->io_wq)
7336 io_wq_cancel_all(ctx->io_wq);
7337
Jens Axboedef596e2019-01-09 08:59:42 -07007338 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07007339 /* if we failed setting up the ctx, we might not have any rings */
7340 if (ctx->rings)
7341 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07007342 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe85faa7b2020-04-09 18:14:00 -06007343 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
7344 queue_work(system_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007345}
7346
7347static int io_uring_release(struct inode *inode, struct file *file)
7348{
7349 struct io_ring_ctx *ctx = file->private_data;
7350
7351 file->private_data = NULL;
7352 io_ring_ctx_wait_and_kill(ctx);
7353 return 0;
7354}
7355
Jens Axboefcb323c2019-10-24 12:39:47 -06007356static void io_uring_cancel_files(struct io_ring_ctx *ctx,
7357 struct files_struct *files)
7358{
7359 struct io_kiocb *req;
7360 DEFINE_WAIT(wait);
7361
7362 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07007363 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06007364
7365 spin_lock_irq(&ctx->inflight_lock);
7366 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07007367 if (req->work.files != files)
7368 continue;
7369 /* req is being completed, ignore */
7370 if (!refcount_inc_not_zero(&req->refs))
7371 continue;
7372 cancel_req = req;
7373 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06007374 }
Jens Axboe768134d2019-11-10 20:30:53 -07007375 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007376 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07007377 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06007378 spin_unlock_irq(&ctx->inflight_lock);
7379
Jens Axboe768134d2019-11-10 20:30:53 -07007380 /* We need to keep going until we don't find a matching req */
7381 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007382 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08007383
Jens Axboe2ca10252020-02-13 17:17:35 -07007384 if (cancel_req->flags & REQ_F_OVERFLOW) {
7385 spin_lock_irq(&ctx->completion_lock);
7386 list_del(&cancel_req->list);
7387 cancel_req->flags &= ~REQ_F_OVERFLOW;
7388 if (list_empty(&ctx->cq_overflow_list)) {
7389 clear_bit(0, &ctx->sq_check_overflow);
7390 clear_bit(0, &ctx->cq_check_overflow);
7391 }
7392 spin_unlock_irq(&ctx->completion_lock);
7393
7394 WRITE_ONCE(ctx->rings->cq_overflow,
7395 atomic_inc_return(&ctx->cached_cq_overflow));
7396
7397 /*
7398 * Put inflight ref and overflow ref. If that's
7399 * all we had, then we're done with this request.
7400 */
7401 if (refcount_sub_and_test(2, &cancel_req->refs)) {
7402 io_put_req(cancel_req);
7403 continue;
7404 }
7405 }
7406
Bob Liu2f6d9b92019-11-13 18:06:24 +08007407 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
7408 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06007409 schedule();
7410 }
Jens Axboe768134d2019-11-10 20:30:53 -07007411 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007412}
7413
7414static int io_uring_flush(struct file *file, void *data)
7415{
7416 struct io_ring_ctx *ctx = file->private_data;
7417
7418 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07007419
7420 /*
7421 * If the task is going away, cancel work it may have pending
7422 */
7423 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
7424 io_wq_cancel_pid(ctx->io_wq, task_pid_vnr(current));
7425
Jens Axboefcb323c2019-10-24 12:39:47 -06007426 return 0;
7427}
7428
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007429static void *io_uring_validate_mmap_request(struct file *file,
7430 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007431{
Jens Axboe2b188cc2019-01-07 10:46:33 -07007432 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007433 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007434 struct page *page;
7435 void *ptr;
7436
7437 switch (offset) {
7438 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00007439 case IORING_OFF_CQ_RING:
7440 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007441 break;
7442 case IORING_OFF_SQES:
7443 ptr = ctx->sq_sqes;
7444 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007445 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007446 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007447 }
7448
7449 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07007450 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007451 return ERR_PTR(-EINVAL);
7452
7453 return ptr;
7454}
7455
7456#ifdef CONFIG_MMU
7457
7458static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7459{
7460 size_t sz = vma->vm_end - vma->vm_start;
7461 unsigned long pfn;
7462 void *ptr;
7463
7464 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
7465 if (IS_ERR(ptr))
7466 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007467
7468 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
7469 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
7470}
7471
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007472#else /* !CONFIG_MMU */
7473
7474static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7475{
7476 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
7477}
7478
7479static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
7480{
7481 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
7482}
7483
7484static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
7485 unsigned long addr, unsigned long len,
7486 unsigned long pgoff, unsigned long flags)
7487{
7488 void *ptr;
7489
7490 ptr = io_uring_validate_mmap_request(file, pgoff, len);
7491 if (IS_ERR(ptr))
7492 return PTR_ERR(ptr);
7493
7494 return (unsigned long) ptr;
7495}
7496
7497#endif /* !CONFIG_MMU */
7498
Jens Axboe2b188cc2019-01-07 10:46:33 -07007499SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
7500 u32, min_complete, u32, flags, const sigset_t __user *, sig,
7501 size_t, sigsz)
7502{
7503 struct io_ring_ctx *ctx;
7504 long ret = -EBADF;
7505 int submitted = 0;
7506 struct fd f;
7507
Jens Axboeb41e9852020-02-17 09:52:41 -07007508 if (current->task_works)
7509 task_work_run();
7510
Jens Axboe6c271ce2019-01-10 11:22:30 -07007511 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007512 return -EINVAL;
7513
7514 f = fdget(fd);
7515 if (!f.file)
7516 return -EBADF;
7517
7518 ret = -EOPNOTSUPP;
7519 if (f.file->f_op != &io_uring_fops)
7520 goto out_fput;
7521
7522 ret = -ENXIO;
7523 ctx = f.file->private_data;
7524 if (!percpu_ref_tryget(&ctx->refs))
7525 goto out_fput;
7526
Jens Axboe6c271ce2019-01-10 11:22:30 -07007527 /*
7528 * For SQ polling, the thread will do all submissions and completions.
7529 * Just return the requested submit count, and wake the thread if
7530 * we were asked to.
7531 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007532 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007533 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07007534 if (!list_empty_careful(&ctx->cq_overflow_list))
7535 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007536 if (flags & IORING_ENTER_SQ_WAKEUP)
7537 wake_up(&ctx->sqo_wait);
7538 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007539 } else if (to_submit) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07007540 mutex_lock(&ctx->uring_lock);
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03007541 submitted = io_submit_sqes(ctx, to_submit, f.file, fd, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007542 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007543
7544 if (submitted != to_submit)
7545 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007546 }
7547 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07007548 unsigned nr_events = 0;
7549
Jens Axboe2b188cc2019-01-07 10:46:33 -07007550 min_complete = min(min_complete, ctx->cq_entries);
7551
Xiaoguang Wang32b22442020-03-11 09:26:09 +08007552 /*
7553 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
7554 * space applications don't need to do io completion events
7555 * polling again, they can rely on io_sq_thread to do polling
7556 * work, which can reduce cpu usage and uring_lock contention.
7557 */
7558 if (ctx->flags & IORING_SETUP_IOPOLL &&
7559 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Jens Axboedef596e2019-01-09 08:59:42 -07007560 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07007561 } else {
7562 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
7563 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007564 }
7565
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007566out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03007567 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007568out_fput:
7569 fdput(f);
7570 return submitted ? submitted : ret;
7571}
7572
Tobias Klauserbebdb652020-02-26 18:38:32 +01007573#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07007574static int io_uring_show_cred(int id, void *p, void *data)
7575{
7576 const struct cred *cred = p;
7577 struct seq_file *m = data;
7578 struct user_namespace *uns = seq_user_ns(m);
7579 struct group_info *gi;
7580 kernel_cap_t cap;
7581 unsigned __capi;
7582 int g;
7583
7584 seq_printf(m, "%5d\n", id);
7585 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
7586 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
7587 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
7588 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
7589 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
7590 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
7591 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
7592 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
7593 seq_puts(m, "\n\tGroups:\t");
7594 gi = cred->group_info;
7595 for (g = 0; g < gi->ngroups; g++) {
7596 seq_put_decimal_ull(m, g ? " " : "",
7597 from_kgid_munged(uns, gi->gid[g]));
7598 }
7599 seq_puts(m, "\n\tCapEff:\t");
7600 cap = cred->cap_effective;
7601 CAP_FOR_EACH_U32(__capi)
7602 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
7603 seq_putc(m, '\n');
7604 return 0;
7605}
7606
7607static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
7608{
7609 int i;
7610
7611 mutex_lock(&ctx->uring_lock);
7612 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
7613 for (i = 0; i < ctx->nr_user_files; i++) {
7614 struct fixed_file_table *table;
7615 struct file *f;
7616
7617 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7618 f = table->files[i & IORING_FILE_TABLE_MASK];
7619 if (f)
7620 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
7621 else
7622 seq_printf(m, "%5u: <none>\n", i);
7623 }
7624 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
7625 for (i = 0; i < ctx->nr_user_bufs; i++) {
7626 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
7627
7628 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
7629 (unsigned int) buf->len);
7630 }
7631 if (!idr_is_empty(&ctx->personality_idr)) {
7632 seq_printf(m, "Personalities:\n");
7633 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
7634 }
Jens Axboed7718a92020-02-14 22:23:12 -07007635 seq_printf(m, "PollList:\n");
7636 spin_lock_irq(&ctx->completion_lock);
7637 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
7638 struct hlist_head *list = &ctx->cancel_hash[i];
7639 struct io_kiocb *req;
7640
7641 hlist_for_each_entry(req, list, hash_node)
7642 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
7643 req->task->task_works != NULL);
7644 }
7645 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07007646 mutex_unlock(&ctx->uring_lock);
7647}
7648
7649static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
7650{
7651 struct io_ring_ctx *ctx = f->private_data;
7652
7653 if (percpu_ref_tryget(&ctx->refs)) {
7654 __io_uring_show_fdinfo(ctx, m);
7655 percpu_ref_put(&ctx->refs);
7656 }
7657}
Tobias Klauserbebdb652020-02-26 18:38:32 +01007658#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07007659
Jens Axboe2b188cc2019-01-07 10:46:33 -07007660static const struct file_operations io_uring_fops = {
7661 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06007662 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07007663 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007664#ifndef CONFIG_MMU
7665 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
7666 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
7667#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07007668 .poll = io_uring_poll,
7669 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01007670#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07007671 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01007672#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07007673};
7674
7675static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
7676 struct io_uring_params *p)
7677{
Hristo Venev75b28af2019-08-26 17:23:46 +00007678 struct io_rings *rings;
7679 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007680
Hristo Venev75b28af2019-08-26 17:23:46 +00007681 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
7682 if (size == SIZE_MAX)
7683 return -EOVERFLOW;
7684
7685 rings = io_mem_alloc(size);
7686 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007687 return -ENOMEM;
7688
Hristo Venev75b28af2019-08-26 17:23:46 +00007689 ctx->rings = rings;
7690 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
7691 rings->sq_ring_mask = p->sq_entries - 1;
7692 rings->cq_ring_mask = p->cq_entries - 1;
7693 rings->sq_ring_entries = p->sq_entries;
7694 rings->cq_ring_entries = p->cq_entries;
7695 ctx->sq_mask = rings->sq_ring_mask;
7696 ctx->cq_mask = rings->cq_ring_mask;
7697 ctx->sq_entries = rings->sq_ring_entries;
7698 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007699
7700 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07007701 if (size == SIZE_MAX) {
7702 io_mem_free(ctx->rings);
7703 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007704 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07007705 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007706
7707 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07007708 if (!ctx->sq_sqes) {
7709 io_mem_free(ctx->rings);
7710 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007711 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07007712 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007713
Jens Axboe2b188cc2019-01-07 10:46:33 -07007714 return 0;
7715}
7716
7717/*
7718 * Allocate an anonymous fd, this is what constitutes the application
7719 * visible backing of an io_uring instance. The application mmaps this
7720 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
7721 * we have to tie this fd to a socket for file garbage collection purposes.
7722 */
7723static int io_uring_get_fd(struct io_ring_ctx *ctx)
7724{
7725 struct file *file;
7726 int ret;
7727
7728#if defined(CONFIG_UNIX)
7729 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
7730 &ctx->ring_sock);
7731 if (ret)
7732 return ret;
7733#endif
7734
7735 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
7736 if (ret < 0)
7737 goto err;
7738
7739 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
7740 O_RDWR | O_CLOEXEC);
7741 if (IS_ERR(file)) {
7742 put_unused_fd(ret);
7743 ret = PTR_ERR(file);
7744 goto err;
7745 }
7746
7747#if defined(CONFIG_UNIX)
7748 ctx->ring_sock->file = file;
7749#endif
7750 fd_install(ret, file);
7751 return ret;
7752err:
7753#if defined(CONFIG_UNIX)
7754 sock_release(ctx->ring_sock);
7755 ctx->ring_sock = NULL;
7756#endif
7757 return ret;
7758}
7759
7760static int io_uring_create(unsigned entries, struct io_uring_params *p)
7761{
7762 struct user_struct *user = NULL;
7763 struct io_ring_ctx *ctx;
7764 bool account_mem;
7765 int ret;
7766
Jens Axboe8110c1a2019-12-28 15:39:54 -07007767 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007768 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07007769 if (entries > IORING_MAX_ENTRIES) {
7770 if (!(p->flags & IORING_SETUP_CLAMP))
7771 return -EINVAL;
7772 entries = IORING_MAX_ENTRIES;
7773 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007774
7775 /*
7776 * Use twice as many entries for the CQ ring. It's possible for the
7777 * application to drive a higher depth than the size of the SQ ring,
7778 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06007779 * some flexibility in overcommitting a bit. If the application has
7780 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
7781 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07007782 */
7783 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06007784 if (p->flags & IORING_SETUP_CQSIZE) {
7785 /*
7786 * If IORING_SETUP_CQSIZE is set, we do the same roundup
7787 * to a power-of-two, if it isn't already. We do NOT impose
7788 * any cq vs sq ring sizing.
7789 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07007790 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06007791 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07007792 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
7793 if (!(p->flags & IORING_SETUP_CLAMP))
7794 return -EINVAL;
7795 p->cq_entries = IORING_MAX_CQ_ENTRIES;
7796 }
Jens Axboe33a107f2019-10-04 12:10:03 -06007797 p->cq_entries = roundup_pow_of_two(p->cq_entries);
7798 } else {
7799 p->cq_entries = 2 * p->sq_entries;
7800 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007801
7802 user = get_uid(current_user());
7803 account_mem = !capable(CAP_IPC_LOCK);
7804
7805 if (account_mem) {
7806 ret = io_account_mem(user,
7807 ring_pages(p->sq_entries, p->cq_entries));
7808 if (ret) {
7809 free_uid(user);
7810 return ret;
7811 }
7812 }
7813
7814 ctx = io_ring_ctx_alloc(p);
7815 if (!ctx) {
7816 if (account_mem)
7817 io_unaccount_mem(user, ring_pages(p->sq_entries,
7818 p->cq_entries));
7819 free_uid(user);
7820 return -ENOMEM;
7821 }
7822 ctx->compat = in_compat_syscall();
7823 ctx->account_mem = account_mem;
7824 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07007825 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07007826
7827 ret = io_allocate_scq_urings(ctx, p);
7828 if (ret)
7829 goto err;
7830
Jens Axboe6c271ce2019-01-10 11:22:30 -07007831 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007832 if (ret)
7833 goto err;
7834
Jens Axboe2b188cc2019-01-07 10:46:33 -07007835 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007836 p->sq_off.head = offsetof(struct io_rings, sq.head);
7837 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
7838 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
7839 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
7840 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
7841 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
7842 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007843
7844 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007845 p->cq_off.head = offsetof(struct io_rings, cq.head);
7846 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
7847 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
7848 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
7849 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
7850 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06007851
Jens Axboe044c1ab2019-10-28 09:15:33 -06007852 /*
7853 * Install ring fd as the very last thing, so we don't risk someone
7854 * having closed it before we finish setup
7855 */
7856 ret = io_uring_get_fd(ctx);
7857 if (ret < 0)
7858 goto err;
7859
Jens Axboeda8c9692019-12-02 18:51:26 -07007860 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboecccf0ee2020-01-27 16:34:48 -07007861 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jens Axboed7718a92020-02-14 22:23:12 -07007862 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007863 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007864 return ret;
7865err:
7866 io_ring_ctx_wait_and_kill(ctx);
7867 return ret;
7868}
7869
7870/*
7871 * Sets up an aio uring context, and returns the fd. Applications asks for a
7872 * ring size, we return the actual sq/cq ring sizes (among other things) in the
7873 * params structure passed in.
7874 */
7875static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
7876{
7877 struct io_uring_params p;
7878 long ret;
7879 int i;
7880
7881 if (copy_from_user(&p, params, sizeof(p)))
7882 return -EFAULT;
7883 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
7884 if (p.resv[i])
7885 return -EINVAL;
7886 }
7887
Jens Axboe6c271ce2019-01-10 11:22:30 -07007888 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07007889 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03007890 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007891 return -EINVAL;
7892
7893 ret = io_uring_create(entries, &p);
7894 if (ret < 0)
7895 return ret;
7896
7897 if (copy_to_user(params, &p, sizeof(p)))
7898 return -EFAULT;
7899
7900 return ret;
7901}
7902
7903SYSCALL_DEFINE2(io_uring_setup, u32, entries,
7904 struct io_uring_params __user *, params)
7905{
7906 return io_uring_setup(entries, params);
7907}
7908
Jens Axboe66f4af92020-01-16 15:36:52 -07007909static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
7910{
7911 struct io_uring_probe *p;
7912 size_t size;
7913 int i, ret;
7914
7915 size = struct_size(p, ops, nr_args);
7916 if (size == SIZE_MAX)
7917 return -EOVERFLOW;
7918 p = kzalloc(size, GFP_KERNEL);
7919 if (!p)
7920 return -ENOMEM;
7921
7922 ret = -EFAULT;
7923 if (copy_from_user(p, arg, size))
7924 goto out;
7925 ret = -EINVAL;
7926 if (memchr_inv(p, 0, size))
7927 goto out;
7928
7929 p->last_op = IORING_OP_LAST - 1;
7930 if (nr_args > IORING_OP_LAST)
7931 nr_args = IORING_OP_LAST;
7932
7933 for (i = 0; i < nr_args; i++) {
7934 p->ops[i].op = i;
7935 if (!io_op_defs[i].not_supported)
7936 p->ops[i].flags = IO_URING_OP_SUPPORTED;
7937 }
7938 p->ops_len = i;
7939
7940 ret = 0;
7941 if (copy_to_user(arg, p, size))
7942 ret = -EFAULT;
7943out:
7944 kfree(p);
7945 return ret;
7946}
7947
Jens Axboe071698e2020-01-28 10:04:42 -07007948static int io_register_personality(struct io_ring_ctx *ctx)
7949{
7950 const struct cred *creds = get_current_cred();
7951 int id;
7952
7953 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
7954 USHRT_MAX, GFP_KERNEL);
7955 if (id < 0)
7956 put_cred(creds);
7957 return id;
7958}
7959
7960static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
7961{
7962 const struct cred *old_creds;
7963
7964 old_creds = idr_remove(&ctx->personality_idr, id);
7965 if (old_creds) {
7966 put_cred(old_creds);
7967 return 0;
7968 }
7969
7970 return -EINVAL;
7971}
7972
7973static bool io_register_op_must_quiesce(int op)
7974{
7975 switch (op) {
7976 case IORING_UNREGISTER_FILES:
7977 case IORING_REGISTER_FILES_UPDATE:
7978 case IORING_REGISTER_PROBE:
7979 case IORING_REGISTER_PERSONALITY:
7980 case IORING_UNREGISTER_PERSONALITY:
7981 return false;
7982 default:
7983 return true;
7984 }
7985}
7986
Jens Axboeedafcce2019-01-09 09:16:05 -07007987static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
7988 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06007989 __releases(ctx->uring_lock)
7990 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07007991{
7992 int ret;
7993
Jens Axboe35fa71a2019-04-22 10:23:23 -06007994 /*
7995 * We're inside the ring mutex, if the ref is already dying, then
7996 * someone else killed the ctx or is already going through
7997 * io_uring_register().
7998 */
7999 if (percpu_ref_is_dying(&ctx->refs))
8000 return -ENXIO;
8001
Jens Axboe071698e2020-01-28 10:04:42 -07008002 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008003 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06008004
Jens Axboe05f3fb32019-12-09 11:22:50 -07008005 /*
8006 * Drop uring mutex before waiting for references to exit. If
8007 * another thread is currently inside io_uring_enter() it might
8008 * need to grab the uring_lock to make progress. If we hold it
8009 * here across the drain wait, then we can deadlock. It's safe
8010 * to drop the mutex here, since no new references will come in
8011 * after we've killed the percpu ref.
8012 */
8013 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07008014 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008015 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07008016 if (ret) {
8017 percpu_ref_resurrect(&ctx->refs);
8018 ret = -EINTR;
8019 goto out;
8020 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008021 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008022
8023 switch (opcode) {
8024 case IORING_REGISTER_BUFFERS:
8025 ret = io_sqe_buffer_register(ctx, arg, nr_args);
8026 break;
8027 case IORING_UNREGISTER_BUFFERS:
8028 ret = -EINVAL;
8029 if (arg || nr_args)
8030 break;
8031 ret = io_sqe_buffer_unregister(ctx);
8032 break;
Jens Axboe6b063142019-01-10 22:13:58 -07008033 case IORING_REGISTER_FILES:
8034 ret = io_sqe_files_register(ctx, arg, nr_args);
8035 break;
8036 case IORING_UNREGISTER_FILES:
8037 ret = -EINVAL;
8038 if (arg || nr_args)
8039 break;
8040 ret = io_sqe_files_unregister(ctx);
8041 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06008042 case IORING_REGISTER_FILES_UPDATE:
8043 ret = io_sqe_files_update(ctx, arg, nr_args);
8044 break;
Jens Axboe9b402842019-04-11 11:45:41 -06008045 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07008046 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06008047 ret = -EINVAL;
8048 if (nr_args != 1)
8049 break;
8050 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07008051 if (ret)
8052 break;
8053 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
8054 ctx->eventfd_async = 1;
8055 else
8056 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06008057 break;
8058 case IORING_UNREGISTER_EVENTFD:
8059 ret = -EINVAL;
8060 if (arg || nr_args)
8061 break;
8062 ret = io_eventfd_unregister(ctx);
8063 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07008064 case IORING_REGISTER_PROBE:
8065 ret = -EINVAL;
8066 if (!arg || nr_args > 256)
8067 break;
8068 ret = io_probe(ctx, arg, nr_args);
8069 break;
Jens Axboe071698e2020-01-28 10:04:42 -07008070 case IORING_REGISTER_PERSONALITY:
8071 ret = -EINVAL;
8072 if (arg || nr_args)
8073 break;
8074 ret = io_register_personality(ctx);
8075 break;
8076 case IORING_UNREGISTER_PERSONALITY:
8077 ret = -EINVAL;
8078 if (arg)
8079 break;
8080 ret = io_unregister_personality(ctx, nr_args);
8081 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008082 default:
8083 ret = -EINVAL;
8084 break;
8085 }
8086
Jens Axboe071698e2020-01-28 10:04:42 -07008087 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008088 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07008089 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07008090out:
8091 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008092 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008093 return ret;
8094}
8095
8096SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
8097 void __user *, arg, unsigned int, nr_args)
8098{
8099 struct io_ring_ctx *ctx;
8100 long ret = -EBADF;
8101 struct fd f;
8102
8103 f = fdget(fd);
8104 if (!f.file)
8105 return -EBADF;
8106
8107 ret = -EOPNOTSUPP;
8108 if (f.file->f_op != &io_uring_fops)
8109 goto out_fput;
8110
8111 ctx = f.file->private_data;
8112
8113 mutex_lock(&ctx->uring_lock);
8114 ret = __io_uring_register(ctx, opcode, arg, nr_args);
8115 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02008116 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
8117 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07008118out_fput:
8119 fdput(f);
8120 return ret;
8121}
8122
Jens Axboe2b188cc2019-01-07 10:46:33 -07008123static int __init io_uring_init(void)
8124{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008125#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
8126 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
8127 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
8128} while (0)
8129
8130#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
8131 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
8132 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
8133 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
8134 BUILD_BUG_SQE_ELEM(1, __u8, flags);
8135 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
8136 BUILD_BUG_SQE_ELEM(4, __s32, fd);
8137 BUILD_BUG_SQE_ELEM(8, __u64, off);
8138 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
8139 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008140 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008141 BUILD_BUG_SQE_ELEM(24, __u32, len);
8142 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
8143 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
8144 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
8145 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
8146 BUILD_BUG_SQE_ELEM(28, __u16, poll_events);
8147 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
8148 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
8149 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
8150 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
8151 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
8152 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
8153 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
8154 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008155 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008156 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
8157 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
8158 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008159 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008160
Jens Axboed3656342019-12-18 09:50:26 -07008161 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07008162 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008163 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
8164 return 0;
8165};
8166__initcall(io_uring_init);