blob: 414e940323d45d4defbb987a80c5d852cebde62a [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 Axboe0f158b42020-05-14 17:18:39 -0600282 struct completion ref_comp;
283 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700284
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;
Jens Axboed3656342019-12-18 09:50:26 -0700683 /* hash wq insertion if file is a regular file */
684 unsigned hash_reg_file : 1;
685 /* unbound wq insertion if file is a non-regular file */
686 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700687 /* opcode is not supported by this kernel */
688 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700689 /* needs file table */
690 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700691 /* needs ->fs */
692 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700693 /* set if opcode supports polled "wait" */
694 unsigned pollin : 1;
695 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700696 /* op supports buffer selection */
697 unsigned buffer_select : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700698};
699
700static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300701 [IORING_OP_NOP] = {},
702 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700703 .async_ctx = 1,
704 .needs_mm = 1,
705 .needs_file = 1,
706 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700707 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700708 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700709 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300710 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700711 .async_ctx = 1,
712 .needs_mm = 1,
713 .needs_file = 1,
714 .hash_reg_file = 1,
715 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700716 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700717 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300718 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700719 .needs_file = 1,
720 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300721 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700722 .needs_file = 1,
723 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700724 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700725 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300726 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700727 .needs_file = 1,
728 .hash_reg_file = 1,
729 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700730 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700731 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300732 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700733 .needs_file = 1,
734 .unbound_nonreg_file = 1,
735 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300736 [IORING_OP_POLL_REMOVE] = {},
737 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700738 .needs_file = 1,
739 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300740 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700741 .async_ctx = 1,
742 .needs_mm = 1,
743 .needs_file = 1,
744 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700745 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700746 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700747 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300748 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700749 .async_ctx = 1,
750 .needs_mm = 1,
751 .needs_file = 1,
752 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700753 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700754 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700755 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700756 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300757 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700758 .async_ctx = 1,
759 .needs_mm = 1,
760 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300761 [IORING_OP_TIMEOUT_REMOVE] = {},
762 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700763 .needs_mm = 1,
764 .needs_file = 1,
765 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700766 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700767 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700768 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300769 [IORING_OP_ASYNC_CANCEL] = {},
770 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700771 .async_ctx = 1,
772 .needs_mm = 1,
773 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300774 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700775 .async_ctx = 1,
776 .needs_mm = 1,
777 .needs_file = 1,
778 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700779 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700780 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300781 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700782 .needs_file = 1,
783 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300784 [IORING_OP_OPENAT] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700785 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700786 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700787 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300788 [IORING_OP_CLOSE] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700789 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700790 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300791 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700792 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700793 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700794 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300795 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700796 .needs_mm = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700797 .needs_fs = 1,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600798 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700799 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300800 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700801 .needs_mm = 1,
802 .needs_file = 1,
803 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700804 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700805 .buffer_select = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700806 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300807 [IORING_OP_WRITE] = {
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 .pollout = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700812 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300813 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700814 .needs_file = 1,
815 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300816 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700817 .needs_mm = 1,
818 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300819 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700820 .needs_mm = 1,
821 .needs_file = 1,
822 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700823 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700824 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300825 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700826 .needs_mm = 1,
827 .needs_file = 1,
828 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700829 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700830 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700831 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300832 [IORING_OP_OPENAT2] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700833 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700834 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700835 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700836 [IORING_OP_EPOLL_CTL] = {
837 .unbound_nonreg_file = 1,
838 .file_table = 1,
839 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300840 [IORING_OP_SPLICE] = {
841 .needs_file = 1,
842 .hash_reg_file = 1,
843 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700844 },
845 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700846 [IORING_OP_REMOVE_BUFFERS] = {},
Jens Axboed3656342019-12-18 09:50:26 -0700847};
848
Jens Axboe561fb042019-10-24 07:25:42 -0600849static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700850static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800851static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700852static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700853static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
854static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700855static int __io_sqe_files_update(struct io_ring_ctx *ctx,
856 struct io_uring_files_update *ip,
857 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700858static int io_grab_files(struct io_kiocb *req);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300859static void io_cleanup_req(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700860static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
861 int fd, struct file **out_file, bool fixed);
862static void __io_queue_sqe(struct io_kiocb *req,
863 const struct io_uring_sqe *sqe);
Jens Axboede0617e2019-04-06 21:51:27 -0600864
Jens Axboe2b188cc2019-01-07 10:46:33 -0700865static struct kmem_cache *req_cachep;
866
867static const struct file_operations io_uring_fops;
868
869struct sock *io_uring_get_socket(struct file *file)
870{
871#if defined(CONFIG_UNIX)
872 if (file->f_op == &io_uring_fops) {
873 struct io_ring_ctx *ctx = file->private_data;
874
875 return ctx->ring_sock->sk;
876 }
877#endif
878 return NULL;
879}
880EXPORT_SYMBOL(io_uring_get_socket);
881
882static void io_ring_ctx_ref_free(struct percpu_ref *ref)
883{
884 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
885
Jens Axboe0f158b42020-05-14 17:18:39 -0600886 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700887}
888
889static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
890{
891 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700892 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700893
894 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
895 if (!ctx)
896 return NULL;
897
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700898 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
899 if (!ctx->fallback_req)
900 goto err;
901
Jens Axboe78076bb2019-12-04 19:56:40 -0700902 /*
903 * Use 5 bits less than the max cq entries, that should give us around
904 * 32 entries per hash list if totally full and uniformly spread.
905 */
906 hash_bits = ilog2(p->cq_entries);
907 hash_bits -= 5;
908 if (hash_bits <= 0)
909 hash_bits = 1;
910 ctx->cancel_hash_bits = hash_bits;
911 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
912 GFP_KERNEL);
913 if (!ctx->cancel_hash)
914 goto err;
915 __hash_init(ctx->cancel_hash, 1U << hash_bits);
916
Roman Gushchin21482892019-05-07 10:01:48 -0700917 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700918 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
919 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700920
921 ctx->flags = p->flags;
922 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700923 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -0600924 init_completion(&ctx->ref_comp);
925 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -0700926 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -0700927 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700928 mutex_init(&ctx->uring_lock);
929 init_waitqueue_head(&ctx->wait);
930 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700931 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600932 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600933 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600934 init_waitqueue_head(&ctx->inflight_wait);
935 spin_lock_init(&ctx->inflight_lock);
936 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700937 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700938err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700939 if (ctx->fallback_req)
940 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -0700941 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700942 kfree(ctx);
943 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700944}
945
Bob Liu9d858b22019-11-13 18:06:25 +0800946static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600947{
Jackie Liua197f662019-11-08 08:09:12 -0700948 struct io_ring_ctx *ctx = req->ctx;
949
Pavel Begunkov31af27c2020-04-15 00:39:50 +0300950 return req->sequence != ctx->cached_cq_tail
951 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600952}
953
Bob Liu9d858b22019-11-13 18:06:25 +0800954static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600955{
Pavel Begunkov87987892020-01-18 01:22:30 +0300956 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +0800957 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600958
Bob Liu9d858b22019-11-13 18:06:25 +0800959 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600960}
961
962static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600963{
964 struct io_kiocb *req;
965
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600966 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800967 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600968 list_del_init(&req->list);
969 return req;
970 }
971
972 return NULL;
973}
974
Jens Axboe5262f562019-09-17 12:26:57 -0600975static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
976{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600977 struct io_kiocb *req;
978
979 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700980 if (req) {
981 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
982 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800983 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700984 list_del_init(&req->list);
985 return req;
986 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600987 }
988
989 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600990}
991
Jens Axboede0617e2019-04-06 21:51:27 -0600992static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700993{
Hristo Venev75b28af2019-08-26 17:23:46 +0000994 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700995
Pavel Begunkov07910152020-01-17 03:52:46 +0300996 /* order cqe stores with ring update */
997 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700998
Pavel Begunkov07910152020-01-17 03:52:46 +0300999 if (wq_has_sleeper(&ctx->cq_wait)) {
1000 wake_up_interruptible(&ctx->cq_wait);
1001 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001002 }
1003}
1004
Jens Axboecccf0ee2020-01-27 16:34:48 -07001005static inline void io_req_work_grab_env(struct io_kiocb *req,
1006 const struct io_op_def *def)
Jens Axboe18d9be12019-09-10 09:13:05 -06001007{
Jens Axboecccf0ee2020-01-27 16:34:48 -07001008 if (!req->work.mm && def->needs_mm) {
1009 mmgrab(current->mm);
1010 req->work.mm = current->mm;
1011 }
1012 if (!req->work.creds)
1013 req->work.creds = get_current_cred();
Jens Axboeff002b32020-02-07 16:05:21 -07001014 if (!req->work.fs && def->needs_fs) {
1015 spin_lock(&current->fs->lock);
1016 if (!current->fs->in_exec) {
1017 req->work.fs = current->fs;
1018 req->work.fs->users++;
1019 } else {
1020 req->work.flags |= IO_WQ_WORK_CANCEL;
1021 }
1022 spin_unlock(&current->fs->lock);
1023 }
Jens Axboe6ab23142020-02-08 20:23:59 -07001024 if (!req->work.task_pid)
1025 req->work.task_pid = task_pid_vnr(current);
Jens Axboecccf0ee2020-01-27 16:34:48 -07001026}
1027
1028static inline void io_req_work_drop_env(struct io_kiocb *req)
1029{
1030 if (req->work.mm) {
1031 mmdrop(req->work.mm);
1032 req->work.mm = NULL;
1033 }
1034 if (req->work.creds) {
1035 put_cred(req->work.creds);
1036 req->work.creds = NULL;
1037 }
Jens Axboeff002b32020-02-07 16:05:21 -07001038 if (req->work.fs) {
1039 struct fs_struct *fs = req->work.fs;
1040
1041 spin_lock(&req->work.fs->lock);
1042 if (--fs->users)
1043 fs = NULL;
1044 spin_unlock(&req->work.fs->lock);
1045 if (fs)
1046 free_fs_struct(fs);
1047 }
Jens Axboe561fb042019-10-24 07:25:42 -06001048}
1049
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001050static inline void io_prep_async_work(struct io_kiocb *req,
Jens Axboe94ae5e72019-11-14 19:39:52 -07001051 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -06001052{
Jens Axboed3656342019-12-18 09:50:26 -07001053 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe54a91f32019-09-10 09:15:04 -06001054
Jens Axboed3656342019-12-18 09:50:26 -07001055 if (req->flags & REQ_F_ISREG) {
1056 if (def->hash_reg_file)
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001057 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001058 } else {
1059 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001060 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001061 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001062
1063 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -06001064
Jens Axboe94ae5e72019-11-14 19:39:52 -07001065 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001066}
1067
Jackie Liua197f662019-11-08 08:09:12 -07001068static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001069{
Jackie Liua197f662019-11-08 08:09:12 -07001070 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001071 struct io_kiocb *link;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001072
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001073 io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06001074
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001075 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1076 &req->work, req->flags);
1077 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001078
1079 if (link)
1080 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -06001081}
1082
Jens Axboe5262f562019-09-17 12:26:57 -06001083static void io_kill_timeout(struct io_kiocb *req)
1084{
1085 int ret;
1086
Jens Axboe2d283902019-12-04 11:08:05 -07001087 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001088 if (ret != -1) {
1089 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -06001090 list_del_init(&req->list);
Pavel Begunkovf0e20b82020-03-07 01:15:22 +03001091 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001092 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001093 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001094 }
1095}
1096
1097static void io_kill_timeouts(struct io_ring_ctx *ctx)
1098{
1099 struct io_kiocb *req, *tmp;
1100
1101 spin_lock_irq(&ctx->completion_lock);
1102 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
1103 io_kill_timeout(req);
1104 spin_unlock_irq(&ctx->completion_lock);
1105}
1106
Jens Axboede0617e2019-04-06 21:51:27 -06001107static void io_commit_cqring(struct io_ring_ctx *ctx)
1108{
1109 struct io_kiocb *req;
1110
Jens Axboe5262f562019-09-17 12:26:57 -06001111 while ((req = io_get_timeout_req(ctx)) != NULL)
1112 io_kill_timeout(req);
1113
Jens Axboede0617e2019-04-06 21:51:27 -06001114 __io_commit_cqring(ctx);
1115
Pavel Begunkov87987892020-01-18 01:22:30 +03001116 while ((req = io_get_deferred_req(ctx)) != NULL)
Jackie Liua197f662019-11-08 08:09:12 -07001117 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -06001118}
1119
Jens Axboe2b188cc2019-01-07 10:46:33 -07001120static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1121{
Hristo Venev75b28af2019-08-26 17:23:46 +00001122 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001123 unsigned tail;
1124
1125 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001126 /*
1127 * writes to the cq entry need to come after reading head; the
1128 * control dependency is enough as we're using WRITE_ONCE to
1129 * fill the cq entry
1130 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001131 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001132 return NULL;
1133
1134 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001135 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001136}
1137
Jens Axboef2842ab2020-01-08 11:04:00 -07001138static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1139{
Jens Axboef0b493e2020-02-01 21:30:11 -07001140 if (!ctx->cq_ev_fd)
1141 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001142 if (!ctx->eventfd_async)
1143 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001144 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001145}
1146
Jens Axboeb41e9852020-02-17 09:52:41 -07001147static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001148{
1149 if (waitqueue_active(&ctx->wait))
1150 wake_up(&ctx->wait);
1151 if (waitqueue_active(&ctx->sqo_wait))
1152 wake_up(&ctx->sqo_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001153 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001154 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001155}
1156
Jens Axboec4a2ed72019-11-21 21:01:26 -07001157/* Returns true if there are no backlogged entries after the flush */
1158static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001159{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001160 struct io_rings *rings = ctx->rings;
1161 struct io_uring_cqe *cqe;
1162 struct io_kiocb *req;
1163 unsigned long flags;
1164 LIST_HEAD(list);
1165
1166 if (!force) {
1167 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001168 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001169 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1170 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001171 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001172 }
1173
1174 spin_lock_irqsave(&ctx->completion_lock, flags);
1175
1176 /* if force is set, the ring is going away. always drop after that */
1177 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001178 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001179
Jens Axboec4a2ed72019-11-21 21:01:26 -07001180 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001181 while (!list_empty(&ctx->cq_overflow_list)) {
1182 cqe = io_get_cqring(ctx);
1183 if (!cqe && !force)
1184 break;
1185
1186 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1187 list);
1188 list_move(&req->list, &list);
Jens Axboe2ca10252020-02-13 17:17:35 -07001189 req->flags &= ~REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001190 if (cqe) {
1191 WRITE_ONCE(cqe->user_data, req->user_data);
1192 WRITE_ONCE(cqe->res, req->result);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001193 WRITE_ONCE(cqe->flags, req->cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001194 } else {
1195 WRITE_ONCE(ctx->rings->cq_overflow,
1196 atomic_inc_return(&ctx->cached_cq_overflow));
1197 }
1198 }
1199
1200 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001201 if (cqe) {
1202 clear_bit(0, &ctx->sq_check_overflow);
1203 clear_bit(0, &ctx->cq_check_overflow);
1204 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001205 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1206 io_cqring_ev_posted(ctx);
1207
1208 while (!list_empty(&list)) {
1209 req = list_first_entry(&list, struct io_kiocb, list);
1210 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001211 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001212 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001213
1214 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001215}
1216
Jens Axboebcda7ba2020-02-23 16:42:51 -07001217static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001218{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001219 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001220 struct io_uring_cqe *cqe;
1221
Jens Axboe78e19bb2019-11-06 15:21:34 -07001222 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001223
Jens Axboe2b188cc2019-01-07 10:46:33 -07001224 /*
1225 * If we can't get a cq entry, userspace overflowed the
1226 * submission (by quite a lot). Increment the overflow count in
1227 * the ring.
1228 */
1229 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001230 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001231 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001232 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001233 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001234 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001235 WRITE_ONCE(ctx->rings->cq_overflow,
1236 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001237 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001238 if (list_empty(&ctx->cq_overflow_list)) {
1239 set_bit(0, &ctx->sq_check_overflow);
1240 set_bit(0, &ctx->cq_check_overflow);
1241 }
Jens Axboe2ca10252020-02-13 17:17:35 -07001242 req->flags |= REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001243 refcount_inc(&req->refs);
1244 req->result = res;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001245 req->cflags = cflags;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001246 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001247 }
1248}
1249
Jens Axboebcda7ba2020-02-23 16:42:51 -07001250static void io_cqring_fill_event(struct io_kiocb *req, long res)
1251{
1252 __io_cqring_fill_event(req, res, 0);
1253}
1254
1255static void __io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001256{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001257 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001258 unsigned long flags;
1259
1260 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001261 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001262 io_commit_cqring(ctx);
1263 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1264
Jens Axboe8c838782019-03-12 15:48:16 -06001265 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001266}
1267
Jens Axboebcda7ba2020-02-23 16:42:51 -07001268static void io_cqring_add_event(struct io_kiocb *req, long res)
1269{
1270 __io_cqring_add_event(req, res, 0);
1271}
1272
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001273static inline bool io_is_fallback_req(struct io_kiocb *req)
1274{
1275 return req == (struct io_kiocb *)
1276 ((unsigned long) req->ctx->fallback_req & ~1UL);
1277}
1278
1279static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1280{
1281 struct io_kiocb *req;
1282
1283 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001284 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001285 return req;
1286
1287 return NULL;
1288}
1289
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001290static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1291 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001292{
Jens Axboefd6fab22019-03-14 16:30:06 -06001293 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001294 struct io_kiocb *req;
1295
Jens Axboe2579f912019-01-09 09:10:43 -07001296 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001297 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001298 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001299 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001300 } else if (!state->free_reqs) {
1301 size_t sz;
1302 int ret;
1303
1304 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001305 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1306
1307 /*
1308 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1309 * retry single alloc to be on the safe side.
1310 */
1311 if (unlikely(ret <= 0)) {
1312 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1313 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001314 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001315 ret = 1;
1316 }
Jens Axboe2579f912019-01-09 09:10:43 -07001317 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001318 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001319 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001320 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001321 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001322 }
1323
Jens Axboe2579f912019-01-09 09:10:43 -07001324 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001325fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001326 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001327}
1328
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001329static inline void io_put_file(struct io_kiocb *req, struct file *file,
1330 bool fixed)
1331{
1332 if (fixed)
Xiaoguang Wang05589552020-03-31 14:05:18 +08001333 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001334 else
1335 fput(file);
1336}
1337
Jens Axboec6ca97b302019-12-28 12:11:08 -07001338static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001339{
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001340 if (req->flags & REQ_F_NEED_CLEANUP)
1341 io_cleanup_req(req);
1342
YueHaibing96fd84d2020-01-07 22:22:44 +08001343 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001344 if (req->file)
1345 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboe3537b6a2020-04-03 11:19:06 -06001346 if (req->task)
1347 put_task_struct(req->task);
Jens Axboecccf0ee2020-01-27 16:34:48 -07001348
1349 io_req_work_drop_env(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001350}
1351
1352static void __io_free_req(struct io_kiocb *req)
1353{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001354 __io_req_aux_free(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06001355
Jens Axboefcb323c2019-10-24 12:39:47 -06001356 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001357 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001358 unsigned long flags;
1359
1360 spin_lock_irqsave(&ctx->inflight_lock, flags);
1361 list_del(&req->inflight_entry);
1362 if (waitqueue_active(&ctx->inflight_wait))
1363 wake_up(&ctx->inflight_wait);
1364 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1365 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001366
1367 percpu_ref_put(&req->ctx->refs);
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03001368 if (likely(!io_is_fallback_req(req)))
1369 kmem_cache_free(req_cachep, req);
1370 else
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001371 clear_bit_unlock(0, (unsigned long *) &req->ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -06001372}
1373
Jens Axboec6ca97b302019-12-28 12:11:08 -07001374struct req_batch {
1375 void *reqs[IO_IOPOLL_BATCH];
1376 int to_free;
1377 int need_iter;
1378};
1379
1380static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1381{
1382 if (!rb->to_free)
1383 return;
1384 if (rb->need_iter) {
1385 int i, inflight = 0;
1386 unsigned long flags;
1387
1388 for (i = 0; i < rb->to_free; i++) {
1389 struct io_kiocb *req = rb->reqs[i];
1390
Jens Axboe10fef4b2020-01-09 07:52:28 -07001391 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001392 req->file = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08001393 percpu_ref_put(req->fixed_file_refs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001394 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001395 if (req->flags & REQ_F_INFLIGHT)
1396 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001397 __io_req_aux_free(req);
1398 }
1399 if (!inflight)
1400 goto do_free;
1401
1402 spin_lock_irqsave(&ctx->inflight_lock, flags);
1403 for (i = 0; i < rb->to_free; i++) {
1404 struct io_kiocb *req = rb->reqs[i];
1405
Jens Axboe10fef4b2020-01-09 07:52:28 -07001406 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001407 list_del(&req->inflight_entry);
1408 if (!--inflight)
1409 break;
1410 }
1411 }
1412 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1413
1414 if (waitqueue_active(&ctx->inflight_wait))
1415 wake_up(&ctx->inflight_wait);
1416 }
1417do_free:
1418 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
1419 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001420 rb->to_free = rb->need_iter = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001421}
1422
Jackie Liua197f662019-11-08 08:09:12 -07001423static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001424{
Jackie Liua197f662019-11-08 08:09:12 -07001425 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001426 int ret;
1427
Jens Axboe2d283902019-12-04 11:08:05 -07001428 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001429 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001430 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001431 io_commit_cqring(ctx);
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001432 req->flags &= ~REQ_F_LINK_HEAD;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001433 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001434 return true;
1435 }
1436
1437 return false;
1438}
1439
Jens Axboeba816ad2019-09-28 11:36:45 -06001440static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001441{
Jens Axboe2665abf2019-11-05 12:40:47 -07001442 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001443 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001444
Jens Axboe4d7dd462019-11-20 13:03:52 -07001445 /* Already got next link */
1446 if (req->flags & REQ_F_LINK_NEXT)
1447 return;
1448
Jens Axboe9e645e112019-05-10 16:07:28 -06001449 /*
1450 * The list should never be empty when we are called here. But could
1451 * potentially happen if the chain is messed up, check to be on the
1452 * safe side.
1453 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001454 while (!list_empty(&req->link_list)) {
1455 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1456 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001457
Pavel Begunkov44932332019-12-05 16:16:35 +03001458 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1459 (nxt->flags & REQ_F_TIMEOUT))) {
1460 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001461 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001462 req->flags &= ~REQ_F_LINK_TIMEOUT;
1463 continue;
1464 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001465
Pavel Begunkov44932332019-12-05 16:16:35 +03001466 list_del_init(&req->link_list);
1467 if (!list_empty(&nxt->link_list))
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001468 nxt->flags |= REQ_F_LINK_HEAD;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001469 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001470 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001471 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001472
Jens Axboe4d7dd462019-11-20 13:03:52 -07001473 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001474 if (wake_ev)
1475 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001476}
1477
1478/*
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001479 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
Jens Axboe9e645e112019-05-10 16:07:28 -06001480 */
1481static void io_fail_links(struct io_kiocb *req)
1482{
Jens Axboe2665abf2019-11-05 12:40:47 -07001483 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001484 unsigned long flags;
1485
1486 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001487
1488 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001489 struct io_kiocb *link = list_first_entry(&req->link_list,
1490 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001491
Pavel Begunkov44932332019-12-05 16:16:35 +03001492 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001493 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001494
1495 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001496 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001497 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001498 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001499 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001500 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001501 }
Jens Axboe5d960722019-11-19 15:31:28 -07001502 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001503 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001504
1505 io_commit_cqring(ctx);
1506 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1507 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001508}
1509
Jens Axboe4d7dd462019-11-20 13:03:52 -07001510static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001511{
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001512 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001513 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001514
Jens Axboe9e645e112019-05-10 16:07:28 -06001515 /*
1516 * If LINK is set, we have dependent requests in this chain. If we
1517 * didn't fail this request, queue the first one up, moving any other
1518 * dependencies to the next request. In case of failure, fail the rest
1519 * of the chain.
1520 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001521 if (req->flags & REQ_F_FAIL_LINK) {
1522 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001523 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1524 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001525 struct io_ring_ctx *ctx = req->ctx;
1526 unsigned long flags;
1527
1528 /*
1529 * If this is a timeout link, we could be racing with the
1530 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001531 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001532 */
1533 spin_lock_irqsave(&ctx->completion_lock, flags);
1534 io_req_link_next(req, nxt);
1535 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1536 } else {
1537 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001538 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001539}
Jens Axboe9e645e112019-05-10 16:07:28 -06001540
Jackie Liuc69f8db2019-11-09 11:00:08 +08001541static void io_free_req(struct io_kiocb *req)
1542{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001543 struct io_kiocb *nxt = NULL;
1544
1545 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001546 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001547
1548 if (nxt)
1549 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001550}
1551
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001552static void io_link_work_cb(struct io_wq_work **workptr)
1553{
Pavel Begunkov18a542f2020-03-23 00:23:29 +03001554 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
1555 struct io_kiocb *link;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001556
Pavel Begunkov18a542f2020-03-23 00:23:29 +03001557 link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001558 io_queue_linked_timeout(link);
1559 io_wq_submit_work(workptr);
1560}
1561
1562static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
1563{
1564 struct io_kiocb *link;
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001565 const struct io_op_def *def = &io_op_defs[nxt->opcode];
1566
1567 if ((nxt->flags & REQ_F_ISREG) && def->hash_reg_file)
1568 io_wq_hash_work(&nxt->work, file_inode(nxt->file));
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001569
1570 *workptr = &nxt->work;
1571 link = io_prep_linked_timeout(nxt);
Pavel Begunkov18a542f2020-03-23 00:23:29 +03001572 if (link)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001573 nxt->work.func = io_link_work_cb;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001574}
1575
Jens Axboeba816ad2019-09-28 11:36:45 -06001576/*
1577 * Drop reference to request, return next in chain (if there is one) if this
1578 * was the last reference to this request.
1579 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001580__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001581static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001582{
Jens Axboe2a44f462020-02-25 13:25:41 -07001583 if (refcount_dec_and_test(&req->refs)) {
1584 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001585 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001586 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001587}
1588
Jens Axboe2b188cc2019-01-07 10:46:33 -07001589static void io_put_req(struct io_kiocb *req)
1590{
Jens Axboedef596e2019-01-09 08:59:42 -07001591 if (refcount_dec_and_test(&req->refs))
1592 io_free_req(req);
1593}
1594
Pavel Begunkove9fd9392020-03-04 16:14:12 +03001595static void io_steal_work(struct io_kiocb *req,
1596 struct io_wq_work **workptr)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001597{
1598 /*
1599 * It's in an io-wq worker, so there always should be at least
1600 * one reference, which will be dropped in io_put_work() just
1601 * after the current handler returns.
1602 *
1603 * It also means, that if the counter dropped to 1, then there is
1604 * no asynchronous users left, so it's safe to steal the next work.
1605 */
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001606 if (refcount_read(&req->refs) == 1) {
1607 struct io_kiocb *nxt = NULL;
1608
1609 io_req_find_next(req, &nxt);
1610 if (nxt)
1611 io_wq_assign_next(workptr, nxt);
1612 }
1613}
1614
Jens Axboe978db572019-11-14 22:39:04 -07001615/*
1616 * Must only be used if we don't need to care about links, usually from
1617 * within the completion handling itself.
1618 */
1619static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001620{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001621 /* drop both submit and complete references */
1622 if (refcount_sub_and_test(2, &req->refs))
1623 __io_free_req(req);
1624}
1625
Jens Axboe978db572019-11-14 22:39:04 -07001626static void io_double_put_req(struct io_kiocb *req)
1627{
1628 /* drop both submit and complete references */
1629 if (refcount_sub_and_test(2, &req->refs))
1630 io_free_req(req);
1631}
1632
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001633static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001634{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001635 struct io_rings *rings = ctx->rings;
1636
Jens Axboead3eb2c2019-12-18 17:12:20 -07001637 if (test_bit(0, &ctx->cq_check_overflow)) {
1638 /*
1639 * noflush == true is from the waitqueue handler, just ensure
1640 * we wake up the task, and the next invocation will flush the
1641 * entries. We cannot safely to it from here.
1642 */
1643 if (noflush && !list_empty(&ctx->cq_overflow_list))
1644 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001645
Jens Axboead3eb2c2019-12-18 17:12:20 -07001646 io_cqring_overflow_flush(ctx, false);
1647 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001648
Jens Axboea3a0e432019-08-20 11:03:11 -06001649 /* See comment at the top of this file */
1650 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001651 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001652}
1653
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001654static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1655{
1656 struct io_rings *rings = ctx->rings;
1657
1658 /* make sure SQ entry isn't read before tail */
1659 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1660}
1661
Jens Axboe8237e042019-12-28 10:48:22 -07001662static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001663{
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001664 if ((req->flags & REQ_F_LINK_HEAD) || io_is_fallback_req(req))
Jens Axboec6ca97b302019-12-28 12:11:08 -07001665 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001666
Jens Axboec6ca97b302019-12-28 12:11:08 -07001667 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1668 rb->need_iter++;
1669
1670 rb->reqs[rb->to_free++] = req;
1671 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1672 io_free_req_many(req->ctx, rb);
1673 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001674}
1675
Jens Axboebcda7ba2020-02-23 16:42:51 -07001676static int io_put_kbuf(struct io_kiocb *req)
1677{
Jens Axboe4d954c22020-02-27 07:31:19 -07001678 struct io_buffer *kbuf;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001679 int cflags;
1680
Jens Axboe4d954c22020-02-27 07:31:19 -07001681 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001682 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
1683 cflags |= IORING_CQE_F_BUFFER;
1684 req->rw.addr = 0;
1685 kfree(kbuf);
1686 return cflags;
1687}
1688
Jens Axboedef596e2019-01-09 08:59:42 -07001689/*
1690 * Find and free completed poll iocbs
1691 */
1692static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1693 struct list_head *done)
1694{
Jens Axboe8237e042019-12-28 10:48:22 -07001695 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001696 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001697
Jens Axboec6ca97b302019-12-28 12:11:08 -07001698 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001699 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07001700 int cflags = 0;
1701
Jens Axboedef596e2019-01-09 08:59:42 -07001702 req = list_first_entry(done, struct io_kiocb, list);
1703 list_del(&req->list);
1704
Jens Axboebcda7ba2020-02-23 16:42:51 -07001705 if (req->flags & REQ_F_BUFFER_SELECTED)
1706 cflags = io_put_kbuf(req);
1707
1708 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07001709 (*nr_events)++;
1710
Jens Axboe8237e042019-12-28 10:48:22 -07001711 if (refcount_dec_and_test(&req->refs) &&
1712 !io_req_multi_free(&rb, req))
1713 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001714 }
Jens Axboedef596e2019-01-09 08:59:42 -07001715
Jens Axboe09bb8392019-03-13 12:39:28 -06001716 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08001717 if (ctx->flags & IORING_SETUP_SQPOLL)
1718 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001719 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001720}
1721
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001722static void io_iopoll_queue(struct list_head *again)
1723{
1724 struct io_kiocb *req;
1725
1726 do {
1727 req = list_first_entry(again, struct io_kiocb, list);
1728 list_del(&req->list);
1729 refcount_inc(&req->refs);
1730 io_queue_async_work(req);
1731 } while (!list_empty(again));
1732}
1733
Jens Axboedef596e2019-01-09 08:59:42 -07001734static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1735 long min)
1736{
1737 struct io_kiocb *req, *tmp;
1738 LIST_HEAD(done);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001739 LIST_HEAD(again);
Jens Axboedef596e2019-01-09 08:59:42 -07001740 bool spin;
1741 int ret;
1742
1743 /*
1744 * Only spin for completions if we don't have multiple devices hanging
1745 * off our complete list, and we're under the requested amount.
1746 */
1747 spin = !ctx->poll_multi_file && *nr_events < min;
1748
1749 ret = 0;
1750 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001751 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001752
1753 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001754 * Move completed and retryable entries to our local lists.
1755 * If we find a request that requires polling, break out
1756 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07001757 */
1758 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1759 list_move_tail(&req->list, &done);
1760 continue;
1761 }
1762 if (!list_empty(&done))
1763 break;
1764
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001765 if (req->result == -EAGAIN) {
1766 list_move_tail(&req->list, &again);
1767 continue;
1768 }
1769 if (!list_empty(&again))
1770 break;
1771
Jens Axboedef596e2019-01-09 08:59:42 -07001772 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1773 if (ret < 0)
1774 break;
1775
1776 if (ret && spin)
1777 spin = false;
1778 ret = 0;
1779 }
1780
1781 if (!list_empty(&done))
1782 io_iopoll_complete(ctx, nr_events, &done);
1783
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001784 if (!list_empty(&again))
1785 io_iopoll_queue(&again);
1786
Jens Axboedef596e2019-01-09 08:59:42 -07001787 return ret;
1788}
1789
1790/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001791 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001792 * non-spinning poll check - we'll still enter the driver poll loop, but only
1793 * as a non-spinning completion check.
1794 */
1795static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1796 long min)
1797{
Jens Axboe08f54392019-08-21 22:19:11 -06001798 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001799 int ret;
1800
1801 ret = io_do_iopoll(ctx, nr_events, min);
1802 if (ret < 0)
1803 return ret;
1804 if (!min || *nr_events >= min)
1805 return 0;
1806 }
1807
1808 return 1;
1809}
1810
1811/*
1812 * We can't just wait for polled events to come to us, we have to actively
1813 * find and complete them.
1814 */
1815static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1816{
1817 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1818 return;
1819
1820 mutex_lock(&ctx->uring_lock);
1821 while (!list_empty(&ctx->poll_list)) {
1822 unsigned int nr_events = 0;
1823
1824 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001825
1826 /*
1827 * Ensure we allow local-to-the-cpu processing to take place,
1828 * in this case we need to ensure that we reap all events.
1829 */
1830 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001831 }
1832 mutex_unlock(&ctx->uring_lock);
1833}
1834
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001835static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1836 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001837{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001838 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001839
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001840 /*
1841 * We disallow the app entering submit/complete with polling, but we
1842 * still need to lock the ring to prevent racing with polled issue
1843 * that got punted to a workqueue.
1844 */
1845 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001846 do {
1847 int tmin = 0;
1848
Jens Axboe500f9fb2019-08-19 12:15:59 -06001849 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001850 * Don't enter poll loop if we already have events pending.
1851 * If we do, we can potentially be spinning for commands that
1852 * already triggered a CQE (eg in error).
1853 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001854 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001855 break;
1856
1857 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001858 * If a submit got punted to a workqueue, we can have the
1859 * application entering polling for a command before it gets
1860 * issued. That app will hold the uring_lock for the duration
1861 * of the poll right here, so we need to take a breather every
1862 * now and then to ensure that the issue has a chance to add
1863 * the poll to the issued list. Otherwise we can spin here
1864 * forever, while the workqueue is stuck trying to acquire the
1865 * very same mutex.
1866 */
1867 if (!(++iters & 7)) {
1868 mutex_unlock(&ctx->uring_lock);
1869 mutex_lock(&ctx->uring_lock);
1870 }
1871
Jens Axboedef596e2019-01-09 08:59:42 -07001872 if (*nr_events < min)
1873 tmin = min - *nr_events;
1874
1875 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1876 if (ret <= 0)
1877 break;
1878 ret = 0;
1879 } while (min && !*nr_events && !need_resched());
1880
Jens Axboe500f9fb2019-08-19 12:15:59 -06001881 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001882 return ret;
1883}
1884
Jens Axboe491381ce2019-10-17 09:20:46 -06001885static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001886{
Jens Axboe491381ce2019-10-17 09:20:46 -06001887 /*
1888 * Tell lockdep we inherited freeze protection from submission
1889 * thread.
1890 */
1891 if (req->flags & REQ_F_ISREG) {
1892 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001893
Jens Axboe491381ce2019-10-17 09:20:46 -06001894 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001895 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001896 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001897}
1898
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001899static inline void req_set_fail_links(struct io_kiocb *req)
1900{
1901 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1902 req->flags |= REQ_F_FAIL_LINK;
1903}
1904
Jens Axboeba816ad2019-09-28 11:36:45 -06001905static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001906{
Jens Axboe9adbd452019-12-20 08:45:55 -07001907 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001908 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001909
Jens Axboe491381ce2019-10-17 09:20:46 -06001910 if (kiocb->ki_flags & IOCB_WRITE)
1911 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001912
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001913 if (res != req->result)
1914 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001915 if (req->flags & REQ_F_BUFFER_SELECTED)
1916 cflags = io_put_kbuf(req);
1917 __io_cqring_add_event(req, res, cflags);
Jens Axboeba816ad2019-09-28 11:36:45 -06001918}
1919
1920static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1921{
Jens Axboe9adbd452019-12-20 08:45:55 -07001922 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001923
1924 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001925 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001926}
1927
Jens Axboedef596e2019-01-09 08:59:42 -07001928static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1929{
Jens Axboe9adbd452019-12-20 08:45:55 -07001930 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001931
Jens Axboe491381ce2019-10-17 09:20:46 -06001932 if (kiocb->ki_flags & IOCB_WRITE)
1933 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001934
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001935 if (res != req->result)
1936 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001937 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001938 if (res != -EAGAIN)
1939 req->flags |= REQ_F_IOPOLL_COMPLETED;
1940}
1941
1942/*
1943 * After the iocb has been issued, it's safe to be found on the poll list.
1944 * Adding the kiocb to the list AFTER submission ensures that we don't
1945 * find it from a io_iopoll_getevents() thread before the issuer is done
1946 * accessing the kiocb cookie.
1947 */
1948static void io_iopoll_req_issued(struct io_kiocb *req)
1949{
1950 struct io_ring_ctx *ctx = req->ctx;
1951
1952 /*
1953 * Track whether we have multiple files in our lists. This will impact
1954 * how we do polling eventually, not spinning if we're on potentially
1955 * different devices.
1956 */
1957 if (list_empty(&ctx->poll_list)) {
1958 ctx->poll_multi_file = false;
1959 } else if (!ctx->poll_multi_file) {
1960 struct io_kiocb *list_req;
1961
1962 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1963 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001964 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001965 ctx->poll_multi_file = true;
1966 }
1967
1968 /*
1969 * For fast devices, IO may have already completed. If it has, add
1970 * it to the front so we find it first.
1971 */
1972 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1973 list_add(&req->list, &ctx->poll_list);
1974 else
1975 list_add_tail(&req->list, &ctx->poll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08001976
1977 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
1978 wq_has_sleeper(&ctx->sqo_wait))
1979 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07001980}
1981
Jens Axboe3d6770f2019-04-13 11:50:54 -06001982static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001983{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001984 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001985 int diff = state->has_refs - state->used_refs;
1986
1987 if (diff)
1988 fput_many(state->file, diff);
1989 state->file = NULL;
1990 }
1991}
1992
1993/*
1994 * Get as many references to a file as we have IOs left in this submission,
1995 * assuming most submissions are for one file, or at least that each file
1996 * has more than one submission.
1997 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001998static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07001999{
2000 if (!state)
2001 return fget(fd);
2002
2003 if (state->file) {
2004 if (state->fd == fd) {
2005 state->used_refs++;
2006 state->ios_left--;
2007 return state->file;
2008 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06002009 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002010 }
2011 state->file = fget_many(fd, state->ios_left);
2012 if (!state->file)
2013 return NULL;
2014
2015 state->fd = fd;
2016 state->has_refs = state->ios_left;
2017 state->used_refs = 1;
2018 state->ios_left--;
2019 return state->file;
2020}
2021
Jens Axboe2b188cc2019-01-07 10:46:33 -07002022/*
2023 * If we tracked the file through the SCM inflight mechanism, we could support
2024 * any file. For now, just ensure that anything potentially problematic is done
2025 * inline.
2026 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002027static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002028{
2029 umode_t mode = file_inode(file)->i_mode;
2030
Jens Axboe10d59342019-12-09 20:16:22 -07002031 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002032 return true;
2033 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
2034 return true;
2035
Jens Axboeaf197f52020-04-28 13:15:06 -06002036 if (!(file->f_mode & FMODE_NOWAIT))
2037 return false;
2038
2039 if (rw == READ)
2040 return file->f_op->read_iter != NULL;
2041
2042 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002043}
2044
Jens Axboe3529d8c2019-12-19 18:24:38 -07002045static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2046 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002047{
Jens Axboedef596e2019-01-09 08:59:42 -07002048 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002049 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002050 unsigned ioprio;
2051 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002052
Jens Axboe491381ce2019-10-17 09:20:46 -06002053 if (S_ISREG(file_inode(req->file)->i_mode))
2054 req->flags |= REQ_F_ISREG;
2055
Jens Axboe2b188cc2019-01-07 10:46:33 -07002056 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002057 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2058 req->flags |= REQ_F_CUR_POS;
2059 kiocb->ki_pos = req->file->f_pos;
2060 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002061 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002062 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2063 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2064 if (unlikely(ret))
2065 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002066
2067 ioprio = READ_ONCE(sqe->ioprio);
2068 if (ioprio) {
2069 ret = ioprio_check_cap(ioprio);
2070 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002071 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002072
2073 kiocb->ki_ioprio = ioprio;
2074 } else
2075 kiocb->ki_ioprio = get_current_ioprio();
2076
Stefan Bühler8449eed2019-04-27 20:34:19 +02002077 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06002078 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
2079 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02002080 req->flags |= REQ_F_NOWAIT;
2081
2082 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002083 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02002084
Jens Axboedef596e2019-01-09 08:59:42 -07002085 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002086 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2087 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002088 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002089
Jens Axboedef596e2019-01-09 08:59:42 -07002090 kiocb->ki_flags |= IOCB_HIPRI;
2091 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06002092 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002093 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002094 if (kiocb->ki_flags & IOCB_HIPRI)
2095 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002096 kiocb->ki_complete = io_complete_rw;
2097 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002098
Jens Axboe3529d8c2019-12-19 18:24:38 -07002099 req->rw.addr = READ_ONCE(sqe->addr);
2100 req->rw.len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002101 /* we own ->private, reuse it for the buffer index / buffer ID */
Jens Axboe9adbd452019-12-20 08:45:55 -07002102 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002103 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002104 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002105}
2106
2107static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2108{
2109 switch (ret) {
2110 case -EIOCBQUEUED:
2111 break;
2112 case -ERESTARTSYS:
2113 case -ERESTARTNOINTR:
2114 case -ERESTARTNOHAND:
2115 case -ERESTART_RESTARTBLOCK:
2116 /*
2117 * We can't just restart the syscall, since previously
2118 * submitted sqes may already be in progress. Just fail this
2119 * IO with EINTR.
2120 */
2121 ret = -EINTR;
2122 /* fall through */
2123 default:
2124 kiocb->ki_complete(kiocb, ret, 0);
2125 }
2126}
2127
Pavel Begunkov014db002020-03-03 21:33:12 +03002128static void kiocb_done(struct kiocb *kiocb, ssize_t ret)
Jens Axboeba816ad2019-09-28 11:36:45 -06002129{
Jens Axboeba042912019-12-25 16:33:42 -07002130 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2131
2132 if (req->flags & REQ_F_CUR_POS)
2133 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002134 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Pavel Begunkov014db002020-03-03 21:33:12 +03002135 io_complete_rw(kiocb, ret, 0);
Jens Axboeba816ad2019-09-28 11:36:45 -06002136 else
2137 io_rw_done(kiocb, ret);
2138}
2139
Jens Axboe9adbd452019-12-20 08:45:55 -07002140static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002141 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002142{
Jens Axboe9adbd452019-12-20 08:45:55 -07002143 struct io_ring_ctx *ctx = req->ctx;
2144 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002145 struct io_mapped_ubuf *imu;
2146 unsigned index, buf_index;
2147 size_t offset;
2148 u64 buf_addr;
2149
2150 /* attempt to use fixed buffers without having provided iovecs */
2151 if (unlikely(!ctx->user_bufs))
2152 return -EFAULT;
2153
Jens Axboe9adbd452019-12-20 08:45:55 -07002154 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07002155 if (unlikely(buf_index >= ctx->nr_user_bufs))
2156 return -EFAULT;
2157
2158 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2159 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002160 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002161
2162 /* overflow */
2163 if (buf_addr + len < buf_addr)
2164 return -EFAULT;
2165 /* not inside the mapped region */
2166 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2167 return -EFAULT;
2168
2169 /*
2170 * May not be a start of buffer, set size appropriately
2171 * and advance us to the beginning.
2172 */
2173 offset = buf_addr - imu->ubuf;
2174 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002175
2176 if (offset) {
2177 /*
2178 * Don't use iov_iter_advance() here, as it's really slow for
2179 * using the latter parts of a big fixed buffer - it iterates
2180 * over each segment manually. We can cheat a bit here, because
2181 * we know that:
2182 *
2183 * 1) it's a BVEC iter, we set it up
2184 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2185 * first and last bvec
2186 *
2187 * So just find our index, and adjust the iterator afterwards.
2188 * If the offset is within the first bvec (or the whole first
2189 * bvec, just use iov_iter_advance(). This makes it easier
2190 * since we can just skip the first segment, which may not
2191 * be PAGE_SIZE aligned.
2192 */
2193 const struct bio_vec *bvec = imu->bvec;
2194
2195 if (offset <= bvec->bv_len) {
2196 iov_iter_advance(iter, offset);
2197 } else {
2198 unsigned long seg_skip;
2199
2200 /* skip first vec */
2201 offset -= bvec->bv_len;
2202 seg_skip = 1 + (offset >> PAGE_SHIFT);
2203
2204 iter->bvec = bvec + seg_skip;
2205 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002206 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002207 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002208 }
2209 }
2210
Jens Axboe5e559562019-11-13 16:12:46 -07002211 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002212}
2213
Jens Axboebcda7ba2020-02-23 16:42:51 -07002214static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2215{
2216 if (needs_lock)
2217 mutex_unlock(&ctx->uring_lock);
2218}
2219
2220static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2221{
2222 /*
2223 * "Normal" inline submissions always hold the uring_lock, since we
2224 * grab it from the system call. Same is true for the SQPOLL offload.
2225 * The only exception is when we've detached the request and issue it
2226 * from an async worker thread, grab the lock for that case.
2227 */
2228 if (needs_lock)
2229 mutex_lock(&ctx->uring_lock);
2230}
2231
2232static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2233 int bgid, struct io_buffer *kbuf,
2234 bool needs_lock)
2235{
2236 struct io_buffer *head;
2237
2238 if (req->flags & REQ_F_BUFFER_SELECTED)
2239 return kbuf;
2240
2241 io_ring_submit_lock(req->ctx, needs_lock);
2242
2243 lockdep_assert_held(&req->ctx->uring_lock);
2244
2245 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2246 if (head) {
2247 if (!list_empty(&head->list)) {
2248 kbuf = list_last_entry(&head->list, struct io_buffer,
2249 list);
2250 list_del(&kbuf->list);
2251 } else {
2252 kbuf = head;
2253 idr_remove(&req->ctx->io_buffer_idr, bgid);
2254 }
2255 if (*len > kbuf->len)
2256 *len = kbuf->len;
2257 } else {
2258 kbuf = ERR_PTR(-ENOBUFS);
2259 }
2260
2261 io_ring_submit_unlock(req->ctx, needs_lock);
2262
2263 return kbuf;
2264}
2265
Jens Axboe4d954c22020-02-27 07:31:19 -07002266static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2267 bool needs_lock)
2268{
2269 struct io_buffer *kbuf;
2270 int bgid;
2271
2272 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2273 bgid = (int) (unsigned long) req->rw.kiocb.private;
2274 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2275 if (IS_ERR(kbuf))
2276 return kbuf;
2277 req->rw.addr = (u64) (unsigned long) kbuf;
2278 req->flags |= REQ_F_BUFFER_SELECTED;
2279 return u64_to_user_ptr(kbuf->addr);
2280}
2281
2282#ifdef CONFIG_COMPAT
2283static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2284 bool needs_lock)
2285{
2286 struct compat_iovec __user *uiov;
2287 compat_ssize_t clen;
2288 void __user *buf;
2289 ssize_t len;
2290
2291 uiov = u64_to_user_ptr(req->rw.addr);
2292 if (!access_ok(uiov, sizeof(*uiov)))
2293 return -EFAULT;
2294 if (__get_user(clen, &uiov->iov_len))
2295 return -EFAULT;
2296 if (clen < 0)
2297 return -EINVAL;
2298
2299 len = clen;
2300 buf = io_rw_buffer_select(req, &len, needs_lock);
2301 if (IS_ERR(buf))
2302 return PTR_ERR(buf);
2303 iov[0].iov_base = buf;
2304 iov[0].iov_len = (compat_size_t) len;
2305 return 0;
2306}
2307#endif
2308
2309static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2310 bool needs_lock)
2311{
2312 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2313 void __user *buf;
2314 ssize_t len;
2315
2316 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2317 return -EFAULT;
2318
2319 len = iov[0].iov_len;
2320 if (len < 0)
2321 return -EINVAL;
2322 buf = io_rw_buffer_select(req, &len, needs_lock);
2323 if (IS_ERR(buf))
2324 return PTR_ERR(buf);
2325 iov[0].iov_base = buf;
2326 iov[0].iov_len = len;
2327 return 0;
2328}
2329
2330static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2331 bool needs_lock)
2332{
2333 if (req->flags & REQ_F_BUFFER_SELECTED)
2334 return 0;
2335 if (!req->rw.len)
2336 return 0;
2337 else if (req->rw.len > 1)
2338 return -EINVAL;
2339
2340#ifdef CONFIG_COMPAT
2341 if (req->ctx->compat)
2342 return io_compat_import(req, iov, needs_lock);
2343#endif
2344
2345 return __io_iov_buffer_select(req, iov, needs_lock);
2346}
2347
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002348static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
Jens Axboebcda7ba2020-02-23 16:42:51 -07002349 struct iovec **iovec, struct iov_iter *iter,
2350 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002351{
Jens Axboe9adbd452019-12-20 08:45:55 -07002352 void __user *buf = u64_to_user_ptr(req->rw.addr);
2353 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002354 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002355 u8 opcode;
2356
Jens Axboed625c6e2019-12-17 19:53:05 -07002357 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002358 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002359 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002360 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002361 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002362
Jens Axboebcda7ba2020-02-23 16:42:51 -07002363 /* buffer index only valid with fixed read/write, or buffer select */
2364 if (req->rw.kiocb.private && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002365 return -EINVAL;
2366
Jens Axboe3a6820f2019-12-22 15:19:35 -07002367 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002368 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002369 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
2370 if (IS_ERR(buf)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002371 *iovec = NULL;
Jens Axboe4d954c22020-02-27 07:31:19 -07002372 return PTR_ERR(buf);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002373 }
Jens Axboe3f9d6442020-03-11 12:27:04 -06002374 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002375 }
2376
Jens Axboe3a6820f2019-12-22 15:19:35 -07002377 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2378 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002379 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002380 }
2381
Jens Axboef67676d2019-12-02 11:03:47 -07002382 if (req->io) {
2383 struct io_async_rw *iorw = &req->io->rw;
2384
2385 *iovec = iorw->iov;
2386 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2387 if (iorw->iov == iorw->fast_iov)
2388 *iovec = NULL;
2389 return iorw->size;
2390 }
2391
Jens Axboe4d954c22020-02-27 07:31:19 -07002392 if (req->flags & REQ_F_BUFFER_SELECT) {
2393 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002394 if (!ret) {
2395 ret = (*iovec)->iov_len;
2396 iov_iter_init(iter, rw, *iovec, 1, ret);
2397 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002398 *iovec = NULL;
2399 return ret;
2400 }
2401
Jens Axboe2b188cc2019-01-07 10:46:33 -07002402#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002403 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002404 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2405 iovec, iter);
2406#endif
2407
2408 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2409}
2410
Jens Axboe32960612019-09-23 11:05:34 -06002411/*
2412 * For files that don't have ->read_iter() and ->write_iter(), handle them
2413 * by looping over ->read() or ->write() manually.
2414 */
2415static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2416 struct iov_iter *iter)
2417{
2418 ssize_t ret = 0;
2419
2420 /*
2421 * Don't support polled IO through this interface, and we can't
2422 * support non-blocking either. For the latter, this just causes
2423 * the kiocb to be handled from an async context.
2424 */
2425 if (kiocb->ki_flags & IOCB_HIPRI)
2426 return -EOPNOTSUPP;
2427 if (kiocb->ki_flags & IOCB_NOWAIT)
2428 return -EAGAIN;
2429
2430 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002431 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002432 ssize_t nr;
2433
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002434 if (!iov_iter_is_bvec(iter)) {
2435 iovec = iov_iter_iovec(iter);
2436 } else {
2437 /* fixed buffers import bvec */
2438 iovec.iov_base = kmap(iter->bvec->bv_page)
2439 + iter->iov_offset;
2440 iovec.iov_len = min(iter->count,
2441 iter->bvec->bv_len - iter->iov_offset);
2442 }
2443
Jens Axboe32960612019-09-23 11:05:34 -06002444 if (rw == READ) {
2445 nr = file->f_op->read(file, iovec.iov_base,
2446 iovec.iov_len, &kiocb->ki_pos);
2447 } else {
2448 nr = file->f_op->write(file, iovec.iov_base,
2449 iovec.iov_len, &kiocb->ki_pos);
2450 }
2451
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002452 if (iov_iter_is_bvec(iter))
2453 kunmap(iter->bvec->bv_page);
2454
Jens Axboe32960612019-09-23 11:05:34 -06002455 if (nr < 0) {
2456 if (!ret)
2457 ret = nr;
2458 break;
2459 }
2460 ret += nr;
2461 if (nr != iovec.iov_len)
2462 break;
2463 iov_iter_advance(iter, nr);
2464 }
2465
2466 return ret;
2467}
2468
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002469static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002470 struct iovec *iovec, struct iovec *fast_iov,
2471 struct iov_iter *iter)
2472{
2473 req->io->rw.nr_segs = iter->nr_segs;
2474 req->io->rw.size = io_size;
2475 req->io->rw.iov = iovec;
2476 if (!req->io->rw.iov) {
2477 req->io->rw.iov = req->io->rw.fast_iov;
Xiaoguang Wang45097da2020-04-08 22:29:58 +08002478 if (req->io->rw.iov != fast_iov)
2479 memcpy(req->io->rw.iov, fast_iov,
2480 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002481 } else {
2482 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002483 }
2484}
2485
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002486static inline int __io_alloc_async_ctx(struct io_kiocb *req)
2487{
2488 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
2489 return req->io == NULL;
2490}
2491
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002492static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002493{
Jens Axboed3656342019-12-18 09:50:26 -07002494 if (!io_op_defs[req->opcode].async_ctx)
2495 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002496
2497 return __io_alloc_async_ctx(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002498}
2499
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002500static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2501 struct iovec *iovec, struct iovec *fast_iov,
2502 struct iov_iter *iter)
2503{
Jens Axboe980ad262020-01-24 23:08:54 -07002504 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002505 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002506 if (!req->io) {
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002507 if (__io_alloc_async_ctx(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07002508 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002509
Jens Axboe5d204bc2020-01-31 12:06:52 -07002510 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2511 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002512 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002513}
2514
Jens Axboe3529d8c2019-12-19 18:24:38 -07002515static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2516 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002517{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002518 struct io_async_ctx *io;
2519 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002520 ssize_t ret;
2521
Jens Axboe3529d8c2019-12-19 18:24:38 -07002522 ret = io_prep_rw(req, sqe, force_nonblock);
2523 if (ret)
2524 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002525
Jens Axboe3529d8c2019-12-19 18:24:38 -07002526 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2527 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002528
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002529 /* either don't need iovec imported or already have it */
2530 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002531 return 0;
2532
2533 io = req->io;
2534 io->rw.iov = io->rw.fast_iov;
2535 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002536 ret = io_import_iovec(READ, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002537 req->io = io;
2538 if (ret < 0)
2539 return ret;
2540
2541 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2542 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002543}
2544
Pavel Begunkov014db002020-03-03 21:33:12 +03002545static int io_read(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002546{
2547 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002548 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002549 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002550 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002551 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002552
Jens Axboebcda7ba2020-02-23 16:42:51 -07002553 ret = io_import_iovec(READ, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07002554 if (ret < 0)
2555 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002556
Jens Axboefd6c2e42019-12-18 12:19:41 -07002557 /* Ensure we clear previously set non-block flag */
2558 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07002559 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002560
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002561 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002562 io_size = ret;
Pavel Begunkovdea3b492020-04-12 02:05:04 +03002563 if (req->flags & REQ_F_LINK_HEAD)
Jens Axboef67676d2019-12-02 11:03:47 -07002564 req->result = io_size;
2565
2566 /*
2567 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2568 * we know to async punt it even if it was opened O_NONBLOCK
2569 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002570 if (force_nonblock && !io_file_supports_async(req->file, READ))
Jens Axboef67676d2019-12-02 11:03:47 -07002571 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002572
Jens Axboe31b51512019-01-18 22:56:34 -07002573 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002574 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002575 if (!ret) {
2576 ssize_t ret2;
2577
Jens Axboe9adbd452019-12-20 08:45:55 -07002578 if (req->file->f_op->read_iter)
2579 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002580 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002581 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002582
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002583 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002584 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkov014db002020-03-03 21:33:12 +03002585 kiocb_done(kiocb, ret2);
Jens Axboef67676d2019-12-02 11:03:47 -07002586 } else {
2587copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002588 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002589 inline_vecs, &iter);
2590 if (ret)
2591 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002592 /* any defer here is final, must blocking retry */
Jens Axboe490e8962020-04-28 13:16:53 -06002593 if (!(req->flags & REQ_F_NOWAIT) &&
2594 !file_can_poll(req->file))
Jens Axboe29de5f62020-02-20 09:56:08 -07002595 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002596 return -EAGAIN;
2597 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002598 }
Jens Axboef67676d2019-12-02 11:03:47 -07002599out_free:
Pavel Begunkov1e950812020-02-06 19:51:16 +03002600 kfree(iovec);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002601 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002602 return ret;
2603}
2604
Jens Axboe3529d8c2019-12-19 18:24:38 -07002605static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2606 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002607{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002608 struct io_async_ctx *io;
2609 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002610 ssize_t ret;
2611
Jens Axboe3529d8c2019-12-19 18:24:38 -07002612 ret = io_prep_rw(req, sqe, force_nonblock);
2613 if (ret)
2614 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002615
Jens Axboe3529d8c2019-12-19 18:24:38 -07002616 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2617 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002618
Jens Axboe4ed734b2020-03-20 11:23:41 -06002619 req->fsize = rlimit(RLIMIT_FSIZE);
2620
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002621 /* either don't need iovec imported or already have it */
2622 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002623 return 0;
2624
2625 io = req->io;
2626 io->rw.iov = io->rw.fast_iov;
2627 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002628 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002629 req->io = io;
2630 if (ret < 0)
2631 return ret;
2632
2633 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2634 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002635}
2636
Pavel Begunkov014db002020-03-03 21:33:12 +03002637static int io_write(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002638{
2639 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002640 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002641 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002642 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002643 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002644
Jens Axboebcda7ba2020-02-23 16:42:51 -07002645 ret = io_import_iovec(WRITE, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07002646 if (ret < 0)
2647 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002648
Jens Axboefd6c2e42019-12-18 12:19:41 -07002649 /* Ensure we clear previously set non-block flag */
2650 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002651 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002652
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002653 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002654 io_size = ret;
Pavel Begunkovdea3b492020-04-12 02:05:04 +03002655 if (req->flags & REQ_F_LINK_HEAD)
Jens Axboef67676d2019-12-02 11:03:47 -07002656 req->result = io_size;
2657
2658 /*
2659 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2660 * we know to async punt it even if it was opened O_NONBLOCK
2661 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002662 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07002663 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07002664
Jens Axboe10d59342019-12-09 20:16:22 -07002665 /* file path doesn't support NOWAIT for non-direct_IO */
2666 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2667 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002668 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002669
Jens Axboe31b51512019-01-18 22:56:34 -07002670 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002671 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002672 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002673 ssize_t ret2;
2674
Jens Axboe2b188cc2019-01-07 10:46:33 -07002675 /*
2676 * Open-code file_start_write here to grab freeze protection,
2677 * which will be released by another thread in
2678 * io_complete_rw(). Fool lockdep by telling it the lock got
2679 * released so that it doesn't complain about the held lock when
2680 * we return to userspace.
2681 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002682 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002683 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002684 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002685 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002686 SB_FREEZE_WRITE);
2687 }
2688 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002689
Jens Axboe4ed734b2020-03-20 11:23:41 -06002690 if (!force_nonblock)
2691 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
2692
Jens Axboe9adbd452019-12-20 08:45:55 -07002693 if (req->file->f_op->write_iter)
2694 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002695 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002696 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboe4ed734b2020-03-20 11:23:41 -06002697
2698 if (!force_nonblock)
2699 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
2700
Jens Axboefaac9962020-02-07 15:45:22 -07002701 /*
Chucheng Luobff60352020-03-25 11:31:38 +08002702 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
Jens Axboefaac9962020-02-07 15:45:22 -07002703 * retry them without IOCB_NOWAIT.
2704 */
2705 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
2706 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07002707 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkov014db002020-03-03 21:33:12 +03002708 kiocb_done(kiocb, ret2);
Jens Axboef67676d2019-12-02 11:03:47 -07002709 } else {
2710copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002711 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002712 inline_vecs, &iter);
2713 if (ret)
2714 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002715 /* any defer here is final, must blocking retry */
Jens Axboe490e8962020-04-28 13:16:53 -06002716 if (!file_can_poll(req->file))
2717 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002718 return -EAGAIN;
2719 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002720 }
Jens Axboe31b51512019-01-18 22:56:34 -07002721out_free:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002722 req->flags &= ~REQ_F_NEED_CLEANUP;
Pavel Begunkov1e950812020-02-06 19:51:16 +03002723 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002724 return ret;
2725}
2726
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002727static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2728{
2729 struct io_splice* sp = &req->splice;
2730 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
2731 int ret;
2732
2733 if (req->flags & REQ_F_NEED_CLEANUP)
2734 return 0;
2735
2736 sp->file_in = NULL;
2737 sp->off_in = READ_ONCE(sqe->splice_off_in);
2738 sp->off_out = READ_ONCE(sqe->off);
2739 sp->len = READ_ONCE(sqe->len);
2740 sp->flags = READ_ONCE(sqe->splice_flags);
2741
2742 if (unlikely(sp->flags & ~valid_flags))
2743 return -EINVAL;
2744
2745 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
2746 (sp->flags & SPLICE_F_FD_IN_FIXED));
2747 if (ret)
2748 return ret;
2749 req->flags |= REQ_F_NEED_CLEANUP;
2750
2751 if (!S_ISREG(file_inode(sp->file_in)->i_mode))
2752 req->work.flags |= IO_WQ_WORK_UNBOUND;
2753
2754 return 0;
2755}
2756
Pavel Begunkov014db002020-03-03 21:33:12 +03002757static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002758{
2759 struct io_splice *sp = &req->splice;
2760 struct file *in = sp->file_in;
2761 struct file *out = sp->file_out;
2762 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
2763 loff_t *poff_in, *poff_out;
2764 long ret;
2765
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03002766 if (force_nonblock)
2767 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002768
2769 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
2770 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
2771 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
2772 if (force_nonblock && ret == -EAGAIN)
2773 return -EAGAIN;
2774
2775 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
2776 req->flags &= ~REQ_F_NEED_CLEANUP;
2777
2778 io_cqring_add_event(req, ret);
2779 if (ret != sp->len)
2780 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03002781 io_put_req(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002782 return 0;
2783}
2784
Jens Axboe2b188cc2019-01-07 10:46:33 -07002785/*
2786 * IORING_OP_NOP just posts a completion event, nothing else.
2787 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002788static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002789{
2790 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002791
Jens Axboedef596e2019-01-09 08:59:42 -07002792 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2793 return -EINVAL;
2794
Jens Axboe78e19bb2019-11-06 15:21:34 -07002795 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002796 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002797 return 0;
2798}
2799
Jens Axboe3529d8c2019-12-19 18:24:38 -07002800static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002801{
Jens Axboe6b063142019-01-10 22:13:58 -07002802 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002803
Jens Axboe09bb8392019-03-13 12:39:28 -06002804 if (!req->file)
2805 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002806
Jens Axboe6b063142019-01-10 22:13:58 -07002807 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002808 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002809 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002810 return -EINVAL;
2811
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002812 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2813 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2814 return -EINVAL;
2815
2816 req->sync.off = READ_ONCE(sqe->off);
2817 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002818 return 0;
2819}
2820
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002821static bool io_req_cancelled(struct io_kiocb *req)
2822{
2823 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2824 req_set_fail_links(req);
2825 io_cqring_add_event(req, -ECANCELED);
2826 io_put_req(req);
2827 return true;
2828 }
2829
2830 return false;
2831}
2832
Pavel Begunkov014db002020-03-03 21:33:12 +03002833static void __io_fsync(struct io_kiocb *req)
Jens Axboe78912932020-01-14 22:09:06 -07002834{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002835 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002836 int ret;
2837
Jens Axboe9adbd452019-12-20 08:45:55 -07002838 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002839 end > 0 ? end : LLONG_MAX,
2840 req->sync.flags & IORING_FSYNC_DATASYNC);
2841 if (ret < 0)
2842 req_set_fail_links(req);
2843 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002844 io_put_req(req);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002845}
2846
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002847static void io_fsync_finish(struct io_wq_work **workptr)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002848{
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002849 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002850
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002851 if (io_req_cancelled(req))
2852 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03002853 __io_fsync(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03002854 io_steal_work(req, workptr);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002855}
2856
Pavel Begunkov014db002020-03-03 21:33:12 +03002857static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002858{
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002859 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002860 if (force_nonblock) {
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002861 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002862 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002863 }
Pavel Begunkov014db002020-03-03 21:33:12 +03002864 __io_fsync(req);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002865 return 0;
2866}
2867
Pavel Begunkov014db002020-03-03 21:33:12 +03002868static void __io_fallocate(struct io_kiocb *req)
Jens Axboed63d1b52019-12-10 10:38:56 -07002869{
Jens Axboed63d1b52019-12-10 10:38:56 -07002870 int ret;
2871
Jens Axboe4ed734b2020-03-20 11:23:41 -06002872 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
Jens Axboed63d1b52019-12-10 10:38:56 -07002873 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2874 req->sync.len);
Jens Axboe4ed734b2020-03-20 11:23:41 -06002875 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
Jens Axboed63d1b52019-12-10 10:38:56 -07002876 if (ret < 0)
2877 req_set_fail_links(req);
2878 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002879 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002880}
2881
Jens Axboe2b188cc2019-01-07 10:46:33 -07002882static void io_fallocate_finish(struct io_wq_work **workptr)
2883{
2884 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Jens Axboed63d1b52019-12-10 10:38:56 -07002885
2886 if (io_req_cancelled(req))
2887 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03002888 __io_fallocate(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03002889 io_steal_work(req, workptr);
Jens Axboed63d1b52019-12-10 10:38:56 -07002890}
2891
2892static int io_fallocate_prep(struct io_kiocb *req,
2893 const struct io_uring_sqe *sqe)
2894{
2895 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2896 return -EINVAL;
2897
2898 req->sync.off = READ_ONCE(sqe->off);
2899 req->sync.len = READ_ONCE(sqe->addr);
2900 req->sync.mode = READ_ONCE(sqe->len);
Jens Axboe4ed734b2020-03-20 11:23:41 -06002901 req->fsize = rlimit(RLIMIT_FSIZE);
Jens Axboed63d1b52019-12-10 10:38:56 -07002902 return 0;
2903}
2904
Pavel Begunkov014db002020-03-03 21:33:12 +03002905static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07002906{
Jens Axboed63d1b52019-12-10 10:38:56 -07002907 /* fallocate always requiring blocking context */
2908 if (force_nonblock) {
Jens Axboed63d1b52019-12-10 10:38:56 -07002909 req->work.func = io_fallocate_finish;
2910 return -EAGAIN;
2911 }
2912
Pavel Begunkov014db002020-03-03 21:33:12 +03002913 __io_fallocate(req);
Jens Axboed63d1b52019-12-10 10:38:56 -07002914 return 0;
2915}
2916
Jens Axboe15b71ab2019-12-11 11:20:36 -07002917static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2918{
Jens Axboef8748882020-01-08 17:47:02 -07002919 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002920 int ret;
2921
2922 if (sqe->ioprio || sqe->buf_index)
2923 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03002924 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07002925 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002926 if (req->flags & REQ_F_NEED_CLEANUP)
2927 return 0;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002928
2929 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002930 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002931 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002932 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06002933 if (force_o_largefile())
2934 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002935
Jens Axboef8748882020-01-08 17:47:02 -07002936 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002937 if (IS_ERR(req->open.filename)) {
2938 ret = PTR_ERR(req->open.filename);
2939 req->open.filename = NULL;
2940 return ret;
2941 }
2942
Jens Axboe4022e7a2020-03-19 19:23:18 -06002943 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002944 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002945 return 0;
2946}
2947
Jens Axboecebdb982020-01-08 17:59:24 -07002948static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2949{
2950 struct open_how __user *how;
2951 const char __user *fname;
2952 size_t len;
2953 int ret;
2954
2955 if (sqe->ioprio || sqe->buf_index)
2956 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03002957 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07002958 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002959 if (req->flags & REQ_F_NEED_CLEANUP)
2960 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07002961
2962 req->open.dfd = READ_ONCE(sqe->fd);
2963 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2964 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2965 len = READ_ONCE(sqe->len);
2966
2967 if (len < OPEN_HOW_SIZE_VER0)
2968 return -EINVAL;
2969
2970 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2971 len);
2972 if (ret)
2973 return ret;
2974
2975 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2976 req->open.how.flags |= O_LARGEFILE;
2977
2978 req->open.filename = getname(fname);
2979 if (IS_ERR(req->open.filename)) {
2980 ret = PTR_ERR(req->open.filename);
2981 req->open.filename = NULL;
2982 return ret;
2983 }
2984
Jens Axboe4022e7a2020-03-19 19:23:18 -06002985 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002986 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboecebdb982020-01-08 17:59:24 -07002987 return 0;
2988}
2989
Pavel Begunkov014db002020-03-03 21:33:12 +03002990static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002991{
2992 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002993 struct file *file;
2994 int ret;
2995
Jens Axboef86cd202020-01-29 13:46:44 -07002996 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002997 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002998
Jens Axboecebdb982020-01-08 17:59:24 -07002999 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003000 if (ret)
3001 goto err;
3002
Jens Axboe4022e7a2020-03-19 19:23:18 -06003003 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003004 if (ret < 0)
3005 goto err;
3006
3007 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3008 if (IS_ERR(file)) {
3009 put_unused_fd(ret);
3010 ret = PTR_ERR(file);
3011 } else {
3012 fsnotify_open(file);
3013 fd_install(ret, file);
3014 }
3015err:
3016 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003017 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003018 if (ret < 0)
3019 req_set_fail_links(req);
3020 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003021 io_put_req(req);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003022 return 0;
3023}
3024
Pavel Begunkov014db002020-03-03 21:33:12 +03003025static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003026{
3027 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
Pavel Begunkov014db002020-03-03 21:33:12 +03003028 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003029}
3030
Jens Axboe067524e2020-03-02 16:32:28 -07003031static int io_remove_buffers_prep(struct io_kiocb *req,
3032 const struct io_uring_sqe *sqe)
3033{
3034 struct io_provide_buf *p = &req->pbuf;
3035 u64 tmp;
3036
3037 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3038 return -EINVAL;
3039
3040 tmp = READ_ONCE(sqe->fd);
3041 if (!tmp || tmp > USHRT_MAX)
3042 return -EINVAL;
3043
3044 memset(p, 0, sizeof(*p));
3045 p->nbufs = tmp;
3046 p->bgid = READ_ONCE(sqe->buf_group);
3047 return 0;
3048}
3049
3050static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3051 int bgid, unsigned nbufs)
3052{
3053 unsigned i = 0;
3054
3055 /* shouldn't happen */
3056 if (!nbufs)
3057 return 0;
3058
3059 /* the head kbuf is the list itself */
3060 while (!list_empty(&buf->list)) {
3061 struct io_buffer *nxt;
3062
3063 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3064 list_del(&nxt->list);
3065 kfree(nxt);
3066 if (++i == nbufs)
3067 return i;
3068 }
3069 i++;
3070 kfree(buf);
3071 idr_remove(&ctx->io_buffer_idr, bgid);
3072
3073 return i;
3074}
3075
3076static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock)
3077{
3078 struct io_provide_buf *p = &req->pbuf;
3079 struct io_ring_ctx *ctx = req->ctx;
3080 struct io_buffer *head;
3081 int ret = 0;
3082
3083 io_ring_submit_lock(ctx, !force_nonblock);
3084
3085 lockdep_assert_held(&ctx->uring_lock);
3086
3087 ret = -ENOENT;
3088 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3089 if (head)
3090 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3091
3092 io_ring_submit_lock(ctx, !force_nonblock);
3093 if (ret < 0)
3094 req_set_fail_links(req);
3095 io_cqring_add_event(req, ret);
3096 io_put_req(req);
3097 return 0;
3098}
3099
Jens Axboeddf0322d2020-02-23 16:41:33 -07003100static int io_provide_buffers_prep(struct io_kiocb *req,
3101 const struct io_uring_sqe *sqe)
3102{
3103 struct io_provide_buf *p = &req->pbuf;
3104 u64 tmp;
3105
3106 if (sqe->ioprio || sqe->rw_flags)
3107 return -EINVAL;
3108
3109 tmp = READ_ONCE(sqe->fd);
3110 if (!tmp || tmp > USHRT_MAX)
3111 return -E2BIG;
3112 p->nbufs = tmp;
3113 p->addr = READ_ONCE(sqe->addr);
3114 p->len = READ_ONCE(sqe->len);
3115
3116 if (!access_ok(u64_to_user_ptr(p->addr), p->len))
3117 return -EFAULT;
3118
3119 p->bgid = READ_ONCE(sqe->buf_group);
3120 tmp = READ_ONCE(sqe->off);
3121 if (tmp > USHRT_MAX)
3122 return -E2BIG;
3123 p->bid = tmp;
3124 return 0;
3125}
3126
3127static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3128{
3129 struct io_buffer *buf;
3130 u64 addr = pbuf->addr;
3131 int i, bid = pbuf->bid;
3132
3133 for (i = 0; i < pbuf->nbufs; i++) {
3134 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3135 if (!buf)
3136 break;
3137
3138 buf->addr = addr;
3139 buf->len = pbuf->len;
3140 buf->bid = bid;
3141 addr += pbuf->len;
3142 bid++;
3143 if (!*head) {
3144 INIT_LIST_HEAD(&buf->list);
3145 *head = buf;
3146 } else {
3147 list_add_tail(&buf->list, &(*head)->list);
3148 }
3149 }
3150
3151 return i ? i : -ENOMEM;
3152}
3153
Jens Axboeddf0322d2020-02-23 16:41:33 -07003154static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock)
3155{
3156 struct io_provide_buf *p = &req->pbuf;
3157 struct io_ring_ctx *ctx = req->ctx;
3158 struct io_buffer *head, *list;
3159 int ret = 0;
3160
3161 io_ring_submit_lock(ctx, !force_nonblock);
3162
3163 lockdep_assert_held(&ctx->uring_lock);
3164
3165 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3166
3167 ret = io_add_buffers(p, &head);
3168 if (ret < 0)
3169 goto out;
3170
3171 if (!list) {
3172 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3173 GFP_KERNEL);
3174 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003175 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003176 goto out;
3177 }
3178 }
3179out:
3180 io_ring_submit_unlock(ctx, !force_nonblock);
3181 if (ret < 0)
3182 req_set_fail_links(req);
3183 io_cqring_add_event(req, ret);
3184 io_put_req(req);
3185 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003186}
3187
Jens Axboe3e4827b2020-01-08 15:18:09 -07003188static int io_epoll_ctl_prep(struct io_kiocb *req,
3189 const struct io_uring_sqe *sqe)
3190{
3191#if defined(CONFIG_EPOLL)
3192 if (sqe->ioprio || sqe->buf_index)
3193 return -EINVAL;
3194
3195 req->epoll.epfd = READ_ONCE(sqe->fd);
3196 req->epoll.op = READ_ONCE(sqe->len);
3197 req->epoll.fd = READ_ONCE(sqe->off);
3198
3199 if (ep_op_has_event(req->epoll.op)) {
3200 struct epoll_event __user *ev;
3201
3202 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3203 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3204 return -EFAULT;
3205 }
3206
3207 return 0;
3208#else
3209 return -EOPNOTSUPP;
3210#endif
3211}
3212
Pavel Begunkov014db002020-03-03 21:33:12 +03003213static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003214{
3215#if defined(CONFIG_EPOLL)
3216 struct io_epoll *ie = &req->epoll;
3217 int ret;
3218
3219 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3220 if (force_nonblock && ret == -EAGAIN)
3221 return -EAGAIN;
3222
3223 if (ret < 0)
3224 req_set_fail_links(req);
3225 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003226 io_put_req(req);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003227 return 0;
3228#else
3229 return -EOPNOTSUPP;
3230#endif
3231}
3232
Jens Axboec1ca7572019-12-25 22:18:28 -07003233static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3234{
3235#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3236 if (sqe->ioprio || sqe->buf_index || sqe->off)
3237 return -EINVAL;
3238
3239 req->madvise.addr = READ_ONCE(sqe->addr);
3240 req->madvise.len = READ_ONCE(sqe->len);
3241 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3242 return 0;
3243#else
3244 return -EOPNOTSUPP;
3245#endif
3246}
3247
Pavel Begunkov014db002020-03-03 21:33:12 +03003248static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003249{
3250#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3251 struct io_madvise *ma = &req->madvise;
3252 int ret;
3253
3254 if (force_nonblock)
3255 return -EAGAIN;
3256
3257 ret = do_madvise(ma->addr, ma->len, ma->advice);
3258 if (ret < 0)
3259 req_set_fail_links(req);
3260 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003261 io_put_req(req);
Jens Axboec1ca7572019-12-25 22:18:28 -07003262 return 0;
3263#else
3264 return -EOPNOTSUPP;
3265#endif
3266}
3267
Jens Axboe4840e412019-12-25 22:03:45 -07003268static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3269{
3270 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3271 return -EINVAL;
3272
3273 req->fadvise.offset = READ_ONCE(sqe->off);
3274 req->fadvise.len = READ_ONCE(sqe->len);
3275 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3276 return 0;
3277}
3278
Pavel Begunkov014db002020-03-03 21:33:12 +03003279static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07003280{
3281 struct io_fadvise *fa = &req->fadvise;
3282 int ret;
3283
Jens Axboe3e694262020-02-01 09:22:49 -07003284 if (force_nonblock) {
3285 switch (fa->advice) {
3286 case POSIX_FADV_NORMAL:
3287 case POSIX_FADV_RANDOM:
3288 case POSIX_FADV_SEQUENTIAL:
3289 break;
3290 default:
3291 return -EAGAIN;
3292 }
3293 }
Jens Axboe4840e412019-12-25 22:03:45 -07003294
3295 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3296 if (ret < 0)
3297 req_set_fail_links(req);
3298 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003299 io_put_req(req);
Jens Axboe4840e412019-12-25 22:03:45 -07003300 return 0;
3301}
3302
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003303static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3304{
Jens Axboef8748882020-01-08 17:47:02 -07003305 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003306 unsigned lookup_flags;
3307 int ret;
3308
3309 if (sqe->ioprio || sqe->buf_index)
3310 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003311 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003312 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003313 if (req->flags & REQ_F_NEED_CLEANUP)
3314 return 0;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003315
3316 req->open.dfd = READ_ONCE(sqe->fd);
3317 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07003318 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003319 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07003320 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003321
Jens Axboec12cedf2020-01-08 17:41:21 -07003322 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003323 return -EINVAL;
3324
Jens Axboef8748882020-01-08 17:47:02 -07003325 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003326 if (IS_ERR(req->open.filename)) {
3327 ret = PTR_ERR(req->open.filename);
3328 req->open.filename = NULL;
3329 return ret;
3330 }
3331
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003332 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003333 return 0;
3334}
3335
Pavel Begunkov014db002020-03-03 21:33:12 +03003336static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003337{
3338 struct io_open *ctx = &req->open;
3339 unsigned lookup_flags;
3340 struct path path;
3341 struct kstat stat;
3342 int ret;
3343
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003344 if (force_nonblock) {
3345 /* only need file table for an actual valid fd */
3346 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
3347 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003348 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003349 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003350
Jens Axboec12cedf2020-01-08 17:41:21 -07003351 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003352 return -EINVAL;
3353
3354retry:
3355 /* filename_lookup() drops it, keep a reference */
3356 ctx->filename->refcnt++;
3357
3358 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
3359 NULL);
3360 if (ret)
3361 goto err;
3362
Jens Axboec12cedf2020-01-08 17:41:21 -07003363 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003364 path_put(&path);
3365 if (retry_estale(ret, lookup_flags)) {
3366 lookup_flags |= LOOKUP_REVAL;
3367 goto retry;
3368 }
3369 if (!ret)
3370 ret = cp_statx(&stat, ctx->buffer);
3371err:
3372 putname(ctx->filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003373 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003374 if (ret < 0)
3375 req_set_fail_links(req);
3376 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003377 io_put_req(req);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003378 return 0;
3379}
3380
Jens Axboeb5dba592019-12-11 14:02:38 -07003381static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3382{
3383 /*
3384 * If we queue this for async, it must not be cancellable. That would
3385 * leave the 'file' in an undeterminate state.
3386 */
3387 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
3388
3389 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
3390 sqe->rw_flags || sqe->buf_index)
3391 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003392 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003393 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07003394
3395 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboeb5dba592019-12-11 14:02:38 -07003396 return 0;
3397}
3398
Pavel Begunkova93b3332020-02-08 14:04:34 +03003399/* only called when __close_fd_get_file() is done */
Pavel Begunkov014db002020-03-03 21:33:12 +03003400static void __io_close_finish(struct io_kiocb *req)
Pavel Begunkova93b3332020-02-08 14:04:34 +03003401{
3402 int ret;
3403
3404 ret = filp_close(req->close.put_file, req->work.files);
3405 if (ret < 0)
3406 req_set_fail_links(req);
3407 io_cqring_add_event(req, ret);
3408 fput(req->close.put_file);
Pavel Begunkov014db002020-03-03 21:33:12 +03003409 io_put_req(req);
Pavel Begunkova93b3332020-02-08 14:04:34 +03003410}
3411
Jens Axboeb5dba592019-12-11 14:02:38 -07003412static void io_close_finish(struct io_wq_work **workptr)
3413{
3414 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Jens Axboeb5dba592019-12-11 14:02:38 -07003415
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03003416 /* not cancellable, don't do io_req_cancelled() */
Pavel Begunkov014db002020-03-03 21:33:12 +03003417 __io_close_finish(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03003418 io_steal_work(req, workptr);
Jens Axboeb5dba592019-12-11 14:02:38 -07003419}
3420
Pavel Begunkov014db002020-03-03 21:33:12 +03003421static int io_close(struct io_kiocb *req, bool force_nonblock)
Jens Axboeb5dba592019-12-11 14:02:38 -07003422{
3423 int ret;
3424
3425 req->close.put_file = NULL;
3426 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
Jens Axboe904fbcb2020-05-08 21:27:24 -06003427 if (ret < 0) {
3428 if (ret == -ENOENT)
3429 ret = -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07003430 return ret;
Jens Axboe904fbcb2020-05-08 21:27:24 -06003431 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003432
3433 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkova2100672020-03-02 23:45:16 +03003434 if (req->close.put_file->f_op->flush && force_nonblock) {
Pavel Begunkov594506f2020-03-03 21:33:11 +03003435 /* submission ref will be dropped, take it for async */
3436 refcount_inc(&req->refs);
3437
Pavel Begunkova2100672020-03-02 23:45:16 +03003438 req->work.func = io_close_finish;
3439 /*
3440 * Do manual async queue here to avoid grabbing files - we don't
3441 * need the files, and it'll cause io_close_finish() to close
3442 * the file again and cause a double CQE entry for this request
3443 */
3444 io_queue_async_work(req);
3445 return 0;
3446 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003447
3448 /*
3449 * No ->flush(), safely close from here and just punt the
3450 * fput() to async context.
3451 */
Pavel Begunkov014db002020-03-03 21:33:12 +03003452 __io_close_finish(req);
Jens Axboe1a417f42020-01-31 17:16:48 -07003453 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07003454}
3455
Jens Axboe3529d8c2019-12-19 18:24:38 -07003456static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003457{
3458 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003459
3460 if (!req->file)
3461 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003462
3463 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3464 return -EINVAL;
3465 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
3466 return -EINVAL;
3467
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003468 req->sync.off = READ_ONCE(sqe->off);
3469 req->sync.len = READ_ONCE(sqe->len);
3470 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003471 return 0;
3472}
3473
Pavel Begunkov014db002020-03-03 21:33:12 +03003474static void __io_sync_file_range(struct io_kiocb *req)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003475{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003476 int ret;
3477
Jens Axboe9adbd452019-12-20 08:45:55 -07003478 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003479 req->sync.flags);
3480 if (ret < 0)
3481 req_set_fail_links(req);
3482 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003483 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03003484}
3485
3486
3487static void io_sync_file_range_finish(struct io_wq_work **workptr)
3488{
3489 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03003490
3491 if (io_req_cancelled(req))
3492 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03003493 __io_sync_file_range(req);
Pavel Begunkov7759a0b2020-05-01 17:09:36 +03003494 io_steal_work(req, workptr);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003495}
3496
Pavel Begunkov014db002020-03-03 21:33:12 +03003497static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003498{
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003499 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003500 if (force_nonblock) {
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003501 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003502 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003503 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003504
Pavel Begunkov014db002020-03-03 21:33:12 +03003505 __io_sync_file_range(req);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003506 return 0;
3507}
3508
YueHaibing469956e2020-03-04 15:53:52 +08003509#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003510static int io_setup_async_msg(struct io_kiocb *req,
3511 struct io_async_msghdr *kmsg)
3512{
3513 if (req->io)
3514 return -EAGAIN;
3515 if (io_alloc_async_ctx(req)) {
3516 if (kmsg->iov != kmsg->fast_iov)
3517 kfree(kmsg->iov);
3518 return -ENOMEM;
3519 }
3520 req->flags |= REQ_F_NEED_CLEANUP;
3521 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
3522 return -EAGAIN;
3523}
3524
Jens Axboe3529d8c2019-12-19 18:24:38 -07003525static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06003526{
Jens Axboee47293f2019-12-20 08:58:21 -07003527 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003528 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003529 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003530
Jens Axboee47293f2019-12-20 08:58:21 -07003531 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3532 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07003533 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003534
Jens Axboed8768362020-02-27 14:17:49 -07003535#ifdef CONFIG_COMPAT
3536 if (req->ctx->compat)
3537 sr->msg_flags |= MSG_CMSG_COMPAT;
3538#endif
3539
Jens Axboefddafac2020-01-04 20:19:44 -07003540 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003541 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003542 /* iovec is already imported */
3543 if (req->flags & REQ_F_NEED_CLEANUP)
3544 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003545
Jens Axboed9688562019-12-09 19:35:20 -07003546 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003547 ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003548 &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003549 if (!ret)
3550 req->flags |= REQ_F_NEED_CLEANUP;
3551 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003552}
3553
Pavel Begunkov014db002020-03-03 21:33:12 +03003554static int io_sendmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003555{
Jens Axboe0b416c32019-12-15 10:57:46 -07003556 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07003557 struct socket *sock;
3558 int ret;
3559
3560 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3561 return -EINVAL;
3562
3563 sock = sock_from_file(req->file, &ret);
3564 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003565 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003566 unsigned flags;
3567
Jens Axboe03b12302019-12-02 18:50:25 -07003568 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003569 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003570 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003571 /* if iov is set, it's allocated already */
3572 if (!kmsg->iov)
3573 kmsg->iov = kmsg->fast_iov;
3574 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003575 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003576 struct io_sr_msg *sr = &req->sr_msg;
3577
Jens Axboe0b416c32019-12-15 10:57:46 -07003578 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003579 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003580
3581 io.msg.iov = io.msg.fast_iov;
3582 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3583 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003584 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003585 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003586 }
3587
Jens Axboee47293f2019-12-20 08:58:21 -07003588 flags = req->sr_msg.msg_flags;
3589 if (flags & MSG_DONTWAIT)
3590 req->flags |= REQ_F_NOWAIT;
3591 else if (force_nonblock)
3592 flags |= MSG_DONTWAIT;
3593
Jens Axboe0b416c32019-12-15 10:57:46 -07003594 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003595 if (force_nonblock && ret == -EAGAIN)
3596 return io_setup_async_msg(req, kmsg);
Jens Axboe03b12302019-12-02 18:50:25 -07003597 if (ret == -ERESTARTSYS)
3598 ret = -EINTR;
3599 }
3600
Pavel Begunkov1e950812020-02-06 19:51:16 +03003601 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003602 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003603 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe03b12302019-12-02 18:50:25 -07003604 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003605 if (ret < 0)
3606 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003607 io_put_req(req);
Jens Axboe03b12302019-12-02 18:50:25 -07003608 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003609}
3610
Pavel Begunkov014db002020-03-03 21:33:12 +03003611static int io_send(struct io_kiocb *req, bool force_nonblock)
Jens Axboefddafac2020-01-04 20:19:44 -07003612{
Jens Axboefddafac2020-01-04 20:19:44 -07003613 struct socket *sock;
3614 int ret;
3615
3616 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3617 return -EINVAL;
3618
3619 sock = sock_from_file(req->file, &ret);
3620 if (sock) {
3621 struct io_sr_msg *sr = &req->sr_msg;
3622 struct msghdr msg;
3623 struct iovec iov;
3624 unsigned flags;
3625
3626 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3627 &msg.msg_iter);
3628 if (ret)
3629 return ret;
3630
3631 msg.msg_name = NULL;
3632 msg.msg_control = NULL;
3633 msg.msg_controllen = 0;
3634 msg.msg_namelen = 0;
3635
3636 flags = req->sr_msg.msg_flags;
3637 if (flags & MSG_DONTWAIT)
3638 req->flags |= REQ_F_NOWAIT;
3639 else if (force_nonblock)
3640 flags |= MSG_DONTWAIT;
3641
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003642 msg.msg_flags = flags;
3643 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003644 if (force_nonblock && ret == -EAGAIN)
3645 return -EAGAIN;
3646 if (ret == -ERESTARTSYS)
3647 ret = -EINTR;
3648 }
3649
3650 io_cqring_add_event(req, ret);
3651 if (ret < 0)
3652 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003653 io_put_req(req);
Jens Axboefddafac2020-01-04 20:19:44 -07003654 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003655}
3656
Jens Axboe52de1fe2020-02-27 10:15:42 -07003657static int __io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
3658{
3659 struct io_sr_msg *sr = &req->sr_msg;
3660 struct iovec __user *uiov;
3661 size_t iov_len;
3662 int ret;
3663
3664 ret = __copy_msghdr_from_user(&io->msg.msg, sr->msg, &io->msg.uaddr,
3665 &uiov, &iov_len);
3666 if (ret)
3667 return ret;
3668
3669 if (req->flags & REQ_F_BUFFER_SELECT) {
3670 if (iov_len > 1)
3671 return -EINVAL;
3672 if (copy_from_user(io->msg.iov, uiov, sizeof(*uiov)))
3673 return -EFAULT;
3674 sr->len = io->msg.iov[0].iov_len;
3675 iov_iter_init(&io->msg.msg.msg_iter, READ, io->msg.iov, 1,
3676 sr->len);
3677 io->msg.iov = NULL;
3678 } else {
3679 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
3680 &io->msg.iov, &io->msg.msg.msg_iter);
3681 if (ret > 0)
3682 ret = 0;
3683 }
3684
3685 return ret;
3686}
3687
3688#ifdef CONFIG_COMPAT
3689static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
3690 struct io_async_ctx *io)
3691{
3692 struct compat_msghdr __user *msg_compat;
3693 struct io_sr_msg *sr = &req->sr_msg;
3694 struct compat_iovec __user *uiov;
3695 compat_uptr_t ptr;
3696 compat_size_t len;
3697 int ret;
3698
3699 msg_compat = (struct compat_msghdr __user *) sr->msg;
3700 ret = __get_compat_msghdr(&io->msg.msg, msg_compat, &io->msg.uaddr,
3701 &ptr, &len);
3702 if (ret)
3703 return ret;
3704
3705 uiov = compat_ptr(ptr);
3706 if (req->flags & REQ_F_BUFFER_SELECT) {
3707 compat_ssize_t clen;
3708
3709 if (len > 1)
3710 return -EINVAL;
3711 if (!access_ok(uiov, sizeof(*uiov)))
3712 return -EFAULT;
3713 if (__get_user(clen, &uiov->iov_len))
3714 return -EFAULT;
3715 if (clen < 0)
3716 return -EINVAL;
3717 sr->len = io->msg.iov[0].iov_len;
3718 io->msg.iov = NULL;
3719 } else {
3720 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
3721 &io->msg.iov,
3722 &io->msg.msg.msg_iter);
3723 if (ret < 0)
3724 return ret;
3725 }
3726
3727 return 0;
3728}
Jens Axboe03b12302019-12-02 18:50:25 -07003729#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07003730
3731static int io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
3732{
3733 io->msg.iov = io->msg.fast_iov;
3734
3735#ifdef CONFIG_COMPAT
3736 if (req->ctx->compat)
3737 return __io_compat_recvmsg_copy_hdr(req, io);
3738#endif
3739
3740 return __io_recvmsg_copy_hdr(req, io);
3741}
3742
Jens Axboebcda7ba2020-02-23 16:42:51 -07003743static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
3744 int *cflags, bool needs_lock)
3745{
3746 struct io_sr_msg *sr = &req->sr_msg;
3747 struct io_buffer *kbuf;
3748
3749 if (!(req->flags & REQ_F_BUFFER_SELECT))
3750 return NULL;
3751
3752 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
3753 if (IS_ERR(kbuf))
3754 return kbuf;
3755
3756 sr->kbuf = kbuf;
3757 req->flags |= REQ_F_BUFFER_SELECTED;
3758
3759 *cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
3760 *cflags |= IORING_CQE_F_BUFFER;
3761 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07003762}
3763
Jens Axboe3529d8c2019-12-19 18:24:38 -07003764static int io_recvmsg_prep(struct io_kiocb *req,
3765 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003766{
Jens Axboee47293f2019-12-20 08:58:21 -07003767 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003768 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003769 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07003770
Jens Axboe3529d8c2019-12-19 18:24:38 -07003771 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3772 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003773 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003774 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003775
Jens Axboed8768362020-02-27 14:17:49 -07003776#ifdef CONFIG_COMPAT
3777 if (req->ctx->compat)
3778 sr->msg_flags |= MSG_CMSG_COMPAT;
3779#endif
3780
Jens Axboefddafac2020-01-04 20:19:44 -07003781 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003782 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003783 /* iovec is already imported */
3784 if (req->flags & REQ_F_NEED_CLEANUP)
3785 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003786
Jens Axboe52de1fe2020-02-27 10:15:42 -07003787 ret = io_recvmsg_copy_hdr(req, io);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003788 if (!ret)
3789 req->flags |= REQ_F_NEED_CLEANUP;
3790 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003791}
3792
Pavel Begunkov014db002020-03-03 21:33:12 +03003793static int io_recvmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003794{
Jens Axboe0b416c32019-12-15 10:57:46 -07003795 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003796 struct socket *sock;
Jens Axboe52de1fe2020-02-27 10:15:42 -07003797 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003798
3799 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3800 return -EINVAL;
3801
3802 sock = sock_from_file(req->file, &ret);
3803 if (sock) {
Jens Axboe52de1fe2020-02-27 10:15:42 -07003804 struct io_buffer *kbuf;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003805 struct io_async_ctx io;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003806 unsigned flags;
3807
Jens Axboe03b12302019-12-02 18:50:25 -07003808 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003809 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003810 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003811 /* if iov is set, it's allocated already */
3812 if (!kmsg->iov)
3813 kmsg->iov = kmsg->fast_iov;
3814 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003815 } else {
Jens Axboe0b416c32019-12-15 10:57:46 -07003816 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003817 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003818
Jens Axboe52de1fe2020-02-27 10:15:42 -07003819 ret = io_recvmsg_copy_hdr(req, &io);
Jens Axboe03b12302019-12-02 18:50:25 -07003820 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003821 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003822 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003823
Jens Axboe52de1fe2020-02-27 10:15:42 -07003824 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
3825 if (IS_ERR(kbuf)) {
3826 return PTR_ERR(kbuf);
3827 } else if (kbuf) {
3828 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3829 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
3830 1, req->sr_msg.len);
3831 }
3832
Jens Axboee47293f2019-12-20 08:58:21 -07003833 flags = req->sr_msg.msg_flags;
3834 if (flags & MSG_DONTWAIT)
3835 req->flags |= REQ_F_NOWAIT;
3836 else if (force_nonblock)
3837 flags |= MSG_DONTWAIT;
3838
3839 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3840 kmsg->uaddr, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003841 if (force_nonblock && ret == -EAGAIN)
3842 return io_setup_async_msg(req, kmsg);
Jens Axboe441cdbd2019-12-02 18:49:10 -07003843 if (ret == -ERESTARTSYS)
3844 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003845 }
3846
Pavel Begunkov1e950812020-02-06 19:51:16 +03003847 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003848 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003849 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe52de1fe2020-02-27 10:15:42 -07003850 __io_cqring_add_event(req, ret, cflags);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003851 if (ret < 0)
3852 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003853 io_put_req(req);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003854 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003855}
3856
Pavel Begunkov014db002020-03-03 21:33:12 +03003857static int io_recv(struct io_kiocb *req, bool force_nonblock)
Jens Axboefddafac2020-01-04 20:19:44 -07003858{
Jens Axboebcda7ba2020-02-23 16:42:51 -07003859 struct io_buffer *kbuf = NULL;
Jens Axboefddafac2020-01-04 20:19:44 -07003860 struct socket *sock;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003861 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003862
3863 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3864 return -EINVAL;
3865
3866 sock = sock_from_file(req->file, &ret);
3867 if (sock) {
3868 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003869 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07003870 struct msghdr msg;
3871 struct iovec iov;
3872 unsigned flags;
3873
Jens Axboebcda7ba2020-02-23 16:42:51 -07003874 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
3875 if (IS_ERR(kbuf))
3876 return PTR_ERR(kbuf);
3877 else if (kbuf)
3878 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07003879
Jens Axboebcda7ba2020-02-23 16:42:51 -07003880 ret = import_single_range(READ, buf, sr->len, &iov,
3881 &msg.msg_iter);
3882 if (ret) {
3883 kfree(kbuf);
3884 return ret;
3885 }
3886
3887 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07003888 msg.msg_name = NULL;
3889 msg.msg_control = NULL;
3890 msg.msg_controllen = 0;
3891 msg.msg_namelen = 0;
3892 msg.msg_iocb = NULL;
3893 msg.msg_flags = 0;
3894
3895 flags = req->sr_msg.msg_flags;
3896 if (flags & MSG_DONTWAIT)
3897 req->flags |= REQ_F_NOWAIT;
3898 else if (force_nonblock)
3899 flags |= MSG_DONTWAIT;
3900
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003901 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07003902 if (force_nonblock && ret == -EAGAIN)
3903 return -EAGAIN;
3904 if (ret == -ERESTARTSYS)
3905 ret = -EINTR;
3906 }
3907
Jens Axboebcda7ba2020-02-23 16:42:51 -07003908 kfree(kbuf);
3909 req->flags &= ~REQ_F_NEED_CLEANUP;
3910 __io_cqring_add_event(req, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07003911 if (ret < 0)
3912 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003913 io_put_req(req);
Jens Axboefddafac2020-01-04 20:19:44 -07003914 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003915}
3916
Jens Axboe3529d8c2019-12-19 18:24:38 -07003917static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003918{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003919 struct io_accept *accept = &req->accept;
3920
Jens Axboe17f2fe32019-10-17 14:42:58 -06003921 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3922 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003923 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003924 return -EINVAL;
3925
Jens Axboed55e5f52019-12-11 16:12:15 -07003926 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3927 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003928 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06003929 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003930 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003931}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003932
Pavel Begunkov014db002020-03-03 21:33:12 +03003933static int __io_accept(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003934{
3935 struct io_accept *accept = &req->accept;
3936 unsigned file_flags;
3937 int ret;
3938
3939 file_flags = force_nonblock ? O_NONBLOCK : 0;
3940 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06003941 accept->addr_len, accept->flags,
3942 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003943 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003944 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003945 if (ret == -ERESTARTSYS)
3946 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003947 if (ret < 0)
3948 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003949 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003950 io_put_req(req);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003951 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003952}
3953
3954static void io_accept_finish(struct io_wq_work **workptr)
3955{
3956 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003957
3958 if (io_req_cancelled(req))
3959 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03003960 __io_accept(req, false);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03003961 io_steal_work(req, workptr);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003962}
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003963
Pavel Begunkov014db002020-03-03 21:33:12 +03003964static int io_accept(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003965{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003966 int ret;
3967
Pavel Begunkov014db002020-03-03 21:33:12 +03003968 ret = __io_accept(req, force_nonblock);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003969 if (ret == -EAGAIN && force_nonblock) {
3970 req->work.func = io_accept_finish;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003971 return -EAGAIN;
3972 }
3973 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003974}
3975
Jens Axboe3529d8c2019-12-19 18:24:38 -07003976static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003977{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003978 struct io_connect *conn = &req->connect;
3979 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003980
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003981 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3982 return -EINVAL;
3983 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3984 return -EINVAL;
3985
Jens Axboe3529d8c2019-12-19 18:24:38 -07003986 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3987 conn->addr_len = READ_ONCE(sqe->addr2);
3988
3989 if (!io)
3990 return 0;
3991
3992 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003993 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003994}
3995
Pavel Begunkov014db002020-03-03 21:33:12 +03003996static int io_connect(struct io_kiocb *req, bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003997{
Jens Axboef499a022019-12-02 16:28:46 -07003998 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003999 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004000 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004001
Jens Axboef499a022019-12-02 16:28:46 -07004002 if (req->io) {
4003 io = req->io;
4004 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004005 ret = move_addr_to_kernel(req->connect.addr,
4006 req->connect.addr_len,
4007 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004008 if (ret)
4009 goto out;
4010 io = &__io;
4011 }
4012
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004013 file_flags = force_nonblock ? O_NONBLOCK : 0;
4014
4015 ret = __sys_connect_file(req->file, &io->connect.address,
4016 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004017 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004018 if (req->io)
4019 return -EAGAIN;
4020 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004021 ret = -ENOMEM;
4022 goto out;
4023 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004024 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004025 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004026 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004027 if (ret == -ERESTARTSYS)
4028 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004029out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004030 if (ret < 0)
4031 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004032 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03004033 io_put_req(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004034 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004035}
YueHaibing469956e2020-03-04 15:53:52 +08004036#else /* !CONFIG_NET */
4037static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4038{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004039 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004040}
4041
YueHaibing469956e2020-03-04 15:53:52 +08004042static int io_sendmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004043{
YueHaibing469956e2020-03-04 15:53:52 +08004044 return -EOPNOTSUPP;
4045}
4046
4047static int io_send(struct io_kiocb *req, bool force_nonblock)
4048{
4049 return -EOPNOTSUPP;
4050}
4051
4052static int io_recvmsg_prep(struct io_kiocb *req,
4053 const struct io_uring_sqe *sqe)
4054{
4055 return -EOPNOTSUPP;
4056}
4057
4058static int io_recvmsg(struct io_kiocb *req, bool force_nonblock)
4059{
4060 return -EOPNOTSUPP;
4061}
4062
4063static int io_recv(struct io_kiocb *req, bool force_nonblock)
4064{
4065 return -EOPNOTSUPP;
4066}
4067
4068static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4069{
4070 return -EOPNOTSUPP;
4071}
4072
4073static int io_accept(struct io_kiocb *req, bool force_nonblock)
4074{
4075 return -EOPNOTSUPP;
4076}
4077
4078static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4079{
4080 return -EOPNOTSUPP;
4081}
4082
4083static int io_connect(struct io_kiocb *req, bool force_nonblock)
4084{
4085 return -EOPNOTSUPP;
4086}
4087#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004088
Jens Axboed7718a92020-02-14 22:23:12 -07004089struct io_poll_table {
4090 struct poll_table_struct pt;
4091 struct io_kiocb *req;
4092 int error;
4093};
4094
4095static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
4096 struct wait_queue_head *head)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004097{
Jens Axboed7718a92020-02-14 22:23:12 -07004098 if (unlikely(poll->head)) {
4099 pt->error = -EINVAL;
4100 return;
4101 }
4102
4103 pt->error = 0;
4104 poll->head = head;
4105 add_wait_queue(head, &poll->wait);
4106}
4107
4108static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4109 struct poll_table_struct *p)
4110{
4111 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4112
4113 __io_queue_proc(&pt->req->apoll->poll, pt, head);
4114}
4115
4116static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4117 __poll_t mask, task_work_func_t func)
4118{
4119 struct task_struct *tsk;
Jens Axboeaa96bf82020-04-03 11:26:26 -06004120 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004121
4122 /* for instances that support it check for an event match first: */
4123 if (mask && !(mask & poll->events))
4124 return 0;
4125
4126 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4127
4128 list_del_init(&poll->wait.entry);
4129
4130 tsk = req->task;
4131 req->result = mask;
4132 init_task_work(&req->task_work, func);
4133 /*
Jens Axboeaa96bf82020-04-03 11:26:26 -06004134 * If this fails, then the task is exiting. Punt to one of the io-wq
4135 * threads to ensure the work gets run, we can't always rely on exit
4136 * cancelation taking care of this.
Jens Axboed7718a92020-02-14 22:23:12 -07004137 */
Jens Axboeaa96bf82020-04-03 11:26:26 -06004138 ret = task_work_add(tsk, &req->task_work, true);
4139 if (unlikely(ret)) {
4140 tsk = io_wq_get_task(req->ctx->io_wq);
4141 task_work_add(tsk, &req->task_work, true);
4142 }
Jens Axboed7718a92020-02-14 22:23:12 -07004143 wake_up_process(tsk);
4144 return 1;
4145}
4146
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004147static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4148 __acquires(&req->ctx->completion_lock)
4149{
4150 struct io_ring_ctx *ctx = req->ctx;
4151
4152 if (!req->result && !READ_ONCE(poll->canceled)) {
4153 struct poll_table_struct pt = { ._key = poll->events };
4154
4155 req->result = vfs_poll(req->file, &pt) & poll->events;
4156 }
4157
4158 spin_lock_irq(&ctx->completion_lock);
4159 if (!req->result && !READ_ONCE(poll->canceled)) {
4160 add_wait_queue(poll->head, &poll->wait);
4161 return true;
4162 }
4163
4164 return false;
4165}
4166
Jens Axboed7718a92020-02-14 22:23:12 -07004167static void io_async_task_func(struct callback_head *cb)
4168{
4169 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4170 struct async_poll *apoll = req->apoll;
4171 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2bae0472020-04-13 11:16:34 -06004172 bool canceled;
Jens Axboed7718a92020-02-14 22:23:12 -07004173
4174 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4175
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004176 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07004177 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004178 return;
Jens Axboed7718a92020-02-14 22:23:12 -07004179 }
4180
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004181 if (hash_hashed(&req->hash_node))
4182 hash_del(&req->hash_node);
4183
Jens Axboe2bae0472020-04-13 11:16:34 -06004184 canceled = READ_ONCE(apoll->poll.canceled);
4185 if (canceled) {
4186 io_cqring_fill_event(req, -ECANCELED);
4187 io_commit_cqring(ctx);
4188 }
4189
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004190 spin_unlock_irq(&ctx->completion_lock);
4191
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004192 /* restore ->work in case we need to retry again */
4193 memcpy(&req->work, &apoll->work, sizeof(req->work));
4194
Jens Axboe2bae0472020-04-13 11:16:34 -06004195 if (canceled) {
4196 kfree(apoll);
4197 io_cqring_ev_posted(ctx);
4198 req_set_fail_links(req);
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004199 io_double_put_req(req);
Jens Axboe2bae0472020-04-13 11:16:34 -06004200 return;
4201 }
4202
Jens Axboed7718a92020-02-14 22:23:12 -07004203 __set_current_state(TASK_RUNNING);
4204 mutex_lock(&ctx->uring_lock);
4205 __io_queue_sqe(req, NULL);
4206 mutex_unlock(&ctx->uring_lock);
4207
4208 kfree(apoll);
4209}
4210
4211static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4212 void *key)
4213{
4214 struct io_kiocb *req = wait->private;
4215 struct io_poll_iocb *poll = &req->apoll->poll;
4216
4217 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4218 key_to_poll(key));
4219
4220 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4221}
4222
4223static void io_poll_req_insert(struct io_kiocb *req)
4224{
4225 struct io_ring_ctx *ctx = req->ctx;
4226 struct hlist_head *list;
4227
4228 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
4229 hlist_add_head(&req->hash_node, list);
4230}
4231
4232static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
4233 struct io_poll_iocb *poll,
4234 struct io_poll_table *ipt, __poll_t mask,
4235 wait_queue_func_t wake_func)
4236 __acquires(&ctx->completion_lock)
4237{
4238 struct io_ring_ctx *ctx = req->ctx;
4239 bool cancel = false;
4240
4241 poll->file = req->file;
4242 poll->head = NULL;
4243 poll->done = poll->canceled = false;
4244 poll->events = mask;
4245
4246 ipt->pt._key = mask;
4247 ipt->req = req;
4248 ipt->error = -EINVAL;
4249
4250 INIT_LIST_HEAD(&poll->wait.entry);
4251 init_waitqueue_func_entry(&poll->wait, wake_func);
4252 poll->wait.private = req;
4253
4254 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
4255
4256 spin_lock_irq(&ctx->completion_lock);
4257 if (likely(poll->head)) {
4258 spin_lock(&poll->head->lock);
4259 if (unlikely(list_empty(&poll->wait.entry))) {
4260 if (ipt->error)
4261 cancel = true;
4262 ipt->error = 0;
4263 mask = 0;
4264 }
4265 if (mask || ipt->error)
4266 list_del_init(&poll->wait.entry);
4267 else if (cancel)
4268 WRITE_ONCE(poll->canceled, true);
4269 else if (!poll->done) /* actually waiting for an event */
4270 io_poll_req_insert(req);
4271 spin_unlock(&poll->head->lock);
4272 }
4273
4274 return mask;
4275}
4276
4277static bool io_arm_poll_handler(struct io_kiocb *req)
4278{
4279 const struct io_op_def *def = &io_op_defs[req->opcode];
4280 struct io_ring_ctx *ctx = req->ctx;
4281 struct async_poll *apoll;
4282 struct io_poll_table ipt;
4283 __poll_t mask, ret;
4284
4285 if (!req->file || !file_can_poll(req->file))
4286 return false;
4287 if (req->flags & (REQ_F_MUST_PUNT | REQ_F_POLLED))
4288 return false;
4289 if (!def->pollin && !def->pollout)
4290 return false;
4291
4292 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
4293 if (unlikely(!apoll))
4294 return false;
4295
4296 req->flags |= REQ_F_POLLED;
4297 memcpy(&apoll->work, &req->work, sizeof(req->work));
4298
Jens Axboe3537b6a2020-04-03 11:19:06 -06004299 get_task_struct(current);
Jens Axboed7718a92020-02-14 22:23:12 -07004300 req->task = current;
4301 req->apoll = apoll;
4302 INIT_HLIST_NODE(&req->hash_node);
4303
Nathan Chancellor8755d972020-03-02 16:01:19 -07004304 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07004305 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07004306 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07004307 if (def->pollout)
4308 mask |= POLLOUT | POLLWRNORM;
4309 mask |= POLLERR | POLLPRI;
4310
4311 ipt.pt._qproc = io_async_queue_proc;
4312
4313 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
4314 io_async_wake);
4315 if (ret) {
4316 ipt.error = 0;
4317 apoll->poll.done = true;
4318 spin_unlock_irq(&ctx->completion_lock);
4319 memcpy(&req->work, &apoll->work, sizeof(req->work));
4320 kfree(apoll);
4321 return false;
4322 }
4323 spin_unlock_irq(&ctx->completion_lock);
4324 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
4325 apoll->poll.events);
4326 return true;
4327}
4328
4329static bool __io_poll_remove_one(struct io_kiocb *req,
4330 struct io_poll_iocb *poll)
4331{
Jens Axboeb41e9852020-02-17 09:52:41 -07004332 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004333
4334 spin_lock(&poll->head->lock);
4335 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07004336 if (!list_empty(&poll->wait.entry)) {
4337 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07004338 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004339 }
4340 spin_unlock(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07004341 return do_complete;
4342}
4343
4344static bool io_poll_remove_one(struct io_kiocb *req)
4345{
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08004346 struct async_poll *apoll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07004347 bool do_complete;
4348
4349 if (req->opcode == IORING_OP_POLL_ADD) {
4350 do_complete = __io_poll_remove_one(req, &req->poll);
4351 } else {
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08004352 apoll = req->apoll;
Jens Axboed7718a92020-02-14 22:23:12 -07004353 /* non-poll requests have submit ref still */
4354 do_complete = __io_poll_remove_one(req, &req->apoll->poll);
4355 if (do_complete)
4356 io_put_req(req);
4357 }
4358
Jens Axboe78076bb2019-12-04 19:56:40 -07004359 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07004360
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004361 if (do_complete && apoll) {
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08004362 /*
4363 * restore ->work because we need to call io_req_work_drop_env.
4364 */
4365 memcpy(&req->work, &apoll->work, sizeof(req->work));
4366 kfree(apoll);
4367 }
4368
Jens Axboeb41e9852020-02-17 09:52:41 -07004369 if (do_complete) {
4370 io_cqring_fill_event(req, -ECANCELED);
4371 io_commit_cqring(req->ctx);
4372 req->flags |= REQ_F_COMP_LOCKED;
4373 io_put_req(req);
4374 }
4375
4376 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004377}
4378
4379static void io_poll_remove_all(struct io_ring_ctx *ctx)
4380{
Jens Axboe78076bb2019-12-04 19:56:40 -07004381 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004382 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004383 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004384
4385 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07004386 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
4387 struct hlist_head *list;
4388
4389 list = &ctx->cancel_hash[i];
4390 hlist_for_each_entry_safe(req, tmp, list, hash_node)
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004391 posted += io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004392 }
4393 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07004394
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004395 if (posted)
4396 io_cqring_ev_posted(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004397}
4398
Jens Axboe47f46762019-11-09 17:43:02 -07004399static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
4400{
Jens Axboe78076bb2019-12-04 19:56:40 -07004401 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07004402 struct io_kiocb *req;
4403
Jens Axboe78076bb2019-12-04 19:56:40 -07004404 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
4405 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07004406 if (sqe_addr != req->user_data)
4407 continue;
4408 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07004409 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07004410 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07004411 }
4412
4413 return -ENOENT;
4414}
4415
Jens Axboe3529d8c2019-12-19 18:24:38 -07004416static int io_poll_remove_prep(struct io_kiocb *req,
4417 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004418{
Jens Axboe221c5eb2019-01-17 09:41:58 -07004419 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4420 return -EINVAL;
4421 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
4422 sqe->poll_events)
4423 return -EINVAL;
4424
Jens Axboe0969e782019-12-17 18:40:57 -07004425 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07004426 return 0;
4427}
4428
4429/*
4430 * Find a running poll command that matches one specified in sqe->addr,
4431 * and remove it if found.
4432 */
4433static int io_poll_remove(struct io_kiocb *req)
4434{
4435 struct io_ring_ctx *ctx = req->ctx;
4436 u64 addr;
4437 int ret;
4438
Jens Axboe0969e782019-12-17 18:40:57 -07004439 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004440 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07004441 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004442 spin_unlock_irq(&ctx->completion_lock);
4443
Jens Axboe78e19bb2019-11-06 15:21:34 -07004444 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004445 if (ret < 0)
4446 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004447 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004448 return 0;
4449}
4450
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004451static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004452{
Jackie Liua197f662019-11-08 08:09:12 -07004453 struct io_ring_ctx *ctx = req->ctx;
4454
Jens Axboe8c838782019-03-12 15:48:16 -06004455 req->poll.done = true;
Pavel Begunkovb0a20342020-02-28 10:36:35 +03004456 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06004457 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004458}
4459
Jens Axboeb41e9852020-02-17 09:52:41 -07004460static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004461{
Jens Axboe221c5eb2019-01-17 09:41:58 -07004462 struct io_ring_ctx *ctx = req->ctx;
Jens Axboea6ba6322020-04-03 11:10:14 -06004463 struct io_poll_iocb *poll = &req->poll;
4464
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004465 if (io_poll_rewait(req, poll)) {
Jens Axboea6ba6322020-04-03 11:10:14 -06004466 spin_unlock_irq(&ctx->completion_lock);
4467 return;
4468 }
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004469
Jens Axboe78076bb2019-12-04 19:56:40 -07004470 hash_del(&req->hash_node);
Jens Axboeb41e9852020-02-17 09:52:41 -07004471 io_poll_complete(req, req->result, 0);
4472 req->flags |= REQ_F_COMP_LOCKED;
4473 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004474 spin_unlock_irq(&ctx->completion_lock);
4475
Jens Axboe8c838782019-03-12 15:48:16 -06004476 io_cqring_ev_posted(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004477}
4478
Jens Axboeb41e9852020-02-17 09:52:41 -07004479static void io_poll_task_func(struct callback_head *cb)
Jens Axboee94f1412019-12-19 12:06:02 -07004480{
Jens Axboeb41e9852020-02-17 09:52:41 -07004481 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4482 struct io_kiocb *nxt = NULL;
Jens Axboee94f1412019-12-19 12:06:02 -07004483
Jens Axboeb41e9852020-02-17 09:52:41 -07004484 io_poll_task_handler(req, &nxt);
Jens Axboed7718a92020-02-14 22:23:12 -07004485 if (nxt) {
4486 struct io_ring_ctx *ctx = nxt->ctx;
Jens Axboee94f1412019-12-19 12:06:02 -07004487
Jens Axboed7718a92020-02-14 22:23:12 -07004488 mutex_lock(&ctx->uring_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07004489 __io_queue_sqe(nxt, NULL);
Jens Axboed7718a92020-02-14 22:23:12 -07004490 mutex_unlock(&ctx->uring_lock);
Jens Axboee94f1412019-12-19 12:06:02 -07004491 }
Jens Axboef0b493e2020-02-01 21:30:11 -07004492}
4493
Jens Axboe221c5eb2019-01-17 09:41:58 -07004494static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4495 void *key)
4496{
Jens Axboec2f2eb72020-02-10 09:07:05 -07004497 struct io_kiocb *req = wait->private;
4498 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004499
Jens Axboed7718a92020-02-14 22:23:12 -07004500 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004501}
4502
Jens Axboe221c5eb2019-01-17 09:41:58 -07004503static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
4504 struct poll_table_struct *p)
4505{
4506 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4507
Jens Axboed7718a92020-02-14 22:23:12 -07004508 __io_queue_proc(&pt->req->poll, pt, head);
Jens Axboeeac406c2019-11-14 12:09:58 -07004509}
4510
Jens Axboe3529d8c2019-12-19 18:24:38 -07004511static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004512{
4513 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004514 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004515
4516 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4517 return -EINVAL;
4518 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
4519 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06004520 if (!poll->file)
4521 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004522
Jens Axboe221c5eb2019-01-17 09:41:58 -07004523 events = READ_ONCE(sqe->poll_events);
4524 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboeb41e9852020-02-17 09:52:41 -07004525
Jens Axboe3537b6a2020-04-03 11:19:06 -06004526 get_task_struct(current);
Jens Axboeb41e9852020-02-17 09:52:41 -07004527 req->task = current;
Jens Axboe0969e782019-12-17 18:40:57 -07004528 return 0;
4529}
4530
Pavel Begunkov014db002020-03-03 21:33:12 +03004531static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07004532{
4533 struct io_poll_iocb *poll = &req->poll;
4534 struct io_ring_ctx *ctx = req->ctx;
4535 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07004536 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07004537
Jens Axboe78076bb2019-12-04 19:56:40 -07004538 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe36703242019-07-25 10:20:18 -06004539 INIT_LIST_HEAD(&req->list);
Jens Axboed7718a92020-02-14 22:23:12 -07004540 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06004541
Jens Axboed7718a92020-02-14 22:23:12 -07004542 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
4543 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004544
Jens Axboe8c838782019-03-12 15:48:16 -06004545 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06004546 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004547 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06004548 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07004549 spin_unlock_irq(&ctx->completion_lock);
4550
Jens Axboe8c838782019-03-12 15:48:16 -06004551 if (mask) {
4552 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03004553 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004554 }
Jens Axboe8c838782019-03-12 15:48:16 -06004555 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004556}
4557
Jens Axboe5262f562019-09-17 12:26:57 -06004558static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
4559{
Jens Axboead8a48a2019-11-15 08:49:11 -07004560 struct io_timeout_data *data = container_of(timer,
4561 struct io_timeout_data, timer);
4562 struct io_kiocb *req = data->req;
4563 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06004564 unsigned long flags;
4565
Jens Axboe5262f562019-09-17 12:26:57 -06004566 atomic_inc(&ctx->cq_timeouts);
4567
4568 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08004569 /*
Jens Axboe11365042019-10-16 09:08:32 -06004570 * We could be racing with timeout deletion. If the list is empty,
4571 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08004572 */
Jens Axboe842f9612019-10-29 12:34:10 -06004573 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06004574 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06004575
Jens Axboe11365042019-10-16 09:08:32 -06004576 /*
4577 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08004578 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06004579 * pointer will be increased, otherwise other timeout reqs may
4580 * return in advance without waiting for enough wait_nr.
4581 */
4582 prev = req;
4583 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
4584 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06004585 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06004586 }
Jens Axboe842f9612019-10-29 12:34:10 -06004587
Jens Axboe78e19bb2019-11-06 15:21:34 -07004588 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06004589 io_commit_cqring(ctx);
4590 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4591
4592 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004593 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004594 io_put_req(req);
4595 return HRTIMER_NORESTART;
4596}
4597
Jens Axboe47f46762019-11-09 17:43:02 -07004598static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
4599{
4600 struct io_kiocb *req;
4601 int ret = -ENOENT;
4602
4603 list_for_each_entry(req, &ctx->timeout_list, list) {
4604 if (user_data == req->user_data) {
4605 list_del_init(&req->list);
4606 ret = 0;
4607 break;
4608 }
4609 }
4610
4611 if (ret == -ENOENT)
4612 return ret;
4613
Jens Axboe2d283902019-12-04 11:08:05 -07004614 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07004615 if (ret == -1)
4616 return -EALREADY;
4617
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004618 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004619 io_cqring_fill_event(req, -ECANCELED);
4620 io_put_req(req);
4621 return 0;
4622}
4623
Jens Axboe3529d8c2019-12-19 18:24:38 -07004624static int io_timeout_remove_prep(struct io_kiocb *req,
4625 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07004626{
Jens Axboeb29472e2019-12-17 18:50:29 -07004627 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4628 return -EINVAL;
4629 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
4630 return -EINVAL;
4631
4632 req->timeout.addr = READ_ONCE(sqe->addr);
4633 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
4634 if (req->timeout.flags)
4635 return -EINVAL;
4636
Jens Axboeb29472e2019-12-17 18:50:29 -07004637 return 0;
4638}
4639
Jens Axboe11365042019-10-16 09:08:32 -06004640/*
4641 * Remove or update an existing timeout command
4642 */
Jens Axboefc4df992019-12-10 14:38:45 -07004643static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06004644{
4645 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07004646 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06004647
Jens Axboe11365042019-10-16 09:08:32 -06004648 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07004649 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06004650
Jens Axboe47f46762019-11-09 17:43:02 -07004651 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06004652 io_commit_cqring(ctx);
4653 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004654 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004655 if (ret < 0)
4656 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08004657 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06004658 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06004659}
4660
Jens Axboe3529d8c2019-12-19 18:24:38 -07004661static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07004662 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06004663{
Jens Axboead8a48a2019-11-15 08:49:11 -07004664 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06004665 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06004666
Jens Axboead8a48a2019-11-15 08:49:11 -07004667 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06004668 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07004669 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06004670 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07004671 if (sqe->off && is_timeout_link)
4672 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06004673 flags = READ_ONCE(sqe->timeout_flags);
4674 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06004675 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06004676
Jens Axboe26a61672019-12-20 09:02:01 -07004677 req->timeout.count = READ_ONCE(sqe->off);
4678
Jens Axboe3529d8c2019-12-19 18:24:38 -07004679 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07004680 return -ENOMEM;
4681
4682 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07004683 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07004684 req->flags |= REQ_F_TIMEOUT;
4685
4686 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06004687 return -EFAULT;
4688
Jens Axboe11365042019-10-16 09:08:32 -06004689 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07004690 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06004691 else
Jens Axboead8a48a2019-11-15 08:49:11 -07004692 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06004693
Jens Axboead8a48a2019-11-15 08:49:11 -07004694 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
4695 return 0;
4696}
4697
Jens Axboefc4df992019-12-10 14:38:45 -07004698static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07004699{
Jens Axboead8a48a2019-11-15 08:49:11 -07004700 struct io_ring_ctx *ctx = req->ctx;
4701 struct io_timeout_data *data;
4702 struct list_head *entry;
4703 unsigned span = 0;
Pavel Begunkovb55ce732020-04-15 00:39:49 +03004704 u32 count = req->timeout.count;
Pavel Begunkov22cad152020-04-15 00:39:48 +03004705 u32 seq = req->sequence;
Jens Axboead8a48a2019-11-15 08:49:11 -07004706
Jens Axboe2d283902019-12-04 11:08:05 -07004707 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004708
Jens Axboe5262f562019-09-17 12:26:57 -06004709 /*
4710 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07004711 * timeout event to be satisfied. If it isn't set, then this is
4712 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06004713 */
Jens Axboe93bd25b2019-11-11 23:34:31 -07004714 if (!count) {
4715 req->flags |= REQ_F_TIMEOUT_NOSEQ;
4716 spin_lock_irq(&ctx->completion_lock);
4717 entry = ctx->timeout_list.prev;
4718 goto add;
4719 }
Jens Axboe5262f562019-09-17 12:26:57 -06004720
Pavel Begunkov22cad152020-04-15 00:39:48 +03004721 req->sequence = seq + count;
Jens Axboe5262f562019-09-17 12:26:57 -06004722
4723 /*
4724 * Insertion sort, ensuring the first entry in the list is always
4725 * the one we need first.
4726 */
Jens Axboe5262f562019-09-17 12:26:57 -06004727 spin_lock_irq(&ctx->completion_lock);
4728 list_for_each_prev(entry, &ctx->timeout_list) {
4729 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
Pavel Begunkov22cad152020-04-15 00:39:48 +03004730 unsigned nxt_seq;
yangerkun5da0fb12019-10-15 21:59:29 +08004731 long long tmp, tmp_nxt;
Pavel Begunkovb55ce732020-04-15 00:39:49 +03004732 u32 nxt_offset = nxt->timeout.count;
Jens Axboe5262f562019-09-17 12:26:57 -06004733
Jens Axboe93bd25b2019-11-11 23:34:31 -07004734 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
4735 continue;
4736
yangerkun5da0fb12019-10-15 21:59:29 +08004737 /*
Pavel Begunkov22cad152020-04-15 00:39:48 +03004738 * Since seq + count can overflow, use type long
yangerkun5da0fb12019-10-15 21:59:29 +08004739 * long to store it.
4740 */
Pavel Begunkov22cad152020-04-15 00:39:48 +03004741 tmp = (long long)seq + count;
4742 nxt_seq = nxt->sequence - nxt_offset;
4743 tmp_nxt = (long long)nxt_seq + nxt_offset;
yangerkun5da0fb12019-10-15 21:59:29 +08004744
4745 /*
4746 * cached_sq_head may overflow, and it will never overflow twice
4747 * once there is some timeout req still be valid.
4748 */
Pavel Begunkov22cad152020-04-15 00:39:48 +03004749 if (seq < nxt_seq)
yangerkun8b07a652019-10-17 12:12:35 +08004750 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08004751
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004752 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06004753 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004754
4755 /*
4756 * Sequence of reqs after the insert one and itself should
4757 * be adjusted because each timeout req consumes a slot.
4758 */
4759 span++;
4760 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06004761 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004762 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004763add:
Jens Axboe5262f562019-09-17 12:26:57 -06004764 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07004765 data->timer.function = io_timeout_fn;
4766 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06004767 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004768 return 0;
4769}
4770
Jens Axboe62755e32019-10-28 21:49:21 -06004771static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06004772{
Jens Axboe62755e32019-10-28 21:49:21 -06004773 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06004774
Jens Axboe62755e32019-10-28 21:49:21 -06004775 return req->user_data == (unsigned long) data;
4776}
4777
Jens Axboee977d6d2019-11-05 12:39:45 -07004778static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06004779{
Jens Axboe62755e32019-10-28 21:49:21 -06004780 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06004781 int ret = 0;
4782
Jens Axboe62755e32019-10-28 21:49:21 -06004783 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
4784 switch (cancel_ret) {
4785 case IO_WQ_CANCEL_OK:
4786 ret = 0;
4787 break;
4788 case IO_WQ_CANCEL_RUNNING:
4789 ret = -EALREADY;
4790 break;
4791 case IO_WQ_CANCEL_NOTFOUND:
4792 ret = -ENOENT;
4793 break;
4794 }
4795
Jens Axboee977d6d2019-11-05 12:39:45 -07004796 return ret;
4797}
4798
Jens Axboe47f46762019-11-09 17:43:02 -07004799static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
4800 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03004801 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07004802{
4803 unsigned long flags;
4804 int ret;
4805
4806 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
4807 if (ret != -ENOENT) {
4808 spin_lock_irqsave(&ctx->completion_lock, flags);
4809 goto done;
4810 }
4811
4812 spin_lock_irqsave(&ctx->completion_lock, flags);
4813 ret = io_timeout_cancel(ctx, sqe_addr);
4814 if (ret != -ENOENT)
4815 goto done;
4816 ret = io_poll_cancel(ctx, sqe_addr);
4817done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004818 if (!ret)
4819 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07004820 io_cqring_fill_event(req, ret);
4821 io_commit_cqring(ctx);
4822 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4823 io_cqring_ev_posted(ctx);
4824
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004825 if (ret < 0)
4826 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03004827 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004828}
4829
Jens Axboe3529d8c2019-12-19 18:24:38 -07004830static int io_async_cancel_prep(struct io_kiocb *req,
4831 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07004832{
Jens Axboefbf23842019-12-17 18:45:56 -07004833 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07004834 return -EINVAL;
4835 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
4836 sqe->cancel_flags)
4837 return -EINVAL;
4838
Jens Axboefbf23842019-12-17 18:45:56 -07004839 req->cancel.addr = READ_ONCE(sqe->addr);
4840 return 0;
4841}
4842
Pavel Begunkov014db002020-03-03 21:33:12 +03004843static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07004844{
4845 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07004846
Pavel Begunkov014db002020-03-03 21:33:12 +03004847 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06004848 return 0;
4849}
4850
Jens Axboe05f3fb32019-12-09 11:22:50 -07004851static int io_files_update_prep(struct io_kiocb *req,
4852 const struct io_uring_sqe *sqe)
4853{
4854 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
4855 return -EINVAL;
4856
4857 req->files_update.offset = READ_ONCE(sqe->off);
4858 req->files_update.nr_args = READ_ONCE(sqe->len);
4859 if (!req->files_update.nr_args)
4860 return -EINVAL;
4861 req->files_update.arg = READ_ONCE(sqe->addr);
4862 return 0;
4863}
4864
4865static int io_files_update(struct io_kiocb *req, bool force_nonblock)
4866{
4867 struct io_ring_ctx *ctx = req->ctx;
4868 struct io_uring_files_update up;
4869 int ret;
4870
Jens Axboef86cd202020-01-29 13:46:44 -07004871 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004872 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004873
4874 up.offset = req->files_update.offset;
4875 up.fds = req->files_update.arg;
4876
4877 mutex_lock(&ctx->uring_lock);
4878 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
4879 mutex_unlock(&ctx->uring_lock);
4880
4881 if (ret < 0)
4882 req_set_fail_links(req);
4883 io_cqring_add_event(req, ret);
4884 io_put_req(req);
4885 return 0;
4886}
4887
Jens Axboe3529d8c2019-12-19 18:24:38 -07004888static int io_req_defer_prep(struct io_kiocb *req,
4889 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07004890{
Jens Axboee7815732019-12-17 19:45:06 -07004891 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07004892
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03004893 if (!sqe)
4894 return 0;
4895
Jens Axboef86cd202020-01-29 13:46:44 -07004896 if (io_op_defs[req->opcode].file_table) {
4897 ret = io_grab_files(req);
4898 if (unlikely(ret))
4899 return ret;
4900 }
4901
Jens Axboecccf0ee2020-01-27 16:34:48 -07004902 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
4903
Jens Axboed625c6e2019-12-17 19:53:05 -07004904 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07004905 case IORING_OP_NOP:
4906 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004907 case IORING_OP_READV:
4908 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004909 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004910 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004911 break;
4912 case IORING_OP_WRITEV:
4913 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004914 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004915 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004916 break;
Jens Axboe0969e782019-12-17 18:40:57 -07004917 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004918 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004919 break;
4920 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004921 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004922 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004923 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004924 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004925 break;
4926 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004927 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004928 break;
Jens Axboe03b12302019-12-02 18:50:25 -07004929 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004930 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004931 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004932 break;
4933 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004934 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004935 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004936 break;
Jens Axboef499a022019-12-02 16:28:46 -07004937 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004938 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004939 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004940 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004941 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004942 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004943 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004944 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004945 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004946 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004947 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004948 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004949 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004950 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004951 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004952 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004953 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004954 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004955 case IORING_OP_FALLOCATE:
4956 ret = io_fallocate_prep(req, sqe);
4957 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004958 case IORING_OP_OPENAT:
4959 ret = io_openat_prep(req, sqe);
4960 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004961 case IORING_OP_CLOSE:
4962 ret = io_close_prep(req, sqe);
4963 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004964 case IORING_OP_FILES_UPDATE:
4965 ret = io_files_update_prep(req, sqe);
4966 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004967 case IORING_OP_STATX:
4968 ret = io_statx_prep(req, sqe);
4969 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004970 case IORING_OP_FADVISE:
4971 ret = io_fadvise_prep(req, sqe);
4972 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004973 case IORING_OP_MADVISE:
4974 ret = io_madvise_prep(req, sqe);
4975 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004976 case IORING_OP_OPENAT2:
4977 ret = io_openat2_prep(req, sqe);
4978 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004979 case IORING_OP_EPOLL_CTL:
4980 ret = io_epoll_ctl_prep(req, sqe);
4981 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004982 case IORING_OP_SPLICE:
4983 ret = io_splice_prep(req, sqe);
4984 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004985 case IORING_OP_PROVIDE_BUFFERS:
4986 ret = io_provide_buffers_prep(req, sqe);
4987 break;
Jens Axboe067524e2020-03-02 16:32:28 -07004988 case IORING_OP_REMOVE_BUFFERS:
4989 ret = io_remove_buffers_prep(req, sqe);
4990 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004991 default:
Jens Axboee7815732019-12-17 19:45:06 -07004992 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
4993 req->opcode);
4994 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004995 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004996 }
4997
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004998 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07004999}
5000
Jens Axboe3529d8c2019-12-19 18:24:38 -07005001static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06005002{
Jackie Liua197f662019-11-08 08:09:12 -07005003 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07005004 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06005005
Bob Liu9d858b22019-11-13 18:06:25 +08005006 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov4ee36312020-05-01 17:09:37 +03005007 if (!req_need_defer(req) && list_empty_careful(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06005008 return 0;
5009
Jens Axboe3529d8c2019-12-19 18:24:38 -07005010 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06005011 return -EAGAIN;
5012
Jens Axboe3529d8c2019-12-19 18:24:38 -07005013 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005014 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07005015 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07005016
Jens Axboede0617e2019-04-06 21:51:27 -06005017 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08005018 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06005019 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06005020 return 0;
5021 }
5022
Jens Axboe915967f2019-11-21 09:01:20 -07005023 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06005024 list_add_tail(&req->list, &ctx->defer_list);
5025 spin_unlock_irq(&ctx->completion_lock);
5026 return -EIOCBQUEUED;
5027}
5028
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005029static void io_cleanup_req(struct io_kiocb *req)
5030{
5031 struct io_async_ctx *io = req->io;
5032
5033 switch (req->opcode) {
5034 case IORING_OP_READV:
5035 case IORING_OP_READ_FIXED:
5036 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005037 if (req->flags & REQ_F_BUFFER_SELECTED)
5038 kfree((void *)(unsigned long)req->rw.addr);
5039 /* fallthrough */
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005040 case IORING_OP_WRITEV:
5041 case IORING_OP_WRITE_FIXED:
5042 case IORING_OP_WRITE:
5043 if (io->rw.iov != io->rw.fast_iov)
5044 kfree(io->rw.iov);
5045 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005046 case IORING_OP_RECVMSG:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005047 if (req->flags & REQ_F_BUFFER_SELECTED)
5048 kfree(req->sr_msg.kbuf);
5049 /* fallthrough */
5050 case IORING_OP_SENDMSG:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005051 if (io->msg.iov != io->msg.fast_iov)
5052 kfree(io->msg.iov);
5053 break;
Jens Axboebcda7ba2020-02-23 16:42:51 -07005054 case IORING_OP_RECV:
5055 if (req->flags & REQ_F_BUFFER_SELECTED)
5056 kfree(req->sr_msg.kbuf);
5057 break;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03005058 case IORING_OP_OPENAT:
5059 case IORING_OP_OPENAT2:
5060 case IORING_OP_STATX:
5061 putname(req->open.filename);
5062 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005063 case IORING_OP_SPLICE:
5064 io_put_file(req, req->splice.file_in,
5065 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5066 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005067 }
5068
5069 req->flags &= ~REQ_F_NEED_CLEANUP;
5070}
5071
Jens Axboe3529d8c2019-12-19 18:24:38 -07005072static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Pavel Begunkov014db002020-03-03 21:33:12 +03005073 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005074{
Jackie Liua197f662019-11-08 08:09:12 -07005075 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005076 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005077
Jens Axboed625c6e2019-12-17 19:53:05 -07005078 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005079 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005080 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005081 break;
5082 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005083 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005084 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005085 if (sqe) {
5086 ret = io_read_prep(req, sqe, force_nonblock);
5087 if (ret < 0)
5088 break;
5089 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005090 ret = io_read(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005091 break;
5092 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07005093 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005094 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005095 if (sqe) {
5096 ret = io_write_prep(req, sqe, force_nonblock);
5097 if (ret < 0)
5098 break;
5099 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005100 ret = io_write(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005101 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005102 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005103 if (sqe) {
5104 ret = io_prep_fsync(req, sqe);
5105 if (ret < 0)
5106 break;
5107 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005108 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005109 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005110 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005111 if (sqe) {
5112 ret = io_poll_add_prep(req, sqe);
5113 if (ret)
5114 break;
5115 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005116 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005117 break;
5118 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005119 if (sqe) {
5120 ret = io_poll_remove_prep(req, sqe);
5121 if (ret < 0)
5122 break;
5123 }
Jens Axboefc4df992019-12-10 14:38:45 -07005124 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005125 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005126 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005127 if (sqe) {
5128 ret = io_prep_sfr(req, sqe);
5129 if (ret < 0)
5130 break;
5131 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005132 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005133 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005134 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005135 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005136 if (sqe) {
5137 ret = io_sendmsg_prep(req, sqe);
5138 if (ret < 0)
5139 break;
5140 }
Jens Axboefddafac2020-01-04 20:19:44 -07005141 if (req->opcode == IORING_OP_SENDMSG)
Pavel Begunkov014db002020-03-03 21:33:12 +03005142 ret = io_sendmsg(req, force_nonblock);
Jens Axboefddafac2020-01-04 20:19:44 -07005143 else
Pavel Begunkov014db002020-03-03 21:33:12 +03005144 ret = io_send(req, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005145 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06005146 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005147 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005148 if (sqe) {
5149 ret = io_recvmsg_prep(req, sqe);
5150 if (ret)
5151 break;
5152 }
Jens Axboefddafac2020-01-04 20:19:44 -07005153 if (req->opcode == IORING_OP_RECVMSG)
Pavel Begunkov014db002020-03-03 21:33:12 +03005154 ret = io_recvmsg(req, force_nonblock);
Jens Axboefddafac2020-01-04 20:19:44 -07005155 else
Pavel Begunkov014db002020-03-03 21:33:12 +03005156 ret = io_recv(req, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06005157 break;
Jens Axboe5262f562019-09-17 12:26:57 -06005158 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005159 if (sqe) {
5160 ret = io_timeout_prep(req, sqe, false);
5161 if (ret)
5162 break;
5163 }
Jens Axboefc4df992019-12-10 14:38:45 -07005164 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005165 break;
Jens Axboe11365042019-10-16 09:08:32 -06005166 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005167 if (sqe) {
5168 ret = io_timeout_remove_prep(req, sqe);
5169 if (ret)
5170 break;
5171 }
Jens Axboefc4df992019-12-10 14:38:45 -07005172 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06005173 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06005174 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005175 if (sqe) {
5176 ret = io_accept_prep(req, sqe);
5177 if (ret)
5178 break;
5179 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005180 ret = io_accept(req, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005181 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005182 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005183 if (sqe) {
5184 ret = io_connect_prep(req, sqe);
5185 if (ret)
5186 break;
5187 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005188 ret = io_connect(req, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005189 break;
Jens Axboe62755e32019-10-28 21:49:21 -06005190 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005191 if (sqe) {
5192 ret = io_async_cancel_prep(req, sqe);
5193 if (ret)
5194 break;
5195 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005196 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005197 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005198 case IORING_OP_FALLOCATE:
5199 if (sqe) {
5200 ret = io_fallocate_prep(req, sqe);
5201 if (ret)
5202 break;
5203 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005204 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005205 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005206 case IORING_OP_OPENAT:
5207 if (sqe) {
5208 ret = io_openat_prep(req, sqe);
5209 if (ret)
5210 break;
5211 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005212 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005213 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005214 case IORING_OP_CLOSE:
5215 if (sqe) {
5216 ret = io_close_prep(req, sqe);
5217 if (ret)
5218 break;
5219 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005220 ret = io_close(req, force_nonblock);
Jens Axboeb5dba592019-12-11 14:02:38 -07005221 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005222 case IORING_OP_FILES_UPDATE:
5223 if (sqe) {
5224 ret = io_files_update_prep(req, sqe);
5225 if (ret)
5226 break;
5227 }
5228 ret = io_files_update(req, force_nonblock);
5229 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005230 case IORING_OP_STATX:
5231 if (sqe) {
5232 ret = io_statx_prep(req, sqe);
5233 if (ret)
5234 break;
5235 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005236 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005237 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005238 case IORING_OP_FADVISE:
5239 if (sqe) {
5240 ret = io_fadvise_prep(req, sqe);
5241 if (ret)
5242 break;
5243 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005244 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005245 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005246 case IORING_OP_MADVISE:
5247 if (sqe) {
5248 ret = io_madvise_prep(req, sqe);
5249 if (ret)
5250 break;
5251 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005252 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005253 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005254 case IORING_OP_OPENAT2:
5255 if (sqe) {
5256 ret = io_openat2_prep(req, sqe);
5257 if (ret)
5258 break;
5259 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005260 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005261 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005262 case IORING_OP_EPOLL_CTL:
5263 if (sqe) {
5264 ret = io_epoll_ctl_prep(req, sqe);
5265 if (ret)
5266 break;
5267 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005268 ret = io_epoll_ctl(req, force_nonblock);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005269 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005270 case IORING_OP_SPLICE:
5271 if (sqe) {
5272 ret = io_splice_prep(req, sqe);
5273 if (ret < 0)
5274 break;
5275 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005276 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005277 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005278 case IORING_OP_PROVIDE_BUFFERS:
5279 if (sqe) {
5280 ret = io_provide_buffers_prep(req, sqe);
5281 if (ret)
5282 break;
5283 }
5284 ret = io_provide_buffers(req, force_nonblock);
5285 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005286 case IORING_OP_REMOVE_BUFFERS:
5287 if (sqe) {
5288 ret = io_remove_buffers_prep(req, sqe);
5289 if (ret)
5290 break;
5291 }
5292 ret = io_remove_buffers(req, force_nonblock);
Jens Axboe31b51512019-01-18 22:56:34 -07005293 break;
5294 default:
5295 ret = -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07005296 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005297 }
5298
Jens Axboe31b51512019-01-18 22:56:34 -07005299 if (ret)
5300 return ret;
5301
5302 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07005303 const bool in_async = io_wq_current_is_worker();
5304
Jens Axboe9e645e112019-05-10 16:07:28 -06005305 if (req->result == -EAGAIN)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005306 return -EAGAIN;
5307
Jens Axboe11ba8202020-01-15 21:51:17 -07005308 /* workqueue context doesn't hold uring_lock, grab it now */
5309 if (in_async)
5310 mutex_lock(&ctx->uring_lock);
5311
Jens Axboe2b188cc2019-01-07 10:46:33 -07005312 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07005313
5314 if (in_async)
5315 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005316 }
5317
5318 return 0;
5319}
5320
Jens Axboe561fb042019-10-24 07:25:42 -06005321static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005322{
Jens Axboe561fb042019-10-24 07:25:42 -06005323 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005324 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06005325 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005326
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005327 /* if NO_CANCEL is set, we must still run the work */
5328 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
5329 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06005330 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005331 }
Jens Axboe31b51512019-01-18 22:56:34 -07005332
Jens Axboe561fb042019-10-24 07:25:42 -06005333 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06005334 do {
Pavel Begunkov014db002020-03-03 21:33:12 +03005335 ret = io_issue_sqe(req, NULL, false);
Jens Axboe561fb042019-10-24 07:25:42 -06005336 /*
5337 * We can get EAGAIN for polled IO even though we're
5338 * forcing a sync submission from here, since we can't
5339 * wait for request slots on the block side.
5340 */
5341 if (ret != -EAGAIN)
5342 break;
5343 cond_resched();
5344 } while (1);
5345 }
Jens Axboe31b51512019-01-18 22:56:34 -07005346
Jens Axboe561fb042019-10-24 07:25:42 -06005347 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005348 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07005349 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06005350 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07005351 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005352
Pavel Begunkove9fd9392020-03-04 16:14:12 +03005353 io_steal_work(req, workptr);
Jens Axboe31b51512019-01-18 22:56:34 -07005354}
Jens Axboe2b188cc2019-01-07 10:46:33 -07005355
Jens Axboe65e19f52019-10-26 07:20:21 -06005356static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
5357 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06005358{
Jens Axboe65e19f52019-10-26 07:20:21 -06005359 struct fixed_file_table *table;
5360
Jens Axboe05f3fb32019-12-09 11:22:50 -07005361 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08005362 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06005363}
5364
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005365static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
5366 int fd, struct file **out_file, bool fixed)
5367{
5368 struct io_ring_ctx *ctx = req->ctx;
5369 struct file *file;
5370
5371 if (fixed) {
5372 if (unlikely(!ctx->file_data ||
5373 (unsigned) fd >= ctx->nr_user_files))
5374 return -EBADF;
5375 fd = array_index_nospec(fd, ctx->nr_user_files);
5376 file = io_file_from_index(ctx, fd);
5377 if (!file)
5378 return -EBADF;
Xiaoguang Wang05589552020-03-31 14:05:18 +08005379 req->fixed_file_refs = ctx->file_data->cur_refs;
5380 percpu_ref_get(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005381 } else {
5382 trace_io_uring_file_get(ctx, fd);
5383 file = __io_file_get(state, fd);
5384 if (unlikely(!file))
5385 return -EBADF;
5386 }
5387
5388 *out_file = file;
5389 return 0;
5390}
5391
Jens Axboe3529d8c2019-12-19 18:24:38 -07005392static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Jens Axboe63ff8222020-05-07 14:56:15 -06005393 int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06005394{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005395 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06005396
Jens Axboe63ff8222020-05-07 14:56:15 -06005397 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005398 if (unlikely(!fixed && req->needs_fixed_file))
5399 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06005400
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005401 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06005402}
5403
Jackie Liua197f662019-11-08 08:09:12 -07005404static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005405{
Jens Axboefcb323c2019-10-24 12:39:47 -06005406 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07005407 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06005408
Jens Axboe5b0bbee2020-04-27 10:41:22 -06005409 if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE))
Jens Axboef86cd202020-01-29 13:46:44 -07005410 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005411 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07005412 return -EBADF;
5413
Jens Axboefcb323c2019-10-24 12:39:47 -06005414 rcu_read_lock();
5415 spin_lock_irq(&ctx->inflight_lock);
5416 /*
5417 * We use the f_ops->flush() handler to ensure that we can flush
5418 * out work accessing these files if the fd is closed. Check if
5419 * the fd has changed since we started down this path, and disallow
5420 * this operation if it has.
5421 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005422 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06005423 list_add(&req->inflight_entry, &ctx->inflight_list);
5424 req->flags |= REQ_F_INFLIGHT;
5425 req->work.files = current->files;
5426 ret = 0;
5427 }
5428 spin_unlock_irq(&ctx->inflight_lock);
5429 rcu_read_unlock();
5430
5431 return ret;
5432}
5433
Jens Axboe2665abf2019-11-05 12:40:47 -07005434static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
5435{
Jens Axboead8a48a2019-11-15 08:49:11 -07005436 struct io_timeout_data *data = container_of(timer,
5437 struct io_timeout_data, timer);
5438 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07005439 struct io_ring_ctx *ctx = req->ctx;
5440 struct io_kiocb *prev = NULL;
5441 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07005442
5443 spin_lock_irqsave(&ctx->completion_lock, flags);
5444
5445 /*
5446 * We don't expect the list to be empty, that will only happen if we
5447 * race with the completion of the linked work.
5448 */
Pavel Begunkov44932332019-12-05 16:16:35 +03005449 if (!list_empty(&req->link_list)) {
5450 prev = list_entry(req->link_list.prev, struct io_kiocb,
5451 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005452 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03005453 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005454 prev->flags &= ~REQ_F_LINK_TIMEOUT;
5455 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07005456 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005457 }
5458
5459 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5460
5461 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005462 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03005463 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07005464 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07005465 } else {
5466 io_cqring_add_event(req, -ETIME);
5467 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005468 }
Jens Axboe2665abf2019-11-05 12:40:47 -07005469 return HRTIMER_NORESTART;
5470}
5471
Jens Axboead8a48a2019-11-15 08:49:11 -07005472static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005473{
Jens Axboe76a46e02019-11-10 23:34:16 -07005474 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07005475
Jens Axboe76a46e02019-11-10 23:34:16 -07005476 /*
5477 * If the list is now empty, then our linked request finished before
5478 * we got a chance to setup the timer
5479 */
5480 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03005481 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07005482 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07005483
Jens Axboead8a48a2019-11-15 08:49:11 -07005484 data->timer.function = io_link_timeout_fn;
5485 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
5486 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07005487 }
Jens Axboe76a46e02019-11-10 23:34:16 -07005488 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07005489
Jens Axboe2665abf2019-11-05 12:40:47 -07005490 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07005491 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005492}
5493
Jens Axboead8a48a2019-11-15 08:49:11 -07005494static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005495{
5496 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005497
Pavel Begunkovdea3b492020-04-12 02:05:04 +03005498 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07005499 return NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005500 /* for polled retry, if flag is set, we already went through here */
5501 if (req->flags & REQ_F_POLLED)
5502 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005503
Pavel Begunkov44932332019-12-05 16:16:35 +03005504 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
5505 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07005506 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07005507 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005508
Jens Axboe76a46e02019-11-10 23:34:16 -07005509 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07005510 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07005511}
5512
Jens Axboe3529d8c2019-12-19 18:24:38 -07005513static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005514{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005515 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005516 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07005517 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005518 int ret;
5519
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005520again:
5521 linked_timeout = io_prep_linked_timeout(req);
5522
Jens Axboe193155c2020-02-22 23:22:19 -07005523 if (req->work.creds && req->work.creds != current_cred()) {
5524 if (old_creds)
5525 revert_creds(old_creds);
5526 if (old_creds == req->work.creds)
5527 old_creds = NULL; /* restored original creds */
5528 else
5529 old_creds = override_creds(req->work.creds);
5530 }
5531
Pavel Begunkov014db002020-03-03 21:33:12 +03005532 ret = io_issue_sqe(req, sqe, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06005533
5534 /*
5535 * We async punt it if the file wasn't marked NOWAIT, or if the file
5536 * doesn't support non-blocking read/write attempts
5537 */
5538 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
5539 (req->flags & REQ_F_MUST_PUNT))) {
Jens Axboed7718a92020-02-14 22:23:12 -07005540 if (io_arm_poll_handler(req)) {
5541 if (linked_timeout)
5542 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005543 goto exit;
Jens Axboed7718a92020-02-14 22:23:12 -07005544 }
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005545punt:
Jens Axboef86cd202020-01-29 13:46:44 -07005546 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005547 ret = io_grab_files(req);
5548 if (ret)
5549 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005550 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005551
5552 /*
5553 * Queued up for async execution, worker will release
5554 * submit reference when the iocb is actually submitted.
5555 */
5556 io_queue_async_work(req);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005557 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005558 }
Jens Axboee65ef562019-03-12 10:16:44 -06005559
Jens Axboefcb323c2019-10-24 12:39:47 -06005560err:
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005561 nxt = NULL;
Jens Axboee65ef562019-03-12 10:16:44 -06005562 /* drop submission reference */
Jens Axboe2a44f462020-02-25 13:25:41 -07005563 io_put_req_find_next(req, &nxt);
Jens Axboee65ef562019-03-12 10:16:44 -06005564
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005565 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07005566 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005567 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005568 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005569 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005570 }
5571
Jens Axboee65ef562019-03-12 10:16:44 -06005572 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06005573 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07005574 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005575 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06005576 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06005577 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005578 if (nxt) {
5579 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005580
5581 if (req->flags & REQ_F_FORCE_ASYNC)
5582 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005583 goto again;
5584 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005585exit:
Jens Axboe193155c2020-02-22 23:22:19 -07005586 if (old_creds)
5587 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005588}
5589
Jens Axboe3529d8c2019-12-19 18:24:38 -07005590static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005591{
5592 int ret;
5593
Jens Axboe3529d8c2019-12-19 18:24:38 -07005594 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005595 if (ret) {
5596 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03005597fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005598 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005599 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07005600 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005601 }
Pavel Begunkov25508782019-12-30 21:24:47 +03005602 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkov11185912020-01-22 23:09:35 +03005603 ret = io_req_defer_prep(req, sqe);
5604 if (unlikely(ret < 0))
5605 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07005606 /*
5607 * Never try inline submit of IOSQE_ASYNC is set, go straight
5608 * to async execution.
5609 */
5610 req->work.flags |= IO_WQ_WORK_CONCURRENT;
5611 io_queue_async_work(req);
5612 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005613 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07005614 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08005615}
5616
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005617static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005618{
Jens Axboe94ae5e72019-11-14 19:39:52 -07005619 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005620 io_cqring_add_event(req, -ECANCELED);
5621 io_double_put_req(req);
5622 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07005623 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005624}
5625
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005626static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Xiaoguang Wang7d01bd72020-05-08 21:19:30 +08005627 struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06005628{
Jackie Liua197f662019-11-08 08:09:12 -07005629 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005630 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06005631
Jens Axboe9e645e112019-05-10 16:07:28 -06005632 /*
5633 * If we already have a head request, queue this one for async
5634 * submittal once the head completes. If we don't have a head but
5635 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
5636 * submitted sync once the chain is complete. If none of those
5637 * conditions are true (normal request), then just queue it.
5638 */
5639 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03005640 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06005641
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03005642 /*
5643 * Taking sequential execution of a link, draining both sides
5644 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
5645 * requests in the link. So, it drains the head and the
5646 * next after the link request. The last one is done via
5647 * drain_next flag to persist the effect across calls.
5648 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005649 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03005650 head->flags |= REQ_F_IO_DRAIN;
5651 ctx->drain_next = 1;
5652 }
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005653 if (io_alloc_async_ctx(req))
5654 return -EAGAIN;
Jens Axboe9e645e112019-05-10 16:07:28 -06005655
Jens Axboe3529d8c2019-12-19 18:24:38 -07005656 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005657 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005658 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03005659 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005660 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07005661 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03005662 trace_io_uring_link(ctx, req, head);
5663 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06005664
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005665 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005666 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005667 io_queue_link_head(head);
5668 *link = NULL;
5669 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005670 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03005671 if (unlikely(ctx->drain_next)) {
5672 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005673 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03005674 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005675 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03005676 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03005677 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03005678
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005679 if (io_alloc_async_ctx(req))
5680 return -EAGAIN;
5681
Pavel Begunkov711be032020-01-17 03:57:59 +03005682 ret = io_req_defer_prep(req, sqe);
5683 if (ret)
5684 req->flags |= REQ_F_FAIL_LINK;
5685 *link = req;
5686 } else {
5687 io_queue_sqe(req, sqe);
5688 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005689 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005690
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005691 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06005692}
5693
Jens Axboe9a56a232019-01-09 09:06:50 -07005694/*
5695 * Batched submission is done, ensure local IO is flushed out.
5696 */
5697static void io_submit_state_end(struct io_submit_state *state)
5698{
5699 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06005700 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07005701 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03005702 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07005703}
5704
5705/*
5706 * Start submission side cache.
5707 */
5708static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08005709 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07005710{
5711 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07005712 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07005713 state->file = NULL;
5714 state->ios_left = max_ios;
5715}
5716
Jens Axboe2b188cc2019-01-07 10:46:33 -07005717static void io_commit_sqring(struct io_ring_ctx *ctx)
5718{
Hristo Venev75b28af2019-08-26 17:23:46 +00005719 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005720
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03005721 /*
5722 * Ensure any loads from the SQEs are done at this point,
5723 * since once we write the new head, the application could
5724 * write new data to them.
5725 */
5726 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005727}
5728
5729/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07005730 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07005731 * that is mapped by userspace. This means that care needs to be taken to
5732 * ensure that reads are stable, as we cannot rely on userspace always
5733 * being a good citizen. If members of the sqe are validated and then later
5734 * used, it's important that those reads are done through READ_ONCE() to
5735 * prevent a re-load down the line.
5736 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03005737static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005738{
Hristo Venev75b28af2019-08-26 17:23:46 +00005739 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005740 unsigned head;
5741
5742 /*
5743 * The cached sq head (or cq tail) serves two purposes:
5744 *
5745 * 1) allows us to batch the cost of updating the user visible
5746 * head updates.
5747 * 2) allows the kernel side to track the head on its own, even
5748 * though the application is the one updating it.
5749 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005750 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03005751 if (likely(head < ctx->sq_entries))
5752 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07005753
5754 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06005755 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005756 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03005757 return NULL;
5758}
5759
5760static inline void io_consume_sqe(struct io_ring_ctx *ctx)
5761{
5762 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005763}
5764
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005765#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
5766 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
5767 IOSQE_BUFFER_SELECT)
5768
5769static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
5770 const struct io_uring_sqe *sqe,
5771 struct io_submit_state *state, bool async)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005772{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005773 unsigned int sqe_flags;
Jens Axboe63ff8222020-05-07 14:56:15 -06005774 int id;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005775
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005776 /*
5777 * All io need record the previous position, if LINK vs DARIN,
5778 * it can be used to mark the position of the first IO in the
5779 * link list.
5780 */
Pavel Begunkov31af27c2020-04-15 00:39:50 +03005781 req->sequence = ctx->cached_sq_head - ctx->cached_sq_dropped;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005782 req->opcode = READ_ONCE(sqe->opcode);
5783 req->user_data = READ_ONCE(sqe->user_data);
5784 req->io = NULL;
5785 req->file = NULL;
5786 req->ctx = ctx;
5787 req->flags = 0;
5788 /* one is dropped after submission, the other at completion */
5789 refcount_set(&req->refs, 2);
5790 req->task = NULL;
5791 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005792 req->needs_fixed_file = async;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005793 INIT_IO_WORK(&req->work, io_wq_submit_work);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005794
5795 if (unlikely(req->opcode >= IORING_OP_LAST))
5796 return -EINVAL;
5797
5798 if (io_op_defs[req->opcode].needs_mm && !current->mm) {
5799 if (unlikely(!mmget_not_zero(ctx->sqo_mm)))
5800 return -EFAULT;
5801 use_mm(ctx->sqo_mm);
5802 }
5803
5804 sqe_flags = READ_ONCE(sqe->flags);
5805 /* enforce forwards compatibility on users */
5806 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
5807 return -EINVAL;
5808
5809 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
5810 !io_op_defs[req->opcode].buffer_select)
5811 return -EOPNOTSUPP;
5812
5813 id = READ_ONCE(sqe->personality);
5814 if (id) {
5815 req->work.creds = idr_find(&ctx->personality_idr, id);
5816 if (unlikely(!req->work.creds))
5817 return -EINVAL;
5818 get_cred(req->work.creds);
5819 }
5820
5821 /* same numerical values with corresponding REQ_F_*, safe to copy */
5822 req->flags |= sqe_flags & (IOSQE_IO_DRAIN | IOSQE_IO_HARDLINK |
5823 IOSQE_ASYNC | IOSQE_FIXED_FILE |
5824 IOSQE_BUFFER_SELECT | IOSQE_IO_LINK);
5825
Jens Axboe63ff8222020-05-07 14:56:15 -06005826 if (!io_op_defs[req->opcode].needs_file)
5827 return 0;
5828
5829 return io_req_set_file(state, req, READ_ONCE(sqe->fd));
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005830}
5831
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005832static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03005833 struct file *ring_file, int ring_fd, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07005834{
5835 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005836 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005837 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005838
Jens Axboec4a2ed72019-11-21 21:01:26 -07005839 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07005840 if (test_bit(0, &ctx->sq_check_overflow)) {
5841 if (!list_empty(&ctx->cq_overflow_list) &&
5842 !io_cqring_overflow_flush(ctx, false))
5843 return -EBUSY;
5844 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005845
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005846 /* make sure SQ entry isn't read before tail */
5847 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03005848
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005849 if (!percpu_ref_tryget_many(&ctx->refs, nr))
5850 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005851
5852 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08005853 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005854 statep = &state;
5855 }
5856
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005857 ctx->ring_fd = ring_fd;
5858 ctx->ring_file = ring_file;
5859
Jens Axboe6c271ce2019-01-10 11:22:30 -07005860 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005861 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03005862 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005863 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005864
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03005865 sqe = io_get_sqe(ctx);
5866 if (unlikely(!sqe)) {
5867 io_consume_sqe(ctx);
5868 break;
5869 }
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005870 req = io_alloc_req(ctx, statep);
Pavel Begunkov196be952019-11-07 01:41:06 +03005871 if (unlikely(!req)) {
5872 if (!submitted)
5873 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005874 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06005875 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005876
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005877 err = io_init_req(ctx, req, sqe, statep, async);
Pavel Begunkov709b3022020-04-08 08:58:43 +03005878 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07005879 /* will complete beyond this point, count as submitted */
5880 submitted++;
5881
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005882 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005883fail_req:
5884 io_cqring_add_event(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07005885 io_double_put_req(req);
5886 break;
5887 }
5888
Jens Axboe354420f2020-01-08 18:55:15 -07005889 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
5890 true, async);
Xiaoguang Wang7d01bd72020-05-08 21:19:30 +08005891 err = io_submit_sqe(req, sqe, &link);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005892 if (err)
5893 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005894 }
5895
Pavel Begunkov9466f432020-01-25 22:34:01 +03005896 if (unlikely(submitted != nr)) {
5897 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
5898
5899 percpu_ref_put_many(&ctx->refs, nr - ref_used);
5900 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005901 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005902 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005903 if (statep)
5904 io_submit_state_end(&state);
5905
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005906 /* Commit SQ ring head once we've consumed and submitted all SQEs */
5907 io_commit_sqring(ctx);
5908
Jens Axboe6c271ce2019-01-10 11:22:30 -07005909 return submitted;
5910}
5911
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03005912static inline void io_sq_thread_drop_mm(struct io_ring_ctx *ctx)
5913{
5914 struct mm_struct *mm = current->mm;
5915
5916 if (mm) {
5917 unuse_mm(mm);
5918 mmput(mm);
5919 }
5920}
5921
Jens Axboe6c271ce2019-01-10 11:22:30 -07005922static int io_sq_thread(void *data)
5923{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005924 struct io_ring_ctx *ctx = data;
Jens Axboe181e4482019-11-25 08:52:30 -07005925 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005926 mm_segment_t old_fs;
5927 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005928 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005929 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005930
Jens Axboe0f158b42020-05-14 17:18:39 -06005931 complete(&ctx->sq_thread_comp);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08005932
Jens Axboe6c271ce2019-01-10 11:22:30 -07005933 old_fs = get_fs();
5934 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07005935 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005936
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005937 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005938 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005939 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005940
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005941 if (!list_empty(&ctx->poll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005942 unsigned nr_events = 0;
5943
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005944 mutex_lock(&ctx->uring_lock);
5945 if (!list_empty(&ctx->poll_list))
5946 io_iopoll_getevents(ctx, &nr_events, 0);
5947 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07005948 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005949 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005950 }
5951
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005952 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005953
5954 /*
5955 * If submit got -EBUSY, flag us as needing the application
5956 * to enter the kernel to reap and flush events.
5957 */
5958 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005959 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01005960 * Drop cur_mm before scheduling, we can't hold it for
5961 * long periods (or over schedule()). Do this before
5962 * adding ourselves to the waitqueue, as the unuse/drop
5963 * may sleep.
5964 */
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03005965 io_sq_thread_drop_mm(ctx);
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01005966
5967 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07005968 * We're polling. If we're within the defined idle
5969 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07005970 * to sleep. The exception is if we got EBUSY doing
5971 * more IO, we should wait for the application to
5972 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07005973 */
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005974 if (!list_empty(&ctx->poll_list) ||
Jens Axboedf069d82020-02-04 16:48:34 -07005975 (!time_after(jiffies, timeout) && ret != -EBUSY &&
5976 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005977 if (current->task_works)
5978 task_work_run();
Jens Axboe9831a902019-09-19 09:48:55 -06005979 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005980 continue;
5981 }
5982
Jens Axboe6c271ce2019-01-10 11:22:30 -07005983 prepare_to_wait(&ctx->sqo_wait, &wait,
5984 TASK_INTERRUPTIBLE);
5985
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005986 /*
5987 * While doing polled IO, before going to sleep, we need
5988 * to check if there are new reqs added to poll_list, it
5989 * is because reqs may have been punted to io worker and
5990 * will be added to poll_list later, hence check the
5991 * poll_list again.
5992 */
5993 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
5994 !list_empty_careful(&ctx->poll_list)) {
5995 finish_wait(&ctx->sqo_wait, &wait);
5996 continue;
5997 }
5998
Jens Axboe6c271ce2019-01-10 11:22:30 -07005999 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00006000 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02006001 /* make sure to read SQ tail after writing flags */
6002 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006003
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006004 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006005 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006006 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006007 finish_wait(&ctx->sqo_wait, &wait);
6008 break;
6009 }
Jens Axboeb41e9852020-02-17 09:52:41 -07006010 if (current->task_works) {
6011 task_work_run();
Hillf Danton10bea962020-04-01 17:19:33 +08006012 finish_wait(&ctx->sqo_wait, &wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07006013 continue;
6014 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006015 if (signal_pending(current))
6016 flush_signals(current);
6017 schedule();
6018 finish_wait(&ctx->sqo_wait, &wait);
6019
Hristo Venev75b28af2019-08-26 17:23:46 +00006020 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006021 continue;
6022 }
6023 finish_wait(&ctx->sqo_wait, &wait);
6024
Hristo Venev75b28af2019-08-26 17:23:46 +00006025 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006026 }
6027
Jens Axboe8a4955f2019-12-09 14:52:35 -07006028 mutex_lock(&ctx->uring_lock);
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03006029 ret = io_submit_sqes(ctx, to_submit, NULL, -1, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07006030 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006031 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006032 }
6033
Jens Axboeb41e9852020-02-17 09:52:41 -07006034 if (current->task_works)
6035 task_work_run();
6036
Jens Axboe6c271ce2019-01-10 11:22:30 -07006037 set_fs(old_fs);
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03006038 io_sq_thread_drop_mm(ctx);
Jens Axboe181e4482019-11-25 08:52:30 -07006039 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006040
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006041 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006042
Jens Axboe6c271ce2019-01-10 11:22:30 -07006043 return 0;
6044}
6045
Jens Axboebda52162019-09-24 13:47:15 -06006046struct io_wait_queue {
6047 struct wait_queue_entry wq;
6048 struct io_ring_ctx *ctx;
6049 unsigned to_wait;
6050 unsigned nr_timeouts;
6051};
6052
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006053static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06006054{
6055 struct io_ring_ctx *ctx = iowq->ctx;
6056
6057 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006058 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006059 * started waiting. For timeouts, we always want to return to userspace,
6060 * regardless of event count.
6061 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006062 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006063 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6064}
6065
6066static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6067 int wake_flags, void *key)
6068{
6069 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6070 wq);
6071
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006072 /* use noflush == true, as we can't safely rely on locking context */
6073 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06006074 return -1;
6075
6076 return autoremove_wake_function(curr, mode, wake_flags, key);
6077}
6078
Jens Axboe2b188cc2019-01-07 10:46:33 -07006079/*
6080 * Wait until events become available, if we don't already have some. The
6081 * application must reap them itself, as they reside on the shared cq ring.
6082 */
6083static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6084 const sigset_t __user *sig, size_t sigsz)
6085{
Jens Axboebda52162019-09-24 13:47:15 -06006086 struct io_wait_queue iowq = {
6087 .wq = {
6088 .private = current,
6089 .func = io_wake_function,
6090 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6091 },
6092 .ctx = ctx,
6093 .to_wait = min_events,
6094 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006095 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006096 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006097
Jens Axboeb41e9852020-02-17 09:52:41 -07006098 do {
6099 if (io_cqring_events(ctx, false) >= min_events)
6100 return 0;
6101 if (!current->task_works)
6102 break;
6103 task_work_run();
6104 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006105
6106 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006107#ifdef CONFIG_COMPAT
6108 if (in_compat_syscall())
6109 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006110 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006111 else
6112#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006113 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006114
Jens Axboe2b188cc2019-01-07 10:46:33 -07006115 if (ret)
6116 return ret;
6117 }
6118
Jens Axboebda52162019-09-24 13:47:15 -06006119 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006120 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006121 do {
6122 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6123 TASK_INTERRUPTIBLE);
Jens Axboeb41e9852020-02-17 09:52:41 -07006124 if (current->task_works)
6125 task_work_run();
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006126 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06006127 break;
6128 schedule();
6129 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006130 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06006131 break;
6132 }
6133 } while (1);
6134 finish_wait(&ctx->wait, &iowq.wq);
6135
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006136 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006137
Hristo Venev75b28af2019-08-26 17:23:46 +00006138 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006139}
6140
Jens Axboe6b063142019-01-10 22:13:58 -07006141static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6142{
6143#if defined(CONFIG_UNIX)
6144 if (ctx->ring_sock) {
6145 struct sock *sock = ctx->ring_sock->sk;
6146 struct sk_buff *skb;
6147
6148 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6149 kfree_skb(skb);
6150 }
6151#else
6152 int i;
6153
Jens Axboe65e19f52019-10-26 07:20:21 -06006154 for (i = 0; i < ctx->nr_user_files; i++) {
6155 struct file *file;
6156
6157 file = io_file_from_index(ctx, i);
6158 if (file)
6159 fput(file);
6160 }
Jens Axboe6b063142019-01-10 22:13:58 -07006161#endif
6162}
6163
Jens Axboe05f3fb32019-12-09 11:22:50 -07006164static void io_file_ref_kill(struct percpu_ref *ref)
6165{
6166 struct fixed_file_data *data;
6167
6168 data = container_of(ref, struct fixed_file_data, refs);
6169 complete(&data->done);
6170}
6171
Jens Axboe6b063142019-01-10 22:13:58 -07006172static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6173{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006174 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006175 struct fixed_file_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006176 unsigned nr_tables, i;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006177 unsigned long flags;
Jens Axboe65e19f52019-10-26 07:20:21 -06006178
Jens Axboe05f3fb32019-12-09 11:22:50 -07006179 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006180 return -ENXIO;
6181
Xiaoguang Wang05589552020-03-31 14:05:18 +08006182 spin_lock_irqsave(&data->lock, flags);
6183 if (!list_empty(&data->ref_list))
6184 ref_node = list_first_entry(&data->ref_list,
6185 struct fixed_file_ref_node, node);
6186 spin_unlock_irqrestore(&data->lock, flags);
6187 if (ref_node)
6188 percpu_ref_kill(&ref_node->refs);
6189
6190 percpu_ref_kill(&data->refs);
6191
6192 /* wait for all refs nodes to complete */
Jens Axboe2faf8522020-02-04 19:54:55 -07006193 wait_for_completion(&data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006194
Jens Axboe6b063142019-01-10 22:13:58 -07006195 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06006196 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6197 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006198 kfree(data->table[i].files);
6199 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006200 percpu_ref_exit(&data->refs);
6201 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006202 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006203 ctx->nr_user_files = 0;
6204 return 0;
6205}
6206
Jens Axboe6c271ce2019-01-10 11:22:30 -07006207static void io_sq_thread_stop(struct io_ring_ctx *ctx)
6208{
6209 if (ctx->sqo_thread) {
Jens Axboe0f158b42020-05-14 17:18:39 -06006210 wait_for_completion(&ctx->sq_thread_comp);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006211 /*
6212 * The park is a bit of a work-around, without it we get
6213 * warning spews on shutdown with SQPOLL set and affinity
6214 * set to a single CPU.
6215 */
Jens Axboe06058632019-04-13 09:26:03 -06006216 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006217 kthread_stop(ctx->sqo_thread);
6218 ctx->sqo_thread = NULL;
6219 }
6220}
6221
Jens Axboe6b063142019-01-10 22:13:58 -07006222static void io_finish_async(struct io_ring_ctx *ctx)
6223{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006224 io_sq_thread_stop(ctx);
6225
Jens Axboe561fb042019-10-24 07:25:42 -06006226 if (ctx->io_wq) {
6227 io_wq_destroy(ctx->io_wq);
6228 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006229 }
6230}
6231
6232#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07006233/*
6234 * Ensure the UNIX gc is aware of our file set, so we are certain that
6235 * the io_uring can be safely unregistered on process exit, even if we have
6236 * loops in the file referencing.
6237 */
6238static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
6239{
6240 struct sock *sk = ctx->ring_sock->sk;
6241 struct scm_fp_list *fpl;
6242 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06006243 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07006244
Jens Axboe6b063142019-01-10 22:13:58 -07006245 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
6246 if (!fpl)
6247 return -ENOMEM;
6248
6249 skb = alloc_skb(0, GFP_KERNEL);
6250 if (!skb) {
6251 kfree(fpl);
6252 return -ENOMEM;
6253 }
6254
6255 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07006256
Jens Axboe08a45172019-10-03 08:11:03 -06006257 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07006258 fpl->user = get_uid(ctx->user);
6259 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006260 struct file *file = io_file_from_index(ctx, i + offset);
6261
6262 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06006263 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06006264 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06006265 unix_inflight(fpl->user, fpl->fp[nr_files]);
6266 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07006267 }
6268
Jens Axboe08a45172019-10-03 08:11:03 -06006269 if (nr_files) {
6270 fpl->max = SCM_MAX_FD;
6271 fpl->count = nr_files;
6272 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006273 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06006274 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
6275 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07006276
Jens Axboe08a45172019-10-03 08:11:03 -06006277 for (i = 0; i < nr_files; i++)
6278 fput(fpl->fp[i]);
6279 } else {
6280 kfree_skb(skb);
6281 kfree(fpl);
6282 }
Jens Axboe6b063142019-01-10 22:13:58 -07006283
6284 return 0;
6285}
6286
6287/*
6288 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
6289 * causes regular reference counting to break down. We rely on the UNIX
6290 * garbage collection to take care of this problem for us.
6291 */
6292static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6293{
6294 unsigned left, total;
6295 int ret = 0;
6296
6297 total = 0;
6298 left = ctx->nr_user_files;
6299 while (left) {
6300 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07006301
6302 ret = __io_sqe_files_scm(ctx, this_files, total);
6303 if (ret)
6304 break;
6305 left -= this_files;
6306 total += this_files;
6307 }
6308
6309 if (!ret)
6310 return 0;
6311
6312 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006313 struct file *file = io_file_from_index(ctx, total);
6314
6315 if (file)
6316 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07006317 total++;
6318 }
6319
6320 return ret;
6321}
6322#else
6323static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6324{
6325 return 0;
6326}
6327#endif
6328
Jens Axboe65e19f52019-10-26 07:20:21 -06006329static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
6330 unsigned nr_files)
6331{
6332 int i;
6333
6334 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006335 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006336 unsigned this_files;
6337
6338 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
6339 table->files = kcalloc(this_files, sizeof(struct file *),
6340 GFP_KERNEL);
6341 if (!table->files)
6342 break;
6343 nr_files -= this_files;
6344 }
6345
6346 if (i == nr_tables)
6347 return 0;
6348
6349 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006350 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006351 kfree(table->files);
6352 }
6353 return 1;
6354}
6355
Jens Axboe05f3fb32019-12-09 11:22:50 -07006356static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06006357{
6358#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06006359 struct sock *sock = ctx->ring_sock->sk;
6360 struct sk_buff_head list, *head = &sock->sk_receive_queue;
6361 struct sk_buff *skb;
6362 int i;
6363
6364 __skb_queue_head_init(&list);
6365
6366 /*
6367 * Find the skb that holds this file in its SCM_RIGHTS. When found,
6368 * remove this entry and rearrange the file array.
6369 */
6370 skb = skb_dequeue(head);
6371 while (skb) {
6372 struct scm_fp_list *fp;
6373
6374 fp = UNIXCB(skb).fp;
6375 for (i = 0; i < fp->count; i++) {
6376 int left;
6377
6378 if (fp->fp[i] != file)
6379 continue;
6380
6381 unix_notinflight(fp->user, fp->fp[i]);
6382 left = fp->count - 1 - i;
6383 if (left) {
6384 memmove(&fp->fp[i], &fp->fp[i + 1],
6385 left * sizeof(struct file *));
6386 }
6387 fp->count--;
6388 if (!fp->count) {
6389 kfree_skb(skb);
6390 skb = NULL;
6391 } else {
6392 __skb_queue_tail(&list, skb);
6393 }
6394 fput(file);
6395 file = NULL;
6396 break;
6397 }
6398
6399 if (!file)
6400 break;
6401
6402 __skb_queue_tail(&list, skb);
6403
6404 skb = skb_dequeue(head);
6405 }
6406
6407 if (skb_peek(&list)) {
6408 spin_lock_irq(&head->lock);
6409 while ((skb = __skb_dequeue(&list)) != NULL)
6410 __skb_queue_tail(head, skb);
6411 spin_unlock_irq(&head->lock);
6412 }
6413#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07006414 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06006415#endif
6416}
6417
Jens Axboe05f3fb32019-12-09 11:22:50 -07006418struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08006419 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006420 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006421};
6422
Xiaoguang Wang05589552020-03-31 14:05:18 +08006423static void io_file_put_work(struct work_struct *work)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006424{
Xiaoguang Wang05589552020-03-31 14:05:18 +08006425 struct fixed_file_ref_node *ref_node;
6426 struct fixed_file_data *file_data;
6427 struct io_ring_ctx *ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006428 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006429 unsigned long flags;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006430
Xiaoguang Wang05589552020-03-31 14:05:18 +08006431 ref_node = container_of(work, struct fixed_file_ref_node, work);
6432 file_data = ref_node->file_data;
6433 ctx = file_data->ctx;
6434
6435 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
6436 list_del_init(&pfile->list);
6437 io_ring_file_put(ctx, pfile->file);
6438 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006439 }
6440
Xiaoguang Wang05589552020-03-31 14:05:18 +08006441 spin_lock_irqsave(&file_data->lock, flags);
6442 list_del_init(&ref_node->node);
6443 spin_unlock_irqrestore(&file_data->lock, flags);
Jens Axboe2faf8522020-02-04 19:54:55 -07006444
Xiaoguang Wang05589552020-03-31 14:05:18 +08006445 percpu_ref_exit(&ref_node->refs);
6446 kfree(ref_node);
6447 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006448}
6449
6450static void io_file_data_ref_zero(struct percpu_ref *ref)
6451{
Xiaoguang Wang05589552020-03-31 14:05:18 +08006452 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006453
Xiaoguang Wang05589552020-03-31 14:05:18 +08006454 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006455
Xiaoguang Wang05589552020-03-31 14:05:18 +08006456 queue_work(system_wq, &ref_node->work);
6457}
6458
6459static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
6460 struct io_ring_ctx *ctx)
6461{
6462 struct fixed_file_ref_node *ref_node;
6463
6464 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
6465 if (!ref_node)
6466 return ERR_PTR(-ENOMEM);
6467
6468 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
6469 0, GFP_KERNEL)) {
6470 kfree(ref_node);
6471 return ERR_PTR(-ENOMEM);
6472 }
6473 INIT_LIST_HEAD(&ref_node->node);
6474 INIT_LIST_HEAD(&ref_node->file_list);
6475 INIT_WORK(&ref_node->work, io_file_put_work);
6476 ref_node->file_data = ctx->file_data;
6477 return ref_node;
6478
6479}
6480
6481static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
6482{
6483 percpu_ref_exit(&ref_node->refs);
6484 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006485}
6486
6487static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
6488 unsigned nr_args)
6489{
6490 __s32 __user *fds = (__s32 __user *) arg;
6491 unsigned nr_tables;
6492 struct file *file;
6493 int fd, ret = 0;
6494 unsigned i;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006495 struct fixed_file_ref_node *ref_node;
6496 unsigned long flags;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006497
6498 if (ctx->file_data)
6499 return -EBUSY;
6500 if (!nr_args)
6501 return -EINVAL;
6502 if (nr_args > IORING_MAX_FIXED_FILES)
6503 return -EMFILE;
6504
6505 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
6506 if (!ctx->file_data)
6507 return -ENOMEM;
6508 ctx->file_data->ctx = ctx;
6509 init_completion(&ctx->file_data->done);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006510 INIT_LIST_HEAD(&ctx->file_data->ref_list);
Xiaoguang Wangf7fe9342020-04-07 20:02:31 +08006511 spin_lock_init(&ctx->file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006512
6513 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
6514 ctx->file_data->table = kcalloc(nr_tables,
6515 sizeof(struct fixed_file_table),
6516 GFP_KERNEL);
6517 if (!ctx->file_data->table) {
6518 kfree(ctx->file_data);
6519 ctx->file_data = NULL;
6520 return -ENOMEM;
6521 }
6522
Xiaoguang Wang05589552020-03-31 14:05:18 +08006523 if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill,
Jens Axboe05f3fb32019-12-09 11:22:50 -07006524 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
6525 kfree(ctx->file_data->table);
6526 kfree(ctx->file_data);
6527 ctx->file_data = NULL;
6528 return -ENOMEM;
6529 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006530
6531 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
6532 percpu_ref_exit(&ctx->file_data->refs);
6533 kfree(ctx->file_data->table);
6534 kfree(ctx->file_data);
6535 ctx->file_data = NULL;
6536 return -ENOMEM;
6537 }
6538
6539 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
6540 struct fixed_file_table *table;
6541 unsigned index;
6542
6543 ret = -EFAULT;
6544 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
6545 break;
6546 /* allow sparse sets */
6547 if (fd == -1) {
6548 ret = 0;
6549 continue;
6550 }
6551
6552 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6553 index = i & IORING_FILE_TABLE_MASK;
6554 file = fget(fd);
6555
6556 ret = -EBADF;
6557 if (!file)
6558 break;
6559
6560 /*
6561 * Don't allow io_uring instances to be registered. If UNIX
6562 * isn't enabled, then this causes a reference cycle and this
6563 * instance can never get freed. If UNIX is enabled we'll
6564 * handle it just fine, but there's still no point in allowing
6565 * a ring fd as it doesn't support regular read/write anyway.
6566 */
6567 if (file->f_op == &io_uring_fops) {
6568 fput(file);
6569 break;
6570 }
6571 ret = 0;
6572 table->files[index] = file;
6573 }
6574
6575 if (ret) {
6576 for (i = 0; i < ctx->nr_user_files; i++) {
6577 file = io_file_from_index(ctx, i);
6578 if (file)
6579 fput(file);
6580 }
6581 for (i = 0; i < nr_tables; i++)
6582 kfree(ctx->file_data->table[i].files);
6583
6584 kfree(ctx->file_data->table);
6585 kfree(ctx->file_data);
6586 ctx->file_data = NULL;
6587 ctx->nr_user_files = 0;
6588 return ret;
6589 }
6590
6591 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006592 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006593 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006594 return ret;
6595 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006596
Xiaoguang Wang05589552020-03-31 14:05:18 +08006597 ref_node = alloc_fixed_file_ref_node(ctx);
6598 if (IS_ERR(ref_node)) {
6599 io_sqe_files_unregister(ctx);
6600 return PTR_ERR(ref_node);
6601 }
6602
6603 ctx->file_data->cur_refs = &ref_node->refs;
6604 spin_lock_irqsave(&ctx->file_data->lock, flags);
6605 list_add(&ref_node->node, &ctx->file_data->ref_list);
6606 spin_unlock_irqrestore(&ctx->file_data->lock, flags);
6607 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006608 return ret;
6609}
6610
Jens Axboec3a31e62019-10-03 13:59:56 -06006611static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
6612 int index)
6613{
6614#if defined(CONFIG_UNIX)
6615 struct sock *sock = ctx->ring_sock->sk;
6616 struct sk_buff_head *head = &sock->sk_receive_queue;
6617 struct sk_buff *skb;
6618
6619 /*
6620 * See if we can merge this file into an existing skb SCM_RIGHTS
6621 * file set. If there's no room, fall back to allocating a new skb
6622 * and filling it in.
6623 */
6624 spin_lock_irq(&head->lock);
6625 skb = skb_peek(head);
6626 if (skb) {
6627 struct scm_fp_list *fpl = UNIXCB(skb).fp;
6628
6629 if (fpl->count < SCM_MAX_FD) {
6630 __skb_unlink(skb, head);
6631 spin_unlock_irq(&head->lock);
6632 fpl->fp[fpl->count] = get_file(file);
6633 unix_inflight(fpl->user, fpl->fp[fpl->count]);
6634 fpl->count++;
6635 spin_lock_irq(&head->lock);
6636 __skb_queue_head(head, skb);
6637 } else {
6638 skb = NULL;
6639 }
6640 }
6641 spin_unlock_irq(&head->lock);
6642
6643 if (skb) {
6644 fput(file);
6645 return 0;
6646 }
6647
6648 return __io_sqe_files_scm(ctx, 1, index);
6649#else
6650 return 0;
6651#endif
6652}
6653
Hillf Dantona5318d32020-03-23 17:47:15 +08006654static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08006655 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006656{
Hillf Dantona5318d32020-03-23 17:47:15 +08006657 struct io_file_put *pfile;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006658 struct percpu_ref *refs = data->cur_refs;
6659 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006660
Jens Axboe05f3fb32019-12-09 11:22:50 -07006661 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08006662 if (!pfile)
6663 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006664
Xiaoguang Wang05589552020-03-31 14:05:18 +08006665 ref_node = container_of(refs, struct fixed_file_ref_node, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006666 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006667 list_add(&pfile->list, &ref_node->file_list);
6668
Hillf Dantona5318d32020-03-23 17:47:15 +08006669 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006670}
6671
6672static int __io_sqe_files_update(struct io_ring_ctx *ctx,
6673 struct io_uring_files_update *up,
6674 unsigned nr_args)
6675{
6676 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006677 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006678 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006679 __s32 __user *fds;
6680 int fd, i, err;
6681 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006682 unsigned long flags;
6683 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06006684
Jens Axboe05f3fb32019-12-09 11:22:50 -07006685 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06006686 return -EOVERFLOW;
6687 if (done > ctx->nr_user_files)
6688 return -EINVAL;
6689
Xiaoguang Wang05589552020-03-31 14:05:18 +08006690 ref_node = alloc_fixed_file_ref_node(ctx);
6691 if (IS_ERR(ref_node))
6692 return PTR_ERR(ref_node);
6693
Jens Axboec3a31e62019-10-03 13:59:56 -06006694 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006695 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06006696 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006697 struct fixed_file_table *table;
6698 unsigned index;
6699
Jens Axboec3a31e62019-10-03 13:59:56 -06006700 err = 0;
6701 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
6702 err = -EFAULT;
6703 break;
6704 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006705 i = array_index_nospec(up->offset, ctx->nr_user_files);
6706 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06006707 index = i & IORING_FILE_TABLE_MASK;
6708 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006709 file = io_file_from_index(ctx, index);
Hillf Dantona5318d32020-03-23 17:47:15 +08006710 err = io_queue_file_removal(data, file);
6711 if (err)
6712 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06006713 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006714 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06006715 }
6716 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06006717 file = fget(fd);
6718 if (!file) {
6719 err = -EBADF;
6720 break;
6721 }
6722 /*
6723 * Don't allow io_uring instances to be registered. If
6724 * UNIX isn't enabled, then this causes a reference
6725 * cycle and this instance can never get freed. If UNIX
6726 * is enabled we'll handle it just fine, but there's
6727 * still no point in allowing a ring fd as it doesn't
6728 * support regular read/write anyway.
6729 */
6730 if (file->f_op == &io_uring_fops) {
6731 fput(file);
6732 err = -EBADF;
6733 break;
6734 }
Jens Axboe65e19f52019-10-26 07:20:21 -06006735 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006736 err = io_sqe_file_register(ctx, file, i);
6737 if (err)
6738 break;
6739 }
6740 nr_args--;
6741 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006742 up->offset++;
6743 }
6744
Xiaoguang Wang05589552020-03-31 14:05:18 +08006745 if (needs_switch) {
6746 percpu_ref_kill(data->cur_refs);
6747 spin_lock_irqsave(&data->lock, flags);
6748 list_add(&ref_node->node, &data->ref_list);
6749 data->cur_refs = &ref_node->refs;
6750 spin_unlock_irqrestore(&data->lock, flags);
6751 percpu_ref_get(&ctx->file_data->refs);
6752 } else
6753 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06006754
6755 return done ? done : err;
6756}
Xiaoguang Wang05589552020-03-31 14:05:18 +08006757
Jens Axboe05f3fb32019-12-09 11:22:50 -07006758static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
6759 unsigned nr_args)
6760{
6761 struct io_uring_files_update up;
6762
6763 if (!ctx->file_data)
6764 return -ENXIO;
6765 if (!nr_args)
6766 return -EINVAL;
6767 if (copy_from_user(&up, arg, sizeof(up)))
6768 return -EFAULT;
6769 if (up.resv)
6770 return -EINVAL;
6771
6772 return __io_sqe_files_update(ctx, &up, nr_args);
6773}
Jens Axboec3a31e62019-10-03 13:59:56 -06006774
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006775static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07006776{
6777 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6778
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006779 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07006780 io_put_req(req);
6781}
6782
Pavel Begunkov24369c22020-01-28 03:15:48 +03006783static int io_init_wq_offload(struct io_ring_ctx *ctx,
6784 struct io_uring_params *p)
6785{
6786 struct io_wq_data data;
6787 struct fd f;
6788 struct io_ring_ctx *ctx_attach;
6789 unsigned int concurrency;
6790 int ret = 0;
6791
6792 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006793 data.free_work = io_free_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03006794
6795 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
6796 /* Do QD, or 4 * CPUS, whatever is smallest */
6797 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
6798
6799 ctx->io_wq = io_wq_create(concurrency, &data);
6800 if (IS_ERR(ctx->io_wq)) {
6801 ret = PTR_ERR(ctx->io_wq);
6802 ctx->io_wq = NULL;
6803 }
6804 return ret;
6805 }
6806
6807 f = fdget(p->wq_fd);
6808 if (!f.file)
6809 return -EBADF;
6810
6811 if (f.file->f_op != &io_uring_fops) {
6812 ret = -EINVAL;
6813 goto out_fput;
6814 }
6815
6816 ctx_attach = f.file->private_data;
6817 /* @io_wq is protected by holding the fd */
6818 if (!io_wq_get(ctx_attach->io_wq, &data)) {
6819 ret = -EINVAL;
6820 goto out_fput;
6821 }
6822
6823 ctx->io_wq = ctx_attach->io_wq;
6824out_fput:
6825 fdput(f);
6826 return ret;
6827}
6828
Jens Axboe6c271ce2019-01-10 11:22:30 -07006829static int io_sq_offload_start(struct io_ring_ctx *ctx,
6830 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006831{
6832 int ret;
6833
Jens Axboe6c271ce2019-01-10 11:22:30 -07006834 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006835 mmgrab(current->mm);
6836 ctx->sqo_mm = current->mm;
6837
Jens Axboe6c271ce2019-01-10 11:22:30 -07006838 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06006839 ret = -EPERM;
6840 if (!capable(CAP_SYS_ADMIN))
6841 goto err;
6842
Jens Axboe917257d2019-04-13 09:28:55 -06006843 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
6844 if (!ctx->sq_thread_idle)
6845 ctx->sq_thread_idle = HZ;
6846
Jens Axboe6c271ce2019-01-10 11:22:30 -07006847 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06006848 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006849
Jens Axboe917257d2019-04-13 09:28:55 -06006850 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06006851 if (cpu >= nr_cpu_ids)
6852 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08006853 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06006854 goto err;
6855
Jens Axboe6c271ce2019-01-10 11:22:30 -07006856 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
6857 ctx, cpu,
6858 "io_uring-sq");
6859 } else {
6860 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
6861 "io_uring-sq");
6862 }
6863 if (IS_ERR(ctx->sqo_thread)) {
6864 ret = PTR_ERR(ctx->sqo_thread);
6865 ctx->sqo_thread = NULL;
6866 goto err;
6867 }
6868 wake_up_process(ctx->sqo_thread);
6869 } else if (p->flags & IORING_SETUP_SQ_AFF) {
6870 /* Can't have SQ_AFF without SQPOLL */
6871 ret = -EINVAL;
6872 goto err;
6873 }
6874
Pavel Begunkov24369c22020-01-28 03:15:48 +03006875 ret = io_init_wq_offload(ctx, p);
6876 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006877 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006878
6879 return 0;
6880err:
Jens Axboe54a91f32019-09-10 09:15:04 -06006881 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006882 mmdrop(ctx->sqo_mm);
6883 ctx->sqo_mm = NULL;
6884 return ret;
6885}
6886
6887static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
6888{
6889 atomic_long_sub(nr_pages, &user->locked_vm);
6890}
6891
6892static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
6893{
6894 unsigned long page_limit, cur_pages, new_pages;
6895
6896 /* Don't allow more pages than we can safely lock */
6897 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
6898
6899 do {
6900 cur_pages = atomic_long_read(&user->locked_vm);
6901 new_pages = cur_pages + nr_pages;
6902 if (new_pages > page_limit)
6903 return -ENOMEM;
6904 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
6905 new_pages) != cur_pages);
6906
6907 return 0;
6908}
6909
6910static void io_mem_free(void *ptr)
6911{
Mark Rutland52e04ef2019-04-30 17:30:21 +01006912 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006913
Mark Rutland52e04ef2019-04-30 17:30:21 +01006914 if (!ptr)
6915 return;
6916
6917 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006918 if (put_page_testzero(page))
6919 free_compound_page(page);
6920}
6921
6922static void *io_mem_alloc(size_t size)
6923{
6924 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
6925 __GFP_NORETRY;
6926
6927 return (void *) __get_free_pages(gfp_flags, get_order(size));
6928}
6929
Hristo Venev75b28af2019-08-26 17:23:46 +00006930static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
6931 size_t *sq_offset)
6932{
6933 struct io_rings *rings;
6934 size_t off, sq_array_size;
6935
6936 off = struct_size(rings, cqes, cq_entries);
6937 if (off == SIZE_MAX)
6938 return SIZE_MAX;
6939
6940#ifdef CONFIG_SMP
6941 off = ALIGN(off, SMP_CACHE_BYTES);
6942 if (off == 0)
6943 return SIZE_MAX;
6944#endif
6945
6946 sq_array_size = array_size(sizeof(u32), sq_entries);
6947 if (sq_array_size == SIZE_MAX)
6948 return SIZE_MAX;
6949
6950 if (check_add_overflow(off, sq_array_size, &off))
6951 return SIZE_MAX;
6952
6953 if (sq_offset)
6954 *sq_offset = off;
6955
6956 return off;
6957}
6958
Jens Axboe2b188cc2019-01-07 10:46:33 -07006959static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
6960{
Hristo Venev75b28af2019-08-26 17:23:46 +00006961 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006962
Hristo Venev75b28af2019-08-26 17:23:46 +00006963 pages = (size_t)1 << get_order(
6964 rings_size(sq_entries, cq_entries, NULL));
6965 pages += (size_t)1 << get_order(
6966 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07006967
Hristo Venev75b28af2019-08-26 17:23:46 +00006968 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006969}
6970
Jens Axboeedafcce2019-01-09 09:16:05 -07006971static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
6972{
6973 int i, j;
6974
6975 if (!ctx->user_bufs)
6976 return -ENXIO;
6977
6978 for (i = 0; i < ctx->nr_user_bufs; i++) {
6979 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6980
6981 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006982 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07006983
6984 if (ctx->account_mem)
6985 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006986 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006987 imu->nr_bvecs = 0;
6988 }
6989
6990 kfree(ctx->user_bufs);
6991 ctx->user_bufs = NULL;
6992 ctx->nr_user_bufs = 0;
6993 return 0;
6994}
6995
6996static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
6997 void __user *arg, unsigned index)
6998{
6999 struct iovec __user *src;
7000
7001#ifdef CONFIG_COMPAT
7002 if (ctx->compat) {
7003 struct compat_iovec __user *ciovs;
7004 struct compat_iovec ciov;
7005
7006 ciovs = (struct compat_iovec __user *) arg;
7007 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
7008 return -EFAULT;
7009
Jens Axboed55e5f52019-12-11 16:12:15 -07007010 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07007011 dst->iov_len = ciov.iov_len;
7012 return 0;
7013 }
7014#endif
7015 src = (struct iovec __user *) arg;
7016 if (copy_from_user(dst, &src[index], sizeof(*dst)))
7017 return -EFAULT;
7018 return 0;
7019}
7020
7021static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
7022 unsigned nr_args)
7023{
7024 struct vm_area_struct **vmas = NULL;
7025 struct page **pages = NULL;
7026 int i, j, got_pages = 0;
7027 int ret = -EINVAL;
7028
7029 if (ctx->user_bufs)
7030 return -EBUSY;
7031 if (!nr_args || nr_args > UIO_MAXIOV)
7032 return -EINVAL;
7033
7034 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
7035 GFP_KERNEL);
7036 if (!ctx->user_bufs)
7037 return -ENOMEM;
7038
7039 for (i = 0; i < nr_args; i++) {
7040 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7041 unsigned long off, start, end, ubuf;
7042 int pret, nr_pages;
7043 struct iovec iov;
7044 size_t size;
7045
7046 ret = io_copy_iov(ctx, &iov, arg, i);
7047 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03007048 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007049
7050 /*
7051 * Don't impose further limits on the size and buffer
7052 * constraints here, we'll -EINVAL later when IO is
7053 * submitted if they are wrong.
7054 */
7055 ret = -EFAULT;
7056 if (!iov.iov_base || !iov.iov_len)
7057 goto err;
7058
7059 /* arbitrary limit, but we need something */
7060 if (iov.iov_len > SZ_1G)
7061 goto err;
7062
7063 ubuf = (unsigned long) iov.iov_base;
7064 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
7065 start = ubuf >> PAGE_SHIFT;
7066 nr_pages = end - start;
7067
7068 if (ctx->account_mem) {
7069 ret = io_account_mem(ctx->user, nr_pages);
7070 if (ret)
7071 goto err;
7072 }
7073
7074 ret = 0;
7075 if (!pages || nr_pages > got_pages) {
7076 kfree(vmas);
7077 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007078 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07007079 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007080 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07007081 sizeof(struct vm_area_struct *),
7082 GFP_KERNEL);
7083 if (!pages || !vmas) {
7084 ret = -ENOMEM;
7085 if (ctx->account_mem)
7086 io_unaccount_mem(ctx->user, nr_pages);
7087 goto err;
7088 }
7089 got_pages = nr_pages;
7090 }
7091
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007092 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07007093 GFP_KERNEL);
7094 ret = -ENOMEM;
7095 if (!imu->bvec) {
7096 if (ctx->account_mem)
7097 io_unaccount_mem(ctx->user, nr_pages);
7098 goto err;
7099 }
7100
7101 ret = 0;
7102 down_read(&current->mm->mmap_sem);
John Hubbard2113b052020-01-30 22:13:13 -08007103 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07007104 FOLL_WRITE | FOLL_LONGTERM,
7105 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007106 if (pret == nr_pages) {
7107 /* don't support file backed memory */
7108 for (j = 0; j < nr_pages; j++) {
7109 struct vm_area_struct *vma = vmas[j];
7110
7111 if (vma->vm_file &&
7112 !is_file_hugepages(vma->vm_file)) {
7113 ret = -EOPNOTSUPP;
7114 break;
7115 }
7116 }
7117 } else {
7118 ret = pret < 0 ? pret : -EFAULT;
7119 }
7120 up_read(&current->mm->mmap_sem);
7121 if (ret) {
7122 /*
7123 * if we did partial map, or found file backed vmas,
7124 * release any pages we did get
7125 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07007126 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007127 unpin_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07007128 if (ctx->account_mem)
7129 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007130 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007131 goto err;
7132 }
7133
7134 off = ubuf & ~PAGE_MASK;
7135 size = iov.iov_len;
7136 for (j = 0; j < nr_pages; j++) {
7137 size_t vec_len;
7138
7139 vec_len = min_t(size_t, size, PAGE_SIZE - off);
7140 imu->bvec[j].bv_page = pages[j];
7141 imu->bvec[j].bv_len = vec_len;
7142 imu->bvec[j].bv_offset = off;
7143 off = 0;
7144 size -= vec_len;
7145 }
7146 /* store original address for later verification */
7147 imu->ubuf = ubuf;
7148 imu->len = iov.iov_len;
7149 imu->nr_bvecs = nr_pages;
7150
7151 ctx->nr_user_bufs++;
7152 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007153 kvfree(pages);
7154 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007155 return 0;
7156err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007157 kvfree(pages);
7158 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007159 io_sqe_buffer_unregister(ctx);
7160 return ret;
7161}
7162
Jens Axboe9b402842019-04-11 11:45:41 -06007163static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
7164{
7165 __s32 __user *fds = arg;
7166 int fd;
7167
7168 if (ctx->cq_ev_fd)
7169 return -EBUSY;
7170
7171 if (copy_from_user(&fd, fds, sizeof(*fds)))
7172 return -EFAULT;
7173
7174 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
7175 if (IS_ERR(ctx->cq_ev_fd)) {
7176 int ret = PTR_ERR(ctx->cq_ev_fd);
7177 ctx->cq_ev_fd = NULL;
7178 return ret;
7179 }
7180
7181 return 0;
7182}
7183
7184static int io_eventfd_unregister(struct io_ring_ctx *ctx)
7185{
7186 if (ctx->cq_ev_fd) {
7187 eventfd_ctx_put(ctx->cq_ev_fd);
7188 ctx->cq_ev_fd = NULL;
7189 return 0;
7190 }
7191
7192 return -ENXIO;
7193}
7194
Jens Axboe5a2e7452020-02-23 16:23:11 -07007195static int __io_destroy_buffers(int id, void *p, void *data)
7196{
7197 struct io_ring_ctx *ctx = data;
7198 struct io_buffer *buf = p;
7199
Jens Axboe067524e2020-03-02 16:32:28 -07007200 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007201 return 0;
7202}
7203
7204static void io_destroy_buffers(struct io_ring_ctx *ctx)
7205{
7206 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
7207 idr_destroy(&ctx->io_buffer_idr);
7208}
7209
Jens Axboe2b188cc2019-01-07 10:46:33 -07007210static void io_ring_ctx_free(struct io_ring_ctx *ctx)
7211{
Jens Axboe6b063142019-01-10 22:13:58 -07007212 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007213 if (ctx->sqo_mm)
7214 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07007215
7216 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07007217 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07007218 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06007219 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007220 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07007221 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07007222
Jens Axboe2b188cc2019-01-07 10:46:33 -07007223#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07007224 if (ctx->ring_sock) {
7225 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007226 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07007227 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007228#endif
7229
Hristo Venev75b28af2019-08-26 17:23:46 +00007230 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007231 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007232
7233 percpu_ref_exit(&ctx->refs);
7234 if (ctx->account_mem)
7235 io_unaccount_mem(ctx->user,
7236 ring_pages(ctx->sq_entries, ctx->cq_entries));
7237 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07007238 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07007239 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07007240 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007241 kfree(ctx);
7242}
7243
7244static __poll_t io_uring_poll(struct file *file, poll_table *wait)
7245{
7246 struct io_ring_ctx *ctx = file->private_data;
7247 __poll_t mask = 0;
7248
7249 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02007250 /*
7251 * synchronizes with barrier from wq_has_sleeper call in
7252 * io_commit_cqring
7253 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007254 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00007255 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
7256 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007257 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01007258 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007259 mask |= EPOLLIN | EPOLLRDNORM;
7260
7261 return mask;
7262}
7263
7264static int io_uring_fasync(int fd, struct file *file, int on)
7265{
7266 struct io_ring_ctx *ctx = file->private_data;
7267
7268 return fasync_helper(fd, file, on, &ctx->cq_fasync);
7269}
7270
Jens Axboe071698e2020-01-28 10:04:42 -07007271static int io_remove_personalities(int id, void *p, void *data)
7272{
7273 struct io_ring_ctx *ctx = data;
7274 const struct cred *cred;
7275
7276 cred = idr_remove(&ctx->personality_idr, id);
7277 if (cred)
7278 put_cred(cred);
7279 return 0;
7280}
7281
Jens Axboe85faa7b2020-04-09 18:14:00 -06007282static void io_ring_exit_work(struct work_struct *work)
7283{
7284 struct io_ring_ctx *ctx;
7285
7286 ctx = container_of(work, struct io_ring_ctx, exit_work);
7287 if (ctx->rings)
7288 io_cqring_overflow_flush(ctx, true);
7289
Jens Axboe0f158b42020-05-14 17:18:39 -06007290 wait_for_completion(&ctx->ref_comp);
Jens Axboe85faa7b2020-04-09 18:14:00 -06007291 io_ring_ctx_free(ctx);
7292}
7293
Jens Axboe2b188cc2019-01-07 10:46:33 -07007294static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
7295{
7296 mutex_lock(&ctx->uring_lock);
7297 percpu_ref_kill(&ctx->refs);
7298 mutex_unlock(&ctx->uring_lock);
7299
Jens Axboedf069d82020-02-04 16:48:34 -07007300 /*
7301 * Wait for sq thread to idle, if we have one. It won't spin on new
7302 * work after we've killed the ctx ref above. This is important to do
7303 * before we cancel existing commands, as the thread could otherwise
7304 * be queueing new work post that. If that's work we need to cancel,
7305 * it could cause shutdown to hang.
7306 */
7307 while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait))
Xiaoguang Wang3fd44c82020-05-01 08:52:56 +08007308 cond_resched();
Jens Axboedf069d82020-02-04 16:48:34 -07007309
Jens Axboe5262f562019-09-17 12:26:57 -06007310 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07007311 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06007312
7313 if (ctx->io_wq)
7314 io_wq_cancel_all(ctx->io_wq);
7315
Jens Axboedef596e2019-01-09 08:59:42 -07007316 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07007317 /* if we failed setting up the ctx, we might not have any rings */
7318 if (ctx->rings)
7319 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07007320 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe85faa7b2020-04-09 18:14:00 -06007321 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
7322 queue_work(system_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007323}
7324
7325static int io_uring_release(struct inode *inode, struct file *file)
7326{
7327 struct io_ring_ctx *ctx = file->private_data;
7328
7329 file->private_data = NULL;
7330 io_ring_ctx_wait_and_kill(ctx);
7331 return 0;
7332}
7333
Jens Axboefcb323c2019-10-24 12:39:47 -06007334static void io_uring_cancel_files(struct io_ring_ctx *ctx,
7335 struct files_struct *files)
7336{
Jens Axboefcb323c2019-10-24 12:39:47 -06007337 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007338 struct io_kiocb *cancel_req = NULL, *req;
7339 DEFINE_WAIT(wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007340
7341 spin_lock_irq(&ctx->inflight_lock);
7342 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07007343 if (req->work.files != files)
7344 continue;
7345 /* req is being completed, ignore */
7346 if (!refcount_inc_not_zero(&req->refs))
7347 continue;
7348 cancel_req = req;
7349 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06007350 }
Jens Axboe768134d2019-11-10 20:30:53 -07007351 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007352 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07007353 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06007354 spin_unlock_irq(&ctx->inflight_lock);
7355
Jens Axboe768134d2019-11-10 20:30:53 -07007356 /* We need to keep going until we don't find a matching req */
7357 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007358 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08007359
Jens Axboe2ca10252020-02-13 17:17:35 -07007360 if (cancel_req->flags & REQ_F_OVERFLOW) {
7361 spin_lock_irq(&ctx->completion_lock);
7362 list_del(&cancel_req->list);
7363 cancel_req->flags &= ~REQ_F_OVERFLOW;
7364 if (list_empty(&ctx->cq_overflow_list)) {
7365 clear_bit(0, &ctx->sq_check_overflow);
7366 clear_bit(0, &ctx->cq_check_overflow);
7367 }
7368 spin_unlock_irq(&ctx->completion_lock);
7369
7370 WRITE_ONCE(ctx->rings->cq_overflow,
7371 atomic_inc_return(&ctx->cached_cq_overflow));
7372
7373 /*
7374 * Put inflight ref and overflow ref. If that's
7375 * all we had, then we're done with this request.
7376 */
7377 if (refcount_sub_and_test(2, &cancel_req->refs)) {
7378 io_put_req(cancel_req);
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007379 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboe2ca10252020-02-13 17:17:35 -07007380 continue;
7381 }
7382 }
7383
Bob Liu2f6d9b92019-11-13 18:06:24 +08007384 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
7385 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06007386 schedule();
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007387 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007388 }
7389}
7390
7391static int io_uring_flush(struct file *file, void *data)
7392{
7393 struct io_ring_ctx *ctx = file->private_data;
7394
7395 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07007396
7397 /*
7398 * If the task is going away, cancel work it may have pending
7399 */
7400 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
7401 io_wq_cancel_pid(ctx->io_wq, task_pid_vnr(current));
7402
Jens Axboefcb323c2019-10-24 12:39:47 -06007403 return 0;
7404}
7405
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007406static void *io_uring_validate_mmap_request(struct file *file,
7407 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007408{
Jens Axboe2b188cc2019-01-07 10:46:33 -07007409 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007410 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007411 struct page *page;
7412 void *ptr;
7413
7414 switch (offset) {
7415 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00007416 case IORING_OFF_CQ_RING:
7417 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007418 break;
7419 case IORING_OFF_SQES:
7420 ptr = ctx->sq_sqes;
7421 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007422 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007423 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007424 }
7425
7426 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07007427 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007428 return ERR_PTR(-EINVAL);
7429
7430 return ptr;
7431}
7432
7433#ifdef CONFIG_MMU
7434
7435static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7436{
7437 size_t sz = vma->vm_end - vma->vm_start;
7438 unsigned long pfn;
7439 void *ptr;
7440
7441 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
7442 if (IS_ERR(ptr))
7443 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007444
7445 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
7446 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
7447}
7448
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007449#else /* !CONFIG_MMU */
7450
7451static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7452{
7453 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
7454}
7455
7456static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
7457{
7458 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
7459}
7460
7461static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
7462 unsigned long addr, unsigned long len,
7463 unsigned long pgoff, unsigned long flags)
7464{
7465 void *ptr;
7466
7467 ptr = io_uring_validate_mmap_request(file, pgoff, len);
7468 if (IS_ERR(ptr))
7469 return PTR_ERR(ptr);
7470
7471 return (unsigned long) ptr;
7472}
7473
7474#endif /* !CONFIG_MMU */
7475
Jens Axboe2b188cc2019-01-07 10:46:33 -07007476SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
7477 u32, min_complete, u32, flags, const sigset_t __user *, sig,
7478 size_t, sigsz)
7479{
7480 struct io_ring_ctx *ctx;
7481 long ret = -EBADF;
7482 int submitted = 0;
7483 struct fd f;
7484
Jens Axboeb41e9852020-02-17 09:52:41 -07007485 if (current->task_works)
7486 task_work_run();
7487
Jens Axboe6c271ce2019-01-10 11:22:30 -07007488 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007489 return -EINVAL;
7490
7491 f = fdget(fd);
7492 if (!f.file)
7493 return -EBADF;
7494
7495 ret = -EOPNOTSUPP;
7496 if (f.file->f_op != &io_uring_fops)
7497 goto out_fput;
7498
7499 ret = -ENXIO;
7500 ctx = f.file->private_data;
7501 if (!percpu_ref_tryget(&ctx->refs))
7502 goto out_fput;
7503
Jens Axboe6c271ce2019-01-10 11:22:30 -07007504 /*
7505 * For SQ polling, the thread will do all submissions and completions.
7506 * Just return the requested submit count, and wake the thread if
7507 * we were asked to.
7508 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007509 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007510 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07007511 if (!list_empty_careful(&ctx->cq_overflow_list))
7512 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007513 if (flags & IORING_ENTER_SQ_WAKEUP)
7514 wake_up(&ctx->sqo_wait);
7515 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007516 } else if (to_submit) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07007517 mutex_lock(&ctx->uring_lock);
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03007518 submitted = io_submit_sqes(ctx, to_submit, f.file, fd, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007519 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007520
7521 if (submitted != to_submit)
7522 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007523 }
7524 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07007525 unsigned nr_events = 0;
7526
Jens Axboe2b188cc2019-01-07 10:46:33 -07007527 min_complete = min(min_complete, ctx->cq_entries);
7528
Xiaoguang Wang32b22442020-03-11 09:26:09 +08007529 /*
7530 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
7531 * space applications don't need to do io completion events
7532 * polling again, they can rely on io_sq_thread to do polling
7533 * work, which can reduce cpu usage and uring_lock contention.
7534 */
7535 if (ctx->flags & IORING_SETUP_IOPOLL &&
7536 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Jens Axboedef596e2019-01-09 08:59:42 -07007537 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07007538 } else {
7539 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
7540 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007541 }
7542
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007543out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03007544 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007545out_fput:
7546 fdput(f);
7547 return submitted ? submitted : ret;
7548}
7549
Tobias Klauserbebdb652020-02-26 18:38:32 +01007550#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07007551static int io_uring_show_cred(int id, void *p, void *data)
7552{
7553 const struct cred *cred = p;
7554 struct seq_file *m = data;
7555 struct user_namespace *uns = seq_user_ns(m);
7556 struct group_info *gi;
7557 kernel_cap_t cap;
7558 unsigned __capi;
7559 int g;
7560
7561 seq_printf(m, "%5d\n", id);
7562 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
7563 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
7564 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
7565 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
7566 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
7567 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
7568 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
7569 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
7570 seq_puts(m, "\n\tGroups:\t");
7571 gi = cred->group_info;
7572 for (g = 0; g < gi->ngroups; g++) {
7573 seq_put_decimal_ull(m, g ? " " : "",
7574 from_kgid_munged(uns, gi->gid[g]));
7575 }
7576 seq_puts(m, "\n\tCapEff:\t");
7577 cap = cred->cap_effective;
7578 CAP_FOR_EACH_U32(__capi)
7579 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
7580 seq_putc(m, '\n');
7581 return 0;
7582}
7583
7584static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
7585{
7586 int i;
7587
7588 mutex_lock(&ctx->uring_lock);
7589 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
7590 for (i = 0; i < ctx->nr_user_files; i++) {
7591 struct fixed_file_table *table;
7592 struct file *f;
7593
7594 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7595 f = table->files[i & IORING_FILE_TABLE_MASK];
7596 if (f)
7597 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
7598 else
7599 seq_printf(m, "%5u: <none>\n", i);
7600 }
7601 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
7602 for (i = 0; i < ctx->nr_user_bufs; i++) {
7603 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
7604
7605 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
7606 (unsigned int) buf->len);
7607 }
7608 if (!idr_is_empty(&ctx->personality_idr)) {
7609 seq_printf(m, "Personalities:\n");
7610 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
7611 }
Jens Axboed7718a92020-02-14 22:23:12 -07007612 seq_printf(m, "PollList:\n");
7613 spin_lock_irq(&ctx->completion_lock);
7614 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
7615 struct hlist_head *list = &ctx->cancel_hash[i];
7616 struct io_kiocb *req;
7617
7618 hlist_for_each_entry(req, list, hash_node)
7619 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
7620 req->task->task_works != NULL);
7621 }
7622 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07007623 mutex_unlock(&ctx->uring_lock);
7624}
7625
7626static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
7627{
7628 struct io_ring_ctx *ctx = f->private_data;
7629
7630 if (percpu_ref_tryget(&ctx->refs)) {
7631 __io_uring_show_fdinfo(ctx, m);
7632 percpu_ref_put(&ctx->refs);
7633 }
7634}
Tobias Klauserbebdb652020-02-26 18:38:32 +01007635#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07007636
Jens Axboe2b188cc2019-01-07 10:46:33 -07007637static const struct file_operations io_uring_fops = {
7638 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06007639 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07007640 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007641#ifndef CONFIG_MMU
7642 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
7643 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
7644#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07007645 .poll = io_uring_poll,
7646 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01007647#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07007648 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01007649#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07007650};
7651
7652static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
7653 struct io_uring_params *p)
7654{
Hristo Venev75b28af2019-08-26 17:23:46 +00007655 struct io_rings *rings;
7656 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007657
Hristo Venev75b28af2019-08-26 17:23:46 +00007658 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
7659 if (size == SIZE_MAX)
7660 return -EOVERFLOW;
7661
7662 rings = io_mem_alloc(size);
7663 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007664 return -ENOMEM;
7665
Hristo Venev75b28af2019-08-26 17:23:46 +00007666 ctx->rings = rings;
7667 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
7668 rings->sq_ring_mask = p->sq_entries - 1;
7669 rings->cq_ring_mask = p->cq_entries - 1;
7670 rings->sq_ring_entries = p->sq_entries;
7671 rings->cq_ring_entries = p->cq_entries;
7672 ctx->sq_mask = rings->sq_ring_mask;
7673 ctx->cq_mask = rings->cq_ring_mask;
7674 ctx->sq_entries = rings->sq_ring_entries;
7675 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007676
7677 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07007678 if (size == SIZE_MAX) {
7679 io_mem_free(ctx->rings);
7680 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007681 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07007682 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007683
7684 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07007685 if (!ctx->sq_sqes) {
7686 io_mem_free(ctx->rings);
7687 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007688 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07007689 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007690
Jens Axboe2b188cc2019-01-07 10:46:33 -07007691 return 0;
7692}
7693
7694/*
7695 * Allocate an anonymous fd, this is what constitutes the application
7696 * visible backing of an io_uring instance. The application mmaps this
7697 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
7698 * we have to tie this fd to a socket for file garbage collection purposes.
7699 */
7700static int io_uring_get_fd(struct io_ring_ctx *ctx)
7701{
7702 struct file *file;
7703 int ret;
7704
7705#if defined(CONFIG_UNIX)
7706 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
7707 &ctx->ring_sock);
7708 if (ret)
7709 return ret;
7710#endif
7711
7712 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
7713 if (ret < 0)
7714 goto err;
7715
7716 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
7717 O_RDWR | O_CLOEXEC);
7718 if (IS_ERR(file)) {
7719 put_unused_fd(ret);
7720 ret = PTR_ERR(file);
7721 goto err;
7722 }
7723
7724#if defined(CONFIG_UNIX)
7725 ctx->ring_sock->file = file;
7726#endif
7727 fd_install(ret, file);
7728 return ret;
7729err:
7730#if defined(CONFIG_UNIX)
7731 sock_release(ctx->ring_sock);
7732 ctx->ring_sock = NULL;
7733#endif
7734 return ret;
7735}
7736
Xiaoguang Wang7f136572020-05-05 16:28:53 +08007737static int io_uring_create(unsigned entries, struct io_uring_params *p,
7738 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007739{
7740 struct user_struct *user = NULL;
7741 struct io_ring_ctx *ctx;
7742 bool account_mem;
7743 int ret;
7744
Jens Axboe8110c1a2019-12-28 15:39:54 -07007745 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007746 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07007747 if (entries > IORING_MAX_ENTRIES) {
7748 if (!(p->flags & IORING_SETUP_CLAMP))
7749 return -EINVAL;
7750 entries = IORING_MAX_ENTRIES;
7751 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007752
7753 /*
7754 * Use twice as many entries for the CQ ring. It's possible for the
7755 * application to drive a higher depth than the size of the SQ ring,
7756 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06007757 * some flexibility in overcommitting a bit. If the application has
7758 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
7759 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07007760 */
7761 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06007762 if (p->flags & IORING_SETUP_CQSIZE) {
7763 /*
7764 * If IORING_SETUP_CQSIZE is set, we do the same roundup
7765 * to a power-of-two, if it isn't already. We do NOT impose
7766 * any cq vs sq ring sizing.
7767 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07007768 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06007769 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07007770 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
7771 if (!(p->flags & IORING_SETUP_CLAMP))
7772 return -EINVAL;
7773 p->cq_entries = IORING_MAX_CQ_ENTRIES;
7774 }
Jens Axboe33a107f2019-10-04 12:10:03 -06007775 p->cq_entries = roundup_pow_of_two(p->cq_entries);
7776 } else {
7777 p->cq_entries = 2 * p->sq_entries;
7778 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007779
7780 user = get_uid(current_user());
7781 account_mem = !capable(CAP_IPC_LOCK);
7782
7783 if (account_mem) {
7784 ret = io_account_mem(user,
7785 ring_pages(p->sq_entries, p->cq_entries));
7786 if (ret) {
7787 free_uid(user);
7788 return ret;
7789 }
7790 }
7791
7792 ctx = io_ring_ctx_alloc(p);
7793 if (!ctx) {
7794 if (account_mem)
7795 io_unaccount_mem(user, ring_pages(p->sq_entries,
7796 p->cq_entries));
7797 free_uid(user);
7798 return -ENOMEM;
7799 }
7800 ctx->compat = in_compat_syscall();
7801 ctx->account_mem = account_mem;
7802 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07007803 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07007804
7805 ret = io_allocate_scq_urings(ctx, p);
7806 if (ret)
7807 goto err;
7808
Jens Axboe6c271ce2019-01-10 11:22:30 -07007809 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007810 if (ret)
7811 goto err;
7812
Jens Axboe2b188cc2019-01-07 10:46:33 -07007813 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007814 p->sq_off.head = offsetof(struct io_rings, sq.head);
7815 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
7816 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
7817 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
7818 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
7819 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
7820 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007821
7822 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007823 p->cq_off.head = offsetof(struct io_rings, cq.head);
7824 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
7825 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
7826 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
7827 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
7828 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06007829
Xiaoguang Wang7f136572020-05-05 16:28:53 +08007830 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
7831 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
7832 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL;
7833
7834 if (copy_to_user(params, p, sizeof(*p))) {
7835 ret = -EFAULT;
7836 goto err;
7837 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06007838 /*
7839 * Install ring fd as the very last thing, so we don't risk someone
7840 * having closed it before we finish setup
7841 */
7842 ret = io_uring_get_fd(ctx);
7843 if (ret < 0)
7844 goto err;
7845
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007846 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007847 return ret;
7848err:
7849 io_ring_ctx_wait_and_kill(ctx);
7850 return ret;
7851}
7852
7853/*
7854 * Sets up an aio uring context, and returns the fd. Applications asks for a
7855 * ring size, we return the actual sq/cq ring sizes (among other things) in the
7856 * params structure passed in.
7857 */
7858static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
7859{
7860 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007861 int i;
7862
7863 if (copy_from_user(&p, params, sizeof(p)))
7864 return -EFAULT;
7865 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
7866 if (p.resv[i])
7867 return -EINVAL;
7868 }
7869
Jens Axboe6c271ce2019-01-10 11:22:30 -07007870 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07007871 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03007872 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007873 return -EINVAL;
7874
Xiaoguang Wang7f136572020-05-05 16:28:53 +08007875 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007876}
7877
7878SYSCALL_DEFINE2(io_uring_setup, u32, entries,
7879 struct io_uring_params __user *, params)
7880{
7881 return io_uring_setup(entries, params);
7882}
7883
Jens Axboe66f4af92020-01-16 15:36:52 -07007884static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
7885{
7886 struct io_uring_probe *p;
7887 size_t size;
7888 int i, ret;
7889
7890 size = struct_size(p, ops, nr_args);
7891 if (size == SIZE_MAX)
7892 return -EOVERFLOW;
7893 p = kzalloc(size, GFP_KERNEL);
7894 if (!p)
7895 return -ENOMEM;
7896
7897 ret = -EFAULT;
7898 if (copy_from_user(p, arg, size))
7899 goto out;
7900 ret = -EINVAL;
7901 if (memchr_inv(p, 0, size))
7902 goto out;
7903
7904 p->last_op = IORING_OP_LAST - 1;
7905 if (nr_args > IORING_OP_LAST)
7906 nr_args = IORING_OP_LAST;
7907
7908 for (i = 0; i < nr_args; i++) {
7909 p->ops[i].op = i;
7910 if (!io_op_defs[i].not_supported)
7911 p->ops[i].flags = IO_URING_OP_SUPPORTED;
7912 }
7913 p->ops_len = i;
7914
7915 ret = 0;
7916 if (copy_to_user(arg, p, size))
7917 ret = -EFAULT;
7918out:
7919 kfree(p);
7920 return ret;
7921}
7922
Jens Axboe071698e2020-01-28 10:04:42 -07007923static int io_register_personality(struct io_ring_ctx *ctx)
7924{
7925 const struct cred *creds = get_current_cred();
7926 int id;
7927
7928 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
7929 USHRT_MAX, GFP_KERNEL);
7930 if (id < 0)
7931 put_cred(creds);
7932 return id;
7933}
7934
7935static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
7936{
7937 const struct cred *old_creds;
7938
7939 old_creds = idr_remove(&ctx->personality_idr, id);
7940 if (old_creds) {
7941 put_cred(old_creds);
7942 return 0;
7943 }
7944
7945 return -EINVAL;
7946}
7947
7948static bool io_register_op_must_quiesce(int op)
7949{
7950 switch (op) {
7951 case IORING_UNREGISTER_FILES:
7952 case IORING_REGISTER_FILES_UPDATE:
7953 case IORING_REGISTER_PROBE:
7954 case IORING_REGISTER_PERSONALITY:
7955 case IORING_UNREGISTER_PERSONALITY:
7956 return false;
7957 default:
7958 return true;
7959 }
7960}
7961
Jens Axboeedafcce2019-01-09 09:16:05 -07007962static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
7963 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06007964 __releases(ctx->uring_lock)
7965 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07007966{
7967 int ret;
7968
Jens Axboe35fa71a2019-04-22 10:23:23 -06007969 /*
7970 * We're inside the ring mutex, if the ref is already dying, then
7971 * someone else killed the ctx or is already going through
7972 * io_uring_register().
7973 */
7974 if (percpu_ref_is_dying(&ctx->refs))
7975 return -ENXIO;
7976
Jens Axboe071698e2020-01-28 10:04:42 -07007977 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007978 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06007979
Jens Axboe05f3fb32019-12-09 11:22:50 -07007980 /*
7981 * Drop uring mutex before waiting for references to exit. If
7982 * another thread is currently inside io_uring_enter() it might
7983 * need to grab the uring_lock to make progress. If we hold it
7984 * here across the drain wait, then we can deadlock. It's safe
7985 * to drop the mutex here, since no new references will come in
7986 * after we've killed the percpu ref.
7987 */
7988 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f158b42020-05-14 17:18:39 -06007989 ret = wait_for_completion_interruptible(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007990 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007991 if (ret) {
7992 percpu_ref_resurrect(&ctx->refs);
7993 ret = -EINTR;
7994 goto out;
7995 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007996 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007997
7998 switch (opcode) {
7999 case IORING_REGISTER_BUFFERS:
8000 ret = io_sqe_buffer_register(ctx, arg, nr_args);
8001 break;
8002 case IORING_UNREGISTER_BUFFERS:
8003 ret = -EINVAL;
8004 if (arg || nr_args)
8005 break;
8006 ret = io_sqe_buffer_unregister(ctx);
8007 break;
Jens Axboe6b063142019-01-10 22:13:58 -07008008 case IORING_REGISTER_FILES:
8009 ret = io_sqe_files_register(ctx, arg, nr_args);
8010 break;
8011 case IORING_UNREGISTER_FILES:
8012 ret = -EINVAL;
8013 if (arg || nr_args)
8014 break;
8015 ret = io_sqe_files_unregister(ctx);
8016 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06008017 case IORING_REGISTER_FILES_UPDATE:
8018 ret = io_sqe_files_update(ctx, arg, nr_args);
8019 break;
Jens Axboe9b402842019-04-11 11:45:41 -06008020 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07008021 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06008022 ret = -EINVAL;
8023 if (nr_args != 1)
8024 break;
8025 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07008026 if (ret)
8027 break;
8028 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
8029 ctx->eventfd_async = 1;
8030 else
8031 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06008032 break;
8033 case IORING_UNREGISTER_EVENTFD:
8034 ret = -EINVAL;
8035 if (arg || nr_args)
8036 break;
8037 ret = io_eventfd_unregister(ctx);
8038 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07008039 case IORING_REGISTER_PROBE:
8040 ret = -EINVAL;
8041 if (!arg || nr_args > 256)
8042 break;
8043 ret = io_probe(ctx, arg, nr_args);
8044 break;
Jens Axboe071698e2020-01-28 10:04:42 -07008045 case IORING_REGISTER_PERSONALITY:
8046 ret = -EINVAL;
8047 if (arg || nr_args)
8048 break;
8049 ret = io_register_personality(ctx);
8050 break;
8051 case IORING_UNREGISTER_PERSONALITY:
8052 ret = -EINVAL;
8053 if (arg)
8054 break;
8055 ret = io_unregister_personality(ctx, nr_args);
8056 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008057 default:
8058 ret = -EINVAL;
8059 break;
8060 }
8061
Jens Axboe071698e2020-01-28 10:04:42 -07008062 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008063 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07008064 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07008065out:
Jens Axboe0f158b42020-05-14 17:18:39 -06008066 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008067 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008068 return ret;
8069}
8070
8071SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
8072 void __user *, arg, unsigned int, nr_args)
8073{
8074 struct io_ring_ctx *ctx;
8075 long ret = -EBADF;
8076 struct fd f;
8077
8078 f = fdget(fd);
8079 if (!f.file)
8080 return -EBADF;
8081
8082 ret = -EOPNOTSUPP;
8083 if (f.file->f_op != &io_uring_fops)
8084 goto out_fput;
8085
8086 ctx = f.file->private_data;
8087
8088 mutex_lock(&ctx->uring_lock);
8089 ret = __io_uring_register(ctx, opcode, arg, nr_args);
8090 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02008091 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
8092 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07008093out_fput:
8094 fdput(f);
8095 return ret;
8096}
8097
Jens Axboe2b188cc2019-01-07 10:46:33 -07008098static int __init io_uring_init(void)
8099{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008100#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
8101 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
8102 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
8103} while (0)
8104
8105#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
8106 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
8107 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
8108 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
8109 BUILD_BUG_SQE_ELEM(1, __u8, flags);
8110 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
8111 BUILD_BUG_SQE_ELEM(4, __s32, fd);
8112 BUILD_BUG_SQE_ELEM(8, __u64, off);
8113 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
8114 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008115 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008116 BUILD_BUG_SQE_ELEM(24, __u32, len);
8117 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
8118 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
8119 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
8120 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
8121 BUILD_BUG_SQE_ELEM(28, __u16, poll_events);
8122 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
8123 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
8124 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
8125 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
8126 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
8127 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
8128 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
8129 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008130 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008131 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
8132 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
8133 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008134 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008135
Jens Axboed3656342019-12-18 09:50:26 -07008136 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07008137 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008138 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
8139 return 0;
8140};
8141__initcall(io_uring_init);