blob: 4c2fe06ae20b49ca8e6b925b77fe3e86258015e4 [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
Jens Axboe52de1fe2020-02-27 10:15:42 -070047#include <net/compat.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070048#include <linux/refcount.h>
49#include <linux/uio.h>
Pavel Begunkov6b47ee62020-01-18 20:22:41 +030050#include <linux/bits.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070051
52#include <linux/sched/signal.h>
53#include <linux/fs.h>
54#include <linux/file.h>
55#include <linux/fdtable.h>
56#include <linux/mm.h>
57#include <linux/mman.h>
58#include <linux/mmu_context.h>
59#include <linux/percpu.h>
60#include <linux/slab.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070061#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070062#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070063#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070064#include <linux/net.h>
65#include <net/sock.h>
66#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070067#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070068#include <linux/anon_inodes.h>
69#include <linux/sched/mm.h>
70#include <linux/uaccess.h>
71#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070072#include <linux/sizes.h>
73#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070074#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070075#include <linux/namei.h>
76#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070077#include <linux/fadvise.h>
Jens Axboe3e4827b2020-01-08 15:18:09 -070078#include <linux/eventpoll.h>
Jens Axboeff002b32020-02-07 16:05:21 -070079#include <linux/fs_struct.h>
Pavel Begunkov7d67af22020-02-24 11:32:45 +030080#include <linux/splice.h>
Jens Axboeb41e9852020-02-17 09:52:41 -070081#include <linux/task_work.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070082
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020083#define CREATE_TRACE_POINTS
84#include <trace/events/io_uring.h>
85
Jens Axboe2b188cc2019-01-07 10:46:33 -070086#include <uapi/linux/io_uring.h>
87
88#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060089#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070090
Daniel Xu5277dea2019-09-14 14:23:45 -070091#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060092#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060093
94/*
95 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
96 */
97#define IORING_FILE_TABLE_SHIFT 9
98#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
99#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
100#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700101
102struct io_uring {
103 u32 head ____cacheline_aligned_in_smp;
104 u32 tail ____cacheline_aligned_in_smp;
105};
106
Stefan Bühler1e84b972019-04-24 23:54:16 +0200107/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000108 * This data is shared with the application through the mmap at offsets
109 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200110 *
111 * The offsets to the member fields are published through struct
112 * io_sqring_offsets when calling io_uring_setup.
113 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000114struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200115 /*
116 * Head and tail offsets into the ring; the offsets need to be
117 * masked to get valid indices.
118 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000119 * The kernel controls head of the sq ring and the tail of the cq ring,
120 * and the application controls tail of the sq ring and the head of the
121 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200122 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000123 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200124 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000125 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200126 * ring_entries - 1)
127 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000128 u32 sq_ring_mask, cq_ring_mask;
129 /* Ring sizes (constant, power of 2) */
130 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200131 /*
132 * Number of invalid entries dropped by the kernel due to
133 * invalid index stored in array
134 *
135 * Written by the kernel, shouldn't be modified by the
136 * application (i.e. get number of "new events" by comparing to
137 * cached value).
138 *
139 * After a new SQ head value was read by the application this
140 * counter includes all submissions that were dropped reaching
141 * the new SQ head (and possibly more).
142 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000143 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200144 /*
145 * Runtime flags
146 *
147 * Written by the kernel, shouldn't be modified by the
148 * application.
149 *
150 * The application needs a full memory barrier before checking
151 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
152 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000153 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200154 /*
155 * Number of completion events lost because the queue was full;
156 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800157 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200158 * the completion queue.
159 *
160 * Written by the kernel, shouldn't be modified by the
161 * application (i.e. get number of "new events" by comparing to
162 * cached value).
163 *
164 * As completion events come in out of order this counter is not
165 * ordered with any other data.
166 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000167 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200168 /*
169 * Ring buffer of completion events.
170 *
171 * The kernel writes completion events fresh every time they are
172 * produced, so the application is allowed to modify pending
173 * entries.
174 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000175 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700176};
177
Jens Axboeedafcce2019-01-09 09:16:05 -0700178struct io_mapped_ubuf {
179 u64 ubuf;
180 size_t len;
181 struct bio_vec *bvec;
182 unsigned int nr_bvecs;
183};
184
Jens Axboe65e19f52019-10-26 07:20:21 -0600185struct fixed_file_table {
186 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700187};
188
Xiaoguang Wang05589552020-03-31 14:05:18 +0800189struct fixed_file_ref_node {
190 struct percpu_ref refs;
191 struct list_head node;
192 struct list_head file_list;
193 struct fixed_file_data *file_data;
194 struct work_struct work;
195};
196
Jens Axboe05f3fb32019-12-09 11:22:50 -0700197struct fixed_file_data {
198 struct fixed_file_table *table;
199 struct io_ring_ctx *ctx;
200
Xiaoguang Wang05589552020-03-31 14:05:18 +0800201 struct percpu_ref *cur_refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700202 struct percpu_ref refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700203 struct completion done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800204 struct list_head ref_list;
205 spinlock_t lock;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700206};
207
Jens Axboe5a2e7452020-02-23 16:23:11 -0700208struct io_buffer {
209 struct list_head list;
210 __u64 addr;
211 __s32 len;
212 __u16 bid;
213};
214
Jens Axboe2b188cc2019-01-07 10:46:33 -0700215struct io_ring_ctx {
216 struct {
217 struct percpu_ref refs;
218 } ____cacheline_aligned_in_smp;
219
220 struct {
221 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800222 unsigned int compat: 1;
223 unsigned int account_mem: 1;
224 unsigned int cq_overflow_flushed: 1;
225 unsigned int drain_next: 1;
226 unsigned int eventfd_async: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700227
Hristo Venev75b28af2019-08-26 17:23:46 +0000228 /*
229 * Ring buffer of indices into array of io_uring_sqe, which is
230 * mmapped by the application using the IORING_OFF_SQES offset.
231 *
232 * This indirection could e.g. be used to assign fixed
233 * io_uring_sqe entries to operations and only submit them to
234 * the queue when needed.
235 *
236 * The kernel modifies neither the indices array nor the entries
237 * array.
238 */
239 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700240 unsigned cached_sq_head;
241 unsigned sq_entries;
242 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700243 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600244 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700245 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700246 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600247
248 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600249 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700250 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700251
Jens Axboefcb323c2019-10-24 12:39:47 -0600252 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700253 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700254 } ____cacheline_aligned_in_smp;
255
Hristo Venev75b28af2019-08-26 17:23:46 +0000256 struct io_rings *rings;
257
Jens Axboe2b188cc2019-01-07 10:46:33 -0700258 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600259 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700260 struct task_struct *sqo_thread; /* if using sq thread polling */
261 struct mm_struct *sqo_mm;
262 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700263
Jens Axboe6b063142019-01-10 22:13:58 -0700264 /*
265 * If used, fixed file set. Writers must ensure that ->refs is dead,
266 * readers must ensure that ->refs is alive as long as the file* is
267 * used. Only updated through io_uring_register(2).
268 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700269 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700270 unsigned nr_user_files;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300271 int ring_fd;
272 struct file *ring_file;
Jens Axboe6b063142019-01-10 22:13:58 -0700273
Jens Axboeedafcce2019-01-09 09:16:05 -0700274 /* if used, fixed mapped user buffers */
275 unsigned nr_user_bufs;
276 struct io_mapped_ubuf *user_bufs;
277
Jens Axboe2b188cc2019-01-07 10:46:33 -0700278 struct user_struct *user;
279
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700280 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700281
Jens Axboe206aefd2019-11-07 18:27:42 -0700282 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
283 struct completion *completions;
284
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700285 /* if all else fails... */
286 struct io_kiocb *fallback_req;
287
Jens Axboe206aefd2019-11-07 18:27:42 -0700288#if defined(CONFIG_UNIX)
289 struct socket *ring_sock;
290#endif
291
Jens Axboe5a2e7452020-02-23 16:23:11 -0700292 struct idr io_buffer_idr;
293
Jens Axboe071698e2020-01-28 10:04:42 -0700294 struct idr personality_idr;
295
Jens Axboe206aefd2019-11-07 18:27:42 -0700296 struct {
297 unsigned cached_cq_tail;
298 unsigned cq_entries;
299 unsigned cq_mask;
300 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700301 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700302 struct wait_queue_head cq_wait;
303 struct fasync_struct *cq_fasync;
304 struct eventfd_ctx *cq_ev_fd;
305 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700306
307 struct {
308 struct mutex uring_lock;
309 wait_queue_head_t wait;
310 } ____cacheline_aligned_in_smp;
311
312 struct {
313 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700314
Jens Axboedef596e2019-01-09 08:59:42 -0700315 /*
316 * ->poll_list is protected by the ctx->uring_lock for
317 * io_uring instances that don't use IORING_SETUP_SQPOLL.
318 * For SQPOLL, only the single threaded io_sq_thread() will
319 * manipulate the list, hence no extra locking is needed there.
320 */
321 struct list_head poll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700322 struct hlist_head *cancel_hash;
323 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700324 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600325
326 spinlock_t inflight_lock;
327 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700328 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600329
330 struct work_struct exit_work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700331};
332
Jens Axboe09bb8392019-03-13 12:39:28 -0600333/*
334 * First field must be the file pointer in all the
335 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
336 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700337struct io_poll_iocb {
338 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700339 union {
340 struct wait_queue_head *head;
341 u64 addr;
342 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700343 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600344 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700345 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700346 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700347};
348
Jens Axboeb5dba592019-12-11 14:02:38 -0700349struct io_close {
350 struct file *file;
351 struct file *put_file;
352 int fd;
353};
354
Jens Axboead8a48a2019-11-15 08:49:11 -0700355struct io_timeout_data {
356 struct io_kiocb *req;
357 struct hrtimer timer;
358 struct timespec64 ts;
359 enum hrtimer_mode mode;
360};
361
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700362struct io_accept {
363 struct file *file;
364 struct sockaddr __user *addr;
365 int __user *addr_len;
366 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600367 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700368};
369
370struct io_sync {
371 struct file *file;
372 loff_t len;
373 loff_t off;
374 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700375 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700376};
377
Jens Axboefbf23842019-12-17 18:45:56 -0700378struct io_cancel {
379 struct file *file;
380 u64 addr;
381};
382
Jens Axboeb29472e2019-12-17 18:50:29 -0700383struct io_timeout {
384 struct file *file;
385 u64 addr;
386 int flags;
Pavel Begunkovb55ce732020-04-15 00:39:49 +0300387 u32 count;
Jens Axboeb29472e2019-12-17 18:50:29 -0700388};
389
Jens Axboe9adbd452019-12-20 08:45:55 -0700390struct io_rw {
391 /* NOTE: kiocb has the file as the first member, so don't do it here */
392 struct kiocb kiocb;
393 u64 addr;
394 u64 len;
395};
396
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700397struct io_connect {
398 struct file *file;
399 struct sockaddr __user *addr;
400 int addr_len;
401};
402
Jens Axboee47293f2019-12-20 08:58:21 -0700403struct io_sr_msg {
404 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700405 union {
406 struct user_msghdr __user *msg;
407 void __user *buf;
408 };
Jens Axboee47293f2019-12-20 08:58:21 -0700409 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700410 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700411 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700412 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700413};
414
Jens Axboe15b71ab2019-12-11 11:20:36 -0700415struct io_open {
416 struct file *file;
417 int dfd;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700418 union {
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700419 unsigned mask;
420 };
Jens Axboe15b71ab2019-12-11 11:20:36 -0700421 struct filename *filename;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700422 struct statx __user *buffer;
Jens Axboec12cedf2020-01-08 17:41:21 -0700423 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600424 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700425};
426
Jens Axboe05f3fb32019-12-09 11:22:50 -0700427struct io_files_update {
428 struct file *file;
429 u64 arg;
430 u32 nr_args;
431 u32 offset;
432};
433
Jens Axboe4840e412019-12-25 22:03:45 -0700434struct io_fadvise {
435 struct file *file;
436 u64 offset;
437 u32 len;
438 u32 advice;
439};
440
Jens Axboec1ca7572019-12-25 22:18:28 -0700441struct io_madvise {
442 struct file *file;
443 u64 addr;
444 u32 len;
445 u32 advice;
446};
447
Jens Axboe3e4827b2020-01-08 15:18:09 -0700448struct io_epoll {
449 struct file *file;
450 int epfd;
451 int op;
452 int fd;
453 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700454};
455
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300456struct io_splice {
457 struct file *file_out;
458 struct file *file_in;
459 loff_t off_out;
460 loff_t off_in;
461 u64 len;
462 unsigned int flags;
463};
464
Jens Axboeddf0322d2020-02-23 16:41:33 -0700465struct io_provide_buf {
466 struct file *file;
467 __u64 addr;
468 __s32 len;
469 __u32 bgid;
470 __u16 nbufs;
471 __u16 bid;
472};
473
Jens Axboef499a022019-12-02 16:28:46 -0700474struct io_async_connect {
475 struct sockaddr_storage address;
476};
477
Jens Axboe03b12302019-12-02 18:50:25 -0700478struct io_async_msghdr {
479 struct iovec fast_iov[UIO_FASTIOV];
480 struct iovec *iov;
481 struct sockaddr __user *uaddr;
482 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700483 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700484};
485
Jens Axboef67676d2019-12-02 11:03:47 -0700486struct io_async_rw {
487 struct iovec fast_iov[UIO_FASTIOV];
488 struct iovec *iov;
489 ssize_t nr_segs;
490 ssize_t size;
491};
492
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700493struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700494 union {
495 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700496 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700497 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700498 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700499 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700500};
501
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300502enum {
503 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
504 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
505 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
506 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
507 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700508 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300509
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300510 REQ_F_LINK_HEAD_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300511 REQ_F_LINK_NEXT_BIT,
512 REQ_F_FAIL_LINK_BIT,
513 REQ_F_INFLIGHT_BIT,
514 REQ_F_CUR_POS_BIT,
515 REQ_F_NOWAIT_BIT,
516 REQ_F_IOPOLL_COMPLETED_BIT,
517 REQ_F_LINK_TIMEOUT_BIT,
518 REQ_F_TIMEOUT_BIT,
519 REQ_F_ISREG_BIT,
520 REQ_F_MUST_PUNT_BIT,
521 REQ_F_TIMEOUT_NOSEQ_BIT,
522 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300523 REQ_F_NEED_CLEANUP_BIT,
Jens Axboe2ca10252020-02-13 17:17:35 -0700524 REQ_F_OVERFLOW_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700525 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700526 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600527 REQ_F_NO_FILE_TABLE_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700528
529 /* not a real bit, just to check we're not overflowing the space */
530 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300531};
532
533enum {
534 /* ctx owns file */
535 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
536 /* drain existing IO first */
537 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
538 /* linked sqes */
539 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
540 /* doesn't sever on completion < 0 */
541 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
542 /* IOSQE_ASYNC */
543 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700544 /* IOSQE_BUFFER_SELECT */
545 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300546
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300547 /* head of a link */
548 REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300549 /* already grabbed next link */
550 REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT),
551 /* fail rest of links */
552 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
553 /* on inflight list */
554 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
555 /* read/write uses file position */
556 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
557 /* must not punt to workers */
558 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
559 /* polled IO has completed */
560 REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT),
561 /* has linked timeout */
562 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
563 /* timeout request */
564 REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT),
565 /* regular file */
566 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
567 /* must be punted even for NONBLOCK */
568 REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT),
569 /* no timeout sequence */
570 REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT),
571 /* completion under lock */
572 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300573 /* needs cleanup */
574 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboe2ca10252020-02-13 17:17:35 -0700575 /* in overflow list */
576 REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700577 /* already went through poll handler */
578 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700579 /* buffer already selected */
580 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600581 /* doesn't need file table for this request */
582 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700583};
584
585struct async_poll {
586 struct io_poll_iocb poll;
587 struct io_wq_work work;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300588};
589
Jens Axboe09bb8392019-03-13 12:39:28 -0600590/*
591 * NOTE! Each of the iocb union members has the file pointer
592 * as the first entry in their struct definition. So you can
593 * access the file pointer through any of the sub-structs,
594 * or directly as just 'ki_filp' in this struct.
595 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700596struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700597 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600598 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700599 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700600 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700601 struct io_accept accept;
602 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700603 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700604 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700605 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700606 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700607 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700608 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700609 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700610 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700611 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700612 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300613 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700614 struct io_provide_buf pbuf;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700615 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700616
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700617 struct io_async_ctx *io;
Pavel Begunkovc398ecb2020-04-09 08:17:59 +0300618 int cflags;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300619 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700620 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700621
622 struct io_ring_ctx *ctx;
Jens Axboed7718a92020-02-14 22:23:12 -0700623 struct list_head list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700624 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700625 refcount_t refs;
Jens Axboe3537b6a2020-04-03 11:19:06 -0600626 struct task_struct *task;
627 unsigned long fsize;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700628 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600629 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600630 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700631
Jens Axboed7718a92020-02-14 22:23:12 -0700632 struct list_head link_list;
633
Jens Axboefcb323c2019-10-24 12:39:47 -0600634 struct list_head inflight_entry;
635
Xiaoguang Wang05589552020-03-31 14:05:18 +0800636 struct percpu_ref *fixed_file_refs;
637
Jens Axboeb41e9852020-02-17 09:52:41 -0700638 union {
639 /*
640 * Only commands that never go async can use the below fields,
Jens Axboed7718a92020-02-14 22:23:12 -0700641 * obviously. Right now only IORING_OP_POLL_ADD uses them, and
642 * async armed poll handlers for regular commands. The latter
643 * restore the work, if needed.
Jens Axboeb41e9852020-02-17 09:52:41 -0700644 */
645 struct {
Jens Axboeb41e9852020-02-17 09:52:41 -0700646 struct callback_head task_work;
Jens Axboed7718a92020-02-14 22:23:12 -0700647 struct hlist_node hash_node;
648 struct async_poll *apoll;
Jens Axboeb41e9852020-02-17 09:52:41 -0700649 };
650 struct io_wq_work work;
651 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700652};
653
654#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700655#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700656
Jens Axboe9a56a232019-01-09 09:06:50 -0700657struct io_submit_state {
658 struct blk_plug plug;
659
660 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700661 * io_kiocb alloc cache
662 */
663 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300664 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700665
666 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700667 * File reference cache
668 */
669 struct file *file;
670 unsigned int fd;
671 unsigned int has_refs;
672 unsigned int used_refs;
673 unsigned int ios_left;
674};
675
Jens Axboed3656342019-12-18 09:50:26 -0700676struct io_op_def {
677 /* needs req->io allocated for deferral/async */
678 unsigned async_ctx : 1;
679 /* needs current->mm setup, does mm access */
680 unsigned needs_mm : 1;
681 /* needs req->file assigned */
682 unsigned needs_file : 1;
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 Axboed3656342019-12-18 09:50:26 -0700789 .needs_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700790 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700791 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300792 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700793 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700794 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700795 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300796 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700797 .needs_mm = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700798 .needs_fs = 1,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600799 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700800 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300801 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700802 .needs_mm = 1,
803 .needs_file = 1,
804 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700805 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700806 .buffer_select = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700807 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300808 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700809 .needs_mm = 1,
810 .needs_file = 1,
811 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700812 .pollout = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700813 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300814 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700815 .needs_file = 1,
816 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300817 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700818 .needs_mm = 1,
819 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300820 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700821 .needs_mm = 1,
822 .needs_file = 1,
823 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700824 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700825 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300826 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700827 .needs_mm = 1,
828 .needs_file = 1,
829 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700830 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700831 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700832 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300833 [IORING_OP_OPENAT2] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700834 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700835 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700836 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700837 [IORING_OP_EPOLL_CTL] = {
838 .unbound_nonreg_file = 1,
839 .file_table = 1,
840 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300841 [IORING_OP_SPLICE] = {
842 .needs_file = 1,
843 .hash_reg_file = 1,
844 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700845 },
846 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700847 [IORING_OP_REMOVE_BUFFERS] = {},
Jens Axboed3656342019-12-18 09:50:26 -0700848};
849
Jens Axboe561fb042019-10-24 07:25:42 -0600850static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700851static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800852static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700853static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700854static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
855static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700856static int __io_sqe_files_update(struct io_ring_ctx *ctx,
857 struct io_uring_files_update *ip,
858 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700859static int io_grab_files(struct io_kiocb *req);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300860static void io_cleanup_req(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700861static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
862 int fd, struct file **out_file, bool fixed);
863static void __io_queue_sqe(struct io_kiocb *req,
864 const struct io_uring_sqe *sqe);
Jens Axboede0617e2019-04-06 21:51:27 -0600865
Jens Axboe2b188cc2019-01-07 10:46:33 -0700866static struct kmem_cache *req_cachep;
867
868static const struct file_operations io_uring_fops;
869
870struct sock *io_uring_get_socket(struct file *file)
871{
872#if defined(CONFIG_UNIX)
873 if (file->f_op == &io_uring_fops) {
874 struct io_ring_ctx *ctx = file->private_data;
875
876 return ctx->ring_sock->sk;
877 }
878#endif
879 return NULL;
880}
881EXPORT_SYMBOL(io_uring_get_socket);
882
883static void io_ring_ctx_ref_free(struct percpu_ref *ref)
884{
885 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
886
Jens Axboe206aefd2019-11-07 18:27:42 -0700887 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700888}
889
890static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
891{
892 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700893 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700894
895 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
896 if (!ctx)
897 return NULL;
898
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700899 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
900 if (!ctx->fallback_req)
901 goto err;
902
Jens Axboe206aefd2019-11-07 18:27:42 -0700903 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
904 if (!ctx->completions)
905 goto err;
906
Jens Axboe78076bb2019-12-04 19:56:40 -0700907 /*
908 * Use 5 bits less than the max cq entries, that should give us around
909 * 32 entries per hash list if totally full and uniformly spread.
910 */
911 hash_bits = ilog2(p->cq_entries);
912 hash_bits -= 5;
913 if (hash_bits <= 0)
914 hash_bits = 1;
915 ctx->cancel_hash_bits = hash_bits;
916 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
917 GFP_KERNEL);
918 if (!ctx->cancel_hash)
919 goto err;
920 __hash_init(ctx->cancel_hash, 1U << hash_bits);
921
Roman Gushchin21482892019-05-07 10:01:48 -0700922 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700923 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
924 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700925
926 ctx->flags = p->flags;
927 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700928 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700929 init_completion(&ctx->completions[0]);
930 init_completion(&ctx->completions[1]);
Jens Axboe5a2e7452020-02-23 16:23:11 -0700931 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -0700932 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700933 mutex_init(&ctx->uring_lock);
934 init_waitqueue_head(&ctx->wait);
935 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700936 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600937 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600938 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600939 init_waitqueue_head(&ctx->inflight_wait);
940 spin_lock_init(&ctx->inflight_lock);
941 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700942 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700943err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700944 if (ctx->fallback_req)
945 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700946 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700947 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700948 kfree(ctx);
949 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700950}
951
Bob Liu9d858b22019-11-13 18:06:25 +0800952static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600953{
Jackie Liua197f662019-11-08 08:09:12 -0700954 struct io_ring_ctx *ctx = req->ctx;
955
Pavel Begunkov31af27c2020-04-15 00:39:50 +0300956 return req->sequence != ctx->cached_cq_tail
957 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600958}
959
Bob Liu9d858b22019-11-13 18:06:25 +0800960static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600961{
Pavel Begunkov87987892020-01-18 01:22:30 +0300962 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +0800963 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600964
Bob Liu9d858b22019-11-13 18:06:25 +0800965 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600966}
967
968static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600969{
970 struct io_kiocb *req;
971
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600972 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800973 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600974 list_del_init(&req->list);
975 return req;
976 }
977
978 return NULL;
979}
980
Jens Axboe5262f562019-09-17 12:26:57 -0600981static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
982{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600983 struct io_kiocb *req;
984
985 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700986 if (req) {
987 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
988 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800989 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700990 list_del_init(&req->list);
991 return req;
992 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600993 }
994
995 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600996}
997
Jens Axboede0617e2019-04-06 21:51:27 -0600998static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700999{
Hristo Venev75b28af2019-08-26 17:23:46 +00001000 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001001
Pavel Begunkov07910152020-01-17 03:52:46 +03001002 /* order cqe stores with ring update */
1003 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001004
Pavel Begunkov07910152020-01-17 03:52:46 +03001005 if (wq_has_sleeper(&ctx->cq_wait)) {
1006 wake_up_interruptible(&ctx->cq_wait);
1007 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001008 }
1009}
1010
Jens Axboecccf0ee2020-01-27 16:34:48 -07001011static inline void io_req_work_grab_env(struct io_kiocb *req,
1012 const struct io_op_def *def)
Jens Axboe18d9be12019-09-10 09:13:05 -06001013{
Jens Axboecccf0ee2020-01-27 16:34:48 -07001014 if (!req->work.mm && def->needs_mm) {
1015 mmgrab(current->mm);
1016 req->work.mm = current->mm;
1017 }
1018 if (!req->work.creds)
1019 req->work.creds = get_current_cred();
Jens Axboeff002b32020-02-07 16:05:21 -07001020 if (!req->work.fs && def->needs_fs) {
1021 spin_lock(&current->fs->lock);
1022 if (!current->fs->in_exec) {
1023 req->work.fs = current->fs;
1024 req->work.fs->users++;
1025 } else {
1026 req->work.flags |= IO_WQ_WORK_CANCEL;
1027 }
1028 spin_unlock(&current->fs->lock);
1029 }
Jens Axboe6ab23142020-02-08 20:23:59 -07001030 if (!req->work.task_pid)
1031 req->work.task_pid = task_pid_vnr(current);
Jens Axboecccf0ee2020-01-27 16:34:48 -07001032}
1033
1034static inline void io_req_work_drop_env(struct io_kiocb *req)
1035{
1036 if (req->work.mm) {
1037 mmdrop(req->work.mm);
1038 req->work.mm = NULL;
1039 }
1040 if (req->work.creds) {
1041 put_cred(req->work.creds);
1042 req->work.creds = NULL;
1043 }
Jens Axboeff002b32020-02-07 16:05:21 -07001044 if (req->work.fs) {
1045 struct fs_struct *fs = req->work.fs;
1046
1047 spin_lock(&req->work.fs->lock);
1048 if (--fs->users)
1049 fs = NULL;
1050 spin_unlock(&req->work.fs->lock);
1051 if (fs)
1052 free_fs_struct(fs);
1053 }
Jens Axboe561fb042019-10-24 07:25:42 -06001054}
1055
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001056static inline void io_prep_async_work(struct io_kiocb *req,
Jens Axboe94ae5e72019-11-14 19:39:52 -07001057 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -06001058{
Jens Axboed3656342019-12-18 09:50:26 -07001059 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe54a91f32019-09-10 09:15:04 -06001060
Jens Axboed3656342019-12-18 09:50:26 -07001061 if (req->flags & REQ_F_ISREG) {
1062 if (def->hash_reg_file)
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001063 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001064 } else {
1065 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001066 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001067 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001068
1069 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -06001070
Jens Axboe94ae5e72019-11-14 19:39:52 -07001071 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001072}
1073
Jackie Liua197f662019-11-08 08:09:12 -07001074static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001075{
Jackie Liua197f662019-11-08 08:09:12 -07001076 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001077 struct io_kiocb *link;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001078
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001079 io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06001080
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001081 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1082 &req->work, req->flags);
1083 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001084
1085 if (link)
1086 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -06001087}
1088
Jens Axboe5262f562019-09-17 12:26:57 -06001089static void io_kill_timeout(struct io_kiocb *req)
1090{
1091 int ret;
1092
Jens Axboe2d283902019-12-04 11:08:05 -07001093 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001094 if (ret != -1) {
1095 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -06001096 list_del_init(&req->list);
Pavel Begunkovf0e20b82020-03-07 01:15:22 +03001097 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001098 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001099 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001100 }
1101}
1102
1103static void io_kill_timeouts(struct io_ring_ctx *ctx)
1104{
1105 struct io_kiocb *req, *tmp;
1106
1107 spin_lock_irq(&ctx->completion_lock);
1108 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
1109 io_kill_timeout(req);
1110 spin_unlock_irq(&ctx->completion_lock);
1111}
1112
Jens Axboede0617e2019-04-06 21:51:27 -06001113static void io_commit_cqring(struct io_ring_ctx *ctx)
1114{
1115 struct io_kiocb *req;
1116
Jens Axboe5262f562019-09-17 12:26:57 -06001117 while ((req = io_get_timeout_req(ctx)) != NULL)
1118 io_kill_timeout(req);
1119
Jens Axboede0617e2019-04-06 21:51:27 -06001120 __io_commit_cqring(ctx);
1121
Pavel Begunkov87987892020-01-18 01:22:30 +03001122 while ((req = io_get_deferred_req(ctx)) != NULL)
Jackie Liua197f662019-11-08 08:09:12 -07001123 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -06001124}
1125
Jens Axboe2b188cc2019-01-07 10:46:33 -07001126static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1127{
Hristo Venev75b28af2019-08-26 17:23:46 +00001128 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001129 unsigned tail;
1130
1131 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001132 /*
1133 * writes to the cq entry need to come after reading head; the
1134 * control dependency is enough as we're using WRITE_ONCE to
1135 * fill the cq entry
1136 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001137 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001138 return NULL;
1139
1140 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001141 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001142}
1143
Jens Axboef2842ab2020-01-08 11:04:00 -07001144static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1145{
Jens Axboef0b493e2020-02-01 21:30:11 -07001146 if (!ctx->cq_ev_fd)
1147 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001148 if (!ctx->eventfd_async)
1149 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001150 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001151}
1152
Jens Axboeb41e9852020-02-17 09:52:41 -07001153static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001154{
1155 if (waitqueue_active(&ctx->wait))
1156 wake_up(&ctx->wait);
1157 if (waitqueue_active(&ctx->sqo_wait))
1158 wake_up(&ctx->sqo_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001159 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001160 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001161}
1162
Jens Axboec4a2ed72019-11-21 21:01:26 -07001163/* Returns true if there are no backlogged entries after the flush */
1164static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001165{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001166 struct io_rings *rings = ctx->rings;
1167 struct io_uring_cqe *cqe;
1168 struct io_kiocb *req;
1169 unsigned long flags;
1170 LIST_HEAD(list);
1171
1172 if (!force) {
1173 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001174 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001175 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1176 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001177 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001178 }
1179
1180 spin_lock_irqsave(&ctx->completion_lock, flags);
1181
1182 /* if force is set, the ring is going away. always drop after that */
1183 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001184 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001185
Jens Axboec4a2ed72019-11-21 21:01:26 -07001186 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001187 while (!list_empty(&ctx->cq_overflow_list)) {
1188 cqe = io_get_cqring(ctx);
1189 if (!cqe && !force)
1190 break;
1191
1192 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1193 list);
1194 list_move(&req->list, &list);
Jens Axboe2ca10252020-02-13 17:17:35 -07001195 req->flags &= ~REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001196 if (cqe) {
1197 WRITE_ONCE(cqe->user_data, req->user_data);
1198 WRITE_ONCE(cqe->res, req->result);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001199 WRITE_ONCE(cqe->flags, req->cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001200 } else {
1201 WRITE_ONCE(ctx->rings->cq_overflow,
1202 atomic_inc_return(&ctx->cached_cq_overflow));
1203 }
1204 }
1205
1206 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001207 if (cqe) {
1208 clear_bit(0, &ctx->sq_check_overflow);
1209 clear_bit(0, &ctx->cq_check_overflow);
1210 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001211 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1212 io_cqring_ev_posted(ctx);
1213
1214 while (!list_empty(&list)) {
1215 req = list_first_entry(&list, struct io_kiocb, list);
1216 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001217 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001218 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001219
1220 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001221}
1222
Jens Axboebcda7ba2020-02-23 16:42:51 -07001223static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001224{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001225 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001226 struct io_uring_cqe *cqe;
1227
Jens Axboe78e19bb2019-11-06 15:21:34 -07001228 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001229
Jens Axboe2b188cc2019-01-07 10:46:33 -07001230 /*
1231 * If we can't get a cq entry, userspace overflowed the
1232 * submission (by quite a lot). Increment the overflow count in
1233 * the ring.
1234 */
1235 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001236 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001237 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001238 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001239 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001240 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001241 WRITE_ONCE(ctx->rings->cq_overflow,
1242 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001243 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001244 if (list_empty(&ctx->cq_overflow_list)) {
1245 set_bit(0, &ctx->sq_check_overflow);
1246 set_bit(0, &ctx->cq_check_overflow);
1247 }
Jens Axboe2ca10252020-02-13 17:17:35 -07001248 req->flags |= REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001249 refcount_inc(&req->refs);
1250 req->result = res;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001251 req->cflags = cflags;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001252 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001253 }
1254}
1255
Jens Axboebcda7ba2020-02-23 16:42:51 -07001256static void io_cqring_fill_event(struct io_kiocb *req, long res)
1257{
1258 __io_cqring_fill_event(req, res, 0);
1259}
1260
1261static void __io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001262{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001263 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001264 unsigned long flags;
1265
1266 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001267 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001268 io_commit_cqring(ctx);
1269 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1270
Jens Axboe8c838782019-03-12 15:48:16 -06001271 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001272}
1273
Jens Axboebcda7ba2020-02-23 16:42:51 -07001274static void io_cqring_add_event(struct io_kiocb *req, long res)
1275{
1276 __io_cqring_add_event(req, res, 0);
1277}
1278
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001279static inline bool io_is_fallback_req(struct io_kiocb *req)
1280{
1281 return req == (struct io_kiocb *)
1282 ((unsigned long) req->ctx->fallback_req & ~1UL);
1283}
1284
1285static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1286{
1287 struct io_kiocb *req;
1288
1289 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001290 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001291 return req;
1292
1293 return NULL;
1294}
1295
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001296static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1297 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001298{
Jens Axboefd6fab22019-03-14 16:30:06 -06001299 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001300 struct io_kiocb *req;
1301
Jens Axboe2579f912019-01-09 09:10:43 -07001302 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001303 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001304 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001305 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001306 } else if (!state->free_reqs) {
1307 size_t sz;
1308 int ret;
1309
1310 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001311 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1312
1313 /*
1314 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1315 * retry single alloc to be on the safe side.
1316 */
1317 if (unlikely(ret <= 0)) {
1318 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1319 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001320 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001321 ret = 1;
1322 }
Jens Axboe2579f912019-01-09 09:10:43 -07001323 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001324 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001325 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001326 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001327 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001328 }
1329
Jens Axboe2579f912019-01-09 09:10:43 -07001330 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001331fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001332 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001333}
1334
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001335static inline void io_put_file(struct io_kiocb *req, struct file *file,
1336 bool fixed)
1337{
1338 if (fixed)
Xiaoguang Wang05589552020-03-31 14:05:18 +08001339 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001340 else
1341 fput(file);
1342}
1343
Jens Axboec6ca97b302019-12-28 12:11:08 -07001344static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001345{
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001346 if (req->flags & REQ_F_NEED_CLEANUP)
1347 io_cleanup_req(req);
1348
YueHaibing96fd84d2020-01-07 22:22:44 +08001349 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001350 if (req->file)
1351 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboe3537b6a2020-04-03 11:19:06 -06001352 if (req->task)
1353 put_task_struct(req->task);
Jens Axboecccf0ee2020-01-27 16:34:48 -07001354
1355 io_req_work_drop_env(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001356}
1357
1358static void __io_free_req(struct io_kiocb *req)
1359{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001360 __io_req_aux_free(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06001361
Jens Axboefcb323c2019-10-24 12:39:47 -06001362 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001363 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001364 unsigned long flags;
1365
1366 spin_lock_irqsave(&ctx->inflight_lock, flags);
1367 list_del(&req->inflight_entry);
1368 if (waitqueue_active(&ctx->inflight_wait))
1369 wake_up(&ctx->inflight_wait);
1370 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1371 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001372
1373 percpu_ref_put(&req->ctx->refs);
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03001374 if (likely(!io_is_fallback_req(req)))
1375 kmem_cache_free(req_cachep, req);
1376 else
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001377 clear_bit_unlock(0, (unsigned long *) &req->ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -06001378}
1379
Jens Axboec6ca97b302019-12-28 12:11:08 -07001380struct req_batch {
1381 void *reqs[IO_IOPOLL_BATCH];
1382 int to_free;
1383 int need_iter;
1384};
1385
1386static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1387{
1388 if (!rb->to_free)
1389 return;
1390 if (rb->need_iter) {
1391 int i, inflight = 0;
1392 unsigned long flags;
1393
1394 for (i = 0; i < rb->to_free; i++) {
1395 struct io_kiocb *req = rb->reqs[i];
1396
Jens Axboe10fef4b2020-01-09 07:52:28 -07001397 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001398 req->file = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08001399 percpu_ref_put(req->fixed_file_refs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001400 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001401 if (req->flags & REQ_F_INFLIGHT)
1402 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001403 __io_req_aux_free(req);
1404 }
1405 if (!inflight)
1406 goto do_free;
1407
1408 spin_lock_irqsave(&ctx->inflight_lock, flags);
1409 for (i = 0; i < rb->to_free; i++) {
1410 struct io_kiocb *req = rb->reqs[i];
1411
Jens Axboe10fef4b2020-01-09 07:52:28 -07001412 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001413 list_del(&req->inflight_entry);
1414 if (!--inflight)
1415 break;
1416 }
1417 }
1418 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1419
1420 if (waitqueue_active(&ctx->inflight_wait))
1421 wake_up(&ctx->inflight_wait);
1422 }
1423do_free:
1424 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
1425 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001426 rb->to_free = rb->need_iter = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001427}
1428
Jackie Liua197f662019-11-08 08:09:12 -07001429static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001430{
Jackie Liua197f662019-11-08 08:09:12 -07001431 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001432 int ret;
1433
Jens Axboe2d283902019-12-04 11:08:05 -07001434 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001435 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001436 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001437 io_commit_cqring(ctx);
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001438 req->flags &= ~REQ_F_LINK_HEAD;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001439 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001440 return true;
1441 }
1442
1443 return false;
1444}
1445
Jens Axboeba816ad2019-09-28 11:36:45 -06001446static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001447{
Jens Axboe2665abf2019-11-05 12:40:47 -07001448 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001449 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001450
Jens Axboe4d7dd462019-11-20 13:03:52 -07001451 /* Already got next link */
1452 if (req->flags & REQ_F_LINK_NEXT)
1453 return;
1454
Jens Axboe9e645e112019-05-10 16:07:28 -06001455 /*
1456 * The list should never be empty when we are called here. But could
1457 * potentially happen if the chain is messed up, check to be on the
1458 * safe side.
1459 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001460 while (!list_empty(&req->link_list)) {
1461 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1462 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001463
Pavel Begunkov44932332019-12-05 16:16:35 +03001464 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1465 (nxt->flags & REQ_F_TIMEOUT))) {
1466 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001467 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001468 req->flags &= ~REQ_F_LINK_TIMEOUT;
1469 continue;
1470 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001471
Pavel Begunkov44932332019-12-05 16:16:35 +03001472 list_del_init(&req->link_list);
1473 if (!list_empty(&nxt->link_list))
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001474 nxt->flags |= REQ_F_LINK_HEAD;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001475 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001476 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001477 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001478
Jens Axboe4d7dd462019-11-20 13:03:52 -07001479 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001480 if (wake_ev)
1481 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001482}
1483
1484/*
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001485 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
Jens Axboe9e645e112019-05-10 16:07:28 -06001486 */
1487static void io_fail_links(struct io_kiocb *req)
1488{
Jens Axboe2665abf2019-11-05 12:40:47 -07001489 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001490 unsigned long flags;
1491
1492 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001493
1494 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001495 struct io_kiocb *link = list_first_entry(&req->link_list,
1496 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001497
Pavel Begunkov44932332019-12-05 16:16:35 +03001498 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001499 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001500
1501 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001502 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001503 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001504 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001505 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001506 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001507 }
Jens Axboe5d960722019-11-19 15:31:28 -07001508 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001509 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001510
1511 io_commit_cqring(ctx);
1512 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1513 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001514}
1515
Jens Axboe4d7dd462019-11-20 13:03:52 -07001516static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001517{
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001518 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001519 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001520
Jens Axboe9e645e112019-05-10 16:07:28 -06001521 /*
1522 * If LINK is set, we have dependent requests in this chain. If we
1523 * didn't fail this request, queue the first one up, moving any other
1524 * dependencies to the next request. In case of failure, fail the rest
1525 * of the chain.
1526 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001527 if (req->flags & REQ_F_FAIL_LINK) {
1528 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001529 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1530 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001531 struct io_ring_ctx *ctx = req->ctx;
1532 unsigned long flags;
1533
1534 /*
1535 * If this is a timeout link, we could be racing with the
1536 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001537 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001538 */
1539 spin_lock_irqsave(&ctx->completion_lock, flags);
1540 io_req_link_next(req, nxt);
1541 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1542 } else {
1543 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001544 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001545}
Jens Axboe9e645e112019-05-10 16:07:28 -06001546
Jackie Liuc69f8db2019-11-09 11:00:08 +08001547static void io_free_req(struct io_kiocb *req)
1548{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001549 struct io_kiocb *nxt = NULL;
1550
1551 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001552 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001553
1554 if (nxt)
1555 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001556}
1557
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001558static void io_link_work_cb(struct io_wq_work **workptr)
1559{
Pavel Begunkov18a542f2020-03-23 00:23:29 +03001560 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
1561 struct io_kiocb *link;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001562
Pavel Begunkov18a542f2020-03-23 00:23:29 +03001563 link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001564 io_queue_linked_timeout(link);
1565 io_wq_submit_work(workptr);
1566}
1567
1568static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
1569{
1570 struct io_kiocb *link;
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001571 const struct io_op_def *def = &io_op_defs[nxt->opcode];
1572
1573 if ((nxt->flags & REQ_F_ISREG) && def->hash_reg_file)
1574 io_wq_hash_work(&nxt->work, file_inode(nxt->file));
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001575
1576 *workptr = &nxt->work;
1577 link = io_prep_linked_timeout(nxt);
Pavel Begunkov18a542f2020-03-23 00:23:29 +03001578 if (link)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001579 nxt->work.func = io_link_work_cb;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001580}
1581
Jens Axboeba816ad2019-09-28 11:36:45 -06001582/*
1583 * Drop reference to request, return next in chain (if there is one) if this
1584 * was the last reference to this request.
1585 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001586__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001587static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001588{
Jens Axboe2a44f462020-02-25 13:25:41 -07001589 if (refcount_dec_and_test(&req->refs)) {
1590 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001591 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001592 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001593}
1594
Jens Axboe2b188cc2019-01-07 10:46:33 -07001595static void io_put_req(struct io_kiocb *req)
1596{
Jens Axboedef596e2019-01-09 08:59:42 -07001597 if (refcount_dec_and_test(&req->refs))
1598 io_free_req(req);
1599}
1600
Pavel Begunkove9fd9392020-03-04 16:14:12 +03001601static void io_steal_work(struct io_kiocb *req,
1602 struct io_wq_work **workptr)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001603{
1604 /*
1605 * It's in an io-wq worker, so there always should be at least
1606 * one reference, which will be dropped in io_put_work() just
1607 * after the current handler returns.
1608 *
1609 * It also means, that if the counter dropped to 1, then there is
1610 * no asynchronous users left, so it's safe to steal the next work.
1611 */
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001612 if (refcount_read(&req->refs) == 1) {
1613 struct io_kiocb *nxt = NULL;
1614
1615 io_req_find_next(req, &nxt);
1616 if (nxt)
1617 io_wq_assign_next(workptr, nxt);
1618 }
1619}
1620
Jens Axboe978db572019-11-14 22:39:04 -07001621/*
1622 * Must only be used if we don't need to care about links, usually from
1623 * within the completion handling itself.
1624 */
1625static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001626{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001627 /* drop both submit and complete references */
1628 if (refcount_sub_and_test(2, &req->refs))
1629 __io_free_req(req);
1630}
1631
Jens Axboe978db572019-11-14 22:39:04 -07001632static void io_double_put_req(struct io_kiocb *req)
1633{
1634 /* drop both submit and complete references */
1635 if (refcount_sub_and_test(2, &req->refs))
1636 io_free_req(req);
1637}
1638
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001639static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001640{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001641 struct io_rings *rings = ctx->rings;
1642
Jens Axboead3eb2c2019-12-18 17:12:20 -07001643 if (test_bit(0, &ctx->cq_check_overflow)) {
1644 /*
1645 * noflush == true is from the waitqueue handler, just ensure
1646 * we wake up the task, and the next invocation will flush the
1647 * entries. We cannot safely to it from here.
1648 */
1649 if (noflush && !list_empty(&ctx->cq_overflow_list))
1650 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001651
Jens Axboead3eb2c2019-12-18 17:12:20 -07001652 io_cqring_overflow_flush(ctx, false);
1653 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001654
Jens Axboea3a0e432019-08-20 11:03:11 -06001655 /* See comment at the top of this file */
1656 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001657 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001658}
1659
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001660static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1661{
1662 struct io_rings *rings = ctx->rings;
1663
1664 /* make sure SQ entry isn't read before tail */
1665 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1666}
1667
Jens Axboe8237e042019-12-28 10:48:22 -07001668static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001669{
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001670 if ((req->flags & REQ_F_LINK_HEAD) || io_is_fallback_req(req))
Jens Axboec6ca97b302019-12-28 12:11:08 -07001671 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001672
Jens Axboec6ca97b302019-12-28 12:11:08 -07001673 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1674 rb->need_iter++;
1675
1676 rb->reqs[rb->to_free++] = req;
1677 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1678 io_free_req_many(req->ctx, rb);
1679 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001680}
1681
Jens Axboebcda7ba2020-02-23 16:42:51 -07001682static int io_put_kbuf(struct io_kiocb *req)
1683{
Jens Axboe4d954c22020-02-27 07:31:19 -07001684 struct io_buffer *kbuf;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001685 int cflags;
1686
Jens Axboe4d954c22020-02-27 07:31:19 -07001687 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001688 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
1689 cflags |= IORING_CQE_F_BUFFER;
1690 req->rw.addr = 0;
1691 kfree(kbuf);
1692 return cflags;
1693}
1694
Jens Axboedef596e2019-01-09 08:59:42 -07001695/*
1696 * Find and free completed poll iocbs
1697 */
1698static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1699 struct list_head *done)
1700{
Jens Axboe8237e042019-12-28 10:48:22 -07001701 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001702 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001703
Jens Axboec6ca97b302019-12-28 12:11:08 -07001704 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001705 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07001706 int cflags = 0;
1707
Jens Axboedef596e2019-01-09 08:59:42 -07001708 req = list_first_entry(done, struct io_kiocb, list);
1709 list_del(&req->list);
1710
Jens Axboebcda7ba2020-02-23 16:42:51 -07001711 if (req->flags & REQ_F_BUFFER_SELECTED)
1712 cflags = io_put_kbuf(req);
1713
1714 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07001715 (*nr_events)++;
1716
Jens Axboe8237e042019-12-28 10:48:22 -07001717 if (refcount_dec_and_test(&req->refs) &&
1718 !io_req_multi_free(&rb, req))
1719 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001720 }
Jens Axboedef596e2019-01-09 08:59:42 -07001721
Jens Axboe09bb8392019-03-13 12:39:28 -06001722 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08001723 if (ctx->flags & IORING_SETUP_SQPOLL)
1724 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001725 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001726}
1727
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001728static void io_iopoll_queue(struct list_head *again)
1729{
1730 struct io_kiocb *req;
1731
1732 do {
1733 req = list_first_entry(again, struct io_kiocb, list);
1734 list_del(&req->list);
1735 refcount_inc(&req->refs);
1736 io_queue_async_work(req);
1737 } while (!list_empty(again));
1738}
1739
Jens Axboedef596e2019-01-09 08:59:42 -07001740static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1741 long min)
1742{
1743 struct io_kiocb *req, *tmp;
1744 LIST_HEAD(done);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001745 LIST_HEAD(again);
Jens Axboedef596e2019-01-09 08:59:42 -07001746 bool spin;
1747 int ret;
1748
1749 /*
1750 * Only spin for completions if we don't have multiple devices hanging
1751 * off our complete list, and we're under the requested amount.
1752 */
1753 spin = !ctx->poll_multi_file && *nr_events < min;
1754
1755 ret = 0;
1756 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001757 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001758
1759 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001760 * Move completed and retryable entries to our local lists.
1761 * If we find a request that requires polling, break out
1762 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07001763 */
1764 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1765 list_move_tail(&req->list, &done);
1766 continue;
1767 }
1768 if (!list_empty(&done))
1769 break;
1770
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001771 if (req->result == -EAGAIN) {
1772 list_move_tail(&req->list, &again);
1773 continue;
1774 }
1775 if (!list_empty(&again))
1776 break;
1777
Jens Axboedef596e2019-01-09 08:59:42 -07001778 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1779 if (ret < 0)
1780 break;
1781
1782 if (ret && spin)
1783 spin = false;
1784 ret = 0;
1785 }
1786
1787 if (!list_empty(&done))
1788 io_iopoll_complete(ctx, nr_events, &done);
1789
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001790 if (!list_empty(&again))
1791 io_iopoll_queue(&again);
1792
Jens Axboedef596e2019-01-09 08:59:42 -07001793 return ret;
1794}
1795
1796/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001797 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001798 * non-spinning poll check - we'll still enter the driver poll loop, but only
1799 * as a non-spinning completion check.
1800 */
1801static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1802 long min)
1803{
Jens Axboe08f54392019-08-21 22:19:11 -06001804 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001805 int ret;
1806
1807 ret = io_do_iopoll(ctx, nr_events, min);
1808 if (ret < 0)
1809 return ret;
1810 if (!min || *nr_events >= min)
1811 return 0;
1812 }
1813
1814 return 1;
1815}
1816
1817/*
1818 * We can't just wait for polled events to come to us, we have to actively
1819 * find and complete them.
1820 */
1821static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1822{
1823 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1824 return;
1825
1826 mutex_lock(&ctx->uring_lock);
1827 while (!list_empty(&ctx->poll_list)) {
1828 unsigned int nr_events = 0;
1829
1830 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001831
1832 /*
1833 * Ensure we allow local-to-the-cpu processing to take place,
1834 * in this case we need to ensure that we reap all events.
1835 */
1836 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001837 }
1838 mutex_unlock(&ctx->uring_lock);
1839}
1840
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001841static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1842 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001843{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001844 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001845
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001846 /*
1847 * We disallow the app entering submit/complete with polling, but we
1848 * still need to lock the ring to prevent racing with polled issue
1849 * that got punted to a workqueue.
1850 */
1851 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001852 do {
1853 int tmin = 0;
1854
Jens Axboe500f9fb2019-08-19 12:15:59 -06001855 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001856 * Don't enter poll loop if we already have events pending.
1857 * If we do, we can potentially be spinning for commands that
1858 * already triggered a CQE (eg in error).
1859 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001860 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001861 break;
1862
1863 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001864 * If a submit got punted to a workqueue, we can have the
1865 * application entering polling for a command before it gets
1866 * issued. That app will hold the uring_lock for the duration
1867 * of the poll right here, so we need to take a breather every
1868 * now and then to ensure that the issue has a chance to add
1869 * the poll to the issued list. Otherwise we can spin here
1870 * forever, while the workqueue is stuck trying to acquire the
1871 * very same mutex.
1872 */
1873 if (!(++iters & 7)) {
1874 mutex_unlock(&ctx->uring_lock);
1875 mutex_lock(&ctx->uring_lock);
1876 }
1877
Jens Axboedef596e2019-01-09 08:59:42 -07001878 if (*nr_events < min)
1879 tmin = min - *nr_events;
1880
1881 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1882 if (ret <= 0)
1883 break;
1884 ret = 0;
1885 } while (min && !*nr_events && !need_resched());
1886
Jens Axboe500f9fb2019-08-19 12:15:59 -06001887 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001888 return ret;
1889}
1890
Jens Axboe491381ce2019-10-17 09:20:46 -06001891static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001892{
Jens Axboe491381ce2019-10-17 09:20:46 -06001893 /*
1894 * Tell lockdep we inherited freeze protection from submission
1895 * thread.
1896 */
1897 if (req->flags & REQ_F_ISREG) {
1898 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001899
Jens Axboe491381ce2019-10-17 09:20:46 -06001900 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001901 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001902 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001903}
1904
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001905static inline void req_set_fail_links(struct io_kiocb *req)
1906{
1907 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1908 req->flags |= REQ_F_FAIL_LINK;
1909}
1910
Jens Axboeba816ad2019-09-28 11:36:45 -06001911static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001912{
Jens Axboe9adbd452019-12-20 08:45:55 -07001913 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001914 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001915
Jens Axboe491381ce2019-10-17 09:20:46 -06001916 if (kiocb->ki_flags & IOCB_WRITE)
1917 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001918
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001919 if (res != req->result)
1920 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001921 if (req->flags & REQ_F_BUFFER_SELECTED)
1922 cflags = io_put_kbuf(req);
1923 __io_cqring_add_event(req, res, cflags);
Jens Axboeba816ad2019-09-28 11:36:45 -06001924}
1925
1926static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1927{
Jens Axboe9adbd452019-12-20 08:45:55 -07001928 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001929
1930 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001931 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001932}
1933
Jens Axboedef596e2019-01-09 08:59:42 -07001934static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1935{
Jens Axboe9adbd452019-12-20 08:45:55 -07001936 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001937
Jens Axboe491381ce2019-10-17 09:20:46 -06001938 if (kiocb->ki_flags & IOCB_WRITE)
1939 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001940
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001941 if (res != req->result)
1942 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001943 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001944 if (res != -EAGAIN)
1945 req->flags |= REQ_F_IOPOLL_COMPLETED;
1946}
1947
1948/*
1949 * After the iocb has been issued, it's safe to be found on the poll list.
1950 * Adding the kiocb to the list AFTER submission ensures that we don't
1951 * find it from a io_iopoll_getevents() thread before the issuer is done
1952 * accessing the kiocb cookie.
1953 */
1954static void io_iopoll_req_issued(struct io_kiocb *req)
1955{
1956 struct io_ring_ctx *ctx = req->ctx;
1957
1958 /*
1959 * Track whether we have multiple files in our lists. This will impact
1960 * how we do polling eventually, not spinning if we're on potentially
1961 * different devices.
1962 */
1963 if (list_empty(&ctx->poll_list)) {
1964 ctx->poll_multi_file = false;
1965 } else if (!ctx->poll_multi_file) {
1966 struct io_kiocb *list_req;
1967
1968 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1969 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001970 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001971 ctx->poll_multi_file = true;
1972 }
1973
1974 /*
1975 * For fast devices, IO may have already completed. If it has, add
1976 * it to the front so we find it first.
1977 */
1978 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1979 list_add(&req->list, &ctx->poll_list);
1980 else
1981 list_add_tail(&req->list, &ctx->poll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08001982
1983 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
1984 wq_has_sleeper(&ctx->sqo_wait))
1985 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07001986}
1987
Jens Axboe3d6770f2019-04-13 11:50:54 -06001988static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001989{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001990 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001991 int diff = state->has_refs - state->used_refs;
1992
1993 if (diff)
1994 fput_many(state->file, diff);
1995 state->file = NULL;
1996 }
1997}
1998
1999/*
2000 * Get as many references to a file as we have IOs left in this submission,
2001 * assuming most submissions are for one file, or at least that each file
2002 * has more than one submission.
2003 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002004static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002005{
2006 if (!state)
2007 return fget(fd);
2008
2009 if (state->file) {
2010 if (state->fd == fd) {
2011 state->used_refs++;
2012 state->ios_left--;
2013 return state->file;
2014 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06002015 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002016 }
2017 state->file = fget_many(fd, state->ios_left);
2018 if (!state->file)
2019 return NULL;
2020
2021 state->fd = fd;
2022 state->has_refs = state->ios_left;
2023 state->used_refs = 1;
2024 state->ios_left--;
2025 return state->file;
2026}
2027
Jens Axboe2b188cc2019-01-07 10:46:33 -07002028/*
2029 * If we tracked the file through the SCM inflight mechanism, we could support
2030 * any file. For now, just ensure that anything potentially problematic is done
2031 * inline.
2032 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002033static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002034{
2035 umode_t mode = file_inode(file)->i_mode;
2036
Jens Axboe10d59342019-12-09 20:16:22 -07002037 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002038 return true;
2039 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
2040 return true;
2041
Jens Axboeaf197f52020-04-28 13:15:06 -06002042 if (!(file->f_mode & FMODE_NOWAIT))
2043 return false;
2044
2045 if (rw == READ)
2046 return file->f_op->read_iter != NULL;
2047
2048 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002049}
2050
Jens Axboe3529d8c2019-12-19 18:24:38 -07002051static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2052 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002053{
Jens Axboedef596e2019-01-09 08:59:42 -07002054 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002055 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002056 unsigned ioprio;
2057 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002058
Jens Axboe491381ce2019-10-17 09:20:46 -06002059 if (S_ISREG(file_inode(req->file)->i_mode))
2060 req->flags |= REQ_F_ISREG;
2061
Jens Axboe2b188cc2019-01-07 10:46:33 -07002062 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002063 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2064 req->flags |= REQ_F_CUR_POS;
2065 kiocb->ki_pos = req->file->f_pos;
2066 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002067 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002068 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2069 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2070 if (unlikely(ret))
2071 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002072
2073 ioprio = READ_ONCE(sqe->ioprio);
2074 if (ioprio) {
2075 ret = ioprio_check_cap(ioprio);
2076 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002077 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002078
2079 kiocb->ki_ioprio = ioprio;
2080 } else
2081 kiocb->ki_ioprio = get_current_ioprio();
2082
Stefan Bühler8449eed2019-04-27 20:34:19 +02002083 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06002084 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
2085 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02002086 req->flags |= REQ_F_NOWAIT;
2087
2088 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002089 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02002090
Jens Axboedef596e2019-01-09 08:59:42 -07002091 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002092 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2093 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002094 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002095
Jens Axboedef596e2019-01-09 08:59:42 -07002096 kiocb->ki_flags |= IOCB_HIPRI;
2097 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06002098 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002099 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002100 if (kiocb->ki_flags & IOCB_HIPRI)
2101 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002102 kiocb->ki_complete = io_complete_rw;
2103 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002104
Jens Axboe3529d8c2019-12-19 18:24:38 -07002105 req->rw.addr = READ_ONCE(sqe->addr);
2106 req->rw.len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002107 /* we own ->private, reuse it for the buffer index / buffer ID */
Jens Axboe9adbd452019-12-20 08:45:55 -07002108 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002109 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002110 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002111}
2112
2113static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2114{
2115 switch (ret) {
2116 case -EIOCBQUEUED:
2117 break;
2118 case -ERESTARTSYS:
2119 case -ERESTARTNOINTR:
2120 case -ERESTARTNOHAND:
2121 case -ERESTART_RESTARTBLOCK:
2122 /*
2123 * We can't just restart the syscall, since previously
2124 * submitted sqes may already be in progress. Just fail this
2125 * IO with EINTR.
2126 */
2127 ret = -EINTR;
2128 /* fall through */
2129 default:
2130 kiocb->ki_complete(kiocb, ret, 0);
2131 }
2132}
2133
Pavel Begunkov014db002020-03-03 21:33:12 +03002134static void kiocb_done(struct kiocb *kiocb, ssize_t ret)
Jens Axboeba816ad2019-09-28 11:36:45 -06002135{
Jens Axboeba042912019-12-25 16:33:42 -07002136 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2137
2138 if (req->flags & REQ_F_CUR_POS)
2139 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002140 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Pavel Begunkov014db002020-03-03 21:33:12 +03002141 io_complete_rw(kiocb, ret, 0);
Jens Axboeba816ad2019-09-28 11:36:45 -06002142 else
2143 io_rw_done(kiocb, ret);
2144}
2145
Jens Axboe9adbd452019-12-20 08:45:55 -07002146static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002147 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002148{
Jens Axboe9adbd452019-12-20 08:45:55 -07002149 struct io_ring_ctx *ctx = req->ctx;
2150 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002151 struct io_mapped_ubuf *imu;
2152 unsigned index, buf_index;
2153 size_t offset;
2154 u64 buf_addr;
2155
2156 /* attempt to use fixed buffers without having provided iovecs */
2157 if (unlikely(!ctx->user_bufs))
2158 return -EFAULT;
2159
Jens Axboe9adbd452019-12-20 08:45:55 -07002160 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07002161 if (unlikely(buf_index >= ctx->nr_user_bufs))
2162 return -EFAULT;
2163
2164 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2165 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002166 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002167
2168 /* overflow */
2169 if (buf_addr + len < buf_addr)
2170 return -EFAULT;
2171 /* not inside the mapped region */
2172 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2173 return -EFAULT;
2174
2175 /*
2176 * May not be a start of buffer, set size appropriately
2177 * and advance us to the beginning.
2178 */
2179 offset = buf_addr - imu->ubuf;
2180 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002181
2182 if (offset) {
2183 /*
2184 * Don't use iov_iter_advance() here, as it's really slow for
2185 * using the latter parts of a big fixed buffer - it iterates
2186 * over each segment manually. We can cheat a bit here, because
2187 * we know that:
2188 *
2189 * 1) it's a BVEC iter, we set it up
2190 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2191 * first and last bvec
2192 *
2193 * So just find our index, and adjust the iterator afterwards.
2194 * If the offset is within the first bvec (or the whole first
2195 * bvec, just use iov_iter_advance(). This makes it easier
2196 * since we can just skip the first segment, which may not
2197 * be PAGE_SIZE aligned.
2198 */
2199 const struct bio_vec *bvec = imu->bvec;
2200
2201 if (offset <= bvec->bv_len) {
2202 iov_iter_advance(iter, offset);
2203 } else {
2204 unsigned long seg_skip;
2205
2206 /* skip first vec */
2207 offset -= bvec->bv_len;
2208 seg_skip = 1 + (offset >> PAGE_SHIFT);
2209
2210 iter->bvec = bvec + seg_skip;
2211 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002212 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002213 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002214 }
2215 }
2216
Jens Axboe5e559562019-11-13 16:12:46 -07002217 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002218}
2219
Jens Axboebcda7ba2020-02-23 16:42:51 -07002220static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2221{
2222 if (needs_lock)
2223 mutex_unlock(&ctx->uring_lock);
2224}
2225
2226static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2227{
2228 /*
2229 * "Normal" inline submissions always hold the uring_lock, since we
2230 * grab it from the system call. Same is true for the SQPOLL offload.
2231 * The only exception is when we've detached the request and issue it
2232 * from an async worker thread, grab the lock for that case.
2233 */
2234 if (needs_lock)
2235 mutex_lock(&ctx->uring_lock);
2236}
2237
2238static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2239 int bgid, struct io_buffer *kbuf,
2240 bool needs_lock)
2241{
2242 struct io_buffer *head;
2243
2244 if (req->flags & REQ_F_BUFFER_SELECTED)
2245 return kbuf;
2246
2247 io_ring_submit_lock(req->ctx, needs_lock);
2248
2249 lockdep_assert_held(&req->ctx->uring_lock);
2250
2251 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2252 if (head) {
2253 if (!list_empty(&head->list)) {
2254 kbuf = list_last_entry(&head->list, struct io_buffer,
2255 list);
2256 list_del(&kbuf->list);
2257 } else {
2258 kbuf = head;
2259 idr_remove(&req->ctx->io_buffer_idr, bgid);
2260 }
2261 if (*len > kbuf->len)
2262 *len = kbuf->len;
2263 } else {
2264 kbuf = ERR_PTR(-ENOBUFS);
2265 }
2266
2267 io_ring_submit_unlock(req->ctx, needs_lock);
2268
2269 return kbuf;
2270}
2271
Jens Axboe4d954c22020-02-27 07:31:19 -07002272static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2273 bool needs_lock)
2274{
2275 struct io_buffer *kbuf;
2276 int bgid;
2277
2278 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2279 bgid = (int) (unsigned long) req->rw.kiocb.private;
2280 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2281 if (IS_ERR(kbuf))
2282 return kbuf;
2283 req->rw.addr = (u64) (unsigned long) kbuf;
2284 req->flags |= REQ_F_BUFFER_SELECTED;
2285 return u64_to_user_ptr(kbuf->addr);
2286}
2287
2288#ifdef CONFIG_COMPAT
2289static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2290 bool needs_lock)
2291{
2292 struct compat_iovec __user *uiov;
2293 compat_ssize_t clen;
2294 void __user *buf;
2295 ssize_t len;
2296
2297 uiov = u64_to_user_ptr(req->rw.addr);
2298 if (!access_ok(uiov, sizeof(*uiov)))
2299 return -EFAULT;
2300 if (__get_user(clen, &uiov->iov_len))
2301 return -EFAULT;
2302 if (clen < 0)
2303 return -EINVAL;
2304
2305 len = clen;
2306 buf = io_rw_buffer_select(req, &len, needs_lock);
2307 if (IS_ERR(buf))
2308 return PTR_ERR(buf);
2309 iov[0].iov_base = buf;
2310 iov[0].iov_len = (compat_size_t) len;
2311 return 0;
2312}
2313#endif
2314
2315static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2316 bool needs_lock)
2317{
2318 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2319 void __user *buf;
2320 ssize_t len;
2321
2322 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2323 return -EFAULT;
2324
2325 len = iov[0].iov_len;
2326 if (len < 0)
2327 return -EINVAL;
2328 buf = io_rw_buffer_select(req, &len, needs_lock);
2329 if (IS_ERR(buf))
2330 return PTR_ERR(buf);
2331 iov[0].iov_base = buf;
2332 iov[0].iov_len = len;
2333 return 0;
2334}
2335
2336static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2337 bool needs_lock)
2338{
2339 if (req->flags & REQ_F_BUFFER_SELECTED)
2340 return 0;
2341 if (!req->rw.len)
2342 return 0;
2343 else if (req->rw.len > 1)
2344 return -EINVAL;
2345
2346#ifdef CONFIG_COMPAT
2347 if (req->ctx->compat)
2348 return io_compat_import(req, iov, needs_lock);
2349#endif
2350
2351 return __io_iov_buffer_select(req, iov, needs_lock);
2352}
2353
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002354static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
Jens Axboebcda7ba2020-02-23 16:42:51 -07002355 struct iovec **iovec, struct iov_iter *iter,
2356 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002357{
Jens Axboe9adbd452019-12-20 08:45:55 -07002358 void __user *buf = u64_to_user_ptr(req->rw.addr);
2359 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002360 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002361 u8 opcode;
2362
Jens Axboed625c6e2019-12-17 19:53:05 -07002363 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002364 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002365 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002366 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002367 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002368
Jens Axboebcda7ba2020-02-23 16:42:51 -07002369 /* buffer index only valid with fixed read/write, or buffer select */
2370 if (req->rw.kiocb.private && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002371 return -EINVAL;
2372
Jens Axboe3a6820f2019-12-22 15:19:35 -07002373 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002374 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002375 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
2376 if (IS_ERR(buf)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002377 *iovec = NULL;
Jens Axboe4d954c22020-02-27 07:31:19 -07002378 return PTR_ERR(buf);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002379 }
Jens Axboe3f9d6442020-03-11 12:27:04 -06002380 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002381 }
2382
Jens Axboe3a6820f2019-12-22 15:19:35 -07002383 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2384 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002385 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002386 }
2387
Jens Axboef67676d2019-12-02 11:03:47 -07002388 if (req->io) {
2389 struct io_async_rw *iorw = &req->io->rw;
2390
2391 *iovec = iorw->iov;
2392 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2393 if (iorw->iov == iorw->fast_iov)
2394 *iovec = NULL;
2395 return iorw->size;
2396 }
2397
Jens Axboe4d954c22020-02-27 07:31:19 -07002398 if (req->flags & REQ_F_BUFFER_SELECT) {
2399 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002400 if (!ret) {
2401 ret = (*iovec)->iov_len;
2402 iov_iter_init(iter, rw, *iovec, 1, ret);
2403 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002404 *iovec = NULL;
2405 return ret;
2406 }
2407
Jens Axboe2b188cc2019-01-07 10:46:33 -07002408#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002409 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002410 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2411 iovec, iter);
2412#endif
2413
2414 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2415}
2416
Jens Axboe32960612019-09-23 11:05:34 -06002417/*
2418 * For files that don't have ->read_iter() and ->write_iter(), handle them
2419 * by looping over ->read() or ->write() manually.
2420 */
2421static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2422 struct iov_iter *iter)
2423{
2424 ssize_t ret = 0;
2425
2426 /*
2427 * Don't support polled IO through this interface, and we can't
2428 * support non-blocking either. For the latter, this just causes
2429 * the kiocb to be handled from an async context.
2430 */
2431 if (kiocb->ki_flags & IOCB_HIPRI)
2432 return -EOPNOTSUPP;
2433 if (kiocb->ki_flags & IOCB_NOWAIT)
2434 return -EAGAIN;
2435
2436 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002437 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002438 ssize_t nr;
2439
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002440 if (!iov_iter_is_bvec(iter)) {
2441 iovec = iov_iter_iovec(iter);
2442 } else {
2443 /* fixed buffers import bvec */
2444 iovec.iov_base = kmap(iter->bvec->bv_page)
2445 + iter->iov_offset;
2446 iovec.iov_len = min(iter->count,
2447 iter->bvec->bv_len - iter->iov_offset);
2448 }
2449
Jens Axboe32960612019-09-23 11:05:34 -06002450 if (rw == READ) {
2451 nr = file->f_op->read(file, iovec.iov_base,
2452 iovec.iov_len, &kiocb->ki_pos);
2453 } else {
2454 nr = file->f_op->write(file, iovec.iov_base,
2455 iovec.iov_len, &kiocb->ki_pos);
2456 }
2457
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002458 if (iov_iter_is_bvec(iter))
2459 kunmap(iter->bvec->bv_page);
2460
Jens Axboe32960612019-09-23 11:05:34 -06002461 if (nr < 0) {
2462 if (!ret)
2463 ret = nr;
2464 break;
2465 }
2466 ret += nr;
2467 if (nr != iovec.iov_len)
2468 break;
2469 iov_iter_advance(iter, nr);
2470 }
2471
2472 return ret;
2473}
2474
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002475static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002476 struct iovec *iovec, struct iovec *fast_iov,
2477 struct iov_iter *iter)
2478{
2479 req->io->rw.nr_segs = iter->nr_segs;
2480 req->io->rw.size = io_size;
2481 req->io->rw.iov = iovec;
2482 if (!req->io->rw.iov) {
2483 req->io->rw.iov = req->io->rw.fast_iov;
Xiaoguang Wang45097da2020-04-08 22:29:58 +08002484 if (req->io->rw.iov != fast_iov)
2485 memcpy(req->io->rw.iov, fast_iov,
2486 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002487 } else {
2488 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002489 }
2490}
2491
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002492static inline int __io_alloc_async_ctx(struct io_kiocb *req)
2493{
2494 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
2495 return req->io == NULL;
2496}
2497
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002498static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002499{
Jens Axboed3656342019-12-18 09:50:26 -07002500 if (!io_op_defs[req->opcode].async_ctx)
2501 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002502
2503 return __io_alloc_async_ctx(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002504}
2505
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002506static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2507 struct iovec *iovec, struct iovec *fast_iov,
2508 struct iov_iter *iter)
2509{
Jens Axboe980ad262020-01-24 23:08:54 -07002510 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002511 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002512 if (!req->io) {
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002513 if (__io_alloc_async_ctx(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07002514 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002515
Jens Axboe5d204bc2020-01-31 12:06:52 -07002516 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2517 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002518 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002519}
2520
Jens Axboe3529d8c2019-12-19 18:24:38 -07002521static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2522 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002523{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002524 struct io_async_ctx *io;
2525 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002526 ssize_t ret;
2527
Jens Axboe3529d8c2019-12-19 18:24:38 -07002528 ret = io_prep_rw(req, sqe, force_nonblock);
2529 if (ret)
2530 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002531
Jens Axboe3529d8c2019-12-19 18:24:38 -07002532 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2533 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002534
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002535 /* either don't need iovec imported or already have it */
2536 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002537 return 0;
2538
2539 io = req->io;
2540 io->rw.iov = io->rw.fast_iov;
2541 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002542 ret = io_import_iovec(READ, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002543 req->io = io;
2544 if (ret < 0)
2545 return ret;
2546
2547 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2548 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002549}
2550
Pavel Begunkov014db002020-03-03 21:33:12 +03002551static int io_read(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002552{
2553 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002554 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002555 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002556 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002557 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002558
Jens Axboebcda7ba2020-02-23 16:42:51 -07002559 ret = io_import_iovec(READ, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07002560 if (ret < 0)
2561 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002562
Jens Axboefd6c2e42019-12-18 12:19:41 -07002563 /* Ensure we clear previously set non-block flag */
2564 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07002565 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002566
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002567 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002568 io_size = ret;
Pavel Begunkovdea3b492020-04-12 02:05:04 +03002569 if (req->flags & REQ_F_LINK_HEAD)
Jens Axboef67676d2019-12-02 11:03:47 -07002570 req->result = io_size;
2571
2572 /*
2573 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2574 * we know to async punt it even if it was opened O_NONBLOCK
2575 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002576 if (force_nonblock && !io_file_supports_async(req->file, READ))
Jens Axboef67676d2019-12-02 11:03:47 -07002577 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002578
Jens Axboe31b51512019-01-18 22:56:34 -07002579 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002580 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002581 if (!ret) {
2582 ssize_t ret2;
2583
Jens Axboe9adbd452019-12-20 08:45:55 -07002584 if (req->file->f_op->read_iter)
2585 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002586 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002587 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002588
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002589 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002590 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkov014db002020-03-03 21:33:12 +03002591 kiocb_done(kiocb, ret2);
Jens Axboef67676d2019-12-02 11:03:47 -07002592 } else {
2593copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002594 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002595 inline_vecs, &iter);
2596 if (ret)
2597 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002598 /* any defer here is final, must blocking retry */
Jens Axboe490e8962020-04-28 13:16:53 -06002599 if (!(req->flags & REQ_F_NOWAIT) &&
2600 !file_can_poll(req->file))
Jens Axboe29de5f62020-02-20 09:56:08 -07002601 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002602 return -EAGAIN;
2603 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002604 }
Jens Axboef67676d2019-12-02 11:03:47 -07002605out_free:
Pavel Begunkov1e950812020-02-06 19:51:16 +03002606 kfree(iovec);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002607 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002608 return ret;
2609}
2610
Jens Axboe3529d8c2019-12-19 18:24:38 -07002611static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2612 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002613{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002614 struct io_async_ctx *io;
2615 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002616 ssize_t ret;
2617
Jens Axboe3529d8c2019-12-19 18:24:38 -07002618 ret = io_prep_rw(req, sqe, force_nonblock);
2619 if (ret)
2620 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002621
Jens Axboe3529d8c2019-12-19 18:24:38 -07002622 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2623 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002624
Jens Axboe4ed734b2020-03-20 11:23:41 -06002625 req->fsize = rlimit(RLIMIT_FSIZE);
2626
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002627 /* either don't need iovec imported or already have it */
2628 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002629 return 0;
2630
2631 io = req->io;
2632 io->rw.iov = io->rw.fast_iov;
2633 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002634 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002635 req->io = io;
2636 if (ret < 0)
2637 return ret;
2638
2639 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2640 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002641}
2642
Pavel Begunkov014db002020-03-03 21:33:12 +03002643static int io_write(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002644{
2645 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002646 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002647 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002648 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002649 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002650
Jens Axboebcda7ba2020-02-23 16:42:51 -07002651 ret = io_import_iovec(WRITE, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07002652 if (ret < 0)
2653 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002654
Jens Axboefd6c2e42019-12-18 12:19:41 -07002655 /* Ensure we clear previously set non-block flag */
2656 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002657 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002658
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002659 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002660 io_size = ret;
Pavel Begunkovdea3b492020-04-12 02:05:04 +03002661 if (req->flags & REQ_F_LINK_HEAD)
Jens Axboef67676d2019-12-02 11:03:47 -07002662 req->result = io_size;
2663
2664 /*
2665 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2666 * we know to async punt it even if it was opened O_NONBLOCK
2667 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002668 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07002669 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07002670
Jens Axboe10d59342019-12-09 20:16:22 -07002671 /* file path doesn't support NOWAIT for non-direct_IO */
2672 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2673 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002674 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002675
Jens Axboe31b51512019-01-18 22:56:34 -07002676 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002677 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002678 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002679 ssize_t ret2;
2680
Jens Axboe2b188cc2019-01-07 10:46:33 -07002681 /*
2682 * Open-code file_start_write here to grab freeze protection,
2683 * which will be released by another thread in
2684 * io_complete_rw(). Fool lockdep by telling it the lock got
2685 * released so that it doesn't complain about the held lock when
2686 * we return to userspace.
2687 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002688 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002689 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002690 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002691 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002692 SB_FREEZE_WRITE);
2693 }
2694 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002695
Jens Axboe4ed734b2020-03-20 11:23:41 -06002696 if (!force_nonblock)
2697 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
2698
Jens Axboe9adbd452019-12-20 08:45:55 -07002699 if (req->file->f_op->write_iter)
2700 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002701 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002702 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboe4ed734b2020-03-20 11:23:41 -06002703
2704 if (!force_nonblock)
2705 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
2706
Jens Axboefaac9962020-02-07 15:45:22 -07002707 /*
Chucheng Luobff60352020-03-25 11:31:38 +08002708 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
Jens Axboefaac9962020-02-07 15:45:22 -07002709 * retry them without IOCB_NOWAIT.
2710 */
2711 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
2712 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07002713 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkov014db002020-03-03 21:33:12 +03002714 kiocb_done(kiocb, ret2);
Jens Axboef67676d2019-12-02 11:03:47 -07002715 } else {
2716copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002717 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002718 inline_vecs, &iter);
2719 if (ret)
2720 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002721 /* any defer here is final, must blocking retry */
Jens Axboe490e8962020-04-28 13:16:53 -06002722 if (!file_can_poll(req->file))
2723 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002724 return -EAGAIN;
2725 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002726 }
Jens Axboe31b51512019-01-18 22:56:34 -07002727out_free:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002728 req->flags &= ~REQ_F_NEED_CLEANUP;
Pavel Begunkov1e950812020-02-06 19:51:16 +03002729 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002730 return ret;
2731}
2732
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002733static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2734{
2735 struct io_splice* sp = &req->splice;
2736 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
2737 int ret;
2738
2739 if (req->flags & REQ_F_NEED_CLEANUP)
2740 return 0;
2741
2742 sp->file_in = NULL;
2743 sp->off_in = READ_ONCE(sqe->splice_off_in);
2744 sp->off_out = READ_ONCE(sqe->off);
2745 sp->len = READ_ONCE(sqe->len);
2746 sp->flags = READ_ONCE(sqe->splice_flags);
2747
2748 if (unlikely(sp->flags & ~valid_flags))
2749 return -EINVAL;
2750
2751 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
2752 (sp->flags & SPLICE_F_FD_IN_FIXED));
2753 if (ret)
2754 return ret;
2755 req->flags |= REQ_F_NEED_CLEANUP;
2756
2757 if (!S_ISREG(file_inode(sp->file_in)->i_mode))
2758 req->work.flags |= IO_WQ_WORK_UNBOUND;
2759
2760 return 0;
2761}
2762
Pavel Begunkov014db002020-03-03 21:33:12 +03002763static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002764{
2765 struct io_splice *sp = &req->splice;
2766 struct file *in = sp->file_in;
2767 struct file *out = sp->file_out;
2768 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
2769 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03002770 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002771
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03002772 if (force_nonblock)
2773 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002774
2775 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
2776 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03002777
2778 if (sp->len) {
2779 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
2780 if (force_nonblock && ret == -EAGAIN)
2781 return -EAGAIN;
2782 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002783
2784 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
2785 req->flags &= ~REQ_F_NEED_CLEANUP;
2786
2787 io_cqring_add_event(req, ret);
2788 if (ret != sp->len)
2789 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03002790 io_put_req(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002791 return 0;
2792}
2793
Jens Axboe2b188cc2019-01-07 10:46:33 -07002794/*
2795 * IORING_OP_NOP just posts a completion event, nothing else.
2796 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002797static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002798{
2799 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002800
Jens Axboedef596e2019-01-09 08:59:42 -07002801 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2802 return -EINVAL;
2803
Jens Axboe78e19bb2019-11-06 15:21:34 -07002804 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002805 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002806 return 0;
2807}
2808
Jens Axboe3529d8c2019-12-19 18:24:38 -07002809static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002810{
Jens Axboe6b063142019-01-10 22:13:58 -07002811 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002812
Jens Axboe09bb8392019-03-13 12:39:28 -06002813 if (!req->file)
2814 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002815
Jens Axboe6b063142019-01-10 22:13:58 -07002816 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002817 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002818 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002819 return -EINVAL;
2820
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002821 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2822 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2823 return -EINVAL;
2824
2825 req->sync.off = READ_ONCE(sqe->off);
2826 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002827 return 0;
2828}
2829
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002830static bool io_req_cancelled(struct io_kiocb *req)
2831{
2832 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2833 req_set_fail_links(req);
2834 io_cqring_add_event(req, -ECANCELED);
2835 io_put_req(req);
2836 return true;
2837 }
2838
2839 return false;
2840}
2841
Pavel Begunkov014db002020-03-03 21:33:12 +03002842static void __io_fsync(struct io_kiocb *req)
Jens Axboe78912932020-01-14 22:09:06 -07002843{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002844 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002845 int ret;
2846
Jens Axboe9adbd452019-12-20 08:45:55 -07002847 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002848 end > 0 ? end : LLONG_MAX,
2849 req->sync.flags & IORING_FSYNC_DATASYNC);
2850 if (ret < 0)
2851 req_set_fail_links(req);
2852 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002853 io_put_req(req);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002854}
2855
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002856static void io_fsync_finish(struct io_wq_work **workptr)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002857{
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002858 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002859
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002860 if (io_req_cancelled(req))
2861 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03002862 __io_fsync(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03002863 io_steal_work(req, workptr);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002864}
2865
Pavel Begunkov014db002020-03-03 21:33:12 +03002866static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002867{
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002868 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002869 if (force_nonblock) {
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002870 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002871 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002872 }
Pavel Begunkov014db002020-03-03 21:33:12 +03002873 __io_fsync(req);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002874 return 0;
2875}
2876
Pavel Begunkov014db002020-03-03 21:33:12 +03002877static void __io_fallocate(struct io_kiocb *req)
Jens Axboed63d1b52019-12-10 10:38:56 -07002878{
Jens Axboed63d1b52019-12-10 10:38:56 -07002879 int ret;
2880
Jens Axboe4ed734b2020-03-20 11:23:41 -06002881 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
Jens Axboed63d1b52019-12-10 10:38:56 -07002882 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2883 req->sync.len);
Jens Axboe4ed734b2020-03-20 11:23:41 -06002884 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
Jens Axboed63d1b52019-12-10 10:38:56 -07002885 if (ret < 0)
2886 req_set_fail_links(req);
2887 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002888 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002889}
2890
Jens Axboe2b188cc2019-01-07 10:46:33 -07002891static void io_fallocate_finish(struct io_wq_work **workptr)
2892{
2893 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Jens Axboed63d1b52019-12-10 10:38:56 -07002894
2895 if (io_req_cancelled(req))
2896 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03002897 __io_fallocate(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03002898 io_steal_work(req, workptr);
Jens Axboed63d1b52019-12-10 10:38:56 -07002899}
2900
2901static int io_fallocate_prep(struct io_kiocb *req,
2902 const struct io_uring_sqe *sqe)
2903{
2904 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2905 return -EINVAL;
2906
2907 req->sync.off = READ_ONCE(sqe->off);
2908 req->sync.len = READ_ONCE(sqe->addr);
2909 req->sync.mode = READ_ONCE(sqe->len);
Jens Axboe4ed734b2020-03-20 11:23:41 -06002910 req->fsize = rlimit(RLIMIT_FSIZE);
Jens Axboed63d1b52019-12-10 10:38:56 -07002911 return 0;
2912}
2913
Pavel Begunkov014db002020-03-03 21:33:12 +03002914static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07002915{
Jens Axboed63d1b52019-12-10 10:38:56 -07002916 /* fallocate always requiring blocking context */
2917 if (force_nonblock) {
Jens Axboed63d1b52019-12-10 10:38:56 -07002918 req->work.func = io_fallocate_finish;
2919 return -EAGAIN;
2920 }
2921
Pavel Begunkov014db002020-03-03 21:33:12 +03002922 __io_fallocate(req);
Jens Axboed63d1b52019-12-10 10:38:56 -07002923 return 0;
2924}
2925
Jens Axboe15b71ab2019-12-11 11:20:36 -07002926static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2927{
Jens Axboef8748882020-01-08 17:47:02 -07002928 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002929 int ret;
2930
2931 if (sqe->ioprio || sqe->buf_index)
2932 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03002933 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07002934 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002935 if (req->flags & REQ_F_NEED_CLEANUP)
2936 return 0;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002937
2938 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002939 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002940 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002941 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06002942 if (force_o_largefile())
2943 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002944
Jens Axboef8748882020-01-08 17:47:02 -07002945 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002946 if (IS_ERR(req->open.filename)) {
2947 ret = PTR_ERR(req->open.filename);
2948 req->open.filename = NULL;
2949 return ret;
2950 }
2951
Jens Axboe4022e7a2020-03-19 19:23:18 -06002952 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002953 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002954 return 0;
2955}
2956
Jens Axboecebdb982020-01-08 17:59:24 -07002957static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2958{
2959 struct open_how __user *how;
2960 const char __user *fname;
2961 size_t len;
2962 int ret;
2963
2964 if (sqe->ioprio || sqe->buf_index)
2965 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03002966 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07002967 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002968 if (req->flags & REQ_F_NEED_CLEANUP)
2969 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07002970
2971 req->open.dfd = READ_ONCE(sqe->fd);
2972 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2973 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2974 len = READ_ONCE(sqe->len);
2975
2976 if (len < OPEN_HOW_SIZE_VER0)
2977 return -EINVAL;
2978
2979 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2980 len);
2981 if (ret)
2982 return ret;
2983
2984 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2985 req->open.how.flags |= O_LARGEFILE;
2986
2987 req->open.filename = getname(fname);
2988 if (IS_ERR(req->open.filename)) {
2989 ret = PTR_ERR(req->open.filename);
2990 req->open.filename = NULL;
2991 return ret;
2992 }
2993
Jens Axboe4022e7a2020-03-19 19:23:18 -06002994 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002995 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboecebdb982020-01-08 17:59:24 -07002996 return 0;
2997}
2998
Pavel Begunkov014db002020-03-03 21:33:12 +03002999static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003000{
3001 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003002 struct file *file;
3003 int ret;
3004
Jens Axboef86cd202020-01-29 13:46:44 -07003005 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003006 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003007
Jens Axboecebdb982020-01-08 17:59:24 -07003008 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003009 if (ret)
3010 goto err;
3011
Jens Axboe4022e7a2020-03-19 19:23:18 -06003012 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003013 if (ret < 0)
3014 goto err;
3015
3016 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3017 if (IS_ERR(file)) {
3018 put_unused_fd(ret);
3019 ret = PTR_ERR(file);
3020 } else {
3021 fsnotify_open(file);
3022 fd_install(ret, file);
3023 }
3024err:
3025 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003026 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003027 if (ret < 0)
3028 req_set_fail_links(req);
3029 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003030 io_put_req(req);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003031 return 0;
3032}
3033
Pavel Begunkov014db002020-03-03 21:33:12 +03003034static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003035{
3036 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
Pavel Begunkov014db002020-03-03 21:33:12 +03003037 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003038}
3039
Jens Axboe067524e2020-03-02 16:32:28 -07003040static int io_remove_buffers_prep(struct io_kiocb *req,
3041 const struct io_uring_sqe *sqe)
3042{
3043 struct io_provide_buf *p = &req->pbuf;
3044 u64 tmp;
3045
3046 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3047 return -EINVAL;
3048
3049 tmp = READ_ONCE(sqe->fd);
3050 if (!tmp || tmp > USHRT_MAX)
3051 return -EINVAL;
3052
3053 memset(p, 0, sizeof(*p));
3054 p->nbufs = tmp;
3055 p->bgid = READ_ONCE(sqe->buf_group);
3056 return 0;
3057}
3058
3059static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3060 int bgid, unsigned nbufs)
3061{
3062 unsigned i = 0;
3063
3064 /* shouldn't happen */
3065 if (!nbufs)
3066 return 0;
3067
3068 /* the head kbuf is the list itself */
3069 while (!list_empty(&buf->list)) {
3070 struct io_buffer *nxt;
3071
3072 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3073 list_del(&nxt->list);
3074 kfree(nxt);
3075 if (++i == nbufs)
3076 return i;
3077 }
3078 i++;
3079 kfree(buf);
3080 idr_remove(&ctx->io_buffer_idr, bgid);
3081
3082 return i;
3083}
3084
3085static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock)
3086{
3087 struct io_provide_buf *p = &req->pbuf;
3088 struct io_ring_ctx *ctx = req->ctx;
3089 struct io_buffer *head;
3090 int ret = 0;
3091
3092 io_ring_submit_lock(ctx, !force_nonblock);
3093
3094 lockdep_assert_held(&ctx->uring_lock);
3095
3096 ret = -ENOENT;
3097 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3098 if (head)
3099 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3100
3101 io_ring_submit_lock(ctx, !force_nonblock);
3102 if (ret < 0)
3103 req_set_fail_links(req);
3104 io_cqring_add_event(req, ret);
3105 io_put_req(req);
3106 return 0;
3107}
3108
Jens Axboeddf0322d2020-02-23 16:41:33 -07003109static int io_provide_buffers_prep(struct io_kiocb *req,
3110 const struct io_uring_sqe *sqe)
3111{
3112 struct io_provide_buf *p = &req->pbuf;
3113 u64 tmp;
3114
3115 if (sqe->ioprio || sqe->rw_flags)
3116 return -EINVAL;
3117
3118 tmp = READ_ONCE(sqe->fd);
3119 if (!tmp || tmp > USHRT_MAX)
3120 return -E2BIG;
3121 p->nbufs = tmp;
3122 p->addr = READ_ONCE(sqe->addr);
3123 p->len = READ_ONCE(sqe->len);
3124
3125 if (!access_ok(u64_to_user_ptr(p->addr), p->len))
3126 return -EFAULT;
3127
3128 p->bgid = READ_ONCE(sqe->buf_group);
3129 tmp = READ_ONCE(sqe->off);
3130 if (tmp > USHRT_MAX)
3131 return -E2BIG;
3132 p->bid = tmp;
3133 return 0;
3134}
3135
3136static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3137{
3138 struct io_buffer *buf;
3139 u64 addr = pbuf->addr;
3140 int i, bid = pbuf->bid;
3141
3142 for (i = 0; i < pbuf->nbufs; i++) {
3143 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3144 if (!buf)
3145 break;
3146
3147 buf->addr = addr;
3148 buf->len = pbuf->len;
3149 buf->bid = bid;
3150 addr += pbuf->len;
3151 bid++;
3152 if (!*head) {
3153 INIT_LIST_HEAD(&buf->list);
3154 *head = buf;
3155 } else {
3156 list_add_tail(&buf->list, &(*head)->list);
3157 }
3158 }
3159
3160 return i ? i : -ENOMEM;
3161}
3162
Jens Axboeddf0322d2020-02-23 16:41:33 -07003163static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock)
3164{
3165 struct io_provide_buf *p = &req->pbuf;
3166 struct io_ring_ctx *ctx = req->ctx;
3167 struct io_buffer *head, *list;
3168 int ret = 0;
3169
3170 io_ring_submit_lock(ctx, !force_nonblock);
3171
3172 lockdep_assert_held(&ctx->uring_lock);
3173
3174 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3175
3176 ret = io_add_buffers(p, &head);
3177 if (ret < 0)
3178 goto out;
3179
3180 if (!list) {
3181 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3182 GFP_KERNEL);
3183 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003184 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003185 goto out;
3186 }
3187 }
3188out:
3189 io_ring_submit_unlock(ctx, !force_nonblock);
3190 if (ret < 0)
3191 req_set_fail_links(req);
3192 io_cqring_add_event(req, ret);
3193 io_put_req(req);
3194 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003195}
3196
Jens Axboe3e4827b2020-01-08 15:18:09 -07003197static int io_epoll_ctl_prep(struct io_kiocb *req,
3198 const struct io_uring_sqe *sqe)
3199{
3200#if defined(CONFIG_EPOLL)
3201 if (sqe->ioprio || sqe->buf_index)
3202 return -EINVAL;
3203
3204 req->epoll.epfd = READ_ONCE(sqe->fd);
3205 req->epoll.op = READ_ONCE(sqe->len);
3206 req->epoll.fd = READ_ONCE(sqe->off);
3207
3208 if (ep_op_has_event(req->epoll.op)) {
3209 struct epoll_event __user *ev;
3210
3211 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3212 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3213 return -EFAULT;
3214 }
3215
3216 return 0;
3217#else
3218 return -EOPNOTSUPP;
3219#endif
3220}
3221
Pavel Begunkov014db002020-03-03 21:33:12 +03003222static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003223{
3224#if defined(CONFIG_EPOLL)
3225 struct io_epoll *ie = &req->epoll;
3226 int ret;
3227
3228 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3229 if (force_nonblock && ret == -EAGAIN)
3230 return -EAGAIN;
3231
3232 if (ret < 0)
3233 req_set_fail_links(req);
3234 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003235 io_put_req(req);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003236 return 0;
3237#else
3238 return -EOPNOTSUPP;
3239#endif
3240}
3241
Jens Axboec1ca7572019-12-25 22:18:28 -07003242static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3243{
3244#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3245 if (sqe->ioprio || sqe->buf_index || sqe->off)
3246 return -EINVAL;
3247
3248 req->madvise.addr = READ_ONCE(sqe->addr);
3249 req->madvise.len = READ_ONCE(sqe->len);
3250 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3251 return 0;
3252#else
3253 return -EOPNOTSUPP;
3254#endif
3255}
3256
Pavel Begunkov014db002020-03-03 21:33:12 +03003257static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003258{
3259#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3260 struct io_madvise *ma = &req->madvise;
3261 int ret;
3262
3263 if (force_nonblock)
3264 return -EAGAIN;
3265
3266 ret = do_madvise(ma->addr, ma->len, ma->advice);
3267 if (ret < 0)
3268 req_set_fail_links(req);
3269 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003270 io_put_req(req);
Jens Axboec1ca7572019-12-25 22:18:28 -07003271 return 0;
3272#else
3273 return -EOPNOTSUPP;
3274#endif
3275}
3276
Jens Axboe4840e412019-12-25 22:03:45 -07003277static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3278{
3279 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3280 return -EINVAL;
3281
3282 req->fadvise.offset = READ_ONCE(sqe->off);
3283 req->fadvise.len = READ_ONCE(sqe->len);
3284 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3285 return 0;
3286}
3287
Pavel Begunkov014db002020-03-03 21:33:12 +03003288static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07003289{
3290 struct io_fadvise *fa = &req->fadvise;
3291 int ret;
3292
Jens Axboe3e694262020-02-01 09:22:49 -07003293 if (force_nonblock) {
3294 switch (fa->advice) {
3295 case POSIX_FADV_NORMAL:
3296 case POSIX_FADV_RANDOM:
3297 case POSIX_FADV_SEQUENTIAL:
3298 break;
3299 default:
3300 return -EAGAIN;
3301 }
3302 }
Jens Axboe4840e412019-12-25 22:03:45 -07003303
3304 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3305 if (ret < 0)
3306 req_set_fail_links(req);
3307 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003308 io_put_req(req);
Jens Axboe4840e412019-12-25 22:03:45 -07003309 return 0;
3310}
3311
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003312static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3313{
Jens Axboef8748882020-01-08 17:47:02 -07003314 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003315 unsigned lookup_flags;
3316 int ret;
3317
3318 if (sqe->ioprio || sqe->buf_index)
3319 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003320 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003321 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003322 if (req->flags & REQ_F_NEED_CLEANUP)
3323 return 0;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003324
3325 req->open.dfd = READ_ONCE(sqe->fd);
3326 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07003327 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003328 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07003329 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003330
Jens Axboec12cedf2020-01-08 17:41:21 -07003331 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003332 return -EINVAL;
3333
Jens Axboef8748882020-01-08 17:47:02 -07003334 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003335 if (IS_ERR(req->open.filename)) {
3336 ret = PTR_ERR(req->open.filename);
3337 req->open.filename = NULL;
3338 return ret;
3339 }
3340
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003341 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003342 return 0;
3343}
3344
Pavel Begunkov014db002020-03-03 21:33:12 +03003345static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003346{
3347 struct io_open *ctx = &req->open;
3348 unsigned lookup_flags;
3349 struct path path;
3350 struct kstat stat;
3351 int ret;
3352
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003353 if (force_nonblock) {
3354 /* only need file table for an actual valid fd */
3355 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
3356 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003357 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003358 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003359
Jens Axboec12cedf2020-01-08 17:41:21 -07003360 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003361 return -EINVAL;
3362
3363retry:
3364 /* filename_lookup() drops it, keep a reference */
3365 ctx->filename->refcnt++;
3366
3367 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
3368 NULL);
3369 if (ret)
3370 goto err;
3371
Jens Axboec12cedf2020-01-08 17:41:21 -07003372 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003373 path_put(&path);
3374 if (retry_estale(ret, lookup_flags)) {
3375 lookup_flags |= LOOKUP_REVAL;
3376 goto retry;
3377 }
3378 if (!ret)
3379 ret = cp_statx(&stat, ctx->buffer);
3380err:
3381 putname(ctx->filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003382 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003383 if (ret < 0)
3384 req_set_fail_links(req);
3385 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003386 io_put_req(req);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003387 return 0;
3388}
3389
Jens Axboeb5dba592019-12-11 14:02:38 -07003390static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3391{
3392 /*
3393 * If we queue this for async, it must not be cancellable. That would
3394 * leave the 'file' in an undeterminate state.
3395 */
3396 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
3397
3398 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
3399 sqe->rw_flags || sqe->buf_index)
3400 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003401 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003402 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07003403
3404 req->close.fd = READ_ONCE(sqe->fd);
3405 if (req->file->f_op == &io_uring_fops ||
Pavel Begunkovb14cca02020-01-17 04:45:59 +03003406 req->close.fd == req->ctx->ring_fd)
Jens Axboeb5dba592019-12-11 14:02:38 -07003407 return -EBADF;
3408
3409 return 0;
3410}
3411
Pavel Begunkova93b3332020-02-08 14:04:34 +03003412/* only called when __close_fd_get_file() is done */
Pavel Begunkov014db002020-03-03 21:33:12 +03003413static void __io_close_finish(struct io_kiocb *req)
Pavel Begunkova93b3332020-02-08 14:04:34 +03003414{
3415 int ret;
3416
3417 ret = filp_close(req->close.put_file, req->work.files);
3418 if (ret < 0)
3419 req_set_fail_links(req);
3420 io_cqring_add_event(req, ret);
3421 fput(req->close.put_file);
Pavel Begunkov014db002020-03-03 21:33:12 +03003422 io_put_req(req);
Pavel Begunkova93b3332020-02-08 14:04:34 +03003423}
3424
Jens Axboeb5dba592019-12-11 14:02:38 -07003425static void io_close_finish(struct io_wq_work **workptr)
3426{
3427 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Jens Axboeb5dba592019-12-11 14:02:38 -07003428
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03003429 /* not cancellable, don't do io_req_cancelled() */
Pavel Begunkov014db002020-03-03 21:33:12 +03003430 __io_close_finish(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03003431 io_steal_work(req, workptr);
Jens Axboeb5dba592019-12-11 14:02:38 -07003432}
3433
Pavel Begunkov014db002020-03-03 21:33:12 +03003434static int io_close(struct io_kiocb *req, bool force_nonblock)
Jens Axboeb5dba592019-12-11 14:02:38 -07003435{
3436 int ret;
3437
3438 req->close.put_file = NULL;
3439 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
3440 if (ret < 0)
3441 return ret;
3442
3443 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkova2100672020-03-02 23:45:16 +03003444 if (req->close.put_file->f_op->flush && force_nonblock) {
Pavel Begunkov594506f2020-03-03 21:33:11 +03003445 /* submission ref will be dropped, take it for async */
3446 refcount_inc(&req->refs);
3447
Pavel Begunkova2100672020-03-02 23:45:16 +03003448 req->work.func = io_close_finish;
3449 /*
3450 * Do manual async queue here to avoid grabbing files - we don't
3451 * need the files, and it'll cause io_close_finish() to close
3452 * the file again and cause a double CQE entry for this request
3453 */
3454 io_queue_async_work(req);
3455 return 0;
3456 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003457
3458 /*
3459 * No ->flush(), safely close from here and just punt the
3460 * fput() to async context.
3461 */
Pavel Begunkov014db002020-03-03 21:33:12 +03003462 __io_close_finish(req);
Jens Axboe1a417f42020-01-31 17:16:48 -07003463 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07003464}
3465
Jens Axboe3529d8c2019-12-19 18:24:38 -07003466static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003467{
3468 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003469
3470 if (!req->file)
3471 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003472
3473 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3474 return -EINVAL;
3475 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
3476 return -EINVAL;
3477
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003478 req->sync.off = READ_ONCE(sqe->off);
3479 req->sync.len = READ_ONCE(sqe->len);
3480 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003481 return 0;
3482}
3483
Pavel Begunkov014db002020-03-03 21:33:12 +03003484static void __io_sync_file_range(struct io_kiocb *req)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003485{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003486 int ret;
3487
Jens Axboe9adbd452019-12-20 08:45:55 -07003488 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003489 req->sync.flags);
3490 if (ret < 0)
3491 req_set_fail_links(req);
3492 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003493 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03003494}
3495
3496
3497static void io_sync_file_range_finish(struct io_wq_work **workptr)
3498{
3499 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03003500
3501 if (io_req_cancelled(req))
3502 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03003503 __io_sync_file_range(req);
Pavel Begunkov7759a0b2020-05-01 17:09:36 +03003504 io_steal_work(req, workptr);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003505}
3506
Pavel Begunkov014db002020-03-03 21:33:12 +03003507static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003508{
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003509 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003510 if (force_nonblock) {
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003511 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003512 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003513 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003514
Pavel Begunkov014db002020-03-03 21:33:12 +03003515 __io_sync_file_range(req);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003516 return 0;
3517}
3518
YueHaibing469956e2020-03-04 15:53:52 +08003519#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003520static int io_setup_async_msg(struct io_kiocb *req,
3521 struct io_async_msghdr *kmsg)
3522{
3523 if (req->io)
3524 return -EAGAIN;
3525 if (io_alloc_async_ctx(req)) {
3526 if (kmsg->iov != kmsg->fast_iov)
3527 kfree(kmsg->iov);
3528 return -ENOMEM;
3529 }
3530 req->flags |= REQ_F_NEED_CLEANUP;
3531 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
3532 return -EAGAIN;
3533}
3534
Jens Axboe3529d8c2019-12-19 18:24:38 -07003535static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06003536{
Jens Axboee47293f2019-12-20 08:58:21 -07003537 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003538 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003539 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003540
Jens Axboee47293f2019-12-20 08:58:21 -07003541 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3542 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07003543 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003544
Jens Axboed8768362020-02-27 14:17:49 -07003545#ifdef CONFIG_COMPAT
3546 if (req->ctx->compat)
3547 sr->msg_flags |= MSG_CMSG_COMPAT;
3548#endif
3549
Jens Axboefddafac2020-01-04 20:19:44 -07003550 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003551 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003552 /* iovec is already imported */
3553 if (req->flags & REQ_F_NEED_CLEANUP)
3554 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003555
Jens Axboed9688562019-12-09 19:35:20 -07003556 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003557 ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003558 &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003559 if (!ret)
3560 req->flags |= REQ_F_NEED_CLEANUP;
3561 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003562}
3563
Pavel Begunkov014db002020-03-03 21:33:12 +03003564static int io_sendmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003565{
Jens Axboe0b416c32019-12-15 10:57:46 -07003566 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07003567 struct socket *sock;
3568 int ret;
3569
3570 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3571 return -EINVAL;
3572
3573 sock = sock_from_file(req->file, &ret);
3574 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003575 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003576 unsigned flags;
3577
Jens Axboe03b12302019-12-02 18:50:25 -07003578 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003579 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003580 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003581 /* if iov is set, it's allocated already */
3582 if (!kmsg->iov)
3583 kmsg->iov = kmsg->fast_iov;
3584 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003585 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003586 struct io_sr_msg *sr = &req->sr_msg;
3587
Jens Axboe0b416c32019-12-15 10:57:46 -07003588 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003589 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003590
3591 io.msg.iov = io.msg.fast_iov;
3592 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3593 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003594 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003595 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003596 }
3597
Jens Axboee47293f2019-12-20 08:58:21 -07003598 flags = req->sr_msg.msg_flags;
3599 if (flags & MSG_DONTWAIT)
3600 req->flags |= REQ_F_NOWAIT;
3601 else if (force_nonblock)
3602 flags |= MSG_DONTWAIT;
3603
Jens Axboe0b416c32019-12-15 10:57:46 -07003604 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003605 if (force_nonblock && ret == -EAGAIN)
3606 return io_setup_async_msg(req, kmsg);
Jens Axboe03b12302019-12-02 18:50:25 -07003607 if (ret == -ERESTARTSYS)
3608 ret = -EINTR;
3609 }
3610
Pavel Begunkov1e950812020-02-06 19:51:16 +03003611 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003612 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003613 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe03b12302019-12-02 18:50:25 -07003614 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003615 if (ret < 0)
3616 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003617 io_put_req(req);
Jens Axboe03b12302019-12-02 18:50:25 -07003618 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003619}
3620
Pavel Begunkov014db002020-03-03 21:33:12 +03003621static int io_send(struct io_kiocb *req, bool force_nonblock)
Jens Axboefddafac2020-01-04 20:19:44 -07003622{
Jens Axboefddafac2020-01-04 20:19:44 -07003623 struct socket *sock;
3624 int ret;
3625
3626 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3627 return -EINVAL;
3628
3629 sock = sock_from_file(req->file, &ret);
3630 if (sock) {
3631 struct io_sr_msg *sr = &req->sr_msg;
3632 struct msghdr msg;
3633 struct iovec iov;
3634 unsigned flags;
3635
3636 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3637 &msg.msg_iter);
3638 if (ret)
3639 return ret;
3640
3641 msg.msg_name = NULL;
3642 msg.msg_control = NULL;
3643 msg.msg_controllen = 0;
3644 msg.msg_namelen = 0;
3645
3646 flags = req->sr_msg.msg_flags;
3647 if (flags & MSG_DONTWAIT)
3648 req->flags |= REQ_F_NOWAIT;
3649 else if (force_nonblock)
3650 flags |= MSG_DONTWAIT;
3651
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003652 msg.msg_flags = flags;
3653 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003654 if (force_nonblock && ret == -EAGAIN)
3655 return -EAGAIN;
3656 if (ret == -ERESTARTSYS)
3657 ret = -EINTR;
3658 }
3659
3660 io_cqring_add_event(req, ret);
3661 if (ret < 0)
3662 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003663 io_put_req(req);
Jens Axboefddafac2020-01-04 20:19:44 -07003664 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003665}
3666
Jens Axboe52de1fe2020-02-27 10:15:42 -07003667static int __io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
3668{
3669 struct io_sr_msg *sr = &req->sr_msg;
3670 struct iovec __user *uiov;
3671 size_t iov_len;
3672 int ret;
3673
3674 ret = __copy_msghdr_from_user(&io->msg.msg, sr->msg, &io->msg.uaddr,
3675 &uiov, &iov_len);
3676 if (ret)
3677 return ret;
3678
3679 if (req->flags & REQ_F_BUFFER_SELECT) {
3680 if (iov_len > 1)
3681 return -EINVAL;
3682 if (copy_from_user(io->msg.iov, uiov, sizeof(*uiov)))
3683 return -EFAULT;
3684 sr->len = io->msg.iov[0].iov_len;
3685 iov_iter_init(&io->msg.msg.msg_iter, READ, io->msg.iov, 1,
3686 sr->len);
3687 io->msg.iov = NULL;
3688 } else {
3689 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
3690 &io->msg.iov, &io->msg.msg.msg_iter);
3691 if (ret > 0)
3692 ret = 0;
3693 }
3694
3695 return ret;
3696}
3697
3698#ifdef CONFIG_COMPAT
3699static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
3700 struct io_async_ctx *io)
3701{
3702 struct compat_msghdr __user *msg_compat;
3703 struct io_sr_msg *sr = &req->sr_msg;
3704 struct compat_iovec __user *uiov;
3705 compat_uptr_t ptr;
3706 compat_size_t len;
3707 int ret;
3708
3709 msg_compat = (struct compat_msghdr __user *) sr->msg;
3710 ret = __get_compat_msghdr(&io->msg.msg, msg_compat, &io->msg.uaddr,
3711 &ptr, &len);
3712 if (ret)
3713 return ret;
3714
3715 uiov = compat_ptr(ptr);
3716 if (req->flags & REQ_F_BUFFER_SELECT) {
3717 compat_ssize_t clen;
3718
3719 if (len > 1)
3720 return -EINVAL;
3721 if (!access_ok(uiov, sizeof(*uiov)))
3722 return -EFAULT;
3723 if (__get_user(clen, &uiov->iov_len))
3724 return -EFAULT;
3725 if (clen < 0)
3726 return -EINVAL;
3727 sr->len = io->msg.iov[0].iov_len;
3728 io->msg.iov = NULL;
3729 } else {
3730 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
3731 &io->msg.iov,
3732 &io->msg.msg.msg_iter);
3733 if (ret < 0)
3734 return ret;
3735 }
3736
3737 return 0;
3738}
Jens Axboe03b12302019-12-02 18:50:25 -07003739#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07003740
3741static int io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
3742{
3743 io->msg.iov = io->msg.fast_iov;
3744
3745#ifdef CONFIG_COMPAT
3746 if (req->ctx->compat)
3747 return __io_compat_recvmsg_copy_hdr(req, io);
3748#endif
3749
3750 return __io_recvmsg_copy_hdr(req, io);
3751}
3752
Jens Axboebcda7ba2020-02-23 16:42:51 -07003753static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
3754 int *cflags, bool needs_lock)
3755{
3756 struct io_sr_msg *sr = &req->sr_msg;
3757 struct io_buffer *kbuf;
3758
3759 if (!(req->flags & REQ_F_BUFFER_SELECT))
3760 return NULL;
3761
3762 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
3763 if (IS_ERR(kbuf))
3764 return kbuf;
3765
3766 sr->kbuf = kbuf;
3767 req->flags |= REQ_F_BUFFER_SELECTED;
3768
3769 *cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
3770 *cflags |= IORING_CQE_F_BUFFER;
3771 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07003772}
3773
Jens Axboe3529d8c2019-12-19 18:24:38 -07003774static int io_recvmsg_prep(struct io_kiocb *req,
3775 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003776{
Jens Axboee47293f2019-12-20 08:58:21 -07003777 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003778 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003779 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07003780
Jens Axboe3529d8c2019-12-19 18:24:38 -07003781 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3782 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003783 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003784 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003785
Jens Axboed8768362020-02-27 14:17:49 -07003786#ifdef CONFIG_COMPAT
3787 if (req->ctx->compat)
3788 sr->msg_flags |= MSG_CMSG_COMPAT;
3789#endif
3790
Jens Axboefddafac2020-01-04 20:19:44 -07003791 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003792 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003793 /* iovec is already imported */
3794 if (req->flags & REQ_F_NEED_CLEANUP)
3795 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003796
Jens Axboe52de1fe2020-02-27 10:15:42 -07003797 ret = io_recvmsg_copy_hdr(req, io);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003798 if (!ret)
3799 req->flags |= REQ_F_NEED_CLEANUP;
3800 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003801}
3802
Pavel Begunkov014db002020-03-03 21:33:12 +03003803static int io_recvmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003804{
Jens Axboe0b416c32019-12-15 10:57:46 -07003805 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003806 struct socket *sock;
Jens Axboe52de1fe2020-02-27 10:15:42 -07003807 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003808
3809 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3810 return -EINVAL;
3811
3812 sock = sock_from_file(req->file, &ret);
3813 if (sock) {
Jens Axboe52de1fe2020-02-27 10:15:42 -07003814 struct io_buffer *kbuf;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003815 struct io_async_ctx io;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003816 unsigned flags;
3817
Jens Axboe03b12302019-12-02 18:50:25 -07003818 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003819 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003820 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003821 /* if iov is set, it's allocated already */
3822 if (!kmsg->iov)
3823 kmsg->iov = kmsg->fast_iov;
3824 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003825 } else {
Jens Axboe0b416c32019-12-15 10:57:46 -07003826 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003827 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003828
Jens Axboe52de1fe2020-02-27 10:15:42 -07003829 ret = io_recvmsg_copy_hdr(req, &io);
Jens Axboe03b12302019-12-02 18:50:25 -07003830 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003831 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003832 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003833
Jens Axboe52de1fe2020-02-27 10:15:42 -07003834 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
3835 if (IS_ERR(kbuf)) {
3836 return PTR_ERR(kbuf);
3837 } else if (kbuf) {
3838 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3839 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
3840 1, req->sr_msg.len);
3841 }
3842
Jens Axboee47293f2019-12-20 08:58:21 -07003843 flags = req->sr_msg.msg_flags;
3844 if (flags & MSG_DONTWAIT)
3845 req->flags |= REQ_F_NOWAIT;
3846 else if (force_nonblock)
3847 flags |= MSG_DONTWAIT;
3848
3849 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3850 kmsg->uaddr, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003851 if (force_nonblock && ret == -EAGAIN)
3852 return io_setup_async_msg(req, kmsg);
Jens Axboe441cdbd2019-12-02 18:49:10 -07003853 if (ret == -ERESTARTSYS)
3854 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003855 }
3856
Pavel Begunkov1e950812020-02-06 19:51:16 +03003857 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003858 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003859 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe52de1fe2020-02-27 10:15:42 -07003860 __io_cqring_add_event(req, ret, cflags);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003861 if (ret < 0)
3862 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003863 io_put_req(req);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003864 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003865}
3866
Pavel Begunkov014db002020-03-03 21:33:12 +03003867static int io_recv(struct io_kiocb *req, bool force_nonblock)
Jens Axboefddafac2020-01-04 20:19:44 -07003868{
Jens Axboebcda7ba2020-02-23 16:42:51 -07003869 struct io_buffer *kbuf = NULL;
Jens Axboefddafac2020-01-04 20:19:44 -07003870 struct socket *sock;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003871 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003872
3873 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3874 return -EINVAL;
3875
3876 sock = sock_from_file(req->file, &ret);
3877 if (sock) {
3878 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003879 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07003880 struct msghdr msg;
3881 struct iovec iov;
3882 unsigned flags;
3883
Jens Axboebcda7ba2020-02-23 16:42:51 -07003884 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
3885 if (IS_ERR(kbuf))
3886 return PTR_ERR(kbuf);
3887 else if (kbuf)
3888 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07003889
Jens Axboebcda7ba2020-02-23 16:42:51 -07003890 ret = import_single_range(READ, buf, sr->len, &iov,
3891 &msg.msg_iter);
3892 if (ret) {
3893 kfree(kbuf);
3894 return ret;
3895 }
3896
3897 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07003898 msg.msg_name = NULL;
3899 msg.msg_control = NULL;
3900 msg.msg_controllen = 0;
3901 msg.msg_namelen = 0;
3902 msg.msg_iocb = NULL;
3903 msg.msg_flags = 0;
3904
3905 flags = req->sr_msg.msg_flags;
3906 if (flags & MSG_DONTWAIT)
3907 req->flags |= REQ_F_NOWAIT;
3908 else if (force_nonblock)
3909 flags |= MSG_DONTWAIT;
3910
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003911 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07003912 if (force_nonblock && ret == -EAGAIN)
3913 return -EAGAIN;
3914 if (ret == -ERESTARTSYS)
3915 ret = -EINTR;
3916 }
3917
Jens Axboebcda7ba2020-02-23 16:42:51 -07003918 kfree(kbuf);
3919 req->flags &= ~REQ_F_NEED_CLEANUP;
3920 __io_cqring_add_event(req, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07003921 if (ret < 0)
3922 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003923 io_put_req(req);
Jens Axboefddafac2020-01-04 20:19:44 -07003924 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003925}
3926
Jens Axboe3529d8c2019-12-19 18:24:38 -07003927static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003928{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003929 struct io_accept *accept = &req->accept;
3930
Jens Axboe17f2fe32019-10-17 14:42:58 -06003931 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3932 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003933 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003934 return -EINVAL;
3935
Jens Axboed55e5f52019-12-11 16:12:15 -07003936 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3937 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003938 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06003939 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003940 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003941}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003942
Pavel Begunkov014db002020-03-03 21:33:12 +03003943static int __io_accept(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003944{
3945 struct io_accept *accept = &req->accept;
3946 unsigned file_flags;
3947 int ret;
3948
3949 file_flags = force_nonblock ? O_NONBLOCK : 0;
3950 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06003951 accept->addr_len, accept->flags,
3952 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003953 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003954 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003955 if (ret == -ERESTARTSYS)
3956 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003957 if (ret < 0)
3958 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003959 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003960 io_put_req(req);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003961 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003962}
3963
3964static void io_accept_finish(struct io_wq_work **workptr)
3965{
3966 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003967
3968 if (io_req_cancelled(req))
3969 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03003970 __io_accept(req, false);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03003971 io_steal_work(req, workptr);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003972}
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003973
Pavel Begunkov014db002020-03-03 21:33:12 +03003974static int io_accept(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003975{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003976 int ret;
3977
Pavel Begunkov014db002020-03-03 21:33:12 +03003978 ret = __io_accept(req, force_nonblock);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003979 if (ret == -EAGAIN && force_nonblock) {
3980 req->work.func = io_accept_finish;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003981 return -EAGAIN;
3982 }
3983 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003984}
3985
Jens Axboe3529d8c2019-12-19 18:24:38 -07003986static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003987{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003988 struct io_connect *conn = &req->connect;
3989 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003990
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003991 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3992 return -EINVAL;
3993 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3994 return -EINVAL;
3995
Jens Axboe3529d8c2019-12-19 18:24:38 -07003996 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3997 conn->addr_len = READ_ONCE(sqe->addr2);
3998
3999 if (!io)
4000 return 0;
4001
4002 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004003 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004004}
4005
Pavel Begunkov014db002020-03-03 21:33:12 +03004006static int io_connect(struct io_kiocb *req, bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004007{
Jens Axboef499a022019-12-02 16:28:46 -07004008 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004009 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004010 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004011
Jens Axboef499a022019-12-02 16:28:46 -07004012 if (req->io) {
4013 io = req->io;
4014 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004015 ret = move_addr_to_kernel(req->connect.addr,
4016 req->connect.addr_len,
4017 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004018 if (ret)
4019 goto out;
4020 io = &__io;
4021 }
4022
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004023 file_flags = force_nonblock ? O_NONBLOCK : 0;
4024
4025 ret = __sys_connect_file(req->file, &io->connect.address,
4026 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004027 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004028 if (req->io)
4029 return -EAGAIN;
4030 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004031 ret = -ENOMEM;
4032 goto out;
4033 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004034 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004035 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004036 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004037 if (ret == -ERESTARTSYS)
4038 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004039out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004040 if (ret < 0)
4041 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004042 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03004043 io_put_req(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004044 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004045}
YueHaibing469956e2020-03-04 15:53:52 +08004046#else /* !CONFIG_NET */
4047static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4048{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004049 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004050}
4051
YueHaibing469956e2020-03-04 15:53:52 +08004052static int io_sendmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004053{
YueHaibing469956e2020-03-04 15:53:52 +08004054 return -EOPNOTSUPP;
4055}
4056
4057static int io_send(struct io_kiocb *req, bool force_nonblock)
4058{
4059 return -EOPNOTSUPP;
4060}
4061
4062static int io_recvmsg_prep(struct io_kiocb *req,
4063 const struct io_uring_sqe *sqe)
4064{
4065 return -EOPNOTSUPP;
4066}
4067
4068static int io_recvmsg(struct io_kiocb *req, bool force_nonblock)
4069{
4070 return -EOPNOTSUPP;
4071}
4072
4073static int io_recv(struct io_kiocb *req, bool force_nonblock)
4074{
4075 return -EOPNOTSUPP;
4076}
4077
4078static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4079{
4080 return -EOPNOTSUPP;
4081}
4082
4083static int io_accept(struct io_kiocb *req, bool force_nonblock)
4084{
4085 return -EOPNOTSUPP;
4086}
4087
4088static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4089{
4090 return -EOPNOTSUPP;
4091}
4092
4093static int io_connect(struct io_kiocb *req, bool force_nonblock)
4094{
4095 return -EOPNOTSUPP;
4096}
4097#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004098
Jens Axboed7718a92020-02-14 22:23:12 -07004099struct io_poll_table {
4100 struct poll_table_struct pt;
4101 struct io_kiocb *req;
4102 int error;
4103};
4104
4105static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
4106 struct wait_queue_head *head)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004107{
Jens Axboed7718a92020-02-14 22:23:12 -07004108 if (unlikely(poll->head)) {
4109 pt->error = -EINVAL;
4110 return;
4111 }
4112
4113 pt->error = 0;
4114 poll->head = head;
4115 add_wait_queue(head, &poll->wait);
4116}
4117
4118static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4119 struct poll_table_struct *p)
4120{
4121 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4122
4123 __io_queue_proc(&pt->req->apoll->poll, pt, head);
4124}
4125
4126static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4127 __poll_t mask, task_work_func_t func)
4128{
4129 struct task_struct *tsk;
Jens Axboeaa96bf82020-04-03 11:26:26 -06004130 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004131
4132 /* for instances that support it check for an event match first: */
4133 if (mask && !(mask & poll->events))
4134 return 0;
4135
4136 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4137
4138 list_del_init(&poll->wait.entry);
4139
4140 tsk = req->task;
4141 req->result = mask;
4142 init_task_work(&req->task_work, func);
4143 /*
Jens Axboeaa96bf82020-04-03 11:26:26 -06004144 * If this fails, then the task is exiting. Punt to one of the io-wq
4145 * threads to ensure the work gets run, we can't always rely on exit
4146 * cancelation taking care of this.
Jens Axboed7718a92020-02-14 22:23:12 -07004147 */
Jens Axboeaa96bf82020-04-03 11:26:26 -06004148 ret = task_work_add(tsk, &req->task_work, true);
4149 if (unlikely(ret)) {
4150 tsk = io_wq_get_task(req->ctx->io_wq);
4151 task_work_add(tsk, &req->task_work, true);
4152 }
Jens Axboed7718a92020-02-14 22:23:12 -07004153 wake_up_process(tsk);
4154 return 1;
4155}
4156
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004157static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4158 __acquires(&req->ctx->completion_lock)
4159{
4160 struct io_ring_ctx *ctx = req->ctx;
4161
4162 if (!req->result && !READ_ONCE(poll->canceled)) {
4163 struct poll_table_struct pt = { ._key = poll->events };
4164
4165 req->result = vfs_poll(req->file, &pt) & poll->events;
4166 }
4167
4168 spin_lock_irq(&ctx->completion_lock);
4169 if (!req->result && !READ_ONCE(poll->canceled)) {
4170 add_wait_queue(poll->head, &poll->wait);
4171 return true;
4172 }
4173
4174 return false;
4175}
4176
Jens Axboed7718a92020-02-14 22:23:12 -07004177static void io_async_task_func(struct callback_head *cb)
4178{
4179 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4180 struct async_poll *apoll = req->apoll;
4181 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2bae0472020-04-13 11:16:34 -06004182 bool canceled;
Jens Axboed7718a92020-02-14 22:23:12 -07004183
4184 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4185
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004186 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07004187 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004188 return;
Jens Axboed7718a92020-02-14 22:23:12 -07004189 }
4190
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004191 if (hash_hashed(&req->hash_node))
4192 hash_del(&req->hash_node);
4193
Jens Axboe2bae0472020-04-13 11:16:34 -06004194 canceled = READ_ONCE(apoll->poll.canceled);
4195 if (canceled) {
4196 io_cqring_fill_event(req, -ECANCELED);
4197 io_commit_cqring(ctx);
4198 }
4199
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004200 spin_unlock_irq(&ctx->completion_lock);
4201
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004202 /* restore ->work in case we need to retry again */
4203 memcpy(&req->work, &apoll->work, sizeof(req->work));
4204
Jens Axboe2bae0472020-04-13 11:16:34 -06004205 if (canceled) {
4206 kfree(apoll);
4207 io_cqring_ev_posted(ctx);
4208 req_set_fail_links(req);
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004209 io_double_put_req(req);
Jens Axboe2bae0472020-04-13 11:16:34 -06004210 return;
4211 }
4212
Jens Axboed7718a92020-02-14 22:23:12 -07004213 __set_current_state(TASK_RUNNING);
4214 mutex_lock(&ctx->uring_lock);
4215 __io_queue_sqe(req, NULL);
4216 mutex_unlock(&ctx->uring_lock);
4217
4218 kfree(apoll);
4219}
4220
4221static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4222 void *key)
4223{
4224 struct io_kiocb *req = wait->private;
4225 struct io_poll_iocb *poll = &req->apoll->poll;
4226
4227 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4228 key_to_poll(key));
4229
4230 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4231}
4232
4233static void io_poll_req_insert(struct io_kiocb *req)
4234{
4235 struct io_ring_ctx *ctx = req->ctx;
4236 struct hlist_head *list;
4237
4238 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
4239 hlist_add_head(&req->hash_node, list);
4240}
4241
4242static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
4243 struct io_poll_iocb *poll,
4244 struct io_poll_table *ipt, __poll_t mask,
4245 wait_queue_func_t wake_func)
4246 __acquires(&ctx->completion_lock)
4247{
4248 struct io_ring_ctx *ctx = req->ctx;
4249 bool cancel = false;
4250
4251 poll->file = req->file;
4252 poll->head = NULL;
4253 poll->done = poll->canceled = false;
4254 poll->events = mask;
4255
4256 ipt->pt._key = mask;
4257 ipt->req = req;
4258 ipt->error = -EINVAL;
4259
4260 INIT_LIST_HEAD(&poll->wait.entry);
4261 init_waitqueue_func_entry(&poll->wait, wake_func);
4262 poll->wait.private = req;
4263
4264 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
4265
4266 spin_lock_irq(&ctx->completion_lock);
4267 if (likely(poll->head)) {
4268 spin_lock(&poll->head->lock);
4269 if (unlikely(list_empty(&poll->wait.entry))) {
4270 if (ipt->error)
4271 cancel = true;
4272 ipt->error = 0;
4273 mask = 0;
4274 }
4275 if (mask || ipt->error)
4276 list_del_init(&poll->wait.entry);
4277 else if (cancel)
4278 WRITE_ONCE(poll->canceled, true);
4279 else if (!poll->done) /* actually waiting for an event */
4280 io_poll_req_insert(req);
4281 spin_unlock(&poll->head->lock);
4282 }
4283
4284 return mask;
4285}
4286
4287static bool io_arm_poll_handler(struct io_kiocb *req)
4288{
4289 const struct io_op_def *def = &io_op_defs[req->opcode];
4290 struct io_ring_ctx *ctx = req->ctx;
4291 struct async_poll *apoll;
4292 struct io_poll_table ipt;
4293 __poll_t mask, ret;
4294
4295 if (!req->file || !file_can_poll(req->file))
4296 return false;
4297 if (req->flags & (REQ_F_MUST_PUNT | REQ_F_POLLED))
4298 return false;
4299 if (!def->pollin && !def->pollout)
4300 return false;
4301
4302 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
4303 if (unlikely(!apoll))
4304 return false;
4305
4306 req->flags |= REQ_F_POLLED;
4307 memcpy(&apoll->work, &req->work, sizeof(req->work));
4308
Jens Axboe3537b6a2020-04-03 11:19:06 -06004309 get_task_struct(current);
Jens Axboed7718a92020-02-14 22:23:12 -07004310 req->task = current;
4311 req->apoll = apoll;
4312 INIT_HLIST_NODE(&req->hash_node);
4313
Nathan Chancellor8755d972020-03-02 16:01:19 -07004314 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07004315 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07004316 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07004317 if (def->pollout)
4318 mask |= POLLOUT | POLLWRNORM;
4319 mask |= POLLERR | POLLPRI;
4320
4321 ipt.pt._qproc = io_async_queue_proc;
4322
4323 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
4324 io_async_wake);
4325 if (ret) {
4326 ipt.error = 0;
4327 apoll->poll.done = true;
4328 spin_unlock_irq(&ctx->completion_lock);
4329 memcpy(&req->work, &apoll->work, sizeof(req->work));
4330 kfree(apoll);
4331 return false;
4332 }
4333 spin_unlock_irq(&ctx->completion_lock);
4334 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
4335 apoll->poll.events);
4336 return true;
4337}
4338
4339static bool __io_poll_remove_one(struct io_kiocb *req,
4340 struct io_poll_iocb *poll)
4341{
Jens Axboeb41e9852020-02-17 09:52:41 -07004342 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004343
4344 spin_lock(&poll->head->lock);
4345 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07004346 if (!list_empty(&poll->wait.entry)) {
4347 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07004348 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004349 }
4350 spin_unlock(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07004351 return do_complete;
4352}
4353
4354static bool io_poll_remove_one(struct io_kiocb *req)
4355{
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08004356 struct async_poll *apoll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07004357 bool do_complete;
4358
4359 if (req->opcode == IORING_OP_POLL_ADD) {
4360 do_complete = __io_poll_remove_one(req, &req->poll);
4361 } else {
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08004362 apoll = req->apoll;
Jens Axboed7718a92020-02-14 22:23:12 -07004363 /* non-poll requests have submit ref still */
4364 do_complete = __io_poll_remove_one(req, &req->apoll->poll);
4365 if (do_complete)
4366 io_put_req(req);
4367 }
4368
Jens Axboe78076bb2019-12-04 19:56:40 -07004369 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07004370
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004371 if (do_complete && apoll) {
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08004372 /*
4373 * restore ->work because we need to call io_req_work_drop_env.
4374 */
4375 memcpy(&req->work, &apoll->work, sizeof(req->work));
4376 kfree(apoll);
4377 }
4378
Jens Axboeb41e9852020-02-17 09:52:41 -07004379 if (do_complete) {
4380 io_cqring_fill_event(req, -ECANCELED);
4381 io_commit_cqring(req->ctx);
4382 req->flags |= REQ_F_COMP_LOCKED;
4383 io_put_req(req);
4384 }
4385
4386 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004387}
4388
4389static void io_poll_remove_all(struct io_ring_ctx *ctx)
4390{
Jens Axboe78076bb2019-12-04 19:56:40 -07004391 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004392 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004393 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004394
4395 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07004396 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
4397 struct hlist_head *list;
4398
4399 list = &ctx->cancel_hash[i];
4400 hlist_for_each_entry_safe(req, tmp, list, hash_node)
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004401 posted += io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004402 }
4403 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07004404
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004405 if (posted)
4406 io_cqring_ev_posted(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004407}
4408
Jens Axboe47f46762019-11-09 17:43:02 -07004409static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
4410{
Jens Axboe78076bb2019-12-04 19:56:40 -07004411 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07004412 struct io_kiocb *req;
4413
Jens Axboe78076bb2019-12-04 19:56:40 -07004414 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
4415 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07004416 if (sqe_addr != req->user_data)
4417 continue;
4418 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07004419 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07004420 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07004421 }
4422
4423 return -ENOENT;
4424}
4425
Jens Axboe3529d8c2019-12-19 18:24:38 -07004426static int io_poll_remove_prep(struct io_kiocb *req,
4427 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004428{
Jens Axboe221c5eb2019-01-17 09:41:58 -07004429 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4430 return -EINVAL;
4431 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
4432 sqe->poll_events)
4433 return -EINVAL;
4434
Jens Axboe0969e782019-12-17 18:40:57 -07004435 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07004436 return 0;
4437}
4438
4439/*
4440 * Find a running poll command that matches one specified in sqe->addr,
4441 * and remove it if found.
4442 */
4443static int io_poll_remove(struct io_kiocb *req)
4444{
4445 struct io_ring_ctx *ctx = req->ctx;
4446 u64 addr;
4447 int ret;
4448
Jens Axboe0969e782019-12-17 18:40:57 -07004449 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004450 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07004451 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004452 spin_unlock_irq(&ctx->completion_lock);
4453
Jens Axboe78e19bb2019-11-06 15:21:34 -07004454 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004455 if (ret < 0)
4456 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004457 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004458 return 0;
4459}
4460
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004461static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004462{
Jackie Liua197f662019-11-08 08:09:12 -07004463 struct io_ring_ctx *ctx = req->ctx;
4464
Jens Axboe8c838782019-03-12 15:48:16 -06004465 req->poll.done = true;
Pavel Begunkovb0a20342020-02-28 10:36:35 +03004466 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06004467 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004468}
4469
Jens Axboeb41e9852020-02-17 09:52:41 -07004470static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004471{
Jens Axboe221c5eb2019-01-17 09:41:58 -07004472 struct io_ring_ctx *ctx = req->ctx;
Jens Axboea6ba6322020-04-03 11:10:14 -06004473 struct io_poll_iocb *poll = &req->poll;
4474
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004475 if (io_poll_rewait(req, poll)) {
Jens Axboea6ba6322020-04-03 11:10:14 -06004476 spin_unlock_irq(&ctx->completion_lock);
4477 return;
4478 }
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004479
Jens Axboe78076bb2019-12-04 19:56:40 -07004480 hash_del(&req->hash_node);
Jens Axboeb41e9852020-02-17 09:52:41 -07004481 io_poll_complete(req, req->result, 0);
4482 req->flags |= REQ_F_COMP_LOCKED;
4483 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004484 spin_unlock_irq(&ctx->completion_lock);
4485
Jens Axboe8c838782019-03-12 15:48:16 -06004486 io_cqring_ev_posted(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004487}
4488
Jens Axboeb41e9852020-02-17 09:52:41 -07004489static void io_poll_task_func(struct callback_head *cb)
Jens Axboee94f1412019-12-19 12:06:02 -07004490{
Jens Axboeb41e9852020-02-17 09:52:41 -07004491 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4492 struct io_kiocb *nxt = NULL;
Jens Axboee94f1412019-12-19 12:06:02 -07004493
Jens Axboeb41e9852020-02-17 09:52:41 -07004494 io_poll_task_handler(req, &nxt);
Jens Axboed7718a92020-02-14 22:23:12 -07004495 if (nxt) {
4496 struct io_ring_ctx *ctx = nxt->ctx;
Jens Axboee94f1412019-12-19 12:06:02 -07004497
Jens Axboed7718a92020-02-14 22:23:12 -07004498 mutex_lock(&ctx->uring_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07004499 __io_queue_sqe(nxt, NULL);
Jens Axboed7718a92020-02-14 22:23:12 -07004500 mutex_unlock(&ctx->uring_lock);
Jens Axboee94f1412019-12-19 12:06:02 -07004501 }
Jens Axboef0b493e2020-02-01 21:30:11 -07004502}
4503
Jens Axboe221c5eb2019-01-17 09:41:58 -07004504static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4505 void *key)
4506{
Jens Axboec2f2eb72020-02-10 09:07:05 -07004507 struct io_kiocb *req = wait->private;
4508 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004509
Jens Axboed7718a92020-02-14 22:23:12 -07004510 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004511}
4512
Jens Axboe221c5eb2019-01-17 09:41:58 -07004513static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
4514 struct poll_table_struct *p)
4515{
4516 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4517
Jens Axboed7718a92020-02-14 22:23:12 -07004518 __io_queue_proc(&pt->req->poll, pt, head);
Jens Axboeeac406c2019-11-14 12:09:58 -07004519}
4520
Jens Axboe3529d8c2019-12-19 18:24:38 -07004521static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004522{
4523 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004524 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004525
4526 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4527 return -EINVAL;
4528 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
4529 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06004530 if (!poll->file)
4531 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004532
Jens Axboe221c5eb2019-01-17 09:41:58 -07004533 events = READ_ONCE(sqe->poll_events);
4534 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboeb41e9852020-02-17 09:52:41 -07004535
Jens Axboe3537b6a2020-04-03 11:19:06 -06004536 get_task_struct(current);
Jens Axboeb41e9852020-02-17 09:52:41 -07004537 req->task = current;
Jens Axboe0969e782019-12-17 18:40:57 -07004538 return 0;
4539}
4540
Pavel Begunkov014db002020-03-03 21:33:12 +03004541static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07004542{
4543 struct io_poll_iocb *poll = &req->poll;
4544 struct io_ring_ctx *ctx = req->ctx;
4545 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07004546 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07004547
Jens Axboe78076bb2019-12-04 19:56:40 -07004548 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe36703242019-07-25 10:20:18 -06004549 INIT_LIST_HEAD(&req->list);
Jens Axboed7718a92020-02-14 22:23:12 -07004550 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06004551
Jens Axboed7718a92020-02-14 22:23:12 -07004552 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
4553 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004554
Jens Axboe8c838782019-03-12 15:48:16 -06004555 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06004556 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004557 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06004558 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07004559 spin_unlock_irq(&ctx->completion_lock);
4560
Jens Axboe8c838782019-03-12 15:48:16 -06004561 if (mask) {
4562 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03004563 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004564 }
Jens Axboe8c838782019-03-12 15:48:16 -06004565 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004566}
4567
Jens Axboe5262f562019-09-17 12:26:57 -06004568static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
4569{
Jens Axboead8a48a2019-11-15 08:49:11 -07004570 struct io_timeout_data *data = container_of(timer,
4571 struct io_timeout_data, timer);
4572 struct io_kiocb *req = data->req;
4573 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06004574 unsigned long flags;
4575
Jens Axboe5262f562019-09-17 12:26:57 -06004576 atomic_inc(&ctx->cq_timeouts);
4577
4578 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08004579 /*
Jens Axboe11365042019-10-16 09:08:32 -06004580 * We could be racing with timeout deletion. If the list is empty,
4581 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08004582 */
Jens Axboe842f9612019-10-29 12:34:10 -06004583 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06004584 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06004585
Jens Axboe11365042019-10-16 09:08:32 -06004586 /*
4587 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08004588 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06004589 * pointer will be increased, otherwise other timeout reqs may
4590 * return in advance without waiting for enough wait_nr.
4591 */
4592 prev = req;
4593 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
4594 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06004595 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06004596 }
Jens Axboe842f9612019-10-29 12:34:10 -06004597
Jens Axboe78e19bb2019-11-06 15:21:34 -07004598 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06004599 io_commit_cqring(ctx);
4600 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4601
4602 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004603 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004604 io_put_req(req);
4605 return HRTIMER_NORESTART;
4606}
4607
Jens Axboe47f46762019-11-09 17:43:02 -07004608static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
4609{
4610 struct io_kiocb *req;
4611 int ret = -ENOENT;
4612
4613 list_for_each_entry(req, &ctx->timeout_list, list) {
4614 if (user_data == req->user_data) {
4615 list_del_init(&req->list);
4616 ret = 0;
4617 break;
4618 }
4619 }
4620
4621 if (ret == -ENOENT)
4622 return ret;
4623
Jens Axboe2d283902019-12-04 11:08:05 -07004624 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07004625 if (ret == -1)
4626 return -EALREADY;
4627
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004628 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004629 io_cqring_fill_event(req, -ECANCELED);
4630 io_put_req(req);
4631 return 0;
4632}
4633
Jens Axboe3529d8c2019-12-19 18:24:38 -07004634static int io_timeout_remove_prep(struct io_kiocb *req,
4635 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07004636{
Jens Axboeb29472e2019-12-17 18:50:29 -07004637 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4638 return -EINVAL;
4639 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
4640 return -EINVAL;
4641
4642 req->timeout.addr = READ_ONCE(sqe->addr);
4643 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
4644 if (req->timeout.flags)
4645 return -EINVAL;
4646
Jens Axboeb29472e2019-12-17 18:50:29 -07004647 return 0;
4648}
4649
Jens Axboe11365042019-10-16 09:08:32 -06004650/*
4651 * Remove or update an existing timeout command
4652 */
Jens Axboefc4df992019-12-10 14:38:45 -07004653static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06004654{
4655 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07004656 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06004657
Jens Axboe11365042019-10-16 09:08:32 -06004658 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07004659 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06004660
Jens Axboe47f46762019-11-09 17:43:02 -07004661 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06004662 io_commit_cqring(ctx);
4663 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004664 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004665 if (ret < 0)
4666 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08004667 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06004668 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06004669}
4670
Jens Axboe3529d8c2019-12-19 18:24:38 -07004671static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07004672 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06004673{
Jens Axboead8a48a2019-11-15 08:49:11 -07004674 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06004675 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06004676
Jens Axboead8a48a2019-11-15 08:49:11 -07004677 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06004678 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07004679 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06004680 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07004681 if (sqe->off && is_timeout_link)
4682 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06004683 flags = READ_ONCE(sqe->timeout_flags);
4684 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06004685 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06004686
Jens Axboe26a61672019-12-20 09:02:01 -07004687 req->timeout.count = READ_ONCE(sqe->off);
4688
Jens Axboe3529d8c2019-12-19 18:24:38 -07004689 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07004690 return -ENOMEM;
4691
4692 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07004693 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07004694 req->flags |= REQ_F_TIMEOUT;
4695
4696 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06004697 return -EFAULT;
4698
Jens Axboe11365042019-10-16 09:08:32 -06004699 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07004700 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06004701 else
Jens Axboead8a48a2019-11-15 08:49:11 -07004702 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06004703
Jens Axboead8a48a2019-11-15 08:49:11 -07004704 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
4705 return 0;
4706}
4707
Jens Axboefc4df992019-12-10 14:38:45 -07004708static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07004709{
Jens Axboead8a48a2019-11-15 08:49:11 -07004710 struct io_ring_ctx *ctx = req->ctx;
4711 struct io_timeout_data *data;
4712 struct list_head *entry;
4713 unsigned span = 0;
Pavel Begunkovb55ce732020-04-15 00:39:49 +03004714 u32 count = req->timeout.count;
Pavel Begunkov22cad152020-04-15 00:39:48 +03004715 u32 seq = req->sequence;
Jens Axboead8a48a2019-11-15 08:49:11 -07004716
Jens Axboe2d283902019-12-04 11:08:05 -07004717 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004718
Jens Axboe5262f562019-09-17 12:26:57 -06004719 /*
4720 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07004721 * timeout event to be satisfied. If it isn't set, then this is
4722 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06004723 */
Jens Axboe93bd25b2019-11-11 23:34:31 -07004724 if (!count) {
4725 req->flags |= REQ_F_TIMEOUT_NOSEQ;
4726 spin_lock_irq(&ctx->completion_lock);
4727 entry = ctx->timeout_list.prev;
4728 goto add;
4729 }
Jens Axboe5262f562019-09-17 12:26:57 -06004730
Pavel Begunkov22cad152020-04-15 00:39:48 +03004731 req->sequence = seq + count;
Jens Axboe5262f562019-09-17 12:26:57 -06004732
4733 /*
4734 * Insertion sort, ensuring the first entry in the list is always
4735 * the one we need first.
4736 */
Jens Axboe5262f562019-09-17 12:26:57 -06004737 spin_lock_irq(&ctx->completion_lock);
4738 list_for_each_prev(entry, &ctx->timeout_list) {
4739 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
Pavel Begunkov22cad152020-04-15 00:39:48 +03004740 unsigned nxt_seq;
yangerkun5da0fb12019-10-15 21:59:29 +08004741 long long tmp, tmp_nxt;
Pavel Begunkovb55ce732020-04-15 00:39:49 +03004742 u32 nxt_offset = nxt->timeout.count;
Jens Axboe5262f562019-09-17 12:26:57 -06004743
Jens Axboe93bd25b2019-11-11 23:34:31 -07004744 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
4745 continue;
4746
yangerkun5da0fb12019-10-15 21:59:29 +08004747 /*
Pavel Begunkov22cad152020-04-15 00:39:48 +03004748 * Since seq + count can overflow, use type long
yangerkun5da0fb12019-10-15 21:59:29 +08004749 * long to store it.
4750 */
Pavel Begunkov22cad152020-04-15 00:39:48 +03004751 tmp = (long long)seq + count;
4752 nxt_seq = nxt->sequence - nxt_offset;
4753 tmp_nxt = (long long)nxt_seq + nxt_offset;
yangerkun5da0fb12019-10-15 21:59:29 +08004754
4755 /*
4756 * cached_sq_head may overflow, and it will never overflow twice
4757 * once there is some timeout req still be valid.
4758 */
Pavel Begunkov22cad152020-04-15 00:39:48 +03004759 if (seq < nxt_seq)
yangerkun8b07a652019-10-17 12:12:35 +08004760 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08004761
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004762 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06004763 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004764
4765 /*
4766 * Sequence of reqs after the insert one and itself should
4767 * be adjusted because each timeout req consumes a slot.
4768 */
4769 span++;
4770 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06004771 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004772 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004773add:
Jens Axboe5262f562019-09-17 12:26:57 -06004774 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07004775 data->timer.function = io_timeout_fn;
4776 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06004777 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004778 return 0;
4779}
4780
Jens Axboe62755e32019-10-28 21:49:21 -06004781static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06004782{
Jens Axboe62755e32019-10-28 21:49:21 -06004783 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06004784
Jens Axboe62755e32019-10-28 21:49:21 -06004785 return req->user_data == (unsigned long) data;
4786}
4787
Jens Axboee977d6d2019-11-05 12:39:45 -07004788static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06004789{
Jens Axboe62755e32019-10-28 21:49:21 -06004790 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06004791 int ret = 0;
4792
Jens Axboe62755e32019-10-28 21:49:21 -06004793 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
4794 switch (cancel_ret) {
4795 case IO_WQ_CANCEL_OK:
4796 ret = 0;
4797 break;
4798 case IO_WQ_CANCEL_RUNNING:
4799 ret = -EALREADY;
4800 break;
4801 case IO_WQ_CANCEL_NOTFOUND:
4802 ret = -ENOENT;
4803 break;
4804 }
4805
Jens Axboee977d6d2019-11-05 12:39:45 -07004806 return ret;
4807}
4808
Jens Axboe47f46762019-11-09 17:43:02 -07004809static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
4810 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03004811 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07004812{
4813 unsigned long flags;
4814 int ret;
4815
4816 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
4817 if (ret != -ENOENT) {
4818 spin_lock_irqsave(&ctx->completion_lock, flags);
4819 goto done;
4820 }
4821
4822 spin_lock_irqsave(&ctx->completion_lock, flags);
4823 ret = io_timeout_cancel(ctx, sqe_addr);
4824 if (ret != -ENOENT)
4825 goto done;
4826 ret = io_poll_cancel(ctx, sqe_addr);
4827done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004828 if (!ret)
4829 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07004830 io_cqring_fill_event(req, ret);
4831 io_commit_cqring(ctx);
4832 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4833 io_cqring_ev_posted(ctx);
4834
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004835 if (ret < 0)
4836 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03004837 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004838}
4839
Jens Axboe3529d8c2019-12-19 18:24:38 -07004840static int io_async_cancel_prep(struct io_kiocb *req,
4841 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07004842{
Jens Axboefbf23842019-12-17 18:45:56 -07004843 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07004844 return -EINVAL;
4845 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
4846 sqe->cancel_flags)
4847 return -EINVAL;
4848
Jens Axboefbf23842019-12-17 18:45:56 -07004849 req->cancel.addr = READ_ONCE(sqe->addr);
4850 return 0;
4851}
4852
Pavel Begunkov014db002020-03-03 21:33:12 +03004853static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07004854{
4855 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07004856
Pavel Begunkov014db002020-03-03 21:33:12 +03004857 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06004858 return 0;
4859}
4860
Jens Axboe05f3fb32019-12-09 11:22:50 -07004861static int io_files_update_prep(struct io_kiocb *req,
4862 const struct io_uring_sqe *sqe)
4863{
4864 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
4865 return -EINVAL;
4866
4867 req->files_update.offset = READ_ONCE(sqe->off);
4868 req->files_update.nr_args = READ_ONCE(sqe->len);
4869 if (!req->files_update.nr_args)
4870 return -EINVAL;
4871 req->files_update.arg = READ_ONCE(sqe->addr);
4872 return 0;
4873}
4874
4875static int io_files_update(struct io_kiocb *req, bool force_nonblock)
4876{
4877 struct io_ring_ctx *ctx = req->ctx;
4878 struct io_uring_files_update up;
4879 int ret;
4880
Jens Axboef86cd202020-01-29 13:46:44 -07004881 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004882 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004883
4884 up.offset = req->files_update.offset;
4885 up.fds = req->files_update.arg;
4886
4887 mutex_lock(&ctx->uring_lock);
4888 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
4889 mutex_unlock(&ctx->uring_lock);
4890
4891 if (ret < 0)
4892 req_set_fail_links(req);
4893 io_cqring_add_event(req, ret);
4894 io_put_req(req);
4895 return 0;
4896}
4897
Jens Axboe3529d8c2019-12-19 18:24:38 -07004898static int io_req_defer_prep(struct io_kiocb *req,
4899 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07004900{
Jens Axboee7815732019-12-17 19:45:06 -07004901 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07004902
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03004903 if (!sqe)
4904 return 0;
4905
Jens Axboef86cd202020-01-29 13:46:44 -07004906 if (io_op_defs[req->opcode].file_table) {
4907 ret = io_grab_files(req);
4908 if (unlikely(ret))
4909 return ret;
4910 }
4911
Jens Axboecccf0ee2020-01-27 16:34:48 -07004912 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
4913
Jens Axboed625c6e2019-12-17 19:53:05 -07004914 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07004915 case IORING_OP_NOP:
4916 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004917 case IORING_OP_READV:
4918 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004919 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004920 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004921 break;
4922 case IORING_OP_WRITEV:
4923 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004924 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004925 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004926 break;
Jens Axboe0969e782019-12-17 18:40:57 -07004927 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004928 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004929 break;
4930 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004931 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004932 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004933 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004934 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004935 break;
4936 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004937 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004938 break;
Jens Axboe03b12302019-12-02 18:50:25 -07004939 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004940 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004941 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004942 break;
4943 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004944 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004945 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004946 break;
Jens Axboef499a022019-12-02 16:28:46 -07004947 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004948 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004949 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004950 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004951 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004952 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004953 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004954 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004955 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004956 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004957 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004958 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004959 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004960 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004961 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004962 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004963 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004964 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004965 case IORING_OP_FALLOCATE:
4966 ret = io_fallocate_prep(req, sqe);
4967 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004968 case IORING_OP_OPENAT:
4969 ret = io_openat_prep(req, sqe);
4970 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004971 case IORING_OP_CLOSE:
4972 ret = io_close_prep(req, sqe);
4973 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004974 case IORING_OP_FILES_UPDATE:
4975 ret = io_files_update_prep(req, sqe);
4976 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004977 case IORING_OP_STATX:
4978 ret = io_statx_prep(req, sqe);
4979 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004980 case IORING_OP_FADVISE:
4981 ret = io_fadvise_prep(req, sqe);
4982 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004983 case IORING_OP_MADVISE:
4984 ret = io_madvise_prep(req, sqe);
4985 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004986 case IORING_OP_OPENAT2:
4987 ret = io_openat2_prep(req, sqe);
4988 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004989 case IORING_OP_EPOLL_CTL:
4990 ret = io_epoll_ctl_prep(req, sqe);
4991 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004992 case IORING_OP_SPLICE:
4993 ret = io_splice_prep(req, sqe);
4994 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004995 case IORING_OP_PROVIDE_BUFFERS:
4996 ret = io_provide_buffers_prep(req, sqe);
4997 break;
Jens Axboe067524e2020-03-02 16:32:28 -07004998 case IORING_OP_REMOVE_BUFFERS:
4999 ret = io_remove_buffers_prep(req, sqe);
5000 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005001 default:
Jens Axboee7815732019-12-17 19:45:06 -07005002 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5003 req->opcode);
5004 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005005 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005006 }
5007
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005008 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07005009}
5010
Jens Axboe3529d8c2019-12-19 18:24:38 -07005011static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06005012{
Jackie Liua197f662019-11-08 08:09:12 -07005013 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07005014 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06005015
Bob Liu9d858b22019-11-13 18:06:25 +08005016 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov4ee36312020-05-01 17:09:37 +03005017 if (!req_need_defer(req) && list_empty_careful(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06005018 return 0;
5019
Jens Axboe3529d8c2019-12-19 18:24:38 -07005020 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06005021 return -EAGAIN;
5022
Jens Axboe3529d8c2019-12-19 18:24:38 -07005023 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005024 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07005025 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07005026
Jens Axboede0617e2019-04-06 21:51:27 -06005027 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08005028 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06005029 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06005030 return 0;
5031 }
5032
Jens Axboe915967f2019-11-21 09:01:20 -07005033 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06005034 list_add_tail(&req->list, &ctx->defer_list);
5035 spin_unlock_irq(&ctx->completion_lock);
5036 return -EIOCBQUEUED;
5037}
5038
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005039static void io_cleanup_req(struct io_kiocb *req)
5040{
5041 struct io_async_ctx *io = req->io;
5042
5043 switch (req->opcode) {
5044 case IORING_OP_READV:
5045 case IORING_OP_READ_FIXED:
5046 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005047 if (req->flags & REQ_F_BUFFER_SELECTED)
5048 kfree((void *)(unsigned long)req->rw.addr);
5049 /* fallthrough */
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005050 case IORING_OP_WRITEV:
5051 case IORING_OP_WRITE_FIXED:
5052 case IORING_OP_WRITE:
5053 if (io->rw.iov != io->rw.fast_iov)
5054 kfree(io->rw.iov);
5055 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005056 case IORING_OP_RECVMSG:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005057 if (req->flags & REQ_F_BUFFER_SELECTED)
5058 kfree(req->sr_msg.kbuf);
5059 /* fallthrough */
5060 case IORING_OP_SENDMSG:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005061 if (io->msg.iov != io->msg.fast_iov)
5062 kfree(io->msg.iov);
5063 break;
Jens Axboebcda7ba2020-02-23 16:42:51 -07005064 case IORING_OP_RECV:
5065 if (req->flags & REQ_F_BUFFER_SELECTED)
5066 kfree(req->sr_msg.kbuf);
5067 break;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03005068 case IORING_OP_OPENAT:
5069 case IORING_OP_OPENAT2:
5070 case IORING_OP_STATX:
5071 putname(req->open.filename);
5072 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005073 case IORING_OP_SPLICE:
5074 io_put_file(req, req->splice.file_in,
5075 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5076 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005077 }
5078
5079 req->flags &= ~REQ_F_NEED_CLEANUP;
5080}
5081
Jens Axboe3529d8c2019-12-19 18:24:38 -07005082static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Pavel Begunkov014db002020-03-03 21:33:12 +03005083 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005084{
Jackie Liua197f662019-11-08 08:09:12 -07005085 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005086 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005087
Jens Axboed625c6e2019-12-17 19:53:05 -07005088 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005089 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005090 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005091 break;
5092 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005093 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005094 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005095 if (sqe) {
5096 ret = io_read_prep(req, sqe, force_nonblock);
5097 if (ret < 0)
5098 break;
5099 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005100 ret = io_read(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005101 break;
5102 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07005103 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005104 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005105 if (sqe) {
5106 ret = io_write_prep(req, sqe, force_nonblock);
5107 if (ret < 0)
5108 break;
5109 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005110 ret = io_write(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005111 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005112 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005113 if (sqe) {
5114 ret = io_prep_fsync(req, sqe);
5115 if (ret < 0)
5116 break;
5117 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005118 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005119 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005120 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005121 if (sqe) {
5122 ret = io_poll_add_prep(req, sqe);
5123 if (ret)
5124 break;
5125 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005126 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005127 break;
5128 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005129 if (sqe) {
5130 ret = io_poll_remove_prep(req, sqe);
5131 if (ret < 0)
5132 break;
5133 }
Jens Axboefc4df992019-12-10 14:38:45 -07005134 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005135 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005136 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005137 if (sqe) {
5138 ret = io_prep_sfr(req, sqe);
5139 if (ret < 0)
5140 break;
5141 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005142 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005143 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005144 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005145 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005146 if (sqe) {
5147 ret = io_sendmsg_prep(req, sqe);
5148 if (ret < 0)
5149 break;
5150 }
Jens Axboefddafac2020-01-04 20:19:44 -07005151 if (req->opcode == IORING_OP_SENDMSG)
Pavel Begunkov014db002020-03-03 21:33:12 +03005152 ret = io_sendmsg(req, force_nonblock);
Jens Axboefddafac2020-01-04 20:19:44 -07005153 else
Pavel Begunkov014db002020-03-03 21:33:12 +03005154 ret = io_send(req, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005155 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06005156 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005157 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005158 if (sqe) {
5159 ret = io_recvmsg_prep(req, sqe);
5160 if (ret)
5161 break;
5162 }
Jens Axboefddafac2020-01-04 20:19:44 -07005163 if (req->opcode == IORING_OP_RECVMSG)
Pavel Begunkov014db002020-03-03 21:33:12 +03005164 ret = io_recvmsg(req, force_nonblock);
Jens Axboefddafac2020-01-04 20:19:44 -07005165 else
Pavel Begunkov014db002020-03-03 21:33:12 +03005166 ret = io_recv(req, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06005167 break;
Jens Axboe5262f562019-09-17 12:26:57 -06005168 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005169 if (sqe) {
5170 ret = io_timeout_prep(req, sqe, false);
5171 if (ret)
5172 break;
5173 }
Jens Axboefc4df992019-12-10 14:38:45 -07005174 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005175 break;
Jens Axboe11365042019-10-16 09:08:32 -06005176 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005177 if (sqe) {
5178 ret = io_timeout_remove_prep(req, sqe);
5179 if (ret)
5180 break;
5181 }
Jens Axboefc4df992019-12-10 14:38:45 -07005182 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06005183 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06005184 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005185 if (sqe) {
5186 ret = io_accept_prep(req, sqe);
5187 if (ret)
5188 break;
5189 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005190 ret = io_accept(req, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005191 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005192 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005193 if (sqe) {
5194 ret = io_connect_prep(req, sqe);
5195 if (ret)
5196 break;
5197 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005198 ret = io_connect(req, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005199 break;
Jens Axboe62755e32019-10-28 21:49:21 -06005200 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005201 if (sqe) {
5202 ret = io_async_cancel_prep(req, sqe);
5203 if (ret)
5204 break;
5205 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005206 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005207 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005208 case IORING_OP_FALLOCATE:
5209 if (sqe) {
5210 ret = io_fallocate_prep(req, sqe);
5211 if (ret)
5212 break;
5213 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005214 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005215 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005216 case IORING_OP_OPENAT:
5217 if (sqe) {
5218 ret = io_openat_prep(req, sqe);
5219 if (ret)
5220 break;
5221 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005222 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005223 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005224 case IORING_OP_CLOSE:
5225 if (sqe) {
5226 ret = io_close_prep(req, sqe);
5227 if (ret)
5228 break;
5229 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005230 ret = io_close(req, force_nonblock);
Jens Axboeb5dba592019-12-11 14:02:38 -07005231 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005232 case IORING_OP_FILES_UPDATE:
5233 if (sqe) {
5234 ret = io_files_update_prep(req, sqe);
5235 if (ret)
5236 break;
5237 }
5238 ret = io_files_update(req, force_nonblock);
5239 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005240 case IORING_OP_STATX:
5241 if (sqe) {
5242 ret = io_statx_prep(req, sqe);
5243 if (ret)
5244 break;
5245 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005246 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005247 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005248 case IORING_OP_FADVISE:
5249 if (sqe) {
5250 ret = io_fadvise_prep(req, sqe);
5251 if (ret)
5252 break;
5253 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005254 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005255 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005256 case IORING_OP_MADVISE:
5257 if (sqe) {
5258 ret = io_madvise_prep(req, sqe);
5259 if (ret)
5260 break;
5261 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005262 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005263 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005264 case IORING_OP_OPENAT2:
5265 if (sqe) {
5266 ret = io_openat2_prep(req, sqe);
5267 if (ret)
5268 break;
5269 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005270 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005271 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005272 case IORING_OP_EPOLL_CTL:
5273 if (sqe) {
5274 ret = io_epoll_ctl_prep(req, sqe);
5275 if (ret)
5276 break;
5277 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005278 ret = io_epoll_ctl(req, force_nonblock);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005279 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005280 case IORING_OP_SPLICE:
5281 if (sqe) {
5282 ret = io_splice_prep(req, sqe);
5283 if (ret < 0)
5284 break;
5285 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005286 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005287 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005288 case IORING_OP_PROVIDE_BUFFERS:
5289 if (sqe) {
5290 ret = io_provide_buffers_prep(req, sqe);
5291 if (ret)
5292 break;
5293 }
5294 ret = io_provide_buffers(req, force_nonblock);
5295 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005296 case IORING_OP_REMOVE_BUFFERS:
5297 if (sqe) {
5298 ret = io_remove_buffers_prep(req, sqe);
5299 if (ret)
5300 break;
5301 }
5302 ret = io_remove_buffers(req, force_nonblock);
Jens Axboe31b51512019-01-18 22:56:34 -07005303 break;
5304 default:
5305 ret = -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07005306 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005307 }
5308
Jens Axboe31b51512019-01-18 22:56:34 -07005309 if (ret)
5310 return ret;
5311
5312 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07005313 const bool in_async = io_wq_current_is_worker();
5314
Jens Axboe9e645e112019-05-10 16:07:28 -06005315 if (req->result == -EAGAIN)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005316 return -EAGAIN;
5317
Jens Axboe11ba8202020-01-15 21:51:17 -07005318 /* workqueue context doesn't hold uring_lock, grab it now */
5319 if (in_async)
5320 mutex_lock(&ctx->uring_lock);
5321
Jens Axboe2b188cc2019-01-07 10:46:33 -07005322 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07005323
5324 if (in_async)
5325 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005326 }
5327
5328 return 0;
5329}
5330
Jens Axboe561fb042019-10-24 07:25:42 -06005331static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005332{
Jens Axboe561fb042019-10-24 07:25:42 -06005333 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005334 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06005335 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005336
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005337 /* if NO_CANCEL is set, we must still run the work */
5338 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
5339 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06005340 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005341 }
Jens Axboe31b51512019-01-18 22:56:34 -07005342
Jens Axboe561fb042019-10-24 07:25:42 -06005343 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06005344 do {
Pavel Begunkov014db002020-03-03 21:33:12 +03005345 ret = io_issue_sqe(req, NULL, false);
Jens Axboe561fb042019-10-24 07:25:42 -06005346 /*
5347 * We can get EAGAIN for polled IO even though we're
5348 * forcing a sync submission from here, since we can't
5349 * wait for request slots on the block side.
5350 */
5351 if (ret != -EAGAIN)
5352 break;
5353 cond_resched();
5354 } while (1);
5355 }
Jens Axboe31b51512019-01-18 22:56:34 -07005356
Jens Axboe561fb042019-10-24 07:25:42 -06005357 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005358 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07005359 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06005360 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07005361 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005362
Pavel Begunkove9fd9392020-03-04 16:14:12 +03005363 io_steal_work(req, workptr);
Jens Axboe31b51512019-01-18 22:56:34 -07005364}
Jens Axboe2b188cc2019-01-07 10:46:33 -07005365
Jens Axboe65e19f52019-10-26 07:20:21 -06005366static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
5367 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06005368{
Jens Axboe65e19f52019-10-26 07:20:21 -06005369 struct fixed_file_table *table;
5370
Jens Axboe05f3fb32019-12-09 11:22:50 -07005371 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
5372 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06005373}
5374
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005375static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
5376 int fd, struct file **out_file, bool fixed)
5377{
5378 struct io_ring_ctx *ctx = req->ctx;
5379 struct file *file;
5380
5381 if (fixed) {
5382 if (unlikely(!ctx->file_data ||
5383 (unsigned) fd >= ctx->nr_user_files))
5384 return -EBADF;
5385 fd = array_index_nospec(fd, ctx->nr_user_files);
5386 file = io_file_from_index(ctx, fd);
5387 if (!file)
5388 return -EBADF;
Xiaoguang Wang05589552020-03-31 14:05:18 +08005389 req->fixed_file_refs = ctx->file_data->cur_refs;
5390 percpu_ref_get(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005391 } else {
5392 trace_io_uring_file_get(ctx, fd);
5393 file = __io_file_get(state, fd);
5394 if (unlikely(!file))
5395 return -EBADF;
5396 }
5397
5398 *out_file = file;
5399 return 0;
5400}
5401
Jens Axboe3529d8c2019-12-19 18:24:38 -07005402static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Jens Axboe63ff8222020-05-07 14:56:15 -06005403 int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06005404{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005405 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06005406
Jens Axboe63ff8222020-05-07 14:56:15 -06005407 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005408 if (unlikely(!fixed && req->needs_fixed_file))
5409 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06005410
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005411 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06005412}
5413
Jackie Liua197f662019-11-08 08:09:12 -07005414static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005415{
Jens Axboefcb323c2019-10-24 12:39:47 -06005416 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07005417 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06005418
Jens Axboe5b0bbee2020-04-27 10:41:22 -06005419 if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE))
Jens Axboef86cd202020-01-29 13:46:44 -07005420 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005421 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07005422 return -EBADF;
5423
Jens Axboefcb323c2019-10-24 12:39:47 -06005424 rcu_read_lock();
5425 spin_lock_irq(&ctx->inflight_lock);
5426 /*
5427 * We use the f_ops->flush() handler to ensure that we can flush
5428 * out work accessing these files if the fd is closed. Check if
5429 * the fd has changed since we started down this path, and disallow
5430 * this operation if it has.
5431 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005432 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06005433 list_add(&req->inflight_entry, &ctx->inflight_list);
5434 req->flags |= REQ_F_INFLIGHT;
5435 req->work.files = current->files;
5436 ret = 0;
5437 }
5438 spin_unlock_irq(&ctx->inflight_lock);
5439 rcu_read_unlock();
5440
5441 return ret;
5442}
5443
Jens Axboe2665abf2019-11-05 12:40:47 -07005444static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
5445{
Jens Axboead8a48a2019-11-15 08:49:11 -07005446 struct io_timeout_data *data = container_of(timer,
5447 struct io_timeout_data, timer);
5448 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07005449 struct io_ring_ctx *ctx = req->ctx;
5450 struct io_kiocb *prev = NULL;
5451 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07005452
5453 spin_lock_irqsave(&ctx->completion_lock, flags);
5454
5455 /*
5456 * We don't expect the list to be empty, that will only happen if we
5457 * race with the completion of the linked work.
5458 */
Pavel Begunkov44932332019-12-05 16:16:35 +03005459 if (!list_empty(&req->link_list)) {
5460 prev = list_entry(req->link_list.prev, struct io_kiocb,
5461 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005462 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03005463 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005464 prev->flags &= ~REQ_F_LINK_TIMEOUT;
5465 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07005466 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005467 }
5468
5469 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5470
5471 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005472 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03005473 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07005474 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07005475 } else {
5476 io_cqring_add_event(req, -ETIME);
5477 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005478 }
Jens Axboe2665abf2019-11-05 12:40:47 -07005479 return HRTIMER_NORESTART;
5480}
5481
Jens Axboead8a48a2019-11-15 08:49:11 -07005482static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005483{
Jens Axboe76a46e02019-11-10 23:34:16 -07005484 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07005485
Jens Axboe76a46e02019-11-10 23:34:16 -07005486 /*
5487 * If the list is now empty, then our linked request finished before
5488 * we got a chance to setup the timer
5489 */
5490 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03005491 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07005492 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07005493
Jens Axboead8a48a2019-11-15 08:49:11 -07005494 data->timer.function = io_link_timeout_fn;
5495 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
5496 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07005497 }
Jens Axboe76a46e02019-11-10 23:34:16 -07005498 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07005499
Jens Axboe2665abf2019-11-05 12:40:47 -07005500 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07005501 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005502}
5503
Jens Axboead8a48a2019-11-15 08:49:11 -07005504static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005505{
5506 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005507
Pavel Begunkovdea3b492020-04-12 02:05:04 +03005508 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07005509 return NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005510 /* for polled retry, if flag is set, we already went through here */
5511 if (req->flags & REQ_F_POLLED)
5512 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005513
Pavel Begunkov44932332019-12-05 16:16:35 +03005514 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
5515 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07005516 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07005517 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005518
Jens Axboe76a46e02019-11-10 23:34:16 -07005519 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07005520 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07005521}
5522
Jens Axboe3529d8c2019-12-19 18:24:38 -07005523static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005524{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005525 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005526 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07005527 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005528 int ret;
5529
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005530again:
5531 linked_timeout = io_prep_linked_timeout(req);
5532
Jens Axboe193155c2020-02-22 23:22:19 -07005533 if (req->work.creds && req->work.creds != current_cred()) {
5534 if (old_creds)
5535 revert_creds(old_creds);
5536 if (old_creds == req->work.creds)
5537 old_creds = NULL; /* restored original creds */
5538 else
5539 old_creds = override_creds(req->work.creds);
5540 }
5541
Pavel Begunkov014db002020-03-03 21:33:12 +03005542 ret = io_issue_sqe(req, sqe, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06005543
5544 /*
5545 * We async punt it if the file wasn't marked NOWAIT, or if the file
5546 * doesn't support non-blocking read/write attempts
5547 */
5548 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
5549 (req->flags & REQ_F_MUST_PUNT))) {
Jens Axboed7718a92020-02-14 22:23:12 -07005550 if (io_arm_poll_handler(req)) {
5551 if (linked_timeout)
5552 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005553 goto exit;
Jens Axboed7718a92020-02-14 22:23:12 -07005554 }
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005555punt:
Jens Axboef86cd202020-01-29 13:46:44 -07005556 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005557 ret = io_grab_files(req);
5558 if (ret)
5559 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005560 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005561
5562 /*
5563 * Queued up for async execution, worker will release
5564 * submit reference when the iocb is actually submitted.
5565 */
5566 io_queue_async_work(req);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005567 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005568 }
Jens Axboee65ef562019-03-12 10:16:44 -06005569
Jens Axboefcb323c2019-10-24 12:39:47 -06005570err:
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005571 nxt = NULL;
Jens Axboee65ef562019-03-12 10:16:44 -06005572 /* drop submission reference */
Jens Axboe2a44f462020-02-25 13:25:41 -07005573 io_put_req_find_next(req, &nxt);
Jens Axboee65ef562019-03-12 10:16:44 -06005574
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005575 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07005576 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005577 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005578 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005579 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005580 }
5581
Jens Axboee65ef562019-03-12 10:16:44 -06005582 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06005583 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07005584 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005585 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06005586 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06005587 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005588 if (nxt) {
5589 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005590
5591 if (req->flags & REQ_F_FORCE_ASYNC)
5592 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005593 goto again;
5594 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005595exit:
Jens Axboe193155c2020-02-22 23:22:19 -07005596 if (old_creds)
5597 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005598}
5599
Jens Axboe3529d8c2019-12-19 18:24:38 -07005600static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005601{
5602 int ret;
5603
Jens Axboe3529d8c2019-12-19 18:24:38 -07005604 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005605 if (ret) {
5606 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03005607fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005608 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005609 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07005610 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005611 }
Pavel Begunkov25508782019-12-30 21:24:47 +03005612 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkov11185912020-01-22 23:09:35 +03005613 ret = io_req_defer_prep(req, sqe);
5614 if (unlikely(ret < 0))
5615 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07005616 /*
5617 * Never try inline submit of IOSQE_ASYNC is set, go straight
5618 * to async execution.
5619 */
5620 req->work.flags |= IO_WQ_WORK_CONCURRENT;
5621 io_queue_async_work(req);
5622 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005623 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07005624 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08005625}
5626
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005627static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005628{
Jens Axboe94ae5e72019-11-14 19:39:52 -07005629 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005630 io_cqring_add_event(req, -ECANCELED);
5631 io_double_put_req(req);
5632 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07005633 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005634}
5635
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005636static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe3529d8c2019-12-19 18:24:38 -07005637 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06005638{
Jackie Liua197f662019-11-08 08:09:12 -07005639 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005640 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06005641
Jens Axboe9e645e112019-05-10 16:07:28 -06005642 /*
5643 * If we already have a head request, queue this one for async
5644 * submittal once the head completes. If we don't have a head but
5645 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
5646 * submitted sync once the chain is complete. If none of those
5647 * conditions are true (normal request), then just queue it.
5648 */
5649 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03005650 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06005651
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03005652 /*
5653 * Taking sequential execution of a link, draining both sides
5654 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
5655 * requests in the link. So, it drains the head and the
5656 * next after the link request. The last one is done via
5657 * drain_next flag to persist the effect across calls.
5658 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005659 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03005660 head->flags |= REQ_F_IO_DRAIN;
5661 ctx->drain_next = 1;
5662 }
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005663 if (io_alloc_async_ctx(req))
5664 return -EAGAIN;
Jens Axboe9e645e112019-05-10 16:07:28 -06005665
Jens Axboe3529d8c2019-12-19 18:24:38 -07005666 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005667 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005668 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03005669 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005670 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07005671 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03005672 trace_io_uring_link(ctx, req, head);
5673 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06005674
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005675 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005676 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005677 io_queue_link_head(head);
5678 *link = NULL;
5679 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005680 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03005681 if (unlikely(ctx->drain_next)) {
5682 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005683 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03005684 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005685 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03005686 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03005687 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03005688
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005689 if (io_alloc_async_ctx(req))
5690 return -EAGAIN;
5691
Pavel Begunkov711be032020-01-17 03:57:59 +03005692 ret = io_req_defer_prep(req, sqe);
5693 if (ret)
5694 req->flags |= REQ_F_FAIL_LINK;
5695 *link = req;
5696 } else {
5697 io_queue_sqe(req, sqe);
5698 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005699 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005700
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005701 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06005702}
5703
Jens Axboe9a56a232019-01-09 09:06:50 -07005704/*
5705 * Batched submission is done, ensure local IO is flushed out.
5706 */
5707static void io_submit_state_end(struct io_submit_state *state)
5708{
5709 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06005710 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07005711 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03005712 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07005713}
5714
5715/*
5716 * Start submission side cache.
5717 */
5718static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08005719 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07005720{
5721 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07005722 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07005723 state->file = NULL;
5724 state->ios_left = max_ios;
5725}
5726
Jens Axboe2b188cc2019-01-07 10:46:33 -07005727static void io_commit_sqring(struct io_ring_ctx *ctx)
5728{
Hristo Venev75b28af2019-08-26 17:23:46 +00005729 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005730
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03005731 /*
5732 * Ensure any loads from the SQEs are done at this point,
5733 * since once we write the new head, the application could
5734 * write new data to them.
5735 */
5736 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005737}
5738
5739/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07005740 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07005741 * that is mapped by userspace. This means that care needs to be taken to
5742 * ensure that reads are stable, as we cannot rely on userspace always
5743 * being a good citizen. If members of the sqe are validated and then later
5744 * used, it's important that those reads are done through READ_ONCE() to
5745 * prevent a re-load down the line.
5746 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03005747static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005748{
Hristo Venev75b28af2019-08-26 17:23:46 +00005749 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005750 unsigned head;
5751
5752 /*
5753 * The cached sq head (or cq tail) serves two purposes:
5754 *
5755 * 1) allows us to batch the cost of updating the user visible
5756 * head updates.
5757 * 2) allows the kernel side to track the head on its own, even
5758 * though the application is the one updating it.
5759 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005760 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03005761 if (likely(head < ctx->sq_entries))
5762 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07005763
5764 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06005765 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005766 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03005767 return NULL;
5768}
5769
5770static inline void io_consume_sqe(struct io_ring_ctx *ctx)
5771{
5772 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005773}
5774
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005775#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
5776 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
5777 IOSQE_BUFFER_SELECT)
5778
5779static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
5780 const struct io_uring_sqe *sqe,
5781 struct io_submit_state *state, bool async)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005782{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005783 unsigned int sqe_flags;
Jens Axboe63ff8222020-05-07 14:56:15 -06005784 int id;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005785
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005786 /*
5787 * All io need record the previous position, if LINK vs DARIN,
5788 * it can be used to mark the position of the first IO in the
5789 * link list.
5790 */
Pavel Begunkov31af27c2020-04-15 00:39:50 +03005791 req->sequence = ctx->cached_sq_head - ctx->cached_sq_dropped;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005792 req->opcode = READ_ONCE(sqe->opcode);
5793 req->user_data = READ_ONCE(sqe->user_data);
5794 req->io = NULL;
5795 req->file = NULL;
5796 req->ctx = ctx;
5797 req->flags = 0;
5798 /* one is dropped after submission, the other at completion */
5799 refcount_set(&req->refs, 2);
5800 req->task = NULL;
5801 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005802 req->needs_fixed_file = async;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005803 INIT_IO_WORK(&req->work, io_wq_submit_work);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005804
5805 if (unlikely(req->opcode >= IORING_OP_LAST))
5806 return -EINVAL;
5807
5808 if (io_op_defs[req->opcode].needs_mm && !current->mm) {
5809 if (unlikely(!mmget_not_zero(ctx->sqo_mm)))
5810 return -EFAULT;
5811 use_mm(ctx->sqo_mm);
5812 }
5813
5814 sqe_flags = READ_ONCE(sqe->flags);
5815 /* enforce forwards compatibility on users */
5816 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
5817 return -EINVAL;
5818
5819 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
5820 !io_op_defs[req->opcode].buffer_select)
5821 return -EOPNOTSUPP;
5822
5823 id = READ_ONCE(sqe->personality);
5824 if (id) {
5825 req->work.creds = idr_find(&ctx->personality_idr, id);
5826 if (unlikely(!req->work.creds))
5827 return -EINVAL;
5828 get_cred(req->work.creds);
5829 }
5830
5831 /* same numerical values with corresponding REQ_F_*, safe to copy */
5832 req->flags |= sqe_flags & (IOSQE_IO_DRAIN | IOSQE_IO_HARDLINK |
5833 IOSQE_ASYNC | IOSQE_FIXED_FILE |
5834 IOSQE_BUFFER_SELECT | IOSQE_IO_LINK);
5835
Jens Axboe63ff8222020-05-07 14:56:15 -06005836 if (!io_op_defs[req->opcode].needs_file)
5837 return 0;
5838
5839 return io_req_set_file(state, req, READ_ONCE(sqe->fd));
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005840}
5841
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005842static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03005843 struct file *ring_file, int ring_fd, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07005844{
5845 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005846 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005847 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005848
Jens Axboec4a2ed72019-11-21 21:01:26 -07005849 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07005850 if (test_bit(0, &ctx->sq_check_overflow)) {
5851 if (!list_empty(&ctx->cq_overflow_list) &&
5852 !io_cqring_overflow_flush(ctx, false))
5853 return -EBUSY;
5854 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005855
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005856 /* make sure SQ entry isn't read before tail */
5857 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03005858
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005859 if (!percpu_ref_tryget_many(&ctx->refs, nr))
5860 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005861
5862 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08005863 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005864 statep = &state;
5865 }
5866
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005867 ctx->ring_fd = ring_fd;
5868 ctx->ring_file = ring_file;
5869
Jens Axboe6c271ce2019-01-10 11:22:30 -07005870 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005871 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03005872 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005873 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005874
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03005875 sqe = io_get_sqe(ctx);
5876 if (unlikely(!sqe)) {
5877 io_consume_sqe(ctx);
5878 break;
5879 }
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005880 req = io_alloc_req(ctx, statep);
Pavel Begunkov196be952019-11-07 01:41:06 +03005881 if (unlikely(!req)) {
5882 if (!submitted)
5883 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005884 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06005885 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005886
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005887 err = io_init_req(ctx, req, sqe, statep, async);
Pavel Begunkov709b3022020-04-08 08:58:43 +03005888 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07005889 /* will complete beyond this point, count as submitted */
5890 submitted++;
5891
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005892 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005893fail_req:
5894 io_cqring_add_event(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07005895 io_double_put_req(req);
5896 break;
5897 }
5898
Jens Axboe354420f2020-01-08 18:55:15 -07005899 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
5900 true, async);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005901 err = io_submit_sqe(req, sqe, statep, &link);
5902 if (err)
5903 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005904 }
5905
Pavel Begunkov9466f432020-01-25 22:34:01 +03005906 if (unlikely(submitted != nr)) {
5907 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
5908
5909 percpu_ref_put_many(&ctx->refs, nr - ref_used);
5910 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005911 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005912 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005913 if (statep)
5914 io_submit_state_end(&state);
5915
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005916 /* Commit SQ ring head once we've consumed and submitted all SQEs */
5917 io_commit_sqring(ctx);
5918
Jens Axboe6c271ce2019-01-10 11:22:30 -07005919 return submitted;
5920}
5921
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03005922static inline void io_sq_thread_drop_mm(struct io_ring_ctx *ctx)
5923{
5924 struct mm_struct *mm = current->mm;
5925
5926 if (mm) {
5927 unuse_mm(mm);
5928 mmput(mm);
5929 }
5930}
5931
Jens Axboe6c271ce2019-01-10 11:22:30 -07005932static int io_sq_thread(void *data)
5933{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005934 struct io_ring_ctx *ctx = data;
Jens Axboe181e4482019-11-25 08:52:30 -07005935 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005936 mm_segment_t old_fs;
5937 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005938 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005939 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005940
Jens Axboe206aefd2019-11-07 18:27:42 -07005941 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08005942
Jens Axboe6c271ce2019-01-10 11:22:30 -07005943 old_fs = get_fs();
5944 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07005945 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005946
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005947 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005948 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005949 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005950
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005951 if (!list_empty(&ctx->poll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005952 unsigned nr_events = 0;
5953
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005954 mutex_lock(&ctx->uring_lock);
5955 if (!list_empty(&ctx->poll_list))
5956 io_iopoll_getevents(ctx, &nr_events, 0);
5957 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07005958 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005959 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005960 }
5961
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005962 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005963
5964 /*
5965 * If submit got -EBUSY, flag us as needing the application
5966 * to enter the kernel to reap and flush events.
5967 */
5968 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005969 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01005970 * Drop cur_mm before scheduling, we can't hold it for
5971 * long periods (or over schedule()). Do this before
5972 * adding ourselves to the waitqueue, as the unuse/drop
5973 * may sleep.
5974 */
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03005975 io_sq_thread_drop_mm(ctx);
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01005976
5977 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07005978 * We're polling. If we're within the defined idle
5979 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07005980 * to sleep. The exception is if we got EBUSY doing
5981 * more IO, we should wait for the application to
5982 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07005983 */
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005984 if (!list_empty(&ctx->poll_list) ||
Jens Axboedf069d82020-02-04 16:48:34 -07005985 (!time_after(jiffies, timeout) && ret != -EBUSY &&
5986 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005987 if (current->task_works)
5988 task_work_run();
Jens Axboe9831a902019-09-19 09:48:55 -06005989 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005990 continue;
5991 }
5992
Jens Axboe6c271ce2019-01-10 11:22:30 -07005993 prepare_to_wait(&ctx->sqo_wait, &wait,
5994 TASK_INTERRUPTIBLE);
5995
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005996 /*
5997 * While doing polled IO, before going to sleep, we need
5998 * to check if there are new reqs added to poll_list, it
5999 * is because reqs may have been punted to io worker and
6000 * will be added to poll_list later, hence check the
6001 * poll_list again.
6002 */
6003 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6004 !list_empty_careful(&ctx->poll_list)) {
6005 finish_wait(&ctx->sqo_wait, &wait);
6006 continue;
6007 }
6008
Jens Axboe6c271ce2019-01-10 11:22:30 -07006009 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00006010 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02006011 /* make sure to read SQ tail after writing flags */
6012 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006013
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006014 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006015 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006016 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006017 finish_wait(&ctx->sqo_wait, &wait);
6018 break;
6019 }
Jens Axboeb41e9852020-02-17 09:52:41 -07006020 if (current->task_works) {
6021 task_work_run();
Hillf Danton10bea962020-04-01 17:19:33 +08006022 finish_wait(&ctx->sqo_wait, &wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07006023 continue;
6024 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006025 if (signal_pending(current))
6026 flush_signals(current);
6027 schedule();
6028 finish_wait(&ctx->sqo_wait, &wait);
6029
Hristo Venev75b28af2019-08-26 17:23:46 +00006030 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006031 continue;
6032 }
6033 finish_wait(&ctx->sqo_wait, &wait);
6034
Hristo Venev75b28af2019-08-26 17:23:46 +00006035 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006036 }
6037
Jens Axboe8a4955f2019-12-09 14:52:35 -07006038 mutex_lock(&ctx->uring_lock);
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03006039 ret = io_submit_sqes(ctx, to_submit, NULL, -1, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07006040 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006041 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006042 }
6043
Jens Axboeb41e9852020-02-17 09:52:41 -07006044 if (current->task_works)
6045 task_work_run();
6046
Jens Axboe6c271ce2019-01-10 11:22:30 -07006047 set_fs(old_fs);
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03006048 io_sq_thread_drop_mm(ctx);
Jens Axboe181e4482019-11-25 08:52:30 -07006049 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006050
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006051 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006052
Jens Axboe6c271ce2019-01-10 11:22:30 -07006053 return 0;
6054}
6055
Jens Axboebda52162019-09-24 13:47:15 -06006056struct io_wait_queue {
6057 struct wait_queue_entry wq;
6058 struct io_ring_ctx *ctx;
6059 unsigned to_wait;
6060 unsigned nr_timeouts;
6061};
6062
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006063static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06006064{
6065 struct io_ring_ctx *ctx = iowq->ctx;
6066
6067 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006068 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006069 * started waiting. For timeouts, we always want to return to userspace,
6070 * regardless of event count.
6071 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006072 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006073 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6074}
6075
6076static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6077 int wake_flags, void *key)
6078{
6079 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6080 wq);
6081
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006082 /* use noflush == true, as we can't safely rely on locking context */
6083 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06006084 return -1;
6085
6086 return autoremove_wake_function(curr, mode, wake_flags, key);
6087}
6088
Jens Axboe2b188cc2019-01-07 10:46:33 -07006089/*
6090 * Wait until events become available, if we don't already have some. The
6091 * application must reap them itself, as they reside on the shared cq ring.
6092 */
6093static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6094 const sigset_t __user *sig, size_t sigsz)
6095{
Jens Axboebda52162019-09-24 13:47:15 -06006096 struct io_wait_queue iowq = {
6097 .wq = {
6098 .private = current,
6099 .func = io_wake_function,
6100 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6101 },
6102 .ctx = ctx,
6103 .to_wait = min_events,
6104 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006105 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006106 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006107
Jens Axboeb41e9852020-02-17 09:52:41 -07006108 do {
6109 if (io_cqring_events(ctx, false) >= min_events)
6110 return 0;
6111 if (!current->task_works)
6112 break;
6113 task_work_run();
6114 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006115
6116 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006117#ifdef CONFIG_COMPAT
6118 if (in_compat_syscall())
6119 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006120 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006121 else
6122#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006123 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006124
Jens Axboe2b188cc2019-01-07 10:46:33 -07006125 if (ret)
6126 return ret;
6127 }
6128
Jens Axboebda52162019-09-24 13:47:15 -06006129 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006130 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006131 do {
6132 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6133 TASK_INTERRUPTIBLE);
Jens Axboeb41e9852020-02-17 09:52:41 -07006134 if (current->task_works)
6135 task_work_run();
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006136 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06006137 break;
6138 schedule();
6139 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006140 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06006141 break;
6142 }
6143 } while (1);
6144 finish_wait(&ctx->wait, &iowq.wq);
6145
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006146 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006147
Hristo Venev75b28af2019-08-26 17:23:46 +00006148 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006149}
6150
Jens Axboe6b063142019-01-10 22:13:58 -07006151static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6152{
6153#if defined(CONFIG_UNIX)
6154 if (ctx->ring_sock) {
6155 struct sock *sock = ctx->ring_sock->sk;
6156 struct sk_buff *skb;
6157
6158 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6159 kfree_skb(skb);
6160 }
6161#else
6162 int i;
6163
Jens Axboe65e19f52019-10-26 07:20:21 -06006164 for (i = 0; i < ctx->nr_user_files; i++) {
6165 struct file *file;
6166
6167 file = io_file_from_index(ctx, i);
6168 if (file)
6169 fput(file);
6170 }
Jens Axboe6b063142019-01-10 22:13:58 -07006171#endif
6172}
6173
Jens Axboe05f3fb32019-12-09 11:22:50 -07006174static void io_file_ref_kill(struct percpu_ref *ref)
6175{
6176 struct fixed_file_data *data;
6177
6178 data = container_of(ref, struct fixed_file_data, refs);
6179 complete(&data->done);
6180}
6181
Jens Axboe6b063142019-01-10 22:13:58 -07006182static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6183{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006184 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006185 struct fixed_file_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006186 unsigned nr_tables, i;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006187 unsigned long flags;
Jens Axboe65e19f52019-10-26 07:20:21 -06006188
Jens Axboe05f3fb32019-12-09 11:22:50 -07006189 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006190 return -ENXIO;
6191
Xiaoguang Wang05589552020-03-31 14:05:18 +08006192 spin_lock_irqsave(&data->lock, flags);
6193 if (!list_empty(&data->ref_list))
6194 ref_node = list_first_entry(&data->ref_list,
6195 struct fixed_file_ref_node, node);
6196 spin_unlock_irqrestore(&data->lock, flags);
6197 if (ref_node)
6198 percpu_ref_kill(&ref_node->refs);
6199
6200 percpu_ref_kill(&data->refs);
6201
6202 /* wait for all refs nodes to complete */
Jens Axboe2faf8522020-02-04 19:54:55 -07006203 wait_for_completion(&data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006204
Jens Axboe6b063142019-01-10 22:13:58 -07006205 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06006206 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6207 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006208 kfree(data->table[i].files);
6209 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006210 percpu_ref_exit(&data->refs);
6211 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006212 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006213 ctx->nr_user_files = 0;
6214 return 0;
6215}
6216
Jens Axboe6c271ce2019-01-10 11:22:30 -07006217static void io_sq_thread_stop(struct io_ring_ctx *ctx)
6218{
6219 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07006220 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006221 /*
6222 * The park is a bit of a work-around, without it we get
6223 * warning spews on shutdown with SQPOLL set and affinity
6224 * set to a single CPU.
6225 */
Jens Axboe06058632019-04-13 09:26:03 -06006226 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006227 kthread_stop(ctx->sqo_thread);
6228 ctx->sqo_thread = NULL;
6229 }
6230}
6231
Jens Axboe6b063142019-01-10 22:13:58 -07006232static void io_finish_async(struct io_ring_ctx *ctx)
6233{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006234 io_sq_thread_stop(ctx);
6235
Jens Axboe561fb042019-10-24 07:25:42 -06006236 if (ctx->io_wq) {
6237 io_wq_destroy(ctx->io_wq);
6238 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006239 }
6240}
6241
6242#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07006243/*
6244 * Ensure the UNIX gc is aware of our file set, so we are certain that
6245 * the io_uring can be safely unregistered on process exit, even if we have
6246 * loops in the file referencing.
6247 */
6248static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
6249{
6250 struct sock *sk = ctx->ring_sock->sk;
6251 struct scm_fp_list *fpl;
6252 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06006253 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07006254
Jens Axboe6b063142019-01-10 22:13:58 -07006255 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
6256 if (!fpl)
6257 return -ENOMEM;
6258
6259 skb = alloc_skb(0, GFP_KERNEL);
6260 if (!skb) {
6261 kfree(fpl);
6262 return -ENOMEM;
6263 }
6264
6265 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07006266
Jens Axboe08a45172019-10-03 08:11:03 -06006267 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07006268 fpl->user = get_uid(ctx->user);
6269 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006270 struct file *file = io_file_from_index(ctx, i + offset);
6271
6272 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06006273 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06006274 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06006275 unix_inflight(fpl->user, fpl->fp[nr_files]);
6276 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07006277 }
6278
Jens Axboe08a45172019-10-03 08:11:03 -06006279 if (nr_files) {
6280 fpl->max = SCM_MAX_FD;
6281 fpl->count = nr_files;
6282 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006283 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06006284 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
6285 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07006286
Jens Axboe08a45172019-10-03 08:11:03 -06006287 for (i = 0; i < nr_files; i++)
6288 fput(fpl->fp[i]);
6289 } else {
6290 kfree_skb(skb);
6291 kfree(fpl);
6292 }
Jens Axboe6b063142019-01-10 22:13:58 -07006293
6294 return 0;
6295}
6296
6297/*
6298 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
6299 * causes regular reference counting to break down. We rely on the UNIX
6300 * garbage collection to take care of this problem for us.
6301 */
6302static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6303{
6304 unsigned left, total;
6305 int ret = 0;
6306
6307 total = 0;
6308 left = ctx->nr_user_files;
6309 while (left) {
6310 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07006311
6312 ret = __io_sqe_files_scm(ctx, this_files, total);
6313 if (ret)
6314 break;
6315 left -= this_files;
6316 total += this_files;
6317 }
6318
6319 if (!ret)
6320 return 0;
6321
6322 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006323 struct file *file = io_file_from_index(ctx, total);
6324
6325 if (file)
6326 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07006327 total++;
6328 }
6329
6330 return ret;
6331}
6332#else
6333static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6334{
6335 return 0;
6336}
6337#endif
6338
Jens Axboe65e19f52019-10-26 07:20:21 -06006339static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
6340 unsigned nr_files)
6341{
6342 int i;
6343
6344 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006345 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006346 unsigned this_files;
6347
6348 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
6349 table->files = kcalloc(this_files, sizeof(struct file *),
6350 GFP_KERNEL);
6351 if (!table->files)
6352 break;
6353 nr_files -= this_files;
6354 }
6355
6356 if (i == nr_tables)
6357 return 0;
6358
6359 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006360 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006361 kfree(table->files);
6362 }
6363 return 1;
6364}
6365
Jens Axboe05f3fb32019-12-09 11:22:50 -07006366static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06006367{
6368#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06006369 struct sock *sock = ctx->ring_sock->sk;
6370 struct sk_buff_head list, *head = &sock->sk_receive_queue;
6371 struct sk_buff *skb;
6372 int i;
6373
6374 __skb_queue_head_init(&list);
6375
6376 /*
6377 * Find the skb that holds this file in its SCM_RIGHTS. When found,
6378 * remove this entry and rearrange the file array.
6379 */
6380 skb = skb_dequeue(head);
6381 while (skb) {
6382 struct scm_fp_list *fp;
6383
6384 fp = UNIXCB(skb).fp;
6385 for (i = 0; i < fp->count; i++) {
6386 int left;
6387
6388 if (fp->fp[i] != file)
6389 continue;
6390
6391 unix_notinflight(fp->user, fp->fp[i]);
6392 left = fp->count - 1 - i;
6393 if (left) {
6394 memmove(&fp->fp[i], &fp->fp[i + 1],
6395 left * sizeof(struct file *));
6396 }
6397 fp->count--;
6398 if (!fp->count) {
6399 kfree_skb(skb);
6400 skb = NULL;
6401 } else {
6402 __skb_queue_tail(&list, skb);
6403 }
6404 fput(file);
6405 file = NULL;
6406 break;
6407 }
6408
6409 if (!file)
6410 break;
6411
6412 __skb_queue_tail(&list, skb);
6413
6414 skb = skb_dequeue(head);
6415 }
6416
6417 if (skb_peek(&list)) {
6418 spin_lock_irq(&head->lock);
6419 while ((skb = __skb_dequeue(&list)) != NULL)
6420 __skb_queue_tail(head, skb);
6421 spin_unlock_irq(&head->lock);
6422 }
6423#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07006424 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06006425#endif
6426}
6427
Jens Axboe05f3fb32019-12-09 11:22:50 -07006428struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08006429 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006430 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006431};
6432
Xiaoguang Wang05589552020-03-31 14:05:18 +08006433static void io_file_put_work(struct work_struct *work)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006434{
Xiaoguang Wang05589552020-03-31 14:05:18 +08006435 struct fixed_file_ref_node *ref_node;
6436 struct fixed_file_data *file_data;
6437 struct io_ring_ctx *ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006438 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006439 unsigned long flags;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006440
Xiaoguang Wang05589552020-03-31 14:05:18 +08006441 ref_node = container_of(work, struct fixed_file_ref_node, work);
6442 file_data = ref_node->file_data;
6443 ctx = file_data->ctx;
6444
6445 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
6446 list_del_init(&pfile->list);
6447 io_ring_file_put(ctx, pfile->file);
6448 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006449 }
6450
Xiaoguang Wang05589552020-03-31 14:05:18 +08006451 spin_lock_irqsave(&file_data->lock, flags);
6452 list_del_init(&ref_node->node);
6453 spin_unlock_irqrestore(&file_data->lock, flags);
Jens Axboe2faf8522020-02-04 19:54:55 -07006454
Xiaoguang Wang05589552020-03-31 14:05:18 +08006455 percpu_ref_exit(&ref_node->refs);
6456 kfree(ref_node);
6457 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006458}
6459
6460static void io_file_data_ref_zero(struct percpu_ref *ref)
6461{
Xiaoguang Wang05589552020-03-31 14:05:18 +08006462 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006463
Xiaoguang Wang05589552020-03-31 14:05:18 +08006464 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006465
Xiaoguang Wang05589552020-03-31 14:05:18 +08006466 queue_work(system_wq, &ref_node->work);
6467}
6468
6469static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
6470 struct io_ring_ctx *ctx)
6471{
6472 struct fixed_file_ref_node *ref_node;
6473
6474 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
6475 if (!ref_node)
6476 return ERR_PTR(-ENOMEM);
6477
6478 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
6479 0, GFP_KERNEL)) {
6480 kfree(ref_node);
6481 return ERR_PTR(-ENOMEM);
6482 }
6483 INIT_LIST_HEAD(&ref_node->node);
6484 INIT_LIST_HEAD(&ref_node->file_list);
6485 INIT_WORK(&ref_node->work, io_file_put_work);
6486 ref_node->file_data = ctx->file_data;
6487 return ref_node;
6488
6489}
6490
6491static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
6492{
6493 percpu_ref_exit(&ref_node->refs);
6494 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006495}
6496
6497static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
6498 unsigned nr_args)
6499{
6500 __s32 __user *fds = (__s32 __user *) arg;
6501 unsigned nr_tables;
6502 struct file *file;
6503 int fd, ret = 0;
6504 unsigned i;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006505 struct fixed_file_ref_node *ref_node;
6506 unsigned long flags;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006507
6508 if (ctx->file_data)
6509 return -EBUSY;
6510 if (!nr_args)
6511 return -EINVAL;
6512 if (nr_args > IORING_MAX_FIXED_FILES)
6513 return -EMFILE;
6514
6515 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
6516 if (!ctx->file_data)
6517 return -ENOMEM;
6518 ctx->file_data->ctx = ctx;
6519 init_completion(&ctx->file_data->done);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006520 INIT_LIST_HEAD(&ctx->file_data->ref_list);
Xiaoguang Wangf7fe9342020-04-07 20:02:31 +08006521 spin_lock_init(&ctx->file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006522
6523 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
6524 ctx->file_data->table = kcalloc(nr_tables,
6525 sizeof(struct fixed_file_table),
6526 GFP_KERNEL);
6527 if (!ctx->file_data->table) {
6528 kfree(ctx->file_data);
6529 ctx->file_data = NULL;
6530 return -ENOMEM;
6531 }
6532
Xiaoguang Wang05589552020-03-31 14:05:18 +08006533 if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill,
Jens Axboe05f3fb32019-12-09 11:22:50 -07006534 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
6535 kfree(ctx->file_data->table);
6536 kfree(ctx->file_data);
6537 ctx->file_data = NULL;
6538 return -ENOMEM;
6539 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006540
6541 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
6542 percpu_ref_exit(&ctx->file_data->refs);
6543 kfree(ctx->file_data->table);
6544 kfree(ctx->file_data);
6545 ctx->file_data = NULL;
6546 return -ENOMEM;
6547 }
6548
6549 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
6550 struct fixed_file_table *table;
6551 unsigned index;
6552
6553 ret = -EFAULT;
6554 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
6555 break;
6556 /* allow sparse sets */
6557 if (fd == -1) {
6558 ret = 0;
6559 continue;
6560 }
6561
6562 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6563 index = i & IORING_FILE_TABLE_MASK;
6564 file = fget(fd);
6565
6566 ret = -EBADF;
6567 if (!file)
6568 break;
6569
6570 /*
6571 * Don't allow io_uring instances to be registered. If UNIX
6572 * isn't enabled, then this causes a reference cycle and this
6573 * instance can never get freed. If UNIX is enabled we'll
6574 * handle it just fine, but there's still no point in allowing
6575 * a ring fd as it doesn't support regular read/write anyway.
6576 */
6577 if (file->f_op == &io_uring_fops) {
6578 fput(file);
6579 break;
6580 }
6581 ret = 0;
6582 table->files[index] = file;
6583 }
6584
6585 if (ret) {
6586 for (i = 0; i < ctx->nr_user_files; i++) {
6587 file = io_file_from_index(ctx, i);
6588 if (file)
6589 fput(file);
6590 }
6591 for (i = 0; i < nr_tables; i++)
6592 kfree(ctx->file_data->table[i].files);
6593
6594 kfree(ctx->file_data->table);
6595 kfree(ctx->file_data);
6596 ctx->file_data = NULL;
6597 ctx->nr_user_files = 0;
6598 return ret;
6599 }
6600
6601 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006602 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006603 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006604 return ret;
6605 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006606
Xiaoguang Wang05589552020-03-31 14:05:18 +08006607 ref_node = alloc_fixed_file_ref_node(ctx);
6608 if (IS_ERR(ref_node)) {
6609 io_sqe_files_unregister(ctx);
6610 return PTR_ERR(ref_node);
6611 }
6612
6613 ctx->file_data->cur_refs = &ref_node->refs;
6614 spin_lock_irqsave(&ctx->file_data->lock, flags);
6615 list_add(&ref_node->node, &ctx->file_data->ref_list);
6616 spin_unlock_irqrestore(&ctx->file_data->lock, flags);
6617 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006618 return ret;
6619}
6620
Jens Axboec3a31e62019-10-03 13:59:56 -06006621static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
6622 int index)
6623{
6624#if defined(CONFIG_UNIX)
6625 struct sock *sock = ctx->ring_sock->sk;
6626 struct sk_buff_head *head = &sock->sk_receive_queue;
6627 struct sk_buff *skb;
6628
6629 /*
6630 * See if we can merge this file into an existing skb SCM_RIGHTS
6631 * file set. If there's no room, fall back to allocating a new skb
6632 * and filling it in.
6633 */
6634 spin_lock_irq(&head->lock);
6635 skb = skb_peek(head);
6636 if (skb) {
6637 struct scm_fp_list *fpl = UNIXCB(skb).fp;
6638
6639 if (fpl->count < SCM_MAX_FD) {
6640 __skb_unlink(skb, head);
6641 spin_unlock_irq(&head->lock);
6642 fpl->fp[fpl->count] = get_file(file);
6643 unix_inflight(fpl->user, fpl->fp[fpl->count]);
6644 fpl->count++;
6645 spin_lock_irq(&head->lock);
6646 __skb_queue_head(head, skb);
6647 } else {
6648 skb = NULL;
6649 }
6650 }
6651 spin_unlock_irq(&head->lock);
6652
6653 if (skb) {
6654 fput(file);
6655 return 0;
6656 }
6657
6658 return __io_sqe_files_scm(ctx, 1, index);
6659#else
6660 return 0;
6661#endif
6662}
6663
Hillf Dantona5318d32020-03-23 17:47:15 +08006664static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08006665 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006666{
Hillf Dantona5318d32020-03-23 17:47:15 +08006667 struct io_file_put *pfile;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006668 struct percpu_ref *refs = data->cur_refs;
6669 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006670
Jens Axboe05f3fb32019-12-09 11:22:50 -07006671 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08006672 if (!pfile)
6673 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006674
Xiaoguang Wang05589552020-03-31 14:05:18 +08006675 ref_node = container_of(refs, struct fixed_file_ref_node, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006676 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006677 list_add(&pfile->list, &ref_node->file_list);
6678
Hillf Dantona5318d32020-03-23 17:47:15 +08006679 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006680}
6681
6682static int __io_sqe_files_update(struct io_ring_ctx *ctx,
6683 struct io_uring_files_update *up,
6684 unsigned nr_args)
6685{
6686 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006687 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006688 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006689 __s32 __user *fds;
6690 int fd, i, err;
6691 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006692 unsigned long flags;
6693 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06006694
Jens Axboe05f3fb32019-12-09 11:22:50 -07006695 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06006696 return -EOVERFLOW;
6697 if (done > ctx->nr_user_files)
6698 return -EINVAL;
6699
Xiaoguang Wang05589552020-03-31 14:05:18 +08006700 ref_node = alloc_fixed_file_ref_node(ctx);
6701 if (IS_ERR(ref_node))
6702 return PTR_ERR(ref_node);
6703
Jens Axboec3a31e62019-10-03 13:59:56 -06006704 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006705 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06006706 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006707 struct fixed_file_table *table;
6708 unsigned index;
6709
Jens Axboec3a31e62019-10-03 13:59:56 -06006710 err = 0;
6711 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
6712 err = -EFAULT;
6713 break;
6714 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006715 i = array_index_nospec(up->offset, ctx->nr_user_files);
6716 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06006717 index = i & IORING_FILE_TABLE_MASK;
6718 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006719 file = io_file_from_index(ctx, index);
Hillf Dantona5318d32020-03-23 17:47:15 +08006720 err = io_queue_file_removal(data, file);
6721 if (err)
6722 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06006723 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006724 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06006725 }
6726 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06006727 file = fget(fd);
6728 if (!file) {
6729 err = -EBADF;
6730 break;
6731 }
6732 /*
6733 * Don't allow io_uring instances to be registered. If
6734 * UNIX isn't enabled, then this causes a reference
6735 * cycle and this instance can never get freed. If UNIX
6736 * is enabled we'll handle it just fine, but there's
6737 * still no point in allowing a ring fd as it doesn't
6738 * support regular read/write anyway.
6739 */
6740 if (file->f_op == &io_uring_fops) {
6741 fput(file);
6742 err = -EBADF;
6743 break;
6744 }
Jens Axboe65e19f52019-10-26 07:20:21 -06006745 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006746 err = io_sqe_file_register(ctx, file, i);
6747 if (err)
6748 break;
6749 }
6750 nr_args--;
6751 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006752 up->offset++;
6753 }
6754
Xiaoguang Wang05589552020-03-31 14:05:18 +08006755 if (needs_switch) {
6756 percpu_ref_kill(data->cur_refs);
6757 spin_lock_irqsave(&data->lock, flags);
6758 list_add(&ref_node->node, &data->ref_list);
6759 data->cur_refs = &ref_node->refs;
6760 spin_unlock_irqrestore(&data->lock, flags);
6761 percpu_ref_get(&ctx->file_data->refs);
6762 } else
6763 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06006764
6765 return done ? done : err;
6766}
Xiaoguang Wang05589552020-03-31 14:05:18 +08006767
Jens Axboe05f3fb32019-12-09 11:22:50 -07006768static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
6769 unsigned nr_args)
6770{
6771 struct io_uring_files_update up;
6772
6773 if (!ctx->file_data)
6774 return -ENXIO;
6775 if (!nr_args)
6776 return -EINVAL;
6777 if (copy_from_user(&up, arg, sizeof(up)))
6778 return -EFAULT;
6779 if (up.resv)
6780 return -EINVAL;
6781
6782 return __io_sqe_files_update(ctx, &up, nr_args);
6783}
Jens Axboec3a31e62019-10-03 13:59:56 -06006784
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006785static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07006786{
6787 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6788
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006789 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07006790 io_put_req(req);
6791}
6792
Pavel Begunkov24369c22020-01-28 03:15:48 +03006793static int io_init_wq_offload(struct io_ring_ctx *ctx,
6794 struct io_uring_params *p)
6795{
6796 struct io_wq_data data;
6797 struct fd f;
6798 struct io_ring_ctx *ctx_attach;
6799 unsigned int concurrency;
6800 int ret = 0;
6801
6802 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006803 data.free_work = io_free_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03006804
6805 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
6806 /* Do QD, or 4 * CPUS, whatever is smallest */
6807 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
6808
6809 ctx->io_wq = io_wq_create(concurrency, &data);
6810 if (IS_ERR(ctx->io_wq)) {
6811 ret = PTR_ERR(ctx->io_wq);
6812 ctx->io_wq = NULL;
6813 }
6814 return ret;
6815 }
6816
6817 f = fdget(p->wq_fd);
6818 if (!f.file)
6819 return -EBADF;
6820
6821 if (f.file->f_op != &io_uring_fops) {
6822 ret = -EINVAL;
6823 goto out_fput;
6824 }
6825
6826 ctx_attach = f.file->private_data;
6827 /* @io_wq is protected by holding the fd */
6828 if (!io_wq_get(ctx_attach->io_wq, &data)) {
6829 ret = -EINVAL;
6830 goto out_fput;
6831 }
6832
6833 ctx->io_wq = ctx_attach->io_wq;
6834out_fput:
6835 fdput(f);
6836 return ret;
6837}
6838
Jens Axboe6c271ce2019-01-10 11:22:30 -07006839static int io_sq_offload_start(struct io_ring_ctx *ctx,
6840 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006841{
6842 int ret;
6843
Jens Axboe6c271ce2019-01-10 11:22:30 -07006844 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006845 mmgrab(current->mm);
6846 ctx->sqo_mm = current->mm;
6847
Jens Axboe6c271ce2019-01-10 11:22:30 -07006848 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06006849 ret = -EPERM;
6850 if (!capable(CAP_SYS_ADMIN))
6851 goto err;
6852
Jens Axboe917257d2019-04-13 09:28:55 -06006853 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
6854 if (!ctx->sq_thread_idle)
6855 ctx->sq_thread_idle = HZ;
6856
Jens Axboe6c271ce2019-01-10 11:22:30 -07006857 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06006858 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006859
Jens Axboe917257d2019-04-13 09:28:55 -06006860 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06006861 if (cpu >= nr_cpu_ids)
6862 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08006863 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06006864 goto err;
6865
Jens Axboe6c271ce2019-01-10 11:22:30 -07006866 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
6867 ctx, cpu,
6868 "io_uring-sq");
6869 } else {
6870 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
6871 "io_uring-sq");
6872 }
6873 if (IS_ERR(ctx->sqo_thread)) {
6874 ret = PTR_ERR(ctx->sqo_thread);
6875 ctx->sqo_thread = NULL;
6876 goto err;
6877 }
6878 wake_up_process(ctx->sqo_thread);
6879 } else if (p->flags & IORING_SETUP_SQ_AFF) {
6880 /* Can't have SQ_AFF without SQPOLL */
6881 ret = -EINVAL;
6882 goto err;
6883 }
6884
Pavel Begunkov24369c22020-01-28 03:15:48 +03006885 ret = io_init_wq_offload(ctx, p);
6886 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006887 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006888
6889 return 0;
6890err:
Jens Axboe54a91f32019-09-10 09:15:04 -06006891 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006892 mmdrop(ctx->sqo_mm);
6893 ctx->sqo_mm = NULL;
6894 return ret;
6895}
6896
6897static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
6898{
6899 atomic_long_sub(nr_pages, &user->locked_vm);
6900}
6901
6902static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
6903{
6904 unsigned long page_limit, cur_pages, new_pages;
6905
6906 /* Don't allow more pages than we can safely lock */
6907 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
6908
6909 do {
6910 cur_pages = atomic_long_read(&user->locked_vm);
6911 new_pages = cur_pages + nr_pages;
6912 if (new_pages > page_limit)
6913 return -ENOMEM;
6914 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
6915 new_pages) != cur_pages);
6916
6917 return 0;
6918}
6919
6920static void io_mem_free(void *ptr)
6921{
Mark Rutland52e04ef2019-04-30 17:30:21 +01006922 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006923
Mark Rutland52e04ef2019-04-30 17:30:21 +01006924 if (!ptr)
6925 return;
6926
6927 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006928 if (put_page_testzero(page))
6929 free_compound_page(page);
6930}
6931
6932static void *io_mem_alloc(size_t size)
6933{
6934 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
6935 __GFP_NORETRY;
6936
6937 return (void *) __get_free_pages(gfp_flags, get_order(size));
6938}
6939
Hristo Venev75b28af2019-08-26 17:23:46 +00006940static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
6941 size_t *sq_offset)
6942{
6943 struct io_rings *rings;
6944 size_t off, sq_array_size;
6945
6946 off = struct_size(rings, cqes, cq_entries);
6947 if (off == SIZE_MAX)
6948 return SIZE_MAX;
6949
6950#ifdef CONFIG_SMP
6951 off = ALIGN(off, SMP_CACHE_BYTES);
6952 if (off == 0)
6953 return SIZE_MAX;
6954#endif
6955
6956 sq_array_size = array_size(sizeof(u32), sq_entries);
6957 if (sq_array_size == SIZE_MAX)
6958 return SIZE_MAX;
6959
6960 if (check_add_overflow(off, sq_array_size, &off))
6961 return SIZE_MAX;
6962
6963 if (sq_offset)
6964 *sq_offset = off;
6965
6966 return off;
6967}
6968
Jens Axboe2b188cc2019-01-07 10:46:33 -07006969static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
6970{
Hristo Venev75b28af2019-08-26 17:23:46 +00006971 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006972
Hristo Venev75b28af2019-08-26 17:23:46 +00006973 pages = (size_t)1 << get_order(
6974 rings_size(sq_entries, cq_entries, NULL));
6975 pages += (size_t)1 << get_order(
6976 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07006977
Hristo Venev75b28af2019-08-26 17:23:46 +00006978 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006979}
6980
Jens Axboeedafcce2019-01-09 09:16:05 -07006981static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
6982{
6983 int i, j;
6984
6985 if (!ctx->user_bufs)
6986 return -ENXIO;
6987
6988 for (i = 0; i < ctx->nr_user_bufs; i++) {
6989 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6990
6991 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006992 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07006993
6994 if (ctx->account_mem)
6995 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006996 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006997 imu->nr_bvecs = 0;
6998 }
6999
7000 kfree(ctx->user_bufs);
7001 ctx->user_bufs = NULL;
7002 ctx->nr_user_bufs = 0;
7003 return 0;
7004}
7005
7006static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
7007 void __user *arg, unsigned index)
7008{
7009 struct iovec __user *src;
7010
7011#ifdef CONFIG_COMPAT
7012 if (ctx->compat) {
7013 struct compat_iovec __user *ciovs;
7014 struct compat_iovec ciov;
7015
7016 ciovs = (struct compat_iovec __user *) arg;
7017 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
7018 return -EFAULT;
7019
Jens Axboed55e5f52019-12-11 16:12:15 -07007020 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07007021 dst->iov_len = ciov.iov_len;
7022 return 0;
7023 }
7024#endif
7025 src = (struct iovec __user *) arg;
7026 if (copy_from_user(dst, &src[index], sizeof(*dst)))
7027 return -EFAULT;
7028 return 0;
7029}
7030
7031static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
7032 unsigned nr_args)
7033{
7034 struct vm_area_struct **vmas = NULL;
7035 struct page **pages = NULL;
7036 int i, j, got_pages = 0;
7037 int ret = -EINVAL;
7038
7039 if (ctx->user_bufs)
7040 return -EBUSY;
7041 if (!nr_args || nr_args > UIO_MAXIOV)
7042 return -EINVAL;
7043
7044 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
7045 GFP_KERNEL);
7046 if (!ctx->user_bufs)
7047 return -ENOMEM;
7048
7049 for (i = 0; i < nr_args; i++) {
7050 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7051 unsigned long off, start, end, ubuf;
7052 int pret, nr_pages;
7053 struct iovec iov;
7054 size_t size;
7055
7056 ret = io_copy_iov(ctx, &iov, arg, i);
7057 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03007058 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007059
7060 /*
7061 * Don't impose further limits on the size and buffer
7062 * constraints here, we'll -EINVAL later when IO is
7063 * submitted if they are wrong.
7064 */
7065 ret = -EFAULT;
7066 if (!iov.iov_base || !iov.iov_len)
7067 goto err;
7068
7069 /* arbitrary limit, but we need something */
7070 if (iov.iov_len > SZ_1G)
7071 goto err;
7072
7073 ubuf = (unsigned long) iov.iov_base;
7074 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
7075 start = ubuf >> PAGE_SHIFT;
7076 nr_pages = end - start;
7077
7078 if (ctx->account_mem) {
7079 ret = io_account_mem(ctx->user, nr_pages);
7080 if (ret)
7081 goto err;
7082 }
7083
7084 ret = 0;
7085 if (!pages || nr_pages > got_pages) {
7086 kfree(vmas);
7087 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007088 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07007089 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007090 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07007091 sizeof(struct vm_area_struct *),
7092 GFP_KERNEL);
7093 if (!pages || !vmas) {
7094 ret = -ENOMEM;
7095 if (ctx->account_mem)
7096 io_unaccount_mem(ctx->user, nr_pages);
7097 goto err;
7098 }
7099 got_pages = nr_pages;
7100 }
7101
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007102 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07007103 GFP_KERNEL);
7104 ret = -ENOMEM;
7105 if (!imu->bvec) {
7106 if (ctx->account_mem)
7107 io_unaccount_mem(ctx->user, nr_pages);
7108 goto err;
7109 }
7110
7111 ret = 0;
7112 down_read(&current->mm->mmap_sem);
John Hubbard2113b052020-01-30 22:13:13 -08007113 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07007114 FOLL_WRITE | FOLL_LONGTERM,
7115 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007116 if (pret == nr_pages) {
7117 /* don't support file backed memory */
7118 for (j = 0; j < nr_pages; j++) {
7119 struct vm_area_struct *vma = vmas[j];
7120
7121 if (vma->vm_file &&
7122 !is_file_hugepages(vma->vm_file)) {
7123 ret = -EOPNOTSUPP;
7124 break;
7125 }
7126 }
7127 } else {
7128 ret = pret < 0 ? pret : -EFAULT;
7129 }
7130 up_read(&current->mm->mmap_sem);
7131 if (ret) {
7132 /*
7133 * if we did partial map, or found file backed vmas,
7134 * release any pages we did get
7135 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07007136 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007137 unpin_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07007138 if (ctx->account_mem)
7139 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007140 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007141 goto err;
7142 }
7143
7144 off = ubuf & ~PAGE_MASK;
7145 size = iov.iov_len;
7146 for (j = 0; j < nr_pages; j++) {
7147 size_t vec_len;
7148
7149 vec_len = min_t(size_t, size, PAGE_SIZE - off);
7150 imu->bvec[j].bv_page = pages[j];
7151 imu->bvec[j].bv_len = vec_len;
7152 imu->bvec[j].bv_offset = off;
7153 off = 0;
7154 size -= vec_len;
7155 }
7156 /* store original address for later verification */
7157 imu->ubuf = ubuf;
7158 imu->len = iov.iov_len;
7159 imu->nr_bvecs = nr_pages;
7160
7161 ctx->nr_user_bufs++;
7162 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007163 kvfree(pages);
7164 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007165 return 0;
7166err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007167 kvfree(pages);
7168 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007169 io_sqe_buffer_unregister(ctx);
7170 return ret;
7171}
7172
Jens Axboe9b402842019-04-11 11:45:41 -06007173static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
7174{
7175 __s32 __user *fds = arg;
7176 int fd;
7177
7178 if (ctx->cq_ev_fd)
7179 return -EBUSY;
7180
7181 if (copy_from_user(&fd, fds, sizeof(*fds)))
7182 return -EFAULT;
7183
7184 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
7185 if (IS_ERR(ctx->cq_ev_fd)) {
7186 int ret = PTR_ERR(ctx->cq_ev_fd);
7187 ctx->cq_ev_fd = NULL;
7188 return ret;
7189 }
7190
7191 return 0;
7192}
7193
7194static int io_eventfd_unregister(struct io_ring_ctx *ctx)
7195{
7196 if (ctx->cq_ev_fd) {
7197 eventfd_ctx_put(ctx->cq_ev_fd);
7198 ctx->cq_ev_fd = NULL;
7199 return 0;
7200 }
7201
7202 return -ENXIO;
7203}
7204
Jens Axboe5a2e7452020-02-23 16:23:11 -07007205static int __io_destroy_buffers(int id, void *p, void *data)
7206{
7207 struct io_ring_ctx *ctx = data;
7208 struct io_buffer *buf = p;
7209
Jens Axboe067524e2020-03-02 16:32:28 -07007210 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007211 return 0;
7212}
7213
7214static void io_destroy_buffers(struct io_ring_ctx *ctx)
7215{
7216 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
7217 idr_destroy(&ctx->io_buffer_idr);
7218}
7219
Jens Axboe2b188cc2019-01-07 10:46:33 -07007220static void io_ring_ctx_free(struct io_ring_ctx *ctx)
7221{
Jens Axboe6b063142019-01-10 22:13:58 -07007222 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007223 if (ctx->sqo_mm)
7224 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07007225
7226 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07007227 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07007228 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06007229 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007230 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07007231 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07007232
Jens Axboe2b188cc2019-01-07 10:46:33 -07007233#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07007234 if (ctx->ring_sock) {
7235 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007236 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07007237 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007238#endif
7239
Hristo Venev75b28af2019-08-26 17:23:46 +00007240 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007241 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007242
7243 percpu_ref_exit(&ctx->refs);
7244 if (ctx->account_mem)
7245 io_unaccount_mem(ctx->user,
7246 ring_pages(ctx->sq_entries, ctx->cq_entries));
7247 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07007248 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07007249 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07007250 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07007251 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007252 kfree(ctx);
7253}
7254
7255static __poll_t io_uring_poll(struct file *file, poll_table *wait)
7256{
7257 struct io_ring_ctx *ctx = file->private_data;
7258 __poll_t mask = 0;
7259
7260 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02007261 /*
7262 * synchronizes with barrier from wq_has_sleeper call in
7263 * io_commit_cqring
7264 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007265 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00007266 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
7267 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007268 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01007269 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007270 mask |= EPOLLIN | EPOLLRDNORM;
7271
7272 return mask;
7273}
7274
7275static int io_uring_fasync(int fd, struct file *file, int on)
7276{
7277 struct io_ring_ctx *ctx = file->private_data;
7278
7279 return fasync_helper(fd, file, on, &ctx->cq_fasync);
7280}
7281
Jens Axboe071698e2020-01-28 10:04:42 -07007282static int io_remove_personalities(int id, void *p, void *data)
7283{
7284 struct io_ring_ctx *ctx = data;
7285 const struct cred *cred;
7286
7287 cred = idr_remove(&ctx->personality_idr, id);
7288 if (cred)
7289 put_cred(cred);
7290 return 0;
7291}
7292
Jens Axboe85faa7b2020-04-09 18:14:00 -06007293static void io_ring_exit_work(struct work_struct *work)
7294{
7295 struct io_ring_ctx *ctx;
7296
7297 ctx = container_of(work, struct io_ring_ctx, exit_work);
7298 if (ctx->rings)
7299 io_cqring_overflow_flush(ctx, true);
7300
7301 wait_for_completion(&ctx->completions[0]);
7302 io_ring_ctx_free(ctx);
7303}
7304
Jens Axboe2b188cc2019-01-07 10:46:33 -07007305static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
7306{
7307 mutex_lock(&ctx->uring_lock);
7308 percpu_ref_kill(&ctx->refs);
7309 mutex_unlock(&ctx->uring_lock);
7310
Jens Axboedf069d82020-02-04 16:48:34 -07007311 /*
7312 * Wait for sq thread to idle, if we have one. It won't spin on new
7313 * work after we've killed the ctx ref above. This is important to do
7314 * before we cancel existing commands, as the thread could otherwise
7315 * be queueing new work post that. If that's work we need to cancel,
7316 * it could cause shutdown to hang.
7317 */
7318 while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait))
Xiaoguang Wang3fd44c82020-05-01 08:52:56 +08007319 cond_resched();
Jens Axboedf069d82020-02-04 16:48:34 -07007320
Jens Axboe5262f562019-09-17 12:26:57 -06007321 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07007322 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06007323
7324 if (ctx->io_wq)
7325 io_wq_cancel_all(ctx->io_wq);
7326
Jens Axboedef596e2019-01-09 08:59:42 -07007327 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07007328 /* if we failed setting up the ctx, we might not have any rings */
7329 if (ctx->rings)
7330 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07007331 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe85faa7b2020-04-09 18:14:00 -06007332 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
7333 queue_work(system_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007334}
7335
7336static int io_uring_release(struct inode *inode, struct file *file)
7337{
7338 struct io_ring_ctx *ctx = file->private_data;
7339
7340 file->private_data = NULL;
7341 io_ring_ctx_wait_and_kill(ctx);
7342 return 0;
7343}
7344
Jens Axboefcb323c2019-10-24 12:39:47 -06007345static void io_uring_cancel_files(struct io_ring_ctx *ctx,
7346 struct files_struct *files)
7347{
Jens Axboefcb323c2019-10-24 12:39:47 -06007348 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007349 struct io_kiocb *cancel_req = NULL, *req;
7350 DEFINE_WAIT(wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007351
7352 spin_lock_irq(&ctx->inflight_lock);
7353 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07007354 if (req->work.files != files)
7355 continue;
7356 /* req is being completed, ignore */
7357 if (!refcount_inc_not_zero(&req->refs))
7358 continue;
7359 cancel_req = req;
7360 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06007361 }
Jens Axboe768134d2019-11-10 20:30:53 -07007362 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007363 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07007364 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06007365 spin_unlock_irq(&ctx->inflight_lock);
7366
Jens Axboe768134d2019-11-10 20:30:53 -07007367 /* We need to keep going until we don't find a matching req */
7368 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007369 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08007370
Jens Axboe2ca10252020-02-13 17:17:35 -07007371 if (cancel_req->flags & REQ_F_OVERFLOW) {
7372 spin_lock_irq(&ctx->completion_lock);
7373 list_del(&cancel_req->list);
7374 cancel_req->flags &= ~REQ_F_OVERFLOW;
7375 if (list_empty(&ctx->cq_overflow_list)) {
7376 clear_bit(0, &ctx->sq_check_overflow);
7377 clear_bit(0, &ctx->cq_check_overflow);
7378 }
7379 spin_unlock_irq(&ctx->completion_lock);
7380
7381 WRITE_ONCE(ctx->rings->cq_overflow,
7382 atomic_inc_return(&ctx->cached_cq_overflow));
7383
7384 /*
7385 * Put inflight ref and overflow ref. If that's
7386 * all we had, then we're done with this request.
7387 */
7388 if (refcount_sub_and_test(2, &cancel_req->refs)) {
7389 io_put_req(cancel_req);
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007390 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboe2ca10252020-02-13 17:17:35 -07007391 continue;
7392 }
7393 }
7394
Bob Liu2f6d9b92019-11-13 18:06:24 +08007395 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
7396 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06007397 schedule();
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007398 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007399 }
7400}
7401
7402static int io_uring_flush(struct file *file, void *data)
7403{
7404 struct io_ring_ctx *ctx = file->private_data;
7405
7406 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07007407
7408 /*
7409 * If the task is going away, cancel work it may have pending
7410 */
7411 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
7412 io_wq_cancel_pid(ctx->io_wq, task_pid_vnr(current));
7413
Jens Axboefcb323c2019-10-24 12:39:47 -06007414 return 0;
7415}
7416
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007417static void *io_uring_validate_mmap_request(struct file *file,
7418 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007419{
Jens Axboe2b188cc2019-01-07 10:46:33 -07007420 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007421 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007422 struct page *page;
7423 void *ptr;
7424
7425 switch (offset) {
7426 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00007427 case IORING_OFF_CQ_RING:
7428 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007429 break;
7430 case IORING_OFF_SQES:
7431 ptr = ctx->sq_sqes;
7432 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007433 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007434 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007435 }
7436
7437 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07007438 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007439 return ERR_PTR(-EINVAL);
7440
7441 return ptr;
7442}
7443
7444#ifdef CONFIG_MMU
7445
7446static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7447{
7448 size_t sz = vma->vm_end - vma->vm_start;
7449 unsigned long pfn;
7450 void *ptr;
7451
7452 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
7453 if (IS_ERR(ptr))
7454 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007455
7456 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
7457 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
7458}
7459
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007460#else /* !CONFIG_MMU */
7461
7462static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7463{
7464 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
7465}
7466
7467static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
7468{
7469 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
7470}
7471
7472static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
7473 unsigned long addr, unsigned long len,
7474 unsigned long pgoff, unsigned long flags)
7475{
7476 void *ptr;
7477
7478 ptr = io_uring_validate_mmap_request(file, pgoff, len);
7479 if (IS_ERR(ptr))
7480 return PTR_ERR(ptr);
7481
7482 return (unsigned long) ptr;
7483}
7484
7485#endif /* !CONFIG_MMU */
7486
Jens Axboe2b188cc2019-01-07 10:46:33 -07007487SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
7488 u32, min_complete, u32, flags, const sigset_t __user *, sig,
7489 size_t, sigsz)
7490{
7491 struct io_ring_ctx *ctx;
7492 long ret = -EBADF;
7493 int submitted = 0;
7494 struct fd f;
7495
Jens Axboeb41e9852020-02-17 09:52:41 -07007496 if (current->task_works)
7497 task_work_run();
7498
Jens Axboe6c271ce2019-01-10 11:22:30 -07007499 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007500 return -EINVAL;
7501
7502 f = fdget(fd);
7503 if (!f.file)
7504 return -EBADF;
7505
7506 ret = -EOPNOTSUPP;
7507 if (f.file->f_op != &io_uring_fops)
7508 goto out_fput;
7509
7510 ret = -ENXIO;
7511 ctx = f.file->private_data;
7512 if (!percpu_ref_tryget(&ctx->refs))
7513 goto out_fput;
7514
Jens Axboe6c271ce2019-01-10 11:22:30 -07007515 /*
7516 * For SQ polling, the thread will do all submissions and completions.
7517 * Just return the requested submit count, and wake the thread if
7518 * we were asked to.
7519 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007520 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007521 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07007522 if (!list_empty_careful(&ctx->cq_overflow_list))
7523 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007524 if (flags & IORING_ENTER_SQ_WAKEUP)
7525 wake_up(&ctx->sqo_wait);
7526 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007527 } else if (to_submit) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07007528 mutex_lock(&ctx->uring_lock);
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03007529 submitted = io_submit_sqes(ctx, to_submit, f.file, fd, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007530 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007531
7532 if (submitted != to_submit)
7533 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007534 }
7535 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07007536 unsigned nr_events = 0;
7537
Jens Axboe2b188cc2019-01-07 10:46:33 -07007538 min_complete = min(min_complete, ctx->cq_entries);
7539
Xiaoguang Wang32b22442020-03-11 09:26:09 +08007540 /*
7541 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
7542 * space applications don't need to do io completion events
7543 * polling again, they can rely on io_sq_thread to do polling
7544 * work, which can reduce cpu usage and uring_lock contention.
7545 */
7546 if (ctx->flags & IORING_SETUP_IOPOLL &&
7547 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Jens Axboedef596e2019-01-09 08:59:42 -07007548 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07007549 } else {
7550 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
7551 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007552 }
7553
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007554out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03007555 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007556out_fput:
7557 fdput(f);
7558 return submitted ? submitted : ret;
7559}
7560
Tobias Klauserbebdb652020-02-26 18:38:32 +01007561#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07007562static int io_uring_show_cred(int id, void *p, void *data)
7563{
7564 const struct cred *cred = p;
7565 struct seq_file *m = data;
7566 struct user_namespace *uns = seq_user_ns(m);
7567 struct group_info *gi;
7568 kernel_cap_t cap;
7569 unsigned __capi;
7570 int g;
7571
7572 seq_printf(m, "%5d\n", id);
7573 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
7574 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
7575 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
7576 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
7577 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
7578 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
7579 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
7580 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
7581 seq_puts(m, "\n\tGroups:\t");
7582 gi = cred->group_info;
7583 for (g = 0; g < gi->ngroups; g++) {
7584 seq_put_decimal_ull(m, g ? " " : "",
7585 from_kgid_munged(uns, gi->gid[g]));
7586 }
7587 seq_puts(m, "\n\tCapEff:\t");
7588 cap = cred->cap_effective;
7589 CAP_FOR_EACH_U32(__capi)
7590 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
7591 seq_putc(m, '\n');
7592 return 0;
7593}
7594
7595static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
7596{
7597 int i;
7598
7599 mutex_lock(&ctx->uring_lock);
7600 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
7601 for (i = 0; i < ctx->nr_user_files; i++) {
7602 struct fixed_file_table *table;
7603 struct file *f;
7604
7605 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7606 f = table->files[i & IORING_FILE_TABLE_MASK];
7607 if (f)
7608 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
7609 else
7610 seq_printf(m, "%5u: <none>\n", i);
7611 }
7612 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
7613 for (i = 0; i < ctx->nr_user_bufs; i++) {
7614 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
7615
7616 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
7617 (unsigned int) buf->len);
7618 }
7619 if (!idr_is_empty(&ctx->personality_idr)) {
7620 seq_printf(m, "Personalities:\n");
7621 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
7622 }
Jens Axboed7718a92020-02-14 22:23:12 -07007623 seq_printf(m, "PollList:\n");
7624 spin_lock_irq(&ctx->completion_lock);
7625 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
7626 struct hlist_head *list = &ctx->cancel_hash[i];
7627 struct io_kiocb *req;
7628
7629 hlist_for_each_entry(req, list, hash_node)
7630 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
7631 req->task->task_works != NULL);
7632 }
7633 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07007634 mutex_unlock(&ctx->uring_lock);
7635}
7636
7637static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
7638{
7639 struct io_ring_ctx *ctx = f->private_data;
7640
7641 if (percpu_ref_tryget(&ctx->refs)) {
7642 __io_uring_show_fdinfo(ctx, m);
7643 percpu_ref_put(&ctx->refs);
7644 }
7645}
Tobias Klauserbebdb652020-02-26 18:38:32 +01007646#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07007647
Jens Axboe2b188cc2019-01-07 10:46:33 -07007648static const struct file_operations io_uring_fops = {
7649 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06007650 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07007651 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007652#ifndef CONFIG_MMU
7653 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
7654 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
7655#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07007656 .poll = io_uring_poll,
7657 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01007658#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07007659 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01007660#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07007661};
7662
7663static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
7664 struct io_uring_params *p)
7665{
Hristo Venev75b28af2019-08-26 17:23:46 +00007666 struct io_rings *rings;
7667 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007668
Hristo Venev75b28af2019-08-26 17:23:46 +00007669 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
7670 if (size == SIZE_MAX)
7671 return -EOVERFLOW;
7672
7673 rings = io_mem_alloc(size);
7674 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007675 return -ENOMEM;
7676
Hristo Venev75b28af2019-08-26 17:23:46 +00007677 ctx->rings = rings;
7678 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
7679 rings->sq_ring_mask = p->sq_entries - 1;
7680 rings->cq_ring_mask = p->cq_entries - 1;
7681 rings->sq_ring_entries = p->sq_entries;
7682 rings->cq_ring_entries = p->cq_entries;
7683 ctx->sq_mask = rings->sq_ring_mask;
7684 ctx->cq_mask = rings->cq_ring_mask;
7685 ctx->sq_entries = rings->sq_ring_entries;
7686 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007687
7688 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07007689 if (size == SIZE_MAX) {
7690 io_mem_free(ctx->rings);
7691 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007692 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07007693 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007694
7695 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07007696 if (!ctx->sq_sqes) {
7697 io_mem_free(ctx->rings);
7698 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007699 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07007700 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007701
Jens Axboe2b188cc2019-01-07 10:46:33 -07007702 return 0;
7703}
7704
7705/*
7706 * Allocate an anonymous fd, this is what constitutes the application
7707 * visible backing of an io_uring instance. The application mmaps this
7708 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
7709 * we have to tie this fd to a socket for file garbage collection purposes.
7710 */
7711static int io_uring_get_fd(struct io_ring_ctx *ctx)
7712{
7713 struct file *file;
7714 int ret;
7715
7716#if defined(CONFIG_UNIX)
7717 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
7718 &ctx->ring_sock);
7719 if (ret)
7720 return ret;
7721#endif
7722
7723 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
7724 if (ret < 0)
7725 goto err;
7726
7727 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
7728 O_RDWR | O_CLOEXEC);
7729 if (IS_ERR(file)) {
7730 put_unused_fd(ret);
7731 ret = PTR_ERR(file);
7732 goto err;
7733 }
7734
7735#if defined(CONFIG_UNIX)
7736 ctx->ring_sock->file = file;
7737#endif
7738 fd_install(ret, file);
7739 return ret;
7740err:
7741#if defined(CONFIG_UNIX)
7742 sock_release(ctx->ring_sock);
7743 ctx->ring_sock = NULL;
7744#endif
7745 return ret;
7746}
7747
Xiaoguang Wang7f136572020-05-05 16:28:53 +08007748static int io_uring_create(unsigned entries, struct io_uring_params *p,
7749 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007750{
7751 struct user_struct *user = NULL;
7752 struct io_ring_ctx *ctx;
7753 bool account_mem;
7754 int ret;
7755
Jens Axboe8110c1a2019-12-28 15:39:54 -07007756 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007757 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07007758 if (entries > IORING_MAX_ENTRIES) {
7759 if (!(p->flags & IORING_SETUP_CLAMP))
7760 return -EINVAL;
7761 entries = IORING_MAX_ENTRIES;
7762 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007763
7764 /*
7765 * Use twice as many entries for the CQ ring. It's possible for the
7766 * application to drive a higher depth than the size of the SQ ring,
7767 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06007768 * some flexibility in overcommitting a bit. If the application has
7769 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
7770 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07007771 */
7772 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06007773 if (p->flags & IORING_SETUP_CQSIZE) {
7774 /*
7775 * If IORING_SETUP_CQSIZE is set, we do the same roundup
7776 * to a power-of-two, if it isn't already. We do NOT impose
7777 * any cq vs sq ring sizing.
7778 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07007779 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06007780 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07007781 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
7782 if (!(p->flags & IORING_SETUP_CLAMP))
7783 return -EINVAL;
7784 p->cq_entries = IORING_MAX_CQ_ENTRIES;
7785 }
Jens Axboe33a107f2019-10-04 12:10:03 -06007786 p->cq_entries = roundup_pow_of_two(p->cq_entries);
7787 } else {
7788 p->cq_entries = 2 * p->sq_entries;
7789 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007790
7791 user = get_uid(current_user());
7792 account_mem = !capable(CAP_IPC_LOCK);
7793
7794 if (account_mem) {
7795 ret = io_account_mem(user,
7796 ring_pages(p->sq_entries, p->cq_entries));
7797 if (ret) {
7798 free_uid(user);
7799 return ret;
7800 }
7801 }
7802
7803 ctx = io_ring_ctx_alloc(p);
7804 if (!ctx) {
7805 if (account_mem)
7806 io_unaccount_mem(user, ring_pages(p->sq_entries,
7807 p->cq_entries));
7808 free_uid(user);
7809 return -ENOMEM;
7810 }
7811 ctx->compat = in_compat_syscall();
7812 ctx->account_mem = account_mem;
7813 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07007814 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07007815
7816 ret = io_allocate_scq_urings(ctx, p);
7817 if (ret)
7818 goto err;
7819
Jens Axboe6c271ce2019-01-10 11:22:30 -07007820 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007821 if (ret)
7822 goto err;
7823
Jens Axboe2b188cc2019-01-07 10:46:33 -07007824 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007825 p->sq_off.head = offsetof(struct io_rings, sq.head);
7826 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
7827 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
7828 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
7829 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
7830 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
7831 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007832
7833 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007834 p->cq_off.head = offsetof(struct io_rings, cq.head);
7835 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
7836 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
7837 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
7838 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
7839 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06007840
Xiaoguang Wang7f136572020-05-05 16:28:53 +08007841 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
7842 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
7843 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL;
7844
7845 if (copy_to_user(params, p, sizeof(*p))) {
7846 ret = -EFAULT;
7847 goto err;
7848 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06007849 /*
7850 * Install ring fd as the very last thing, so we don't risk someone
7851 * having closed it before we finish setup
7852 */
7853 ret = io_uring_get_fd(ctx);
7854 if (ret < 0)
7855 goto err;
7856
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007857 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007858 return ret;
7859err:
7860 io_ring_ctx_wait_and_kill(ctx);
7861 return ret;
7862}
7863
7864/*
7865 * Sets up an aio uring context, and returns the fd. Applications asks for a
7866 * ring size, we return the actual sq/cq ring sizes (among other things) in the
7867 * params structure passed in.
7868 */
7869static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
7870{
7871 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007872 int i;
7873
7874 if (copy_from_user(&p, params, sizeof(p)))
7875 return -EFAULT;
7876 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
7877 if (p.resv[i])
7878 return -EINVAL;
7879 }
7880
Jens Axboe6c271ce2019-01-10 11:22:30 -07007881 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07007882 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03007883 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007884 return -EINVAL;
7885
Xiaoguang Wang7f136572020-05-05 16:28:53 +08007886 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007887}
7888
7889SYSCALL_DEFINE2(io_uring_setup, u32, entries,
7890 struct io_uring_params __user *, params)
7891{
7892 return io_uring_setup(entries, params);
7893}
7894
Jens Axboe66f4af92020-01-16 15:36:52 -07007895static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
7896{
7897 struct io_uring_probe *p;
7898 size_t size;
7899 int i, ret;
7900
7901 size = struct_size(p, ops, nr_args);
7902 if (size == SIZE_MAX)
7903 return -EOVERFLOW;
7904 p = kzalloc(size, GFP_KERNEL);
7905 if (!p)
7906 return -ENOMEM;
7907
7908 ret = -EFAULT;
7909 if (copy_from_user(p, arg, size))
7910 goto out;
7911 ret = -EINVAL;
7912 if (memchr_inv(p, 0, size))
7913 goto out;
7914
7915 p->last_op = IORING_OP_LAST - 1;
7916 if (nr_args > IORING_OP_LAST)
7917 nr_args = IORING_OP_LAST;
7918
7919 for (i = 0; i < nr_args; i++) {
7920 p->ops[i].op = i;
7921 if (!io_op_defs[i].not_supported)
7922 p->ops[i].flags = IO_URING_OP_SUPPORTED;
7923 }
7924 p->ops_len = i;
7925
7926 ret = 0;
7927 if (copy_to_user(arg, p, size))
7928 ret = -EFAULT;
7929out:
7930 kfree(p);
7931 return ret;
7932}
7933
Jens Axboe071698e2020-01-28 10:04:42 -07007934static int io_register_personality(struct io_ring_ctx *ctx)
7935{
7936 const struct cred *creds = get_current_cred();
7937 int id;
7938
7939 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
7940 USHRT_MAX, GFP_KERNEL);
7941 if (id < 0)
7942 put_cred(creds);
7943 return id;
7944}
7945
7946static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
7947{
7948 const struct cred *old_creds;
7949
7950 old_creds = idr_remove(&ctx->personality_idr, id);
7951 if (old_creds) {
7952 put_cred(old_creds);
7953 return 0;
7954 }
7955
7956 return -EINVAL;
7957}
7958
7959static bool io_register_op_must_quiesce(int op)
7960{
7961 switch (op) {
7962 case IORING_UNREGISTER_FILES:
7963 case IORING_REGISTER_FILES_UPDATE:
7964 case IORING_REGISTER_PROBE:
7965 case IORING_REGISTER_PERSONALITY:
7966 case IORING_UNREGISTER_PERSONALITY:
7967 return false;
7968 default:
7969 return true;
7970 }
7971}
7972
Jens Axboeedafcce2019-01-09 09:16:05 -07007973static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
7974 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06007975 __releases(ctx->uring_lock)
7976 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07007977{
7978 int ret;
7979
Jens Axboe35fa71a2019-04-22 10:23:23 -06007980 /*
7981 * We're inside the ring mutex, if the ref is already dying, then
7982 * someone else killed the ctx or is already going through
7983 * io_uring_register().
7984 */
7985 if (percpu_ref_is_dying(&ctx->refs))
7986 return -ENXIO;
7987
Jens Axboe071698e2020-01-28 10:04:42 -07007988 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007989 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06007990
Jens Axboe05f3fb32019-12-09 11:22:50 -07007991 /*
7992 * Drop uring mutex before waiting for references to exit. If
7993 * another thread is currently inside io_uring_enter() it might
7994 * need to grab the uring_lock to make progress. If we hold it
7995 * here across the drain wait, then we can deadlock. It's safe
7996 * to drop the mutex here, since no new references will come in
7997 * after we've killed the percpu ref.
7998 */
7999 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07008000 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008001 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07008002 if (ret) {
8003 percpu_ref_resurrect(&ctx->refs);
8004 ret = -EINTR;
8005 goto out;
8006 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008007 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008008
8009 switch (opcode) {
8010 case IORING_REGISTER_BUFFERS:
8011 ret = io_sqe_buffer_register(ctx, arg, nr_args);
8012 break;
8013 case IORING_UNREGISTER_BUFFERS:
8014 ret = -EINVAL;
8015 if (arg || nr_args)
8016 break;
8017 ret = io_sqe_buffer_unregister(ctx);
8018 break;
Jens Axboe6b063142019-01-10 22:13:58 -07008019 case IORING_REGISTER_FILES:
8020 ret = io_sqe_files_register(ctx, arg, nr_args);
8021 break;
8022 case IORING_UNREGISTER_FILES:
8023 ret = -EINVAL;
8024 if (arg || nr_args)
8025 break;
8026 ret = io_sqe_files_unregister(ctx);
8027 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06008028 case IORING_REGISTER_FILES_UPDATE:
8029 ret = io_sqe_files_update(ctx, arg, nr_args);
8030 break;
Jens Axboe9b402842019-04-11 11:45:41 -06008031 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07008032 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06008033 ret = -EINVAL;
8034 if (nr_args != 1)
8035 break;
8036 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07008037 if (ret)
8038 break;
8039 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
8040 ctx->eventfd_async = 1;
8041 else
8042 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06008043 break;
8044 case IORING_UNREGISTER_EVENTFD:
8045 ret = -EINVAL;
8046 if (arg || nr_args)
8047 break;
8048 ret = io_eventfd_unregister(ctx);
8049 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07008050 case IORING_REGISTER_PROBE:
8051 ret = -EINVAL;
8052 if (!arg || nr_args > 256)
8053 break;
8054 ret = io_probe(ctx, arg, nr_args);
8055 break;
Jens Axboe071698e2020-01-28 10:04:42 -07008056 case IORING_REGISTER_PERSONALITY:
8057 ret = -EINVAL;
8058 if (arg || nr_args)
8059 break;
8060 ret = io_register_personality(ctx);
8061 break;
8062 case IORING_UNREGISTER_PERSONALITY:
8063 ret = -EINVAL;
8064 if (arg)
8065 break;
8066 ret = io_unregister_personality(ctx, nr_args);
8067 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008068 default:
8069 ret = -EINVAL;
8070 break;
8071 }
8072
Jens Axboe071698e2020-01-28 10:04:42 -07008073 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008074 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07008075 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07008076out:
8077 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008078 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008079 return ret;
8080}
8081
8082SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
8083 void __user *, arg, unsigned int, nr_args)
8084{
8085 struct io_ring_ctx *ctx;
8086 long ret = -EBADF;
8087 struct fd f;
8088
8089 f = fdget(fd);
8090 if (!f.file)
8091 return -EBADF;
8092
8093 ret = -EOPNOTSUPP;
8094 if (f.file->f_op != &io_uring_fops)
8095 goto out_fput;
8096
8097 ctx = f.file->private_data;
8098
8099 mutex_lock(&ctx->uring_lock);
8100 ret = __io_uring_register(ctx, opcode, arg, nr_args);
8101 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02008102 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
8103 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07008104out_fput:
8105 fdput(f);
8106 return ret;
8107}
8108
Jens Axboe2b188cc2019-01-07 10:46:33 -07008109static int __init io_uring_init(void)
8110{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008111#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
8112 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
8113 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
8114} while (0)
8115
8116#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
8117 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
8118 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
8119 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
8120 BUILD_BUG_SQE_ELEM(1, __u8, flags);
8121 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
8122 BUILD_BUG_SQE_ELEM(4, __s32, fd);
8123 BUILD_BUG_SQE_ELEM(8, __u64, off);
8124 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
8125 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008126 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008127 BUILD_BUG_SQE_ELEM(24, __u32, len);
8128 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
8129 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
8130 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
8131 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
8132 BUILD_BUG_SQE_ELEM(28, __u16, poll_events);
8133 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
8134 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
8135 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
8136 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
8137 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
8138 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
8139 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
8140 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008141 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008142 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
8143 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
8144 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008145 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008146
Jens Axboed3656342019-12-18 09:50:26 -07008147 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07008148 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008149 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
8150 return 0;
8151};
8152__initcall(io_uring_init);