blob: 084dfade5cdaf8c63b976a5aba70527a7c1669f6 [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 Axboe5b0bbee2020-04-27 10:41:22 -0600527 REQ_F_NO_FILE_TABLE_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700528
529 /* not a real bit, just to check we're not overflowing the space */
530 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300531};
532
533enum {
534 /* ctx owns file */
535 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
536 /* drain existing IO first */
537 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
538 /* linked sqes */
539 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
540 /* doesn't sever on completion < 0 */
541 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
542 /* IOSQE_ASYNC */
543 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700544 /* IOSQE_BUFFER_SELECT */
545 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300546
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300547 /* head of a link */
548 REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300549 /* already grabbed next link */
550 REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT),
551 /* fail rest of links */
552 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
553 /* on inflight list */
554 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
555 /* read/write uses file position */
556 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
557 /* must not punt to workers */
558 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
559 /* polled IO has completed */
560 REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT),
561 /* has linked timeout */
562 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
563 /* timeout request */
564 REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT),
565 /* regular file */
566 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
567 /* must be punted even for NONBLOCK */
568 REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT),
569 /* no timeout sequence */
570 REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT),
571 /* completion under lock */
572 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300573 /* needs cleanup */
574 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboe2ca10252020-02-13 17:17:35 -0700575 /* in overflow list */
576 REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700577 /* already went through poll handler */
578 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700579 /* buffer already selected */
580 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600581 /* doesn't need file table for this request */
582 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700583};
584
585struct async_poll {
586 struct io_poll_iocb poll;
587 struct io_wq_work work;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300588};
589
Jens Axboe09bb8392019-03-13 12:39:28 -0600590/*
591 * NOTE! Each of the iocb union members has the file pointer
592 * as the first entry in their struct definition. So you can
593 * access the file pointer through any of the sub-structs,
594 * or directly as just 'ki_filp' in this struct.
595 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700596struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700597 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600598 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700599 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700600 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700601 struct io_accept accept;
602 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700603 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700604 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700605 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700606 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700607 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700608 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700609 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700610 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700611 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700612 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300613 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700614 struct io_provide_buf pbuf;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700615 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700616
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700617 struct io_async_ctx *io;
Pavel Begunkovc398ecb2020-04-09 08:17:59 +0300618 int cflags;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300619 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700620 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700621
622 struct io_ring_ctx *ctx;
Jens Axboed7718a92020-02-14 22:23:12 -0700623 struct list_head list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700624 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700625 refcount_t refs;
Jens Axboe3537b6a2020-04-03 11:19:06 -0600626 struct task_struct *task;
627 unsigned long fsize;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700628 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600629 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600630 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700631
Jens Axboed7718a92020-02-14 22:23:12 -0700632 struct list_head link_list;
633
Jens Axboefcb323c2019-10-24 12:39:47 -0600634 struct list_head inflight_entry;
635
Xiaoguang Wang05589552020-03-31 14:05:18 +0800636 struct percpu_ref *fixed_file_refs;
637
Jens Axboeb41e9852020-02-17 09:52:41 -0700638 union {
639 /*
640 * Only commands that never go async can use the below fields,
Jens Axboed7718a92020-02-14 22:23:12 -0700641 * obviously. Right now only IORING_OP_POLL_ADD uses them, and
642 * async armed poll handlers for regular commands. The latter
643 * restore the work, if needed.
Jens Axboeb41e9852020-02-17 09:52:41 -0700644 */
645 struct {
Jens Axboeb41e9852020-02-17 09:52:41 -0700646 struct callback_head task_work;
Jens Axboed7718a92020-02-14 22:23:12 -0700647 struct hlist_node hash_node;
648 struct async_poll *apoll;
Jens Axboeb41e9852020-02-17 09:52:41 -0700649 };
650 struct io_wq_work work;
651 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700652};
653
654#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700655#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700656
Jens Axboe9a56a232019-01-09 09:06:50 -0700657struct io_submit_state {
658 struct blk_plug plug;
659
660 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700661 * io_kiocb alloc cache
662 */
663 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300664 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700665
666 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700667 * File reference cache
668 */
669 struct file *file;
670 unsigned int fd;
671 unsigned int has_refs;
672 unsigned int used_refs;
673 unsigned int ios_left;
674};
675
Jens Axboed3656342019-12-18 09:50:26 -0700676struct io_op_def {
677 /* needs req->io allocated for deferral/async */
678 unsigned async_ctx : 1;
679 /* needs current->mm setup, does mm access */
680 unsigned needs_mm : 1;
681 /* needs req->file assigned */
682 unsigned needs_file : 1;
683 /* needs req->file assigned IFF fd is >= 0 */
684 unsigned fd_non_neg : 1;
685 /* hash wq insertion if file is a regular file */
686 unsigned hash_reg_file : 1;
687 /* unbound wq insertion if file is a non-regular file */
688 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700689 /* opcode is not supported by this kernel */
690 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700691 /* needs file table */
692 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700693 /* needs ->fs */
694 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700695 /* set if opcode supports polled "wait" */
696 unsigned pollin : 1;
697 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700698 /* op supports buffer selection */
699 unsigned buffer_select : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700700};
701
702static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300703 [IORING_OP_NOP] = {},
704 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700705 .async_ctx = 1,
706 .needs_mm = 1,
707 .needs_file = 1,
708 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700709 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700710 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700711 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300712 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700713 .async_ctx = 1,
714 .needs_mm = 1,
715 .needs_file = 1,
716 .hash_reg_file = 1,
717 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700718 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700719 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300720 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700721 .needs_file = 1,
722 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300723 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700724 .needs_file = 1,
725 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700726 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700727 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300728 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700729 .needs_file = 1,
730 .hash_reg_file = 1,
731 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700732 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700733 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300734 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700735 .needs_file = 1,
736 .unbound_nonreg_file = 1,
737 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300738 [IORING_OP_POLL_REMOVE] = {},
739 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700740 .needs_file = 1,
741 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300742 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700743 .async_ctx = 1,
744 .needs_mm = 1,
745 .needs_file = 1,
746 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700747 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700748 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700749 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300750 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700751 .async_ctx = 1,
752 .needs_mm = 1,
753 .needs_file = 1,
754 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700755 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700756 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700757 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700758 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300759 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700760 .async_ctx = 1,
761 .needs_mm = 1,
762 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300763 [IORING_OP_TIMEOUT_REMOVE] = {},
764 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700765 .needs_mm = 1,
766 .needs_file = 1,
767 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700768 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700769 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700770 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300771 [IORING_OP_ASYNC_CANCEL] = {},
772 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700773 .async_ctx = 1,
774 .needs_mm = 1,
775 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300776 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700777 .async_ctx = 1,
778 .needs_mm = 1,
779 .needs_file = 1,
780 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700781 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700782 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300783 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700784 .needs_file = 1,
785 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300786 [IORING_OP_OPENAT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700787 .needs_file = 1,
788 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700789 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700790 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700791 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300792 [IORING_OP_CLOSE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700793 .needs_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700794 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700795 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300796 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700797 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700798 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700799 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300800 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700801 .needs_mm = 1,
802 .needs_file = 1,
803 .fd_non_neg = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700804 .needs_fs = 1,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600805 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700806 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300807 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700808 .needs_mm = 1,
809 .needs_file = 1,
810 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700811 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700812 .buffer_select = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700813 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300814 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700815 .needs_mm = 1,
816 .needs_file = 1,
817 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700818 .pollout = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700819 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300820 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700821 .needs_file = 1,
822 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300823 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700824 .needs_mm = 1,
825 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300826 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700827 .needs_mm = 1,
828 .needs_file = 1,
829 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700830 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700831 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300832 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700833 .needs_mm = 1,
834 .needs_file = 1,
835 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700836 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700837 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700838 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300839 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -0700840 .needs_file = 1,
841 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700842 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700843 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700844 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700845 [IORING_OP_EPOLL_CTL] = {
846 .unbound_nonreg_file = 1,
847 .file_table = 1,
848 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300849 [IORING_OP_SPLICE] = {
850 .needs_file = 1,
851 .hash_reg_file = 1,
852 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700853 },
854 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700855 [IORING_OP_REMOVE_BUFFERS] = {},
Jens Axboed3656342019-12-18 09:50:26 -0700856};
857
Jens Axboe561fb042019-10-24 07:25:42 -0600858static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700859static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800860static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700861static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700862static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
863static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700864static int __io_sqe_files_update(struct io_ring_ctx *ctx,
865 struct io_uring_files_update *ip,
866 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700867static int io_grab_files(struct io_kiocb *req);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300868static void io_cleanup_req(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700869static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
870 int fd, struct file **out_file, bool fixed);
871static void __io_queue_sqe(struct io_kiocb *req,
872 const struct io_uring_sqe *sqe);
Jens Axboede0617e2019-04-06 21:51:27 -0600873
Jens Axboe2b188cc2019-01-07 10:46:33 -0700874static struct kmem_cache *req_cachep;
875
876static const struct file_operations io_uring_fops;
877
878struct sock *io_uring_get_socket(struct file *file)
879{
880#if defined(CONFIG_UNIX)
881 if (file->f_op == &io_uring_fops) {
882 struct io_ring_ctx *ctx = file->private_data;
883
884 return ctx->ring_sock->sk;
885 }
886#endif
887 return NULL;
888}
889EXPORT_SYMBOL(io_uring_get_socket);
890
891static void io_ring_ctx_ref_free(struct percpu_ref *ref)
892{
893 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
894
Jens Axboe206aefd2019-11-07 18:27:42 -0700895 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700896}
897
898static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
899{
900 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700901 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700902
903 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
904 if (!ctx)
905 return NULL;
906
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700907 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
908 if (!ctx->fallback_req)
909 goto err;
910
Jens Axboe206aefd2019-11-07 18:27:42 -0700911 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
912 if (!ctx->completions)
913 goto err;
914
Jens Axboe78076bb2019-12-04 19:56:40 -0700915 /*
916 * Use 5 bits less than the max cq entries, that should give us around
917 * 32 entries per hash list if totally full and uniformly spread.
918 */
919 hash_bits = ilog2(p->cq_entries);
920 hash_bits -= 5;
921 if (hash_bits <= 0)
922 hash_bits = 1;
923 ctx->cancel_hash_bits = hash_bits;
924 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
925 GFP_KERNEL);
926 if (!ctx->cancel_hash)
927 goto err;
928 __hash_init(ctx->cancel_hash, 1U << hash_bits);
929
Roman Gushchin21482892019-05-07 10:01:48 -0700930 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700931 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
932 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700933
934 ctx->flags = p->flags;
935 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700936 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700937 init_completion(&ctx->completions[0]);
938 init_completion(&ctx->completions[1]);
Jens Axboe5a2e7452020-02-23 16:23:11 -0700939 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -0700940 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700941 mutex_init(&ctx->uring_lock);
942 init_waitqueue_head(&ctx->wait);
943 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700944 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600945 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600946 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600947 init_waitqueue_head(&ctx->inflight_wait);
948 spin_lock_init(&ctx->inflight_lock);
949 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700950 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700951err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700952 if (ctx->fallback_req)
953 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700954 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700955 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700956 kfree(ctx);
957 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700958}
959
Bob Liu9d858b22019-11-13 18:06:25 +0800960static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600961{
Jackie Liua197f662019-11-08 08:09:12 -0700962 struct io_ring_ctx *ctx = req->ctx;
963
Pavel Begunkov31af27c2020-04-15 00:39:50 +0300964 return req->sequence != ctx->cached_cq_tail
965 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600966}
967
Bob Liu9d858b22019-11-13 18:06:25 +0800968static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600969{
Pavel Begunkov87987892020-01-18 01:22:30 +0300970 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +0800971 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600972
Bob Liu9d858b22019-11-13 18:06:25 +0800973 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600974}
975
976static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600977{
978 struct io_kiocb *req;
979
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600980 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800981 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600982 list_del_init(&req->list);
983 return req;
984 }
985
986 return NULL;
987}
988
Jens Axboe5262f562019-09-17 12:26:57 -0600989static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
990{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600991 struct io_kiocb *req;
992
993 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700994 if (req) {
995 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
996 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800997 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700998 list_del_init(&req->list);
999 return req;
1000 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001001 }
1002
1003 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -06001004}
1005
Jens Axboede0617e2019-04-06 21:51:27 -06001006static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001007{
Hristo Venev75b28af2019-08-26 17:23:46 +00001008 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001009
Pavel Begunkov07910152020-01-17 03:52:46 +03001010 /* order cqe stores with ring update */
1011 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001012
Pavel Begunkov07910152020-01-17 03:52:46 +03001013 if (wq_has_sleeper(&ctx->cq_wait)) {
1014 wake_up_interruptible(&ctx->cq_wait);
1015 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001016 }
1017}
1018
Jens Axboecccf0ee2020-01-27 16:34:48 -07001019static inline void io_req_work_grab_env(struct io_kiocb *req,
1020 const struct io_op_def *def)
Jens Axboe18d9be12019-09-10 09:13:05 -06001021{
Jens Axboecccf0ee2020-01-27 16:34:48 -07001022 if (!req->work.mm && def->needs_mm) {
1023 mmgrab(current->mm);
1024 req->work.mm = current->mm;
1025 }
1026 if (!req->work.creds)
1027 req->work.creds = get_current_cred();
Jens Axboeff002b32020-02-07 16:05:21 -07001028 if (!req->work.fs && def->needs_fs) {
1029 spin_lock(&current->fs->lock);
1030 if (!current->fs->in_exec) {
1031 req->work.fs = current->fs;
1032 req->work.fs->users++;
1033 } else {
1034 req->work.flags |= IO_WQ_WORK_CANCEL;
1035 }
1036 spin_unlock(&current->fs->lock);
1037 }
Jens Axboe6ab23142020-02-08 20:23:59 -07001038 if (!req->work.task_pid)
1039 req->work.task_pid = task_pid_vnr(current);
Jens Axboecccf0ee2020-01-27 16:34:48 -07001040}
1041
1042static inline void io_req_work_drop_env(struct io_kiocb *req)
1043{
1044 if (req->work.mm) {
1045 mmdrop(req->work.mm);
1046 req->work.mm = NULL;
1047 }
1048 if (req->work.creds) {
1049 put_cred(req->work.creds);
1050 req->work.creds = NULL;
1051 }
Jens Axboeff002b32020-02-07 16:05:21 -07001052 if (req->work.fs) {
1053 struct fs_struct *fs = req->work.fs;
1054
1055 spin_lock(&req->work.fs->lock);
1056 if (--fs->users)
1057 fs = NULL;
1058 spin_unlock(&req->work.fs->lock);
1059 if (fs)
1060 free_fs_struct(fs);
1061 }
Jens Axboe561fb042019-10-24 07:25:42 -06001062}
1063
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001064static inline void io_prep_async_work(struct io_kiocb *req,
Jens Axboe94ae5e72019-11-14 19:39:52 -07001065 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -06001066{
Jens Axboed3656342019-12-18 09:50:26 -07001067 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe54a91f32019-09-10 09:15:04 -06001068
Jens Axboed3656342019-12-18 09:50:26 -07001069 if (req->flags & REQ_F_ISREG) {
1070 if (def->hash_reg_file)
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001071 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001072 } else {
1073 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001074 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001075 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001076
1077 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -06001078
Jens Axboe94ae5e72019-11-14 19:39:52 -07001079 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001080}
1081
Jackie Liua197f662019-11-08 08:09:12 -07001082static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001083{
Jackie Liua197f662019-11-08 08:09:12 -07001084 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001085 struct io_kiocb *link;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001086
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001087 io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06001088
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001089 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1090 &req->work, req->flags);
1091 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001092
1093 if (link)
1094 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -06001095}
1096
Jens Axboe5262f562019-09-17 12:26:57 -06001097static void io_kill_timeout(struct io_kiocb *req)
1098{
1099 int ret;
1100
Jens Axboe2d283902019-12-04 11:08:05 -07001101 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001102 if (ret != -1) {
1103 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -06001104 list_del_init(&req->list);
Pavel Begunkovf0e20b82020-03-07 01:15:22 +03001105 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001106 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001107 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001108 }
1109}
1110
1111static void io_kill_timeouts(struct io_ring_ctx *ctx)
1112{
1113 struct io_kiocb *req, *tmp;
1114
1115 spin_lock_irq(&ctx->completion_lock);
1116 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
1117 io_kill_timeout(req);
1118 spin_unlock_irq(&ctx->completion_lock);
1119}
1120
Jens Axboede0617e2019-04-06 21:51:27 -06001121static void io_commit_cqring(struct io_ring_ctx *ctx)
1122{
1123 struct io_kiocb *req;
1124
Jens Axboe5262f562019-09-17 12:26:57 -06001125 while ((req = io_get_timeout_req(ctx)) != NULL)
1126 io_kill_timeout(req);
1127
Jens Axboede0617e2019-04-06 21:51:27 -06001128 __io_commit_cqring(ctx);
1129
Pavel Begunkov87987892020-01-18 01:22:30 +03001130 while ((req = io_get_deferred_req(ctx)) != NULL)
Jackie Liua197f662019-11-08 08:09:12 -07001131 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -06001132}
1133
Jens Axboe2b188cc2019-01-07 10:46:33 -07001134static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1135{
Hristo Venev75b28af2019-08-26 17:23:46 +00001136 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001137 unsigned tail;
1138
1139 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001140 /*
1141 * writes to the cq entry need to come after reading head; the
1142 * control dependency is enough as we're using WRITE_ONCE to
1143 * fill the cq entry
1144 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001145 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001146 return NULL;
1147
1148 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001149 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001150}
1151
Jens Axboef2842ab2020-01-08 11:04:00 -07001152static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1153{
Jens Axboef0b493e2020-02-01 21:30:11 -07001154 if (!ctx->cq_ev_fd)
1155 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001156 if (!ctx->eventfd_async)
1157 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001158 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001159}
1160
Jens Axboeb41e9852020-02-17 09:52:41 -07001161static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001162{
1163 if (waitqueue_active(&ctx->wait))
1164 wake_up(&ctx->wait);
1165 if (waitqueue_active(&ctx->sqo_wait))
1166 wake_up(&ctx->sqo_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001167 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001168 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001169}
1170
Jens Axboec4a2ed72019-11-21 21:01:26 -07001171/* Returns true if there are no backlogged entries after the flush */
1172static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001173{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001174 struct io_rings *rings = ctx->rings;
1175 struct io_uring_cqe *cqe;
1176 struct io_kiocb *req;
1177 unsigned long flags;
1178 LIST_HEAD(list);
1179
1180 if (!force) {
1181 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001182 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001183 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1184 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001185 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001186 }
1187
1188 spin_lock_irqsave(&ctx->completion_lock, flags);
1189
1190 /* if force is set, the ring is going away. always drop after that */
1191 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001192 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001193
Jens Axboec4a2ed72019-11-21 21:01:26 -07001194 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001195 while (!list_empty(&ctx->cq_overflow_list)) {
1196 cqe = io_get_cqring(ctx);
1197 if (!cqe && !force)
1198 break;
1199
1200 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1201 list);
1202 list_move(&req->list, &list);
Jens Axboe2ca10252020-02-13 17:17:35 -07001203 req->flags &= ~REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001204 if (cqe) {
1205 WRITE_ONCE(cqe->user_data, req->user_data);
1206 WRITE_ONCE(cqe->res, req->result);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001207 WRITE_ONCE(cqe->flags, req->cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001208 } else {
1209 WRITE_ONCE(ctx->rings->cq_overflow,
1210 atomic_inc_return(&ctx->cached_cq_overflow));
1211 }
1212 }
1213
1214 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001215 if (cqe) {
1216 clear_bit(0, &ctx->sq_check_overflow);
1217 clear_bit(0, &ctx->cq_check_overflow);
1218 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001219 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1220 io_cqring_ev_posted(ctx);
1221
1222 while (!list_empty(&list)) {
1223 req = list_first_entry(&list, struct io_kiocb, list);
1224 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001225 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001226 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001227
1228 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001229}
1230
Jens Axboebcda7ba2020-02-23 16:42:51 -07001231static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001232{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001233 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001234 struct io_uring_cqe *cqe;
1235
Jens Axboe78e19bb2019-11-06 15:21:34 -07001236 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001237
Jens Axboe2b188cc2019-01-07 10:46:33 -07001238 /*
1239 * If we can't get a cq entry, userspace overflowed the
1240 * submission (by quite a lot). Increment the overflow count in
1241 * the ring.
1242 */
1243 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001244 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001245 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001246 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001247 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001248 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001249 WRITE_ONCE(ctx->rings->cq_overflow,
1250 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001251 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001252 if (list_empty(&ctx->cq_overflow_list)) {
1253 set_bit(0, &ctx->sq_check_overflow);
1254 set_bit(0, &ctx->cq_check_overflow);
1255 }
Jens Axboe2ca10252020-02-13 17:17:35 -07001256 req->flags |= REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001257 refcount_inc(&req->refs);
1258 req->result = res;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001259 req->cflags = cflags;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001260 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001261 }
1262}
1263
Jens Axboebcda7ba2020-02-23 16:42:51 -07001264static void io_cqring_fill_event(struct io_kiocb *req, long res)
1265{
1266 __io_cqring_fill_event(req, res, 0);
1267}
1268
1269static void __io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001270{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001271 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001272 unsigned long flags;
1273
1274 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001275 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001276 io_commit_cqring(ctx);
1277 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1278
Jens Axboe8c838782019-03-12 15:48:16 -06001279 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001280}
1281
Jens Axboebcda7ba2020-02-23 16:42:51 -07001282static void io_cqring_add_event(struct io_kiocb *req, long res)
1283{
1284 __io_cqring_add_event(req, res, 0);
1285}
1286
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001287static inline bool io_is_fallback_req(struct io_kiocb *req)
1288{
1289 return req == (struct io_kiocb *)
1290 ((unsigned long) req->ctx->fallback_req & ~1UL);
1291}
1292
1293static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1294{
1295 struct io_kiocb *req;
1296
1297 req = ctx->fallback_req;
1298 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1299 return req;
1300
1301 return NULL;
1302}
1303
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001304static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1305 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001306{
Jens Axboefd6fab22019-03-14 16:30:06 -06001307 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001308 struct io_kiocb *req;
1309
Jens Axboe2579f912019-01-09 09:10:43 -07001310 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001311 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001312 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001313 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001314 } else if (!state->free_reqs) {
1315 size_t sz;
1316 int ret;
1317
1318 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001319 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1320
1321 /*
1322 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1323 * retry single alloc to be on the safe side.
1324 */
1325 if (unlikely(ret <= 0)) {
1326 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1327 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001328 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001329 ret = 1;
1330 }
Jens Axboe2579f912019-01-09 09:10:43 -07001331 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001332 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001333 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001334 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001335 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001336 }
1337
Jens Axboe2579f912019-01-09 09:10:43 -07001338 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001339fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001340 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001341}
1342
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001343static inline void io_put_file(struct io_kiocb *req, struct file *file,
1344 bool fixed)
1345{
1346 if (fixed)
Xiaoguang Wang05589552020-03-31 14:05:18 +08001347 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001348 else
1349 fput(file);
1350}
1351
Jens Axboec6ca97b302019-12-28 12:11:08 -07001352static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001353{
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001354 if (req->flags & REQ_F_NEED_CLEANUP)
1355 io_cleanup_req(req);
1356
YueHaibing96fd84d2020-01-07 22:22:44 +08001357 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001358 if (req->file)
1359 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboe3537b6a2020-04-03 11:19:06 -06001360 if (req->task)
1361 put_task_struct(req->task);
Jens Axboecccf0ee2020-01-27 16:34:48 -07001362
1363 io_req_work_drop_env(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001364}
1365
1366static void __io_free_req(struct io_kiocb *req)
1367{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001368 __io_req_aux_free(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06001369
Jens Axboefcb323c2019-10-24 12:39:47 -06001370 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001371 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001372 unsigned long flags;
1373
1374 spin_lock_irqsave(&ctx->inflight_lock, flags);
1375 list_del(&req->inflight_entry);
1376 if (waitqueue_active(&ctx->inflight_wait))
1377 wake_up(&ctx->inflight_wait);
1378 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1379 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001380
1381 percpu_ref_put(&req->ctx->refs);
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03001382 if (likely(!io_is_fallback_req(req)))
1383 kmem_cache_free(req_cachep, req);
1384 else
1385 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -06001386}
1387
Jens Axboec6ca97b302019-12-28 12:11:08 -07001388struct req_batch {
1389 void *reqs[IO_IOPOLL_BATCH];
1390 int to_free;
1391 int need_iter;
1392};
1393
1394static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1395{
1396 if (!rb->to_free)
1397 return;
1398 if (rb->need_iter) {
1399 int i, inflight = 0;
1400 unsigned long flags;
1401
1402 for (i = 0; i < rb->to_free; i++) {
1403 struct io_kiocb *req = rb->reqs[i];
1404
Jens Axboe10fef4b2020-01-09 07:52:28 -07001405 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001406 req->file = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08001407 percpu_ref_put(req->fixed_file_refs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001408 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001409 if (req->flags & REQ_F_INFLIGHT)
1410 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001411 __io_req_aux_free(req);
1412 }
1413 if (!inflight)
1414 goto do_free;
1415
1416 spin_lock_irqsave(&ctx->inflight_lock, flags);
1417 for (i = 0; i < rb->to_free; i++) {
1418 struct io_kiocb *req = rb->reqs[i];
1419
Jens Axboe10fef4b2020-01-09 07:52:28 -07001420 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001421 list_del(&req->inflight_entry);
1422 if (!--inflight)
1423 break;
1424 }
1425 }
1426 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1427
1428 if (waitqueue_active(&ctx->inflight_wait))
1429 wake_up(&ctx->inflight_wait);
1430 }
1431do_free:
1432 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
1433 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001434 rb->to_free = rb->need_iter = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001435}
1436
Jackie Liua197f662019-11-08 08:09:12 -07001437static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001438{
Jackie Liua197f662019-11-08 08:09:12 -07001439 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001440 int ret;
1441
Jens Axboe2d283902019-12-04 11:08:05 -07001442 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001443 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001444 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001445 io_commit_cqring(ctx);
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001446 req->flags &= ~REQ_F_LINK_HEAD;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001447 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001448 return true;
1449 }
1450
1451 return false;
1452}
1453
Jens Axboeba816ad2019-09-28 11:36:45 -06001454static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001455{
Jens Axboe2665abf2019-11-05 12:40:47 -07001456 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001457 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001458
Jens Axboe4d7dd462019-11-20 13:03:52 -07001459 /* Already got next link */
1460 if (req->flags & REQ_F_LINK_NEXT)
1461 return;
1462
Jens Axboe9e645e112019-05-10 16:07:28 -06001463 /*
1464 * The list should never be empty when we are called here. But could
1465 * potentially happen if the chain is messed up, check to be on the
1466 * safe side.
1467 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001468 while (!list_empty(&req->link_list)) {
1469 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1470 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001471
Pavel Begunkov44932332019-12-05 16:16:35 +03001472 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1473 (nxt->flags & REQ_F_TIMEOUT))) {
1474 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001475 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001476 req->flags &= ~REQ_F_LINK_TIMEOUT;
1477 continue;
1478 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001479
Pavel Begunkov44932332019-12-05 16:16:35 +03001480 list_del_init(&req->link_list);
1481 if (!list_empty(&nxt->link_list))
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001482 nxt->flags |= REQ_F_LINK_HEAD;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001483 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001484 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001485 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001486
Jens Axboe4d7dd462019-11-20 13:03:52 -07001487 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001488 if (wake_ev)
1489 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001490}
1491
1492/*
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001493 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
Jens Axboe9e645e112019-05-10 16:07:28 -06001494 */
1495static void io_fail_links(struct io_kiocb *req)
1496{
Jens Axboe2665abf2019-11-05 12:40:47 -07001497 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001498 unsigned long flags;
1499
1500 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001501
1502 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001503 struct io_kiocb *link = list_first_entry(&req->link_list,
1504 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001505
Pavel Begunkov44932332019-12-05 16:16:35 +03001506 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001507 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001508
1509 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001510 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001511 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001512 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001513 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001514 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001515 }
Jens Axboe5d960722019-11-19 15:31:28 -07001516 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001517 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001518
1519 io_commit_cqring(ctx);
1520 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1521 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001522}
1523
Jens Axboe4d7dd462019-11-20 13:03:52 -07001524static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001525{
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001526 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001527 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001528
Jens Axboe9e645e112019-05-10 16:07:28 -06001529 /*
1530 * If LINK is set, we have dependent requests in this chain. If we
1531 * didn't fail this request, queue the first one up, moving any other
1532 * dependencies to the next request. In case of failure, fail the rest
1533 * of the chain.
1534 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001535 if (req->flags & REQ_F_FAIL_LINK) {
1536 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001537 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1538 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001539 struct io_ring_ctx *ctx = req->ctx;
1540 unsigned long flags;
1541
1542 /*
1543 * If this is a timeout link, we could be racing with the
1544 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001545 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001546 */
1547 spin_lock_irqsave(&ctx->completion_lock, flags);
1548 io_req_link_next(req, nxt);
1549 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1550 } else {
1551 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001552 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001553}
Jens Axboe9e645e112019-05-10 16:07:28 -06001554
Jackie Liuc69f8db2019-11-09 11:00:08 +08001555static void io_free_req(struct io_kiocb *req)
1556{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001557 struct io_kiocb *nxt = NULL;
1558
1559 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001560 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001561
1562 if (nxt)
1563 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001564}
1565
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001566static void io_link_work_cb(struct io_wq_work **workptr)
1567{
Pavel Begunkov18a542f2020-03-23 00:23:29 +03001568 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
1569 struct io_kiocb *link;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001570
Pavel Begunkov18a542f2020-03-23 00:23:29 +03001571 link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001572 io_queue_linked_timeout(link);
1573 io_wq_submit_work(workptr);
1574}
1575
1576static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
1577{
1578 struct io_kiocb *link;
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001579 const struct io_op_def *def = &io_op_defs[nxt->opcode];
1580
1581 if ((nxt->flags & REQ_F_ISREG) && def->hash_reg_file)
1582 io_wq_hash_work(&nxt->work, file_inode(nxt->file));
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001583
1584 *workptr = &nxt->work;
1585 link = io_prep_linked_timeout(nxt);
Pavel Begunkov18a542f2020-03-23 00:23:29 +03001586 if (link)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001587 nxt->work.func = io_link_work_cb;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001588}
1589
Jens Axboeba816ad2019-09-28 11:36:45 -06001590/*
1591 * Drop reference to request, return next in chain (if there is one) if this
1592 * was the last reference to this request.
1593 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001594__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001595static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001596{
Jens Axboe2a44f462020-02-25 13:25:41 -07001597 if (refcount_dec_and_test(&req->refs)) {
1598 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001599 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001600 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001601}
1602
Jens Axboe2b188cc2019-01-07 10:46:33 -07001603static void io_put_req(struct io_kiocb *req)
1604{
Jens Axboedef596e2019-01-09 08:59:42 -07001605 if (refcount_dec_and_test(&req->refs))
1606 io_free_req(req);
1607}
1608
Pavel Begunkove9fd9392020-03-04 16:14:12 +03001609static void io_steal_work(struct io_kiocb *req,
1610 struct io_wq_work **workptr)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001611{
1612 /*
1613 * It's in an io-wq worker, so there always should be at least
1614 * one reference, which will be dropped in io_put_work() just
1615 * after the current handler returns.
1616 *
1617 * It also means, that if the counter dropped to 1, then there is
1618 * no asynchronous users left, so it's safe to steal the next work.
1619 */
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001620 if (refcount_read(&req->refs) == 1) {
1621 struct io_kiocb *nxt = NULL;
1622
1623 io_req_find_next(req, &nxt);
1624 if (nxt)
1625 io_wq_assign_next(workptr, nxt);
1626 }
1627}
1628
Jens Axboe978db572019-11-14 22:39:04 -07001629/*
1630 * Must only be used if we don't need to care about links, usually from
1631 * within the completion handling itself.
1632 */
1633static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001634{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001635 /* drop both submit and complete references */
1636 if (refcount_sub_and_test(2, &req->refs))
1637 __io_free_req(req);
1638}
1639
Jens Axboe978db572019-11-14 22:39:04 -07001640static void io_double_put_req(struct io_kiocb *req)
1641{
1642 /* drop both submit and complete references */
1643 if (refcount_sub_and_test(2, &req->refs))
1644 io_free_req(req);
1645}
1646
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001647static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001648{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001649 struct io_rings *rings = ctx->rings;
1650
Jens Axboead3eb2c2019-12-18 17:12:20 -07001651 if (test_bit(0, &ctx->cq_check_overflow)) {
1652 /*
1653 * noflush == true is from the waitqueue handler, just ensure
1654 * we wake up the task, and the next invocation will flush the
1655 * entries. We cannot safely to it from here.
1656 */
1657 if (noflush && !list_empty(&ctx->cq_overflow_list))
1658 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001659
Jens Axboead3eb2c2019-12-18 17:12:20 -07001660 io_cqring_overflow_flush(ctx, false);
1661 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001662
Jens Axboea3a0e432019-08-20 11:03:11 -06001663 /* See comment at the top of this file */
1664 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001665 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001666}
1667
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001668static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1669{
1670 struct io_rings *rings = ctx->rings;
1671
1672 /* make sure SQ entry isn't read before tail */
1673 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1674}
1675
Jens Axboe8237e042019-12-28 10:48:22 -07001676static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001677{
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001678 if ((req->flags & REQ_F_LINK_HEAD) || io_is_fallback_req(req))
Jens Axboec6ca97b302019-12-28 12:11:08 -07001679 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001680
Jens Axboec6ca97b302019-12-28 12:11:08 -07001681 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1682 rb->need_iter++;
1683
1684 rb->reqs[rb->to_free++] = req;
1685 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1686 io_free_req_many(req->ctx, rb);
1687 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001688}
1689
Jens Axboebcda7ba2020-02-23 16:42:51 -07001690static int io_put_kbuf(struct io_kiocb *req)
1691{
Jens Axboe4d954c22020-02-27 07:31:19 -07001692 struct io_buffer *kbuf;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001693 int cflags;
1694
Jens Axboe4d954c22020-02-27 07:31:19 -07001695 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001696 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
1697 cflags |= IORING_CQE_F_BUFFER;
1698 req->rw.addr = 0;
1699 kfree(kbuf);
1700 return cflags;
1701}
1702
Jens Axboedef596e2019-01-09 08:59:42 -07001703/*
1704 * Find and free completed poll iocbs
1705 */
1706static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1707 struct list_head *done)
1708{
Jens Axboe8237e042019-12-28 10:48:22 -07001709 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001710 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001711
Jens Axboec6ca97b302019-12-28 12:11:08 -07001712 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001713 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07001714 int cflags = 0;
1715
Jens Axboedef596e2019-01-09 08:59:42 -07001716 req = list_first_entry(done, struct io_kiocb, list);
1717 list_del(&req->list);
1718
Jens Axboebcda7ba2020-02-23 16:42:51 -07001719 if (req->flags & REQ_F_BUFFER_SELECTED)
1720 cflags = io_put_kbuf(req);
1721
1722 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07001723 (*nr_events)++;
1724
Jens Axboe8237e042019-12-28 10:48:22 -07001725 if (refcount_dec_and_test(&req->refs) &&
1726 !io_req_multi_free(&rb, req))
1727 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001728 }
Jens Axboedef596e2019-01-09 08:59:42 -07001729
Jens Axboe09bb8392019-03-13 12:39:28 -06001730 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08001731 if (ctx->flags & IORING_SETUP_SQPOLL)
1732 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001733 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001734}
1735
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001736static void io_iopoll_queue(struct list_head *again)
1737{
1738 struct io_kiocb *req;
1739
1740 do {
1741 req = list_first_entry(again, struct io_kiocb, list);
1742 list_del(&req->list);
1743 refcount_inc(&req->refs);
1744 io_queue_async_work(req);
1745 } while (!list_empty(again));
1746}
1747
Jens Axboedef596e2019-01-09 08:59:42 -07001748static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1749 long min)
1750{
1751 struct io_kiocb *req, *tmp;
1752 LIST_HEAD(done);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001753 LIST_HEAD(again);
Jens Axboedef596e2019-01-09 08:59:42 -07001754 bool spin;
1755 int ret;
1756
1757 /*
1758 * Only spin for completions if we don't have multiple devices hanging
1759 * off our complete list, and we're under the requested amount.
1760 */
1761 spin = !ctx->poll_multi_file && *nr_events < min;
1762
1763 ret = 0;
1764 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001765 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001766
1767 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001768 * Move completed and retryable entries to our local lists.
1769 * If we find a request that requires polling, break out
1770 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07001771 */
1772 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1773 list_move_tail(&req->list, &done);
1774 continue;
1775 }
1776 if (!list_empty(&done))
1777 break;
1778
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001779 if (req->result == -EAGAIN) {
1780 list_move_tail(&req->list, &again);
1781 continue;
1782 }
1783 if (!list_empty(&again))
1784 break;
1785
Jens Axboedef596e2019-01-09 08:59:42 -07001786 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1787 if (ret < 0)
1788 break;
1789
1790 if (ret && spin)
1791 spin = false;
1792 ret = 0;
1793 }
1794
1795 if (!list_empty(&done))
1796 io_iopoll_complete(ctx, nr_events, &done);
1797
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001798 if (!list_empty(&again))
1799 io_iopoll_queue(&again);
1800
Jens Axboedef596e2019-01-09 08:59:42 -07001801 return ret;
1802}
1803
1804/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001805 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001806 * non-spinning poll check - we'll still enter the driver poll loop, but only
1807 * as a non-spinning completion check.
1808 */
1809static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1810 long min)
1811{
Jens Axboe08f54392019-08-21 22:19:11 -06001812 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001813 int ret;
1814
1815 ret = io_do_iopoll(ctx, nr_events, min);
1816 if (ret < 0)
1817 return ret;
1818 if (!min || *nr_events >= min)
1819 return 0;
1820 }
1821
1822 return 1;
1823}
1824
1825/*
1826 * We can't just wait for polled events to come to us, we have to actively
1827 * find and complete them.
1828 */
1829static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1830{
1831 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1832 return;
1833
1834 mutex_lock(&ctx->uring_lock);
1835 while (!list_empty(&ctx->poll_list)) {
1836 unsigned int nr_events = 0;
1837
1838 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001839
1840 /*
1841 * Ensure we allow local-to-the-cpu processing to take place,
1842 * in this case we need to ensure that we reap all events.
1843 */
1844 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001845 }
1846 mutex_unlock(&ctx->uring_lock);
1847}
1848
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001849static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1850 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001851{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001852 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001853
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001854 /*
1855 * We disallow the app entering submit/complete with polling, but we
1856 * still need to lock the ring to prevent racing with polled issue
1857 * that got punted to a workqueue.
1858 */
1859 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001860 do {
1861 int tmin = 0;
1862
Jens Axboe500f9fb2019-08-19 12:15:59 -06001863 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001864 * Don't enter poll loop if we already have events pending.
1865 * If we do, we can potentially be spinning for commands that
1866 * already triggered a CQE (eg in error).
1867 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001868 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001869 break;
1870
1871 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001872 * If a submit got punted to a workqueue, we can have the
1873 * application entering polling for a command before it gets
1874 * issued. That app will hold the uring_lock for the duration
1875 * of the poll right here, so we need to take a breather every
1876 * now and then to ensure that the issue has a chance to add
1877 * the poll to the issued list. Otherwise we can spin here
1878 * forever, while the workqueue is stuck trying to acquire the
1879 * very same mutex.
1880 */
1881 if (!(++iters & 7)) {
1882 mutex_unlock(&ctx->uring_lock);
1883 mutex_lock(&ctx->uring_lock);
1884 }
1885
Jens Axboedef596e2019-01-09 08:59:42 -07001886 if (*nr_events < min)
1887 tmin = min - *nr_events;
1888
1889 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1890 if (ret <= 0)
1891 break;
1892 ret = 0;
1893 } while (min && !*nr_events && !need_resched());
1894
Jens Axboe500f9fb2019-08-19 12:15:59 -06001895 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001896 return ret;
1897}
1898
Jens Axboe491381ce2019-10-17 09:20:46 -06001899static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001900{
Jens Axboe491381ce2019-10-17 09:20:46 -06001901 /*
1902 * Tell lockdep we inherited freeze protection from submission
1903 * thread.
1904 */
1905 if (req->flags & REQ_F_ISREG) {
1906 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001907
Jens Axboe491381ce2019-10-17 09:20:46 -06001908 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001909 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001910 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001911}
1912
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001913static inline void req_set_fail_links(struct io_kiocb *req)
1914{
1915 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1916 req->flags |= REQ_F_FAIL_LINK;
1917}
1918
Jens Axboeba816ad2019-09-28 11:36:45 -06001919static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001920{
Jens Axboe9adbd452019-12-20 08:45:55 -07001921 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001922 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001923
Jens Axboe491381ce2019-10-17 09:20:46 -06001924 if (kiocb->ki_flags & IOCB_WRITE)
1925 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001926
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001927 if (res != req->result)
1928 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001929 if (req->flags & REQ_F_BUFFER_SELECTED)
1930 cflags = io_put_kbuf(req);
1931 __io_cqring_add_event(req, res, cflags);
Jens Axboeba816ad2019-09-28 11:36:45 -06001932}
1933
1934static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1935{
Jens Axboe9adbd452019-12-20 08:45:55 -07001936 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001937
1938 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001939 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001940}
1941
Jens Axboedef596e2019-01-09 08:59:42 -07001942static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1943{
Jens Axboe9adbd452019-12-20 08:45:55 -07001944 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001945
Jens Axboe491381ce2019-10-17 09:20:46 -06001946 if (kiocb->ki_flags & IOCB_WRITE)
1947 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001948
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001949 if (res != req->result)
1950 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001951 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001952 if (res != -EAGAIN)
1953 req->flags |= REQ_F_IOPOLL_COMPLETED;
1954}
1955
1956/*
1957 * After the iocb has been issued, it's safe to be found on the poll list.
1958 * Adding the kiocb to the list AFTER submission ensures that we don't
1959 * find it from a io_iopoll_getevents() thread before the issuer is done
1960 * accessing the kiocb cookie.
1961 */
1962static void io_iopoll_req_issued(struct io_kiocb *req)
1963{
1964 struct io_ring_ctx *ctx = req->ctx;
1965
1966 /*
1967 * Track whether we have multiple files in our lists. This will impact
1968 * how we do polling eventually, not spinning if we're on potentially
1969 * different devices.
1970 */
1971 if (list_empty(&ctx->poll_list)) {
1972 ctx->poll_multi_file = false;
1973 } else if (!ctx->poll_multi_file) {
1974 struct io_kiocb *list_req;
1975
1976 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1977 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001978 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001979 ctx->poll_multi_file = true;
1980 }
1981
1982 /*
1983 * For fast devices, IO may have already completed. If it has, add
1984 * it to the front so we find it first.
1985 */
1986 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1987 list_add(&req->list, &ctx->poll_list);
1988 else
1989 list_add_tail(&req->list, &ctx->poll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08001990
1991 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
1992 wq_has_sleeper(&ctx->sqo_wait))
1993 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07001994}
1995
Jens Axboe3d6770f2019-04-13 11:50:54 -06001996static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001997{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001998 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001999 int diff = state->has_refs - state->used_refs;
2000
2001 if (diff)
2002 fput_many(state->file, diff);
2003 state->file = NULL;
2004 }
2005}
2006
2007/*
2008 * Get as many references to a file as we have IOs left in this submission,
2009 * assuming most submissions are for one file, or at least that each file
2010 * has more than one submission.
2011 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002012static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002013{
2014 if (!state)
2015 return fget(fd);
2016
2017 if (state->file) {
2018 if (state->fd == fd) {
2019 state->used_refs++;
2020 state->ios_left--;
2021 return state->file;
2022 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06002023 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002024 }
2025 state->file = fget_many(fd, state->ios_left);
2026 if (!state->file)
2027 return NULL;
2028
2029 state->fd = fd;
2030 state->has_refs = state->ios_left;
2031 state->used_refs = 1;
2032 state->ios_left--;
2033 return state->file;
2034}
2035
Jens Axboe2b188cc2019-01-07 10:46:33 -07002036/*
2037 * If we tracked the file through the SCM inflight mechanism, we could support
2038 * any file. For now, just ensure that anything potentially problematic is done
2039 * inline.
2040 */
2041static bool io_file_supports_async(struct file *file)
2042{
2043 umode_t mode = file_inode(file)->i_mode;
2044
Jens Axboe10d59342019-12-09 20:16:22 -07002045 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002046 return true;
2047 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
2048 return true;
2049
2050 return false;
2051}
2052
Jens Axboe3529d8c2019-12-19 18:24:38 -07002053static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2054 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002055{
Jens Axboedef596e2019-01-09 08:59:42 -07002056 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002057 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002058 unsigned ioprio;
2059 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002060
Jens Axboe491381ce2019-10-17 09:20:46 -06002061 if (S_ISREG(file_inode(req->file)->i_mode))
2062 req->flags |= REQ_F_ISREG;
2063
Jens Axboe2b188cc2019-01-07 10:46:33 -07002064 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002065 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2066 req->flags |= REQ_F_CUR_POS;
2067 kiocb->ki_pos = req->file->f_pos;
2068 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002069 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002070 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2071 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2072 if (unlikely(ret))
2073 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002074
2075 ioprio = READ_ONCE(sqe->ioprio);
2076 if (ioprio) {
2077 ret = ioprio_check_cap(ioprio);
2078 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002079 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002080
2081 kiocb->ki_ioprio = ioprio;
2082 } else
2083 kiocb->ki_ioprio = get_current_ioprio();
2084
Stefan Bühler8449eed2019-04-27 20:34:19 +02002085 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06002086 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
2087 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02002088 req->flags |= REQ_F_NOWAIT;
2089
2090 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002091 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02002092
Jens Axboedef596e2019-01-09 08:59:42 -07002093 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002094 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2095 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002096 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002097
Jens Axboedef596e2019-01-09 08:59:42 -07002098 kiocb->ki_flags |= IOCB_HIPRI;
2099 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06002100 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002101 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002102 if (kiocb->ki_flags & IOCB_HIPRI)
2103 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002104 kiocb->ki_complete = io_complete_rw;
2105 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002106
Jens Axboe3529d8c2019-12-19 18:24:38 -07002107 req->rw.addr = READ_ONCE(sqe->addr);
2108 req->rw.len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002109 /* we own ->private, reuse it for the buffer index / buffer ID */
Jens Axboe9adbd452019-12-20 08:45:55 -07002110 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002111 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002112 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002113}
2114
2115static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2116{
2117 switch (ret) {
2118 case -EIOCBQUEUED:
2119 break;
2120 case -ERESTARTSYS:
2121 case -ERESTARTNOINTR:
2122 case -ERESTARTNOHAND:
2123 case -ERESTART_RESTARTBLOCK:
2124 /*
2125 * We can't just restart the syscall, since previously
2126 * submitted sqes may already be in progress. Just fail this
2127 * IO with EINTR.
2128 */
2129 ret = -EINTR;
2130 /* fall through */
2131 default:
2132 kiocb->ki_complete(kiocb, ret, 0);
2133 }
2134}
2135
Pavel Begunkov014db002020-03-03 21:33:12 +03002136static void kiocb_done(struct kiocb *kiocb, ssize_t ret)
Jens Axboeba816ad2019-09-28 11:36:45 -06002137{
Jens Axboeba042912019-12-25 16:33:42 -07002138 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2139
2140 if (req->flags & REQ_F_CUR_POS)
2141 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002142 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Pavel Begunkov014db002020-03-03 21:33:12 +03002143 io_complete_rw(kiocb, ret, 0);
Jens Axboeba816ad2019-09-28 11:36:45 -06002144 else
2145 io_rw_done(kiocb, ret);
2146}
2147
Jens Axboe9adbd452019-12-20 08:45:55 -07002148static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002149 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002150{
Jens Axboe9adbd452019-12-20 08:45:55 -07002151 struct io_ring_ctx *ctx = req->ctx;
2152 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002153 struct io_mapped_ubuf *imu;
2154 unsigned index, buf_index;
2155 size_t offset;
2156 u64 buf_addr;
2157
2158 /* attempt to use fixed buffers without having provided iovecs */
2159 if (unlikely(!ctx->user_bufs))
2160 return -EFAULT;
2161
Jens Axboe9adbd452019-12-20 08:45:55 -07002162 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07002163 if (unlikely(buf_index >= ctx->nr_user_bufs))
2164 return -EFAULT;
2165
2166 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2167 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002168 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002169
2170 /* overflow */
2171 if (buf_addr + len < buf_addr)
2172 return -EFAULT;
2173 /* not inside the mapped region */
2174 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2175 return -EFAULT;
2176
2177 /*
2178 * May not be a start of buffer, set size appropriately
2179 * and advance us to the beginning.
2180 */
2181 offset = buf_addr - imu->ubuf;
2182 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002183
2184 if (offset) {
2185 /*
2186 * Don't use iov_iter_advance() here, as it's really slow for
2187 * using the latter parts of a big fixed buffer - it iterates
2188 * over each segment manually. We can cheat a bit here, because
2189 * we know that:
2190 *
2191 * 1) it's a BVEC iter, we set it up
2192 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2193 * first and last bvec
2194 *
2195 * So just find our index, and adjust the iterator afterwards.
2196 * If the offset is within the first bvec (or the whole first
2197 * bvec, just use iov_iter_advance(). This makes it easier
2198 * since we can just skip the first segment, which may not
2199 * be PAGE_SIZE aligned.
2200 */
2201 const struct bio_vec *bvec = imu->bvec;
2202
2203 if (offset <= bvec->bv_len) {
2204 iov_iter_advance(iter, offset);
2205 } else {
2206 unsigned long seg_skip;
2207
2208 /* skip first vec */
2209 offset -= bvec->bv_len;
2210 seg_skip = 1 + (offset >> PAGE_SHIFT);
2211
2212 iter->bvec = bvec + seg_skip;
2213 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002214 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002215 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002216 }
2217 }
2218
Jens Axboe5e559562019-11-13 16:12:46 -07002219 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002220}
2221
Jens Axboebcda7ba2020-02-23 16:42:51 -07002222static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2223{
2224 if (needs_lock)
2225 mutex_unlock(&ctx->uring_lock);
2226}
2227
2228static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2229{
2230 /*
2231 * "Normal" inline submissions always hold the uring_lock, since we
2232 * grab it from the system call. Same is true for the SQPOLL offload.
2233 * The only exception is when we've detached the request and issue it
2234 * from an async worker thread, grab the lock for that case.
2235 */
2236 if (needs_lock)
2237 mutex_lock(&ctx->uring_lock);
2238}
2239
2240static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2241 int bgid, struct io_buffer *kbuf,
2242 bool needs_lock)
2243{
2244 struct io_buffer *head;
2245
2246 if (req->flags & REQ_F_BUFFER_SELECTED)
2247 return kbuf;
2248
2249 io_ring_submit_lock(req->ctx, needs_lock);
2250
2251 lockdep_assert_held(&req->ctx->uring_lock);
2252
2253 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2254 if (head) {
2255 if (!list_empty(&head->list)) {
2256 kbuf = list_last_entry(&head->list, struct io_buffer,
2257 list);
2258 list_del(&kbuf->list);
2259 } else {
2260 kbuf = head;
2261 idr_remove(&req->ctx->io_buffer_idr, bgid);
2262 }
2263 if (*len > kbuf->len)
2264 *len = kbuf->len;
2265 } else {
2266 kbuf = ERR_PTR(-ENOBUFS);
2267 }
2268
2269 io_ring_submit_unlock(req->ctx, needs_lock);
2270
2271 return kbuf;
2272}
2273
Jens Axboe4d954c22020-02-27 07:31:19 -07002274static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2275 bool needs_lock)
2276{
2277 struct io_buffer *kbuf;
2278 int bgid;
2279
2280 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2281 bgid = (int) (unsigned long) req->rw.kiocb.private;
2282 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2283 if (IS_ERR(kbuf))
2284 return kbuf;
2285 req->rw.addr = (u64) (unsigned long) kbuf;
2286 req->flags |= REQ_F_BUFFER_SELECTED;
2287 return u64_to_user_ptr(kbuf->addr);
2288}
2289
2290#ifdef CONFIG_COMPAT
2291static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2292 bool needs_lock)
2293{
2294 struct compat_iovec __user *uiov;
2295 compat_ssize_t clen;
2296 void __user *buf;
2297 ssize_t len;
2298
2299 uiov = u64_to_user_ptr(req->rw.addr);
2300 if (!access_ok(uiov, sizeof(*uiov)))
2301 return -EFAULT;
2302 if (__get_user(clen, &uiov->iov_len))
2303 return -EFAULT;
2304 if (clen < 0)
2305 return -EINVAL;
2306
2307 len = clen;
2308 buf = io_rw_buffer_select(req, &len, needs_lock);
2309 if (IS_ERR(buf))
2310 return PTR_ERR(buf);
2311 iov[0].iov_base = buf;
2312 iov[0].iov_len = (compat_size_t) len;
2313 return 0;
2314}
2315#endif
2316
2317static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2318 bool needs_lock)
2319{
2320 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2321 void __user *buf;
2322 ssize_t len;
2323
2324 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2325 return -EFAULT;
2326
2327 len = iov[0].iov_len;
2328 if (len < 0)
2329 return -EINVAL;
2330 buf = io_rw_buffer_select(req, &len, needs_lock);
2331 if (IS_ERR(buf))
2332 return PTR_ERR(buf);
2333 iov[0].iov_base = buf;
2334 iov[0].iov_len = len;
2335 return 0;
2336}
2337
2338static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2339 bool needs_lock)
2340{
2341 if (req->flags & REQ_F_BUFFER_SELECTED)
2342 return 0;
2343 if (!req->rw.len)
2344 return 0;
2345 else if (req->rw.len > 1)
2346 return -EINVAL;
2347
2348#ifdef CONFIG_COMPAT
2349 if (req->ctx->compat)
2350 return io_compat_import(req, iov, needs_lock);
2351#endif
2352
2353 return __io_iov_buffer_select(req, iov, needs_lock);
2354}
2355
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002356static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
Jens Axboebcda7ba2020-02-23 16:42:51 -07002357 struct iovec **iovec, struct iov_iter *iter,
2358 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002359{
Jens Axboe9adbd452019-12-20 08:45:55 -07002360 void __user *buf = u64_to_user_ptr(req->rw.addr);
2361 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002362 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002363 u8 opcode;
2364
Jens Axboed625c6e2019-12-17 19:53:05 -07002365 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002366 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002367 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002368 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002369 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002370
Jens Axboebcda7ba2020-02-23 16:42:51 -07002371 /* buffer index only valid with fixed read/write, or buffer select */
2372 if (req->rw.kiocb.private && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002373 return -EINVAL;
2374
Jens Axboe3a6820f2019-12-22 15:19:35 -07002375 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002376 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002377 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
2378 if (IS_ERR(buf)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002379 *iovec = NULL;
Jens Axboe4d954c22020-02-27 07:31:19 -07002380 return PTR_ERR(buf);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002381 }
Jens Axboe3f9d6442020-03-11 12:27:04 -06002382 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002383 }
2384
Jens Axboe3a6820f2019-12-22 15:19:35 -07002385 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2386 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002387 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002388 }
2389
Jens Axboef67676d2019-12-02 11:03:47 -07002390 if (req->io) {
2391 struct io_async_rw *iorw = &req->io->rw;
2392
2393 *iovec = iorw->iov;
2394 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2395 if (iorw->iov == iorw->fast_iov)
2396 *iovec = NULL;
2397 return iorw->size;
2398 }
2399
Jens Axboe4d954c22020-02-27 07:31:19 -07002400 if (req->flags & REQ_F_BUFFER_SELECT) {
2401 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002402 if (!ret) {
2403 ret = (*iovec)->iov_len;
2404 iov_iter_init(iter, rw, *iovec, 1, ret);
2405 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002406 *iovec = NULL;
2407 return ret;
2408 }
2409
Jens Axboe2b188cc2019-01-07 10:46:33 -07002410#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002411 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002412 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2413 iovec, iter);
2414#endif
2415
2416 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2417}
2418
Jens Axboe32960612019-09-23 11:05:34 -06002419/*
2420 * For files that don't have ->read_iter() and ->write_iter(), handle them
2421 * by looping over ->read() or ->write() manually.
2422 */
2423static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2424 struct iov_iter *iter)
2425{
2426 ssize_t ret = 0;
2427
2428 /*
2429 * Don't support polled IO through this interface, and we can't
2430 * support non-blocking either. For the latter, this just causes
2431 * the kiocb to be handled from an async context.
2432 */
2433 if (kiocb->ki_flags & IOCB_HIPRI)
2434 return -EOPNOTSUPP;
2435 if (kiocb->ki_flags & IOCB_NOWAIT)
2436 return -EAGAIN;
2437
2438 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002439 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002440 ssize_t nr;
2441
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002442 if (!iov_iter_is_bvec(iter)) {
2443 iovec = iov_iter_iovec(iter);
2444 } else {
2445 /* fixed buffers import bvec */
2446 iovec.iov_base = kmap(iter->bvec->bv_page)
2447 + iter->iov_offset;
2448 iovec.iov_len = min(iter->count,
2449 iter->bvec->bv_len - iter->iov_offset);
2450 }
2451
Jens Axboe32960612019-09-23 11:05:34 -06002452 if (rw == READ) {
2453 nr = file->f_op->read(file, iovec.iov_base,
2454 iovec.iov_len, &kiocb->ki_pos);
2455 } else {
2456 nr = file->f_op->write(file, iovec.iov_base,
2457 iovec.iov_len, &kiocb->ki_pos);
2458 }
2459
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002460 if (iov_iter_is_bvec(iter))
2461 kunmap(iter->bvec->bv_page);
2462
Jens Axboe32960612019-09-23 11:05:34 -06002463 if (nr < 0) {
2464 if (!ret)
2465 ret = nr;
2466 break;
2467 }
2468 ret += nr;
2469 if (nr != iovec.iov_len)
2470 break;
2471 iov_iter_advance(iter, nr);
2472 }
2473
2474 return ret;
2475}
2476
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002477static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002478 struct iovec *iovec, struct iovec *fast_iov,
2479 struct iov_iter *iter)
2480{
2481 req->io->rw.nr_segs = iter->nr_segs;
2482 req->io->rw.size = io_size;
2483 req->io->rw.iov = iovec;
2484 if (!req->io->rw.iov) {
2485 req->io->rw.iov = req->io->rw.fast_iov;
Xiaoguang Wang45097da2020-04-08 22:29:58 +08002486 if (req->io->rw.iov != fast_iov)
2487 memcpy(req->io->rw.iov, fast_iov,
2488 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002489 } else {
2490 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002491 }
2492}
2493
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002494static inline int __io_alloc_async_ctx(struct io_kiocb *req)
2495{
2496 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
2497 return req->io == NULL;
2498}
2499
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002500static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002501{
Jens Axboed3656342019-12-18 09:50:26 -07002502 if (!io_op_defs[req->opcode].async_ctx)
2503 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002504
2505 return __io_alloc_async_ctx(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002506}
2507
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002508static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2509 struct iovec *iovec, struct iovec *fast_iov,
2510 struct iov_iter *iter)
2511{
Jens Axboe980ad262020-01-24 23:08:54 -07002512 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002513 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002514 if (!req->io) {
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002515 if (__io_alloc_async_ctx(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07002516 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002517
Jens Axboe5d204bc2020-01-31 12:06:52 -07002518 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2519 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002520 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002521}
2522
Jens Axboe3529d8c2019-12-19 18:24:38 -07002523static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2524 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002525{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002526 struct io_async_ctx *io;
2527 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002528 ssize_t ret;
2529
Jens Axboe3529d8c2019-12-19 18:24:38 -07002530 ret = io_prep_rw(req, sqe, force_nonblock);
2531 if (ret)
2532 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002533
Jens Axboe3529d8c2019-12-19 18:24:38 -07002534 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2535 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002536
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002537 /* either don't need iovec imported or already have it */
2538 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002539 return 0;
2540
2541 io = req->io;
2542 io->rw.iov = io->rw.fast_iov;
2543 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002544 ret = io_import_iovec(READ, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002545 req->io = io;
2546 if (ret < 0)
2547 return ret;
2548
2549 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2550 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002551}
2552
Pavel Begunkov014db002020-03-03 21:33:12 +03002553static int io_read(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002554{
2555 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002556 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002557 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002558 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002559 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002560
Jens Axboebcda7ba2020-02-23 16:42:51 -07002561 ret = io_import_iovec(READ, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07002562 if (ret < 0)
2563 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002564
Jens Axboefd6c2e42019-12-18 12:19:41 -07002565 /* Ensure we clear previously set non-block flag */
2566 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07002567 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002568
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002569 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002570 io_size = ret;
Pavel Begunkovdea3b492020-04-12 02:05:04 +03002571 if (req->flags & REQ_F_LINK_HEAD)
Jens Axboef67676d2019-12-02 11:03:47 -07002572 req->result = io_size;
2573
2574 /*
2575 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2576 * we know to async punt it even if it was opened O_NONBLOCK
2577 */
Jens Axboe29de5f62020-02-20 09:56:08 -07002578 if (force_nonblock && !io_file_supports_async(req->file))
Jens Axboef67676d2019-12-02 11:03:47 -07002579 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002580
Jens Axboe31b51512019-01-18 22:56:34 -07002581 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002582 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002583 if (!ret) {
2584 ssize_t ret2;
2585
Jens Axboe9adbd452019-12-20 08:45:55 -07002586 if (req->file->f_op->read_iter)
2587 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002588 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002589 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002590
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002591 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002592 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkov014db002020-03-03 21:33:12 +03002593 kiocb_done(kiocb, ret2);
Jens Axboef67676d2019-12-02 11:03:47 -07002594 } else {
2595copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002596 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002597 inline_vecs, &iter);
2598 if (ret)
2599 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002600 /* any defer here is final, must blocking retry */
2601 if (!(req->flags & REQ_F_NOWAIT))
2602 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002603 return -EAGAIN;
2604 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002605 }
Jens Axboef67676d2019-12-02 11:03:47 -07002606out_free:
Pavel Begunkov1e950812020-02-06 19:51:16 +03002607 kfree(iovec);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002608 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002609 return ret;
2610}
2611
Jens Axboe3529d8c2019-12-19 18:24:38 -07002612static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2613 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002614{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002615 struct io_async_ctx *io;
2616 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002617 ssize_t ret;
2618
Jens Axboe3529d8c2019-12-19 18:24:38 -07002619 ret = io_prep_rw(req, sqe, force_nonblock);
2620 if (ret)
2621 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002622
Jens Axboe3529d8c2019-12-19 18:24:38 -07002623 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2624 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002625
Jens Axboe4ed734b2020-03-20 11:23:41 -06002626 req->fsize = rlimit(RLIMIT_FSIZE);
2627
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002628 /* either don't need iovec imported or already have it */
2629 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002630 return 0;
2631
2632 io = req->io;
2633 io->rw.iov = io->rw.fast_iov;
2634 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002635 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002636 req->io = io;
2637 if (ret < 0)
2638 return ret;
2639
2640 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2641 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002642}
2643
Pavel Begunkov014db002020-03-03 21:33:12 +03002644static int io_write(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002645{
2646 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002647 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002648 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002649 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002650 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002651
Jens Axboebcda7ba2020-02-23 16:42:51 -07002652 ret = io_import_iovec(WRITE, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07002653 if (ret < 0)
2654 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002655
Jens Axboefd6c2e42019-12-18 12:19:41 -07002656 /* Ensure we clear previously set non-block flag */
2657 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002658 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002659
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002660 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002661 io_size = ret;
Pavel Begunkovdea3b492020-04-12 02:05:04 +03002662 if (req->flags & REQ_F_LINK_HEAD)
Jens Axboef67676d2019-12-02 11:03:47 -07002663 req->result = io_size;
2664
2665 /*
2666 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2667 * we know to async punt it even if it was opened O_NONBLOCK
2668 */
Jens Axboe29de5f62020-02-20 09:56:08 -07002669 if (force_nonblock && !io_file_supports_async(req->file))
Jens Axboef67676d2019-12-02 11:03:47 -07002670 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07002671
Jens Axboe10d59342019-12-09 20:16:22 -07002672 /* file path doesn't support NOWAIT for non-direct_IO */
2673 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2674 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002675 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002676
Jens Axboe31b51512019-01-18 22:56:34 -07002677 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002678 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002679 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002680 ssize_t ret2;
2681
Jens Axboe2b188cc2019-01-07 10:46:33 -07002682 /*
2683 * Open-code file_start_write here to grab freeze protection,
2684 * which will be released by another thread in
2685 * io_complete_rw(). Fool lockdep by telling it the lock got
2686 * released so that it doesn't complain about the held lock when
2687 * we return to userspace.
2688 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002689 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002690 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002691 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002692 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002693 SB_FREEZE_WRITE);
2694 }
2695 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002696
Jens Axboe4ed734b2020-03-20 11:23:41 -06002697 if (!force_nonblock)
2698 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
2699
Jens Axboe9adbd452019-12-20 08:45:55 -07002700 if (req->file->f_op->write_iter)
2701 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002702 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002703 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboe4ed734b2020-03-20 11:23:41 -06002704
2705 if (!force_nonblock)
2706 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
2707
Jens Axboefaac9962020-02-07 15:45:22 -07002708 /*
Chucheng Luobff60352020-03-25 11:31:38 +08002709 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
Jens Axboefaac9962020-02-07 15:45:22 -07002710 * retry them without IOCB_NOWAIT.
2711 */
2712 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
2713 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07002714 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkov014db002020-03-03 21:33:12 +03002715 kiocb_done(kiocb, ret2);
Jens Axboef67676d2019-12-02 11:03:47 -07002716 } else {
2717copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002718 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002719 inline_vecs, &iter);
2720 if (ret)
2721 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002722 /* any defer here is final, must blocking retry */
2723 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002724 return -EAGAIN;
2725 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002726 }
Jens Axboe31b51512019-01-18 22:56:34 -07002727out_free:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002728 req->flags &= ~REQ_F_NEED_CLEANUP;
Pavel Begunkov1e950812020-02-06 19:51:16 +03002729 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002730 return ret;
2731}
2732
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002733static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2734{
2735 struct io_splice* sp = &req->splice;
2736 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
2737 int ret;
2738
2739 if (req->flags & REQ_F_NEED_CLEANUP)
2740 return 0;
2741
2742 sp->file_in = NULL;
2743 sp->off_in = READ_ONCE(sqe->splice_off_in);
2744 sp->off_out = READ_ONCE(sqe->off);
2745 sp->len = READ_ONCE(sqe->len);
2746 sp->flags = READ_ONCE(sqe->splice_flags);
2747
2748 if (unlikely(sp->flags & ~valid_flags))
2749 return -EINVAL;
2750
2751 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
2752 (sp->flags & SPLICE_F_FD_IN_FIXED));
2753 if (ret)
2754 return ret;
2755 req->flags |= REQ_F_NEED_CLEANUP;
2756
2757 if (!S_ISREG(file_inode(sp->file_in)->i_mode))
2758 req->work.flags |= IO_WQ_WORK_UNBOUND;
2759
2760 return 0;
2761}
2762
2763static bool io_splice_punt(struct file *file)
2764{
2765 if (get_pipe_info(file))
2766 return false;
2767 if (!io_file_supports_async(file))
2768 return true;
Jens Axboe88357582020-04-12 21:12:49 -06002769 return !(file->f_flags & O_NONBLOCK);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002770}
2771
Pavel Begunkov014db002020-03-03 21:33:12 +03002772static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002773{
2774 struct io_splice *sp = &req->splice;
2775 struct file *in = sp->file_in;
2776 struct file *out = sp->file_out;
2777 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
2778 loff_t *poff_in, *poff_out;
2779 long ret;
2780
2781 if (force_nonblock) {
2782 if (io_splice_punt(in) || io_splice_punt(out))
2783 return -EAGAIN;
2784 flags |= SPLICE_F_NONBLOCK;
2785 }
2786
2787 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
2788 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
2789 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
2790 if (force_nonblock && ret == -EAGAIN)
2791 return -EAGAIN;
2792
2793 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
2794 req->flags &= ~REQ_F_NEED_CLEANUP;
2795
2796 io_cqring_add_event(req, ret);
2797 if (ret != sp->len)
2798 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03002799 io_put_req(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002800 return 0;
2801}
2802
Jens Axboe2b188cc2019-01-07 10:46:33 -07002803/*
2804 * IORING_OP_NOP just posts a completion event, nothing else.
2805 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002806static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002807{
2808 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002809
Jens Axboedef596e2019-01-09 08:59:42 -07002810 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2811 return -EINVAL;
2812
Jens Axboe78e19bb2019-11-06 15:21:34 -07002813 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002814 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002815 return 0;
2816}
2817
Jens Axboe3529d8c2019-12-19 18:24:38 -07002818static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002819{
Jens Axboe6b063142019-01-10 22:13:58 -07002820 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002821
Jens Axboe09bb8392019-03-13 12:39:28 -06002822 if (!req->file)
2823 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002824
Jens Axboe6b063142019-01-10 22:13:58 -07002825 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002826 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002827 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002828 return -EINVAL;
2829
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002830 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2831 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2832 return -EINVAL;
2833
2834 req->sync.off = READ_ONCE(sqe->off);
2835 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002836 return 0;
2837}
2838
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002839static bool io_req_cancelled(struct io_kiocb *req)
2840{
2841 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2842 req_set_fail_links(req);
2843 io_cqring_add_event(req, -ECANCELED);
2844 io_put_req(req);
2845 return true;
2846 }
2847
2848 return false;
2849}
2850
Pavel Begunkov014db002020-03-03 21:33:12 +03002851static void __io_fsync(struct io_kiocb *req)
Jens Axboe78912932020-01-14 22:09:06 -07002852{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002853 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002854 int ret;
2855
Jens Axboe9adbd452019-12-20 08:45:55 -07002856 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002857 end > 0 ? end : LLONG_MAX,
2858 req->sync.flags & IORING_FSYNC_DATASYNC);
2859 if (ret < 0)
2860 req_set_fail_links(req);
2861 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002862 io_put_req(req);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002863}
2864
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002865static void io_fsync_finish(struct io_wq_work **workptr)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002866{
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002867 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002868
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002869 if (io_req_cancelled(req))
2870 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03002871 __io_fsync(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03002872 io_steal_work(req, workptr);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002873}
2874
Pavel Begunkov014db002020-03-03 21:33:12 +03002875static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002876{
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002877 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002878 if (force_nonblock) {
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002879 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002880 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002881 }
Pavel Begunkov014db002020-03-03 21:33:12 +03002882 __io_fsync(req);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002883 return 0;
2884}
2885
Pavel Begunkov014db002020-03-03 21:33:12 +03002886static void __io_fallocate(struct io_kiocb *req)
Jens Axboed63d1b52019-12-10 10:38:56 -07002887{
Jens Axboed63d1b52019-12-10 10:38:56 -07002888 int ret;
2889
Jens Axboe4ed734b2020-03-20 11:23:41 -06002890 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
Jens Axboed63d1b52019-12-10 10:38:56 -07002891 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2892 req->sync.len);
Jens Axboe4ed734b2020-03-20 11:23:41 -06002893 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
Jens Axboed63d1b52019-12-10 10:38:56 -07002894 if (ret < 0)
2895 req_set_fail_links(req);
2896 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002897 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002898}
2899
Jens Axboe2b188cc2019-01-07 10:46:33 -07002900static void io_fallocate_finish(struct io_wq_work **workptr)
2901{
2902 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Jens Axboed63d1b52019-12-10 10:38:56 -07002903
2904 if (io_req_cancelled(req))
2905 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03002906 __io_fallocate(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03002907 io_steal_work(req, workptr);
Jens Axboed63d1b52019-12-10 10:38:56 -07002908}
2909
2910static int io_fallocate_prep(struct io_kiocb *req,
2911 const struct io_uring_sqe *sqe)
2912{
2913 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2914 return -EINVAL;
2915
2916 req->sync.off = READ_ONCE(sqe->off);
2917 req->sync.len = READ_ONCE(sqe->addr);
2918 req->sync.mode = READ_ONCE(sqe->len);
Jens Axboe4ed734b2020-03-20 11:23:41 -06002919 req->fsize = rlimit(RLIMIT_FSIZE);
Jens Axboed63d1b52019-12-10 10:38:56 -07002920 return 0;
2921}
2922
Pavel Begunkov014db002020-03-03 21:33:12 +03002923static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07002924{
Jens Axboed63d1b52019-12-10 10:38:56 -07002925 /* fallocate always requiring blocking context */
2926 if (force_nonblock) {
Jens Axboed63d1b52019-12-10 10:38:56 -07002927 req->work.func = io_fallocate_finish;
2928 return -EAGAIN;
2929 }
2930
Pavel Begunkov014db002020-03-03 21:33:12 +03002931 __io_fallocate(req);
Jens Axboed63d1b52019-12-10 10:38:56 -07002932 return 0;
2933}
2934
Jens Axboe15b71ab2019-12-11 11:20:36 -07002935static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2936{
Jens Axboef8748882020-01-08 17:47:02 -07002937 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002938 int ret;
2939
2940 if (sqe->ioprio || sqe->buf_index)
2941 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03002942 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07002943 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002944 if (req->flags & REQ_F_NEED_CLEANUP)
2945 return 0;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002946
2947 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002948 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002949 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002950 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06002951 if (force_o_largefile())
2952 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002953
Jens Axboef8748882020-01-08 17:47:02 -07002954 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002955 if (IS_ERR(req->open.filename)) {
2956 ret = PTR_ERR(req->open.filename);
2957 req->open.filename = NULL;
2958 return ret;
2959 }
2960
Jens Axboe4022e7a2020-03-19 19:23:18 -06002961 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002962 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002963 return 0;
2964}
2965
Jens Axboecebdb982020-01-08 17:59:24 -07002966static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2967{
2968 struct open_how __user *how;
2969 const char __user *fname;
2970 size_t len;
2971 int ret;
2972
2973 if (sqe->ioprio || sqe->buf_index)
2974 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03002975 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07002976 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002977 if (req->flags & REQ_F_NEED_CLEANUP)
2978 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07002979
2980 req->open.dfd = READ_ONCE(sqe->fd);
2981 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2982 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2983 len = READ_ONCE(sqe->len);
2984
2985 if (len < OPEN_HOW_SIZE_VER0)
2986 return -EINVAL;
2987
2988 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2989 len);
2990 if (ret)
2991 return ret;
2992
2993 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2994 req->open.how.flags |= O_LARGEFILE;
2995
2996 req->open.filename = getname(fname);
2997 if (IS_ERR(req->open.filename)) {
2998 ret = PTR_ERR(req->open.filename);
2999 req->open.filename = NULL;
3000 return ret;
3001 }
3002
Jens Axboe4022e7a2020-03-19 19:23:18 -06003003 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003004 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboecebdb982020-01-08 17:59:24 -07003005 return 0;
3006}
3007
Pavel Begunkov014db002020-03-03 21:33:12 +03003008static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003009{
3010 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003011 struct file *file;
3012 int ret;
3013
Jens Axboef86cd202020-01-29 13:46:44 -07003014 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003015 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003016
Jens Axboecebdb982020-01-08 17:59:24 -07003017 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003018 if (ret)
3019 goto err;
3020
Jens Axboe4022e7a2020-03-19 19:23:18 -06003021 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003022 if (ret < 0)
3023 goto err;
3024
3025 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3026 if (IS_ERR(file)) {
3027 put_unused_fd(ret);
3028 ret = PTR_ERR(file);
3029 } else {
3030 fsnotify_open(file);
3031 fd_install(ret, file);
3032 }
3033err:
3034 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003035 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003036 if (ret < 0)
3037 req_set_fail_links(req);
3038 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003039 io_put_req(req);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003040 return 0;
3041}
3042
Pavel Begunkov014db002020-03-03 21:33:12 +03003043static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003044{
3045 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
Pavel Begunkov014db002020-03-03 21:33:12 +03003046 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003047}
3048
Jens Axboe067524e2020-03-02 16:32:28 -07003049static int io_remove_buffers_prep(struct io_kiocb *req,
3050 const struct io_uring_sqe *sqe)
3051{
3052 struct io_provide_buf *p = &req->pbuf;
3053 u64 tmp;
3054
3055 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3056 return -EINVAL;
3057
3058 tmp = READ_ONCE(sqe->fd);
3059 if (!tmp || tmp > USHRT_MAX)
3060 return -EINVAL;
3061
3062 memset(p, 0, sizeof(*p));
3063 p->nbufs = tmp;
3064 p->bgid = READ_ONCE(sqe->buf_group);
3065 return 0;
3066}
3067
3068static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3069 int bgid, unsigned nbufs)
3070{
3071 unsigned i = 0;
3072
3073 /* shouldn't happen */
3074 if (!nbufs)
3075 return 0;
3076
3077 /* the head kbuf is the list itself */
3078 while (!list_empty(&buf->list)) {
3079 struct io_buffer *nxt;
3080
3081 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3082 list_del(&nxt->list);
3083 kfree(nxt);
3084 if (++i == nbufs)
3085 return i;
3086 }
3087 i++;
3088 kfree(buf);
3089 idr_remove(&ctx->io_buffer_idr, bgid);
3090
3091 return i;
3092}
3093
3094static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock)
3095{
3096 struct io_provide_buf *p = &req->pbuf;
3097 struct io_ring_ctx *ctx = req->ctx;
3098 struct io_buffer *head;
3099 int ret = 0;
3100
3101 io_ring_submit_lock(ctx, !force_nonblock);
3102
3103 lockdep_assert_held(&ctx->uring_lock);
3104
3105 ret = -ENOENT;
3106 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3107 if (head)
3108 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3109
3110 io_ring_submit_lock(ctx, !force_nonblock);
3111 if (ret < 0)
3112 req_set_fail_links(req);
3113 io_cqring_add_event(req, ret);
3114 io_put_req(req);
3115 return 0;
3116}
3117
Jens Axboeddf0322d2020-02-23 16:41:33 -07003118static int io_provide_buffers_prep(struct io_kiocb *req,
3119 const struct io_uring_sqe *sqe)
3120{
3121 struct io_provide_buf *p = &req->pbuf;
3122 u64 tmp;
3123
3124 if (sqe->ioprio || sqe->rw_flags)
3125 return -EINVAL;
3126
3127 tmp = READ_ONCE(sqe->fd);
3128 if (!tmp || tmp > USHRT_MAX)
3129 return -E2BIG;
3130 p->nbufs = tmp;
3131 p->addr = READ_ONCE(sqe->addr);
3132 p->len = READ_ONCE(sqe->len);
3133
3134 if (!access_ok(u64_to_user_ptr(p->addr), p->len))
3135 return -EFAULT;
3136
3137 p->bgid = READ_ONCE(sqe->buf_group);
3138 tmp = READ_ONCE(sqe->off);
3139 if (tmp > USHRT_MAX)
3140 return -E2BIG;
3141 p->bid = tmp;
3142 return 0;
3143}
3144
3145static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3146{
3147 struct io_buffer *buf;
3148 u64 addr = pbuf->addr;
3149 int i, bid = pbuf->bid;
3150
3151 for (i = 0; i < pbuf->nbufs; i++) {
3152 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3153 if (!buf)
3154 break;
3155
3156 buf->addr = addr;
3157 buf->len = pbuf->len;
3158 buf->bid = bid;
3159 addr += pbuf->len;
3160 bid++;
3161 if (!*head) {
3162 INIT_LIST_HEAD(&buf->list);
3163 *head = buf;
3164 } else {
3165 list_add_tail(&buf->list, &(*head)->list);
3166 }
3167 }
3168
3169 return i ? i : -ENOMEM;
3170}
3171
Jens Axboeddf0322d2020-02-23 16:41:33 -07003172static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock)
3173{
3174 struct io_provide_buf *p = &req->pbuf;
3175 struct io_ring_ctx *ctx = req->ctx;
3176 struct io_buffer *head, *list;
3177 int ret = 0;
3178
3179 io_ring_submit_lock(ctx, !force_nonblock);
3180
3181 lockdep_assert_held(&ctx->uring_lock);
3182
3183 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3184
3185 ret = io_add_buffers(p, &head);
3186 if (ret < 0)
3187 goto out;
3188
3189 if (!list) {
3190 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3191 GFP_KERNEL);
3192 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003193 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003194 goto out;
3195 }
3196 }
3197out:
3198 io_ring_submit_unlock(ctx, !force_nonblock);
3199 if (ret < 0)
3200 req_set_fail_links(req);
3201 io_cqring_add_event(req, ret);
3202 io_put_req(req);
3203 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003204}
3205
Jens Axboe3e4827b2020-01-08 15:18:09 -07003206static int io_epoll_ctl_prep(struct io_kiocb *req,
3207 const struct io_uring_sqe *sqe)
3208{
3209#if defined(CONFIG_EPOLL)
3210 if (sqe->ioprio || sqe->buf_index)
3211 return -EINVAL;
3212
3213 req->epoll.epfd = READ_ONCE(sqe->fd);
3214 req->epoll.op = READ_ONCE(sqe->len);
3215 req->epoll.fd = READ_ONCE(sqe->off);
3216
3217 if (ep_op_has_event(req->epoll.op)) {
3218 struct epoll_event __user *ev;
3219
3220 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3221 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3222 return -EFAULT;
3223 }
3224
3225 return 0;
3226#else
3227 return -EOPNOTSUPP;
3228#endif
3229}
3230
Pavel Begunkov014db002020-03-03 21:33:12 +03003231static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003232{
3233#if defined(CONFIG_EPOLL)
3234 struct io_epoll *ie = &req->epoll;
3235 int ret;
3236
3237 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3238 if (force_nonblock && ret == -EAGAIN)
3239 return -EAGAIN;
3240
3241 if (ret < 0)
3242 req_set_fail_links(req);
3243 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003244 io_put_req(req);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003245 return 0;
3246#else
3247 return -EOPNOTSUPP;
3248#endif
3249}
3250
Jens Axboec1ca7572019-12-25 22:18:28 -07003251static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3252{
3253#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3254 if (sqe->ioprio || sqe->buf_index || sqe->off)
3255 return -EINVAL;
3256
3257 req->madvise.addr = READ_ONCE(sqe->addr);
3258 req->madvise.len = READ_ONCE(sqe->len);
3259 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3260 return 0;
3261#else
3262 return -EOPNOTSUPP;
3263#endif
3264}
3265
Pavel Begunkov014db002020-03-03 21:33:12 +03003266static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003267{
3268#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3269 struct io_madvise *ma = &req->madvise;
3270 int ret;
3271
3272 if (force_nonblock)
3273 return -EAGAIN;
3274
3275 ret = do_madvise(ma->addr, ma->len, ma->advice);
3276 if (ret < 0)
3277 req_set_fail_links(req);
3278 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003279 io_put_req(req);
Jens Axboec1ca7572019-12-25 22:18:28 -07003280 return 0;
3281#else
3282 return -EOPNOTSUPP;
3283#endif
3284}
3285
Jens Axboe4840e412019-12-25 22:03:45 -07003286static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3287{
3288 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3289 return -EINVAL;
3290
3291 req->fadvise.offset = READ_ONCE(sqe->off);
3292 req->fadvise.len = READ_ONCE(sqe->len);
3293 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3294 return 0;
3295}
3296
Pavel Begunkov014db002020-03-03 21:33:12 +03003297static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07003298{
3299 struct io_fadvise *fa = &req->fadvise;
3300 int ret;
3301
Jens Axboe3e694262020-02-01 09:22:49 -07003302 if (force_nonblock) {
3303 switch (fa->advice) {
3304 case POSIX_FADV_NORMAL:
3305 case POSIX_FADV_RANDOM:
3306 case POSIX_FADV_SEQUENTIAL:
3307 break;
3308 default:
3309 return -EAGAIN;
3310 }
3311 }
Jens Axboe4840e412019-12-25 22:03:45 -07003312
3313 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3314 if (ret < 0)
3315 req_set_fail_links(req);
3316 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003317 io_put_req(req);
Jens Axboe4840e412019-12-25 22:03:45 -07003318 return 0;
3319}
3320
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003321static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3322{
Jens Axboef8748882020-01-08 17:47:02 -07003323 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003324 unsigned lookup_flags;
3325 int ret;
3326
3327 if (sqe->ioprio || sqe->buf_index)
3328 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003329 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003330 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003331 if (req->flags & REQ_F_NEED_CLEANUP)
3332 return 0;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003333
3334 req->open.dfd = READ_ONCE(sqe->fd);
3335 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07003336 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003337 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07003338 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003339
Jens Axboec12cedf2020-01-08 17:41:21 -07003340 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003341 return -EINVAL;
3342
Jens Axboef8748882020-01-08 17:47:02 -07003343 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003344 if (IS_ERR(req->open.filename)) {
3345 ret = PTR_ERR(req->open.filename);
3346 req->open.filename = NULL;
3347 return ret;
3348 }
3349
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003350 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003351 return 0;
3352}
3353
Pavel Begunkov014db002020-03-03 21:33:12 +03003354static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003355{
3356 struct io_open *ctx = &req->open;
3357 unsigned lookup_flags;
3358 struct path path;
3359 struct kstat stat;
3360 int ret;
3361
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003362 if (force_nonblock) {
3363 /* only need file table for an actual valid fd */
3364 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
3365 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003366 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003367 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003368
Jens Axboec12cedf2020-01-08 17:41:21 -07003369 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003370 return -EINVAL;
3371
3372retry:
3373 /* filename_lookup() drops it, keep a reference */
3374 ctx->filename->refcnt++;
3375
3376 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
3377 NULL);
3378 if (ret)
3379 goto err;
3380
Jens Axboec12cedf2020-01-08 17:41:21 -07003381 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003382 path_put(&path);
3383 if (retry_estale(ret, lookup_flags)) {
3384 lookup_flags |= LOOKUP_REVAL;
3385 goto retry;
3386 }
3387 if (!ret)
3388 ret = cp_statx(&stat, ctx->buffer);
3389err:
3390 putname(ctx->filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003391 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003392 if (ret < 0)
3393 req_set_fail_links(req);
3394 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003395 io_put_req(req);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003396 return 0;
3397}
3398
Jens Axboeb5dba592019-12-11 14:02:38 -07003399static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3400{
3401 /*
3402 * If we queue this for async, it must not be cancellable. That would
3403 * leave the 'file' in an undeterminate state.
3404 */
3405 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
3406
3407 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
3408 sqe->rw_flags || sqe->buf_index)
3409 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003410 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003411 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07003412
3413 req->close.fd = READ_ONCE(sqe->fd);
3414 if (req->file->f_op == &io_uring_fops ||
Pavel Begunkovb14cca02020-01-17 04:45:59 +03003415 req->close.fd == req->ctx->ring_fd)
Jens Axboeb5dba592019-12-11 14:02:38 -07003416 return -EBADF;
3417
3418 return 0;
3419}
3420
Pavel Begunkova93b3332020-02-08 14:04:34 +03003421/* only called when __close_fd_get_file() is done */
Pavel Begunkov014db002020-03-03 21:33:12 +03003422static void __io_close_finish(struct io_kiocb *req)
Pavel Begunkova93b3332020-02-08 14:04:34 +03003423{
3424 int ret;
3425
3426 ret = filp_close(req->close.put_file, req->work.files);
3427 if (ret < 0)
3428 req_set_fail_links(req);
3429 io_cqring_add_event(req, ret);
3430 fput(req->close.put_file);
Pavel Begunkov014db002020-03-03 21:33:12 +03003431 io_put_req(req);
Pavel Begunkova93b3332020-02-08 14:04:34 +03003432}
3433
Jens Axboeb5dba592019-12-11 14:02:38 -07003434static void io_close_finish(struct io_wq_work **workptr)
3435{
3436 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Jens Axboeb5dba592019-12-11 14:02:38 -07003437
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03003438 /* not cancellable, don't do io_req_cancelled() */
Pavel Begunkov014db002020-03-03 21:33:12 +03003439 __io_close_finish(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03003440 io_steal_work(req, workptr);
Jens Axboeb5dba592019-12-11 14:02:38 -07003441}
3442
Pavel Begunkov014db002020-03-03 21:33:12 +03003443static int io_close(struct io_kiocb *req, bool force_nonblock)
Jens Axboeb5dba592019-12-11 14:02:38 -07003444{
3445 int ret;
3446
3447 req->close.put_file = NULL;
3448 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
3449 if (ret < 0)
3450 return ret;
3451
3452 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkova2100672020-03-02 23:45:16 +03003453 if (req->close.put_file->f_op->flush && force_nonblock) {
Pavel Begunkov594506f2020-03-03 21:33:11 +03003454 /* submission ref will be dropped, take it for async */
3455 refcount_inc(&req->refs);
3456
Pavel Begunkova2100672020-03-02 23:45:16 +03003457 req->work.func = io_close_finish;
3458 /*
3459 * Do manual async queue here to avoid grabbing files - we don't
3460 * need the files, and it'll cause io_close_finish() to close
3461 * the file again and cause a double CQE entry for this request
3462 */
3463 io_queue_async_work(req);
3464 return 0;
3465 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003466
3467 /*
3468 * No ->flush(), safely close from here and just punt the
3469 * fput() to async context.
3470 */
Pavel Begunkov014db002020-03-03 21:33:12 +03003471 __io_close_finish(req);
Jens Axboe1a417f42020-01-31 17:16:48 -07003472 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07003473}
3474
Jens Axboe3529d8c2019-12-19 18:24:38 -07003475static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003476{
3477 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003478
3479 if (!req->file)
3480 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003481
3482 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3483 return -EINVAL;
3484 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
3485 return -EINVAL;
3486
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003487 req->sync.off = READ_ONCE(sqe->off);
3488 req->sync.len = READ_ONCE(sqe->len);
3489 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003490 return 0;
3491}
3492
Pavel Begunkov014db002020-03-03 21:33:12 +03003493static void __io_sync_file_range(struct io_kiocb *req)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003494{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003495 int ret;
3496
Jens Axboe9adbd452019-12-20 08:45:55 -07003497 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003498 req->sync.flags);
3499 if (ret < 0)
3500 req_set_fail_links(req);
3501 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003502 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03003503}
3504
3505
3506static void io_sync_file_range_finish(struct io_wq_work **workptr)
3507{
3508 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03003509
3510 if (io_req_cancelled(req))
3511 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03003512 __io_sync_file_range(req);
Pavel Begunkov594506f2020-03-03 21:33:11 +03003513 io_put_req(req); /* put submission ref */
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003514}
3515
Pavel Begunkov014db002020-03-03 21:33:12 +03003516static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003517{
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003518 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003519 if (force_nonblock) {
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003520 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003521 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003522 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003523
Pavel Begunkov014db002020-03-03 21:33:12 +03003524 __io_sync_file_range(req);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003525 return 0;
3526}
3527
YueHaibing469956e2020-03-04 15:53:52 +08003528#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003529static int io_setup_async_msg(struct io_kiocb *req,
3530 struct io_async_msghdr *kmsg)
3531{
3532 if (req->io)
3533 return -EAGAIN;
3534 if (io_alloc_async_ctx(req)) {
3535 if (kmsg->iov != kmsg->fast_iov)
3536 kfree(kmsg->iov);
3537 return -ENOMEM;
3538 }
3539 req->flags |= REQ_F_NEED_CLEANUP;
3540 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
3541 return -EAGAIN;
3542}
3543
Jens Axboe3529d8c2019-12-19 18:24:38 -07003544static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06003545{
Jens Axboee47293f2019-12-20 08:58:21 -07003546 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003547 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003548 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003549
Jens Axboee47293f2019-12-20 08:58:21 -07003550 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3551 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07003552 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003553
Jens Axboed8768362020-02-27 14:17:49 -07003554#ifdef CONFIG_COMPAT
3555 if (req->ctx->compat)
3556 sr->msg_flags |= MSG_CMSG_COMPAT;
3557#endif
3558
Jens Axboefddafac2020-01-04 20:19:44 -07003559 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003560 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003561 /* iovec is already imported */
3562 if (req->flags & REQ_F_NEED_CLEANUP)
3563 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003564
Jens Axboed9688562019-12-09 19:35:20 -07003565 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003566 ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003567 &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003568 if (!ret)
3569 req->flags |= REQ_F_NEED_CLEANUP;
3570 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003571}
3572
Pavel Begunkov014db002020-03-03 21:33:12 +03003573static int io_sendmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003574{
Jens Axboe0b416c32019-12-15 10:57:46 -07003575 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07003576 struct socket *sock;
3577 int ret;
3578
3579 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3580 return -EINVAL;
3581
3582 sock = sock_from_file(req->file, &ret);
3583 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003584 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003585 unsigned flags;
3586
Jens Axboe03b12302019-12-02 18:50:25 -07003587 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003588 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003589 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003590 /* if iov is set, it's allocated already */
3591 if (!kmsg->iov)
3592 kmsg->iov = kmsg->fast_iov;
3593 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003594 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003595 struct io_sr_msg *sr = &req->sr_msg;
3596
Jens Axboe0b416c32019-12-15 10:57:46 -07003597 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003598 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003599
3600 io.msg.iov = io.msg.fast_iov;
3601 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3602 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003603 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003604 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003605 }
3606
Jens Axboee47293f2019-12-20 08:58:21 -07003607 flags = req->sr_msg.msg_flags;
3608 if (flags & MSG_DONTWAIT)
3609 req->flags |= REQ_F_NOWAIT;
3610 else if (force_nonblock)
3611 flags |= MSG_DONTWAIT;
3612
Jens Axboe0b416c32019-12-15 10:57:46 -07003613 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003614 if (force_nonblock && ret == -EAGAIN)
3615 return io_setup_async_msg(req, kmsg);
Jens Axboe03b12302019-12-02 18:50:25 -07003616 if (ret == -ERESTARTSYS)
3617 ret = -EINTR;
3618 }
3619
Pavel Begunkov1e950812020-02-06 19:51:16 +03003620 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003621 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003622 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe03b12302019-12-02 18:50:25 -07003623 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003624 if (ret < 0)
3625 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003626 io_put_req(req);
Jens Axboe03b12302019-12-02 18:50:25 -07003627 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003628}
3629
Pavel Begunkov014db002020-03-03 21:33:12 +03003630static int io_send(struct io_kiocb *req, bool force_nonblock)
Jens Axboefddafac2020-01-04 20:19:44 -07003631{
Jens Axboefddafac2020-01-04 20:19:44 -07003632 struct socket *sock;
3633 int ret;
3634
3635 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3636 return -EINVAL;
3637
3638 sock = sock_from_file(req->file, &ret);
3639 if (sock) {
3640 struct io_sr_msg *sr = &req->sr_msg;
3641 struct msghdr msg;
3642 struct iovec iov;
3643 unsigned flags;
3644
3645 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3646 &msg.msg_iter);
3647 if (ret)
3648 return ret;
3649
3650 msg.msg_name = NULL;
3651 msg.msg_control = NULL;
3652 msg.msg_controllen = 0;
3653 msg.msg_namelen = 0;
3654
3655 flags = req->sr_msg.msg_flags;
3656 if (flags & MSG_DONTWAIT)
3657 req->flags |= REQ_F_NOWAIT;
3658 else if (force_nonblock)
3659 flags |= MSG_DONTWAIT;
3660
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003661 msg.msg_flags = flags;
3662 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003663 if (force_nonblock && ret == -EAGAIN)
3664 return -EAGAIN;
3665 if (ret == -ERESTARTSYS)
3666 ret = -EINTR;
3667 }
3668
3669 io_cqring_add_event(req, ret);
3670 if (ret < 0)
3671 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003672 io_put_req(req);
Jens Axboefddafac2020-01-04 20:19:44 -07003673 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003674}
3675
Jens Axboe52de1fe2020-02-27 10:15:42 -07003676static int __io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
3677{
3678 struct io_sr_msg *sr = &req->sr_msg;
3679 struct iovec __user *uiov;
3680 size_t iov_len;
3681 int ret;
3682
3683 ret = __copy_msghdr_from_user(&io->msg.msg, sr->msg, &io->msg.uaddr,
3684 &uiov, &iov_len);
3685 if (ret)
3686 return ret;
3687
3688 if (req->flags & REQ_F_BUFFER_SELECT) {
3689 if (iov_len > 1)
3690 return -EINVAL;
3691 if (copy_from_user(io->msg.iov, uiov, sizeof(*uiov)))
3692 return -EFAULT;
3693 sr->len = io->msg.iov[0].iov_len;
3694 iov_iter_init(&io->msg.msg.msg_iter, READ, io->msg.iov, 1,
3695 sr->len);
3696 io->msg.iov = NULL;
3697 } else {
3698 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
3699 &io->msg.iov, &io->msg.msg.msg_iter);
3700 if (ret > 0)
3701 ret = 0;
3702 }
3703
3704 return ret;
3705}
3706
3707#ifdef CONFIG_COMPAT
3708static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
3709 struct io_async_ctx *io)
3710{
3711 struct compat_msghdr __user *msg_compat;
3712 struct io_sr_msg *sr = &req->sr_msg;
3713 struct compat_iovec __user *uiov;
3714 compat_uptr_t ptr;
3715 compat_size_t len;
3716 int ret;
3717
3718 msg_compat = (struct compat_msghdr __user *) sr->msg;
3719 ret = __get_compat_msghdr(&io->msg.msg, msg_compat, &io->msg.uaddr,
3720 &ptr, &len);
3721 if (ret)
3722 return ret;
3723
3724 uiov = compat_ptr(ptr);
3725 if (req->flags & REQ_F_BUFFER_SELECT) {
3726 compat_ssize_t clen;
3727
3728 if (len > 1)
3729 return -EINVAL;
3730 if (!access_ok(uiov, sizeof(*uiov)))
3731 return -EFAULT;
3732 if (__get_user(clen, &uiov->iov_len))
3733 return -EFAULT;
3734 if (clen < 0)
3735 return -EINVAL;
3736 sr->len = io->msg.iov[0].iov_len;
3737 io->msg.iov = NULL;
3738 } else {
3739 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
3740 &io->msg.iov,
3741 &io->msg.msg.msg_iter);
3742 if (ret < 0)
3743 return ret;
3744 }
3745
3746 return 0;
3747}
Jens Axboe03b12302019-12-02 18:50:25 -07003748#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07003749
3750static int io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
3751{
3752 io->msg.iov = io->msg.fast_iov;
3753
3754#ifdef CONFIG_COMPAT
3755 if (req->ctx->compat)
3756 return __io_compat_recvmsg_copy_hdr(req, io);
3757#endif
3758
3759 return __io_recvmsg_copy_hdr(req, io);
3760}
3761
Jens Axboebcda7ba2020-02-23 16:42:51 -07003762static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
3763 int *cflags, bool needs_lock)
3764{
3765 struct io_sr_msg *sr = &req->sr_msg;
3766 struct io_buffer *kbuf;
3767
3768 if (!(req->flags & REQ_F_BUFFER_SELECT))
3769 return NULL;
3770
3771 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
3772 if (IS_ERR(kbuf))
3773 return kbuf;
3774
3775 sr->kbuf = kbuf;
3776 req->flags |= REQ_F_BUFFER_SELECTED;
3777
3778 *cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
3779 *cflags |= IORING_CQE_F_BUFFER;
3780 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07003781}
3782
Jens Axboe3529d8c2019-12-19 18:24:38 -07003783static int io_recvmsg_prep(struct io_kiocb *req,
3784 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003785{
Jens Axboee47293f2019-12-20 08:58:21 -07003786 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003787 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003788 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07003789
Jens Axboe3529d8c2019-12-19 18:24:38 -07003790 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3791 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003792 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003793 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003794
Jens Axboed8768362020-02-27 14:17:49 -07003795#ifdef CONFIG_COMPAT
3796 if (req->ctx->compat)
3797 sr->msg_flags |= MSG_CMSG_COMPAT;
3798#endif
3799
Jens Axboefddafac2020-01-04 20:19:44 -07003800 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003801 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003802 /* iovec is already imported */
3803 if (req->flags & REQ_F_NEED_CLEANUP)
3804 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003805
Jens Axboe52de1fe2020-02-27 10:15:42 -07003806 ret = io_recvmsg_copy_hdr(req, io);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003807 if (!ret)
3808 req->flags |= REQ_F_NEED_CLEANUP;
3809 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003810}
3811
Pavel Begunkov014db002020-03-03 21:33:12 +03003812static int io_recvmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003813{
Jens Axboe0b416c32019-12-15 10:57:46 -07003814 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003815 struct socket *sock;
Jens Axboe52de1fe2020-02-27 10:15:42 -07003816 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003817
3818 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3819 return -EINVAL;
3820
3821 sock = sock_from_file(req->file, &ret);
3822 if (sock) {
Jens Axboe52de1fe2020-02-27 10:15:42 -07003823 struct io_buffer *kbuf;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003824 struct io_async_ctx io;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003825 unsigned flags;
3826
Jens Axboe03b12302019-12-02 18:50:25 -07003827 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003828 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003829 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003830 /* if iov is set, it's allocated already */
3831 if (!kmsg->iov)
3832 kmsg->iov = kmsg->fast_iov;
3833 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003834 } else {
Jens Axboe0b416c32019-12-15 10:57:46 -07003835 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003836 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003837
Jens Axboe52de1fe2020-02-27 10:15:42 -07003838 ret = io_recvmsg_copy_hdr(req, &io);
Jens Axboe03b12302019-12-02 18:50:25 -07003839 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003840 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003841 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003842
Jens Axboe52de1fe2020-02-27 10:15:42 -07003843 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
3844 if (IS_ERR(kbuf)) {
3845 return PTR_ERR(kbuf);
3846 } else if (kbuf) {
3847 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3848 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
3849 1, req->sr_msg.len);
3850 }
3851
Jens Axboee47293f2019-12-20 08:58:21 -07003852 flags = req->sr_msg.msg_flags;
3853 if (flags & MSG_DONTWAIT)
3854 req->flags |= REQ_F_NOWAIT;
3855 else if (force_nonblock)
3856 flags |= MSG_DONTWAIT;
3857
3858 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3859 kmsg->uaddr, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003860 if (force_nonblock && ret == -EAGAIN)
3861 return io_setup_async_msg(req, kmsg);
Jens Axboe441cdbd2019-12-02 18:49:10 -07003862 if (ret == -ERESTARTSYS)
3863 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003864 }
3865
Pavel Begunkov1e950812020-02-06 19:51:16 +03003866 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003867 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003868 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe52de1fe2020-02-27 10:15:42 -07003869 __io_cqring_add_event(req, ret, cflags);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003870 if (ret < 0)
3871 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003872 io_put_req(req);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003873 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003874}
3875
Pavel Begunkov014db002020-03-03 21:33:12 +03003876static int io_recv(struct io_kiocb *req, bool force_nonblock)
Jens Axboefddafac2020-01-04 20:19:44 -07003877{
Jens Axboebcda7ba2020-02-23 16:42:51 -07003878 struct io_buffer *kbuf = NULL;
Jens Axboefddafac2020-01-04 20:19:44 -07003879 struct socket *sock;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003880 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003881
3882 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3883 return -EINVAL;
3884
3885 sock = sock_from_file(req->file, &ret);
3886 if (sock) {
3887 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003888 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07003889 struct msghdr msg;
3890 struct iovec iov;
3891 unsigned flags;
3892
Jens Axboebcda7ba2020-02-23 16:42:51 -07003893 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
3894 if (IS_ERR(kbuf))
3895 return PTR_ERR(kbuf);
3896 else if (kbuf)
3897 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07003898
Jens Axboebcda7ba2020-02-23 16:42:51 -07003899 ret = import_single_range(READ, buf, sr->len, &iov,
3900 &msg.msg_iter);
3901 if (ret) {
3902 kfree(kbuf);
3903 return ret;
3904 }
3905
3906 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07003907 msg.msg_name = NULL;
3908 msg.msg_control = NULL;
3909 msg.msg_controllen = 0;
3910 msg.msg_namelen = 0;
3911 msg.msg_iocb = NULL;
3912 msg.msg_flags = 0;
3913
3914 flags = req->sr_msg.msg_flags;
3915 if (flags & MSG_DONTWAIT)
3916 req->flags |= REQ_F_NOWAIT;
3917 else if (force_nonblock)
3918 flags |= MSG_DONTWAIT;
3919
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003920 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07003921 if (force_nonblock && ret == -EAGAIN)
3922 return -EAGAIN;
3923 if (ret == -ERESTARTSYS)
3924 ret = -EINTR;
3925 }
3926
Jens Axboebcda7ba2020-02-23 16:42:51 -07003927 kfree(kbuf);
3928 req->flags &= ~REQ_F_NEED_CLEANUP;
3929 __io_cqring_add_event(req, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07003930 if (ret < 0)
3931 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003932 io_put_req(req);
Jens Axboefddafac2020-01-04 20:19:44 -07003933 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003934}
3935
Jens Axboe3529d8c2019-12-19 18:24:38 -07003936static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003937{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003938 struct io_accept *accept = &req->accept;
3939
Jens Axboe17f2fe32019-10-17 14:42:58 -06003940 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3941 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003942 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003943 return -EINVAL;
3944
Jens Axboed55e5f52019-12-11 16:12:15 -07003945 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3946 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003947 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06003948 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003949 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003950}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003951
Pavel Begunkov014db002020-03-03 21:33:12 +03003952static int __io_accept(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003953{
3954 struct io_accept *accept = &req->accept;
3955 unsigned file_flags;
3956 int ret;
3957
3958 file_flags = force_nonblock ? O_NONBLOCK : 0;
3959 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06003960 accept->addr_len, accept->flags,
3961 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003962 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003963 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003964 if (ret == -ERESTARTSYS)
3965 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003966 if (ret < 0)
3967 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003968 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003969 io_put_req(req);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003970 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003971}
3972
3973static void io_accept_finish(struct io_wq_work **workptr)
3974{
3975 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003976
3977 if (io_req_cancelled(req))
3978 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03003979 __io_accept(req, false);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03003980 io_steal_work(req, workptr);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003981}
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003982
Pavel Begunkov014db002020-03-03 21:33:12 +03003983static int io_accept(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003984{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003985 int ret;
3986
Pavel Begunkov014db002020-03-03 21:33:12 +03003987 ret = __io_accept(req, force_nonblock);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003988 if (ret == -EAGAIN && force_nonblock) {
3989 req->work.func = io_accept_finish;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003990 return -EAGAIN;
3991 }
3992 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003993}
3994
Jens Axboe3529d8c2019-12-19 18:24:38 -07003995static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003996{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003997 struct io_connect *conn = &req->connect;
3998 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003999
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004000 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4001 return -EINVAL;
4002 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4003 return -EINVAL;
4004
Jens Axboe3529d8c2019-12-19 18:24:38 -07004005 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4006 conn->addr_len = READ_ONCE(sqe->addr2);
4007
4008 if (!io)
4009 return 0;
4010
4011 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004012 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004013}
4014
Pavel Begunkov014db002020-03-03 21:33:12 +03004015static int io_connect(struct io_kiocb *req, bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004016{
Jens Axboef499a022019-12-02 16:28:46 -07004017 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004018 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004019 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004020
Jens Axboef499a022019-12-02 16:28:46 -07004021 if (req->io) {
4022 io = req->io;
4023 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004024 ret = move_addr_to_kernel(req->connect.addr,
4025 req->connect.addr_len,
4026 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004027 if (ret)
4028 goto out;
4029 io = &__io;
4030 }
4031
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004032 file_flags = force_nonblock ? O_NONBLOCK : 0;
4033
4034 ret = __sys_connect_file(req->file, &io->connect.address,
4035 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004036 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004037 if (req->io)
4038 return -EAGAIN;
4039 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004040 ret = -ENOMEM;
4041 goto out;
4042 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004043 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004044 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004045 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004046 if (ret == -ERESTARTSYS)
4047 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004048out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004049 if (ret < 0)
4050 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004051 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03004052 io_put_req(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004053 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004054}
YueHaibing469956e2020-03-04 15:53:52 +08004055#else /* !CONFIG_NET */
4056static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4057{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004058 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004059}
4060
YueHaibing469956e2020-03-04 15:53:52 +08004061static int io_sendmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004062{
YueHaibing469956e2020-03-04 15:53:52 +08004063 return -EOPNOTSUPP;
4064}
4065
4066static int io_send(struct io_kiocb *req, bool force_nonblock)
4067{
4068 return -EOPNOTSUPP;
4069}
4070
4071static int io_recvmsg_prep(struct io_kiocb *req,
4072 const struct io_uring_sqe *sqe)
4073{
4074 return -EOPNOTSUPP;
4075}
4076
4077static int io_recvmsg(struct io_kiocb *req, bool force_nonblock)
4078{
4079 return -EOPNOTSUPP;
4080}
4081
4082static int io_recv(struct io_kiocb *req, bool force_nonblock)
4083{
4084 return -EOPNOTSUPP;
4085}
4086
4087static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4088{
4089 return -EOPNOTSUPP;
4090}
4091
4092static int io_accept(struct io_kiocb *req, bool force_nonblock)
4093{
4094 return -EOPNOTSUPP;
4095}
4096
4097static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4098{
4099 return -EOPNOTSUPP;
4100}
4101
4102static int io_connect(struct io_kiocb *req, bool force_nonblock)
4103{
4104 return -EOPNOTSUPP;
4105}
4106#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004107
Jens Axboed7718a92020-02-14 22:23:12 -07004108struct io_poll_table {
4109 struct poll_table_struct pt;
4110 struct io_kiocb *req;
4111 int error;
4112};
4113
4114static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
4115 struct wait_queue_head *head)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004116{
Jens Axboed7718a92020-02-14 22:23:12 -07004117 if (unlikely(poll->head)) {
4118 pt->error = -EINVAL;
4119 return;
4120 }
4121
4122 pt->error = 0;
4123 poll->head = head;
4124 add_wait_queue(head, &poll->wait);
4125}
4126
4127static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4128 struct poll_table_struct *p)
4129{
4130 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4131
4132 __io_queue_proc(&pt->req->apoll->poll, pt, head);
4133}
4134
4135static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4136 __poll_t mask, task_work_func_t func)
4137{
4138 struct task_struct *tsk;
Jens Axboeaa96bf82020-04-03 11:26:26 -06004139 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004140
4141 /* for instances that support it check for an event match first: */
4142 if (mask && !(mask & poll->events))
4143 return 0;
4144
4145 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4146
4147 list_del_init(&poll->wait.entry);
4148
4149 tsk = req->task;
4150 req->result = mask;
4151 init_task_work(&req->task_work, func);
4152 /*
Jens Axboeaa96bf82020-04-03 11:26:26 -06004153 * If this fails, then the task is exiting. Punt to one of the io-wq
4154 * threads to ensure the work gets run, we can't always rely on exit
4155 * cancelation taking care of this.
Jens Axboed7718a92020-02-14 22:23:12 -07004156 */
Jens Axboeaa96bf82020-04-03 11:26:26 -06004157 ret = task_work_add(tsk, &req->task_work, true);
4158 if (unlikely(ret)) {
4159 tsk = io_wq_get_task(req->ctx->io_wq);
4160 task_work_add(tsk, &req->task_work, true);
4161 }
Jens Axboed7718a92020-02-14 22:23:12 -07004162 wake_up_process(tsk);
4163 return 1;
4164}
4165
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004166static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4167 __acquires(&req->ctx->completion_lock)
4168{
4169 struct io_ring_ctx *ctx = req->ctx;
4170
4171 if (!req->result && !READ_ONCE(poll->canceled)) {
4172 struct poll_table_struct pt = { ._key = poll->events };
4173
4174 req->result = vfs_poll(req->file, &pt) & poll->events;
4175 }
4176
4177 spin_lock_irq(&ctx->completion_lock);
4178 if (!req->result && !READ_ONCE(poll->canceled)) {
4179 add_wait_queue(poll->head, &poll->wait);
4180 return true;
4181 }
4182
4183 return false;
4184}
4185
Jens Axboed7718a92020-02-14 22:23:12 -07004186static void io_async_task_func(struct callback_head *cb)
4187{
4188 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4189 struct async_poll *apoll = req->apoll;
4190 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2bae0472020-04-13 11:16:34 -06004191 bool canceled;
Jens Axboed7718a92020-02-14 22:23:12 -07004192
4193 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4194
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004195 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07004196 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004197 return;
Jens Axboed7718a92020-02-14 22:23:12 -07004198 }
4199
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004200 if (hash_hashed(&req->hash_node))
4201 hash_del(&req->hash_node);
4202
Jens Axboe2bae0472020-04-13 11:16:34 -06004203 canceled = READ_ONCE(apoll->poll.canceled);
4204 if (canceled) {
4205 io_cqring_fill_event(req, -ECANCELED);
4206 io_commit_cqring(ctx);
4207 }
4208
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004209 spin_unlock_irq(&ctx->completion_lock);
4210
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004211 /* restore ->work in case we need to retry again */
4212 memcpy(&req->work, &apoll->work, sizeof(req->work));
4213
Jens Axboe2bae0472020-04-13 11:16:34 -06004214 if (canceled) {
4215 kfree(apoll);
4216 io_cqring_ev_posted(ctx);
4217 req_set_fail_links(req);
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004218 io_double_put_req(req);
Jens Axboe2bae0472020-04-13 11:16:34 -06004219 return;
4220 }
4221
Jens Axboed7718a92020-02-14 22:23:12 -07004222 __set_current_state(TASK_RUNNING);
4223 mutex_lock(&ctx->uring_lock);
4224 __io_queue_sqe(req, NULL);
4225 mutex_unlock(&ctx->uring_lock);
4226
4227 kfree(apoll);
4228}
4229
4230static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4231 void *key)
4232{
4233 struct io_kiocb *req = wait->private;
4234 struct io_poll_iocb *poll = &req->apoll->poll;
4235
4236 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4237 key_to_poll(key));
4238
4239 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4240}
4241
4242static void io_poll_req_insert(struct io_kiocb *req)
4243{
4244 struct io_ring_ctx *ctx = req->ctx;
4245 struct hlist_head *list;
4246
4247 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
4248 hlist_add_head(&req->hash_node, list);
4249}
4250
4251static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
4252 struct io_poll_iocb *poll,
4253 struct io_poll_table *ipt, __poll_t mask,
4254 wait_queue_func_t wake_func)
4255 __acquires(&ctx->completion_lock)
4256{
4257 struct io_ring_ctx *ctx = req->ctx;
4258 bool cancel = false;
4259
4260 poll->file = req->file;
4261 poll->head = NULL;
4262 poll->done = poll->canceled = false;
4263 poll->events = mask;
4264
4265 ipt->pt._key = mask;
4266 ipt->req = req;
4267 ipt->error = -EINVAL;
4268
4269 INIT_LIST_HEAD(&poll->wait.entry);
4270 init_waitqueue_func_entry(&poll->wait, wake_func);
4271 poll->wait.private = req;
4272
4273 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
4274
4275 spin_lock_irq(&ctx->completion_lock);
4276 if (likely(poll->head)) {
4277 spin_lock(&poll->head->lock);
4278 if (unlikely(list_empty(&poll->wait.entry))) {
4279 if (ipt->error)
4280 cancel = true;
4281 ipt->error = 0;
4282 mask = 0;
4283 }
4284 if (mask || ipt->error)
4285 list_del_init(&poll->wait.entry);
4286 else if (cancel)
4287 WRITE_ONCE(poll->canceled, true);
4288 else if (!poll->done) /* actually waiting for an event */
4289 io_poll_req_insert(req);
4290 spin_unlock(&poll->head->lock);
4291 }
4292
4293 return mask;
4294}
4295
4296static bool io_arm_poll_handler(struct io_kiocb *req)
4297{
4298 const struct io_op_def *def = &io_op_defs[req->opcode];
4299 struct io_ring_ctx *ctx = req->ctx;
4300 struct async_poll *apoll;
4301 struct io_poll_table ipt;
4302 __poll_t mask, ret;
4303
4304 if (!req->file || !file_can_poll(req->file))
4305 return false;
4306 if (req->flags & (REQ_F_MUST_PUNT | REQ_F_POLLED))
4307 return false;
4308 if (!def->pollin && !def->pollout)
4309 return false;
4310
4311 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
4312 if (unlikely(!apoll))
4313 return false;
4314
4315 req->flags |= REQ_F_POLLED;
4316 memcpy(&apoll->work, &req->work, sizeof(req->work));
4317
Jens Axboe3537b6a2020-04-03 11:19:06 -06004318 get_task_struct(current);
Jens Axboed7718a92020-02-14 22:23:12 -07004319 req->task = current;
4320 req->apoll = apoll;
4321 INIT_HLIST_NODE(&req->hash_node);
4322
Nathan Chancellor8755d972020-03-02 16:01:19 -07004323 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07004324 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07004325 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07004326 if (def->pollout)
4327 mask |= POLLOUT | POLLWRNORM;
4328 mask |= POLLERR | POLLPRI;
4329
4330 ipt.pt._qproc = io_async_queue_proc;
4331
4332 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
4333 io_async_wake);
4334 if (ret) {
4335 ipt.error = 0;
4336 apoll->poll.done = true;
4337 spin_unlock_irq(&ctx->completion_lock);
4338 memcpy(&req->work, &apoll->work, sizeof(req->work));
4339 kfree(apoll);
4340 return false;
4341 }
4342 spin_unlock_irq(&ctx->completion_lock);
4343 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
4344 apoll->poll.events);
4345 return true;
4346}
4347
4348static bool __io_poll_remove_one(struct io_kiocb *req,
4349 struct io_poll_iocb *poll)
4350{
Jens Axboeb41e9852020-02-17 09:52:41 -07004351 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004352
4353 spin_lock(&poll->head->lock);
4354 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07004355 if (!list_empty(&poll->wait.entry)) {
4356 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07004357 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004358 }
4359 spin_unlock(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07004360 return do_complete;
4361}
4362
4363static bool io_poll_remove_one(struct io_kiocb *req)
4364{
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08004365 struct async_poll *apoll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07004366 bool do_complete;
4367
4368 if (req->opcode == IORING_OP_POLL_ADD) {
4369 do_complete = __io_poll_remove_one(req, &req->poll);
4370 } else {
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08004371 apoll = req->apoll;
Jens Axboed7718a92020-02-14 22:23:12 -07004372 /* non-poll requests have submit ref still */
4373 do_complete = __io_poll_remove_one(req, &req->apoll->poll);
4374 if (do_complete)
4375 io_put_req(req);
4376 }
4377
Jens Axboe78076bb2019-12-04 19:56:40 -07004378 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07004379
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004380 if (do_complete && apoll) {
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08004381 /*
4382 * restore ->work because we need to call io_req_work_drop_env.
4383 */
4384 memcpy(&req->work, &apoll->work, sizeof(req->work));
4385 kfree(apoll);
4386 }
4387
Jens Axboeb41e9852020-02-17 09:52:41 -07004388 if (do_complete) {
4389 io_cqring_fill_event(req, -ECANCELED);
4390 io_commit_cqring(req->ctx);
4391 req->flags |= REQ_F_COMP_LOCKED;
4392 io_put_req(req);
4393 }
4394
4395 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004396}
4397
4398static void io_poll_remove_all(struct io_ring_ctx *ctx)
4399{
Jens Axboe78076bb2019-12-04 19:56:40 -07004400 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004401 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004402 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004403
4404 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07004405 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
4406 struct hlist_head *list;
4407
4408 list = &ctx->cancel_hash[i];
4409 hlist_for_each_entry_safe(req, tmp, list, hash_node)
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004410 posted += io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004411 }
4412 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07004413
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004414 if (posted)
4415 io_cqring_ev_posted(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004416}
4417
Jens Axboe47f46762019-11-09 17:43:02 -07004418static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
4419{
Jens Axboe78076bb2019-12-04 19:56:40 -07004420 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07004421 struct io_kiocb *req;
4422
Jens Axboe78076bb2019-12-04 19:56:40 -07004423 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
4424 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07004425 if (sqe_addr != req->user_data)
4426 continue;
4427 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07004428 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07004429 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07004430 }
4431
4432 return -ENOENT;
4433}
4434
Jens Axboe3529d8c2019-12-19 18:24:38 -07004435static int io_poll_remove_prep(struct io_kiocb *req,
4436 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004437{
Jens Axboe221c5eb2019-01-17 09:41:58 -07004438 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4439 return -EINVAL;
4440 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
4441 sqe->poll_events)
4442 return -EINVAL;
4443
Jens Axboe0969e782019-12-17 18:40:57 -07004444 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07004445 return 0;
4446}
4447
4448/*
4449 * Find a running poll command that matches one specified in sqe->addr,
4450 * and remove it if found.
4451 */
4452static int io_poll_remove(struct io_kiocb *req)
4453{
4454 struct io_ring_ctx *ctx = req->ctx;
4455 u64 addr;
4456 int ret;
4457
Jens Axboe0969e782019-12-17 18:40:57 -07004458 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004459 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07004460 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004461 spin_unlock_irq(&ctx->completion_lock);
4462
Jens Axboe78e19bb2019-11-06 15:21:34 -07004463 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004464 if (ret < 0)
4465 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004466 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004467 return 0;
4468}
4469
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004470static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004471{
Jackie Liua197f662019-11-08 08:09:12 -07004472 struct io_ring_ctx *ctx = req->ctx;
4473
Jens Axboe8c838782019-03-12 15:48:16 -06004474 req->poll.done = true;
Pavel Begunkovb0a20342020-02-28 10:36:35 +03004475 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06004476 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004477}
4478
Jens Axboeb41e9852020-02-17 09:52:41 -07004479static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004480{
Jens Axboe221c5eb2019-01-17 09:41:58 -07004481 struct io_ring_ctx *ctx = req->ctx;
Jens Axboea6ba6322020-04-03 11:10:14 -06004482 struct io_poll_iocb *poll = &req->poll;
4483
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004484 if (io_poll_rewait(req, poll)) {
Jens Axboea6ba6322020-04-03 11:10:14 -06004485 spin_unlock_irq(&ctx->completion_lock);
4486 return;
4487 }
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004488
Jens Axboe78076bb2019-12-04 19:56:40 -07004489 hash_del(&req->hash_node);
Jens Axboeb41e9852020-02-17 09:52:41 -07004490 io_poll_complete(req, req->result, 0);
4491 req->flags |= REQ_F_COMP_LOCKED;
4492 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004493 spin_unlock_irq(&ctx->completion_lock);
4494
Jens Axboe8c838782019-03-12 15:48:16 -06004495 io_cqring_ev_posted(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004496}
4497
Jens Axboeb41e9852020-02-17 09:52:41 -07004498static void io_poll_task_func(struct callback_head *cb)
Jens Axboee94f1412019-12-19 12:06:02 -07004499{
Jens Axboeb41e9852020-02-17 09:52:41 -07004500 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4501 struct io_kiocb *nxt = NULL;
Jens Axboee94f1412019-12-19 12:06:02 -07004502
Jens Axboeb41e9852020-02-17 09:52:41 -07004503 io_poll_task_handler(req, &nxt);
Jens Axboed7718a92020-02-14 22:23:12 -07004504 if (nxt) {
4505 struct io_ring_ctx *ctx = nxt->ctx;
Jens Axboee94f1412019-12-19 12:06:02 -07004506
Jens Axboed7718a92020-02-14 22:23:12 -07004507 mutex_lock(&ctx->uring_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07004508 __io_queue_sqe(nxt, NULL);
Jens Axboed7718a92020-02-14 22:23:12 -07004509 mutex_unlock(&ctx->uring_lock);
Jens Axboee94f1412019-12-19 12:06:02 -07004510 }
Jens Axboef0b493e2020-02-01 21:30:11 -07004511}
4512
Jens Axboe221c5eb2019-01-17 09:41:58 -07004513static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4514 void *key)
4515{
Jens Axboec2f2eb72020-02-10 09:07:05 -07004516 struct io_kiocb *req = wait->private;
4517 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004518
Jens Axboed7718a92020-02-14 22:23:12 -07004519 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004520}
4521
Jens Axboe221c5eb2019-01-17 09:41:58 -07004522static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
4523 struct poll_table_struct *p)
4524{
4525 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4526
Jens Axboed7718a92020-02-14 22:23:12 -07004527 __io_queue_proc(&pt->req->poll, pt, head);
Jens Axboeeac406c2019-11-14 12:09:58 -07004528}
4529
Jens Axboe3529d8c2019-12-19 18:24:38 -07004530static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004531{
4532 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004533 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004534
4535 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4536 return -EINVAL;
4537 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
4538 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06004539 if (!poll->file)
4540 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004541
Jens Axboe221c5eb2019-01-17 09:41:58 -07004542 events = READ_ONCE(sqe->poll_events);
4543 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboeb41e9852020-02-17 09:52:41 -07004544
Jens Axboe3537b6a2020-04-03 11:19:06 -06004545 get_task_struct(current);
Jens Axboeb41e9852020-02-17 09:52:41 -07004546 req->task = current;
Jens Axboe0969e782019-12-17 18:40:57 -07004547 return 0;
4548}
4549
Pavel Begunkov014db002020-03-03 21:33:12 +03004550static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07004551{
4552 struct io_poll_iocb *poll = &req->poll;
4553 struct io_ring_ctx *ctx = req->ctx;
4554 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07004555 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07004556
Jens Axboe78076bb2019-12-04 19:56:40 -07004557 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe36703242019-07-25 10:20:18 -06004558 INIT_LIST_HEAD(&req->list);
Jens Axboed7718a92020-02-14 22:23:12 -07004559 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06004560
Jens Axboed7718a92020-02-14 22:23:12 -07004561 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
4562 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004563
Jens Axboe8c838782019-03-12 15:48:16 -06004564 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06004565 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004566 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06004567 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07004568 spin_unlock_irq(&ctx->completion_lock);
4569
Jens Axboe8c838782019-03-12 15:48:16 -06004570 if (mask) {
4571 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03004572 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004573 }
Jens Axboe8c838782019-03-12 15:48:16 -06004574 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004575}
4576
Jens Axboe5262f562019-09-17 12:26:57 -06004577static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
4578{
Jens Axboead8a48a2019-11-15 08:49:11 -07004579 struct io_timeout_data *data = container_of(timer,
4580 struct io_timeout_data, timer);
4581 struct io_kiocb *req = data->req;
4582 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06004583 unsigned long flags;
4584
Jens Axboe5262f562019-09-17 12:26:57 -06004585 atomic_inc(&ctx->cq_timeouts);
4586
4587 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08004588 /*
Jens Axboe11365042019-10-16 09:08:32 -06004589 * We could be racing with timeout deletion. If the list is empty,
4590 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08004591 */
Jens Axboe842f9612019-10-29 12:34:10 -06004592 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06004593 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06004594
Jens Axboe11365042019-10-16 09:08:32 -06004595 /*
4596 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08004597 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06004598 * pointer will be increased, otherwise other timeout reqs may
4599 * return in advance without waiting for enough wait_nr.
4600 */
4601 prev = req;
4602 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
4603 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06004604 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06004605 }
Jens Axboe842f9612019-10-29 12:34:10 -06004606
Jens Axboe78e19bb2019-11-06 15:21:34 -07004607 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06004608 io_commit_cqring(ctx);
4609 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4610
4611 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004612 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004613 io_put_req(req);
4614 return HRTIMER_NORESTART;
4615}
4616
Jens Axboe47f46762019-11-09 17:43:02 -07004617static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
4618{
4619 struct io_kiocb *req;
4620 int ret = -ENOENT;
4621
4622 list_for_each_entry(req, &ctx->timeout_list, list) {
4623 if (user_data == req->user_data) {
4624 list_del_init(&req->list);
4625 ret = 0;
4626 break;
4627 }
4628 }
4629
4630 if (ret == -ENOENT)
4631 return ret;
4632
Jens Axboe2d283902019-12-04 11:08:05 -07004633 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07004634 if (ret == -1)
4635 return -EALREADY;
4636
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004637 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004638 io_cqring_fill_event(req, -ECANCELED);
4639 io_put_req(req);
4640 return 0;
4641}
4642
Jens Axboe3529d8c2019-12-19 18:24:38 -07004643static int io_timeout_remove_prep(struct io_kiocb *req,
4644 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07004645{
Jens Axboeb29472e2019-12-17 18:50:29 -07004646 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4647 return -EINVAL;
4648 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
4649 return -EINVAL;
4650
4651 req->timeout.addr = READ_ONCE(sqe->addr);
4652 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
4653 if (req->timeout.flags)
4654 return -EINVAL;
4655
Jens Axboeb29472e2019-12-17 18:50:29 -07004656 return 0;
4657}
4658
Jens Axboe11365042019-10-16 09:08:32 -06004659/*
4660 * Remove or update an existing timeout command
4661 */
Jens Axboefc4df992019-12-10 14:38:45 -07004662static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06004663{
4664 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07004665 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06004666
Jens Axboe11365042019-10-16 09:08:32 -06004667 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07004668 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06004669
Jens Axboe47f46762019-11-09 17:43:02 -07004670 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06004671 io_commit_cqring(ctx);
4672 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004673 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004674 if (ret < 0)
4675 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08004676 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06004677 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06004678}
4679
Jens Axboe3529d8c2019-12-19 18:24:38 -07004680static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07004681 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06004682{
Jens Axboead8a48a2019-11-15 08:49:11 -07004683 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06004684 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06004685
Jens Axboead8a48a2019-11-15 08:49:11 -07004686 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06004687 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07004688 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06004689 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07004690 if (sqe->off && is_timeout_link)
4691 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06004692 flags = READ_ONCE(sqe->timeout_flags);
4693 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06004694 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06004695
Jens Axboe26a61672019-12-20 09:02:01 -07004696 req->timeout.count = READ_ONCE(sqe->off);
4697
Jens Axboe3529d8c2019-12-19 18:24:38 -07004698 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07004699 return -ENOMEM;
4700
4701 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07004702 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07004703 req->flags |= REQ_F_TIMEOUT;
4704
4705 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06004706 return -EFAULT;
4707
Jens Axboe11365042019-10-16 09:08:32 -06004708 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07004709 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06004710 else
Jens Axboead8a48a2019-11-15 08:49:11 -07004711 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06004712
Jens Axboead8a48a2019-11-15 08:49:11 -07004713 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
4714 return 0;
4715}
4716
Jens Axboefc4df992019-12-10 14:38:45 -07004717static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07004718{
Jens Axboead8a48a2019-11-15 08:49:11 -07004719 struct io_ring_ctx *ctx = req->ctx;
4720 struct io_timeout_data *data;
4721 struct list_head *entry;
4722 unsigned span = 0;
Pavel Begunkovb55ce732020-04-15 00:39:49 +03004723 u32 count = req->timeout.count;
Pavel Begunkov22cad152020-04-15 00:39:48 +03004724 u32 seq = req->sequence;
Jens Axboead8a48a2019-11-15 08:49:11 -07004725
Jens Axboe2d283902019-12-04 11:08:05 -07004726 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004727
Jens Axboe5262f562019-09-17 12:26:57 -06004728 /*
4729 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07004730 * timeout event to be satisfied. If it isn't set, then this is
4731 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06004732 */
Jens Axboe93bd25b2019-11-11 23:34:31 -07004733 if (!count) {
4734 req->flags |= REQ_F_TIMEOUT_NOSEQ;
4735 spin_lock_irq(&ctx->completion_lock);
4736 entry = ctx->timeout_list.prev;
4737 goto add;
4738 }
Jens Axboe5262f562019-09-17 12:26:57 -06004739
Pavel Begunkov22cad152020-04-15 00:39:48 +03004740 req->sequence = seq + count;
Jens Axboe5262f562019-09-17 12:26:57 -06004741
4742 /*
4743 * Insertion sort, ensuring the first entry in the list is always
4744 * the one we need first.
4745 */
Jens Axboe5262f562019-09-17 12:26:57 -06004746 spin_lock_irq(&ctx->completion_lock);
4747 list_for_each_prev(entry, &ctx->timeout_list) {
4748 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
Pavel Begunkov22cad152020-04-15 00:39:48 +03004749 unsigned nxt_seq;
yangerkun5da0fb12019-10-15 21:59:29 +08004750 long long tmp, tmp_nxt;
Pavel Begunkovb55ce732020-04-15 00:39:49 +03004751 u32 nxt_offset = nxt->timeout.count;
Jens Axboe5262f562019-09-17 12:26:57 -06004752
Jens Axboe93bd25b2019-11-11 23:34:31 -07004753 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
4754 continue;
4755
yangerkun5da0fb12019-10-15 21:59:29 +08004756 /*
Pavel Begunkov22cad152020-04-15 00:39:48 +03004757 * Since seq + count can overflow, use type long
yangerkun5da0fb12019-10-15 21:59:29 +08004758 * long to store it.
4759 */
Pavel Begunkov22cad152020-04-15 00:39:48 +03004760 tmp = (long long)seq + count;
4761 nxt_seq = nxt->sequence - nxt_offset;
4762 tmp_nxt = (long long)nxt_seq + nxt_offset;
yangerkun5da0fb12019-10-15 21:59:29 +08004763
4764 /*
4765 * cached_sq_head may overflow, and it will never overflow twice
4766 * once there is some timeout req still be valid.
4767 */
Pavel Begunkov22cad152020-04-15 00:39:48 +03004768 if (seq < nxt_seq)
yangerkun8b07a652019-10-17 12:12:35 +08004769 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08004770
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004771 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06004772 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004773
4774 /*
4775 * Sequence of reqs after the insert one and itself should
4776 * be adjusted because each timeout req consumes a slot.
4777 */
4778 span++;
4779 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06004780 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004781 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004782add:
Jens Axboe5262f562019-09-17 12:26:57 -06004783 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07004784 data->timer.function = io_timeout_fn;
4785 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06004786 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004787 return 0;
4788}
4789
Jens Axboe62755e32019-10-28 21:49:21 -06004790static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06004791{
Jens Axboe62755e32019-10-28 21:49:21 -06004792 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06004793
Jens Axboe62755e32019-10-28 21:49:21 -06004794 return req->user_data == (unsigned long) data;
4795}
4796
Jens Axboee977d6d2019-11-05 12:39:45 -07004797static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06004798{
Jens Axboe62755e32019-10-28 21:49:21 -06004799 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06004800 int ret = 0;
4801
Jens Axboe62755e32019-10-28 21:49:21 -06004802 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
4803 switch (cancel_ret) {
4804 case IO_WQ_CANCEL_OK:
4805 ret = 0;
4806 break;
4807 case IO_WQ_CANCEL_RUNNING:
4808 ret = -EALREADY;
4809 break;
4810 case IO_WQ_CANCEL_NOTFOUND:
4811 ret = -ENOENT;
4812 break;
4813 }
4814
Jens Axboee977d6d2019-11-05 12:39:45 -07004815 return ret;
4816}
4817
Jens Axboe47f46762019-11-09 17:43:02 -07004818static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
4819 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03004820 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07004821{
4822 unsigned long flags;
4823 int ret;
4824
4825 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
4826 if (ret != -ENOENT) {
4827 spin_lock_irqsave(&ctx->completion_lock, flags);
4828 goto done;
4829 }
4830
4831 spin_lock_irqsave(&ctx->completion_lock, flags);
4832 ret = io_timeout_cancel(ctx, sqe_addr);
4833 if (ret != -ENOENT)
4834 goto done;
4835 ret = io_poll_cancel(ctx, sqe_addr);
4836done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004837 if (!ret)
4838 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07004839 io_cqring_fill_event(req, ret);
4840 io_commit_cqring(ctx);
4841 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4842 io_cqring_ev_posted(ctx);
4843
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004844 if (ret < 0)
4845 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03004846 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004847}
4848
Jens Axboe3529d8c2019-12-19 18:24:38 -07004849static int io_async_cancel_prep(struct io_kiocb *req,
4850 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07004851{
Jens Axboefbf23842019-12-17 18:45:56 -07004852 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07004853 return -EINVAL;
4854 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
4855 sqe->cancel_flags)
4856 return -EINVAL;
4857
Jens Axboefbf23842019-12-17 18:45:56 -07004858 req->cancel.addr = READ_ONCE(sqe->addr);
4859 return 0;
4860}
4861
Pavel Begunkov014db002020-03-03 21:33:12 +03004862static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07004863{
4864 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07004865
Pavel Begunkov014db002020-03-03 21:33:12 +03004866 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06004867 return 0;
4868}
4869
Jens Axboe05f3fb32019-12-09 11:22:50 -07004870static int io_files_update_prep(struct io_kiocb *req,
4871 const struct io_uring_sqe *sqe)
4872{
4873 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
4874 return -EINVAL;
4875
4876 req->files_update.offset = READ_ONCE(sqe->off);
4877 req->files_update.nr_args = READ_ONCE(sqe->len);
4878 if (!req->files_update.nr_args)
4879 return -EINVAL;
4880 req->files_update.arg = READ_ONCE(sqe->addr);
4881 return 0;
4882}
4883
4884static int io_files_update(struct io_kiocb *req, bool force_nonblock)
4885{
4886 struct io_ring_ctx *ctx = req->ctx;
4887 struct io_uring_files_update up;
4888 int ret;
4889
Jens Axboef86cd202020-01-29 13:46:44 -07004890 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004891 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004892
4893 up.offset = req->files_update.offset;
4894 up.fds = req->files_update.arg;
4895
4896 mutex_lock(&ctx->uring_lock);
4897 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
4898 mutex_unlock(&ctx->uring_lock);
4899
4900 if (ret < 0)
4901 req_set_fail_links(req);
4902 io_cqring_add_event(req, ret);
4903 io_put_req(req);
4904 return 0;
4905}
4906
Jens Axboe3529d8c2019-12-19 18:24:38 -07004907static int io_req_defer_prep(struct io_kiocb *req,
4908 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07004909{
Jens Axboee7815732019-12-17 19:45:06 -07004910 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07004911
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03004912 if (!sqe)
4913 return 0;
4914
Jens Axboef86cd202020-01-29 13:46:44 -07004915 if (io_op_defs[req->opcode].file_table) {
4916 ret = io_grab_files(req);
4917 if (unlikely(ret))
4918 return ret;
4919 }
4920
Jens Axboecccf0ee2020-01-27 16:34:48 -07004921 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
4922
Jens Axboed625c6e2019-12-17 19:53:05 -07004923 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07004924 case IORING_OP_NOP:
4925 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004926 case IORING_OP_READV:
4927 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004928 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004929 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004930 break;
4931 case IORING_OP_WRITEV:
4932 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004933 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004934 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004935 break;
Jens Axboe0969e782019-12-17 18:40:57 -07004936 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004937 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004938 break;
4939 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004940 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004941 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004942 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004943 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004944 break;
4945 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004946 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004947 break;
Jens Axboe03b12302019-12-02 18:50:25 -07004948 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004949 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004950 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004951 break;
4952 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004953 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004954 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004955 break;
Jens Axboef499a022019-12-02 16:28:46 -07004956 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004957 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004958 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004959 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004960 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004961 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004962 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004963 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004964 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004965 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004966 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004967 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004968 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004969 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004970 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004971 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004972 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004973 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004974 case IORING_OP_FALLOCATE:
4975 ret = io_fallocate_prep(req, sqe);
4976 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004977 case IORING_OP_OPENAT:
4978 ret = io_openat_prep(req, sqe);
4979 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004980 case IORING_OP_CLOSE:
4981 ret = io_close_prep(req, sqe);
4982 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004983 case IORING_OP_FILES_UPDATE:
4984 ret = io_files_update_prep(req, sqe);
4985 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004986 case IORING_OP_STATX:
4987 ret = io_statx_prep(req, sqe);
4988 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004989 case IORING_OP_FADVISE:
4990 ret = io_fadvise_prep(req, sqe);
4991 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004992 case IORING_OP_MADVISE:
4993 ret = io_madvise_prep(req, sqe);
4994 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004995 case IORING_OP_OPENAT2:
4996 ret = io_openat2_prep(req, sqe);
4997 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004998 case IORING_OP_EPOLL_CTL:
4999 ret = io_epoll_ctl_prep(req, sqe);
5000 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005001 case IORING_OP_SPLICE:
5002 ret = io_splice_prep(req, sqe);
5003 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005004 case IORING_OP_PROVIDE_BUFFERS:
5005 ret = io_provide_buffers_prep(req, sqe);
5006 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005007 case IORING_OP_REMOVE_BUFFERS:
5008 ret = io_remove_buffers_prep(req, sqe);
5009 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005010 default:
Jens Axboee7815732019-12-17 19:45:06 -07005011 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5012 req->opcode);
5013 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005014 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005015 }
5016
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005017 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07005018}
5019
Jens Axboe3529d8c2019-12-19 18:24:38 -07005020static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06005021{
Jackie Liua197f662019-11-08 08:09:12 -07005022 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07005023 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06005024
Bob Liu9d858b22019-11-13 18:06:25 +08005025 /* Still need defer if there is pending req in defer list. */
5026 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06005027 return 0;
5028
Jens Axboe3529d8c2019-12-19 18:24:38 -07005029 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06005030 return -EAGAIN;
5031
Jens Axboe3529d8c2019-12-19 18:24:38 -07005032 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005033 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07005034 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07005035
Jens Axboede0617e2019-04-06 21:51:27 -06005036 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08005037 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06005038 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06005039 return 0;
5040 }
5041
Jens Axboe915967f2019-11-21 09:01:20 -07005042 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06005043 list_add_tail(&req->list, &ctx->defer_list);
5044 spin_unlock_irq(&ctx->completion_lock);
5045 return -EIOCBQUEUED;
5046}
5047
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005048static void io_cleanup_req(struct io_kiocb *req)
5049{
5050 struct io_async_ctx *io = req->io;
5051
5052 switch (req->opcode) {
5053 case IORING_OP_READV:
5054 case IORING_OP_READ_FIXED:
5055 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005056 if (req->flags & REQ_F_BUFFER_SELECTED)
5057 kfree((void *)(unsigned long)req->rw.addr);
5058 /* fallthrough */
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005059 case IORING_OP_WRITEV:
5060 case IORING_OP_WRITE_FIXED:
5061 case IORING_OP_WRITE:
5062 if (io->rw.iov != io->rw.fast_iov)
5063 kfree(io->rw.iov);
5064 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005065 case IORING_OP_RECVMSG:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005066 if (req->flags & REQ_F_BUFFER_SELECTED)
5067 kfree(req->sr_msg.kbuf);
5068 /* fallthrough */
5069 case IORING_OP_SENDMSG:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005070 if (io->msg.iov != io->msg.fast_iov)
5071 kfree(io->msg.iov);
5072 break;
Jens Axboebcda7ba2020-02-23 16:42:51 -07005073 case IORING_OP_RECV:
5074 if (req->flags & REQ_F_BUFFER_SELECTED)
5075 kfree(req->sr_msg.kbuf);
5076 break;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03005077 case IORING_OP_OPENAT:
5078 case IORING_OP_OPENAT2:
5079 case IORING_OP_STATX:
5080 putname(req->open.filename);
5081 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005082 case IORING_OP_SPLICE:
5083 io_put_file(req, req->splice.file_in,
5084 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5085 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005086 }
5087
5088 req->flags &= ~REQ_F_NEED_CLEANUP;
5089}
5090
Jens Axboe3529d8c2019-12-19 18:24:38 -07005091static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Pavel Begunkov014db002020-03-03 21:33:12 +03005092 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005093{
Jackie Liua197f662019-11-08 08:09:12 -07005094 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005095 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005096
Jens Axboed625c6e2019-12-17 19:53:05 -07005097 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005098 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005099 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005100 break;
5101 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005102 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005103 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005104 if (sqe) {
5105 ret = io_read_prep(req, sqe, force_nonblock);
5106 if (ret < 0)
5107 break;
5108 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005109 ret = io_read(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005110 break;
5111 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07005112 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005113 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005114 if (sqe) {
5115 ret = io_write_prep(req, sqe, force_nonblock);
5116 if (ret < 0)
5117 break;
5118 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005119 ret = io_write(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005120 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005121 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005122 if (sqe) {
5123 ret = io_prep_fsync(req, sqe);
5124 if (ret < 0)
5125 break;
5126 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005127 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005128 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005129 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005130 if (sqe) {
5131 ret = io_poll_add_prep(req, sqe);
5132 if (ret)
5133 break;
5134 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005135 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005136 break;
5137 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005138 if (sqe) {
5139 ret = io_poll_remove_prep(req, sqe);
5140 if (ret < 0)
5141 break;
5142 }
Jens Axboefc4df992019-12-10 14:38:45 -07005143 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005144 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005145 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005146 if (sqe) {
5147 ret = io_prep_sfr(req, sqe);
5148 if (ret < 0)
5149 break;
5150 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005151 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005152 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005153 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005154 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005155 if (sqe) {
5156 ret = io_sendmsg_prep(req, sqe);
5157 if (ret < 0)
5158 break;
5159 }
Jens Axboefddafac2020-01-04 20:19:44 -07005160 if (req->opcode == IORING_OP_SENDMSG)
Pavel Begunkov014db002020-03-03 21:33:12 +03005161 ret = io_sendmsg(req, force_nonblock);
Jens Axboefddafac2020-01-04 20:19:44 -07005162 else
Pavel Begunkov014db002020-03-03 21:33:12 +03005163 ret = io_send(req, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005164 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06005165 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005166 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005167 if (sqe) {
5168 ret = io_recvmsg_prep(req, sqe);
5169 if (ret)
5170 break;
5171 }
Jens Axboefddafac2020-01-04 20:19:44 -07005172 if (req->opcode == IORING_OP_RECVMSG)
Pavel Begunkov014db002020-03-03 21:33:12 +03005173 ret = io_recvmsg(req, force_nonblock);
Jens Axboefddafac2020-01-04 20:19:44 -07005174 else
Pavel Begunkov014db002020-03-03 21:33:12 +03005175 ret = io_recv(req, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06005176 break;
Jens Axboe5262f562019-09-17 12:26:57 -06005177 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005178 if (sqe) {
5179 ret = io_timeout_prep(req, sqe, false);
5180 if (ret)
5181 break;
5182 }
Jens Axboefc4df992019-12-10 14:38:45 -07005183 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005184 break;
Jens Axboe11365042019-10-16 09:08:32 -06005185 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005186 if (sqe) {
5187 ret = io_timeout_remove_prep(req, sqe);
5188 if (ret)
5189 break;
5190 }
Jens Axboefc4df992019-12-10 14:38:45 -07005191 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06005192 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06005193 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005194 if (sqe) {
5195 ret = io_accept_prep(req, sqe);
5196 if (ret)
5197 break;
5198 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005199 ret = io_accept(req, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005200 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005201 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005202 if (sqe) {
5203 ret = io_connect_prep(req, sqe);
5204 if (ret)
5205 break;
5206 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005207 ret = io_connect(req, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005208 break;
Jens Axboe62755e32019-10-28 21:49:21 -06005209 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005210 if (sqe) {
5211 ret = io_async_cancel_prep(req, sqe);
5212 if (ret)
5213 break;
5214 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005215 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005216 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005217 case IORING_OP_FALLOCATE:
5218 if (sqe) {
5219 ret = io_fallocate_prep(req, sqe);
5220 if (ret)
5221 break;
5222 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005223 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005224 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005225 case IORING_OP_OPENAT:
5226 if (sqe) {
5227 ret = io_openat_prep(req, sqe);
5228 if (ret)
5229 break;
5230 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005231 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005232 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005233 case IORING_OP_CLOSE:
5234 if (sqe) {
5235 ret = io_close_prep(req, sqe);
5236 if (ret)
5237 break;
5238 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005239 ret = io_close(req, force_nonblock);
Jens Axboeb5dba592019-12-11 14:02:38 -07005240 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005241 case IORING_OP_FILES_UPDATE:
5242 if (sqe) {
5243 ret = io_files_update_prep(req, sqe);
5244 if (ret)
5245 break;
5246 }
5247 ret = io_files_update(req, force_nonblock);
5248 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005249 case IORING_OP_STATX:
5250 if (sqe) {
5251 ret = io_statx_prep(req, sqe);
5252 if (ret)
5253 break;
5254 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005255 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005256 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005257 case IORING_OP_FADVISE:
5258 if (sqe) {
5259 ret = io_fadvise_prep(req, sqe);
5260 if (ret)
5261 break;
5262 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005263 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005264 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005265 case IORING_OP_MADVISE:
5266 if (sqe) {
5267 ret = io_madvise_prep(req, sqe);
5268 if (ret)
5269 break;
5270 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005271 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005272 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005273 case IORING_OP_OPENAT2:
5274 if (sqe) {
5275 ret = io_openat2_prep(req, sqe);
5276 if (ret)
5277 break;
5278 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005279 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005280 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005281 case IORING_OP_EPOLL_CTL:
5282 if (sqe) {
5283 ret = io_epoll_ctl_prep(req, sqe);
5284 if (ret)
5285 break;
5286 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005287 ret = io_epoll_ctl(req, force_nonblock);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005288 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005289 case IORING_OP_SPLICE:
5290 if (sqe) {
5291 ret = io_splice_prep(req, sqe);
5292 if (ret < 0)
5293 break;
5294 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005295 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005296 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005297 case IORING_OP_PROVIDE_BUFFERS:
5298 if (sqe) {
5299 ret = io_provide_buffers_prep(req, sqe);
5300 if (ret)
5301 break;
5302 }
5303 ret = io_provide_buffers(req, force_nonblock);
5304 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005305 case IORING_OP_REMOVE_BUFFERS:
5306 if (sqe) {
5307 ret = io_remove_buffers_prep(req, sqe);
5308 if (ret)
5309 break;
5310 }
5311 ret = io_remove_buffers(req, force_nonblock);
Jens Axboe31b51512019-01-18 22:56:34 -07005312 break;
5313 default:
5314 ret = -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07005315 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005316 }
5317
Jens Axboe31b51512019-01-18 22:56:34 -07005318 if (ret)
5319 return ret;
5320
5321 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07005322 const bool in_async = io_wq_current_is_worker();
5323
Jens Axboe9e645e112019-05-10 16:07:28 -06005324 if (req->result == -EAGAIN)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005325 return -EAGAIN;
5326
Jens Axboe11ba8202020-01-15 21:51:17 -07005327 /* workqueue context doesn't hold uring_lock, grab it now */
5328 if (in_async)
5329 mutex_lock(&ctx->uring_lock);
5330
Jens Axboe2b188cc2019-01-07 10:46:33 -07005331 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07005332
5333 if (in_async)
5334 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005335 }
5336
5337 return 0;
5338}
5339
Jens Axboe561fb042019-10-24 07:25:42 -06005340static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005341{
Jens Axboe561fb042019-10-24 07:25:42 -06005342 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005343 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06005344 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005345
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005346 /* if NO_CANCEL is set, we must still run the work */
5347 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
5348 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06005349 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005350 }
Jens Axboe31b51512019-01-18 22:56:34 -07005351
Jens Axboe561fb042019-10-24 07:25:42 -06005352 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06005353 do {
Pavel Begunkov014db002020-03-03 21:33:12 +03005354 ret = io_issue_sqe(req, NULL, false);
Jens Axboe561fb042019-10-24 07:25:42 -06005355 /*
5356 * We can get EAGAIN for polled IO even though we're
5357 * forcing a sync submission from here, since we can't
5358 * wait for request slots on the block side.
5359 */
5360 if (ret != -EAGAIN)
5361 break;
5362 cond_resched();
5363 } while (1);
5364 }
Jens Axboe31b51512019-01-18 22:56:34 -07005365
Jens Axboe561fb042019-10-24 07:25:42 -06005366 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005367 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07005368 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06005369 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07005370 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005371
Pavel Begunkove9fd9392020-03-04 16:14:12 +03005372 io_steal_work(req, workptr);
Jens Axboe31b51512019-01-18 22:56:34 -07005373}
Jens Axboe2b188cc2019-01-07 10:46:33 -07005374
Jens Axboe15b71ab2019-12-11 11:20:36 -07005375static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe9e3aa612019-12-11 15:55:43 -07005376{
Jens Axboed3656342019-12-18 09:50:26 -07005377 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07005378 return 0;
Jens Axboe0b5faf62020-02-06 21:42:51 -07005379 if ((fd == -1 || fd == AT_FDCWD) && io_op_defs[req->opcode].fd_non_neg)
Jens Axboed3656342019-12-18 09:50:26 -07005380 return 0;
5381 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06005382}
5383
Jens Axboe65e19f52019-10-26 07:20:21 -06005384static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
5385 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06005386{
Jens Axboe65e19f52019-10-26 07:20:21 -06005387 struct fixed_file_table *table;
5388
Jens Axboe05f3fb32019-12-09 11:22:50 -07005389 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
5390 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06005391}
5392
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005393static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
5394 int fd, struct file **out_file, bool fixed)
5395{
5396 struct io_ring_ctx *ctx = req->ctx;
5397 struct file *file;
5398
5399 if (fixed) {
5400 if (unlikely(!ctx->file_data ||
5401 (unsigned) fd >= ctx->nr_user_files))
5402 return -EBADF;
5403 fd = array_index_nospec(fd, ctx->nr_user_files);
5404 file = io_file_from_index(ctx, fd);
5405 if (!file)
5406 return -EBADF;
Xiaoguang Wang05589552020-03-31 14:05:18 +08005407 req->fixed_file_refs = ctx->file_data->cur_refs;
5408 percpu_ref_get(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005409 } else {
5410 trace_io_uring_file_get(ctx, fd);
5411 file = __io_file_get(state, fd);
5412 if (unlikely(!file))
5413 return -EBADF;
5414 }
5415
5416 *out_file = file;
5417 return 0;
5418}
5419
Jens Axboe3529d8c2019-12-19 18:24:38 -07005420static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Pavel Begunkov9c280f92020-04-08 08:58:46 +03005421 int fd, unsigned int flags)
Jens Axboe09bb8392019-03-13 12:39:28 -06005422{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005423 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06005424
Jens Axboed3656342019-12-18 09:50:26 -07005425 if (!io_req_needs_file(req, fd))
5426 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06005427
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005428 fixed = (flags & IOSQE_FIXED_FILE);
5429 if (unlikely(!fixed && req->needs_fixed_file))
5430 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06005431
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005432 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06005433}
5434
Jackie Liua197f662019-11-08 08:09:12 -07005435static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005436{
Jens Axboefcb323c2019-10-24 12:39:47 -06005437 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07005438 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06005439
Jens Axboe5b0bbee2020-04-27 10:41:22 -06005440 if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE))
Jens Axboef86cd202020-01-29 13:46:44 -07005441 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005442 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07005443 return -EBADF;
5444
Jens Axboefcb323c2019-10-24 12:39:47 -06005445 rcu_read_lock();
5446 spin_lock_irq(&ctx->inflight_lock);
5447 /*
5448 * We use the f_ops->flush() handler to ensure that we can flush
5449 * out work accessing these files if the fd is closed. Check if
5450 * the fd has changed since we started down this path, and disallow
5451 * this operation if it has.
5452 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005453 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06005454 list_add(&req->inflight_entry, &ctx->inflight_list);
5455 req->flags |= REQ_F_INFLIGHT;
5456 req->work.files = current->files;
5457 ret = 0;
5458 }
5459 spin_unlock_irq(&ctx->inflight_lock);
5460 rcu_read_unlock();
5461
5462 return ret;
5463}
5464
Jens Axboe2665abf2019-11-05 12:40:47 -07005465static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
5466{
Jens Axboead8a48a2019-11-15 08:49:11 -07005467 struct io_timeout_data *data = container_of(timer,
5468 struct io_timeout_data, timer);
5469 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07005470 struct io_ring_ctx *ctx = req->ctx;
5471 struct io_kiocb *prev = NULL;
5472 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07005473
5474 spin_lock_irqsave(&ctx->completion_lock, flags);
5475
5476 /*
5477 * We don't expect the list to be empty, that will only happen if we
5478 * race with the completion of the linked work.
5479 */
Pavel Begunkov44932332019-12-05 16:16:35 +03005480 if (!list_empty(&req->link_list)) {
5481 prev = list_entry(req->link_list.prev, struct io_kiocb,
5482 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005483 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03005484 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005485 prev->flags &= ~REQ_F_LINK_TIMEOUT;
5486 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07005487 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005488 }
5489
5490 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5491
5492 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005493 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03005494 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07005495 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07005496 } else {
5497 io_cqring_add_event(req, -ETIME);
5498 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005499 }
Jens Axboe2665abf2019-11-05 12:40:47 -07005500 return HRTIMER_NORESTART;
5501}
5502
Jens Axboead8a48a2019-11-15 08:49:11 -07005503static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005504{
Jens Axboe76a46e02019-11-10 23:34:16 -07005505 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07005506
Jens Axboe76a46e02019-11-10 23:34:16 -07005507 /*
5508 * If the list is now empty, then our linked request finished before
5509 * we got a chance to setup the timer
5510 */
5511 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03005512 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07005513 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07005514
Jens Axboead8a48a2019-11-15 08:49:11 -07005515 data->timer.function = io_link_timeout_fn;
5516 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
5517 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07005518 }
Jens Axboe76a46e02019-11-10 23:34:16 -07005519 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07005520
Jens Axboe2665abf2019-11-05 12:40:47 -07005521 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07005522 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005523}
5524
Jens Axboead8a48a2019-11-15 08:49:11 -07005525static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005526{
5527 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005528
Pavel Begunkovdea3b492020-04-12 02:05:04 +03005529 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07005530 return NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005531 /* for polled retry, if flag is set, we already went through here */
5532 if (req->flags & REQ_F_POLLED)
5533 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005534
Pavel Begunkov44932332019-12-05 16:16:35 +03005535 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
5536 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07005537 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07005538 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005539
Jens Axboe76a46e02019-11-10 23:34:16 -07005540 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07005541 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07005542}
5543
Jens Axboe3529d8c2019-12-19 18:24:38 -07005544static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005545{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005546 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005547 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07005548 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005549 int ret;
5550
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005551again:
5552 linked_timeout = io_prep_linked_timeout(req);
5553
Jens Axboe193155c2020-02-22 23:22:19 -07005554 if (req->work.creds && req->work.creds != current_cred()) {
5555 if (old_creds)
5556 revert_creds(old_creds);
5557 if (old_creds == req->work.creds)
5558 old_creds = NULL; /* restored original creds */
5559 else
5560 old_creds = override_creds(req->work.creds);
5561 }
5562
Pavel Begunkov014db002020-03-03 21:33:12 +03005563 ret = io_issue_sqe(req, sqe, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06005564
5565 /*
5566 * We async punt it if the file wasn't marked NOWAIT, or if the file
5567 * doesn't support non-blocking read/write attempts
5568 */
5569 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
5570 (req->flags & REQ_F_MUST_PUNT))) {
Jens Axboed7718a92020-02-14 22:23:12 -07005571 if (io_arm_poll_handler(req)) {
5572 if (linked_timeout)
5573 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005574 goto exit;
Jens Axboed7718a92020-02-14 22:23:12 -07005575 }
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005576punt:
Jens Axboef86cd202020-01-29 13:46:44 -07005577 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005578 ret = io_grab_files(req);
5579 if (ret)
5580 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005581 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005582
5583 /*
5584 * Queued up for async execution, worker will release
5585 * submit reference when the iocb is actually submitted.
5586 */
5587 io_queue_async_work(req);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005588 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005589 }
Jens Axboee65ef562019-03-12 10:16:44 -06005590
Jens Axboefcb323c2019-10-24 12:39:47 -06005591err:
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005592 nxt = NULL;
Jens Axboee65ef562019-03-12 10:16:44 -06005593 /* drop submission reference */
Jens Axboe2a44f462020-02-25 13:25:41 -07005594 io_put_req_find_next(req, &nxt);
Jens Axboee65ef562019-03-12 10:16:44 -06005595
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005596 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07005597 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005598 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005599 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005600 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005601 }
5602
Jens Axboee65ef562019-03-12 10:16:44 -06005603 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06005604 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07005605 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005606 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06005607 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06005608 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005609 if (nxt) {
5610 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005611
5612 if (req->flags & REQ_F_FORCE_ASYNC)
5613 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005614 goto again;
5615 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005616exit:
Jens Axboe193155c2020-02-22 23:22:19 -07005617 if (old_creds)
5618 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005619}
5620
Jens Axboe3529d8c2019-12-19 18:24:38 -07005621static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005622{
5623 int ret;
5624
Jens Axboe3529d8c2019-12-19 18:24:38 -07005625 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005626 if (ret) {
5627 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03005628fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005629 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005630 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07005631 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005632 }
Pavel Begunkov25508782019-12-30 21:24:47 +03005633 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkov11185912020-01-22 23:09:35 +03005634 ret = io_req_defer_prep(req, sqe);
5635 if (unlikely(ret < 0))
5636 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07005637 /*
5638 * Never try inline submit of IOSQE_ASYNC is set, go straight
5639 * to async execution.
5640 */
5641 req->work.flags |= IO_WQ_WORK_CONCURRENT;
5642 io_queue_async_work(req);
5643 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005644 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07005645 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08005646}
5647
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005648static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005649{
Jens Axboe94ae5e72019-11-14 19:39:52 -07005650 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005651 io_cqring_add_event(req, -ECANCELED);
5652 io_double_put_req(req);
5653 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07005654 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005655}
5656
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005657static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe3529d8c2019-12-19 18:24:38 -07005658 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06005659{
Jackie Liua197f662019-11-08 08:09:12 -07005660 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005661 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06005662
Jens Axboe9e645e112019-05-10 16:07:28 -06005663 /*
5664 * If we already have a head request, queue this one for async
5665 * submittal once the head completes. If we don't have a head but
5666 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
5667 * submitted sync once the chain is complete. If none of those
5668 * conditions are true (normal request), then just queue it.
5669 */
5670 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03005671 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06005672
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03005673 /*
5674 * Taking sequential execution of a link, draining both sides
5675 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
5676 * requests in the link. So, it drains the head and the
5677 * next after the link request. The last one is done via
5678 * drain_next flag to persist the effect across calls.
5679 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005680 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03005681 head->flags |= REQ_F_IO_DRAIN;
5682 ctx->drain_next = 1;
5683 }
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005684 if (io_alloc_async_ctx(req))
5685 return -EAGAIN;
Jens Axboe9e645e112019-05-10 16:07:28 -06005686
Jens Axboe3529d8c2019-12-19 18:24:38 -07005687 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005688 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005689 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03005690 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005691 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07005692 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03005693 trace_io_uring_link(ctx, req, head);
5694 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06005695
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005696 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005697 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005698 io_queue_link_head(head);
5699 *link = NULL;
5700 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005701 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03005702 if (unlikely(ctx->drain_next)) {
5703 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005704 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03005705 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005706 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03005707 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03005708 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03005709
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005710 if (io_alloc_async_ctx(req))
5711 return -EAGAIN;
5712
Pavel Begunkov711be032020-01-17 03:57:59 +03005713 ret = io_req_defer_prep(req, sqe);
5714 if (ret)
5715 req->flags |= REQ_F_FAIL_LINK;
5716 *link = req;
5717 } else {
5718 io_queue_sqe(req, sqe);
5719 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005720 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005721
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005722 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06005723}
5724
Jens Axboe9a56a232019-01-09 09:06:50 -07005725/*
5726 * Batched submission is done, ensure local IO is flushed out.
5727 */
5728static void io_submit_state_end(struct io_submit_state *state)
5729{
5730 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06005731 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07005732 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03005733 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07005734}
5735
5736/*
5737 * Start submission side cache.
5738 */
5739static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08005740 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07005741{
5742 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07005743 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07005744 state->file = NULL;
5745 state->ios_left = max_ios;
5746}
5747
Jens Axboe2b188cc2019-01-07 10:46:33 -07005748static void io_commit_sqring(struct io_ring_ctx *ctx)
5749{
Hristo Venev75b28af2019-08-26 17:23:46 +00005750 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005751
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03005752 /*
5753 * Ensure any loads from the SQEs are done at this point,
5754 * since once we write the new head, the application could
5755 * write new data to them.
5756 */
5757 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005758}
5759
5760/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07005761 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07005762 * that is mapped by userspace. This means that care needs to be taken to
5763 * ensure that reads are stable, as we cannot rely on userspace always
5764 * being a good citizen. If members of the sqe are validated and then later
5765 * used, it's important that those reads are done through READ_ONCE() to
5766 * prevent a re-load down the line.
5767 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03005768static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005769{
Hristo Venev75b28af2019-08-26 17:23:46 +00005770 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005771 unsigned head;
5772
5773 /*
5774 * The cached sq head (or cq tail) serves two purposes:
5775 *
5776 * 1) allows us to batch the cost of updating the user visible
5777 * head updates.
5778 * 2) allows the kernel side to track the head on its own, even
5779 * though the application is the one updating it.
5780 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005781 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03005782 if (likely(head < ctx->sq_entries))
5783 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07005784
5785 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06005786 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005787 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03005788 return NULL;
5789}
5790
5791static inline void io_consume_sqe(struct io_ring_ctx *ctx)
5792{
5793 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005794}
5795
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005796#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
5797 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
5798 IOSQE_BUFFER_SELECT)
5799
5800static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
5801 const struct io_uring_sqe *sqe,
5802 struct io_submit_state *state, bool async)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005803{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005804 unsigned int sqe_flags;
5805 int id, fd;
5806
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005807 /*
5808 * All io need record the previous position, if LINK vs DARIN,
5809 * it can be used to mark the position of the first IO in the
5810 * link list.
5811 */
Pavel Begunkov31af27c2020-04-15 00:39:50 +03005812 req->sequence = ctx->cached_sq_head - ctx->cached_sq_dropped;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005813 req->opcode = READ_ONCE(sqe->opcode);
5814 req->user_data = READ_ONCE(sqe->user_data);
5815 req->io = NULL;
5816 req->file = NULL;
5817 req->ctx = ctx;
5818 req->flags = 0;
5819 /* one is dropped after submission, the other at completion */
5820 refcount_set(&req->refs, 2);
5821 req->task = NULL;
5822 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005823 req->needs_fixed_file = async;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005824 INIT_IO_WORK(&req->work, io_wq_submit_work);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005825
5826 if (unlikely(req->opcode >= IORING_OP_LAST))
5827 return -EINVAL;
5828
5829 if (io_op_defs[req->opcode].needs_mm && !current->mm) {
5830 if (unlikely(!mmget_not_zero(ctx->sqo_mm)))
5831 return -EFAULT;
5832 use_mm(ctx->sqo_mm);
5833 }
5834
5835 sqe_flags = READ_ONCE(sqe->flags);
5836 /* enforce forwards compatibility on users */
5837 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
5838 return -EINVAL;
5839
5840 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
5841 !io_op_defs[req->opcode].buffer_select)
5842 return -EOPNOTSUPP;
5843
5844 id = READ_ONCE(sqe->personality);
5845 if (id) {
5846 req->work.creds = idr_find(&ctx->personality_idr, id);
5847 if (unlikely(!req->work.creds))
5848 return -EINVAL;
5849 get_cred(req->work.creds);
5850 }
5851
5852 /* same numerical values with corresponding REQ_F_*, safe to copy */
5853 req->flags |= sqe_flags & (IOSQE_IO_DRAIN | IOSQE_IO_HARDLINK |
5854 IOSQE_ASYNC | IOSQE_FIXED_FILE |
5855 IOSQE_BUFFER_SELECT | IOSQE_IO_LINK);
5856
5857 fd = READ_ONCE(sqe->fd);
5858 return io_req_set_file(state, req, fd, sqe_flags);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005859}
5860
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005861static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03005862 struct file *ring_file, int ring_fd, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07005863{
5864 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005865 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005866 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005867
Jens Axboec4a2ed72019-11-21 21:01:26 -07005868 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07005869 if (test_bit(0, &ctx->sq_check_overflow)) {
5870 if (!list_empty(&ctx->cq_overflow_list) &&
5871 !io_cqring_overflow_flush(ctx, false))
5872 return -EBUSY;
5873 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005874
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005875 /* make sure SQ entry isn't read before tail */
5876 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03005877
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005878 if (!percpu_ref_tryget_many(&ctx->refs, nr))
5879 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005880
5881 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08005882 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005883 statep = &state;
5884 }
5885
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005886 ctx->ring_fd = ring_fd;
5887 ctx->ring_file = ring_file;
5888
Jens Axboe6c271ce2019-01-10 11:22:30 -07005889 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005890 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03005891 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005892 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005893
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03005894 sqe = io_get_sqe(ctx);
5895 if (unlikely(!sqe)) {
5896 io_consume_sqe(ctx);
5897 break;
5898 }
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005899 req = io_alloc_req(ctx, statep);
Pavel Begunkov196be952019-11-07 01:41:06 +03005900 if (unlikely(!req)) {
5901 if (!submitted)
5902 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005903 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06005904 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005905
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005906 err = io_init_req(ctx, req, sqe, statep, async);
Pavel Begunkov709b3022020-04-08 08:58:43 +03005907 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07005908 /* will complete beyond this point, count as submitted */
5909 submitted++;
5910
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005911 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005912fail_req:
5913 io_cqring_add_event(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07005914 io_double_put_req(req);
5915 break;
5916 }
5917
Jens Axboe354420f2020-01-08 18:55:15 -07005918 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
5919 true, async);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005920 err = io_submit_sqe(req, sqe, statep, &link);
5921 if (err)
5922 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005923 }
5924
Pavel Begunkov9466f432020-01-25 22:34:01 +03005925 if (unlikely(submitted != nr)) {
5926 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
5927
5928 percpu_ref_put_many(&ctx->refs, nr - ref_used);
5929 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005930 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005931 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005932 if (statep)
5933 io_submit_state_end(&state);
5934
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005935 /* Commit SQ ring head once we've consumed and submitted all SQEs */
5936 io_commit_sqring(ctx);
5937
Jens Axboe6c271ce2019-01-10 11:22:30 -07005938 return submitted;
5939}
5940
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03005941static inline void io_sq_thread_drop_mm(struct io_ring_ctx *ctx)
5942{
5943 struct mm_struct *mm = current->mm;
5944
5945 if (mm) {
5946 unuse_mm(mm);
5947 mmput(mm);
5948 }
5949}
5950
Jens Axboe6c271ce2019-01-10 11:22:30 -07005951static int io_sq_thread(void *data)
5952{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005953 struct io_ring_ctx *ctx = data;
Jens Axboe181e4482019-11-25 08:52:30 -07005954 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005955 mm_segment_t old_fs;
5956 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005957 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005958 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005959
Jens Axboe206aefd2019-11-07 18:27:42 -07005960 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08005961
Jens Axboe6c271ce2019-01-10 11:22:30 -07005962 old_fs = get_fs();
5963 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07005964 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005965
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005966 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005967 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005968 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005969
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005970 if (!list_empty(&ctx->poll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005971 unsigned nr_events = 0;
5972
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005973 mutex_lock(&ctx->uring_lock);
5974 if (!list_empty(&ctx->poll_list))
5975 io_iopoll_getevents(ctx, &nr_events, 0);
5976 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07005977 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005978 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005979 }
5980
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005981 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005982
5983 /*
5984 * If submit got -EBUSY, flag us as needing the application
5985 * to enter the kernel to reap and flush events.
5986 */
5987 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005988 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01005989 * Drop cur_mm before scheduling, we can't hold it for
5990 * long periods (or over schedule()). Do this before
5991 * adding ourselves to the waitqueue, as the unuse/drop
5992 * may sleep.
5993 */
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03005994 io_sq_thread_drop_mm(ctx);
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01005995
5996 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07005997 * We're polling. If we're within the defined idle
5998 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07005999 * to sleep. The exception is if we got EBUSY doing
6000 * more IO, we should wait for the application to
6001 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07006002 */
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006003 if (!list_empty(&ctx->poll_list) ||
Jens Axboedf069d82020-02-04 16:48:34 -07006004 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6005 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboeb41e9852020-02-17 09:52:41 -07006006 if (current->task_works)
6007 task_work_run();
Jens Axboe9831a902019-09-19 09:48:55 -06006008 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006009 continue;
6010 }
6011
Jens Axboe6c271ce2019-01-10 11:22:30 -07006012 prepare_to_wait(&ctx->sqo_wait, &wait,
6013 TASK_INTERRUPTIBLE);
6014
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006015 /*
6016 * While doing polled IO, before going to sleep, we need
6017 * to check if there are new reqs added to poll_list, it
6018 * is because reqs may have been punted to io worker and
6019 * will be added to poll_list later, hence check the
6020 * poll_list again.
6021 */
6022 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6023 !list_empty_careful(&ctx->poll_list)) {
6024 finish_wait(&ctx->sqo_wait, &wait);
6025 continue;
6026 }
6027
Jens Axboe6c271ce2019-01-10 11:22:30 -07006028 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00006029 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02006030 /* make sure to read SQ tail after writing flags */
6031 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006032
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006033 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006034 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006035 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006036 finish_wait(&ctx->sqo_wait, &wait);
6037 break;
6038 }
Jens Axboeb41e9852020-02-17 09:52:41 -07006039 if (current->task_works) {
6040 task_work_run();
Hillf Danton10bea962020-04-01 17:19:33 +08006041 finish_wait(&ctx->sqo_wait, &wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07006042 continue;
6043 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006044 if (signal_pending(current))
6045 flush_signals(current);
6046 schedule();
6047 finish_wait(&ctx->sqo_wait, &wait);
6048
Hristo Venev75b28af2019-08-26 17:23:46 +00006049 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006050 continue;
6051 }
6052 finish_wait(&ctx->sqo_wait, &wait);
6053
Hristo Venev75b28af2019-08-26 17:23:46 +00006054 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006055 }
6056
Jens Axboe8a4955f2019-12-09 14:52:35 -07006057 mutex_lock(&ctx->uring_lock);
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03006058 ret = io_submit_sqes(ctx, to_submit, NULL, -1, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07006059 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006060 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006061 }
6062
Jens Axboeb41e9852020-02-17 09:52:41 -07006063 if (current->task_works)
6064 task_work_run();
6065
Jens Axboe6c271ce2019-01-10 11:22:30 -07006066 set_fs(old_fs);
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03006067 io_sq_thread_drop_mm(ctx);
Jens Axboe181e4482019-11-25 08:52:30 -07006068 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006069
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006070 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006071
Jens Axboe6c271ce2019-01-10 11:22:30 -07006072 return 0;
6073}
6074
Jens Axboebda52162019-09-24 13:47:15 -06006075struct io_wait_queue {
6076 struct wait_queue_entry wq;
6077 struct io_ring_ctx *ctx;
6078 unsigned to_wait;
6079 unsigned nr_timeouts;
6080};
6081
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006082static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06006083{
6084 struct io_ring_ctx *ctx = iowq->ctx;
6085
6086 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006087 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006088 * started waiting. For timeouts, we always want to return to userspace,
6089 * regardless of event count.
6090 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006091 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006092 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6093}
6094
6095static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6096 int wake_flags, void *key)
6097{
6098 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6099 wq);
6100
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006101 /* use noflush == true, as we can't safely rely on locking context */
6102 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06006103 return -1;
6104
6105 return autoremove_wake_function(curr, mode, wake_flags, key);
6106}
6107
Jens Axboe2b188cc2019-01-07 10:46:33 -07006108/*
6109 * Wait until events become available, if we don't already have some. The
6110 * application must reap them itself, as they reside on the shared cq ring.
6111 */
6112static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6113 const sigset_t __user *sig, size_t sigsz)
6114{
Jens Axboebda52162019-09-24 13:47:15 -06006115 struct io_wait_queue iowq = {
6116 .wq = {
6117 .private = current,
6118 .func = io_wake_function,
6119 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6120 },
6121 .ctx = ctx,
6122 .to_wait = min_events,
6123 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006124 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006125 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006126
Jens Axboeb41e9852020-02-17 09:52:41 -07006127 do {
6128 if (io_cqring_events(ctx, false) >= min_events)
6129 return 0;
6130 if (!current->task_works)
6131 break;
6132 task_work_run();
6133 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006134
6135 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006136#ifdef CONFIG_COMPAT
6137 if (in_compat_syscall())
6138 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006139 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006140 else
6141#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006142 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006143
Jens Axboe2b188cc2019-01-07 10:46:33 -07006144 if (ret)
6145 return ret;
6146 }
6147
Jens Axboebda52162019-09-24 13:47:15 -06006148 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006149 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006150 do {
6151 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6152 TASK_INTERRUPTIBLE);
Jens Axboeb41e9852020-02-17 09:52:41 -07006153 if (current->task_works)
6154 task_work_run();
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006155 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06006156 break;
6157 schedule();
6158 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006159 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06006160 break;
6161 }
6162 } while (1);
6163 finish_wait(&ctx->wait, &iowq.wq);
6164
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006165 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006166
Hristo Venev75b28af2019-08-26 17:23:46 +00006167 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006168}
6169
Jens Axboe6b063142019-01-10 22:13:58 -07006170static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6171{
6172#if defined(CONFIG_UNIX)
6173 if (ctx->ring_sock) {
6174 struct sock *sock = ctx->ring_sock->sk;
6175 struct sk_buff *skb;
6176
6177 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6178 kfree_skb(skb);
6179 }
6180#else
6181 int i;
6182
Jens Axboe65e19f52019-10-26 07:20:21 -06006183 for (i = 0; i < ctx->nr_user_files; i++) {
6184 struct file *file;
6185
6186 file = io_file_from_index(ctx, i);
6187 if (file)
6188 fput(file);
6189 }
Jens Axboe6b063142019-01-10 22:13:58 -07006190#endif
6191}
6192
Jens Axboe05f3fb32019-12-09 11:22:50 -07006193static void io_file_ref_kill(struct percpu_ref *ref)
6194{
6195 struct fixed_file_data *data;
6196
6197 data = container_of(ref, struct fixed_file_data, refs);
6198 complete(&data->done);
6199}
6200
Jens Axboe6b063142019-01-10 22:13:58 -07006201static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6202{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006203 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006204 struct fixed_file_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006205 unsigned nr_tables, i;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006206 unsigned long flags;
Jens Axboe65e19f52019-10-26 07:20:21 -06006207
Jens Axboe05f3fb32019-12-09 11:22:50 -07006208 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006209 return -ENXIO;
6210
Xiaoguang Wang05589552020-03-31 14:05:18 +08006211 spin_lock_irqsave(&data->lock, flags);
6212 if (!list_empty(&data->ref_list))
6213 ref_node = list_first_entry(&data->ref_list,
6214 struct fixed_file_ref_node, node);
6215 spin_unlock_irqrestore(&data->lock, flags);
6216 if (ref_node)
6217 percpu_ref_kill(&ref_node->refs);
6218
6219 percpu_ref_kill(&data->refs);
6220
6221 /* wait for all refs nodes to complete */
Jens Axboe2faf8522020-02-04 19:54:55 -07006222 wait_for_completion(&data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006223
Jens Axboe6b063142019-01-10 22:13:58 -07006224 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06006225 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6226 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006227 kfree(data->table[i].files);
6228 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006229 percpu_ref_exit(&data->refs);
6230 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006231 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006232 ctx->nr_user_files = 0;
6233 return 0;
6234}
6235
Jens Axboe6c271ce2019-01-10 11:22:30 -07006236static void io_sq_thread_stop(struct io_ring_ctx *ctx)
6237{
6238 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07006239 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006240 /*
6241 * The park is a bit of a work-around, without it we get
6242 * warning spews on shutdown with SQPOLL set and affinity
6243 * set to a single CPU.
6244 */
Jens Axboe06058632019-04-13 09:26:03 -06006245 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006246 kthread_stop(ctx->sqo_thread);
6247 ctx->sqo_thread = NULL;
6248 }
6249}
6250
Jens Axboe6b063142019-01-10 22:13:58 -07006251static void io_finish_async(struct io_ring_ctx *ctx)
6252{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006253 io_sq_thread_stop(ctx);
6254
Jens Axboe561fb042019-10-24 07:25:42 -06006255 if (ctx->io_wq) {
6256 io_wq_destroy(ctx->io_wq);
6257 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006258 }
6259}
6260
6261#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07006262/*
6263 * Ensure the UNIX gc is aware of our file set, so we are certain that
6264 * the io_uring can be safely unregistered on process exit, even if we have
6265 * loops in the file referencing.
6266 */
6267static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
6268{
6269 struct sock *sk = ctx->ring_sock->sk;
6270 struct scm_fp_list *fpl;
6271 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06006272 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07006273
Jens Axboe6b063142019-01-10 22:13:58 -07006274 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
6275 if (!fpl)
6276 return -ENOMEM;
6277
6278 skb = alloc_skb(0, GFP_KERNEL);
6279 if (!skb) {
6280 kfree(fpl);
6281 return -ENOMEM;
6282 }
6283
6284 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07006285
Jens Axboe08a45172019-10-03 08:11:03 -06006286 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07006287 fpl->user = get_uid(ctx->user);
6288 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006289 struct file *file = io_file_from_index(ctx, i + offset);
6290
6291 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06006292 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06006293 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06006294 unix_inflight(fpl->user, fpl->fp[nr_files]);
6295 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07006296 }
6297
Jens Axboe08a45172019-10-03 08:11:03 -06006298 if (nr_files) {
6299 fpl->max = SCM_MAX_FD;
6300 fpl->count = nr_files;
6301 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006302 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06006303 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
6304 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07006305
Jens Axboe08a45172019-10-03 08:11:03 -06006306 for (i = 0; i < nr_files; i++)
6307 fput(fpl->fp[i]);
6308 } else {
6309 kfree_skb(skb);
6310 kfree(fpl);
6311 }
Jens Axboe6b063142019-01-10 22:13:58 -07006312
6313 return 0;
6314}
6315
6316/*
6317 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
6318 * causes regular reference counting to break down. We rely on the UNIX
6319 * garbage collection to take care of this problem for us.
6320 */
6321static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6322{
6323 unsigned left, total;
6324 int ret = 0;
6325
6326 total = 0;
6327 left = ctx->nr_user_files;
6328 while (left) {
6329 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07006330
6331 ret = __io_sqe_files_scm(ctx, this_files, total);
6332 if (ret)
6333 break;
6334 left -= this_files;
6335 total += this_files;
6336 }
6337
6338 if (!ret)
6339 return 0;
6340
6341 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006342 struct file *file = io_file_from_index(ctx, total);
6343
6344 if (file)
6345 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07006346 total++;
6347 }
6348
6349 return ret;
6350}
6351#else
6352static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6353{
6354 return 0;
6355}
6356#endif
6357
Jens Axboe65e19f52019-10-26 07:20:21 -06006358static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
6359 unsigned nr_files)
6360{
6361 int i;
6362
6363 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006364 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006365 unsigned this_files;
6366
6367 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
6368 table->files = kcalloc(this_files, sizeof(struct file *),
6369 GFP_KERNEL);
6370 if (!table->files)
6371 break;
6372 nr_files -= this_files;
6373 }
6374
6375 if (i == nr_tables)
6376 return 0;
6377
6378 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006379 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006380 kfree(table->files);
6381 }
6382 return 1;
6383}
6384
Jens Axboe05f3fb32019-12-09 11:22:50 -07006385static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06006386{
6387#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06006388 struct sock *sock = ctx->ring_sock->sk;
6389 struct sk_buff_head list, *head = &sock->sk_receive_queue;
6390 struct sk_buff *skb;
6391 int i;
6392
6393 __skb_queue_head_init(&list);
6394
6395 /*
6396 * Find the skb that holds this file in its SCM_RIGHTS. When found,
6397 * remove this entry and rearrange the file array.
6398 */
6399 skb = skb_dequeue(head);
6400 while (skb) {
6401 struct scm_fp_list *fp;
6402
6403 fp = UNIXCB(skb).fp;
6404 for (i = 0; i < fp->count; i++) {
6405 int left;
6406
6407 if (fp->fp[i] != file)
6408 continue;
6409
6410 unix_notinflight(fp->user, fp->fp[i]);
6411 left = fp->count - 1 - i;
6412 if (left) {
6413 memmove(&fp->fp[i], &fp->fp[i + 1],
6414 left * sizeof(struct file *));
6415 }
6416 fp->count--;
6417 if (!fp->count) {
6418 kfree_skb(skb);
6419 skb = NULL;
6420 } else {
6421 __skb_queue_tail(&list, skb);
6422 }
6423 fput(file);
6424 file = NULL;
6425 break;
6426 }
6427
6428 if (!file)
6429 break;
6430
6431 __skb_queue_tail(&list, skb);
6432
6433 skb = skb_dequeue(head);
6434 }
6435
6436 if (skb_peek(&list)) {
6437 spin_lock_irq(&head->lock);
6438 while ((skb = __skb_dequeue(&list)) != NULL)
6439 __skb_queue_tail(head, skb);
6440 spin_unlock_irq(&head->lock);
6441 }
6442#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07006443 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06006444#endif
6445}
6446
Jens Axboe05f3fb32019-12-09 11:22:50 -07006447struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08006448 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006449 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006450};
6451
Xiaoguang Wang05589552020-03-31 14:05:18 +08006452static void io_file_put_work(struct work_struct *work)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006453{
Xiaoguang Wang05589552020-03-31 14:05:18 +08006454 struct fixed_file_ref_node *ref_node;
6455 struct fixed_file_data *file_data;
6456 struct io_ring_ctx *ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006457 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006458 unsigned long flags;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006459
Xiaoguang Wang05589552020-03-31 14:05:18 +08006460 ref_node = container_of(work, struct fixed_file_ref_node, work);
6461 file_data = ref_node->file_data;
6462 ctx = file_data->ctx;
6463
6464 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
6465 list_del_init(&pfile->list);
6466 io_ring_file_put(ctx, pfile->file);
6467 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006468 }
6469
Xiaoguang Wang05589552020-03-31 14:05:18 +08006470 spin_lock_irqsave(&file_data->lock, flags);
6471 list_del_init(&ref_node->node);
6472 spin_unlock_irqrestore(&file_data->lock, flags);
Jens Axboe2faf8522020-02-04 19:54:55 -07006473
Xiaoguang Wang05589552020-03-31 14:05:18 +08006474 percpu_ref_exit(&ref_node->refs);
6475 kfree(ref_node);
6476 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006477}
6478
6479static void io_file_data_ref_zero(struct percpu_ref *ref)
6480{
Xiaoguang Wang05589552020-03-31 14:05:18 +08006481 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006482
Xiaoguang Wang05589552020-03-31 14:05:18 +08006483 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006484
Xiaoguang Wang05589552020-03-31 14:05:18 +08006485 queue_work(system_wq, &ref_node->work);
6486}
6487
6488static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
6489 struct io_ring_ctx *ctx)
6490{
6491 struct fixed_file_ref_node *ref_node;
6492
6493 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
6494 if (!ref_node)
6495 return ERR_PTR(-ENOMEM);
6496
6497 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
6498 0, GFP_KERNEL)) {
6499 kfree(ref_node);
6500 return ERR_PTR(-ENOMEM);
6501 }
6502 INIT_LIST_HEAD(&ref_node->node);
6503 INIT_LIST_HEAD(&ref_node->file_list);
6504 INIT_WORK(&ref_node->work, io_file_put_work);
6505 ref_node->file_data = ctx->file_data;
6506 return ref_node;
6507
6508}
6509
6510static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
6511{
6512 percpu_ref_exit(&ref_node->refs);
6513 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006514}
6515
6516static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
6517 unsigned nr_args)
6518{
6519 __s32 __user *fds = (__s32 __user *) arg;
6520 unsigned nr_tables;
6521 struct file *file;
6522 int fd, ret = 0;
6523 unsigned i;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006524 struct fixed_file_ref_node *ref_node;
6525 unsigned long flags;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006526
6527 if (ctx->file_data)
6528 return -EBUSY;
6529 if (!nr_args)
6530 return -EINVAL;
6531 if (nr_args > IORING_MAX_FIXED_FILES)
6532 return -EMFILE;
6533
6534 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
6535 if (!ctx->file_data)
6536 return -ENOMEM;
6537 ctx->file_data->ctx = ctx;
6538 init_completion(&ctx->file_data->done);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006539 INIT_LIST_HEAD(&ctx->file_data->ref_list);
Xiaoguang Wangf7fe9342020-04-07 20:02:31 +08006540 spin_lock_init(&ctx->file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006541
6542 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
6543 ctx->file_data->table = kcalloc(nr_tables,
6544 sizeof(struct fixed_file_table),
6545 GFP_KERNEL);
6546 if (!ctx->file_data->table) {
6547 kfree(ctx->file_data);
6548 ctx->file_data = NULL;
6549 return -ENOMEM;
6550 }
6551
Xiaoguang Wang05589552020-03-31 14:05:18 +08006552 if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill,
Jens Axboe05f3fb32019-12-09 11:22:50 -07006553 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
6554 kfree(ctx->file_data->table);
6555 kfree(ctx->file_data);
6556 ctx->file_data = NULL;
6557 return -ENOMEM;
6558 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006559
6560 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
6561 percpu_ref_exit(&ctx->file_data->refs);
6562 kfree(ctx->file_data->table);
6563 kfree(ctx->file_data);
6564 ctx->file_data = NULL;
6565 return -ENOMEM;
6566 }
6567
6568 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
6569 struct fixed_file_table *table;
6570 unsigned index;
6571
6572 ret = -EFAULT;
6573 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
6574 break;
6575 /* allow sparse sets */
6576 if (fd == -1) {
6577 ret = 0;
6578 continue;
6579 }
6580
6581 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6582 index = i & IORING_FILE_TABLE_MASK;
6583 file = fget(fd);
6584
6585 ret = -EBADF;
6586 if (!file)
6587 break;
6588
6589 /*
6590 * Don't allow io_uring instances to be registered. If UNIX
6591 * isn't enabled, then this causes a reference cycle and this
6592 * instance can never get freed. If UNIX is enabled we'll
6593 * handle it just fine, but there's still no point in allowing
6594 * a ring fd as it doesn't support regular read/write anyway.
6595 */
6596 if (file->f_op == &io_uring_fops) {
6597 fput(file);
6598 break;
6599 }
6600 ret = 0;
6601 table->files[index] = file;
6602 }
6603
6604 if (ret) {
6605 for (i = 0; i < ctx->nr_user_files; i++) {
6606 file = io_file_from_index(ctx, i);
6607 if (file)
6608 fput(file);
6609 }
6610 for (i = 0; i < nr_tables; i++)
6611 kfree(ctx->file_data->table[i].files);
6612
6613 kfree(ctx->file_data->table);
6614 kfree(ctx->file_data);
6615 ctx->file_data = NULL;
6616 ctx->nr_user_files = 0;
6617 return ret;
6618 }
6619
6620 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006621 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006622 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006623 return ret;
6624 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006625
Xiaoguang Wang05589552020-03-31 14:05:18 +08006626 ref_node = alloc_fixed_file_ref_node(ctx);
6627 if (IS_ERR(ref_node)) {
6628 io_sqe_files_unregister(ctx);
6629 return PTR_ERR(ref_node);
6630 }
6631
6632 ctx->file_data->cur_refs = &ref_node->refs;
6633 spin_lock_irqsave(&ctx->file_data->lock, flags);
6634 list_add(&ref_node->node, &ctx->file_data->ref_list);
6635 spin_unlock_irqrestore(&ctx->file_data->lock, flags);
6636 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006637 return ret;
6638}
6639
Jens Axboec3a31e62019-10-03 13:59:56 -06006640static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
6641 int index)
6642{
6643#if defined(CONFIG_UNIX)
6644 struct sock *sock = ctx->ring_sock->sk;
6645 struct sk_buff_head *head = &sock->sk_receive_queue;
6646 struct sk_buff *skb;
6647
6648 /*
6649 * See if we can merge this file into an existing skb SCM_RIGHTS
6650 * file set. If there's no room, fall back to allocating a new skb
6651 * and filling it in.
6652 */
6653 spin_lock_irq(&head->lock);
6654 skb = skb_peek(head);
6655 if (skb) {
6656 struct scm_fp_list *fpl = UNIXCB(skb).fp;
6657
6658 if (fpl->count < SCM_MAX_FD) {
6659 __skb_unlink(skb, head);
6660 spin_unlock_irq(&head->lock);
6661 fpl->fp[fpl->count] = get_file(file);
6662 unix_inflight(fpl->user, fpl->fp[fpl->count]);
6663 fpl->count++;
6664 spin_lock_irq(&head->lock);
6665 __skb_queue_head(head, skb);
6666 } else {
6667 skb = NULL;
6668 }
6669 }
6670 spin_unlock_irq(&head->lock);
6671
6672 if (skb) {
6673 fput(file);
6674 return 0;
6675 }
6676
6677 return __io_sqe_files_scm(ctx, 1, index);
6678#else
6679 return 0;
6680#endif
6681}
6682
Hillf Dantona5318d32020-03-23 17:47:15 +08006683static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08006684 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006685{
Hillf Dantona5318d32020-03-23 17:47:15 +08006686 struct io_file_put *pfile;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006687 struct percpu_ref *refs = data->cur_refs;
6688 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006689
Jens Axboe05f3fb32019-12-09 11:22:50 -07006690 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08006691 if (!pfile)
6692 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006693
Xiaoguang Wang05589552020-03-31 14:05:18 +08006694 ref_node = container_of(refs, struct fixed_file_ref_node, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006695 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006696 list_add(&pfile->list, &ref_node->file_list);
6697
Hillf Dantona5318d32020-03-23 17:47:15 +08006698 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006699}
6700
6701static int __io_sqe_files_update(struct io_ring_ctx *ctx,
6702 struct io_uring_files_update *up,
6703 unsigned nr_args)
6704{
6705 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006706 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006707 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006708 __s32 __user *fds;
6709 int fd, i, err;
6710 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006711 unsigned long flags;
6712 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06006713
Jens Axboe05f3fb32019-12-09 11:22:50 -07006714 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06006715 return -EOVERFLOW;
6716 if (done > ctx->nr_user_files)
6717 return -EINVAL;
6718
Xiaoguang Wang05589552020-03-31 14:05:18 +08006719 ref_node = alloc_fixed_file_ref_node(ctx);
6720 if (IS_ERR(ref_node))
6721 return PTR_ERR(ref_node);
6722
Jens Axboec3a31e62019-10-03 13:59:56 -06006723 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006724 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06006725 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006726 struct fixed_file_table *table;
6727 unsigned index;
6728
Jens Axboec3a31e62019-10-03 13:59:56 -06006729 err = 0;
6730 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
6731 err = -EFAULT;
6732 break;
6733 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006734 i = array_index_nospec(up->offset, ctx->nr_user_files);
6735 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06006736 index = i & IORING_FILE_TABLE_MASK;
6737 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006738 file = io_file_from_index(ctx, index);
Hillf Dantona5318d32020-03-23 17:47:15 +08006739 err = io_queue_file_removal(data, file);
6740 if (err)
6741 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06006742 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006743 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06006744 }
6745 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06006746 file = fget(fd);
6747 if (!file) {
6748 err = -EBADF;
6749 break;
6750 }
6751 /*
6752 * Don't allow io_uring instances to be registered. If
6753 * UNIX isn't enabled, then this causes a reference
6754 * cycle and this instance can never get freed. If UNIX
6755 * is enabled we'll handle it just fine, but there's
6756 * still no point in allowing a ring fd as it doesn't
6757 * support regular read/write anyway.
6758 */
6759 if (file->f_op == &io_uring_fops) {
6760 fput(file);
6761 err = -EBADF;
6762 break;
6763 }
Jens Axboe65e19f52019-10-26 07:20:21 -06006764 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006765 err = io_sqe_file_register(ctx, file, i);
6766 if (err)
6767 break;
6768 }
6769 nr_args--;
6770 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006771 up->offset++;
6772 }
6773
Xiaoguang Wang05589552020-03-31 14:05:18 +08006774 if (needs_switch) {
6775 percpu_ref_kill(data->cur_refs);
6776 spin_lock_irqsave(&data->lock, flags);
6777 list_add(&ref_node->node, &data->ref_list);
6778 data->cur_refs = &ref_node->refs;
6779 spin_unlock_irqrestore(&data->lock, flags);
6780 percpu_ref_get(&ctx->file_data->refs);
6781 } else
6782 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06006783
6784 return done ? done : err;
6785}
Xiaoguang Wang05589552020-03-31 14:05:18 +08006786
Jens Axboe05f3fb32019-12-09 11:22:50 -07006787static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
6788 unsigned nr_args)
6789{
6790 struct io_uring_files_update up;
6791
6792 if (!ctx->file_data)
6793 return -ENXIO;
6794 if (!nr_args)
6795 return -EINVAL;
6796 if (copy_from_user(&up, arg, sizeof(up)))
6797 return -EFAULT;
6798 if (up.resv)
6799 return -EINVAL;
6800
6801 return __io_sqe_files_update(ctx, &up, nr_args);
6802}
Jens Axboec3a31e62019-10-03 13:59:56 -06006803
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006804static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07006805{
6806 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6807
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006808 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07006809 io_put_req(req);
6810}
6811
Pavel Begunkov24369c22020-01-28 03:15:48 +03006812static int io_init_wq_offload(struct io_ring_ctx *ctx,
6813 struct io_uring_params *p)
6814{
6815 struct io_wq_data data;
6816 struct fd f;
6817 struct io_ring_ctx *ctx_attach;
6818 unsigned int concurrency;
6819 int ret = 0;
6820
6821 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006822 data.free_work = io_free_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03006823
6824 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
6825 /* Do QD, or 4 * CPUS, whatever is smallest */
6826 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
6827
6828 ctx->io_wq = io_wq_create(concurrency, &data);
6829 if (IS_ERR(ctx->io_wq)) {
6830 ret = PTR_ERR(ctx->io_wq);
6831 ctx->io_wq = NULL;
6832 }
6833 return ret;
6834 }
6835
6836 f = fdget(p->wq_fd);
6837 if (!f.file)
6838 return -EBADF;
6839
6840 if (f.file->f_op != &io_uring_fops) {
6841 ret = -EINVAL;
6842 goto out_fput;
6843 }
6844
6845 ctx_attach = f.file->private_data;
6846 /* @io_wq is protected by holding the fd */
6847 if (!io_wq_get(ctx_attach->io_wq, &data)) {
6848 ret = -EINVAL;
6849 goto out_fput;
6850 }
6851
6852 ctx->io_wq = ctx_attach->io_wq;
6853out_fput:
6854 fdput(f);
6855 return ret;
6856}
6857
Jens Axboe6c271ce2019-01-10 11:22:30 -07006858static int io_sq_offload_start(struct io_ring_ctx *ctx,
6859 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006860{
6861 int ret;
6862
Jens Axboe6c271ce2019-01-10 11:22:30 -07006863 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006864 mmgrab(current->mm);
6865 ctx->sqo_mm = current->mm;
6866
Jens Axboe6c271ce2019-01-10 11:22:30 -07006867 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06006868 ret = -EPERM;
6869 if (!capable(CAP_SYS_ADMIN))
6870 goto err;
6871
Jens Axboe917257d2019-04-13 09:28:55 -06006872 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
6873 if (!ctx->sq_thread_idle)
6874 ctx->sq_thread_idle = HZ;
6875
Jens Axboe6c271ce2019-01-10 11:22:30 -07006876 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06006877 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006878
Jens Axboe917257d2019-04-13 09:28:55 -06006879 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06006880 if (cpu >= nr_cpu_ids)
6881 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08006882 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06006883 goto err;
6884
Jens Axboe6c271ce2019-01-10 11:22:30 -07006885 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
6886 ctx, cpu,
6887 "io_uring-sq");
6888 } else {
6889 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
6890 "io_uring-sq");
6891 }
6892 if (IS_ERR(ctx->sqo_thread)) {
6893 ret = PTR_ERR(ctx->sqo_thread);
6894 ctx->sqo_thread = NULL;
6895 goto err;
6896 }
6897 wake_up_process(ctx->sqo_thread);
6898 } else if (p->flags & IORING_SETUP_SQ_AFF) {
6899 /* Can't have SQ_AFF without SQPOLL */
6900 ret = -EINVAL;
6901 goto err;
6902 }
6903
Pavel Begunkov24369c22020-01-28 03:15:48 +03006904 ret = io_init_wq_offload(ctx, p);
6905 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006906 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006907
6908 return 0;
6909err:
Jens Axboe54a91f32019-09-10 09:15:04 -06006910 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006911 mmdrop(ctx->sqo_mm);
6912 ctx->sqo_mm = NULL;
6913 return ret;
6914}
6915
6916static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
6917{
6918 atomic_long_sub(nr_pages, &user->locked_vm);
6919}
6920
6921static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
6922{
6923 unsigned long page_limit, cur_pages, new_pages;
6924
6925 /* Don't allow more pages than we can safely lock */
6926 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
6927
6928 do {
6929 cur_pages = atomic_long_read(&user->locked_vm);
6930 new_pages = cur_pages + nr_pages;
6931 if (new_pages > page_limit)
6932 return -ENOMEM;
6933 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
6934 new_pages) != cur_pages);
6935
6936 return 0;
6937}
6938
6939static void io_mem_free(void *ptr)
6940{
Mark Rutland52e04ef2019-04-30 17:30:21 +01006941 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006942
Mark Rutland52e04ef2019-04-30 17:30:21 +01006943 if (!ptr)
6944 return;
6945
6946 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006947 if (put_page_testzero(page))
6948 free_compound_page(page);
6949}
6950
6951static void *io_mem_alloc(size_t size)
6952{
6953 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
6954 __GFP_NORETRY;
6955
6956 return (void *) __get_free_pages(gfp_flags, get_order(size));
6957}
6958
Hristo Venev75b28af2019-08-26 17:23:46 +00006959static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
6960 size_t *sq_offset)
6961{
6962 struct io_rings *rings;
6963 size_t off, sq_array_size;
6964
6965 off = struct_size(rings, cqes, cq_entries);
6966 if (off == SIZE_MAX)
6967 return SIZE_MAX;
6968
6969#ifdef CONFIG_SMP
6970 off = ALIGN(off, SMP_CACHE_BYTES);
6971 if (off == 0)
6972 return SIZE_MAX;
6973#endif
6974
6975 sq_array_size = array_size(sizeof(u32), sq_entries);
6976 if (sq_array_size == SIZE_MAX)
6977 return SIZE_MAX;
6978
6979 if (check_add_overflow(off, sq_array_size, &off))
6980 return SIZE_MAX;
6981
6982 if (sq_offset)
6983 *sq_offset = off;
6984
6985 return off;
6986}
6987
Jens Axboe2b188cc2019-01-07 10:46:33 -07006988static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
6989{
Hristo Venev75b28af2019-08-26 17:23:46 +00006990 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006991
Hristo Venev75b28af2019-08-26 17:23:46 +00006992 pages = (size_t)1 << get_order(
6993 rings_size(sq_entries, cq_entries, NULL));
6994 pages += (size_t)1 << get_order(
6995 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07006996
Hristo Venev75b28af2019-08-26 17:23:46 +00006997 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006998}
6999
Jens Axboeedafcce2019-01-09 09:16:05 -07007000static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
7001{
7002 int i, j;
7003
7004 if (!ctx->user_bufs)
7005 return -ENXIO;
7006
7007 for (i = 0; i < ctx->nr_user_bufs; i++) {
7008 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7009
7010 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007011 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07007012
7013 if (ctx->account_mem)
7014 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007015 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007016 imu->nr_bvecs = 0;
7017 }
7018
7019 kfree(ctx->user_bufs);
7020 ctx->user_bufs = NULL;
7021 ctx->nr_user_bufs = 0;
7022 return 0;
7023}
7024
7025static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
7026 void __user *arg, unsigned index)
7027{
7028 struct iovec __user *src;
7029
7030#ifdef CONFIG_COMPAT
7031 if (ctx->compat) {
7032 struct compat_iovec __user *ciovs;
7033 struct compat_iovec ciov;
7034
7035 ciovs = (struct compat_iovec __user *) arg;
7036 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
7037 return -EFAULT;
7038
Jens Axboed55e5f52019-12-11 16:12:15 -07007039 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07007040 dst->iov_len = ciov.iov_len;
7041 return 0;
7042 }
7043#endif
7044 src = (struct iovec __user *) arg;
7045 if (copy_from_user(dst, &src[index], sizeof(*dst)))
7046 return -EFAULT;
7047 return 0;
7048}
7049
7050static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
7051 unsigned nr_args)
7052{
7053 struct vm_area_struct **vmas = NULL;
7054 struct page **pages = NULL;
7055 int i, j, got_pages = 0;
7056 int ret = -EINVAL;
7057
7058 if (ctx->user_bufs)
7059 return -EBUSY;
7060 if (!nr_args || nr_args > UIO_MAXIOV)
7061 return -EINVAL;
7062
7063 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
7064 GFP_KERNEL);
7065 if (!ctx->user_bufs)
7066 return -ENOMEM;
7067
7068 for (i = 0; i < nr_args; i++) {
7069 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7070 unsigned long off, start, end, ubuf;
7071 int pret, nr_pages;
7072 struct iovec iov;
7073 size_t size;
7074
7075 ret = io_copy_iov(ctx, &iov, arg, i);
7076 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03007077 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007078
7079 /*
7080 * Don't impose further limits on the size and buffer
7081 * constraints here, we'll -EINVAL later when IO is
7082 * submitted if they are wrong.
7083 */
7084 ret = -EFAULT;
7085 if (!iov.iov_base || !iov.iov_len)
7086 goto err;
7087
7088 /* arbitrary limit, but we need something */
7089 if (iov.iov_len > SZ_1G)
7090 goto err;
7091
7092 ubuf = (unsigned long) iov.iov_base;
7093 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
7094 start = ubuf >> PAGE_SHIFT;
7095 nr_pages = end - start;
7096
7097 if (ctx->account_mem) {
7098 ret = io_account_mem(ctx->user, nr_pages);
7099 if (ret)
7100 goto err;
7101 }
7102
7103 ret = 0;
7104 if (!pages || nr_pages > got_pages) {
7105 kfree(vmas);
7106 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007107 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07007108 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007109 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07007110 sizeof(struct vm_area_struct *),
7111 GFP_KERNEL);
7112 if (!pages || !vmas) {
7113 ret = -ENOMEM;
7114 if (ctx->account_mem)
7115 io_unaccount_mem(ctx->user, nr_pages);
7116 goto err;
7117 }
7118 got_pages = nr_pages;
7119 }
7120
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007121 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07007122 GFP_KERNEL);
7123 ret = -ENOMEM;
7124 if (!imu->bvec) {
7125 if (ctx->account_mem)
7126 io_unaccount_mem(ctx->user, nr_pages);
7127 goto err;
7128 }
7129
7130 ret = 0;
7131 down_read(&current->mm->mmap_sem);
John Hubbard2113b052020-01-30 22:13:13 -08007132 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07007133 FOLL_WRITE | FOLL_LONGTERM,
7134 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007135 if (pret == nr_pages) {
7136 /* don't support file backed memory */
7137 for (j = 0; j < nr_pages; j++) {
7138 struct vm_area_struct *vma = vmas[j];
7139
7140 if (vma->vm_file &&
7141 !is_file_hugepages(vma->vm_file)) {
7142 ret = -EOPNOTSUPP;
7143 break;
7144 }
7145 }
7146 } else {
7147 ret = pret < 0 ? pret : -EFAULT;
7148 }
7149 up_read(&current->mm->mmap_sem);
7150 if (ret) {
7151 /*
7152 * if we did partial map, or found file backed vmas,
7153 * release any pages we did get
7154 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07007155 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007156 unpin_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07007157 if (ctx->account_mem)
7158 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007159 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007160 goto err;
7161 }
7162
7163 off = ubuf & ~PAGE_MASK;
7164 size = iov.iov_len;
7165 for (j = 0; j < nr_pages; j++) {
7166 size_t vec_len;
7167
7168 vec_len = min_t(size_t, size, PAGE_SIZE - off);
7169 imu->bvec[j].bv_page = pages[j];
7170 imu->bvec[j].bv_len = vec_len;
7171 imu->bvec[j].bv_offset = off;
7172 off = 0;
7173 size -= vec_len;
7174 }
7175 /* store original address for later verification */
7176 imu->ubuf = ubuf;
7177 imu->len = iov.iov_len;
7178 imu->nr_bvecs = nr_pages;
7179
7180 ctx->nr_user_bufs++;
7181 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007182 kvfree(pages);
7183 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007184 return 0;
7185err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007186 kvfree(pages);
7187 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007188 io_sqe_buffer_unregister(ctx);
7189 return ret;
7190}
7191
Jens Axboe9b402842019-04-11 11:45:41 -06007192static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
7193{
7194 __s32 __user *fds = arg;
7195 int fd;
7196
7197 if (ctx->cq_ev_fd)
7198 return -EBUSY;
7199
7200 if (copy_from_user(&fd, fds, sizeof(*fds)))
7201 return -EFAULT;
7202
7203 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
7204 if (IS_ERR(ctx->cq_ev_fd)) {
7205 int ret = PTR_ERR(ctx->cq_ev_fd);
7206 ctx->cq_ev_fd = NULL;
7207 return ret;
7208 }
7209
7210 return 0;
7211}
7212
7213static int io_eventfd_unregister(struct io_ring_ctx *ctx)
7214{
7215 if (ctx->cq_ev_fd) {
7216 eventfd_ctx_put(ctx->cq_ev_fd);
7217 ctx->cq_ev_fd = NULL;
7218 return 0;
7219 }
7220
7221 return -ENXIO;
7222}
7223
Jens Axboe5a2e7452020-02-23 16:23:11 -07007224static int __io_destroy_buffers(int id, void *p, void *data)
7225{
7226 struct io_ring_ctx *ctx = data;
7227 struct io_buffer *buf = p;
7228
Jens Axboe067524e2020-03-02 16:32:28 -07007229 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007230 return 0;
7231}
7232
7233static void io_destroy_buffers(struct io_ring_ctx *ctx)
7234{
7235 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
7236 idr_destroy(&ctx->io_buffer_idr);
7237}
7238
Jens Axboe2b188cc2019-01-07 10:46:33 -07007239static void io_ring_ctx_free(struct io_ring_ctx *ctx)
7240{
Jens Axboe6b063142019-01-10 22:13:58 -07007241 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007242 if (ctx->sqo_mm)
7243 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07007244
7245 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07007246 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07007247 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06007248 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007249 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07007250 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07007251
Jens Axboe2b188cc2019-01-07 10:46:33 -07007252#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07007253 if (ctx->ring_sock) {
7254 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007255 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07007256 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007257#endif
7258
Hristo Venev75b28af2019-08-26 17:23:46 +00007259 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007260 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007261
7262 percpu_ref_exit(&ctx->refs);
7263 if (ctx->account_mem)
7264 io_unaccount_mem(ctx->user,
7265 ring_pages(ctx->sq_entries, ctx->cq_entries));
7266 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07007267 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07007268 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07007269 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07007270 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007271 kfree(ctx);
7272}
7273
7274static __poll_t io_uring_poll(struct file *file, poll_table *wait)
7275{
7276 struct io_ring_ctx *ctx = file->private_data;
7277 __poll_t mask = 0;
7278
7279 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02007280 /*
7281 * synchronizes with barrier from wq_has_sleeper call in
7282 * io_commit_cqring
7283 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007284 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00007285 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
7286 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007287 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01007288 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007289 mask |= EPOLLIN | EPOLLRDNORM;
7290
7291 return mask;
7292}
7293
7294static int io_uring_fasync(int fd, struct file *file, int on)
7295{
7296 struct io_ring_ctx *ctx = file->private_data;
7297
7298 return fasync_helper(fd, file, on, &ctx->cq_fasync);
7299}
7300
Jens Axboe071698e2020-01-28 10:04:42 -07007301static int io_remove_personalities(int id, void *p, void *data)
7302{
7303 struct io_ring_ctx *ctx = data;
7304 const struct cred *cred;
7305
7306 cred = idr_remove(&ctx->personality_idr, id);
7307 if (cred)
7308 put_cred(cred);
7309 return 0;
7310}
7311
Jens Axboe85faa7b2020-04-09 18:14:00 -06007312static void io_ring_exit_work(struct work_struct *work)
7313{
7314 struct io_ring_ctx *ctx;
7315
7316 ctx = container_of(work, struct io_ring_ctx, exit_work);
7317 if (ctx->rings)
7318 io_cqring_overflow_flush(ctx, true);
7319
7320 wait_for_completion(&ctx->completions[0]);
7321 io_ring_ctx_free(ctx);
7322}
7323
Jens Axboe2b188cc2019-01-07 10:46:33 -07007324static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
7325{
7326 mutex_lock(&ctx->uring_lock);
7327 percpu_ref_kill(&ctx->refs);
7328 mutex_unlock(&ctx->uring_lock);
7329
Jens Axboedf069d82020-02-04 16:48:34 -07007330 /*
7331 * Wait for sq thread to idle, if we have one. It won't spin on new
7332 * work after we've killed the ctx ref above. This is important to do
7333 * before we cancel existing commands, as the thread could otherwise
7334 * be queueing new work post that. If that's work we need to cancel,
7335 * it could cause shutdown to hang.
7336 */
7337 while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait))
7338 cpu_relax();
7339
Jens Axboe5262f562019-09-17 12:26:57 -06007340 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07007341 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06007342
7343 if (ctx->io_wq)
7344 io_wq_cancel_all(ctx->io_wq);
7345
Jens Axboedef596e2019-01-09 08:59:42 -07007346 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07007347 /* if we failed setting up the ctx, we might not have any rings */
7348 if (ctx->rings)
7349 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07007350 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe85faa7b2020-04-09 18:14:00 -06007351 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
7352 queue_work(system_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007353}
7354
7355static int io_uring_release(struct inode *inode, struct file *file)
7356{
7357 struct io_ring_ctx *ctx = file->private_data;
7358
7359 file->private_data = NULL;
7360 io_ring_ctx_wait_and_kill(ctx);
7361 return 0;
7362}
7363
Jens Axboefcb323c2019-10-24 12:39:47 -06007364static void io_uring_cancel_files(struct io_ring_ctx *ctx,
7365 struct files_struct *files)
7366{
7367 struct io_kiocb *req;
7368 DEFINE_WAIT(wait);
7369
7370 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07007371 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06007372
7373 spin_lock_irq(&ctx->inflight_lock);
7374 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07007375 if (req->work.files != files)
7376 continue;
7377 /* req is being completed, ignore */
7378 if (!refcount_inc_not_zero(&req->refs))
7379 continue;
7380 cancel_req = req;
7381 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06007382 }
Jens Axboe768134d2019-11-10 20:30:53 -07007383 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007384 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07007385 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06007386 spin_unlock_irq(&ctx->inflight_lock);
7387
Jens Axboe768134d2019-11-10 20:30:53 -07007388 /* We need to keep going until we don't find a matching req */
7389 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007390 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08007391
Jens Axboe2ca10252020-02-13 17:17:35 -07007392 if (cancel_req->flags & REQ_F_OVERFLOW) {
7393 spin_lock_irq(&ctx->completion_lock);
7394 list_del(&cancel_req->list);
7395 cancel_req->flags &= ~REQ_F_OVERFLOW;
7396 if (list_empty(&ctx->cq_overflow_list)) {
7397 clear_bit(0, &ctx->sq_check_overflow);
7398 clear_bit(0, &ctx->cq_check_overflow);
7399 }
7400 spin_unlock_irq(&ctx->completion_lock);
7401
7402 WRITE_ONCE(ctx->rings->cq_overflow,
7403 atomic_inc_return(&ctx->cached_cq_overflow));
7404
7405 /*
7406 * Put inflight ref and overflow ref. If that's
7407 * all we had, then we're done with this request.
7408 */
7409 if (refcount_sub_and_test(2, &cancel_req->refs)) {
7410 io_put_req(cancel_req);
7411 continue;
7412 }
7413 }
7414
Bob Liu2f6d9b92019-11-13 18:06:24 +08007415 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
7416 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06007417 schedule();
7418 }
Jens Axboe768134d2019-11-10 20:30:53 -07007419 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007420}
7421
7422static int io_uring_flush(struct file *file, void *data)
7423{
7424 struct io_ring_ctx *ctx = file->private_data;
7425
7426 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07007427
7428 /*
7429 * If the task is going away, cancel work it may have pending
7430 */
7431 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
7432 io_wq_cancel_pid(ctx->io_wq, task_pid_vnr(current));
7433
Jens Axboefcb323c2019-10-24 12:39:47 -06007434 return 0;
7435}
7436
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007437static void *io_uring_validate_mmap_request(struct file *file,
7438 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007439{
Jens Axboe2b188cc2019-01-07 10:46:33 -07007440 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007441 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007442 struct page *page;
7443 void *ptr;
7444
7445 switch (offset) {
7446 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00007447 case IORING_OFF_CQ_RING:
7448 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007449 break;
7450 case IORING_OFF_SQES:
7451 ptr = ctx->sq_sqes;
7452 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007453 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007454 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007455 }
7456
7457 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07007458 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007459 return ERR_PTR(-EINVAL);
7460
7461 return ptr;
7462}
7463
7464#ifdef CONFIG_MMU
7465
7466static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7467{
7468 size_t sz = vma->vm_end - vma->vm_start;
7469 unsigned long pfn;
7470 void *ptr;
7471
7472 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
7473 if (IS_ERR(ptr))
7474 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007475
7476 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
7477 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
7478}
7479
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007480#else /* !CONFIG_MMU */
7481
7482static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7483{
7484 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
7485}
7486
7487static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
7488{
7489 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
7490}
7491
7492static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
7493 unsigned long addr, unsigned long len,
7494 unsigned long pgoff, unsigned long flags)
7495{
7496 void *ptr;
7497
7498 ptr = io_uring_validate_mmap_request(file, pgoff, len);
7499 if (IS_ERR(ptr))
7500 return PTR_ERR(ptr);
7501
7502 return (unsigned long) ptr;
7503}
7504
7505#endif /* !CONFIG_MMU */
7506
Jens Axboe2b188cc2019-01-07 10:46:33 -07007507SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
7508 u32, min_complete, u32, flags, const sigset_t __user *, sig,
7509 size_t, sigsz)
7510{
7511 struct io_ring_ctx *ctx;
7512 long ret = -EBADF;
7513 int submitted = 0;
7514 struct fd f;
7515
Jens Axboeb41e9852020-02-17 09:52:41 -07007516 if (current->task_works)
7517 task_work_run();
7518
Jens Axboe6c271ce2019-01-10 11:22:30 -07007519 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007520 return -EINVAL;
7521
7522 f = fdget(fd);
7523 if (!f.file)
7524 return -EBADF;
7525
7526 ret = -EOPNOTSUPP;
7527 if (f.file->f_op != &io_uring_fops)
7528 goto out_fput;
7529
7530 ret = -ENXIO;
7531 ctx = f.file->private_data;
7532 if (!percpu_ref_tryget(&ctx->refs))
7533 goto out_fput;
7534
Jens Axboe6c271ce2019-01-10 11:22:30 -07007535 /*
7536 * For SQ polling, the thread will do all submissions and completions.
7537 * Just return the requested submit count, and wake the thread if
7538 * we were asked to.
7539 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007540 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007541 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07007542 if (!list_empty_careful(&ctx->cq_overflow_list))
7543 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007544 if (flags & IORING_ENTER_SQ_WAKEUP)
7545 wake_up(&ctx->sqo_wait);
7546 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007547 } else if (to_submit) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07007548 mutex_lock(&ctx->uring_lock);
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03007549 submitted = io_submit_sqes(ctx, to_submit, f.file, fd, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007550 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007551
7552 if (submitted != to_submit)
7553 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007554 }
7555 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07007556 unsigned nr_events = 0;
7557
Jens Axboe2b188cc2019-01-07 10:46:33 -07007558 min_complete = min(min_complete, ctx->cq_entries);
7559
Xiaoguang Wang32b22442020-03-11 09:26:09 +08007560 /*
7561 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
7562 * space applications don't need to do io completion events
7563 * polling again, they can rely on io_sq_thread to do polling
7564 * work, which can reduce cpu usage and uring_lock contention.
7565 */
7566 if (ctx->flags & IORING_SETUP_IOPOLL &&
7567 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Jens Axboedef596e2019-01-09 08:59:42 -07007568 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07007569 } else {
7570 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
7571 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007572 }
7573
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007574out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03007575 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007576out_fput:
7577 fdput(f);
7578 return submitted ? submitted : ret;
7579}
7580
Tobias Klauserbebdb652020-02-26 18:38:32 +01007581#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07007582static int io_uring_show_cred(int id, void *p, void *data)
7583{
7584 const struct cred *cred = p;
7585 struct seq_file *m = data;
7586 struct user_namespace *uns = seq_user_ns(m);
7587 struct group_info *gi;
7588 kernel_cap_t cap;
7589 unsigned __capi;
7590 int g;
7591
7592 seq_printf(m, "%5d\n", id);
7593 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
7594 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
7595 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
7596 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
7597 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
7598 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
7599 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
7600 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
7601 seq_puts(m, "\n\tGroups:\t");
7602 gi = cred->group_info;
7603 for (g = 0; g < gi->ngroups; g++) {
7604 seq_put_decimal_ull(m, g ? " " : "",
7605 from_kgid_munged(uns, gi->gid[g]));
7606 }
7607 seq_puts(m, "\n\tCapEff:\t");
7608 cap = cred->cap_effective;
7609 CAP_FOR_EACH_U32(__capi)
7610 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
7611 seq_putc(m, '\n');
7612 return 0;
7613}
7614
7615static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
7616{
7617 int i;
7618
7619 mutex_lock(&ctx->uring_lock);
7620 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
7621 for (i = 0; i < ctx->nr_user_files; i++) {
7622 struct fixed_file_table *table;
7623 struct file *f;
7624
7625 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7626 f = table->files[i & IORING_FILE_TABLE_MASK];
7627 if (f)
7628 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
7629 else
7630 seq_printf(m, "%5u: <none>\n", i);
7631 }
7632 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
7633 for (i = 0; i < ctx->nr_user_bufs; i++) {
7634 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
7635
7636 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
7637 (unsigned int) buf->len);
7638 }
7639 if (!idr_is_empty(&ctx->personality_idr)) {
7640 seq_printf(m, "Personalities:\n");
7641 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
7642 }
Jens Axboed7718a92020-02-14 22:23:12 -07007643 seq_printf(m, "PollList:\n");
7644 spin_lock_irq(&ctx->completion_lock);
7645 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
7646 struct hlist_head *list = &ctx->cancel_hash[i];
7647 struct io_kiocb *req;
7648
7649 hlist_for_each_entry(req, list, hash_node)
7650 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
7651 req->task->task_works != NULL);
7652 }
7653 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07007654 mutex_unlock(&ctx->uring_lock);
7655}
7656
7657static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
7658{
7659 struct io_ring_ctx *ctx = f->private_data;
7660
7661 if (percpu_ref_tryget(&ctx->refs)) {
7662 __io_uring_show_fdinfo(ctx, m);
7663 percpu_ref_put(&ctx->refs);
7664 }
7665}
Tobias Klauserbebdb652020-02-26 18:38:32 +01007666#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07007667
Jens Axboe2b188cc2019-01-07 10:46:33 -07007668static const struct file_operations io_uring_fops = {
7669 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06007670 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07007671 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007672#ifndef CONFIG_MMU
7673 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
7674 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
7675#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07007676 .poll = io_uring_poll,
7677 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01007678#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07007679 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01007680#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07007681};
7682
7683static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
7684 struct io_uring_params *p)
7685{
Hristo Venev75b28af2019-08-26 17:23:46 +00007686 struct io_rings *rings;
7687 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007688
Hristo Venev75b28af2019-08-26 17:23:46 +00007689 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
7690 if (size == SIZE_MAX)
7691 return -EOVERFLOW;
7692
7693 rings = io_mem_alloc(size);
7694 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007695 return -ENOMEM;
7696
Hristo Venev75b28af2019-08-26 17:23:46 +00007697 ctx->rings = rings;
7698 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
7699 rings->sq_ring_mask = p->sq_entries - 1;
7700 rings->cq_ring_mask = p->cq_entries - 1;
7701 rings->sq_ring_entries = p->sq_entries;
7702 rings->cq_ring_entries = p->cq_entries;
7703 ctx->sq_mask = rings->sq_ring_mask;
7704 ctx->cq_mask = rings->cq_ring_mask;
7705 ctx->sq_entries = rings->sq_ring_entries;
7706 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007707
7708 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07007709 if (size == SIZE_MAX) {
7710 io_mem_free(ctx->rings);
7711 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007712 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07007713 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007714
7715 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07007716 if (!ctx->sq_sqes) {
7717 io_mem_free(ctx->rings);
7718 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007719 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07007720 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007721
Jens Axboe2b188cc2019-01-07 10:46:33 -07007722 return 0;
7723}
7724
7725/*
7726 * Allocate an anonymous fd, this is what constitutes the application
7727 * visible backing of an io_uring instance. The application mmaps this
7728 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
7729 * we have to tie this fd to a socket for file garbage collection purposes.
7730 */
7731static int io_uring_get_fd(struct io_ring_ctx *ctx)
7732{
7733 struct file *file;
7734 int ret;
7735
7736#if defined(CONFIG_UNIX)
7737 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
7738 &ctx->ring_sock);
7739 if (ret)
7740 return ret;
7741#endif
7742
7743 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
7744 if (ret < 0)
7745 goto err;
7746
7747 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
7748 O_RDWR | O_CLOEXEC);
7749 if (IS_ERR(file)) {
7750 put_unused_fd(ret);
7751 ret = PTR_ERR(file);
7752 goto err;
7753 }
7754
7755#if defined(CONFIG_UNIX)
7756 ctx->ring_sock->file = file;
7757#endif
7758 fd_install(ret, file);
7759 return ret;
7760err:
7761#if defined(CONFIG_UNIX)
7762 sock_release(ctx->ring_sock);
7763 ctx->ring_sock = NULL;
7764#endif
7765 return ret;
7766}
7767
7768static int io_uring_create(unsigned entries, struct io_uring_params *p)
7769{
7770 struct user_struct *user = NULL;
7771 struct io_ring_ctx *ctx;
7772 bool account_mem;
7773 int ret;
7774
Jens Axboe8110c1a2019-12-28 15:39:54 -07007775 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007776 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07007777 if (entries > IORING_MAX_ENTRIES) {
7778 if (!(p->flags & IORING_SETUP_CLAMP))
7779 return -EINVAL;
7780 entries = IORING_MAX_ENTRIES;
7781 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007782
7783 /*
7784 * Use twice as many entries for the CQ ring. It's possible for the
7785 * application to drive a higher depth than the size of the SQ ring,
7786 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06007787 * some flexibility in overcommitting a bit. If the application has
7788 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
7789 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07007790 */
7791 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06007792 if (p->flags & IORING_SETUP_CQSIZE) {
7793 /*
7794 * If IORING_SETUP_CQSIZE is set, we do the same roundup
7795 * to a power-of-two, if it isn't already. We do NOT impose
7796 * any cq vs sq ring sizing.
7797 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07007798 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06007799 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07007800 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
7801 if (!(p->flags & IORING_SETUP_CLAMP))
7802 return -EINVAL;
7803 p->cq_entries = IORING_MAX_CQ_ENTRIES;
7804 }
Jens Axboe33a107f2019-10-04 12:10:03 -06007805 p->cq_entries = roundup_pow_of_two(p->cq_entries);
7806 } else {
7807 p->cq_entries = 2 * p->sq_entries;
7808 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007809
7810 user = get_uid(current_user());
7811 account_mem = !capable(CAP_IPC_LOCK);
7812
7813 if (account_mem) {
7814 ret = io_account_mem(user,
7815 ring_pages(p->sq_entries, p->cq_entries));
7816 if (ret) {
7817 free_uid(user);
7818 return ret;
7819 }
7820 }
7821
7822 ctx = io_ring_ctx_alloc(p);
7823 if (!ctx) {
7824 if (account_mem)
7825 io_unaccount_mem(user, ring_pages(p->sq_entries,
7826 p->cq_entries));
7827 free_uid(user);
7828 return -ENOMEM;
7829 }
7830 ctx->compat = in_compat_syscall();
7831 ctx->account_mem = account_mem;
7832 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07007833 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07007834
7835 ret = io_allocate_scq_urings(ctx, p);
7836 if (ret)
7837 goto err;
7838
Jens Axboe6c271ce2019-01-10 11:22:30 -07007839 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007840 if (ret)
7841 goto err;
7842
Jens Axboe2b188cc2019-01-07 10:46:33 -07007843 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007844 p->sq_off.head = offsetof(struct io_rings, sq.head);
7845 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
7846 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
7847 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
7848 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
7849 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
7850 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007851
7852 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007853 p->cq_off.head = offsetof(struct io_rings, cq.head);
7854 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
7855 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
7856 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
7857 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
7858 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06007859
Jens Axboe044c1ab2019-10-28 09:15:33 -06007860 /*
7861 * Install ring fd as the very last thing, so we don't risk someone
7862 * having closed it before we finish setup
7863 */
7864 ret = io_uring_get_fd(ctx);
7865 if (ret < 0)
7866 goto err;
7867
Jens Axboeda8c9692019-12-02 18:51:26 -07007868 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboecccf0ee2020-01-27 16:34:48 -07007869 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jens Axboed7718a92020-02-14 22:23:12 -07007870 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007871 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007872 return ret;
7873err:
7874 io_ring_ctx_wait_and_kill(ctx);
7875 return ret;
7876}
7877
7878/*
7879 * Sets up an aio uring context, and returns the fd. Applications asks for a
7880 * ring size, we return the actual sq/cq ring sizes (among other things) in the
7881 * params structure passed in.
7882 */
7883static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
7884{
7885 struct io_uring_params p;
7886 long ret;
7887 int i;
7888
7889 if (copy_from_user(&p, params, sizeof(p)))
7890 return -EFAULT;
7891 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
7892 if (p.resv[i])
7893 return -EINVAL;
7894 }
7895
Jens Axboe6c271ce2019-01-10 11:22:30 -07007896 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07007897 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03007898 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007899 return -EINVAL;
7900
7901 ret = io_uring_create(entries, &p);
7902 if (ret < 0)
7903 return ret;
7904
7905 if (copy_to_user(params, &p, sizeof(p)))
7906 return -EFAULT;
7907
7908 return ret;
7909}
7910
7911SYSCALL_DEFINE2(io_uring_setup, u32, entries,
7912 struct io_uring_params __user *, params)
7913{
7914 return io_uring_setup(entries, params);
7915}
7916
Jens Axboe66f4af92020-01-16 15:36:52 -07007917static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
7918{
7919 struct io_uring_probe *p;
7920 size_t size;
7921 int i, ret;
7922
7923 size = struct_size(p, ops, nr_args);
7924 if (size == SIZE_MAX)
7925 return -EOVERFLOW;
7926 p = kzalloc(size, GFP_KERNEL);
7927 if (!p)
7928 return -ENOMEM;
7929
7930 ret = -EFAULT;
7931 if (copy_from_user(p, arg, size))
7932 goto out;
7933 ret = -EINVAL;
7934 if (memchr_inv(p, 0, size))
7935 goto out;
7936
7937 p->last_op = IORING_OP_LAST - 1;
7938 if (nr_args > IORING_OP_LAST)
7939 nr_args = IORING_OP_LAST;
7940
7941 for (i = 0; i < nr_args; i++) {
7942 p->ops[i].op = i;
7943 if (!io_op_defs[i].not_supported)
7944 p->ops[i].flags = IO_URING_OP_SUPPORTED;
7945 }
7946 p->ops_len = i;
7947
7948 ret = 0;
7949 if (copy_to_user(arg, p, size))
7950 ret = -EFAULT;
7951out:
7952 kfree(p);
7953 return ret;
7954}
7955
Jens Axboe071698e2020-01-28 10:04:42 -07007956static int io_register_personality(struct io_ring_ctx *ctx)
7957{
7958 const struct cred *creds = get_current_cred();
7959 int id;
7960
7961 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
7962 USHRT_MAX, GFP_KERNEL);
7963 if (id < 0)
7964 put_cred(creds);
7965 return id;
7966}
7967
7968static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
7969{
7970 const struct cred *old_creds;
7971
7972 old_creds = idr_remove(&ctx->personality_idr, id);
7973 if (old_creds) {
7974 put_cred(old_creds);
7975 return 0;
7976 }
7977
7978 return -EINVAL;
7979}
7980
7981static bool io_register_op_must_quiesce(int op)
7982{
7983 switch (op) {
7984 case IORING_UNREGISTER_FILES:
7985 case IORING_REGISTER_FILES_UPDATE:
7986 case IORING_REGISTER_PROBE:
7987 case IORING_REGISTER_PERSONALITY:
7988 case IORING_UNREGISTER_PERSONALITY:
7989 return false;
7990 default:
7991 return true;
7992 }
7993}
7994
Jens Axboeedafcce2019-01-09 09:16:05 -07007995static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
7996 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06007997 __releases(ctx->uring_lock)
7998 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07007999{
8000 int ret;
8001
Jens Axboe35fa71a2019-04-22 10:23:23 -06008002 /*
8003 * We're inside the ring mutex, if the ref is already dying, then
8004 * someone else killed the ctx or is already going through
8005 * io_uring_register().
8006 */
8007 if (percpu_ref_is_dying(&ctx->refs))
8008 return -ENXIO;
8009
Jens Axboe071698e2020-01-28 10:04:42 -07008010 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008011 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06008012
Jens Axboe05f3fb32019-12-09 11:22:50 -07008013 /*
8014 * Drop uring mutex before waiting for references to exit. If
8015 * another thread is currently inside io_uring_enter() it might
8016 * need to grab the uring_lock to make progress. If we hold it
8017 * here across the drain wait, then we can deadlock. It's safe
8018 * to drop the mutex here, since no new references will come in
8019 * after we've killed the percpu ref.
8020 */
8021 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07008022 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008023 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07008024 if (ret) {
8025 percpu_ref_resurrect(&ctx->refs);
8026 ret = -EINTR;
8027 goto out;
8028 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008029 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008030
8031 switch (opcode) {
8032 case IORING_REGISTER_BUFFERS:
8033 ret = io_sqe_buffer_register(ctx, arg, nr_args);
8034 break;
8035 case IORING_UNREGISTER_BUFFERS:
8036 ret = -EINVAL;
8037 if (arg || nr_args)
8038 break;
8039 ret = io_sqe_buffer_unregister(ctx);
8040 break;
Jens Axboe6b063142019-01-10 22:13:58 -07008041 case IORING_REGISTER_FILES:
8042 ret = io_sqe_files_register(ctx, arg, nr_args);
8043 break;
8044 case IORING_UNREGISTER_FILES:
8045 ret = -EINVAL;
8046 if (arg || nr_args)
8047 break;
8048 ret = io_sqe_files_unregister(ctx);
8049 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06008050 case IORING_REGISTER_FILES_UPDATE:
8051 ret = io_sqe_files_update(ctx, arg, nr_args);
8052 break;
Jens Axboe9b402842019-04-11 11:45:41 -06008053 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07008054 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06008055 ret = -EINVAL;
8056 if (nr_args != 1)
8057 break;
8058 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07008059 if (ret)
8060 break;
8061 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
8062 ctx->eventfd_async = 1;
8063 else
8064 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06008065 break;
8066 case IORING_UNREGISTER_EVENTFD:
8067 ret = -EINVAL;
8068 if (arg || nr_args)
8069 break;
8070 ret = io_eventfd_unregister(ctx);
8071 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07008072 case IORING_REGISTER_PROBE:
8073 ret = -EINVAL;
8074 if (!arg || nr_args > 256)
8075 break;
8076 ret = io_probe(ctx, arg, nr_args);
8077 break;
Jens Axboe071698e2020-01-28 10:04:42 -07008078 case IORING_REGISTER_PERSONALITY:
8079 ret = -EINVAL;
8080 if (arg || nr_args)
8081 break;
8082 ret = io_register_personality(ctx);
8083 break;
8084 case IORING_UNREGISTER_PERSONALITY:
8085 ret = -EINVAL;
8086 if (arg)
8087 break;
8088 ret = io_unregister_personality(ctx, nr_args);
8089 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008090 default:
8091 ret = -EINVAL;
8092 break;
8093 }
8094
Jens Axboe071698e2020-01-28 10:04:42 -07008095 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008096 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07008097 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07008098out:
8099 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008100 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008101 return ret;
8102}
8103
8104SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
8105 void __user *, arg, unsigned int, nr_args)
8106{
8107 struct io_ring_ctx *ctx;
8108 long ret = -EBADF;
8109 struct fd f;
8110
8111 f = fdget(fd);
8112 if (!f.file)
8113 return -EBADF;
8114
8115 ret = -EOPNOTSUPP;
8116 if (f.file->f_op != &io_uring_fops)
8117 goto out_fput;
8118
8119 ctx = f.file->private_data;
8120
8121 mutex_lock(&ctx->uring_lock);
8122 ret = __io_uring_register(ctx, opcode, arg, nr_args);
8123 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02008124 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
8125 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07008126out_fput:
8127 fdput(f);
8128 return ret;
8129}
8130
Jens Axboe2b188cc2019-01-07 10:46:33 -07008131static int __init io_uring_init(void)
8132{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008133#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
8134 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
8135 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
8136} while (0)
8137
8138#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
8139 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
8140 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
8141 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
8142 BUILD_BUG_SQE_ELEM(1, __u8, flags);
8143 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
8144 BUILD_BUG_SQE_ELEM(4, __s32, fd);
8145 BUILD_BUG_SQE_ELEM(8, __u64, off);
8146 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
8147 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008148 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008149 BUILD_BUG_SQE_ELEM(24, __u32, len);
8150 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
8151 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
8152 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
8153 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
8154 BUILD_BUG_SQE_ELEM(28, __u16, poll_events);
8155 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
8156 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
8157 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
8158 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
8159 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
8160 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
8161 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
8162 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008163 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008164 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
8165 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
8166 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008167 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008168
Jens Axboed3656342019-12-18 09:50:26 -07008169 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07008170 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008171 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
8172 return 0;
8173};
8174__initcall(io_uring_init);