Commit | Line | Data |
---|---|---|
2b188cc1 JA |
1 | // 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 | |
1e84b97b SB |
7 | * 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 | |
d068b506 | 14 | * through a control-dependency in io_get_cqe (smp_store_release to |
1e84b97b SB |
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. | |
2b188cc1 JA |
29 | * |
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 | |
c992fe29 | 40 | * Copyright (c) 2018-2019 Christoph Hellwig |
2b188cc1 JA |
41 | */ |
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> | |
52de1fe1 | 47 | #include <net/compat.h> |
2b188cc1 JA |
48 | #include <linux/refcount.h> |
49 | #include <linux/uio.h> | |
6b47ee6e | 50 | #include <linux/bits.h> |
2b188cc1 JA |
51 | |
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> | |
2b188cc1 JA |
58 | #include <linux/percpu.h> |
59 | #include <linux/slab.h> | |
2b188cc1 | 60 | #include <linux/blkdev.h> |
edafccee | 61 | #include <linux/bvec.h> |
2b188cc1 JA |
62 | #include <linux/net.h> |
63 | #include <net/sock.h> | |
64 | #include <net/af_unix.h> | |
6b06314c | 65 | #include <net/scm.h> |
2b188cc1 JA |
66 | #include <linux/anon_inodes.h> |
67 | #include <linux/sched/mm.h> | |
68 | #include <linux/uaccess.h> | |
69 | #include <linux/nospec.h> | |
edafccee JA |
70 | #include <linux/sizes.h> |
71 | #include <linux/hugetlb.h> | |
aa4c3967 | 72 | #include <linux/highmem.h> |
15b71abe JA |
73 | #include <linux/namei.h> |
74 | #include <linux/fsnotify.h> | |
4840e418 | 75 | #include <linux/fadvise.h> |
3e4827b0 | 76 | #include <linux/eventpoll.h> |
7d67af2c | 77 | #include <linux/splice.h> |
b41e9852 | 78 | #include <linux/task_work.h> |
bcf5a063 | 79 | #include <linux/pagemap.h> |
0f212204 | 80 | #include <linux/io_uring.h> |
ef98eb04 | 81 | #include <linux/tracehook.h> |
5bd2182d | 82 | #include <linux/audit.h> |
cdc1404a | 83 | #include <linux/security.h> |
2b188cc1 | 84 | |
c826bd7a DD |
85 | #define CREATE_TRACE_POINTS |
86 | #include <trace/events/io_uring.h> | |
87 | ||
2b188cc1 JA |
88 | #include <uapi/linux/io_uring.h> |
89 | ||
90 | #include "internal.h" | |
561fb04a | 91 | #include "io-wq.h" |
2b188cc1 | 92 | |
5277deaa | 93 | #define IORING_MAX_ENTRIES 32768 |
33a107f0 | 94 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
4ce8ad95 | 95 | #define IORING_SQPOLL_CAP_ENTRIES_VALUE 8 |
65e19f54 | 96 | |
187f08c1 | 97 | /* only define max */ |
042b0d85 | 98 | #define IORING_MAX_FIXED_FILES (1U << 15) |
21b55dbc SG |
99 | #define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \ |
100 | IORING_REGISTER_LAST + IORING_OP_LAST) | |
2b188cc1 | 101 | |
187f08c1 | 102 | #define IO_RSRC_TAG_TABLE_SHIFT (PAGE_SHIFT - 3) |
2d091d62 PB |
103 | #define IO_RSRC_TAG_TABLE_MAX (1U << IO_RSRC_TAG_TABLE_SHIFT) |
104 | #define IO_RSRC_TAG_TABLE_MASK (IO_RSRC_TAG_TABLE_MAX - 1) | |
105 | ||
489809e2 PB |
106 | #define IORING_MAX_REG_BUFFERS (1U << 14) |
107 | ||
68fe256a PB |
108 | #define SQE_COMMON_FLAGS (IOSQE_FIXED_FILE | IOSQE_IO_LINK | \ |
109 | IOSQE_IO_HARDLINK | IOSQE_ASYNC) | |
110 | ||
111 | #define SQE_VALID_FLAGS (SQE_COMMON_FLAGS|IOSQE_BUFFER_SELECT|IOSQE_IO_DRAIN) | |
112 | ||
c854357b | 113 | #define IO_REQ_CLEAN_FLAGS (REQ_F_BUFFER_SELECTED | REQ_F_NEED_CLEANUP | \ |
d886e185 PB |
114 | REQ_F_POLLED | REQ_F_INFLIGHT | REQ_F_CREDS | \ |
115 | REQ_F_ASYNC_DATA) | |
b16fed66 | 116 | |
09899b19 PB |
117 | #define IO_TCTX_REFS_CACHE_NR (1U << 10) |
118 | ||
2b188cc1 JA |
119 | struct io_uring { |
120 | u32 head ____cacheline_aligned_in_smp; | |
121 | u32 tail ____cacheline_aligned_in_smp; | |
122 | }; | |
123 | ||
1e84b97b | 124 | /* |
75b28aff HV |
125 | * This data is shared with the application through the mmap at offsets |
126 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. | |
1e84b97b SB |
127 | * |
128 | * The offsets to the member fields are published through struct | |
129 | * io_sqring_offsets when calling io_uring_setup. | |
130 | */ | |
75b28aff | 131 | struct io_rings { |
1e84b97b SB |
132 | /* |
133 | * Head and tail offsets into the ring; the offsets need to be | |
134 | * masked to get valid indices. | |
135 | * | |
75b28aff HV |
136 | * The kernel controls head of the sq ring and the tail of the cq ring, |
137 | * and the application controls tail of the sq ring and the head of the | |
138 | * cq ring. | |
1e84b97b | 139 | */ |
75b28aff | 140 | struct io_uring sq, cq; |
1e84b97b | 141 | /* |
75b28aff | 142 | * Bitmasks to apply to head and tail offsets (constant, equals |
1e84b97b SB |
143 | * ring_entries - 1) |
144 | */ | |
75b28aff HV |
145 | u32 sq_ring_mask, cq_ring_mask; |
146 | /* Ring sizes (constant, power of 2) */ | |
147 | u32 sq_ring_entries, cq_ring_entries; | |
1e84b97b SB |
148 | /* |
149 | * Number of invalid entries dropped by the kernel due to | |
150 | * invalid index stored in array | |
151 | * | |
152 | * Written by the kernel, shouldn't be modified by the | |
153 | * application (i.e. get number of "new events" by comparing to | |
154 | * cached value). | |
155 | * | |
156 | * After a new SQ head value was read by the application this | |
157 | * counter includes all submissions that were dropped reaching | |
158 | * the new SQ head (and possibly more). | |
159 | */ | |
75b28aff | 160 | u32 sq_dropped; |
1e84b97b | 161 | /* |
0d9b5b3a | 162 | * Runtime SQ flags |
1e84b97b SB |
163 | * |
164 | * Written by the kernel, shouldn't be modified by the | |
165 | * application. | |
166 | * | |
167 | * The application needs a full memory barrier before checking | |
168 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. | |
169 | */ | |
75b28aff | 170 | u32 sq_flags; |
0d9b5b3a SG |
171 | /* |
172 | * Runtime CQ flags | |
173 | * | |
174 | * Written by the application, shouldn't be modified by the | |
175 | * kernel. | |
176 | */ | |
fe7e3257 | 177 | u32 cq_flags; |
1e84b97b SB |
178 | /* |
179 | * Number of completion events lost because the queue was full; | |
180 | * this should be avoided by the application by making sure | |
0b4295b5 | 181 | * there are not more requests pending than there is space in |
1e84b97b SB |
182 | * the completion queue. |
183 | * | |
184 | * Written by the kernel, shouldn't be modified by the | |
185 | * application (i.e. get number of "new events" by comparing to | |
186 | * cached value). | |
187 | * | |
188 | * As completion events come in out of order this counter is not | |
189 | * ordered with any other data. | |
190 | */ | |
75b28aff | 191 | u32 cq_overflow; |
1e84b97b SB |
192 | /* |
193 | * Ring buffer of completion events. | |
194 | * | |
195 | * The kernel writes completion events fresh every time they are | |
196 | * produced, so the application is allowed to modify pending | |
197 | * entries. | |
198 | */ | |
75b28aff | 199 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
2b188cc1 JA |
200 | }; |
201 | ||
45d189c6 | 202 | enum io_uring_cmd_flags { |
51aac424 | 203 | IO_URING_F_COMPLETE_DEFER = 1, |
3b44b371 | 204 | IO_URING_F_UNLOCKED = 2, |
51aac424 PB |
205 | /* int's last bit, sign checks are usually faster than a bit test */ |
206 | IO_URING_F_NONBLOCK = INT_MIN, | |
45d189c6 PB |
207 | }; |
208 | ||
edafccee JA |
209 | struct io_mapped_ubuf { |
210 | u64 ubuf; | |
4751f53d | 211 | u64 ubuf_end; |
edafccee | 212 | unsigned int nr_bvecs; |
de293938 | 213 | unsigned long acct_pages; |
41edf1a5 | 214 | struct bio_vec bvec[]; |
edafccee JA |
215 | }; |
216 | ||
50238531 BM |
217 | struct io_ring_ctx; |
218 | ||
6c2450ae PB |
219 | struct io_overflow_cqe { |
220 | struct io_uring_cqe cqe; | |
221 | struct list_head list; | |
222 | }; | |
223 | ||
a04b0ac0 PB |
224 | struct io_fixed_file { |
225 | /* file * with additional FFS_* flags */ | |
226 | unsigned long file_ptr; | |
227 | }; | |
228 | ||
269bbe5f BM |
229 | struct io_rsrc_put { |
230 | struct list_head list; | |
b60c8dce | 231 | u64 tag; |
50238531 BM |
232 | union { |
233 | void *rsrc; | |
234 | struct file *file; | |
bd54b6fe | 235 | struct io_mapped_ubuf *buf; |
50238531 | 236 | }; |
269bbe5f BM |
237 | }; |
238 | ||
aeca241b | 239 | struct io_file_table { |
042b0d85 | 240 | struct io_fixed_file *files; |
31b51510 JA |
241 | }; |
242 | ||
b895c9a6 | 243 | struct io_rsrc_node { |
05589553 XW |
244 | struct percpu_ref refs; |
245 | struct list_head node; | |
269bbe5f | 246 | struct list_head rsrc_list; |
b895c9a6 | 247 | struct io_rsrc_data *rsrc_data; |
4a38aed2 | 248 | struct llist_node llist; |
e297822b | 249 | bool done; |
05589553 XW |
250 | }; |
251 | ||
40ae0ff7 PB |
252 | typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc); |
253 | ||
b895c9a6 | 254 | struct io_rsrc_data { |
05f3fb3c JA |
255 | struct io_ring_ctx *ctx; |
256 | ||
2d091d62 PB |
257 | u64 **tags; |
258 | unsigned int nr; | |
40ae0ff7 | 259 | rsrc_put_fn *do_put; |
3e942498 | 260 | atomic_t refs; |
05f3fb3c | 261 | struct completion done; |
8bad28d8 | 262 | bool quiesce; |
05f3fb3c JA |
263 | }; |
264 | ||
5a2e745d JA |
265 | struct io_buffer { |
266 | struct list_head list; | |
267 | __u64 addr; | |
d1f82808 | 268 | __u32 len; |
5a2e745d JA |
269 | __u16 bid; |
270 | }; | |
271 | ||
21b55dbc SG |
272 | struct io_restriction { |
273 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); | |
274 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); | |
275 | u8 sqe_flags_allowed; | |
276 | u8 sqe_flags_required; | |
7e84e1c7 | 277 | bool registered; |
21b55dbc SG |
278 | }; |
279 | ||
37d1e2e3 JA |
280 | enum { |
281 | IO_SQ_THREAD_SHOULD_STOP = 0, | |
282 | IO_SQ_THREAD_SHOULD_PARK, | |
283 | }; | |
284 | ||
534ca6d6 JA |
285 | struct io_sq_data { |
286 | refcount_t refs; | |
9e138a48 | 287 | atomic_t park_pending; |
09a6f4ef | 288 | struct mutex lock; |
69fb2131 JA |
289 | |
290 | /* ctx's that are using this sqd */ | |
291 | struct list_head ctx_list; | |
69fb2131 | 292 | |
534ca6d6 JA |
293 | struct task_struct *thread; |
294 | struct wait_queue_head wait; | |
08369246 XW |
295 | |
296 | unsigned sq_thread_idle; | |
37d1e2e3 JA |
297 | int sq_cpu; |
298 | pid_t task_pid; | |
5c2469e0 | 299 | pid_t task_tgid; |
37d1e2e3 JA |
300 | |
301 | unsigned long state; | |
37d1e2e3 | 302 | struct completion exited; |
534ca6d6 JA |
303 | }; |
304 | ||
6dd0be1e | 305 | #define IO_COMPL_BATCH 32 |
6ff119a6 | 306 | #define IO_REQ_CACHE_SIZE 32 |
bf019da7 | 307 | #define IO_REQ_ALLOC_BATCH 8 |
258b29a9 | 308 | |
a1ab7b35 PB |
309 | struct io_submit_link { |
310 | struct io_kiocb *head; | |
311 | struct io_kiocb *last; | |
312 | }; | |
313 | ||
258b29a9 | 314 | struct io_submit_state { |
5a158c6b PB |
315 | /* inline/task_work completion list, under ->uring_lock */ |
316 | struct io_wq_work_node free_list; | |
317 | /* batch completion logic */ | |
318 | struct io_wq_work_list compl_reqs; | |
a1ab7b35 | 319 | struct io_submit_link link; |
258b29a9 | 320 | |
258b29a9 | 321 | bool plug_started; |
4b628aeb | 322 | bool need_plug; |
5ca7a8b3 | 323 | unsigned short submit_nr; |
5a158c6b | 324 | struct blk_plug plug; |
258b29a9 PB |
325 | }; |
326 | ||
2b188cc1 | 327 | struct io_ring_ctx { |
b52ecf8c | 328 | /* const or read-mostly hot data */ |
2b188cc1 JA |
329 | struct { |
330 | struct percpu_ref refs; | |
2b188cc1 | 331 | |
b52ecf8c | 332 | struct io_rings *rings; |
2b188cc1 | 333 | unsigned int flags; |
e1d85334 | 334 | unsigned int compat: 1; |
e1d85334 RD |
335 | unsigned int drain_next: 1; |
336 | unsigned int eventfd_async: 1; | |
21b55dbc | 337 | unsigned int restricted: 1; |
f18ee4cf | 338 | unsigned int off_timeout_used: 1; |
10c66904 | 339 | unsigned int drain_active: 1; |
b52ecf8c | 340 | } ____cacheline_aligned_in_smp; |
2b188cc1 | 341 | |
7f1129d2 | 342 | /* submission data */ |
b52ecf8c | 343 | struct { |
0499e582 PB |
344 | struct mutex uring_lock; |
345 | ||
75b28aff HV |
346 | /* |
347 | * Ring buffer of indices into array of io_uring_sqe, which is | |
348 | * mmapped by the application using the IORING_OFF_SQES offset. | |
349 | * | |
350 | * This indirection could e.g. be used to assign fixed | |
351 | * io_uring_sqe entries to operations and only submit them to | |
352 | * the queue when needed. | |
353 | * | |
354 | * The kernel modifies neither the indices array nor the entries | |
355 | * array. | |
356 | */ | |
357 | u32 *sq_array; | |
c7af47cf | 358 | struct io_uring_sqe *sq_sqes; |
2b188cc1 JA |
359 | unsigned cached_sq_head; |
360 | unsigned sq_entries; | |
de0617e4 | 361 | struct list_head defer_list; |
7f1129d2 PB |
362 | |
363 | /* | |
364 | * Fixed resources fast path, should be accessed only under | |
365 | * uring_lock, and updated through io_uring_register(2) | |
366 | */ | |
367 | struct io_rsrc_node *rsrc_node; | |
ab409402 | 368 | int rsrc_cached_refs; |
7f1129d2 PB |
369 | struct io_file_table file_table; |
370 | unsigned nr_user_files; | |
371 | unsigned nr_user_bufs; | |
372 | struct io_mapped_ubuf **user_bufs; | |
373 | ||
374 | struct io_submit_state submit_state; | |
5262f567 | 375 | struct list_head timeout_list; |
ef9dd637 | 376 | struct list_head ltimeout_list; |
1d7bb1d5 | 377 | struct list_head cq_overflow_list; |
7f1129d2 PB |
378 | struct xarray io_buffers; |
379 | struct xarray personalities; | |
380 | u32 pers_next; | |
381 | unsigned sq_thread_idle; | |
2b188cc1 JA |
382 | } ____cacheline_aligned_in_smp; |
383 | ||
d0acdee2 | 384 | /* IRQ completion list, under ->completion_lock */ |
c2b6c6bc | 385 | struct io_wq_work_list locked_free_list; |
d0acdee2 | 386 | unsigned int locked_free_nr; |
3c1a2ead | 387 | |
7c30f36a | 388 | const struct cred *sq_creds; /* cred used for __io_sq_thread() */ |
534ca6d6 JA |
389 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
390 | ||
90554200 | 391 | struct wait_queue_head sqo_sq_wait; |
69fb2131 | 392 | struct list_head sqd_list; |
75b28aff | 393 | |
5ed7a37d PB |
394 | unsigned long check_cq_overflow; |
395 | ||
206aefde JA |
396 | struct { |
397 | unsigned cached_cq_tail; | |
398 | unsigned cq_entries; | |
0499e582 | 399 | struct eventfd_ctx *cq_ev_fd; |
0499e582 PB |
400 | struct wait_queue_head cq_wait; |
401 | unsigned cq_extra; | |
402 | atomic_t cq_timeouts; | |
0499e582 | 403 | unsigned cq_last_tm_flush; |
206aefde | 404 | } ____cacheline_aligned_in_smp; |
2b188cc1 | 405 | |
2b188cc1 JA |
406 | struct { |
407 | spinlock_t completion_lock; | |
e94f141b | 408 | |
89850fce JA |
409 | spinlock_t timeout_lock; |
410 | ||
def596e9 | 411 | /* |
540e32a0 | 412 | * ->iopoll_list is protected by the ctx->uring_lock for |
def596e9 JA |
413 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
414 | * For SQPOLL, only the single threaded io_sq_thread() will | |
415 | * manipulate the list, hence no extra locking is needed there. | |
416 | */ | |
5eef4e87 | 417 | struct io_wq_work_list iopoll_list; |
78076bb6 JA |
418 | struct hlist_head *cancel_hash; |
419 | unsigned cancel_hash_bits; | |
915b3dde | 420 | bool poll_multi_queue; |
2b188cc1 | 421 | } ____cacheline_aligned_in_smp; |
85faa7b8 | 422 | |
21b55dbc | 423 | struct io_restriction restrictions; |
3c1a2ead | 424 | |
b13a8918 PB |
425 | /* slow path rsrc auxilary data, used by update/register */ |
426 | struct { | |
427 | struct io_rsrc_node *rsrc_backup_node; | |
428 | struct io_mapped_ubuf *dummy_ubuf; | |
429 | struct io_rsrc_data *file_data; | |
430 | struct io_rsrc_data *buf_data; | |
431 | ||
432 | struct delayed_work rsrc_put_work; | |
433 | struct llist_head rsrc_put_llist; | |
434 | struct list_head rsrc_ref_list; | |
435 | spinlock_t rsrc_ref_lock; | |
436 | }; | |
437 | ||
3c1a2ead | 438 | /* Keep this last, we don't need it for the fast path */ |
b986af7e PB |
439 | struct { |
440 | #if defined(CONFIG_UNIX) | |
441 | struct socket *ring_sock; | |
442 | #endif | |
443 | /* hashed buffered write serialization */ | |
444 | struct io_wq_hash *hash_map; | |
445 | ||
446 | /* Only used for accounting purposes */ | |
447 | struct user_struct *user; | |
448 | struct mm_struct *mm_account; | |
449 | ||
450 | /* ctx exit and cancelation */ | |
9011bf9a PB |
451 | struct llist_head fallback_llist; |
452 | struct delayed_work fallback_work; | |
b986af7e PB |
453 | struct work_struct exit_work; |
454 | struct list_head tctx_list; | |
455 | struct completion ref_comp; | |
e139a1ec PB |
456 | u32 iowq_limits[2]; |
457 | bool iowq_limits_set; | |
b986af7e | 458 | }; |
2b188cc1 JA |
459 | }; |
460 | ||
53e043b2 SM |
461 | struct io_uring_task { |
462 | /* submission side */ | |
09899b19 | 463 | int cached_refs; |
53e043b2 SM |
464 | struct xarray xa; |
465 | struct wait_queue_head wait; | |
ee53fb2b SM |
466 | const struct io_ring_ctx *last; |
467 | struct io_wq *io_wq; | |
53e043b2 | 468 | struct percpu_counter inflight; |
b303fe2e | 469 | atomic_t inflight_tracked; |
53e043b2 | 470 | atomic_t in_idle; |
53e043b2 SM |
471 | |
472 | spinlock_t task_lock; | |
473 | struct io_wq_work_list task_list; | |
53e043b2 | 474 | struct callback_head task_work; |
6294f368 | 475 | bool task_running; |
53e043b2 SM |
476 | }; |
477 | ||
09bb8394 JA |
478 | /* |
479 | * First field must be the file pointer in all the | |
480 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> | |
481 | */ | |
221c5eb2 JA |
482 | struct io_poll_iocb { |
483 | struct file *file; | |
018043be | 484 | struct wait_queue_head *head; |
221c5eb2 | 485 | __poll_t events; |
8c838788 | 486 | bool done; |
221c5eb2 | 487 | bool canceled; |
392edb45 | 488 | struct wait_queue_entry wait; |
221c5eb2 JA |
489 | }; |
490 | ||
9d805892 | 491 | struct io_poll_update { |
018043be | 492 | struct file *file; |
9d805892 PB |
493 | u64 old_user_data; |
494 | u64 new_user_data; | |
495 | __poll_t events; | |
b69de288 JA |
496 | bool update_events; |
497 | bool update_user_data; | |
018043be PB |
498 | }; |
499 | ||
b5dba59e JA |
500 | struct io_close { |
501 | struct file *file; | |
b5dba59e | 502 | int fd; |
7df778be | 503 | u32 file_slot; |
b5dba59e JA |
504 | }; |
505 | ||
ad8a48ac JA |
506 | struct io_timeout_data { |
507 | struct io_kiocb *req; | |
508 | struct hrtimer timer; | |
509 | struct timespec64 ts; | |
510 | enum hrtimer_mode mode; | |
50c1df2b | 511 | u32 flags; |
ad8a48ac JA |
512 | }; |
513 | ||
8ed8d3c3 JA |
514 | struct io_accept { |
515 | struct file *file; | |
516 | struct sockaddr __user *addr; | |
517 | int __user *addr_len; | |
518 | int flags; | |
aaa4db12 | 519 | u32 file_slot; |
09952e3e | 520 | unsigned long nofile; |
8ed8d3c3 JA |
521 | }; |
522 | ||
523 | struct io_sync { | |
524 | struct file *file; | |
525 | loff_t len; | |
526 | loff_t off; | |
527 | int flags; | |
d63d1b5e | 528 | int mode; |
8ed8d3c3 JA |
529 | }; |
530 | ||
fbf23849 JA |
531 | struct io_cancel { |
532 | struct file *file; | |
533 | u64 addr; | |
534 | }; | |
535 | ||
b29472ee JA |
536 | struct io_timeout { |
537 | struct file *file; | |
bfe68a22 PB |
538 | u32 off; |
539 | u32 target_seq; | |
135fcde8 | 540 | struct list_head list; |
90cd7e42 PB |
541 | /* head of the link, used by linked timeouts only */ |
542 | struct io_kiocb *head; | |
89b263f6 JA |
543 | /* for linked completions */ |
544 | struct io_kiocb *prev; | |
b29472ee JA |
545 | }; |
546 | ||
0bdf7a2d PB |
547 | struct io_timeout_rem { |
548 | struct file *file; | |
549 | u64 addr; | |
9c8e11b3 PB |
550 | |
551 | /* timeout update */ | |
552 | struct timespec64 ts; | |
553 | u32 flags; | |
f1042b6c | 554 | bool ltimeout; |
0bdf7a2d PB |
555 | }; |
556 | ||
9adbd45d JA |
557 | struct io_rw { |
558 | /* NOTE: kiocb has the file as the first member, so don't do it here */ | |
559 | struct kiocb kiocb; | |
560 | u64 addr; | |
561 | u64 len; | |
562 | }; | |
563 | ||
3fbb51c1 JA |
564 | struct io_connect { |
565 | struct file *file; | |
566 | struct sockaddr __user *addr; | |
567 | int addr_len; | |
568 | }; | |
569 | ||
e47293fd JA |
570 | struct io_sr_msg { |
571 | struct file *file; | |
fddaface | 572 | union { |
4af3417a PB |
573 | struct compat_msghdr __user *umsg_compat; |
574 | struct user_msghdr __user *umsg; | |
575 | void __user *buf; | |
fddaface | 576 | }; |
e47293fd | 577 | int msg_flags; |
bcda7baa | 578 | int bgid; |
fddaface | 579 | size_t len; |
e47293fd JA |
580 | }; |
581 | ||
15b71abe JA |
582 | struct io_open { |
583 | struct file *file; | |
584 | int dfd; | |
b9445598 | 585 | u32 file_slot; |
15b71abe | 586 | struct filename *filename; |
c12cedf2 | 587 | struct open_how how; |
4022e7af | 588 | unsigned long nofile; |
15b71abe JA |
589 | }; |
590 | ||
269bbe5f | 591 | struct io_rsrc_update { |
05f3fb3c JA |
592 | struct file *file; |
593 | u64 arg; | |
594 | u32 nr_args; | |
595 | u32 offset; | |
596 | }; | |
597 | ||
4840e418 JA |
598 | struct io_fadvise { |
599 | struct file *file; | |
600 | u64 offset; | |
601 | u32 len; | |
602 | u32 advice; | |
603 | }; | |
604 | ||
c1ca757b JA |
605 | struct io_madvise { |
606 | struct file *file; | |
607 | u64 addr; | |
608 | u32 len; | |
609 | u32 advice; | |
610 | }; | |
611 | ||
3e4827b0 JA |
612 | struct io_epoll { |
613 | struct file *file; | |
614 | int epfd; | |
615 | int op; | |
616 | int fd; | |
617 | struct epoll_event event; | |
e47293fd JA |
618 | }; |
619 | ||
7d67af2c PB |
620 | struct io_splice { |
621 | struct file *file_out; | |
622 | struct file *file_in; | |
623 | loff_t off_out; | |
624 | loff_t off_in; | |
625 | u64 len; | |
626 | unsigned int flags; | |
627 | }; | |
628 | ||
ddf0322d JA |
629 | struct io_provide_buf { |
630 | struct file *file; | |
631 | __u64 addr; | |
38134ada | 632 | __u32 len; |
ddf0322d JA |
633 | __u32 bgid; |
634 | __u16 nbufs; | |
635 | __u16 bid; | |
636 | }; | |
637 | ||
1d9e1288 BM |
638 | struct io_statx { |
639 | struct file *file; | |
640 | int dfd; | |
641 | unsigned int mask; | |
642 | unsigned int flags; | |
e62753e4 | 643 | const char __user *filename; |
1d9e1288 BM |
644 | struct statx __user *buffer; |
645 | }; | |
646 | ||
36f4fa68 JA |
647 | struct io_shutdown { |
648 | struct file *file; | |
649 | int how; | |
650 | }; | |
651 | ||
80a261fd JA |
652 | struct io_rename { |
653 | struct file *file; | |
654 | int old_dfd; | |
655 | int new_dfd; | |
656 | struct filename *oldpath; | |
657 | struct filename *newpath; | |
658 | int flags; | |
659 | }; | |
660 | ||
14a1143b JA |
661 | struct io_unlink { |
662 | struct file *file; | |
663 | int dfd; | |
664 | int flags; | |
665 | struct filename *filename; | |
666 | }; | |
667 | ||
e34a02dc DK |
668 | struct io_mkdir { |
669 | struct file *file; | |
670 | int dfd; | |
671 | umode_t mode; | |
672 | struct filename *filename; | |
673 | }; | |
674 | ||
7a8721f8 DK |
675 | struct io_symlink { |
676 | struct file *file; | |
677 | int new_dfd; | |
678 | struct filename *oldpath; | |
679 | struct filename *newpath; | |
680 | }; | |
681 | ||
cf30da90 DK |
682 | struct io_hardlink { |
683 | struct file *file; | |
684 | int old_dfd; | |
685 | int new_dfd; | |
686 | struct filename *oldpath; | |
687 | struct filename *newpath; | |
688 | int flags; | |
689 | }; | |
690 | ||
f499a021 JA |
691 | struct io_async_connect { |
692 | struct sockaddr_storage address; | |
693 | }; | |
694 | ||
03b1230c JA |
695 | struct io_async_msghdr { |
696 | struct iovec fast_iov[UIO_FASTIOV]; | |
257e84a5 PB |
697 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
698 | struct iovec *free_iov; | |
03b1230c JA |
699 | struct sockaddr __user *uaddr; |
700 | struct msghdr msg; | |
b537916c | 701 | struct sockaddr_storage addr; |
03b1230c JA |
702 | }; |
703 | ||
538941e2 | 704 | struct io_rw_state { |
ff6165b2 | 705 | struct iov_iter iter; |
cd658695 | 706 | struct iov_iter_state iter_state; |
c88598a9 | 707 | struct iovec fast_iov[UIO_FASTIOV]; |
538941e2 PB |
708 | }; |
709 | ||
710 | struct io_async_rw { | |
711 | struct io_rw_state s; | |
712 | const struct iovec *free_iovec; | |
227c0c96 | 713 | size_t bytes_done; |
bcf5a063 | 714 | struct wait_page_queue wpq; |
f67676d1 JA |
715 | }; |
716 | ||
6b47ee6e PB |
717 | enum { |
718 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, | |
719 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, | |
720 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, | |
721 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, | |
722 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, | |
bcda7baa | 723 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
6b47ee6e | 724 | |
dddca226 | 725 | /* first byte is taken by user flags, shift it to not overlap */ |
93d2bcd2 | 726 | REQ_F_FAIL_BIT = 8, |
6b47ee6e PB |
727 | REQ_F_INFLIGHT_BIT, |
728 | REQ_F_CUR_POS_BIT, | |
729 | REQ_F_NOWAIT_BIT, | |
6b47ee6e | 730 | REQ_F_LINK_TIMEOUT_BIT, |
99bc4c38 | 731 | REQ_F_NEED_CLEANUP_BIT, |
d7718a9d | 732 | REQ_F_POLLED_BIT, |
bcda7baa | 733 | REQ_F_BUFFER_SELECTED_BIT, |
e342c807 | 734 | REQ_F_COMPLETE_INLINE_BIT, |
230d50d4 | 735 | REQ_F_REISSUE_BIT, |
b8e64b53 | 736 | REQ_F_CREDS_BIT, |
20e60a38 | 737 | REQ_F_REFCOUNT_BIT, |
4d13d1a4 | 738 | REQ_F_ARM_LTIMEOUT_BIT, |
d886e185 | 739 | REQ_F_ASYNC_DATA_BIT, |
7b29f92d | 740 | /* keep async read/write and isreg together and in order */ |
35645ac3 | 741 | REQ_F_SUPPORT_NOWAIT_BIT, |
7b29f92d | 742 | REQ_F_ISREG_BIT, |
84557871 JA |
743 | |
744 | /* not a real bit, just to check we're not overflowing the space */ | |
745 | __REQ_F_LAST_BIT, | |
6b47ee6e PB |
746 | }; |
747 | ||
748 | enum { | |
749 | /* ctx owns file */ | |
750 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), | |
751 | /* drain existing IO first */ | |
752 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), | |
753 | /* linked sqes */ | |
754 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), | |
755 | /* doesn't sever on completion < 0 */ | |
756 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), | |
757 | /* IOSQE_ASYNC */ | |
758 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), | |
bcda7baa JA |
759 | /* IOSQE_BUFFER_SELECT */ |
760 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), | |
6b47ee6e | 761 | |
6b47ee6e | 762 | /* fail rest of links */ |
93d2bcd2 | 763 | REQ_F_FAIL = BIT(REQ_F_FAIL_BIT), |
b05a1bcd | 764 | /* on inflight list, should be cancelled and waited on exit reliably */ |
6b47ee6e PB |
765 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
766 | /* read/write uses file position */ | |
767 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), | |
768 | /* must not punt to workers */ | |
769 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), | |
900fad45 | 770 | /* has or had linked timeout */ |
6b47ee6e | 771 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
99bc4c38 PB |
772 | /* needs cleanup */ |
773 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), | |
d7718a9d JA |
774 | /* already went through poll handler */ |
775 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), | |
bcda7baa JA |
776 | /* buffer already selected */ |
777 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), | |
e342c807 PB |
778 | /* completion is deferred through io_comp_state */ |
779 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), | |
230d50d4 JA |
780 | /* caller should reissue async */ |
781 | REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT), | |
35645ac3 PB |
782 | /* supports async reads/writes */ |
783 | REQ_F_SUPPORT_NOWAIT = BIT(REQ_F_SUPPORT_NOWAIT_BIT), | |
7b29f92d JA |
784 | /* regular file */ |
785 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), | |
b8e64b53 PB |
786 | /* has creds assigned */ |
787 | REQ_F_CREDS = BIT(REQ_F_CREDS_BIT), | |
20e60a38 PB |
788 | /* skip refcounting if not set */ |
789 | REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT), | |
4d13d1a4 PB |
790 | /* there is a linked timeout that has to be armed */ |
791 | REQ_F_ARM_LTIMEOUT = BIT(REQ_F_ARM_LTIMEOUT_BIT), | |
d886e185 PB |
792 | /* ->async_data allocated */ |
793 | REQ_F_ASYNC_DATA = BIT(REQ_F_ASYNC_DATA_BIT), | |
d7718a9d JA |
794 | }; |
795 | ||
796 | struct async_poll { | |
797 | struct io_poll_iocb poll; | |
807abcb0 | 798 | struct io_poll_iocb *double_poll; |
6b47ee6e PB |
799 | }; |
800 | ||
f237c30a | 801 | typedef void (*io_req_tw_func_t)(struct io_kiocb *req, bool *locked); |
5b0a6acc | 802 | |
7cbf1722 | 803 | struct io_task_work { |
5b0a6acc PB |
804 | union { |
805 | struct io_wq_work_node node; | |
806 | struct llist_node fallback_node; | |
807 | }; | |
808 | io_req_tw_func_t func; | |
7cbf1722 JA |
809 | }; |
810 | ||
992da01a PB |
811 | enum { |
812 | IORING_RSRC_FILE = 0, | |
813 | IORING_RSRC_BUFFER = 1, | |
814 | }; | |
815 | ||
09bb8394 JA |
816 | /* |
817 | * NOTE! Each of the iocb union members has the file pointer | |
818 | * as the first entry in their struct definition. So you can | |
819 | * access the file pointer through any of the sub-structs, | |
820 | * or directly as just 'ki_filp' in this struct. | |
821 | */ | |
2b188cc1 | 822 | struct io_kiocb { |
221c5eb2 | 823 | union { |
09bb8394 | 824 | struct file *file; |
9adbd45d | 825 | struct io_rw rw; |
221c5eb2 | 826 | struct io_poll_iocb poll; |
9d805892 | 827 | struct io_poll_update poll_update; |
8ed8d3c3 JA |
828 | struct io_accept accept; |
829 | struct io_sync sync; | |
fbf23849 | 830 | struct io_cancel cancel; |
b29472ee | 831 | struct io_timeout timeout; |
0bdf7a2d | 832 | struct io_timeout_rem timeout_rem; |
3fbb51c1 | 833 | struct io_connect connect; |
e47293fd | 834 | struct io_sr_msg sr_msg; |
15b71abe | 835 | struct io_open open; |
b5dba59e | 836 | struct io_close close; |
269bbe5f | 837 | struct io_rsrc_update rsrc_update; |
4840e418 | 838 | struct io_fadvise fadvise; |
c1ca757b | 839 | struct io_madvise madvise; |
3e4827b0 | 840 | struct io_epoll epoll; |
7d67af2c | 841 | struct io_splice splice; |
ddf0322d | 842 | struct io_provide_buf pbuf; |
1d9e1288 | 843 | struct io_statx statx; |
36f4fa68 | 844 | struct io_shutdown shutdown; |
80a261fd | 845 | struct io_rename rename; |
14a1143b | 846 | struct io_unlink unlink; |
e34a02dc | 847 | struct io_mkdir mkdir; |
7a8721f8 | 848 | struct io_symlink symlink; |
cf30da90 | 849 | struct io_hardlink hardlink; |
221c5eb2 | 850 | }; |
2b188cc1 | 851 | |
d625c6ee | 852 | u8 opcode; |
65a6543d XW |
853 | /* polled IO has completed */ |
854 | u8 iopoll_completed; | |
4f4eeba8 | 855 | u16 buf_index; |
d17e56eb PB |
856 | unsigned int flags; |
857 | ||
858 | u64 user_data; | |
9cf7c104 | 859 | u32 result; |
d17e56eb | 860 | u32 cflags; |
4f4eeba8 | 861 | |
010e8e6b | 862 | struct io_ring_ctx *ctx; |
010e8e6b | 863 | struct task_struct *task; |
d7718a9d | 864 | |
269bbe5f | 865 | struct percpu_ref *fixed_rsrc_refs; |
d886e185 PB |
866 | /* store used ubuf, so we can prevent reloading */ |
867 | struct io_mapped_ubuf *imu; | |
fcb323cc | 868 | |
7e3709d5 | 869 | /* used by request caches, completion batching and iopoll */ |
ef05d9eb | 870 | struct io_wq_work_node comp_list; |
d17e56eb | 871 | atomic_t refs; |
7e3709d5 | 872 | struct io_kiocb *link; |
5b0a6acc | 873 | struct io_task_work io_task_work; |
010e8e6b PB |
874 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
875 | struct hlist_node hash_node; | |
7e3709d5 | 876 | /* internal polling, see IORING_FEAT_FAST_POLL */ |
010e8e6b | 877 | struct async_poll *apoll; |
d886e185 PB |
878 | /* opcode allocated if it needs to store data for async defer */ |
879 | void *async_data; | |
010e8e6b | 880 | struct io_wq_work work; |
7e3709d5 | 881 | /* custom credentials, valid IFF REQ_F_CREDS is set */ |
fe7e3257 | 882 | const struct cred *creds; |
7e3709d5 | 883 | /* stores selected buf, valid IFF REQ_F_BUFFER_SELECTED is set */ |
30d51dd4 | 884 | struct io_buffer *kbuf; |
2b188cc1 | 885 | }; |
05589553 | 886 | |
13bf43f5 PB |
887 | struct io_tctx_node { |
888 | struct list_head ctx_node; | |
889 | struct task_struct *task; | |
13bf43f5 PB |
890 | struct io_ring_ctx *ctx; |
891 | }; | |
892 | ||
27dc8338 PB |
893 | struct io_defer_entry { |
894 | struct list_head list; | |
895 | struct io_kiocb *req; | |
9cf7c104 | 896 | u32 seq; |
2b188cc1 JA |
897 | }; |
898 | ||
d3656344 | 899 | struct io_op_def { |
d3656344 JA |
900 | /* needs req->file assigned */ |
901 | unsigned needs_file : 1; | |
6d63416d PB |
902 | /* should block plug */ |
903 | unsigned plug : 1; | |
d3656344 JA |
904 | /* hash wq insertion if file is a regular file */ |
905 | unsigned hash_reg_file : 1; | |
906 | /* unbound wq insertion if file is a non-regular file */ | |
907 | unsigned unbound_nonreg_file : 1; | |
8a72758c JA |
908 | /* set if opcode supports polled "wait" */ |
909 | unsigned pollin : 1; | |
910 | unsigned pollout : 1; | |
bcda7baa JA |
911 | /* op supports buffer selection */ |
912 | unsigned buffer_select : 1; | |
26f0505a PB |
913 | /* do prep async if is going to be punted */ |
914 | unsigned needs_async_setup : 1; | |
6d63416d PB |
915 | /* opcode is not supported by this kernel */ |
916 | unsigned not_supported : 1; | |
5bd2182d PM |
917 | /* skip auditing */ |
918 | unsigned audit_skip : 1; | |
e8c2bc1f JA |
919 | /* size of async data needed, if any */ |
920 | unsigned short async_size; | |
d3656344 JA |
921 | }; |
922 | ||
0918682b | 923 | static const struct io_op_def io_op_defs[] = { |
0463b6c5 PB |
924 | [IORING_OP_NOP] = {}, |
925 | [IORING_OP_READV] = { | |
d3656344 JA |
926 | .needs_file = 1, |
927 | .unbound_nonreg_file = 1, | |
8a72758c | 928 | .pollin = 1, |
4d954c25 | 929 | .buffer_select = 1, |
26f0505a | 930 | .needs_async_setup = 1, |
27926b68 | 931 | .plug = 1, |
5bd2182d | 932 | .audit_skip = 1, |
e8c2bc1f | 933 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 934 | }, |
0463b6c5 | 935 | [IORING_OP_WRITEV] = { |
d3656344 JA |
936 | .needs_file = 1, |
937 | .hash_reg_file = 1, | |
938 | .unbound_nonreg_file = 1, | |
8a72758c | 939 | .pollout = 1, |
26f0505a | 940 | .needs_async_setup = 1, |
27926b68 | 941 | .plug = 1, |
5bd2182d | 942 | .audit_skip = 1, |
e8c2bc1f | 943 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 944 | }, |
0463b6c5 | 945 | [IORING_OP_FSYNC] = { |
d3656344 | 946 | .needs_file = 1, |
5bd2182d | 947 | .audit_skip = 1, |
d3656344 | 948 | }, |
0463b6c5 | 949 | [IORING_OP_READ_FIXED] = { |
d3656344 JA |
950 | .needs_file = 1, |
951 | .unbound_nonreg_file = 1, | |
8a72758c | 952 | .pollin = 1, |
27926b68 | 953 | .plug = 1, |
5bd2182d | 954 | .audit_skip = 1, |
e8c2bc1f | 955 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 956 | }, |
0463b6c5 | 957 | [IORING_OP_WRITE_FIXED] = { |
d3656344 JA |
958 | .needs_file = 1, |
959 | .hash_reg_file = 1, | |
960 | .unbound_nonreg_file = 1, | |
8a72758c | 961 | .pollout = 1, |
27926b68 | 962 | .plug = 1, |
5bd2182d | 963 | .audit_skip = 1, |
e8c2bc1f | 964 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 965 | }, |
0463b6c5 | 966 | [IORING_OP_POLL_ADD] = { |
d3656344 JA |
967 | .needs_file = 1, |
968 | .unbound_nonreg_file = 1, | |
5bd2182d PM |
969 | .audit_skip = 1, |
970 | }, | |
971 | [IORING_OP_POLL_REMOVE] = { | |
972 | .audit_skip = 1, | |
d3656344 | 973 | }, |
0463b6c5 | 974 | [IORING_OP_SYNC_FILE_RANGE] = { |
d3656344 | 975 | .needs_file = 1, |
5bd2182d | 976 | .audit_skip = 1, |
d3656344 | 977 | }, |
0463b6c5 | 978 | [IORING_OP_SENDMSG] = { |
d3656344 JA |
979 | .needs_file = 1, |
980 | .unbound_nonreg_file = 1, | |
8a72758c | 981 | .pollout = 1, |
26f0505a | 982 | .needs_async_setup = 1, |
e8c2bc1f | 983 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 984 | }, |
0463b6c5 | 985 | [IORING_OP_RECVMSG] = { |
d3656344 JA |
986 | .needs_file = 1, |
987 | .unbound_nonreg_file = 1, | |
8a72758c | 988 | .pollin = 1, |
52de1fe1 | 989 | .buffer_select = 1, |
26f0505a | 990 | .needs_async_setup = 1, |
e8c2bc1f | 991 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 992 | }, |
0463b6c5 | 993 | [IORING_OP_TIMEOUT] = { |
5bd2182d | 994 | .audit_skip = 1, |
e8c2bc1f | 995 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 996 | }, |
9c8e11b3 PB |
997 | [IORING_OP_TIMEOUT_REMOVE] = { |
998 | /* used by timeout updates' prep() */ | |
5bd2182d | 999 | .audit_skip = 1, |
9c8e11b3 | 1000 | }, |
0463b6c5 | 1001 | [IORING_OP_ACCEPT] = { |
d3656344 JA |
1002 | .needs_file = 1, |
1003 | .unbound_nonreg_file = 1, | |
8a72758c | 1004 | .pollin = 1, |
d3656344 | 1005 | }, |
5bd2182d PM |
1006 | [IORING_OP_ASYNC_CANCEL] = { |
1007 | .audit_skip = 1, | |
1008 | }, | |
0463b6c5 | 1009 | [IORING_OP_LINK_TIMEOUT] = { |
5bd2182d | 1010 | .audit_skip = 1, |
e8c2bc1f | 1011 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 1012 | }, |
0463b6c5 | 1013 | [IORING_OP_CONNECT] = { |
d3656344 JA |
1014 | .needs_file = 1, |
1015 | .unbound_nonreg_file = 1, | |
8a72758c | 1016 | .pollout = 1, |
26f0505a | 1017 | .needs_async_setup = 1, |
e8c2bc1f | 1018 | .async_size = sizeof(struct io_async_connect), |
d3656344 | 1019 | }, |
0463b6c5 | 1020 | [IORING_OP_FALLOCATE] = { |
d3656344 | 1021 | .needs_file = 1, |
d3656344 | 1022 | }, |
44526bed JA |
1023 | [IORING_OP_OPENAT] = {}, |
1024 | [IORING_OP_CLOSE] = {}, | |
5bd2182d PM |
1025 | [IORING_OP_FILES_UPDATE] = { |
1026 | .audit_skip = 1, | |
1027 | }, | |
1028 | [IORING_OP_STATX] = { | |
1029 | .audit_skip = 1, | |
1030 | }, | |
0463b6c5 | 1031 | [IORING_OP_READ] = { |
3a6820f2 JA |
1032 | .needs_file = 1, |
1033 | .unbound_nonreg_file = 1, | |
8a72758c | 1034 | .pollin = 1, |
bcda7baa | 1035 | .buffer_select = 1, |
27926b68 | 1036 | .plug = 1, |
5bd2182d | 1037 | .audit_skip = 1, |
e8c2bc1f | 1038 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 1039 | }, |
0463b6c5 | 1040 | [IORING_OP_WRITE] = { |
3a6820f2 | 1041 | .needs_file = 1, |
7b3188e7 | 1042 | .hash_reg_file = 1, |
3a6820f2 | 1043 | .unbound_nonreg_file = 1, |
8a72758c | 1044 | .pollout = 1, |
27926b68 | 1045 | .plug = 1, |
5bd2182d | 1046 | .audit_skip = 1, |
e8c2bc1f | 1047 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 1048 | }, |
0463b6c5 | 1049 | [IORING_OP_FADVISE] = { |
4840e418 | 1050 | .needs_file = 1, |
5bd2182d | 1051 | .audit_skip = 1, |
c1ca757b | 1052 | }, |
44526bed | 1053 | [IORING_OP_MADVISE] = {}, |
0463b6c5 | 1054 | [IORING_OP_SEND] = { |
fddaface JA |
1055 | .needs_file = 1, |
1056 | .unbound_nonreg_file = 1, | |
8a72758c | 1057 | .pollout = 1, |
5bd2182d | 1058 | .audit_skip = 1, |
fddaface | 1059 | }, |
0463b6c5 | 1060 | [IORING_OP_RECV] = { |
fddaface JA |
1061 | .needs_file = 1, |
1062 | .unbound_nonreg_file = 1, | |
8a72758c | 1063 | .pollin = 1, |
bcda7baa | 1064 | .buffer_select = 1, |
5bd2182d | 1065 | .audit_skip = 1, |
fddaface | 1066 | }, |
0463b6c5 | 1067 | [IORING_OP_OPENAT2] = { |
cebdb986 | 1068 | }, |
3e4827b0 JA |
1069 | [IORING_OP_EPOLL_CTL] = { |
1070 | .unbound_nonreg_file = 1, | |
5bd2182d | 1071 | .audit_skip = 1, |
3e4827b0 | 1072 | }, |
7d67af2c PB |
1073 | [IORING_OP_SPLICE] = { |
1074 | .needs_file = 1, | |
1075 | .hash_reg_file = 1, | |
1076 | .unbound_nonreg_file = 1, | |
5bd2182d PM |
1077 | .audit_skip = 1, |
1078 | }, | |
1079 | [IORING_OP_PROVIDE_BUFFERS] = { | |
1080 | .audit_skip = 1, | |
1081 | }, | |
1082 | [IORING_OP_REMOVE_BUFFERS] = { | |
1083 | .audit_skip = 1, | |
ddf0322d | 1084 | }, |
f2a8d5c7 PB |
1085 | [IORING_OP_TEE] = { |
1086 | .needs_file = 1, | |
1087 | .hash_reg_file = 1, | |
1088 | .unbound_nonreg_file = 1, | |
5bd2182d | 1089 | .audit_skip = 1, |
f2a8d5c7 | 1090 | }, |
36f4fa68 JA |
1091 | [IORING_OP_SHUTDOWN] = { |
1092 | .needs_file = 1, | |
1093 | }, | |
44526bed JA |
1094 | [IORING_OP_RENAMEAT] = {}, |
1095 | [IORING_OP_UNLINKAT] = {}, | |
e34a02dc | 1096 | [IORING_OP_MKDIRAT] = {}, |
7a8721f8 | 1097 | [IORING_OP_SYMLINKAT] = {}, |
cf30da90 | 1098 | [IORING_OP_LINKAT] = {}, |
d3656344 JA |
1099 | }; |
1100 | ||
0756a869 PB |
1101 | /* requests with any of those set should undergo io_disarm_next() */ |
1102 | #define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL) | |
1103 | ||
7a612350 | 1104 | static bool io_disarm_next(struct io_kiocb *req); |
eef51daa | 1105 | static void io_uring_del_tctx_node(unsigned long index); |
9936c7c2 PB |
1106 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
1107 | struct task_struct *task, | |
3dd0c97a | 1108 | bool cancel_all); |
78cc687b | 1109 | static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd); |
1ffc5422 | 1110 | |
d4d19c19 | 1111 | static bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
54daa9b2 | 1112 | s32 res, u32 cflags); |
ec9c02ad | 1113 | static void io_put_req(struct io_kiocb *req); |
91c2f697 | 1114 | static void io_put_req_deferred(struct io_kiocb *req); |
c7dae4ba | 1115 | static void io_dismantle_req(struct io_kiocb *req); |
94ae5e77 | 1116 | static void io_queue_linked_timeout(struct io_kiocb *req); |
fdecb662 | 1117 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 1118 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 | 1119 | unsigned nr_args); |
68fb8979 | 1120 | static void io_clean_op(struct io_kiocb *req); |
ac177053 | 1121 | static struct file *io_file_get(struct io_ring_ctx *ctx, |
8371adf5 | 1122 | struct io_kiocb *req, int fd, bool fixed); |
c5eef2b9 | 1123 | static void __io_queue_sqe(struct io_kiocb *req); |
269bbe5f | 1124 | static void io_rsrc_put_work(struct work_struct *work); |
de0617e4 | 1125 | |
907d1df3 | 1126 | static void io_req_task_queue(struct io_kiocb *req); |
c450178d | 1127 | static void __io_submit_flush_completions(struct io_ring_ctx *ctx); |
179ae0d1 | 1128 | static int io_req_prep_async(struct io_kiocb *req); |
de0617e4 | 1129 | |
b9445598 PB |
1130 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
1131 | unsigned int issue_flags, u32 slot_index); | |
7df778be PB |
1132 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags); |
1133 | ||
f1042b6c | 1134 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer); |
b9445598 | 1135 | |
2b188cc1 JA |
1136 | static struct kmem_cache *req_cachep; |
1137 | ||
0918682b | 1138 | static const struct file_operations io_uring_fops; |
2b188cc1 JA |
1139 | |
1140 | struct sock *io_uring_get_socket(struct file *file) | |
1141 | { | |
1142 | #if defined(CONFIG_UNIX) | |
1143 | if (file->f_op == &io_uring_fops) { | |
1144 | struct io_ring_ctx *ctx = file->private_data; | |
1145 | ||
1146 | return ctx->ring_sock->sk; | |
1147 | } | |
1148 | #endif | |
1149 | return NULL; | |
1150 | } | |
1151 | EXPORT_SYMBOL(io_uring_get_socket); | |
1152 | ||
f237c30a PB |
1153 | static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked) |
1154 | { | |
1155 | if (!*locked) { | |
1156 | mutex_lock(&ctx->uring_lock); | |
1157 | *locked = true; | |
1158 | } | |
1159 | } | |
1160 | ||
f2f87370 PB |
1161 | #define io_for_each_link(pos, head) \ |
1162 | for (pos = (head); pos; pos = pos->link) | |
1163 | ||
21c843d5 PB |
1164 | /* |
1165 | * Shamelessly stolen from the mm implementation of page reference checking, | |
1166 | * see commit f958d7b528b1 for details. | |
1167 | */ | |
1168 | #define req_ref_zero_or_close_to_overflow(req) \ | |
1169 | ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u) | |
1170 | ||
1171 | static inline bool req_ref_inc_not_zero(struct io_kiocb *req) | |
1172 | { | |
20e60a38 | 1173 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1174 | return atomic_inc_not_zero(&req->refs); |
1175 | } | |
1176 | ||
21c843d5 PB |
1177 | static inline bool req_ref_put_and_test(struct io_kiocb *req) |
1178 | { | |
20e60a38 PB |
1179 | if (likely(!(req->flags & REQ_F_REFCOUNT))) |
1180 | return true; | |
1181 | ||
21c843d5 PB |
1182 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1183 | return atomic_dec_and_test(&req->refs); | |
1184 | } | |
1185 | ||
1186 | static inline void req_ref_put(struct io_kiocb *req) | |
1187 | { | |
20e60a38 | 1188 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1189 | WARN_ON_ONCE(req_ref_put_and_test(req)); |
1190 | } | |
1191 | ||
1192 | static inline void req_ref_get(struct io_kiocb *req) | |
1193 | { | |
20e60a38 | 1194 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1195 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1196 | atomic_inc(&req->refs); | |
1197 | } | |
1198 | ||
c450178d PB |
1199 | static inline void io_submit_flush_completions(struct io_ring_ctx *ctx) |
1200 | { | |
6f33b0bc | 1201 | if (!wq_list_empty(&ctx->submit_state.compl_reqs)) |
c450178d PB |
1202 | __io_submit_flush_completions(ctx); |
1203 | } | |
1204 | ||
48dcd38d | 1205 | static inline void __io_req_set_refcount(struct io_kiocb *req, int nr) |
20e60a38 PB |
1206 | { |
1207 | if (!(req->flags & REQ_F_REFCOUNT)) { | |
1208 | req->flags |= REQ_F_REFCOUNT; | |
48dcd38d | 1209 | atomic_set(&req->refs, nr); |
20e60a38 PB |
1210 | } |
1211 | } | |
1212 | ||
48dcd38d PB |
1213 | static inline void io_req_set_refcount(struct io_kiocb *req) |
1214 | { | |
1215 | __io_req_set_refcount(req, 1); | |
1216 | } | |
1217 | ||
ab409402 PB |
1218 | #define IO_RSRC_REF_BATCH 100 |
1219 | ||
1220 | static inline void io_req_put_rsrc_locked(struct io_kiocb *req, | |
1221 | struct io_ring_ctx *ctx) | |
1222 | __must_hold(&ctx->uring_lock) | |
36f72fe2 | 1223 | { |
ab409402 PB |
1224 | struct percpu_ref *ref = req->fixed_rsrc_refs; |
1225 | ||
1226 | if (ref) { | |
1227 | if (ref == &ctx->rsrc_node->refs) | |
1228 | ctx->rsrc_cached_refs++; | |
1229 | else | |
1230 | percpu_ref_put(ref); | |
1231 | } | |
1232 | } | |
1233 | ||
1234 | static inline void io_req_put_rsrc(struct io_kiocb *req, struct io_ring_ctx *ctx) | |
1235 | { | |
1236 | if (req->fixed_rsrc_refs) | |
1237 | percpu_ref_put(req->fixed_rsrc_refs); | |
1238 | } | |
1239 | ||
1240 | static __cold void io_rsrc_refs_drop(struct io_ring_ctx *ctx) | |
1241 | __must_hold(&ctx->uring_lock) | |
1242 | { | |
1243 | if (ctx->rsrc_cached_refs) { | |
1244 | percpu_ref_put_many(&ctx->rsrc_node->refs, ctx->rsrc_cached_refs); | |
1245 | ctx->rsrc_cached_refs = 0; | |
1246 | } | |
1247 | } | |
1248 | ||
1249 | static void io_rsrc_refs_refill(struct io_ring_ctx *ctx) | |
1250 | __must_hold(&ctx->uring_lock) | |
1251 | { | |
1252 | ctx->rsrc_cached_refs += IO_RSRC_REF_BATCH; | |
1253 | percpu_ref_get_many(&ctx->rsrc_node->refs, IO_RSRC_REF_BATCH); | |
1254 | } | |
36f72fe2 | 1255 | |
a46be971 PB |
1256 | static inline void io_req_set_rsrc_node(struct io_kiocb *req, |
1257 | struct io_ring_ctx *ctx) | |
36f72fe2 | 1258 | { |
269bbe5f | 1259 | if (!req->fixed_rsrc_refs) { |
a7f0ed5a | 1260 | req->fixed_rsrc_refs = &ctx->rsrc_node->refs; |
ab409402 PB |
1261 | ctx->rsrc_cached_refs--; |
1262 | if (unlikely(ctx->rsrc_cached_refs < 0)) | |
1263 | io_rsrc_refs_refill(ctx); | |
36f72fe2 PB |
1264 | } |
1265 | } | |
1266 | ||
f70865db PB |
1267 | static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl) |
1268 | { | |
1269 | bool got = percpu_ref_tryget(ref); | |
1270 | ||
1271 | /* already at zero, wait for ->release() */ | |
1272 | if (!got) | |
1273 | wait_for_completion(compl); | |
1274 | percpu_ref_resurrect(ref); | |
1275 | if (got) | |
1276 | percpu_ref_put(ref); | |
1277 | } | |
1278 | ||
3dd0c97a PB |
1279 | static bool io_match_task(struct io_kiocb *head, struct task_struct *task, |
1280 | bool cancel_all) | |
08d23634 PB |
1281 | { |
1282 | struct io_kiocb *req; | |
1283 | ||
68207680 | 1284 | if (task && head->task != task) |
08d23634 | 1285 | return false; |
3dd0c97a | 1286 | if (cancel_all) |
08d23634 PB |
1287 | return true; |
1288 | ||
1289 | io_for_each_link(req, head) { | |
b05a1bcd | 1290 | if (req->flags & REQ_F_INFLIGHT) |
02a13674 | 1291 | return true; |
08d23634 PB |
1292 | } |
1293 | return false; | |
1294 | } | |
1295 | ||
d886e185 PB |
1296 | static inline bool req_has_async_data(struct io_kiocb *req) |
1297 | { | |
1298 | return req->flags & REQ_F_ASYNC_DATA; | |
1299 | } | |
1300 | ||
93d2bcd2 | 1301 | static inline void req_set_fail(struct io_kiocb *req) |
c40f6379 | 1302 | { |
93d2bcd2 | 1303 | req->flags |= REQ_F_FAIL; |
c40f6379 | 1304 | } |
4a38aed2 | 1305 | |
a8295b98 HX |
1306 | static inline void req_fail_link_node(struct io_kiocb *req, int res) |
1307 | { | |
1308 | req_set_fail(req); | |
1309 | req->result = res; | |
1310 | } | |
1311 | ||
c072481d | 1312 | static __cold void io_ring_ctx_ref_free(struct percpu_ref *ref) |
2b188cc1 JA |
1313 | { |
1314 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
1315 | ||
0f158b4c | 1316 | complete(&ctx->ref_comp); |
2b188cc1 JA |
1317 | } |
1318 | ||
8eb7e2d0 PB |
1319 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
1320 | { | |
1321 | return !req->timeout.off; | |
1322 | } | |
1323 | ||
c072481d | 1324 | static __cold void io_fallback_req_func(struct work_struct *work) |
f56165e6 PB |
1325 | { |
1326 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, | |
1327 | fallback_work.work); | |
1328 | struct llist_node *node = llist_del_all(&ctx->fallback_llist); | |
1329 | struct io_kiocb *req, *tmp; | |
f237c30a | 1330 | bool locked = false; |
f56165e6 PB |
1331 | |
1332 | percpu_ref_get(&ctx->refs); | |
1333 | llist_for_each_entry_safe(req, tmp, node, io_task_work.fallback_node) | |
f237c30a | 1334 | req->io_task_work.func(req, &locked); |
5636c00d | 1335 | |
f237c30a | 1336 | if (locked) { |
c450178d | 1337 | io_submit_flush_completions(ctx); |
f237c30a PB |
1338 | mutex_unlock(&ctx->uring_lock); |
1339 | } | |
f56165e6 PB |
1340 | percpu_ref_put(&ctx->refs); |
1341 | } | |
1342 | ||
c072481d | 1343 | static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
2b188cc1 JA |
1344 | { |
1345 | struct io_ring_ctx *ctx; | |
78076bb6 | 1346 | int hash_bits; |
2b188cc1 JA |
1347 | |
1348 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1349 | if (!ctx) | |
1350 | return NULL; | |
1351 | ||
78076bb6 JA |
1352 | /* |
1353 | * Use 5 bits less than the max cq entries, that should give us around | |
1354 | * 32 entries per hash list if totally full and uniformly spread. | |
1355 | */ | |
1356 | hash_bits = ilog2(p->cq_entries); | |
1357 | hash_bits -= 5; | |
1358 | if (hash_bits <= 0) | |
1359 | hash_bits = 1; | |
1360 | ctx->cancel_hash_bits = hash_bits; | |
1361 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
1362 | GFP_KERNEL); | |
1363 | if (!ctx->cancel_hash) | |
1364 | goto err; | |
1365 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
1366 | ||
6224843d PB |
1367 | ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL); |
1368 | if (!ctx->dummy_ubuf) | |
1369 | goto err; | |
1370 | /* set invalid range, so io_import_fixed() fails meeting it */ | |
1371 | ctx->dummy_ubuf->ubuf = -1UL; | |
1372 | ||
21482896 | 1373 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
1374 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
1375 | goto err; | |
2b188cc1 JA |
1376 | |
1377 | ctx->flags = p->flags; | |
90554200 | 1378 | init_waitqueue_head(&ctx->sqo_sq_wait); |
69fb2131 | 1379 | INIT_LIST_HEAD(&ctx->sqd_list); |
1d7bb1d5 | 1380 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
0f158b4c | 1381 | init_completion(&ctx->ref_comp); |
9e15c3a0 | 1382 | xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1); |
61cf9370 | 1383 | xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1); |
2b188cc1 | 1384 | mutex_init(&ctx->uring_lock); |
311997b3 | 1385 | init_waitqueue_head(&ctx->cq_wait); |
2b188cc1 | 1386 | spin_lock_init(&ctx->completion_lock); |
89850fce | 1387 | spin_lock_init(&ctx->timeout_lock); |
5eef4e87 | 1388 | INIT_WQ_LIST(&ctx->iopoll_list); |
de0617e4 | 1389 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 1390 | INIT_LIST_HEAD(&ctx->timeout_list); |
ef9dd637 | 1391 | INIT_LIST_HEAD(&ctx->ltimeout_list); |
d67d2263 BM |
1392 | spin_lock_init(&ctx->rsrc_ref_lock); |
1393 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); | |
269bbe5f BM |
1394 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
1395 | init_llist_head(&ctx->rsrc_put_llist); | |
13bf43f5 | 1396 | INIT_LIST_HEAD(&ctx->tctx_list); |
c2b6c6bc PB |
1397 | ctx->submit_state.free_list.next = NULL; |
1398 | INIT_WQ_LIST(&ctx->locked_free_list); | |
9011bf9a | 1399 | INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func); |
6f33b0bc | 1400 | INIT_WQ_LIST(&ctx->submit_state.compl_reqs); |
2b188cc1 | 1401 | return ctx; |
206aefde | 1402 | err: |
6224843d | 1403 | kfree(ctx->dummy_ubuf); |
78076bb6 | 1404 | kfree(ctx->cancel_hash); |
206aefde JA |
1405 | kfree(ctx); |
1406 | return NULL; | |
2b188cc1 JA |
1407 | } |
1408 | ||
8f6ed49a PB |
1409 | static void io_account_cq_overflow(struct io_ring_ctx *ctx) |
1410 | { | |
1411 | struct io_rings *r = ctx->rings; | |
1412 | ||
1413 | WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1); | |
1414 | ctx->cq_extra--; | |
1415 | } | |
1416 | ||
9cf7c104 | 1417 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
7adf4eaf | 1418 | { |
2bc9930e JA |
1419 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
1420 | struct io_ring_ctx *ctx = req->ctx; | |
a197f664 | 1421 | |
8f6ed49a | 1422 | return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail; |
2bc9930e | 1423 | } |
de0617e4 | 1424 | |
9d858b21 | 1425 | return false; |
de0617e4 JA |
1426 | } |
1427 | ||
35645ac3 PB |
1428 | #define FFS_NOWAIT 0x1UL |
1429 | #define FFS_ISREG 0x2UL | |
1430 | #define FFS_MASK ~(FFS_NOWAIT|FFS_ISREG) | |
c97d8a0f PB |
1431 | |
1432 | static inline bool io_req_ffs_set(struct io_kiocb *req) | |
1433 | { | |
35645ac3 | 1434 | return req->flags & REQ_F_FIXED_FILE; |
c97d8a0f PB |
1435 | } |
1436 | ||
c072481d | 1437 | static inline void io_req_track_inflight(struct io_kiocb *req) |
ce3d5aae | 1438 | { |
ce3d5aae | 1439 | if (!(req->flags & REQ_F_INFLIGHT)) { |
ce3d5aae | 1440 | req->flags |= REQ_F_INFLIGHT; |
b303fe2e | 1441 | atomic_inc(¤t->io_uring->inflight_tracked); |
ce3d5aae PB |
1442 | } |
1443 | } | |
1444 | ||
fd08e530 PB |
1445 | static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req) |
1446 | { | |
906c6caa PB |
1447 | if (WARN_ON_ONCE(!req->link)) |
1448 | return NULL; | |
1449 | ||
4d13d1a4 PB |
1450 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
1451 | req->flags |= REQ_F_LINK_TIMEOUT; | |
fd08e530 PB |
1452 | |
1453 | /* linked timeouts should have two refs once prep'ed */ | |
48dcd38d | 1454 | io_req_set_refcount(req); |
4d13d1a4 PB |
1455 | __io_req_set_refcount(req->link, 2); |
1456 | return req->link; | |
fd08e530 PB |
1457 | } |
1458 | ||
1459 | static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) | |
1460 | { | |
4d13d1a4 | 1461 | if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT))) |
fd08e530 PB |
1462 | return NULL; |
1463 | return __io_prep_linked_timeout(req); | |
1464 | } | |
1465 | ||
1e6fa521 JA |
1466 | static void io_prep_async_work(struct io_kiocb *req) |
1467 | { | |
1468 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
1e6fa521 JA |
1469 | struct io_ring_ctx *ctx = req->ctx; |
1470 | ||
b8e64b53 PB |
1471 | if (!(req->flags & REQ_F_CREDS)) { |
1472 | req->flags |= REQ_F_CREDS; | |
c10d1f98 | 1473 | req->creds = get_current_cred(); |
b8e64b53 | 1474 | } |
003e8dcc | 1475 | |
e1d675df PB |
1476 | req->work.list.next = NULL; |
1477 | req->work.flags = 0; | |
feaadc4f PB |
1478 | if (req->flags & REQ_F_FORCE_ASYNC) |
1479 | req->work.flags |= IO_WQ_WORK_CONCURRENT; | |
1480 | ||
1e6fa521 JA |
1481 | if (req->flags & REQ_F_ISREG) { |
1482 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) | |
1483 | io_wq_hash_work(&req->work, file_inode(req->file)); | |
4b982bd0 | 1484 | } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) { |
1e6fa521 JA |
1485 | if (def->unbound_nonreg_file) |
1486 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1487 | } | |
e1d675df PB |
1488 | |
1489 | switch (req->opcode) { | |
1490 | case IORING_OP_SPLICE: | |
1491 | case IORING_OP_TEE: | |
e1d675df PB |
1492 | if (!S_ISREG(file_inode(req->splice.file_in)->i_mode)) |
1493 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1494 | break; | |
1495 | } | |
561fb04a | 1496 | } |
cccf0ee8 | 1497 | |
cbdcb435 | 1498 | static void io_prep_async_link(struct io_kiocb *req) |
561fb04a | 1499 | { |
cbdcb435 | 1500 | struct io_kiocb *cur; |
54a91f3b | 1501 | |
44eff40a PB |
1502 | if (req->flags & REQ_F_LINK_TIMEOUT) { |
1503 | struct io_ring_ctx *ctx = req->ctx; | |
1504 | ||
79ebeaee | 1505 | spin_lock(&ctx->completion_lock); |
44eff40a PB |
1506 | io_for_each_link(cur, req) |
1507 | io_prep_async_work(cur); | |
79ebeaee | 1508 | spin_unlock(&ctx->completion_lock); |
44eff40a PB |
1509 | } else { |
1510 | io_for_each_link(cur, req) | |
1511 | io_prep_async_work(cur); | |
1512 | } | |
561fb04a JA |
1513 | } |
1514 | ||
fff4e40e PB |
1515 | static inline void io_req_add_compl_list(struct io_kiocb *req) |
1516 | { | |
1517 | struct io_submit_state *state = &req->ctx->submit_state; | |
1518 | ||
1519 | wq_list_add_tail(&req->comp_list, &state->compl_reqs); | |
1520 | } | |
1521 | ||
00169246 | 1522 | static void io_queue_async_work(struct io_kiocb *req, bool *dont_use) |
561fb04a | 1523 | { |
a197f664 | 1524 | struct io_ring_ctx *ctx = req->ctx; |
cbdcb435 | 1525 | struct io_kiocb *link = io_prep_linked_timeout(req); |
5aa75ed5 | 1526 | struct io_uring_task *tctx = req->task->io_uring; |
561fb04a | 1527 | |
3bfe6106 JA |
1528 | BUG_ON(!tctx); |
1529 | BUG_ON(!tctx->io_wq); | |
561fb04a | 1530 | |
cbdcb435 PB |
1531 | /* init ->work of the whole link before punting */ |
1532 | io_prep_async_link(req); | |
991468dc JA |
1533 | |
1534 | /* | |
1535 | * Not expected to happen, but if we do have a bug where this _can_ | |
1536 | * happen, catch it here and ensure the request is marked as | |
1537 | * canceled. That will make io-wq go through the usual work cancel | |
1538 | * procedure rather than attempt to run this request (or create a new | |
1539 | * worker for it). | |
1540 | */ | |
1541 | if (WARN_ON_ONCE(!same_thread_group(req->task, current))) | |
1542 | req->work.flags |= IO_WQ_WORK_CANCEL; | |
1543 | ||
d07f1e8a PB |
1544 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
1545 | &req->work, req->flags); | |
ebf93667 | 1546 | io_wq_enqueue(tctx->io_wq, &req->work); |
7271ef3a JA |
1547 | if (link) |
1548 | io_queue_linked_timeout(link); | |
cbdcb435 PB |
1549 | } |
1550 | ||
1ee4160c | 1551 | static void io_kill_timeout(struct io_kiocb *req, int status) |
8c855885 | 1552 | __must_hold(&req->ctx->completion_lock) |
89850fce | 1553 | __must_hold(&req->ctx->timeout_lock) |
5262f567 | 1554 | { |
e8c2bc1f | 1555 | struct io_timeout_data *io = req->async_data; |
5262f567 | 1556 | |
fd9c7bc5 | 1557 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
2ae2eb9d PB |
1558 | if (status) |
1559 | req_set_fail(req); | |
01cec8c1 PB |
1560 | atomic_set(&req->ctx->cq_timeouts, |
1561 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
135fcde8 | 1562 | list_del_init(&req->timeout.list); |
d4d19c19 | 1563 | io_cqring_fill_event(req->ctx, req->user_data, status, 0); |
91c2f697 | 1564 | io_put_req_deferred(req); |
5262f567 JA |
1565 | } |
1566 | } | |
1567 | ||
c072481d | 1568 | static __cold void io_queue_deferred(struct io_ring_ctx *ctx) |
de0617e4 | 1569 | { |
441b8a78 | 1570 | while (!list_empty(&ctx->defer_list)) { |
27dc8338 PB |
1571 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
1572 | struct io_defer_entry, list); | |
de0617e4 | 1573 | |
9cf7c104 | 1574 | if (req_need_defer(de->req, de->seq)) |
04518945 | 1575 | break; |
27dc8338 | 1576 | list_del_init(&de->list); |
907d1df3 | 1577 | io_req_task_queue(de->req); |
27dc8338 | 1578 | kfree(de); |
441b8a78 | 1579 | } |
04518945 PB |
1580 | } |
1581 | ||
c072481d | 1582 | static __cold void io_flush_timeouts(struct io_ring_ctx *ctx) |
89850fce | 1583 | __must_hold(&ctx->completion_lock) |
de0617e4 | 1584 | { |
441b8a78 | 1585 | u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
f010505b | 1586 | |
79ebeaee | 1587 | spin_lock_irq(&ctx->timeout_lock); |
f18ee4cf | 1588 | while (!list_empty(&ctx->timeout_list)) { |
f010505b | 1589 | u32 events_needed, events_got; |
360428f8 | 1590 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
135fcde8 | 1591 | struct io_kiocb, timeout.list); |
de0617e4 | 1592 | |
8eb7e2d0 | 1593 | if (io_is_timeout_noseq(req)) |
360428f8 | 1594 | break; |
f010505b MDG |
1595 | |
1596 | /* | |
1597 | * Since seq can easily wrap around over time, subtract | |
1598 | * the last seq at which timeouts were flushed before comparing. | |
1599 | * Assuming not more than 2^31-1 events have happened since, | |
1600 | * these subtractions won't have wrapped, so we can check if | |
1601 | * target is in [last_seq, current_seq] by comparing the two. | |
1602 | */ | |
1603 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; | |
1604 | events_got = seq - ctx->cq_last_tm_flush; | |
1605 | if (events_got < events_needed) | |
360428f8 | 1606 | break; |
bfe68a22 | 1607 | |
135fcde8 | 1608 | list_del_init(&req->timeout.list); |
1ee4160c | 1609 | io_kill_timeout(req, 0); |
f18ee4cf | 1610 | } |
f010505b | 1611 | ctx->cq_last_tm_flush = seq; |
79ebeaee | 1612 | spin_unlock_irq(&ctx->timeout_lock); |
360428f8 | 1613 | } |
5262f567 | 1614 | |
c072481d | 1615 | static __cold void __io_commit_cqring_flush(struct io_ring_ctx *ctx) |
360428f8 | 1616 | { |
2335f6f5 PB |
1617 | if (ctx->off_timeout_used) |
1618 | io_flush_timeouts(ctx); | |
1619 | if (ctx->drain_active) | |
1620 | io_queue_deferred(ctx); | |
1621 | } | |
1622 | ||
1623 | static inline void io_commit_cqring(struct io_ring_ctx *ctx) | |
1624 | { | |
1625 | if (unlikely(ctx->off_timeout_used || ctx->drain_active)) | |
1626 | __io_commit_cqring_flush(ctx); | |
ec30e04b PB |
1627 | /* order cqe stores with ring update */ |
1628 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); | |
de0617e4 JA |
1629 | } |
1630 | ||
90554200 JA |
1631 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
1632 | { | |
1633 | struct io_rings *r = ctx->rings; | |
1634 | ||
a566c556 | 1635 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries; |
90554200 JA |
1636 | } |
1637 | ||
888aae2e PB |
1638 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
1639 | { | |
1640 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); | |
1641 | } | |
1642 | ||
d068b506 | 1643 | static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx) |
2b188cc1 | 1644 | { |
75b28aff | 1645 | struct io_rings *rings = ctx->rings; |
ea5ab3b5 | 1646 | unsigned tail, mask = ctx->cq_entries - 1; |
2b188cc1 | 1647 | |
115e12e5 SB |
1648 | /* |
1649 | * writes to the cq entry need to come after reading head; the | |
1650 | * control dependency is enough as we're using WRITE_ONCE to | |
1651 | * fill the cq entry | |
1652 | */ | |
a566c556 | 1653 | if (__io_cqring_events(ctx) == ctx->cq_entries) |
2b188cc1 JA |
1654 | return NULL; |
1655 | ||
888aae2e | 1656 | tail = ctx->cached_cq_tail++; |
ea5ab3b5 | 1657 | return &rings->cqes[tail & mask]; |
2b188cc1 JA |
1658 | } |
1659 | ||
f2842ab5 JA |
1660 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
1661 | { | |
44c769de | 1662 | if (likely(!ctx->cq_ev_fd)) |
f0b493e6 | 1663 | return false; |
7e55a19c SG |
1664 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
1665 | return false; | |
44c769de | 1666 | return !ctx->eventfd_async || io_wq_current_is_worker(); |
f2842ab5 JA |
1667 | } |
1668 | ||
2c5d763c JA |
1669 | /* |
1670 | * This should only get called when at least one event has been posted. | |
1671 | * Some applications rely on the eventfd notification count only changing | |
1672 | * IFF a new CQE has been added to the CQ ring. There's no depedency on | |
1673 | * 1:1 relationship between how many times this function is called (and | |
1674 | * hence the eventfd count) and number of CQEs posted to the CQ ring. | |
1675 | */ | |
b41e9852 | 1676 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
1d7bb1d5 | 1677 | { |
5fd46178 JA |
1678 | /* |
1679 | * wake_up_all() may seem excessive, but io_wake_function() and | |
1680 | * io_should_wake() handle the termination of the loop and only | |
1681 | * wake as many waiters as we need to. | |
1682 | */ | |
1683 | if (wq_has_sleeper(&ctx->cq_wait)) | |
1684 | wake_up_all(&ctx->cq_wait); | |
b41e9852 | 1685 | if (io_should_trigger_evfd(ctx)) |
1d7bb1d5 JA |
1686 | eventfd_signal(ctx->cq_ev_fd, 1); |
1687 | } | |
1688 | ||
80c18e4a PB |
1689 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
1690 | { | |
c57a91fb PB |
1691 | /* see waitqueue_active() comment */ |
1692 | smp_mb(); | |
1693 | ||
80c18e4a | 1694 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
c57a91fb | 1695 | if (waitqueue_active(&ctx->cq_wait)) |
5fd46178 | 1696 | wake_up_all(&ctx->cq_wait); |
80c18e4a PB |
1697 | } |
1698 | if (io_should_trigger_evfd(ctx)) | |
1699 | eventfd_signal(ctx->cq_ev_fd, 1); | |
1700 | } | |
1701 | ||
c4a2ed72 | 1702 | /* Returns true if there are no backlogged entries after the flush */ |
6c2450ae | 1703 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force) |
1d7bb1d5 | 1704 | { |
b18032bb | 1705 | bool all_flushed, posted; |
1d7bb1d5 | 1706 | |
a566c556 | 1707 | if (!force && __io_cqring_events(ctx) == ctx->cq_entries) |
e23de15f | 1708 | return false; |
1d7bb1d5 | 1709 | |
b18032bb | 1710 | posted = false; |
79ebeaee | 1711 | spin_lock(&ctx->completion_lock); |
6c2450ae | 1712 | while (!list_empty(&ctx->cq_overflow_list)) { |
d068b506 | 1713 | struct io_uring_cqe *cqe = io_get_cqe(ctx); |
6c2450ae | 1714 | struct io_overflow_cqe *ocqe; |
e6c8aa9a | 1715 | |
1d7bb1d5 JA |
1716 | if (!cqe && !force) |
1717 | break; | |
6c2450ae PB |
1718 | ocqe = list_first_entry(&ctx->cq_overflow_list, |
1719 | struct io_overflow_cqe, list); | |
1720 | if (cqe) | |
1721 | memcpy(cqe, &ocqe->cqe, sizeof(*cqe)); | |
1722 | else | |
8f6ed49a PB |
1723 | io_account_cq_overflow(ctx); |
1724 | ||
b18032bb | 1725 | posted = true; |
6c2450ae PB |
1726 | list_del(&ocqe->list); |
1727 | kfree(ocqe); | |
1d7bb1d5 JA |
1728 | } |
1729 | ||
09e88404 PB |
1730 | all_flushed = list_empty(&ctx->cq_overflow_list); |
1731 | if (all_flushed) { | |
5ed7a37d | 1732 | clear_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
1733 | WRITE_ONCE(ctx->rings->sq_flags, |
1734 | ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW); | |
09e88404 | 1735 | } |
46930143 | 1736 | |
b18032bb JA |
1737 | if (posted) |
1738 | io_commit_cqring(ctx); | |
79ebeaee | 1739 | spin_unlock(&ctx->completion_lock); |
b18032bb JA |
1740 | if (posted) |
1741 | io_cqring_ev_posted(ctx); | |
09e88404 | 1742 | return all_flushed; |
1d7bb1d5 JA |
1743 | } |
1744 | ||
90f67366 | 1745 | static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx) |
6c503150 | 1746 | { |
ca0a2651 JA |
1747 | bool ret = true; |
1748 | ||
5ed7a37d | 1749 | if (test_bit(0, &ctx->check_cq_overflow)) { |
6c503150 PB |
1750 | /* iopoll syncs against uring_lock, not completion_lock */ |
1751 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1752 | mutex_lock(&ctx->uring_lock); | |
90f67366 | 1753 | ret = __io_cqring_overflow_flush(ctx, false); |
6c503150 PB |
1754 | if (ctx->flags & IORING_SETUP_IOPOLL) |
1755 | mutex_unlock(&ctx->uring_lock); | |
1756 | } | |
ca0a2651 JA |
1757 | |
1758 | return ret; | |
6c503150 PB |
1759 | } |
1760 | ||
6a290a14 PB |
1761 | /* must to be called somewhat shortly after putting a request */ |
1762 | static inline void io_put_task(struct task_struct *task, int nr) | |
1763 | { | |
1764 | struct io_uring_task *tctx = task->io_uring; | |
1765 | ||
e98e49b2 PB |
1766 | if (likely(task == current)) { |
1767 | tctx->cached_refs += nr; | |
1768 | } else { | |
1769 | percpu_counter_sub(&tctx->inflight, nr); | |
1770 | if (unlikely(atomic_read(&tctx->in_idle))) | |
1771 | wake_up(&tctx->wait); | |
1772 | put_task_struct_many(task, nr); | |
1773 | } | |
6a290a14 PB |
1774 | } |
1775 | ||
9a10867a PB |
1776 | static void io_task_refs_refill(struct io_uring_task *tctx) |
1777 | { | |
1778 | unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR; | |
1779 | ||
1780 | percpu_counter_add(&tctx->inflight, refill); | |
1781 | refcount_add(refill, ¤t->usage); | |
1782 | tctx->cached_refs += refill; | |
1783 | } | |
1784 | ||
1785 | static inline void io_get_task_refs(int nr) | |
1786 | { | |
1787 | struct io_uring_task *tctx = current->io_uring; | |
1788 | ||
1789 | tctx->cached_refs -= nr; | |
1790 | if (unlikely(tctx->cached_refs < 0)) | |
1791 | io_task_refs_refill(tctx); | |
1792 | } | |
1793 | ||
d4d19c19 | 1794 | static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data, |
54daa9b2 | 1795 | s32 res, u32 cflags) |
2b188cc1 | 1796 | { |
cce4b8b0 | 1797 | struct io_overflow_cqe *ocqe; |
2b188cc1 | 1798 | |
cce4b8b0 PB |
1799 | ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT); |
1800 | if (!ocqe) { | |
1801 | /* | |
1802 | * If we're in ring overflow flush mode, or in task cancel mode, | |
1803 | * or cannot allocate an overflow entry, then we need to drop it | |
1804 | * on the floor. | |
1805 | */ | |
8f6ed49a | 1806 | io_account_cq_overflow(ctx); |
cce4b8b0 | 1807 | return false; |
2b188cc1 | 1808 | } |
cce4b8b0 | 1809 | if (list_empty(&ctx->cq_overflow_list)) { |
5ed7a37d | 1810 | set_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
1811 | WRITE_ONCE(ctx->rings->sq_flags, |
1812 | ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW); | |
1813 | ||
cce4b8b0 | 1814 | } |
d4d19c19 | 1815 | ocqe->cqe.user_data = user_data; |
cce4b8b0 PB |
1816 | ocqe->cqe.res = res; |
1817 | ocqe->cqe.flags = cflags; | |
1818 | list_add_tail(&ocqe->list, &ctx->cq_overflow_list); | |
1819 | return true; | |
2b188cc1 JA |
1820 | } |
1821 | ||
d4d19c19 | 1822 | static inline bool __io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
54daa9b2 | 1823 | s32 res, u32 cflags) |
2b188cc1 JA |
1824 | { |
1825 | struct io_uring_cqe *cqe; | |
1826 | ||
d4d19c19 | 1827 | trace_io_uring_complete(ctx, user_data, res, cflags); |
51c3ff62 | 1828 | |
2b188cc1 JA |
1829 | /* |
1830 | * If we can't get a cq entry, userspace overflowed the | |
1831 | * submission (by quite a lot). Increment the overflow count in | |
1832 | * the ring. | |
1833 | */ | |
d068b506 | 1834 | cqe = io_get_cqe(ctx); |
1d7bb1d5 | 1835 | if (likely(cqe)) { |
d4d19c19 | 1836 | WRITE_ONCE(cqe->user_data, user_data); |
2b188cc1 | 1837 | WRITE_ONCE(cqe->res, res); |
bcda7baa | 1838 | WRITE_ONCE(cqe->flags, cflags); |
8d13326e | 1839 | return true; |
2b188cc1 | 1840 | } |
d4d19c19 | 1841 | return io_cqring_event_overflow(ctx, user_data, res, cflags); |
2b188cc1 JA |
1842 | } |
1843 | ||
8d13326e | 1844 | /* not as hot to bloat with inlining */ |
d4d19c19 | 1845 | static noinline bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
54daa9b2 | 1846 | s32 res, u32 cflags) |
bcda7baa | 1847 | { |
d4d19c19 | 1848 | return __io_cqring_fill_event(ctx, user_data, res, cflags); |
bcda7baa JA |
1849 | } |
1850 | ||
54daa9b2 PB |
1851 | static void io_req_complete_post(struct io_kiocb *req, s32 res, |
1852 | u32 cflags) | |
2b188cc1 | 1853 | { |
78e19bbe | 1854 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 | 1855 | |
79ebeaee | 1856 | spin_lock(&ctx->completion_lock); |
d4d19c19 | 1857 | __io_cqring_fill_event(ctx, req->user_data, res, cflags); |
c7dae4ba JA |
1858 | /* |
1859 | * If we're the last reference to this request, add to our locked | |
1860 | * free_list cache. | |
1861 | */ | |
de9b4cca | 1862 | if (req_ref_put_and_test(req)) { |
7a612350 | 1863 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
0756a869 | 1864 | if (req->flags & IO_DISARM_MASK) |
7a612350 PB |
1865 | io_disarm_next(req); |
1866 | if (req->link) { | |
1867 | io_req_task_queue(req->link); | |
1868 | req->link = NULL; | |
1869 | } | |
1870 | } | |
ab409402 | 1871 | io_req_put_rsrc(req, ctx); |
c7dae4ba JA |
1872 | io_dismantle_req(req); |
1873 | io_put_task(req->task, 1); | |
c2b6c6bc | 1874 | wq_list_add_head(&req->comp_list, &ctx->locked_free_list); |
d0acdee2 | 1875 | ctx->locked_free_nr++; |
180f829f | 1876 | } |
7a612350 | 1877 | io_commit_cqring(ctx); |
79ebeaee | 1878 | spin_unlock(&ctx->completion_lock); |
a3f34907 | 1879 | io_cqring_ev_posted(ctx); |
4e3d9ff9 JA |
1880 | } |
1881 | ||
54daa9b2 PB |
1882 | static inline void io_req_complete_state(struct io_kiocb *req, s32 res, |
1883 | u32 cflags) | |
229a7b63 | 1884 | { |
a38d68db | 1885 | req->result = res; |
d17e56eb | 1886 | req->cflags = cflags; |
e342c807 | 1887 | req->flags |= REQ_F_COMPLETE_INLINE; |
e1e16097 JA |
1888 | } |
1889 | ||
889fca73 | 1890 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
54daa9b2 | 1891 | s32 res, u32 cflags) |
bcda7baa | 1892 | { |
889fca73 PB |
1893 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
1894 | io_req_complete_state(req, res, cflags); | |
a38d68db | 1895 | else |
c7dae4ba | 1896 | io_req_complete_post(req, res, cflags); |
bcda7baa JA |
1897 | } |
1898 | ||
54daa9b2 | 1899 | static inline void io_req_complete(struct io_kiocb *req, s32 res) |
0ddf92e8 | 1900 | { |
889fca73 | 1901 | __io_req_complete(req, 0, res, 0); |
0ddf92e8 JA |
1902 | } |
1903 | ||
54daa9b2 | 1904 | static void io_req_complete_failed(struct io_kiocb *req, s32 res) |
f41db273 | 1905 | { |
93d2bcd2 | 1906 | req_set_fail(req); |
f41db273 PB |
1907 | io_req_complete_post(req, res, 0); |
1908 | } | |
1909 | ||
c6d3d9cb PB |
1910 | static void io_req_complete_fail_submit(struct io_kiocb *req) |
1911 | { | |
1912 | /* | |
1913 | * We don't submit, fail them all, for that replace hardlinks with | |
1914 | * normal links. Extra REQ_F_LINK is tolerated. | |
1915 | */ | |
1916 | req->flags &= ~REQ_F_HARDLINK; | |
1917 | req->flags |= REQ_F_LINK; | |
1918 | io_req_complete_failed(req, req->result); | |
1919 | } | |
1920 | ||
864ea921 PB |
1921 | /* |
1922 | * Don't initialise the fields below on every allocation, but do that in | |
1923 | * advance and keep them valid across allocations. | |
1924 | */ | |
1925 | static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx) | |
1926 | { | |
1927 | req->ctx = ctx; | |
1928 | req->link = NULL; | |
1929 | req->async_data = NULL; | |
1930 | /* not necessary, but safer to zero */ | |
1931 | req->result = 0; | |
1932 | } | |
1933 | ||
dac7a098 | 1934 | static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx, |
cd0ca2e0 | 1935 | struct io_submit_state *state) |
dac7a098 | 1936 | { |
79ebeaee | 1937 | spin_lock(&ctx->completion_lock); |
c2b6c6bc | 1938 | wq_list_splice(&ctx->locked_free_list, &state->free_list); |
d0acdee2 | 1939 | ctx->locked_free_nr = 0; |
79ebeaee | 1940 | spin_unlock(&ctx->completion_lock); |
dac7a098 PB |
1941 | } |
1942 | ||
dd78f492 | 1943 | /* Returns true IFF there are requests in the cache */ |
c7dae4ba | 1944 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
0ddf92e8 | 1945 | { |
c7dae4ba | 1946 | struct io_submit_state *state = &ctx->submit_state; |
0ddf92e8 | 1947 | |
c7dae4ba JA |
1948 | /* |
1949 | * If we have more than a batch's worth of requests in our IRQ side | |
1950 | * locked cache, grab the lock and move them over to our submission | |
1951 | * side cache. | |
1952 | */ | |
d0acdee2 | 1953 | if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH) |
cd0ca2e0 | 1954 | io_flush_cached_locked_reqs(ctx, state); |
c2b6c6bc | 1955 | return !!state->free_list.next; |
0ddf92e8 JA |
1956 | } |
1957 | ||
5d5901a3 PB |
1958 | /* |
1959 | * A request might get retired back into the request caches even before opcode | |
1960 | * handlers and io_issue_sqe() are done with it, e.g. inline completion path. | |
1961 | * Because of that, io_alloc_req() should be called only under ->uring_lock | |
1962 | * and with extra caution to not get a request that is still worked on. | |
1963 | */ | |
c072481d | 1964 | static __cold bool __io_alloc_req_refill(struct io_ring_ctx *ctx) |
5d5901a3 | 1965 | __must_hold(&ctx->uring_lock) |
2b188cc1 | 1966 | { |
e5d1bc0a | 1967 | struct io_submit_state *state = &ctx->submit_state; |
864ea921 | 1968 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
3ab665b7 PB |
1969 | void *reqs[IO_REQ_ALLOC_BATCH]; |
1970 | struct io_kiocb *req; | |
864ea921 | 1971 | int ret, i; |
e5d1bc0a | 1972 | |
c2b6c6bc | 1973 | if (likely(state->free_list.next || io_flush_cached_reqs(ctx))) |
a33ae9ce | 1974 | return true; |
e5d1bc0a | 1975 | |
3ab665b7 | 1976 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, ARRAY_SIZE(reqs), reqs); |
fd6fab2c | 1977 | |
864ea921 PB |
1978 | /* |
1979 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
1980 | * retry single alloc to be on the safe side. | |
1981 | */ | |
1982 | if (unlikely(ret <= 0)) { | |
3ab665b7 PB |
1983 | reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
1984 | if (!reqs[0]) | |
a33ae9ce | 1985 | return false; |
864ea921 | 1986 | ret = 1; |
2b188cc1 | 1987 | } |
864ea921 | 1988 | |
37f0e767 | 1989 | percpu_ref_get_many(&ctx->refs, ret); |
3ab665b7 PB |
1990 | for (i = 0; i < ret; i++) { |
1991 | req = reqs[i]; | |
1992 | ||
1993 | io_preinit_req(req, ctx); | |
c2b6c6bc | 1994 | wq_stack_add_head(&req->comp_list, &state->free_list); |
3ab665b7 | 1995 | } |
a33ae9ce PB |
1996 | return true; |
1997 | } | |
1998 | ||
1999 | static inline bool io_alloc_req_refill(struct io_ring_ctx *ctx) | |
2000 | { | |
2001 | if (unlikely(!ctx->submit_state.free_list.next)) | |
2002 | return __io_alloc_req_refill(ctx); | |
2003 | return true; | |
2004 | } | |
2005 | ||
2006 | static inline struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) | |
2007 | { | |
2008 | struct io_wq_work_node *node; | |
2009 | ||
2010 | node = wq_stack_extract(&ctx->submit_state.free_list); | |
c2b6c6bc | 2011 | return container_of(node, struct io_kiocb, comp_list); |
2b188cc1 JA |
2012 | } |
2013 | ||
e1d767f0 | 2014 | static inline void io_put_file(struct file *file) |
8da11c19 | 2015 | { |
e1d767f0 | 2016 | if (file) |
8da11c19 PB |
2017 | fput(file); |
2018 | } | |
2019 | ||
6b639522 | 2020 | static inline void io_dismantle_req(struct io_kiocb *req) |
2b188cc1 | 2021 | { |
094bae49 | 2022 | unsigned int flags = req->flags; |
929a3af9 | 2023 | |
867f8fa5 | 2024 | if (unlikely(flags & IO_REQ_CLEAN_FLAGS)) |
3a0a6902 | 2025 | io_clean_op(req); |
e1d767f0 PB |
2026 | if (!(flags & REQ_F_FIXED_FILE)) |
2027 | io_put_file(req->file); | |
e65ef56d JA |
2028 | } |
2029 | ||
c072481d | 2030 | static __cold void __io_free_req(struct io_kiocb *req) |
c6ca97b3 | 2031 | { |
51a4cc11 | 2032 | struct io_ring_ctx *ctx = req->ctx; |
c6ca97b3 | 2033 | |
ab409402 | 2034 | io_req_put_rsrc(req, ctx); |
216578e5 | 2035 | io_dismantle_req(req); |
7c660731 | 2036 | io_put_task(req->task, 1); |
c6ca97b3 | 2037 | |
79ebeaee | 2038 | spin_lock(&ctx->completion_lock); |
c2b6c6bc | 2039 | wq_list_add_head(&req->comp_list, &ctx->locked_free_list); |
c34b025f | 2040 | ctx->locked_free_nr++; |
79ebeaee | 2041 | spin_unlock(&ctx->completion_lock); |
e65ef56d JA |
2042 | } |
2043 | ||
f2f87370 PB |
2044 | static inline void io_remove_next_linked(struct io_kiocb *req) |
2045 | { | |
2046 | struct io_kiocb *nxt = req->link; | |
2047 | ||
2048 | req->link = nxt->link; | |
2049 | nxt->link = NULL; | |
2050 | } | |
2051 | ||
33cc89a9 PB |
2052 | static bool io_kill_linked_timeout(struct io_kiocb *req) |
2053 | __must_hold(&req->ctx->completion_lock) | |
89b263f6 | 2054 | __must_hold(&req->ctx->timeout_lock) |
2665abfd | 2055 | { |
33cc89a9 | 2056 | struct io_kiocb *link = req->link; |
f2f87370 | 2057 | |
b97e736a | 2058 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
c9abd7ad | 2059 | struct io_timeout_data *io = link->async_data; |
7c86ffee | 2060 | |
f2f87370 | 2061 | io_remove_next_linked(req); |
90cd7e42 | 2062 | link->timeout.head = NULL; |
fd9c7bc5 | 2063 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
ef9dd637 | 2064 | list_del(&link->timeout.list); |
d4d19c19 PB |
2065 | io_cqring_fill_event(link->ctx, link->user_data, |
2066 | -ECANCELED, 0); | |
91c2f697 | 2067 | io_put_req_deferred(link); |
d4729fbd | 2068 | return true; |
c9abd7ad PB |
2069 | } |
2070 | } | |
d4729fbd | 2071 | return false; |
7c86ffee PB |
2072 | } |
2073 | ||
d148ca4b | 2074 | static void io_fail_links(struct io_kiocb *req) |
33cc89a9 | 2075 | __must_hold(&req->ctx->completion_lock) |
9e645e11 | 2076 | { |
33cc89a9 | 2077 | struct io_kiocb *nxt, *link = req->link; |
9e645e11 | 2078 | |
f2f87370 | 2079 | req->link = NULL; |
f2f87370 | 2080 | while (link) { |
a8295b98 HX |
2081 | long res = -ECANCELED; |
2082 | ||
2083 | if (link->flags & REQ_F_FAIL) | |
2084 | res = link->result; | |
2085 | ||
f2f87370 PB |
2086 | nxt = link->link; |
2087 | link->link = NULL; | |
2665abfd | 2088 | |
f2f87370 | 2089 | trace_io_uring_fail_link(req, link); |
a8295b98 | 2090 | io_cqring_fill_event(link->ctx, link->user_data, res, 0); |
91c2f697 | 2091 | io_put_req_deferred(link); |
f2f87370 | 2092 | link = nxt; |
9e645e11 | 2093 | } |
33cc89a9 | 2094 | } |
9e645e11 | 2095 | |
33cc89a9 PB |
2096 | static bool io_disarm_next(struct io_kiocb *req) |
2097 | __must_hold(&req->ctx->completion_lock) | |
2098 | { | |
2099 | bool posted = false; | |
2100 | ||
0756a869 PB |
2101 | if (req->flags & REQ_F_ARM_LTIMEOUT) { |
2102 | struct io_kiocb *link = req->link; | |
2103 | ||
906c6caa | 2104 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
0756a869 PB |
2105 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
2106 | io_remove_next_linked(req); | |
2107 | io_cqring_fill_event(link->ctx, link->user_data, | |
2108 | -ECANCELED, 0); | |
2109 | io_put_req_deferred(link); | |
2110 | posted = true; | |
2111 | } | |
2112 | } else if (req->flags & REQ_F_LINK_TIMEOUT) { | |
89b263f6 JA |
2113 | struct io_ring_ctx *ctx = req->ctx; |
2114 | ||
2115 | spin_lock_irq(&ctx->timeout_lock); | |
33cc89a9 | 2116 | posted = io_kill_linked_timeout(req); |
89b263f6 JA |
2117 | spin_unlock_irq(&ctx->timeout_lock); |
2118 | } | |
93d2bcd2 | 2119 | if (unlikely((req->flags & REQ_F_FAIL) && |
e4335ed3 | 2120 | !(req->flags & REQ_F_HARDLINK))) { |
33cc89a9 PB |
2121 | posted |= (req->link != NULL); |
2122 | io_fail_links(req); | |
2123 | } | |
2124 | return posted; | |
9e645e11 JA |
2125 | } |
2126 | ||
d81499bf PB |
2127 | static void __io_req_find_next_prep(struct io_kiocb *req) |
2128 | { | |
2129 | struct io_ring_ctx *ctx = req->ctx; | |
2130 | bool posted; | |
2131 | ||
2132 | spin_lock(&ctx->completion_lock); | |
2133 | posted = io_disarm_next(req); | |
2134 | if (posted) | |
2135 | io_commit_cqring(req->ctx); | |
2136 | spin_unlock(&ctx->completion_lock); | |
2137 | if (posted) | |
2138 | io_cqring_ev_posted(ctx); | |
2139 | } | |
2140 | ||
2141 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) | |
c69f8dbe | 2142 | { |
33cc89a9 | 2143 | struct io_kiocb *nxt; |
944e58bf | 2144 | |
d81499bf PB |
2145 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
2146 | return NULL; | |
9e645e11 JA |
2147 | /* |
2148 | * If LINK is set, we have dependent requests in this chain. If we | |
2149 | * didn't fail this request, queue the first one up, moving any other | |
2150 | * dependencies to the next request. In case of failure, fail the rest | |
2151 | * of the chain. | |
2152 | */ | |
d81499bf PB |
2153 | if (unlikely(req->flags & IO_DISARM_MASK)) |
2154 | __io_req_find_next_prep(req); | |
33cc89a9 PB |
2155 | nxt = req->link; |
2156 | req->link = NULL; | |
2157 | return nxt; | |
4d7dd462 | 2158 | } |
9e645e11 | 2159 | |
f237c30a | 2160 | static void ctx_flush_and_put(struct io_ring_ctx *ctx, bool *locked) |
2c32395d PB |
2161 | { |
2162 | if (!ctx) | |
2163 | return; | |
f237c30a | 2164 | if (*locked) { |
c450178d | 2165 | io_submit_flush_completions(ctx); |
2c32395d | 2166 | mutex_unlock(&ctx->uring_lock); |
f237c30a | 2167 | *locked = false; |
2c32395d PB |
2168 | } |
2169 | percpu_ref_put(&ctx->refs); | |
2170 | } | |
2171 | ||
7cbf1722 | 2172 | static void tctx_task_work(struct callback_head *cb) |
c40f6379 | 2173 | { |
f237c30a | 2174 | bool locked = false; |
ebd0df2e | 2175 | struct io_ring_ctx *ctx = NULL; |
3f18407d PB |
2176 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, |
2177 | task_work); | |
c40f6379 | 2178 | |
16f72070 | 2179 | while (1) { |
3f18407d PB |
2180 | struct io_wq_work_node *node; |
2181 | ||
c450178d | 2182 | if (!tctx->task_list.first && locked) |
8d4ad41e PB |
2183 | io_submit_flush_completions(ctx); |
2184 | ||
3f18407d | 2185 | spin_lock_irq(&tctx->task_lock); |
c6538be9 | 2186 | node = tctx->task_list.first; |
3f18407d | 2187 | INIT_WQ_LIST(&tctx->task_list); |
6294f368 PB |
2188 | if (!node) |
2189 | tctx->task_running = false; | |
3f18407d | 2190 | spin_unlock_irq(&tctx->task_lock); |
6294f368 PB |
2191 | if (!node) |
2192 | break; | |
3f18407d | 2193 | |
6294f368 | 2194 | do { |
3f18407d PB |
2195 | struct io_wq_work_node *next = node->next; |
2196 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2197 | io_task_work.node); | |
2198 | ||
2199 | if (req->ctx != ctx) { | |
f237c30a | 2200 | ctx_flush_and_put(ctx, &locked); |
3f18407d | 2201 | ctx = req->ctx; |
126180b9 PB |
2202 | /* if not contended, grab and improve batching */ |
2203 | locked = mutex_trylock(&ctx->uring_lock); | |
3f18407d PB |
2204 | percpu_ref_get(&ctx->refs); |
2205 | } | |
f237c30a | 2206 | req->io_task_work.func(req, &locked); |
3f18407d | 2207 | node = next; |
6294f368 PB |
2208 | } while (node); |
2209 | ||
7cbf1722 | 2210 | cond_resched(); |
3f18407d | 2211 | } |
ebd0df2e | 2212 | |
f237c30a | 2213 | ctx_flush_and_put(ctx, &locked); |
7cbf1722 JA |
2214 | } |
2215 | ||
e09ee510 | 2216 | static void io_req_task_work_add(struct io_kiocb *req) |
7cbf1722 | 2217 | { |
c15b79de | 2218 | struct task_struct *tsk = req->task; |
7cbf1722 | 2219 | struct io_uring_task *tctx = tsk->io_uring; |
c15b79de | 2220 | enum task_work_notify_mode notify; |
e09ee510 | 2221 | struct io_wq_work_node *node; |
0b81e80c | 2222 | unsigned long flags; |
6294f368 | 2223 | bool running; |
7cbf1722 JA |
2224 | |
2225 | WARN_ON_ONCE(!tctx); | |
2226 | ||
0b81e80c | 2227 | spin_lock_irqsave(&tctx->task_lock, flags); |
7cbf1722 | 2228 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); |
6294f368 PB |
2229 | running = tctx->task_running; |
2230 | if (!running) | |
2231 | tctx->task_running = true; | |
0b81e80c | 2232 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
2233 | |
2234 | /* task_work already pending, we're done */ | |
6294f368 | 2235 | if (running) |
e09ee510 | 2236 | return; |
7cbf1722 | 2237 | |
c15b79de PB |
2238 | /* |
2239 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For | |
2240 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're | |
2241 | * processing task_work. There's no reliable way to tell if TWA_RESUME | |
2242 | * will do the job. | |
2243 | */ | |
2244 | notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL; | |
d97ec623 PB |
2245 | if (likely(!task_work_add(tsk, &tctx->task_work, notify))) { |
2246 | if (notify == TWA_NONE) | |
2247 | wake_up_process(tsk); | |
e09ee510 | 2248 | return; |
c15b79de | 2249 | } |
2215bed9 | 2250 | |
0b81e80c | 2251 | spin_lock_irqsave(&tctx->task_lock, flags); |
6294f368 | 2252 | tctx->task_running = false; |
e09ee510 PB |
2253 | node = tctx->task_list.first; |
2254 | INIT_WQ_LIST(&tctx->task_list); | |
0b81e80c | 2255 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 | 2256 | |
e09ee510 PB |
2257 | while (node) { |
2258 | req = container_of(node, struct io_kiocb, io_task_work.node); | |
2259 | node = node->next; | |
2260 | if (llist_add(&req->io_task_work.fallback_node, | |
2261 | &req->ctx->fallback_llist)) | |
2262 | schedule_delayed_work(&req->ctx->fallback_work, 1); | |
2263 | } | |
eab30c4d PB |
2264 | } |
2265 | ||
f237c30a | 2266 | static void io_req_task_cancel(struct io_kiocb *req, bool *locked) |
c40f6379 | 2267 | { |
87ceb6a6 | 2268 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 | 2269 | |
b18a1a45 | 2270 | /* not needed for normal modes, but SQPOLL depends on it */ |
f237c30a | 2271 | io_tw_lock(ctx, locked); |
2593553a | 2272 | io_req_complete_failed(req, req->result); |
c40f6379 JA |
2273 | } |
2274 | ||
f237c30a | 2275 | static void io_req_task_submit(struct io_kiocb *req, bool *locked) |
c40f6379 JA |
2276 | { |
2277 | struct io_ring_ctx *ctx = req->ctx; | |
2278 | ||
f237c30a | 2279 | io_tw_lock(ctx, locked); |
316319e8 | 2280 | /* req->task == current here, checking PF_EXITING is safe */ |
af066f31 | 2281 | if (likely(!(req->task->flags & PF_EXITING))) |
c5eef2b9 | 2282 | __io_queue_sqe(req); |
81b6d05c | 2283 | else |
2593553a | 2284 | io_req_complete_failed(req, -EFAULT); |
c40f6379 JA |
2285 | } |
2286 | ||
2c4b8eb6 | 2287 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
c40f6379 | 2288 | { |
2c4b8eb6 | 2289 | req->result = ret; |
5b0a6acc | 2290 | req->io_task_work.func = io_req_task_cancel; |
e09ee510 | 2291 | io_req_task_work_add(req); |
c40f6379 JA |
2292 | } |
2293 | ||
2c4b8eb6 | 2294 | static void io_req_task_queue(struct io_kiocb *req) |
a3df7698 | 2295 | { |
5b0a6acc | 2296 | req->io_task_work.func = io_req_task_submit; |
e09ee510 | 2297 | io_req_task_work_add(req); |
a3df7698 PB |
2298 | } |
2299 | ||
773af691 JA |
2300 | static void io_req_task_queue_reissue(struct io_kiocb *req) |
2301 | { | |
2302 | req->io_task_work.func = io_queue_async_work; | |
2303 | io_req_task_work_add(req); | |
2304 | } | |
2305 | ||
f2f87370 | 2306 | static inline void io_queue_next(struct io_kiocb *req) |
c69f8dbe | 2307 | { |
9b5f7bd9 | 2308 | struct io_kiocb *nxt = io_req_find_next(req); |
944e58bf PB |
2309 | |
2310 | if (nxt) | |
906a8c3f | 2311 | io_req_task_queue(nxt); |
c69f8dbe JL |
2312 | } |
2313 | ||
c3524383 | 2314 | static void io_free_req(struct io_kiocb *req) |
7a743e22 | 2315 | { |
c3524383 PB |
2316 | io_queue_next(req); |
2317 | __io_free_req(req); | |
2318 | } | |
8766dd51 | 2319 | |
f237c30a PB |
2320 | static void io_free_req_work(struct io_kiocb *req, bool *locked) |
2321 | { | |
2322 | io_free_req(req); | |
2323 | } | |
2324 | ||
3aa83bfb | 2325 | static void io_free_batch_list(struct io_ring_ctx *ctx, |
1cce17ac | 2326 | struct io_wq_work_node *node) |
3aa83bfb | 2327 | __must_hold(&ctx->uring_lock) |
5af1d13e | 2328 | { |
d4b7a5ef | 2329 | struct task_struct *task = NULL; |
37f0e767 | 2330 | int task_refs = 0; |
5af1d13e | 2331 | |
3aa83bfb PB |
2332 | do { |
2333 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2334 | comp_list); | |
2d6500d4 | 2335 | |
def77acf | 2336 | if (unlikely(req->flags & REQ_F_REFCOUNT)) { |
c1e53a69 | 2337 | node = req->comp_list.next; |
def77acf PB |
2338 | if (!req_ref_put_and_test(req)) |
2339 | continue; | |
c1e53a69 | 2340 | } |
2d6500d4 | 2341 | |
ab409402 | 2342 | io_req_put_rsrc_locked(req, ctx); |
d4b7a5ef PB |
2343 | io_queue_next(req); |
2344 | io_dismantle_req(req); | |
5af1d13e | 2345 | |
d4b7a5ef PB |
2346 | if (req->task != task) { |
2347 | if (task) | |
2348 | io_put_task(task, task_refs); | |
2349 | task = req->task; | |
2350 | task_refs = 0; | |
2351 | } | |
2352 | task_refs++; | |
c1e53a69 | 2353 | node = req->comp_list.next; |
d4b7a5ef | 2354 | wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list); |
3aa83bfb | 2355 | } while (node); |
d4b7a5ef | 2356 | |
d4b7a5ef PB |
2357 | if (task) |
2358 | io_put_task(task, task_refs); | |
7a743e22 PB |
2359 | } |
2360 | ||
c450178d | 2361 | static void __io_submit_flush_completions(struct io_ring_ctx *ctx) |
a141dd89 | 2362 | __must_hold(&ctx->uring_lock) |
905c172f | 2363 | { |
6f33b0bc | 2364 | struct io_wq_work_node *node, *prev; |
cd0ca2e0 | 2365 | struct io_submit_state *state = &ctx->submit_state; |
905c172f | 2366 | |
79ebeaee | 2367 | spin_lock(&ctx->completion_lock); |
6f33b0bc PB |
2368 | wq_list_for_each(node, prev, &state->compl_reqs) { |
2369 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2370 | comp_list); | |
5182ed2e | 2371 | |
d4d19c19 | 2372 | __io_cqring_fill_event(ctx, req->user_data, req->result, |
d17e56eb | 2373 | req->cflags); |
905c172f PB |
2374 | } |
2375 | io_commit_cqring(ctx); | |
79ebeaee | 2376 | spin_unlock(&ctx->completion_lock); |
905c172f | 2377 | io_cqring_ev_posted(ctx); |
5182ed2e | 2378 | |
1cce17ac | 2379 | io_free_batch_list(ctx, state->compl_reqs.first); |
6f33b0bc | 2380 | INIT_WQ_LIST(&state->compl_reqs); |
7a743e22 PB |
2381 | } |
2382 | ||
ba816ad6 JA |
2383 | /* |
2384 | * Drop reference to request, return next in chain (if there is one) if this | |
2385 | * was the last reference to this request. | |
2386 | */ | |
0d85035a | 2387 | static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
e65ef56d | 2388 | { |
9b5f7bd9 PB |
2389 | struct io_kiocb *nxt = NULL; |
2390 | ||
de9b4cca | 2391 | if (req_ref_put_and_test(req)) { |
9b5f7bd9 | 2392 | nxt = io_req_find_next(req); |
4d7dd462 | 2393 | __io_free_req(req); |
2a44f467 | 2394 | } |
9b5f7bd9 | 2395 | return nxt; |
2b188cc1 JA |
2396 | } |
2397 | ||
0d85035a | 2398 | static inline void io_put_req(struct io_kiocb *req) |
e65ef56d | 2399 | { |
de9b4cca | 2400 | if (req_ref_put_and_test(req)) |
e65ef56d | 2401 | io_free_req(req); |
2b188cc1 JA |
2402 | } |
2403 | ||
91c2f697 | 2404 | static inline void io_put_req_deferred(struct io_kiocb *req) |
216578e5 | 2405 | { |
91c2f697 | 2406 | if (req_ref_put_and_test(req)) { |
f237c30a | 2407 | req->io_task_work.func = io_free_req_work; |
543af3a1 PB |
2408 | io_req_task_work_add(req); |
2409 | } | |
216578e5 PB |
2410 | } |
2411 | ||
6c503150 | 2412 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
a3a0e43f JA |
2413 | { |
2414 | /* See comment at the top of this file */ | |
2415 | smp_rmb(); | |
e23de15f | 2416 | return __io_cqring_events(ctx); |
a3a0e43f JA |
2417 | } |
2418 | ||
fb5ccc98 PB |
2419 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
2420 | { | |
2421 | struct io_rings *rings = ctx->rings; | |
2422 | ||
2423 | /* make sure SQ entry isn't read before tail */ | |
2424 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
2425 | } | |
2426 | ||
8ff069bf | 2427 | static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf) |
e94f141b | 2428 | { |
8ff069bf | 2429 | unsigned int cflags; |
e94f141b | 2430 | |
bcda7baa JA |
2431 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
2432 | cflags |= IORING_CQE_F_BUFFER; | |
0e1b6fe3 | 2433 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
bcda7baa JA |
2434 | kfree(kbuf); |
2435 | return cflags; | |
e94f141b JA |
2436 | } |
2437 | ||
8ff069bf | 2438 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
bcda7baa | 2439 | { |
ae421d93 PB |
2440 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) |
2441 | return 0; | |
30d51dd4 | 2442 | return io_put_kbuf(req, req->kbuf); |
8ff069bf PB |
2443 | } |
2444 | ||
4c6e277c JA |
2445 | static inline bool io_run_task_work(void) |
2446 | { | |
ef98eb04 | 2447 | if (test_thread_flag(TIF_NOTIFY_SIGNAL) || current->task_works) { |
4c6e277c | 2448 | __set_current_state(TASK_RUNNING); |
ef98eb04 | 2449 | tracehook_notify_signal(); |
4c6e277c JA |
2450 | return true; |
2451 | } | |
2452 | ||
2453 | return false; | |
bcda7baa JA |
2454 | } |
2455 | ||
5ba3c874 | 2456 | static int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin) |
def596e9 | 2457 | { |
5eef4e87 | 2458 | struct io_wq_work_node *pos, *start, *prev; |
d729cf9a | 2459 | unsigned int poll_flags = BLK_POLL_NOSLEEP; |
b688f11e | 2460 | DEFINE_IO_COMP_BATCH(iob); |
5ba3c874 | 2461 | int nr_events = 0; |
def596e9 JA |
2462 | |
2463 | /* | |
2464 | * Only spin for completions if we don't have multiple devices hanging | |
87a115fb | 2465 | * off our complete list. |
def596e9 | 2466 | */ |
87a115fb | 2467 | if (ctx->poll_multi_queue || force_nonspin) |
ef99b2d3 | 2468 | poll_flags |= BLK_POLL_ONESHOT; |
def596e9 | 2469 | |
5eef4e87 PB |
2470 | wq_list_for_each(pos, start, &ctx->iopoll_list) { |
2471 | struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); | |
9adbd45d | 2472 | struct kiocb *kiocb = &req->rw.kiocb; |
a2416e1e | 2473 | int ret; |
def596e9 JA |
2474 | |
2475 | /* | |
581f9810 BM |
2476 | * Move completed and retryable entries to our local lists. |
2477 | * If we find a request that requires polling, break out | |
2478 | * and complete those lists first, if we have entries there. | |
def596e9 | 2479 | */ |
e3f721e6 | 2480 | if (READ_ONCE(req->iopoll_completed)) |
def596e9 JA |
2481 | break; |
2482 | ||
b688f11e | 2483 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, &iob, poll_flags); |
a2416e1e PB |
2484 | if (unlikely(ret < 0)) |
2485 | return ret; | |
2486 | else if (ret) | |
ef99b2d3 | 2487 | poll_flags |= BLK_POLL_ONESHOT; |
def596e9 | 2488 | |
3aadc23e | 2489 | /* iopoll may have completed current req */ |
b688f11e JA |
2490 | if (!rq_list_empty(iob.req_list) || |
2491 | READ_ONCE(req->iopoll_completed)) | |
e3f721e6 | 2492 | break; |
def596e9 JA |
2493 | } |
2494 | ||
b688f11e JA |
2495 | if (!rq_list_empty(iob.req_list)) |
2496 | iob.complete(&iob); | |
5eef4e87 PB |
2497 | else if (!pos) |
2498 | return 0; | |
def596e9 | 2499 | |
5eef4e87 PB |
2500 | prev = start; |
2501 | wq_list_for_each_resume(pos, prev) { | |
2502 | struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); | |
2503 | ||
b3fa03fd PB |
2504 | /* order with io_complete_rw_iopoll(), e.g. ->result updates */ |
2505 | if (!smp_load_acquire(&req->iopoll_completed)) | |
e3f721e6 | 2506 | break; |
b3fa03fd | 2507 | __io_cqring_fill_event(ctx, req->user_data, req->result, |
f5ed3bcd | 2508 | io_put_rw_kbuf(req)); |
e3f721e6 PB |
2509 | nr_events++; |
2510 | } | |
def596e9 | 2511 | |
f5ed3bcd PB |
2512 | if (unlikely(!nr_events)) |
2513 | return 0; | |
2514 | ||
2515 | io_commit_cqring(ctx); | |
2516 | io_cqring_ev_posted_iopoll(ctx); | |
1cce17ac | 2517 | pos = start ? start->next : ctx->iopoll_list.first; |
5eef4e87 | 2518 | wq_list_cut(&ctx->iopoll_list, prev, start); |
1cce17ac | 2519 | io_free_batch_list(ctx, pos); |
5ba3c874 | 2520 | return nr_events; |
def596e9 JA |
2521 | } |
2522 | ||
def596e9 JA |
2523 | /* |
2524 | * We can't just wait for polled events to come to us, we have to actively | |
2525 | * find and complete them. | |
2526 | */ | |
c072481d | 2527 | static __cold void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
def596e9 JA |
2528 | { |
2529 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
2530 | return; | |
2531 | ||
2532 | mutex_lock(&ctx->uring_lock); | |
5eef4e87 | 2533 | while (!wq_list_empty(&ctx->iopoll_list)) { |
b2edc0a7 | 2534 | /* let it sleep and repeat later if can't complete a request */ |
5ba3c874 | 2535 | if (io_do_iopoll(ctx, true) == 0) |
b2edc0a7 | 2536 | break; |
08f5439f JA |
2537 | /* |
2538 | * Ensure we allow local-to-the-cpu processing to take place, | |
2539 | * in this case we need to ensure that we reap all events. | |
3fcee5a6 | 2540 | * Also let task_work, etc. to progress by releasing the mutex |
08f5439f | 2541 | */ |
3fcee5a6 PB |
2542 | if (need_resched()) { |
2543 | mutex_unlock(&ctx->uring_lock); | |
2544 | cond_resched(); | |
2545 | mutex_lock(&ctx->uring_lock); | |
2546 | } | |
def596e9 JA |
2547 | } |
2548 | mutex_unlock(&ctx->uring_lock); | |
2549 | } | |
2550 | ||
7668b92a | 2551 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
def596e9 | 2552 | { |
7668b92a | 2553 | unsigned int nr_events = 0; |
e9979b36 | 2554 | int ret = 0; |
500f9fba | 2555 | |
c7849be9 XW |
2556 | /* |
2557 | * We disallow the app entering submit/complete with polling, but we | |
2558 | * still need to lock the ring to prevent racing with polled issue | |
2559 | * that got punted to a workqueue. | |
2560 | */ | |
2561 | mutex_lock(&ctx->uring_lock); | |
f39c8a5b PB |
2562 | /* |
2563 | * Don't enter poll loop if we already have events pending. | |
2564 | * If we do, we can potentially be spinning for commands that | |
2565 | * already triggered a CQE (eg in error). | |
2566 | */ | |
5ed7a37d | 2567 | if (test_bit(0, &ctx->check_cq_overflow)) |
f39c8a5b PB |
2568 | __io_cqring_overflow_flush(ctx, false); |
2569 | if (io_cqring_events(ctx)) | |
2570 | goto out; | |
def596e9 | 2571 | do { |
500f9fba JA |
2572 | /* |
2573 | * If a submit got punted to a workqueue, we can have the | |
2574 | * application entering polling for a command before it gets | |
2575 | * issued. That app will hold the uring_lock for the duration | |
2576 | * of the poll right here, so we need to take a breather every | |
2577 | * now and then to ensure that the issue has a chance to add | |
2578 | * the poll to the issued list. Otherwise we can spin here | |
2579 | * forever, while the workqueue is stuck trying to acquire the | |
2580 | * very same mutex. | |
2581 | */ | |
5eef4e87 | 2582 | if (wq_list_empty(&ctx->iopoll_list)) { |
8f487ef2 PB |
2583 | u32 tail = ctx->cached_cq_tail; |
2584 | ||
500f9fba | 2585 | mutex_unlock(&ctx->uring_lock); |
4c6e277c | 2586 | io_run_task_work(); |
500f9fba | 2587 | mutex_lock(&ctx->uring_lock); |
def596e9 | 2588 | |
8f487ef2 PB |
2589 | /* some requests don't go through iopoll_list */ |
2590 | if (tail != ctx->cached_cq_tail || | |
5eef4e87 | 2591 | wq_list_empty(&ctx->iopoll_list)) |
e9979b36 | 2592 | break; |
500f9fba | 2593 | } |
5ba3c874 PB |
2594 | ret = io_do_iopoll(ctx, !min); |
2595 | if (ret < 0) | |
2596 | break; | |
2597 | nr_events += ret; | |
2598 | ret = 0; | |
2599 | } while (nr_events < min && !need_resched()); | |
f39c8a5b | 2600 | out: |
500f9fba | 2601 | mutex_unlock(&ctx->uring_lock); |
def596e9 JA |
2602 | return ret; |
2603 | } | |
2604 | ||
491381ce | 2605 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 2606 | { |
491381ce JA |
2607 | /* |
2608 | * Tell lockdep we inherited freeze protection from submission | |
2609 | * thread. | |
2610 | */ | |
2611 | if (req->flags & REQ_F_ISREG) { | |
1c98679d | 2612 | struct super_block *sb = file_inode(req->file)->i_sb; |
2b188cc1 | 2613 | |
1c98679d PB |
2614 | __sb_writers_acquired(sb, SB_FREEZE_WRITE); |
2615 | sb_end_write(sb); | |
2b188cc1 JA |
2616 | } |
2617 | } | |
2618 | ||
b63534c4 | 2619 | #ifdef CONFIG_BLOCK |
dc2a6e9a | 2620 | static bool io_resubmit_prep(struct io_kiocb *req) |
b63534c4 | 2621 | { |
ab454438 | 2622 | struct io_async_rw *rw = req->async_data; |
b63534c4 | 2623 | |
d886e185 | 2624 | if (!req_has_async_data(req)) |
ab454438 | 2625 | return !io_req_prep_async(req); |
538941e2 | 2626 | iov_iter_restore(&rw->s.iter, &rw->s.iter_state); |
ab454438 | 2627 | return true; |
b63534c4 | 2628 | } |
b63534c4 | 2629 | |
3e6a0d3c | 2630 | static bool io_rw_should_reissue(struct io_kiocb *req) |
b63534c4 | 2631 | { |
355afaeb | 2632 | umode_t mode = file_inode(req->file)->i_mode; |
3e6a0d3c | 2633 | struct io_ring_ctx *ctx = req->ctx; |
b63534c4 | 2634 | |
355afaeb JA |
2635 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
2636 | return false; | |
3e6a0d3c JA |
2637 | if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() && |
2638 | !(ctx->flags & IORING_SETUP_IOPOLL))) | |
b63534c4 | 2639 | return false; |
7c977a58 JA |
2640 | /* |
2641 | * If ref is dying, we might be running poll reap from the exit work. | |
2642 | * Don't attempt to reissue from that path, just let it fail with | |
2643 | * -EAGAIN. | |
2644 | */ | |
3e6a0d3c JA |
2645 | if (percpu_ref_is_dying(&ctx->refs)) |
2646 | return false; | |
ef046888 JA |
2647 | /* |
2648 | * Play it safe and assume not safe to re-import and reissue if we're | |
2649 | * not in the original thread group (or in task context). | |
2650 | */ | |
2651 | if (!same_thread_group(req->task, current) || !in_task()) | |
2652 | return false; | |
3e6a0d3c JA |
2653 | return true; |
2654 | } | |
e82ad485 | 2655 | #else |
a1ff1e3f | 2656 | static bool io_resubmit_prep(struct io_kiocb *req) |
e82ad485 JA |
2657 | { |
2658 | return false; | |
2659 | } | |
e82ad485 | 2660 | static bool io_rw_should_reissue(struct io_kiocb *req) |
3e6a0d3c | 2661 | { |
b63534c4 JA |
2662 | return false; |
2663 | } | |
3e6a0d3c | 2664 | #endif |
b63534c4 | 2665 | |
8ef12efe | 2666 | static bool __io_complete_rw_common(struct io_kiocb *req, long res) |
a1d7c393 | 2667 | { |
b65c128f PB |
2668 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) |
2669 | kiocb_end_write(req); | |
258f3a7f | 2670 | if (unlikely(res != req->result)) { |
9532b99b PB |
2671 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && |
2672 | io_rw_should_reissue(req)) { | |
2673 | req->flags |= REQ_F_REISSUE; | |
8ef12efe | 2674 | return true; |
9532b99b | 2675 | } |
93d2bcd2 | 2676 | req_set_fail(req); |
8ef12efe | 2677 | req->result = res; |
9532b99b | 2678 | } |
8ef12efe JA |
2679 | return false; |
2680 | } | |
2681 | ||
f237c30a | 2682 | static void io_req_task_complete(struct io_kiocb *req, bool *locked) |
8ef12efe | 2683 | { |
126180b9 | 2684 | unsigned int cflags = io_put_rw_kbuf(req); |
54daa9b2 | 2685 | int res = req->result; |
126180b9 PB |
2686 | |
2687 | if (*locked) { | |
126180b9 | 2688 | io_req_complete_state(req, res, cflags); |
fff4e40e | 2689 | io_req_add_compl_list(req); |
126180b9 PB |
2690 | } else { |
2691 | io_req_complete_post(req, res, cflags); | |
2692 | } | |
8ef12efe JA |
2693 | } |
2694 | ||
2695 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, | |
2696 | unsigned int issue_flags) | |
2697 | { | |
2698 | if (__io_complete_rw_common(req, res)) | |
2699 | return; | |
63637853 | 2700 | __io_req_complete(req, issue_flags, req->result, io_put_rw_kbuf(req)); |
ba816ad6 JA |
2701 | } |
2702 | ||
6b19b766 | 2703 | static void io_complete_rw(struct kiocb *kiocb, long res) |
ba816ad6 | 2704 | { |
9adbd45d | 2705 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 | 2706 | |
8ef12efe JA |
2707 | if (__io_complete_rw_common(req, res)) |
2708 | return; | |
2709 | req->result = res; | |
2710 | req->io_task_work.func = io_req_task_complete; | |
2711 | io_req_task_work_add(req); | |
2b188cc1 JA |
2712 | } |
2713 | ||
6b19b766 | 2714 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res) |
def596e9 | 2715 | { |
9adbd45d | 2716 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 2717 | |
491381ce JA |
2718 | if (kiocb->ki_flags & IOCB_WRITE) |
2719 | kiocb_end_write(req); | |
9532b99b | 2720 | if (unlikely(res != req->result)) { |
b66ceaf3 PB |
2721 | if (res == -EAGAIN && io_rw_should_reissue(req)) { |
2722 | req->flags |= REQ_F_REISSUE; | |
2723 | return; | |
9532b99b | 2724 | } |
258f3a7f | 2725 | req->result = res; |
8c130827 | 2726 | } |
bbde017a | 2727 | |
b3fa03fd PB |
2728 | /* order with io_iopoll_complete() checking ->iopoll_completed */ |
2729 | smp_store_release(&req->iopoll_completed, 1); | |
def596e9 JA |
2730 | } |
2731 | ||
2732 | /* | |
2733 | * After the iocb has been issued, it's safe to be found on the poll list. | |
2734 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
f39c8a5b | 2735 | * find it from a io_do_iopoll() thread before the issuer is done |
def596e9 JA |
2736 | * accessing the kiocb cookie. |
2737 | */ | |
9882131c | 2738 | static void io_iopoll_req_issued(struct io_kiocb *req, unsigned int issue_flags) |
def596e9 JA |
2739 | { |
2740 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 2741 | const bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
cb3d8972 PB |
2742 | |
2743 | /* workqueue context doesn't hold uring_lock, grab it now */ | |
3b44b371 | 2744 | if (unlikely(needs_lock)) |
cb3d8972 | 2745 | mutex_lock(&ctx->uring_lock); |
def596e9 JA |
2746 | |
2747 | /* | |
2748 | * Track whether we have multiple files in our lists. This will impact | |
2749 | * how we do polling eventually, not spinning if we're on potentially | |
2750 | * different devices. | |
2751 | */ | |
5eef4e87 | 2752 | if (wq_list_empty(&ctx->iopoll_list)) { |
915b3dde HX |
2753 | ctx->poll_multi_queue = false; |
2754 | } else if (!ctx->poll_multi_queue) { | |
def596e9 JA |
2755 | struct io_kiocb *list_req; |
2756 | ||
5eef4e87 PB |
2757 | list_req = container_of(ctx->iopoll_list.first, struct io_kiocb, |
2758 | comp_list); | |
30da1b45 | 2759 | if (list_req->file != req->file) |
915b3dde | 2760 | ctx->poll_multi_queue = true; |
def596e9 JA |
2761 | } |
2762 | ||
2763 | /* | |
2764 | * For fast devices, IO may have already completed. If it has, add | |
2765 | * it to the front so we find it first. | |
2766 | */ | |
65a6543d | 2767 | if (READ_ONCE(req->iopoll_completed)) |
5eef4e87 | 2768 | wq_list_add_head(&req->comp_list, &ctx->iopoll_list); |
def596e9 | 2769 | else |
5eef4e87 | 2770 | wq_list_add_tail(&req->comp_list, &ctx->iopoll_list); |
bdcd3eab | 2771 | |
3b44b371 | 2772 | if (unlikely(needs_lock)) { |
cb3d8972 PB |
2773 | /* |
2774 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handle | |
2775 | * in sq thread task context or in io worker task context. If | |
2776 | * current task context is sq thread, we don't need to check | |
2777 | * whether should wake up sq thread. | |
2778 | */ | |
2779 | if ((ctx->flags & IORING_SETUP_SQPOLL) && | |
2780 | wq_has_sleeper(&ctx->sq_data->wait)) | |
2781 | wake_up(&ctx->sq_data->wait); | |
2782 | ||
2783 | mutex_unlock(&ctx->uring_lock); | |
2784 | } | |
def596e9 JA |
2785 | } |
2786 | ||
4503b767 JA |
2787 | static bool io_bdev_nowait(struct block_device *bdev) |
2788 | { | |
9ba0d0c8 | 2789 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
4503b767 JA |
2790 | } |
2791 | ||
2b188cc1 JA |
2792 | /* |
2793 | * If we tracked the file through the SCM inflight mechanism, we could support | |
2794 | * any file. For now, just ensure that anything potentially problematic is done | |
2795 | * inline. | |
2796 | */ | |
88459b50 | 2797 | static bool __io_file_supports_nowait(struct file *file, umode_t mode) |
2b188cc1 | 2798 | { |
4503b767 | 2799 | if (S_ISBLK(mode)) { |
4e7b5671 CH |
2800 | if (IS_ENABLED(CONFIG_BLOCK) && |
2801 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) | |
4503b767 JA |
2802 | return true; |
2803 | return false; | |
2804 | } | |
976517f1 | 2805 | if (S_ISSOCK(mode)) |
2b188cc1 | 2806 | return true; |
4503b767 | 2807 | if (S_ISREG(mode)) { |
4e7b5671 CH |
2808 | if (IS_ENABLED(CONFIG_BLOCK) && |
2809 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && | |
4503b767 JA |
2810 | file->f_op != &io_uring_fops) |
2811 | return true; | |
2812 | return false; | |
2813 | } | |
2b188cc1 | 2814 | |
c5b85625 JA |
2815 | /* any ->read/write should understand O_NONBLOCK */ |
2816 | if (file->f_flags & O_NONBLOCK) | |
2817 | return true; | |
35645ac3 | 2818 | return file->f_mode & FMODE_NOWAIT; |
2b188cc1 | 2819 | } |
c5b85625 | 2820 | |
88459b50 PB |
2821 | /* |
2822 | * If we tracked the file through the SCM inflight mechanism, we could support | |
2823 | * any file. For now, just ensure that anything potentially problematic is done | |
2824 | * inline. | |
2825 | */ | |
2826 | static unsigned int io_file_get_flags(struct file *file) | |
2827 | { | |
2828 | umode_t mode = file_inode(file)->i_mode; | |
2829 | unsigned int res = 0; | |
af197f50 | 2830 | |
88459b50 PB |
2831 | if (S_ISREG(mode)) |
2832 | res |= FFS_ISREG; | |
2833 | if (__io_file_supports_nowait(file, mode)) | |
2834 | res |= FFS_NOWAIT; | |
2835 | return res; | |
2b188cc1 JA |
2836 | } |
2837 | ||
35645ac3 | 2838 | static inline bool io_file_supports_nowait(struct io_kiocb *req) |
7b29f92d | 2839 | { |
88459b50 | 2840 | return req->flags & REQ_F_SUPPORT_NOWAIT; |
7b29f92d JA |
2841 | } |
2842 | ||
b9a6b8f9 | 2843 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2b188cc1 | 2844 | { |
def596e9 | 2845 | struct io_ring_ctx *ctx = req->ctx; |
9adbd45d | 2846 | struct kiocb *kiocb = &req->rw.kiocb; |
75c668cd | 2847 | struct file *file = req->file; |
09bb8394 JA |
2848 | unsigned ioprio; |
2849 | int ret; | |
2b188cc1 | 2850 | |
88459b50 PB |
2851 | if (!io_req_ffs_set(req)) |
2852 | req->flags |= io_file_get_flags(file) << REQ_F_SUPPORT_NOWAIT_BIT; | |
491381ce | 2853 | |
2b188cc1 | 2854 | kiocb->ki_pos = READ_ONCE(sqe->off); |
75c668cd | 2855 | if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) { |
ba04291e | 2856 | req->flags |= REQ_F_CUR_POS; |
75c668cd | 2857 | kiocb->ki_pos = file->f_pos; |
ba04291e | 2858 | } |
5cb03d63 | 2859 | kiocb->ki_flags = iocb_flags(file); |
3e577dcd PB |
2860 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
2861 | if (unlikely(ret)) | |
2862 | return ret; | |
2b188cc1 | 2863 | |
5d329e12 JA |
2864 | /* |
2865 | * If the file is marked O_NONBLOCK, still allow retry for it if it | |
2866 | * supports async. Otherwise it's impossible to use O_NONBLOCK files | |
2867 | * reliably. If not, or it IOCB_NOWAIT is set, don't retry. | |
2868 | */ | |
2869 | if ((kiocb->ki_flags & IOCB_NOWAIT) || | |
35645ac3 | 2870 | ((file->f_flags & O_NONBLOCK) && !io_file_supports_nowait(req))) |
75c668cd PB |
2871 | req->flags |= REQ_F_NOWAIT; |
2872 | ||
def596e9 | 2873 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
5cb03d63 | 2874 | if (!(kiocb->ki_flags & IOCB_DIRECT) || !file->f_op->iopoll) |
09bb8394 | 2875 | return -EOPNOTSUPP; |
2b188cc1 | 2876 | |
394918eb | 2877 | kiocb->ki_flags |= IOCB_HIPRI | IOCB_ALLOC_CACHE; |
def596e9 | 2878 | kiocb->ki_complete = io_complete_rw_iopoll; |
65a6543d | 2879 | req->iopoll_completed = 0; |
def596e9 | 2880 | } else { |
09bb8394 JA |
2881 | if (kiocb->ki_flags & IOCB_HIPRI) |
2882 | return -EINVAL; | |
def596e9 JA |
2883 | kiocb->ki_complete = io_complete_rw; |
2884 | } | |
9adbd45d | 2885 | |
fb27274a PB |
2886 | ioprio = READ_ONCE(sqe->ioprio); |
2887 | if (ioprio) { | |
2888 | ret = ioprio_check_cap(ioprio); | |
2889 | if (ret) | |
2890 | return ret; | |
2891 | ||
2892 | kiocb->ki_ioprio = ioprio; | |
2893 | } else { | |
2894 | kiocb->ki_ioprio = get_current_ioprio(); | |
eae071c9 PB |
2895 | } |
2896 | ||
578c0ee2 | 2897 | req->imu = NULL; |
3529d8c2 JA |
2898 | req->rw.addr = READ_ONCE(sqe->addr); |
2899 | req->rw.len = READ_ONCE(sqe->len); | |
4f4eeba8 | 2900 | req->buf_index = READ_ONCE(sqe->buf_index); |
2b188cc1 | 2901 | return 0; |
2b188cc1 JA |
2902 | } |
2903 | ||
2904 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
2905 | { | |
2906 | switch (ret) { | |
2907 | case -EIOCBQUEUED: | |
2908 | break; | |
2909 | case -ERESTARTSYS: | |
2910 | case -ERESTARTNOINTR: | |
2911 | case -ERESTARTNOHAND: | |
2912 | case -ERESTART_RESTARTBLOCK: | |
2913 | /* | |
2914 | * We can't just restart the syscall, since previously | |
2915 | * submitted sqes may already be in progress. Just fail this | |
2916 | * IO with EINTR. | |
2917 | */ | |
2918 | ret = -EINTR; | |
df561f66 | 2919 | fallthrough; |
2b188cc1 | 2920 | default: |
6b19b766 | 2921 | kiocb->ki_complete(kiocb, ret); |
2b188cc1 JA |
2922 | } |
2923 | } | |
2924 | ||
a1d7c393 | 2925 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
889fca73 | 2926 | unsigned int issue_flags) |
ba816ad6 | 2927 | { |
ba04291e | 2928 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
e8c2bc1f | 2929 | struct io_async_rw *io = req->async_data; |
ba04291e | 2930 | |
227c0c96 | 2931 | /* add previously done IO, if any */ |
d886e185 | 2932 | if (req_has_async_data(req) && io->bytes_done > 0) { |
227c0c96 | 2933 | if (ret < 0) |
e8c2bc1f | 2934 | ret = io->bytes_done; |
227c0c96 | 2935 | else |
e8c2bc1f | 2936 | ret += io->bytes_done; |
227c0c96 JA |
2937 | } |
2938 | ||
ba04291e JA |
2939 | if (req->flags & REQ_F_CUR_POS) |
2940 | req->file->f_pos = kiocb->ki_pos; | |
b66ceaf3 | 2941 | if (ret >= 0 && (kiocb->ki_complete == io_complete_rw)) |
889fca73 | 2942 | __io_complete_rw(req, ret, 0, issue_flags); |
ba816ad6 JA |
2943 | else |
2944 | io_rw_done(kiocb, ret); | |
97284637 | 2945 | |
b66ceaf3 | 2946 | if (req->flags & REQ_F_REISSUE) { |
97284637 | 2947 | req->flags &= ~REQ_F_REISSUE; |
a7be7c23 | 2948 | if (io_resubmit_prep(req)) { |
773af691 | 2949 | io_req_task_queue_reissue(req); |
8c130827 | 2950 | } else { |
b66ceaf3 PB |
2951 | unsigned int cflags = io_put_rw_kbuf(req); |
2952 | struct io_ring_ctx *ctx = req->ctx; | |
2953 | ||
93d2bcd2 | 2954 | req_set_fail(req); |
3b44b371 | 2955 | if (issue_flags & IO_URING_F_UNLOCKED) { |
b66ceaf3 PB |
2956 | mutex_lock(&ctx->uring_lock); |
2957 | __io_req_complete(req, issue_flags, ret, cflags); | |
2958 | mutex_unlock(&ctx->uring_lock); | |
2959 | } else { | |
2960 | __io_req_complete(req, issue_flags, ret, cflags); | |
2961 | } | |
97284637 PB |
2962 | } |
2963 | } | |
ba816ad6 JA |
2964 | } |
2965 | ||
eae071c9 PB |
2966 | static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter, |
2967 | struct io_mapped_ubuf *imu) | |
edafccee | 2968 | { |
9adbd45d | 2969 | size_t len = req->rw.len; |
75769e3f | 2970 | u64 buf_end, buf_addr = req->rw.addr; |
edafccee | 2971 | size_t offset; |
edafccee | 2972 | |
75769e3f | 2973 | if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end))) |
edafccee JA |
2974 | return -EFAULT; |
2975 | /* not inside the mapped region */ | |
4751f53d | 2976 | if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end)) |
edafccee JA |
2977 | return -EFAULT; |
2978 | ||
2979 | /* | |
2980 | * May not be a start of buffer, set size appropriately | |
2981 | * and advance us to the beginning. | |
2982 | */ | |
2983 | offset = buf_addr - imu->ubuf; | |
2984 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
2985 | |
2986 | if (offset) { | |
2987 | /* | |
2988 | * Don't use iov_iter_advance() here, as it's really slow for | |
2989 | * using the latter parts of a big fixed buffer - it iterates | |
2990 | * over each segment manually. We can cheat a bit here, because | |
2991 | * we know that: | |
2992 | * | |
2993 | * 1) it's a BVEC iter, we set it up | |
2994 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
2995 | * first and last bvec | |
2996 | * | |
2997 | * So just find our index, and adjust the iterator afterwards. | |
2998 | * If the offset is within the first bvec (or the whole first | |
2999 | * bvec, just use iov_iter_advance(). This makes it easier | |
3000 | * since we can just skip the first segment, which may not | |
3001 | * be PAGE_SIZE aligned. | |
3002 | */ | |
3003 | const struct bio_vec *bvec = imu->bvec; | |
3004 | ||
3005 | if (offset <= bvec->bv_len) { | |
3006 | iov_iter_advance(iter, offset); | |
3007 | } else { | |
3008 | unsigned long seg_skip; | |
3009 | ||
3010 | /* skip first vec */ | |
3011 | offset -= bvec->bv_len; | |
3012 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
3013 | ||
3014 | iter->bvec = bvec + seg_skip; | |
3015 | iter->nr_segs -= seg_skip; | |
99c79f66 | 3016 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 3017 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
3018 | } |
3019 | } | |
3020 | ||
847595de | 3021 | return 0; |
edafccee JA |
3022 | } |
3023 | ||
eae071c9 PB |
3024 | static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter) |
3025 | { | |
eae071c9 PB |
3026 | struct io_mapped_ubuf *imu = req->imu; |
3027 | u16 index, buf_index = req->buf_index; | |
3028 | ||
3029 | if (likely(!imu)) { | |
578c0ee2 PB |
3030 | struct io_ring_ctx *ctx = req->ctx; |
3031 | ||
eae071c9 PB |
3032 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
3033 | return -EFAULT; | |
578c0ee2 | 3034 | io_req_set_rsrc_node(req, ctx); |
eae071c9 PB |
3035 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
3036 | imu = READ_ONCE(ctx->user_bufs[index]); | |
3037 | req->imu = imu; | |
3038 | } | |
3039 | return __io_import_fixed(req, rw, iter, imu); | |
3040 | } | |
3041 | ||
bcda7baa JA |
3042 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
3043 | { | |
3044 | if (needs_lock) | |
3045 | mutex_unlock(&ctx->uring_lock); | |
3046 | } | |
3047 | ||
3048 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) | |
3049 | { | |
3050 | /* | |
3051 | * "Normal" inline submissions always hold the uring_lock, since we | |
3052 | * grab it from the system call. Same is true for the SQPOLL offload. | |
3053 | * The only exception is when we've detached the request and issue it | |
3054 | * from an async worker thread, grab the lock for that case. | |
3055 | */ | |
3056 | if (needs_lock) | |
3057 | mutex_lock(&ctx->uring_lock); | |
3058 | } | |
3059 | ||
3060 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, | |
51aac424 | 3061 | int bgid, unsigned int issue_flags) |
bcda7baa | 3062 | { |
30d51dd4 | 3063 | struct io_buffer *kbuf = req->kbuf; |
bcda7baa | 3064 | struct io_buffer *head; |
3b44b371 | 3065 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
bcda7baa JA |
3066 | |
3067 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
3068 | return kbuf; | |
3069 | ||
3070 | io_ring_submit_lock(req->ctx, needs_lock); | |
3071 | ||
3072 | lockdep_assert_held(&req->ctx->uring_lock); | |
3073 | ||
9e15c3a0 | 3074 | head = xa_load(&req->ctx->io_buffers, bgid); |
bcda7baa JA |
3075 | if (head) { |
3076 | if (!list_empty(&head->list)) { | |
3077 | kbuf = list_last_entry(&head->list, struct io_buffer, | |
3078 | list); | |
3079 | list_del(&kbuf->list); | |
3080 | } else { | |
3081 | kbuf = head; | |
9e15c3a0 | 3082 | xa_erase(&req->ctx->io_buffers, bgid); |
bcda7baa JA |
3083 | } |
3084 | if (*len > kbuf->len) | |
3085 | *len = kbuf->len; | |
30d51dd4 PB |
3086 | req->flags |= REQ_F_BUFFER_SELECTED; |
3087 | req->kbuf = kbuf; | |
bcda7baa JA |
3088 | } else { |
3089 | kbuf = ERR_PTR(-ENOBUFS); | |
3090 | } | |
3091 | ||
3092 | io_ring_submit_unlock(req->ctx, needs_lock); | |
bcda7baa JA |
3093 | return kbuf; |
3094 | } | |
3095 | ||
4d954c25 | 3096 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
51aac424 | 3097 | unsigned int issue_flags) |
4d954c25 JA |
3098 | { |
3099 | struct io_buffer *kbuf; | |
4f4eeba8 | 3100 | u16 bgid; |
4d954c25 | 3101 | |
4f4eeba8 | 3102 | bgid = req->buf_index; |
51aac424 | 3103 | kbuf = io_buffer_select(req, len, bgid, issue_flags); |
4d954c25 JA |
3104 | if (IS_ERR(kbuf)) |
3105 | return kbuf; | |
4d954c25 JA |
3106 | return u64_to_user_ptr(kbuf->addr); |
3107 | } | |
3108 | ||
3109 | #ifdef CONFIG_COMPAT | |
3110 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, | |
51aac424 | 3111 | unsigned int issue_flags) |
4d954c25 JA |
3112 | { |
3113 | struct compat_iovec __user *uiov; | |
3114 | compat_ssize_t clen; | |
3115 | void __user *buf; | |
3116 | ssize_t len; | |
3117 | ||
3118 | uiov = u64_to_user_ptr(req->rw.addr); | |
3119 | if (!access_ok(uiov, sizeof(*uiov))) | |
3120 | return -EFAULT; | |
3121 | if (__get_user(clen, &uiov->iov_len)) | |
3122 | return -EFAULT; | |
3123 | if (clen < 0) | |
3124 | return -EINVAL; | |
3125 | ||
3126 | len = clen; | |
51aac424 | 3127 | buf = io_rw_buffer_select(req, &len, issue_flags); |
4d954c25 JA |
3128 | if (IS_ERR(buf)) |
3129 | return PTR_ERR(buf); | |
3130 | iov[0].iov_base = buf; | |
3131 | iov[0].iov_len = (compat_size_t) len; | |
3132 | return 0; | |
3133 | } | |
3134 | #endif | |
3135 | ||
3136 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
51aac424 | 3137 | unsigned int issue_flags) |
4d954c25 JA |
3138 | { |
3139 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); | |
3140 | void __user *buf; | |
3141 | ssize_t len; | |
3142 | ||
3143 | if (copy_from_user(iov, uiov, sizeof(*uiov))) | |
3144 | return -EFAULT; | |
3145 | ||
3146 | len = iov[0].iov_len; | |
3147 | if (len < 0) | |
3148 | return -EINVAL; | |
51aac424 | 3149 | buf = io_rw_buffer_select(req, &len, issue_flags); |
4d954c25 JA |
3150 | if (IS_ERR(buf)) |
3151 | return PTR_ERR(buf); | |
3152 | iov[0].iov_base = buf; | |
3153 | iov[0].iov_len = len; | |
3154 | return 0; | |
3155 | } | |
3156 | ||
3157 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
51aac424 | 3158 | unsigned int issue_flags) |
4d954c25 | 3159 | { |
dddb3e26 | 3160 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
30d51dd4 | 3161 | struct io_buffer *kbuf = req->kbuf; |
dddb3e26 | 3162 | |
dddb3e26 JA |
3163 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
3164 | iov[0].iov_len = kbuf->len; | |
4d954c25 | 3165 | return 0; |
dddb3e26 | 3166 | } |
dd201662 | 3167 | if (req->rw.len != 1) |
4d954c25 JA |
3168 | return -EINVAL; |
3169 | ||
3170 | #ifdef CONFIG_COMPAT | |
3171 | if (req->ctx->compat) | |
51aac424 | 3172 | return io_compat_import(req, iov, issue_flags); |
4d954c25 JA |
3173 | #endif |
3174 | ||
51aac424 | 3175 | return __io_iov_buffer_select(req, iov, issue_flags); |
4d954c25 JA |
3176 | } |
3177 | ||
caa8fe6e PB |
3178 | static struct iovec *__io_import_iovec(int rw, struct io_kiocb *req, |
3179 | struct io_rw_state *s, | |
3180 | unsigned int issue_flags) | |
2b188cc1 | 3181 | { |
5e49c973 | 3182 | struct iov_iter *iter = &s->iter; |
847595de | 3183 | u8 opcode = req->opcode; |
caa8fe6e | 3184 | struct iovec *iovec; |
d1d681b0 PB |
3185 | void __user *buf; |
3186 | size_t sqe_len; | |
4d954c25 | 3187 | ssize_t ret; |
edafccee | 3188 | |
caa8fe6e PB |
3189 | BUILD_BUG_ON(ERR_PTR(0) != NULL); |
3190 | ||
3191 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) | |
3192 | return ERR_PTR(io_import_fixed(req, rw, iter)); | |
2b188cc1 | 3193 | |
bcda7baa | 3194 | /* buffer index only valid with fixed read/write, or buffer select */ |
d1d681b0 | 3195 | if (unlikely(req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))) |
caa8fe6e | 3196 | return ERR_PTR(-EINVAL); |
9adbd45d | 3197 | |
d1d681b0 PB |
3198 | buf = u64_to_user_ptr(req->rw.addr); |
3199 | sqe_len = req->rw.len; | |
9adbd45d | 3200 | |
3a6820f2 | 3201 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
bcda7baa | 3202 | if (req->flags & REQ_F_BUFFER_SELECT) { |
51aac424 | 3203 | buf = io_rw_buffer_select(req, &sqe_len, issue_flags); |
867a23ea | 3204 | if (IS_ERR(buf)) |
898df244 | 3205 | return ERR_CAST(buf); |
3f9d6441 | 3206 | req->rw.len = sqe_len; |
bcda7baa JA |
3207 | } |
3208 | ||
5e49c973 | 3209 | ret = import_single_range(rw, buf, sqe_len, s->fast_iov, iter); |
caa8fe6e | 3210 | return ERR_PTR(ret); |
3a6820f2 JA |
3211 | } |
3212 | ||
caa8fe6e | 3213 | iovec = s->fast_iov; |
4d954c25 | 3214 | if (req->flags & REQ_F_BUFFER_SELECT) { |
caa8fe6e | 3215 | ret = io_iov_buffer_select(req, iovec, issue_flags); |
847595de | 3216 | if (!ret) |
caa8fe6e PB |
3217 | iov_iter_init(iter, rw, iovec, 1, iovec->iov_len); |
3218 | return ERR_PTR(ret); | |
4d954c25 JA |
3219 | } |
3220 | ||
caa8fe6e | 3221 | ret = __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, &iovec, iter, |
89cd35c5 | 3222 | req->ctx->compat); |
caa8fe6e PB |
3223 | if (unlikely(ret < 0)) |
3224 | return ERR_PTR(ret); | |
3225 | return iovec; | |
2b188cc1 JA |
3226 | } |
3227 | ||
5e49c973 PB |
3228 | static inline int io_import_iovec(int rw, struct io_kiocb *req, |
3229 | struct iovec **iovec, struct io_rw_state *s, | |
3230 | unsigned int issue_flags) | |
3231 | { | |
caa8fe6e PB |
3232 | *iovec = __io_import_iovec(rw, req, s, issue_flags); |
3233 | if (unlikely(IS_ERR(*iovec))) | |
3234 | return PTR_ERR(*iovec); | |
5e49c973 | 3235 | |
5e49c973 | 3236 | iov_iter_save_state(&s->iter, &s->iter_state); |
caa8fe6e | 3237 | return 0; |
2b188cc1 JA |
3238 | } |
3239 | ||
0fef9483 JA |
3240 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
3241 | { | |
5b09e37e | 3242 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
0fef9483 JA |
3243 | } |
3244 | ||
31b51510 | 3245 | /* |
32960613 JA |
3246 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
3247 | * by looping over ->read() or ->write() manually. | |
31b51510 | 3248 | */ |
4017eb91 | 3249 | static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) |
32960613 | 3250 | { |
4017eb91 JA |
3251 | struct kiocb *kiocb = &req->rw.kiocb; |
3252 | struct file *file = req->file; | |
32960613 JA |
3253 | ssize_t ret = 0; |
3254 | ||
3255 | /* | |
3256 | * Don't support polled IO through this interface, and we can't | |
3257 | * support non-blocking either. For the latter, this just causes | |
3258 | * the kiocb to be handled from an async context. | |
3259 | */ | |
3260 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3261 | return -EOPNOTSUPP; | |
35645ac3 PB |
3262 | if ((kiocb->ki_flags & IOCB_NOWAIT) && |
3263 | !(kiocb->ki_filp->f_flags & O_NONBLOCK)) | |
32960613 JA |
3264 | return -EAGAIN; |
3265 | ||
3266 | while (iov_iter_count(iter)) { | |
311ae9e1 | 3267 | struct iovec iovec; |
32960613 JA |
3268 | ssize_t nr; |
3269 | ||
311ae9e1 PB |
3270 | if (!iov_iter_is_bvec(iter)) { |
3271 | iovec = iov_iter_iovec(iter); | |
3272 | } else { | |
4017eb91 JA |
3273 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
3274 | iovec.iov_len = req->rw.len; | |
311ae9e1 PB |
3275 | } |
3276 | ||
32960613 JA |
3277 | if (rw == READ) { |
3278 | nr = file->f_op->read(file, iovec.iov_base, | |
0fef9483 | 3279 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3280 | } else { |
3281 | nr = file->f_op->write(file, iovec.iov_base, | |
0fef9483 | 3282 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3283 | } |
3284 | ||
3285 | if (nr < 0) { | |
3286 | if (!ret) | |
3287 | ret = nr; | |
3288 | break; | |
3289 | } | |
16c8d2df JA |
3290 | if (!iov_iter_is_bvec(iter)) { |
3291 | iov_iter_advance(iter, nr); | |
3292 | } else { | |
3293 | req->rw.len -= nr; | |
3294 | req->rw.addr += nr; | |
3295 | } | |
32960613 JA |
3296 | ret += nr; |
3297 | if (nr != iovec.iov_len) | |
3298 | break; | |
32960613 JA |
3299 | } |
3300 | ||
3301 | return ret; | |
3302 | } | |
3303 | ||
ff6165b2 JA |
3304 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
3305 | const struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 3306 | { |
e8c2bc1f | 3307 | struct io_async_rw *rw = req->async_data; |
b64e3444 | 3308 | |
538941e2 | 3309 | memcpy(&rw->s.iter, iter, sizeof(*iter)); |
afb87658 | 3310 | rw->free_iovec = iovec; |
227c0c96 | 3311 | rw->bytes_done = 0; |
ff6165b2 | 3312 | /* can only be fixed buffers, no need to do anything */ |
9c3a205c | 3313 | if (iov_iter_is_bvec(iter)) |
ff6165b2 | 3314 | return; |
b64e3444 | 3315 | if (!iovec) { |
ff6165b2 JA |
3316 | unsigned iov_off = 0; |
3317 | ||
538941e2 | 3318 | rw->s.iter.iov = rw->s.fast_iov; |
ff6165b2 JA |
3319 | if (iter->iov != fast_iov) { |
3320 | iov_off = iter->iov - fast_iov; | |
538941e2 | 3321 | rw->s.iter.iov += iov_off; |
ff6165b2 | 3322 | } |
538941e2 PB |
3323 | if (rw->s.fast_iov != fast_iov) |
3324 | memcpy(rw->s.fast_iov + iov_off, fast_iov + iov_off, | |
45097dae | 3325 | sizeof(struct iovec) * iter->nr_segs); |
99bc4c38 PB |
3326 | } else { |
3327 | req->flags |= REQ_F_NEED_CLEANUP; | |
f67676d1 JA |
3328 | } |
3329 | } | |
3330 | ||
8d4af685 | 3331 | static inline bool io_alloc_async_data(struct io_kiocb *req) |
3d9932a8 | 3332 | { |
e8c2bc1f JA |
3333 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
3334 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); | |
d886e185 PB |
3335 | if (req->async_data) { |
3336 | req->flags |= REQ_F_ASYNC_DATA; | |
3337 | return false; | |
3338 | } | |
3339 | return true; | |
3d9932a8 XW |
3340 | } |
3341 | ||
ff6165b2 | 3342 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
c88598a9 | 3343 | struct io_rw_state *s, bool force) |
b7bb4f7d | 3344 | { |
26f0505a | 3345 | if (!force && !io_op_defs[req->opcode].needs_async_setup) |
74566df3 | 3346 | return 0; |
d886e185 | 3347 | if (!req_has_async_data(req)) { |
cd658695 JA |
3348 | struct io_async_rw *iorw; |
3349 | ||
6cb78689 | 3350 | if (io_alloc_async_data(req)) { |
6bf985dc | 3351 | kfree(iovec); |
5d204bcf | 3352 | return -ENOMEM; |
6bf985dc | 3353 | } |
b7bb4f7d | 3354 | |
c88598a9 | 3355 | io_req_map_rw(req, iovec, s->fast_iov, &s->iter); |
cd658695 JA |
3356 | iorw = req->async_data; |
3357 | /* we've copied and mapped the iter, ensure state is saved */ | |
538941e2 | 3358 | iov_iter_save_state(&iorw->s.iter, &iorw->s.iter_state); |
5d204bcf | 3359 | } |
b7bb4f7d | 3360 | return 0; |
f67676d1 JA |
3361 | } |
3362 | ||
73debe68 | 3363 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw) |
c3e330a4 | 3364 | { |
e8c2bc1f | 3365 | struct io_async_rw *iorw = req->async_data; |
5e49c973 | 3366 | struct iovec *iov; |
847595de | 3367 | int ret; |
c3e330a4 | 3368 | |
51aac424 | 3369 | /* submission path, ->uring_lock should already be taken */ |
3b44b371 | 3370 | ret = io_import_iovec(rw, req, &iov, &iorw->s, 0); |
c3e330a4 PB |
3371 | if (unlikely(ret < 0)) |
3372 | return ret; | |
3373 | ||
ab0b196c PB |
3374 | iorw->bytes_done = 0; |
3375 | iorw->free_iovec = iov; | |
3376 | if (iov) | |
3377 | req->flags |= REQ_F_NEED_CLEANUP; | |
c3e330a4 PB |
3378 | return 0; |
3379 | } | |
3380 | ||
73debe68 | 3381 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3382 | { |
3529d8c2 JA |
3383 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
3384 | return -EBADF; | |
b9a6b8f9 | 3385 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3386 | } |
3387 | ||
c1dd91d1 | 3388 | /* |
ffdc8dab | 3389 | * This is our waitqueue callback handler, registered through __folio_lock_async() |
c1dd91d1 JA |
3390 | * when we initially tried to do the IO with the iocb armed our waitqueue. |
3391 | * This gets called when the page is unlocked, and we generally expect that to | |
3392 | * happen when the page IO is completed and the page is now uptodate. This will | |
3393 | * queue a task_work based retry of the operation, attempting to copy the data | |
3394 | * again. If the latter fails because the page was NOT uptodate, then we will | |
3395 | * do a thread based blocking retry of the operation. That's the unexpected | |
3396 | * slow path. | |
3397 | */ | |
bcf5a063 JA |
3398 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
3399 | int sync, void *arg) | |
3400 | { | |
3401 | struct wait_page_queue *wpq; | |
3402 | struct io_kiocb *req = wait->private; | |
bcf5a063 | 3403 | struct wait_page_key *key = arg; |
bcf5a063 JA |
3404 | |
3405 | wpq = container_of(wait, struct wait_page_queue, wait); | |
3406 | ||
cdc8fcb4 LT |
3407 | if (!wake_page_match(wpq, key)) |
3408 | return 0; | |
3409 | ||
c8d317aa | 3410 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
bcf5a063 | 3411 | list_del_init(&wait->entry); |
921b9054 | 3412 | io_req_task_queue(req); |
bcf5a063 JA |
3413 | return 1; |
3414 | } | |
3415 | ||
c1dd91d1 JA |
3416 | /* |
3417 | * This controls whether a given IO request should be armed for async page | |
3418 | * based retry. If we return false here, the request is handed to the async | |
3419 | * worker threads for retry. If we're doing buffered reads on a regular file, | |
3420 | * we prepare a private wait_page_queue entry and retry the operation. This | |
3421 | * will either succeed because the page is now uptodate and unlocked, or it | |
3422 | * will register a callback when the page is unlocked at IO completion. Through | |
3423 | * that callback, io_uring uses task_work to setup a retry of the operation. | |
3424 | * That retry will attempt the buffered read again. The retry will generally | |
3425 | * succeed, or in rare cases where it fails, we then fall back to using the | |
3426 | * async worker threads for a blocking retry. | |
3427 | */ | |
227c0c96 | 3428 | static bool io_rw_should_retry(struct io_kiocb *req) |
f67676d1 | 3429 | { |
e8c2bc1f JA |
3430 | struct io_async_rw *rw = req->async_data; |
3431 | struct wait_page_queue *wait = &rw->wpq; | |
bcf5a063 | 3432 | struct kiocb *kiocb = &req->rw.kiocb; |
f67676d1 | 3433 | |
bcf5a063 JA |
3434 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
3435 | if (req->flags & REQ_F_NOWAIT) | |
3436 | return false; | |
f67676d1 | 3437 | |
227c0c96 | 3438 | /* Only for buffered IO */ |
3b2a4439 | 3439 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
bcf5a063 | 3440 | return false; |
3b2a4439 | 3441 | |
bcf5a063 JA |
3442 | /* |
3443 | * just use poll if we can, and don't attempt if the fs doesn't | |
3444 | * support callback based unlocks | |
3445 | */ | |
3446 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) | |
3447 | return false; | |
f67676d1 | 3448 | |
3b2a4439 JA |
3449 | wait->wait.func = io_async_buf_func; |
3450 | wait->wait.private = req; | |
3451 | wait->wait.flags = 0; | |
3452 | INIT_LIST_HEAD(&wait->wait.entry); | |
3453 | kiocb->ki_flags |= IOCB_WAITQ; | |
c8d317aa | 3454 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3b2a4439 | 3455 | kiocb->ki_waitq = wait; |
3b2a4439 | 3456 | return true; |
bcf5a063 JA |
3457 | } |
3458 | ||
aeab9506 | 3459 | static inline int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) |
bcf5a063 | 3460 | { |
607b6fb8 | 3461 | if (likely(req->file->f_op->read_iter)) |
bcf5a063 | 3462 | return call_read_iter(req->file, &req->rw.kiocb, iter); |
2dd2111d | 3463 | else if (req->file->f_op->read) |
4017eb91 | 3464 | return loop_rw_iter(READ, req, iter); |
2dd2111d GH |
3465 | else |
3466 | return -EINVAL; | |
f67676d1 JA |
3467 | } |
3468 | ||
7db30437 ML |
3469 | static bool need_read_all(struct io_kiocb *req) |
3470 | { | |
3471 | return req->flags & REQ_F_ISREG || | |
3472 | S_ISBLK(file_inode(req->file)->i_mode); | |
3473 | } | |
3474 | ||
889fca73 | 3475 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 3476 | { |
607b6fb8 | 3477 | struct io_rw_state __s, *s = &__s; |
c88598a9 | 3478 | struct iovec *iovec; |
9adbd45d | 3479 | struct kiocb *kiocb = &req->rw.kiocb; |
45d189c6 | 3480 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
d886e185 | 3481 | struct io_async_rw *rw; |
cd658695 | 3482 | ssize_t ret, ret2; |
ff6165b2 | 3483 | |
607b6fb8 PB |
3484 | if (!req_has_async_data(req)) { |
3485 | ret = io_import_iovec(READ, req, &iovec, s, issue_flags); | |
3486 | if (unlikely(ret < 0)) | |
3487 | return ret; | |
3488 | } else { | |
d886e185 | 3489 | rw = req->async_data; |
c88598a9 | 3490 | s = &rw->s; |
cd658695 JA |
3491 | /* |
3492 | * We come here from an earlier attempt, restore our state to | |
3493 | * match in case it doesn't. It's cheap enough that we don't | |
3494 | * need to make this conditional. | |
3495 | */ | |
c88598a9 | 3496 | iov_iter_restore(&s->iter, &s->iter_state); |
2846c481 | 3497 | iovec = NULL; |
2846c481 | 3498 | } |
c88598a9 | 3499 | req->result = iov_iter_count(&s->iter); |
2b188cc1 | 3500 | |
607b6fb8 PB |
3501 | if (force_nonblock) { |
3502 | /* If the file doesn't support async, just async punt */ | |
35645ac3 | 3503 | if (unlikely(!io_file_supports_nowait(req))) { |
607b6fb8 PB |
3504 | ret = io_setup_async_rw(req, iovec, s, true); |
3505 | return ret ?: -EAGAIN; | |
3506 | } | |
a88fc400 | 3507 | kiocb->ki_flags |= IOCB_NOWAIT; |
607b6fb8 PB |
3508 | } else { |
3509 | /* Ensure we clear previously set non-block flag */ | |
3510 | kiocb->ki_flags &= ~IOCB_NOWAIT; | |
6713e7a6 | 3511 | } |
9e645e11 | 3512 | |
cd658695 | 3513 | ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), req->result); |
5ea5dd45 PB |
3514 | if (unlikely(ret)) { |
3515 | kfree(iovec); | |
3516 | return ret; | |
3517 | } | |
2b188cc1 | 3518 | |
c88598a9 | 3519 | ret = io_iter_do_read(req, &s->iter); |
32960613 | 3520 | |
230d50d4 | 3521 | if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) { |
6ad7f233 | 3522 | req->flags &= ~REQ_F_REISSUE; |
eefdf30f JA |
3523 | /* IOPOLL retry should happen for io-wq threads */ |
3524 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
f91daf56 | 3525 | goto done; |
75c668cd PB |
3526 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3527 | if (req->flags & REQ_F_NOWAIT) | |
355afaeb | 3528 | goto done; |
f38c7e3a | 3529 | ret = 0; |
230d50d4 JA |
3530 | } else if (ret == -EIOCBQUEUED) { |
3531 | goto out_free; | |
f80a50a6 | 3532 | } else if (ret == req->result || ret <= 0 || !force_nonblock || |
7db30437 | 3533 | (req->flags & REQ_F_NOWAIT) || !need_read_all(req)) { |
7335e3bf | 3534 | /* read all, failed, already did sync or don't want to retry */ |
00d23d51 | 3535 | goto done; |
227c0c96 JA |
3536 | } |
3537 | ||
cd658695 JA |
3538 | /* |
3539 | * Don't depend on the iter state matching what was consumed, or being | |
3540 | * untouched in case of error. Restore it and we'll advance it | |
3541 | * manually if we need to. | |
3542 | */ | |
c88598a9 | 3543 | iov_iter_restore(&s->iter, &s->iter_state); |
cd658695 | 3544 | |
c88598a9 | 3545 | ret2 = io_setup_async_rw(req, iovec, s, true); |
6bf985dc PB |
3546 | if (ret2) |
3547 | return ret2; | |
3548 | ||
fe1cdd55 | 3549 | iovec = NULL; |
e8c2bc1f | 3550 | rw = req->async_data; |
c88598a9 | 3551 | s = &rw->s; |
cd658695 JA |
3552 | /* |
3553 | * Now use our persistent iterator and state, if we aren't already. | |
3554 | * We've restored and mapped the iter to match. | |
3555 | */ | |
227c0c96 | 3556 | |
b23df91b | 3557 | do { |
cd658695 JA |
3558 | /* |
3559 | * We end up here because of a partial read, either from | |
3560 | * above or inside this loop. Advance the iter by the bytes | |
3561 | * that were consumed. | |
3562 | */ | |
c88598a9 PB |
3563 | iov_iter_advance(&s->iter, ret); |
3564 | if (!iov_iter_count(&s->iter)) | |
cd658695 | 3565 | break; |
b23df91b | 3566 | rw->bytes_done += ret; |
c88598a9 | 3567 | iov_iter_save_state(&s->iter, &s->iter_state); |
cd658695 | 3568 | |
b23df91b PB |
3569 | /* if we can retry, do so with the callbacks armed */ |
3570 | if (!io_rw_should_retry(req)) { | |
3571 | kiocb->ki_flags &= ~IOCB_WAITQ; | |
3572 | return -EAGAIN; | |
3573 | } | |
3574 | ||
3575 | /* | |
3576 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If | |
3577 | * we get -EIOCBQUEUED, then we'll get a notification when the | |
3578 | * desired page gets unlocked. We can also get a partial read | |
3579 | * here, and if we do, then just retry at the new offset. | |
3580 | */ | |
c88598a9 | 3581 | ret = io_iter_do_read(req, &s->iter); |
b23df91b PB |
3582 | if (ret == -EIOCBQUEUED) |
3583 | return 0; | |
227c0c96 | 3584 | /* we got some bytes, but not all. retry. */ |
b5b0ecb7 | 3585 | kiocb->ki_flags &= ~IOCB_WAITQ; |
c88598a9 | 3586 | iov_iter_restore(&s->iter, &s->iter_state); |
cd658695 | 3587 | } while (ret > 0); |
227c0c96 | 3588 | done: |
889fca73 | 3589 | kiocb_done(kiocb, ret, issue_flags); |
fe1cdd55 PB |
3590 | out_free: |
3591 | /* it's faster to check here then delegate to kfree */ | |
3592 | if (iovec) | |
3593 | kfree(iovec); | |
5ea5dd45 | 3594 | return 0; |
2b188cc1 JA |
3595 | } |
3596 | ||
73debe68 | 3597 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3598 | { |
3529d8c2 JA |
3599 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
3600 | return -EBADF; | |
3884b83d | 3601 | req->rw.kiocb.ki_hint = ki_hint_validate(file_write_hint(req->file)); |
b9a6b8f9 | 3602 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3603 | } |
3604 | ||
889fca73 | 3605 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 3606 | { |
607b6fb8 | 3607 | struct io_rw_state __s, *s = &__s; |
c88598a9 | 3608 | struct iovec *iovec; |
9adbd45d | 3609 | struct kiocb *kiocb = &req->rw.kiocb; |
45d189c6 | 3610 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
cd658695 | 3611 | ssize_t ret, ret2; |
2b188cc1 | 3612 | |
607b6fb8 | 3613 | if (!req_has_async_data(req)) { |
5e49c973 PB |
3614 | ret = io_import_iovec(WRITE, req, &iovec, s, issue_flags); |
3615 | if (unlikely(ret < 0)) | |
2846c481 | 3616 | return ret; |
607b6fb8 PB |
3617 | } else { |
3618 | struct io_async_rw *rw = req->async_data; | |
3619 | ||
3620 | s = &rw->s; | |
3621 | iov_iter_restore(&s->iter, &s->iter_state); | |
2846c481 | 3622 | iovec = NULL; |
2846c481 | 3623 | } |
c88598a9 | 3624 | req->result = iov_iter_count(&s->iter); |
2b188cc1 | 3625 | |
607b6fb8 PB |
3626 | if (force_nonblock) { |
3627 | /* If the file doesn't support async, just async punt */ | |
35645ac3 | 3628 | if (unlikely(!io_file_supports_nowait(req))) |
607b6fb8 | 3629 | goto copy_iov; |
fd6c2e4c | 3630 | |
607b6fb8 PB |
3631 | /* file path doesn't support NOWAIT for non-direct_IO */ |
3632 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
3633 | (req->flags & REQ_F_ISREG)) | |
3634 | goto copy_iov; | |
31b51510 | 3635 | |
607b6fb8 PB |
3636 | kiocb->ki_flags |= IOCB_NOWAIT; |
3637 | } else { | |
3638 | /* Ensure we clear previously set non-block flag */ | |
3639 | kiocb->ki_flags &= ~IOCB_NOWAIT; | |
3640 | } | |
31b51510 | 3641 | |
cd658695 | 3642 | ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), req->result); |
fa15bafb PB |
3643 | if (unlikely(ret)) |
3644 | goto out_free; | |
4ed734b0 | 3645 | |
fa15bafb PB |
3646 | /* |
3647 | * Open-code file_start_write here to grab freeze protection, | |
3648 | * which will be released by another thread in | |
3649 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
3650 | * released so that it doesn't complain about the held lock when | |
3651 | * we return to userspace. | |
3652 | */ | |
3653 | if (req->flags & REQ_F_ISREG) { | |
8a3c84b6 | 3654 | sb_start_write(file_inode(req->file)->i_sb); |
fa15bafb PB |
3655 | __sb_writers_release(file_inode(req->file)->i_sb, |
3656 | SB_FREEZE_WRITE); | |
3657 | } | |
3658 | kiocb->ki_flags |= IOCB_WRITE; | |
4ed734b0 | 3659 | |
35645ac3 | 3660 | if (likely(req->file->f_op->write_iter)) |
c88598a9 | 3661 | ret2 = call_write_iter(req->file, kiocb, &s->iter); |
2dd2111d | 3662 | else if (req->file->f_op->write) |
c88598a9 | 3663 | ret2 = loop_rw_iter(WRITE, req, &s->iter); |
2dd2111d GH |
3664 | else |
3665 | ret2 = -EINVAL; | |
4ed734b0 | 3666 | |
6ad7f233 PB |
3667 | if (req->flags & REQ_F_REISSUE) { |
3668 | req->flags &= ~REQ_F_REISSUE; | |
230d50d4 | 3669 | ret2 = -EAGAIN; |
6ad7f233 | 3670 | } |
230d50d4 | 3671 | |
fa15bafb PB |
3672 | /* |
3673 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just | |
3674 | * retry them without IOCB_NOWAIT. | |
3675 | */ | |
3676 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) | |
3677 | ret2 = -EAGAIN; | |
75c668cd PB |
3678 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3679 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) | |
355afaeb | 3680 | goto done; |
fa15bafb | 3681 | if (!force_nonblock || ret2 != -EAGAIN) { |
eefdf30f | 3682 | /* IOPOLL retry should happen for io-wq threads */ |
b10841c9 | 3683 | if (ret2 == -EAGAIN && (req->ctx->flags & IORING_SETUP_IOPOLL)) |
eefdf30f | 3684 | goto copy_iov; |
355afaeb | 3685 | done: |
889fca73 | 3686 | kiocb_done(kiocb, ret2, issue_flags); |
fa15bafb | 3687 | } else { |
f67676d1 | 3688 | copy_iov: |
c88598a9 PB |
3689 | iov_iter_restore(&s->iter, &s->iter_state); |
3690 | ret = io_setup_async_rw(req, iovec, s, false); | |
6bf985dc | 3691 | return ret ?: -EAGAIN; |
2b188cc1 | 3692 | } |
31b51510 | 3693 | out_free: |
f261c168 | 3694 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 3695 | if (iovec) |
6f2cc166 | 3696 | kfree(iovec); |
2b188cc1 JA |
3697 | return ret; |
3698 | } | |
3699 | ||
80a261fd JA |
3700 | static int io_renameat_prep(struct io_kiocb *req, |
3701 | const struct io_uring_sqe *sqe) | |
3702 | { | |
3703 | struct io_rename *ren = &req->rename; | |
3704 | const char __user *oldf, *newf; | |
3705 | ||
ed7eb259 JA |
3706 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3707 | return -EINVAL; | |
26578cda | 3708 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
ed7eb259 | 3709 | return -EINVAL; |
80a261fd JA |
3710 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
3711 | return -EBADF; | |
3712 | ||
3713 | ren->old_dfd = READ_ONCE(sqe->fd); | |
3714 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3715 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3716 | ren->new_dfd = READ_ONCE(sqe->len); | |
3717 | ren->flags = READ_ONCE(sqe->rename_flags); | |
3718 | ||
3719 | ren->oldpath = getname(oldf); | |
3720 | if (IS_ERR(ren->oldpath)) | |
3721 | return PTR_ERR(ren->oldpath); | |
3722 | ||
3723 | ren->newpath = getname(newf); | |
3724 | if (IS_ERR(ren->newpath)) { | |
3725 | putname(ren->oldpath); | |
3726 | return PTR_ERR(ren->newpath); | |
3727 | } | |
3728 | ||
3729 | req->flags |= REQ_F_NEED_CLEANUP; | |
3730 | return 0; | |
3731 | } | |
3732 | ||
45d189c6 | 3733 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
80a261fd JA |
3734 | { |
3735 | struct io_rename *ren = &req->rename; | |
3736 | int ret; | |
3737 | ||
45d189c6 | 3738 | if (issue_flags & IO_URING_F_NONBLOCK) |
80a261fd JA |
3739 | return -EAGAIN; |
3740 | ||
3741 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, | |
3742 | ren->newpath, ren->flags); | |
3743 | ||
3744 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3745 | if (ret < 0) | |
93d2bcd2 | 3746 | req_set_fail(req); |
80a261fd JA |
3747 | io_req_complete(req, ret); |
3748 | return 0; | |
3749 | } | |
3750 | ||
14a1143b JA |
3751 | static int io_unlinkat_prep(struct io_kiocb *req, |
3752 | const struct io_uring_sqe *sqe) | |
3753 | { | |
3754 | struct io_unlink *un = &req->unlink; | |
3755 | const char __user *fname; | |
3756 | ||
22634bc5 JA |
3757 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3758 | return -EINVAL; | |
26578cda PB |
3759 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
3760 | sqe->splice_fd_in) | |
22634bc5 | 3761 | return -EINVAL; |
14a1143b JA |
3762 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
3763 | return -EBADF; | |
3764 | ||
3765 | un->dfd = READ_ONCE(sqe->fd); | |
3766 | ||
3767 | un->flags = READ_ONCE(sqe->unlink_flags); | |
3768 | if (un->flags & ~AT_REMOVEDIR) | |
3769 | return -EINVAL; | |
3770 | ||
3771 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3772 | un->filename = getname(fname); | |
3773 | if (IS_ERR(un->filename)) | |
3774 | return PTR_ERR(un->filename); | |
3775 | ||
3776 | req->flags |= REQ_F_NEED_CLEANUP; | |
3777 | return 0; | |
3778 | } | |
3779 | ||
45d189c6 | 3780 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
14a1143b JA |
3781 | { |
3782 | struct io_unlink *un = &req->unlink; | |
3783 | int ret; | |
3784 | ||
45d189c6 | 3785 | if (issue_flags & IO_URING_F_NONBLOCK) |
14a1143b JA |
3786 | return -EAGAIN; |
3787 | ||
3788 | if (un->flags & AT_REMOVEDIR) | |
3789 | ret = do_rmdir(un->dfd, un->filename); | |
3790 | else | |
3791 | ret = do_unlinkat(un->dfd, un->filename); | |
3792 | ||
3793 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3794 | if (ret < 0) | |
93d2bcd2 | 3795 | req_set_fail(req); |
14a1143b JA |
3796 | io_req_complete(req, ret); |
3797 | return 0; | |
3798 | } | |
3799 | ||
e34a02dc DK |
3800 | static int io_mkdirat_prep(struct io_kiocb *req, |
3801 | const struct io_uring_sqe *sqe) | |
3802 | { | |
3803 | struct io_mkdir *mkd = &req->mkdir; | |
3804 | const char __user *fname; | |
3805 | ||
3806 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3807 | return -EINVAL; | |
3808 | if (sqe->ioprio || sqe->off || sqe->rw_flags || sqe->buf_index || | |
3809 | sqe->splice_fd_in) | |
3810 | return -EINVAL; | |
3811 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3812 | return -EBADF; | |
3813 | ||
3814 | mkd->dfd = READ_ONCE(sqe->fd); | |
3815 | mkd->mode = READ_ONCE(sqe->len); | |
3816 | ||
3817 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3818 | mkd->filename = getname(fname); | |
3819 | if (IS_ERR(mkd->filename)) | |
3820 | return PTR_ERR(mkd->filename); | |
3821 | ||
3822 | req->flags |= REQ_F_NEED_CLEANUP; | |
3823 | return 0; | |
3824 | } | |
3825 | ||
04f34081 | 3826 | static int io_mkdirat(struct io_kiocb *req, unsigned int issue_flags) |
e34a02dc DK |
3827 | { |
3828 | struct io_mkdir *mkd = &req->mkdir; | |
3829 | int ret; | |
3830 | ||
3831 | if (issue_flags & IO_URING_F_NONBLOCK) | |
3832 | return -EAGAIN; | |
3833 | ||
3834 | ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode); | |
3835 | ||
3836 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3837 | if (ret < 0) | |
3838 | req_set_fail(req); | |
3839 | io_req_complete(req, ret); | |
3840 | return 0; | |
3841 | } | |
3842 | ||
7a8721f8 DK |
3843 | static int io_symlinkat_prep(struct io_kiocb *req, |
3844 | const struct io_uring_sqe *sqe) | |
3845 | { | |
3846 | struct io_symlink *sl = &req->symlink; | |
3847 | const char __user *oldpath, *newpath; | |
3848 | ||
3849 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3850 | return -EINVAL; | |
3851 | if (sqe->ioprio || sqe->len || sqe->rw_flags || sqe->buf_index || | |
3852 | sqe->splice_fd_in) | |
3853 | return -EINVAL; | |
3854 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3855 | return -EBADF; | |
3856 | ||
3857 | sl->new_dfd = READ_ONCE(sqe->fd); | |
3858 | oldpath = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3859 | newpath = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3860 | ||
3861 | sl->oldpath = getname(oldpath); | |
3862 | if (IS_ERR(sl->oldpath)) | |
3863 | return PTR_ERR(sl->oldpath); | |
3864 | ||
3865 | sl->newpath = getname(newpath); | |
3866 | if (IS_ERR(sl->newpath)) { | |
3867 | putname(sl->oldpath); | |
3868 | return PTR_ERR(sl->newpath); | |
3869 | } | |
3870 | ||
3871 | req->flags |= REQ_F_NEED_CLEANUP; | |
3872 | return 0; | |
3873 | } | |
3874 | ||
04f34081 | 3875 | static int io_symlinkat(struct io_kiocb *req, unsigned int issue_flags) |
7a8721f8 DK |
3876 | { |
3877 | struct io_symlink *sl = &req->symlink; | |
3878 | int ret; | |
3879 | ||
3880 | if (issue_flags & IO_URING_F_NONBLOCK) | |
3881 | return -EAGAIN; | |
3882 | ||
3883 | ret = do_symlinkat(sl->oldpath, sl->new_dfd, sl->newpath); | |
3884 | ||
3885 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3886 | if (ret < 0) | |
3887 | req_set_fail(req); | |
3888 | io_req_complete(req, ret); | |
3889 | return 0; | |
3890 | } | |
3891 | ||
cf30da90 DK |
3892 | static int io_linkat_prep(struct io_kiocb *req, |
3893 | const struct io_uring_sqe *sqe) | |
3894 | { | |
3895 | struct io_hardlink *lnk = &req->hardlink; | |
3896 | const char __user *oldf, *newf; | |
3897 | ||
3898 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3899 | return -EINVAL; | |
3900 | if (sqe->ioprio || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in) | |
3901 | return -EINVAL; | |
3902 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3903 | return -EBADF; | |
3904 | ||
3905 | lnk->old_dfd = READ_ONCE(sqe->fd); | |
3906 | lnk->new_dfd = READ_ONCE(sqe->len); | |
3907 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3908 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3909 | lnk->flags = READ_ONCE(sqe->hardlink_flags); | |
3910 | ||
3911 | lnk->oldpath = getname(oldf); | |
3912 | if (IS_ERR(lnk->oldpath)) | |
3913 | return PTR_ERR(lnk->oldpath); | |
3914 | ||
3915 | lnk->newpath = getname(newf); | |
3916 | if (IS_ERR(lnk->newpath)) { | |
3917 | putname(lnk->oldpath); | |
3918 | return PTR_ERR(lnk->newpath); | |
3919 | } | |
3920 | ||
3921 | req->flags |= REQ_F_NEED_CLEANUP; | |
3922 | return 0; | |
3923 | } | |
3924 | ||
04f34081 | 3925 | static int io_linkat(struct io_kiocb *req, unsigned int issue_flags) |
cf30da90 DK |
3926 | { |
3927 | struct io_hardlink *lnk = &req->hardlink; | |
3928 | int ret; | |
3929 | ||
3930 | if (issue_flags & IO_URING_F_NONBLOCK) | |
3931 | return -EAGAIN; | |
3932 | ||
3933 | ret = do_linkat(lnk->old_dfd, lnk->oldpath, lnk->new_dfd, | |
3934 | lnk->newpath, lnk->flags); | |
3935 | ||
3936 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3937 | if (ret < 0) | |
3938 | req_set_fail(req); | |
3939 | io_req_complete(req, ret); | |
3940 | return 0; | |
3941 | } | |
3942 | ||
36f4fa68 JA |
3943 | static int io_shutdown_prep(struct io_kiocb *req, |
3944 | const struct io_uring_sqe *sqe) | |
3945 | { | |
3946 | #if defined(CONFIG_NET) | |
3947 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3948 | return -EINVAL; | |
26578cda PB |
3949 | if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || |
3950 | sqe->buf_index || sqe->splice_fd_in)) | |
36f4fa68 JA |
3951 | return -EINVAL; |
3952 | ||
3953 | req->shutdown.how = READ_ONCE(sqe->len); | |
3954 | return 0; | |
3955 | #else | |
3956 | return -EOPNOTSUPP; | |
3957 | #endif | |
3958 | } | |
3959 | ||
45d189c6 | 3960 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
36f4fa68 JA |
3961 | { |
3962 | #if defined(CONFIG_NET) | |
3963 | struct socket *sock; | |
3964 | int ret; | |
3965 | ||
45d189c6 | 3966 | if (issue_flags & IO_URING_F_NONBLOCK) |
36f4fa68 JA |
3967 | return -EAGAIN; |
3968 | ||
48aba79b | 3969 | sock = sock_from_file(req->file); |
36f4fa68 | 3970 | if (unlikely(!sock)) |
48aba79b | 3971 | return -ENOTSOCK; |
36f4fa68 JA |
3972 | |
3973 | ret = __sys_shutdown_sock(sock, req->shutdown.how); | |
a146468d | 3974 | if (ret < 0) |
93d2bcd2 | 3975 | req_set_fail(req); |
36f4fa68 JA |
3976 | io_req_complete(req, ret); |
3977 | return 0; | |
3978 | #else | |
3979 | return -EOPNOTSUPP; | |
3980 | #endif | |
3981 | } | |
3982 | ||
f2a8d5c7 PB |
3983 | static int __io_splice_prep(struct io_kiocb *req, |
3984 | const struct io_uring_sqe *sqe) | |
7d67af2c | 3985 | { |
fe7e3257 | 3986 | struct io_splice *sp = &req->splice; |
7d67af2c | 3987 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
7d67af2c | 3988 | |
3232dd02 PB |
3989 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3990 | return -EINVAL; | |
7d67af2c PB |
3991 | |
3992 | sp->file_in = NULL; | |
7d67af2c PB |
3993 | sp->len = READ_ONCE(sqe->len); |
3994 | sp->flags = READ_ONCE(sqe->splice_flags); | |
3995 | ||
3996 | if (unlikely(sp->flags & ~valid_flags)) | |
3997 | return -EINVAL; | |
3998 | ||
62906e89 | 3999 | sp->file_in = io_file_get(req->ctx, req, READ_ONCE(sqe->splice_fd_in), |
8371adf5 PB |
4000 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
4001 | if (!sp->file_in) | |
4002 | return -EBADF; | |
7d67af2c | 4003 | req->flags |= REQ_F_NEED_CLEANUP; |
7d67af2c PB |
4004 | return 0; |
4005 | } | |
4006 | ||
f2a8d5c7 PB |
4007 | static int io_tee_prep(struct io_kiocb *req, |
4008 | const struct io_uring_sqe *sqe) | |
4009 | { | |
4010 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) | |
4011 | return -EINVAL; | |
4012 | return __io_splice_prep(req, sqe); | |
4013 | } | |
4014 | ||
45d189c6 | 4015 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
f2a8d5c7 PB |
4016 | { |
4017 | struct io_splice *sp = &req->splice; | |
4018 | struct file *in = sp->file_in; | |
4019 | struct file *out = sp->file_out; | |
4020 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
4021 | long ret = 0; | |
4022 | ||
45d189c6 | 4023 | if (issue_flags & IO_URING_F_NONBLOCK) |
f2a8d5c7 PB |
4024 | return -EAGAIN; |
4025 | if (sp->len) | |
4026 | ret = do_tee(in, out, sp->len, flags); | |
4027 | ||
e1d767f0 PB |
4028 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
4029 | io_put_file(in); | |
f2a8d5c7 PB |
4030 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4031 | ||
f2a8d5c7 | 4032 | if (ret != sp->len) |
93d2bcd2 | 4033 | req_set_fail(req); |
e1e16097 | 4034 | io_req_complete(req, ret); |
f2a8d5c7 PB |
4035 | return 0; |
4036 | } | |
4037 | ||
4038 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4039 | { | |
fe7e3257 | 4040 | struct io_splice *sp = &req->splice; |
f2a8d5c7 PB |
4041 | |
4042 | sp->off_in = READ_ONCE(sqe->splice_off_in); | |
4043 | sp->off_out = READ_ONCE(sqe->off); | |
4044 | return __io_splice_prep(req, sqe); | |
4045 | } | |
4046 | ||
45d189c6 | 4047 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
7d67af2c PB |
4048 | { |
4049 | struct io_splice *sp = &req->splice; | |
4050 | struct file *in = sp->file_in; | |
4051 | struct file *out = sp->file_out; | |
4052 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
4053 | loff_t *poff_in, *poff_out; | |
c9687426 | 4054 | long ret = 0; |
7d67af2c | 4055 | |
45d189c6 | 4056 | if (issue_flags & IO_URING_F_NONBLOCK) |
2fb3e822 | 4057 | return -EAGAIN; |
7d67af2c PB |
4058 | |
4059 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; | |
4060 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; | |
c9687426 | 4061 | |
948a7749 | 4062 | if (sp->len) |
c9687426 | 4063 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
7d67af2c | 4064 | |
e1d767f0 PB |
4065 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
4066 | io_put_file(in); | |
7d67af2c PB |
4067 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4068 | ||
7d67af2c | 4069 | if (ret != sp->len) |
93d2bcd2 | 4070 | req_set_fail(req); |
e1e16097 | 4071 | io_req_complete(req, ret); |
7d67af2c PB |
4072 | return 0; |
4073 | } | |
4074 | ||
2b188cc1 JA |
4075 | /* |
4076 | * IORING_OP_NOP just posts a completion event, nothing else. | |
4077 | */ | |
889fca73 | 4078 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
4079 | { |
4080 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 4081 | |
def596e9 JA |
4082 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4083 | return -EINVAL; | |
4084 | ||
889fca73 | 4085 | __io_req_complete(req, issue_flags, 0, 0); |
2b188cc1 JA |
4086 | return 0; |
4087 | } | |
4088 | ||
1155c76a | 4089 | static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 4090 | { |
6b06314c | 4091 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 4092 | |
09bb8394 JA |
4093 | if (!req->file) |
4094 | return -EBADF; | |
c992fe29 | 4095 | |
6b06314c | 4096 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 4097 | return -EINVAL; |
26578cda PB |
4098 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
4099 | sqe->splice_fd_in)) | |
c992fe29 CH |
4100 | return -EINVAL; |
4101 | ||
8ed8d3c3 JA |
4102 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
4103 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
4104 | return -EINVAL; | |
4105 | ||
4106 | req->sync.off = READ_ONCE(sqe->off); | |
4107 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
4108 | return 0; |
4109 | } | |
4110 | ||
45d189c6 | 4111 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4112 | { |
8ed8d3c3 | 4113 | loff_t end = req->sync.off + req->sync.len; |
8ed8d3c3 JA |
4114 | int ret; |
4115 | ||
ac45abc0 | 4116 | /* fsync always requires a blocking context */ |
45d189c6 | 4117 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4118 | return -EAGAIN; |
4119 | ||
9adbd45d | 4120 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
4121 | end > 0 ? end : LLONG_MAX, |
4122 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
4123 | if (ret < 0) | |
93d2bcd2 | 4124 | req_set_fail(req); |
e1e16097 | 4125 | io_req_complete(req, ret); |
c992fe29 CH |
4126 | return 0; |
4127 | } | |
4128 | ||
d63d1b5e JA |
4129 | static int io_fallocate_prep(struct io_kiocb *req, |
4130 | const struct io_uring_sqe *sqe) | |
4131 | { | |
26578cda PB |
4132 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags || |
4133 | sqe->splice_fd_in) | |
d63d1b5e | 4134 | return -EINVAL; |
3232dd02 PB |
4135 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4136 | return -EINVAL; | |
d63d1b5e JA |
4137 | |
4138 | req->sync.off = READ_ONCE(sqe->off); | |
4139 | req->sync.len = READ_ONCE(sqe->addr); | |
4140 | req->sync.mode = READ_ONCE(sqe->len); | |
4141 | return 0; | |
4142 | } | |
4143 | ||
45d189c6 | 4144 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
5d17b4a4 | 4145 | { |
ac45abc0 PB |
4146 | int ret; |
4147 | ||
d63d1b5e | 4148 | /* fallocate always requiring blocking context */ |
45d189c6 | 4149 | if (issue_flags & IO_URING_F_NONBLOCK) |
5d17b4a4 | 4150 | return -EAGAIN; |
ac45abc0 PB |
4151 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
4152 | req->sync.len); | |
ac45abc0 | 4153 | if (ret < 0) |
93d2bcd2 | 4154 | req_set_fail(req); |
e1e16097 | 4155 | io_req_complete(req, ret); |
5d17b4a4 JA |
4156 | return 0; |
4157 | } | |
4158 | ||
ec65fea5 | 4159 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 4160 | { |
f8748881 | 4161 | const char __user *fname; |
15b71abe | 4162 | int ret; |
b7bb4f7d | 4163 | |
d3fddf6d PB |
4164 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4165 | return -EINVAL; | |
b9445598 | 4166 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
15b71abe | 4167 | return -EINVAL; |
ec65fea5 | 4168 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
cf3040ca | 4169 | return -EBADF; |
03b1230c | 4170 | |
ec65fea5 PB |
4171 | /* open.how should be already initialised */ |
4172 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
08a1d26e | 4173 | req->open.how.flags |= O_LARGEFILE; |
3529d8c2 | 4174 | |
25e72d10 PB |
4175 | req->open.dfd = READ_ONCE(sqe->fd); |
4176 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
f8748881 | 4177 | req->open.filename = getname(fname); |
15b71abe JA |
4178 | if (IS_ERR(req->open.filename)) { |
4179 | ret = PTR_ERR(req->open.filename); | |
4180 | req->open.filename = NULL; | |
4181 | return ret; | |
4182 | } | |
b9445598 PB |
4183 | |
4184 | req->open.file_slot = READ_ONCE(sqe->file_index); | |
4185 | if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC)) | |
4186 | return -EINVAL; | |
4187 | ||
4022e7af | 4188 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
8fef80bf | 4189 | req->flags |= REQ_F_NEED_CLEANUP; |
15b71abe | 4190 | return 0; |
03b1230c JA |
4191 | } |
4192 | ||
ec65fea5 PB |
4193 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4194 | { | |
d3fddf6d PB |
4195 | u64 mode = READ_ONCE(sqe->len); |
4196 | u64 flags = READ_ONCE(sqe->open_flags); | |
ec65fea5 | 4197 | |
ec65fea5 PB |
4198 | req->open.how = build_open_how(flags, mode); |
4199 | return __io_openat_prep(req, sqe); | |
4200 | } | |
4201 | ||
cebdb986 | 4202 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 4203 | { |
cebdb986 | 4204 | struct open_how __user *how; |
cebdb986 | 4205 | size_t len; |
0fa03c62 JA |
4206 | int ret; |
4207 | ||
cebdb986 JA |
4208 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4209 | len = READ_ONCE(sqe->len); | |
cebdb986 JA |
4210 | if (len < OPEN_HOW_SIZE_VER0) |
4211 | return -EINVAL; | |
3529d8c2 | 4212 | |
cebdb986 JA |
4213 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
4214 | len); | |
4215 | if (ret) | |
4216 | return ret; | |
3529d8c2 | 4217 | |
ec65fea5 | 4218 | return __io_openat_prep(req, sqe); |
cebdb986 JA |
4219 | } |
4220 | ||
45d189c6 | 4221 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
15b71abe JA |
4222 | { |
4223 | struct open_flags op; | |
15b71abe | 4224 | struct file *file; |
b9445598 PB |
4225 | bool resolve_nonblock, nonblock_set; |
4226 | bool fixed = !!req->open.file_slot; | |
15b71abe JA |
4227 | int ret; |
4228 | ||
cebdb986 | 4229 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
4230 | if (ret) |
4231 | goto err; | |
3a81fd02 JA |
4232 | nonblock_set = op.open_flag & O_NONBLOCK; |
4233 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; | |
45d189c6 | 4234 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3a81fd02 JA |
4235 | /* |
4236 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, | |
4237 | * it'll always -EAGAIN | |
4238 | */ | |
4239 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) | |
4240 | return -EAGAIN; | |
4241 | op.lookup_flags |= LOOKUP_CACHED; | |
4242 | op.open_flag |= O_NONBLOCK; | |
4243 | } | |
15b71abe | 4244 | |
b9445598 PB |
4245 | if (!fixed) { |
4246 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); | |
4247 | if (ret < 0) | |
4248 | goto err; | |
4249 | } | |
15b71abe JA |
4250 | |
4251 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
12dcb58a | 4252 | if (IS_ERR(file)) { |
944d1444 | 4253 | /* |
12dcb58a PB |
4254 | * We could hang on to this 'fd' on retrying, but seems like |
4255 | * marginal gain for something that is now known to be a slower | |
4256 | * path. So just put it, and we'll get a new one when we retry. | |
944d1444 | 4257 | */ |
b9445598 PB |
4258 | if (!fixed) |
4259 | put_unused_fd(ret); | |
3a81fd02 | 4260 | |
15b71abe | 4261 | ret = PTR_ERR(file); |
12dcb58a PB |
4262 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
4263 | if (ret == -EAGAIN && | |
4264 | (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK))) | |
4265 | return -EAGAIN; | |
4266 | goto err; | |
15b71abe | 4267 | } |
12dcb58a PB |
4268 | |
4269 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) | |
4270 | file->f_flags &= ~O_NONBLOCK; | |
4271 | fsnotify_open(file); | |
b9445598 PB |
4272 | |
4273 | if (!fixed) | |
4274 | fd_install(ret, file); | |
4275 | else | |
4276 | ret = io_install_fixed_file(req, file, issue_flags, | |
4277 | req->open.file_slot - 1); | |
15b71abe JA |
4278 | err: |
4279 | putname(req->open.filename); | |
8fef80bf | 4280 | req->flags &= ~REQ_F_NEED_CLEANUP; |
15b71abe | 4281 | if (ret < 0) |
93d2bcd2 | 4282 | req_set_fail(req); |
0bdf3398 | 4283 | __io_req_complete(req, issue_flags, ret, 0); |
15b71abe JA |
4284 | return 0; |
4285 | } | |
4286 | ||
45d189c6 | 4287 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
cebdb986 | 4288 | { |
e45cff58 | 4289 | return io_openat2(req, issue_flags); |
cebdb986 JA |
4290 | } |
4291 | ||
067524e9 JA |
4292 | static int io_remove_buffers_prep(struct io_kiocb *req, |
4293 | const struct io_uring_sqe *sqe) | |
4294 | { | |
4295 | struct io_provide_buf *p = &req->pbuf; | |
4296 | u64 tmp; | |
4297 | ||
26578cda PB |
4298 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off || |
4299 | sqe->splice_fd_in) | |
067524e9 JA |
4300 | return -EINVAL; |
4301 | ||
4302 | tmp = READ_ONCE(sqe->fd); | |
4303 | if (!tmp || tmp > USHRT_MAX) | |
4304 | return -EINVAL; | |
4305 | ||
4306 | memset(p, 0, sizeof(*p)); | |
4307 | p->nbufs = tmp; | |
4308 | p->bgid = READ_ONCE(sqe->buf_group); | |
4309 | return 0; | |
4310 | } | |
4311 | ||
4312 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, | |
4313 | int bgid, unsigned nbufs) | |
4314 | { | |
4315 | unsigned i = 0; | |
4316 | ||
4317 | /* shouldn't happen */ | |
4318 | if (!nbufs) | |
4319 | return 0; | |
4320 | ||
4321 | /* the head kbuf is the list itself */ | |
4322 | while (!list_empty(&buf->list)) { | |
4323 | struct io_buffer *nxt; | |
4324 | ||
4325 | nxt = list_first_entry(&buf->list, struct io_buffer, list); | |
4326 | list_del(&nxt->list); | |
4327 | kfree(nxt); | |
4328 | if (++i == nbufs) | |
4329 | return i; | |
4330 | } | |
4331 | i++; | |
4332 | kfree(buf); | |
9e15c3a0 | 4333 | xa_erase(&ctx->io_buffers, bgid); |
067524e9 JA |
4334 | |
4335 | return i; | |
4336 | } | |
4337 | ||
889fca73 | 4338 | static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags) |
067524e9 JA |
4339 | { |
4340 | struct io_provide_buf *p = &req->pbuf; | |
4341 | struct io_ring_ctx *ctx = req->ctx; | |
4342 | struct io_buffer *head; | |
4343 | int ret = 0; | |
3b44b371 | 4344 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
067524e9 | 4345 | |
3b44b371 | 4346 | io_ring_submit_lock(ctx, needs_lock); |
067524e9 JA |
4347 | |
4348 | lockdep_assert_held(&ctx->uring_lock); | |
4349 | ||
4350 | ret = -ENOENT; | |
9e15c3a0 | 4351 | head = xa_load(&ctx->io_buffers, p->bgid); |
067524e9 JA |
4352 | if (head) |
4353 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); | |
067524e9 | 4354 | if (ret < 0) |
93d2bcd2 | 4355 | req_set_fail(req); |
067524e9 | 4356 | |
9fb8cb49 PB |
4357 | /* complete before unlock, IOPOLL may need the lock */ |
4358 | __io_req_complete(req, issue_flags, ret, 0); | |
3b44b371 | 4359 | io_ring_submit_unlock(ctx, needs_lock); |
067524e9 JA |
4360 | return 0; |
4361 | } | |
4362 | ||
ddf0322d JA |
4363 | static int io_provide_buffers_prep(struct io_kiocb *req, |
4364 | const struct io_uring_sqe *sqe) | |
4365 | { | |
38134ada | 4366 | unsigned long size, tmp_check; |
ddf0322d JA |
4367 | struct io_provide_buf *p = &req->pbuf; |
4368 | u64 tmp; | |
4369 | ||
26578cda | 4370 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
ddf0322d JA |
4371 | return -EINVAL; |
4372 | ||
4373 | tmp = READ_ONCE(sqe->fd); | |
4374 | if (!tmp || tmp > USHRT_MAX) | |
4375 | return -E2BIG; | |
4376 | p->nbufs = tmp; | |
4377 | p->addr = READ_ONCE(sqe->addr); | |
4378 | p->len = READ_ONCE(sqe->len); | |
4379 | ||
38134ada PB |
4380 | if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs, |
4381 | &size)) | |
4382 | return -EOVERFLOW; | |
4383 | if (check_add_overflow((unsigned long)p->addr, size, &tmp_check)) | |
4384 | return -EOVERFLOW; | |
4385 | ||
d81269fe PB |
4386 | size = (unsigned long)p->len * p->nbufs; |
4387 | if (!access_ok(u64_to_user_ptr(p->addr), size)) | |
ddf0322d JA |
4388 | return -EFAULT; |
4389 | ||
4390 | p->bgid = READ_ONCE(sqe->buf_group); | |
4391 | tmp = READ_ONCE(sqe->off); | |
4392 | if (tmp > USHRT_MAX) | |
4393 | return -E2BIG; | |
4394 | p->bid = tmp; | |
4395 | return 0; | |
4396 | } | |
4397 | ||
4398 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) | |
4399 | { | |
4400 | struct io_buffer *buf; | |
4401 | u64 addr = pbuf->addr; | |
4402 | int i, bid = pbuf->bid; | |
4403 | ||
4404 | for (i = 0; i < pbuf->nbufs; i++) { | |
9990da93 | 4405 | buf = kmalloc(sizeof(*buf), GFP_KERNEL_ACCOUNT); |
ddf0322d JA |
4406 | if (!buf) |
4407 | break; | |
4408 | ||
4409 | buf->addr = addr; | |
d1f82808 | 4410 | buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT); |
ddf0322d JA |
4411 | buf->bid = bid; |
4412 | addr += pbuf->len; | |
4413 | bid++; | |
4414 | if (!*head) { | |
4415 | INIT_LIST_HEAD(&buf->list); | |
4416 | *head = buf; | |
4417 | } else { | |
4418 | list_add_tail(&buf->list, &(*head)->list); | |
4419 | } | |
4420 | } | |
4421 | ||
4422 | return i ? i : -ENOMEM; | |
4423 | } | |
4424 | ||
889fca73 | 4425 | static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags) |
ddf0322d JA |
4426 | { |
4427 | struct io_provide_buf *p = &req->pbuf; | |
4428 | struct io_ring_ctx *ctx = req->ctx; | |
4429 | struct io_buffer *head, *list; | |
4430 | int ret = 0; | |
3b44b371 | 4431 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
ddf0322d | 4432 | |
3b44b371 | 4433 | io_ring_submit_lock(ctx, needs_lock); |
ddf0322d JA |
4434 | |
4435 | lockdep_assert_held(&ctx->uring_lock); | |
4436 | ||
9e15c3a0 | 4437 | list = head = xa_load(&ctx->io_buffers, p->bgid); |
ddf0322d JA |
4438 | |
4439 | ret = io_add_buffers(p, &head); | |
9e15c3a0 JA |
4440 | if (ret >= 0 && !list) { |
4441 | ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL); | |
4442 | if (ret < 0) | |
067524e9 | 4443 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
ddf0322d | 4444 | } |
ddf0322d | 4445 | if (ret < 0) |
93d2bcd2 | 4446 | req_set_fail(req); |
9fb8cb49 PB |
4447 | /* complete before unlock, IOPOLL may need the lock */ |
4448 | __io_req_complete(req, issue_flags, ret, 0); | |
3b44b371 | 4449 | io_ring_submit_unlock(ctx, needs_lock); |
ddf0322d | 4450 | return 0; |
cebdb986 JA |
4451 | } |
4452 | ||
3e4827b0 JA |
4453 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
4454 | const struct io_uring_sqe *sqe) | |
4455 | { | |
4456 | #if defined(CONFIG_EPOLL) | |
26578cda | 4457 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
3e4827b0 | 4458 | return -EINVAL; |
2d74d042 | 4459 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4460 | return -EINVAL; |
3e4827b0 JA |
4461 | |
4462 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
4463 | req->epoll.op = READ_ONCE(sqe->len); | |
4464 | req->epoll.fd = READ_ONCE(sqe->off); | |
4465 | ||
4466 | if (ep_op_has_event(req->epoll.op)) { | |
4467 | struct epoll_event __user *ev; | |
4468 | ||
4469 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4470 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
4471 | return -EFAULT; | |
4472 | } | |
4473 | ||
4474 | return 0; | |
4475 | #else | |
4476 | return -EOPNOTSUPP; | |
4477 | #endif | |
4478 | } | |
4479 | ||
889fca73 | 4480 | static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags) |
3e4827b0 JA |
4481 | { |
4482 | #if defined(CONFIG_EPOLL) | |
4483 | struct io_epoll *ie = &req->epoll; | |
4484 | int ret; | |
45d189c6 | 4485 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
3e4827b0 JA |
4486 | |
4487 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
4488 | if (force_nonblock && ret == -EAGAIN) | |
4489 | return -EAGAIN; | |
4490 | ||
4491 | if (ret < 0) | |
93d2bcd2 | 4492 | req_set_fail(req); |
889fca73 | 4493 | __io_req_complete(req, issue_flags, ret, 0); |
3e4827b0 JA |
4494 | return 0; |
4495 | #else | |
4496 | return -EOPNOTSUPP; | |
4497 | #endif | |
4498 | } | |
4499 | ||
c1ca757b JA |
4500 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4501 | { | |
4502 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
26578cda | 4503 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in) |
c1ca757b | 4504 | return -EINVAL; |
3232dd02 PB |
4505 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4506 | return -EINVAL; | |
c1ca757b JA |
4507 | |
4508 | req->madvise.addr = READ_ONCE(sqe->addr); | |
4509 | req->madvise.len = READ_ONCE(sqe->len); | |
4510 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4511 | return 0; | |
4512 | #else | |
4513 | return -EOPNOTSUPP; | |
4514 | #endif | |
4515 | } | |
4516 | ||
45d189c6 | 4517 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
c1ca757b JA |
4518 | { |
4519 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4520 | struct io_madvise *ma = &req->madvise; | |
4521 | int ret; | |
4522 | ||
45d189c6 | 4523 | if (issue_flags & IO_URING_F_NONBLOCK) |
c1ca757b JA |
4524 | return -EAGAIN; |
4525 | ||
0726b01e | 4526 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
c1ca757b | 4527 | if (ret < 0) |
93d2bcd2 | 4528 | req_set_fail(req); |
e1e16097 | 4529 | io_req_complete(req, ret); |
c1ca757b JA |
4530 | return 0; |
4531 | #else | |
4532 | return -EOPNOTSUPP; | |
4533 | #endif | |
4534 | } | |
4535 | ||
4840e418 JA |
4536 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4537 | { | |
26578cda | 4538 | if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in) |
4840e418 | 4539 | return -EINVAL; |
3232dd02 PB |
4540 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4541 | return -EINVAL; | |
4840e418 JA |
4542 | |
4543 | req->fadvise.offset = READ_ONCE(sqe->off); | |
4544 | req->fadvise.len = READ_ONCE(sqe->len); | |
4545 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4546 | return 0; | |
4547 | } | |
4548 | ||
45d189c6 | 4549 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
4840e418 JA |
4550 | { |
4551 | struct io_fadvise *fa = &req->fadvise; | |
4552 | int ret; | |
4553 | ||
45d189c6 | 4554 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3e69426d JA |
4555 | switch (fa->advice) { |
4556 | case POSIX_FADV_NORMAL: | |
4557 | case POSIX_FADV_RANDOM: | |
4558 | case POSIX_FADV_SEQUENTIAL: | |
4559 | break; | |
4560 | default: | |
4561 | return -EAGAIN; | |
4562 | } | |
4563 | } | |
4840e418 JA |
4564 | |
4565 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
4566 | if (ret < 0) | |
93d2bcd2 | 4567 | req_set_fail(req); |
0bdf3398 | 4568 | __io_req_complete(req, issue_flags, ret, 0); |
4840e418 JA |
4569 | return 0; |
4570 | } | |
4571 | ||
eddc7ef5 JA |
4572 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4573 | { | |
2d74d042 | 4574 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4575 | return -EINVAL; |
26578cda | 4576 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
eddc7ef5 | 4577 | return -EINVAL; |
9c280f90 | 4578 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4579 | return -EBADF; |
eddc7ef5 | 4580 | |
1d9e1288 BM |
4581 | req->statx.dfd = READ_ONCE(sqe->fd); |
4582 | req->statx.mask = READ_ONCE(sqe->len); | |
e62753e4 | 4583 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
1d9e1288 BM |
4584 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4585 | req->statx.flags = READ_ONCE(sqe->statx_flags); | |
eddc7ef5 JA |
4586 | |
4587 | return 0; | |
4588 | } | |
4589 | ||
45d189c6 | 4590 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
eddc7ef5 | 4591 | { |
1d9e1288 | 4592 | struct io_statx *ctx = &req->statx; |
eddc7ef5 JA |
4593 | int ret; |
4594 | ||
59d70013 | 4595 | if (issue_flags & IO_URING_F_NONBLOCK) |
eddc7ef5 JA |
4596 | return -EAGAIN; |
4597 | ||
e62753e4 BM |
4598 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
4599 | ctx->buffer); | |
eddc7ef5 | 4600 | |
eddc7ef5 | 4601 | if (ret < 0) |
93d2bcd2 | 4602 | req_set_fail(req); |
e1e16097 | 4603 | io_req_complete(req, ret); |
eddc7ef5 JA |
4604 | return 0; |
4605 | } | |
4606 | ||
b5dba59e JA |
4607 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4608 | { | |
14587a46 | 4609 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4610 | return -EINVAL; |
b5dba59e | 4611 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
7df778be | 4612 | sqe->rw_flags || sqe->buf_index) |
b5dba59e | 4613 | return -EINVAL; |
9c280f90 | 4614 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4615 | return -EBADF; |
b5dba59e JA |
4616 | |
4617 | req->close.fd = READ_ONCE(sqe->fd); | |
7df778be PB |
4618 | req->close.file_slot = READ_ONCE(sqe->file_index); |
4619 | if (req->close.file_slot && req->close.fd) | |
4620 | return -EINVAL; | |
4621 | ||
b5dba59e | 4622 | return 0; |
b5dba59e JA |
4623 | } |
4624 | ||
889fca73 | 4625 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
b5dba59e | 4626 | { |
9eac1904 | 4627 | struct files_struct *files = current->files; |
3af73b28 | 4628 | struct io_close *close = &req->close; |
9eac1904 | 4629 | struct fdtable *fdt; |
a1fde923 PB |
4630 | struct file *file = NULL; |
4631 | int ret = -EBADF; | |
b5dba59e | 4632 | |
7df778be PB |
4633 | if (req->close.file_slot) { |
4634 | ret = io_close_fixed(req, issue_flags); | |
4635 | goto err; | |
4636 | } | |
4637 | ||
9eac1904 JA |
4638 | spin_lock(&files->file_lock); |
4639 | fdt = files_fdtable(files); | |
4640 | if (close->fd >= fdt->max_fds) { | |
4641 | spin_unlock(&files->file_lock); | |
4642 | goto err; | |
4643 | } | |
4644 | file = fdt->fd[close->fd]; | |
a1fde923 | 4645 | if (!file || file->f_op == &io_uring_fops) { |
9eac1904 JA |
4646 | spin_unlock(&files->file_lock); |
4647 | file = NULL; | |
4648 | goto err; | |
3af73b28 | 4649 | } |
b5dba59e JA |
4650 | |
4651 | /* if the file has a flush method, be safe and punt to async */ | |
45d189c6 | 4652 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
9eac1904 | 4653 | spin_unlock(&files->file_lock); |
0bf0eefd | 4654 | return -EAGAIN; |
a2100672 | 4655 | } |
b5dba59e | 4656 | |
9eac1904 JA |
4657 | ret = __close_fd_get_file(close->fd, &file); |
4658 | spin_unlock(&files->file_lock); | |
4659 | if (ret < 0) { | |
4660 | if (ret == -ENOENT) | |
4661 | ret = -EBADF; | |
4662 | goto err; | |
4663 | } | |
4664 | ||
3af73b28 | 4665 | /* No ->flush() or already async, safely close from here */ |
9eac1904 JA |
4666 | ret = filp_close(file, current->files); |
4667 | err: | |
3af73b28 | 4668 | if (ret < 0) |
93d2bcd2 | 4669 | req_set_fail(req); |
9eac1904 JA |
4670 | if (file) |
4671 | fput(file); | |
889fca73 | 4672 | __io_req_complete(req, issue_flags, ret, 0); |
1a417f4e | 4673 | return 0; |
b5dba59e JA |
4674 | } |
4675 | ||
1155c76a | 4676 | static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
4677 | { |
4678 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 | 4679 | |
5d17b4a4 JA |
4680 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4681 | return -EINVAL; | |
26578cda PB |
4682 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
4683 | sqe->splice_fd_in)) | |
5d17b4a4 JA |
4684 | return -EINVAL; |
4685 | ||
8ed8d3c3 JA |
4686 | req->sync.off = READ_ONCE(sqe->off); |
4687 | req->sync.len = READ_ONCE(sqe->len); | |
4688 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
4689 | return 0; |
4690 | } | |
4691 | ||
45d189c6 | 4692 | static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4693 | { |
8ed8d3c3 JA |
4694 | int ret; |
4695 | ||
ac45abc0 | 4696 | /* sync_file_range always requires a blocking context */ |
45d189c6 | 4697 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4698 | return -EAGAIN; |
4699 | ||
9adbd45d | 4700 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
4701 | req->sync.flags); |
4702 | if (ret < 0) | |
93d2bcd2 | 4703 | req_set_fail(req); |
e1e16097 | 4704 | io_req_complete(req, ret); |
5d17b4a4 JA |
4705 | return 0; |
4706 | } | |
4707 | ||
469956e8 | 4708 | #if defined(CONFIG_NET) |
02d27d89 PB |
4709 | static int io_setup_async_msg(struct io_kiocb *req, |
4710 | struct io_async_msghdr *kmsg) | |
4711 | { | |
e8c2bc1f JA |
4712 | struct io_async_msghdr *async_msg = req->async_data; |
4713 | ||
4714 | if (async_msg) | |
02d27d89 | 4715 | return -EAGAIN; |
e8c2bc1f | 4716 | if (io_alloc_async_data(req)) { |
257e84a5 | 4717 | kfree(kmsg->free_iov); |
02d27d89 PB |
4718 | return -ENOMEM; |
4719 | } | |
e8c2bc1f | 4720 | async_msg = req->async_data; |
02d27d89 | 4721 | req->flags |= REQ_F_NEED_CLEANUP; |
e8c2bc1f | 4722 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
2a780802 | 4723 | async_msg->msg.msg_name = &async_msg->addr; |
257e84a5 PB |
4724 | /* if were using fast_iov, set it to the new one */ |
4725 | if (!async_msg->free_iov) | |
4726 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; | |
4727 | ||
02d27d89 PB |
4728 | return -EAGAIN; |
4729 | } | |
4730 | ||
2ae523ed PB |
4731 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
4732 | struct io_async_msghdr *iomsg) | |
4733 | { | |
2ae523ed | 4734 | iomsg->msg.msg_name = &iomsg->addr; |
257e84a5 | 4735 | iomsg->free_iov = iomsg->fast_iov; |
2ae523ed | 4736 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
257e84a5 | 4737 | req->sr_msg.msg_flags, &iomsg->free_iov); |
2ae523ed PB |
4738 | } |
4739 | ||
93642ef8 PB |
4740 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
4741 | { | |
4742 | int ret; | |
4743 | ||
93642ef8 PB |
4744 | ret = io_sendmsg_copy_hdr(req, req->async_data); |
4745 | if (!ret) | |
4746 | req->flags |= REQ_F_NEED_CLEANUP; | |
4747 | return ret; | |
4748 | } | |
4749 | ||
3529d8c2 | 4750 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 4751 | { |
e47293fd | 4752 | struct io_sr_msg *sr = &req->sr_msg; |
03b1230c | 4753 | |
d2b6f48b PB |
4754 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4755 | return -EINVAL; | |
4756 | ||
270a5940 | 4757 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
fddaface | 4758 | sr->len = READ_ONCE(sqe->len); |
04411806 PB |
4759 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
4760 | if (sr->msg_flags & MSG_DONTWAIT) | |
4761 | req->flags |= REQ_F_NOWAIT; | |
3529d8c2 | 4762 | |
d8768362 JA |
4763 | #ifdef CONFIG_COMPAT |
4764 | if (req->ctx->compat) | |
4765 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4766 | #endif | |
93642ef8 | 4767 | return 0; |
03b1230c JA |
4768 | } |
4769 | ||
889fca73 | 4770 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4771 | { |
6b754c8b | 4772 | struct io_async_msghdr iomsg, *kmsg; |
0fa03c62 | 4773 | struct socket *sock; |
7a7cacba | 4774 | unsigned flags; |
0031275d | 4775 | int min_ret = 0; |
0fa03c62 JA |
4776 | int ret; |
4777 | ||
dba4a925 | 4778 | sock = sock_from_file(req->file); |
7a7cacba | 4779 | if (unlikely(!sock)) |
dba4a925 | 4780 | return -ENOTSOCK; |
3529d8c2 | 4781 | |
d886e185 PB |
4782 | if (req_has_async_data(req)) { |
4783 | kmsg = req->async_data; | |
4784 | } else { | |
7a7cacba PB |
4785 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
4786 | if (ret) | |
4787 | return ret; | |
4788 | kmsg = &iomsg; | |
0fa03c62 | 4789 | } |
0fa03c62 | 4790 | |
04411806 PB |
4791 | flags = req->sr_msg.msg_flags; |
4792 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 4793 | flags |= MSG_DONTWAIT; |
0031275d SM |
4794 | if (flags & MSG_WAITALL) |
4795 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
4796 | ||
7a7cacba | 4797 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
45d189c6 | 4798 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4799 | return io_setup_async_msg(req, kmsg); |
4800 | if (ret == -ERESTARTSYS) | |
4801 | ret = -EINTR; | |
0fa03c62 | 4802 | |
257e84a5 PB |
4803 | /* fast path, check for non-NULL to avoid function call */ |
4804 | if (kmsg->free_iov) | |
4805 | kfree(kmsg->free_iov); | |
99bc4c38 | 4806 | req->flags &= ~REQ_F_NEED_CLEANUP; |
0031275d | 4807 | if (ret < min_ret) |
93d2bcd2 | 4808 | req_set_fail(req); |
889fca73 | 4809 | __io_req_complete(req, issue_flags, ret, 0); |
5d17b4a4 | 4810 | return 0; |
03b1230c | 4811 | } |
aa1fa28f | 4812 | |
889fca73 | 4813 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4814 | { |
7a7cacba PB |
4815 | struct io_sr_msg *sr = &req->sr_msg; |
4816 | struct msghdr msg; | |
4817 | struct iovec iov; | |
fddaface | 4818 | struct socket *sock; |
7a7cacba | 4819 | unsigned flags; |
0031275d | 4820 | int min_ret = 0; |
fddaface JA |
4821 | int ret; |
4822 | ||
dba4a925 | 4823 | sock = sock_from_file(req->file); |
7a7cacba | 4824 | if (unlikely(!sock)) |
dba4a925 | 4825 | return -ENOTSOCK; |
fddaface | 4826 | |
7a7cacba PB |
4827 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
4828 | if (unlikely(ret)) | |
14db8411 | 4829 | return ret; |
fddaface | 4830 | |
7a7cacba PB |
4831 | msg.msg_name = NULL; |
4832 | msg.msg_control = NULL; | |
4833 | msg.msg_controllen = 0; | |
4834 | msg.msg_namelen = 0; | |
fddaface | 4835 | |
04411806 PB |
4836 | flags = req->sr_msg.msg_flags; |
4837 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 4838 | flags |= MSG_DONTWAIT; |
0031275d SM |
4839 | if (flags & MSG_WAITALL) |
4840 | min_ret = iov_iter_count(&msg.msg_iter); | |
4841 | ||
7a7cacba PB |
4842 | msg.msg_flags = flags; |
4843 | ret = sock_sendmsg(sock, &msg); | |
45d189c6 | 4844 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4845 | return -EAGAIN; |
4846 | if (ret == -ERESTARTSYS) | |
4847 | ret = -EINTR; | |
fddaface | 4848 | |
0031275d | 4849 | if (ret < min_ret) |
93d2bcd2 | 4850 | req_set_fail(req); |
889fca73 | 4851 | __io_req_complete(req, issue_flags, ret, 0); |
fddaface | 4852 | return 0; |
fddaface JA |
4853 | } |
4854 | ||
1400e697 PB |
4855 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
4856 | struct io_async_msghdr *iomsg) | |
52de1fe1 JA |
4857 | { |
4858 | struct io_sr_msg *sr = &req->sr_msg; | |
4859 | struct iovec __user *uiov; | |
4860 | size_t iov_len; | |
4861 | int ret; | |
4862 | ||
1400e697 PB |
4863 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
4864 | &iomsg->uaddr, &uiov, &iov_len); | |
52de1fe1 JA |
4865 | if (ret) |
4866 | return ret; | |
4867 | ||
4868 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4869 | if (iov_len > 1) | |
4870 | return -EINVAL; | |
5476dfed | 4871 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
52de1fe1 | 4872 | return -EFAULT; |
5476dfed | 4873 | sr->len = iomsg->fast_iov[0].iov_len; |
257e84a5 | 4874 | iomsg->free_iov = NULL; |
52de1fe1 | 4875 | } else { |
257e84a5 | 4876 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4877 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
257e84a5 | 4878 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
89cd35c5 | 4879 | false); |
52de1fe1 JA |
4880 | if (ret > 0) |
4881 | ret = 0; | |
4882 | } | |
4883 | ||
4884 | return ret; | |
4885 | } | |
4886 | ||
4887 | #ifdef CONFIG_COMPAT | |
4888 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, | |
1400e697 | 4889 | struct io_async_msghdr *iomsg) |
52de1fe1 | 4890 | { |
52de1fe1 JA |
4891 | struct io_sr_msg *sr = &req->sr_msg; |
4892 | struct compat_iovec __user *uiov; | |
4893 | compat_uptr_t ptr; | |
4894 | compat_size_t len; | |
4895 | int ret; | |
4896 | ||
4af3417a PB |
4897 | ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr, |
4898 | &ptr, &len); | |
52de1fe1 JA |
4899 | if (ret) |
4900 | return ret; | |
4901 | ||
4902 | uiov = compat_ptr(ptr); | |
4903 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4904 | compat_ssize_t clen; | |
4905 | ||
4906 | if (len > 1) | |
4907 | return -EINVAL; | |
4908 | if (!access_ok(uiov, sizeof(*uiov))) | |
4909 | return -EFAULT; | |
4910 | if (__get_user(clen, &uiov->iov_len)) | |
4911 | return -EFAULT; | |
4912 | if (clen < 0) | |
4913 | return -EINVAL; | |
2d280bc8 | 4914 | sr->len = clen; |
257e84a5 | 4915 | iomsg->free_iov = NULL; |
52de1fe1 | 4916 | } else { |
257e84a5 | 4917 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4918 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
257e84a5 | 4919 | UIO_FASTIOV, &iomsg->free_iov, |
89cd35c5 | 4920 | &iomsg->msg.msg_iter, true); |
52de1fe1 JA |
4921 | if (ret < 0) |
4922 | return ret; | |
4923 | } | |
4924 | ||
4925 | return 0; | |
4926 | } | |
4927 | #endif | |
4928 | ||
1400e697 PB |
4929 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
4930 | struct io_async_msghdr *iomsg) | |
52de1fe1 | 4931 | { |
1400e697 | 4932 | iomsg->msg.msg_name = &iomsg->addr; |
52de1fe1 JA |
4933 | |
4934 | #ifdef CONFIG_COMPAT | |
4935 | if (req->ctx->compat) | |
1400e697 | 4936 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
fddaface | 4937 | #endif |
52de1fe1 | 4938 | |
1400e697 | 4939 | return __io_recvmsg_copy_hdr(req, iomsg); |
52de1fe1 JA |
4940 | } |
4941 | ||
bcda7baa | 4942 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
51aac424 | 4943 | unsigned int issue_flags) |
bcda7baa JA |
4944 | { |
4945 | struct io_sr_msg *sr = &req->sr_msg; | |
bcda7baa | 4946 | |
51aac424 | 4947 | return io_buffer_select(req, &sr->len, sr->bgid, issue_flags); |
fddaface JA |
4948 | } |
4949 | ||
7fbb1b54 PB |
4950 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
4951 | { | |
30d51dd4 | 4952 | return io_put_kbuf(req, req->kbuf); |
7fbb1b54 PB |
4953 | } |
4954 | ||
93642ef8 | 4955 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
aa1fa28f | 4956 | { |
99bc4c38 | 4957 | int ret; |
3529d8c2 | 4958 | |
93642ef8 PB |
4959 | ret = io_recvmsg_copy_hdr(req, req->async_data); |
4960 | if (!ret) | |
4961 | req->flags |= REQ_F_NEED_CLEANUP; | |
4962 | return ret; | |
4963 | } | |
4964 | ||
4965 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4966 | { | |
4967 | struct io_sr_msg *sr = &req->sr_msg; | |
4968 | ||
d2b6f48b PB |
4969 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4970 | return -EINVAL; | |
4971 | ||
270a5940 | 4972 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
0b7b21e4 | 4973 | sr->len = READ_ONCE(sqe->len); |
bcda7baa | 4974 | sr->bgid = READ_ONCE(sqe->buf_group); |
04411806 PB |
4975 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
4976 | if (sr->msg_flags & MSG_DONTWAIT) | |
4977 | req->flags |= REQ_F_NOWAIT; | |
06b76d44 | 4978 | |
d8768362 JA |
4979 | #ifdef CONFIG_COMPAT |
4980 | if (req->ctx->compat) | |
4981 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4982 | #endif | |
93642ef8 | 4983 | return 0; |
aa1fa28f JA |
4984 | } |
4985 | ||
889fca73 | 4986 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4987 | { |
6b754c8b | 4988 | struct io_async_msghdr iomsg, *kmsg; |
03b1230c | 4989 | struct socket *sock; |
7fbb1b54 | 4990 | struct io_buffer *kbuf; |
7a7cacba | 4991 | unsigned flags; |
0031275d | 4992 | int min_ret = 0; |
52de1fe1 | 4993 | int ret, cflags = 0; |
45d189c6 | 4994 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
03b1230c | 4995 | |
dba4a925 | 4996 | sock = sock_from_file(req->file); |
7a7cacba | 4997 | if (unlikely(!sock)) |
dba4a925 | 4998 | return -ENOTSOCK; |
3529d8c2 | 4999 | |
d886e185 PB |
5000 | if (req_has_async_data(req)) { |
5001 | kmsg = req->async_data; | |
5002 | } else { | |
7a7cacba PB |
5003 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
5004 | if (ret) | |
681fda8d | 5005 | return ret; |
7a7cacba PB |
5006 | kmsg = &iomsg; |
5007 | } | |
03b1230c | 5008 | |
bc02ef33 | 5009 | if (req->flags & REQ_F_BUFFER_SELECT) { |
51aac424 | 5010 | kbuf = io_recv_buffer_select(req, issue_flags); |
bc02ef33 | 5011 | if (IS_ERR(kbuf)) |
52de1fe1 | 5012 | return PTR_ERR(kbuf); |
7a7cacba | 5013 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
5476dfed PB |
5014 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
5015 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, | |
7a7cacba PB |
5016 | 1, req->sr_msg.len); |
5017 | } | |
52de1fe1 | 5018 | |
04411806 PB |
5019 | flags = req->sr_msg.msg_flags; |
5020 | if (force_nonblock) | |
7a7cacba | 5021 | flags |= MSG_DONTWAIT; |
0031275d SM |
5022 | if (flags & MSG_WAITALL) |
5023 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
5024 | ||
7a7cacba PB |
5025 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
5026 | kmsg->uaddr, flags); | |
0e1b6fe3 PB |
5027 | if (force_nonblock && ret == -EAGAIN) |
5028 | return io_setup_async_msg(req, kmsg); | |
7a7cacba PB |
5029 | if (ret == -ERESTARTSYS) |
5030 | ret = -EINTR; | |
03b1230c | 5031 | |
7fbb1b54 PB |
5032 | if (req->flags & REQ_F_BUFFER_SELECTED) |
5033 | cflags = io_put_recv_kbuf(req); | |
257e84a5 PB |
5034 | /* fast path, check for non-NULL to avoid function call */ |
5035 | if (kmsg->free_iov) | |
5036 | kfree(kmsg->free_iov); | |
99bc4c38 | 5037 | req->flags &= ~REQ_F_NEED_CLEANUP; |
0031275d | 5038 | if (ret < min_ret || ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)))) |
93d2bcd2 | 5039 | req_set_fail(req); |
889fca73 | 5040 | __io_req_complete(req, issue_flags, ret, cflags); |
03b1230c | 5041 | return 0; |
0fa03c62 | 5042 | } |
5d17b4a4 | 5043 | |
889fca73 | 5044 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 5045 | { |
6b754c8b | 5046 | struct io_buffer *kbuf; |
7a7cacba PB |
5047 | struct io_sr_msg *sr = &req->sr_msg; |
5048 | struct msghdr msg; | |
5049 | void __user *buf = sr->buf; | |
fddaface | 5050 | struct socket *sock; |
7a7cacba PB |
5051 | struct iovec iov; |
5052 | unsigned flags; | |
0031275d | 5053 | int min_ret = 0; |
bcda7baa | 5054 | int ret, cflags = 0; |
45d189c6 | 5055 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
fddaface | 5056 | |
dba4a925 | 5057 | sock = sock_from_file(req->file); |
7a7cacba | 5058 | if (unlikely(!sock)) |
dba4a925 | 5059 | return -ENOTSOCK; |
fddaface | 5060 | |
bc02ef33 | 5061 | if (req->flags & REQ_F_BUFFER_SELECT) { |
51aac424 | 5062 | kbuf = io_recv_buffer_select(req, issue_flags); |
bcda7baa JA |
5063 | if (IS_ERR(kbuf)) |
5064 | return PTR_ERR(kbuf); | |
7a7cacba | 5065 | buf = u64_to_user_ptr(kbuf->addr); |
bc02ef33 | 5066 | } |
bcda7baa | 5067 | |
7a7cacba | 5068 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
14c32eee PB |
5069 | if (unlikely(ret)) |
5070 | goto out_free; | |
fddaface | 5071 | |
7a7cacba PB |
5072 | msg.msg_name = NULL; |
5073 | msg.msg_control = NULL; | |
5074 | msg.msg_controllen = 0; | |
5075 | msg.msg_namelen = 0; | |
5076 | msg.msg_iocb = NULL; | |
5077 | msg.msg_flags = 0; | |
fddaface | 5078 | |
04411806 PB |
5079 | flags = req->sr_msg.msg_flags; |
5080 | if (force_nonblock) | |
7a7cacba | 5081 | flags |= MSG_DONTWAIT; |
0031275d SM |
5082 | if (flags & MSG_WAITALL) |
5083 | min_ret = iov_iter_count(&msg.msg_iter); | |
5084 | ||
7a7cacba PB |
5085 | ret = sock_recvmsg(sock, &msg, flags); |
5086 | if (force_nonblock && ret == -EAGAIN) | |
5087 | return -EAGAIN; | |
5088 | if (ret == -ERESTARTSYS) | |
5089 | ret = -EINTR; | |
14c32eee | 5090 | out_free: |
7fbb1b54 PB |
5091 | if (req->flags & REQ_F_BUFFER_SELECTED) |
5092 | cflags = io_put_recv_kbuf(req); | |
0031275d | 5093 | if (ret < min_ret || ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)))) |
93d2bcd2 | 5094 | req_set_fail(req); |
889fca73 | 5095 | __io_req_complete(req, issue_flags, ret, cflags); |
fddaface | 5096 | return 0; |
fddaface JA |
5097 | } |
5098 | ||
3529d8c2 | 5099 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 | 5100 | { |
8ed8d3c3 JA |
5101 | struct io_accept *accept = &req->accept; |
5102 | ||
14587a46 | 5103 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
17f2fe35 | 5104 | return -EINVAL; |
aaa4db12 | 5105 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
5106 | return -EINVAL; |
5107 | ||
d55e5f5b JA |
5108 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5109 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 5110 | accept->flags = READ_ONCE(sqe->accept_flags); |
09952e3e | 5111 | accept->nofile = rlimit(RLIMIT_NOFILE); |
a7083ad5 | 5112 | |
aaa4db12 PB |
5113 | accept->file_slot = READ_ONCE(sqe->file_index); |
5114 | if (accept->file_slot && ((req->open.how.flags & O_CLOEXEC) || | |
5115 | (accept->flags & SOCK_CLOEXEC))) | |
5116 | return -EINVAL; | |
a7083ad5 PB |
5117 | if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) |
5118 | return -EINVAL; | |
5119 | if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK)) | |
5120 | accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK; | |
8ed8d3c3 | 5121 | return 0; |
8ed8d3c3 | 5122 | } |
17f2fe35 | 5123 | |
889fca73 | 5124 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 JA |
5125 | { |
5126 | struct io_accept *accept = &req->accept; | |
45d189c6 | 5127 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ac45abc0 | 5128 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
aaa4db12 | 5129 | bool fixed = !!accept->file_slot; |
a7083ad5 PB |
5130 | struct file *file; |
5131 | int ret, fd; | |
8ed8d3c3 | 5132 | |
e697deed JX |
5133 | if (req->file->f_flags & O_NONBLOCK) |
5134 | req->flags |= REQ_F_NOWAIT; | |
5135 | ||
aaa4db12 PB |
5136 | if (!fixed) { |
5137 | fd = __get_unused_fd_flags(accept->flags, accept->nofile); | |
5138 | if (unlikely(fd < 0)) | |
5139 | return fd; | |
5140 | } | |
a7083ad5 PB |
5141 | file = do_accept(req->file, file_flags, accept->addr, accept->addr_len, |
5142 | accept->flags); | |
5143 | if (IS_ERR(file)) { | |
aaa4db12 PB |
5144 | if (!fixed) |
5145 | put_unused_fd(fd); | |
a7083ad5 PB |
5146 | ret = PTR_ERR(file); |
5147 | if (ret == -EAGAIN && force_nonblock) | |
5148 | return -EAGAIN; | |
ac45abc0 PB |
5149 | if (ret == -ERESTARTSYS) |
5150 | ret = -EINTR; | |
93d2bcd2 | 5151 | req_set_fail(req); |
aaa4db12 | 5152 | } else if (!fixed) { |
a7083ad5 PB |
5153 | fd_install(fd, file); |
5154 | ret = fd; | |
aaa4db12 PB |
5155 | } else { |
5156 | ret = io_install_fixed_file(req, file, issue_flags, | |
5157 | accept->file_slot - 1); | |
ac45abc0 | 5158 | } |
889fca73 | 5159 | __io_req_complete(req, issue_flags, ret, 0); |
17f2fe35 | 5160 | return 0; |
8ed8d3c3 JA |
5161 | } |
5162 | ||
93642ef8 PB |
5163 | static int io_connect_prep_async(struct io_kiocb *req) |
5164 | { | |
5165 | struct io_async_connect *io = req->async_data; | |
5166 | struct io_connect *conn = &req->connect; | |
5167 | ||
5168 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); | |
5169 | } | |
5170 | ||
3529d8c2 | 5171 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 | 5172 | { |
3529d8c2 | 5173 | struct io_connect *conn = &req->connect; |
f499a021 | 5174 | |
14587a46 | 5175 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3fbb51c1 | 5176 | return -EINVAL; |
26578cda PB |
5177 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags || |
5178 | sqe->splice_fd_in) | |
3fbb51c1 JA |
5179 | return -EINVAL; |
5180 | ||
3529d8c2 JA |
5181 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5182 | conn->addr_len = READ_ONCE(sqe->addr2); | |
93642ef8 | 5183 | return 0; |
f499a021 JA |
5184 | } |
5185 | ||
889fca73 | 5186 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
f8e85cf2 | 5187 | { |
e8c2bc1f | 5188 | struct io_async_connect __io, *io; |
f8e85cf2 | 5189 | unsigned file_flags; |
3fbb51c1 | 5190 | int ret; |
45d189c6 | 5191 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
f8e85cf2 | 5192 | |
d886e185 | 5193 | if (req_has_async_data(req)) { |
e8c2bc1f | 5194 | io = req->async_data; |
f499a021 | 5195 | } else { |
3529d8c2 JA |
5196 | ret = move_addr_to_kernel(req->connect.addr, |
5197 | req->connect.addr_len, | |
e8c2bc1f | 5198 | &__io.address); |
f499a021 JA |
5199 | if (ret) |
5200 | goto out; | |
5201 | io = &__io; | |
5202 | } | |
5203 | ||
3fbb51c1 JA |
5204 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
5205 | ||
e8c2bc1f | 5206 | ret = __sys_connect_file(req->file, &io->address, |
3fbb51c1 | 5207 | req->connect.addr_len, file_flags); |
87f80d62 | 5208 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
d886e185 | 5209 | if (req_has_async_data(req)) |
b7bb4f7d | 5210 | return -EAGAIN; |
e8c2bc1f | 5211 | if (io_alloc_async_data(req)) { |
f499a021 JA |
5212 | ret = -ENOMEM; |
5213 | goto out; | |
5214 | } | |
e8c2bc1f | 5215 | memcpy(req->async_data, &__io, sizeof(__io)); |
f8e85cf2 | 5216 | return -EAGAIN; |
f499a021 | 5217 | } |
f8e85cf2 JA |
5218 | if (ret == -ERESTARTSYS) |
5219 | ret = -EINTR; | |
f499a021 | 5220 | out: |
4e88d6e7 | 5221 | if (ret < 0) |
93d2bcd2 | 5222 | req_set_fail(req); |
889fca73 | 5223 | __io_req_complete(req, issue_flags, ret, 0); |
f8e85cf2 | 5224 | return 0; |
469956e8 Y |
5225 | } |
5226 | #else /* !CONFIG_NET */ | |
99a10081 JA |
5227 | #define IO_NETOP_FN(op) \ |
5228 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ | |
5229 | { \ | |
5230 | return -EOPNOTSUPP; \ | |
5231 | } | |
5232 | ||
5233 | #define IO_NETOP_PREP(op) \ | |
5234 | IO_NETOP_FN(op) \ | |
5235 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ | |
5236 | { \ | |
5237 | return -EOPNOTSUPP; \ | |
5238 | } \ | |
5239 | ||
5240 | #define IO_NETOP_PREP_ASYNC(op) \ | |
5241 | IO_NETOP_PREP(op) \ | |
5242 | static int io_##op##_prep_async(struct io_kiocb *req) \ | |
5243 | { \ | |
5244 | return -EOPNOTSUPP; \ | |
5245 | } | |
5246 | ||
5247 | IO_NETOP_PREP_ASYNC(sendmsg); | |
5248 | IO_NETOP_PREP_ASYNC(recvmsg); | |
5249 | IO_NETOP_PREP_ASYNC(connect); | |
5250 | IO_NETOP_PREP(accept); | |
5251 | IO_NETOP_FN(send); | |
5252 | IO_NETOP_FN(recv); | |
469956e8 | 5253 | #endif /* CONFIG_NET */ |
f8e85cf2 | 5254 | |
d7718a9d JA |
5255 | struct io_poll_table { |
5256 | struct poll_table_struct pt; | |
5257 | struct io_kiocb *req; | |
68b11e8b | 5258 | int nr_entries; |
d7718a9d JA |
5259 | int error; |
5260 | }; | |
ce593a6c | 5261 | |
d7718a9d | 5262 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
5b0a6acc | 5263 | __poll_t mask, io_req_tw_func_t func) |
d7718a9d | 5264 | { |
d7718a9d JA |
5265 | /* for instances that support it check for an event match first: */ |
5266 | if (mask && !(mask & poll->events)) | |
5267 | return 0; | |
5268 | ||
5269 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); | |
5270 | ||
5271 | list_del_init(&poll->wait.entry); | |
5272 | ||
d7718a9d | 5273 | req->result = mask; |
5b0a6acc | 5274 | req->io_task_work.func = func; |
6d816e08 | 5275 | |
d7718a9d | 5276 | /* |
e3aabf95 JA |
5277 | * If this fails, then the task is exiting. When a task exits, the |
5278 | * work gets canceled, so just cancel this request as well instead | |
5279 | * of executing it. We can't safely execute it anyway, as we may not | |
5280 | * have the needed state needed for it anyway. | |
d7718a9d | 5281 | */ |
e09ee510 | 5282 | io_req_task_work_add(req); |
d7718a9d JA |
5283 | return 1; |
5284 | } | |
5285 | ||
74ce6ce4 JA |
5286 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
5287 | __acquires(&req->ctx->completion_lock) | |
5288 | { | |
5289 | struct io_ring_ctx *ctx = req->ctx; | |
5290 | ||
316319e8 | 5291 | /* req->task == current here, checking PF_EXITING is safe */ |
e09ee510 PB |
5292 | if (unlikely(req->task->flags & PF_EXITING)) |
5293 | WRITE_ONCE(poll->canceled, true); | |
5294 | ||
74ce6ce4 JA |
5295 | if (!req->result && !READ_ONCE(poll->canceled)) { |
5296 | struct poll_table_struct pt = { ._key = poll->events }; | |
5297 | ||
5298 | req->result = vfs_poll(req->file, &pt) & poll->events; | |
5299 | } | |
5300 | ||
79ebeaee | 5301 | spin_lock(&ctx->completion_lock); |
74ce6ce4 JA |
5302 | if (!req->result && !READ_ONCE(poll->canceled)) { |
5303 | add_wait_queue(poll->head, &poll->wait); | |
5304 | return true; | |
5305 | } | |
5306 | ||
5307 | return false; | |
5308 | } | |
5309 | ||
d4e7cd36 | 5310 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
18bceab1 | 5311 | { |
e8c2bc1f | 5312 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
d4e7cd36 | 5313 | if (req->opcode == IORING_OP_POLL_ADD) |
e8c2bc1f | 5314 | return req->async_data; |
d4e7cd36 JA |
5315 | return req->apoll->double_poll; |
5316 | } | |
5317 | ||
5318 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) | |
5319 | { | |
5320 | if (req->opcode == IORING_OP_POLL_ADD) | |
5321 | return &req->poll; | |
5322 | return &req->apoll->poll; | |
5323 | } | |
5324 | ||
5325 | static void io_poll_remove_double(struct io_kiocb *req) | |
e07785b0 | 5326 | __must_hold(&req->ctx->completion_lock) |
d4e7cd36 JA |
5327 | { |
5328 | struct io_poll_iocb *poll = io_poll_get_double(req); | |
18bceab1 JA |
5329 | |
5330 | lockdep_assert_held(&req->ctx->completion_lock); | |
5331 | ||
5332 | if (poll && poll->head) { | |
5333 | struct wait_queue_head *head = poll->head; | |
5334 | ||
79ebeaee | 5335 | spin_lock_irq(&head->lock); |
18bceab1 JA |
5336 | list_del_init(&poll->wait.entry); |
5337 | if (poll->wait.private) | |
de9b4cca | 5338 | req_ref_put(req); |
18bceab1 | 5339 | poll->head = NULL; |
79ebeaee | 5340 | spin_unlock_irq(&head->lock); |
18bceab1 JA |
5341 | } |
5342 | } | |
5343 | ||
31efe48e | 5344 | static bool __io_poll_complete(struct io_kiocb *req, __poll_t mask) |
e07785b0 | 5345 | __must_hold(&req->ctx->completion_lock) |
18bceab1 JA |
5346 | { |
5347 | struct io_ring_ctx *ctx = req->ctx; | |
88e41cf9 | 5348 | unsigned flags = IORING_CQE_F_MORE; |
e27414be | 5349 | int error; |
18bceab1 | 5350 | |
e27414be | 5351 | if (READ_ONCE(req->poll.canceled)) { |
45ab03b1 | 5352 | error = -ECANCELED; |
88e41cf9 | 5353 | req->poll.events |= EPOLLONESHOT; |
e27414be | 5354 | } else { |
5082620f | 5355 | error = mangle_poll(mask); |
e27414be | 5356 | } |
b69de288 JA |
5357 | if (req->poll.events & EPOLLONESHOT) |
5358 | flags = 0; | |
a62682f9 HX |
5359 | if (!io_cqring_fill_event(ctx, req->user_data, error, flags)) { |
5360 | req->poll.events |= EPOLLONESHOT; | |
88e41cf9 | 5361 | flags = 0; |
a62682f9 | 5362 | } |
7b289c38 HX |
5363 | if (flags & IORING_CQE_F_MORE) |
5364 | ctx->cq_extra++; | |
18bceab1 | 5365 | |
88e41cf9 | 5366 | return !(flags & IORING_CQE_F_MORE); |
18bceab1 JA |
5367 | } |
5368 | ||
f237c30a | 5369 | static void io_poll_task_func(struct io_kiocb *req, bool *locked) |
18bceab1 JA |
5370 | { |
5371 | struct io_ring_ctx *ctx = req->ctx; | |
dd221f46 | 5372 | struct io_kiocb *nxt; |
18bceab1 JA |
5373 | |
5374 | if (io_poll_rewait(req, &req->poll)) { | |
79ebeaee | 5375 | spin_unlock(&ctx->completion_lock); |
dd221f46 | 5376 | } else { |
f40b964a | 5377 | bool done; |
18bceab1 | 5378 | |
5b7aa38d HX |
5379 | if (req->poll.done) { |
5380 | spin_unlock(&ctx->completion_lock); | |
5381 | return; | |
5382 | } | |
31efe48e | 5383 | done = __io_poll_complete(req, req->result); |
88e41cf9 | 5384 | if (done) { |
a890d01e | 5385 | io_poll_remove_double(req); |
88e41cf9 | 5386 | hash_del(&req->hash_node); |
bd99c71b | 5387 | req->poll.done = true; |
f40b964a | 5388 | } else { |
88e41cf9 JA |
5389 | req->result = 0; |
5390 | add_wait_queue(req->poll.head, &req->poll.wait); | |
5391 | } | |
31efe48e | 5392 | io_commit_cqring(ctx); |
79ebeaee | 5393 | spin_unlock(&ctx->completion_lock); |
dd221f46 | 5394 | io_cqring_ev_posted(ctx); |
18bceab1 | 5395 | |
88e41cf9 JA |
5396 | if (done) { |
5397 | nxt = io_put_req_find_next(req); | |
5398 | if (nxt) | |
f237c30a | 5399 | io_req_task_submit(nxt, locked); |
88e41cf9 | 5400 | } |
dd221f46 | 5401 | } |
18bceab1 JA |
5402 | } |
5403 | ||
5404 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, | |
5405 | int sync, void *key) | |
5406 | { | |
5407 | struct io_kiocb *req = wait->private; | |
d4e7cd36 | 5408 | struct io_poll_iocb *poll = io_poll_get_single(req); |
18bceab1 | 5409 | __poll_t mask = key_to_poll(key); |
79ebeaee | 5410 | unsigned long flags; |
18bceab1 JA |
5411 | |
5412 | /* for instances that support it check for an event match first: */ | |
5413 | if (mask && !(mask & poll->events)) | |
5414 | return 0; | |
88e41cf9 JA |
5415 | if (!(poll->events & EPOLLONESHOT)) |
5416 | return poll->wait.func(&poll->wait, mode, sync, key); | |
18bceab1 | 5417 | |
8706e04e JA |
5418 | list_del_init(&wait->entry); |
5419 | ||
9ce85ef2 | 5420 | if (poll->head) { |
18bceab1 JA |
5421 | bool done; |
5422 | ||
79ebeaee | 5423 | spin_lock_irqsave(&poll->head->lock, flags); |
807abcb0 | 5424 | done = list_empty(&poll->wait.entry); |
18bceab1 | 5425 | if (!done) |
807abcb0 | 5426 | list_del_init(&poll->wait.entry); |
d4e7cd36 JA |
5427 | /* make sure double remove sees this as being gone */ |
5428 | wait->private = NULL; | |
79ebeaee | 5429 | spin_unlock_irqrestore(&poll->head->lock, flags); |
c8b5e260 JA |
5430 | if (!done) { |
5431 | /* use wait func handler, so it matches the rq type */ | |
5432 | poll->wait.func(&poll->wait, mode, sync, key); | |
5433 | } | |
18bceab1 | 5434 | } |
de9b4cca | 5435 | req_ref_put(req); |
18bceab1 JA |
5436 | return 1; |
5437 | } | |
5438 | ||
5439 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, | |
5440 | wait_queue_func_t wake_func) | |
5441 | { | |
5442 | poll->head = NULL; | |
5443 | poll->done = false; | |
5444 | poll->canceled = false; | |
464dca61 JA |
5445 | #define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP) |
5446 | /* mask in events that we always want/need */ | |
5447 | poll->events = events | IO_POLL_UNMASK; | |
18bceab1 JA |
5448 | INIT_LIST_HEAD(&poll->wait.entry); |
5449 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
5450 | } | |
5451 | ||
5452 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, | |
807abcb0 JA |
5453 | struct wait_queue_head *head, |
5454 | struct io_poll_iocb **poll_ptr) | |
18bceab1 JA |
5455 | { |
5456 | struct io_kiocb *req = pt->req; | |
5457 | ||
5458 | /* | |
68b11e8b PB |
5459 | * The file being polled uses multiple waitqueues for poll handling |
5460 | * (e.g. one for read, one for write). Setup a separate io_poll_iocb | |
5461 | * if this happens. | |
18bceab1 | 5462 | */ |
68b11e8b | 5463 | if (unlikely(pt->nr_entries)) { |
58852d4d PB |
5464 | struct io_poll_iocb *poll_one = poll; |
5465 | ||
23a65db8 PB |
5466 | /* double add on the same waitqueue head, ignore */ |
5467 | if (poll_one->head == head) | |
5468 | return; | |
18bceab1 | 5469 | /* already have a 2nd entry, fail a third attempt */ |
807abcb0 | 5470 | if (*poll_ptr) { |
23a65db8 PB |
5471 | if ((*poll_ptr)->head == head) |
5472 | return; | |
18bceab1 JA |
5473 | pt->error = -EINVAL; |
5474 | return; | |
5475 | } | |
ea6a693d JA |
5476 | /* |
5477 | * Can't handle multishot for double wait for now, turn it | |
5478 | * into one-shot mode. | |
5479 | */ | |
7a274727 PB |
5480 | if (!(poll_one->events & EPOLLONESHOT)) |
5481 | poll_one->events |= EPOLLONESHOT; | |
18bceab1 JA |
5482 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
5483 | if (!poll) { | |
5484 | pt->error = -ENOMEM; | |
5485 | return; | |
5486 | } | |
58852d4d | 5487 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
de9b4cca | 5488 | req_ref_get(req); |
18bceab1 | 5489 | poll->wait.private = req; |
d886e185 | 5490 | |
807abcb0 | 5491 | *poll_ptr = poll; |
d886e185 PB |
5492 | if (req->opcode == IORING_OP_POLL_ADD) |
5493 | req->flags |= REQ_F_ASYNC_DATA; | |
18bceab1 JA |
5494 | } |
5495 | ||
68b11e8b | 5496 | pt->nr_entries++; |
18bceab1 | 5497 | poll->head = head; |
a31eb4a2 JX |
5498 | |
5499 | if (poll->events & EPOLLEXCLUSIVE) | |
5500 | add_wait_queue_exclusive(head, &poll->wait); | |
5501 | else | |
5502 | add_wait_queue(head, &poll->wait); | |
18bceab1 JA |
5503 | } |
5504 | ||
5505 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
5506 | struct poll_table_struct *p) | |
5507 | { | |
5508 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
807abcb0 | 5509 | struct async_poll *apoll = pt->req->apoll; |
18bceab1 | 5510 | |
807abcb0 | 5511 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
18bceab1 JA |
5512 | } |
5513 | ||
f237c30a | 5514 | static void io_async_task_func(struct io_kiocb *req, bool *locked) |
d7718a9d | 5515 | { |
d7718a9d JA |
5516 | struct async_poll *apoll = req->apoll; |
5517 | struct io_ring_ctx *ctx = req->ctx; | |
5518 | ||
236daeae | 5519 | trace_io_uring_task_run(req->ctx, req, req->opcode, req->user_data); |
d7718a9d | 5520 | |
74ce6ce4 | 5521 | if (io_poll_rewait(req, &apoll->poll)) { |
79ebeaee | 5522 | spin_unlock(&ctx->completion_lock); |
74ce6ce4 | 5523 | return; |
d7718a9d JA |
5524 | } |
5525 | ||
0ea13b44 | 5526 | hash_del(&req->hash_node); |
d4e7cd36 | 5527 | io_poll_remove_double(req); |
bd99c71b | 5528 | apoll->poll.done = true; |
79ebeaee | 5529 | spin_unlock(&ctx->completion_lock); |
74ce6ce4 | 5530 | |
0be0b0e3 | 5531 | if (!READ_ONCE(apoll->poll.canceled)) |
f237c30a | 5532 | io_req_task_submit(req, locked); |
0be0b0e3 | 5533 | else |
2593553a | 5534 | io_req_complete_failed(req, -ECANCELED); |
d7718a9d JA |
5535 | } |
5536 | ||
5537 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, | |
5538 | void *key) | |
5539 | { | |
5540 | struct io_kiocb *req = wait->private; | |
5541 | struct io_poll_iocb *poll = &req->apoll->poll; | |
5542 | ||
5543 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, | |
5544 | key_to_poll(key)); | |
5545 | ||
5546 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); | |
5547 | } | |
5548 | ||
5549 | static void io_poll_req_insert(struct io_kiocb *req) | |
5550 | { | |
5551 | struct io_ring_ctx *ctx = req->ctx; | |
5552 | struct hlist_head *list; | |
5553 | ||
5554 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; | |
5555 | hlist_add_head(&req->hash_node, list); | |
5556 | } | |
5557 | ||
5558 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, | |
5559 | struct io_poll_iocb *poll, | |
5560 | struct io_poll_table *ipt, __poll_t mask, | |
5561 | wait_queue_func_t wake_func) | |
5562 | __acquires(&ctx->completion_lock) | |
5563 | { | |
5564 | struct io_ring_ctx *ctx = req->ctx; | |
5565 | bool cancel = false; | |
5566 | ||
4d52f338 | 5567 | INIT_HLIST_NODE(&req->hash_node); |
18bceab1 | 5568 | io_init_poll_iocb(poll, mask, wake_func); |
b90cd197 | 5569 | poll->file = req->file; |
18bceab1 | 5570 | poll->wait.private = req; |
d7718a9d JA |
5571 | |
5572 | ipt->pt._key = mask; | |
5573 | ipt->req = req; | |
68b11e8b PB |
5574 | ipt->error = 0; |
5575 | ipt->nr_entries = 0; | |
d7718a9d | 5576 | |
d7718a9d | 5577 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
68b11e8b PB |
5578 | if (unlikely(!ipt->nr_entries) && !ipt->error) |
5579 | ipt->error = -EINVAL; | |
d7718a9d | 5580 | |
79ebeaee | 5581 | spin_lock(&ctx->completion_lock); |
a890d01e | 5582 | if (ipt->error || (mask && (poll->events & EPOLLONESHOT))) |
46fee9ab | 5583 | io_poll_remove_double(req); |
d7718a9d | 5584 | if (likely(poll->head)) { |
79ebeaee | 5585 | spin_lock_irq(&poll->head->lock); |
d7718a9d JA |
5586 | if (unlikely(list_empty(&poll->wait.entry))) { |
5587 | if (ipt->error) | |
5588 | cancel = true; | |
5589 | ipt->error = 0; | |
5590 | mask = 0; | |
5591 | } | |
88e41cf9 | 5592 | if ((mask && (poll->events & EPOLLONESHOT)) || ipt->error) |
d7718a9d JA |
5593 | list_del_init(&poll->wait.entry); |
5594 | else if (cancel) | |
5595 | WRITE_ONCE(poll->canceled, true); | |
5596 | else if (!poll->done) /* actually waiting for an event */ | |
5597 | io_poll_req_insert(req); | |
79ebeaee | 5598 | spin_unlock_irq(&poll->head->lock); |
d7718a9d JA |
5599 | } |
5600 | ||
5601 | return mask; | |
5602 | } | |
5603 | ||
59b735ae OL |
5604 | enum { |
5605 | IO_APOLL_OK, | |
5606 | IO_APOLL_ABORTED, | |
5607 | IO_APOLL_READY | |
5608 | }; | |
5609 | ||
5610 | static int io_arm_poll_handler(struct io_kiocb *req) | |
d7718a9d JA |
5611 | { |
5612 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
5613 | struct io_ring_ctx *ctx = req->ctx; | |
5614 | struct async_poll *apoll; | |
5615 | struct io_poll_table ipt; | |
b2d9c3da | 5616 | __poll_t ret, mask = EPOLLONESHOT | POLLERR | POLLPRI; |
d7718a9d | 5617 | |
b2d9c3da PB |
5618 | if (!def->pollin && !def->pollout) |
5619 | return IO_APOLL_ABORTED; | |
658d0a40 PB |
5620 | if (!file_can_poll(req->file) || (req->flags & REQ_F_POLLED)) |
5621 | return IO_APOLL_ABORTED; | |
b2d9c3da PB |
5622 | |
5623 | if (def->pollin) { | |
b2d9c3da PB |
5624 | mask |= POLLIN | POLLRDNORM; |
5625 | ||
5626 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ | |
5627 | if ((req->opcode == IORING_OP_RECVMSG) && | |
5628 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) | |
5629 | mask &= ~POLLIN; | |
5630 | } else { | |
b2d9c3da PB |
5631 | mask |= POLLOUT | POLLWRNORM; |
5632 | } | |
5633 | ||
d7718a9d JA |
5634 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); |
5635 | if (unlikely(!apoll)) | |
59b735ae | 5636 | return IO_APOLL_ABORTED; |
807abcb0 | 5637 | apoll->double_poll = NULL; |
d7718a9d | 5638 | req->apoll = apoll; |
b2d9c3da | 5639 | req->flags |= REQ_F_POLLED; |
d7718a9d | 5640 | ipt.pt._qproc = io_async_queue_proc; |
48dcd38d | 5641 | io_req_set_refcount(req); |
d7718a9d JA |
5642 | |
5643 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, | |
5644 | io_async_wake); | |
79ebeaee | 5645 | spin_unlock(&ctx->completion_lock); |
41a5169c HX |
5646 | if (ret || ipt.error) |
5647 | return ret ? IO_APOLL_READY : IO_APOLL_ABORTED; | |
5648 | ||
236daeae OL |
5649 | trace_io_uring_poll_arm(ctx, req, req->opcode, req->user_data, |
5650 | mask, apoll->poll.events); | |
59b735ae | 5651 | return IO_APOLL_OK; |
d7718a9d JA |
5652 | } |
5653 | ||
5654 | static bool __io_poll_remove_one(struct io_kiocb *req, | |
b2e720ac | 5655 | struct io_poll_iocb *poll, bool do_cancel) |
e07785b0 | 5656 | __must_hold(&req->ctx->completion_lock) |
221c5eb2 | 5657 | { |
b41e9852 | 5658 | bool do_complete = false; |
221c5eb2 | 5659 | |
5082620f JA |
5660 | if (!poll->head) |
5661 | return false; | |
79ebeaee | 5662 | spin_lock_irq(&poll->head->lock); |
b2e720ac JA |
5663 | if (do_cancel) |
5664 | WRITE_ONCE(poll->canceled, true); | |
392edb45 JA |
5665 | if (!list_empty(&poll->wait.entry)) { |
5666 | list_del_init(&poll->wait.entry); | |
b41e9852 | 5667 | do_complete = true; |
221c5eb2 | 5668 | } |
79ebeaee | 5669 | spin_unlock_irq(&poll->head->lock); |
3bfa5bcb | 5670 | hash_del(&req->hash_node); |
d7718a9d JA |
5671 | return do_complete; |
5672 | } | |
5673 | ||
5d709043 | 5674 | static bool io_poll_remove_one(struct io_kiocb *req) |
e07785b0 | 5675 | __must_hold(&req->ctx->completion_lock) |
d7718a9d JA |
5676 | { |
5677 | bool do_complete; | |
5678 | ||
d4e7cd36 | 5679 | io_poll_remove_double(req); |
e31001a3 | 5680 | do_complete = __io_poll_remove_one(req, io_poll_get_single(req), true); |
d4e7cd36 | 5681 | |
b41e9852 | 5682 | if (do_complete) { |
d4d19c19 | 5683 | io_cqring_fill_event(req->ctx, req->user_data, -ECANCELED, 0); |
b41e9852 | 5684 | io_commit_cqring(req->ctx); |
93d2bcd2 | 5685 | req_set_fail(req); |
91c2f697 | 5686 | io_put_req_deferred(req); |
5d709043 | 5687 | } |
b41e9852 | 5688 | return do_complete; |
221c5eb2 JA |
5689 | } |
5690 | ||
76e1b642 JA |
5691 | /* |
5692 | * Returns true if we found and killed one or more poll requests | |
5693 | */ | |
c072481d PB |
5694 | static __cold bool io_poll_remove_all(struct io_ring_ctx *ctx, |
5695 | struct task_struct *tsk, bool cancel_all) | |
221c5eb2 | 5696 | { |
78076bb6 | 5697 | struct hlist_node *tmp; |
221c5eb2 | 5698 | struct io_kiocb *req; |
8e2e1faf | 5699 | int posted = 0, i; |
221c5eb2 | 5700 | |
79ebeaee | 5701 | spin_lock(&ctx->completion_lock); |
78076bb6 JA |
5702 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
5703 | struct hlist_head *list; | |
5704 | ||
5705 | list = &ctx->cancel_hash[i]; | |
f3606e3a | 5706 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
3dd0c97a | 5707 | if (io_match_task(req, tsk, cancel_all)) |
f3606e3a JA |
5708 | posted += io_poll_remove_one(req); |
5709 | } | |
221c5eb2 | 5710 | } |
79ebeaee | 5711 | spin_unlock(&ctx->completion_lock); |
b41e9852 | 5712 | |
8e2e1faf JA |
5713 | if (posted) |
5714 | io_cqring_ev_posted(ctx); | |
76e1b642 JA |
5715 | |
5716 | return posted != 0; | |
221c5eb2 JA |
5717 | } |
5718 | ||
9ba5fac8 PB |
5719 | static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr, |
5720 | bool poll_only) | |
e07785b0 | 5721 | __must_hold(&ctx->completion_lock) |
47f46768 | 5722 | { |
78076bb6 | 5723 | struct hlist_head *list; |
47f46768 JA |
5724 | struct io_kiocb *req; |
5725 | ||
78076bb6 JA |
5726 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
5727 | hlist_for_each_entry(req, list, hash_node) { | |
b41e9852 JA |
5728 | if (sqe_addr != req->user_data) |
5729 | continue; | |
9ba5fac8 PB |
5730 | if (poll_only && req->opcode != IORING_OP_POLL_ADD) |
5731 | continue; | |
b2cb805f | 5732 | return req; |
47f46768 | 5733 | } |
b2cb805f JA |
5734 | return NULL; |
5735 | } | |
5736 | ||
9ba5fac8 PB |
5737 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr, |
5738 | bool poll_only) | |
e07785b0 | 5739 | __must_hold(&ctx->completion_lock) |
b2cb805f JA |
5740 | { |
5741 | struct io_kiocb *req; | |
5742 | ||
9ba5fac8 | 5743 | req = io_poll_find(ctx, sqe_addr, poll_only); |
b2cb805f JA |
5744 | if (!req) |
5745 | return -ENOENT; | |
5746 | if (io_poll_remove_one(req)) | |
5747 | return 0; | |
5748 | ||
5749 | return -EALREADY; | |
47f46768 JA |
5750 | } |
5751 | ||
9096af3e PB |
5752 | static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe, |
5753 | unsigned int flags) | |
5754 | { | |
5755 | u32 events; | |
47f46768 | 5756 | |
9096af3e PB |
5757 | events = READ_ONCE(sqe->poll32_events); |
5758 | #ifdef __BIG_ENDIAN | |
5759 | events = swahw32(events); | |
5760 | #endif | |
5761 | if (!(flags & IORING_POLL_ADD_MULTI)) | |
5762 | events |= EPOLLONESHOT; | |
5763 | return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT)); | |
47f46768 JA |
5764 | } |
5765 | ||
c5de0036 | 5766 | static int io_poll_update_prep(struct io_kiocb *req, |
3529d8c2 | 5767 | const struct io_uring_sqe *sqe) |
0969e783 | 5768 | { |
c5de0036 PB |
5769 | struct io_poll_update *upd = &req->poll_update; |
5770 | u32 flags; | |
5771 | ||
0969e783 JA |
5772 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5773 | return -EINVAL; | |
26578cda | 5774 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
c5de0036 PB |
5775 | return -EINVAL; |
5776 | flags = READ_ONCE(sqe->len); | |
5777 | if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA | | |
5778 | IORING_POLL_ADD_MULTI)) | |
5779 | return -EINVAL; | |
5780 | /* meaningless without update */ | |
5781 | if (flags == IORING_POLL_ADD_MULTI) | |
0969e783 JA |
5782 | return -EINVAL; |
5783 | ||
c5de0036 PB |
5784 | upd->old_user_data = READ_ONCE(sqe->addr); |
5785 | upd->update_events = flags & IORING_POLL_UPDATE_EVENTS; | |
5786 | upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA; | |
221c5eb2 | 5787 | |
c5de0036 PB |
5788 | upd->new_user_data = READ_ONCE(sqe->off); |
5789 | if (!upd->update_user_data && upd->new_user_data) | |
5790 | return -EINVAL; | |
5791 | if (upd->update_events) | |
5792 | upd->events = io_poll_parse_events(sqe, flags); | |
5793 | else if (sqe->poll32_events) | |
5794 | return -EINVAL; | |
221c5eb2 | 5795 | |
221c5eb2 JA |
5796 | return 0; |
5797 | } | |
5798 | ||
221c5eb2 JA |
5799 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
5800 | void *key) | |
5801 | { | |
c2f2eb7d JA |
5802 | struct io_kiocb *req = wait->private; |
5803 | struct io_poll_iocb *poll = &req->poll; | |
221c5eb2 | 5804 | |
d7718a9d | 5805 | return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func); |
221c5eb2 JA |
5806 | } |
5807 | ||
221c5eb2 JA |
5808 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
5809 | struct poll_table_struct *p) | |
5810 | { | |
5811 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
5812 | ||
e8c2bc1f | 5813 | __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data); |
eac406c6 JA |
5814 | } |
5815 | ||
3529d8c2 | 5816 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
5817 | { |
5818 | struct io_poll_iocb *poll = &req->poll; | |
c5de0036 | 5819 | u32 flags; |
221c5eb2 JA |
5820 | |
5821 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
5822 | return -EINVAL; | |
c5de0036 | 5823 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr) |
88e41cf9 JA |
5824 | return -EINVAL; |
5825 | flags = READ_ONCE(sqe->len); | |
c5de0036 | 5826 | if (flags & ~IORING_POLL_ADD_MULTI) |
221c5eb2 JA |
5827 | return -EINVAL; |
5828 | ||
48dcd38d | 5829 | io_req_set_refcount(req); |
c5de0036 | 5830 | poll->events = io_poll_parse_events(sqe, flags); |
0969e783 JA |
5831 | return 0; |
5832 | } | |
5833 | ||
61e98203 | 5834 | static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags) |
0969e783 JA |
5835 | { |
5836 | struct io_poll_iocb *poll = &req->poll; | |
5837 | struct io_ring_ctx *ctx = req->ctx; | |
5838 | struct io_poll_table ipt; | |
0969e783 | 5839 | __poll_t mask; |
5b7aa38d | 5840 | bool done; |
0969e783 | 5841 | |
d7718a9d | 5842 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 5843 | |
d7718a9d JA |
5844 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
5845 | io_poll_wake); | |
221c5eb2 | 5846 | |
8c838788 | 5847 | if (mask) { /* no async, we'd stolen it */ |
221c5eb2 | 5848 | ipt.error = 0; |
eb6e6f06 PB |
5849 | done = __io_poll_complete(req, mask); |
5850 | io_commit_cqring(req->ctx); | |
221c5eb2 | 5851 | } |
79ebeaee | 5852 | spin_unlock(&ctx->completion_lock); |
221c5eb2 | 5853 | |
8c838788 JA |
5854 | if (mask) { |
5855 | io_cqring_ev_posted(ctx); | |
5b7aa38d | 5856 | if (done) |
88e41cf9 | 5857 | io_put_req(req); |
221c5eb2 | 5858 | } |
8c838788 | 5859 | return ipt.error; |
221c5eb2 JA |
5860 | } |
5861 | ||
c5de0036 | 5862 | static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags) |
b69de288 JA |
5863 | { |
5864 | struct io_ring_ctx *ctx = req->ctx; | |
5865 | struct io_kiocb *preq; | |
cb3b200e | 5866 | bool completing; |
b69de288 JA |
5867 | int ret; |
5868 | ||
79ebeaee | 5869 | spin_lock(&ctx->completion_lock); |
9ba5fac8 | 5870 | preq = io_poll_find(ctx, req->poll_update.old_user_data, true); |
b69de288 JA |
5871 | if (!preq) { |
5872 | ret = -ENOENT; | |
5873 | goto err; | |
b69de288 | 5874 | } |
cb3b200e | 5875 | |
c5de0036 PB |
5876 | if (!req->poll_update.update_events && !req->poll_update.update_user_data) { |
5877 | completing = true; | |
5878 | ret = io_poll_remove_one(preq) ? 0 : -EALREADY; | |
5879 | goto err; | |
5880 | } | |
5881 | ||
cb3b200e JA |
5882 | /* |
5883 | * Don't allow racy completion with singleshot, as we cannot safely | |
5884 | * update those. For multishot, if we're racing with completion, just | |
5885 | * let completion re-add it. | |
5886 | */ | |
5887 | completing = !__io_poll_remove_one(preq, &preq->poll, false); | |
5888 | if (completing && (preq->poll.events & EPOLLONESHOT)) { | |
5889 | ret = -EALREADY; | |
5890 | goto err; | |
b69de288 JA |
5891 | } |
5892 | /* we now have a detached poll request. reissue. */ | |
5893 | ret = 0; | |
5894 | err: | |
b69de288 | 5895 | if (ret < 0) { |
79ebeaee | 5896 | spin_unlock(&ctx->completion_lock); |
93d2bcd2 | 5897 | req_set_fail(req); |
b69de288 JA |
5898 | io_req_complete(req, ret); |
5899 | return 0; | |
5900 | } | |
5901 | /* only mask one event flags, keep behavior flags */ | |
9d805892 | 5902 | if (req->poll_update.update_events) { |
b69de288 | 5903 | preq->poll.events &= ~0xffff; |
9d805892 | 5904 | preq->poll.events |= req->poll_update.events & 0xffff; |
b69de288 JA |
5905 | preq->poll.events |= IO_POLL_UNMASK; |
5906 | } | |
9d805892 PB |
5907 | if (req->poll_update.update_user_data) |
5908 | preq->user_data = req->poll_update.new_user_data; | |
79ebeaee | 5909 | spin_unlock(&ctx->completion_lock); |
cb3b200e | 5910 | |
b69de288 JA |
5911 | /* complete update request, we're done with it */ |
5912 | io_req_complete(req, ret); | |
5913 | ||
cb3b200e | 5914 | if (!completing) { |
c5de0036 | 5915 | ret = io_poll_add(preq, issue_flags); |
cb3b200e | 5916 | if (ret < 0) { |
93d2bcd2 | 5917 | req_set_fail(preq); |
cb3b200e JA |
5918 | io_req_complete(preq, ret); |
5919 | } | |
b69de288 JA |
5920 | } |
5921 | return 0; | |
5922 | } | |
5923 | ||
f237c30a | 5924 | static void io_req_task_timeout(struct io_kiocb *req, bool *locked) |
89850fce | 5925 | { |
6224590d PB |
5926 | struct io_timeout_data *data = req->async_data; |
5927 | ||
5928 | if (!(data->flags & IORING_TIMEOUT_ETIME_SUCCESS)) | |
5929 | req_set_fail(req); | |
505657bc | 5930 | io_req_complete_post(req, -ETIME, 0); |
89850fce JA |
5931 | } |
5932 | ||
5262f567 JA |
5933 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
5934 | { | |
ad8a48ac JA |
5935 | struct io_timeout_data *data = container_of(timer, |
5936 | struct io_timeout_data, timer); | |
5937 | struct io_kiocb *req = data->req; | |
5938 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
5939 | unsigned long flags; |
5940 | ||
89850fce | 5941 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
a71976f3 | 5942 | list_del_init(&req->timeout.list); |
01cec8c1 PB |
5943 | atomic_set(&req->ctx->cq_timeouts, |
5944 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
89850fce | 5945 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); |
01cec8c1 | 5946 | |
89850fce JA |
5947 | req->io_task_work.func = io_req_task_timeout; |
5948 | io_req_task_work_add(req); | |
5262f567 JA |
5949 | return HRTIMER_NORESTART; |
5950 | } | |
5951 | ||
fbd15848 PB |
5952 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
5953 | __u64 user_data) | |
89850fce | 5954 | __must_hold(&ctx->timeout_lock) |
f254ac04 | 5955 | { |
fbd15848 | 5956 | struct io_timeout_data *io; |
47f46768 | 5957 | struct io_kiocb *req; |
fd9c7bc5 | 5958 | bool found = false; |
f254ac04 | 5959 | |
135fcde8 | 5960 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
fd9c7bc5 PB |
5961 | found = user_data == req->user_data; |
5962 | if (found) | |
47f46768 | 5963 | break; |
47f46768 | 5964 | } |
fd9c7bc5 PB |
5965 | if (!found) |
5966 | return ERR_PTR(-ENOENT); | |
fbd15848 PB |
5967 | |
5968 | io = req->async_data; | |
fd9c7bc5 | 5969 | if (hrtimer_try_to_cancel(&io->timer) == -1) |
fbd15848 | 5970 | return ERR_PTR(-EALREADY); |
a71976f3 | 5971 | list_del_init(&req->timeout.list); |
fbd15848 PB |
5972 | return req; |
5973 | } | |
47f46768 | 5974 | |
fbd15848 | 5975 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
ec3c3d0f | 5976 | __must_hold(&ctx->completion_lock) |
89850fce | 5977 | __must_hold(&ctx->timeout_lock) |
fbd15848 PB |
5978 | { |
5979 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); | |
5980 | ||
5981 | if (IS_ERR(req)) | |
5982 | return PTR_ERR(req); | |
f254ac04 | 5983 | |
93d2bcd2 | 5984 | req_set_fail(req); |
d4d19c19 | 5985 | io_cqring_fill_event(ctx, req->user_data, -ECANCELED, 0); |
91c2f697 | 5986 | io_put_req_deferred(req); |
f254ac04 JA |
5987 | return 0; |
5988 | } | |
5989 | ||
50c1df2b JA |
5990 | static clockid_t io_timeout_get_clock(struct io_timeout_data *data) |
5991 | { | |
5992 | switch (data->flags & IORING_TIMEOUT_CLOCK_MASK) { | |
5993 | case IORING_TIMEOUT_BOOTTIME: | |
5994 | return CLOCK_BOOTTIME; | |
5995 | case IORING_TIMEOUT_REALTIME: | |
5996 | return CLOCK_REALTIME; | |
5997 | default: | |
5998 | /* can't happen, vetted at prep time */ | |
5999 | WARN_ON_ONCE(1); | |
6000 | fallthrough; | |
6001 | case 0: | |
6002 | return CLOCK_MONOTONIC; | |
6003 | } | |
6004 | } | |
6005 | ||
f1042b6c PB |
6006 | static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
6007 | struct timespec64 *ts, enum hrtimer_mode mode) | |
6008 | __must_hold(&ctx->timeout_lock) | |
6009 | { | |
6010 | struct io_timeout_data *io; | |
6011 | struct io_kiocb *req; | |
6012 | bool found = false; | |
6013 | ||
6014 | list_for_each_entry(req, &ctx->ltimeout_list, timeout.list) { | |
6015 | found = user_data == req->user_data; | |
6016 | if (found) | |
6017 | break; | |
6018 | } | |
6019 | if (!found) | |
6020 | return -ENOENT; | |
6021 | ||
6022 | io = req->async_data; | |
6023 | if (hrtimer_try_to_cancel(&io->timer) == -1) | |
6024 | return -EALREADY; | |
6025 | hrtimer_init(&io->timer, io_timeout_get_clock(io), mode); | |
6026 | io->timer.function = io_link_timeout_fn; | |
6027 | hrtimer_start(&io->timer, timespec64_to_ktime(*ts), mode); | |
6028 | return 0; | |
6029 | } | |
6030 | ||
9c8e11b3 PB |
6031 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
6032 | struct timespec64 *ts, enum hrtimer_mode mode) | |
89850fce | 6033 | __must_hold(&ctx->timeout_lock) |
47f46768 | 6034 | { |
9c8e11b3 PB |
6035 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
6036 | struct io_timeout_data *data; | |
47f46768 | 6037 | |
9c8e11b3 PB |
6038 | if (IS_ERR(req)) |
6039 | return PTR_ERR(req); | |
47f46768 | 6040 | |
9c8e11b3 PB |
6041 | req->timeout.off = 0; /* noseq */ |
6042 | data = req->async_data; | |
6043 | list_add_tail(&req->timeout.list, &ctx->timeout_list); | |
50c1df2b | 6044 | hrtimer_init(&data->timer, io_timeout_get_clock(data), mode); |
9c8e11b3 PB |
6045 | data->timer.function = io_timeout_fn; |
6046 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); | |
6047 | return 0; | |
47f46768 JA |
6048 | } |
6049 | ||
3529d8c2 JA |
6050 | static int io_timeout_remove_prep(struct io_kiocb *req, |
6051 | const struct io_uring_sqe *sqe) | |
b29472ee | 6052 | { |
9c8e11b3 PB |
6053 | struct io_timeout_rem *tr = &req->timeout_rem; |
6054 | ||
b29472ee JA |
6055 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
6056 | return -EINVAL; | |
61710e43 DA |
6057 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6058 | return -EINVAL; | |
26578cda | 6059 | if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in) |
b29472ee JA |
6060 | return -EINVAL; |
6061 | ||
f1042b6c | 6062 | tr->ltimeout = false; |
9c8e11b3 PB |
6063 | tr->addr = READ_ONCE(sqe->addr); |
6064 | tr->flags = READ_ONCE(sqe->timeout_flags); | |
f1042b6c PB |
6065 | if (tr->flags & IORING_TIMEOUT_UPDATE_MASK) { |
6066 | if (hweight32(tr->flags & IORING_TIMEOUT_CLOCK_MASK) > 1) | |
6067 | return -EINVAL; | |
6068 | if (tr->flags & IORING_LINK_TIMEOUT_UPDATE) | |
6069 | tr->ltimeout = true; | |
6070 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE_MASK|IORING_TIMEOUT_ABS)) | |
9c8e11b3 PB |
6071 | return -EINVAL; |
6072 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) | |
6073 | return -EFAULT; | |
6074 | } else if (tr->flags) { | |
6075 | /* timeout removal doesn't support flags */ | |
b29472ee | 6076 | return -EINVAL; |
9c8e11b3 | 6077 | } |
b29472ee | 6078 | |
b29472ee JA |
6079 | return 0; |
6080 | } | |
6081 | ||
8662daec PB |
6082 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
6083 | { | |
6084 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS | |
6085 | : HRTIMER_MODE_REL; | |
6086 | } | |
6087 | ||
11365043 JA |
6088 | /* |
6089 | * Remove or update an existing timeout command | |
6090 | */ | |
61e98203 | 6091 | static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags) |
11365043 | 6092 | { |
9c8e11b3 | 6093 | struct io_timeout_rem *tr = &req->timeout_rem; |
11365043 | 6094 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 | 6095 | int ret; |
11365043 | 6096 | |
ec3c3d0f PB |
6097 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) { |
6098 | spin_lock(&ctx->completion_lock); | |
6099 | spin_lock_irq(&ctx->timeout_lock); | |
9c8e11b3 | 6100 | ret = io_timeout_cancel(ctx, tr->addr); |
ec3c3d0f PB |
6101 | spin_unlock_irq(&ctx->timeout_lock); |
6102 | spin_unlock(&ctx->completion_lock); | |
6103 | } else { | |
f1042b6c PB |
6104 | enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags); |
6105 | ||
ec3c3d0f | 6106 | spin_lock_irq(&ctx->timeout_lock); |
f1042b6c PB |
6107 | if (tr->ltimeout) |
6108 | ret = io_linked_timeout_update(ctx, tr->addr, &tr->ts, mode); | |
6109 | else | |
6110 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode); | |
ec3c3d0f PB |
6111 | spin_unlock_irq(&ctx->timeout_lock); |
6112 | } | |
11365043 | 6113 | |
4e88d6e7 | 6114 | if (ret < 0) |
93d2bcd2 | 6115 | req_set_fail(req); |
505657bc | 6116 | io_req_complete_post(req, ret, 0); |
11365043 | 6117 | return 0; |
5262f567 JA |
6118 | } |
6119 | ||
3529d8c2 | 6120 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 6121 | bool is_timeout_link) |
5262f567 | 6122 | { |
ad8a48ac | 6123 | struct io_timeout_data *data; |
a41525ab | 6124 | unsigned flags; |
56080b02 | 6125 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 6126 | |
ad8a48ac | 6127 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 6128 | return -EINVAL; |
26578cda PB |
6129 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1 || |
6130 | sqe->splice_fd_in) | |
a41525ab | 6131 | return -EINVAL; |
56080b02 | 6132 | if (off && is_timeout_link) |
2d28390a | 6133 | return -EINVAL; |
a41525ab | 6134 | flags = READ_ONCE(sqe->timeout_flags); |
6224590d PB |
6135 | if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK | |
6136 | IORING_TIMEOUT_ETIME_SUCCESS)) | |
50c1df2b JA |
6137 | return -EINVAL; |
6138 | /* more than one clock specified is invalid, obviously */ | |
6139 | if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1) | |
5262f567 | 6140 | return -EINVAL; |
bdf20073 | 6141 | |
ef9dd637 | 6142 | INIT_LIST_HEAD(&req->timeout.list); |
bfe68a22 | 6143 | req->timeout.off = off; |
f18ee4cf PB |
6144 | if (unlikely(off && !req->ctx->off_timeout_used)) |
6145 | req->ctx->off_timeout_used = true; | |
26a61679 | 6146 | |
d6a644a7 PB |
6147 | if (WARN_ON_ONCE(req_has_async_data(req))) |
6148 | return -EFAULT; | |
6149 | if (io_alloc_async_data(req)) | |
26a61679 JA |
6150 | return -ENOMEM; |
6151 | ||
e8c2bc1f | 6152 | data = req->async_data; |
ad8a48ac | 6153 | data->req = req; |
50c1df2b | 6154 | data->flags = flags; |
ad8a48ac JA |
6155 | |
6156 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
6157 | return -EFAULT; |
6158 | ||
8662daec | 6159 | data->mode = io_translate_timeout_mode(flags); |
50c1df2b | 6160 | hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode); |
b97e736a PB |
6161 | |
6162 | if (is_timeout_link) { | |
6163 | struct io_submit_link *link = &req->ctx->submit_state.link; | |
6164 | ||
6165 | if (!link->head) | |
6166 | return -EINVAL; | |
6167 | if (link->last->opcode == IORING_OP_LINK_TIMEOUT) | |
6168 | return -EINVAL; | |
4d13d1a4 PB |
6169 | req->timeout.head = link->last; |
6170 | link->last->flags |= REQ_F_ARM_LTIMEOUT; | |
b97e736a | 6171 | } |
ad8a48ac JA |
6172 | return 0; |
6173 | } | |
6174 | ||
61e98203 | 6175 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
ad8a48ac | 6176 | { |
ad8a48ac | 6177 | struct io_ring_ctx *ctx = req->ctx; |
e8c2bc1f | 6178 | struct io_timeout_data *data = req->async_data; |
ad8a48ac | 6179 | struct list_head *entry; |
bfe68a22 | 6180 | u32 tail, off = req->timeout.off; |
ad8a48ac | 6181 | |
89850fce | 6182 | spin_lock_irq(&ctx->timeout_lock); |
93bd25bb | 6183 | |
5262f567 JA |
6184 | /* |
6185 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
6186 | * timeout event to be satisfied. If it isn't set, then this is |
6187 | * a pure timeout request, sequence isn't used. | |
5262f567 | 6188 | */ |
8eb7e2d0 | 6189 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
6190 | entry = ctx->timeout_list.prev; |
6191 | goto add; | |
6192 | } | |
5262f567 | 6193 | |
bfe68a22 PB |
6194 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
6195 | req->timeout.target_seq = tail + off; | |
5262f567 | 6196 | |
f010505b MDG |
6197 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
6198 | * This is safe because ->completion_lock is held, and submissions | |
6199 | * and completions are never mixed in the same ->completion_lock section. | |
6200 | */ | |
6201 | ctx->cq_last_tm_flush = tail; | |
6202 | ||
5262f567 JA |
6203 | /* |
6204 | * Insertion sort, ensuring the first entry in the list is always | |
6205 | * the one we need first. | |
6206 | */ | |
5262f567 | 6207 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
6208 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
6209 | timeout.list); | |
5262f567 | 6210 | |
8eb7e2d0 | 6211 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 6212 | continue; |
bfe68a22 PB |
6213 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
6214 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
6215 | break; |
6216 | } | |
93bd25bb | 6217 | add: |
135fcde8 | 6218 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
6219 | data->timer.function = io_timeout_fn; |
6220 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
89850fce | 6221 | spin_unlock_irq(&ctx->timeout_lock); |
5262f567 JA |
6222 | return 0; |
6223 | } | |
5262f567 | 6224 | |
f458dd84 PB |
6225 | struct io_cancel_data { |
6226 | struct io_ring_ctx *ctx; | |
6227 | u64 user_data; | |
6228 | }; | |
6229 | ||
62755e35 JA |
6230 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
6231 | { | |
6232 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
f458dd84 | 6233 | struct io_cancel_data *cd = data; |
62755e35 | 6234 | |
f458dd84 | 6235 | return req->ctx == cd->ctx && req->user_data == cd->user_data; |
62755e35 JA |
6236 | } |
6237 | ||
f458dd84 PB |
6238 | static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data, |
6239 | struct io_ring_ctx *ctx) | |
62755e35 | 6240 | { |
f458dd84 | 6241 | struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, }; |
62755e35 | 6242 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
6243 | int ret = 0; |
6244 | ||
f458dd84 | 6245 | if (!tctx || !tctx->io_wq) |
5aa75ed5 JA |
6246 | return -ENOENT; |
6247 | ||
f458dd84 | 6248 | cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false); |
62755e35 JA |
6249 | switch (cancel_ret) { |
6250 | case IO_WQ_CANCEL_OK: | |
6251 | ret = 0; | |
6252 | break; | |
6253 | case IO_WQ_CANCEL_RUNNING: | |
6254 | ret = -EALREADY; | |
6255 | break; | |
6256 | case IO_WQ_CANCEL_NOTFOUND: | |
6257 | ret = -ENOENT; | |
6258 | break; | |
6259 | } | |
6260 | ||
e977d6d3 JA |
6261 | return ret; |
6262 | } | |
6263 | ||
8cb01fac | 6264 | static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr) |
47f46768 | 6265 | { |
8cb01fac | 6266 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 JA |
6267 | int ret; |
6268 | ||
dadebc35 | 6269 | WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current); |
8cb01fac | 6270 | |
f458dd84 | 6271 | ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx); |
df9727af | 6272 | if (ret != -ENOENT) |
8cb01fac | 6273 | return ret; |
505657bc PB |
6274 | |
6275 | spin_lock(&ctx->completion_lock); | |
79ebeaee | 6276 | spin_lock_irq(&ctx->timeout_lock); |
47f46768 | 6277 | ret = io_timeout_cancel(ctx, sqe_addr); |
79ebeaee | 6278 | spin_unlock_irq(&ctx->timeout_lock); |
47f46768 | 6279 | if (ret != -ENOENT) |
505657bc PB |
6280 | goto out; |
6281 | ret = io_poll_cancel(ctx, sqe_addr, false); | |
6282 | out: | |
6283 | spin_unlock(&ctx->completion_lock); | |
6284 | return ret; | |
47f46768 JA |
6285 | } |
6286 | ||
3529d8c2 JA |
6287 | static int io_async_cancel_prep(struct io_kiocb *req, |
6288 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 6289 | { |
fbf23849 | 6290 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 6291 | return -EINVAL; |
61710e43 DA |
6292 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6293 | return -EINVAL; | |
26578cda PB |
6294 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags || |
6295 | sqe->splice_fd_in) | |
e977d6d3 JA |
6296 | return -EINVAL; |
6297 | ||
fbf23849 JA |
6298 | req->cancel.addr = READ_ONCE(sqe->addr); |
6299 | return 0; | |
6300 | } | |
6301 | ||
61e98203 | 6302 | static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6303 | { |
6304 | struct io_ring_ctx *ctx = req->ctx; | |
58f99373 | 6305 | u64 sqe_addr = req->cancel.addr; |
3b44b371 | 6306 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
58f99373 PB |
6307 | struct io_tctx_node *node; |
6308 | int ret; | |
6309 | ||
8cb01fac | 6310 | ret = io_try_cancel_userdata(req, sqe_addr); |
58f99373 PB |
6311 | if (ret != -ENOENT) |
6312 | goto done; | |
58f99373 PB |
6313 | |
6314 | /* slow path, try all io-wq's */ | |
3b44b371 | 6315 | io_ring_submit_lock(ctx, needs_lock); |
58f99373 PB |
6316 | ret = -ENOENT; |
6317 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
6318 | struct io_uring_task *tctx = node->task->io_uring; | |
fbf23849 | 6319 | |
58f99373 PB |
6320 | ret = io_async_cancel_one(tctx, req->cancel.addr, ctx); |
6321 | if (ret != -ENOENT) | |
6322 | break; | |
6323 | } | |
3b44b371 | 6324 | io_ring_submit_unlock(ctx, needs_lock); |
58f99373 | 6325 | done: |
58f99373 | 6326 | if (ret < 0) |
93d2bcd2 | 6327 | req_set_fail(req); |
505657bc | 6328 | io_req_complete_post(req, ret, 0); |
5262f567 JA |
6329 | return 0; |
6330 | } | |
6331 | ||
269bbe5f | 6332 | static int io_rsrc_update_prep(struct io_kiocb *req, |
05f3fb3c JA |
6333 | const struct io_uring_sqe *sqe) |
6334 | { | |
61710e43 DA |
6335 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6336 | return -EINVAL; | |
26578cda | 6337 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
05f3fb3c JA |
6338 | return -EINVAL; |
6339 | ||
269bbe5f BM |
6340 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
6341 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); | |
6342 | if (!req->rsrc_update.nr_args) | |
05f3fb3c | 6343 | return -EINVAL; |
269bbe5f | 6344 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
05f3fb3c JA |
6345 | return 0; |
6346 | } | |
6347 | ||
889fca73 | 6348 | static int io_files_update(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6349 | { |
6350 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 6351 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
c3bdad02 | 6352 | struct io_uring_rsrc_update2 up; |
05f3fb3c | 6353 | int ret; |
fbf23849 | 6354 | |
269bbe5f BM |
6355 | up.offset = req->rsrc_update.offset; |
6356 | up.data = req->rsrc_update.arg; | |
c3bdad02 PB |
6357 | up.nr = 0; |
6358 | up.tags = 0; | |
615cee49 | 6359 | up.resv = 0; |
05f3fb3c | 6360 | |
3b44b371 | 6361 | io_ring_submit_lock(ctx, needs_lock); |
fdecb662 | 6362 | ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE, |
98f0b3b4 | 6363 | &up, req->rsrc_update.nr_args); |
3b44b371 | 6364 | io_ring_submit_unlock(ctx, needs_lock); |
05f3fb3c JA |
6365 | |
6366 | if (ret < 0) | |
93d2bcd2 | 6367 | req_set_fail(req); |
889fca73 | 6368 | __io_req_complete(req, issue_flags, ret, 0); |
5262f567 JA |
6369 | return 0; |
6370 | } | |
6371 | ||
bfe76559 | 6372 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 6373 | { |
d625c6ee | 6374 | switch (req->opcode) { |
e781573e | 6375 | case IORING_OP_NOP: |
bfe76559 | 6376 | return 0; |
f67676d1 JA |
6377 | case IORING_OP_READV: |
6378 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 6379 | case IORING_OP_READ: |
bfe76559 | 6380 | return io_read_prep(req, sqe); |
f67676d1 JA |
6381 | case IORING_OP_WRITEV: |
6382 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 6383 | case IORING_OP_WRITE: |
bfe76559 | 6384 | return io_write_prep(req, sqe); |
0969e783 | 6385 | case IORING_OP_POLL_ADD: |
bfe76559 | 6386 | return io_poll_add_prep(req, sqe); |
0969e783 | 6387 | case IORING_OP_POLL_REMOVE: |
c5de0036 | 6388 | return io_poll_update_prep(req, sqe); |
8ed8d3c3 | 6389 | case IORING_OP_FSYNC: |
1155c76a | 6390 | return io_fsync_prep(req, sqe); |
8ed8d3c3 | 6391 | case IORING_OP_SYNC_FILE_RANGE: |
1155c76a | 6392 | return io_sfr_prep(req, sqe); |
03b1230c | 6393 | case IORING_OP_SENDMSG: |
fddaface | 6394 | case IORING_OP_SEND: |
bfe76559 | 6395 | return io_sendmsg_prep(req, sqe); |
03b1230c | 6396 | case IORING_OP_RECVMSG: |
fddaface | 6397 | case IORING_OP_RECV: |
bfe76559 | 6398 | return io_recvmsg_prep(req, sqe); |
f499a021 | 6399 | case IORING_OP_CONNECT: |
bfe76559 | 6400 | return io_connect_prep(req, sqe); |
2d28390a | 6401 | case IORING_OP_TIMEOUT: |
bfe76559 | 6402 | return io_timeout_prep(req, sqe, false); |
b29472ee | 6403 | case IORING_OP_TIMEOUT_REMOVE: |
bfe76559 | 6404 | return io_timeout_remove_prep(req, sqe); |
fbf23849 | 6405 | case IORING_OP_ASYNC_CANCEL: |
bfe76559 | 6406 | return io_async_cancel_prep(req, sqe); |
2d28390a | 6407 | case IORING_OP_LINK_TIMEOUT: |
bfe76559 | 6408 | return io_timeout_prep(req, sqe, true); |
8ed8d3c3 | 6409 | case IORING_OP_ACCEPT: |
bfe76559 | 6410 | return io_accept_prep(req, sqe); |
d63d1b5e | 6411 | case IORING_OP_FALLOCATE: |
bfe76559 | 6412 | return io_fallocate_prep(req, sqe); |
15b71abe | 6413 | case IORING_OP_OPENAT: |
bfe76559 | 6414 | return io_openat_prep(req, sqe); |
b5dba59e | 6415 | case IORING_OP_CLOSE: |
bfe76559 | 6416 | return io_close_prep(req, sqe); |
05f3fb3c | 6417 | case IORING_OP_FILES_UPDATE: |
269bbe5f | 6418 | return io_rsrc_update_prep(req, sqe); |
eddc7ef5 | 6419 | case IORING_OP_STATX: |
bfe76559 | 6420 | return io_statx_prep(req, sqe); |
4840e418 | 6421 | case IORING_OP_FADVISE: |
bfe76559 | 6422 | return io_fadvise_prep(req, sqe); |
c1ca757b | 6423 | case IORING_OP_MADVISE: |
bfe76559 | 6424 | return io_madvise_prep(req, sqe); |
cebdb986 | 6425 | case IORING_OP_OPENAT2: |
bfe76559 | 6426 | return io_openat2_prep(req, sqe); |
3e4827b0 | 6427 | case IORING_OP_EPOLL_CTL: |
bfe76559 | 6428 | return io_epoll_ctl_prep(req, sqe); |
7d67af2c | 6429 | case IORING_OP_SPLICE: |
bfe76559 | 6430 | return io_splice_prep(req, sqe); |
ddf0322d | 6431 | case IORING_OP_PROVIDE_BUFFERS: |
bfe76559 | 6432 | return io_provide_buffers_prep(req, sqe); |
067524e9 | 6433 | case IORING_OP_REMOVE_BUFFERS: |
bfe76559 | 6434 | return io_remove_buffers_prep(req, sqe); |
f2a8d5c7 | 6435 | case IORING_OP_TEE: |
bfe76559 | 6436 | return io_tee_prep(req, sqe); |
36f4fa68 JA |
6437 | case IORING_OP_SHUTDOWN: |
6438 | return io_shutdown_prep(req, sqe); | |
80a261fd JA |
6439 | case IORING_OP_RENAMEAT: |
6440 | return io_renameat_prep(req, sqe); | |
14a1143b JA |
6441 | case IORING_OP_UNLINKAT: |
6442 | return io_unlinkat_prep(req, sqe); | |
e34a02dc DK |
6443 | case IORING_OP_MKDIRAT: |
6444 | return io_mkdirat_prep(req, sqe); | |
7a8721f8 DK |
6445 | case IORING_OP_SYMLINKAT: |
6446 | return io_symlinkat_prep(req, sqe); | |
cf30da90 DK |
6447 | case IORING_OP_LINKAT: |
6448 | return io_linkat_prep(req, sqe); | |
f67676d1 JA |
6449 | } |
6450 | ||
bfe76559 PB |
6451 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
6452 | req->opcode); | |
bd54b6fe | 6453 | return -EINVAL; |
bfe76559 PB |
6454 | } |
6455 | ||
93642ef8 | 6456 | static int io_req_prep_async(struct io_kiocb *req) |
bfe76559 | 6457 | { |
b7e298d2 PB |
6458 | if (!io_op_defs[req->opcode].needs_async_setup) |
6459 | return 0; | |
d886e185 | 6460 | if (WARN_ON_ONCE(req_has_async_data(req))) |
b7e298d2 PB |
6461 | return -EFAULT; |
6462 | if (io_alloc_async_data(req)) | |
6463 | return -EAGAIN; | |
6464 | ||
93642ef8 PB |
6465 | switch (req->opcode) { |
6466 | case IORING_OP_READV: | |
93642ef8 PB |
6467 | return io_rw_prep_async(req, READ); |
6468 | case IORING_OP_WRITEV: | |
93642ef8 PB |
6469 | return io_rw_prep_async(req, WRITE); |
6470 | case IORING_OP_SENDMSG: | |
93642ef8 PB |
6471 | return io_sendmsg_prep_async(req); |
6472 | case IORING_OP_RECVMSG: | |
93642ef8 PB |
6473 | return io_recvmsg_prep_async(req); |
6474 | case IORING_OP_CONNECT: | |
6475 | return io_connect_prep_async(req); | |
6476 | } | |
b7e298d2 PB |
6477 | printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n", |
6478 | req->opcode); | |
6479 | return -EFAULT; | |
f67676d1 JA |
6480 | } |
6481 | ||
9cf7c104 PB |
6482 | static u32 io_get_sequence(struct io_kiocb *req) |
6483 | { | |
a3dbdf54 | 6484 | u32 seq = req->ctx->cached_sq_head; |
9cf7c104 | 6485 | |
a3dbdf54 PB |
6486 | /* need original cached_sq_head, but it was increased for each req */ |
6487 | io_for_each_link(req, req) | |
6488 | seq--; | |
6489 | return seq; | |
9cf7c104 PB |
6490 | } |
6491 | ||
c072481d | 6492 | static __cold void io_drain_req(struct io_kiocb *req) |
de0617e4 | 6493 | { |
a197f664 | 6494 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 6495 | struct io_defer_entry *de; |
f67676d1 | 6496 | int ret; |
e0eb71dc | 6497 | u32 seq = io_get_sequence(req); |
3c19966d | 6498 | |
9d858b21 | 6499 | /* Still need defer if there is pending req in defer list. */ |
5e371265 | 6500 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) { |
e0eb71dc | 6501 | queue: |
10c66904 | 6502 | ctx->drain_active = false; |
e0eb71dc PB |
6503 | io_req_task_queue(req); |
6504 | return; | |
10c66904 | 6505 | } |
9cf7c104 | 6506 | |
b7e298d2 | 6507 | ret = io_req_prep_async(req); |
e0eb71dc PB |
6508 | if (ret) { |
6509 | fail: | |
6510 | io_req_complete_failed(req, ret); | |
6511 | return; | |
6512 | } | |
cbdcb435 | 6513 | io_prep_async_link(req); |
27dc8338 | 6514 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
76cc33d7 | 6515 | if (!de) { |
1b48773f | 6516 | ret = -ENOMEM; |
e0eb71dc | 6517 | goto fail; |
76cc33d7 | 6518 | } |
2d28390a | 6519 | |
79ebeaee | 6520 | spin_lock(&ctx->completion_lock); |
9cf7c104 | 6521 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
79ebeaee | 6522 | spin_unlock(&ctx->completion_lock); |
27dc8338 | 6523 | kfree(de); |
e0eb71dc | 6524 | goto queue; |
de0617e4 JA |
6525 | } |
6526 | ||
915967f6 | 6527 | trace_io_uring_defer(ctx, req, req->user_data); |
27dc8338 | 6528 | de->req = req; |
9cf7c104 | 6529 | de->seq = seq; |
27dc8338 | 6530 | list_add_tail(&de->list, &ctx->defer_list); |
79ebeaee | 6531 | spin_unlock(&ctx->completion_lock); |
de0617e4 JA |
6532 | } |
6533 | ||
68fb8979 | 6534 | static void io_clean_op(struct io_kiocb *req) |
99bc4c38 | 6535 | { |
0e1b6fe3 | 6536 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
30d51dd4 PB |
6537 | kfree(req->kbuf); |
6538 | req->kbuf = NULL; | |
99bc4c38 PB |
6539 | } |
6540 | ||
0e1b6fe3 PB |
6541 | if (req->flags & REQ_F_NEED_CLEANUP) { |
6542 | switch (req->opcode) { | |
6543 | case IORING_OP_READV: | |
6544 | case IORING_OP_READ_FIXED: | |
6545 | case IORING_OP_READ: | |
6546 | case IORING_OP_WRITEV: | |
6547 | case IORING_OP_WRITE_FIXED: | |
e8c2bc1f JA |
6548 | case IORING_OP_WRITE: { |
6549 | struct io_async_rw *io = req->async_data; | |
1dacb4df PB |
6550 | |
6551 | kfree(io->free_iovec); | |
0e1b6fe3 | 6552 | break; |
e8c2bc1f | 6553 | } |
0e1b6fe3 | 6554 | case IORING_OP_RECVMSG: |
e8c2bc1f JA |
6555 | case IORING_OP_SENDMSG: { |
6556 | struct io_async_msghdr *io = req->async_data; | |
257e84a5 PB |
6557 | |
6558 | kfree(io->free_iov); | |
0e1b6fe3 | 6559 | break; |
e8c2bc1f | 6560 | } |
0e1b6fe3 PB |
6561 | case IORING_OP_SPLICE: |
6562 | case IORING_OP_TEE: | |
e1d767f0 PB |
6563 | if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED)) |
6564 | io_put_file(req->splice.file_in); | |
0e1b6fe3 | 6565 | break; |
f3cd4850 JA |
6566 | case IORING_OP_OPENAT: |
6567 | case IORING_OP_OPENAT2: | |
6568 | if (req->open.filename) | |
6569 | putname(req->open.filename); | |
6570 | break; | |
80a261fd JA |
6571 | case IORING_OP_RENAMEAT: |
6572 | putname(req->rename.oldpath); | |
6573 | putname(req->rename.newpath); | |
6574 | break; | |
14a1143b JA |
6575 | case IORING_OP_UNLINKAT: |
6576 | putname(req->unlink.filename); | |
6577 | break; | |
e34a02dc DK |
6578 | case IORING_OP_MKDIRAT: |
6579 | putname(req->mkdir.filename); | |
6580 | break; | |
7a8721f8 DK |
6581 | case IORING_OP_SYMLINKAT: |
6582 | putname(req->symlink.oldpath); | |
6583 | putname(req->symlink.newpath); | |
6584 | break; | |
cf30da90 DK |
6585 | case IORING_OP_LINKAT: |
6586 | putname(req->hardlink.oldpath); | |
6587 | putname(req->hardlink.newpath); | |
6588 | break; | |
0e1b6fe3 | 6589 | } |
99bc4c38 | 6590 | } |
75652a30 JA |
6591 | if ((req->flags & REQ_F_POLLED) && req->apoll) { |
6592 | kfree(req->apoll->double_poll); | |
6593 | kfree(req->apoll); | |
6594 | req->apoll = NULL; | |
6595 | } | |
3a0a6902 PB |
6596 | if (req->flags & REQ_F_INFLIGHT) { |
6597 | struct io_uring_task *tctx = req->task->io_uring; | |
6598 | ||
6599 | atomic_dec(&tctx->inflight_tracked); | |
3a0a6902 | 6600 | } |
c854357b | 6601 | if (req->flags & REQ_F_CREDS) |
b8e64b53 | 6602 | put_cred(req->creds); |
d886e185 PB |
6603 | if (req->flags & REQ_F_ASYNC_DATA) { |
6604 | kfree(req->async_data); | |
6605 | req->async_data = NULL; | |
6606 | } | |
c854357b | 6607 | req->flags &= ~IO_REQ_CLEAN_FLAGS; |
99bc4c38 PB |
6608 | } |
6609 | ||
889fca73 | 6610 | static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 6611 | { |
5730b27e | 6612 | const struct cred *creds = NULL; |
d625c6ee | 6613 | int ret; |
2b188cc1 | 6614 | |
6878b40e | 6615 | if (unlikely((req->flags & REQ_F_CREDS) && req->creds != current_cred())) |
c10d1f98 | 6616 | creds = override_creds(req->creds); |
5730b27e | 6617 | |
5bd2182d PM |
6618 | if (!io_op_defs[req->opcode].audit_skip) |
6619 | audit_uring_entry(req->opcode); | |
6620 | ||
d625c6ee | 6621 | switch (req->opcode) { |
2b188cc1 | 6622 | case IORING_OP_NOP: |
889fca73 | 6623 | ret = io_nop(req, issue_flags); |
2b188cc1 JA |
6624 | break; |
6625 | case IORING_OP_READV: | |
edafccee | 6626 | case IORING_OP_READ_FIXED: |
3a6820f2 | 6627 | case IORING_OP_READ: |
889fca73 | 6628 | ret = io_read(req, issue_flags); |
edafccee | 6629 | break; |
3529d8c2 | 6630 | case IORING_OP_WRITEV: |
edafccee | 6631 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 6632 | case IORING_OP_WRITE: |
889fca73 | 6633 | ret = io_write(req, issue_flags); |
2b188cc1 | 6634 | break; |
c992fe29 | 6635 | case IORING_OP_FSYNC: |
45d189c6 | 6636 | ret = io_fsync(req, issue_flags); |
c992fe29 | 6637 | break; |
221c5eb2 | 6638 | case IORING_OP_POLL_ADD: |
61e98203 | 6639 | ret = io_poll_add(req, issue_flags); |
221c5eb2 JA |
6640 | break; |
6641 | case IORING_OP_POLL_REMOVE: | |
c5de0036 | 6642 | ret = io_poll_update(req, issue_flags); |
221c5eb2 | 6643 | break; |
5d17b4a4 | 6644 | case IORING_OP_SYNC_FILE_RANGE: |
45d189c6 | 6645 | ret = io_sync_file_range(req, issue_flags); |
5d17b4a4 | 6646 | break; |
0fa03c62 | 6647 | case IORING_OP_SENDMSG: |
889fca73 | 6648 | ret = io_sendmsg(req, issue_flags); |
062d04d7 | 6649 | break; |
fddaface | 6650 | case IORING_OP_SEND: |
889fca73 | 6651 | ret = io_send(req, issue_flags); |
0fa03c62 | 6652 | break; |
aa1fa28f | 6653 | case IORING_OP_RECVMSG: |
889fca73 | 6654 | ret = io_recvmsg(req, issue_flags); |
062d04d7 | 6655 | break; |
fddaface | 6656 | case IORING_OP_RECV: |
889fca73 | 6657 | ret = io_recv(req, issue_flags); |
aa1fa28f | 6658 | break; |
5262f567 | 6659 | case IORING_OP_TIMEOUT: |
61e98203 | 6660 | ret = io_timeout(req, issue_flags); |
5262f567 | 6661 | break; |
11365043 | 6662 | case IORING_OP_TIMEOUT_REMOVE: |
61e98203 | 6663 | ret = io_timeout_remove(req, issue_flags); |
11365043 | 6664 | break; |
17f2fe35 | 6665 | case IORING_OP_ACCEPT: |
889fca73 | 6666 | ret = io_accept(req, issue_flags); |
17f2fe35 | 6667 | break; |
f8e85cf2 | 6668 | case IORING_OP_CONNECT: |
889fca73 | 6669 | ret = io_connect(req, issue_flags); |
f8e85cf2 | 6670 | break; |
62755e35 | 6671 | case IORING_OP_ASYNC_CANCEL: |
61e98203 | 6672 | ret = io_async_cancel(req, issue_flags); |
62755e35 | 6673 | break; |
d63d1b5e | 6674 | case IORING_OP_FALLOCATE: |
45d189c6 | 6675 | ret = io_fallocate(req, issue_flags); |
d63d1b5e | 6676 | break; |
15b71abe | 6677 | case IORING_OP_OPENAT: |
45d189c6 | 6678 | ret = io_openat(req, issue_flags); |
15b71abe | 6679 | break; |
b5dba59e | 6680 | case IORING_OP_CLOSE: |
889fca73 | 6681 | ret = io_close(req, issue_flags); |
b5dba59e | 6682 | break; |
05f3fb3c | 6683 | case IORING_OP_FILES_UPDATE: |
889fca73 | 6684 | ret = io_files_update(req, issue_flags); |
05f3fb3c | 6685 | break; |
eddc7ef5 | 6686 | case IORING_OP_STATX: |
45d189c6 | 6687 | ret = io_statx(req, issue_flags); |
eddc7ef5 | 6688 | break; |
4840e418 | 6689 | case IORING_OP_FADVISE: |
45d189c6 | 6690 | ret = io_fadvise(req, issue_flags); |
4840e418 | 6691 | break; |
c1ca757b | 6692 | case IORING_OP_MADVISE: |
45d189c6 | 6693 | ret = io_madvise(req, issue_flags); |
c1ca757b | 6694 | break; |
cebdb986 | 6695 | case IORING_OP_OPENAT2: |
45d189c6 | 6696 | ret = io_openat2(req, issue_flags); |
cebdb986 | 6697 | break; |
3e4827b0 | 6698 | case IORING_OP_EPOLL_CTL: |
889fca73 | 6699 | ret = io_epoll_ctl(req, issue_flags); |
3e4827b0 | 6700 | break; |
7d67af2c | 6701 | case IORING_OP_SPLICE: |
45d189c6 | 6702 | ret = io_splice(req, issue_flags); |
7d67af2c | 6703 | break; |
ddf0322d | 6704 | case IORING_OP_PROVIDE_BUFFERS: |
889fca73 | 6705 | ret = io_provide_buffers(req, issue_flags); |
ddf0322d | 6706 | break; |
067524e9 | 6707 | case IORING_OP_REMOVE_BUFFERS: |
889fca73 | 6708 | ret = io_remove_buffers(req, issue_flags); |
3e4827b0 | 6709 | break; |
f2a8d5c7 | 6710 | case IORING_OP_TEE: |
45d189c6 | 6711 | ret = io_tee(req, issue_flags); |
f2a8d5c7 | 6712 | break; |
36f4fa68 | 6713 | case IORING_OP_SHUTDOWN: |
45d189c6 | 6714 | ret = io_shutdown(req, issue_flags); |
36f4fa68 | 6715 | break; |
80a261fd | 6716 | case IORING_OP_RENAMEAT: |
45d189c6 | 6717 | ret = io_renameat(req, issue_flags); |
80a261fd | 6718 | break; |
14a1143b | 6719 | case IORING_OP_UNLINKAT: |
45d189c6 | 6720 | ret = io_unlinkat(req, issue_flags); |
14a1143b | 6721 | break; |
e34a02dc DK |
6722 | case IORING_OP_MKDIRAT: |
6723 | ret = io_mkdirat(req, issue_flags); | |
6724 | break; | |
7a8721f8 DK |
6725 | case IORING_OP_SYMLINKAT: |
6726 | ret = io_symlinkat(req, issue_flags); | |
6727 | break; | |
cf30da90 DK |
6728 | case IORING_OP_LINKAT: |
6729 | ret = io_linkat(req, issue_flags); | |
6730 | break; | |
2b188cc1 JA |
6731 | default: |
6732 | ret = -EINVAL; | |
6733 | break; | |
6734 | } | |
6735 | ||
5bd2182d PM |
6736 | if (!io_op_defs[req->opcode].audit_skip) |
6737 | audit_uring_exit(!ret, ret); | |
6738 | ||
5730b27e JA |
6739 | if (creds) |
6740 | revert_creds(creds); | |
def596e9 JA |
6741 | if (ret) |
6742 | return ret; | |
b532576e | 6743 | /* If the op doesn't have a file, we're not polling for it */ |
9983028e | 6744 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && req->file) |
9882131c | 6745 | io_iopoll_req_issued(req, issue_flags); |
def596e9 JA |
6746 | |
6747 | return 0; | |
2b188cc1 JA |
6748 | } |
6749 | ||
ebc11b6c PB |
6750 | static struct io_wq_work *io_wq_free_work(struct io_wq_work *work) |
6751 | { | |
6752 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6753 | ||
6754 | req = io_put_req_find_next(req); | |
6755 | return req ? &req->work : NULL; | |
6756 | } | |
6757 | ||
5280f7e5 | 6758 | static void io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
6759 | { |
6760 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
d01905db PB |
6761 | unsigned int issue_flags = IO_URING_F_UNLOCKED; |
6762 | bool needs_poll = false; | |
6df1db6b | 6763 | struct io_kiocb *timeout; |
561fb04a | 6764 | int ret = 0; |
2b188cc1 | 6765 | |
48dcd38d PB |
6766 | /* one will be dropped by ->io_free_work() after returning to io-wq */ |
6767 | if (!(req->flags & REQ_F_REFCOUNT)) | |
6768 | __io_req_set_refcount(req, 2); | |
6769 | else | |
6770 | req_ref_get(req); | |
5d5901a3 | 6771 | |
6df1db6b PB |
6772 | timeout = io_prep_linked_timeout(req); |
6773 | if (timeout) | |
6774 | io_queue_linked_timeout(timeout); | |
d4c81f38 | 6775 | |
dadebc35 | 6776 | /* either cancelled or io-wq is dying, so don't touch tctx->iowq */ |
d01905db PB |
6777 | if (work->flags & IO_WQ_WORK_CANCEL) { |
6778 | io_req_task_queue_fail(req, -ECANCELED); | |
6779 | return; | |
6780 | } | |
31b51510 | 6781 | |
d01905db | 6782 | if (req->flags & REQ_F_FORCE_ASYNC) { |
afb7f56f PB |
6783 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
6784 | bool opcode_poll = def->pollin || def->pollout; | |
6785 | ||
6786 | if (opcode_poll && file_can_poll(req->file)) { | |
6787 | needs_poll = true; | |
d01905db | 6788 | issue_flags |= IO_URING_F_NONBLOCK; |
afb7f56f | 6789 | } |
561fb04a | 6790 | } |
31b51510 | 6791 | |
d01905db PB |
6792 | do { |
6793 | ret = io_issue_sqe(req, issue_flags); | |
6794 | if (ret != -EAGAIN) | |
6795 | break; | |
6796 | /* | |
6797 | * We can get EAGAIN for iopolled IO even though we're | |
6798 | * forcing a sync submission from here, since we can't | |
6799 | * wait for request slots on the block side. | |
6800 | */ | |
6801 | if (!needs_poll) { | |
6802 | cond_resched(); | |
6803 | continue; | |
90fa0288 HX |
6804 | } |
6805 | ||
d01905db PB |
6806 | if (io_arm_poll_handler(req) == IO_APOLL_OK) |
6807 | return; | |
6808 | /* aborted or ready, in either case retry blocking */ | |
6809 | needs_poll = false; | |
6810 | issue_flags &= ~IO_URING_F_NONBLOCK; | |
6811 | } while (1); | |
31b51510 | 6812 | |
a3df7698 | 6813 | /* avoid locking problems by failing it from a clean context */ |
5d5901a3 | 6814 | if (ret) |
a3df7698 | 6815 | io_req_task_queue_fail(req, ret); |
2b188cc1 JA |
6816 | } |
6817 | ||
aeca241b | 6818 | static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table, |
042b0d85 | 6819 | unsigned i) |
65e19f54 | 6820 | { |
042b0d85 | 6821 | return &table->files[i]; |
dafecf19 PB |
6822 | } |
6823 | ||
65e19f54 JA |
6824 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
6825 | int index) | |
6826 | { | |
aeca241b | 6827 | struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index); |
65e19f54 | 6828 | |
a04b0ac0 | 6829 | return (struct file *) (slot->file_ptr & FFS_MASK); |
65e19f54 JA |
6830 | } |
6831 | ||
a04b0ac0 | 6832 | static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file) |
9a321c98 PB |
6833 | { |
6834 | unsigned long file_ptr = (unsigned long) file; | |
6835 | ||
88459b50 | 6836 | file_ptr |= io_file_get_flags(file); |
a04b0ac0 | 6837 | file_slot->file_ptr = file_ptr; |
65e19f54 JA |
6838 | } |
6839 | ||
ac177053 PB |
6840 | static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx, |
6841 | struct io_kiocb *req, int fd) | |
09bb8394 | 6842 | { |
8da11c19 | 6843 | struct file *file; |
ac177053 | 6844 | unsigned long file_ptr; |
09bb8394 | 6845 | |
ac177053 PB |
6846 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
6847 | return NULL; | |
6848 | fd = array_index_nospec(fd, ctx->nr_user_files); | |
6849 | file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr; | |
6850 | file = (struct file *) (file_ptr & FFS_MASK); | |
6851 | file_ptr &= ~FFS_MASK; | |
6852 | /* mask in overlapping REQ_F and FFS bits */ | |
35645ac3 | 6853 | req->flags |= (file_ptr << REQ_F_SUPPORT_NOWAIT_BIT); |
a46be971 | 6854 | io_req_set_rsrc_node(req, ctx); |
ac177053 PB |
6855 | return file; |
6856 | } | |
d44f554e | 6857 | |
ac177053 | 6858 | static struct file *io_file_get_normal(struct io_ring_ctx *ctx, |
ac177053 PB |
6859 | struct io_kiocb *req, int fd) |
6860 | { | |
62906e89 | 6861 | struct file *file = fget(fd); |
ac177053 PB |
6862 | |
6863 | trace_io_uring_file_get(ctx, fd); | |
09bb8394 | 6864 | |
ac177053 PB |
6865 | /* we don't allow fixed io_uring files */ |
6866 | if (file && unlikely(file->f_op == &io_uring_fops)) | |
6867 | io_req_track_inflight(req); | |
8371adf5 | 6868 | return file; |
09bb8394 JA |
6869 | } |
6870 | ||
ac177053 | 6871 | static inline struct file *io_file_get(struct io_ring_ctx *ctx, |
ac177053 PB |
6872 | struct io_kiocb *req, int fd, bool fixed) |
6873 | { | |
6874 | if (fixed) | |
6875 | return io_file_get_fixed(ctx, req, fd); | |
6876 | else | |
62906e89 | 6877 | return io_file_get_normal(ctx, req, fd); |
ac177053 PB |
6878 | } |
6879 | ||
f237c30a | 6880 | static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked) |
89b263f6 JA |
6881 | { |
6882 | struct io_kiocb *prev = req->timeout.prev; | |
8cb01fac | 6883 | int ret; |
89b263f6 JA |
6884 | |
6885 | if (prev) { | |
8cb01fac | 6886 | ret = io_try_cancel_userdata(req, prev->user_data); |
505657bc | 6887 | io_req_complete_post(req, ret ?: -ETIME, 0); |
89b263f6 | 6888 | io_put_req(prev); |
89b263f6 JA |
6889 | } else { |
6890 | io_req_complete_post(req, -ETIME, 0); | |
6891 | } | |
6892 | } | |
6893 | ||
2665abfd | 6894 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 6895 | { |
ad8a48ac JA |
6896 | struct io_timeout_data *data = container_of(timer, |
6897 | struct io_timeout_data, timer); | |
90cd7e42 | 6898 | struct io_kiocb *prev, *req = data->req; |
2665abfd | 6899 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 6900 | unsigned long flags; |
2665abfd | 6901 | |
89b263f6 | 6902 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
90cd7e42 PB |
6903 | prev = req->timeout.head; |
6904 | req->timeout.head = NULL; | |
2665abfd JA |
6905 | |
6906 | /* | |
6907 | * We don't expect the list to be empty, that will only happen if we | |
6908 | * race with the completion of the linked work. | |
6909 | */ | |
447c19f3 | 6910 | if (prev) { |
f2f87370 | 6911 | io_remove_next_linked(prev); |
447c19f3 PB |
6912 | if (!req_ref_inc_not_zero(prev)) |
6913 | prev = NULL; | |
6914 | } | |
ef9dd637 | 6915 | list_del(&req->timeout.list); |
89b263f6 JA |
6916 | req->timeout.prev = prev; |
6917 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); | |
2665abfd | 6918 | |
89b263f6 JA |
6919 | req->io_task_work.func = io_req_task_link_timeout; |
6920 | io_req_task_work_add(req); | |
2665abfd JA |
6921 | return HRTIMER_NORESTART; |
6922 | } | |
6923 | ||
de968c18 | 6924 | static void io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 6925 | { |
de968c18 PB |
6926 | struct io_ring_ctx *ctx = req->ctx; |
6927 | ||
89b263f6 | 6928 | spin_lock_irq(&ctx->timeout_lock); |
76a46e06 | 6929 | /* |
f2f87370 PB |
6930 | * If the back reference is NULL, then our linked request finished |
6931 | * before we got a chance to setup the timer | |
76a46e06 | 6932 | */ |
90cd7e42 | 6933 | if (req->timeout.head) { |
e8c2bc1f | 6934 | struct io_timeout_data *data = req->async_data; |
94ae5e77 | 6935 | |
ad8a48ac JA |
6936 | data->timer.function = io_link_timeout_fn; |
6937 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
6938 | data->mode); | |
ef9dd637 | 6939 | list_add_tail(&req->timeout.list, &ctx->ltimeout_list); |
2665abfd | 6940 | } |
89b263f6 | 6941 | spin_unlock_irq(&ctx->timeout_lock); |
2665abfd | 6942 | /* drop submission reference */ |
76a46e06 JA |
6943 | io_put_req(req); |
6944 | } | |
2665abfd | 6945 | |
d475a9a6 PB |
6946 | static void io_queue_sqe_arm_apoll(struct io_kiocb *req) |
6947 | __must_hold(&req->ctx->uring_lock) | |
6948 | { | |
6949 | struct io_kiocb *linked_timeout = io_prep_linked_timeout(req); | |
6950 | ||
6951 | switch (io_arm_poll_handler(req)) { | |
6952 | case IO_APOLL_READY: | |
d475a9a6 PB |
6953 | io_req_task_queue(req); |
6954 | break; | |
6955 | case IO_APOLL_ABORTED: | |
6956 | /* | |
6957 | * Queued up for async execution, worker will release | |
6958 | * submit reference when the iocb is actually submitted. | |
6959 | */ | |
6960 | io_queue_async_work(req, NULL); | |
6961 | break; | |
6962 | } | |
6963 | ||
6964 | if (linked_timeout) | |
6965 | io_queue_linked_timeout(linked_timeout); | |
6966 | } | |
6967 | ||
6968 | static inline void __io_queue_sqe(struct io_kiocb *req) | |
282cdc86 | 6969 | __must_hold(&req->ctx->uring_lock) |
2b188cc1 | 6970 | { |
906c6caa | 6971 | struct io_kiocb *linked_timeout; |
e0c5c576 | 6972 | int ret; |
2b188cc1 | 6973 | |
c5eef2b9 | 6974 | ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER); |
193155c8 | 6975 | |
fff4e40e PB |
6976 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
6977 | io_req_add_compl_list(req); | |
d9f9d284 | 6978 | return; |
fff4e40e | 6979 | } |
491381ce JA |
6980 | /* |
6981 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
6982 | * doesn't support non-blocking read/write attempts | |
6983 | */ | |
1840038e | 6984 | if (likely(!ret)) { |
906c6caa PB |
6985 | linked_timeout = io_prep_linked_timeout(req); |
6986 | if (linked_timeout) | |
6987 | io_queue_linked_timeout(linked_timeout); | |
1840038e | 6988 | } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
d475a9a6 | 6989 | io_queue_sqe_arm_apoll(req); |
0d63c148 | 6990 | } else { |
f41db273 | 6991 | io_req_complete_failed(req, ret); |
9e645e11 | 6992 | } |
2b188cc1 JA |
6993 | } |
6994 | ||
4652fe3f | 6995 | static void io_queue_sqe_fallback(struct io_kiocb *req) |
282cdc86 | 6996 | __must_hold(&req->ctx->uring_lock) |
4fe2c963 | 6997 | { |
4652fe3f | 6998 | if (req->flags & REQ_F_FAIL) { |
c6d3d9cb | 6999 | io_req_complete_fail_submit(req); |
e0eb71dc PB |
7000 | } else if (unlikely(req->ctx->drain_active)) { |
7001 | io_drain_req(req); | |
76cc33d7 PB |
7002 | } else { |
7003 | int ret = io_req_prep_async(req); | |
7004 | ||
7005 | if (unlikely(ret)) | |
7006 | io_req_complete_failed(req, ret); | |
7007 | else | |
f237c30a | 7008 | io_queue_async_work(req, NULL); |
ce35a47a | 7009 | } |
4fe2c963 JL |
7010 | } |
7011 | ||
4652fe3f PB |
7012 | static inline void io_queue_sqe(struct io_kiocb *req) |
7013 | __must_hold(&req->ctx->uring_lock) | |
7014 | { | |
7015 | if (likely(!(req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL)))) | |
7016 | __io_queue_sqe(req); | |
7017 | else | |
7018 | io_queue_sqe_fallback(req); | |
7019 | } | |
7020 | ||
b16fed66 PB |
7021 | /* |
7022 | * Check SQE restrictions (opcode and flags). | |
7023 | * | |
7024 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
7025 | */ | |
7026 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
7027 | struct io_kiocb *req, | |
7028 | unsigned int sqe_flags) | |
4fe2c963 | 7029 | { |
b16fed66 PB |
7030 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) |
7031 | return false; | |
7032 | ||
7033 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
7034 | ctx->restrictions.sqe_flags_required) | |
7035 | return false; | |
7036 | ||
7037 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
7038 | ctx->restrictions.sqe_flags_required)) | |
7039 | return false; | |
7040 | ||
7041 | return true; | |
4fe2c963 JL |
7042 | } |
7043 | ||
22b2ca31 PB |
7044 | static void io_init_req_drain(struct io_kiocb *req) |
7045 | { | |
7046 | struct io_ring_ctx *ctx = req->ctx; | |
7047 | struct io_kiocb *head = ctx->submit_state.link.head; | |
7048 | ||
7049 | ctx->drain_active = true; | |
7050 | if (head) { | |
7051 | /* | |
7052 | * If we need to drain a request in the middle of a link, drain | |
7053 | * the head request and the next request/link after the current | |
7054 | * link. Considering sequential execution of links, | |
7055 | * IOSQE_IO_DRAIN will be maintained for every request of our | |
7056 | * link. | |
7057 | */ | |
7058 | head->flags |= IOSQE_IO_DRAIN | REQ_F_FORCE_ASYNC; | |
7059 | ctx->drain_next = true; | |
7060 | } | |
7061 | } | |
7062 | ||
b16fed66 PB |
7063 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
7064 | const struct io_uring_sqe *sqe) | |
282cdc86 | 7065 | __must_hold(&ctx->uring_lock) |
b16fed66 | 7066 | { |
b16fed66 | 7067 | unsigned int sqe_flags; |
fc0ae024 | 7068 | int personality; |
4a04d1d1 | 7069 | u8 opcode; |
b16fed66 | 7070 | |
864ea921 | 7071 | /* req is partially pre-initialised, see io_preinit_req() */ |
4a04d1d1 | 7072 | req->opcode = opcode = READ_ONCE(sqe->opcode); |
b16fed66 PB |
7073 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
7074 | req->flags = sqe_flags = READ_ONCE(sqe->flags); | |
7075 | req->user_data = READ_ONCE(sqe->user_data); | |
b16fed66 | 7076 | req->file = NULL; |
b16fed66 | 7077 | req->fixed_rsrc_refs = NULL; |
b16fed66 | 7078 | req->task = current; |
b16fed66 | 7079 | |
4a04d1d1 PB |
7080 | if (unlikely(opcode >= IORING_OP_LAST)) { |
7081 | req->opcode = 0; | |
b16fed66 | 7082 | return -EINVAL; |
4a04d1d1 | 7083 | } |
68fe256a PB |
7084 | if (unlikely(sqe_flags & ~SQE_COMMON_FLAGS)) { |
7085 | /* enforce forwards compatibility on users */ | |
7086 | if (sqe_flags & ~SQE_VALID_FLAGS) | |
7087 | return -EINVAL; | |
7088 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && | |
4a04d1d1 | 7089 | !io_op_defs[opcode].buffer_select) |
68fe256a | 7090 | return -EOPNOTSUPP; |
22b2ca31 PB |
7091 | if (sqe_flags & IOSQE_IO_DRAIN) |
7092 | io_init_req_drain(req); | |
2a56a9bd PB |
7093 | } |
7094 | if (unlikely(ctx->restricted || ctx->drain_active || ctx->drain_next)) { | |
7095 | if (ctx->restricted && !io_check_restriction(ctx, req, sqe_flags)) | |
7096 | return -EACCES; | |
7097 | /* knock it to the slow queue path, will be drained there */ | |
7098 | if (ctx->drain_active) | |
7099 | req->flags |= REQ_F_FORCE_ASYNC; | |
7100 | /* if there is no link, we're at "next" request and need to drain */ | |
7101 | if (unlikely(ctx->drain_next) && !ctx->submit_state.link.head) { | |
7102 | ctx->drain_next = false; | |
7103 | ctx->drain_active = true; | |
22b2ca31 | 7104 | req->flags |= IOSQE_IO_DRAIN | REQ_F_FORCE_ASYNC; |
2a56a9bd | 7105 | } |
68fe256a | 7106 | } |
b16fed66 | 7107 | |
4a04d1d1 | 7108 | if (io_op_defs[opcode].needs_file) { |
6d63416d PB |
7109 | struct io_submit_state *state = &ctx->submit_state; |
7110 | ||
7111 | /* | |
7112 | * Plug now if we have more than 2 IO left after this, and the | |
7113 | * target is potentially a read/write to block based storage. | |
7114 | */ | |
4a04d1d1 | 7115 | if (state->need_plug && io_op_defs[opcode].plug) { |
6d63416d PB |
7116 | state->plug_started = true; |
7117 | state->need_plug = false; | |
5ca7a8b3 | 7118 | blk_start_plug_nr_ios(&state->plug, state->submit_nr); |
6d63416d PB |
7119 | } |
7120 | ||
62906e89 | 7121 | req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd), |
ac177053 | 7122 | (sqe_flags & IOSQE_FIXED_FILE)); |
b16fed66 | 7123 | if (unlikely(!req->file)) |
fc0ae024 | 7124 | return -EBADF; |
b16fed66 | 7125 | } |
863e0560 | 7126 | |
003e8dcc JA |
7127 | personality = READ_ONCE(sqe->personality); |
7128 | if (personality) { | |
cdab10bf LT |
7129 | int ret; |
7130 | ||
c10d1f98 PB |
7131 | req->creds = xa_load(&ctx->personalities, personality); |
7132 | if (!req->creds) | |
003e8dcc | 7133 | return -EINVAL; |
c10d1f98 | 7134 | get_cred(req->creds); |
cdc1404a PM |
7135 | ret = security_uring_override_creds(req->creds); |
7136 | if (ret) { | |
7137 | put_cred(req->creds); | |
7138 | return ret; | |
7139 | } | |
b8e64b53 | 7140 | req->flags |= REQ_F_CREDS; |
003e8dcc | 7141 | } |
b16fed66 | 7142 | |
fc0ae024 | 7143 | return io_req_prep(req, sqe); |
b16fed66 PB |
7144 | } |
7145 | ||
a6b8cadc | 7146 | static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
a1ab7b35 | 7147 | const struct io_uring_sqe *sqe) |
282cdc86 | 7148 | __must_hold(&ctx->uring_lock) |
9e645e11 | 7149 | { |
a1ab7b35 | 7150 | struct io_submit_link *link = &ctx->submit_state.link; |
ef4ff581 | 7151 | int ret; |
9e645e11 | 7152 | |
a6b8cadc PB |
7153 | ret = io_init_req(ctx, req, sqe); |
7154 | if (unlikely(ret)) { | |
a87acfde JA |
7155 | trace_io_uring_req_failed(sqe, ret); |
7156 | ||
a8295b98 | 7157 | /* fail even hard links since we don't submit */ |
de59bc10 | 7158 | if (link->head) { |
a8295b98 HX |
7159 | /* |
7160 | * we can judge a link req is failed or cancelled by if | |
7161 | * REQ_F_FAIL is set, but the head is an exception since | |
7162 | * it may be set REQ_F_FAIL because of other req's failure | |
7163 | * so let's leverage req->result to distinguish if a head | |
7164 | * is set REQ_F_FAIL because of its failure or other req's | |
7165 | * failure so that we can set the correct ret code for it. | |
7166 | * init result here to avoid affecting the normal path. | |
7167 | */ | |
7168 | if (!(link->head->flags & REQ_F_FAIL)) | |
7169 | req_fail_link_node(link->head, -ECANCELED); | |
7170 | } else if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { | |
7171 | /* | |
7172 | * the current req is a normal req, we should return | |
7173 | * error and thus break the submittion loop. | |
7174 | */ | |
7175 | io_req_complete_failed(req, ret); | |
7176 | return ret; | |
de59bc10 | 7177 | } |
a8295b98 | 7178 | req_fail_link_node(req, ret); |
a6b8cadc | 7179 | } |
441b8a78 | 7180 | |
be7053b7 | 7181 | /* don't need @sqe from now on */ |
236daeae OL |
7182 | trace_io_uring_submit_sqe(ctx, req, req->opcode, req->user_data, |
7183 | req->flags, true, | |
7184 | ctx->flags & IORING_SETUP_SQPOLL); | |
a6b8cadc | 7185 | |
9e645e11 JA |
7186 | /* |
7187 | * If we already have a head request, queue this one for async | |
7188 | * submittal once the head completes. If we don't have a head but | |
7189 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
7190 | * submitted sync once the chain is complete. If none of those | |
7191 | * conditions are true (normal request), then just queue it. | |
7192 | */ | |
863e0560 PB |
7193 | if (link->head) { |
7194 | struct io_kiocb *head = link->head; | |
4e88d6e7 | 7195 | |
a8295b98 HX |
7196 | if (!(req->flags & REQ_F_FAIL)) { |
7197 | ret = io_req_prep_async(req); | |
7198 | if (unlikely(ret)) { | |
7199 | req_fail_link_node(req, ret); | |
7200 | if (!(head->flags & REQ_F_FAIL)) | |
7201 | req_fail_link_node(head, -ECANCELED); | |
7202 | } | |
7203 | } | |
9d76377f | 7204 | trace_io_uring_link(ctx, req, head); |
f2f87370 | 7205 | link->last->link = req; |
863e0560 | 7206 | link->last = req; |
32fe525b | 7207 | |
f15a3431 PB |
7208 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) |
7209 | return 0; | |
32fe525b | 7210 | /* last request of a link, enqueue the link */ |
f15a3431 PB |
7211 | link->head = NULL; |
7212 | req = head; | |
7213 | } else if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { | |
7214 | link->head = req; | |
7215 | link->last = req; | |
7216 | return 0; | |
9e645e11 | 7217 | } |
2e6e1fde | 7218 | |
f15a3431 | 7219 | io_queue_sqe(req); |
1d4240cc | 7220 | return 0; |
9e645e11 JA |
7221 | } |
7222 | ||
9a56a232 JA |
7223 | /* |
7224 | * Batched submission is done, ensure local IO is flushed out. | |
7225 | */ | |
553deffd | 7226 | static void io_submit_state_end(struct io_ring_ctx *ctx) |
9a56a232 | 7227 | { |
553deffd PB |
7228 | struct io_submit_state *state = &ctx->submit_state; |
7229 | ||
a1ab7b35 | 7230 | if (state->link.head) |
de59bc10 | 7231 | io_queue_sqe(state->link.head); |
553deffd | 7232 | /* flush only after queuing links as they can generate completions */ |
c450178d | 7233 | io_submit_flush_completions(ctx); |
27926b68 JA |
7234 | if (state->plug_started) |
7235 | blk_finish_plug(&state->plug); | |
9a56a232 JA |
7236 | } |
7237 | ||
7238 | /* | |
7239 | * Start submission side cache. | |
7240 | */ | |
7241 | static void io_submit_state_start(struct io_submit_state *state, | |
ba88ff11 | 7242 | unsigned int max_ios) |
9a56a232 | 7243 | { |
27926b68 | 7244 | state->plug_started = false; |
4b628aeb | 7245 | state->need_plug = max_ios > 2; |
5ca7a8b3 | 7246 | state->submit_nr = max_ios; |
a1ab7b35 PB |
7247 | /* set only head, no need to init link_last in advance */ |
7248 | state->link.head = NULL; | |
9a56a232 JA |
7249 | } |
7250 | ||
2b188cc1 JA |
7251 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
7252 | { | |
75b28aff | 7253 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 7254 | |
caf582c6 PB |
7255 | /* |
7256 | * Ensure any loads from the SQEs are done at this point, | |
7257 | * since once we write the new head, the application could | |
7258 | * write new data to them. | |
7259 | */ | |
7260 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
7261 | } |
7262 | ||
2b188cc1 | 7263 | /* |
dd9ae8a0 | 7264 | * Fetch an sqe, if one is available. Note this returns a pointer to memory |
2b188cc1 JA |
7265 | * that is mapped by userspace. This means that care needs to be taken to |
7266 | * ensure that reads are stable, as we cannot rely on userspace always | |
7267 | * being a good citizen. If members of the sqe are validated and then later | |
7268 | * used, it's important that those reads are done through READ_ONCE() to | |
7269 | * prevent a re-load down the line. | |
7270 | */ | |
709b302f | 7271 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 7272 | { |
ea5ab3b5 | 7273 | unsigned head, mask = ctx->sq_entries - 1; |
17d3aeb3 | 7274 | unsigned sq_idx = ctx->cached_sq_head++ & mask; |
2b188cc1 JA |
7275 | |
7276 | /* | |
7277 | * The cached sq head (or cq tail) serves two purposes: | |
7278 | * | |
7279 | * 1) allows us to batch the cost of updating the user visible | |
7280 | * head updates. | |
7281 | * 2) allows the kernel side to track the head on its own, even | |
7282 | * though the application is the one updating it. | |
7283 | */ | |
17d3aeb3 | 7284 | head = READ_ONCE(ctx->sq_array[sq_idx]); |
709b302f PB |
7285 | if (likely(head < ctx->sq_entries)) |
7286 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
7287 | |
7288 | /* drop invalid entries */ | |
15641e42 PB |
7289 | ctx->cq_extra--; |
7290 | WRITE_ONCE(ctx->rings->sq_dropped, | |
7291 | READ_ONCE(ctx->rings->sq_dropped) + 1); | |
709b302f PB |
7292 | return NULL; |
7293 | } | |
7294 | ||
0f212204 | 7295 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
282cdc86 | 7296 | __must_hold(&ctx->uring_lock) |
6c271ce2 | 7297 | { |
69629809 | 7298 | unsigned int entries = io_sqring_entries(ctx); |
46c4e16a | 7299 | int submitted = 0; |
6c271ce2 | 7300 | |
51d48dab | 7301 | if (unlikely(!entries)) |
69629809 | 7302 | return 0; |
ee7d46d9 | 7303 | /* make sure SQ entry isn't read before tail */ |
69629809 | 7304 | nr = min3(nr, ctx->sq_entries, entries); |
9a10867a | 7305 | io_get_task_refs(nr); |
6c271ce2 | 7306 | |
ba88ff11 | 7307 | io_submit_state_start(&ctx->submit_state, nr); |
69629809 | 7308 | do { |
3529d8c2 | 7309 | const struct io_uring_sqe *sqe; |
196be95c | 7310 | struct io_kiocb *req; |
fb5ccc98 | 7311 | |
a33ae9ce | 7312 | if (unlikely(!io_alloc_req_refill(ctx))) { |
196be95c PB |
7313 | if (!submitted) |
7314 | submitted = -EAGAIN; | |
fb5ccc98 | 7315 | break; |
196be95c | 7316 | } |
a33ae9ce | 7317 | req = io_alloc_req(ctx); |
4fccfcbb PB |
7318 | sqe = io_get_sqe(ctx); |
7319 | if (unlikely(!sqe)) { | |
c2b6c6bc | 7320 | wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list); |
4fccfcbb PB |
7321 | break; |
7322 | } | |
d3656344 JA |
7323 | /* will complete beyond this point, count as submitted */ |
7324 | submitted++; | |
a1ab7b35 | 7325 | if (io_submit_sqe(ctx, req, sqe)) |
196be95c | 7326 | break; |
69629809 | 7327 | } while (submitted < nr); |
6c271ce2 | 7328 | |
9466f437 PB |
7329 | if (unlikely(submitted != nr)) { |
7330 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; | |
d8a6df10 | 7331 | int unused = nr - ref_used; |
9466f437 | 7332 | |
09899b19 | 7333 | current->io_uring->cached_refs += unused; |
9466f437 | 7334 | } |
6c271ce2 | 7335 | |
553deffd | 7336 | io_submit_state_end(ctx); |
ae9428ca PB |
7337 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
7338 | io_commit_sqring(ctx); | |
7339 | ||
6c271ce2 JA |
7340 | return submitted; |
7341 | } | |
7342 | ||
e4b6d902 PB |
7343 | static inline bool io_sqd_events_pending(struct io_sq_data *sqd) |
7344 | { | |
7345 | return READ_ONCE(sqd->state); | |
7346 | } | |
7347 | ||
23b3628e XW |
7348 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
7349 | { | |
7350 | /* Tell userspace we may need a wakeup call */ | |
79ebeaee | 7351 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
7352 | WRITE_ONCE(ctx->rings->sq_flags, |
7353 | ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 7354 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
7355 | } |
7356 | ||
7357 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) | |
7358 | { | |
79ebeaee | 7359 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
7360 | WRITE_ONCE(ctx->rings->sq_flags, |
7361 | ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 7362 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
7363 | } |
7364 | ||
08369246 | 7365 | static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) |
6c271ce2 | 7366 | { |
c8d1ba58 | 7367 | unsigned int to_submit; |
bdcd3eab | 7368 | int ret = 0; |
6c271ce2 | 7369 | |
c8d1ba58 | 7370 | to_submit = io_sqring_entries(ctx); |
e95eee2d | 7371 | /* if we're handling multiple rings, cap submit size for fairness */ |
4ce8ad95 OL |
7372 | if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE) |
7373 | to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE; | |
e95eee2d | 7374 | |
5eef4e87 | 7375 | if (!wq_list_empty(&ctx->iopoll_list) || to_submit) { |
948e1947 PB |
7376 | const struct cred *creds = NULL; |
7377 | ||
7378 | if (ctx->sq_creds != current_cred()) | |
7379 | creds = override_creds(ctx->sq_creds); | |
a4c0b3de | 7380 | |
c8d1ba58 | 7381 | mutex_lock(&ctx->uring_lock); |
5eef4e87 | 7382 | if (!wq_list_empty(&ctx->iopoll_list)) |
5ba3c874 | 7383 | io_do_iopoll(ctx, true); |
906a3c6f | 7384 | |
3b763ba1 PB |
7385 | /* |
7386 | * Don't submit if refs are dying, good for io_uring_register(), | |
7387 | * but also it is relied upon by io_ring_exit_work() | |
7388 | */ | |
0298ef96 PB |
7389 | if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) && |
7390 | !(ctx->flags & IORING_SETUP_R_DISABLED)) | |
08369246 | 7391 | ret = io_submit_sqes(ctx, to_submit); |
c8d1ba58 | 7392 | mutex_unlock(&ctx->uring_lock); |
6c271ce2 | 7393 | |
acfb381d PB |
7394 | if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait)) |
7395 | wake_up(&ctx->sqo_sq_wait); | |
948e1947 PB |
7396 | if (creds) |
7397 | revert_creds(creds); | |
acfb381d | 7398 | } |
6c271ce2 | 7399 | |
08369246 XW |
7400 | return ret; |
7401 | } | |
6c271ce2 | 7402 | |
c072481d | 7403 | static __cold void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
08369246 XW |
7404 | { |
7405 | struct io_ring_ctx *ctx; | |
7406 | unsigned sq_thread_idle = 0; | |
6c271ce2 | 7407 | |
c9dca27d PB |
7408 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7409 | sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle); | |
08369246 | 7410 | sqd->sq_thread_idle = sq_thread_idle; |
c8d1ba58 | 7411 | } |
6c271ce2 | 7412 | |
e4b6d902 PB |
7413 | static bool io_sqd_handle_event(struct io_sq_data *sqd) |
7414 | { | |
7415 | bool did_sig = false; | |
7416 | struct ksignal ksig; | |
7417 | ||
7418 | if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) || | |
7419 | signal_pending(current)) { | |
7420 | mutex_unlock(&sqd->lock); | |
7421 | if (signal_pending(current)) | |
7422 | did_sig = get_signal(&ksig); | |
7423 | cond_resched(); | |
7424 | mutex_lock(&sqd->lock); | |
7425 | } | |
e4b6d902 PB |
7426 | return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
7427 | } | |
7428 | ||
c8d1ba58 JA |
7429 | static int io_sq_thread(void *data) |
7430 | { | |
69fb2131 JA |
7431 | struct io_sq_data *sqd = data; |
7432 | struct io_ring_ctx *ctx; | |
a0d9205f | 7433 | unsigned long timeout = 0; |
37d1e2e3 | 7434 | char buf[TASK_COMM_LEN]; |
08369246 | 7435 | DEFINE_WAIT(wait); |
6c271ce2 | 7436 | |
696ee88a | 7437 | snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid); |
37d1e2e3 | 7438 | set_task_comm(current, buf); |
37d1e2e3 JA |
7439 | |
7440 | if (sqd->sq_cpu != -1) | |
7441 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); | |
7442 | else | |
7443 | set_cpus_allowed_ptr(current, cpu_online_mask); | |
7444 | current->flags |= PF_NO_SETAFFINITY; | |
7445 | ||
5bd2182d PM |
7446 | audit_alloc_kernel(current); |
7447 | ||
09a6f4ef | 7448 | mutex_lock(&sqd->lock); |
e4b6d902 | 7449 | while (1) { |
1a924a80 | 7450 | bool cap_entries, sqt_spin = false; |
c1edbf5f | 7451 | |
e4b6d902 PB |
7452 | if (io_sqd_events_pending(sqd) || signal_pending(current)) { |
7453 | if (io_sqd_handle_event(sqd)) | |
c7d95613 | 7454 | break; |
08369246 XW |
7455 | timeout = jiffies + sqd->sq_thread_idle; |
7456 | } | |
e4b6d902 | 7457 | |
e95eee2d | 7458 | cap_entries = !list_is_singular(&sqd->ctx_list); |
69fb2131 | 7459 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
948e1947 | 7460 | int ret = __io_sq_thread(ctx, cap_entries); |
7c30f36a | 7461 | |
5eef4e87 | 7462 | if (!sqt_spin && (ret > 0 || !wq_list_empty(&ctx->iopoll_list))) |
08369246 | 7463 | sqt_spin = true; |
69fb2131 | 7464 | } |
dd432ea5 PB |
7465 | if (io_run_task_work()) |
7466 | sqt_spin = true; | |
6c271ce2 | 7467 | |
08369246 | 7468 | if (sqt_spin || !time_after(jiffies, timeout)) { |
c8d1ba58 | 7469 | cond_resched(); |
08369246 XW |
7470 | if (sqt_spin) |
7471 | timeout = jiffies + sqd->sq_thread_idle; | |
7472 | continue; | |
7473 | } | |
7474 | ||
08369246 | 7475 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); |
dd432ea5 | 7476 | if (!io_sqd_events_pending(sqd) && !current->task_works) { |
1a924a80 PB |
7477 | bool needs_sched = true; |
7478 | ||
724cb4f9 | 7479 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
aaa9f0f4 PB |
7480 | io_ring_set_wakeup_flag(ctx); |
7481 | ||
724cb4f9 | 7482 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
5eef4e87 | 7483 | !wq_list_empty(&ctx->iopoll_list)) { |
724cb4f9 HX |
7484 | needs_sched = false; |
7485 | break; | |
7486 | } | |
7487 | if (io_sqring_entries(ctx)) { | |
7488 | needs_sched = false; | |
7489 | break; | |
7490 | } | |
7491 | } | |
7492 | ||
7493 | if (needs_sched) { | |
7494 | mutex_unlock(&sqd->lock); | |
7495 | schedule(); | |
7496 | mutex_lock(&sqd->lock); | |
7497 | } | |
69fb2131 JA |
7498 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7499 | io_ring_clear_wakeup_flag(ctx); | |
6c271ce2 | 7500 | } |
08369246 XW |
7501 | |
7502 | finish_wait(&sqd->wait, &wait); | |
7503 | timeout = jiffies + sqd->sq_thread_idle; | |
6c271ce2 | 7504 | } |
28cea78a | 7505 | |
78cc687b | 7506 | io_uring_cancel_generic(true, sqd); |
37d1e2e3 | 7507 | sqd->thread = NULL; |
05962f95 | 7508 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
5f3f26f9 | 7509 | io_ring_set_wakeup_flag(ctx); |
521d6a73 | 7510 | io_run_task_work(); |
734551df PB |
7511 | mutex_unlock(&sqd->lock); |
7512 | ||
5bd2182d PM |
7513 | audit_free(current); |
7514 | ||
37d1e2e3 JA |
7515 | complete(&sqd->exited); |
7516 | do_exit(0); | |
6c271ce2 JA |
7517 | } |
7518 | ||
bda52162 JA |
7519 | struct io_wait_queue { |
7520 | struct wait_queue_entry wq; | |
7521 | struct io_ring_ctx *ctx; | |
5fd46178 | 7522 | unsigned cq_tail; |
bda52162 JA |
7523 | unsigned nr_timeouts; |
7524 | }; | |
7525 | ||
6c503150 | 7526 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
bda52162 JA |
7527 | { |
7528 | struct io_ring_ctx *ctx = iowq->ctx; | |
5fd46178 | 7529 | int dist = ctx->cached_cq_tail - (int) iowq->cq_tail; |
bda52162 JA |
7530 | |
7531 | /* | |
d195a66e | 7532 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
7533 | * started waiting. For timeouts, we always want to return to userspace, |
7534 | * regardless of event count. | |
7535 | */ | |
5fd46178 | 7536 | return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
bda52162 JA |
7537 | } |
7538 | ||
7539 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
7540 | int wake_flags, void *key) | |
7541 | { | |
7542 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
7543 | wq); | |
7544 | ||
6c503150 PB |
7545 | /* |
7546 | * Cannot safely flush overflowed CQEs from here, ensure we wake up | |
7547 | * the task, and the next invocation will do it. | |
7548 | */ | |
5ed7a37d | 7549 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->check_cq_overflow)) |
6c503150 PB |
7550 | return autoremove_wake_function(curr, mode, wake_flags, key); |
7551 | return -1; | |
bda52162 JA |
7552 | } |
7553 | ||
af9c1a44 JA |
7554 | static int io_run_task_work_sig(void) |
7555 | { | |
7556 | if (io_run_task_work()) | |
7557 | return 1; | |
7558 | if (!signal_pending(current)) | |
7559 | return 0; | |
0b8cfa97 | 7560 | if (test_thread_flag(TIF_NOTIFY_SIGNAL)) |
792ee0f6 | 7561 | return -ERESTARTSYS; |
af9c1a44 JA |
7562 | return -EINTR; |
7563 | } | |
7564 | ||
eeb60b9a PB |
7565 | /* when returns >0, the caller should retry */ |
7566 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, | |
7567 | struct io_wait_queue *iowq, | |
7568 | signed long *timeout) | |
7569 | { | |
7570 | int ret; | |
7571 | ||
7572 | /* make sure we run task_work before checking for signals */ | |
7573 | ret = io_run_task_work_sig(); | |
7574 | if (ret || io_should_wake(iowq)) | |
7575 | return ret; | |
7576 | /* let the caller flush overflows, retry */ | |
5ed7a37d | 7577 | if (test_bit(0, &ctx->check_cq_overflow)) |
eeb60b9a PB |
7578 | return 1; |
7579 | ||
7580 | *timeout = schedule_timeout(*timeout); | |
7581 | return !*timeout ? -ETIME : 1; | |
7582 | } | |
7583 | ||
2b188cc1 JA |
7584 | /* |
7585 | * Wait until events become available, if we don't already have some. The | |
7586 | * application must reap them itself, as they reside on the shared cq ring. | |
7587 | */ | |
7588 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
c73ebb68 HX |
7589 | const sigset_t __user *sig, size_t sigsz, |
7590 | struct __kernel_timespec __user *uts) | |
2b188cc1 | 7591 | { |
90291099 | 7592 | struct io_wait_queue iowq; |
75b28aff | 7593 | struct io_rings *rings = ctx->rings; |
c1d5a224 PB |
7594 | signed long timeout = MAX_SCHEDULE_TIMEOUT; |
7595 | int ret; | |
2b188cc1 | 7596 | |
b41e9852 | 7597 | do { |
90f67366 | 7598 | io_cqring_overflow_flush(ctx); |
6c503150 | 7599 | if (io_cqring_events(ctx) >= min_events) |
b41e9852 | 7600 | return 0; |
4c6e277c | 7601 | if (!io_run_task_work()) |
b41e9852 | 7602 | break; |
b41e9852 | 7603 | } while (1); |
2b188cc1 | 7604 | |
44df58d4 XW |
7605 | if (uts) { |
7606 | struct timespec64 ts; | |
7607 | ||
7608 | if (get_timespec64(&ts, uts)) | |
7609 | return -EFAULT; | |
7610 | timeout = timespec64_to_jiffies(&ts); | |
7611 | } | |
7612 | ||
2b188cc1 | 7613 | if (sig) { |
9e75ad5d AB |
7614 | #ifdef CONFIG_COMPAT |
7615 | if (in_compat_syscall()) | |
7616 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 7617 | sigsz); |
9e75ad5d AB |
7618 | else |
7619 | #endif | |
b772434b | 7620 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 7621 | |
2b188cc1 JA |
7622 | if (ret) |
7623 | return ret; | |
7624 | } | |
7625 | ||
90291099 PB |
7626 | init_waitqueue_func_entry(&iowq.wq, io_wake_function); |
7627 | iowq.wq.private = current; | |
7628 | INIT_LIST_HEAD(&iowq.wq.entry); | |
7629 | iowq.ctx = ctx; | |
bda52162 | 7630 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
5fd46178 | 7631 | iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events; |
90291099 | 7632 | |
c826bd7a | 7633 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 | 7634 | do { |
ca0a2651 | 7635 | /* if we can't even flush overflow, don't wait for more */ |
90f67366 | 7636 | if (!io_cqring_overflow_flush(ctx)) { |
ca0a2651 JA |
7637 | ret = -EBUSY; |
7638 | break; | |
7639 | } | |
311997b3 | 7640 | prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq, |
bda52162 | 7641 | TASK_INTERRUPTIBLE); |
eeb60b9a | 7642 | ret = io_cqring_wait_schedule(ctx, &iowq, &timeout); |
311997b3 | 7643 | finish_wait(&ctx->cq_wait, &iowq.wq); |
ca0a2651 | 7644 | cond_resched(); |
eeb60b9a | 7645 | } while (ret > 0); |
bda52162 | 7646 | |
b7db41c9 | 7647 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 7648 | |
75b28aff | 7649 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
7650 | } |
7651 | ||
9123c8ff | 7652 | static void io_free_page_table(void **table, size_t size) |
05f3fb3c | 7653 | { |
9123c8ff | 7654 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); |
05f3fb3c | 7655 | |
846a4ef2 | 7656 | for (i = 0; i < nr_tables; i++) |
9123c8ff PB |
7657 | kfree(table[i]); |
7658 | kfree(table); | |
7659 | } | |
7660 | ||
c072481d | 7661 | static __cold void **io_alloc_page_table(size_t size) |
9123c8ff PB |
7662 | { |
7663 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); | |
7664 | size_t init_size = size; | |
7665 | void **table; | |
7666 | ||
0bea96f5 | 7667 | table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
7668 | if (!table) |
7669 | return NULL; | |
7670 | ||
7671 | for (i = 0; i < nr_tables; i++) { | |
27f6b318 | 7672 | unsigned int this_size = min_t(size_t, size, PAGE_SIZE); |
9123c8ff | 7673 | |
0bea96f5 | 7674 | table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
7675 | if (!table[i]) { |
7676 | io_free_page_table(table, init_size); | |
7677 | return NULL; | |
7678 | } | |
7679 | size -= this_size; | |
7680 | } | |
7681 | return table; | |
05f3fb3c JA |
7682 | } |
7683 | ||
28a9fe25 | 7684 | static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node) |
1642b445 | 7685 | { |
28a9fe25 PB |
7686 | percpu_ref_exit(&ref_node->refs); |
7687 | kfree(ref_node); | |
1642b445 PB |
7688 | } |
7689 | ||
c072481d | 7690 | static __cold void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
b9bd2bea PB |
7691 | { |
7692 | struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs); | |
7693 | struct io_ring_ctx *ctx = node->rsrc_data->ctx; | |
7694 | unsigned long flags; | |
7695 | bool first_add = false; | |
7696 | ||
7697 | spin_lock_irqsave(&ctx->rsrc_ref_lock, flags); | |
7698 | node->done = true; | |
7699 | ||
7700 | while (!list_empty(&ctx->rsrc_ref_list)) { | |
7701 | node = list_first_entry(&ctx->rsrc_ref_list, | |
7702 | struct io_rsrc_node, node); | |
7703 | /* recycle ref nodes in order */ | |
7704 | if (!node->done) | |
7705 | break; | |
7706 | list_del(&node->node); | |
7707 | first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist); | |
7708 | } | |
7709 | spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags); | |
7710 | ||
7711 | if (first_add) | |
7712 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, HZ); | |
7713 | } | |
7714 | ||
7715 | static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx) | |
7716 | { | |
7717 | struct io_rsrc_node *ref_node; | |
7718 | ||
7719 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); | |
7720 | if (!ref_node) | |
7721 | return NULL; | |
7722 | ||
7723 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, | |
7724 | 0, GFP_KERNEL)) { | |
7725 | kfree(ref_node); | |
7726 | return NULL; | |
7727 | } | |
7728 | INIT_LIST_HEAD(&ref_node->node); | |
7729 | INIT_LIST_HEAD(&ref_node->rsrc_list); | |
7730 | ref_node->done = false; | |
7731 | return ref_node; | |
7732 | } | |
7733 | ||
a7f0ed5a PB |
7734 | static void io_rsrc_node_switch(struct io_ring_ctx *ctx, |
7735 | struct io_rsrc_data *data_to_kill) | |
ab409402 | 7736 | __must_hold(&ctx->uring_lock) |
6b06314c | 7737 | { |
a7f0ed5a PB |
7738 | WARN_ON_ONCE(!ctx->rsrc_backup_node); |
7739 | WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node); | |
6b06314c | 7740 | |
ab409402 PB |
7741 | io_rsrc_refs_drop(ctx); |
7742 | ||
a7f0ed5a PB |
7743 | if (data_to_kill) { |
7744 | struct io_rsrc_node *rsrc_node = ctx->rsrc_node; | |
82fbcfa9 | 7745 | |
a7f0ed5a | 7746 | rsrc_node->rsrc_data = data_to_kill; |
4956b9ea | 7747 | spin_lock_irq(&ctx->rsrc_ref_lock); |
a7f0ed5a | 7748 | list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list); |
4956b9ea | 7749 | spin_unlock_irq(&ctx->rsrc_ref_lock); |
82fbcfa9 | 7750 | |
3e942498 | 7751 | atomic_inc(&data_to_kill->refs); |
a7f0ed5a PB |
7752 | percpu_ref_kill(&rsrc_node->refs); |
7753 | ctx->rsrc_node = NULL; | |
7754 | } | |
6b06314c | 7755 | |
a7f0ed5a PB |
7756 | if (!ctx->rsrc_node) { |
7757 | ctx->rsrc_node = ctx->rsrc_backup_node; | |
7758 | ctx->rsrc_backup_node = NULL; | |
7759 | } | |
8bad28d8 HX |
7760 | } |
7761 | ||
a7f0ed5a | 7762 | static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx) |
8dd03afe PB |
7763 | { |
7764 | if (ctx->rsrc_backup_node) | |
7765 | return 0; | |
b895c9a6 | 7766 | ctx->rsrc_backup_node = io_rsrc_node_alloc(ctx); |
8dd03afe | 7767 | return ctx->rsrc_backup_node ? 0 : -ENOMEM; |
8bad28d8 HX |
7768 | } |
7769 | ||
c072481d PB |
7770 | static __cold int io_rsrc_ref_quiesce(struct io_rsrc_data *data, |
7771 | struct io_ring_ctx *ctx) | |
8bad28d8 HX |
7772 | { |
7773 | int ret; | |
05589553 | 7774 | |
215c3902 | 7775 | /* As we may drop ->uring_lock, other task may have started quiesce */ |
8bad28d8 HX |
7776 | if (data->quiesce) |
7777 | return -ENXIO; | |
05589553 | 7778 | |
8bad28d8 | 7779 | data->quiesce = true; |
1ffc5422 | 7780 | do { |
a7f0ed5a | 7781 | ret = io_rsrc_node_switch_start(ctx); |
8dd03afe | 7782 | if (ret) |
f2303b1f | 7783 | break; |
a7f0ed5a | 7784 | io_rsrc_node_switch(ctx, data); |
f2303b1f | 7785 | |
3e942498 PB |
7786 | /* kill initial ref, already quiesced if zero */ |
7787 | if (atomic_dec_and_test(&data->refs)) | |
7788 | break; | |
c018db4a | 7789 | mutex_unlock(&ctx->uring_lock); |
8bad28d8 | 7790 | flush_delayed_work(&ctx->rsrc_put_work); |
1ffc5422 | 7791 | ret = wait_for_completion_interruptible(&data->done); |
c018db4a JA |
7792 | if (!ret) { |
7793 | mutex_lock(&ctx->uring_lock); | |
1ffc5422 | 7794 | break; |
c018db4a | 7795 | } |
8bad28d8 | 7796 | |
3e942498 PB |
7797 | atomic_inc(&data->refs); |
7798 | /* wait for all works potentially completing data->done */ | |
7799 | flush_delayed_work(&ctx->rsrc_put_work); | |
cb5e1b81 | 7800 | reinit_completion(&data->done); |
8dd03afe | 7801 | |
1ffc5422 | 7802 | ret = io_run_task_work_sig(); |
8bad28d8 | 7803 | mutex_lock(&ctx->uring_lock); |
f2303b1f | 7804 | } while (ret >= 0); |
8bad28d8 | 7805 | data->quiesce = false; |
05f3fb3c | 7806 | |
8bad28d8 | 7807 | return ret; |
d7954b2b BM |
7808 | } |
7809 | ||
2d091d62 PB |
7810 | static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx) |
7811 | { | |
7812 | unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK; | |
7813 | unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT; | |
7814 | ||
7815 | return &data->tags[table_idx][off]; | |
7816 | } | |
7817 | ||
44b31f2f | 7818 | static void io_rsrc_data_free(struct io_rsrc_data *data) |
1ad555c6 | 7819 | { |
2d091d62 PB |
7820 | size_t size = data->nr * sizeof(data->tags[0][0]); |
7821 | ||
7822 | if (data->tags) | |
7823 | io_free_page_table((void **)data->tags, size); | |
44b31f2f PB |
7824 | kfree(data); |
7825 | } | |
7826 | ||
c072481d PB |
7827 | static __cold int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put, |
7828 | u64 __user *utags, unsigned nr, | |
7829 | struct io_rsrc_data **pdata) | |
1ad555c6 | 7830 | { |
b895c9a6 | 7831 | struct io_rsrc_data *data; |
2d091d62 | 7832 | int ret = -ENOMEM; |
d878c816 | 7833 | unsigned i; |
1ad555c6 BM |
7834 | |
7835 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
7836 | if (!data) | |
d878c816 | 7837 | return -ENOMEM; |
2d091d62 | 7838 | data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0])); |
b60c8dce | 7839 | if (!data->tags) { |
1ad555c6 | 7840 | kfree(data); |
d878c816 PB |
7841 | return -ENOMEM; |
7842 | } | |
2d091d62 PB |
7843 | |
7844 | data->nr = nr; | |
7845 | data->ctx = ctx; | |
7846 | data->do_put = do_put; | |
d878c816 | 7847 | if (utags) { |
2d091d62 | 7848 | ret = -EFAULT; |
d878c816 | 7849 | for (i = 0; i < nr; i++) { |
fdd1dc31 CIK |
7850 | u64 *tag_slot = io_get_tag_slot(data, i); |
7851 | ||
7852 | if (copy_from_user(tag_slot, &utags[i], | |
7853 | sizeof(*tag_slot))) | |
2d091d62 | 7854 | goto fail; |
d878c816 | 7855 | } |
1ad555c6 | 7856 | } |
b60c8dce | 7857 | |
3e942498 | 7858 | atomic_set(&data->refs, 1); |
1ad555c6 | 7859 | init_completion(&data->done); |
d878c816 PB |
7860 | *pdata = data; |
7861 | return 0; | |
2d091d62 PB |
7862 | fail: |
7863 | io_rsrc_data_free(data); | |
7864 | return ret; | |
1ad555c6 BM |
7865 | } |
7866 | ||
9123c8ff PB |
7867 | static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files) |
7868 | { | |
0bea96f5 PB |
7869 | table->files = kvcalloc(nr_files, sizeof(table->files[0]), |
7870 | GFP_KERNEL_ACCOUNT); | |
9123c8ff PB |
7871 | return !!table->files; |
7872 | } | |
7873 | ||
042b0d85 | 7874 | static void io_free_file_tables(struct io_file_table *table) |
9123c8ff | 7875 | { |
042b0d85 | 7876 | kvfree(table->files); |
9123c8ff PB |
7877 | table->files = NULL; |
7878 | } | |
7879 | ||
fff4db76 | 7880 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
1ad555c6 | 7881 | { |
fff4db76 PB |
7882 | #if defined(CONFIG_UNIX) |
7883 | if (ctx->ring_sock) { | |
7884 | struct sock *sock = ctx->ring_sock->sk; | |
7885 | struct sk_buff *skb; | |
7886 | ||
7887 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
7888 | kfree_skb(skb); | |
7889 | } | |
7890 | #else | |
7891 | int i; | |
7892 | ||
7893 | for (i = 0; i < ctx->nr_user_files; i++) { | |
7894 | struct file *file; | |
7895 | ||
7896 | file = io_file_from_index(ctx, i); | |
7897 | if (file) | |
7898 | fput(file); | |
7899 | } | |
7900 | #endif | |
042b0d85 | 7901 | io_free_file_tables(&ctx->file_table); |
44b31f2f | 7902 | io_rsrc_data_free(ctx->file_data); |
fff4db76 PB |
7903 | ctx->file_data = NULL; |
7904 | ctx->nr_user_files = 0; | |
1ad555c6 BM |
7905 | } |
7906 | ||
d7954b2b BM |
7907 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7908 | { | |
d7954b2b BM |
7909 | int ret; |
7910 | ||
08480400 | 7911 | if (!ctx->file_data) |
d7954b2b | 7912 | return -ENXIO; |
08480400 PB |
7913 | ret = io_rsrc_ref_quiesce(ctx->file_data, ctx); |
7914 | if (!ret) | |
7915 | __io_sqe_files_unregister(ctx); | |
7916 | return ret; | |
6b06314c JA |
7917 | } |
7918 | ||
37d1e2e3 | 7919 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
09a6f4ef | 7920 | __releases(&sqd->lock) |
37d1e2e3 | 7921 | { |
521d6a73 PB |
7922 | WARN_ON_ONCE(sqd->thread == current); |
7923 | ||
9e138a48 PB |
7924 | /* |
7925 | * Do the dance but not conditional clear_bit() because it'd race with | |
7926 | * other threads incrementing park_pending and setting the bit. | |
7927 | */ | |
37d1e2e3 | 7928 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
9e138a48 PB |
7929 | if (atomic_dec_return(&sqd->park_pending)) |
7930 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); | |
09a6f4ef | 7931 | mutex_unlock(&sqd->lock); |
37d1e2e3 JA |
7932 | } |
7933 | ||
86e0d676 | 7934 | static void io_sq_thread_park(struct io_sq_data *sqd) |
09a6f4ef | 7935 | __acquires(&sqd->lock) |
37d1e2e3 | 7936 | { |
521d6a73 PB |
7937 | WARN_ON_ONCE(sqd->thread == current); |
7938 | ||
9e138a48 | 7939 | atomic_inc(&sqd->park_pending); |
86e0d676 | 7940 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
09a6f4ef | 7941 | mutex_lock(&sqd->lock); |
05962f95 | 7942 | if (sqd->thread) |
86e0d676 | 7943 | wake_up_process(sqd->thread); |
37d1e2e3 JA |
7944 | } |
7945 | ||
7946 | static void io_sq_thread_stop(struct io_sq_data *sqd) | |
7947 | { | |
521d6a73 | 7948 | WARN_ON_ONCE(sqd->thread == current); |
88885f66 | 7949 | WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)); |
521d6a73 | 7950 | |
05962f95 | 7951 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
88885f66 | 7952 | mutex_lock(&sqd->lock); |
e8f98f24 JA |
7953 | if (sqd->thread) |
7954 | wake_up_process(sqd->thread); | |
09a6f4ef | 7955 | mutex_unlock(&sqd->lock); |
05962f95 | 7956 | wait_for_completion(&sqd->exited); |
37d1e2e3 JA |
7957 | } |
7958 | ||
534ca6d6 | 7959 | static void io_put_sq_data(struct io_sq_data *sqd) |
6c271ce2 | 7960 | { |
534ca6d6 | 7961 | if (refcount_dec_and_test(&sqd->refs)) { |
9e138a48 PB |
7962 | WARN_ON_ONCE(atomic_read(&sqd->park_pending)); |
7963 | ||
37d1e2e3 JA |
7964 | io_sq_thread_stop(sqd); |
7965 | kfree(sqd); | |
7966 | } | |
7967 | } | |
7968 | ||
7969 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) | |
7970 | { | |
7971 | struct io_sq_data *sqd = ctx->sq_data; | |
7972 | ||
7973 | if (sqd) { | |
05962f95 | 7974 | io_sq_thread_park(sqd); |
521d6a73 | 7975 | list_del_init(&ctx->sqd_list); |
37d1e2e3 | 7976 | io_sqd_update_thread_idle(sqd); |
05962f95 | 7977 | io_sq_thread_unpark(sqd); |
37d1e2e3 JA |
7978 | |
7979 | io_put_sq_data(sqd); | |
7980 | ctx->sq_data = NULL; | |
534ca6d6 JA |
7981 | } |
7982 | } | |
7983 | ||
aa06165d JA |
7984 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
7985 | { | |
7986 | struct io_ring_ctx *ctx_attach; | |
7987 | struct io_sq_data *sqd; | |
7988 | struct fd f; | |
7989 | ||
7990 | f = fdget(p->wq_fd); | |
7991 | if (!f.file) | |
7992 | return ERR_PTR(-ENXIO); | |
7993 | if (f.file->f_op != &io_uring_fops) { | |
7994 | fdput(f); | |
7995 | return ERR_PTR(-EINVAL); | |
7996 | } | |
7997 | ||
7998 | ctx_attach = f.file->private_data; | |
7999 | sqd = ctx_attach->sq_data; | |
8000 | if (!sqd) { | |
8001 | fdput(f); | |
8002 | return ERR_PTR(-EINVAL); | |
8003 | } | |
5c2469e0 JA |
8004 | if (sqd->task_tgid != current->tgid) { |
8005 | fdput(f); | |
8006 | return ERR_PTR(-EPERM); | |
8007 | } | |
aa06165d JA |
8008 | |
8009 | refcount_inc(&sqd->refs); | |
8010 | fdput(f); | |
8011 | return sqd; | |
8012 | } | |
8013 | ||
26984fbf PB |
8014 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p, |
8015 | bool *attached) | |
534ca6d6 JA |
8016 | { |
8017 | struct io_sq_data *sqd; | |
8018 | ||
26984fbf | 8019 | *attached = false; |
5c2469e0 JA |
8020 | if (p->flags & IORING_SETUP_ATTACH_WQ) { |
8021 | sqd = io_attach_sq_data(p); | |
26984fbf PB |
8022 | if (!IS_ERR(sqd)) { |
8023 | *attached = true; | |
5c2469e0 | 8024 | return sqd; |
26984fbf | 8025 | } |
5c2469e0 JA |
8026 | /* fall through for EPERM case, setup new sqd/task */ |
8027 | if (PTR_ERR(sqd) != -EPERM) | |
8028 | return sqd; | |
8029 | } | |
aa06165d | 8030 | |
534ca6d6 JA |
8031 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
8032 | if (!sqd) | |
8033 | return ERR_PTR(-ENOMEM); | |
8034 | ||
9e138a48 | 8035 | atomic_set(&sqd->park_pending, 0); |
534ca6d6 | 8036 | refcount_set(&sqd->refs, 1); |
69fb2131 | 8037 | INIT_LIST_HEAD(&sqd->ctx_list); |
09a6f4ef | 8038 | mutex_init(&sqd->lock); |
534ca6d6 | 8039 | init_waitqueue_head(&sqd->wait); |
37d1e2e3 | 8040 | init_completion(&sqd->exited); |
534ca6d6 JA |
8041 | return sqd; |
8042 | } | |
8043 | ||
6b06314c | 8044 | #if defined(CONFIG_UNIX) |
6b06314c JA |
8045 | /* |
8046 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
8047 | * the io_uring can be safely unregistered on process exit, even if we have | |
8048 | * loops in the file referencing. | |
8049 | */ | |
8050 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
8051 | { | |
8052 | struct sock *sk = ctx->ring_sock->sk; | |
8053 | struct scm_fp_list *fpl; | |
8054 | struct sk_buff *skb; | |
08a45173 | 8055 | int i, nr_files; |
6b06314c | 8056 | |
6b06314c JA |
8057 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
8058 | if (!fpl) | |
8059 | return -ENOMEM; | |
8060 | ||
8061 | skb = alloc_skb(0, GFP_KERNEL); | |
8062 | if (!skb) { | |
8063 | kfree(fpl); | |
8064 | return -ENOMEM; | |
8065 | } | |
8066 | ||
8067 | skb->sk = sk; | |
6b06314c | 8068 | |
08a45173 | 8069 | nr_files = 0; |
62e398be | 8070 | fpl->user = get_uid(current_user()); |
6b06314c | 8071 | for (i = 0; i < nr; i++) { |
65e19f54 JA |
8072 | struct file *file = io_file_from_index(ctx, i + offset); |
8073 | ||
8074 | if (!file) | |
08a45173 | 8075 | continue; |
65e19f54 | 8076 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
8077 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
8078 | nr_files++; | |
6b06314c JA |
8079 | } |
8080 | ||
08a45173 JA |
8081 | if (nr_files) { |
8082 | fpl->max = SCM_MAX_FD; | |
8083 | fpl->count = nr_files; | |
8084 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 8085 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
8086 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
8087 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 8088 | |
08a45173 JA |
8089 | for (i = 0; i < nr_files; i++) |
8090 | fput(fpl->fp[i]); | |
8091 | } else { | |
8092 | kfree_skb(skb); | |
8093 | kfree(fpl); | |
8094 | } | |
6b06314c JA |
8095 | |
8096 | return 0; | |
8097 | } | |
8098 | ||
8099 | /* | |
8100 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
8101 | * causes regular reference counting to break down. We rely on the UNIX | |
8102 | * garbage collection to take care of this problem for us. | |
8103 | */ | |
8104 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
8105 | { | |
8106 | unsigned left, total; | |
8107 | int ret = 0; | |
8108 | ||
8109 | total = 0; | |
8110 | left = ctx->nr_user_files; | |
8111 | while (left) { | |
8112 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
8113 | |
8114 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
8115 | if (ret) | |
8116 | break; | |
8117 | left -= this_files; | |
8118 | total += this_files; | |
8119 | } | |
8120 | ||
8121 | if (!ret) | |
8122 | return 0; | |
8123 | ||
8124 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
8125 | struct file *file = io_file_from_index(ctx, total); |
8126 | ||
8127 | if (file) | |
8128 | fput(file); | |
6b06314c JA |
8129 | total++; |
8130 | } | |
8131 | ||
8132 | return ret; | |
8133 | } | |
8134 | #else | |
8135 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
8136 | { | |
8137 | return 0; | |
8138 | } | |
8139 | #endif | |
8140 | ||
47e90392 | 8141 | static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
05f3fb3c | 8142 | { |
50238531 | 8143 | struct file *file = prsrc->file; |
05f3fb3c JA |
8144 | #if defined(CONFIG_UNIX) |
8145 | struct sock *sock = ctx->ring_sock->sk; | |
8146 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
8147 | struct sk_buff *skb; | |
8148 | int i; | |
8149 | ||
8150 | __skb_queue_head_init(&list); | |
8151 | ||
8152 | /* | |
8153 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
8154 | * remove this entry and rearrange the file array. | |
8155 | */ | |
8156 | skb = skb_dequeue(head); | |
8157 | while (skb) { | |
8158 | struct scm_fp_list *fp; | |
8159 | ||
8160 | fp = UNIXCB(skb).fp; | |
8161 | for (i = 0; i < fp->count; i++) { | |
8162 | int left; | |
8163 | ||
8164 | if (fp->fp[i] != file) | |
8165 | continue; | |
8166 | ||
8167 | unix_notinflight(fp->user, fp->fp[i]); | |
8168 | left = fp->count - 1 - i; | |
8169 | if (left) { | |
8170 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
8171 | left * sizeof(struct file *)); | |
8172 | } | |
8173 | fp->count--; | |
8174 | if (!fp->count) { | |
8175 | kfree_skb(skb); | |
8176 | skb = NULL; | |
8177 | } else { | |
8178 | __skb_queue_tail(&list, skb); | |
8179 | } | |
8180 | fput(file); | |
8181 | file = NULL; | |
8182 | break; | |
8183 | } | |
8184 | ||
8185 | if (!file) | |
8186 | break; | |
8187 | ||
8188 | __skb_queue_tail(&list, skb); | |
8189 | ||
8190 | skb = skb_dequeue(head); | |
8191 | } | |
8192 | ||
8193 | if (skb_peek(&list)) { | |
8194 | spin_lock_irq(&head->lock); | |
8195 | while ((skb = __skb_dequeue(&list)) != NULL) | |
8196 | __skb_queue_tail(head, skb); | |
8197 | spin_unlock_irq(&head->lock); | |
8198 | } | |
8199 | #else | |
8200 | fput(file); | |
8201 | #endif | |
8202 | } | |
8203 | ||
b895c9a6 | 8204 | static void __io_rsrc_put_work(struct io_rsrc_node *ref_node) |
65e19f54 | 8205 | { |
b895c9a6 | 8206 | struct io_rsrc_data *rsrc_data = ref_node->rsrc_data; |
269bbe5f BM |
8207 | struct io_ring_ctx *ctx = rsrc_data->ctx; |
8208 | struct io_rsrc_put *prsrc, *tmp; | |
05589553 | 8209 | |
269bbe5f BM |
8210 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
8211 | list_del(&prsrc->list); | |
b60c8dce PB |
8212 | |
8213 | if (prsrc->tag) { | |
8214 | bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL; | |
b60c8dce PB |
8215 | |
8216 | io_ring_submit_lock(ctx, lock_ring); | |
79ebeaee | 8217 | spin_lock(&ctx->completion_lock); |
b60c8dce | 8218 | io_cqring_fill_event(ctx, prsrc->tag, 0, 0); |
2840f710 | 8219 | ctx->cq_extra++; |
b60c8dce | 8220 | io_commit_cqring(ctx); |
79ebeaee | 8221 | spin_unlock(&ctx->completion_lock); |
b60c8dce PB |
8222 | io_cqring_ev_posted(ctx); |
8223 | io_ring_submit_unlock(ctx, lock_ring); | |
8224 | } | |
8225 | ||
40ae0ff7 | 8226 | rsrc_data->do_put(ctx, prsrc); |
269bbe5f | 8227 | kfree(prsrc); |
65e19f54 | 8228 | } |
05589553 | 8229 | |
28a9fe25 | 8230 | io_rsrc_node_destroy(ref_node); |
3e942498 PB |
8231 | if (atomic_dec_and_test(&rsrc_data->refs)) |
8232 | complete(&rsrc_data->done); | |
2faf852d | 8233 | } |
65e19f54 | 8234 | |
269bbe5f | 8235 | static void io_rsrc_put_work(struct work_struct *work) |
4a38aed2 JA |
8236 | { |
8237 | struct io_ring_ctx *ctx; | |
8238 | struct llist_node *node; | |
8239 | ||
269bbe5f BM |
8240 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
8241 | node = llist_del_all(&ctx->rsrc_put_llist); | |
4a38aed2 JA |
8242 | |
8243 | while (node) { | |
b895c9a6 | 8244 | struct io_rsrc_node *ref_node; |
4a38aed2 JA |
8245 | struct llist_node *next = node->next; |
8246 | ||
b895c9a6 | 8247 | ref_node = llist_entry(node, struct io_rsrc_node, llist); |
269bbe5f | 8248 | __io_rsrc_put_work(ref_node); |
4a38aed2 JA |
8249 | node = next; |
8250 | } | |
8251 | } | |
8252 | ||
6b06314c | 8253 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
792e3582 | 8254 | unsigned nr_args, u64 __user *tags) |
6b06314c JA |
8255 | { |
8256 | __s32 __user *fds = (__s32 __user *) arg; | |
05f3fb3c | 8257 | struct file *file; |
f3baed39 | 8258 | int fd, ret; |
846a4ef2 | 8259 | unsigned i; |
6b06314c | 8260 | |
05f3fb3c | 8261 | if (ctx->file_data) |
6b06314c JA |
8262 | return -EBUSY; |
8263 | if (!nr_args) | |
8264 | return -EINVAL; | |
8265 | if (nr_args > IORING_MAX_FIXED_FILES) | |
8266 | return -EMFILE; | |
3a1b8a4e PB |
8267 | if (nr_args > rlimit(RLIMIT_NOFILE)) |
8268 | return -EMFILE; | |
a7f0ed5a | 8269 | ret = io_rsrc_node_switch_start(ctx); |
f3baed39 PB |
8270 | if (ret) |
8271 | return ret; | |
d878c816 PB |
8272 | ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args, |
8273 | &ctx->file_data); | |
8274 | if (ret) | |
8275 | return ret; | |
6b06314c | 8276 | |
f3baed39 | 8277 | ret = -ENOMEM; |
aeca241b | 8278 | if (!io_alloc_file_tables(&ctx->file_table, nr_args)) |
1ad555c6 | 8279 | goto out_free; |
65e19f54 | 8280 | |
08a45173 | 8281 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
d878c816 | 8282 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
600cf3f8 PB |
8283 | ret = -EFAULT; |
8284 | goto out_fput; | |
8285 | } | |
08a45173 | 8286 | /* allow sparse sets */ |
792e3582 PB |
8287 | if (fd == -1) { |
8288 | ret = -EINVAL; | |
2d091d62 | 8289 | if (unlikely(*io_get_tag_slot(ctx->file_data, i))) |
792e3582 | 8290 | goto out_fput; |
08a45173 | 8291 | continue; |
792e3582 | 8292 | } |
6b06314c | 8293 | |
05f3fb3c | 8294 | file = fget(fd); |
6b06314c | 8295 | ret = -EBADF; |
792e3582 | 8296 | if (unlikely(!file)) |
600cf3f8 | 8297 | goto out_fput; |
05f3fb3c | 8298 | |
6b06314c JA |
8299 | /* |
8300 | * Don't allow io_uring instances to be registered. If UNIX | |
8301 | * isn't enabled, then this causes a reference cycle and this | |
8302 | * instance can never get freed. If UNIX is enabled we'll | |
8303 | * handle it just fine, but there's still no point in allowing | |
8304 | * a ring fd as it doesn't support regular read/write anyway. | |
8305 | */ | |
05f3fb3c JA |
8306 | if (file->f_op == &io_uring_fops) { |
8307 | fput(file); | |
600cf3f8 | 8308 | goto out_fput; |
6b06314c | 8309 | } |
aeca241b | 8310 | io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file); |
6b06314c JA |
8311 | } |
8312 | ||
6b06314c | 8313 | ret = io_sqe_files_scm(ctx); |
05589553 | 8314 | if (ret) { |
08480400 | 8315 | __io_sqe_files_unregister(ctx); |
05589553 XW |
8316 | return ret; |
8317 | } | |
6b06314c | 8318 | |
a7f0ed5a | 8319 | io_rsrc_node_switch(ctx, NULL); |
6b06314c | 8320 | return ret; |
600cf3f8 PB |
8321 | out_fput: |
8322 | for (i = 0; i < ctx->nr_user_files; i++) { | |
8323 | file = io_file_from_index(ctx, i); | |
8324 | if (file) | |
8325 | fput(file); | |
8326 | } | |
042b0d85 | 8327 | io_free_file_tables(&ctx->file_table); |
600cf3f8 | 8328 | ctx->nr_user_files = 0; |
600cf3f8 | 8329 | out_free: |
44b31f2f | 8330 | io_rsrc_data_free(ctx->file_data); |
55cbc256 | 8331 | ctx->file_data = NULL; |
6b06314c JA |
8332 | return ret; |
8333 | } | |
8334 | ||
c3a31e60 JA |
8335 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
8336 | int index) | |
8337 | { | |
8338 | #if defined(CONFIG_UNIX) | |
8339 | struct sock *sock = ctx->ring_sock->sk; | |
8340 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
8341 | struct sk_buff *skb; | |
8342 | ||
8343 | /* | |
8344 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
8345 | * file set. If there's no room, fall back to allocating a new skb | |
8346 | * and filling it in. | |
8347 | */ | |
8348 | spin_lock_irq(&head->lock); | |
8349 | skb = skb_peek(head); | |
8350 | if (skb) { | |
8351 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
8352 | ||
8353 | if (fpl->count < SCM_MAX_FD) { | |
8354 | __skb_unlink(skb, head); | |
8355 | spin_unlock_irq(&head->lock); | |
8356 | fpl->fp[fpl->count] = get_file(file); | |
8357 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
8358 | fpl->count++; | |
8359 | spin_lock_irq(&head->lock); | |
8360 | __skb_queue_head(head, skb); | |
8361 | } else { | |
8362 | skb = NULL; | |
8363 | } | |
8364 | } | |
8365 | spin_unlock_irq(&head->lock); | |
8366 | ||
8367 | if (skb) { | |
8368 | fput(file); | |
8369 | return 0; | |
8370 | } | |
8371 | ||
8372 | return __io_sqe_files_scm(ctx, 1, index); | |
8373 | #else | |
8374 | return 0; | |
8375 | #endif | |
8376 | } | |
8377 | ||
9c7b0ba8 PB |
8378 | static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, |
8379 | struct io_rsrc_node *node, void *rsrc) | |
8380 | { | |
8381 | struct io_rsrc_put *prsrc; | |
8382 | ||
8383 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); | |
8384 | if (!prsrc) | |
8385 | return -ENOMEM; | |
8386 | ||
8387 | prsrc->tag = *io_get_tag_slot(data, idx); | |
8388 | prsrc->rsrc = rsrc; | |
8389 | list_add(&prsrc->list, &node->rsrc_list); | |
8390 | return 0; | |
8391 | } | |
8392 | ||
b9445598 PB |
8393 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
8394 | unsigned int issue_flags, u32 slot_index) | |
8395 | { | |
8396 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 8397 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
9c7b0ba8 | 8398 | bool needs_switch = false; |
b9445598 PB |
8399 | struct io_fixed_file *file_slot; |
8400 | int ret = -EBADF; | |
8401 | ||
3b44b371 | 8402 | io_ring_submit_lock(ctx, needs_lock); |
b9445598 PB |
8403 | if (file->f_op == &io_uring_fops) |
8404 | goto err; | |
8405 | ret = -ENXIO; | |
8406 | if (!ctx->file_data) | |
8407 | goto err; | |
8408 | ret = -EINVAL; | |
8409 | if (slot_index >= ctx->nr_user_files) | |
8410 | goto err; | |
8411 | ||
8412 | slot_index = array_index_nospec(slot_index, ctx->nr_user_files); | |
8413 | file_slot = io_fixed_file_slot(&ctx->file_table, slot_index); | |
9c7b0ba8 PB |
8414 | |
8415 | if (file_slot->file_ptr) { | |
8416 | struct file *old_file; | |
8417 | ||
8418 | ret = io_rsrc_node_switch_start(ctx); | |
8419 | if (ret) | |
8420 | goto err; | |
8421 | ||
8422 | old_file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
8423 | ret = io_queue_rsrc_removal(ctx->file_data, slot_index, | |
8424 | ctx->rsrc_node, old_file); | |
8425 | if (ret) | |
8426 | goto err; | |
8427 | file_slot->file_ptr = 0; | |
8428 | needs_switch = true; | |
8429 | } | |
b9445598 PB |
8430 | |
8431 | *io_get_tag_slot(ctx->file_data, slot_index) = 0; | |
8432 | io_fixed_file_set(file_slot, file); | |
8433 | ret = io_sqe_file_register(ctx, file, slot_index); | |
8434 | if (ret) { | |
8435 | file_slot->file_ptr = 0; | |
8436 | goto err; | |
8437 | } | |
8438 | ||
8439 | ret = 0; | |
8440 | err: | |
9c7b0ba8 PB |
8441 | if (needs_switch) |
8442 | io_rsrc_node_switch(ctx, ctx->file_data); | |
3b44b371 | 8443 | io_ring_submit_unlock(ctx, needs_lock); |
b9445598 PB |
8444 | if (ret) |
8445 | fput(file); | |
8446 | return ret; | |
8447 | } | |
8448 | ||
7df778be PB |
8449 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags) |
8450 | { | |
8451 | unsigned int offset = req->close.file_slot - 1; | |
8452 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 8453 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
7df778be PB |
8454 | struct io_fixed_file *file_slot; |
8455 | struct file *file; | |
8456 | int ret, i; | |
8457 | ||
3b44b371 | 8458 | io_ring_submit_lock(ctx, needs_lock); |
7df778be PB |
8459 | ret = -ENXIO; |
8460 | if (unlikely(!ctx->file_data)) | |
8461 | goto out; | |
8462 | ret = -EINVAL; | |
8463 | if (offset >= ctx->nr_user_files) | |
8464 | goto out; | |
8465 | ret = io_rsrc_node_switch_start(ctx); | |
8466 | if (ret) | |
8467 | goto out; | |
8468 | ||
8469 | i = array_index_nospec(offset, ctx->nr_user_files); | |
8470 | file_slot = io_fixed_file_slot(&ctx->file_table, i); | |
8471 | ret = -EBADF; | |
8472 | if (!file_slot->file_ptr) | |
8473 | goto out; | |
8474 | ||
8475 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
8476 | ret = io_queue_rsrc_removal(ctx->file_data, offset, ctx->rsrc_node, file); | |
8477 | if (ret) | |
8478 | goto out; | |
8479 | ||
8480 | file_slot->file_ptr = 0; | |
8481 | io_rsrc_node_switch(ctx, ctx->file_data); | |
8482 | ret = 0; | |
8483 | out: | |
3b44b371 | 8484 | io_ring_submit_unlock(ctx, needs_lock); |
7df778be PB |
8485 | return ret; |
8486 | } | |
8487 | ||
05f3fb3c | 8488 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
c3bdad02 | 8489 | struct io_uring_rsrc_update2 *up, |
05f3fb3c JA |
8490 | unsigned nr_args) |
8491 | { | |
c3bdad02 | 8492 | u64 __user *tags = u64_to_user_ptr(up->tags); |
98f0b3b4 | 8493 | __s32 __user *fds = u64_to_user_ptr(up->data); |
b895c9a6 | 8494 | struct io_rsrc_data *data = ctx->file_data; |
a04b0ac0 PB |
8495 | struct io_fixed_file *file_slot; |
8496 | struct file *file; | |
98f0b3b4 PB |
8497 | int fd, i, err = 0; |
8498 | unsigned int done; | |
05589553 | 8499 | bool needs_switch = false; |
c3a31e60 | 8500 | |
98f0b3b4 PB |
8501 | if (!ctx->file_data) |
8502 | return -ENXIO; | |
8503 | if (up->offset + nr_args > ctx->nr_user_files) | |
c3a31e60 JA |
8504 | return -EINVAL; |
8505 | ||
67973b93 | 8506 | for (done = 0; done < nr_args; done++) { |
c3bdad02 PB |
8507 | u64 tag = 0; |
8508 | ||
8509 | if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) || | |
8510 | copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
c3a31e60 JA |
8511 | err = -EFAULT; |
8512 | break; | |
8513 | } | |
c3bdad02 PB |
8514 | if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) { |
8515 | err = -EINVAL; | |
8516 | break; | |
8517 | } | |
4e0377a1 | 8518 | if (fd == IORING_REGISTER_FILES_SKIP) |
8519 | continue; | |
8520 | ||
67973b93 | 8521 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
aeca241b | 8522 | file_slot = io_fixed_file_slot(&ctx->file_table, i); |
ea64ec02 | 8523 | |
a04b0ac0 PB |
8524 | if (file_slot->file_ptr) { |
8525 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
b60c8dce PB |
8526 | err = io_queue_rsrc_removal(data, up->offset + done, |
8527 | ctx->rsrc_node, file); | |
a5318d3c HD |
8528 | if (err) |
8529 | break; | |
a04b0ac0 | 8530 | file_slot->file_ptr = 0; |
05589553 | 8531 | needs_switch = true; |
c3a31e60 JA |
8532 | } |
8533 | if (fd != -1) { | |
c3a31e60 JA |
8534 | file = fget(fd); |
8535 | if (!file) { | |
8536 | err = -EBADF; | |
8537 | break; | |
8538 | } | |
8539 | /* | |
8540 | * Don't allow io_uring instances to be registered. If | |
8541 | * UNIX isn't enabled, then this causes a reference | |
8542 | * cycle and this instance can never get freed. If UNIX | |
8543 | * is enabled we'll handle it just fine, but there's | |
8544 | * still no point in allowing a ring fd as it doesn't | |
8545 | * support regular read/write anyway. | |
8546 | */ | |
8547 | if (file->f_op == &io_uring_fops) { | |
8548 | fput(file); | |
8549 | err = -EBADF; | |
8550 | break; | |
8551 | } | |
2d091d62 | 8552 | *io_get_tag_slot(data, up->offset + done) = tag; |
9a321c98 | 8553 | io_fixed_file_set(file_slot, file); |
c3a31e60 | 8554 | err = io_sqe_file_register(ctx, file, i); |
f3bd9dae | 8555 | if (err) { |
a04b0ac0 | 8556 | file_slot->file_ptr = 0; |
f3bd9dae | 8557 | fput(file); |
c3a31e60 | 8558 | break; |
f3bd9dae | 8559 | } |
c3a31e60 | 8560 | } |
05f3fb3c JA |
8561 | } |
8562 | ||
a7f0ed5a PB |
8563 | if (needs_switch) |
8564 | io_rsrc_node_switch(ctx, data); | |
c3a31e60 JA |
8565 | return done ? done : err; |
8566 | } | |
05589553 | 8567 | |
685fe7fe JA |
8568 | static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx, |
8569 | struct task_struct *task) | |
24369c2e | 8570 | { |
e941894e | 8571 | struct io_wq_hash *hash; |
24369c2e | 8572 | struct io_wq_data data; |
24369c2e | 8573 | unsigned int concurrency; |
24369c2e | 8574 | |
362a9e65 | 8575 | mutex_lock(&ctx->uring_lock); |
e941894e JA |
8576 | hash = ctx->hash_map; |
8577 | if (!hash) { | |
8578 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); | |
362a9e65 YY |
8579 | if (!hash) { |
8580 | mutex_unlock(&ctx->uring_lock); | |
e941894e | 8581 | return ERR_PTR(-ENOMEM); |
362a9e65 | 8582 | } |
e941894e JA |
8583 | refcount_set(&hash->refs, 1); |
8584 | init_waitqueue_head(&hash->wait); | |
8585 | ctx->hash_map = hash; | |
24369c2e | 8586 | } |
362a9e65 | 8587 | mutex_unlock(&ctx->uring_lock); |
24369c2e | 8588 | |
e941894e | 8589 | data.hash = hash; |
685fe7fe | 8590 | data.task = task; |
ebc11b6c | 8591 | data.free_work = io_wq_free_work; |
f5fa38c5 | 8592 | data.do_work = io_wq_submit_work; |
24369c2e | 8593 | |
d25e3a3d JA |
8594 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
8595 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
24369c2e | 8596 | |
5aa75ed5 | 8597 | return io_wq_create(concurrency, &data); |
24369c2e PB |
8598 | } |
8599 | ||
c072481d PB |
8600 | static __cold int io_uring_alloc_task_context(struct task_struct *task, |
8601 | struct io_ring_ctx *ctx) | |
0f212204 JA |
8602 | { |
8603 | struct io_uring_task *tctx; | |
d8a6df10 | 8604 | int ret; |
0f212204 | 8605 | |
09899b19 | 8606 | tctx = kzalloc(sizeof(*tctx), GFP_KERNEL); |
0f212204 JA |
8607 | if (unlikely(!tctx)) |
8608 | return -ENOMEM; | |
8609 | ||
d8a6df10 JA |
8610 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
8611 | if (unlikely(ret)) { | |
8612 | kfree(tctx); | |
8613 | return ret; | |
8614 | } | |
8615 | ||
685fe7fe | 8616 | tctx->io_wq = io_init_wq_offload(ctx, task); |
5aa75ed5 JA |
8617 | if (IS_ERR(tctx->io_wq)) { |
8618 | ret = PTR_ERR(tctx->io_wq); | |
8619 | percpu_counter_destroy(&tctx->inflight); | |
8620 | kfree(tctx); | |
8621 | return ret; | |
8622 | } | |
8623 | ||
0f212204 JA |
8624 | xa_init(&tctx->xa); |
8625 | init_waitqueue_head(&tctx->wait); | |
fdaf083c | 8626 | atomic_set(&tctx->in_idle, 0); |
b303fe2e | 8627 | atomic_set(&tctx->inflight_tracked, 0); |
0f212204 | 8628 | task->io_uring = tctx; |
7cbf1722 JA |
8629 | spin_lock_init(&tctx->task_lock); |
8630 | INIT_WQ_LIST(&tctx->task_list); | |
7cbf1722 | 8631 | init_task_work(&tctx->task_work, tctx_task_work); |
0f212204 JA |
8632 | return 0; |
8633 | } | |
8634 | ||
8635 | void __io_uring_free(struct task_struct *tsk) | |
8636 | { | |
8637 | struct io_uring_task *tctx = tsk->io_uring; | |
8638 | ||
8639 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); | |
ef8eaa4e | 8640 | WARN_ON_ONCE(tctx->io_wq); |
09899b19 | 8641 | WARN_ON_ONCE(tctx->cached_refs); |
ef8eaa4e | 8642 | |
d8a6df10 | 8643 | percpu_counter_destroy(&tctx->inflight); |
0f212204 JA |
8644 | kfree(tctx); |
8645 | tsk->io_uring = NULL; | |
8646 | } | |
8647 | ||
c072481d PB |
8648 | static __cold int io_sq_offload_create(struct io_ring_ctx *ctx, |
8649 | struct io_uring_params *p) | |
2b188cc1 JA |
8650 | { |
8651 | int ret; | |
8652 | ||
d25e3a3d JA |
8653 | /* Retain compatibility with failing for an invalid attach attempt */ |
8654 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == | |
8655 | IORING_SETUP_ATTACH_WQ) { | |
8656 | struct fd f; | |
8657 | ||
8658 | f = fdget(p->wq_fd); | |
8659 | if (!f.file) | |
8660 | return -ENXIO; | |
0cc936f7 JA |
8661 | if (f.file->f_op != &io_uring_fops) { |
8662 | fdput(f); | |
f2a48dd0 | 8663 | return -EINVAL; |
0cc936f7 JA |
8664 | } |
8665 | fdput(f); | |
d25e3a3d | 8666 | } |
6c271ce2 | 8667 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
46fe18b1 | 8668 | struct task_struct *tsk; |
534ca6d6 | 8669 | struct io_sq_data *sqd; |
26984fbf | 8670 | bool attached; |
534ca6d6 | 8671 | |
cdc1404a PM |
8672 | ret = security_uring_sqpoll(); |
8673 | if (ret) | |
8674 | return ret; | |
8675 | ||
26984fbf | 8676 | sqd = io_get_sq_data(p, &attached); |
534ca6d6 JA |
8677 | if (IS_ERR(sqd)) { |
8678 | ret = PTR_ERR(sqd); | |
8679 | goto err; | |
8680 | } | |
69fb2131 | 8681 | |
7c30f36a | 8682 | ctx->sq_creds = get_current_cred(); |
534ca6d6 | 8683 | ctx->sq_data = sqd; |
917257da JA |
8684 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
8685 | if (!ctx->sq_thread_idle) | |
8686 | ctx->sq_thread_idle = HZ; | |
8687 | ||
78d7f6ba | 8688 | io_sq_thread_park(sqd); |
de75a3d3 PB |
8689 | list_add(&ctx->sqd_list, &sqd->ctx_list); |
8690 | io_sqd_update_thread_idle(sqd); | |
26984fbf | 8691 | /* don't attach to a dying SQPOLL thread, would be racy */ |
f2a48dd0 | 8692 | ret = (attached && !sqd->thread) ? -ENXIO : 0; |
78d7f6ba PB |
8693 | io_sq_thread_unpark(sqd); |
8694 | ||
de75a3d3 PB |
8695 | if (ret < 0) |
8696 | goto err; | |
8697 | if (attached) | |
5aa75ed5 | 8698 | return 0; |
aa06165d | 8699 | |
6c271ce2 | 8700 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 8701 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 8702 | |
917257da | 8703 | ret = -EINVAL; |
f2a48dd0 | 8704 | if (cpu >= nr_cpu_ids || !cpu_online(cpu)) |
e8f98f24 | 8705 | goto err_sqpoll; |
37d1e2e3 | 8706 | sqd->sq_cpu = cpu; |
6c271ce2 | 8707 | } else { |
37d1e2e3 | 8708 | sqd->sq_cpu = -1; |
6c271ce2 | 8709 | } |
37d1e2e3 JA |
8710 | |
8711 | sqd->task_pid = current->pid; | |
5c2469e0 | 8712 | sqd->task_tgid = current->tgid; |
46fe18b1 JA |
8713 | tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE); |
8714 | if (IS_ERR(tsk)) { | |
8715 | ret = PTR_ERR(tsk); | |
e8f98f24 | 8716 | goto err_sqpoll; |
6c271ce2 | 8717 | } |
97a73a0f | 8718 | |
46fe18b1 | 8719 | sqd->thread = tsk; |
97a73a0f | 8720 | ret = io_uring_alloc_task_context(tsk, ctx); |
46fe18b1 | 8721 | wake_up_new_task(tsk); |
0f212204 JA |
8722 | if (ret) |
8723 | goto err; | |
6c271ce2 JA |
8724 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
8725 | /* Can't have SQ_AFF without SQPOLL */ | |
8726 | ret = -EINVAL; | |
8727 | goto err; | |
8728 | } | |
8729 | ||
2b188cc1 | 8730 | return 0; |
f2a48dd0 PB |
8731 | err_sqpoll: |
8732 | complete(&ctx->sq_data->exited); | |
2b188cc1 | 8733 | err: |
37d1e2e3 | 8734 | io_sq_thread_finish(ctx); |
2b188cc1 JA |
8735 | return ret; |
8736 | } | |
8737 | ||
a087e2b5 BM |
8738 | static inline void __io_unaccount_mem(struct user_struct *user, |
8739 | unsigned long nr_pages) | |
2b188cc1 JA |
8740 | { |
8741 | atomic_long_sub(nr_pages, &user->locked_vm); | |
8742 | } | |
8743 | ||
a087e2b5 BM |
8744 | static inline int __io_account_mem(struct user_struct *user, |
8745 | unsigned long nr_pages) | |
2b188cc1 JA |
8746 | { |
8747 | unsigned long page_limit, cur_pages, new_pages; | |
8748 | ||
8749 | /* Don't allow more pages than we can safely lock */ | |
8750 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
8751 | ||
8752 | do { | |
8753 | cur_pages = atomic_long_read(&user->locked_vm); | |
8754 | new_pages = cur_pages + nr_pages; | |
8755 | if (new_pages > page_limit) | |
8756 | return -ENOMEM; | |
8757 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
8758 | new_pages) != cur_pages); | |
8759 | ||
8760 | return 0; | |
8761 | } | |
8762 | ||
26bfa89e | 8763 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 8764 | { |
62e398be | 8765 | if (ctx->user) |
a087e2b5 | 8766 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 8767 | |
26bfa89e JA |
8768 | if (ctx->mm_account) |
8769 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
8770 | } |
8771 | ||
26bfa89e | 8772 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 8773 | { |
30975825 BM |
8774 | int ret; |
8775 | ||
62e398be | 8776 | if (ctx->user) { |
30975825 BM |
8777 | ret = __io_account_mem(ctx->user, nr_pages); |
8778 | if (ret) | |
8779 | return ret; | |
8780 | } | |
8781 | ||
26bfa89e JA |
8782 | if (ctx->mm_account) |
8783 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
8784 | |
8785 | return 0; | |
8786 | } | |
8787 | ||
2b188cc1 JA |
8788 | static void io_mem_free(void *ptr) |
8789 | { | |
52e04ef4 MR |
8790 | struct page *page; |
8791 | ||
8792 | if (!ptr) | |
8793 | return; | |
2b188cc1 | 8794 | |
52e04ef4 | 8795 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
8796 | if (put_page_testzero(page)) |
8797 | free_compound_page(page); | |
8798 | } | |
8799 | ||
8800 | static void *io_mem_alloc(size_t size) | |
8801 | { | |
8802 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | | |
26bfa89e | 8803 | __GFP_NORETRY | __GFP_ACCOUNT; |
2b188cc1 JA |
8804 | |
8805 | return (void *) __get_free_pages(gfp_flags, get_order(size)); | |
8806 | } | |
8807 | ||
75b28aff HV |
8808 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
8809 | size_t *sq_offset) | |
8810 | { | |
8811 | struct io_rings *rings; | |
8812 | size_t off, sq_array_size; | |
8813 | ||
8814 | off = struct_size(rings, cqes, cq_entries); | |
8815 | if (off == SIZE_MAX) | |
8816 | return SIZE_MAX; | |
8817 | ||
8818 | #ifdef CONFIG_SMP | |
8819 | off = ALIGN(off, SMP_CACHE_BYTES); | |
8820 | if (off == 0) | |
8821 | return SIZE_MAX; | |
8822 | #endif | |
8823 | ||
b36200f5 DV |
8824 | if (sq_offset) |
8825 | *sq_offset = off; | |
8826 | ||
75b28aff HV |
8827 | sq_array_size = array_size(sizeof(u32), sq_entries); |
8828 | if (sq_array_size == SIZE_MAX) | |
8829 | return SIZE_MAX; | |
8830 | ||
8831 | if (check_add_overflow(off, sq_array_size, &off)) | |
8832 | return SIZE_MAX; | |
8833 | ||
75b28aff HV |
8834 | return off; |
8835 | } | |
8836 | ||
41edf1a5 | 8837 | static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot) |
7f61a1e9 | 8838 | { |
41edf1a5 | 8839 | struct io_mapped_ubuf *imu = *slot; |
7f61a1e9 PB |
8840 | unsigned int i; |
8841 | ||
6224843d PB |
8842 | if (imu != ctx->dummy_ubuf) { |
8843 | for (i = 0; i < imu->nr_bvecs; i++) | |
8844 | unpin_user_page(imu->bvec[i].bv_page); | |
8845 | if (imu->acct_pages) | |
8846 | io_unaccount_mem(ctx, imu->acct_pages); | |
8847 | kvfree(imu); | |
8848 | } | |
41edf1a5 | 8849 | *slot = NULL; |
7f61a1e9 PB |
8850 | } |
8851 | ||
bd54b6fe | 8852 | static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
edafccee | 8853 | { |
634d00df PB |
8854 | io_buffer_unmap(ctx, &prsrc->buf); |
8855 | prsrc->buf = NULL; | |
bd54b6fe | 8856 | } |
edafccee | 8857 | |
bd54b6fe BM |
8858 | static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
8859 | { | |
8860 | unsigned int i; | |
edafccee | 8861 | |
7f61a1e9 PB |
8862 | for (i = 0; i < ctx->nr_user_bufs; i++) |
8863 | io_buffer_unmap(ctx, &ctx->user_bufs[i]); | |
edafccee | 8864 | kfree(ctx->user_bufs); |
bb6659cc | 8865 | io_rsrc_data_free(ctx->buf_data); |
edafccee | 8866 | ctx->user_bufs = NULL; |
bd54b6fe | 8867 | ctx->buf_data = NULL; |
edafccee | 8868 | ctx->nr_user_bufs = 0; |
bd54b6fe BM |
8869 | } |
8870 | ||
0a96bbe4 | 8871 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
edafccee | 8872 | { |
bd54b6fe | 8873 | int ret; |
edafccee | 8874 | |
bd54b6fe | 8875 | if (!ctx->buf_data) |
edafccee JA |
8876 | return -ENXIO; |
8877 | ||
bd54b6fe BM |
8878 | ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx); |
8879 | if (!ret) | |
8880 | __io_sqe_buffers_unregister(ctx); | |
8881 | return ret; | |
edafccee JA |
8882 | } |
8883 | ||
8884 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
8885 | void __user *arg, unsigned index) | |
8886 | { | |
8887 | struct iovec __user *src; | |
8888 | ||
8889 | #ifdef CONFIG_COMPAT | |
8890 | if (ctx->compat) { | |
8891 | struct compat_iovec __user *ciovs; | |
8892 | struct compat_iovec ciov; | |
8893 | ||
8894 | ciovs = (struct compat_iovec __user *) arg; | |
8895 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
8896 | return -EFAULT; | |
8897 | ||
d55e5f5b | 8898 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
8899 | dst->iov_len = ciov.iov_len; |
8900 | return 0; | |
8901 | } | |
8902 | #endif | |
8903 | src = (struct iovec __user *) arg; | |
8904 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
8905 | return -EFAULT; | |
8906 | return 0; | |
8907 | } | |
8908 | ||
de293938 JA |
8909 | /* |
8910 | * Not super efficient, but this is just a registration time. And we do cache | |
8911 | * the last compound head, so generally we'll only do a full search if we don't | |
8912 | * match that one. | |
8913 | * | |
8914 | * We check if the given compound head page has already been accounted, to | |
8915 | * avoid double accounting it. This allows us to account the full size of the | |
8916 | * page, not just the constituent pages of a huge page. | |
8917 | */ | |
8918 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, | |
8919 | int nr_pages, struct page *hpage) | |
8920 | { | |
8921 | int i, j; | |
8922 | ||
8923 | /* check current page array */ | |
8924 | for (i = 0; i < nr_pages; i++) { | |
8925 | if (!PageCompound(pages[i])) | |
8926 | continue; | |
8927 | if (compound_head(pages[i]) == hpage) | |
8928 | return true; | |
8929 | } | |
8930 | ||
8931 | /* check previously registered pages */ | |
8932 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
41edf1a5 | 8933 | struct io_mapped_ubuf *imu = ctx->user_bufs[i]; |
de293938 JA |
8934 | |
8935 | for (j = 0; j < imu->nr_bvecs; j++) { | |
8936 | if (!PageCompound(imu->bvec[j].bv_page)) | |
8937 | continue; | |
8938 | if (compound_head(imu->bvec[j].bv_page) == hpage) | |
8939 | return true; | |
8940 | } | |
8941 | } | |
8942 | ||
8943 | return false; | |
8944 | } | |
8945 | ||
8946 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, | |
8947 | int nr_pages, struct io_mapped_ubuf *imu, | |
8948 | struct page **last_hpage) | |
8949 | { | |
8950 | int i, ret; | |
8951 | ||
216e5835 | 8952 | imu->acct_pages = 0; |
de293938 JA |
8953 | for (i = 0; i < nr_pages; i++) { |
8954 | if (!PageCompound(pages[i])) { | |
8955 | imu->acct_pages++; | |
8956 | } else { | |
8957 | struct page *hpage; | |
8958 | ||
8959 | hpage = compound_head(pages[i]); | |
8960 | if (hpage == *last_hpage) | |
8961 | continue; | |
8962 | *last_hpage = hpage; | |
8963 | if (headpage_already_acct(ctx, pages, i, hpage)) | |
8964 | continue; | |
8965 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; | |
8966 | } | |
8967 | } | |
8968 | ||
8969 | if (!imu->acct_pages) | |
8970 | return 0; | |
8971 | ||
26bfa89e | 8972 | ret = io_account_mem(ctx, imu->acct_pages); |
de293938 JA |
8973 | if (ret) |
8974 | imu->acct_pages = 0; | |
8975 | return ret; | |
8976 | } | |
8977 | ||
0a96bbe4 | 8978 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
41edf1a5 | 8979 | struct io_mapped_ubuf **pimu, |
0a96bbe4 | 8980 | struct page **last_hpage) |
edafccee | 8981 | { |
41edf1a5 | 8982 | struct io_mapped_ubuf *imu = NULL; |
edafccee JA |
8983 | struct vm_area_struct **vmas = NULL; |
8984 | struct page **pages = NULL; | |
0a96bbe4 BM |
8985 | unsigned long off, start, end, ubuf; |
8986 | size_t size; | |
8987 | int ret, pret, nr_pages, i; | |
8988 | ||
6224843d PB |
8989 | if (!iov->iov_base) { |
8990 | *pimu = ctx->dummy_ubuf; | |
8991 | return 0; | |
8992 | } | |
8993 | ||
0a96bbe4 BM |
8994 | ubuf = (unsigned long) iov->iov_base; |
8995 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
8996 | start = ubuf >> PAGE_SHIFT; | |
8997 | nr_pages = end - start; | |
8998 | ||
41edf1a5 | 8999 | *pimu = NULL; |
0a96bbe4 BM |
9000 | ret = -ENOMEM; |
9001 | ||
9002 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); | |
9003 | if (!pages) | |
9004 | goto done; | |
9005 | ||
9006 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), | |
9007 | GFP_KERNEL); | |
9008 | if (!vmas) | |
9009 | goto done; | |
edafccee | 9010 | |
41edf1a5 | 9011 | imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL); |
a2b4198c | 9012 | if (!imu) |
0a96bbe4 BM |
9013 | goto done; |
9014 | ||
9015 | ret = 0; | |
9016 | mmap_read_lock(current->mm); | |
9017 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, | |
9018 | pages, vmas); | |
9019 | if (pret == nr_pages) { | |
9020 | /* don't support file backed memory */ | |
9021 | for (i = 0; i < nr_pages; i++) { | |
9022 | struct vm_area_struct *vma = vmas[i]; | |
9023 | ||
40dad765 PB |
9024 | if (vma_is_shmem(vma)) |
9025 | continue; | |
0a96bbe4 BM |
9026 | if (vma->vm_file && |
9027 | !is_file_hugepages(vma->vm_file)) { | |
9028 | ret = -EOPNOTSUPP; | |
9029 | break; | |
9030 | } | |
9031 | } | |
9032 | } else { | |
9033 | ret = pret < 0 ? pret : -EFAULT; | |
9034 | } | |
9035 | mmap_read_unlock(current->mm); | |
9036 | if (ret) { | |
9037 | /* | |
9038 | * if we did partial map, or found file backed vmas, | |
9039 | * release any pages we did get | |
9040 | */ | |
9041 | if (pret > 0) | |
9042 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
9043 | goto done; |
9044 | } | |
9045 | ||
9046 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); | |
9047 | if (ret) { | |
9048 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
9049 | goto done; |
9050 | } | |
9051 | ||
9052 | off = ubuf & ~PAGE_MASK; | |
9053 | size = iov->iov_len; | |
9054 | for (i = 0; i < nr_pages; i++) { | |
9055 | size_t vec_len; | |
9056 | ||
9057 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
9058 | imu->bvec[i].bv_page = pages[i]; | |
9059 | imu->bvec[i].bv_len = vec_len; | |
9060 | imu->bvec[i].bv_offset = off; | |
9061 | off = 0; | |
9062 | size -= vec_len; | |
9063 | } | |
9064 | /* store original address for later verification */ | |
9065 | imu->ubuf = ubuf; | |
4751f53d | 9066 | imu->ubuf_end = ubuf + iov->iov_len; |
0a96bbe4 | 9067 | imu->nr_bvecs = nr_pages; |
41edf1a5 | 9068 | *pimu = imu; |
0a96bbe4 BM |
9069 | ret = 0; |
9070 | done: | |
41edf1a5 PB |
9071 | if (ret) |
9072 | kvfree(imu); | |
0a96bbe4 BM |
9073 | kvfree(pages); |
9074 | kvfree(vmas); | |
9075 | return ret; | |
9076 | } | |
9077 | ||
2b358604 | 9078 | static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args) |
0a96bbe4 | 9079 | { |
87094465 PB |
9080 | ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL); |
9081 | return ctx->user_bufs ? 0 : -ENOMEM; | |
2b358604 | 9082 | } |
edafccee | 9083 | |
2b358604 BM |
9084 | static int io_buffer_validate(struct iovec *iov) |
9085 | { | |
50e96989 PB |
9086 | unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1); |
9087 | ||
2b358604 BM |
9088 | /* |
9089 | * Don't impose further limits on the size and buffer | |
9090 | * constraints here, we'll -EINVAL later when IO is | |
9091 | * submitted if they are wrong. | |
9092 | */ | |
6224843d PB |
9093 | if (!iov->iov_base) |
9094 | return iov->iov_len ? -EFAULT : 0; | |
9095 | if (!iov->iov_len) | |
2b358604 | 9096 | return -EFAULT; |
edafccee | 9097 | |
2b358604 BM |
9098 | /* arbitrary limit, but we need something */ |
9099 | if (iov->iov_len > SZ_1G) | |
9100 | return -EFAULT; | |
edafccee | 9101 | |
50e96989 PB |
9102 | if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp)) |
9103 | return -EOVERFLOW; | |
9104 | ||
2b358604 BM |
9105 | return 0; |
9106 | } | |
edafccee | 9107 | |
2b358604 | 9108 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
634d00df | 9109 | unsigned int nr_args, u64 __user *tags) |
2b358604 | 9110 | { |
bd54b6fe BM |
9111 | struct page *last_hpage = NULL; |
9112 | struct io_rsrc_data *data; | |
2b358604 BM |
9113 | int i, ret; |
9114 | struct iovec iov; | |
edafccee | 9115 | |
87094465 PB |
9116 | if (ctx->user_bufs) |
9117 | return -EBUSY; | |
489809e2 | 9118 | if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS) |
87094465 | 9119 | return -EINVAL; |
bd54b6fe | 9120 | ret = io_rsrc_node_switch_start(ctx); |
2b358604 BM |
9121 | if (ret) |
9122 | return ret; | |
d878c816 PB |
9123 | ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data); |
9124 | if (ret) | |
9125 | return ret; | |
bd54b6fe BM |
9126 | ret = io_buffers_map_alloc(ctx, nr_args); |
9127 | if (ret) { | |
bb6659cc | 9128 | io_rsrc_data_free(data); |
bd54b6fe BM |
9129 | return ret; |
9130 | } | |
edafccee | 9131 | |
87094465 | 9132 | for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) { |
edafccee JA |
9133 | ret = io_copy_iov(ctx, &iov, arg, i); |
9134 | if (ret) | |
0a96bbe4 | 9135 | break; |
2b358604 BM |
9136 | ret = io_buffer_validate(&iov); |
9137 | if (ret) | |
0a96bbe4 | 9138 | break; |
2d091d62 | 9139 | if (!iov.iov_base && *io_get_tag_slot(data, i)) { |
cf3770e7 CIK |
9140 | ret = -EINVAL; |
9141 | break; | |
9142 | } | |
edafccee | 9143 | |
41edf1a5 PB |
9144 | ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i], |
9145 | &last_hpage); | |
0a96bbe4 BM |
9146 | if (ret) |
9147 | break; | |
edafccee | 9148 | } |
0a96bbe4 | 9149 | |
bd54b6fe | 9150 | WARN_ON_ONCE(ctx->buf_data); |
0a96bbe4 | 9151 | |
bd54b6fe BM |
9152 | ctx->buf_data = data; |
9153 | if (ret) | |
9154 | __io_sqe_buffers_unregister(ctx); | |
9155 | else | |
9156 | io_rsrc_node_switch(ctx, NULL); | |
edafccee JA |
9157 | return ret; |
9158 | } | |
9159 | ||
634d00df PB |
9160 | static int __io_sqe_buffers_update(struct io_ring_ctx *ctx, |
9161 | struct io_uring_rsrc_update2 *up, | |
9162 | unsigned int nr_args) | |
9163 | { | |
9164 | u64 __user *tags = u64_to_user_ptr(up->tags); | |
9165 | struct iovec iov, __user *iovs = u64_to_user_ptr(up->data); | |
634d00df PB |
9166 | struct page *last_hpage = NULL; |
9167 | bool needs_switch = false; | |
9168 | __u32 done; | |
9169 | int i, err; | |
9170 | ||
9171 | if (!ctx->buf_data) | |
9172 | return -ENXIO; | |
9173 | if (up->offset + nr_args > ctx->nr_user_bufs) | |
9174 | return -EINVAL; | |
9175 | ||
9176 | for (done = 0; done < nr_args; done++) { | |
0b8c0e7c PB |
9177 | struct io_mapped_ubuf *imu; |
9178 | int offset = up->offset + done; | |
634d00df PB |
9179 | u64 tag = 0; |
9180 | ||
9181 | err = io_copy_iov(ctx, &iov, iovs, done); | |
9182 | if (err) | |
9183 | break; | |
9184 | if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) { | |
9185 | err = -EFAULT; | |
9186 | break; | |
9187 | } | |
0b8c0e7c PB |
9188 | err = io_buffer_validate(&iov); |
9189 | if (err) | |
9190 | break; | |
cf3770e7 CIK |
9191 | if (!iov.iov_base && tag) { |
9192 | err = -EINVAL; | |
9193 | break; | |
9194 | } | |
0b8c0e7c PB |
9195 | err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage); |
9196 | if (err) | |
9197 | break; | |
634d00df | 9198 | |
0b8c0e7c | 9199 | i = array_index_nospec(offset, ctx->nr_user_bufs); |
6224843d | 9200 | if (ctx->user_bufs[i] != ctx->dummy_ubuf) { |
0b8c0e7c PB |
9201 | err = io_queue_rsrc_removal(ctx->buf_data, offset, |
9202 | ctx->rsrc_node, ctx->user_bufs[i]); | |
9203 | if (unlikely(err)) { | |
9204 | io_buffer_unmap(ctx, &imu); | |
634d00df | 9205 | break; |
0b8c0e7c | 9206 | } |
634d00df PB |
9207 | ctx->user_bufs[i] = NULL; |
9208 | needs_switch = true; | |
9209 | } | |
9210 | ||
0b8c0e7c | 9211 | ctx->user_bufs[i] = imu; |
2d091d62 | 9212 | *io_get_tag_slot(ctx->buf_data, offset) = tag; |
634d00df PB |
9213 | } |
9214 | ||
9215 | if (needs_switch) | |
9216 | io_rsrc_node_switch(ctx, ctx->buf_data); | |
9217 | return done ? done : err; | |
9218 | } | |
9219 | ||
9b402849 JA |
9220 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
9221 | { | |
9222 | __s32 __user *fds = arg; | |
9223 | int fd; | |
9224 | ||
9225 | if (ctx->cq_ev_fd) | |
9226 | return -EBUSY; | |
9227 | ||
9228 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
9229 | return -EFAULT; | |
9230 | ||
9231 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); | |
9232 | if (IS_ERR(ctx->cq_ev_fd)) { | |
9233 | int ret = PTR_ERR(ctx->cq_ev_fd); | |
fe7e3257 | 9234 | |
9b402849 JA |
9235 | ctx->cq_ev_fd = NULL; |
9236 | return ret; | |
9237 | } | |
9238 | ||
9239 | return 0; | |
9240 | } | |
9241 | ||
9242 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
9243 | { | |
9244 | if (ctx->cq_ev_fd) { | |
9245 | eventfd_ctx_put(ctx->cq_ev_fd); | |
9246 | ctx->cq_ev_fd = NULL; | |
9247 | return 0; | |
9248 | } | |
9249 | ||
9250 | return -ENXIO; | |
9251 | } | |
9252 | ||
5a2e745d JA |
9253 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
9254 | { | |
9e15c3a0 JA |
9255 | struct io_buffer *buf; |
9256 | unsigned long index; | |
9257 | ||
8bab4c09 | 9258 | xa_for_each(&ctx->io_buffers, index, buf) { |
9e15c3a0 | 9259 | __io_remove_buffers(ctx, buf, index, -1U); |
8bab4c09 JA |
9260 | cond_resched(); |
9261 | } | |
5a2e745d JA |
9262 | } |
9263 | ||
4010fec4 | 9264 | static void io_req_caches_free(struct io_ring_ctx *ctx) |
2b188cc1 | 9265 | { |
cd0ca2e0 | 9266 | struct io_submit_state *state = &ctx->submit_state; |
37f0e767 | 9267 | int nr = 0; |
bf019da7 | 9268 | |
9a4fdbd8 | 9269 | mutex_lock(&ctx->uring_lock); |
cd0ca2e0 | 9270 | io_flush_cached_locked_reqs(ctx, state); |
9a4fdbd8 | 9271 | |
c2b6c6bc PB |
9272 | while (state->free_list.next) { |
9273 | struct io_wq_work_node *node; | |
9274 | struct io_kiocb *req; | |
9a4fdbd8 | 9275 | |
c2b6c6bc PB |
9276 | node = wq_stack_extract(&state->free_list); |
9277 | req = container_of(node, struct io_kiocb, comp_list); | |
9278 | kmem_cache_free(req_cachep, req); | |
37f0e767 | 9279 | nr++; |
c2b6c6bc | 9280 | } |
37f0e767 PB |
9281 | if (nr) |
9282 | percpu_ref_put_many(&ctx->refs, nr); | |
9a4fdbd8 JA |
9283 | mutex_unlock(&ctx->uring_lock); |
9284 | } | |
9285 | ||
43597aac | 9286 | static void io_wait_rsrc_data(struct io_rsrc_data *data) |
2b188cc1 | 9287 | { |
43597aac | 9288 | if (data && !atomic_dec_and_test(&data->refs)) |
bd54b6fe | 9289 | wait_for_completion(&data->done); |
bd54b6fe | 9290 | } |
04fc6c80 | 9291 | |
c072481d | 9292 | static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx) |
2b188cc1 | 9293 | { |
37d1e2e3 | 9294 | io_sq_thread_finish(ctx); |
2aede0e4 | 9295 | |
37d1e2e3 | 9296 | if (ctx->mm_account) { |
2aede0e4 JA |
9297 | mmdrop(ctx->mm_account); |
9298 | ctx->mm_account = NULL; | |
30975825 | 9299 | } |
def596e9 | 9300 | |
ab409402 | 9301 | io_rsrc_refs_drop(ctx); |
43597aac PB |
9302 | /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */ |
9303 | io_wait_rsrc_data(ctx->buf_data); | |
9304 | io_wait_rsrc_data(ctx->file_data); | |
9305 | ||
8bad28d8 | 9306 | mutex_lock(&ctx->uring_lock); |
43597aac | 9307 | if (ctx->buf_data) |
bd54b6fe | 9308 | __io_sqe_buffers_unregister(ctx); |
43597aac | 9309 | if (ctx->file_data) |
08480400 | 9310 | __io_sqe_files_unregister(ctx); |
c4ea060e PB |
9311 | if (ctx->rings) |
9312 | __io_cqring_overflow_flush(ctx, true); | |
8bad28d8 | 9313 | mutex_unlock(&ctx->uring_lock); |
9b402849 | 9314 | io_eventfd_unregister(ctx); |
5a2e745d | 9315 | io_destroy_buffers(ctx); |
07db298a PB |
9316 | if (ctx->sq_creds) |
9317 | put_cred(ctx->sq_creds); | |
def596e9 | 9318 | |
a7f0ed5a PB |
9319 | /* there are no registered resources left, nobody uses it */ |
9320 | if (ctx->rsrc_node) | |
9321 | io_rsrc_node_destroy(ctx->rsrc_node); | |
8dd03afe | 9322 | if (ctx->rsrc_backup_node) |
b895c9a6 | 9323 | io_rsrc_node_destroy(ctx->rsrc_backup_node); |
a7f0ed5a | 9324 | flush_delayed_work(&ctx->rsrc_put_work); |
756ab7c0 | 9325 | flush_delayed_work(&ctx->fallback_work); |
a7f0ed5a PB |
9326 | |
9327 | WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list)); | |
9328 | WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist)); | |
def596e9 | 9329 | |
2b188cc1 | 9330 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
9331 | if (ctx->ring_sock) { |
9332 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 9333 | sock_release(ctx->ring_sock); |
355e8d26 | 9334 | } |
2b188cc1 | 9335 | #endif |
ef9dd637 | 9336 | WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list)); |
2b188cc1 | 9337 | |
75b28aff | 9338 | io_mem_free(ctx->rings); |
2b188cc1 | 9339 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
9340 | |
9341 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 9342 | free_uid(ctx->user); |
4010fec4 | 9343 | io_req_caches_free(ctx); |
e941894e JA |
9344 | if (ctx->hash_map) |
9345 | io_wq_put_hash(ctx->hash_map); | |
78076bb6 | 9346 | kfree(ctx->cancel_hash); |
6224843d | 9347 | kfree(ctx->dummy_ubuf); |
2b188cc1 JA |
9348 | kfree(ctx); |
9349 | } | |
9350 | ||
9351 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
9352 | { | |
9353 | struct io_ring_ctx *ctx = file->private_data; | |
9354 | __poll_t mask = 0; | |
9355 | ||
d60aa65b | 9356 | poll_wait(file, &ctx->cq_wait, wait); |
4f7067c3 SB |
9357 | /* |
9358 | * synchronizes with barrier from wq_has_sleeper call in | |
9359 | * io_commit_cqring | |
9360 | */ | |
2b188cc1 | 9361 | smp_rmb(); |
90554200 | 9362 | if (!io_sqring_full(ctx)) |
2b188cc1 | 9363 | mask |= EPOLLOUT | EPOLLWRNORM; |
ed670c3f HX |
9364 | |
9365 | /* | |
9366 | * Don't flush cqring overflow list here, just do a simple check. | |
9367 | * Otherwise there could possible be ABBA deadlock: | |
9368 | * CPU0 CPU1 | |
9369 | * ---- ---- | |
9370 | * lock(&ctx->uring_lock); | |
9371 | * lock(&ep->mtx); | |
9372 | * lock(&ctx->uring_lock); | |
9373 | * lock(&ep->mtx); | |
9374 | * | |
9375 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this | |
9376 | * pushs them to do the flush. | |
9377 | */ | |
5ed7a37d | 9378 | if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow)) |
2b188cc1 JA |
9379 | mask |= EPOLLIN | EPOLLRDNORM; |
9380 | ||
9381 | return mask; | |
9382 | } | |
9383 | ||
0bead8cd | 9384 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
071698e1 | 9385 | { |
4379bf8b | 9386 | const struct cred *creds; |
071698e1 | 9387 | |
61cf9370 | 9388 | creds = xa_erase(&ctx->personalities, id); |
4379bf8b JA |
9389 | if (creds) { |
9390 | put_cred(creds); | |
0bead8cd | 9391 | return 0; |
1e6fa521 | 9392 | } |
0bead8cd YD |
9393 | |
9394 | return -EINVAL; | |
9395 | } | |
9396 | ||
d56d938b PB |
9397 | struct io_tctx_exit { |
9398 | struct callback_head task_work; | |
9399 | struct completion completion; | |
baf186c4 | 9400 | struct io_ring_ctx *ctx; |
d56d938b PB |
9401 | }; |
9402 | ||
c072481d | 9403 | static __cold void io_tctx_exit_cb(struct callback_head *cb) |
d56d938b PB |
9404 | { |
9405 | struct io_uring_task *tctx = current->io_uring; | |
9406 | struct io_tctx_exit *work; | |
9407 | ||
9408 | work = container_of(cb, struct io_tctx_exit, task_work); | |
9409 | /* | |
9410 | * When @in_idle, we're in cancellation and it's racy to remove the | |
9411 | * node. It'll be removed by the end of cancellation, just ignore it. | |
9412 | */ | |
9413 | if (!atomic_read(&tctx->in_idle)) | |
eef51daa | 9414 | io_uring_del_tctx_node((unsigned long)work->ctx); |
d56d938b PB |
9415 | complete(&work->completion); |
9416 | } | |
9417 | ||
c072481d | 9418 | static __cold bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
28090c13 PB |
9419 | { |
9420 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
9421 | ||
9422 | return req->ctx == data; | |
9423 | } | |
9424 | ||
c072481d | 9425 | static __cold void io_ring_exit_work(struct work_struct *work) |
85faa7b8 | 9426 | { |
d56d938b | 9427 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work); |
b5bb3a24 | 9428 | unsigned long timeout = jiffies + HZ * 60 * 5; |
58d3be2c | 9429 | unsigned long interval = HZ / 20; |
d56d938b PB |
9430 | struct io_tctx_exit exit; |
9431 | struct io_tctx_node *node; | |
9432 | int ret; | |
85faa7b8 | 9433 | |
56952e91 JA |
9434 | /* |
9435 | * If we're doing polled IO and end up having requests being | |
9436 | * submitted async (out-of-line), then completions can come in while | |
9437 | * we're waiting for refs to drop. We need to reap these manually, | |
9438 | * as nobody else will be looking for them. | |
9439 | */ | |
b2edc0a7 | 9440 | do { |
3dd0c97a | 9441 | io_uring_try_cancel_requests(ctx, NULL, true); |
28090c13 PB |
9442 | if (ctx->sq_data) { |
9443 | struct io_sq_data *sqd = ctx->sq_data; | |
9444 | struct task_struct *tsk; | |
9445 | ||
9446 | io_sq_thread_park(sqd); | |
9447 | tsk = sqd->thread; | |
9448 | if (tsk && tsk->io_uring && tsk->io_uring->io_wq) | |
9449 | io_wq_cancel_cb(tsk->io_uring->io_wq, | |
9450 | io_cancel_ctx_cb, ctx, true); | |
9451 | io_sq_thread_unpark(sqd); | |
9452 | } | |
b5bb3a24 | 9453 | |
37f0e767 PB |
9454 | io_req_caches_free(ctx); |
9455 | ||
58d3be2c PB |
9456 | if (WARN_ON_ONCE(time_after(jiffies, timeout))) { |
9457 | /* there is little hope left, don't run it too often */ | |
9458 | interval = HZ * 60; | |
9459 | } | |
9460 | } while (!wait_for_completion_timeout(&ctx->ref_comp, interval)); | |
d56d938b | 9461 | |
7f00651a PB |
9462 | init_completion(&exit.completion); |
9463 | init_task_work(&exit.task_work, io_tctx_exit_cb); | |
9464 | exit.ctx = ctx; | |
89b5066e PB |
9465 | /* |
9466 | * Some may use context even when all refs and requests have been put, | |
9467 | * and they are free to do so while still holding uring_lock or | |
5b0a6acc | 9468 | * completion_lock, see io_req_task_submit(). Apart from other work, |
89b5066e PB |
9469 | * this lock/unlock section also waits them to finish. |
9470 | */ | |
d56d938b PB |
9471 | mutex_lock(&ctx->uring_lock); |
9472 | while (!list_empty(&ctx->tctx_list)) { | |
b5bb3a24 PB |
9473 | WARN_ON_ONCE(time_after(jiffies, timeout)); |
9474 | ||
d56d938b PB |
9475 | node = list_first_entry(&ctx->tctx_list, struct io_tctx_node, |
9476 | ctx_node); | |
7f00651a PB |
9477 | /* don't spin on a single task if cancellation failed */ |
9478 | list_rotate_left(&ctx->tctx_list); | |
d56d938b PB |
9479 | ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL); |
9480 | if (WARN_ON_ONCE(ret)) | |
9481 | continue; | |
d56d938b PB |
9482 | |
9483 | mutex_unlock(&ctx->uring_lock); | |
9484 | wait_for_completion(&exit.completion); | |
d56d938b PB |
9485 | mutex_lock(&ctx->uring_lock); |
9486 | } | |
9487 | mutex_unlock(&ctx->uring_lock); | |
79ebeaee JA |
9488 | spin_lock(&ctx->completion_lock); |
9489 | spin_unlock(&ctx->completion_lock); | |
d56d938b | 9490 | |
85faa7b8 JA |
9491 | io_ring_ctx_free(ctx); |
9492 | } | |
9493 | ||
80c4cbdb | 9494 | /* Returns true if we found and killed one or more timeouts */ |
c072481d PB |
9495 | static __cold bool io_kill_timeouts(struct io_ring_ctx *ctx, |
9496 | struct task_struct *tsk, bool cancel_all) | |
80c4cbdb PB |
9497 | { |
9498 | struct io_kiocb *req, *tmp; | |
9499 | int canceled = 0; | |
9500 | ||
79ebeaee JA |
9501 | spin_lock(&ctx->completion_lock); |
9502 | spin_lock_irq(&ctx->timeout_lock); | |
80c4cbdb | 9503 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
3dd0c97a | 9504 | if (io_match_task(req, tsk, cancel_all)) { |
80c4cbdb PB |
9505 | io_kill_timeout(req, -ECANCELED); |
9506 | canceled++; | |
9507 | } | |
9508 | } | |
79ebeaee | 9509 | spin_unlock_irq(&ctx->timeout_lock); |
51520426 PB |
9510 | if (canceled != 0) |
9511 | io_commit_cqring(ctx); | |
79ebeaee | 9512 | spin_unlock(&ctx->completion_lock); |
80c4cbdb PB |
9513 | if (canceled != 0) |
9514 | io_cqring_ev_posted(ctx); | |
9515 | return canceled != 0; | |
9516 | } | |
9517 | ||
c072481d | 9518 | static __cold void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
2b188cc1 | 9519 | { |
61cf9370 MWO |
9520 | unsigned long index; |
9521 | struct creds *creds; | |
9522 | ||
2b188cc1 JA |
9523 | mutex_lock(&ctx->uring_lock); |
9524 | percpu_ref_kill(&ctx->refs); | |
634578f8 | 9525 | if (ctx->rings) |
6c2450ae | 9526 | __io_cqring_overflow_flush(ctx, true); |
61cf9370 MWO |
9527 | xa_for_each(&ctx->personalities, index, creds) |
9528 | io_unregister_personality(ctx, index); | |
2b188cc1 JA |
9529 | mutex_unlock(&ctx->uring_lock); |
9530 | ||
3dd0c97a PB |
9531 | io_kill_timeouts(ctx, NULL, true); |
9532 | io_poll_remove_all(ctx, NULL, true); | |
561fb04a | 9533 | |
15dff286 | 9534 | /* if we failed setting up the ctx, we might not have any rings */ |
b2edc0a7 | 9535 | io_iopoll_try_reap_events(ctx); |
309fc03a | 9536 | |
85faa7b8 | 9537 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
9538 | /* |
9539 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
9540 | * if we're exiting a ton of rings at the same time. It just adds | |
9541 | * noise and overhead, there's no discernable change in runtime | |
9542 | * over using system_wq. | |
9543 | */ | |
9544 | queue_work(system_unbound_wq, &ctx->exit_work); | |
2b188cc1 JA |
9545 | } |
9546 | ||
9547 | static int io_uring_release(struct inode *inode, struct file *file) | |
9548 | { | |
9549 | struct io_ring_ctx *ctx = file->private_data; | |
9550 | ||
9551 | file->private_data = NULL; | |
9552 | io_ring_ctx_wait_and_kill(ctx); | |
9553 | return 0; | |
9554 | } | |
9555 | ||
f6edbabb PB |
9556 | struct io_task_cancel { |
9557 | struct task_struct *task; | |
3dd0c97a | 9558 | bool all; |
f6edbabb | 9559 | }; |
f254ac04 | 9560 | |
f6edbabb | 9561 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
b711d4ea | 9562 | { |
9a472ef7 | 9563 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
f6edbabb | 9564 | struct io_task_cancel *cancel = data; |
9a472ef7 PB |
9565 | bool ret; |
9566 | ||
3dd0c97a | 9567 | if (!cancel->all && (req->flags & REQ_F_LINK_TIMEOUT)) { |
9a472ef7 PB |
9568 | struct io_ring_ctx *ctx = req->ctx; |
9569 | ||
9570 | /* protect against races with linked timeouts */ | |
79ebeaee | 9571 | spin_lock(&ctx->completion_lock); |
3dd0c97a | 9572 | ret = io_match_task(req, cancel->task, cancel->all); |
79ebeaee | 9573 | spin_unlock(&ctx->completion_lock); |
9a472ef7 | 9574 | } else { |
3dd0c97a | 9575 | ret = io_match_task(req, cancel->task, cancel->all); |
9a472ef7 PB |
9576 | } |
9577 | return ret; | |
b711d4ea JA |
9578 | } |
9579 | ||
c072481d PB |
9580 | static __cold bool io_cancel_defer_files(struct io_ring_ctx *ctx, |
9581 | struct task_struct *task, | |
9582 | bool cancel_all) | |
b7ddce3c | 9583 | { |
e1915f76 | 9584 | struct io_defer_entry *de; |
b7ddce3c PB |
9585 | LIST_HEAD(list); |
9586 | ||
79ebeaee | 9587 | spin_lock(&ctx->completion_lock); |
b7ddce3c | 9588 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
3dd0c97a | 9589 | if (io_match_task(de->req, task, cancel_all)) { |
b7ddce3c PB |
9590 | list_cut_position(&list, &ctx->defer_list, &de->list); |
9591 | break; | |
9592 | } | |
9593 | } | |
79ebeaee | 9594 | spin_unlock(&ctx->completion_lock); |
e1915f76 PB |
9595 | if (list_empty(&list)) |
9596 | return false; | |
b7ddce3c PB |
9597 | |
9598 | while (!list_empty(&list)) { | |
9599 | de = list_first_entry(&list, struct io_defer_entry, list); | |
9600 | list_del_init(&de->list); | |
f41db273 | 9601 | io_req_complete_failed(de->req, -ECANCELED); |
b7ddce3c PB |
9602 | kfree(de); |
9603 | } | |
e1915f76 | 9604 | return true; |
b7ddce3c PB |
9605 | } |
9606 | ||
c072481d | 9607 | static __cold bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx) |
1b00764f PB |
9608 | { |
9609 | struct io_tctx_node *node; | |
9610 | enum io_wq_cancel cret; | |
9611 | bool ret = false; | |
9612 | ||
9613 | mutex_lock(&ctx->uring_lock); | |
9614 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
9615 | struct io_uring_task *tctx = node->task->io_uring; | |
9616 | ||
9617 | /* | |
9618 | * io_wq will stay alive while we hold uring_lock, because it's | |
9619 | * killed after ctx nodes, which requires to take the lock. | |
9620 | */ | |
9621 | if (!tctx || !tctx->io_wq) | |
9622 | continue; | |
9623 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true); | |
9624 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
9625 | } | |
9626 | mutex_unlock(&ctx->uring_lock); | |
9627 | ||
9628 | return ret; | |
9629 | } | |
9630 | ||
c072481d PB |
9631 | static __cold void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
9632 | struct task_struct *task, | |
9633 | bool cancel_all) | |
9936c7c2 | 9634 | { |
3dd0c97a | 9635 | struct io_task_cancel cancel = { .task = task, .all = cancel_all, }; |
1b00764f | 9636 | struct io_uring_task *tctx = task ? task->io_uring : NULL; |
9936c7c2 PB |
9637 | |
9638 | while (1) { | |
9639 | enum io_wq_cancel cret; | |
9640 | bool ret = false; | |
9641 | ||
1b00764f PB |
9642 | if (!task) { |
9643 | ret |= io_uring_try_cancel_iowq(ctx); | |
9644 | } else if (tctx && tctx->io_wq) { | |
9645 | /* | |
9646 | * Cancels requests of all rings, not only @ctx, but | |
9647 | * it's fine as the task is in exit/exec. | |
9648 | */ | |
5aa75ed5 | 9649 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, |
9936c7c2 PB |
9650 | &cancel, true); |
9651 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
9652 | } | |
9653 | ||
9654 | /* SQPOLL thread does its own polling */ | |
3dd0c97a | 9655 | if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) || |
d052d1d6 | 9656 | (ctx->sq_data && ctx->sq_data->thread == current)) { |
5eef4e87 | 9657 | while (!wq_list_empty(&ctx->iopoll_list)) { |
9936c7c2 PB |
9658 | io_iopoll_try_reap_events(ctx); |
9659 | ret = true; | |
9660 | } | |
9661 | } | |
9662 | ||
3dd0c97a PB |
9663 | ret |= io_cancel_defer_files(ctx, task, cancel_all); |
9664 | ret |= io_poll_remove_all(ctx, task, cancel_all); | |
9665 | ret |= io_kill_timeouts(ctx, task, cancel_all); | |
e5dc480d PB |
9666 | if (task) |
9667 | ret |= io_run_task_work(); | |
9936c7c2 PB |
9668 | if (!ret) |
9669 | break; | |
9670 | cond_resched(); | |
9671 | } | |
9672 | } | |
9673 | ||
eef51daa | 9674 | static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
0f212204 | 9675 | { |
236434c3 | 9676 | struct io_uring_task *tctx = current->io_uring; |
13bf43f5 | 9677 | struct io_tctx_node *node; |
a528b04e | 9678 | int ret; |
236434c3 MWO |
9679 | |
9680 | if (unlikely(!tctx)) { | |
5aa75ed5 | 9681 | ret = io_uring_alloc_task_context(current, ctx); |
0f212204 JA |
9682 | if (unlikely(ret)) |
9683 | return ret; | |
e139a1ec | 9684 | |
236434c3 | 9685 | tctx = current->io_uring; |
e139a1ec PB |
9686 | if (ctx->iowq_limits_set) { |
9687 | unsigned int limits[2] = { ctx->iowq_limits[0], | |
9688 | ctx->iowq_limits[1], }; | |
9689 | ||
9690 | ret = io_wq_max_workers(tctx->io_wq, limits); | |
9691 | if (ret) | |
9692 | return ret; | |
9693 | } | |
0f212204 | 9694 | } |
cf27f3b1 PB |
9695 | if (!xa_load(&tctx->xa, (unsigned long)ctx)) { |
9696 | node = kmalloc(sizeof(*node), GFP_KERNEL); | |
9697 | if (!node) | |
9698 | return -ENOMEM; | |
9699 | node->ctx = ctx; | |
9700 | node->task = current; | |
13bf43f5 | 9701 | |
cf27f3b1 PB |
9702 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx, |
9703 | node, GFP_KERNEL)); | |
9704 | if (ret) { | |
9705 | kfree(node); | |
9706 | return ret; | |
0f212204 | 9707 | } |
cf27f3b1 PB |
9708 | |
9709 | mutex_lock(&ctx->uring_lock); | |
9710 | list_add(&node->ctx_node, &ctx->tctx_list); | |
9711 | mutex_unlock(&ctx->uring_lock); | |
0f212204 | 9712 | } |
cf27f3b1 | 9713 | tctx->last = ctx; |
0f212204 JA |
9714 | return 0; |
9715 | } | |
9716 | ||
cf27f3b1 PB |
9717 | /* |
9718 | * Note that this task has used io_uring. We use it for cancelation purposes. | |
9719 | */ | |
eef51daa | 9720 | static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
cf27f3b1 PB |
9721 | { |
9722 | struct io_uring_task *tctx = current->io_uring; | |
9723 | ||
9724 | if (likely(tctx && tctx->last == ctx)) | |
9725 | return 0; | |
eef51daa | 9726 | return __io_uring_add_tctx_node(ctx); |
cf27f3b1 PB |
9727 | } |
9728 | ||
0f212204 JA |
9729 | /* |
9730 | * Remove this io_uring_file -> task mapping. | |
9731 | */ | |
c072481d | 9732 | static __cold void io_uring_del_tctx_node(unsigned long index) |
0f212204 JA |
9733 | { |
9734 | struct io_uring_task *tctx = current->io_uring; | |
13bf43f5 | 9735 | struct io_tctx_node *node; |
2941267b | 9736 | |
eebd2e37 PB |
9737 | if (!tctx) |
9738 | return; | |
13bf43f5 PB |
9739 | node = xa_erase(&tctx->xa, index); |
9740 | if (!node) | |
2941267b | 9741 | return; |
0f212204 | 9742 | |
13bf43f5 PB |
9743 | WARN_ON_ONCE(current != node->task); |
9744 | WARN_ON_ONCE(list_empty(&node->ctx_node)); | |
9745 | ||
9746 | mutex_lock(&node->ctx->uring_lock); | |
9747 | list_del(&node->ctx_node); | |
9748 | mutex_unlock(&node->ctx->uring_lock); | |
9749 | ||
baf186c4 | 9750 | if (tctx->last == node->ctx) |
0f212204 | 9751 | tctx->last = NULL; |
13bf43f5 | 9752 | kfree(node); |
0f212204 JA |
9753 | } |
9754 | ||
c072481d | 9755 | static __cold void io_uring_clean_tctx(struct io_uring_task *tctx) |
de7f1d9e | 9756 | { |
ba5ef6dc | 9757 | struct io_wq *wq = tctx->io_wq; |
13bf43f5 | 9758 | struct io_tctx_node *node; |
de7f1d9e PB |
9759 | unsigned long index; |
9760 | ||
8bab4c09 | 9761 | xa_for_each(&tctx->xa, index, node) { |
eef51daa | 9762 | io_uring_del_tctx_node(index); |
8bab4c09 JA |
9763 | cond_resched(); |
9764 | } | |
b16ef427 ME |
9765 | if (wq) { |
9766 | /* | |
f6f9b278 | 9767 | * Must be after io_uring_del_tctx_node() (removes nodes under |
b16ef427 ME |
9768 | * uring_lock) to avoid race with io_uring_try_cancel_iowq(). |
9769 | */ | |
ba5ef6dc | 9770 | io_wq_put_and_exit(wq); |
dadebc35 | 9771 | tctx->io_wq = NULL; |
b16ef427 | 9772 | } |
de7f1d9e PB |
9773 | } |
9774 | ||
3f48cf18 | 9775 | static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked) |
521d6a73 | 9776 | { |
3f48cf18 PB |
9777 | if (tracked) |
9778 | return atomic_read(&tctx->inflight_tracked); | |
521d6a73 PB |
9779 | return percpu_counter_sum(&tctx->inflight); |
9780 | } | |
9781 | ||
c072481d | 9782 | static __cold void io_uring_drop_tctx_refs(struct task_struct *task) |
09899b19 PB |
9783 | { |
9784 | struct io_uring_task *tctx = task->io_uring; | |
9785 | unsigned int refs = tctx->cached_refs; | |
9786 | ||
e9dbe221 PB |
9787 | if (refs) { |
9788 | tctx->cached_refs = 0; | |
9789 | percpu_counter_sub(&tctx->inflight, refs); | |
9790 | put_task_struct_many(task, refs); | |
9791 | } | |
09899b19 PB |
9792 | } |
9793 | ||
78cc687b PB |
9794 | /* |
9795 | * Find any io_uring ctx that this task has registered or done IO on, and cancel | |
9796 | * requests. @sqd should be not-null IIF it's an SQPOLL thread cancellation. | |
9797 | */ | |
c072481d PB |
9798 | static __cold void io_uring_cancel_generic(bool cancel_all, |
9799 | struct io_sq_data *sqd) | |
0e9ddb39 | 9800 | { |
521d6a73 | 9801 | struct io_uring_task *tctx = current->io_uring; |
734551df | 9802 | struct io_ring_ctx *ctx; |
0e9ddb39 PB |
9803 | s64 inflight; |
9804 | DEFINE_WAIT(wait); | |
fdaf083c | 9805 | |
78cc687b PB |
9806 | WARN_ON_ONCE(sqd && sqd->thread != current); |
9807 | ||
6d042ffb PO |
9808 | if (!current->io_uring) |
9809 | return; | |
17a91051 PB |
9810 | if (tctx->io_wq) |
9811 | io_wq_exit_start(tctx->io_wq); | |
9812 | ||
0e9ddb39 PB |
9813 | atomic_inc(&tctx->in_idle); |
9814 | do { | |
e9dbe221 | 9815 | io_uring_drop_tctx_refs(current); |
0e9ddb39 | 9816 | /* read completions before cancelations */ |
78cc687b | 9817 | inflight = tctx_inflight(tctx, !cancel_all); |
0e9ddb39 PB |
9818 | if (!inflight) |
9819 | break; | |
fdaf083c | 9820 | |
78cc687b PB |
9821 | if (!sqd) { |
9822 | struct io_tctx_node *node; | |
9823 | unsigned long index; | |
0f212204 | 9824 | |
78cc687b PB |
9825 | xa_for_each(&tctx->xa, index, node) { |
9826 | /* sqpoll task will cancel all its requests */ | |
9827 | if (node->ctx->sq_data) | |
9828 | continue; | |
9829 | io_uring_try_cancel_requests(node->ctx, current, | |
9830 | cancel_all); | |
9831 | } | |
9832 | } else { | |
9833 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) | |
9834 | io_uring_try_cancel_requests(ctx, current, | |
9835 | cancel_all); | |
9836 | } | |
17a91051 | 9837 | |
0f212204 | 9838 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
e9dbe221 | 9839 | io_uring_drop_tctx_refs(current); |
0f212204 | 9840 | /* |
a1bb3cd5 PB |
9841 | * If we've seen completions, retry without waiting. This |
9842 | * avoids a race where a completion comes in before we did | |
9843 | * prepare_to_wait(). | |
0f212204 | 9844 | */ |
3dd0c97a | 9845 | if (inflight == tctx_inflight(tctx, !cancel_all)) |
a1bb3cd5 | 9846 | schedule(); |
f57555ed | 9847 | finish_wait(&tctx->wait, &wait); |
d8a6df10 | 9848 | } while (1); |
fdaf083c | 9849 | atomic_dec(&tctx->in_idle); |
de7f1d9e | 9850 | |
8452d4a6 | 9851 | io_uring_clean_tctx(tctx); |
3dd0c97a | 9852 | if (cancel_all) { |
3f48cf18 PB |
9853 | /* for exec all current's requests should be gone, kill tctx */ |
9854 | __io_uring_free(current); | |
9855 | } | |
44e728b8 PB |
9856 | } |
9857 | ||
f552a27a | 9858 | void __io_uring_cancel(bool cancel_all) |
78cc687b | 9859 | { |
f552a27a | 9860 | io_uring_cancel_generic(cancel_all, NULL); |
78cc687b PB |
9861 | } |
9862 | ||
6c5c240e RP |
9863 | static void *io_uring_validate_mmap_request(struct file *file, |
9864 | loff_t pgoff, size_t sz) | |
2b188cc1 | 9865 | { |
2b188cc1 | 9866 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 9867 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
9868 | struct page *page; |
9869 | void *ptr; | |
9870 | ||
9871 | switch (offset) { | |
9872 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
9873 | case IORING_OFF_CQ_RING: |
9874 | ptr = ctx->rings; | |
2b188cc1 JA |
9875 | break; |
9876 | case IORING_OFF_SQES: | |
9877 | ptr = ctx->sq_sqes; | |
9878 | break; | |
2b188cc1 | 9879 | default: |
6c5c240e | 9880 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
9881 | } |
9882 | ||
9883 | page = virt_to_head_page(ptr); | |
a50b854e | 9884 | if (sz > page_size(page)) |
6c5c240e RP |
9885 | return ERR_PTR(-EINVAL); |
9886 | ||
9887 | return ptr; | |
9888 | } | |
9889 | ||
9890 | #ifdef CONFIG_MMU | |
9891 | ||
c072481d | 9892 | static __cold int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
6c5c240e RP |
9893 | { |
9894 | size_t sz = vma->vm_end - vma->vm_start; | |
9895 | unsigned long pfn; | |
9896 | void *ptr; | |
9897 | ||
9898 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
9899 | if (IS_ERR(ptr)) | |
9900 | return PTR_ERR(ptr); | |
2b188cc1 JA |
9901 | |
9902 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
9903 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
9904 | } | |
9905 | ||
6c5c240e RP |
9906 | #else /* !CONFIG_MMU */ |
9907 | ||
9908 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9909 | { | |
9910 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
9911 | } | |
9912 | ||
9913 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
9914 | { | |
9915 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
9916 | } | |
9917 | ||
9918 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
9919 | unsigned long addr, unsigned long len, | |
9920 | unsigned long pgoff, unsigned long flags) | |
9921 | { | |
9922 | void *ptr; | |
9923 | ||
9924 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
9925 | if (IS_ERR(ptr)) | |
9926 | return PTR_ERR(ptr); | |
9927 | ||
9928 | return (unsigned long) ptr; | |
9929 | } | |
9930 | ||
9931 | #endif /* !CONFIG_MMU */ | |
9932 | ||
d9d05217 | 9933 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
90554200 JA |
9934 | { |
9935 | DEFINE_WAIT(wait); | |
9936 | ||
9937 | do { | |
9938 | if (!io_sqring_full(ctx)) | |
9939 | break; | |
90554200 JA |
9940 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
9941 | ||
9942 | if (!io_sqring_full(ctx)) | |
9943 | break; | |
90554200 JA |
9944 | schedule(); |
9945 | } while (!signal_pending(current)); | |
9946 | ||
9947 | finish_wait(&ctx->sqo_sq_wait, &wait); | |
5199328a | 9948 | return 0; |
90554200 JA |
9949 | } |
9950 | ||
c73ebb68 HX |
9951 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
9952 | struct __kernel_timespec __user **ts, | |
9953 | const sigset_t __user **sig) | |
9954 | { | |
9955 | struct io_uring_getevents_arg arg; | |
9956 | ||
9957 | /* | |
9958 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer | |
9959 | * is just a pointer to the sigset_t. | |
9960 | */ | |
9961 | if (!(flags & IORING_ENTER_EXT_ARG)) { | |
9962 | *sig = (const sigset_t __user *) argp; | |
9963 | *ts = NULL; | |
9964 | return 0; | |
9965 | } | |
9966 | ||
9967 | /* | |
9968 | * EXT_ARG is set - ensure we agree on the size of it and copy in our | |
9969 | * timespec and sigset_t pointers if good. | |
9970 | */ | |
9971 | if (*argsz != sizeof(arg)) | |
9972 | return -EINVAL; | |
9973 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
9974 | return -EFAULT; | |
9975 | *sig = u64_to_user_ptr(arg.sigmask); | |
9976 | *argsz = arg.sigmask_sz; | |
9977 | *ts = u64_to_user_ptr(arg.ts); | |
9978 | return 0; | |
9979 | } | |
9980 | ||
2b188cc1 | 9981 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
c73ebb68 HX |
9982 | u32, min_complete, u32, flags, const void __user *, argp, |
9983 | size_t, argsz) | |
2b188cc1 JA |
9984 | { |
9985 | struct io_ring_ctx *ctx; | |
2b188cc1 JA |
9986 | int submitted = 0; |
9987 | struct fd f; | |
33f993da | 9988 | long ret; |
2b188cc1 | 9989 | |
4c6e277c | 9990 | io_run_task_work(); |
b41e9852 | 9991 | |
33f993da PB |
9992 | if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
9993 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))) | |
2b188cc1 JA |
9994 | return -EINVAL; |
9995 | ||
9996 | f = fdget(fd); | |
33f993da | 9997 | if (unlikely(!f.file)) |
2b188cc1 JA |
9998 | return -EBADF; |
9999 | ||
10000 | ret = -EOPNOTSUPP; | |
33f993da | 10001 | if (unlikely(f.file->f_op != &io_uring_fops)) |
2b188cc1 JA |
10002 | goto out_fput; |
10003 | ||
10004 | ret = -ENXIO; | |
10005 | ctx = f.file->private_data; | |
33f993da | 10006 | if (unlikely(!percpu_ref_tryget(&ctx->refs))) |
2b188cc1 JA |
10007 | goto out_fput; |
10008 | ||
7e84e1c7 | 10009 | ret = -EBADFD; |
33f993da | 10010 | if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED)) |
7e84e1c7 SG |
10011 | goto out; |
10012 | ||
6c271ce2 JA |
10013 | /* |
10014 | * For SQ polling, the thread will do all submissions and completions. | |
10015 | * Just return the requested submit count, and wake the thread if | |
10016 | * we were asked to. | |
10017 | */ | |
b2a9eada | 10018 | ret = 0; |
6c271ce2 | 10019 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
90f67366 | 10020 | io_cqring_overflow_flush(ctx); |
89448c47 | 10021 | |
21f96522 JA |
10022 | if (unlikely(ctx->sq_data->thread == NULL)) { |
10023 | ret = -EOWNERDEAD; | |
04147488 | 10024 | goto out; |
21f96522 | 10025 | } |
6c271ce2 | 10026 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 10027 | wake_up(&ctx->sq_data->wait); |
d9d05217 PB |
10028 | if (flags & IORING_ENTER_SQ_WAIT) { |
10029 | ret = io_sqpoll_wait_sq(ctx); | |
10030 | if (ret) | |
10031 | goto out; | |
10032 | } | |
6c271ce2 | 10033 | submitted = to_submit; |
b2a9eada | 10034 | } else if (to_submit) { |
eef51daa | 10035 | ret = io_uring_add_tctx_node(ctx); |
0f212204 JA |
10036 | if (unlikely(ret)) |
10037 | goto out; | |
2b188cc1 | 10038 | mutex_lock(&ctx->uring_lock); |
0f212204 | 10039 | submitted = io_submit_sqes(ctx, to_submit); |
2b188cc1 | 10040 | mutex_unlock(&ctx->uring_lock); |
7c504e65 PB |
10041 | |
10042 | if (submitted != to_submit) | |
10043 | goto out; | |
2b188cc1 JA |
10044 | } |
10045 | if (flags & IORING_ENTER_GETEVENTS) { | |
c73ebb68 HX |
10046 | const sigset_t __user *sig; |
10047 | struct __kernel_timespec __user *ts; | |
10048 | ||
10049 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); | |
10050 | if (unlikely(ret)) | |
10051 | goto out; | |
10052 | ||
2b188cc1 JA |
10053 | min_complete = min(min_complete, ctx->cq_entries); |
10054 | ||
32b2244a XW |
10055 | /* |
10056 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
10057 | * space applications don't need to do io completion events | |
10058 | * polling again, they can rely on io_sq_thread to do polling | |
10059 | * work, which can reduce cpu usage and uring_lock contention. | |
10060 | */ | |
10061 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
10062 | !(ctx->flags & IORING_SETUP_SQPOLL)) { | |
7668b92a | 10063 | ret = io_iopoll_check(ctx, min_complete); |
def596e9 | 10064 | } else { |
c73ebb68 | 10065 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
def596e9 | 10066 | } |
2b188cc1 JA |
10067 | } |
10068 | ||
7c504e65 | 10069 | out: |
6805b32e | 10070 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
10071 | out_fput: |
10072 | fdput(f); | |
10073 | return submitted ? submitted : ret; | |
10074 | } | |
10075 | ||
bebdb65e | 10076 | #ifdef CONFIG_PROC_FS |
c072481d | 10077 | static __cold int io_uring_show_cred(struct seq_file *m, unsigned int id, |
61cf9370 | 10078 | const struct cred *cred) |
87ce955b | 10079 | { |
87ce955b JA |
10080 | struct user_namespace *uns = seq_user_ns(m); |
10081 | struct group_info *gi; | |
10082 | kernel_cap_t cap; | |
10083 | unsigned __capi; | |
10084 | int g; | |
10085 | ||
10086 | seq_printf(m, "%5d\n", id); | |
10087 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
10088 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
10089 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
10090 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
10091 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
10092 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
10093 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
10094 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
10095 | seq_puts(m, "\n\tGroups:\t"); | |
10096 | gi = cred->group_info; | |
10097 | for (g = 0; g < gi->ngroups; g++) { | |
10098 | seq_put_decimal_ull(m, g ? " " : "", | |
10099 | from_kgid_munged(uns, gi->gid[g])); | |
10100 | } | |
10101 | seq_puts(m, "\n\tCapEff:\t"); | |
10102 | cap = cred->cap_effective; | |
10103 | CAP_FOR_EACH_U32(__capi) | |
10104 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
10105 | seq_putc(m, '\n'); | |
10106 | return 0; | |
10107 | } | |
10108 | ||
c072481d PB |
10109 | static __cold void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, |
10110 | struct seq_file *m) | |
87ce955b | 10111 | { |
dbbe9c64 | 10112 | struct io_sq_data *sq = NULL; |
83f84356 HX |
10113 | struct io_overflow_cqe *ocqe; |
10114 | struct io_rings *r = ctx->rings; | |
10115 | unsigned int sq_mask = ctx->sq_entries - 1, cq_mask = ctx->cq_entries - 1; | |
83f84356 HX |
10116 | unsigned int sq_head = READ_ONCE(r->sq.head); |
10117 | unsigned int sq_tail = READ_ONCE(r->sq.tail); | |
10118 | unsigned int cq_head = READ_ONCE(r->cq.head); | |
10119 | unsigned int cq_tail = READ_ONCE(r->cq.tail); | |
f75d1183 | 10120 | unsigned int sq_entries, cq_entries; |
fad8e0de | 10121 | bool has_lock; |
83f84356 HX |
10122 | unsigned int i; |
10123 | ||
10124 | /* | |
10125 | * we may get imprecise sqe and cqe info if uring is actively running | |
10126 | * since we get cached_sq_head and cached_cq_tail without uring_lock | |
10127 | * and sq_tail and cq_head are changed by userspace. But it's ok since | |
10128 | * we usually use these info when it is stuck. | |
10129 | */ | |
f75d1183 JA |
10130 | seq_printf(m, "SqMask:\t\t0x%x\n", sq_mask); |
10131 | seq_printf(m, "SqHead:\t%u\n", sq_head); | |
10132 | seq_printf(m, "SqTail:\t%u\n", sq_tail); | |
10133 | seq_printf(m, "CachedSqHead:\t%u\n", ctx->cached_sq_head); | |
10134 | seq_printf(m, "CqMask:\t0x%x\n", cq_mask); | |
10135 | seq_printf(m, "CqHead:\t%u\n", cq_head); | |
10136 | seq_printf(m, "CqTail:\t%u\n", cq_tail); | |
10137 | seq_printf(m, "CachedCqTail:\t%u\n", ctx->cached_cq_tail); | |
10138 | seq_printf(m, "SQEs:\t%u\n", sq_tail - ctx->cached_sq_head); | |
10139 | sq_entries = min(sq_tail - sq_head, ctx->sq_entries); | |
10140 | for (i = 0; i < sq_entries; i++) { | |
10141 | unsigned int entry = i + sq_head; | |
10142 | unsigned int sq_idx = READ_ONCE(ctx->sq_array[entry & sq_mask]); | |
a1957780 | 10143 | struct io_uring_sqe *sqe; |
f75d1183 JA |
10144 | |
10145 | if (sq_idx > sq_mask) | |
10146 | continue; | |
10147 | sqe = &ctx->sq_sqes[sq_idx]; | |
10148 | seq_printf(m, "%5u: opcode:%d, fd:%d, flags:%x, user_data:%llu\n", | |
10149 | sq_idx, sqe->opcode, sqe->fd, sqe->flags, | |
10150 | sqe->user_data); | |
83f84356 | 10151 | } |
f75d1183 JA |
10152 | seq_printf(m, "CQEs:\t%u\n", cq_tail - cq_head); |
10153 | cq_entries = min(cq_tail - cq_head, ctx->cq_entries); | |
10154 | for (i = 0; i < cq_entries; i++) { | |
10155 | unsigned int entry = i + cq_head; | |
10156 | struct io_uring_cqe *cqe = &r->cqes[entry & cq_mask]; | |
83f84356 HX |
10157 | |
10158 | seq_printf(m, "%5u: user_data:%llu, res:%d, flag:%x\n", | |
f75d1183 JA |
10159 | entry & cq_mask, cqe->user_data, cqe->res, |
10160 | cqe->flags); | |
83f84356 | 10161 | } |
87ce955b | 10162 | |
fad8e0de JA |
10163 | /* |
10164 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, | |
10165 | * since fdinfo case grabs it in the opposite direction of normal use | |
10166 | * cases. If we fail to get the lock, we just don't iterate any | |
10167 | * structures that could be going away outside the io_uring mutex. | |
10168 | */ | |
10169 | has_lock = mutex_trylock(&ctx->uring_lock); | |
10170 | ||
5f3f26f9 | 10171 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) { |
dbbe9c64 | 10172 | sq = ctx->sq_data; |
5f3f26f9 JA |
10173 | if (!sq->thread) |
10174 | sq = NULL; | |
10175 | } | |
dbbe9c64 JQ |
10176 | |
10177 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); | |
10178 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); | |
87ce955b | 10179 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
fad8e0de | 10180 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
7b29f92d | 10181 | struct file *f = io_file_from_index(ctx, i); |
87ce955b | 10182 | |
87ce955b JA |
10183 | if (f) |
10184 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
10185 | else | |
10186 | seq_printf(m, "%5u: <none>\n", i); | |
10187 | } | |
10188 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
fad8e0de | 10189 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
41edf1a5 | 10190 | struct io_mapped_ubuf *buf = ctx->user_bufs[i]; |
4751f53d | 10191 | unsigned int len = buf->ubuf_end - buf->ubuf; |
87ce955b | 10192 | |
4751f53d | 10193 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len); |
87ce955b | 10194 | } |
61cf9370 MWO |
10195 | if (has_lock && !xa_empty(&ctx->personalities)) { |
10196 | unsigned long index; | |
10197 | const struct cred *cred; | |
10198 | ||
87ce955b | 10199 | seq_printf(m, "Personalities:\n"); |
61cf9370 MWO |
10200 | xa_for_each(&ctx->personalities, index, cred) |
10201 | io_uring_show_cred(m, index, cred); | |
87ce955b | 10202 | } |
83f84356 HX |
10203 | if (has_lock) |
10204 | mutex_unlock(&ctx->uring_lock); | |
10205 | ||
10206 | seq_puts(m, "PollList:\n"); | |
79ebeaee | 10207 | spin_lock(&ctx->completion_lock); |
d7718a9d JA |
10208 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
10209 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
10210 | struct io_kiocb *req; | |
10211 | ||
10212 | hlist_for_each_entry(req, list, hash_node) | |
10213 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
10214 | req->task->task_works != NULL); | |
10215 | } | |
83f84356 HX |
10216 | |
10217 | seq_puts(m, "CqOverflowList:\n"); | |
10218 | list_for_each_entry(ocqe, &ctx->cq_overflow_list, list) { | |
10219 | struct io_uring_cqe *cqe = &ocqe->cqe; | |
10220 | ||
10221 | seq_printf(m, " user_data=%llu, res=%d, flags=%x\n", | |
10222 | cqe->user_data, cqe->res, cqe->flags); | |
10223 | ||
10224 | } | |
10225 | ||
79ebeaee | 10226 | spin_unlock(&ctx->completion_lock); |
87ce955b JA |
10227 | } |
10228 | ||
c072481d | 10229 | static __cold void io_uring_show_fdinfo(struct seq_file *m, struct file *f) |
87ce955b JA |
10230 | { |
10231 | struct io_ring_ctx *ctx = f->private_data; | |
10232 | ||
10233 | if (percpu_ref_tryget(&ctx->refs)) { | |
10234 | __io_uring_show_fdinfo(ctx, m); | |
10235 | percpu_ref_put(&ctx->refs); | |
10236 | } | |
10237 | } | |
bebdb65e | 10238 | #endif |
87ce955b | 10239 | |
2b188cc1 JA |
10240 | static const struct file_operations io_uring_fops = { |
10241 | .release = io_uring_release, | |
10242 | .mmap = io_uring_mmap, | |
6c5c240e RP |
10243 | #ifndef CONFIG_MMU |
10244 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
10245 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
10246 | #endif | |
2b188cc1 | 10247 | .poll = io_uring_poll, |
bebdb65e | 10248 | #ifdef CONFIG_PROC_FS |
87ce955b | 10249 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 10250 | #endif |
2b188cc1 JA |
10251 | }; |
10252 | ||
c072481d PB |
10253 | static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
10254 | struct io_uring_params *p) | |
2b188cc1 | 10255 | { |
75b28aff HV |
10256 | struct io_rings *rings; |
10257 | size_t size, sq_array_offset; | |
2b188cc1 | 10258 | |
bd740481 JA |
10259 | /* make sure these are sane, as we already accounted them */ |
10260 | ctx->sq_entries = p->sq_entries; | |
10261 | ctx->cq_entries = p->cq_entries; | |
10262 | ||
75b28aff HV |
10263 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
10264 | if (size == SIZE_MAX) | |
10265 | return -EOVERFLOW; | |
10266 | ||
10267 | rings = io_mem_alloc(size); | |
10268 | if (!rings) | |
2b188cc1 JA |
10269 | return -ENOMEM; |
10270 | ||
75b28aff HV |
10271 | ctx->rings = rings; |
10272 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
10273 | rings->sq_ring_mask = p->sq_entries - 1; | |
10274 | rings->cq_ring_mask = p->cq_entries - 1; | |
10275 | rings->sq_ring_entries = p->sq_entries; | |
10276 | rings->cq_ring_entries = p->cq_entries; | |
2b188cc1 JA |
10277 | |
10278 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
10279 | if (size == SIZE_MAX) { |
10280 | io_mem_free(ctx->rings); | |
10281 | ctx->rings = NULL; | |
2b188cc1 | 10282 | return -EOVERFLOW; |
eb065d30 | 10283 | } |
2b188cc1 JA |
10284 | |
10285 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
10286 | if (!ctx->sq_sqes) { |
10287 | io_mem_free(ctx->rings); | |
10288 | ctx->rings = NULL; | |
2b188cc1 | 10289 | return -ENOMEM; |
eb065d30 | 10290 | } |
2b188cc1 | 10291 | |
2b188cc1 JA |
10292 | return 0; |
10293 | } | |
10294 | ||
9faadcc8 PB |
10295 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
10296 | { | |
10297 | int ret, fd; | |
10298 | ||
10299 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
10300 | if (fd < 0) | |
10301 | return fd; | |
10302 | ||
eef51daa | 10303 | ret = io_uring_add_tctx_node(ctx); |
9faadcc8 PB |
10304 | if (ret) { |
10305 | put_unused_fd(fd); | |
10306 | return ret; | |
10307 | } | |
10308 | fd_install(fd, file); | |
10309 | return fd; | |
10310 | } | |
10311 | ||
2b188cc1 JA |
10312 | /* |
10313 | * Allocate an anonymous fd, this is what constitutes the application | |
10314 | * visible backing of an io_uring instance. The application mmaps this | |
10315 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
10316 | * we have to tie this fd to a socket for file garbage collection purposes. | |
10317 | */ | |
9faadcc8 | 10318 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
2b188cc1 JA |
10319 | { |
10320 | struct file *file; | |
9faadcc8 | 10321 | #if defined(CONFIG_UNIX) |
2b188cc1 JA |
10322 | int ret; |
10323 | ||
2b188cc1 JA |
10324 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
10325 | &ctx->ring_sock); | |
10326 | if (ret) | |
9faadcc8 | 10327 | return ERR_PTR(ret); |
2b188cc1 JA |
10328 | #endif |
10329 | ||
91a9ab7c PM |
10330 | file = anon_inode_getfile_secure("[io_uring]", &io_uring_fops, ctx, |
10331 | O_RDWR | O_CLOEXEC, NULL); | |
2b188cc1 | 10332 | #if defined(CONFIG_UNIX) |
9faadcc8 PB |
10333 | if (IS_ERR(file)) { |
10334 | sock_release(ctx->ring_sock); | |
10335 | ctx->ring_sock = NULL; | |
10336 | } else { | |
10337 | ctx->ring_sock->file = file; | |
0f212204 | 10338 | } |
2b188cc1 | 10339 | #endif |
9faadcc8 | 10340 | return file; |
2b188cc1 JA |
10341 | } |
10342 | ||
c072481d PB |
10343 | static __cold int io_uring_create(unsigned entries, struct io_uring_params *p, |
10344 | struct io_uring_params __user *params) | |
2b188cc1 | 10345 | { |
2b188cc1 | 10346 | struct io_ring_ctx *ctx; |
9faadcc8 | 10347 | struct file *file; |
2b188cc1 JA |
10348 | int ret; |
10349 | ||
8110c1a6 | 10350 | if (!entries) |
2b188cc1 | 10351 | return -EINVAL; |
8110c1a6 JA |
10352 | if (entries > IORING_MAX_ENTRIES) { |
10353 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
10354 | return -EINVAL; | |
10355 | entries = IORING_MAX_ENTRIES; | |
10356 | } | |
2b188cc1 JA |
10357 | |
10358 | /* | |
10359 | * Use twice as many entries for the CQ ring. It's possible for the | |
10360 | * application to drive a higher depth than the size of the SQ ring, | |
10361 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
10362 | * some flexibility in overcommitting a bit. If the application has |
10363 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
10364 | * of CQ ring entries manually. | |
2b188cc1 JA |
10365 | */ |
10366 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
10367 | if (p->flags & IORING_SETUP_CQSIZE) { |
10368 | /* | |
10369 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
10370 | * to a power-of-two, if it isn't already. We do NOT impose | |
10371 | * any cq vs sq ring sizing. | |
10372 | */ | |
eb2667b3 | 10373 | if (!p->cq_entries) |
33a107f0 | 10374 | return -EINVAL; |
8110c1a6 JA |
10375 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
10376 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
10377 | return -EINVAL; | |
10378 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
10379 | } | |
eb2667b3 JQ |
10380 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
10381 | if (p->cq_entries < p->sq_entries) | |
10382 | return -EINVAL; | |
33a107f0 JA |
10383 | } else { |
10384 | p->cq_entries = 2 * p->sq_entries; | |
10385 | } | |
2b188cc1 | 10386 | |
2b188cc1 | 10387 | ctx = io_ring_ctx_alloc(p); |
62e398be | 10388 | if (!ctx) |
2b188cc1 | 10389 | return -ENOMEM; |
2b188cc1 | 10390 | ctx->compat = in_compat_syscall(); |
62e398be JA |
10391 | if (!capable(CAP_IPC_LOCK)) |
10392 | ctx->user = get_uid(current_user()); | |
2aede0e4 JA |
10393 | |
10394 | /* | |
10395 | * This is just grabbed for accounting purposes. When a process exits, | |
10396 | * the mm is exited and dropped before the files, hence we need to hang | |
10397 | * on to this mm purely for the purposes of being able to unaccount | |
10398 | * memory (locked/pinned vm). It's not used for anything else. | |
10399 | */ | |
6b7898eb | 10400 | mmgrab(current->mm); |
2aede0e4 | 10401 | ctx->mm_account = current->mm; |
6b7898eb | 10402 | |
2b188cc1 JA |
10403 | ret = io_allocate_scq_urings(ctx, p); |
10404 | if (ret) | |
10405 | goto err; | |
10406 | ||
7e84e1c7 | 10407 | ret = io_sq_offload_create(ctx, p); |
2b188cc1 JA |
10408 | if (ret) |
10409 | goto err; | |
eae071c9 | 10410 | /* always set a rsrc node */ |
47b228ce PB |
10411 | ret = io_rsrc_node_switch_start(ctx); |
10412 | if (ret) | |
10413 | goto err; | |
eae071c9 | 10414 | io_rsrc_node_switch(ctx, NULL); |
2b188cc1 | 10415 | |
2b188cc1 | 10416 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
10417 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
10418 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
10419 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
10420 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
10421 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
10422 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
10423 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
10424 | |
10425 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
10426 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
10427 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
10428 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
10429 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
10430 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
10431 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 10432 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 10433 | |
7f13657d XW |
10434 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
10435 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 | 10436 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
c73ebb68 | 10437 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
9690557e PB |
10438 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS | |
10439 | IORING_FEAT_RSRC_TAGS; | |
7f13657d XW |
10440 | |
10441 | if (copy_to_user(params, p, sizeof(*p))) { | |
10442 | ret = -EFAULT; | |
10443 | goto err; | |
10444 | } | |
d1719f70 | 10445 | |
9faadcc8 PB |
10446 | file = io_uring_get_file(ctx); |
10447 | if (IS_ERR(file)) { | |
10448 | ret = PTR_ERR(file); | |
10449 | goto err; | |
10450 | } | |
10451 | ||
044c1ab3 JA |
10452 | /* |
10453 | * Install ring fd as the very last thing, so we don't risk someone | |
10454 | * having closed it before we finish setup | |
10455 | */ | |
9faadcc8 PB |
10456 | ret = io_uring_install_fd(ctx, file); |
10457 | if (ret < 0) { | |
10458 | /* fput will clean it up */ | |
10459 | fput(file); | |
10460 | return ret; | |
10461 | } | |
044c1ab3 | 10462 | |
c826bd7a | 10463 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
10464 | return ret; |
10465 | err: | |
10466 | io_ring_ctx_wait_and_kill(ctx); | |
10467 | return ret; | |
10468 | } | |
10469 | ||
10470 | /* | |
10471 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
10472 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
10473 | * params structure passed in. | |
10474 | */ | |
10475 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
10476 | { | |
10477 | struct io_uring_params p; | |
2b188cc1 JA |
10478 | int i; |
10479 | ||
10480 | if (copy_from_user(&p, params, sizeof(p))) | |
10481 | return -EFAULT; | |
10482 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
10483 | if (p.resv[i]) | |
10484 | return -EINVAL; | |
10485 | } | |
10486 | ||
6c271ce2 | 10487 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 10488 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 SG |
10489 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
10490 | IORING_SETUP_R_DISABLED)) | |
2b188cc1 JA |
10491 | return -EINVAL; |
10492 | ||
7f13657d | 10493 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
10494 | } |
10495 | ||
10496 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
10497 | struct io_uring_params __user *, params) | |
10498 | { | |
10499 | return io_uring_setup(entries, params); | |
10500 | } | |
10501 | ||
c072481d PB |
10502 | static __cold int io_probe(struct io_ring_ctx *ctx, void __user *arg, |
10503 | unsigned nr_args) | |
66f4af93 JA |
10504 | { |
10505 | struct io_uring_probe *p; | |
10506 | size_t size; | |
10507 | int i, ret; | |
10508 | ||
10509 | size = struct_size(p, ops, nr_args); | |
10510 | if (size == SIZE_MAX) | |
10511 | return -EOVERFLOW; | |
10512 | p = kzalloc(size, GFP_KERNEL); | |
10513 | if (!p) | |
10514 | return -ENOMEM; | |
10515 | ||
10516 | ret = -EFAULT; | |
10517 | if (copy_from_user(p, arg, size)) | |
10518 | goto out; | |
10519 | ret = -EINVAL; | |
10520 | if (memchr_inv(p, 0, size)) | |
10521 | goto out; | |
10522 | ||
10523 | p->last_op = IORING_OP_LAST - 1; | |
10524 | if (nr_args > IORING_OP_LAST) | |
10525 | nr_args = IORING_OP_LAST; | |
10526 | ||
10527 | for (i = 0; i < nr_args; i++) { | |
10528 | p->ops[i].op = i; | |
10529 | if (!io_op_defs[i].not_supported) | |
10530 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
10531 | } | |
10532 | p->ops_len = i; | |
10533 | ||
10534 | ret = 0; | |
10535 | if (copy_to_user(arg, p, size)) | |
10536 | ret = -EFAULT; | |
10537 | out: | |
10538 | kfree(p); | |
10539 | return ret; | |
10540 | } | |
10541 | ||
071698e1 JA |
10542 | static int io_register_personality(struct io_ring_ctx *ctx) |
10543 | { | |
4379bf8b | 10544 | const struct cred *creds; |
61cf9370 | 10545 | u32 id; |
1e6fa521 | 10546 | int ret; |
071698e1 | 10547 | |
4379bf8b | 10548 | creds = get_current_cred(); |
1e6fa521 | 10549 | |
61cf9370 MWO |
10550 | ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds, |
10551 | XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL); | |
a30f895a JA |
10552 | if (ret < 0) { |
10553 | put_cred(creds); | |
10554 | return ret; | |
10555 | } | |
10556 | return id; | |
071698e1 JA |
10557 | } |
10558 | ||
c072481d PB |
10559 | static __cold int io_register_restrictions(struct io_ring_ctx *ctx, |
10560 | void __user *arg, unsigned int nr_args) | |
21b55dbc SG |
10561 | { |
10562 | struct io_uring_restriction *res; | |
10563 | size_t size; | |
10564 | int i, ret; | |
10565 | ||
7e84e1c7 SG |
10566 | /* Restrictions allowed only if rings started disabled */ |
10567 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
10568 | return -EBADFD; | |
10569 | ||
21b55dbc | 10570 | /* We allow only a single restrictions registration */ |
7e84e1c7 | 10571 | if (ctx->restrictions.registered) |
21b55dbc SG |
10572 | return -EBUSY; |
10573 | ||
10574 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) | |
10575 | return -EINVAL; | |
10576 | ||
10577 | size = array_size(nr_args, sizeof(*res)); | |
10578 | if (size == SIZE_MAX) | |
10579 | return -EOVERFLOW; | |
10580 | ||
10581 | res = memdup_user(arg, size); | |
10582 | if (IS_ERR(res)) | |
10583 | return PTR_ERR(res); | |
10584 | ||
10585 | ret = 0; | |
10586 | ||
10587 | for (i = 0; i < nr_args; i++) { | |
10588 | switch (res[i].opcode) { | |
10589 | case IORING_RESTRICTION_REGISTER_OP: | |
10590 | if (res[i].register_op >= IORING_REGISTER_LAST) { | |
10591 | ret = -EINVAL; | |
10592 | goto out; | |
10593 | } | |
10594 | ||
10595 | __set_bit(res[i].register_op, | |
10596 | ctx->restrictions.register_op); | |
10597 | break; | |
10598 | case IORING_RESTRICTION_SQE_OP: | |
10599 | if (res[i].sqe_op >= IORING_OP_LAST) { | |
10600 | ret = -EINVAL; | |
10601 | goto out; | |
10602 | } | |
10603 | ||
10604 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); | |
10605 | break; | |
10606 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: | |
10607 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; | |
10608 | break; | |
10609 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: | |
10610 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; | |
10611 | break; | |
10612 | default: | |
10613 | ret = -EINVAL; | |
10614 | goto out; | |
10615 | } | |
10616 | } | |
10617 | ||
10618 | out: | |
10619 | /* Reset all restrictions if an error happened */ | |
10620 | if (ret != 0) | |
10621 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); | |
10622 | else | |
7e84e1c7 | 10623 | ctx->restrictions.registered = true; |
21b55dbc SG |
10624 | |
10625 | kfree(res); | |
10626 | return ret; | |
10627 | } | |
10628 | ||
7e84e1c7 SG |
10629 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
10630 | { | |
10631 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
10632 | return -EBADFD; | |
10633 | ||
10634 | if (ctx->restrictions.registered) | |
10635 | ctx->restricted = 1; | |
10636 | ||
0298ef96 PB |
10637 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
10638 | if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait)) | |
10639 | wake_up(&ctx->sq_data->wait); | |
7e84e1c7 SG |
10640 | return 0; |
10641 | } | |
10642 | ||
fdecb662 | 10643 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 10644 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 PB |
10645 | unsigned nr_args) |
10646 | { | |
10647 | __u32 tmp; | |
10648 | int err; | |
10649 | ||
c3bdad02 PB |
10650 | if (up->resv) |
10651 | return -EINVAL; | |
98f0b3b4 PB |
10652 | if (check_add_overflow(up->offset, nr_args, &tmp)) |
10653 | return -EOVERFLOW; | |
10654 | err = io_rsrc_node_switch_start(ctx); | |
10655 | if (err) | |
10656 | return err; | |
10657 | ||
fdecb662 PB |
10658 | switch (type) { |
10659 | case IORING_RSRC_FILE: | |
98f0b3b4 | 10660 | return __io_sqe_files_update(ctx, up, nr_args); |
634d00df PB |
10661 | case IORING_RSRC_BUFFER: |
10662 | return __io_sqe_buffers_update(ctx, up, nr_args); | |
98f0b3b4 PB |
10663 | } |
10664 | return -EINVAL; | |
10665 | } | |
10666 | ||
c3bdad02 PB |
10667 | static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg, |
10668 | unsigned nr_args) | |
98f0b3b4 | 10669 | { |
c3bdad02 | 10670 | struct io_uring_rsrc_update2 up; |
98f0b3b4 PB |
10671 | |
10672 | if (!nr_args) | |
10673 | return -EINVAL; | |
c3bdad02 PB |
10674 | memset(&up, 0, sizeof(up)); |
10675 | if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update))) | |
10676 | return -EFAULT; | |
10677 | return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args); | |
10678 | } | |
10679 | ||
10680 | static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg, | |
992da01a | 10681 | unsigned size, unsigned type) |
c3bdad02 PB |
10682 | { |
10683 | struct io_uring_rsrc_update2 up; | |
10684 | ||
10685 | if (size != sizeof(up)) | |
10686 | return -EINVAL; | |
98f0b3b4 PB |
10687 | if (copy_from_user(&up, arg, sizeof(up))) |
10688 | return -EFAULT; | |
992da01a | 10689 | if (!up.nr || up.resv) |
98f0b3b4 | 10690 | return -EINVAL; |
992da01a | 10691 | return __io_register_rsrc_update(ctx, type, &up, up.nr); |
98f0b3b4 PB |
10692 | } |
10693 | ||
c072481d | 10694 | static __cold int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg, |
992da01a | 10695 | unsigned int size, unsigned int type) |
792e3582 PB |
10696 | { |
10697 | struct io_uring_rsrc_register rr; | |
10698 | ||
10699 | /* keep it extendible */ | |
10700 | if (size != sizeof(rr)) | |
10701 | return -EINVAL; | |
10702 | ||
10703 | memset(&rr, 0, sizeof(rr)); | |
10704 | if (copy_from_user(&rr, arg, size)) | |
10705 | return -EFAULT; | |
992da01a | 10706 | if (!rr.nr || rr.resv || rr.resv2) |
792e3582 PB |
10707 | return -EINVAL; |
10708 | ||
992da01a | 10709 | switch (type) { |
792e3582 PB |
10710 | case IORING_RSRC_FILE: |
10711 | return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data), | |
10712 | rr.nr, u64_to_user_ptr(rr.tags)); | |
634d00df PB |
10713 | case IORING_RSRC_BUFFER: |
10714 | return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data), | |
10715 | rr.nr, u64_to_user_ptr(rr.tags)); | |
792e3582 PB |
10716 | } |
10717 | return -EINVAL; | |
10718 | } | |
10719 | ||
c072481d PB |
10720 | static __cold int io_register_iowq_aff(struct io_ring_ctx *ctx, |
10721 | void __user *arg, unsigned len) | |
fe76421d JA |
10722 | { |
10723 | struct io_uring_task *tctx = current->io_uring; | |
10724 | cpumask_var_t new_mask; | |
10725 | int ret; | |
10726 | ||
10727 | if (!tctx || !tctx->io_wq) | |
10728 | return -EINVAL; | |
10729 | ||
10730 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) | |
10731 | return -ENOMEM; | |
10732 | ||
10733 | cpumask_clear(new_mask); | |
10734 | if (len > cpumask_size()) | |
10735 | len = cpumask_size(); | |
10736 | ||
10737 | if (copy_from_user(new_mask, arg, len)) { | |
10738 | free_cpumask_var(new_mask); | |
10739 | return -EFAULT; | |
10740 | } | |
10741 | ||
10742 | ret = io_wq_cpu_affinity(tctx->io_wq, new_mask); | |
10743 | free_cpumask_var(new_mask); | |
10744 | return ret; | |
10745 | } | |
10746 | ||
c072481d | 10747 | static __cold int io_unregister_iowq_aff(struct io_ring_ctx *ctx) |
fe76421d JA |
10748 | { |
10749 | struct io_uring_task *tctx = current->io_uring; | |
10750 | ||
10751 | if (!tctx || !tctx->io_wq) | |
10752 | return -EINVAL; | |
10753 | ||
10754 | return io_wq_cpu_affinity(tctx->io_wq, NULL); | |
10755 | } | |
10756 | ||
c072481d PB |
10757 | static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx, |
10758 | void __user *arg) | |
b22fa62a | 10759 | __must_hold(&ctx->uring_lock) |
2e480058 | 10760 | { |
b22fa62a | 10761 | struct io_tctx_node *node; |
fa84693b JA |
10762 | struct io_uring_task *tctx = NULL; |
10763 | struct io_sq_data *sqd = NULL; | |
2e480058 JA |
10764 | __u32 new_count[2]; |
10765 | int i, ret; | |
10766 | ||
2e480058 JA |
10767 | if (copy_from_user(new_count, arg, sizeof(new_count))) |
10768 | return -EFAULT; | |
10769 | for (i = 0; i < ARRAY_SIZE(new_count); i++) | |
10770 | if (new_count[i] > INT_MAX) | |
10771 | return -EINVAL; | |
10772 | ||
fa84693b JA |
10773 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
10774 | sqd = ctx->sq_data; | |
10775 | if (sqd) { | |
009ad9f0 JA |
10776 | /* |
10777 | * Observe the correct sqd->lock -> ctx->uring_lock | |
10778 | * ordering. Fine to drop uring_lock here, we hold | |
10779 | * a ref to the ctx. | |
10780 | */ | |
41d3a6bd | 10781 | refcount_inc(&sqd->refs); |
009ad9f0 | 10782 | mutex_unlock(&ctx->uring_lock); |
fa84693b | 10783 | mutex_lock(&sqd->lock); |
009ad9f0 | 10784 | mutex_lock(&ctx->uring_lock); |
41d3a6bd JA |
10785 | if (sqd->thread) |
10786 | tctx = sqd->thread->io_uring; | |
fa84693b JA |
10787 | } |
10788 | } else { | |
10789 | tctx = current->io_uring; | |
10790 | } | |
10791 | ||
e139a1ec | 10792 | BUILD_BUG_ON(sizeof(new_count) != sizeof(ctx->iowq_limits)); |
fa84693b | 10793 | |
bad119b9 PB |
10794 | for (i = 0; i < ARRAY_SIZE(new_count); i++) |
10795 | if (new_count[i]) | |
10796 | ctx->iowq_limits[i] = new_count[i]; | |
e139a1ec PB |
10797 | ctx->iowq_limits_set = true; |
10798 | ||
e139a1ec PB |
10799 | if (tctx && tctx->io_wq) { |
10800 | ret = io_wq_max_workers(tctx->io_wq, new_count); | |
10801 | if (ret) | |
10802 | goto err; | |
10803 | } else { | |
10804 | memset(new_count, 0, sizeof(new_count)); | |
10805 | } | |
fa84693b | 10806 | |
41d3a6bd | 10807 | if (sqd) { |
fa84693b | 10808 | mutex_unlock(&sqd->lock); |
41d3a6bd JA |
10809 | io_put_sq_data(sqd); |
10810 | } | |
2e480058 JA |
10811 | |
10812 | if (copy_to_user(arg, new_count, sizeof(new_count))) | |
10813 | return -EFAULT; | |
10814 | ||
b22fa62a PB |
10815 | /* that's it for SQPOLL, only the SQPOLL task creates requests */ |
10816 | if (sqd) | |
10817 | return 0; | |
10818 | ||
10819 | /* now propagate the restriction to all registered users */ | |
10820 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
10821 | struct io_uring_task *tctx = node->task->io_uring; | |
10822 | ||
10823 | if (WARN_ON_ONCE(!tctx->io_wq)) | |
10824 | continue; | |
10825 | ||
10826 | for (i = 0; i < ARRAY_SIZE(new_count); i++) | |
10827 | new_count[i] = ctx->iowq_limits[i]; | |
10828 | /* ignore errors, it always returns zero anyway */ | |
10829 | (void)io_wq_max_workers(tctx->io_wq, new_count); | |
10830 | } | |
2e480058 | 10831 | return 0; |
fa84693b | 10832 | err: |
41d3a6bd | 10833 | if (sqd) { |
fa84693b | 10834 | mutex_unlock(&sqd->lock); |
41d3a6bd JA |
10835 | io_put_sq_data(sqd); |
10836 | } | |
fa84693b | 10837 | return ret; |
2e480058 JA |
10838 | } |
10839 | ||
071698e1 JA |
10840 | static bool io_register_op_must_quiesce(int op) |
10841 | { | |
10842 | switch (op) { | |
bd54b6fe BM |
10843 | case IORING_REGISTER_BUFFERS: |
10844 | case IORING_UNREGISTER_BUFFERS: | |
f4f7d21c | 10845 | case IORING_REGISTER_FILES: |
071698e1 JA |
10846 | case IORING_UNREGISTER_FILES: |
10847 | case IORING_REGISTER_FILES_UPDATE: | |
10848 | case IORING_REGISTER_PROBE: | |
10849 | case IORING_REGISTER_PERSONALITY: | |
10850 | case IORING_UNREGISTER_PERSONALITY: | |
992da01a PB |
10851 | case IORING_REGISTER_FILES2: |
10852 | case IORING_REGISTER_FILES_UPDATE2: | |
10853 | case IORING_REGISTER_BUFFERS2: | |
10854 | case IORING_REGISTER_BUFFERS_UPDATE: | |
fe76421d JA |
10855 | case IORING_REGISTER_IOWQ_AFF: |
10856 | case IORING_UNREGISTER_IOWQ_AFF: | |
2e480058 | 10857 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
071698e1 JA |
10858 | return false; |
10859 | default: | |
10860 | return true; | |
10861 | } | |
10862 | } | |
10863 | ||
c072481d | 10864 | static __cold int io_ctx_quiesce(struct io_ring_ctx *ctx) |
e73c5c7c PB |
10865 | { |
10866 | long ret; | |
10867 | ||
10868 | percpu_ref_kill(&ctx->refs); | |
10869 | ||
10870 | /* | |
10871 | * Drop uring mutex before waiting for references to exit. If another | |
10872 | * thread is currently inside io_uring_enter() it might need to grab the | |
10873 | * uring_lock to make progress. If we hold it here across the drain | |
10874 | * wait, then we can deadlock. It's safe to drop the mutex here, since | |
10875 | * no new references will come in after we've killed the percpu ref. | |
10876 | */ | |
10877 | mutex_unlock(&ctx->uring_lock); | |
10878 | do { | |
37f0e767 PB |
10879 | ret = wait_for_completion_interruptible_timeout(&ctx->ref_comp, HZ); |
10880 | if (ret) { | |
10881 | ret = min(0L, ret); | |
e73c5c7c | 10882 | break; |
37f0e767 PB |
10883 | } |
10884 | ||
e73c5c7c | 10885 | ret = io_run_task_work_sig(); |
37f0e767 | 10886 | io_req_caches_free(ctx); |
e73c5c7c PB |
10887 | } while (ret >= 0); |
10888 | mutex_lock(&ctx->uring_lock); | |
10889 | ||
10890 | if (ret) | |
10891 | io_refs_resurrect(&ctx->refs, &ctx->ref_comp); | |
10892 | return ret; | |
10893 | } | |
10894 | ||
edafccee JA |
10895 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
10896 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
10897 | __releases(ctx->uring_lock) |
10898 | __acquires(ctx->uring_lock) | |
edafccee JA |
10899 | { |
10900 | int ret; | |
10901 | ||
35fa71a0 JA |
10902 | /* |
10903 | * We're inside the ring mutex, if the ref is already dying, then | |
10904 | * someone else killed the ctx or is already going through | |
10905 | * io_uring_register(). | |
10906 | */ | |
10907 | if (percpu_ref_is_dying(&ctx->refs)) | |
10908 | return -ENXIO; | |
10909 | ||
75c4021a PB |
10910 | if (ctx->restricted) { |
10911 | if (opcode >= IORING_REGISTER_LAST) | |
10912 | return -EINVAL; | |
10913 | opcode = array_index_nospec(opcode, IORING_REGISTER_LAST); | |
10914 | if (!test_bit(opcode, ctx->restrictions.register_op)) | |
10915 | return -EACCES; | |
10916 | } | |
10917 | ||
071698e1 | 10918 | if (io_register_op_must_quiesce(opcode)) { |
e73c5c7c PB |
10919 | ret = io_ctx_quiesce(ctx); |
10920 | if (ret) | |
f70865db | 10921 | return ret; |
05f3fb3c | 10922 | } |
edafccee JA |
10923 | |
10924 | switch (opcode) { | |
10925 | case IORING_REGISTER_BUFFERS: | |
634d00df | 10926 | ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL); |
edafccee JA |
10927 | break; |
10928 | case IORING_UNREGISTER_BUFFERS: | |
10929 | ret = -EINVAL; | |
10930 | if (arg || nr_args) | |
10931 | break; | |
0a96bbe4 | 10932 | ret = io_sqe_buffers_unregister(ctx); |
edafccee | 10933 | break; |
6b06314c | 10934 | case IORING_REGISTER_FILES: |
792e3582 | 10935 | ret = io_sqe_files_register(ctx, arg, nr_args, NULL); |
6b06314c JA |
10936 | break; |
10937 | case IORING_UNREGISTER_FILES: | |
10938 | ret = -EINVAL; | |
10939 | if (arg || nr_args) | |
10940 | break; | |
10941 | ret = io_sqe_files_unregister(ctx); | |
10942 | break; | |
c3a31e60 | 10943 | case IORING_REGISTER_FILES_UPDATE: |
c3bdad02 | 10944 | ret = io_register_files_update(ctx, arg, nr_args); |
c3a31e60 | 10945 | break; |
9b402849 | 10946 | case IORING_REGISTER_EVENTFD: |
f2842ab5 | 10947 | case IORING_REGISTER_EVENTFD_ASYNC: |
9b402849 JA |
10948 | ret = -EINVAL; |
10949 | if (nr_args != 1) | |
10950 | break; | |
10951 | ret = io_eventfd_register(ctx, arg); | |
f2842ab5 JA |
10952 | if (ret) |
10953 | break; | |
10954 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) | |
10955 | ctx->eventfd_async = 1; | |
10956 | else | |
10957 | ctx->eventfd_async = 0; | |
9b402849 JA |
10958 | break; |
10959 | case IORING_UNREGISTER_EVENTFD: | |
10960 | ret = -EINVAL; | |
10961 | if (arg || nr_args) | |
10962 | break; | |
10963 | ret = io_eventfd_unregister(ctx); | |
10964 | break; | |
66f4af93 JA |
10965 | case IORING_REGISTER_PROBE: |
10966 | ret = -EINVAL; | |
10967 | if (!arg || nr_args > 256) | |
10968 | break; | |
10969 | ret = io_probe(ctx, arg, nr_args); | |
10970 | break; | |
071698e1 JA |
10971 | case IORING_REGISTER_PERSONALITY: |
10972 | ret = -EINVAL; | |
10973 | if (arg || nr_args) | |
10974 | break; | |
10975 | ret = io_register_personality(ctx); | |
10976 | break; | |
10977 | case IORING_UNREGISTER_PERSONALITY: | |
10978 | ret = -EINVAL; | |
10979 | if (arg) | |
10980 | break; | |
10981 | ret = io_unregister_personality(ctx, nr_args); | |
10982 | break; | |
7e84e1c7 SG |
10983 | case IORING_REGISTER_ENABLE_RINGS: |
10984 | ret = -EINVAL; | |
10985 | if (arg || nr_args) | |
10986 | break; | |
10987 | ret = io_register_enable_rings(ctx); | |
10988 | break; | |
21b55dbc SG |
10989 | case IORING_REGISTER_RESTRICTIONS: |
10990 | ret = io_register_restrictions(ctx, arg, nr_args); | |
10991 | break; | |
992da01a PB |
10992 | case IORING_REGISTER_FILES2: |
10993 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE); | |
10994 | break; | |
10995 | case IORING_REGISTER_FILES_UPDATE2: | |
10996 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
10997 | IORING_RSRC_FILE); | |
10998 | break; | |
10999 | case IORING_REGISTER_BUFFERS2: | |
11000 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER); | |
792e3582 | 11001 | break; |
992da01a PB |
11002 | case IORING_REGISTER_BUFFERS_UPDATE: |
11003 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
11004 | IORING_RSRC_BUFFER); | |
c3bdad02 | 11005 | break; |
fe76421d JA |
11006 | case IORING_REGISTER_IOWQ_AFF: |
11007 | ret = -EINVAL; | |
11008 | if (!arg || !nr_args) | |
11009 | break; | |
11010 | ret = io_register_iowq_aff(ctx, arg, nr_args); | |
11011 | break; | |
11012 | case IORING_UNREGISTER_IOWQ_AFF: | |
11013 | ret = -EINVAL; | |
11014 | if (arg || nr_args) | |
11015 | break; | |
11016 | ret = io_unregister_iowq_aff(ctx); | |
11017 | break; | |
2e480058 JA |
11018 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
11019 | ret = -EINVAL; | |
11020 | if (!arg || nr_args != 2) | |
11021 | break; | |
11022 | ret = io_register_iowq_max_workers(ctx, arg); | |
11023 | break; | |
edafccee JA |
11024 | default: |
11025 | ret = -EINVAL; | |
11026 | break; | |
11027 | } | |
11028 | ||
071698e1 | 11029 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 11030 | /* bring the ctx back to life */ |
05f3fb3c | 11031 | percpu_ref_reinit(&ctx->refs); |
0f158b4c | 11032 | reinit_completion(&ctx->ref_comp); |
05f3fb3c | 11033 | } |
edafccee JA |
11034 | return ret; |
11035 | } | |
11036 | ||
11037 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
11038 | void __user *, arg, unsigned int, nr_args) | |
11039 | { | |
11040 | struct io_ring_ctx *ctx; | |
11041 | long ret = -EBADF; | |
11042 | struct fd f; | |
11043 | ||
11044 | f = fdget(fd); | |
11045 | if (!f.file) | |
11046 | return -EBADF; | |
11047 | ||
11048 | ret = -EOPNOTSUPP; | |
11049 | if (f.file->f_op != &io_uring_fops) | |
11050 | goto out_fput; | |
11051 | ||
11052 | ctx = f.file->private_data; | |
11053 | ||
b6c23dd5 PB |
11054 | io_run_task_work(); |
11055 | ||
edafccee JA |
11056 | mutex_lock(&ctx->uring_lock); |
11057 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
11058 | mutex_unlock(&ctx->uring_lock); | |
c826bd7a DD |
11059 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
11060 | ctx->cq_ev_fd != NULL, ret); | |
edafccee JA |
11061 | out_fput: |
11062 | fdput(f); | |
11063 | return ret; | |
11064 | } | |
11065 | ||
2b188cc1 JA |
11066 | static int __init io_uring_init(void) |
11067 | { | |
d7f62e82 SM |
11068 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
11069 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
11070 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
11071 | } while (0) | |
11072 | ||
11073 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
11074 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
11075 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
11076 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
11077 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
11078 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
11079 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
11080 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
11081 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
11082 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 11083 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
11084 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
11085 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
11086 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
11087 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
11088 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
11089 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
11090 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
11091 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
11092 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
11093 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
11094 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
11095 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
11096 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
11097 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
11098 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 11099 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
11100 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
11101 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
16340eab | 11102 | BUILD_BUG_SQE_ELEM(40, __u16, buf_group); |
d7f62e82 | 11103 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
7d67af2c | 11104 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
b9445598 | 11105 | BUILD_BUG_SQE_ELEM(44, __u32, file_index); |
d7f62e82 | 11106 | |
b0d658ec PB |
11107 | BUILD_BUG_ON(sizeof(struct io_uring_files_update) != |
11108 | sizeof(struct io_uring_rsrc_update)); | |
11109 | BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) > | |
11110 | sizeof(struct io_uring_rsrc_update2)); | |
90499ad0 PB |
11111 | |
11112 | /* ->buf_index is u16 */ | |
11113 | BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16)); | |
11114 | ||
b0d658ec PB |
11115 | /* should fit into one byte */ |
11116 | BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8)); | |
68fe256a PB |
11117 | BUILD_BUG_ON(SQE_COMMON_FLAGS >= (1 << 8)); |
11118 | BUILD_BUG_ON((SQE_VALID_FLAGS | SQE_COMMON_FLAGS) != SQE_VALID_FLAGS); | |
b0d658ec | 11119 | |
d3656344 | 11120 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
32c2d33e | 11121 | BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof(int)); |
16340eab | 11122 | |
91f245d5 JA |
11123 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
11124 | SLAB_ACCOUNT); | |
2b188cc1 JA |
11125 | return 0; |
11126 | }; | |
11127 | __initcall(io_uring_init); |