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> | |
edce22e1 | 60 | #include <linux/blk-mq.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> |
5bd2182d | 81 | #include <linux/audit.h> |
cdc1404a | 82 | #include <linux/security.h> |
2b188cc1 | 83 | |
c826bd7a DD |
84 | #define CREATE_TRACE_POINTS |
85 | #include <trace/events/io_uring.h> | |
86 | ||
2b188cc1 JA |
87 | #include <uapi/linux/io_uring.h> |
88 | ||
89 | #include "internal.h" | |
561fb04a | 90 | #include "io-wq.h" |
2b188cc1 | 91 | |
5277deaa | 92 | #define IORING_MAX_ENTRIES 32768 |
33a107f0 | 93 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
4ce8ad95 | 94 | #define IORING_SQPOLL_CAP_ENTRIES_VALUE 8 |
65e19f54 | 95 | |
187f08c1 | 96 | /* only define max */ |
042b0d85 | 97 | #define IORING_MAX_FIXED_FILES (1U << 15) |
21b55dbc SG |
98 | #define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \ |
99 | IORING_REGISTER_LAST + IORING_OP_LAST) | |
2b188cc1 | 100 | |
187f08c1 | 101 | #define IO_RSRC_TAG_TABLE_SHIFT (PAGE_SHIFT - 3) |
2d091d62 PB |
102 | #define IO_RSRC_TAG_TABLE_MAX (1U << IO_RSRC_TAG_TABLE_SHIFT) |
103 | #define IO_RSRC_TAG_TABLE_MASK (IO_RSRC_TAG_TABLE_MAX - 1) | |
104 | ||
489809e2 PB |
105 | #define IORING_MAX_REG_BUFFERS (1U << 14) |
106 | ||
68fe256a PB |
107 | #define SQE_COMMON_FLAGS (IOSQE_FIXED_FILE | IOSQE_IO_LINK | \ |
108 | IOSQE_IO_HARDLINK | IOSQE_ASYNC) | |
109 | ||
5562a8d7 PB |
110 | #define SQE_VALID_FLAGS (SQE_COMMON_FLAGS | IOSQE_BUFFER_SELECT | \ |
111 | IOSQE_IO_DRAIN | IOSQE_CQE_SKIP_SUCCESS) | |
68fe256a | 112 | |
c854357b | 113 | #define IO_REQ_CLEAN_FLAGS (REQ_F_BUFFER_SELECTED | REQ_F_NEED_CLEANUP | \ |
d5361233 | 114 | REQ_F_POLLED | REQ_F_CREDS | REQ_F_ASYNC_DATA) |
b16fed66 | 115 | |
a538be5b PB |
116 | #define IO_REQ_CLEAN_SLOW_FLAGS (REQ_F_REFCOUNT | REQ_F_LINK | REQ_F_HARDLINK |\ |
117 | IO_REQ_CLEAN_FLAGS) | |
118 | ||
09899b19 PB |
119 | #define IO_TCTX_REFS_CACHE_NR (1U << 10) |
120 | ||
2b188cc1 JA |
121 | struct io_uring { |
122 | u32 head ____cacheline_aligned_in_smp; | |
123 | u32 tail ____cacheline_aligned_in_smp; | |
124 | }; | |
125 | ||
1e84b97b | 126 | /* |
75b28aff HV |
127 | * This data is shared with the application through the mmap at offsets |
128 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. | |
1e84b97b SB |
129 | * |
130 | * The offsets to the member fields are published through struct | |
131 | * io_sqring_offsets when calling io_uring_setup. | |
132 | */ | |
75b28aff | 133 | struct io_rings { |
1e84b97b SB |
134 | /* |
135 | * Head and tail offsets into the ring; the offsets need to be | |
136 | * masked to get valid indices. | |
137 | * | |
75b28aff HV |
138 | * The kernel controls head of the sq ring and the tail of the cq ring, |
139 | * and the application controls tail of the sq ring and the head of the | |
140 | * cq ring. | |
1e84b97b | 141 | */ |
75b28aff | 142 | struct io_uring sq, cq; |
1e84b97b | 143 | /* |
75b28aff | 144 | * Bitmasks to apply to head and tail offsets (constant, equals |
1e84b97b SB |
145 | * ring_entries - 1) |
146 | */ | |
75b28aff HV |
147 | u32 sq_ring_mask, cq_ring_mask; |
148 | /* Ring sizes (constant, power of 2) */ | |
149 | u32 sq_ring_entries, cq_ring_entries; | |
1e84b97b SB |
150 | /* |
151 | * Number of invalid entries dropped by the kernel due to | |
152 | * invalid index stored in array | |
153 | * | |
154 | * Written by the kernel, shouldn't be modified by the | |
155 | * application (i.e. get number of "new events" by comparing to | |
156 | * cached value). | |
157 | * | |
158 | * After a new SQ head value was read by the application this | |
159 | * counter includes all submissions that were dropped reaching | |
160 | * the new SQ head (and possibly more). | |
161 | */ | |
75b28aff | 162 | u32 sq_dropped; |
1e84b97b | 163 | /* |
0d9b5b3a | 164 | * Runtime SQ flags |
1e84b97b SB |
165 | * |
166 | * Written by the kernel, shouldn't be modified by the | |
167 | * application. | |
168 | * | |
169 | * The application needs a full memory barrier before checking | |
170 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. | |
171 | */ | |
75b28aff | 172 | u32 sq_flags; |
0d9b5b3a SG |
173 | /* |
174 | * Runtime CQ flags | |
175 | * | |
176 | * Written by the application, shouldn't be modified by the | |
177 | * kernel. | |
178 | */ | |
fe7e3257 | 179 | u32 cq_flags; |
1e84b97b SB |
180 | /* |
181 | * Number of completion events lost because the queue was full; | |
182 | * this should be avoided by the application by making sure | |
0b4295b5 | 183 | * there are not more requests pending than there is space in |
1e84b97b SB |
184 | * the completion queue. |
185 | * | |
186 | * Written by the kernel, shouldn't be modified by the | |
187 | * application (i.e. get number of "new events" by comparing to | |
188 | * cached value). | |
189 | * | |
190 | * As completion events come in out of order this counter is not | |
191 | * ordered with any other data. | |
192 | */ | |
75b28aff | 193 | u32 cq_overflow; |
1e84b97b SB |
194 | /* |
195 | * Ring buffer of completion events. | |
196 | * | |
197 | * The kernel writes completion events fresh every time they are | |
198 | * produced, so the application is allowed to modify pending | |
199 | * entries. | |
200 | */ | |
75b28aff | 201 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
2b188cc1 JA |
202 | }; |
203 | ||
45d189c6 | 204 | enum io_uring_cmd_flags { |
51aac424 | 205 | IO_URING_F_COMPLETE_DEFER = 1, |
3b44b371 | 206 | IO_URING_F_UNLOCKED = 2, |
51aac424 PB |
207 | /* int's last bit, sign checks are usually faster than a bit test */ |
208 | IO_URING_F_NONBLOCK = INT_MIN, | |
45d189c6 PB |
209 | }; |
210 | ||
edafccee JA |
211 | struct io_mapped_ubuf { |
212 | u64 ubuf; | |
4751f53d | 213 | u64 ubuf_end; |
edafccee | 214 | unsigned int nr_bvecs; |
de293938 | 215 | unsigned long acct_pages; |
41edf1a5 | 216 | struct bio_vec bvec[]; |
edafccee JA |
217 | }; |
218 | ||
50238531 BM |
219 | struct io_ring_ctx; |
220 | ||
6c2450ae PB |
221 | struct io_overflow_cqe { |
222 | struct io_uring_cqe cqe; | |
223 | struct list_head list; | |
224 | }; | |
225 | ||
a04b0ac0 PB |
226 | struct io_fixed_file { |
227 | /* file * with additional FFS_* flags */ | |
228 | unsigned long file_ptr; | |
229 | }; | |
230 | ||
269bbe5f BM |
231 | struct io_rsrc_put { |
232 | struct list_head list; | |
b60c8dce | 233 | u64 tag; |
50238531 BM |
234 | union { |
235 | void *rsrc; | |
236 | struct file *file; | |
bd54b6fe | 237 | struct io_mapped_ubuf *buf; |
50238531 | 238 | }; |
269bbe5f BM |
239 | }; |
240 | ||
aeca241b | 241 | struct io_file_table { |
042b0d85 | 242 | struct io_fixed_file *files; |
31b51510 JA |
243 | }; |
244 | ||
b895c9a6 | 245 | struct io_rsrc_node { |
05589553 XW |
246 | struct percpu_ref refs; |
247 | struct list_head node; | |
269bbe5f | 248 | struct list_head rsrc_list; |
b895c9a6 | 249 | struct io_rsrc_data *rsrc_data; |
4a38aed2 | 250 | struct llist_node llist; |
e297822b | 251 | bool done; |
05589553 XW |
252 | }; |
253 | ||
40ae0ff7 PB |
254 | typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc); |
255 | ||
b895c9a6 | 256 | struct io_rsrc_data { |
05f3fb3c JA |
257 | struct io_ring_ctx *ctx; |
258 | ||
2d091d62 PB |
259 | u64 **tags; |
260 | unsigned int nr; | |
40ae0ff7 | 261 | rsrc_put_fn *do_put; |
3e942498 | 262 | atomic_t refs; |
05f3fb3c | 263 | struct completion done; |
8bad28d8 | 264 | bool quiesce; |
05f3fb3c JA |
265 | }; |
266 | ||
dbc7d452 JA |
267 | struct io_buffer_list { |
268 | struct list_head list; | |
269 | struct list_head buf_list; | |
270 | __u16 bgid; | |
271 | }; | |
272 | ||
5a2e745d JA |
273 | struct io_buffer { |
274 | struct list_head list; | |
275 | __u64 addr; | |
d1f82808 | 276 | __u32 len; |
5a2e745d | 277 | __u16 bid; |
b1c62645 | 278 | __u16 bgid; |
5a2e745d JA |
279 | }; |
280 | ||
21b55dbc SG |
281 | struct io_restriction { |
282 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); | |
283 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); | |
284 | u8 sqe_flags_allowed; | |
285 | u8 sqe_flags_required; | |
7e84e1c7 | 286 | bool registered; |
21b55dbc SG |
287 | }; |
288 | ||
37d1e2e3 JA |
289 | enum { |
290 | IO_SQ_THREAD_SHOULD_STOP = 0, | |
291 | IO_SQ_THREAD_SHOULD_PARK, | |
292 | }; | |
293 | ||
534ca6d6 JA |
294 | struct io_sq_data { |
295 | refcount_t refs; | |
9e138a48 | 296 | atomic_t park_pending; |
09a6f4ef | 297 | struct mutex lock; |
69fb2131 JA |
298 | |
299 | /* ctx's that are using this sqd */ | |
300 | struct list_head ctx_list; | |
69fb2131 | 301 | |
534ca6d6 JA |
302 | struct task_struct *thread; |
303 | struct wait_queue_head wait; | |
08369246 XW |
304 | |
305 | unsigned sq_thread_idle; | |
37d1e2e3 JA |
306 | int sq_cpu; |
307 | pid_t task_pid; | |
5c2469e0 | 308 | pid_t task_tgid; |
37d1e2e3 JA |
309 | |
310 | unsigned long state; | |
37d1e2e3 | 311 | struct completion exited; |
534ca6d6 JA |
312 | }; |
313 | ||
6dd0be1e | 314 | #define IO_COMPL_BATCH 32 |
6ff119a6 | 315 | #define IO_REQ_CACHE_SIZE 32 |
bf019da7 | 316 | #define IO_REQ_ALLOC_BATCH 8 |
258b29a9 | 317 | |
a1ab7b35 PB |
318 | struct io_submit_link { |
319 | struct io_kiocb *head; | |
320 | struct io_kiocb *last; | |
321 | }; | |
322 | ||
258b29a9 | 323 | struct io_submit_state { |
5a158c6b PB |
324 | /* inline/task_work completion list, under ->uring_lock */ |
325 | struct io_wq_work_node free_list; | |
326 | /* batch completion logic */ | |
327 | struct io_wq_work_list compl_reqs; | |
a1ab7b35 | 328 | struct io_submit_link link; |
258b29a9 | 329 | |
258b29a9 | 330 | bool plug_started; |
4b628aeb | 331 | bool need_plug; |
3d4aeb9f | 332 | bool flush_cqes; |
5ca7a8b3 | 333 | unsigned short submit_nr; |
5a158c6b | 334 | struct blk_plug plug; |
258b29a9 PB |
335 | }; |
336 | ||
77bc59b4 UA |
337 | struct io_ev_fd { |
338 | struct eventfd_ctx *cq_ev_fd; | |
c75312dd | 339 | unsigned int eventfd_async: 1; |
77bc59b4 UA |
340 | struct rcu_head rcu; |
341 | }; | |
342 | ||
dbc7d452 JA |
343 | #define IO_BUFFERS_HASH_BITS 5 |
344 | ||
2b188cc1 | 345 | struct io_ring_ctx { |
b52ecf8c | 346 | /* const or read-mostly hot data */ |
2b188cc1 JA |
347 | struct { |
348 | struct percpu_ref refs; | |
2b188cc1 | 349 | |
b52ecf8c | 350 | struct io_rings *rings; |
2b188cc1 | 351 | unsigned int flags; |
e1d85334 | 352 | unsigned int compat: 1; |
e1d85334 | 353 | unsigned int drain_next: 1; |
21b55dbc | 354 | unsigned int restricted: 1; |
f18ee4cf | 355 | unsigned int off_timeout_used: 1; |
10c66904 | 356 | unsigned int drain_active: 1; |
5562a8d7 | 357 | unsigned int drain_disabled: 1; |
9aa8dfde | 358 | unsigned int has_evfd: 1; |
773697b6 | 359 | unsigned int syscall_iopoll: 1; |
b52ecf8c | 360 | } ____cacheline_aligned_in_smp; |
2b188cc1 | 361 | |
7f1129d2 | 362 | /* submission data */ |
b52ecf8c | 363 | struct { |
0499e582 PB |
364 | struct mutex uring_lock; |
365 | ||
75b28aff HV |
366 | /* |
367 | * Ring buffer of indices into array of io_uring_sqe, which is | |
368 | * mmapped by the application using the IORING_OFF_SQES offset. | |
369 | * | |
370 | * This indirection could e.g. be used to assign fixed | |
371 | * io_uring_sqe entries to operations and only submit them to | |
372 | * the queue when needed. | |
373 | * | |
374 | * The kernel modifies neither the indices array nor the entries | |
375 | * array. | |
376 | */ | |
377 | u32 *sq_array; | |
c7af47cf | 378 | struct io_uring_sqe *sq_sqes; |
2b188cc1 JA |
379 | unsigned cached_sq_head; |
380 | unsigned sq_entries; | |
de0617e4 | 381 | struct list_head defer_list; |
7f1129d2 PB |
382 | |
383 | /* | |
384 | * Fixed resources fast path, should be accessed only under | |
385 | * uring_lock, and updated through io_uring_register(2) | |
386 | */ | |
387 | struct io_rsrc_node *rsrc_node; | |
ab409402 | 388 | int rsrc_cached_refs; |
7f1129d2 PB |
389 | struct io_file_table file_table; |
390 | unsigned nr_user_files; | |
391 | unsigned nr_user_bufs; | |
392 | struct io_mapped_ubuf **user_bufs; | |
393 | ||
394 | struct io_submit_state submit_state; | |
5262f567 | 395 | struct list_head timeout_list; |
ef9dd637 | 396 | struct list_head ltimeout_list; |
1d7bb1d5 | 397 | struct list_head cq_overflow_list; |
dbc7d452 | 398 | struct list_head *io_buffers; |
cc3cec83 | 399 | struct list_head io_buffers_cache; |
4d9237e3 | 400 | struct list_head apoll_cache; |
7f1129d2 PB |
401 | struct xarray personalities; |
402 | u32 pers_next; | |
403 | unsigned sq_thread_idle; | |
2b188cc1 JA |
404 | } ____cacheline_aligned_in_smp; |
405 | ||
d0acdee2 | 406 | /* IRQ completion list, under ->completion_lock */ |
c2b6c6bc | 407 | struct io_wq_work_list locked_free_list; |
d0acdee2 | 408 | unsigned int locked_free_nr; |
3c1a2ead | 409 | |
7c30f36a | 410 | const struct cred *sq_creds; /* cred used for __io_sq_thread() */ |
534ca6d6 JA |
411 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
412 | ||
90554200 | 413 | struct wait_queue_head sqo_sq_wait; |
69fb2131 | 414 | struct list_head sqd_list; |
75b28aff | 415 | |
5ed7a37d PB |
416 | unsigned long check_cq_overflow; |
417 | ||
206aefde JA |
418 | struct { |
419 | unsigned cached_cq_tail; | |
420 | unsigned cq_entries; | |
77bc59b4 | 421 | struct io_ev_fd __rcu *io_ev_fd; |
0499e582 PB |
422 | struct wait_queue_head cq_wait; |
423 | unsigned cq_extra; | |
424 | atomic_t cq_timeouts; | |
0499e582 | 425 | unsigned cq_last_tm_flush; |
206aefde | 426 | } ____cacheline_aligned_in_smp; |
2b188cc1 | 427 | |
2b188cc1 JA |
428 | struct { |
429 | spinlock_t completion_lock; | |
e94f141b | 430 | |
89850fce JA |
431 | spinlock_t timeout_lock; |
432 | ||
def596e9 | 433 | /* |
540e32a0 | 434 | * ->iopoll_list is protected by the ctx->uring_lock for |
def596e9 JA |
435 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
436 | * For SQPOLL, only the single threaded io_sq_thread() will | |
437 | * manipulate the list, hence no extra locking is needed there. | |
438 | */ | |
5eef4e87 | 439 | struct io_wq_work_list iopoll_list; |
78076bb6 JA |
440 | struct hlist_head *cancel_hash; |
441 | unsigned cancel_hash_bits; | |
915b3dde | 442 | bool poll_multi_queue; |
cc3cec83 JA |
443 | |
444 | struct list_head io_buffers_comp; | |
2b188cc1 | 445 | } ____cacheline_aligned_in_smp; |
85faa7b8 | 446 | |
21b55dbc | 447 | struct io_restriction restrictions; |
3c1a2ead | 448 | |
b13a8918 PB |
449 | /* slow path rsrc auxilary data, used by update/register */ |
450 | struct { | |
451 | struct io_rsrc_node *rsrc_backup_node; | |
452 | struct io_mapped_ubuf *dummy_ubuf; | |
453 | struct io_rsrc_data *file_data; | |
454 | struct io_rsrc_data *buf_data; | |
455 | ||
456 | struct delayed_work rsrc_put_work; | |
457 | struct llist_head rsrc_put_llist; | |
458 | struct list_head rsrc_ref_list; | |
459 | spinlock_t rsrc_ref_lock; | |
cc3cec83 JA |
460 | |
461 | struct list_head io_buffers_pages; | |
b13a8918 PB |
462 | }; |
463 | ||
3c1a2ead | 464 | /* Keep this last, we don't need it for the fast path */ |
b986af7e PB |
465 | struct { |
466 | #if defined(CONFIG_UNIX) | |
467 | struct socket *ring_sock; | |
468 | #endif | |
469 | /* hashed buffered write serialization */ | |
470 | struct io_wq_hash *hash_map; | |
471 | ||
472 | /* Only used for accounting purposes */ | |
473 | struct user_struct *user; | |
474 | struct mm_struct *mm_account; | |
475 | ||
476 | /* ctx exit and cancelation */ | |
9011bf9a PB |
477 | struct llist_head fallback_llist; |
478 | struct delayed_work fallback_work; | |
b986af7e PB |
479 | struct work_struct exit_work; |
480 | struct list_head tctx_list; | |
481 | struct completion ref_comp; | |
e139a1ec PB |
482 | u32 iowq_limits[2]; |
483 | bool iowq_limits_set; | |
b986af7e | 484 | }; |
2b188cc1 JA |
485 | }; |
486 | ||
e7a6c00d JA |
487 | /* |
488 | * Arbitrary limit, can be raised if need be | |
489 | */ | |
490 | #define IO_RINGFD_REG_MAX 16 | |
491 | ||
53e043b2 SM |
492 | struct io_uring_task { |
493 | /* submission side */ | |
09899b19 | 494 | int cached_refs; |
53e043b2 SM |
495 | struct xarray xa; |
496 | struct wait_queue_head wait; | |
ee53fb2b SM |
497 | const struct io_ring_ctx *last; |
498 | struct io_wq *io_wq; | |
53e043b2 SM |
499 | struct percpu_counter inflight; |
500 | atomic_t in_idle; | |
53e043b2 SM |
501 | |
502 | spinlock_t task_lock; | |
503 | struct io_wq_work_list task_list; | |
4813c377 | 504 | struct io_wq_work_list prior_task_list; |
53e043b2 | 505 | struct callback_head task_work; |
e7a6c00d | 506 | struct file **registered_rings; |
6294f368 | 507 | bool task_running; |
53e043b2 SM |
508 | }; |
509 | ||
09bb8394 JA |
510 | /* |
511 | * First field must be the file pointer in all the | |
512 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> | |
513 | */ | |
221c5eb2 JA |
514 | struct io_poll_iocb { |
515 | struct file *file; | |
018043be | 516 | struct wait_queue_head *head; |
221c5eb2 | 517 | __poll_t events; |
392edb45 | 518 | struct wait_queue_entry wait; |
221c5eb2 JA |
519 | }; |
520 | ||
9d805892 | 521 | struct io_poll_update { |
018043be | 522 | struct file *file; |
9d805892 PB |
523 | u64 old_user_data; |
524 | u64 new_user_data; | |
525 | __poll_t events; | |
b69de288 JA |
526 | bool update_events; |
527 | bool update_user_data; | |
018043be PB |
528 | }; |
529 | ||
b5dba59e JA |
530 | struct io_close { |
531 | struct file *file; | |
b5dba59e | 532 | int fd; |
7df778be | 533 | u32 file_slot; |
b5dba59e JA |
534 | }; |
535 | ||
ad8a48ac JA |
536 | struct io_timeout_data { |
537 | struct io_kiocb *req; | |
538 | struct hrtimer timer; | |
539 | struct timespec64 ts; | |
540 | enum hrtimer_mode mode; | |
50c1df2b | 541 | u32 flags; |
ad8a48ac JA |
542 | }; |
543 | ||
8ed8d3c3 JA |
544 | struct io_accept { |
545 | struct file *file; | |
546 | struct sockaddr __user *addr; | |
547 | int __user *addr_len; | |
548 | int flags; | |
aaa4db12 | 549 | u32 file_slot; |
09952e3e | 550 | unsigned long nofile; |
8ed8d3c3 JA |
551 | }; |
552 | ||
553 | struct io_sync { | |
554 | struct file *file; | |
555 | loff_t len; | |
556 | loff_t off; | |
557 | int flags; | |
d63d1b5e | 558 | int mode; |
8ed8d3c3 JA |
559 | }; |
560 | ||
fbf23849 JA |
561 | struct io_cancel { |
562 | struct file *file; | |
563 | u64 addr; | |
564 | }; | |
565 | ||
b29472ee JA |
566 | struct io_timeout { |
567 | struct file *file; | |
bfe68a22 PB |
568 | u32 off; |
569 | u32 target_seq; | |
135fcde8 | 570 | struct list_head list; |
90cd7e42 PB |
571 | /* head of the link, used by linked timeouts only */ |
572 | struct io_kiocb *head; | |
89b263f6 JA |
573 | /* for linked completions */ |
574 | struct io_kiocb *prev; | |
b29472ee JA |
575 | }; |
576 | ||
0bdf7a2d PB |
577 | struct io_timeout_rem { |
578 | struct file *file; | |
579 | u64 addr; | |
9c8e11b3 PB |
580 | |
581 | /* timeout update */ | |
582 | struct timespec64 ts; | |
583 | u32 flags; | |
f1042b6c | 584 | bool ltimeout; |
0bdf7a2d PB |
585 | }; |
586 | ||
9adbd45d JA |
587 | struct io_rw { |
588 | /* NOTE: kiocb has the file as the first member, so don't do it here */ | |
589 | struct kiocb kiocb; | |
590 | u64 addr; | |
584b0180 JA |
591 | u32 len; |
592 | u32 flags; | |
9adbd45d JA |
593 | }; |
594 | ||
3fbb51c1 JA |
595 | struct io_connect { |
596 | struct file *file; | |
597 | struct sockaddr __user *addr; | |
598 | int addr_len; | |
599 | }; | |
600 | ||
e47293fd JA |
601 | struct io_sr_msg { |
602 | struct file *file; | |
fddaface | 603 | union { |
4af3417a PB |
604 | struct compat_msghdr __user *umsg_compat; |
605 | struct user_msghdr __user *umsg; | |
606 | void __user *buf; | |
fddaface | 607 | }; |
e47293fd | 608 | int msg_flags; |
bcda7baa | 609 | int bgid; |
fddaface | 610 | size_t len; |
7ba89d2a | 611 | size_t done_io; |
e47293fd JA |
612 | }; |
613 | ||
15b71abe JA |
614 | struct io_open { |
615 | struct file *file; | |
616 | int dfd; | |
b9445598 | 617 | u32 file_slot; |
15b71abe | 618 | struct filename *filename; |
c12cedf2 | 619 | struct open_how how; |
4022e7af | 620 | unsigned long nofile; |
15b71abe JA |
621 | }; |
622 | ||
269bbe5f | 623 | struct io_rsrc_update { |
05f3fb3c JA |
624 | struct file *file; |
625 | u64 arg; | |
626 | u32 nr_args; | |
627 | u32 offset; | |
628 | }; | |
629 | ||
4840e418 JA |
630 | struct io_fadvise { |
631 | struct file *file; | |
632 | u64 offset; | |
633 | u32 len; | |
634 | u32 advice; | |
635 | }; | |
636 | ||
c1ca757b JA |
637 | struct io_madvise { |
638 | struct file *file; | |
639 | u64 addr; | |
640 | u32 len; | |
641 | u32 advice; | |
642 | }; | |
643 | ||
3e4827b0 JA |
644 | struct io_epoll { |
645 | struct file *file; | |
646 | int epfd; | |
647 | int op; | |
648 | int fd; | |
649 | struct epoll_event event; | |
e47293fd JA |
650 | }; |
651 | ||
7d67af2c PB |
652 | struct io_splice { |
653 | struct file *file_out; | |
7d67af2c PB |
654 | loff_t off_out; |
655 | loff_t off_in; | |
656 | u64 len; | |
a3e4bc23 | 657 | int splice_fd_in; |
7d67af2c PB |
658 | unsigned int flags; |
659 | }; | |
660 | ||
ddf0322d JA |
661 | struct io_provide_buf { |
662 | struct file *file; | |
663 | __u64 addr; | |
38134ada | 664 | __u32 len; |
ddf0322d JA |
665 | __u32 bgid; |
666 | __u16 nbufs; | |
667 | __u16 bid; | |
668 | }; | |
669 | ||
1d9e1288 BM |
670 | struct io_statx { |
671 | struct file *file; | |
672 | int dfd; | |
673 | unsigned int mask; | |
674 | unsigned int flags; | |
1b6fe6e0 | 675 | struct filename *filename; |
1d9e1288 BM |
676 | struct statx __user *buffer; |
677 | }; | |
678 | ||
36f4fa68 JA |
679 | struct io_shutdown { |
680 | struct file *file; | |
681 | int how; | |
682 | }; | |
683 | ||
80a261fd JA |
684 | struct io_rename { |
685 | struct file *file; | |
686 | int old_dfd; | |
687 | int new_dfd; | |
688 | struct filename *oldpath; | |
689 | struct filename *newpath; | |
690 | int flags; | |
691 | }; | |
692 | ||
14a1143b JA |
693 | struct io_unlink { |
694 | struct file *file; | |
695 | int dfd; | |
696 | int flags; | |
697 | struct filename *filename; | |
698 | }; | |
699 | ||
e34a02dc DK |
700 | struct io_mkdir { |
701 | struct file *file; | |
702 | int dfd; | |
703 | umode_t mode; | |
704 | struct filename *filename; | |
705 | }; | |
706 | ||
7a8721f8 DK |
707 | struct io_symlink { |
708 | struct file *file; | |
709 | int new_dfd; | |
710 | struct filename *oldpath; | |
711 | struct filename *newpath; | |
712 | }; | |
713 | ||
cf30da90 DK |
714 | struct io_hardlink { |
715 | struct file *file; | |
716 | int old_dfd; | |
717 | int new_dfd; | |
718 | struct filename *oldpath; | |
719 | struct filename *newpath; | |
720 | int flags; | |
721 | }; | |
722 | ||
4f57f06c JA |
723 | struct io_msg { |
724 | struct file *file; | |
725 | u64 user_data; | |
726 | u32 len; | |
727 | }; | |
728 | ||
f499a021 JA |
729 | struct io_async_connect { |
730 | struct sockaddr_storage address; | |
731 | }; | |
732 | ||
03b1230c JA |
733 | struct io_async_msghdr { |
734 | struct iovec fast_iov[UIO_FASTIOV]; | |
257e84a5 PB |
735 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
736 | struct iovec *free_iov; | |
03b1230c JA |
737 | struct sockaddr __user *uaddr; |
738 | struct msghdr msg; | |
b537916c | 739 | struct sockaddr_storage addr; |
03b1230c JA |
740 | }; |
741 | ||
538941e2 | 742 | struct io_rw_state { |
ff6165b2 | 743 | struct iov_iter iter; |
cd658695 | 744 | struct iov_iter_state iter_state; |
c88598a9 | 745 | struct iovec fast_iov[UIO_FASTIOV]; |
538941e2 PB |
746 | }; |
747 | ||
748 | struct io_async_rw { | |
749 | struct io_rw_state s; | |
750 | const struct iovec *free_iovec; | |
227c0c96 | 751 | size_t bytes_done; |
bcf5a063 | 752 | struct wait_page_queue wpq; |
f67676d1 JA |
753 | }; |
754 | ||
6b47ee6e PB |
755 | enum { |
756 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, | |
757 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, | |
758 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, | |
759 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, | |
760 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, | |
bcda7baa | 761 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
04c76b41 | 762 | REQ_F_CQE_SKIP_BIT = IOSQE_CQE_SKIP_SUCCESS_BIT, |
6b47ee6e | 763 | |
dddca226 | 764 | /* first byte is taken by user flags, shift it to not overlap */ |
93d2bcd2 | 765 | REQ_F_FAIL_BIT = 8, |
6b47ee6e PB |
766 | REQ_F_INFLIGHT_BIT, |
767 | REQ_F_CUR_POS_BIT, | |
768 | REQ_F_NOWAIT_BIT, | |
6b47ee6e | 769 | REQ_F_LINK_TIMEOUT_BIT, |
99bc4c38 | 770 | REQ_F_NEED_CLEANUP_BIT, |
d7718a9d | 771 | REQ_F_POLLED_BIT, |
bcda7baa | 772 | REQ_F_BUFFER_SELECTED_BIT, |
e342c807 | 773 | REQ_F_COMPLETE_INLINE_BIT, |
230d50d4 | 774 | REQ_F_REISSUE_BIT, |
b8e64b53 | 775 | REQ_F_CREDS_BIT, |
20e60a38 | 776 | REQ_F_REFCOUNT_BIT, |
4d13d1a4 | 777 | REQ_F_ARM_LTIMEOUT_BIT, |
d886e185 | 778 | REQ_F_ASYNC_DATA_BIT, |
04c76b41 | 779 | REQ_F_SKIP_LINK_CQES_BIT, |
91eac1c6 JA |
780 | REQ_F_SINGLE_POLL_BIT, |
781 | REQ_F_DOUBLE_POLL_BIT, | |
8a3e8ee5 | 782 | REQ_F_PARTIAL_IO_BIT, |
7b29f92d | 783 | /* keep async read/write and isreg together and in order */ |
35645ac3 | 784 | REQ_F_SUPPORT_NOWAIT_BIT, |
7b29f92d | 785 | REQ_F_ISREG_BIT, |
84557871 JA |
786 | |
787 | /* not a real bit, just to check we're not overflowing the space */ | |
788 | __REQ_F_LAST_BIT, | |
6b47ee6e PB |
789 | }; |
790 | ||
791 | enum { | |
792 | /* ctx owns file */ | |
793 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), | |
794 | /* drain existing IO first */ | |
795 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), | |
796 | /* linked sqes */ | |
797 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), | |
798 | /* doesn't sever on completion < 0 */ | |
799 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), | |
800 | /* IOSQE_ASYNC */ | |
801 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), | |
bcda7baa JA |
802 | /* IOSQE_BUFFER_SELECT */ |
803 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), | |
04c76b41 PB |
804 | /* IOSQE_CQE_SKIP_SUCCESS */ |
805 | REQ_F_CQE_SKIP = BIT(REQ_F_CQE_SKIP_BIT), | |
6b47ee6e | 806 | |
6b47ee6e | 807 | /* fail rest of links */ |
93d2bcd2 | 808 | REQ_F_FAIL = BIT(REQ_F_FAIL_BIT), |
b05a1bcd | 809 | /* on inflight list, should be cancelled and waited on exit reliably */ |
6b47ee6e PB |
810 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
811 | /* read/write uses file position */ | |
812 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), | |
813 | /* must not punt to workers */ | |
814 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), | |
900fad45 | 815 | /* has or had linked timeout */ |
6b47ee6e | 816 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
99bc4c38 PB |
817 | /* needs cleanup */ |
818 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), | |
d7718a9d JA |
819 | /* already went through poll handler */ |
820 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), | |
bcda7baa JA |
821 | /* buffer already selected */ |
822 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), | |
e342c807 PB |
823 | /* completion is deferred through io_comp_state */ |
824 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), | |
230d50d4 JA |
825 | /* caller should reissue async */ |
826 | REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT), | |
35645ac3 PB |
827 | /* supports async reads/writes */ |
828 | REQ_F_SUPPORT_NOWAIT = BIT(REQ_F_SUPPORT_NOWAIT_BIT), | |
7b29f92d JA |
829 | /* regular file */ |
830 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), | |
b8e64b53 PB |
831 | /* has creds assigned */ |
832 | REQ_F_CREDS = BIT(REQ_F_CREDS_BIT), | |
20e60a38 PB |
833 | /* skip refcounting if not set */ |
834 | REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT), | |
4d13d1a4 PB |
835 | /* there is a linked timeout that has to be armed */ |
836 | REQ_F_ARM_LTIMEOUT = BIT(REQ_F_ARM_LTIMEOUT_BIT), | |
d886e185 PB |
837 | /* ->async_data allocated */ |
838 | REQ_F_ASYNC_DATA = BIT(REQ_F_ASYNC_DATA_BIT), | |
04c76b41 PB |
839 | /* don't post CQEs while failing linked requests */ |
840 | REQ_F_SKIP_LINK_CQES = BIT(REQ_F_SKIP_LINK_CQES_BIT), | |
91eac1c6 JA |
841 | /* single poll may be active */ |
842 | REQ_F_SINGLE_POLL = BIT(REQ_F_SINGLE_POLL_BIT), | |
843 | /* double poll may active */ | |
844 | REQ_F_DOUBLE_POLL = BIT(REQ_F_DOUBLE_POLL_BIT), | |
8a3e8ee5 JA |
845 | /* request has already done partial IO */ |
846 | REQ_F_PARTIAL_IO = BIT(REQ_F_PARTIAL_IO_BIT), | |
d7718a9d JA |
847 | }; |
848 | ||
849 | struct async_poll { | |
850 | struct io_poll_iocb poll; | |
807abcb0 | 851 | struct io_poll_iocb *double_poll; |
6b47ee6e PB |
852 | }; |
853 | ||
f237c30a | 854 | typedef void (*io_req_tw_func_t)(struct io_kiocb *req, bool *locked); |
5b0a6acc | 855 | |
7cbf1722 | 856 | struct io_task_work { |
5b0a6acc PB |
857 | union { |
858 | struct io_wq_work_node node; | |
859 | struct llist_node fallback_node; | |
860 | }; | |
861 | io_req_tw_func_t func; | |
7cbf1722 JA |
862 | }; |
863 | ||
992da01a PB |
864 | enum { |
865 | IORING_RSRC_FILE = 0, | |
866 | IORING_RSRC_BUFFER = 1, | |
867 | }; | |
868 | ||
09bb8394 JA |
869 | /* |
870 | * NOTE! Each of the iocb union members has the file pointer | |
871 | * as the first entry in their struct definition. So you can | |
872 | * access the file pointer through any of the sub-structs, | |
63c36549 | 873 | * or directly as just 'file' in this struct. |
09bb8394 | 874 | */ |
2b188cc1 | 875 | struct io_kiocb { |
221c5eb2 | 876 | union { |
09bb8394 | 877 | struct file *file; |
9adbd45d | 878 | struct io_rw rw; |
221c5eb2 | 879 | struct io_poll_iocb poll; |
9d805892 | 880 | struct io_poll_update poll_update; |
8ed8d3c3 JA |
881 | struct io_accept accept; |
882 | struct io_sync sync; | |
fbf23849 | 883 | struct io_cancel cancel; |
b29472ee | 884 | struct io_timeout timeout; |
0bdf7a2d | 885 | struct io_timeout_rem timeout_rem; |
3fbb51c1 | 886 | struct io_connect connect; |
e47293fd | 887 | struct io_sr_msg sr_msg; |
15b71abe | 888 | struct io_open open; |
b5dba59e | 889 | struct io_close close; |
269bbe5f | 890 | struct io_rsrc_update rsrc_update; |
4840e418 | 891 | struct io_fadvise fadvise; |
c1ca757b | 892 | struct io_madvise madvise; |
3e4827b0 | 893 | struct io_epoll epoll; |
7d67af2c | 894 | struct io_splice splice; |
ddf0322d | 895 | struct io_provide_buf pbuf; |
1d9e1288 | 896 | struct io_statx statx; |
36f4fa68 | 897 | struct io_shutdown shutdown; |
80a261fd | 898 | struct io_rename rename; |
14a1143b | 899 | struct io_unlink unlink; |
e34a02dc | 900 | struct io_mkdir mkdir; |
7a8721f8 | 901 | struct io_symlink symlink; |
cf30da90 | 902 | struct io_hardlink hardlink; |
4f57f06c | 903 | struct io_msg msg; |
221c5eb2 | 904 | }; |
2b188cc1 | 905 | |
d625c6ee | 906 | u8 opcode; |
65a6543d XW |
907 | /* polled IO has completed */ |
908 | u8 iopoll_completed; | |
4f4eeba8 | 909 | u16 buf_index; |
d17e56eb PB |
910 | unsigned int flags; |
911 | ||
912 | u64 user_data; | |
9cf7c104 | 913 | u32 result; |
82733d16 JA |
914 | /* fd initially, then cflags for completion */ |
915 | union { | |
916 | u32 cflags; | |
917 | int fd; | |
918 | }; | |
4f4eeba8 | 919 | |
010e8e6b | 920 | struct io_ring_ctx *ctx; |
010e8e6b | 921 | struct task_struct *task; |
d7718a9d | 922 | |
269bbe5f | 923 | struct percpu_ref *fixed_rsrc_refs; |
d886e185 PB |
924 | /* store used ubuf, so we can prevent reloading */ |
925 | struct io_mapped_ubuf *imu; | |
fcb323cc | 926 | |
2804ecd8 JA |
927 | union { |
928 | /* used by request caches, completion batching and iopoll */ | |
929 | struct io_wq_work_node comp_list; | |
930 | /* cache ->apoll->events */ | |
931 | int apoll_events; | |
932 | }; | |
d17e56eb | 933 | atomic_t refs; |
521d61fc | 934 | atomic_t poll_refs; |
5b0a6acc | 935 | struct io_task_work io_task_work; |
010e8e6b PB |
936 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
937 | struct hlist_node hash_node; | |
7e3709d5 | 938 | /* internal polling, see IORING_FEAT_FAST_POLL */ |
010e8e6b | 939 | struct async_poll *apoll; |
d886e185 PB |
940 | /* opcode allocated if it needs to store data for async defer */ |
941 | void *async_data; | |
7e3709d5 | 942 | /* stores selected buf, valid IFF REQ_F_BUFFER_SELECTED is set */ |
30d51dd4 | 943 | struct io_buffer *kbuf; |
41cdcc22 | 944 | /* linked requests, IFF REQ_F_HARDLINK or REQ_F_LINK are set */ |
34d2bfe7 | 945 | struct io_kiocb *link; |
41cdcc22 | 946 | /* custom credentials, valid IFF REQ_F_CREDS is set */ |
521d61fc JA |
947 | const struct cred *creds; |
948 | struct io_wq_work work; | |
2b188cc1 | 949 | }; |
05589553 | 950 | |
13bf43f5 PB |
951 | struct io_tctx_node { |
952 | struct list_head ctx_node; | |
953 | struct task_struct *task; | |
13bf43f5 PB |
954 | struct io_ring_ctx *ctx; |
955 | }; | |
956 | ||
27dc8338 PB |
957 | struct io_defer_entry { |
958 | struct list_head list; | |
959 | struct io_kiocb *req; | |
9cf7c104 | 960 | u32 seq; |
2b188cc1 JA |
961 | }; |
962 | ||
d3656344 | 963 | struct io_op_def { |
d3656344 JA |
964 | /* needs req->file assigned */ |
965 | unsigned needs_file : 1; | |
6d63416d PB |
966 | /* should block plug */ |
967 | unsigned plug : 1; | |
d3656344 JA |
968 | /* hash wq insertion if file is a regular file */ |
969 | unsigned hash_reg_file : 1; | |
970 | /* unbound wq insertion if file is a non-regular file */ | |
971 | unsigned unbound_nonreg_file : 1; | |
8a72758c JA |
972 | /* set if opcode supports polled "wait" */ |
973 | unsigned pollin : 1; | |
974 | unsigned pollout : 1; | |
52dd8640 | 975 | unsigned poll_exclusive : 1; |
bcda7baa JA |
976 | /* op supports buffer selection */ |
977 | unsigned buffer_select : 1; | |
26f0505a PB |
978 | /* do prep async if is going to be punted */ |
979 | unsigned needs_async_setup : 1; | |
6d63416d PB |
980 | /* opcode is not supported by this kernel */ |
981 | unsigned not_supported : 1; | |
5bd2182d PM |
982 | /* skip auditing */ |
983 | unsigned audit_skip : 1; | |
e8c2bc1f JA |
984 | /* size of async data needed, if any */ |
985 | unsigned short async_size; | |
d3656344 JA |
986 | }; |
987 | ||
0918682b | 988 | static const struct io_op_def io_op_defs[] = { |
0463b6c5 PB |
989 | [IORING_OP_NOP] = {}, |
990 | [IORING_OP_READV] = { | |
d3656344 JA |
991 | .needs_file = 1, |
992 | .unbound_nonreg_file = 1, | |
8a72758c | 993 | .pollin = 1, |
4d954c25 | 994 | .buffer_select = 1, |
26f0505a | 995 | .needs_async_setup = 1, |
27926b68 | 996 | .plug = 1, |
5bd2182d | 997 | .audit_skip = 1, |
e8c2bc1f | 998 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 999 | }, |
0463b6c5 | 1000 | [IORING_OP_WRITEV] = { |
d3656344 JA |
1001 | .needs_file = 1, |
1002 | .hash_reg_file = 1, | |
1003 | .unbound_nonreg_file = 1, | |
8a72758c | 1004 | .pollout = 1, |
26f0505a | 1005 | .needs_async_setup = 1, |
27926b68 | 1006 | .plug = 1, |
5bd2182d | 1007 | .audit_skip = 1, |
e8c2bc1f | 1008 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 1009 | }, |
0463b6c5 | 1010 | [IORING_OP_FSYNC] = { |
d3656344 | 1011 | .needs_file = 1, |
5bd2182d | 1012 | .audit_skip = 1, |
d3656344 | 1013 | }, |
0463b6c5 | 1014 | [IORING_OP_READ_FIXED] = { |
d3656344 JA |
1015 | .needs_file = 1, |
1016 | .unbound_nonreg_file = 1, | |
8a72758c | 1017 | .pollin = 1, |
27926b68 | 1018 | .plug = 1, |
5bd2182d | 1019 | .audit_skip = 1, |
e8c2bc1f | 1020 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 1021 | }, |
0463b6c5 | 1022 | [IORING_OP_WRITE_FIXED] = { |
d3656344 JA |
1023 | .needs_file = 1, |
1024 | .hash_reg_file = 1, | |
1025 | .unbound_nonreg_file = 1, | |
8a72758c | 1026 | .pollout = 1, |
27926b68 | 1027 | .plug = 1, |
5bd2182d | 1028 | .audit_skip = 1, |
e8c2bc1f | 1029 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 1030 | }, |
0463b6c5 | 1031 | [IORING_OP_POLL_ADD] = { |
d3656344 JA |
1032 | .needs_file = 1, |
1033 | .unbound_nonreg_file = 1, | |
5bd2182d PM |
1034 | .audit_skip = 1, |
1035 | }, | |
1036 | [IORING_OP_POLL_REMOVE] = { | |
1037 | .audit_skip = 1, | |
d3656344 | 1038 | }, |
0463b6c5 | 1039 | [IORING_OP_SYNC_FILE_RANGE] = { |
d3656344 | 1040 | .needs_file = 1, |
5bd2182d | 1041 | .audit_skip = 1, |
d3656344 | 1042 | }, |
0463b6c5 | 1043 | [IORING_OP_SENDMSG] = { |
d3656344 JA |
1044 | .needs_file = 1, |
1045 | .unbound_nonreg_file = 1, | |
8a72758c | 1046 | .pollout = 1, |
26f0505a | 1047 | .needs_async_setup = 1, |
e8c2bc1f | 1048 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 1049 | }, |
0463b6c5 | 1050 | [IORING_OP_RECVMSG] = { |
d3656344 JA |
1051 | .needs_file = 1, |
1052 | .unbound_nonreg_file = 1, | |
8a72758c | 1053 | .pollin = 1, |
52de1fe1 | 1054 | .buffer_select = 1, |
26f0505a | 1055 | .needs_async_setup = 1, |
e8c2bc1f | 1056 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 1057 | }, |
0463b6c5 | 1058 | [IORING_OP_TIMEOUT] = { |
5bd2182d | 1059 | .audit_skip = 1, |
e8c2bc1f | 1060 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 1061 | }, |
9c8e11b3 PB |
1062 | [IORING_OP_TIMEOUT_REMOVE] = { |
1063 | /* used by timeout updates' prep() */ | |
5bd2182d | 1064 | .audit_skip = 1, |
9c8e11b3 | 1065 | }, |
0463b6c5 | 1066 | [IORING_OP_ACCEPT] = { |
d3656344 JA |
1067 | .needs_file = 1, |
1068 | .unbound_nonreg_file = 1, | |
8a72758c | 1069 | .pollin = 1, |
52dd8640 | 1070 | .poll_exclusive = 1, |
d3656344 | 1071 | }, |
5bd2182d PM |
1072 | [IORING_OP_ASYNC_CANCEL] = { |
1073 | .audit_skip = 1, | |
1074 | }, | |
0463b6c5 | 1075 | [IORING_OP_LINK_TIMEOUT] = { |
5bd2182d | 1076 | .audit_skip = 1, |
e8c2bc1f | 1077 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 1078 | }, |
0463b6c5 | 1079 | [IORING_OP_CONNECT] = { |
d3656344 JA |
1080 | .needs_file = 1, |
1081 | .unbound_nonreg_file = 1, | |
8a72758c | 1082 | .pollout = 1, |
26f0505a | 1083 | .needs_async_setup = 1, |
e8c2bc1f | 1084 | .async_size = sizeof(struct io_async_connect), |
d3656344 | 1085 | }, |
0463b6c5 | 1086 | [IORING_OP_FALLOCATE] = { |
d3656344 | 1087 | .needs_file = 1, |
d3656344 | 1088 | }, |
44526bed JA |
1089 | [IORING_OP_OPENAT] = {}, |
1090 | [IORING_OP_CLOSE] = {}, | |
5bd2182d PM |
1091 | [IORING_OP_FILES_UPDATE] = { |
1092 | .audit_skip = 1, | |
1093 | }, | |
1094 | [IORING_OP_STATX] = { | |
1095 | .audit_skip = 1, | |
1096 | }, | |
0463b6c5 | 1097 | [IORING_OP_READ] = { |
3a6820f2 JA |
1098 | .needs_file = 1, |
1099 | .unbound_nonreg_file = 1, | |
8a72758c | 1100 | .pollin = 1, |
bcda7baa | 1101 | .buffer_select = 1, |
27926b68 | 1102 | .plug = 1, |
5bd2182d | 1103 | .audit_skip = 1, |
e8c2bc1f | 1104 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 1105 | }, |
0463b6c5 | 1106 | [IORING_OP_WRITE] = { |
3a6820f2 | 1107 | .needs_file = 1, |
7b3188e7 | 1108 | .hash_reg_file = 1, |
3a6820f2 | 1109 | .unbound_nonreg_file = 1, |
8a72758c | 1110 | .pollout = 1, |
27926b68 | 1111 | .plug = 1, |
5bd2182d | 1112 | .audit_skip = 1, |
e8c2bc1f | 1113 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 1114 | }, |
0463b6c5 | 1115 | [IORING_OP_FADVISE] = { |
4840e418 | 1116 | .needs_file = 1, |
5bd2182d | 1117 | .audit_skip = 1, |
c1ca757b | 1118 | }, |
44526bed | 1119 | [IORING_OP_MADVISE] = {}, |
0463b6c5 | 1120 | [IORING_OP_SEND] = { |
fddaface JA |
1121 | .needs_file = 1, |
1122 | .unbound_nonreg_file = 1, | |
8a72758c | 1123 | .pollout = 1, |
5bd2182d | 1124 | .audit_skip = 1, |
fddaface | 1125 | }, |
0463b6c5 | 1126 | [IORING_OP_RECV] = { |
fddaface JA |
1127 | .needs_file = 1, |
1128 | .unbound_nonreg_file = 1, | |
8a72758c | 1129 | .pollin = 1, |
bcda7baa | 1130 | .buffer_select = 1, |
5bd2182d | 1131 | .audit_skip = 1, |
fddaface | 1132 | }, |
0463b6c5 | 1133 | [IORING_OP_OPENAT2] = { |
cebdb986 | 1134 | }, |
3e4827b0 JA |
1135 | [IORING_OP_EPOLL_CTL] = { |
1136 | .unbound_nonreg_file = 1, | |
5bd2182d | 1137 | .audit_skip = 1, |
3e4827b0 | 1138 | }, |
7d67af2c PB |
1139 | [IORING_OP_SPLICE] = { |
1140 | .needs_file = 1, | |
1141 | .hash_reg_file = 1, | |
1142 | .unbound_nonreg_file = 1, | |
5bd2182d PM |
1143 | .audit_skip = 1, |
1144 | }, | |
1145 | [IORING_OP_PROVIDE_BUFFERS] = { | |
1146 | .audit_skip = 1, | |
1147 | }, | |
1148 | [IORING_OP_REMOVE_BUFFERS] = { | |
1149 | .audit_skip = 1, | |
ddf0322d | 1150 | }, |
f2a8d5c7 PB |
1151 | [IORING_OP_TEE] = { |
1152 | .needs_file = 1, | |
1153 | .hash_reg_file = 1, | |
1154 | .unbound_nonreg_file = 1, | |
5bd2182d | 1155 | .audit_skip = 1, |
f2a8d5c7 | 1156 | }, |
36f4fa68 JA |
1157 | [IORING_OP_SHUTDOWN] = { |
1158 | .needs_file = 1, | |
1159 | }, | |
44526bed JA |
1160 | [IORING_OP_RENAMEAT] = {}, |
1161 | [IORING_OP_UNLINKAT] = {}, | |
e34a02dc | 1162 | [IORING_OP_MKDIRAT] = {}, |
7a8721f8 | 1163 | [IORING_OP_SYMLINKAT] = {}, |
cf30da90 | 1164 | [IORING_OP_LINKAT] = {}, |
4f57f06c JA |
1165 | [IORING_OP_MSG_RING] = { |
1166 | .needs_file = 1, | |
1167 | }, | |
d3656344 JA |
1168 | }; |
1169 | ||
0756a869 PB |
1170 | /* requests with any of those set should undergo io_disarm_next() */ |
1171 | #define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL) | |
1172 | ||
7a612350 | 1173 | static bool io_disarm_next(struct io_kiocb *req); |
eef51daa | 1174 | static void io_uring_del_tctx_node(unsigned long index); |
9936c7c2 PB |
1175 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
1176 | struct task_struct *task, | |
3dd0c97a | 1177 | bool cancel_all); |
78cc687b | 1178 | static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd); |
1ffc5422 | 1179 | |
913a571a PB |
1180 | static void io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags); |
1181 | ||
ec9c02ad | 1182 | static void io_put_req(struct io_kiocb *req); |
91c2f697 | 1183 | static void io_put_req_deferred(struct io_kiocb *req); |
c7dae4ba | 1184 | static void io_dismantle_req(struct io_kiocb *req); |
94ae5e77 | 1185 | static void io_queue_linked_timeout(struct io_kiocb *req); |
fdecb662 | 1186 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 1187 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 | 1188 | unsigned nr_args); |
68fb8979 | 1189 | static void io_clean_op(struct io_kiocb *req); |
5106dd6e JA |
1190 | static inline struct file *io_file_get_fixed(struct io_kiocb *req, int fd, |
1191 | unsigned issue_flags); | |
1192 | static inline struct file *io_file_get_normal(struct io_kiocb *req, int fd); | |
d5361233 JA |
1193 | static void io_drop_inflight_file(struct io_kiocb *req); |
1194 | static bool io_assign_file(struct io_kiocb *req, unsigned int issue_flags); | |
c5eef2b9 | 1195 | static void __io_queue_sqe(struct io_kiocb *req); |
269bbe5f | 1196 | static void io_rsrc_put_work(struct work_struct *work); |
de0617e4 | 1197 | |
907d1df3 | 1198 | static void io_req_task_queue(struct io_kiocb *req); |
c450178d | 1199 | static void __io_submit_flush_completions(struct io_ring_ctx *ctx); |
179ae0d1 | 1200 | static int io_req_prep_async(struct io_kiocb *req); |
de0617e4 | 1201 | |
b9445598 PB |
1202 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
1203 | unsigned int issue_flags, u32 slot_index); | |
7df778be PB |
1204 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags); |
1205 | ||
f1042b6c | 1206 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer); |
9aa8dfde | 1207 | static void io_eventfd_signal(struct io_ring_ctx *ctx); |
b9445598 | 1208 | |
2b188cc1 JA |
1209 | static struct kmem_cache *req_cachep; |
1210 | ||
0918682b | 1211 | static const struct file_operations io_uring_fops; |
2b188cc1 JA |
1212 | |
1213 | struct sock *io_uring_get_socket(struct file *file) | |
1214 | { | |
1215 | #if defined(CONFIG_UNIX) | |
1216 | if (file->f_op == &io_uring_fops) { | |
1217 | struct io_ring_ctx *ctx = file->private_data; | |
1218 | ||
1219 | return ctx->ring_sock->sk; | |
1220 | } | |
1221 | #endif | |
1222 | return NULL; | |
1223 | } | |
1224 | EXPORT_SYMBOL(io_uring_get_socket); | |
1225 | ||
1f59bc0f PB |
1226 | #if defined(CONFIG_UNIX) |
1227 | static inline bool io_file_need_scm(struct file *filp) | |
1228 | { | |
1229 | return !!unix_get_socket(filp); | |
1230 | } | |
1231 | #else | |
1232 | static inline bool io_file_need_scm(struct file *filp) | |
1233 | { | |
1234 | return 0; | |
1235 | } | |
1236 | #endif | |
1237 | ||
f8929630 PB |
1238 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, unsigned issue_flags) |
1239 | { | |
1240 | lockdep_assert_held(&ctx->uring_lock); | |
1241 | if (issue_flags & IO_URING_F_UNLOCKED) | |
1242 | mutex_unlock(&ctx->uring_lock); | |
1243 | } | |
1244 | ||
1245 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, unsigned issue_flags) | |
1246 | { | |
1247 | /* | |
1248 | * "Normal" inline submissions always hold the uring_lock, since we | |
1249 | * grab it from the system call. Same is true for the SQPOLL offload. | |
1250 | * The only exception is when we've detached the request and issue it | |
1251 | * from an async worker thread, grab the lock for that case. | |
1252 | */ | |
1253 | if (issue_flags & IO_URING_F_UNLOCKED) | |
1254 | mutex_lock(&ctx->uring_lock); | |
1255 | lockdep_assert_held(&ctx->uring_lock); | |
1256 | } | |
1257 | ||
f237c30a PB |
1258 | static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked) |
1259 | { | |
1260 | if (!*locked) { | |
1261 | mutex_lock(&ctx->uring_lock); | |
1262 | *locked = true; | |
1263 | } | |
1264 | } | |
1265 | ||
f2f87370 PB |
1266 | #define io_for_each_link(pos, head) \ |
1267 | for (pos = (head); pos; pos = pos->link) | |
1268 | ||
21c843d5 PB |
1269 | /* |
1270 | * Shamelessly stolen from the mm implementation of page reference checking, | |
1271 | * see commit f958d7b528b1 for details. | |
1272 | */ | |
1273 | #define req_ref_zero_or_close_to_overflow(req) \ | |
1274 | ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u) | |
1275 | ||
1276 | static inline bool req_ref_inc_not_zero(struct io_kiocb *req) | |
1277 | { | |
20e60a38 | 1278 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1279 | return atomic_inc_not_zero(&req->refs); |
1280 | } | |
1281 | ||
21c843d5 PB |
1282 | static inline bool req_ref_put_and_test(struct io_kiocb *req) |
1283 | { | |
20e60a38 PB |
1284 | if (likely(!(req->flags & REQ_F_REFCOUNT))) |
1285 | return true; | |
1286 | ||
21c843d5 PB |
1287 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1288 | return atomic_dec_and_test(&req->refs); | |
1289 | } | |
1290 | ||
21c843d5 PB |
1291 | static inline void req_ref_get(struct io_kiocb *req) |
1292 | { | |
20e60a38 | 1293 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1294 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1295 | atomic_inc(&req->refs); | |
1296 | } | |
1297 | ||
c450178d PB |
1298 | static inline void io_submit_flush_completions(struct io_ring_ctx *ctx) |
1299 | { | |
6f33b0bc | 1300 | if (!wq_list_empty(&ctx->submit_state.compl_reqs)) |
c450178d PB |
1301 | __io_submit_flush_completions(ctx); |
1302 | } | |
1303 | ||
48dcd38d | 1304 | static inline void __io_req_set_refcount(struct io_kiocb *req, int nr) |
20e60a38 PB |
1305 | { |
1306 | if (!(req->flags & REQ_F_REFCOUNT)) { | |
1307 | req->flags |= REQ_F_REFCOUNT; | |
48dcd38d | 1308 | atomic_set(&req->refs, nr); |
20e60a38 PB |
1309 | } |
1310 | } | |
1311 | ||
48dcd38d PB |
1312 | static inline void io_req_set_refcount(struct io_kiocb *req) |
1313 | { | |
1314 | __io_req_set_refcount(req, 1); | |
1315 | } | |
1316 | ||
ab409402 PB |
1317 | #define IO_RSRC_REF_BATCH 100 |
1318 | ||
1319 | static inline void io_req_put_rsrc_locked(struct io_kiocb *req, | |
1320 | struct io_ring_ctx *ctx) | |
1321 | __must_hold(&ctx->uring_lock) | |
36f72fe2 | 1322 | { |
ab409402 PB |
1323 | struct percpu_ref *ref = req->fixed_rsrc_refs; |
1324 | ||
1325 | if (ref) { | |
1326 | if (ref == &ctx->rsrc_node->refs) | |
1327 | ctx->rsrc_cached_refs++; | |
1328 | else | |
1329 | percpu_ref_put(ref); | |
1330 | } | |
1331 | } | |
1332 | ||
1333 | static inline void io_req_put_rsrc(struct io_kiocb *req, struct io_ring_ctx *ctx) | |
1334 | { | |
1335 | if (req->fixed_rsrc_refs) | |
1336 | percpu_ref_put(req->fixed_rsrc_refs); | |
1337 | } | |
1338 | ||
1339 | static __cold void io_rsrc_refs_drop(struct io_ring_ctx *ctx) | |
1340 | __must_hold(&ctx->uring_lock) | |
1341 | { | |
1342 | if (ctx->rsrc_cached_refs) { | |
1343 | percpu_ref_put_many(&ctx->rsrc_node->refs, ctx->rsrc_cached_refs); | |
1344 | ctx->rsrc_cached_refs = 0; | |
1345 | } | |
1346 | } | |
1347 | ||
1348 | static void io_rsrc_refs_refill(struct io_ring_ctx *ctx) | |
1349 | __must_hold(&ctx->uring_lock) | |
1350 | { | |
1351 | ctx->rsrc_cached_refs += IO_RSRC_REF_BATCH; | |
1352 | percpu_ref_get_many(&ctx->rsrc_node->refs, IO_RSRC_REF_BATCH); | |
1353 | } | |
36f72fe2 | 1354 | |
a46be971 | 1355 | static inline void io_req_set_rsrc_node(struct io_kiocb *req, |
5106dd6e JA |
1356 | struct io_ring_ctx *ctx, |
1357 | unsigned int issue_flags) | |
36f72fe2 | 1358 | { |
269bbe5f | 1359 | if (!req->fixed_rsrc_refs) { |
a7f0ed5a | 1360 | req->fixed_rsrc_refs = &ctx->rsrc_node->refs; |
5106dd6e JA |
1361 | |
1362 | if (!(issue_flags & IO_URING_F_UNLOCKED)) { | |
1363 | lockdep_assert_held(&ctx->uring_lock); | |
1364 | ctx->rsrc_cached_refs--; | |
1365 | if (unlikely(ctx->rsrc_cached_refs < 0)) | |
1366 | io_rsrc_refs_refill(ctx); | |
1367 | } else { | |
1368 | percpu_ref_get(req->fixed_rsrc_refs); | |
1369 | } | |
36f72fe2 PB |
1370 | } |
1371 | } | |
1372 | ||
cc3cec83 | 1373 | static unsigned int __io_put_kbuf(struct io_kiocb *req, struct list_head *list) |
3648e526 | 1374 | { |
d1fd1c20 | 1375 | struct io_buffer *kbuf = req->kbuf; |
3648e526 HX |
1376 | unsigned int cflags; |
1377 | ||
cc3cec83 | 1378 | cflags = IORING_CQE_F_BUFFER | (kbuf->bid << IORING_CQE_BUFFER_SHIFT); |
3648e526 | 1379 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
cc3cec83 | 1380 | list_add(&kbuf->list, list); |
d1fd1c20 | 1381 | req->kbuf = NULL; |
3648e526 HX |
1382 | return cflags; |
1383 | } | |
1384 | ||
cc3cec83 | 1385 | static inline unsigned int io_put_kbuf_comp(struct io_kiocb *req) |
3648e526 | 1386 | { |
8197b053 PB |
1387 | lockdep_assert_held(&req->ctx->completion_lock); |
1388 | ||
3648e526 HX |
1389 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) |
1390 | return 0; | |
cc3cec83 JA |
1391 | return __io_put_kbuf(req, &req->ctx->io_buffers_comp); |
1392 | } | |
1393 | ||
1394 | static inline unsigned int io_put_kbuf(struct io_kiocb *req, | |
1395 | unsigned issue_flags) | |
1396 | { | |
1397 | unsigned int cflags; | |
1398 | ||
1399 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) | |
1400 | return 0; | |
1401 | ||
1402 | /* | |
1403 | * We can add this buffer back to two lists: | |
1404 | * | |
1405 | * 1) The io_buffers_cache list. This one is protected by the | |
1406 | * ctx->uring_lock. If we already hold this lock, add back to this | |
1407 | * list as we can grab it from issue as well. | |
1408 | * 2) The io_buffers_comp list. This one is protected by the | |
1409 | * ctx->completion_lock. | |
1410 | * | |
1411 | * We migrate buffers from the comp_list to the issue cache list | |
1412 | * when we need one. | |
1413 | */ | |
1414 | if (issue_flags & IO_URING_F_UNLOCKED) { | |
1415 | struct io_ring_ctx *ctx = req->ctx; | |
1416 | ||
1417 | spin_lock(&ctx->completion_lock); | |
1418 | cflags = __io_put_kbuf(req, &ctx->io_buffers_comp); | |
1419 | spin_unlock(&ctx->completion_lock); | |
1420 | } else { | |
ab0ac095 PB |
1421 | lockdep_assert_held(&req->ctx->uring_lock); |
1422 | ||
cc3cec83 JA |
1423 | cflags = __io_put_kbuf(req, &req->ctx->io_buffers_cache); |
1424 | } | |
1425 | ||
1426 | return cflags; | |
3648e526 HX |
1427 | } |
1428 | ||
dbc7d452 JA |
1429 | static struct io_buffer_list *io_buffer_get_list(struct io_ring_ctx *ctx, |
1430 | unsigned int bgid) | |
1431 | { | |
1432 | struct list_head *hash_list; | |
1433 | struct io_buffer_list *bl; | |
1434 | ||
1435 | hash_list = &ctx->io_buffers[hash_32(bgid, IO_BUFFERS_HASH_BITS)]; | |
1436 | list_for_each_entry(bl, hash_list, list) | |
1437 | if (bl->bgid == bgid || bgid == -1U) | |
1438 | return bl; | |
1439 | ||
1440 | return NULL; | |
1441 | } | |
1442 | ||
4d55f238 | 1443 | static void io_kbuf_recycle(struct io_kiocb *req, unsigned issue_flags) |
b1c62645 JA |
1444 | { |
1445 | struct io_ring_ctx *ctx = req->ctx; | |
dbc7d452 JA |
1446 | struct io_buffer_list *bl; |
1447 | struct io_buffer *buf; | |
b1c62645 JA |
1448 | |
1449 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) | |
1450 | return; | |
8a3e8ee5 JA |
1451 | /* don't recycle if we already did IO to this buffer */ |
1452 | if (req->flags & REQ_F_PARTIAL_IO) | |
1453 | return; | |
b1c62645 | 1454 | |
f8929630 | 1455 | io_ring_submit_lock(ctx, issue_flags); |
b1c62645 JA |
1456 | |
1457 | buf = req->kbuf; | |
dbc7d452 JA |
1458 | bl = io_buffer_get_list(ctx, buf->bgid); |
1459 | list_add(&buf->list, &bl->buf_list); | |
b1c62645 JA |
1460 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
1461 | req->kbuf = NULL; | |
4d55f238 | 1462 | |
f8929630 | 1463 | io_ring_submit_unlock(ctx, issue_flags); |
b1c62645 JA |
1464 | } |
1465 | ||
3dd0c97a PB |
1466 | static bool io_match_task(struct io_kiocb *head, struct task_struct *task, |
1467 | bool cancel_all) | |
6af3f48b | 1468 | __must_hold(&req->ctx->timeout_lock) |
08d23634 | 1469 | { |
68207680 | 1470 | if (task && head->task != task) |
08d23634 | 1471 | return false; |
d5361233 | 1472 | return cancel_all; |
6af3f48b PB |
1473 | } |
1474 | ||
1475 | /* | |
1476 | * As io_match_task() but protected against racing with linked timeouts. | |
1477 | * User must not hold timeout_lock. | |
1478 | */ | |
1479 | static bool io_match_task_safe(struct io_kiocb *head, struct task_struct *task, | |
1480 | bool cancel_all) | |
1481 | { | |
6af3f48b PB |
1482 | if (task && head->task != task) |
1483 | return false; | |
d5361233 | 1484 | return cancel_all; |
6af3f48b PB |
1485 | } |
1486 | ||
d886e185 PB |
1487 | static inline bool req_has_async_data(struct io_kiocb *req) |
1488 | { | |
1489 | return req->flags & REQ_F_ASYNC_DATA; | |
1490 | } | |
1491 | ||
93d2bcd2 | 1492 | static inline void req_set_fail(struct io_kiocb *req) |
c40f6379 | 1493 | { |
93d2bcd2 | 1494 | req->flags |= REQ_F_FAIL; |
04c76b41 PB |
1495 | if (req->flags & REQ_F_CQE_SKIP) { |
1496 | req->flags &= ~REQ_F_CQE_SKIP; | |
1497 | req->flags |= REQ_F_SKIP_LINK_CQES; | |
1498 | } | |
c40f6379 | 1499 | } |
4a38aed2 | 1500 | |
a8295b98 HX |
1501 | static inline void req_fail_link_node(struct io_kiocb *req, int res) |
1502 | { | |
1503 | req_set_fail(req); | |
1504 | req->result = res; | |
1505 | } | |
1506 | ||
c072481d | 1507 | static __cold void io_ring_ctx_ref_free(struct percpu_ref *ref) |
2b188cc1 JA |
1508 | { |
1509 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
1510 | ||
0f158b4c | 1511 | complete(&ctx->ref_comp); |
2b188cc1 JA |
1512 | } |
1513 | ||
8eb7e2d0 PB |
1514 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
1515 | { | |
1516 | return !req->timeout.off; | |
1517 | } | |
1518 | ||
c072481d | 1519 | static __cold void io_fallback_req_func(struct work_struct *work) |
f56165e6 PB |
1520 | { |
1521 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, | |
1522 | fallback_work.work); | |
1523 | struct llist_node *node = llist_del_all(&ctx->fallback_llist); | |
1524 | struct io_kiocb *req, *tmp; | |
f237c30a | 1525 | bool locked = false; |
f56165e6 PB |
1526 | |
1527 | percpu_ref_get(&ctx->refs); | |
1528 | llist_for_each_entry_safe(req, tmp, node, io_task_work.fallback_node) | |
f237c30a | 1529 | req->io_task_work.func(req, &locked); |
5636c00d | 1530 | |
f237c30a | 1531 | if (locked) { |
c450178d | 1532 | io_submit_flush_completions(ctx); |
f237c30a PB |
1533 | mutex_unlock(&ctx->uring_lock); |
1534 | } | |
f56165e6 PB |
1535 | percpu_ref_put(&ctx->refs); |
1536 | } | |
1537 | ||
c072481d | 1538 | static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
2b188cc1 JA |
1539 | { |
1540 | struct io_ring_ctx *ctx; | |
dbc7d452 | 1541 | int i, hash_bits; |
2b188cc1 JA |
1542 | |
1543 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1544 | if (!ctx) | |
1545 | return NULL; | |
1546 | ||
78076bb6 JA |
1547 | /* |
1548 | * Use 5 bits less than the max cq entries, that should give us around | |
1549 | * 32 entries per hash list if totally full and uniformly spread. | |
1550 | */ | |
1551 | hash_bits = ilog2(p->cq_entries); | |
1552 | hash_bits -= 5; | |
1553 | if (hash_bits <= 0) | |
1554 | hash_bits = 1; | |
1555 | ctx->cancel_hash_bits = hash_bits; | |
1556 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
1557 | GFP_KERNEL); | |
1558 | if (!ctx->cancel_hash) | |
1559 | goto err; | |
1560 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
1561 | ||
6224843d PB |
1562 | ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL); |
1563 | if (!ctx->dummy_ubuf) | |
1564 | goto err; | |
1565 | /* set invalid range, so io_import_fixed() fails meeting it */ | |
1566 | ctx->dummy_ubuf->ubuf = -1UL; | |
1567 | ||
dbc7d452 JA |
1568 | ctx->io_buffers = kcalloc(1U << IO_BUFFERS_HASH_BITS, |
1569 | sizeof(struct list_head), GFP_KERNEL); | |
1570 | if (!ctx->io_buffers) | |
1571 | goto err; | |
1572 | for (i = 0; i < (1U << IO_BUFFERS_HASH_BITS); i++) | |
1573 | INIT_LIST_HEAD(&ctx->io_buffers[i]); | |
1574 | ||
21482896 | 1575 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
1576 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
1577 | goto err; | |
2b188cc1 JA |
1578 | |
1579 | ctx->flags = p->flags; | |
90554200 | 1580 | init_waitqueue_head(&ctx->sqo_sq_wait); |
69fb2131 | 1581 | INIT_LIST_HEAD(&ctx->sqd_list); |
1d7bb1d5 | 1582 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
cc3cec83 | 1583 | INIT_LIST_HEAD(&ctx->io_buffers_cache); |
4d9237e3 | 1584 | INIT_LIST_HEAD(&ctx->apoll_cache); |
0f158b4c | 1585 | init_completion(&ctx->ref_comp); |
61cf9370 | 1586 | xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1); |
2b188cc1 | 1587 | mutex_init(&ctx->uring_lock); |
311997b3 | 1588 | init_waitqueue_head(&ctx->cq_wait); |
2b188cc1 | 1589 | spin_lock_init(&ctx->completion_lock); |
89850fce | 1590 | spin_lock_init(&ctx->timeout_lock); |
5eef4e87 | 1591 | INIT_WQ_LIST(&ctx->iopoll_list); |
cc3cec83 JA |
1592 | INIT_LIST_HEAD(&ctx->io_buffers_pages); |
1593 | INIT_LIST_HEAD(&ctx->io_buffers_comp); | |
de0617e4 | 1594 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 1595 | INIT_LIST_HEAD(&ctx->timeout_list); |
ef9dd637 | 1596 | INIT_LIST_HEAD(&ctx->ltimeout_list); |
d67d2263 BM |
1597 | spin_lock_init(&ctx->rsrc_ref_lock); |
1598 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); | |
269bbe5f BM |
1599 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
1600 | init_llist_head(&ctx->rsrc_put_llist); | |
13bf43f5 | 1601 | INIT_LIST_HEAD(&ctx->tctx_list); |
c2b6c6bc PB |
1602 | ctx->submit_state.free_list.next = NULL; |
1603 | INIT_WQ_LIST(&ctx->locked_free_list); | |
9011bf9a | 1604 | INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func); |
6f33b0bc | 1605 | INIT_WQ_LIST(&ctx->submit_state.compl_reqs); |
2b188cc1 | 1606 | return ctx; |
206aefde | 1607 | err: |
6224843d | 1608 | kfree(ctx->dummy_ubuf); |
78076bb6 | 1609 | kfree(ctx->cancel_hash); |
dbc7d452 | 1610 | kfree(ctx->io_buffers); |
206aefde JA |
1611 | kfree(ctx); |
1612 | return NULL; | |
2b188cc1 JA |
1613 | } |
1614 | ||
8f6ed49a PB |
1615 | static void io_account_cq_overflow(struct io_ring_ctx *ctx) |
1616 | { | |
1617 | struct io_rings *r = ctx->rings; | |
1618 | ||
1619 | WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1); | |
1620 | ctx->cq_extra--; | |
1621 | } | |
1622 | ||
9cf7c104 | 1623 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
7adf4eaf | 1624 | { |
2bc9930e JA |
1625 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
1626 | struct io_ring_ctx *ctx = req->ctx; | |
a197f664 | 1627 | |
8f6ed49a | 1628 | return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail; |
2bc9930e | 1629 | } |
de0617e4 | 1630 | |
9d858b21 | 1631 | return false; |
de0617e4 JA |
1632 | } |
1633 | ||
35645ac3 PB |
1634 | #define FFS_NOWAIT 0x1UL |
1635 | #define FFS_ISREG 0x2UL | |
1636 | #define FFS_MASK ~(FFS_NOWAIT|FFS_ISREG) | |
c97d8a0f PB |
1637 | |
1638 | static inline bool io_req_ffs_set(struct io_kiocb *req) | |
1639 | { | |
35645ac3 | 1640 | return req->flags & REQ_F_FIXED_FILE; |
c97d8a0f PB |
1641 | } |
1642 | ||
fd08e530 PB |
1643 | static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req) |
1644 | { | |
906c6caa PB |
1645 | if (WARN_ON_ONCE(!req->link)) |
1646 | return NULL; | |
1647 | ||
4d13d1a4 PB |
1648 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
1649 | req->flags |= REQ_F_LINK_TIMEOUT; | |
fd08e530 PB |
1650 | |
1651 | /* linked timeouts should have two refs once prep'ed */ | |
48dcd38d | 1652 | io_req_set_refcount(req); |
4d13d1a4 PB |
1653 | __io_req_set_refcount(req->link, 2); |
1654 | return req->link; | |
fd08e530 PB |
1655 | } |
1656 | ||
1657 | static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) | |
1658 | { | |
4d13d1a4 | 1659 | if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT))) |
fd08e530 PB |
1660 | return NULL; |
1661 | return __io_prep_linked_timeout(req); | |
1662 | } | |
1663 | ||
1e6fa521 JA |
1664 | static void io_prep_async_work(struct io_kiocb *req) |
1665 | { | |
1666 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
1e6fa521 JA |
1667 | struct io_ring_ctx *ctx = req->ctx; |
1668 | ||
b8e64b53 PB |
1669 | if (!(req->flags & REQ_F_CREDS)) { |
1670 | req->flags |= REQ_F_CREDS; | |
c10d1f98 | 1671 | req->creds = get_current_cred(); |
b8e64b53 | 1672 | } |
003e8dcc | 1673 | |
e1d675df PB |
1674 | req->work.list.next = NULL; |
1675 | req->work.flags = 0; | |
feaadc4f PB |
1676 | if (req->flags & REQ_F_FORCE_ASYNC) |
1677 | req->work.flags |= IO_WQ_WORK_CONCURRENT; | |
1678 | ||
1e6fa521 JA |
1679 | if (req->flags & REQ_F_ISREG) { |
1680 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) | |
1681 | io_wq_hash_work(&req->work, file_inode(req->file)); | |
4b982bd0 | 1682 | } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) { |
1e6fa521 JA |
1683 | if (def->unbound_nonreg_file) |
1684 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1685 | } | |
561fb04a | 1686 | } |
cccf0ee8 | 1687 | |
cbdcb435 | 1688 | static void io_prep_async_link(struct io_kiocb *req) |
561fb04a | 1689 | { |
cbdcb435 | 1690 | struct io_kiocb *cur; |
54a91f3b | 1691 | |
44eff40a PB |
1692 | if (req->flags & REQ_F_LINK_TIMEOUT) { |
1693 | struct io_ring_ctx *ctx = req->ctx; | |
1694 | ||
674ee8e1 | 1695 | spin_lock_irq(&ctx->timeout_lock); |
44eff40a PB |
1696 | io_for_each_link(cur, req) |
1697 | io_prep_async_work(cur); | |
674ee8e1 | 1698 | spin_unlock_irq(&ctx->timeout_lock); |
44eff40a PB |
1699 | } else { |
1700 | io_for_each_link(cur, req) | |
1701 | io_prep_async_work(cur); | |
1702 | } | |
561fb04a JA |
1703 | } |
1704 | ||
fff4e40e PB |
1705 | static inline void io_req_add_compl_list(struct io_kiocb *req) |
1706 | { | |
775a1f2f | 1707 | struct io_submit_state *state = &req->ctx->submit_state; |
fff4e40e | 1708 | |
3d4aeb9f | 1709 | if (!(req->flags & REQ_F_CQE_SKIP)) |
775a1f2f | 1710 | state->flush_cqes = true; |
fff4e40e PB |
1711 | wq_list_add_tail(&req->comp_list, &state->compl_reqs); |
1712 | } | |
1713 | ||
00169246 | 1714 | static void io_queue_async_work(struct io_kiocb *req, bool *dont_use) |
561fb04a | 1715 | { |
a197f664 | 1716 | struct io_ring_ctx *ctx = req->ctx; |
cbdcb435 | 1717 | struct io_kiocb *link = io_prep_linked_timeout(req); |
5aa75ed5 | 1718 | struct io_uring_task *tctx = req->task->io_uring; |
561fb04a | 1719 | |
3bfe6106 JA |
1720 | BUG_ON(!tctx); |
1721 | BUG_ON(!tctx->io_wq); | |
561fb04a | 1722 | |
cbdcb435 PB |
1723 | /* init ->work of the whole link before punting */ |
1724 | io_prep_async_link(req); | |
991468dc JA |
1725 | |
1726 | /* | |
1727 | * Not expected to happen, but if we do have a bug where this _can_ | |
1728 | * happen, catch it here and ensure the request is marked as | |
1729 | * canceled. That will make io-wq go through the usual work cancel | |
1730 | * procedure rather than attempt to run this request (or create a new | |
1731 | * worker for it). | |
1732 | */ | |
1733 | if (WARN_ON_ONCE(!same_thread_group(req->task, current))) | |
1734 | req->work.flags |= IO_WQ_WORK_CANCEL; | |
1735 | ||
502c87d6 SR |
1736 | trace_io_uring_queue_async_work(ctx, req, req->user_data, req->opcode, req->flags, |
1737 | &req->work, io_wq_is_hashed(&req->work)); | |
ebf93667 | 1738 | io_wq_enqueue(tctx->io_wq, &req->work); |
7271ef3a JA |
1739 | if (link) |
1740 | io_queue_linked_timeout(link); | |
cbdcb435 PB |
1741 | } |
1742 | ||
1ee4160c | 1743 | static void io_kill_timeout(struct io_kiocb *req, int status) |
8c855885 | 1744 | __must_hold(&req->ctx->completion_lock) |
89850fce | 1745 | __must_hold(&req->ctx->timeout_lock) |
5262f567 | 1746 | { |
e8c2bc1f | 1747 | struct io_timeout_data *io = req->async_data; |
5262f567 | 1748 | |
fd9c7bc5 | 1749 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
2ae2eb9d PB |
1750 | if (status) |
1751 | req_set_fail(req); | |
01cec8c1 PB |
1752 | atomic_set(&req->ctx->cq_timeouts, |
1753 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
135fcde8 | 1754 | list_del_init(&req->timeout.list); |
913a571a | 1755 | io_fill_cqe_req(req, status, 0); |
91c2f697 | 1756 | io_put_req_deferred(req); |
5262f567 JA |
1757 | } |
1758 | } | |
1759 | ||
c072481d | 1760 | static __cold void io_queue_deferred(struct io_ring_ctx *ctx) |
de0617e4 | 1761 | { |
441b8a78 | 1762 | while (!list_empty(&ctx->defer_list)) { |
27dc8338 PB |
1763 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
1764 | struct io_defer_entry, list); | |
de0617e4 | 1765 | |
9cf7c104 | 1766 | if (req_need_defer(de->req, de->seq)) |
04518945 | 1767 | break; |
27dc8338 | 1768 | list_del_init(&de->list); |
907d1df3 | 1769 | io_req_task_queue(de->req); |
27dc8338 | 1770 | kfree(de); |
441b8a78 | 1771 | } |
04518945 PB |
1772 | } |
1773 | ||
c072481d | 1774 | static __cold void io_flush_timeouts(struct io_ring_ctx *ctx) |
89850fce | 1775 | __must_hold(&ctx->completion_lock) |
de0617e4 | 1776 | { |
441b8a78 | 1777 | u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
e677edbc | 1778 | struct io_kiocb *req, *tmp; |
f010505b | 1779 | |
79ebeaee | 1780 | spin_lock_irq(&ctx->timeout_lock); |
e677edbc | 1781 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
f010505b | 1782 | u32 events_needed, events_got; |
de0617e4 | 1783 | |
8eb7e2d0 | 1784 | if (io_is_timeout_noseq(req)) |
360428f8 | 1785 | break; |
f010505b MDG |
1786 | |
1787 | /* | |
1788 | * Since seq can easily wrap around over time, subtract | |
1789 | * the last seq at which timeouts were flushed before comparing. | |
1790 | * Assuming not more than 2^31-1 events have happened since, | |
1791 | * these subtractions won't have wrapped, so we can check if | |
1792 | * target is in [last_seq, current_seq] by comparing the two. | |
1793 | */ | |
1794 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; | |
1795 | events_got = seq - ctx->cq_last_tm_flush; | |
1796 | if (events_got < events_needed) | |
360428f8 | 1797 | break; |
bfe68a22 | 1798 | |
1ee4160c | 1799 | io_kill_timeout(req, 0); |
f18ee4cf | 1800 | } |
f010505b | 1801 | ctx->cq_last_tm_flush = seq; |
79ebeaee | 1802 | spin_unlock_irq(&ctx->timeout_lock); |
360428f8 | 1803 | } |
5262f567 | 1804 | |
9333f6b4 PB |
1805 | static inline void io_commit_cqring(struct io_ring_ctx *ctx) |
1806 | { | |
1807 | /* order cqe stores with ring update */ | |
1808 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); | |
1809 | } | |
1810 | ||
9aa8dfde | 1811 | static void __io_commit_cqring_flush(struct io_ring_ctx *ctx) |
360428f8 | 1812 | { |
9aa8dfde PB |
1813 | if (ctx->off_timeout_used || ctx->drain_active) { |
1814 | spin_lock(&ctx->completion_lock); | |
1815 | if (ctx->off_timeout_used) | |
1816 | io_flush_timeouts(ctx); | |
1817 | if (ctx->drain_active) | |
1818 | io_queue_deferred(ctx); | |
1819 | io_commit_cqring(ctx); | |
1820 | spin_unlock(&ctx->completion_lock); | |
1821 | } | |
1822 | if (ctx->has_evfd) | |
1823 | io_eventfd_signal(ctx); | |
de0617e4 JA |
1824 | } |
1825 | ||
90554200 JA |
1826 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
1827 | { | |
1828 | struct io_rings *r = ctx->rings; | |
1829 | ||
a566c556 | 1830 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries; |
90554200 JA |
1831 | } |
1832 | ||
888aae2e PB |
1833 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
1834 | { | |
1835 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); | |
1836 | } | |
1837 | ||
d068b506 | 1838 | static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx) |
2b188cc1 | 1839 | { |
75b28aff | 1840 | struct io_rings *rings = ctx->rings; |
ea5ab3b5 | 1841 | unsigned tail, mask = ctx->cq_entries - 1; |
2b188cc1 | 1842 | |
115e12e5 SB |
1843 | /* |
1844 | * writes to the cq entry need to come after reading head; the | |
1845 | * control dependency is enough as we're using WRITE_ONCE to | |
1846 | * fill the cq entry | |
1847 | */ | |
a566c556 | 1848 | if (__io_cqring_events(ctx) == ctx->cq_entries) |
2b188cc1 JA |
1849 | return NULL; |
1850 | ||
888aae2e | 1851 | tail = ctx->cached_cq_tail++; |
ea5ab3b5 | 1852 | return &rings->cqes[tail & mask]; |
2b188cc1 JA |
1853 | } |
1854 | ||
77bc59b4 | 1855 | static void io_eventfd_signal(struct io_ring_ctx *ctx) |
f2842ab5 | 1856 | { |
77bc59b4 UA |
1857 | struct io_ev_fd *ev_fd; |
1858 | ||
77bc59b4 UA |
1859 | rcu_read_lock(); |
1860 | /* | |
1861 | * rcu_dereference ctx->io_ev_fd once and use it for both for checking | |
1862 | * and eventfd_signal | |
1863 | */ | |
1864 | ev_fd = rcu_dereference(ctx->io_ev_fd); | |
1865 | ||
1866 | /* | |
1867 | * Check again if ev_fd exists incase an io_eventfd_unregister call | |
1868 | * completed between the NULL check of ctx->io_ev_fd at the start of | |
1869 | * the function and rcu_read_lock. | |
1870 | */ | |
1871 | if (unlikely(!ev_fd)) | |
1872 | goto out; | |
7e55a19c | 1873 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
77bc59b4 UA |
1874 | goto out; |
1875 | ||
c75312dd | 1876 | if (!ev_fd->eventfd_async || io_wq_current_is_worker()) |
77bc59b4 | 1877 | eventfd_signal(ev_fd->cq_ev_fd, 1); |
77bc59b4 UA |
1878 | out: |
1879 | rcu_read_unlock(); | |
f2842ab5 JA |
1880 | } |
1881 | ||
9aa8dfde PB |
1882 | static inline void io_cqring_wake(struct io_ring_ctx *ctx) |
1883 | { | |
1884 | /* | |
1885 | * wake_up_all() may seem excessive, but io_wake_function() and | |
1886 | * io_should_wake() handle the termination of the loop and only | |
1887 | * wake as many waiters as we need to. | |
1888 | */ | |
1889 | if (wq_has_sleeper(&ctx->cq_wait)) | |
1890 | wake_up_all(&ctx->cq_wait); | |
1891 | } | |
1892 | ||
2c5d763c JA |
1893 | /* |
1894 | * This should only get called when at least one event has been posted. | |
1895 | * Some applications rely on the eventfd notification count only changing | |
1896 | * IFF a new CQE has been added to the CQ ring. There's no depedency on | |
1897 | * 1:1 relationship between how many times this function is called (and | |
1898 | * hence the eventfd count) and number of CQEs posted to the CQ ring. | |
1899 | */ | |
66fc25ca | 1900 | static inline void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
1d7bb1d5 | 1901 | { |
9aa8dfde PB |
1902 | if (unlikely(ctx->off_timeout_used || ctx->drain_active || |
1903 | ctx->has_evfd)) | |
9333f6b4 PB |
1904 | __io_commit_cqring_flush(ctx); |
1905 | ||
9aa8dfde | 1906 | io_cqring_wake(ctx); |
1d7bb1d5 JA |
1907 | } |
1908 | ||
80c18e4a PB |
1909 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
1910 | { | |
9aa8dfde PB |
1911 | if (unlikely(ctx->off_timeout_used || ctx->drain_active || |
1912 | ctx->has_evfd)) | |
9333f6b4 PB |
1913 | __io_commit_cqring_flush(ctx); |
1914 | ||
9aa8dfde PB |
1915 | if (ctx->flags & IORING_SETUP_SQPOLL) |
1916 | io_cqring_wake(ctx); | |
80c18e4a PB |
1917 | } |
1918 | ||
c4a2ed72 | 1919 | /* Returns true if there are no backlogged entries after the flush */ |
6c2450ae | 1920 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force) |
1d7bb1d5 | 1921 | { |
b18032bb | 1922 | bool all_flushed, posted; |
1d7bb1d5 | 1923 | |
a566c556 | 1924 | if (!force && __io_cqring_events(ctx) == ctx->cq_entries) |
e23de15f | 1925 | return false; |
1d7bb1d5 | 1926 | |
b18032bb | 1927 | posted = false; |
79ebeaee | 1928 | spin_lock(&ctx->completion_lock); |
6c2450ae | 1929 | while (!list_empty(&ctx->cq_overflow_list)) { |
d068b506 | 1930 | struct io_uring_cqe *cqe = io_get_cqe(ctx); |
6c2450ae | 1931 | struct io_overflow_cqe *ocqe; |
e6c8aa9a | 1932 | |
1d7bb1d5 JA |
1933 | if (!cqe && !force) |
1934 | break; | |
6c2450ae PB |
1935 | ocqe = list_first_entry(&ctx->cq_overflow_list, |
1936 | struct io_overflow_cqe, list); | |
1937 | if (cqe) | |
1938 | memcpy(cqe, &ocqe->cqe, sizeof(*cqe)); | |
1939 | else | |
8f6ed49a PB |
1940 | io_account_cq_overflow(ctx); |
1941 | ||
b18032bb | 1942 | posted = true; |
6c2450ae PB |
1943 | list_del(&ocqe->list); |
1944 | kfree(ocqe); | |
1d7bb1d5 JA |
1945 | } |
1946 | ||
09e88404 PB |
1947 | all_flushed = list_empty(&ctx->cq_overflow_list); |
1948 | if (all_flushed) { | |
5ed7a37d | 1949 | clear_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
1950 | WRITE_ONCE(ctx->rings->sq_flags, |
1951 | ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW); | |
09e88404 | 1952 | } |
46930143 | 1953 | |
60053be8 | 1954 | io_commit_cqring(ctx); |
79ebeaee | 1955 | spin_unlock(&ctx->completion_lock); |
b18032bb JA |
1956 | if (posted) |
1957 | io_cqring_ev_posted(ctx); | |
09e88404 | 1958 | return all_flushed; |
1d7bb1d5 JA |
1959 | } |
1960 | ||
90f67366 | 1961 | static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx) |
6c503150 | 1962 | { |
ca0a2651 JA |
1963 | bool ret = true; |
1964 | ||
5ed7a37d | 1965 | if (test_bit(0, &ctx->check_cq_overflow)) { |
6c503150 PB |
1966 | /* iopoll syncs against uring_lock, not completion_lock */ |
1967 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1968 | mutex_lock(&ctx->uring_lock); | |
90f67366 | 1969 | ret = __io_cqring_overflow_flush(ctx, false); |
6c503150 PB |
1970 | if (ctx->flags & IORING_SETUP_IOPOLL) |
1971 | mutex_unlock(&ctx->uring_lock); | |
1972 | } | |
ca0a2651 JA |
1973 | |
1974 | return ret; | |
6c503150 PB |
1975 | } |
1976 | ||
9d170164 | 1977 | static void __io_put_task(struct task_struct *task, int nr) |
6a290a14 PB |
1978 | { |
1979 | struct io_uring_task *tctx = task->io_uring; | |
1980 | ||
9d170164 PB |
1981 | percpu_counter_sub(&tctx->inflight, nr); |
1982 | if (unlikely(atomic_read(&tctx->in_idle))) | |
1983 | wake_up(&tctx->wait); | |
1984 | put_task_struct_many(task, nr); | |
1985 | } | |
1986 | ||
1987 | /* must to be called somewhat shortly after putting a request */ | |
1988 | static inline void io_put_task(struct task_struct *task, int nr) | |
1989 | { | |
1990 | if (likely(task == current)) | |
1991 | task->io_uring->cached_refs += nr; | |
1992 | else | |
1993 | __io_put_task(task, nr); | |
6a290a14 PB |
1994 | } |
1995 | ||
9a10867a PB |
1996 | static void io_task_refs_refill(struct io_uring_task *tctx) |
1997 | { | |
1998 | unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR; | |
1999 | ||
2000 | percpu_counter_add(&tctx->inflight, refill); | |
2001 | refcount_add(refill, ¤t->usage); | |
2002 | tctx->cached_refs += refill; | |
2003 | } | |
2004 | ||
2005 | static inline void io_get_task_refs(int nr) | |
2006 | { | |
2007 | struct io_uring_task *tctx = current->io_uring; | |
2008 | ||
2009 | tctx->cached_refs -= nr; | |
2010 | if (unlikely(tctx->cached_refs < 0)) | |
2011 | io_task_refs_refill(tctx); | |
2012 | } | |
2013 | ||
3cc7fdb9 PB |
2014 | static __cold void io_uring_drop_tctx_refs(struct task_struct *task) |
2015 | { | |
2016 | struct io_uring_task *tctx = task->io_uring; | |
2017 | unsigned int refs = tctx->cached_refs; | |
2018 | ||
2019 | if (refs) { | |
2020 | tctx->cached_refs = 0; | |
2021 | percpu_counter_sub(&tctx->inflight, refs); | |
2022 | put_task_struct_many(task, refs); | |
2023 | } | |
2024 | } | |
2025 | ||
d4d19c19 | 2026 | static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data, |
54daa9b2 | 2027 | s32 res, u32 cflags) |
2b188cc1 | 2028 | { |
cce4b8b0 | 2029 | struct io_overflow_cqe *ocqe; |
2b188cc1 | 2030 | |
cce4b8b0 PB |
2031 | ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT); |
2032 | if (!ocqe) { | |
2033 | /* | |
2034 | * If we're in ring overflow flush mode, or in task cancel mode, | |
2035 | * or cannot allocate an overflow entry, then we need to drop it | |
2036 | * on the floor. | |
2037 | */ | |
8f6ed49a | 2038 | io_account_cq_overflow(ctx); |
cce4b8b0 | 2039 | return false; |
2b188cc1 | 2040 | } |
cce4b8b0 | 2041 | if (list_empty(&ctx->cq_overflow_list)) { |
5ed7a37d | 2042 | set_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
2043 | WRITE_ONCE(ctx->rings->sq_flags, |
2044 | ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW); | |
2045 | ||
cce4b8b0 | 2046 | } |
d4d19c19 | 2047 | ocqe->cqe.user_data = user_data; |
cce4b8b0 PB |
2048 | ocqe->cqe.res = res; |
2049 | ocqe->cqe.flags = cflags; | |
2050 | list_add_tail(&ocqe->list, &ctx->cq_overflow_list); | |
2051 | return true; | |
2b188cc1 JA |
2052 | } |
2053 | ||
ae4da189 | 2054 | static inline bool __io_fill_cqe(struct io_ring_ctx *ctx, u64 user_data, |
913a571a | 2055 | s32 res, u32 cflags) |
2b188cc1 JA |
2056 | { |
2057 | struct io_uring_cqe *cqe; | |
2058 | ||
2059 | /* | |
2060 | * If we can't get a cq entry, userspace overflowed the | |
2061 | * submission (by quite a lot). Increment the overflow count in | |
2062 | * the ring. | |
2063 | */ | |
d068b506 | 2064 | cqe = io_get_cqe(ctx); |
1d7bb1d5 | 2065 | if (likely(cqe)) { |
d4d19c19 | 2066 | WRITE_ONCE(cqe->user_data, user_data); |
2b188cc1 | 2067 | WRITE_ONCE(cqe->res, res); |
bcda7baa | 2068 | WRITE_ONCE(cqe->flags, cflags); |
8d13326e | 2069 | return true; |
2b188cc1 | 2070 | } |
d4d19c19 | 2071 | return io_cqring_event_overflow(ctx, user_data, res, cflags); |
2b188cc1 JA |
2072 | } |
2073 | ||
ae4da189 | 2074 | static inline bool __io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags) |
d5ec1dfa | 2075 | { |
502c87d6 | 2076 | trace_io_uring_complete(req->ctx, req, req->user_data, res, cflags); |
ae4da189 | 2077 | return __io_fill_cqe(req->ctx, req->user_data, res, cflags); |
d5ec1dfa SR |
2078 | } |
2079 | ||
913a571a | 2080 | static noinline void io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags) |
bcda7baa | 2081 | { |
04c76b41 | 2082 | if (!(req->flags & REQ_F_CQE_SKIP)) |
ae4da189 | 2083 | __io_fill_cqe_req(req, res, cflags); |
bcda7baa JA |
2084 | } |
2085 | ||
913a571a PB |
2086 | static noinline bool io_fill_cqe_aux(struct io_ring_ctx *ctx, u64 user_data, |
2087 | s32 res, u32 cflags) | |
bcda7baa | 2088 | { |
913a571a | 2089 | ctx->cq_extra++; |
502c87d6 | 2090 | trace_io_uring_complete(ctx, NULL, user_data, res, cflags); |
ae4da189 | 2091 | return __io_fill_cqe(ctx, user_data, res, cflags); |
bcda7baa JA |
2092 | } |
2093 | ||
a37fae8a HX |
2094 | static void __io_req_complete_post(struct io_kiocb *req, s32 res, |
2095 | u32 cflags) | |
2b188cc1 | 2096 | { |
78e19bbe | 2097 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 | 2098 | |
04c76b41 | 2099 | if (!(req->flags & REQ_F_CQE_SKIP)) |
ae4da189 | 2100 | __io_fill_cqe_req(req, res, cflags); |
c7dae4ba JA |
2101 | /* |
2102 | * If we're the last reference to this request, add to our locked | |
2103 | * free_list cache. | |
2104 | */ | |
de9b4cca | 2105 | if (req_ref_put_and_test(req)) { |
7a612350 | 2106 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
0756a869 | 2107 | if (req->flags & IO_DISARM_MASK) |
7a612350 PB |
2108 | io_disarm_next(req); |
2109 | if (req->link) { | |
2110 | io_req_task_queue(req->link); | |
2111 | req->link = NULL; | |
2112 | } | |
2113 | } | |
ab409402 | 2114 | io_req_put_rsrc(req, ctx); |
8197b053 PB |
2115 | /* |
2116 | * Selected buffer deallocation in io_clean_op() assumes that | |
2117 | * we don't hold ->completion_lock. Clean them here to avoid | |
2118 | * deadlocks. | |
2119 | */ | |
2120 | io_put_kbuf_comp(req); | |
c7dae4ba JA |
2121 | io_dismantle_req(req); |
2122 | io_put_task(req->task, 1); | |
c2b6c6bc | 2123 | wq_list_add_head(&req->comp_list, &ctx->locked_free_list); |
d0acdee2 | 2124 | ctx->locked_free_nr++; |
180f829f | 2125 | } |
a37fae8a HX |
2126 | } |
2127 | ||
2128 | static void io_req_complete_post(struct io_kiocb *req, s32 res, | |
2129 | u32 cflags) | |
2130 | { | |
2131 | struct io_ring_ctx *ctx = req->ctx; | |
2132 | ||
2133 | spin_lock(&ctx->completion_lock); | |
2134 | __io_req_complete_post(req, res, cflags); | |
7a612350 | 2135 | io_commit_cqring(ctx); |
79ebeaee | 2136 | spin_unlock(&ctx->completion_lock); |
a3f34907 | 2137 | io_cqring_ev_posted(ctx); |
4e3d9ff9 JA |
2138 | } |
2139 | ||
54daa9b2 PB |
2140 | static inline void io_req_complete_state(struct io_kiocb *req, s32 res, |
2141 | u32 cflags) | |
229a7b63 | 2142 | { |
a38d68db | 2143 | req->result = res; |
d17e56eb | 2144 | req->cflags = cflags; |
e342c807 | 2145 | req->flags |= REQ_F_COMPLETE_INLINE; |
e1e16097 JA |
2146 | } |
2147 | ||
889fca73 | 2148 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
54daa9b2 | 2149 | s32 res, u32 cflags) |
bcda7baa | 2150 | { |
889fca73 PB |
2151 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
2152 | io_req_complete_state(req, res, cflags); | |
a38d68db | 2153 | else |
c7dae4ba | 2154 | io_req_complete_post(req, res, cflags); |
bcda7baa JA |
2155 | } |
2156 | ||
54daa9b2 | 2157 | static inline void io_req_complete(struct io_kiocb *req, s32 res) |
0ddf92e8 | 2158 | { |
889fca73 | 2159 | __io_req_complete(req, 0, res, 0); |
0ddf92e8 JA |
2160 | } |
2161 | ||
54daa9b2 | 2162 | static void io_req_complete_failed(struct io_kiocb *req, s32 res) |
f41db273 | 2163 | { |
93d2bcd2 | 2164 | req_set_fail(req); |
ab0ac095 | 2165 | io_req_complete_post(req, res, io_put_kbuf(req, IO_URING_F_UNLOCKED)); |
f41db273 PB |
2166 | } |
2167 | ||
c6d3d9cb PB |
2168 | static void io_req_complete_fail_submit(struct io_kiocb *req) |
2169 | { | |
2170 | /* | |
2171 | * We don't submit, fail them all, for that replace hardlinks with | |
2172 | * normal links. Extra REQ_F_LINK is tolerated. | |
2173 | */ | |
2174 | req->flags &= ~REQ_F_HARDLINK; | |
2175 | req->flags |= REQ_F_LINK; | |
2176 | io_req_complete_failed(req, req->result); | |
2177 | } | |
2178 | ||
864ea921 PB |
2179 | /* |
2180 | * Don't initialise the fields below on every allocation, but do that in | |
2181 | * advance and keep them valid across allocations. | |
2182 | */ | |
2183 | static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx) | |
2184 | { | |
2185 | req->ctx = ctx; | |
2186 | req->link = NULL; | |
2187 | req->async_data = NULL; | |
2188 | /* not necessary, but safer to zero */ | |
2189 | req->result = 0; | |
2190 | } | |
2191 | ||
dac7a098 | 2192 | static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx, |
cd0ca2e0 | 2193 | struct io_submit_state *state) |
dac7a098 | 2194 | { |
79ebeaee | 2195 | spin_lock(&ctx->completion_lock); |
c2b6c6bc | 2196 | wq_list_splice(&ctx->locked_free_list, &state->free_list); |
d0acdee2 | 2197 | ctx->locked_free_nr = 0; |
79ebeaee | 2198 | spin_unlock(&ctx->completion_lock); |
dac7a098 PB |
2199 | } |
2200 | ||
dd78f492 | 2201 | /* Returns true IFF there are requests in the cache */ |
c7dae4ba | 2202 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
0ddf92e8 | 2203 | { |
c7dae4ba | 2204 | struct io_submit_state *state = &ctx->submit_state; |
0ddf92e8 | 2205 | |
c7dae4ba JA |
2206 | /* |
2207 | * If we have more than a batch's worth of requests in our IRQ side | |
2208 | * locked cache, grab the lock and move them over to our submission | |
2209 | * side cache. | |
2210 | */ | |
d0acdee2 | 2211 | if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH) |
cd0ca2e0 | 2212 | io_flush_cached_locked_reqs(ctx, state); |
c2b6c6bc | 2213 | return !!state->free_list.next; |
0ddf92e8 JA |
2214 | } |
2215 | ||
5d5901a3 PB |
2216 | /* |
2217 | * A request might get retired back into the request caches even before opcode | |
2218 | * handlers and io_issue_sqe() are done with it, e.g. inline completion path. | |
2219 | * Because of that, io_alloc_req() should be called only under ->uring_lock | |
2220 | * and with extra caution to not get a request that is still worked on. | |
2221 | */ | |
c072481d | 2222 | static __cold bool __io_alloc_req_refill(struct io_ring_ctx *ctx) |
5d5901a3 | 2223 | __must_hold(&ctx->uring_lock) |
2b188cc1 | 2224 | { |
e5d1bc0a | 2225 | struct io_submit_state *state = &ctx->submit_state; |
864ea921 | 2226 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
3ab665b7 PB |
2227 | void *reqs[IO_REQ_ALLOC_BATCH]; |
2228 | struct io_kiocb *req; | |
864ea921 | 2229 | int ret, i; |
e5d1bc0a | 2230 | |
c2b6c6bc | 2231 | if (likely(state->free_list.next || io_flush_cached_reqs(ctx))) |
a33ae9ce | 2232 | return true; |
e5d1bc0a | 2233 | |
3ab665b7 | 2234 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, ARRAY_SIZE(reqs), reqs); |
fd6fab2c | 2235 | |
864ea921 PB |
2236 | /* |
2237 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
2238 | * retry single alloc to be on the safe side. | |
2239 | */ | |
2240 | if (unlikely(ret <= 0)) { | |
3ab665b7 PB |
2241 | reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
2242 | if (!reqs[0]) | |
a33ae9ce | 2243 | return false; |
864ea921 | 2244 | ret = 1; |
2b188cc1 | 2245 | } |
864ea921 | 2246 | |
37f0e767 | 2247 | percpu_ref_get_many(&ctx->refs, ret); |
3ab665b7 PB |
2248 | for (i = 0; i < ret; i++) { |
2249 | req = reqs[i]; | |
2250 | ||
2251 | io_preinit_req(req, ctx); | |
c2b6c6bc | 2252 | wq_stack_add_head(&req->comp_list, &state->free_list); |
3ab665b7 | 2253 | } |
a33ae9ce PB |
2254 | return true; |
2255 | } | |
2256 | ||
2257 | static inline bool io_alloc_req_refill(struct io_ring_ctx *ctx) | |
2258 | { | |
2259 | if (unlikely(!ctx->submit_state.free_list.next)) | |
2260 | return __io_alloc_req_refill(ctx); | |
2261 | return true; | |
2262 | } | |
2263 | ||
2264 | static inline struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) | |
2265 | { | |
2266 | struct io_wq_work_node *node; | |
2267 | ||
2268 | node = wq_stack_extract(&ctx->submit_state.free_list); | |
c2b6c6bc | 2269 | return container_of(node, struct io_kiocb, comp_list); |
2b188cc1 JA |
2270 | } |
2271 | ||
e1d767f0 | 2272 | static inline void io_put_file(struct file *file) |
8da11c19 | 2273 | { |
e1d767f0 | 2274 | if (file) |
8da11c19 PB |
2275 | fput(file); |
2276 | } | |
2277 | ||
6b639522 | 2278 | static inline void io_dismantle_req(struct io_kiocb *req) |
2b188cc1 | 2279 | { |
094bae49 | 2280 | unsigned int flags = req->flags; |
929a3af9 | 2281 | |
867f8fa5 | 2282 | if (unlikely(flags & IO_REQ_CLEAN_FLAGS)) |
3a0a6902 | 2283 | io_clean_op(req); |
e1d767f0 PB |
2284 | if (!(flags & REQ_F_FIXED_FILE)) |
2285 | io_put_file(req->file); | |
e65ef56d JA |
2286 | } |
2287 | ||
c072481d | 2288 | static __cold void __io_free_req(struct io_kiocb *req) |
c6ca97b3 | 2289 | { |
51a4cc11 | 2290 | struct io_ring_ctx *ctx = req->ctx; |
c6ca97b3 | 2291 | |
ab409402 | 2292 | io_req_put_rsrc(req, ctx); |
216578e5 | 2293 | io_dismantle_req(req); |
7c660731 | 2294 | io_put_task(req->task, 1); |
c6ca97b3 | 2295 | |
79ebeaee | 2296 | spin_lock(&ctx->completion_lock); |
c2b6c6bc | 2297 | wq_list_add_head(&req->comp_list, &ctx->locked_free_list); |
c34b025f | 2298 | ctx->locked_free_nr++; |
79ebeaee | 2299 | spin_unlock(&ctx->completion_lock); |
e65ef56d JA |
2300 | } |
2301 | ||
f2f87370 PB |
2302 | static inline void io_remove_next_linked(struct io_kiocb *req) |
2303 | { | |
2304 | struct io_kiocb *nxt = req->link; | |
2305 | ||
2306 | req->link = nxt->link; | |
2307 | nxt->link = NULL; | |
2308 | } | |
2309 | ||
33cc89a9 PB |
2310 | static bool io_kill_linked_timeout(struct io_kiocb *req) |
2311 | __must_hold(&req->ctx->completion_lock) | |
89b263f6 | 2312 | __must_hold(&req->ctx->timeout_lock) |
2665abfd | 2313 | { |
33cc89a9 | 2314 | struct io_kiocb *link = req->link; |
f2f87370 | 2315 | |
b97e736a | 2316 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
c9abd7ad | 2317 | struct io_timeout_data *io = link->async_data; |
7c86ffee | 2318 | |
f2f87370 | 2319 | io_remove_next_linked(req); |
90cd7e42 | 2320 | link->timeout.head = NULL; |
fd9c7bc5 | 2321 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
ef9dd637 | 2322 | list_del(&link->timeout.list); |
04c76b41 | 2323 | /* leave REQ_F_CQE_SKIP to io_fill_cqe_req */ |
913a571a | 2324 | io_fill_cqe_req(link, -ECANCELED, 0); |
91c2f697 | 2325 | io_put_req_deferred(link); |
d4729fbd | 2326 | return true; |
c9abd7ad PB |
2327 | } |
2328 | } | |
d4729fbd | 2329 | return false; |
7c86ffee PB |
2330 | } |
2331 | ||
d148ca4b | 2332 | static void io_fail_links(struct io_kiocb *req) |
33cc89a9 | 2333 | __must_hold(&req->ctx->completion_lock) |
9e645e11 | 2334 | { |
33cc89a9 | 2335 | struct io_kiocb *nxt, *link = req->link; |
04c76b41 | 2336 | bool ignore_cqes = req->flags & REQ_F_SKIP_LINK_CQES; |
9e645e11 | 2337 | |
f2f87370 | 2338 | req->link = NULL; |
f2f87370 | 2339 | while (link) { |
a8295b98 HX |
2340 | long res = -ECANCELED; |
2341 | ||
2342 | if (link->flags & REQ_F_FAIL) | |
2343 | res = link->result; | |
2344 | ||
f2f87370 PB |
2345 | nxt = link->link; |
2346 | link->link = NULL; | |
2665abfd | 2347 | |
502c87d6 SR |
2348 | trace_io_uring_fail_link(req->ctx, req, req->user_data, |
2349 | req->opcode, link); | |
2350 | ||
04c76b41 PB |
2351 | if (!ignore_cqes) { |
2352 | link->flags &= ~REQ_F_CQE_SKIP; | |
2353 | io_fill_cqe_req(link, res, 0); | |
2354 | } | |
91c2f697 | 2355 | io_put_req_deferred(link); |
f2f87370 | 2356 | link = nxt; |
9e645e11 | 2357 | } |
33cc89a9 | 2358 | } |
9e645e11 | 2359 | |
33cc89a9 PB |
2360 | static bool io_disarm_next(struct io_kiocb *req) |
2361 | __must_hold(&req->ctx->completion_lock) | |
2362 | { | |
2363 | bool posted = false; | |
2364 | ||
0756a869 PB |
2365 | if (req->flags & REQ_F_ARM_LTIMEOUT) { |
2366 | struct io_kiocb *link = req->link; | |
2367 | ||
906c6caa | 2368 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
0756a869 PB |
2369 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
2370 | io_remove_next_linked(req); | |
04c76b41 | 2371 | /* leave REQ_F_CQE_SKIP to io_fill_cqe_req */ |
913a571a | 2372 | io_fill_cqe_req(link, -ECANCELED, 0); |
0756a869 PB |
2373 | io_put_req_deferred(link); |
2374 | posted = true; | |
2375 | } | |
2376 | } else if (req->flags & REQ_F_LINK_TIMEOUT) { | |
89b263f6 JA |
2377 | struct io_ring_ctx *ctx = req->ctx; |
2378 | ||
2379 | spin_lock_irq(&ctx->timeout_lock); | |
33cc89a9 | 2380 | posted = io_kill_linked_timeout(req); |
89b263f6 JA |
2381 | spin_unlock_irq(&ctx->timeout_lock); |
2382 | } | |
93d2bcd2 | 2383 | if (unlikely((req->flags & REQ_F_FAIL) && |
e4335ed3 | 2384 | !(req->flags & REQ_F_HARDLINK))) { |
33cc89a9 PB |
2385 | posted |= (req->link != NULL); |
2386 | io_fail_links(req); | |
2387 | } | |
2388 | return posted; | |
9e645e11 JA |
2389 | } |
2390 | ||
d81499bf PB |
2391 | static void __io_req_find_next_prep(struct io_kiocb *req) |
2392 | { | |
2393 | struct io_ring_ctx *ctx = req->ctx; | |
2394 | bool posted; | |
2395 | ||
2396 | spin_lock(&ctx->completion_lock); | |
2397 | posted = io_disarm_next(req); | |
60053be8 | 2398 | io_commit_cqring(ctx); |
d81499bf PB |
2399 | spin_unlock(&ctx->completion_lock); |
2400 | if (posted) | |
2401 | io_cqring_ev_posted(ctx); | |
2402 | } | |
2403 | ||
2404 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) | |
c69f8dbe | 2405 | { |
33cc89a9 | 2406 | struct io_kiocb *nxt; |
944e58bf | 2407 | |
9e645e11 JA |
2408 | /* |
2409 | * If LINK is set, we have dependent requests in this chain. If we | |
2410 | * didn't fail this request, queue the first one up, moving any other | |
2411 | * dependencies to the next request. In case of failure, fail the rest | |
2412 | * of the chain. | |
2413 | */ | |
d81499bf PB |
2414 | if (unlikely(req->flags & IO_DISARM_MASK)) |
2415 | __io_req_find_next_prep(req); | |
33cc89a9 PB |
2416 | nxt = req->link; |
2417 | req->link = NULL; | |
2418 | return nxt; | |
4d7dd462 | 2419 | } |
9e645e11 | 2420 | |
f237c30a | 2421 | static void ctx_flush_and_put(struct io_ring_ctx *ctx, bool *locked) |
2c32395d PB |
2422 | { |
2423 | if (!ctx) | |
2424 | return; | |
f237c30a | 2425 | if (*locked) { |
c450178d | 2426 | io_submit_flush_completions(ctx); |
2c32395d | 2427 | mutex_unlock(&ctx->uring_lock); |
f237c30a | 2428 | *locked = false; |
2c32395d PB |
2429 | } |
2430 | percpu_ref_put(&ctx->refs); | |
2431 | } | |
2432 | ||
f28c240e HX |
2433 | static inline void ctx_commit_and_unlock(struct io_ring_ctx *ctx) |
2434 | { | |
2435 | io_commit_cqring(ctx); | |
2436 | spin_unlock(&ctx->completion_lock); | |
2437 | io_cqring_ev_posted(ctx); | |
2438 | } | |
2439 | ||
2440 | static void handle_prev_tw_list(struct io_wq_work_node *node, | |
2441 | struct io_ring_ctx **ctx, bool *uring_locked) | |
2442 | { | |
2443 | if (*ctx && !*uring_locked) | |
2444 | spin_lock(&(*ctx)->completion_lock); | |
2445 | ||
2446 | do { | |
2447 | struct io_wq_work_node *next = node->next; | |
2448 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2449 | io_task_work.node); | |
2450 | ||
34d2bfe7 JA |
2451 | prefetch(container_of(next, struct io_kiocb, io_task_work.node)); |
2452 | ||
f28c240e HX |
2453 | if (req->ctx != *ctx) { |
2454 | if (unlikely(!*uring_locked && *ctx)) | |
2455 | ctx_commit_and_unlock(*ctx); | |
2456 | ||
2457 | ctx_flush_and_put(*ctx, uring_locked); | |
2458 | *ctx = req->ctx; | |
2459 | /* if not contended, grab and improve batching */ | |
2460 | *uring_locked = mutex_trylock(&(*ctx)->uring_lock); | |
2461 | percpu_ref_get(&(*ctx)->refs); | |
2462 | if (unlikely(!*uring_locked)) | |
2463 | spin_lock(&(*ctx)->completion_lock); | |
2464 | } | |
2465 | if (likely(*uring_locked)) | |
2466 | req->io_task_work.func(req, uring_locked); | |
2467 | else | |
cc3cec83 JA |
2468 | __io_req_complete_post(req, req->result, |
2469 | io_put_kbuf_comp(req)); | |
f28c240e HX |
2470 | node = next; |
2471 | } while (node); | |
2472 | ||
2473 | if (unlikely(!*uring_locked)) | |
2474 | ctx_commit_and_unlock(*ctx); | |
2475 | } | |
2476 | ||
2477 | static void handle_tw_list(struct io_wq_work_node *node, | |
2478 | struct io_ring_ctx **ctx, bool *locked) | |
9f8d032a HX |
2479 | { |
2480 | do { | |
2481 | struct io_wq_work_node *next = node->next; | |
2482 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2483 | io_task_work.node); | |
2484 | ||
34d2bfe7 JA |
2485 | prefetch(container_of(next, struct io_kiocb, io_task_work.node)); |
2486 | ||
9f8d032a HX |
2487 | if (req->ctx != *ctx) { |
2488 | ctx_flush_and_put(*ctx, locked); | |
2489 | *ctx = req->ctx; | |
2490 | /* if not contended, grab and improve batching */ | |
2491 | *locked = mutex_trylock(&(*ctx)->uring_lock); | |
2492 | percpu_ref_get(&(*ctx)->refs); | |
2493 | } | |
2494 | req->io_task_work.func(req, locked); | |
2495 | node = next; | |
2496 | } while (node); | |
2497 | } | |
2498 | ||
7cbf1722 | 2499 | static void tctx_task_work(struct callback_head *cb) |
c40f6379 | 2500 | { |
f28c240e | 2501 | bool uring_locked = false; |
ebd0df2e | 2502 | struct io_ring_ctx *ctx = NULL; |
3f18407d PB |
2503 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, |
2504 | task_work); | |
c40f6379 | 2505 | |
16f72070 | 2506 | while (1) { |
f28c240e | 2507 | struct io_wq_work_node *node1, *node2; |
3f18407d PB |
2508 | |
2509 | spin_lock_irq(&tctx->task_lock); | |
f28c240e HX |
2510 | node1 = tctx->prior_task_list.first; |
2511 | node2 = tctx->task_list.first; | |
3f18407d | 2512 | INIT_WQ_LIST(&tctx->task_list); |
f28c240e HX |
2513 | INIT_WQ_LIST(&tctx->prior_task_list); |
2514 | if (!node2 && !node1) | |
6294f368 | 2515 | tctx->task_running = false; |
3f18407d | 2516 | spin_unlock_irq(&tctx->task_lock); |
f28c240e | 2517 | if (!node2 && !node1) |
6294f368 | 2518 | break; |
3f18407d | 2519 | |
f28c240e HX |
2520 | if (node1) |
2521 | handle_prev_tw_list(node1, &ctx, &uring_locked); | |
f28c240e HX |
2522 | if (node2) |
2523 | handle_tw_list(node2, &ctx, &uring_locked); | |
7cbf1722 | 2524 | cond_resched(); |
68ca8fc0 PB |
2525 | |
2526 | if (!tctx->task_list.first && | |
2527 | !tctx->prior_task_list.first && uring_locked) | |
2528 | io_submit_flush_completions(ctx); | |
3f18407d | 2529 | } |
ebd0df2e | 2530 | |
f28c240e | 2531 | ctx_flush_and_put(ctx, &uring_locked); |
3cc7fdb9 PB |
2532 | |
2533 | /* relaxed read is enough as only the task itself sets ->in_idle */ | |
2534 | if (unlikely(atomic_read(&tctx->in_idle))) | |
2535 | io_uring_drop_tctx_refs(current); | |
7cbf1722 JA |
2536 | } |
2537 | ||
4813c377 | 2538 | static void io_req_task_work_add(struct io_kiocb *req, bool priority) |
7cbf1722 | 2539 | { |
c15b79de | 2540 | struct task_struct *tsk = req->task; |
7cbf1722 | 2541 | struct io_uring_task *tctx = tsk->io_uring; |
c15b79de | 2542 | enum task_work_notify_mode notify; |
e09ee510 | 2543 | struct io_wq_work_node *node; |
0b81e80c | 2544 | unsigned long flags; |
6294f368 | 2545 | bool running; |
7cbf1722 JA |
2546 | |
2547 | WARN_ON_ONCE(!tctx); | |
2548 | ||
d5361233 JA |
2549 | io_drop_inflight_file(req); |
2550 | ||
0b81e80c | 2551 | spin_lock_irqsave(&tctx->task_lock, flags); |
4813c377 HX |
2552 | if (priority) |
2553 | wq_list_add_tail(&req->io_task_work.node, &tctx->prior_task_list); | |
2554 | else | |
2555 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); | |
6294f368 PB |
2556 | running = tctx->task_running; |
2557 | if (!running) | |
2558 | tctx->task_running = true; | |
0b81e80c | 2559 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
2560 | |
2561 | /* task_work already pending, we're done */ | |
6294f368 | 2562 | if (running) |
e09ee510 | 2563 | return; |
7cbf1722 | 2564 | |
c15b79de PB |
2565 | /* |
2566 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For | |
2567 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're | |
2568 | * processing task_work. There's no reliable way to tell if TWA_RESUME | |
2569 | * will do the job. | |
2570 | */ | |
2571 | notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL; | |
d97ec623 PB |
2572 | if (likely(!task_work_add(tsk, &tctx->task_work, notify))) { |
2573 | if (notify == TWA_NONE) | |
2574 | wake_up_process(tsk); | |
e09ee510 | 2575 | return; |
c15b79de | 2576 | } |
2215bed9 | 2577 | |
0b81e80c | 2578 | spin_lock_irqsave(&tctx->task_lock, flags); |
6294f368 | 2579 | tctx->task_running = false; |
4813c377 | 2580 | node = wq_list_merge(&tctx->prior_task_list, &tctx->task_list); |
0b81e80c | 2581 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 | 2582 | |
e09ee510 PB |
2583 | while (node) { |
2584 | req = container_of(node, struct io_kiocb, io_task_work.node); | |
2585 | node = node->next; | |
2586 | if (llist_add(&req->io_task_work.fallback_node, | |
2587 | &req->ctx->fallback_llist)) | |
2588 | schedule_delayed_work(&req->ctx->fallback_work, 1); | |
2589 | } | |
eab30c4d PB |
2590 | } |
2591 | ||
f237c30a | 2592 | static void io_req_task_cancel(struct io_kiocb *req, bool *locked) |
c40f6379 | 2593 | { |
87ceb6a6 | 2594 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 | 2595 | |
b18a1a45 | 2596 | /* not needed for normal modes, but SQPOLL depends on it */ |
f237c30a | 2597 | io_tw_lock(ctx, locked); |
2593553a | 2598 | io_req_complete_failed(req, req->result); |
c40f6379 JA |
2599 | } |
2600 | ||
f237c30a | 2601 | static void io_req_task_submit(struct io_kiocb *req, bool *locked) |
c40f6379 JA |
2602 | { |
2603 | struct io_ring_ctx *ctx = req->ctx; | |
2604 | ||
f237c30a | 2605 | io_tw_lock(ctx, locked); |
316319e8 | 2606 | /* req->task == current here, checking PF_EXITING is safe */ |
af066f31 | 2607 | if (likely(!(req->task->flags & PF_EXITING))) |
c5eef2b9 | 2608 | __io_queue_sqe(req); |
81b6d05c | 2609 | else |
2593553a | 2610 | io_req_complete_failed(req, -EFAULT); |
c40f6379 JA |
2611 | } |
2612 | ||
2c4b8eb6 | 2613 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
c40f6379 | 2614 | { |
2c4b8eb6 | 2615 | req->result = ret; |
5b0a6acc | 2616 | req->io_task_work.func = io_req_task_cancel; |
4813c377 | 2617 | io_req_task_work_add(req, false); |
c40f6379 JA |
2618 | } |
2619 | ||
2c4b8eb6 | 2620 | static void io_req_task_queue(struct io_kiocb *req) |
a3df7698 | 2621 | { |
5b0a6acc | 2622 | req->io_task_work.func = io_req_task_submit; |
4813c377 | 2623 | io_req_task_work_add(req, false); |
a3df7698 PB |
2624 | } |
2625 | ||
773af691 JA |
2626 | static void io_req_task_queue_reissue(struct io_kiocb *req) |
2627 | { | |
2628 | req->io_task_work.func = io_queue_async_work; | |
4813c377 | 2629 | io_req_task_work_add(req, false); |
773af691 JA |
2630 | } |
2631 | ||
57859f4d | 2632 | static void io_queue_next(struct io_kiocb *req) |
c69f8dbe | 2633 | { |
57859f4d | 2634 | struct io_kiocb *nxt = io_req_find_next(req); |
944e58bf | 2635 | |
57859f4d PB |
2636 | if (nxt) |
2637 | io_req_task_queue(nxt); | |
c69f8dbe JL |
2638 | } |
2639 | ||
c3524383 | 2640 | static void io_free_req(struct io_kiocb *req) |
7a743e22 | 2641 | { |
c3524383 PB |
2642 | io_queue_next(req); |
2643 | __io_free_req(req); | |
2644 | } | |
8766dd51 | 2645 | |
f237c30a PB |
2646 | static void io_free_req_work(struct io_kiocb *req, bool *locked) |
2647 | { | |
2648 | io_free_req(req); | |
2649 | } | |
2650 | ||
3aa83bfb | 2651 | static void io_free_batch_list(struct io_ring_ctx *ctx, |
1cce17ac | 2652 | struct io_wq_work_node *node) |
3aa83bfb | 2653 | __must_hold(&ctx->uring_lock) |
5af1d13e | 2654 | { |
d4b7a5ef | 2655 | struct task_struct *task = NULL; |
37f0e767 | 2656 | int task_refs = 0; |
5af1d13e | 2657 | |
3aa83bfb PB |
2658 | do { |
2659 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2660 | comp_list); | |
2d6500d4 | 2661 | |
a538be5b PB |
2662 | if (unlikely(req->flags & IO_REQ_CLEAN_SLOW_FLAGS)) { |
2663 | if (req->flags & REQ_F_REFCOUNT) { | |
2664 | node = req->comp_list.next; | |
2665 | if (!req_ref_put_and_test(req)) | |
2666 | continue; | |
2667 | } | |
b605a7fa PB |
2668 | if ((req->flags & REQ_F_POLLED) && req->apoll) { |
2669 | struct async_poll *apoll = req->apoll; | |
2670 | ||
2671 | if (apoll->double_poll) | |
2672 | kfree(apoll->double_poll); | |
2673 | list_add(&apoll->poll.wait.entry, | |
2674 | &ctx->apoll_cache); | |
2675 | req->flags &= ~REQ_F_POLLED; | |
2676 | } | |
57859f4d PB |
2677 | if (req->flags & (REQ_F_LINK|REQ_F_HARDLINK)) |
2678 | io_queue_next(req); | |
a538be5b PB |
2679 | if (unlikely(req->flags & IO_REQ_CLEAN_FLAGS)) |
2680 | io_clean_op(req); | |
c1e53a69 | 2681 | } |
a538be5b PB |
2682 | if (!(req->flags & REQ_F_FIXED_FILE)) |
2683 | io_put_file(req->file); | |
2d6500d4 | 2684 | |
ab409402 | 2685 | io_req_put_rsrc_locked(req, ctx); |
5af1d13e | 2686 | |
d4b7a5ef PB |
2687 | if (req->task != task) { |
2688 | if (task) | |
2689 | io_put_task(task, task_refs); | |
2690 | task = req->task; | |
2691 | task_refs = 0; | |
2692 | } | |
2693 | task_refs++; | |
c1e53a69 | 2694 | node = req->comp_list.next; |
d4b7a5ef | 2695 | wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list); |
3aa83bfb | 2696 | } while (node); |
d4b7a5ef | 2697 | |
d4b7a5ef PB |
2698 | if (task) |
2699 | io_put_task(task, task_refs); | |
7a743e22 PB |
2700 | } |
2701 | ||
c450178d | 2702 | static void __io_submit_flush_completions(struct io_ring_ctx *ctx) |
a141dd89 | 2703 | __must_hold(&ctx->uring_lock) |
905c172f | 2704 | { |
6f33b0bc | 2705 | struct io_wq_work_node *node, *prev; |
cd0ca2e0 | 2706 | struct io_submit_state *state = &ctx->submit_state; |
905c172f | 2707 | |
3d4aeb9f PB |
2708 | if (state->flush_cqes) { |
2709 | spin_lock(&ctx->completion_lock); | |
2710 | wq_list_for_each(node, prev, &state->compl_reqs) { | |
2711 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
6f33b0bc | 2712 | comp_list); |
5182ed2e | 2713 | |
3d4aeb9f | 2714 | if (!(req->flags & REQ_F_CQE_SKIP)) |
ae4da189 | 2715 | __io_fill_cqe_req(req, req->result, req->cflags); |
3d4aeb9f PB |
2716 | } |
2717 | ||
2718 | io_commit_cqring(ctx); | |
2719 | spin_unlock(&ctx->completion_lock); | |
2720 | io_cqring_ev_posted(ctx); | |
2721 | state->flush_cqes = false; | |
905c172f | 2722 | } |
5182ed2e | 2723 | |
1cce17ac | 2724 | io_free_batch_list(ctx, state->compl_reqs.first); |
6f33b0bc | 2725 | INIT_WQ_LIST(&state->compl_reqs); |
7a743e22 PB |
2726 | } |
2727 | ||
ba816ad6 JA |
2728 | /* |
2729 | * Drop reference to request, return next in chain (if there is one) if this | |
2730 | * was the last reference to this request. | |
2731 | */ | |
0d85035a | 2732 | static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
e65ef56d | 2733 | { |
9b5f7bd9 PB |
2734 | struct io_kiocb *nxt = NULL; |
2735 | ||
de9b4cca | 2736 | if (req_ref_put_and_test(req)) { |
7819a1f6 PB |
2737 | if (unlikely(req->flags & (REQ_F_LINK|REQ_F_HARDLINK))) |
2738 | nxt = io_req_find_next(req); | |
4d7dd462 | 2739 | __io_free_req(req); |
2a44f467 | 2740 | } |
9b5f7bd9 | 2741 | return nxt; |
2b188cc1 JA |
2742 | } |
2743 | ||
0d85035a | 2744 | static inline void io_put_req(struct io_kiocb *req) |
e65ef56d | 2745 | { |
de9b4cca | 2746 | if (req_ref_put_and_test(req)) |
e65ef56d | 2747 | io_free_req(req); |
2b188cc1 JA |
2748 | } |
2749 | ||
91c2f697 | 2750 | static inline void io_put_req_deferred(struct io_kiocb *req) |
216578e5 | 2751 | { |
91c2f697 | 2752 | if (req_ref_put_and_test(req)) { |
f237c30a | 2753 | req->io_task_work.func = io_free_req_work; |
4813c377 | 2754 | io_req_task_work_add(req, false); |
543af3a1 | 2755 | } |
216578e5 PB |
2756 | } |
2757 | ||
6c503150 | 2758 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
a3a0e43f JA |
2759 | { |
2760 | /* See comment at the top of this file */ | |
2761 | smp_rmb(); | |
e23de15f | 2762 | return __io_cqring_events(ctx); |
a3a0e43f JA |
2763 | } |
2764 | ||
fb5ccc98 PB |
2765 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
2766 | { | |
2767 | struct io_rings *rings = ctx->rings; | |
2768 | ||
2769 | /* make sure SQ entry isn't read before tail */ | |
2770 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
2771 | } | |
2772 | ||
4c6e277c JA |
2773 | static inline bool io_run_task_work(void) |
2774 | { | |
7f62d40d | 2775 | if (test_thread_flag(TIF_NOTIFY_SIGNAL) || task_work_pending(current)) { |
4c6e277c | 2776 | __set_current_state(TASK_RUNNING); |
7c5d8fa6 EB |
2777 | clear_notify_signal(); |
2778 | if (task_work_pending(current)) | |
2779 | task_work_run(); | |
4c6e277c JA |
2780 | return true; |
2781 | } | |
2782 | ||
2783 | return false; | |
bcda7baa JA |
2784 | } |
2785 | ||
5ba3c874 | 2786 | static int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin) |
def596e9 | 2787 | { |
5eef4e87 | 2788 | struct io_wq_work_node *pos, *start, *prev; |
d729cf9a | 2789 | unsigned int poll_flags = BLK_POLL_NOSLEEP; |
b688f11e | 2790 | DEFINE_IO_COMP_BATCH(iob); |
5ba3c874 | 2791 | int nr_events = 0; |
def596e9 JA |
2792 | |
2793 | /* | |
2794 | * Only spin for completions if we don't have multiple devices hanging | |
87a115fb | 2795 | * off our complete list. |
def596e9 | 2796 | */ |
87a115fb | 2797 | if (ctx->poll_multi_queue || force_nonspin) |
ef99b2d3 | 2798 | poll_flags |= BLK_POLL_ONESHOT; |
def596e9 | 2799 | |
5eef4e87 PB |
2800 | wq_list_for_each(pos, start, &ctx->iopoll_list) { |
2801 | struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); | |
9adbd45d | 2802 | struct kiocb *kiocb = &req->rw.kiocb; |
a2416e1e | 2803 | int ret; |
def596e9 JA |
2804 | |
2805 | /* | |
581f9810 BM |
2806 | * Move completed and retryable entries to our local lists. |
2807 | * If we find a request that requires polling, break out | |
2808 | * and complete those lists first, if we have entries there. | |
def596e9 | 2809 | */ |
e3f721e6 | 2810 | if (READ_ONCE(req->iopoll_completed)) |
def596e9 JA |
2811 | break; |
2812 | ||
b688f11e | 2813 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, &iob, poll_flags); |
a2416e1e PB |
2814 | if (unlikely(ret < 0)) |
2815 | return ret; | |
2816 | else if (ret) | |
ef99b2d3 | 2817 | poll_flags |= BLK_POLL_ONESHOT; |
def596e9 | 2818 | |
3aadc23e | 2819 | /* iopoll may have completed current req */ |
b688f11e JA |
2820 | if (!rq_list_empty(iob.req_list) || |
2821 | READ_ONCE(req->iopoll_completed)) | |
e3f721e6 | 2822 | break; |
def596e9 JA |
2823 | } |
2824 | ||
b688f11e JA |
2825 | if (!rq_list_empty(iob.req_list)) |
2826 | iob.complete(&iob); | |
5eef4e87 PB |
2827 | else if (!pos) |
2828 | return 0; | |
def596e9 | 2829 | |
5eef4e87 PB |
2830 | prev = start; |
2831 | wq_list_for_each_resume(pos, prev) { | |
2832 | struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); | |
2833 | ||
b3fa03fd PB |
2834 | /* order with io_complete_rw_iopoll(), e.g. ->result updates */ |
2835 | if (!smp_load_acquire(&req->iopoll_completed)) | |
e3f721e6 | 2836 | break; |
c0713540 | 2837 | nr_events++; |
83a13a41 PB |
2838 | if (unlikely(req->flags & REQ_F_CQE_SKIP)) |
2839 | continue; | |
ae4da189 | 2840 | __io_fill_cqe_req(req, req->result, io_put_kbuf(req, 0)); |
e3f721e6 | 2841 | } |
def596e9 | 2842 | |
f5ed3bcd PB |
2843 | if (unlikely(!nr_events)) |
2844 | return 0; | |
2845 | ||
2846 | io_commit_cqring(ctx); | |
2847 | io_cqring_ev_posted_iopoll(ctx); | |
1cce17ac | 2848 | pos = start ? start->next : ctx->iopoll_list.first; |
5eef4e87 | 2849 | wq_list_cut(&ctx->iopoll_list, prev, start); |
1cce17ac | 2850 | io_free_batch_list(ctx, pos); |
5ba3c874 | 2851 | return nr_events; |
def596e9 JA |
2852 | } |
2853 | ||
def596e9 JA |
2854 | /* |
2855 | * We can't just wait for polled events to come to us, we have to actively | |
2856 | * find and complete them. | |
2857 | */ | |
c072481d | 2858 | static __cold void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
def596e9 JA |
2859 | { |
2860 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
2861 | return; | |
2862 | ||
2863 | mutex_lock(&ctx->uring_lock); | |
5eef4e87 | 2864 | while (!wq_list_empty(&ctx->iopoll_list)) { |
b2edc0a7 | 2865 | /* let it sleep and repeat later if can't complete a request */ |
5ba3c874 | 2866 | if (io_do_iopoll(ctx, true) == 0) |
b2edc0a7 | 2867 | break; |
08f5439f JA |
2868 | /* |
2869 | * Ensure we allow local-to-the-cpu processing to take place, | |
2870 | * in this case we need to ensure that we reap all events. | |
3fcee5a6 | 2871 | * Also let task_work, etc. to progress by releasing the mutex |
08f5439f | 2872 | */ |
3fcee5a6 PB |
2873 | if (need_resched()) { |
2874 | mutex_unlock(&ctx->uring_lock); | |
2875 | cond_resched(); | |
2876 | mutex_lock(&ctx->uring_lock); | |
2877 | } | |
def596e9 JA |
2878 | } |
2879 | mutex_unlock(&ctx->uring_lock); | |
2880 | } | |
2881 | ||
7668b92a | 2882 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
def596e9 | 2883 | { |
7668b92a | 2884 | unsigned int nr_events = 0; |
e9979b36 | 2885 | int ret = 0; |
500f9fba | 2886 | |
f39c8a5b PB |
2887 | /* |
2888 | * Don't enter poll loop if we already have events pending. | |
2889 | * If we do, we can potentially be spinning for commands that | |
2890 | * already triggered a CQE (eg in error). | |
2891 | */ | |
5ed7a37d | 2892 | if (test_bit(0, &ctx->check_cq_overflow)) |
f39c8a5b PB |
2893 | __io_cqring_overflow_flush(ctx, false); |
2894 | if (io_cqring_events(ctx)) | |
d487b43c | 2895 | return 0; |
def596e9 | 2896 | do { |
500f9fba JA |
2897 | /* |
2898 | * If a submit got punted to a workqueue, we can have the | |
2899 | * application entering polling for a command before it gets | |
2900 | * issued. That app will hold the uring_lock for the duration | |
2901 | * of the poll right here, so we need to take a breather every | |
2902 | * now and then to ensure that the issue has a chance to add | |
2903 | * the poll to the issued list. Otherwise we can spin here | |
2904 | * forever, while the workqueue is stuck trying to acquire the | |
2905 | * very same mutex. | |
2906 | */ | |
5eef4e87 | 2907 | if (wq_list_empty(&ctx->iopoll_list)) { |
8f487ef2 PB |
2908 | u32 tail = ctx->cached_cq_tail; |
2909 | ||
500f9fba | 2910 | mutex_unlock(&ctx->uring_lock); |
4c6e277c | 2911 | io_run_task_work(); |
500f9fba | 2912 | mutex_lock(&ctx->uring_lock); |
def596e9 | 2913 | |
8f487ef2 PB |
2914 | /* some requests don't go through iopoll_list */ |
2915 | if (tail != ctx->cached_cq_tail || | |
5eef4e87 | 2916 | wq_list_empty(&ctx->iopoll_list)) |
e9979b36 | 2917 | break; |
500f9fba | 2918 | } |
5ba3c874 PB |
2919 | ret = io_do_iopoll(ctx, !min); |
2920 | if (ret < 0) | |
2921 | break; | |
2922 | nr_events += ret; | |
2923 | ret = 0; | |
2924 | } while (nr_events < min && !need_resched()); | |
d487b43c | 2925 | |
def596e9 JA |
2926 | return ret; |
2927 | } | |
2928 | ||
491381ce | 2929 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 2930 | { |
491381ce JA |
2931 | /* |
2932 | * Tell lockdep we inherited freeze protection from submission | |
2933 | * thread. | |
2934 | */ | |
2935 | if (req->flags & REQ_F_ISREG) { | |
1c98679d | 2936 | struct super_block *sb = file_inode(req->file)->i_sb; |
2b188cc1 | 2937 | |
1c98679d PB |
2938 | __sb_writers_acquired(sb, SB_FREEZE_WRITE); |
2939 | sb_end_write(sb); | |
2b188cc1 JA |
2940 | } |
2941 | } | |
2942 | ||
b63534c4 | 2943 | #ifdef CONFIG_BLOCK |
dc2a6e9a | 2944 | static bool io_resubmit_prep(struct io_kiocb *req) |
b63534c4 | 2945 | { |
ab454438 | 2946 | struct io_async_rw *rw = req->async_data; |
b63534c4 | 2947 | |
d886e185 | 2948 | if (!req_has_async_data(req)) |
ab454438 | 2949 | return !io_req_prep_async(req); |
538941e2 | 2950 | iov_iter_restore(&rw->s.iter, &rw->s.iter_state); |
ab454438 | 2951 | return true; |
b63534c4 | 2952 | } |
b63534c4 | 2953 | |
3e6a0d3c | 2954 | static bool io_rw_should_reissue(struct io_kiocb *req) |
b63534c4 | 2955 | { |
355afaeb | 2956 | umode_t mode = file_inode(req->file)->i_mode; |
3e6a0d3c | 2957 | struct io_ring_ctx *ctx = req->ctx; |
b63534c4 | 2958 | |
355afaeb JA |
2959 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
2960 | return false; | |
3e6a0d3c JA |
2961 | if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() && |
2962 | !(ctx->flags & IORING_SETUP_IOPOLL))) | |
b63534c4 | 2963 | return false; |
7c977a58 JA |
2964 | /* |
2965 | * If ref is dying, we might be running poll reap from the exit work. | |
2966 | * Don't attempt to reissue from that path, just let it fail with | |
2967 | * -EAGAIN. | |
2968 | */ | |
3e6a0d3c JA |
2969 | if (percpu_ref_is_dying(&ctx->refs)) |
2970 | return false; | |
ef046888 JA |
2971 | /* |
2972 | * Play it safe and assume not safe to re-import and reissue if we're | |
2973 | * not in the original thread group (or in task context). | |
2974 | */ | |
2975 | if (!same_thread_group(req->task, current) || !in_task()) | |
2976 | return false; | |
3e6a0d3c JA |
2977 | return true; |
2978 | } | |
e82ad485 | 2979 | #else |
a1ff1e3f | 2980 | static bool io_resubmit_prep(struct io_kiocb *req) |
e82ad485 JA |
2981 | { |
2982 | return false; | |
2983 | } | |
e82ad485 | 2984 | static bool io_rw_should_reissue(struct io_kiocb *req) |
3e6a0d3c | 2985 | { |
b63534c4 JA |
2986 | return false; |
2987 | } | |
3e6a0d3c | 2988 | #endif |
b63534c4 | 2989 | |
8ef12efe | 2990 | static bool __io_complete_rw_common(struct io_kiocb *req, long res) |
a1d7c393 | 2991 | { |
f63cf519 | 2992 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) { |
b65c128f | 2993 | kiocb_end_write(req); |
f63cf519 JA |
2994 | fsnotify_modify(req->file); |
2995 | } else { | |
2996 | fsnotify_access(req->file); | |
2997 | } | |
258f3a7f | 2998 | if (unlikely(res != req->result)) { |
9532b99b PB |
2999 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && |
3000 | io_rw_should_reissue(req)) { | |
3001 | req->flags |= REQ_F_REISSUE; | |
8ef12efe | 3002 | return true; |
9532b99b | 3003 | } |
93d2bcd2 | 3004 | req_set_fail(req); |
8ef12efe | 3005 | req->result = res; |
9532b99b | 3006 | } |
8ef12efe JA |
3007 | return false; |
3008 | } | |
3009 | ||
cc8e9ba7 | 3010 | static inline void io_req_task_complete(struct io_kiocb *req, bool *locked) |
8ef12efe | 3011 | { |
54daa9b2 | 3012 | int res = req->result; |
126180b9 PB |
3013 | |
3014 | if (*locked) { | |
cc3cec83 | 3015 | io_req_complete_state(req, res, io_put_kbuf(req, 0)); |
fff4e40e | 3016 | io_req_add_compl_list(req); |
126180b9 | 3017 | } else { |
cc3cec83 JA |
3018 | io_req_complete_post(req, res, |
3019 | io_put_kbuf(req, IO_URING_F_UNLOCKED)); | |
126180b9 | 3020 | } |
8ef12efe JA |
3021 | } |
3022 | ||
00f6e68b | 3023 | static void __io_complete_rw(struct io_kiocb *req, long res, |
8ef12efe JA |
3024 | unsigned int issue_flags) |
3025 | { | |
3026 | if (__io_complete_rw_common(req, res)) | |
3027 | return; | |
cc3cec83 JA |
3028 | __io_req_complete(req, issue_flags, req->result, |
3029 | io_put_kbuf(req, issue_flags)); | |
ba816ad6 JA |
3030 | } |
3031 | ||
6b19b766 | 3032 | static void io_complete_rw(struct kiocb *kiocb, long res) |
ba816ad6 | 3033 | { |
9adbd45d | 3034 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 | 3035 | |
8ef12efe JA |
3036 | if (__io_complete_rw_common(req, res)) |
3037 | return; | |
3038 | req->result = res; | |
3039 | req->io_task_work.func = io_req_task_complete; | |
f28c240e | 3040 | io_req_task_work_add(req, !!(req->ctx->flags & IORING_SETUP_SQPOLL)); |
2b188cc1 JA |
3041 | } |
3042 | ||
6b19b766 | 3043 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res) |
def596e9 | 3044 | { |
9adbd45d | 3045 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 3046 | |
491381ce JA |
3047 | if (kiocb->ki_flags & IOCB_WRITE) |
3048 | kiocb_end_write(req); | |
9532b99b | 3049 | if (unlikely(res != req->result)) { |
b66ceaf3 PB |
3050 | if (res == -EAGAIN && io_rw_should_reissue(req)) { |
3051 | req->flags |= REQ_F_REISSUE; | |
3052 | return; | |
9532b99b | 3053 | } |
258f3a7f | 3054 | req->result = res; |
8c130827 | 3055 | } |
bbde017a | 3056 | |
b3fa03fd PB |
3057 | /* order with io_iopoll_complete() checking ->iopoll_completed */ |
3058 | smp_store_release(&req->iopoll_completed, 1); | |
def596e9 JA |
3059 | } |
3060 | ||
3061 | /* | |
3062 | * After the iocb has been issued, it's safe to be found on the poll list. | |
3063 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
f39c8a5b | 3064 | * find it from a io_do_iopoll() thread before the issuer is done |
def596e9 JA |
3065 | * accessing the kiocb cookie. |
3066 | */ | |
9882131c | 3067 | static void io_iopoll_req_issued(struct io_kiocb *req, unsigned int issue_flags) |
def596e9 JA |
3068 | { |
3069 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 3070 | const bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
cb3d8972 PB |
3071 | |
3072 | /* workqueue context doesn't hold uring_lock, grab it now */ | |
3b44b371 | 3073 | if (unlikely(needs_lock)) |
cb3d8972 | 3074 | mutex_lock(&ctx->uring_lock); |
def596e9 JA |
3075 | |
3076 | /* | |
3077 | * Track whether we have multiple files in our lists. This will impact | |
3078 | * how we do polling eventually, not spinning if we're on potentially | |
3079 | * different devices. | |
3080 | */ | |
5eef4e87 | 3081 | if (wq_list_empty(&ctx->iopoll_list)) { |
915b3dde HX |
3082 | ctx->poll_multi_queue = false; |
3083 | } else if (!ctx->poll_multi_queue) { | |
def596e9 JA |
3084 | struct io_kiocb *list_req; |
3085 | ||
5eef4e87 PB |
3086 | list_req = container_of(ctx->iopoll_list.first, struct io_kiocb, |
3087 | comp_list); | |
30da1b45 | 3088 | if (list_req->file != req->file) |
915b3dde | 3089 | ctx->poll_multi_queue = true; |
def596e9 JA |
3090 | } |
3091 | ||
3092 | /* | |
3093 | * For fast devices, IO may have already completed. If it has, add | |
3094 | * it to the front so we find it first. | |
3095 | */ | |
65a6543d | 3096 | if (READ_ONCE(req->iopoll_completed)) |
5eef4e87 | 3097 | wq_list_add_head(&req->comp_list, &ctx->iopoll_list); |
def596e9 | 3098 | else |
5eef4e87 | 3099 | wq_list_add_tail(&req->comp_list, &ctx->iopoll_list); |
bdcd3eab | 3100 | |
3b44b371 | 3101 | if (unlikely(needs_lock)) { |
cb3d8972 PB |
3102 | /* |
3103 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handle | |
3104 | * in sq thread task context or in io worker task context. If | |
3105 | * current task context is sq thread, we don't need to check | |
3106 | * whether should wake up sq thread. | |
3107 | */ | |
3108 | if ((ctx->flags & IORING_SETUP_SQPOLL) && | |
3109 | wq_has_sleeper(&ctx->sq_data->wait)) | |
3110 | wake_up(&ctx->sq_data->wait); | |
3111 | ||
3112 | mutex_unlock(&ctx->uring_lock); | |
3113 | } | |
def596e9 JA |
3114 | } |
3115 | ||
4503b767 JA |
3116 | static bool io_bdev_nowait(struct block_device *bdev) |
3117 | { | |
9ba0d0c8 | 3118 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
4503b767 JA |
3119 | } |
3120 | ||
2b188cc1 JA |
3121 | /* |
3122 | * If we tracked the file through the SCM inflight mechanism, we could support | |
3123 | * any file. For now, just ensure that anything potentially problematic is done | |
3124 | * inline. | |
3125 | */ | |
88459b50 | 3126 | static bool __io_file_supports_nowait(struct file *file, umode_t mode) |
2b188cc1 | 3127 | { |
4503b767 | 3128 | if (S_ISBLK(mode)) { |
4e7b5671 CH |
3129 | if (IS_ENABLED(CONFIG_BLOCK) && |
3130 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) | |
4503b767 JA |
3131 | return true; |
3132 | return false; | |
3133 | } | |
976517f1 | 3134 | if (S_ISSOCK(mode)) |
2b188cc1 | 3135 | return true; |
4503b767 | 3136 | if (S_ISREG(mode)) { |
4e7b5671 CH |
3137 | if (IS_ENABLED(CONFIG_BLOCK) && |
3138 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && | |
4503b767 JA |
3139 | file->f_op != &io_uring_fops) |
3140 | return true; | |
3141 | return false; | |
3142 | } | |
2b188cc1 | 3143 | |
c5b85625 JA |
3144 | /* any ->read/write should understand O_NONBLOCK */ |
3145 | if (file->f_flags & O_NONBLOCK) | |
3146 | return true; | |
35645ac3 | 3147 | return file->f_mode & FMODE_NOWAIT; |
2b188cc1 | 3148 | } |
c5b85625 | 3149 | |
88459b50 PB |
3150 | /* |
3151 | * If we tracked the file through the SCM inflight mechanism, we could support | |
3152 | * any file. For now, just ensure that anything potentially problematic is done | |
3153 | * inline. | |
3154 | */ | |
3155 | static unsigned int io_file_get_flags(struct file *file) | |
3156 | { | |
3157 | umode_t mode = file_inode(file)->i_mode; | |
3158 | unsigned int res = 0; | |
af197f50 | 3159 | |
88459b50 PB |
3160 | if (S_ISREG(mode)) |
3161 | res |= FFS_ISREG; | |
3162 | if (__io_file_supports_nowait(file, mode)) | |
3163 | res |= FFS_NOWAIT; | |
3164 | return res; | |
2b188cc1 JA |
3165 | } |
3166 | ||
35645ac3 | 3167 | static inline bool io_file_supports_nowait(struct io_kiocb *req) |
7b29f92d | 3168 | { |
88459b50 | 3169 | return req->flags & REQ_F_SUPPORT_NOWAIT; |
7b29f92d JA |
3170 | } |
3171 | ||
b9a6b8f9 | 3172 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2b188cc1 | 3173 | { |
9adbd45d | 3174 | struct kiocb *kiocb = &req->rw.kiocb; |
09bb8394 JA |
3175 | unsigned ioprio; |
3176 | int ret; | |
2b188cc1 | 3177 | |
2b188cc1 | 3178 | kiocb->ki_pos = READ_ONCE(sqe->off); |
9adbd45d | 3179 | |
fb27274a PB |
3180 | ioprio = READ_ONCE(sqe->ioprio); |
3181 | if (ioprio) { | |
3182 | ret = ioprio_check_cap(ioprio); | |
3183 | if (ret) | |
3184 | return ret; | |
3185 | ||
3186 | kiocb->ki_ioprio = ioprio; | |
3187 | } else { | |
3188 | kiocb->ki_ioprio = get_current_ioprio(); | |
eae071c9 PB |
3189 | } |
3190 | ||
578c0ee2 | 3191 | req->imu = NULL; |
3529d8c2 JA |
3192 | req->rw.addr = READ_ONCE(sqe->addr); |
3193 | req->rw.len = READ_ONCE(sqe->len); | |
584b0180 | 3194 | req->rw.flags = READ_ONCE(sqe->rw_flags); |
4f4eeba8 | 3195 | req->buf_index = READ_ONCE(sqe->buf_index); |
2b188cc1 | 3196 | return 0; |
2b188cc1 JA |
3197 | } |
3198 | ||
3199 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
3200 | { | |
3201 | switch (ret) { | |
3202 | case -EIOCBQUEUED: | |
3203 | break; | |
3204 | case -ERESTARTSYS: | |
3205 | case -ERESTARTNOINTR: | |
3206 | case -ERESTARTNOHAND: | |
3207 | case -ERESTART_RESTARTBLOCK: | |
3208 | /* | |
3209 | * We can't just restart the syscall, since previously | |
3210 | * submitted sqes may already be in progress. Just fail this | |
3211 | * IO with EINTR. | |
3212 | */ | |
3213 | ret = -EINTR; | |
df561f66 | 3214 | fallthrough; |
2b188cc1 | 3215 | default: |
6b19b766 | 3216 | kiocb->ki_complete(kiocb, ret); |
2b188cc1 JA |
3217 | } |
3218 | } | |
3219 | ||
b4aec400 | 3220 | static inline loff_t *io_kiocb_update_pos(struct io_kiocb *req) |
d34e1e5b DY |
3221 | { |
3222 | struct kiocb *kiocb = &req->rw.kiocb; | |
3223 | ||
6f83ab22 JA |
3224 | if (kiocb->ki_pos != -1) |
3225 | return &kiocb->ki_pos; | |
3226 | ||
3227 | if (!(req->file->f_mode & FMODE_STREAM)) { | |
3228 | req->flags |= REQ_F_CUR_POS; | |
3229 | kiocb->ki_pos = req->file->f_pos; | |
3230 | return &kiocb->ki_pos; | |
d34e1e5b | 3231 | } |
6f83ab22 JA |
3232 | |
3233 | kiocb->ki_pos = 0; | |
3234 | return NULL; | |
d34e1e5b DY |
3235 | } |
3236 | ||
2ea537ca | 3237 | static void kiocb_done(struct io_kiocb *req, ssize_t ret, |
889fca73 | 3238 | unsigned int issue_flags) |
ba816ad6 | 3239 | { |
e8c2bc1f | 3240 | struct io_async_rw *io = req->async_data; |
ba04291e | 3241 | |
227c0c96 | 3242 | /* add previously done IO, if any */ |
d886e185 | 3243 | if (req_has_async_data(req) && io->bytes_done > 0) { |
227c0c96 | 3244 | if (ret < 0) |
e8c2bc1f | 3245 | ret = io->bytes_done; |
227c0c96 | 3246 | else |
e8c2bc1f | 3247 | ret += io->bytes_done; |
227c0c96 JA |
3248 | } |
3249 | ||
ba04291e | 3250 | if (req->flags & REQ_F_CUR_POS) |
2ea537ca PB |
3251 | req->file->f_pos = req->rw.kiocb.ki_pos; |
3252 | if (ret >= 0 && (req->rw.kiocb.ki_complete == io_complete_rw)) | |
00f6e68b | 3253 | __io_complete_rw(req, ret, issue_flags); |
ba816ad6 | 3254 | else |
2ea537ca | 3255 | io_rw_done(&req->rw.kiocb, ret); |
97284637 | 3256 | |
b66ceaf3 | 3257 | if (req->flags & REQ_F_REISSUE) { |
97284637 | 3258 | req->flags &= ~REQ_F_REISSUE; |
b91ef187 | 3259 | if (io_resubmit_prep(req)) |
773af691 | 3260 | io_req_task_queue_reissue(req); |
b91ef187 PB |
3261 | else |
3262 | io_req_task_queue_fail(req, ret); | |
97284637 | 3263 | } |
ba816ad6 JA |
3264 | } |
3265 | ||
eae071c9 PB |
3266 | static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter, |
3267 | struct io_mapped_ubuf *imu) | |
edafccee | 3268 | { |
9adbd45d | 3269 | size_t len = req->rw.len; |
75769e3f | 3270 | u64 buf_end, buf_addr = req->rw.addr; |
edafccee | 3271 | size_t offset; |
edafccee | 3272 | |
75769e3f | 3273 | if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end))) |
edafccee JA |
3274 | return -EFAULT; |
3275 | /* not inside the mapped region */ | |
4751f53d | 3276 | if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end)) |
edafccee JA |
3277 | return -EFAULT; |
3278 | ||
3279 | /* | |
3280 | * May not be a start of buffer, set size appropriately | |
3281 | * and advance us to the beginning. | |
3282 | */ | |
3283 | offset = buf_addr - imu->ubuf; | |
3284 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
3285 | |
3286 | if (offset) { | |
3287 | /* | |
3288 | * Don't use iov_iter_advance() here, as it's really slow for | |
3289 | * using the latter parts of a big fixed buffer - it iterates | |
3290 | * over each segment manually. We can cheat a bit here, because | |
3291 | * we know that: | |
3292 | * | |
3293 | * 1) it's a BVEC iter, we set it up | |
3294 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
3295 | * first and last bvec | |
3296 | * | |
3297 | * So just find our index, and adjust the iterator afterwards. | |
3298 | * If the offset is within the first bvec (or the whole first | |
3299 | * bvec, just use iov_iter_advance(). This makes it easier | |
3300 | * since we can just skip the first segment, which may not | |
3301 | * be PAGE_SIZE aligned. | |
3302 | */ | |
3303 | const struct bio_vec *bvec = imu->bvec; | |
3304 | ||
3305 | if (offset <= bvec->bv_len) { | |
3306 | iov_iter_advance(iter, offset); | |
3307 | } else { | |
3308 | unsigned long seg_skip; | |
3309 | ||
3310 | /* skip first vec */ | |
3311 | offset -= bvec->bv_len; | |
3312 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
3313 | ||
3314 | iter->bvec = bvec + seg_skip; | |
3315 | iter->nr_segs -= seg_skip; | |
99c79f66 | 3316 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 3317 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
3318 | } |
3319 | } | |
3320 | ||
847595de | 3321 | return 0; |
edafccee JA |
3322 | } |
3323 | ||
5106dd6e JA |
3324 | static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter, |
3325 | unsigned int issue_flags) | |
eae071c9 | 3326 | { |
eae071c9 PB |
3327 | struct io_mapped_ubuf *imu = req->imu; |
3328 | u16 index, buf_index = req->buf_index; | |
3329 | ||
3330 | if (likely(!imu)) { | |
578c0ee2 PB |
3331 | struct io_ring_ctx *ctx = req->ctx; |
3332 | ||
eae071c9 PB |
3333 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
3334 | return -EFAULT; | |
5106dd6e | 3335 | io_req_set_rsrc_node(req, ctx, issue_flags); |
eae071c9 PB |
3336 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
3337 | imu = READ_ONCE(ctx->user_bufs[index]); | |
3338 | req->imu = imu; | |
3339 | } | |
3340 | return __io_import_fixed(req, rw, iter, imu); | |
3341 | } | |
3342 | ||
dbc7d452 JA |
3343 | static void io_buffer_add_list(struct io_ring_ctx *ctx, |
3344 | struct io_buffer_list *bl, unsigned int bgid) | |
3345 | { | |
3346 | struct list_head *list; | |
3347 | ||
3348 | list = &ctx->io_buffers[hash_32(bgid, IO_BUFFERS_HASH_BITS)]; | |
3349 | INIT_LIST_HEAD(&bl->buf_list); | |
3350 | bl->bgid = bgid; | |
3351 | list_add(&bl->list, list); | |
3352 | } | |
3353 | ||
bcda7baa | 3354 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, |
51aac424 | 3355 | int bgid, unsigned int issue_flags) |
bcda7baa | 3356 | { |
30d51dd4 | 3357 | struct io_buffer *kbuf = req->kbuf; |
dbc7d452 JA |
3358 | struct io_ring_ctx *ctx = req->ctx; |
3359 | struct io_buffer_list *bl; | |
bcda7baa JA |
3360 | |
3361 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
3362 | return kbuf; | |
3363 | ||
f8929630 | 3364 | io_ring_submit_lock(req->ctx, issue_flags); |
bcda7baa | 3365 | |
dbc7d452 JA |
3366 | bl = io_buffer_get_list(ctx, bgid); |
3367 | if (bl && !list_empty(&bl->buf_list)) { | |
3368 | kbuf = list_first_entry(&bl->buf_list, struct io_buffer, list); | |
3369 | list_del(&kbuf->list); | |
bcda7baa JA |
3370 | if (*len > kbuf->len) |
3371 | *len = kbuf->len; | |
30d51dd4 PB |
3372 | req->flags |= REQ_F_BUFFER_SELECTED; |
3373 | req->kbuf = kbuf; | |
bcda7baa JA |
3374 | } else { |
3375 | kbuf = ERR_PTR(-ENOBUFS); | |
3376 | } | |
3377 | ||
f8929630 | 3378 | io_ring_submit_unlock(req->ctx, issue_flags); |
bcda7baa JA |
3379 | return kbuf; |
3380 | } | |
3381 | ||
4d954c25 | 3382 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
51aac424 | 3383 | unsigned int issue_flags) |
4d954c25 JA |
3384 | { |
3385 | struct io_buffer *kbuf; | |
4f4eeba8 | 3386 | u16 bgid; |
4d954c25 | 3387 | |
4f4eeba8 | 3388 | bgid = req->buf_index; |
51aac424 | 3389 | kbuf = io_buffer_select(req, len, bgid, issue_flags); |
4d954c25 JA |
3390 | if (IS_ERR(kbuf)) |
3391 | return kbuf; | |
4d954c25 JA |
3392 | return u64_to_user_ptr(kbuf->addr); |
3393 | } | |
3394 | ||
3395 | #ifdef CONFIG_COMPAT | |
3396 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, | |
51aac424 | 3397 | unsigned int issue_flags) |
4d954c25 JA |
3398 | { |
3399 | struct compat_iovec __user *uiov; | |
3400 | compat_ssize_t clen; | |
3401 | void __user *buf; | |
3402 | ssize_t len; | |
3403 | ||
3404 | uiov = u64_to_user_ptr(req->rw.addr); | |
3405 | if (!access_ok(uiov, sizeof(*uiov))) | |
3406 | return -EFAULT; | |
3407 | if (__get_user(clen, &uiov->iov_len)) | |
3408 | return -EFAULT; | |
3409 | if (clen < 0) | |
3410 | return -EINVAL; | |
3411 | ||
3412 | len = clen; | |
51aac424 | 3413 | buf = io_rw_buffer_select(req, &len, issue_flags); |
4d954c25 JA |
3414 | if (IS_ERR(buf)) |
3415 | return PTR_ERR(buf); | |
3416 | iov[0].iov_base = buf; | |
3417 | iov[0].iov_len = (compat_size_t) len; | |
3418 | return 0; | |
3419 | } | |
3420 | #endif | |
3421 | ||
3422 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
51aac424 | 3423 | unsigned int issue_flags) |
4d954c25 JA |
3424 | { |
3425 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); | |
3426 | void __user *buf; | |
3427 | ssize_t len; | |
3428 | ||
3429 | if (copy_from_user(iov, uiov, sizeof(*uiov))) | |
3430 | return -EFAULT; | |
3431 | ||
3432 | len = iov[0].iov_len; | |
3433 | if (len < 0) | |
3434 | return -EINVAL; | |
51aac424 | 3435 | buf = io_rw_buffer_select(req, &len, issue_flags); |
4d954c25 JA |
3436 | if (IS_ERR(buf)) |
3437 | return PTR_ERR(buf); | |
3438 | iov[0].iov_base = buf; | |
3439 | iov[0].iov_len = len; | |
3440 | return 0; | |
3441 | } | |
3442 | ||
3443 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
51aac424 | 3444 | unsigned int issue_flags) |
4d954c25 | 3445 | { |
dddb3e26 | 3446 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
30d51dd4 | 3447 | struct io_buffer *kbuf = req->kbuf; |
dddb3e26 | 3448 | |
dddb3e26 JA |
3449 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
3450 | iov[0].iov_len = kbuf->len; | |
4d954c25 | 3451 | return 0; |
dddb3e26 | 3452 | } |
dd201662 | 3453 | if (req->rw.len != 1) |
4d954c25 JA |
3454 | return -EINVAL; |
3455 | ||
3456 | #ifdef CONFIG_COMPAT | |
3457 | if (req->ctx->compat) | |
51aac424 | 3458 | return io_compat_import(req, iov, issue_flags); |
4d954c25 JA |
3459 | #endif |
3460 | ||
51aac424 | 3461 | return __io_iov_buffer_select(req, iov, issue_flags); |
4d954c25 JA |
3462 | } |
3463 | ||
caa8fe6e PB |
3464 | static struct iovec *__io_import_iovec(int rw, struct io_kiocb *req, |
3465 | struct io_rw_state *s, | |
3466 | unsigned int issue_flags) | |
2b188cc1 | 3467 | { |
5e49c973 | 3468 | struct iov_iter *iter = &s->iter; |
847595de | 3469 | u8 opcode = req->opcode; |
caa8fe6e | 3470 | struct iovec *iovec; |
d1d681b0 PB |
3471 | void __user *buf; |
3472 | size_t sqe_len; | |
4d954c25 | 3473 | ssize_t ret; |
edafccee | 3474 | |
f3251183 | 3475 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
5106dd6e | 3476 | ret = io_import_fixed(req, rw, iter, issue_flags); |
f3251183 PB |
3477 | if (ret) |
3478 | return ERR_PTR(ret); | |
3479 | return NULL; | |
3480 | } | |
2b188cc1 | 3481 | |
bcda7baa | 3482 | /* buffer index only valid with fixed read/write, or buffer select */ |
d1d681b0 | 3483 | if (unlikely(req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))) |
caa8fe6e | 3484 | return ERR_PTR(-EINVAL); |
9adbd45d | 3485 | |
d1d681b0 PB |
3486 | buf = u64_to_user_ptr(req->rw.addr); |
3487 | sqe_len = req->rw.len; | |
9adbd45d | 3488 | |
3a6820f2 | 3489 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
bcda7baa | 3490 | if (req->flags & REQ_F_BUFFER_SELECT) { |
51aac424 | 3491 | buf = io_rw_buffer_select(req, &sqe_len, issue_flags); |
867a23ea | 3492 | if (IS_ERR(buf)) |
898df244 | 3493 | return ERR_CAST(buf); |
3f9d6441 | 3494 | req->rw.len = sqe_len; |
bcda7baa JA |
3495 | } |
3496 | ||
5e49c973 | 3497 | ret = import_single_range(rw, buf, sqe_len, s->fast_iov, iter); |
f3251183 PB |
3498 | if (ret) |
3499 | return ERR_PTR(ret); | |
3500 | return NULL; | |
3a6820f2 JA |
3501 | } |
3502 | ||
caa8fe6e | 3503 | iovec = s->fast_iov; |
4d954c25 | 3504 | if (req->flags & REQ_F_BUFFER_SELECT) { |
caa8fe6e | 3505 | ret = io_iov_buffer_select(req, iovec, issue_flags); |
f3251183 PB |
3506 | if (ret) |
3507 | return ERR_PTR(ret); | |
3508 | iov_iter_init(iter, rw, iovec, 1, iovec->iov_len); | |
3509 | return NULL; | |
4d954c25 JA |
3510 | } |
3511 | ||
caa8fe6e | 3512 | ret = __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, &iovec, iter, |
89cd35c5 | 3513 | req->ctx->compat); |
caa8fe6e PB |
3514 | if (unlikely(ret < 0)) |
3515 | return ERR_PTR(ret); | |
3516 | return iovec; | |
2b188cc1 JA |
3517 | } |
3518 | ||
5e49c973 PB |
3519 | static inline int io_import_iovec(int rw, struct io_kiocb *req, |
3520 | struct iovec **iovec, struct io_rw_state *s, | |
3521 | unsigned int issue_flags) | |
3522 | { | |
caa8fe6e PB |
3523 | *iovec = __io_import_iovec(rw, req, s, issue_flags); |
3524 | if (unlikely(IS_ERR(*iovec))) | |
3525 | return PTR_ERR(*iovec); | |
5e49c973 | 3526 | |
5e49c973 | 3527 | iov_iter_save_state(&s->iter, &s->iter_state); |
caa8fe6e | 3528 | return 0; |
2b188cc1 JA |
3529 | } |
3530 | ||
0fef9483 JA |
3531 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
3532 | { | |
5b09e37e | 3533 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
0fef9483 JA |
3534 | } |
3535 | ||
31b51510 | 3536 | /* |
32960613 JA |
3537 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
3538 | * by looping over ->read() or ->write() manually. | |
31b51510 | 3539 | */ |
4017eb91 | 3540 | static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) |
32960613 | 3541 | { |
4017eb91 JA |
3542 | struct kiocb *kiocb = &req->rw.kiocb; |
3543 | struct file *file = req->file; | |
32960613 | 3544 | ssize_t ret = 0; |
af9c45ec | 3545 | loff_t *ppos; |
32960613 JA |
3546 | |
3547 | /* | |
3548 | * Don't support polled IO through this interface, and we can't | |
3549 | * support non-blocking either. For the latter, this just causes | |
3550 | * the kiocb to be handled from an async context. | |
3551 | */ | |
3552 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3553 | return -EOPNOTSUPP; | |
35645ac3 PB |
3554 | if ((kiocb->ki_flags & IOCB_NOWAIT) && |
3555 | !(kiocb->ki_filp->f_flags & O_NONBLOCK)) | |
32960613 JA |
3556 | return -EAGAIN; |
3557 | ||
af9c45ec DY |
3558 | ppos = io_kiocb_ppos(kiocb); |
3559 | ||
32960613 | 3560 | while (iov_iter_count(iter)) { |
311ae9e1 | 3561 | struct iovec iovec; |
32960613 JA |
3562 | ssize_t nr; |
3563 | ||
311ae9e1 PB |
3564 | if (!iov_iter_is_bvec(iter)) { |
3565 | iovec = iov_iter_iovec(iter); | |
3566 | } else { | |
4017eb91 JA |
3567 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
3568 | iovec.iov_len = req->rw.len; | |
311ae9e1 PB |
3569 | } |
3570 | ||
32960613 JA |
3571 | if (rw == READ) { |
3572 | nr = file->f_op->read(file, iovec.iov_base, | |
af9c45ec | 3573 | iovec.iov_len, ppos); |
32960613 JA |
3574 | } else { |
3575 | nr = file->f_op->write(file, iovec.iov_base, | |
af9c45ec | 3576 | iovec.iov_len, ppos); |
32960613 JA |
3577 | } |
3578 | ||
3579 | if (nr < 0) { | |
3580 | if (!ret) | |
3581 | ret = nr; | |
3582 | break; | |
3583 | } | |
5e929367 | 3584 | ret += nr; |
16c8d2df JA |
3585 | if (!iov_iter_is_bvec(iter)) { |
3586 | iov_iter_advance(iter, nr); | |
3587 | } else { | |
16c8d2df | 3588 | req->rw.addr += nr; |
5e929367 JA |
3589 | req->rw.len -= nr; |
3590 | if (!req->rw.len) | |
3591 | break; | |
16c8d2df | 3592 | } |
32960613 JA |
3593 | if (nr != iovec.iov_len) |
3594 | break; | |
32960613 JA |
3595 | } |
3596 | ||
3597 | return ret; | |
3598 | } | |
3599 | ||
ff6165b2 JA |
3600 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
3601 | const struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 3602 | { |
e8c2bc1f | 3603 | struct io_async_rw *rw = req->async_data; |
b64e3444 | 3604 | |
538941e2 | 3605 | memcpy(&rw->s.iter, iter, sizeof(*iter)); |
afb87658 | 3606 | rw->free_iovec = iovec; |
227c0c96 | 3607 | rw->bytes_done = 0; |
ff6165b2 | 3608 | /* can only be fixed buffers, no need to do anything */ |
9c3a205c | 3609 | if (iov_iter_is_bvec(iter)) |
ff6165b2 | 3610 | return; |
b64e3444 | 3611 | if (!iovec) { |
ff6165b2 JA |
3612 | unsigned iov_off = 0; |
3613 | ||
538941e2 | 3614 | rw->s.iter.iov = rw->s.fast_iov; |
ff6165b2 JA |
3615 | if (iter->iov != fast_iov) { |
3616 | iov_off = iter->iov - fast_iov; | |
538941e2 | 3617 | rw->s.iter.iov += iov_off; |
ff6165b2 | 3618 | } |
538941e2 PB |
3619 | if (rw->s.fast_iov != fast_iov) |
3620 | memcpy(rw->s.fast_iov + iov_off, fast_iov + iov_off, | |
45097dae | 3621 | sizeof(struct iovec) * iter->nr_segs); |
99bc4c38 PB |
3622 | } else { |
3623 | req->flags |= REQ_F_NEED_CLEANUP; | |
f67676d1 JA |
3624 | } |
3625 | } | |
3626 | ||
8d4af685 | 3627 | static inline bool io_alloc_async_data(struct io_kiocb *req) |
3d9932a8 | 3628 | { |
e8c2bc1f JA |
3629 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
3630 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); | |
d886e185 PB |
3631 | if (req->async_data) { |
3632 | req->flags |= REQ_F_ASYNC_DATA; | |
3633 | return false; | |
3634 | } | |
3635 | return true; | |
3d9932a8 XW |
3636 | } |
3637 | ||
ff6165b2 | 3638 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
c88598a9 | 3639 | struct io_rw_state *s, bool force) |
b7bb4f7d | 3640 | { |
26f0505a | 3641 | if (!force && !io_op_defs[req->opcode].needs_async_setup) |
74566df3 | 3642 | return 0; |
d886e185 | 3643 | if (!req_has_async_data(req)) { |
cd658695 JA |
3644 | struct io_async_rw *iorw; |
3645 | ||
6cb78689 | 3646 | if (io_alloc_async_data(req)) { |
6bf985dc | 3647 | kfree(iovec); |
5d204bcf | 3648 | return -ENOMEM; |
6bf985dc | 3649 | } |
b7bb4f7d | 3650 | |
c88598a9 | 3651 | io_req_map_rw(req, iovec, s->fast_iov, &s->iter); |
cd658695 JA |
3652 | iorw = req->async_data; |
3653 | /* we've copied and mapped the iter, ensure state is saved */ | |
538941e2 | 3654 | iov_iter_save_state(&iorw->s.iter, &iorw->s.iter_state); |
5d204bcf | 3655 | } |
b7bb4f7d | 3656 | return 0; |
f67676d1 JA |
3657 | } |
3658 | ||
73debe68 | 3659 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw) |
c3e330a4 | 3660 | { |
e8c2bc1f | 3661 | struct io_async_rw *iorw = req->async_data; |
5e49c973 | 3662 | struct iovec *iov; |
847595de | 3663 | int ret; |
c3e330a4 | 3664 | |
51aac424 | 3665 | /* submission path, ->uring_lock should already be taken */ |
3b44b371 | 3666 | ret = io_import_iovec(rw, req, &iov, &iorw->s, 0); |
c3e330a4 PB |
3667 | if (unlikely(ret < 0)) |
3668 | return ret; | |
3669 | ||
ab0b196c PB |
3670 | iorw->bytes_done = 0; |
3671 | iorw->free_iovec = iov; | |
3672 | if (iov) | |
3673 | req->flags |= REQ_F_NEED_CLEANUP; | |
c3e330a4 PB |
3674 | return 0; |
3675 | } | |
3676 | ||
c1dd91d1 | 3677 | /* |
ffdc8dab | 3678 | * This is our waitqueue callback handler, registered through __folio_lock_async() |
c1dd91d1 JA |
3679 | * when we initially tried to do the IO with the iocb armed our waitqueue. |
3680 | * This gets called when the page is unlocked, and we generally expect that to | |
3681 | * happen when the page IO is completed and the page is now uptodate. This will | |
3682 | * queue a task_work based retry of the operation, attempting to copy the data | |
3683 | * again. If the latter fails because the page was NOT uptodate, then we will | |
3684 | * do a thread based blocking retry of the operation. That's the unexpected | |
3685 | * slow path. | |
3686 | */ | |
bcf5a063 JA |
3687 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
3688 | int sync, void *arg) | |
3689 | { | |
3690 | struct wait_page_queue *wpq; | |
3691 | struct io_kiocb *req = wait->private; | |
bcf5a063 | 3692 | struct wait_page_key *key = arg; |
bcf5a063 JA |
3693 | |
3694 | wpq = container_of(wait, struct wait_page_queue, wait); | |
3695 | ||
cdc8fcb4 LT |
3696 | if (!wake_page_match(wpq, key)) |
3697 | return 0; | |
3698 | ||
c8d317aa | 3699 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
bcf5a063 | 3700 | list_del_init(&wait->entry); |
921b9054 | 3701 | io_req_task_queue(req); |
bcf5a063 JA |
3702 | return 1; |
3703 | } | |
3704 | ||
c1dd91d1 JA |
3705 | /* |
3706 | * This controls whether a given IO request should be armed for async page | |
3707 | * based retry. If we return false here, the request is handed to the async | |
3708 | * worker threads for retry. If we're doing buffered reads on a regular file, | |
3709 | * we prepare a private wait_page_queue entry and retry the operation. This | |
3710 | * will either succeed because the page is now uptodate and unlocked, or it | |
3711 | * will register a callback when the page is unlocked at IO completion. Through | |
3712 | * that callback, io_uring uses task_work to setup a retry of the operation. | |
3713 | * That retry will attempt the buffered read again. The retry will generally | |
3714 | * succeed, or in rare cases where it fails, we then fall back to using the | |
3715 | * async worker threads for a blocking retry. | |
3716 | */ | |
227c0c96 | 3717 | static bool io_rw_should_retry(struct io_kiocb *req) |
f67676d1 | 3718 | { |
e8c2bc1f JA |
3719 | struct io_async_rw *rw = req->async_data; |
3720 | struct wait_page_queue *wait = &rw->wpq; | |
bcf5a063 | 3721 | struct kiocb *kiocb = &req->rw.kiocb; |
f67676d1 | 3722 | |
bcf5a063 JA |
3723 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
3724 | if (req->flags & REQ_F_NOWAIT) | |
3725 | return false; | |
f67676d1 | 3726 | |
227c0c96 | 3727 | /* Only for buffered IO */ |
3b2a4439 | 3728 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
bcf5a063 | 3729 | return false; |
3b2a4439 | 3730 | |
bcf5a063 JA |
3731 | /* |
3732 | * just use poll if we can, and don't attempt if the fs doesn't | |
3733 | * support callback based unlocks | |
3734 | */ | |
3735 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) | |
3736 | return false; | |
f67676d1 | 3737 | |
3b2a4439 JA |
3738 | wait->wait.func = io_async_buf_func; |
3739 | wait->wait.private = req; | |
3740 | wait->wait.flags = 0; | |
3741 | INIT_LIST_HEAD(&wait->wait.entry); | |
3742 | kiocb->ki_flags |= IOCB_WAITQ; | |
c8d317aa | 3743 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3b2a4439 | 3744 | kiocb->ki_waitq = wait; |
3b2a4439 | 3745 | return true; |
bcf5a063 JA |
3746 | } |
3747 | ||
aeab9506 | 3748 | static inline int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) |
bcf5a063 | 3749 | { |
607b6fb8 | 3750 | if (likely(req->file->f_op->read_iter)) |
bcf5a063 | 3751 | return call_read_iter(req->file, &req->rw.kiocb, iter); |
2dd2111d | 3752 | else if (req->file->f_op->read) |
4017eb91 | 3753 | return loop_rw_iter(READ, req, iter); |
2dd2111d GH |
3754 | else |
3755 | return -EINVAL; | |
f67676d1 JA |
3756 | } |
3757 | ||
7db30437 ML |
3758 | static bool need_read_all(struct io_kiocb *req) |
3759 | { | |
3760 | return req->flags & REQ_F_ISREG || | |
3761 | S_ISBLK(file_inode(req->file)->i_mode); | |
3762 | } | |
3763 | ||
584b0180 JA |
3764 | static int io_rw_init_file(struct io_kiocb *req, fmode_t mode) |
3765 | { | |
3766 | struct kiocb *kiocb = &req->rw.kiocb; | |
3767 | struct io_ring_ctx *ctx = req->ctx; | |
3768 | struct file *file = req->file; | |
3769 | int ret; | |
3770 | ||
3771 | if (unlikely(!file || !(file->f_mode & mode))) | |
3772 | return -EBADF; | |
3773 | ||
3774 | if (!io_req_ffs_set(req)) | |
3775 | req->flags |= io_file_get_flags(file) << REQ_F_SUPPORT_NOWAIT_BIT; | |
3776 | ||
3777 | kiocb->ki_flags = iocb_flags(file); | |
3778 | ret = kiocb_set_rw_flags(kiocb, req->rw.flags); | |
3779 | if (unlikely(ret)) | |
3780 | return ret; | |
3781 | ||
3782 | /* | |
3783 | * If the file is marked O_NONBLOCK, still allow retry for it if it | |
3784 | * supports async. Otherwise it's impossible to use O_NONBLOCK files | |
3785 | * reliably. If not, or it IOCB_NOWAIT is set, don't retry. | |
3786 | */ | |
3787 | if ((kiocb->ki_flags & IOCB_NOWAIT) || | |
3788 | ((file->f_flags & O_NONBLOCK) && !io_file_supports_nowait(req))) | |
3789 | req->flags |= REQ_F_NOWAIT; | |
3790 | ||
3791 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
3792 | if (!(kiocb->ki_flags & IOCB_DIRECT) || !file->f_op->iopoll) | |
3793 | return -EOPNOTSUPP; | |
3794 | ||
3795 | kiocb->ki_flags |= IOCB_HIPRI | IOCB_ALLOC_CACHE; | |
3796 | kiocb->ki_complete = io_complete_rw_iopoll; | |
3797 | req->iopoll_completed = 0; | |
3798 | } else { | |
3799 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3800 | return -EINVAL; | |
3801 | kiocb->ki_complete = io_complete_rw; | |
3802 | } | |
3803 | ||
3804 | return 0; | |
3805 | } | |
3806 | ||
889fca73 | 3807 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 3808 | { |
607b6fb8 | 3809 | struct io_rw_state __s, *s = &__s; |
c88598a9 | 3810 | struct iovec *iovec; |
9adbd45d | 3811 | struct kiocb *kiocb = &req->rw.kiocb; |
45d189c6 | 3812 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
d886e185 | 3813 | struct io_async_rw *rw; |
cd658695 | 3814 | ssize_t ret, ret2; |
b4aec400 | 3815 | loff_t *ppos; |
ff6165b2 | 3816 | |
607b6fb8 PB |
3817 | if (!req_has_async_data(req)) { |
3818 | ret = io_import_iovec(READ, req, &iovec, s, issue_flags); | |
3819 | if (unlikely(ret < 0)) | |
3820 | return ret; | |
3821 | } else { | |
2be2eb02 JA |
3822 | /* |
3823 | * Safe and required to re-import if we're using provided | |
3824 | * buffers, as we dropped the selected one before retry. | |
3825 | */ | |
3826 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
3827 | ret = io_import_iovec(READ, req, &iovec, s, issue_flags); | |
3828 | if (unlikely(ret < 0)) | |
3829 | return ret; | |
3830 | } | |
3831 | ||
d886e185 | 3832 | rw = req->async_data; |
c88598a9 | 3833 | s = &rw->s; |
cd658695 JA |
3834 | /* |
3835 | * We come here from an earlier attempt, restore our state to | |
3836 | * match in case it doesn't. It's cheap enough that we don't | |
3837 | * need to make this conditional. | |
3838 | */ | |
c88598a9 | 3839 | iov_iter_restore(&s->iter, &s->iter_state); |
2846c481 | 3840 | iovec = NULL; |
2846c481 | 3841 | } |
584b0180 | 3842 | ret = io_rw_init_file(req, FMODE_READ); |
323b190b JA |
3843 | if (unlikely(ret)) { |
3844 | kfree(iovec); | |
584b0180 | 3845 | return ret; |
323b190b | 3846 | } |
c88598a9 | 3847 | req->result = iov_iter_count(&s->iter); |
2b188cc1 | 3848 | |
607b6fb8 PB |
3849 | if (force_nonblock) { |
3850 | /* If the file doesn't support async, just async punt */ | |
35645ac3 | 3851 | if (unlikely(!io_file_supports_nowait(req))) { |
607b6fb8 PB |
3852 | ret = io_setup_async_rw(req, iovec, s, true); |
3853 | return ret ?: -EAGAIN; | |
3854 | } | |
a88fc400 | 3855 | kiocb->ki_flags |= IOCB_NOWAIT; |
607b6fb8 PB |
3856 | } else { |
3857 | /* Ensure we clear previously set non-block flag */ | |
3858 | kiocb->ki_flags &= ~IOCB_NOWAIT; | |
6713e7a6 | 3859 | } |
9e645e11 | 3860 | |
b4aec400 | 3861 | ppos = io_kiocb_update_pos(req); |
d34e1e5b | 3862 | |
b4aec400 | 3863 | ret = rw_verify_area(READ, req->file, ppos, req->result); |
5ea5dd45 PB |
3864 | if (unlikely(ret)) { |
3865 | kfree(iovec); | |
3866 | return ret; | |
3867 | } | |
2b188cc1 | 3868 | |
c88598a9 | 3869 | ret = io_iter_do_read(req, &s->iter); |
32960613 | 3870 | |
230d50d4 | 3871 | if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) { |
6ad7f233 | 3872 | req->flags &= ~REQ_F_REISSUE; |
9af177ee JA |
3873 | /* if we can poll, just do that */ |
3874 | if (req->opcode == IORING_OP_READ && file_can_poll(req->file)) | |
3875 | return -EAGAIN; | |
eefdf30f JA |
3876 | /* IOPOLL retry should happen for io-wq threads */ |
3877 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
f91daf56 | 3878 | goto done; |
75c668cd PB |
3879 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3880 | if (req->flags & REQ_F_NOWAIT) | |
355afaeb | 3881 | goto done; |
f38c7e3a | 3882 | ret = 0; |
230d50d4 JA |
3883 | } else if (ret == -EIOCBQUEUED) { |
3884 | goto out_free; | |
f80a50a6 | 3885 | } else if (ret == req->result || ret <= 0 || !force_nonblock || |
7db30437 | 3886 | (req->flags & REQ_F_NOWAIT) || !need_read_all(req)) { |
7335e3bf | 3887 | /* read all, failed, already did sync or don't want to retry */ |
00d23d51 | 3888 | goto done; |
227c0c96 JA |
3889 | } |
3890 | ||
cd658695 JA |
3891 | /* |
3892 | * Don't depend on the iter state matching what was consumed, or being | |
3893 | * untouched in case of error. Restore it and we'll advance it | |
3894 | * manually if we need to. | |
3895 | */ | |
c88598a9 | 3896 | iov_iter_restore(&s->iter, &s->iter_state); |
cd658695 | 3897 | |
c88598a9 | 3898 | ret2 = io_setup_async_rw(req, iovec, s, true); |
6bf985dc PB |
3899 | if (ret2) |
3900 | return ret2; | |
3901 | ||
fe1cdd55 | 3902 | iovec = NULL; |
e8c2bc1f | 3903 | rw = req->async_data; |
c88598a9 | 3904 | s = &rw->s; |
cd658695 JA |
3905 | /* |
3906 | * Now use our persistent iterator and state, if we aren't already. | |
3907 | * We've restored and mapped the iter to match. | |
3908 | */ | |
227c0c96 | 3909 | |
b23df91b | 3910 | do { |
cd658695 JA |
3911 | /* |
3912 | * We end up here because of a partial read, either from | |
3913 | * above or inside this loop. Advance the iter by the bytes | |
3914 | * that were consumed. | |
3915 | */ | |
c88598a9 PB |
3916 | iov_iter_advance(&s->iter, ret); |
3917 | if (!iov_iter_count(&s->iter)) | |
cd658695 | 3918 | break; |
b23df91b | 3919 | rw->bytes_done += ret; |
c88598a9 | 3920 | iov_iter_save_state(&s->iter, &s->iter_state); |
cd658695 | 3921 | |
b23df91b PB |
3922 | /* if we can retry, do so with the callbacks armed */ |
3923 | if (!io_rw_should_retry(req)) { | |
3924 | kiocb->ki_flags &= ~IOCB_WAITQ; | |
3925 | return -EAGAIN; | |
3926 | } | |
3927 | ||
3928 | /* | |
3929 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If | |
3930 | * we get -EIOCBQUEUED, then we'll get a notification when the | |
3931 | * desired page gets unlocked. We can also get a partial read | |
3932 | * here, and if we do, then just retry at the new offset. | |
3933 | */ | |
c88598a9 | 3934 | ret = io_iter_do_read(req, &s->iter); |
b23df91b PB |
3935 | if (ret == -EIOCBQUEUED) |
3936 | return 0; | |
227c0c96 | 3937 | /* we got some bytes, but not all. retry. */ |
b5b0ecb7 | 3938 | kiocb->ki_flags &= ~IOCB_WAITQ; |
c88598a9 | 3939 | iov_iter_restore(&s->iter, &s->iter_state); |
cd658695 | 3940 | } while (ret > 0); |
227c0c96 | 3941 | done: |
2ea537ca | 3942 | kiocb_done(req, ret, issue_flags); |
fe1cdd55 PB |
3943 | out_free: |
3944 | /* it's faster to check here then delegate to kfree */ | |
3945 | if (iovec) | |
3946 | kfree(iovec); | |
5ea5dd45 | 3947 | return 0; |
2b188cc1 JA |
3948 | } |
3949 | ||
889fca73 | 3950 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 3951 | { |
607b6fb8 | 3952 | struct io_rw_state __s, *s = &__s; |
c88598a9 | 3953 | struct iovec *iovec; |
9adbd45d | 3954 | struct kiocb *kiocb = &req->rw.kiocb; |
45d189c6 | 3955 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
cd658695 | 3956 | ssize_t ret, ret2; |
b4aec400 | 3957 | loff_t *ppos; |
2b188cc1 | 3958 | |
607b6fb8 | 3959 | if (!req_has_async_data(req)) { |
5e49c973 PB |
3960 | ret = io_import_iovec(WRITE, req, &iovec, s, issue_flags); |
3961 | if (unlikely(ret < 0)) | |
2846c481 | 3962 | return ret; |
607b6fb8 PB |
3963 | } else { |
3964 | struct io_async_rw *rw = req->async_data; | |
3965 | ||
3966 | s = &rw->s; | |
3967 | iov_iter_restore(&s->iter, &s->iter_state); | |
2846c481 | 3968 | iovec = NULL; |
2846c481 | 3969 | } |
584b0180 | 3970 | ret = io_rw_init_file(req, FMODE_WRITE); |
323b190b JA |
3971 | if (unlikely(ret)) { |
3972 | kfree(iovec); | |
584b0180 | 3973 | return ret; |
323b190b | 3974 | } |
c88598a9 | 3975 | req->result = iov_iter_count(&s->iter); |
2b188cc1 | 3976 | |
607b6fb8 PB |
3977 | if (force_nonblock) { |
3978 | /* If the file doesn't support async, just async punt */ | |
35645ac3 | 3979 | if (unlikely(!io_file_supports_nowait(req))) |
607b6fb8 | 3980 | goto copy_iov; |
fd6c2e4c | 3981 | |
607b6fb8 PB |
3982 | /* file path doesn't support NOWAIT for non-direct_IO */ |
3983 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
3984 | (req->flags & REQ_F_ISREG)) | |
3985 | goto copy_iov; | |
31b51510 | 3986 | |
607b6fb8 PB |
3987 | kiocb->ki_flags |= IOCB_NOWAIT; |
3988 | } else { | |
3989 | /* Ensure we clear previously set non-block flag */ | |
3990 | kiocb->ki_flags &= ~IOCB_NOWAIT; | |
3991 | } | |
31b51510 | 3992 | |
b4aec400 | 3993 | ppos = io_kiocb_update_pos(req); |
d34e1e5b | 3994 | |
b4aec400 | 3995 | ret = rw_verify_area(WRITE, req->file, ppos, req->result); |
fa15bafb PB |
3996 | if (unlikely(ret)) |
3997 | goto out_free; | |
4ed734b0 | 3998 | |
fa15bafb PB |
3999 | /* |
4000 | * Open-code file_start_write here to grab freeze protection, | |
4001 | * which will be released by another thread in | |
4002 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
4003 | * released so that it doesn't complain about the held lock when | |
4004 | * we return to userspace. | |
4005 | */ | |
4006 | if (req->flags & REQ_F_ISREG) { | |
8a3c84b6 | 4007 | sb_start_write(file_inode(req->file)->i_sb); |
fa15bafb PB |
4008 | __sb_writers_release(file_inode(req->file)->i_sb, |
4009 | SB_FREEZE_WRITE); | |
4010 | } | |
4011 | kiocb->ki_flags |= IOCB_WRITE; | |
4ed734b0 | 4012 | |
35645ac3 | 4013 | if (likely(req->file->f_op->write_iter)) |
c88598a9 | 4014 | ret2 = call_write_iter(req->file, kiocb, &s->iter); |
2dd2111d | 4015 | else if (req->file->f_op->write) |
c88598a9 | 4016 | ret2 = loop_rw_iter(WRITE, req, &s->iter); |
2dd2111d GH |
4017 | else |
4018 | ret2 = -EINVAL; | |
4ed734b0 | 4019 | |
6ad7f233 PB |
4020 | if (req->flags & REQ_F_REISSUE) { |
4021 | req->flags &= ~REQ_F_REISSUE; | |
230d50d4 | 4022 | ret2 = -EAGAIN; |
6ad7f233 | 4023 | } |
230d50d4 | 4024 | |
fa15bafb PB |
4025 | /* |
4026 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just | |
4027 | * retry them without IOCB_NOWAIT. | |
4028 | */ | |
4029 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) | |
4030 | ret2 = -EAGAIN; | |
75c668cd PB |
4031 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
4032 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) | |
355afaeb | 4033 | goto done; |
fa15bafb | 4034 | if (!force_nonblock || ret2 != -EAGAIN) { |
eefdf30f | 4035 | /* IOPOLL retry should happen for io-wq threads */ |
b10841c9 | 4036 | if (ret2 == -EAGAIN && (req->ctx->flags & IORING_SETUP_IOPOLL)) |
eefdf30f | 4037 | goto copy_iov; |
355afaeb | 4038 | done: |
2ea537ca | 4039 | kiocb_done(req, ret2, issue_flags); |
fa15bafb | 4040 | } else { |
f67676d1 | 4041 | copy_iov: |
c88598a9 PB |
4042 | iov_iter_restore(&s->iter, &s->iter_state); |
4043 | ret = io_setup_async_rw(req, iovec, s, false); | |
6bf985dc | 4044 | return ret ?: -EAGAIN; |
2b188cc1 | 4045 | } |
31b51510 | 4046 | out_free: |
f261c168 | 4047 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 4048 | if (iovec) |
6f2cc166 | 4049 | kfree(iovec); |
2b188cc1 JA |
4050 | return ret; |
4051 | } | |
4052 | ||
80a261fd JA |
4053 | static int io_renameat_prep(struct io_kiocb *req, |
4054 | const struct io_uring_sqe *sqe) | |
4055 | { | |
4056 | struct io_rename *ren = &req->rename; | |
4057 | const char __user *oldf, *newf; | |
4058 | ||
ed7eb259 JA |
4059 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4060 | return -EINVAL; | |
26578cda | 4061 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
ed7eb259 | 4062 | return -EINVAL; |
80a261fd JA |
4063 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
4064 | return -EBADF; | |
4065 | ||
4066 | ren->old_dfd = READ_ONCE(sqe->fd); | |
4067 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4068 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
4069 | ren->new_dfd = READ_ONCE(sqe->len); | |
4070 | ren->flags = READ_ONCE(sqe->rename_flags); | |
4071 | ||
4072 | ren->oldpath = getname(oldf); | |
4073 | if (IS_ERR(ren->oldpath)) | |
4074 | return PTR_ERR(ren->oldpath); | |
4075 | ||
4076 | ren->newpath = getname(newf); | |
4077 | if (IS_ERR(ren->newpath)) { | |
4078 | putname(ren->oldpath); | |
4079 | return PTR_ERR(ren->newpath); | |
4080 | } | |
4081 | ||
4082 | req->flags |= REQ_F_NEED_CLEANUP; | |
4083 | return 0; | |
4084 | } | |
4085 | ||
45d189c6 | 4086 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
80a261fd JA |
4087 | { |
4088 | struct io_rename *ren = &req->rename; | |
4089 | int ret; | |
4090 | ||
45d189c6 | 4091 | if (issue_flags & IO_URING_F_NONBLOCK) |
80a261fd JA |
4092 | return -EAGAIN; |
4093 | ||
4094 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, | |
4095 | ren->newpath, ren->flags); | |
4096 | ||
4097 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4098 | if (ret < 0) | |
93d2bcd2 | 4099 | req_set_fail(req); |
80a261fd JA |
4100 | io_req_complete(req, ret); |
4101 | return 0; | |
4102 | } | |
4103 | ||
14a1143b JA |
4104 | static int io_unlinkat_prep(struct io_kiocb *req, |
4105 | const struct io_uring_sqe *sqe) | |
4106 | { | |
4107 | struct io_unlink *un = &req->unlink; | |
4108 | const char __user *fname; | |
4109 | ||
22634bc5 JA |
4110 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4111 | return -EINVAL; | |
26578cda PB |
4112 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
4113 | sqe->splice_fd_in) | |
22634bc5 | 4114 | return -EINVAL; |
14a1143b JA |
4115 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
4116 | return -EBADF; | |
4117 | ||
4118 | un->dfd = READ_ONCE(sqe->fd); | |
4119 | ||
4120 | un->flags = READ_ONCE(sqe->unlink_flags); | |
4121 | if (un->flags & ~AT_REMOVEDIR) | |
4122 | return -EINVAL; | |
4123 | ||
4124 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4125 | un->filename = getname(fname); | |
4126 | if (IS_ERR(un->filename)) | |
4127 | return PTR_ERR(un->filename); | |
4128 | ||
4129 | req->flags |= REQ_F_NEED_CLEANUP; | |
4130 | return 0; | |
4131 | } | |
4132 | ||
45d189c6 | 4133 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
14a1143b JA |
4134 | { |
4135 | struct io_unlink *un = &req->unlink; | |
4136 | int ret; | |
4137 | ||
45d189c6 | 4138 | if (issue_flags & IO_URING_F_NONBLOCK) |
14a1143b JA |
4139 | return -EAGAIN; |
4140 | ||
4141 | if (un->flags & AT_REMOVEDIR) | |
4142 | ret = do_rmdir(un->dfd, un->filename); | |
4143 | else | |
4144 | ret = do_unlinkat(un->dfd, un->filename); | |
4145 | ||
4146 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4147 | if (ret < 0) | |
93d2bcd2 | 4148 | req_set_fail(req); |
14a1143b JA |
4149 | io_req_complete(req, ret); |
4150 | return 0; | |
4151 | } | |
4152 | ||
e34a02dc DK |
4153 | static int io_mkdirat_prep(struct io_kiocb *req, |
4154 | const struct io_uring_sqe *sqe) | |
4155 | { | |
4156 | struct io_mkdir *mkd = &req->mkdir; | |
4157 | const char __user *fname; | |
4158 | ||
4159 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
4160 | return -EINVAL; | |
4161 | if (sqe->ioprio || sqe->off || sqe->rw_flags || sqe->buf_index || | |
4162 | sqe->splice_fd_in) | |
4163 | return -EINVAL; | |
4164 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
4165 | return -EBADF; | |
4166 | ||
4167 | mkd->dfd = READ_ONCE(sqe->fd); | |
4168 | mkd->mode = READ_ONCE(sqe->len); | |
4169 | ||
4170 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4171 | mkd->filename = getname(fname); | |
4172 | if (IS_ERR(mkd->filename)) | |
4173 | return PTR_ERR(mkd->filename); | |
4174 | ||
4175 | req->flags |= REQ_F_NEED_CLEANUP; | |
4176 | return 0; | |
4177 | } | |
4178 | ||
04f34081 | 4179 | static int io_mkdirat(struct io_kiocb *req, unsigned int issue_flags) |
e34a02dc DK |
4180 | { |
4181 | struct io_mkdir *mkd = &req->mkdir; | |
4182 | int ret; | |
4183 | ||
4184 | if (issue_flags & IO_URING_F_NONBLOCK) | |
4185 | return -EAGAIN; | |
4186 | ||
4187 | ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode); | |
4188 | ||
4189 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4190 | if (ret < 0) | |
4191 | req_set_fail(req); | |
4192 | io_req_complete(req, ret); | |
4193 | return 0; | |
4194 | } | |
4195 | ||
7a8721f8 DK |
4196 | static int io_symlinkat_prep(struct io_kiocb *req, |
4197 | const struct io_uring_sqe *sqe) | |
4198 | { | |
4199 | struct io_symlink *sl = &req->symlink; | |
4200 | const char __user *oldpath, *newpath; | |
4201 | ||
4202 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
4203 | return -EINVAL; | |
4204 | if (sqe->ioprio || sqe->len || sqe->rw_flags || sqe->buf_index || | |
4205 | sqe->splice_fd_in) | |
4206 | return -EINVAL; | |
4207 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
4208 | return -EBADF; | |
4209 | ||
4210 | sl->new_dfd = READ_ONCE(sqe->fd); | |
4211 | oldpath = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4212 | newpath = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
4213 | ||
4214 | sl->oldpath = getname(oldpath); | |
4215 | if (IS_ERR(sl->oldpath)) | |
4216 | return PTR_ERR(sl->oldpath); | |
4217 | ||
4218 | sl->newpath = getname(newpath); | |
4219 | if (IS_ERR(sl->newpath)) { | |
4220 | putname(sl->oldpath); | |
4221 | return PTR_ERR(sl->newpath); | |
4222 | } | |
4223 | ||
4224 | req->flags |= REQ_F_NEED_CLEANUP; | |
4225 | return 0; | |
4226 | } | |
4227 | ||
04f34081 | 4228 | static int io_symlinkat(struct io_kiocb *req, unsigned int issue_flags) |
7a8721f8 DK |
4229 | { |
4230 | struct io_symlink *sl = &req->symlink; | |
4231 | int ret; | |
4232 | ||
4233 | if (issue_flags & IO_URING_F_NONBLOCK) | |
4234 | return -EAGAIN; | |
4235 | ||
4236 | ret = do_symlinkat(sl->oldpath, sl->new_dfd, sl->newpath); | |
4237 | ||
4238 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4239 | if (ret < 0) | |
4240 | req_set_fail(req); | |
4241 | io_req_complete(req, ret); | |
4242 | return 0; | |
4243 | } | |
4244 | ||
cf30da90 DK |
4245 | static int io_linkat_prep(struct io_kiocb *req, |
4246 | const struct io_uring_sqe *sqe) | |
4247 | { | |
4248 | struct io_hardlink *lnk = &req->hardlink; | |
4249 | const char __user *oldf, *newf; | |
4250 | ||
4251 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
4252 | return -EINVAL; | |
4253 | if (sqe->ioprio || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in) | |
4254 | return -EINVAL; | |
4255 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
4256 | return -EBADF; | |
4257 | ||
4258 | lnk->old_dfd = READ_ONCE(sqe->fd); | |
4259 | lnk->new_dfd = READ_ONCE(sqe->len); | |
4260 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4261 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
4262 | lnk->flags = READ_ONCE(sqe->hardlink_flags); | |
4263 | ||
4264 | lnk->oldpath = getname(oldf); | |
4265 | if (IS_ERR(lnk->oldpath)) | |
4266 | return PTR_ERR(lnk->oldpath); | |
4267 | ||
4268 | lnk->newpath = getname(newf); | |
4269 | if (IS_ERR(lnk->newpath)) { | |
4270 | putname(lnk->oldpath); | |
4271 | return PTR_ERR(lnk->newpath); | |
4272 | } | |
4273 | ||
4274 | req->flags |= REQ_F_NEED_CLEANUP; | |
4275 | return 0; | |
4276 | } | |
4277 | ||
04f34081 | 4278 | static int io_linkat(struct io_kiocb *req, unsigned int issue_flags) |
cf30da90 DK |
4279 | { |
4280 | struct io_hardlink *lnk = &req->hardlink; | |
4281 | int ret; | |
4282 | ||
4283 | if (issue_flags & IO_URING_F_NONBLOCK) | |
4284 | return -EAGAIN; | |
4285 | ||
4286 | ret = do_linkat(lnk->old_dfd, lnk->oldpath, lnk->new_dfd, | |
4287 | lnk->newpath, lnk->flags); | |
4288 | ||
4289 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4290 | if (ret < 0) | |
4291 | req_set_fail(req); | |
4292 | io_req_complete(req, ret); | |
4293 | return 0; | |
4294 | } | |
4295 | ||
36f4fa68 JA |
4296 | static int io_shutdown_prep(struct io_kiocb *req, |
4297 | const struct io_uring_sqe *sqe) | |
4298 | { | |
4299 | #if defined(CONFIG_NET) | |
4300 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
4301 | return -EINVAL; | |
26578cda PB |
4302 | if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || |
4303 | sqe->buf_index || sqe->splice_fd_in)) | |
36f4fa68 JA |
4304 | return -EINVAL; |
4305 | ||
4306 | req->shutdown.how = READ_ONCE(sqe->len); | |
4307 | return 0; | |
4308 | #else | |
4309 | return -EOPNOTSUPP; | |
4310 | #endif | |
4311 | } | |
4312 | ||
45d189c6 | 4313 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
36f4fa68 JA |
4314 | { |
4315 | #if defined(CONFIG_NET) | |
4316 | struct socket *sock; | |
4317 | int ret; | |
4318 | ||
45d189c6 | 4319 | if (issue_flags & IO_URING_F_NONBLOCK) |
36f4fa68 JA |
4320 | return -EAGAIN; |
4321 | ||
48aba79b | 4322 | sock = sock_from_file(req->file); |
36f4fa68 | 4323 | if (unlikely(!sock)) |
48aba79b | 4324 | return -ENOTSOCK; |
36f4fa68 JA |
4325 | |
4326 | ret = __sys_shutdown_sock(sock, req->shutdown.how); | |
a146468d | 4327 | if (ret < 0) |
93d2bcd2 | 4328 | req_set_fail(req); |
36f4fa68 JA |
4329 | io_req_complete(req, ret); |
4330 | return 0; | |
4331 | #else | |
4332 | return -EOPNOTSUPP; | |
4333 | #endif | |
4334 | } | |
4335 | ||
f2a8d5c7 PB |
4336 | static int __io_splice_prep(struct io_kiocb *req, |
4337 | const struct io_uring_sqe *sqe) | |
7d67af2c | 4338 | { |
fe7e3257 | 4339 | struct io_splice *sp = &req->splice; |
7d67af2c | 4340 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
7d67af2c | 4341 | |
3232dd02 PB |
4342 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4343 | return -EINVAL; | |
7d67af2c | 4344 | |
7d67af2c PB |
4345 | sp->len = READ_ONCE(sqe->len); |
4346 | sp->flags = READ_ONCE(sqe->splice_flags); | |
7d67af2c PB |
4347 | if (unlikely(sp->flags & ~valid_flags)) |
4348 | return -EINVAL; | |
a3e4bc23 | 4349 | sp->splice_fd_in = READ_ONCE(sqe->splice_fd_in); |
7d67af2c PB |
4350 | return 0; |
4351 | } | |
4352 | ||
f2a8d5c7 PB |
4353 | static int io_tee_prep(struct io_kiocb *req, |
4354 | const struct io_uring_sqe *sqe) | |
4355 | { | |
4356 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) | |
4357 | return -EINVAL; | |
4358 | return __io_splice_prep(req, sqe); | |
4359 | } | |
4360 | ||
45d189c6 | 4361 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
f2a8d5c7 PB |
4362 | { |
4363 | struct io_splice *sp = &req->splice; | |
f2a8d5c7 PB |
4364 | struct file *out = sp->file_out; |
4365 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
a3e4bc23 | 4366 | struct file *in; |
f2a8d5c7 PB |
4367 | long ret = 0; |
4368 | ||
45d189c6 | 4369 | if (issue_flags & IO_URING_F_NONBLOCK) |
f2a8d5c7 | 4370 | return -EAGAIN; |
a3e4bc23 | 4371 | |
5106dd6e | 4372 | if (sp->flags & SPLICE_F_FD_IN_FIXED) |
e9419766 | 4373 | in = io_file_get_fixed(req, sp->splice_fd_in, issue_flags); |
5106dd6e JA |
4374 | else |
4375 | in = io_file_get_normal(req, sp->splice_fd_in); | |
a3e4bc23 JA |
4376 | if (!in) { |
4377 | ret = -EBADF; | |
4378 | goto done; | |
4379 | } | |
4380 | ||
f2a8d5c7 PB |
4381 | if (sp->len) |
4382 | ret = do_tee(in, out, sp->len, flags); | |
4383 | ||
e1d767f0 PB |
4384 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
4385 | io_put_file(in); | |
a3e4bc23 | 4386 | done: |
f2a8d5c7 | 4387 | if (ret != sp->len) |
93d2bcd2 | 4388 | req_set_fail(req); |
e1e16097 | 4389 | io_req_complete(req, ret); |
f2a8d5c7 PB |
4390 | return 0; |
4391 | } | |
4392 | ||
4393 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4394 | { | |
fe7e3257 | 4395 | struct io_splice *sp = &req->splice; |
f2a8d5c7 PB |
4396 | |
4397 | sp->off_in = READ_ONCE(sqe->splice_off_in); | |
4398 | sp->off_out = READ_ONCE(sqe->off); | |
4399 | return __io_splice_prep(req, sqe); | |
4400 | } | |
4401 | ||
45d189c6 | 4402 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
7d67af2c PB |
4403 | { |
4404 | struct io_splice *sp = &req->splice; | |
7d67af2c PB |
4405 | struct file *out = sp->file_out; |
4406 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
4407 | loff_t *poff_in, *poff_out; | |
a3e4bc23 | 4408 | struct file *in; |
c9687426 | 4409 | long ret = 0; |
7d67af2c | 4410 | |
45d189c6 | 4411 | if (issue_flags & IO_URING_F_NONBLOCK) |
2fb3e822 | 4412 | return -EAGAIN; |
7d67af2c | 4413 | |
5106dd6e | 4414 | if (sp->flags & SPLICE_F_FD_IN_FIXED) |
e9419766 | 4415 | in = io_file_get_fixed(req, sp->splice_fd_in, issue_flags); |
5106dd6e JA |
4416 | else |
4417 | in = io_file_get_normal(req, sp->splice_fd_in); | |
a3e4bc23 JA |
4418 | if (!in) { |
4419 | ret = -EBADF; | |
4420 | goto done; | |
4421 | } | |
4422 | ||
7d67af2c PB |
4423 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; |
4424 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; | |
c9687426 | 4425 | |
948a7749 | 4426 | if (sp->len) |
c9687426 | 4427 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
7d67af2c | 4428 | |
e1d767f0 PB |
4429 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
4430 | io_put_file(in); | |
a3e4bc23 | 4431 | done: |
7d67af2c | 4432 | if (ret != sp->len) |
93d2bcd2 | 4433 | req_set_fail(req); |
e1e16097 | 4434 | io_req_complete(req, ret); |
7d67af2c PB |
4435 | return 0; |
4436 | } | |
4437 | ||
2b188cc1 JA |
4438 | /* |
4439 | * IORING_OP_NOP just posts a completion event, nothing else. | |
4440 | */ | |
889fca73 | 4441 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
4442 | { |
4443 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 4444 | |
def596e9 JA |
4445 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4446 | return -EINVAL; | |
4447 | ||
889fca73 | 4448 | __io_req_complete(req, issue_flags, 0, 0); |
2b188cc1 JA |
4449 | return 0; |
4450 | } | |
4451 | ||
4f57f06c JA |
4452 | static int io_msg_ring_prep(struct io_kiocb *req, |
4453 | const struct io_uring_sqe *sqe) | |
4454 | { | |
f3b6a41e JA |
4455 | if (unlikely(sqe->addr || sqe->ioprio || sqe->rw_flags || |
4456 | sqe->splice_fd_in || sqe->buf_index || sqe->personality)) | |
4f57f06c JA |
4457 | return -EINVAL; |
4458 | ||
4f57f06c JA |
4459 | req->msg.user_data = READ_ONCE(sqe->off); |
4460 | req->msg.len = READ_ONCE(sqe->len); | |
4461 | return 0; | |
4462 | } | |
4463 | ||
4464 | static int io_msg_ring(struct io_kiocb *req, unsigned int issue_flags) | |
4465 | { | |
4466 | struct io_ring_ctx *target_ctx; | |
4467 | struct io_msg *msg = &req->msg; | |
4f57f06c | 4468 | bool filled; |
3f1d52ab | 4469 | int ret; |
4f57f06c | 4470 | |
3f1d52ab JA |
4471 | ret = -EBADFD; |
4472 | if (req->file->f_op != &io_uring_fops) | |
4473 | goto done; | |
4f57f06c | 4474 | |
3f1d52ab | 4475 | ret = -EOVERFLOW; |
4f57f06c JA |
4476 | target_ctx = req->file->private_data; |
4477 | ||
4478 | spin_lock(&target_ctx->completion_lock); | |
7ef66d18 | 4479 | filled = io_fill_cqe_aux(target_ctx, msg->user_data, msg->len, 0); |
4f57f06c JA |
4480 | io_commit_cqring(target_ctx); |
4481 | spin_unlock(&target_ctx->completion_lock); | |
4482 | ||
4483 | if (filled) { | |
4484 | io_cqring_ev_posted(target_ctx); | |
4485 | ret = 0; | |
4486 | } | |
4487 | ||
3f1d52ab | 4488 | done: |
9666d420 JA |
4489 | if (ret < 0) |
4490 | req_set_fail(req); | |
4f57f06c JA |
4491 | __io_req_complete(req, issue_flags, ret, 0); |
4492 | return 0; | |
4493 | } | |
4494 | ||
1155c76a | 4495 | static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 4496 | { |
6b06314c | 4497 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 4498 | |
6b06314c | 4499 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 4500 | return -EINVAL; |
26578cda PB |
4501 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
4502 | sqe->splice_fd_in)) | |
c992fe29 CH |
4503 | return -EINVAL; |
4504 | ||
8ed8d3c3 JA |
4505 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
4506 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
4507 | return -EINVAL; | |
4508 | ||
4509 | req->sync.off = READ_ONCE(sqe->off); | |
4510 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
4511 | return 0; |
4512 | } | |
4513 | ||
45d189c6 | 4514 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4515 | { |
8ed8d3c3 | 4516 | loff_t end = req->sync.off + req->sync.len; |
8ed8d3c3 JA |
4517 | int ret; |
4518 | ||
ac45abc0 | 4519 | /* fsync always requires a blocking context */ |
45d189c6 | 4520 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4521 | return -EAGAIN; |
4522 | ||
9adbd45d | 4523 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
4524 | end > 0 ? end : LLONG_MAX, |
4525 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
4526 | if (ret < 0) | |
93d2bcd2 | 4527 | req_set_fail(req); |
e1e16097 | 4528 | io_req_complete(req, ret); |
c992fe29 CH |
4529 | return 0; |
4530 | } | |
4531 | ||
d63d1b5e JA |
4532 | static int io_fallocate_prep(struct io_kiocb *req, |
4533 | const struct io_uring_sqe *sqe) | |
4534 | { | |
26578cda PB |
4535 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags || |
4536 | sqe->splice_fd_in) | |
d63d1b5e | 4537 | return -EINVAL; |
3232dd02 PB |
4538 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4539 | return -EINVAL; | |
d63d1b5e JA |
4540 | |
4541 | req->sync.off = READ_ONCE(sqe->off); | |
4542 | req->sync.len = READ_ONCE(sqe->addr); | |
4543 | req->sync.mode = READ_ONCE(sqe->len); | |
4544 | return 0; | |
4545 | } | |
4546 | ||
45d189c6 | 4547 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
5d17b4a4 | 4548 | { |
ac45abc0 PB |
4549 | int ret; |
4550 | ||
d63d1b5e | 4551 | /* fallocate always requiring blocking context */ |
45d189c6 | 4552 | if (issue_flags & IO_URING_F_NONBLOCK) |
5d17b4a4 | 4553 | return -EAGAIN; |
ac45abc0 PB |
4554 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
4555 | req->sync.len); | |
ac45abc0 | 4556 | if (ret < 0) |
93d2bcd2 | 4557 | req_set_fail(req); |
f63cf519 JA |
4558 | else |
4559 | fsnotify_modify(req->file); | |
e1e16097 | 4560 | io_req_complete(req, ret); |
5d17b4a4 JA |
4561 | return 0; |
4562 | } | |
4563 | ||
ec65fea5 | 4564 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 4565 | { |
f8748881 | 4566 | const char __user *fname; |
15b71abe | 4567 | int ret; |
b7bb4f7d | 4568 | |
d3fddf6d PB |
4569 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4570 | return -EINVAL; | |
b9445598 | 4571 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
15b71abe | 4572 | return -EINVAL; |
ec65fea5 | 4573 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
cf3040ca | 4574 | return -EBADF; |
03b1230c | 4575 | |
ec65fea5 PB |
4576 | /* open.how should be already initialised */ |
4577 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
08a1d26e | 4578 | req->open.how.flags |= O_LARGEFILE; |
3529d8c2 | 4579 | |
25e72d10 PB |
4580 | req->open.dfd = READ_ONCE(sqe->fd); |
4581 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
f8748881 | 4582 | req->open.filename = getname(fname); |
15b71abe JA |
4583 | if (IS_ERR(req->open.filename)) { |
4584 | ret = PTR_ERR(req->open.filename); | |
4585 | req->open.filename = NULL; | |
4586 | return ret; | |
4587 | } | |
b9445598 PB |
4588 | |
4589 | req->open.file_slot = READ_ONCE(sqe->file_index); | |
4590 | if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC)) | |
4591 | return -EINVAL; | |
4592 | ||
4022e7af | 4593 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
8fef80bf | 4594 | req->flags |= REQ_F_NEED_CLEANUP; |
15b71abe | 4595 | return 0; |
03b1230c JA |
4596 | } |
4597 | ||
ec65fea5 PB |
4598 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4599 | { | |
d3fddf6d PB |
4600 | u64 mode = READ_ONCE(sqe->len); |
4601 | u64 flags = READ_ONCE(sqe->open_flags); | |
ec65fea5 | 4602 | |
ec65fea5 PB |
4603 | req->open.how = build_open_how(flags, mode); |
4604 | return __io_openat_prep(req, sqe); | |
4605 | } | |
4606 | ||
cebdb986 | 4607 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 4608 | { |
cebdb986 | 4609 | struct open_how __user *how; |
cebdb986 | 4610 | size_t len; |
0fa03c62 JA |
4611 | int ret; |
4612 | ||
cebdb986 JA |
4613 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4614 | len = READ_ONCE(sqe->len); | |
cebdb986 JA |
4615 | if (len < OPEN_HOW_SIZE_VER0) |
4616 | return -EINVAL; | |
3529d8c2 | 4617 | |
cebdb986 JA |
4618 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
4619 | len); | |
4620 | if (ret) | |
4621 | return ret; | |
3529d8c2 | 4622 | |
ec65fea5 | 4623 | return __io_openat_prep(req, sqe); |
cebdb986 JA |
4624 | } |
4625 | ||
45d189c6 | 4626 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
15b71abe JA |
4627 | { |
4628 | struct open_flags op; | |
15b71abe | 4629 | struct file *file; |
b9445598 PB |
4630 | bool resolve_nonblock, nonblock_set; |
4631 | bool fixed = !!req->open.file_slot; | |
15b71abe JA |
4632 | int ret; |
4633 | ||
cebdb986 | 4634 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
4635 | if (ret) |
4636 | goto err; | |
3a81fd02 JA |
4637 | nonblock_set = op.open_flag & O_NONBLOCK; |
4638 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; | |
45d189c6 | 4639 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3a81fd02 JA |
4640 | /* |
4641 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, | |
4642 | * it'll always -EAGAIN | |
4643 | */ | |
4644 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) | |
4645 | return -EAGAIN; | |
4646 | op.lookup_flags |= LOOKUP_CACHED; | |
4647 | op.open_flag |= O_NONBLOCK; | |
4648 | } | |
15b71abe | 4649 | |
b9445598 PB |
4650 | if (!fixed) { |
4651 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); | |
4652 | if (ret < 0) | |
4653 | goto err; | |
4654 | } | |
15b71abe JA |
4655 | |
4656 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
12dcb58a | 4657 | if (IS_ERR(file)) { |
944d1444 | 4658 | /* |
12dcb58a PB |
4659 | * We could hang on to this 'fd' on retrying, but seems like |
4660 | * marginal gain for something that is now known to be a slower | |
4661 | * path. So just put it, and we'll get a new one when we retry. | |
944d1444 | 4662 | */ |
b9445598 PB |
4663 | if (!fixed) |
4664 | put_unused_fd(ret); | |
3a81fd02 | 4665 | |
15b71abe | 4666 | ret = PTR_ERR(file); |
12dcb58a PB |
4667 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
4668 | if (ret == -EAGAIN && | |
4669 | (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK))) | |
4670 | return -EAGAIN; | |
4671 | goto err; | |
15b71abe | 4672 | } |
12dcb58a PB |
4673 | |
4674 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) | |
4675 | file->f_flags &= ~O_NONBLOCK; | |
4676 | fsnotify_open(file); | |
b9445598 PB |
4677 | |
4678 | if (!fixed) | |
4679 | fd_install(ret, file); | |
4680 | else | |
4681 | ret = io_install_fixed_file(req, file, issue_flags, | |
4682 | req->open.file_slot - 1); | |
15b71abe JA |
4683 | err: |
4684 | putname(req->open.filename); | |
8fef80bf | 4685 | req->flags &= ~REQ_F_NEED_CLEANUP; |
15b71abe | 4686 | if (ret < 0) |
93d2bcd2 | 4687 | req_set_fail(req); |
0bdf3398 | 4688 | __io_req_complete(req, issue_flags, ret, 0); |
15b71abe JA |
4689 | return 0; |
4690 | } | |
4691 | ||
45d189c6 | 4692 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
cebdb986 | 4693 | { |
e45cff58 | 4694 | return io_openat2(req, issue_flags); |
cebdb986 JA |
4695 | } |
4696 | ||
067524e9 JA |
4697 | static int io_remove_buffers_prep(struct io_kiocb *req, |
4698 | const struct io_uring_sqe *sqe) | |
4699 | { | |
4700 | struct io_provide_buf *p = &req->pbuf; | |
4701 | u64 tmp; | |
4702 | ||
26578cda PB |
4703 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off || |
4704 | sqe->splice_fd_in) | |
067524e9 JA |
4705 | return -EINVAL; |
4706 | ||
4707 | tmp = READ_ONCE(sqe->fd); | |
4708 | if (!tmp || tmp > USHRT_MAX) | |
4709 | return -EINVAL; | |
4710 | ||
4711 | memset(p, 0, sizeof(*p)); | |
4712 | p->nbufs = tmp; | |
4713 | p->bgid = READ_ONCE(sqe->buf_group); | |
4714 | return 0; | |
4715 | } | |
4716 | ||
dbc7d452 JA |
4717 | static int __io_remove_buffers(struct io_ring_ctx *ctx, |
4718 | struct io_buffer_list *bl, unsigned nbufs) | |
067524e9 JA |
4719 | { |
4720 | unsigned i = 0; | |
4721 | ||
4722 | /* shouldn't happen */ | |
4723 | if (!nbufs) | |
4724 | return 0; | |
4725 | ||
4726 | /* the head kbuf is the list itself */ | |
dbc7d452 | 4727 | while (!list_empty(&bl->buf_list)) { |
067524e9 JA |
4728 | struct io_buffer *nxt; |
4729 | ||
dbc7d452 | 4730 | nxt = list_first_entry(&bl->buf_list, struct io_buffer, list); |
067524e9 | 4731 | list_del(&nxt->list); |
067524e9 JA |
4732 | if (++i == nbufs) |
4733 | return i; | |
1d0254e6 | 4734 | cond_resched(); |
067524e9 JA |
4735 | } |
4736 | i++; | |
067524e9 JA |
4737 | |
4738 | return i; | |
4739 | } | |
4740 | ||
889fca73 | 4741 | static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags) |
067524e9 JA |
4742 | { |
4743 | struct io_provide_buf *p = &req->pbuf; | |
4744 | struct io_ring_ctx *ctx = req->ctx; | |
dbc7d452 | 4745 | struct io_buffer_list *bl; |
067524e9 JA |
4746 | int ret = 0; |
4747 | ||
f8929630 | 4748 | io_ring_submit_lock(ctx, issue_flags); |
067524e9 JA |
4749 | |
4750 | ret = -ENOENT; | |
dbc7d452 JA |
4751 | bl = io_buffer_get_list(ctx, p->bgid); |
4752 | if (bl) | |
4753 | ret = __io_remove_buffers(ctx, bl, p->nbufs); | |
067524e9 | 4754 | if (ret < 0) |
93d2bcd2 | 4755 | req_set_fail(req); |
067524e9 | 4756 | |
9fb8cb49 PB |
4757 | /* complete before unlock, IOPOLL may need the lock */ |
4758 | __io_req_complete(req, issue_flags, ret, 0); | |
f8929630 | 4759 | io_ring_submit_unlock(ctx, issue_flags); |
067524e9 JA |
4760 | return 0; |
4761 | } | |
4762 | ||
ddf0322d JA |
4763 | static int io_provide_buffers_prep(struct io_kiocb *req, |
4764 | const struct io_uring_sqe *sqe) | |
4765 | { | |
38134ada | 4766 | unsigned long size, tmp_check; |
ddf0322d JA |
4767 | struct io_provide_buf *p = &req->pbuf; |
4768 | u64 tmp; | |
4769 | ||
26578cda | 4770 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
ddf0322d JA |
4771 | return -EINVAL; |
4772 | ||
4773 | tmp = READ_ONCE(sqe->fd); | |
4774 | if (!tmp || tmp > USHRT_MAX) | |
4775 | return -E2BIG; | |
4776 | p->nbufs = tmp; | |
4777 | p->addr = READ_ONCE(sqe->addr); | |
4778 | p->len = READ_ONCE(sqe->len); | |
4779 | ||
38134ada PB |
4780 | if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs, |
4781 | &size)) | |
4782 | return -EOVERFLOW; | |
4783 | if (check_add_overflow((unsigned long)p->addr, size, &tmp_check)) | |
4784 | return -EOVERFLOW; | |
4785 | ||
d81269fe PB |
4786 | size = (unsigned long)p->len * p->nbufs; |
4787 | if (!access_ok(u64_to_user_ptr(p->addr), size)) | |
ddf0322d JA |
4788 | return -EFAULT; |
4789 | ||
4790 | p->bgid = READ_ONCE(sqe->buf_group); | |
4791 | tmp = READ_ONCE(sqe->off); | |
4792 | if (tmp > USHRT_MAX) | |
4793 | return -E2BIG; | |
4794 | p->bid = tmp; | |
4795 | return 0; | |
4796 | } | |
4797 | ||
cc3cec83 JA |
4798 | static int io_refill_buffer_cache(struct io_ring_ctx *ctx) |
4799 | { | |
4800 | struct io_buffer *buf; | |
4801 | struct page *page; | |
4802 | int bufs_in_page; | |
4803 | ||
4804 | /* | |
4805 | * Completions that don't happen inline (eg not under uring_lock) will | |
4806 | * add to ->io_buffers_comp. If we don't have any free buffers, check | |
4807 | * the completion list and splice those entries first. | |
4808 | */ | |
4809 | if (!list_empty_careful(&ctx->io_buffers_comp)) { | |
4810 | spin_lock(&ctx->completion_lock); | |
4811 | if (!list_empty(&ctx->io_buffers_comp)) { | |
4812 | list_splice_init(&ctx->io_buffers_comp, | |
4813 | &ctx->io_buffers_cache); | |
4814 | spin_unlock(&ctx->completion_lock); | |
4815 | return 0; | |
4816 | } | |
4817 | spin_unlock(&ctx->completion_lock); | |
4818 | } | |
4819 | ||
4820 | /* | |
4821 | * No free buffers and no completion entries either. Allocate a new | |
4822 | * page worth of buffer entries and add those to our freelist. | |
4823 | */ | |
4824 | page = alloc_page(GFP_KERNEL_ACCOUNT); | |
4825 | if (!page) | |
4826 | return -ENOMEM; | |
4827 | ||
4828 | list_add(&page->lru, &ctx->io_buffers_pages); | |
4829 | ||
4830 | buf = page_address(page); | |
4831 | bufs_in_page = PAGE_SIZE / sizeof(*buf); | |
4832 | while (bufs_in_page) { | |
4833 | list_add_tail(&buf->list, &ctx->io_buffers_cache); | |
4834 | buf++; | |
4835 | bufs_in_page--; | |
4836 | } | |
4837 | ||
4838 | return 0; | |
4839 | } | |
4840 | ||
4841 | static int io_add_buffers(struct io_ring_ctx *ctx, struct io_provide_buf *pbuf, | |
dbc7d452 | 4842 | struct io_buffer_list *bl) |
ddf0322d JA |
4843 | { |
4844 | struct io_buffer *buf; | |
4845 | u64 addr = pbuf->addr; | |
4846 | int i, bid = pbuf->bid; | |
4847 | ||
4848 | for (i = 0; i < pbuf->nbufs; i++) { | |
cc3cec83 JA |
4849 | if (list_empty(&ctx->io_buffers_cache) && |
4850 | io_refill_buffer_cache(ctx)) | |
ddf0322d | 4851 | break; |
cc3cec83 JA |
4852 | buf = list_first_entry(&ctx->io_buffers_cache, struct io_buffer, |
4853 | list); | |
dbc7d452 | 4854 | list_move_tail(&buf->list, &bl->buf_list); |
ddf0322d | 4855 | buf->addr = addr; |
d1f82808 | 4856 | buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT); |
ddf0322d | 4857 | buf->bid = bid; |
b1c62645 | 4858 | buf->bgid = pbuf->bgid; |
ddf0322d JA |
4859 | addr += pbuf->len; |
4860 | bid++; | |
f240762f | 4861 | cond_resched(); |
ddf0322d JA |
4862 | } |
4863 | ||
dbc7d452 | 4864 | return i ? 0 : -ENOMEM; |
ddf0322d JA |
4865 | } |
4866 | ||
889fca73 | 4867 | static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags) |
ddf0322d JA |
4868 | { |
4869 | struct io_provide_buf *p = &req->pbuf; | |
4870 | struct io_ring_ctx *ctx = req->ctx; | |
dbc7d452 | 4871 | struct io_buffer_list *bl; |
ddf0322d | 4872 | int ret = 0; |
ddf0322d | 4873 | |
f8929630 | 4874 | io_ring_submit_lock(ctx, issue_flags); |
ddf0322d | 4875 | |
dbc7d452 JA |
4876 | bl = io_buffer_get_list(ctx, p->bgid); |
4877 | if (unlikely(!bl)) { | |
4878 | bl = kmalloc(sizeof(*bl), GFP_KERNEL); | |
4879 | if (!bl) { | |
4880 | ret = -ENOMEM; | |
4881 | goto err; | |
4882 | } | |
4883 | io_buffer_add_list(ctx, bl, p->bgid); | |
ddf0322d | 4884 | } |
dbc7d452 JA |
4885 | |
4886 | ret = io_add_buffers(ctx, p, bl); | |
4887 | err: | |
ddf0322d | 4888 | if (ret < 0) |
93d2bcd2 | 4889 | req_set_fail(req); |
9fb8cb49 PB |
4890 | /* complete before unlock, IOPOLL may need the lock */ |
4891 | __io_req_complete(req, issue_flags, ret, 0); | |
f8929630 | 4892 | io_ring_submit_unlock(ctx, issue_flags); |
ddf0322d | 4893 | return 0; |
cebdb986 JA |
4894 | } |
4895 | ||
3e4827b0 JA |
4896 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
4897 | const struct io_uring_sqe *sqe) | |
4898 | { | |
4899 | #if defined(CONFIG_EPOLL) | |
26578cda | 4900 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
3e4827b0 | 4901 | return -EINVAL; |
2d74d042 | 4902 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4903 | return -EINVAL; |
3e4827b0 JA |
4904 | |
4905 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
4906 | req->epoll.op = READ_ONCE(sqe->len); | |
4907 | req->epoll.fd = READ_ONCE(sqe->off); | |
4908 | ||
4909 | if (ep_op_has_event(req->epoll.op)) { | |
4910 | struct epoll_event __user *ev; | |
4911 | ||
4912 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4913 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
4914 | return -EFAULT; | |
4915 | } | |
4916 | ||
4917 | return 0; | |
4918 | #else | |
4919 | return -EOPNOTSUPP; | |
4920 | #endif | |
4921 | } | |
4922 | ||
889fca73 | 4923 | static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags) |
3e4827b0 JA |
4924 | { |
4925 | #if defined(CONFIG_EPOLL) | |
4926 | struct io_epoll *ie = &req->epoll; | |
4927 | int ret; | |
45d189c6 | 4928 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
3e4827b0 JA |
4929 | |
4930 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
4931 | if (force_nonblock && ret == -EAGAIN) | |
4932 | return -EAGAIN; | |
4933 | ||
4934 | if (ret < 0) | |
93d2bcd2 | 4935 | req_set_fail(req); |
889fca73 | 4936 | __io_req_complete(req, issue_flags, ret, 0); |
3e4827b0 JA |
4937 | return 0; |
4938 | #else | |
4939 | return -EOPNOTSUPP; | |
4940 | #endif | |
4941 | } | |
4942 | ||
c1ca757b JA |
4943 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4944 | { | |
4945 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
26578cda | 4946 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in) |
c1ca757b | 4947 | return -EINVAL; |
3232dd02 PB |
4948 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4949 | return -EINVAL; | |
c1ca757b JA |
4950 | |
4951 | req->madvise.addr = READ_ONCE(sqe->addr); | |
4952 | req->madvise.len = READ_ONCE(sqe->len); | |
4953 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4954 | return 0; | |
4955 | #else | |
4956 | return -EOPNOTSUPP; | |
4957 | #endif | |
4958 | } | |
4959 | ||
45d189c6 | 4960 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
c1ca757b JA |
4961 | { |
4962 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4963 | struct io_madvise *ma = &req->madvise; | |
4964 | int ret; | |
4965 | ||
45d189c6 | 4966 | if (issue_flags & IO_URING_F_NONBLOCK) |
c1ca757b JA |
4967 | return -EAGAIN; |
4968 | ||
0726b01e | 4969 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
c1ca757b | 4970 | if (ret < 0) |
93d2bcd2 | 4971 | req_set_fail(req); |
e1e16097 | 4972 | io_req_complete(req, ret); |
c1ca757b JA |
4973 | return 0; |
4974 | #else | |
4975 | return -EOPNOTSUPP; | |
4976 | #endif | |
4977 | } | |
4978 | ||
4840e418 JA |
4979 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4980 | { | |
26578cda | 4981 | if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in) |
4840e418 | 4982 | return -EINVAL; |
3232dd02 PB |
4983 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4984 | return -EINVAL; | |
4840e418 JA |
4985 | |
4986 | req->fadvise.offset = READ_ONCE(sqe->off); | |
4987 | req->fadvise.len = READ_ONCE(sqe->len); | |
4988 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4989 | return 0; | |
4990 | } | |
4991 | ||
45d189c6 | 4992 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
4840e418 JA |
4993 | { |
4994 | struct io_fadvise *fa = &req->fadvise; | |
4995 | int ret; | |
4996 | ||
45d189c6 | 4997 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3e69426d JA |
4998 | switch (fa->advice) { |
4999 | case POSIX_FADV_NORMAL: | |
5000 | case POSIX_FADV_RANDOM: | |
5001 | case POSIX_FADV_SEQUENTIAL: | |
5002 | break; | |
5003 | default: | |
5004 | return -EAGAIN; | |
5005 | } | |
5006 | } | |
4840e418 JA |
5007 | |
5008 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
5009 | if (ret < 0) | |
93d2bcd2 | 5010 | req_set_fail(req); |
0bdf3398 | 5011 | __io_req_complete(req, issue_flags, ret, 0); |
4840e418 JA |
5012 | return 0; |
5013 | } | |
5014 | ||
eddc7ef5 JA |
5015 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5016 | { | |
1b6fe6e0 SR |
5017 | const char __user *path; |
5018 | ||
2d74d042 | 5019 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 5020 | return -EINVAL; |
26578cda | 5021 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
eddc7ef5 | 5022 | return -EINVAL; |
9c280f90 | 5023 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 5024 | return -EBADF; |
eddc7ef5 | 5025 | |
1d9e1288 BM |
5026 | req->statx.dfd = READ_ONCE(sqe->fd); |
5027 | req->statx.mask = READ_ONCE(sqe->len); | |
1b6fe6e0 | 5028 | path = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
1d9e1288 BM |
5029 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
5030 | req->statx.flags = READ_ONCE(sqe->statx_flags); | |
eddc7ef5 | 5031 | |
1b6fe6e0 SR |
5032 | req->statx.filename = getname_flags(path, |
5033 | getname_statx_lookup_flags(req->statx.flags), | |
5034 | NULL); | |
5035 | ||
5036 | if (IS_ERR(req->statx.filename)) { | |
5037 | int ret = PTR_ERR(req->statx.filename); | |
5038 | ||
5039 | req->statx.filename = NULL; | |
5040 | return ret; | |
5041 | } | |
5042 | ||
5043 | req->flags |= REQ_F_NEED_CLEANUP; | |
eddc7ef5 JA |
5044 | return 0; |
5045 | } | |
5046 | ||
45d189c6 | 5047 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
eddc7ef5 | 5048 | { |
1d9e1288 | 5049 | struct io_statx *ctx = &req->statx; |
eddc7ef5 JA |
5050 | int ret; |
5051 | ||
59d70013 | 5052 | if (issue_flags & IO_URING_F_NONBLOCK) |
eddc7ef5 JA |
5053 | return -EAGAIN; |
5054 | ||
e62753e4 BM |
5055 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
5056 | ctx->buffer); | |
eddc7ef5 | 5057 | |
eddc7ef5 | 5058 | if (ret < 0) |
93d2bcd2 | 5059 | req_set_fail(req); |
e1e16097 | 5060 | io_req_complete(req, ret); |
eddc7ef5 JA |
5061 | return 0; |
5062 | } | |
5063 | ||
b5dba59e JA |
5064 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5065 | { | |
14587a46 | 5066 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 5067 | return -EINVAL; |
b5dba59e | 5068 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
7df778be | 5069 | sqe->rw_flags || sqe->buf_index) |
b5dba59e | 5070 | return -EINVAL; |
9c280f90 | 5071 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 5072 | return -EBADF; |
b5dba59e JA |
5073 | |
5074 | req->close.fd = READ_ONCE(sqe->fd); | |
7df778be PB |
5075 | req->close.file_slot = READ_ONCE(sqe->file_index); |
5076 | if (req->close.file_slot && req->close.fd) | |
5077 | return -EINVAL; | |
5078 | ||
b5dba59e | 5079 | return 0; |
b5dba59e JA |
5080 | } |
5081 | ||
889fca73 | 5082 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
b5dba59e | 5083 | { |
9eac1904 | 5084 | struct files_struct *files = current->files; |
3af73b28 | 5085 | struct io_close *close = &req->close; |
9eac1904 | 5086 | struct fdtable *fdt; |
a1fde923 PB |
5087 | struct file *file = NULL; |
5088 | int ret = -EBADF; | |
b5dba59e | 5089 | |
7df778be PB |
5090 | if (req->close.file_slot) { |
5091 | ret = io_close_fixed(req, issue_flags); | |
5092 | goto err; | |
5093 | } | |
5094 | ||
9eac1904 JA |
5095 | spin_lock(&files->file_lock); |
5096 | fdt = files_fdtable(files); | |
5097 | if (close->fd >= fdt->max_fds) { | |
5098 | spin_unlock(&files->file_lock); | |
5099 | goto err; | |
5100 | } | |
5101 | file = fdt->fd[close->fd]; | |
a1fde923 | 5102 | if (!file || file->f_op == &io_uring_fops) { |
9eac1904 JA |
5103 | spin_unlock(&files->file_lock); |
5104 | file = NULL; | |
5105 | goto err; | |
3af73b28 | 5106 | } |
b5dba59e JA |
5107 | |
5108 | /* if the file has a flush method, be safe and punt to async */ | |
45d189c6 | 5109 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
9eac1904 | 5110 | spin_unlock(&files->file_lock); |
0bf0eefd | 5111 | return -EAGAIN; |
a2100672 | 5112 | } |
b5dba59e | 5113 | |
9eac1904 JA |
5114 | ret = __close_fd_get_file(close->fd, &file); |
5115 | spin_unlock(&files->file_lock); | |
5116 | if (ret < 0) { | |
5117 | if (ret == -ENOENT) | |
5118 | ret = -EBADF; | |
5119 | goto err; | |
5120 | } | |
5121 | ||
3af73b28 | 5122 | /* No ->flush() or already async, safely close from here */ |
9eac1904 JA |
5123 | ret = filp_close(file, current->files); |
5124 | err: | |
3af73b28 | 5125 | if (ret < 0) |
93d2bcd2 | 5126 | req_set_fail(req); |
9eac1904 JA |
5127 | if (file) |
5128 | fput(file); | |
889fca73 | 5129 | __io_req_complete(req, issue_flags, ret, 0); |
1a417f4e | 5130 | return 0; |
b5dba59e JA |
5131 | } |
5132 | ||
1155c76a | 5133 | static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
5134 | { |
5135 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 | 5136 | |
5d17b4a4 JA |
5137 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
5138 | return -EINVAL; | |
26578cda PB |
5139 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
5140 | sqe->splice_fd_in)) | |
5d17b4a4 JA |
5141 | return -EINVAL; |
5142 | ||
8ed8d3c3 JA |
5143 | req->sync.off = READ_ONCE(sqe->off); |
5144 | req->sync.len = READ_ONCE(sqe->len); | |
5145 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
5146 | return 0; |
5147 | } | |
5148 | ||
45d189c6 | 5149 | static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 5150 | { |
8ed8d3c3 JA |
5151 | int ret; |
5152 | ||
ac45abc0 | 5153 | /* sync_file_range always requires a blocking context */ |
45d189c6 | 5154 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
5155 | return -EAGAIN; |
5156 | ||
9adbd45d | 5157 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
5158 | req->sync.flags); |
5159 | if (ret < 0) | |
93d2bcd2 | 5160 | req_set_fail(req); |
e1e16097 | 5161 | io_req_complete(req, ret); |
5d17b4a4 JA |
5162 | return 0; |
5163 | } | |
5164 | ||
469956e8 | 5165 | #if defined(CONFIG_NET) |
02d27d89 PB |
5166 | static int io_setup_async_msg(struct io_kiocb *req, |
5167 | struct io_async_msghdr *kmsg) | |
5168 | { | |
e8c2bc1f JA |
5169 | struct io_async_msghdr *async_msg = req->async_data; |
5170 | ||
5171 | if (async_msg) | |
02d27d89 | 5172 | return -EAGAIN; |
e8c2bc1f | 5173 | if (io_alloc_async_data(req)) { |
257e84a5 | 5174 | kfree(kmsg->free_iov); |
02d27d89 PB |
5175 | return -ENOMEM; |
5176 | } | |
e8c2bc1f | 5177 | async_msg = req->async_data; |
02d27d89 | 5178 | req->flags |= REQ_F_NEED_CLEANUP; |
e8c2bc1f | 5179 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
2a780802 | 5180 | async_msg->msg.msg_name = &async_msg->addr; |
257e84a5 PB |
5181 | /* if were using fast_iov, set it to the new one */ |
5182 | if (!async_msg->free_iov) | |
5183 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; | |
5184 | ||
02d27d89 PB |
5185 | return -EAGAIN; |
5186 | } | |
5187 | ||
2ae523ed PB |
5188 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
5189 | struct io_async_msghdr *iomsg) | |
5190 | { | |
2ae523ed | 5191 | iomsg->msg.msg_name = &iomsg->addr; |
257e84a5 | 5192 | iomsg->free_iov = iomsg->fast_iov; |
2ae523ed | 5193 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
257e84a5 | 5194 | req->sr_msg.msg_flags, &iomsg->free_iov); |
2ae523ed PB |
5195 | } |
5196 | ||
93642ef8 PB |
5197 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
5198 | { | |
5199 | int ret; | |
5200 | ||
93642ef8 PB |
5201 | ret = io_sendmsg_copy_hdr(req, req->async_data); |
5202 | if (!ret) | |
5203 | req->flags |= REQ_F_NEED_CLEANUP; | |
5204 | return ret; | |
5205 | } | |
5206 | ||
3529d8c2 | 5207 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 5208 | { |
e47293fd | 5209 | struct io_sr_msg *sr = &req->sr_msg; |
03b1230c | 5210 | |
d2b6f48b PB |
5211 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5212 | return -EINVAL; | |
5213 | ||
270a5940 | 5214 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
fddaface | 5215 | sr->len = READ_ONCE(sqe->len); |
04411806 PB |
5216 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
5217 | if (sr->msg_flags & MSG_DONTWAIT) | |
5218 | req->flags |= REQ_F_NOWAIT; | |
3529d8c2 | 5219 | |
d8768362 JA |
5220 | #ifdef CONFIG_COMPAT |
5221 | if (req->ctx->compat) | |
5222 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
5223 | #endif | |
93642ef8 | 5224 | return 0; |
03b1230c JA |
5225 | } |
5226 | ||
889fca73 | 5227 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 5228 | { |
6b754c8b | 5229 | struct io_async_msghdr iomsg, *kmsg; |
0fa03c62 | 5230 | struct socket *sock; |
7a7cacba | 5231 | unsigned flags; |
0031275d | 5232 | int min_ret = 0; |
0fa03c62 JA |
5233 | int ret; |
5234 | ||
dba4a925 | 5235 | sock = sock_from_file(req->file); |
7a7cacba | 5236 | if (unlikely(!sock)) |
dba4a925 | 5237 | return -ENOTSOCK; |
3529d8c2 | 5238 | |
d886e185 PB |
5239 | if (req_has_async_data(req)) { |
5240 | kmsg = req->async_data; | |
5241 | } else { | |
7a7cacba PB |
5242 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
5243 | if (ret) | |
5244 | return ret; | |
5245 | kmsg = &iomsg; | |
0fa03c62 | 5246 | } |
0fa03c62 | 5247 | |
04411806 PB |
5248 | flags = req->sr_msg.msg_flags; |
5249 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 5250 | flags |= MSG_DONTWAIT; |
0031275d SM |
5251 | if (flags & MSG_WAITALL) |
5252 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
5253 | ||
7a7cacba | 5254 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
0fa03c62 | 5255 | |
7297ce3d PB |
5256 | if (ret < min_ret) { |
5257 | if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK)) | |
5258 | return io_setup_async_msg(req, kmsg); | |
5259 | if (ret == -ERESTARTSYS) | |
5260 | ret = -EINTR; | |
5261 | req_set_fail(req); | |
5262 | } | |
257e84a5 PB |
5263 | /* fast path, check for non-NULL to avoid function call */ |
5264 | if (kmsg->free_iov) | |
5265 | kfree(kmsg->free_iov); | |
99bc4c38 | 5266 | req->flags &= ~REQ_F_NEED_CLEANUP; |
889fca73 | 5267 | __io_req_complete(req, issue_flags, ret, 0); |
5d17b4a4 | 5268 | return 0; |
03b1230c | 5269 | } |
aa1fa28f | 5270 | |
889fca73 | 5271 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 5272 | { |
7a7cacba PB |
5273 | struct io_sr_msg *sr = &req->sr_msg; |
5274 | struct msghdr msg; | |
5275 | struct iovec iov; | |
fddaface | 5276 | struct socket *sock; |
7a7cacba | 5277 | unsigned flags; |
0031275d | 5278 | int min_ret = 0; |
fddaface JA |
5279 | int ret; |
5280 | ||
dba4a925 | 5281 | sock = sock_from_file(req->file); |
7a7cacba | 5282 | if (unlikely(!sock)) |
dba4a925 | 5283 | return -ENOTSOCK; |
fddaface | 5284 | |
7a7cacba PB |
5285 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
5286 | if (unlikely(ret)) | |
14db8411 | 5287 | return ret; |
fddaface | 5288 | |
7a7cacba PB |
5289 | msg.msg_name = NULL; |
5290 | msg.msg_control = NULL; | |
5291 | msg.msg_controllen = 0; | |
5292 | msg.msg_namelen = 0; | |
fddaface | 5293 | |
04411806 PB |
5294 | flags = req->sr_msg.msg_flags; |
5295 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 5296 | flags |= MSG_DONTWAIT; |
0031275d SM |
5297 | if (flags & MSG_WAITALL) |
5298 | min_ret = iov_iter_count(&msg.msg_iter); | |
5299 | ||
7a7cacba PB |
5300 | msg.msg_flags = flags; |
5301 | ret = sock_sendmsg(sock, &msg); | |
7297ce3d PB |
5302 | if (ret < min_ret) { |
5303 | if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK)) | |
5304 | return -EAGAIN; | |
5305 | if (ret == -ERESTARTSYS) | |
5306 | ret = -EINTR; | |
93d2bcd2 | 5307 | req_set_fail(req); |
7297ce3d | 5308 | } |
889fca73 | 5309 | __io_req_complete(req, issue_flags, ret, 0); |
fddaface | 5310 | return 0; |
fddaface JA |
5311 | } |
5312 | ||
1400e697 PB |
5313 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
5314 | struct io_async_msghdr *iomsg) | |
52de1fe1 JA |
5315 | { |
5316 | struct io_sr_msg *sr = &req->sr_msg; | |
5317 | struct iovec __user *uiov; | |
5318 | size_t iov_len; | |
5319 | int ret; | |
5320 | ||
1400e697 PB |
5321 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
5322 | &iomsg->uaddr, &uiov, &iov_len); | |
52de1fe1 JA |
5323 | if (ret) |
5324 | return ret; | |
5325 | ||
5326 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
5327 | if (iov_len > 1) | |
5328 | return -EINVAL; | |
5476dfed | 5329 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
52de1fe1 | 5330 | return -EFAULT; |
5476dfed | 5331 | sr->len = iomsg->fast_iov[0].iov_len; |
257e84a5 | 5332 | iomsg->free_iov = NULL; |
52de1fe1 | 5333 | } else { |
257e84a5 | 5334 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 5335 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
257e84a5 | 5336 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
89cd35c5 | 5337 | false); |
52de1fe1 JA |
5338 | if (ret > 0) |
5339 | ret = 0; | |
5340 | } | |
5341 | ||
5342 | return ret; | |
5343 | } | |
5344 | ||
5345 | #ifdef CONFIG_COMPAT | |
5346 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, | |
1400e697 | 5347 | struct io_async_msghdr *iomsg) |
52de1fe1 | 5348 | { |
52de1fe1 JA |
5349 | struct io_sr_msg *sr = &req->sr_msg; |
5350 | struct compat_iovec __user *uiov; | |
5351 | compat_uptr_t ptr; | |
5352 | compat_size_t len; | |
5353 | int ret; | |
5354 | ||
4af3417a PB |
5355 | ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr, |
5356 | &ptr, &len); | |
52de1fe1 JA |
5357 | if (ret) |
5358 | return ret; | |
5359 | ||
5360 | uiov = compat_ptr(ptr); | |
5361 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
5362 | compat_ssize_t clen; | |
5363 | ||
5364 | if (len > 1) | |
5365 | return -EINVAL; | |
5366 | if (!access_ok(uiov, sizeof(*uiov))) | |
5367 | return -EFAULT; | |
5368 | if (__get_user(clen, &uiov->iov_len)) | |
5369 | return -EFAULT; | |
5370 | if (clen < 0) | |
5371 | return -EINVAL; | |
2d280bc8 | 5372 | sr->len = clen; |
257e84a5 | 5373 | iomsg->free_iov = NULL; |
52de1fe1 | 5374 | } else { |
257e84a5 | 5375 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 5376 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
257e84a5 | 5377 | UIO_FASTIOV, &iomsg->free_iov, |
89cd35c5 | 5378 | &iomsg->msg.msg_iter, true); |
52de1fe1 JA |
5379 | if (ret < 0) |
5380 | return ret; | |
5381 | } | |
5382 | ||
5383 | return 0; | |
5384 | } | |
5385 | #endif | |
5386 | ||
1400e697 PB |
5387 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
5388 | struct io_async_msghdr *iomsg) | |
52de1fe1 | 5389 | { |
1400e697 | 5390 | iomsg->msg.msg_name = &iomsg->addr; |
52de1fe1 JA |
5391 | |
5392 | #ifdef CONFIG_COMPAT | |
5393 | if (req->ctx->compat) | |
1400e697 | 5394 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
fddaface | 5395 | #endif |
52de1fe1 | 5396 | |
1400e697 | 5397 | return __io_recvmsg_copy_hdr(req, iomsg); |
52de1fe1 JA |
5398 | } |
5399 | ||
bcda7baa | 5400 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
51aac424 | 5401 | unsigned int issue_flags) |
bcda7baa JA |
5402 | { |
5403 | struct io_sr_msg *sr = &req->sr_msg; | |
bcda7baa | 5404 | |
51aac424 | 5405 | return io_buffer_select(req, &sr->len, sr->bgid, issue_flags); |
fddaface JA |
5406 | } |
5407 | ||
93642ef8 | 5408 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
aa1fa28f | 5409 | { |
99bc4c38 | 5410 | int ret; |
3529d8c2 | 5411 | |
93642ef8 PB |
5412 | ret = io_recvmsg_copy_hdr(req, req->async_data); |
5413 | if (!ret) | |
5414 | req->flags |= REQ_F_NEED_CLEANUP; | |
5415 | return ret; | |
5416 | } | |
5417 | ||
5418 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
5419 | { | |
5420 | struct io_sr_msg *sr = &req->sr_msg; | |
5421 | ||
d2b6f48b PB |
5422 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5423 | return -EINVAL; | |
5424 | ||
270a5940 | 5425 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
0b7b21e4 | 5426 | sr->len = READ_ONCE(sqe->len); |
bcda7baa | 5427 | sr->bgid = READ_ONCE(sqe->buf_group); |
04411806 PB |
5428 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
5429 | if (sr->msg_flags & MSG_DONTWAIT) | |
5430 | req->flags |= REQ_F_NOWAIT; | |
06b76d44 | 5431 | |
d8768362 JA |
5432 | #ifdef CONFIG_COMPAT |
5433 | if (req->ctx->compat) | |
5434 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
5435 | #endif | |
7ba89d2a | 5436 | sr->done_io = 0; |
93642ef8 | 5437 | return 0; |
aa1fa28f JA |
5438 | } |
5439 | ||
7ba89d2a JA |
5440 | static bool io_net_retry(struct socket *sock, int flags) |
5441 | { | |
5442 | if (!(flags & MSG_WAITALL)) | |
5443 | return false; | |
5444 | return sock->type == SOCK_STREAM || sock->type == SOCK_SEQPACKET; | |
5445 | } | |
5446 | ||
889fca73 | 5447 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 5448 | { |
6b754c8b | 5449 | struct io_async_msghdr iomsg, *kmsg; |
7ba89d2a | 5450 | struct io_sr_msg *sr = &req->sr_msg; |
03b1230c | 5451 | struct socket *sock; |
7fbb1b54 | 5452 | struct io_buffer *kbuf; |
7a7cacba | 5453 | unsigned flags; |
d1fd1c20 | 5454 | int ret, min_ret = 0; |
45d189c6 | 5455 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
03b1230c | 5456 | |
dba4a925 | 5457 | sock = sock_from_file(req->file); |
7a7cacba | 5458 | if (unlikely(!sock)) |
dba4a925 | 5459 | return -ENOTSOCK; |
3529d8c2 | 5460 | |
d886e185 PB |
5461 | if (req_has_async_data(req)) { |
5462 | kmsg = req->async_data; | |
5463 | } else { | |
7a7cacba PB |
5464 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
5465 | if (ret) | |
681fda8d | 5466 | return ret; |
7a7cacba PB |
5467 | kmsg = &iomsg; |
5468 | } | |
03b1230c | 5469 | |
bc02ef33 | 5470 | if (req->flags & REQ_F_BUFFER_SELECT) { |
51aac424 | 5471 | kbuf = io_recv_buffer_select(req, issue_flags); |
bc02ef33 | 5472 | if (IS_ERR(kbuf)) |
52de1fe1 | 5473 | return PTR_ERR(kbuf); |
7a7cacba | 5474 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
5476dfed PB |
5475 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
5476 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, | |
7a7cacba PB |
5477 | 1, req->sr_msg.len); |
5478 | } | |
52de1fe1 | 5479 | |
04411806 PB |
5480 | flags = req->sr_msg.msg_flags; |
5481 | if (force_nonblock) | |
7a7cacba | 5482 | flags |= MSG_DONTWAIT; |
0031275d SM |
5483 | if (flags & MSG_WAITALL) |
5484 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
5485 | ||
7a7cacba PB |
5486 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
5487 | kmsg->uaddr, flags); | |
7297ce3d PB |
5488 | if (ret < min_ret) { |
5489 | if (ret == -EAGAIN && force_nonblock) | |
5490 | return io_setup_async_msg(req, kmsg); | |
5491 | if (ret == -ERESTARTSYS) | |
5492 | ret = -EINTR; | |
7ba89d2a JA |
5493 | if (ret > 0 && io_net_retry(sock, flags)) { |
5494 | sr->done_io += ret; | |
8a3e8ee5 | 5495 | req->flags |= REQ_F_PARTIAL_IO; |
7ba89d2a JA |
5496 | return io_setup_async_msg(req, kmsg); |
5497 | } | |
7297ce3d PB |
5498 | req_set_fail(req); |
5499 | } else if ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) { | |
5500 | req_set_fail(req); | |
5501 | } | |
03b1230c | 5502 | |
257e84a5 PB |
5503 | /* fast path, check for non-NULL to avoid function call */ |
5504 | if (kmsg->free_iov) | |
5505 | kfree(kmsg->free_iov); | |
99bc4c38 | 5506 | req->flags &= ~REQ_F_NEED_CLEANUP; |
7ba89d2a JA |
5507 | if (ret >= 0) |
5508 | ret += sr->done_io; | |
5509 | else if (sr->done_io) | |
5510 | ret = sr->done_io; | |
cc3cec83 | 5511 | __io_req_complete(req, issue_flags, ret, io_put_kbuf(req, issue_flags)); |
03b1230c | 5512 | return 0; |
0fa03c62 | 5513 | } |
5d17b4a4 | 5514 | |
889fca73 | 5515 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 5516 | { |
6b754c8b | 5517 | struct io_buffer *kbuf; |
7a7cacba PB |
5518 | struct io_sr_msg *sr = &req->sr_msg; |
5519 | struct msghdr msg; | |
5520 | void __user *buf = sr->buf; | |
fddaface | 5521 | struct socket *sock; |
7a7cacba PB |
5522 | struct iovec iov; |
5523 | unsigned flags; | |
d1fd1c20 | 5524 | int ret, min_ret = 0; |
45d189c6 | 5525 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
fddaface | 5526 | |
dba4a925 | 5527 | sock = sock_from_file(req->file); |
7a7cacba | 5528 | if (unlikely(!sock)) |
dba4a925 | 5529 | return -ENOTSOCK; |
fddaface | 5530 | |
bc02ef33 | 5531 | if (req->flags & REQ_F_BUFFER_SELECT) { |
51aac424 | 5532 | kbuf = io_recv_buffer_select(req, issue_flags); |
bcda7baa JA |
5533 | if (IS_ERR(kbuf)) |
5534 | return PTR_ERR(kbuf); | |
7a7cacba | 5535 | buf = u64_to_user_ptr(kbuf->addr); |
bc02ef33 | 5536 | } |
bcda7baa | 5537 | |
7a7cacba | 5538 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
14c32eee PB |
5539 | if (unlikely(ret)) |
5540 | goto out_free; | |
fddaface | 5541 | |
7a7cacba PB |
5542 | msg.msg_name = NULL; |
5543 | msg.msg_control = NULL; | |
5544 | msg.msg_controllen = 0; | |
5545 | msg.msg_namelen = 0; | |
5546 | msg.msg_iocb = NULL; | |
5547 | msg.msg_flags = 0; | |
fddaface | 5548 | |
04411806 PB |
5549 | flags = req->sr_msg.msg_flags; |
5550 | if (force_nonblock) | |
7a7cacba | 5551 | flags |= MSG_DONTWAIT; |
0031275d SM |
5552 | if (flags & MSG_WAITALL) |
5553 | min_ret = iov_iter_count(&msg.msg_iter); | |
5554 | ||
7a7cacba | 5555 | ret = sock_recvmsg(sock, &msg, flags); |
7297ce3d PB |
5556 | if (ret < min_ret) { |
5557 | if (ret == -EAGAIN && force_nonblock) | |
5558 | return -EAGAIN; | |
5559 | if (ret == -ERESTARTSYS) | |
5560 | ret = -EINTR; | |
7ba89d2a JA |
5561 | if (ret > 0 && io_net_retry(sock, flags)) { |
5562 | sr->len -= ret; | |
5563 | sr->buf += ret; | |
5564 | sr->done_io += ret; | |
8a3e8ee5 | 5565 | req->flags |= REQ_F_PARTIAL_IO; |
7ba89d2a JA |
5566 | return -EAGAIN; |
5567 | } | |
7297ce3d PB |
5568 | req_set_fail(req); |
5569 | } else if ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) { | |
0d7c1153 | 5570 | out_free: |
93d2bcd2 | 5571 | req_set_fail(req); |
7297ce3d | 5572 | } |
cc3cec83 | 5573 | |
7ba89d2a JA |
5574 | if (ret >= 0) |
5575 | ret += sr->done_io; | |
5576 | else if (sr->done_io) | |
5577 | ret = sr->done_io; | |
cc3cec83 | 5578 | __io_req_complete(req, issue_flags, ret, io_put_kbuf(req, issue_flags)); |
fddaface | 5579 | return 0; |
fddaface JA |
5580 | } |
5581 | ||
3529d8c2 | 5582 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 | 5583 | { |
8ed8d3c3 JA |
5584 | struct io_accept *accept = &req->accept; |
5585 | ||
14587a46 | 5586 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
17f2fe35 | 5587 | return -EINVAL; |
aaa4db12 | 5588 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
5589 | return -EINVAL; |
5590 | ||
d55e5f5b JA |
5591 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5592 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 5593 | accept->flags = READ_ONCE(sqe->accept_flags); |
09952e3e | 5594 | accept->nofile = rlimit(RLIMIT_NOFILE); |
a7083ad5 | 5595 | |
aaa4db12 | 5596 | accept->file_slot = READ_ONCE(sqe->file_index); |
adf3a9e9 | 5597 | if (accept->file_slot && (accept->flags & SOCK_CLOEXEC)) |
aaa4db12 | 5598 | return -EINVAL; |
a7083ad5 PB |
5599 | if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) |
5600 | return -EINVAL; | |
5601 | if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK)) | |
5602 | accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK; | |
8ed8d3c3 | 5603 | return 0; |
8ed8d3c3 | 5604 | } |
17f2fe35 | 5605 | |
889fca73 | 5606 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 JA |
5607 | { |
5608 | struct io_accept *accept = &req->accept; | |
45d189c6 | 5609 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ac45abc0 | 5610 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
aaa4db12 | 5611 | bool fixed = !!accept->file_slot; |
a7083ad5 PB |
5612 | struct file *file; |
5613 | int ret, fd; | |
8ed8d3c3 | 5614 | |
aaa4db12 PB |
5615 | if (!fixed) { |
5616 | fd = __get_unused_fd_flags(accept->flags, accept->nofile); | |
5617 | if (unlikely(fd < 0)) | |
5618 | return fd; | |
5619 | } | |
a7083ad5 PB |
5620 | file = do_accept(req->file, file_flags, accept->addr, accept->addr_len, |
5621 | accept->flags); | |
5622 | if (IS_ERR(file)) { | |
aaa4db12 PB |
5623 | if (!fixed) |
5624 | put_unused_fd(fd); | |
a7083ad5 PB |
5625 | ret = PTR_ERR(file); |
5626 | if (ret == -EAGAIN && force_nonblock) | |
5627 | return -EAGAIN; | |
ac45abc0 PB |
5628 | if (ret == -ERESTARTSYS) |
5629 | ret = -EINTR; | |
93d2bcd2 | 5630 | req_set_fail(req); |
aaa4db12 | 5631 | } else if (!fixed) { |
a7083ad5 PB |
5632 | fd_install(fd, file); |
5633 | ret = fd; | |
aaa4db12 PB |
5634 | } else { |
5635 | ret = io_install_fixed_file(req, file, issue_flags, | |
5636 | accept->file_slot - 1); | |
ac45abc0 | 5637 | } |
889fca73 | 5638 | __io_req_complete(req, issue_flags, ret, 0); |
17f2fe35 | 5639 | return 0; |
8ed8d3c3 JA |
5640 | } |
5641 | ||
93642ef8 PB |
5642 | static int io_connect_prep_async(struct io_kiocb *req) |
5643 | { | |
5644 | struct io_async_connect *io = req->async_data; | |
5645 | struct io_connect *conn = &req->connect; | |
5646 | ||
5647 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); | |
5648 | } | |
5649 | ||
3529d8c2 | 5650 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 | 5651 | { |
3529d8c2 | 5652 | struct io_connect *conn = &req->connect; |
f499a021 | 5653 | |
14587a46 | 5654 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3fbb51c1 | 5655 | return -EINVAL; |
26578cda PB |
5656 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags || |
5657 | sqe->splice_fd_in) | |
3fbb51c1 JA |
5658 | return -EINVAL; |
5659 | ||
3529d8c2 JA |
5660 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5661 | conn->addr_len = READ_ONCE(sqe->addr2); | |
93642ef8 | 5662 | return 0; |
f499a021 JA |
5663 | } |
5664 | ||
889fca73 | 5665 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
f8e85cf2 | 5666 | { |
e8c2bc1f | 5667 | struct io_async_connect __io, *io; |
f8e85cf2 | 5668 | unsigned file_flags; |
3fbb51c1 | 5669 | int ret; |
45d189c6 | 5670 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
f8e85cf2 | 5671 | |
d886e185 | 5672 | if (req_has_async_data(req)) { |
e8c2bc1f | 5673 | io = req->async_data; |
f499a021 | 5674 | } else { |
3529d8c2 JA |
5675 | ret = move_addr_to_kernel(req->connect.addr, |
5676 | req->connect.addr_len, | |
e8c2bc1f | 5677 | &__io.address); |
f499a021 JA |
5678 | if (ret) |
5679 | goto out; | |
5680 | io = &__io; | |
5681 | } | |
5682 | ||
3fbb51c1 JA |
5683 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
5684 | ||
e8c2bc1f | 5685 | ret = __sys_connect_file(req->file, &io->address, |
3fbb51c1 | 5686 | req->connect.addr_len, file_flags); |
87f80d62 | 5687 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
d886e185 | 5688 | if (req_has_async_data(req)) |
b7bb4f7d | 5689 | return -EAGAIN; |
e8c2bc1f | 5690 | if (io_alloc_async_data(req)) { |
f499a021 JA |
5691 | ret = -ENOMEM; |
5692 | goto out; | |
5693 | } | |
e8c2bc1f | 5694 | memcpy(req->async_data, &__io, sizeof(__io)); |
f8e85cf2 | 5695 | return -EAGAIN; |
f499a021 | 5696 | } |
f8e85cf2 JA |
5697 | if (ret == -ERESTARTSYS) |
5698 | ret = -EINTR; | |
f499a021 | 5699 | out: |
4e88d6e7 | 5700 | if (ret < 0) |
93d2bcd2 | 5701 | req_set_fail(req); |
889fca73 | 5702 | __io_req_complete(req, issue_flags, ret, 0); |
f8e85cf2 | 5703 | return 0; |
469956e8 Y |
5704 | } |
5705 | #else /* !CONFIG_NET */ | |
99a10081 JA |
5706 | #define IO_NETOP_FN(op) \ |
5707 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ | |
5708 | { \ | |
5709 | return -EOPNOTSUPP; \ | |
5710 | } | |
5711 | ||
5712 | #define IO_NETOP_PREP(op) \ | |
5713 | IO_NETOP_FN(op) \ | |
5714 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ | |
5715 | { \ | |
5716 | return -EOPNOTSUPP; \ | |
5717 | } \ | |
5718 | ||
5719 | #define IO_NETOP_PREP_ASYNC(op) \ | |
5720 | IO_NETOP_PREP(op) \ | |
5721 | static int io_##op##_prep_async(struct io_kiocb *req) \ | |
5722 | { \ | |
5723 | return -EOPNOTSUPP; \ | |
5724 | } | |
5725 | ||
5726 | IO_NETOP_PREP_ASYNC(sendmsg); | |
5727 | IO_NETOP_PREP_ASYNC(recvmsg); | |
5728 | IO_NETOP_PREP_ASYNC(connect); | |
5729 | IO_NETOP_PREP(accept); | |
5730 | IO_NETOP_FN(send); | |
5731 | IO_NETOP_FN(recv); | |
469956e8 | 5732 | #endif /* CONFIG_NET */ |
f8e85cf2 | 5733 | |
d7718a9d JA |
5734 | struct io_poll_table { |
5735 | struct poll_table_struct pt; | |
5736 | struct io_kiocb *req; | |
68b11e8b | 5737 | int nr_entries; |
d7718a9d JA |
5738 | int error; |
5739 | }; | |
ce593a6c | 5740 | |
aa43477b | 5741 | #define IO_POLL_CANCEL_FLAG BIT(31) |
e2c0cb7c | 5742 | #define IO_POLL_REF_MASK GENMASK(30, 0) |
6d816e08 | 5743 | |
aa43477b PB |
5744 | /* |
5745 | * If refs part of ->poll_refs (see IO_POLL_REF_MASK) is 0, it's free. We can | |
5746 | * bump it and acquire ownership. It's disallowed to modify requests while not | |
5747 | * owning it, that prevents from races for enqueueing task_work's and b/w | |
5748 | * arming poll and wakeups. | |
5749 | */ | |
5750 | static inline bool io_poll_get_ownership(struct io_kiocb *req) | |
5751 | { | |
5752 | return !(atomic_fetch_inc(&req->poll_refs) & IO_POLL_REF_MASK); | |
d7718a9d JA |
5753 | } |
5754 | ||
aa43477b | 5755 | static void io_poll_mark_cancelled(struct io_kiocb *req) |
74ce6ce4 | 5756 | { |
aa43477b | 5757 | atomic_or(IO_POLL_CANCEL_FLAG, &req->poll_refs); |
74ce6ce4 JA |
5758 | } |
5759 | ||
d4e7cd36 | 5760 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
18bceab1 | 5761 | { |
e8c2bc1f | 5762 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
d4e7cd36 | 5763 | if (req->opcode == IORING_OP_POLL_ADD) |
e8c2bc1f | 5764 | return req->async_data; |
d4e7cd36 JA |
5765 | return req->apoll->double_poll; |
5766 | } | |
5767 | ||
5768 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) | |
5769 | { | |
5770 | if (req->opcode == IORING_OP_POLL_ADD) | |
5771 | return &req->poll; | |
5772 | return &req->apoll->poll; | |
5773 | } | |
5774 | ||
5641897a | 5775 | static void io_poll_req_insert(struct io_kiocb *req) |
d4e7cd36 | 5776 | { |
5641897a PB |
5777 | struct io_ring_ctx *ctx = req->ctx; |
5778 | struct hlist_head *list; | |
18bceab1 | 5779 | |
5641897a PB |
5780 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
5781 | hlist_add_head(&req->hash_node, list); | |
18bceab1 JA |
5782 | } |
5783 | ||
5641897a PB |
5784 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, |
5785 | wait_queue_func_t wake_func) | |
18bceab1 | 5786 | { |
5641897a | 5787 | poll->head = NULL; |
5641897a PB |
5788 | #define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP) |
5789 | /* mask in events that we always want/need */ | |
5790 | poll->events = events | IO_POLL_UNMASK; | |
5791 | INIT_LIST_HEAD(&poll->wait.entry); | |
5792 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
18bceab1 JA |
5793 | } |
5794 | ||
aa43477b | 5795 | static inline void io_poll_remove_entry(struct io_poll_iocb *poll) |
18bceab1 | 5796 | { |
791f3465 | 5797 | struct wait_queue_head *head = smp_load_acquire(&poll->head); |
18bceab1 | 5798 | |
791f3465 PB |
5799 | if (head) { |
5800 | spin_lock_irq(&head->lock); | |
5801 | list_del_init(&poll->wait.entry); | |
5802 | poll->head = NULL; | |
5803 | spin_unlock_irq(&head->lock); | |
5804 | } | |
aa43477b | 5805 | } |
18bceab1 | 5806 | |
aa43477b PB |
5807 | static void io_poll_remove_entries(struct io_kiocb *req) |
5808 | { | |
91eac1c6 JA |
5809 | /* |
5810 | * Nothing to do if neither of those flags are set. Avoid dipping | |
5811 | * into the poll/apoll/double cachelines if we can. | |
5812 | */ | |
5813 | if (!(req->flags & (REQ_F_SINGLE_POLL | REQ_F_DOUBLE_POLL))) | |
5814 | return; | |
18bceab1 | 5815 | |
791f3465 PB |
5816 | /* |
5817 | * While we hold the waitqueue lock and the waitqueue is nonempty, | |
5818 | * wake_up_pollfree() will wait for us. However, taking the waitqueue | |
5819 | * lock in the first place can race with the waitqueue being freed. | |
5820 | * | |
5821 | * We solve this as eventpoll does: by taking advantage of the fact that | |
5822 | * all users of wake_up_pollfree() will RCU-delay the actual free. If | |
5823 | * we enter rcu_read_lock() and see that the pointer to the queue is | |
5824 | * non-NULL, we can then lock it without the memory being freed out from | |
5825 | * under us. | |
5826 | * | |
5827 | * Keep holding rcu_read_lock() as long as we hold the queue lock, in | |
5828 | * case the caller deletes the entry from the queue, leaving it empty. | |
5829 | * In that case, only RCU prevents the queue memory from being freed. | |
5830 | */ | |
5831 | rcu_read_lock(); | |
91eac1c6 JA |
5832 | if (req->flags & REQ_F_SINGLE_POLL) |
5833 | io_poll_remove_entry(io_poll_get_single(req)); | |
5834 | if (req->flags & REQ_F_DOUBLE_POLL) | |
5835 | io_poll_remove_entry(io_poll_get_double(req)); | |
791f3465 | 5836 | rcu_read_unlock(); |
18bceab1 JA |
5837 | } |
5838 | ||
aa43477b PB |
5839 | /* |
5840 | * All poll tw should go through this. Checks for poll events, manages | |
5841 | * references, does rewait, etc. | |
5842 | * | |
5843 | * Returns a negative error on failure. >0 when no action require, which is | |
5844 | * either spurious wakeup or multishot CQE is served. 0 when it's done with | |
5845 | * the request, then the mask is stored in req->result. | |
5846 | */ | |
5106dd6e | 5847 | static int io_poll_check_events(struct io_kiocb *req, bool locked) |
18bceab1 | 5848 | { |
74ce6ce4 | 5849 | struct io_ring_ctx *ctx = req->ctx; |
aa43477b | 5850 | int v; |
18bceab1 | 5851 | |
316319e8 | 5852 | /* req->task == current here, checking PF_EXITING is safe */ |
e09ee510 | 5853 | if (unlikely(req->task->flags & PF_EXITING)) |
aa43477b | 5854 | io_poll_mark_cancelled(req); |
18bceab1 | 5855 | |
aa43477b PB |
5856 | do { |
5857 | v = atomic_read(&req->poll_refs); | |
74ce6ce4 | 5858 | |
aa43477b PB |
5859 | /* tw handler should be the owner, and so have some references */ |
5860 | if (WARN_ON_ONCE(!(v & IO_POLL_REF_MASK))) | |
5861 | return 0; | |
5862 | if (v & IO_POLL_CANCEL_FLAG) | |
5863 | return -ECANCELED; | |
8706e04e | 5864 | |
aa43477b | 5865 | if (!req->result) { |
2804ecd8 | 5866 | struct poll_table_struct pt = { ._key = req->apoll_events }; |
cce64ef0 | 5867 | unsigned flags = locked ? 0 : IO_URING_F_UNLOCKED; |
18bceab1 | 5868 | |
cce64ef0 | 5869 | if (unlikely(!io_assign_file(req, flags))) |
7179c3ce PB |
5870 | return -EBADF; |
5871 | req->result = vfs_poll(req->file, &pt) & req->apoll_events; | |
c8b5e260 | 5872 | } |
74ce6ce4 | 5873 | |
aa43477b | 5874 | /* multishot, just fill an CQE and proceed */ |
2804ecd8 JA |
5875 | if (req->result && !(req->apoll_events & EPOLLONESHOT)) { |
5876 | __poll_t mask = mangle_poll(req->result & req->apoll_events); | |
aa43477b | 5877 | bool filled; |
18bceab1 | 5878 | |
aa43477b PB |
5879 | spin_lock(&ctx->completion_lock); |
5880 | filled = io_fill_cqe_aux(ctx, req->user_data, mask, | |
5881 | IORING_CQE_F_MORE); | |
5882 | io_commit_cqring(ctx); | |
5883 | spin_unlock(&ctx->completion_lock); | |
5884 | if (unlikely(!filled)) | |
5885 | return -ECANCELED; | |
5886 | io_cqring_ev_posted(ctx); | |
5887 | } else if (req->result) { | |
5888 | return 0; | |
5889 | } | |
18bceab1 | 5890 | |
aa43477b PB |
5891 | /* |
5892 | * Release all references, retry if someone tried to restart | |
5893 | * task_work while we were executing it. | |
5894 | */ | |
5895 | } while (atomic_sub_return(v & IO_POLL_REF_MASK, &req->poll_refs)); | |
18bceab1 | 5896 | |
18bceab1 JA |
5897 | return 1; |
5898 | } | |
5899 | ||
aa43477b | 5900 | static void io_poll_task_func(struct io_kiocb *req, bool *locked) |
18bceab1 | 5901 | { |
18bceab1 | 5902 | struct io_ring_ctx *ctx = req->ctx; |
aa43477b | 5903 | int ret; |
18bceab1 | 5904 | |
5106dd6e | 5905 | ret = io_poll_check_events(req, *locked); |
aa43477b PB |
5906 | if (ret > 0) |
5907 | return; | |
5908 | ||
5909 | if (!ret) { | |
5910 | req->result = mangle_poll(req->result & req->poll.events); | |
e27414be | 5911 | } else { |
aa43477b PB |
5912 | req->result = ret; |
5913 | req_set_fail(req); | |
a62682f9 | 5914 | } |
aa43477b PB |
5915 | |
5916 | io_poll_remove_entries(req); | |
5917 | spin_lock(&ctx->completion_lock); | |
5918 | hash_del(&req->hash_node); | |
5919 | __io_req_complete_post(req, req->result, 0); | |
5920 | io_commit_cqring(ctx); | |
5921 | spin_unlock(&ctx->completion_lock); | |
5922 | io_cqring_ev_posted(ctx); | |
18bceab1 JA |
5923 | } |
5924 | ||
aa43477b | 5925 | static void io_apoll_task_func(struct io_kiocb *req, bool *locked) |
18bceab1 JA |
5926 | { |
5927 | struct io_ring_ctx *ctx = req->ctx; | |
aa43477b | 5928 | int ret; |
18bceab1 | 5929 | |
5106dd6e | 5930 | ret = io_poll_check_events(req, *locked); |
aa43477b PB |
5931 | if (ret > 0) |
5932 | return; | |
18bceab1 | 5933 | |
aa43477b PB |
5934 | io_poll_remove_entries(req); |
5935 | spin_lock(&ctx->completion_lock); | |
5936 | hash_del(&req->hash_node); | |
5937 | spin_unlock(&ctx->completion_lock); | |
18bceab1 | 5938 | |
aa43477b PB |
5939 | if (!ret) |
5940 | io_req_task_submit(req, locked); | |
5941 | else | |
5942 | io_req_complete_failed(req, ret); | |
18bceab1 JA |
5943 | } |
5944 | ||
81459350 | 5945 | static void __io_poll_execute(struct io_kiocb *req, int mask, int events) |
aa43477b PB |
5946 | { |
5947 | req->result = mask; | |
81459350 JA |
5948 | /* |
5949 | * This is useful for poll that is armed on behalf of another | |
5950 | * request, and where the wakeup path could be on a different | |
5951 | * CPU. We want to avoid pulling in req->apoll->events for that | |
5952 | * case. | |
5953 | */ | |
2804ecd8 | 5954 | req->apoll_events = events; |
aa43477b PB |
5955 | if (req->opcode == IORING_OP_POLL_ADD) |
5956 | req->io_task_work.func = io_poll_task_func; | |
5957 | else | |
5958 | req->io_task_work.func = io_apoll_task_func; | |
5959 | ||
502c87d6 | 5960 | trace_io_uring_task_add(req->ctx, req, req->user_data, req->opcode, mask); |
aa43477b PB |
5961 | io_req_task_work_add(req, false); |
5962 | } | |
5963 | ||
81459350 | 5964 | static inline void io_poll_execute(struct io_kiocb *req, int res, int events) |
aa43477b PB |
5965 | { |
5966 | if (io_poll_get_ownership(req)) | |
81459350 | 5967 | __io_poll_execute(req, res, events); |
aa43477b PB |
5968 | } |
5969 | ||
5970 | static void io_poll_cancel_req(struct io_kiocb *req) | |
5971 | { | |
5972 | io_poll_mark_cancelled(req); | |
5973 | /* kick tw, which should complete the request */ | |
81459350 | 5974 | io_poll_execute(req, 0, 0); |
aa43477b PB |
5975 | } |
5976 | ||
d89a4fac JA |
5977 | #define wqe_to_req(wait) ((void *)((unsigned long) (wait)->private & ~1)) |
5978 | #define wqe_is_double(wait) ((unsigned long) (wait)->private & 1) | |
5979 | ||
aa43477b PB |
5980 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
5981 | void *key) | |
18bceab1 | 5982 | { |
d89a4fac | 5983 | struct io_kiocb *req = wqe_to_req(wait); |
aa43477b PB |
5984 | struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb, |
5985 | wait); | |
18bceab1 JA |
5986 | __poll_t mask = key_to_poll(key); |
5987 | ||
791f3465 PB |
5988 | if (unlikely(mask & POLLFREE)) { |
5989 | io_poll_mark_cancelled(req); | |
5990 | /* we have to kick tw in case it's not already */ | |
81459350 | 5991 | io_poll_execute(req, 0, poll->events); |
791f3465 PB |
5992 | |
5993 | /* | |
5994 | * If the waitqueue is being freed early but someone is already | |
5995 | * holds ownership over it, we have to tear down the request as | |
5996 | * best we can. That means immediately removing the request from | |
5997 | * its waitqueue and preventing all further accesses to the | |
5998 | * waitqueue via the request. | |
5999 | */ | |
6000 | list_del_init(&poll->wait.entry); | |
6001 | ||
6002 | /* | |
6003 | * Careful: this *must* be the last step, since as soon | |
6004 | * as req->head is NULL'ed out, the request can be | |
6005 | * completed and freed, since aio_poll_complete_work() | |
6006 | * will no longer need to take the waitqueue lock. | |
6007 | */ | |
6008 | smp_store_release(&poll->head, NULL); | |
6009 | return 1; | |
6010 | } | |
6011 | ||
aa43477b | 6012 | /* for instances that support it check for an event match first */ |
18bceab1 JA |
6013 | if (mask && !(mask & poll->events)) |
6014 | return 0; | |
6015 | ||
eb0089d6 PB |
6016 | if (io_poll_get_ownership(req)) { |
6017 | /* optional, saves extra locking for removal in tw handler */ | |
6018 | if (mask && poll->events & EPOLLONESHOT) { | |
6019 | list_del_init(&poll->wait.entry); | |
6020 | poll->head = NULL; | |
d89a4fac JA |
6021 | if (wqe_is_double(wait)) |
6022 | req->flags &= ~REQ_F_DOUBLE_POLL; | |
6023 | else | |
6024 | req->flags &= ~REQ_F_SINGLE_POLL; | |
eb0089d6 | 6025 | } |
81459350 | 6026 | __io_poll_execute(req, mask, poll->events); |
eb0089d6 | 6027 | } |
18bceab1 | 6028 | return 1; |
18bceab1 JA |
6029 | } |
6030 | ||
6031 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, | |
807abcb0 JA |
6032 | struct wait_queue_head *head, |
6033 | struct io_poll_iocb **poll_ptr) | |
18bceab1 JA |
6034 | { |
6035 | struct io_kiocb *req = pt->req; | |
d89a4fac | 6036 | unsigned long wqe_private = (unsigned long) req; |
18bceab1 JA |
6037 | |
6038 | /* | |
68b11e8b PB |
6039 | * The file being polled uses multiple waitqueues for poll handling |
6040 | * (e.g. one for read, one for write). Setup a separate io_poll_iocb | |
6041 | * if this happens. | |
18bceab1 | 6042 | */ |
68b11e8b | 6043 | if (unlikely(pt->nr_entries)) { |
aa43477b | 6044 | struct io_poll_iocb *first = poll; |
58852d4d | 6045 | |
23a65db8 | 6046 | /* double add on the same waitqueue head, ignore */ |
aa43477b | 6047 | if (first->head == head) |
23a65db8 | 6048 | return; |
18bceab1 | 6049 | /* already have a 2nd entry, fail a third attempt */ |
807abcb0 | 6050 | if (*poll_ptr) { |
23a65db8 PB |
6051 | if ((*poll_ptr)->head == head) |
6052 | return; | |
18bceab1 JA |
6053 | pt->error = -EINVAL; |
6054 | return; | |
6055 | } | |
aa43477b | 6056 | |
18bceab1 JA |
6057 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
6058 | if (!poll) { | |
6059 | pt->error = -ENOMEM; | |
6060 | return; | |
6061 | } | |
d89a4fac JA |
6062 | /* mark as double wq entry */ |
6063 | wqe_private |= 1; | |
91eac1c6 | 6064 | req->flags |= REQ_F_DOUBLE_POLL; |
aa43477b | 6065 | io_init_poll_iocb(poll, first->events, first->wait.func); |
807abcb0 | 6066 | *poll_ptr = poll; |
d886e185 PB |
6067 | if (req->opcode == IORING_OP_POLL_ADD) |
6068 | req->flags |= REQ_F_ASYNC_DATA; | |
18bceab1 JA |
6069 | } |
6070 | ||
91eac1c6 | 6071 | req->flags |= REQ_F_SINGLE_POLL; |
68b11e8b | 6072 | pt->nr_entries++; |
18bceab1 | 6073 | poll->head = head; |
d89a4fac | 6074 | poll->wait.private = (void *) wqe_private; |
a31eb4a2 JX |
6075 | |
6076 | if (poll->events & EPOLLEXCLUSIVE) | |
6077 | add_wait_queue_exclusive(head, &poll->wait); | |
6078 | else | |
6079 | add_wait_queue(head, &poll->wait); | |
18bceab1 JA |
6080 | } |
6081 | ||
aa43477b | 6082 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
18bceab1 JA |
6083 | struct poll_table_struct *p) |
6084 | { | |
6085 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
d7718a9d | 6086 | |
aa43477b PB |
6087 | __io_queue_proc(&pt->req->poll, pt, head, |
6088 | (struct io_poll_iocb **) &pt->req->async_data); | |
d7718a9d JA |
6089 | } |
6090 | ||
aa43477b PB |
6091 | static int __io_arm_poll_handler(struct io_kiocb *req, |
6092 | struct io_poll_iocb *poll, | |
6093 | struct io_poll_table *ipt, __poll_t mask) | |
d7718a9d JA |
6094 | { |
6095 | struct io_ring_ctx *ctx = req->ctx; | |
aa43477b | 6096 | int v; |
d7718a9d | 6097 | |
4d52f338 | 6098 | INIT_HLIST_NODE(&req->hash_node); |
aa43477b | 6099 | io_init_poll_iocb(poll, mask, io_poll_wake); |
b90cd197 | 6100 | poll->file = req->file; |
d7718a9d JA |
6101 | |
6102 | ipt->pt._key = mask; | |
6103 | ipt->req = req; | |
68b11e8b PB |
6104 | ipt->error = 0; |
6105 | ipt->nr_entries = 0; | |
d7718a9d | 6106 | |
aa43477b PB |
6107 | /* |
6108 | * Take the ownership to delay any tw execution up until we're done | |
6109 | * with poll arming. see io_poll_get_ownership(). | |
6110 | */ | |
6111 | atomic_set(&req->poll_refs, 1); | |
d7718a9d | 6112 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
aa43477b PB |
6113 | |
6114 | if (mask && (poll->events & EPOLLONESHOT)) { | |
6115 | io_poll_remove_entries(req); | |
6116 | /* no one else has access to the req, forget about the ref */ | |
6117 | return mask; | |
6118 | } | |
6119 | if (!mask && unlikely(ipt->error || !ipt->nr_entries)) { | |
6120 | io_poll_remove_entries(req); | |
6121 | if (!ipt->error) | |
6122 | ipt->error = -EINVAL; | |
6123 | return 0; | |
6124 | } | |
d7718a9d | 6125 | |
79ebeaee | 6126 | spin_lock(&ctx->completion_lock); |
aa43477b PB |
6127 | io_poll_req_insert(req); |
6128 | spin_unlock(&ctx->completion_lock); | |
6129 | ||
6130 | if (mask) { | |
6131 | /* can't multishot if failed, just queue the event we've got */ | |
6132 | if (unlikely(ipt->error || !ipt->nr_entries)) | |
6133 | poll->events |= EPOLLONESHOT; | |
81459350 | 6134 | __io_poll_execute(req, mask, poll->events); |
aa43477b | 6135 | return 0; |
d7718a9d JA |
6136 | } |
6137 | ||
aa43477b PB |
6138 | /* |
6139 | * Release ownership. If someone tried to queue a tw while it was | |
6140 | * locked, kick it off for them. | |
6141 | */ | |
6142 | v = atomic_dec_return(&req->poll_refs); | |
6143 | if (unlikely(v & IO_POLL_REF_MASK)) | |
81459350 | 6144 | __io_poll_execute(req, 0, poll->events); |
aa43477b PB |
6145 | return 0; |
6146 | } | |
6147 | ||
6148 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
6149 | struct poll_table_struct *p) | |
6150 | { | |
6151 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
6152 | struct async_poll *apoll = pt->req->apoll; | |
6153 | ||
6154 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); | |
d7718a9d JA |
6155 | } |
6156 | ||
59b735ae OL |
6157 | enum { |
6158 | IO_APOLL_OK, | |
6159 | IO_APOLL_ABORTED, | |
6160 | IO_APOLL_READY | |
6161 | }; | |
6162 | ||
4d9237e3 | 6163 | static int io_arm_poll_handler(struct io_kiocb *req, unsigned issue_flags) |
d7718a9d JA |
6164 | { |
6165 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
6166 | struct io_ring_ctx *ctx = req->ctx; | |
6167 | struct async_poll *apoll; | |
6168 | struct io_poll_table ipt; | |
aa43477b PB |
6169 | __poll_t mask = EPOLLONESHOT | POLLERR | POLLPRI; |
6170 | int ret; | |
d7718a9d | 6171 | |
b2d9c3da PB |
6172 | if (!def->pollin && !def->pollout) |
6173 | return IO_APOLL_ABORTED; | |
658d0a40 PB |
6174 | if (!file_can_poll(req->file) || (req->flags & REQ_F_POLLED)) |
6175 | return IO_APOLL_ABORTED; | |
b2d9c3da PB |
6176 | |
6177 | if (def->pollin) { | |
b2d9c3da PB |
6178 | mask |= POLLIN | POLLRDNORM; |
6179 | ||
6180 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ | |
6181 | if ((req->opcode == IORING_OP_RECVMSG) && | |
6182 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) | |
6183 | mask &= ~POLLIN; | |
6184 | } else { | |
b2d9c3da PB |
6185 | mask |= POLLOUT | POLLWRNORM; |
6186 | } | |
52dd8640 DY |
6187 | if (def->poll_exclusive) |
6188 | mask |= EPOLLEXCLUSIVE; | |
4d9237e3 JA |
6189 | if (!(issue_flags & IO_URING_F_UNLOCKED) && |
6190 | !list_empty(&ctx->apoll_cache)) { | |
6191 | apoll = list_first_entry(&ctx->apoll_cache, struct async_poll, | |
6192 | poll.wait.entry); | |
6193 | list_del_init(&apoll->poll.wait.entry); | |
6194 | } else { | |
6195 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); | |
6196 | if (unlikely(!apoll)) | |
6197 | return IO_APOLL_ABORTED; | |
6198 | } | |
807abcb0 | 6199 | apoll->double_poll = NULL; |
d7718a9d | 6200 | req->apoll = apoll; |
b2d9c3da | 6201 | req->flags |= REQ_F_POLLED; |
d7718a9d JA |
6202 | ipt.pt._qproc = io_async_queue_proc; |
6203 | ||
4d55f238 | 6204 | io_kbuf_recycle(req, issue_flags); |
abdad709 | 6205 | |
aa43477b | 6206 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask); |
41a5169c HX |
6207 | if (ret || ipt.error) |
6208 | return ret ? IO_APOLL_READY : IO_APOLL_ABORTED; | |
6209 | ||
502c87d6 | 6210 | trace_io_uring_poll_arm(ctx, req, req->user_data, req->opcode, |
236daeae | 6211 | mask, apoll->poll.events); |
59b735ae | 6212 | return IO_APOLL_OK; |
d7718a9d JA |
6213 | } |
6214 | ||
76e1b642 JA |
6215 | /* |
6216 | * Returns true if we found and killed one or more poll requests | |
6217 | */ | |
c072481d PB |
6218 | static __cold bool io_poll_remove_all(struct io_ring_ctx *ctx, |
6219 | struct task_struct *tsk, bool cancel_all) | |
221c5eb2 | 6220 | { |
78076bb6 | 6221 | struct hlist_node *tmp; |
221c5eb2 | 6222 | struct io_kiocb *req; |
aa43477b PB |
6223 | bool found = false; |
6224 | int i; | |
221c5eb2 | 6225 | |
79ebeaee | 6226 | spin_lock(&ctx->completion_lock); |
78076bb6 JA |
6227 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
6228 | struct hlist_head *list; | |
6229 | ||
6230 | list = &ctx->cancel_hash[i]; | |
f3606e3a | 6231 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
42a7b4ed | 6232 | if (io_match_task_safe(req, tsk, cancel_all)) { |
61bc84c4 | 6233 | hlist_del_init(&req->hash_node); |
aa43477b PB |
6234 | io_poll_cancel_req(req); |
6235 | found = true; | |
6236 | } | |
f3606e3a | 6237 | } |
221c5eb2 | 6238 | } |
79ebeaee | 6239 | spin_unlock(&ctx->completion_lock); |
aa43477b | 6240 | return found; |
221c5eb2 JA |
6241 | } |
6242 | ||
9ba5fac8 PB |
6243 | static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr, |
6244 | bool poll_only) | |
e07785b0 | 6245 | __must_hold(&ctx->completion_lock) |
47f46768 | 6246 | { |
78076bb6 | 6247 | struct hlist_head *list; |
47f46768 JA |
6248 | struct io_kiocb *req; |
6249 | ||
78076bb6 JA |
6250 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
6251 | hlist_for_each_entry(req, list, hash_node) { | |
b41e9852 JA |
6252 | if (sqe_addr != req->user_data) |
6253 | continue; | |
9ba5fac8 PB |
6254 | if (poll_only && req->opcode != IORING_OP_POLL_ADD) |
6255 | continue; | |
b2cb805f | 6256 | return req; |
47f46768 | 6257 | } |
b2cb805f JA |
6258 | return NULL; |
6259 | } | |
6260 | ||
aa43477b PB |
6261 | static bool io_poll_disarm(struct io_kiocb *req) |
6262 | __must_hold(&ctx->completion_lock) | |
6263 | { | |
6264 | if (!io_poll_get_ownership(req)) | |
6265 | return false; | |
6266 | io_poll_remove_entries(req); | |
6267 | hash_del(&req->hash_node); | |
6268 | return true; | |
6269 | } | |
6270 | ||
9ba5fac8 PB |
6271 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr, |
6272 | bool poll_only) | |
e07785b0 | 6273 | __must_hold(&ctx->completion_lock) |
b2cb805f | 6274 | { |
aa43477b | 6275 | struct io_kiocb *req = io_poll_find(ctx, sqe_addr, poll_only); |
b2cb805f | 6276 | |
b2cb805f JA |
6277 | if (!req) |
6278 | return -ENOENT; | |
aa43477b PB |
6279 | io_poll_cancel_req(req); |
6280 | return 0; | |
47f46768 JA |
6281 | } |
6282 | ||
9096af3e PB |
6283 | static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe, |
6284 | unsigned int flags) | |
6285 | { | |
6286 | u32 events; | |
47f46768 | 6287 | |
9096af3e PB |
6288 | events = READ_ONCE(sqe->poll32_events); |
6289 | #ifdef __BIG_ENDIAN | |
6290 | events = swahw32(events); | |
6291 | #endif | |
6292 | if (!(flags & IORING_POLL_ADD_MULTI)) | |
6293 | events |= EPOLLONESHOT; | |
6294 | return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT)); | |
47f46768 JA |
6295 | } |
6296 | ||
c5de0036 | 6297 | static int io_poll_update_prep(struct io_kiocb *req, |
3529d8c2 | 6298 | const struct io_uring_sqe *sqe) |
0969e783 | 6299 | { |
c5de0036 PB |
6300 | struct io_poll_update *upd = &req->poll_update; |
6301 | u32 flags; | |
6302 | ||
0969e783 JA |
6303 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
6304 | return -EINVAL; | |
26578cda | 6305 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
c5de0036 PB |
6306 | return -EINVAL; |
6307 | flags = READ_ONCE(sqe->len); | |
6308 | if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA | | |
6309 | IORING_POLL_ADD_MULTI)) | |
6310 | return -EINVAL; | |
6311 | /* meaningless without update */ | |
6312 | if (flags == IORING_POLL_ADD_MULTI) | |
0969e783 JA |
6313 | return -EINVAL; |
6314 | ||
c5de0036 PB |
6315 | upd->old_user_data = READ_ONCE(sqe->addr); |
6316 | upd->update_events = flags & IORING_POLL_UPDATE_EVENTS; | |
6317 | upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA; | |
221c5eb2 | 6318 | |
c5de0036 PB |
6319 | upd->new_user_data = READ_ONCE(sqe->off); |
6320 | if (!upd->update_user_data && upd->new_user_data) | |
6321 | return -EINVAL; | |
6322 | if (upd->update_events) | |
6323 | upd->events = io_poll_parse_events(sqe, flags); | |
6324 | else if (sqe->poll32_events) | |
6325 | return -EINVAL; | |
221c5eb2 | 6326 | |
221c5eb2 JA |
6327 | return 0; |
6328 | } | |
6329 | ||
3529d8c2 | 6330 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
6331 | { |
6332 | struct io_poll_iocb *poll = &req->poll; | |
c5de0036 | 6333 | u32 flags; |
221c5eb2 JA |
6334 | |
6335 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
6336 | return -EINVAL; | |
c5de0036 | 6337 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr) |
88e41cf9 JA |
6338 | return -EINVAL; |
6339 | flags = READ_ONCE(sqe->len); | |
c5de0036 | 6340 | if (flags & ~IORING_POLL_ADD_MULTI) |
221c5eb2 | 6341 | return -EINVAL; |
04c76b41 PB |
6342 | if ((flags & IORING_POLL_ADD_MULTI) && (req->flags & REQ_F_CQE_SKIP)) |
6343 | return -EINVAL; | |
221c5eb2 | 6344 | |
48dcd38d | 6345 | io_req_set_refcount(req); |
2804ecd8 | 6346 | req->apoll_events = poll->events = io_poll_parse_events(sqe, flags); |
0969e783 JA |
6347 | return 0; |
6348 | } | |
6349 | ||
61e98203 | 6350 | static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags) |
0969e783 JA |
6351 | { |
6352 | struct io_poll_iocb *poll = &req->poll; | |
0969e783 | 6353 | struct io_poll_table ipt; |
aa43477b | 6354 | int ret; |
0969e783 | 6355 | |
d7718a9d | 6356 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 6357 | |
aa43477b PB |
6358 | ret = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events); |
6359 | ret = ret ?: ipt.error; | |
6360 | if (ret) | |
6361 | __io_req_complete(req, issue_flags, ret, 0); | |
6362 | return 0; | |
221c5eb2 JA |
6363 | } |
6364 | ||
c5de0036 | 6365 | static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags) |
b69de288 JA |
6366 | { |
6367 | struct io_ring_ctx *ctx = req->ctx; | |
6368 | struct io_kiocb *preq; | |
2bbb146d | 6369 | int ret2, ret = 0; |
cc8e9ba7 | 6370 | bool locked; |
b69de288 | 6371 | |
79ebeaee | 6372 | spin_lock(&ctx->completion_lock); |
9ba5fac8 | 6373 | preq = io_poll_find(ctx, req->poll_update.old_user_data, true); |
aa43477b | 6374 | if (!preq || !io_poll_disarm(preq)) { |
79ebeaee | 6375 | spin_unlock(&ctx->completion_lock); |
aa43477b | 6376 | ret = preq ? -EALREADY : -ENOENT; |
2bbb146d | 6377 | goto out; |
b69de288 | 6378 | } |
79ebeaee | 6379 | spin_unlock(&ctx->completion_lock); |
cb3b200e | 6380 | |
2bbb146d PB |
6381 | if (req->poll_update.update_events || req->poll_update.update_user_data) { |
6382 | /* only mask one event flags, keep behavior flags */ | |
6383 | if (req->poll_update.update_events) { | |
6384 | preq->poll.events &= ~0xffff; | |
6385 | preq->poll.events |= req->poll_update.events & 0xffff; | |
6386 | preq->poll.events |= IO_POLL_UNMASK; | |
cb3b200e | 6387 | } |
2bbb146d PB |
6388 | if (req->poll_update.update_user_data) |
6389 | preq->user_data = req->poll_update.new_user_data; | |
b69de288 | 6390 | |
2bbb146d PB |
6391 | ret2 = io_poll_add(preq, issue_flags); |
6392 | /* successfully updated, don't complete poll request */ | |
6393 | if (!ret2) | |
6394 | goto out; | |
b69de288 | 6395 | } |
6224590d | 6396 | |
2bbb146d | 6397 | req_set_fail(preq); |
cc8e9ba7 PB |
6398 | preq->result = -ECANCELED; |
6399 | locked = !(issue_flags & IO_URING_F_UNLOCKED); | |
6400 | io_req_task_complete(preq, &locked); | |
2bbb146d PB |
6401 | out: |
6402 | if (ret < 0) | |
6224590d | 6403 | req_set_fail(req); |
2bbb146d | 6404 | /* complete update request, we're done with it */ |
cc8e9ba7 | 6405 | __io_req_complete(req, issue_flags, ret, 0); |
b69de288 | 6406 | return 0; |
89850fce JA |
6407 | } |
6408 | ||
5262f567 JA |
6409 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
6410 | { | |
ad8a48ac JA |
6411 | struct io_timeout_data *data = container_of(timer, |
6412 | struct io_timeout_data, timer); | |
6413 | struct io_kiocb *req = data->req; | |
6414 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
6415 | unsigned long flags; |
6416 | ||
89850fce | 6417 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
a71976f3 | 6418 | list_del_init(&req->timeout.list); |
01cec8c1 PB |
6419 | atomic_set(&req->ctx->cq_timeouts, |
6420 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
89850fce | 6421 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); |
01cec8c1 | 6422 | |
a90c8bf6 PB |
6423 | if (!(data->flags & IORING_TIMEOUT_ETIME_SUCCESS)) |
6424 | req_set_fail(req); | |
6425 | ||
6426 | req->result = -ETIME; | |
6427 | req->io_task_work.func = io_req_task_complete; | |
4813c377 | 6428 | io_req_task_work_add(req, false); |
5262f567 JA |
6429 | return HRTIMER_NORESTART; |
6430 | } | |
6431 | ||
fbd15848 PB |
6432 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
6433 | __u64 user_data) | |
89850fce | 6434 | __must_hold(&ctx->timeout_lock) |
f254ac04 | 6435 | { |
fbd15848 | 6436 | struct io_timeout_data *io; |
47f46768 | 6437 | struct io_kiocb *req; |
fd9c7bc5 | 6438 | bool found = false; |
f254ac04 | 6439 | |
135fcde8 | 6440 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
fd9c7bc5 PB |
6441 | found = user_data == req->user_data; |
6442 | if (found) | |
47f46768 | 6443 | break; |
47f46768 | 6444 | } |
fd9c7bc5 PB |
6445 | if (!found) |
6446 | return ERR_PTR(-ENOENT); | |
fbd15848 PB |
6447 | |
6448 | io = req->async_data; | |
fd9c7bc5 | 6449 | if (hrtimer_try_to_cancel(&io->timer) == -1) |
fbd15848 | 6450 | return ERR_PTR(-EALREADY); |
a71976f3 | 6451 | list_del_init(&req->timeout.list); |
fbd15848 PB |
6452 | return req; |
6453 | } | |
47f46768 | 6454 | |
fbd15848 | 6455 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
ec3c3d0f | 6456 | __must_hold(&ctx->completion_lock) |
89850fce | 6457 | __must_hold(&ctx->timeout_lock) |
fbd15848 PB |
6458 | { |
6459 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); | |
6460 | ||
6461 | if (IS_ERR(req)) | |
6462 | return PTR_ERR(req); | |
6695490d | 6463 | io_req_task_queue_fail(req, -ECANCELED); |
f254ac04 JA |
6464 | return 0; |
6465 | } | |
6466 | ||
50c1df2b JA |
6467 | static clockid_t io_timeout_get_clock(struct io_timeout_data *data) |
6468 | { | |
6469 | switch (data->flags & IORING_TIMEOUT_CLOCK_MASK) { | |
6470 | case IORING_TIMEOUT_BOOTTIME: | |
6471 | return CLOCK_BOOTTIME; | |
6472 | case IORING_TIMEOUT_REALTIME: | |
6473 | return CLOCK_REALTIME; | |
6474 | default: | |
6475 | /* can't happen, vetted at prep time */ | |
6476 | WARN_ON_ONCE(1); | |
6477 | fallthrough; | |
6478 | case 0: | |
6479 | return CLOCK_MONOTONIC; | |
6480 | } | |
6481 | } | |
6482 | ||
f1042b6c PB |
6483 | static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
6484 | struct timespec64 *ts, enum hrtimer_mode mode) | |
6485 | __must_hold(&ctx->timeout_lock) | |
6486 | { | |
6487 | struct io_timeout_data *io; | |
6488 | struct io_kiocb *req; | |
6489 | bool found = false; | |
6490 | ||
6491 | list_for_each_entry(req, &ctx->ltimeout_list, timeout.list) { | |
6492 | found = user_data == req->user_data; | |
6493 | if (found) | |
6494 | break; | |
6495 | } | |
6496 | if (!found) | |
6497 | return -ENOENT; | |
6498 | ||
6499 | io = req->async_data; | |
6500 | if (hrtimer_try_to_cancel(&io->timer) == -1) | |
6501 | return -EALREADY; | |
6502 | hrtimer_init(&io->timer, io_timeout_get_clock(io), mode); | |
6503 | io->timer.function = io_link_timeout_fn; | |
6504 | hrtimer_start(&io->timer, timespec64_to_ktime(*ts), mode); | |
6505 | return 0; | |
6506 | } | |
6507 | ||
9c8e11b3 PB |
6508 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
6509 | struct timespec64 *ts, enum hrtimer_mode mode) | |
89850fce | 6510 | __must_hold(&ctx->timeout_lock) |
47f46768 | 6511 | { |
9c8e11b3 PB |
6512 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
6513 | struct io_timeout_data *data; | |
47f46768 | 6514 | |
9c8e11b3 PB |
6515 | if (IS_ERR(req)) |
6516 | return PTR_ERR(req); | |
47f46768 | 6517 | |
9c8e11b3 PB |
6518 | req->timeout.off = 0; /* noseq */ |
6519 | data = req->async_data; | |
6520 | list_add_tail(&req->timeout.list, &ctx->timeout_list); | |
50c1df2b | 6521 | hrtimer_init(&data->timer, io_timeout_get_clock(data), mode); |
9c8e11b3 PB |
6522 | data->timer.function = io_timeout_fn; |
6523 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); | |
6524 | return 0; | |
47f46768 JA |
6525 | } |
6526 | ||
3529d8c2 JA |
6527 | static int io_timeout_remove_prep(struct io_kiocb *req, |
6528 | const struct io_uring_sqe *sqe) | |
b29472ee | 6529 | { |
9c8e11b3 PB |
6530 | struct io_timeout_rem *tr = &req->timeout_rem; |
6531 | ||
b29472ee JA |
6532 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
6533 | return -EINVAL; | |
61710e43 DA |
6534 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6535 | return -EINVAL; | |
26578cda | 6536 | if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in) |
b29472ee JA |
6537 | return -EINVAL; |
6538 | ||
f1042b6c | 6539 | tr->ltimeout = false; |
9c8e11b3 PB |
6540 | tr->addr = READ_ONCE(sqe->addr); |
6541 | tr->flags = READ_ONCE(sqe->timeout_flags); | |
f1042b6c PB |
6542 | if (tr->flags & IORING_TIMEOUT_UPDATE_MASK) { |
6543 | if (hweight32(tr->flags & IORING_TIMEOUT_CLOCK_MASK) > 1) | |
6544 | return -EINVAL; | |
6545 | if (tr->flags & IORING_LINK_TIMEOUT_UPDATE) | |
6546 | tr->ltimeout = true; | |
6547 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE_MASK|IORING_TIMEOUT_ABS)) | |
9c8e11b3 PB |
6548 | return -EINVAL; |
6549 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) | |
6550 | return -EFAULT; | |
2087009c YB |
6551 | if (tr->ts.tv_sec < 0 || tr->ts.tv_nsec < 0) |
6552 | return -EINVAL; | |
9c8e11b3 PB |
6553 | } else if (tr->flags) { |
6554 | /* timeout removal doesn't support flags */ | |
b29472ee | 6555 | return -EINVAL; |
9c8e11b3 | 6556 | } |
b29472ee | 6557 | |
b29472ee JA |
6558 | return 0; |
6559 | } | |
6560 | ||
8662daec PB |
6561 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
6562 | { | |
6563 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS | |
6564 | : HRTIMER_MODE_REL; | |
6565 | } | |
6566 | ||
11365043 JA |
6567 | /* |
6568 | * Remove or update an existing timeout command | |
6569 | */ | |
61e98203 | 6570 | static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags) |
11365043 | 6571 | { |
9c8e11b3 | 6572 | struct io_timeout_rem *tr = &req->timeout_rem; |
11365043 | 6573 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 | 6574 | int ret; |
11365043 | 6575 | |
ec3c3d0f PB |
6576 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) { |
6577 | spin_lock(&ctx->completion_lock); | |
6578 | spin_lock_irq(&ctx->timeout_lock); | |
9c8e11b3 | 6579 | ret = io_timeout_cancel(ctx, tr->addr); |
ec3c3d0f PB |
6580 | spin_unlock_irq(&ctx->timeout_lock); |
6581 | spin_unlock(&ctx->completion_lock); | |
6582 | } else { | |
f1042b6c PB |
6583 | enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags); |
6584 | ||
ec3c3d0f | 6585 | spin_lock_irq(&ctx->timeout_lock); |
f1042b6c PB |
6586 | if (tr->ltimeout) |
6587 | ret = io_linked_timeout_update(ctx, tr->addr, &tr->ts, mode); | |
6588 | else | |
6589 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode); | |
ec3c3d0f PB |
6590 | spin_unlock_irq(&ctx->timeout_lock); |
6591 | } | |
11365043 | 6592 | |
4e88d6e7 | 6593 | if (ret < 0) |
93d2bcd2 | 6594 | req_set_fail(req); |
505657bc | 6595 | io_req_complete_post(req, ret, 0); |
11365043 | 6596 | return 0; |
5262f567 JA |
6597 | } |
6598 | ||
3529d8c2 | 6599 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 6600 | bool is_timeout_link) |
5262f567 | 6601 | { |
ad8a48ac | 6602 | struct io_timeout_data *data; |
a41525ab | 6603 | unsigned flags; |
56080b02 | 6604 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 6605 | |
ad8a48ac | 6606 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 6607 | return -EINVAL; |
26578cda PB |
6608 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1 || |
6609 | sqe->splice_fd_in) | |
a41525ab | 6610 | return -EINVAL; |
56080b02 | 6611 | if (off && is_timeout_link) |
2d28390a | 6612 | return -EINVAL; |
a41525ab | 6613 | flags = READ_ONCE(sqe->timeout_flags); |
6224590d PB |
6614 | if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK | |
6615 | IORING_TIMEOUT_ETIME_SUCCESS)) | |
50c1df2b JA |
6616 | return -EINVAL; |
6617 | /* more than one clock specified is invalid, obviously */ | |
6618 | if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1) | |
5262f567 | 6619 | return -EINVAL; |
bdf20073 | 6620 | |
ef9dd637 | 6621 | INIT_LIST_HEAD(&req->timeout.list); |
bfe68a22 | 6622 | req->timeout.off = off; |
f18ee4cf PB |
6623 | if (unlikely(off && !req->ctx->off_timeout_used)) |
6624 | req->ctx->off_timeout_used = true; | |
26a61679 | 6625 | |
d6a644a7 PB |
6626 | if (WARN_ON_ONCE(req_has_async_data(req))) |
6627 | return -EFAULT; | |
6628 | if (io_alloc_async_data(req)) | |
26a61679 JA |
6629 | return -ENOMEM; |
6630 | ||
e8c2bc1f | 6631 | data = req->async_data; |
ad8a48ac | 6632 | data->req = req; |
50c1df2b | 6633 | data->flags = flags; |
ad8a48ac JA |
6634 | |
6635 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
6636 | return -EFAULT; |
6637 | ||
f6223ff7 YB |
6638 | if (data->ts.tv_sec < 0 || data->ts.tv_nsec < 0) |
6639 | return -EINVAL; | |
6640 | ||
e677edbc | 6641 | INIT_LIST_HEAD(&req->timeout.list); |
8662daec | 6642 | data->mode = io_translate_timeout_mode(flags); |
50c1df2b | 6643 | hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode); |
b97e736a PB |
6644 | |
6645 | if (is_timeout_link) { | |
6646 | struct io_submit_link *link = &req->ctx->submit_state.link; | |
6647 | ||
6648 | if (!link->head) | |
6649 | return -EINVAL; | |
6650 | if (link->last->opcode == IORING_OP_LINK_TIMEOUT) | |
6651 | return -EINVAL; | |
4d13d1a4 PB |
6652 | req->timeout.head = link->last; |
6653 | link->last->flags |= REQ_F_ARM_LTIMEOUT; | |
b97e736a | 6654 | } |
ad8a48ac JA |
6655 | return 0; |
6656 | } | |
6657 | ||
61e98203 | 6658 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
ad8a48ac | 6659 | { |
ad8a48ac | 6660 | struct io_ring_ctx *ctx = req->ctx; |
e8c2bc1f | 6661 | struct io_timeout_data *data = req->async_data; |
ad8a48ac | 6662 | struct list_head *entry; |
bfe68a22 | 6663 | u32 tail, off = req->timeout.off; |
ad8a48ac | 6664 | |
89850fce | 6665 | spin_lock_irq(&ctx->timeout_lock); |
93bd25bb | 6666 | |
5262f567 JA |
6667 | /* |
6668 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
6669 | * timeout event to be satisfied. If it isn't set, then this is |
6670 | * a pure timeout request, sequence isn't used. | |
5262f567 | 6671 | */ |
8eb7e2d0 | 6672 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
6673 | entry = ctx->timeout_list.prev; |
6674 | goto add; | |
6675 | } | |
5262f567 | 6676 | |
bfe68a22 PB |
6677 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
6678 | req->timeout.target_seq = tail + off; | |
5262f567 | 6679 | |
f010505b MDG |
6680 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
6681 | * This is safe because ->completion_lock is held, and submissions | |
6682 | * and completions are never mixed in the same ->completion_lock section. | |
6683 | */ | |
6684 | ctx->cq_last_tm_flush = tail; | |
6685 | ||
5262f567 JA |
6686 | /* |
6687 | * Insertion sort, ensuring the first entry in the list is always | |
6688 | * the one we need first. | |
6689 | */ | |
5262f567 | 6690 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
6691 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
6692 | timeout.list); | |
5262f567 | 6693 | |
8eb7e2d0 | 6694 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 6695 | continue; |
bfe68a22 PB |
6696 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
6697 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
6698 | break; |
6699 | } | |
93bd25bb | 6700 | add: |
135fcde8 | 6701 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
6702 | data->timer.function = io_timeout_fn; |
6703 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
89850fce | 6704 | spin_unlock_irq(&ctx->timeout_lock); |
5262f567 JA |
6705 | return 0; |
6706 | } | |
5262f567 | 6707 | |
f458dd84 PB |
6708 | struct io_cancel_data { |
6709 | struct io_ring_ctx *ctx; | |
6710 | u64 user_data; | |
6711 | }; | |
6712 | ||
62755e35 JA |
6713 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
6714 | { | |
6715 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
f458dd84 | 6716 | struct io_cancel_data *cd = data; |
62755e35 | 6717 | |
f458dd84 | 6718 | return req->ctx == cd->ctx && req->user_data == cd->user_data; |
62755e35 JA |
6719 | } |
6720 | ||
f458dd84 PB |
6721 | static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data, |
6722 | struct io_ring_ctx *ctx) | |
62755e35 | 6723 | { |
f458dd84 | 6724 | struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, }; |
62755e35 | 6725 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
6726 | int ret = 0; |
6727 | ||
f458dd84 | 6728 | if (!tctx || !tctx->io_wq) |
5aa75ed5 JA |
6729 | return -ENOENT; |
6730 | ||
f458dd84 | 6731 | cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false); |
62755e35 JA |
6732 | switch (cancel_ret) { |
6733 | case IO_WQ_CANCEL_OK: | |
6734 | ret = 0; | |
6735 | break; | |
6736 | case IO_WQ_CANCEL_RUNNING: | |
6737 | ret = -EALREADY; | |
6738 | break; | |
6739 | case IO_WQ_CANCEL_NOTFOUND: | |
6740 | ret = -ENOENT; | |
6741 | break; | |
6742 | } | |
6743 | ||
e977d6d3 JA |
6744 | return ret; |
6745 | } | |
6746 | ||
8cb01fac | 6747 | static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr) |
47f46768 | 6748 | { |
8cb01fac | 6749 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 JA |
6750 | int ret; |
6751 | ||
dadebc35 | 6752 | WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current); |
8cb01fac | 6753 | |
f458dd84 | 6754 | ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx); |
ccbf7261 JA |
6755 | /* |
6756 | * Fall-through even for -EALREADY, as we may have poll armed | |
6757 | * that need unarming. | |
6758 | */ | |
6759 | if (!ret) | |
6760 | return 0; | |
505657bc PB |
6761 | |
6762 | spin_lock(&ctx->completion_lock); | |
ccbf7261 JA |
6763 | ret = io_poll_cancel(ctx, sqe_addr, false); |
6764 | if (ret != -ENOENT) | |
6765 | goto out; | |
6766 | ||
79ebeaee | 6767 | spin_lock_irq(&ctx->timeout_lock); |
47f46768 | 6768 | ret = io_timeout_cancel(ctx, sqe_addr); |
79ebeaee | 6769 | spin_unlock_irq(&ctx->timeout_lock); |
505657bc PB |
6770 | out: |
6771 | spin_unlock(&ctx->completion_lock); | |
6772 | return ret; | |
47f46768 JA |
6773 | } |
6774 | ||
3529d8c2 JA |
6775 | static int io_async_cancel_prep(struct io_kiocb *req, |
6776 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 6777 | { |
fbf23849 | 6778 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 6779 | return -EINVAL; |
61710e43 DA |
6780 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6781 | return -EINVAL; | |
26578cda PB |
6782 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags || |
6783 | sqe->splice_fd_in) | |
e977d6d3 JA |
6784 | return -EINVAL; |
6785 | ||
fbf23849 JA |
6786 | req->cancel.addr = READ_ONCE(sqe->addr); |
6787 | return 0; | |
6788 | } | |
6789 | ||
61e98203 | 6790 | static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6791 | { |
6792 | struct io_ring_ctx *ctx = req->ctx; | |
58f99373 PB |
6793 | u64 sqe_addr = req->cancel.addr; |
6794 | struct io_tctx_node *node; | |
6795 | int ret; | |
6796 | ||
8cb01fac | 6797 | ret = io_try_cancel_userdata(req, sqe_addr); |
58f99373 PB |
6798 | if (ret != -ENOENT) |
6799 | goto done; | |
58f99373 PB |
6800 | |
6801 | /* slow path, try all io-wq's */ | |
f8929630 | 6802 | io_ring_submit_lock(ctx, issue_flags); |
58f99373 PB |
6803 | ret = -ENOENT; |
6804 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
6805 | struct io_uring_task *tctx = node->task->io_uring; | |
fbf23849 | 6806 | |
58f99373 PB |
6807 | ret = io_async_cancel_one(tctx, req->cancel.addr, ctx); |
6808 | if (ret != -ENOENT) | |
6809 | break; | |
6810 | } | |
f8929630 | 6811 | io_ring_submit_unlock(ctx, issue_flags); |
58f99373 | 6812 | done: |
58f99373 | 6813 | if (ret < 0) |
93d2bcd2 | 6814 | req_set_fail(req); |
505657bc | 6815 | io_req_complete_post(req, ret, 0); |
5262f567 JA |
6816 | return 0; |
6817 | } | |
6818 | ||
269bbe5f | 6819 | static int io_rsrc_update_prep(struct io_kiocb *req, |
05f3fb3c JA |
6820 | const struct io_uring_sqe *sqe) |
6821 | { | |
61710e43 DA |
6822 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6823 | return -EINVAL; | |
26578cda | 6824 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
05f3fb3c JA |
6825 | return -EINVAL; |
6826 | ||
269bbe5f BM |
6827 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
6828 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); | |
6829 | if (!req->rsrc_update.nr_args) | |
05f3fb3c | 6830 | return -EINVAL; |
269bbe5f | 6831 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
05f3fb3c JA |
6832 | return 0; |
6833 | } | |
6834 | ||
889fca73 | 6835 | static int io_files_update(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6836 | { |
6837 | struct io_ring_ctx *ctx = req->ctx; | |
c3bdad02 | 6838 | struct io_uring_rsrc_update2 up; |
05f3fb3c | 6839 | int ret; |
fbf23849 | 6840 | |
269bbe5f BM |
6841 | up.offset = req->rsrc_update.offset; |
6842 | up.data = req->rsrc_update.arg; | |
c3bdad02 PB |
6843 | up.nr = 0; |
6844 | up.tags = 0; | |
615cee49 | 6845 | up.resv = 0; |
d8a3ba9c | 6846 | up.resv2 = 0; |
05f3fb3c | 6847 | |
f8929630 | 6848 | io_ring_submit_lock(ctx, issue_flags); |
fdecb662 | 6849 | ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE, |
98f0b3b4 | 6850 | &up, req->rsrc_update.nr_args); |
f8929630 | 6851 | io_ring_submit_unlock(ctx, issue_flags); |
05f3fb3c JA |
6852 | |
6853 | if (ret < 0) | |
93d2bcd2 | 6854 | req_set_fail(req); |
889fca73 | 6855 | __io_req_complete(req, issue_flags, ret, 0); |
5262f567 JA |
6856 | return 0; |
6857 | } | |
6858 | ||
bfe76559 | 6859 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 6860 | { |
d625c6ee | 6861 | switch (req->opcode) { |
e781573e | 6862 | case IORING_OP_NOP: |
bfe76559 | 6863 | return 0; |
f67676d1 JA |
6864 | case IORING_OP_READV: |
6865 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 6866 | case IORING_OP_READ: |
f67676d1 JA |
6867 | case IORING_OP_WRITEV: |
6868 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 6869 | case IORING_OP_WRITE: |
584b0180 | 6870 | return io_prep_rw(req, sqe); |
0969e783 | 6871 | case IORING_OP_POLL_ADD: |
bfe76559 | 6872 | return io_poll_add_prep(req, sqe); |
0969e783 | 6873 | case IORING_OP_POLL_REMOVE: |
c5de0036 | 6874 | return io_poll_update_prep(req, sqe); |
8ed8d3c3 | 6875 | case IORING_OP_FSYNC: |
1155c76a | 6876 | return io_fsync_prep(req, sqe); |
8ed8d3c3 | 6877 | case IORING_OP_SYNC_FILE_RANGE: |
1155c76a | 6878 | return io_sfr_prep(req, sqe); |
03b1230c | 6879 | case IORING_OP_SENDMSG: |
fddaface | 6880 | case IORING_OP_SEND: |
bfe76559 | 6881 | return io_sendmsg_prep(req, sqe); |
03b1230c | 6882 | case IORING_OP_RECVMSG: |
fddaface | 6883 | case IORING_OP_RECV: |
bfe76559 | 6884 | return io_recvmsg_prep(req, sqe); |
f499a021 | 6885 | case IORING_OP_CONNECT: |
bfe76559 | 6886 | return io_connect_prep(req, sqe); |
2d28390a | 6887 | case IORING_OP_TIMEOUT: |
bfe76559 | 6888 | return io_timeout_prep(req, sqe, false); |
b29472ee | 6889 | case IORING_OP_TIMEOUT_REMOVE: |
bfe76559 | 6890 | return io_timeout_remove_prep(req, sqe); |
fbf23849 | 6891 | case IORING_OP_ASYNC_CANCEL: |
bfe76559 | 6892 | return io_async_cancel_prep(req, sqe); |
2d28390a | 6893 | case IORING_OP_LINK_TIMEOUT: |
bfe76559 | 6894 | return io_timeout_prep(req, sqe, true); |
8ed8d3c3 | 6895 | case IORING_OP_ACCEPT: |
bfe76559 | 6896 | return io_accept_prep(req, sqe); |
d63d1b5e | 6897 | case IORING_OP_FALLOCATE: |
bfe76559 | 6898 | return io_fallocate_prep(req, sqe); |
15b71abe | 6899 | case IORING_OP_OPENAT: |
bfe76559 | 6900 | return io_openat_prep(req, sqe); |
b5dba59e | 6901 | case IORING_OP_CLOSE: |
bfe76559 | 6902 | return io_close_prep(req, sqe); |
05f3fb3c | 6903 | case IORING_OP_FILES_UPDATE: |
269bbe5f | 6904 | return io_rsrc_update_prep(req, sqe); |
eddc7ef5 | 6905 | case IORING_OP_STATX: |
bfe76559 | 6906 | return io_statx_prep(req, sqe); |
4840e418 | 6907 | case IORING_OP_FADVISE: |
bfe76559 | 6908 | return io_fadvise_prep(req, sqe); |
c1ca757b | 6909 | case IORING_OP_MADVISE: |
bfe76559 | 6910 | return io_madvise_prep(req, sqe); |
cebdb986 | 6911 | case IORING_OP_OPENAT2: |
bfe76559 | 6912 | return io_openat2_prep(req, sqe); |
3e4827b0 | 6913 | case IORING_OP_EPOLL_CTL: |
bfe76559 | 6914 | return io_epoll_ctl_prep(req, sqe); |
7d67af2c | 6915 | case IORING_OP_SPLICE: |
bfe76559 | 6916 | return io_splice_prep(req, sqe); |
ddf0322d | 6917 | case IORING_OP_PROVIDE_BUFFERS: |
bfe76559 | 6918 | return io_provide_buffers_prep(req, sqe); |
067524e9 | 6919 | case IORING_OP_REMOVE_BUFFERS: |
bfe76559 | 6920 | return io_remove_buffers_prep(req, sqe); |
f2a8d5c7 | 6921 | case IORING_OP_TEE: |
bfe76559 | 6922 | return io_tee_prep(req, sqe); |
36f4fa68 JA |
6923 | case IORING_OP_SHUTDOWN: |
6924 | return io_shutdown_prep(req, sqe); | |
80a261fd JA |
6925 | case IORING_OP_RENAMEAT: |
6926 | return io_renameat_prep(req, sqe); | |
14a1143b JA |
6927 | case IORING_OP_UNLINKAT: |
6928 | return io_unlinkat_prep(req, sqe); | |
e34a02dc DK |
6929 | case IORING_OP_MKDIRAT: |
6930 | return io_mkdirat_prep(req, sqe); | |
7a8721f8 DK |
6931 | case IORING_OP_SYMLINKAT: |
6932 | return io_symlinkat_prep(req, sqe); | |
cf30da90 DK |
6933 | case IORING_OP_LINKAT: |
6934 | return io_linkat_prep(req, sqe); | |
4f57f06c JA |
6935 | case IORING_OP_MSG_RING: |
6936 | return io_msg_ring_prep(req, sqe); | |
f67676d1 JA |
6937 | } |
6938 | ||
bfe76559 PB |
6939 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
6940 | req->opcode); | |
bd54b6fe | 6941 | return -EINVAL; |
bfe76559 PB |
6942 | } |
6943 | ||
93642ef8 | 6944 | static int io_req_prep_async(struct io_kiocb *req) |
bfe76559 | 6945 | { |
b7e298d2 PB |
6946 | if (!io_op_defs[req->opcode].needs_async_setup) |
6947 | return 0; | |
d886e185 | 6948 | if (WARN_ON_ONCE(req_has_async_data(req))) |
b7e298d2 PB |
6949 | return -EFAULT; |
6950 | if (io_alloc_async_data(req)) | |
6951 | return -EAGAIN; | |
6952 | ||
93642ef8 PB |
6953 | switch (req->opcode) { |
6954 | case IORING_OP_READV: | |
93642ef8 PB |
6955 | return io_rw_prep_async(req, READ); |
6956 | case IORING_OP_WRITEV: | |
93642ef8 PB |
6957 | return io_rw_prep_async(req, WRITE); |
6958 | case IORING_OP_SENDMSG: | |
93642ef8 PB |
6959 | return io_sendmsg_prep_async(req); |
6960 | case IORING_OP_RECVMSG: | |
93642ef8 PB |
6961 | return io_recvmsg_prep_async(req); |
6962 | case IORING_OP_CONNECT: | |
6963 | return io_connect_prep_async(req); | |
6964 | } | |
b7e298d2 PB |
6965 | printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n", |
6966 | req->opcode); | |
6967 | return -EFAULT; | |
f67676d1 JA |
6968 | } |
6969 | ||
9cf7c104 PB |
6970 | static u32 io_get_sequence(struct io_kiocb *req) |
6971 | { | |
a3dbdf54 | 6972 | u32 seq = req->ctx->cached_sq_head; |
963c6abb | 6973 | struct io_kiocb *cur; |
9cf7c104 | 6974 | |
a3dbdf54 | 6975 | /* need original cached_sq_head, but it was increased for each req */ |
963c6abb | 6976 | io_for_each_link(cur, req) |
a3dbdf54 PB |
6977 | seq--; |
6978 | return seq; | |
9cf7c104 PB |
6979 | } |
6980 | ||
c072481d | 6981 | static __cold void io_drain_req(struct io_kiocb *req) |
de0617e4 | 6982 | { |
a197f664 | 6983 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 6984 | struct io_defer_entry *de; |
f67676d1 | 6985 | int ret; |
e0eb71dc | 6986 | u32 seq = io_get_sequence(req); |
3c19966d | 6987 | |
9d858b21 | 6988 | /* Still need defer if there is pending req in defer list. */ |
e302f104 | 6989 | spin_lock(&ctx->completion_lock); |
5e371265 | 6990 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) { |
e302f104 | 6991 | spin_unlock(&ctx->completion_lock); |
e0eb71dc | 6992 | queue: |
10c66904 | 6993 | ctx->drain_active = false; |
e0eb71dc PB |
6994 | io_req_task_queue(req); |
6995 | return; | |
10c66904 | 6996 | } |
e302f104 | 6997 | spin_unlock(&ctx->completion_lock); |
9cf7c104 | 6998 | |
b7e298d2 | 6999 | ret = io_req_prep_async(req); |
e0eb71dc PB |
7000 | if (ret) { |
7001 | fail: | |
7002 | io_req_complete_failed(req, ret); | |
7003 | return; | |
7004 | } | |
cbdcb435 | 7005 | io_prep_async_link(req); |
27dc8338 | 7006 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
76cc33d7 | 7007 | if (!de) { |
1b48773f | 7008 | ret = -ENOMEM; |
e0eb71dc | 7009 | goto fail; |
76cc33d7 | 7010 | } |
2d28390a | 7011 | |
79ebeaee | 7012 | spin_lock(&ctx->completion_lock); |
9cf7c104 | 7013 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
79ebeaee | 7014 | spin_unlock(&ctx->completion_lock); |
27dc8338 | 7015 | kfree(de); |
e0eb71dc | 7016 | goto queue; |
de0617e4 JA |
7017 | } |
7018 | ||
502c87d6 | 7019 | trace_io_uring_defer(ctx, req, req->user_data, req->opcode); |
27dc8338 | 7020 | de->req = req; |
9cf7c104 | 7021 | de->seq = seq; |
27dc8338 | 7022 | list_add_tail(&de->list, &ctx->defer_list); |
79ebeaee | 7023 | spin_unlock(&ctx->completion_lock); |
de0617e4 JA |
7024 | } |
7025 | ||
68fb8979 | 7026 | static void io_clean_op(struct io_kiocb *req) |
99bc4c38 | 7027 | { |
8197b053 PB |
7028 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
7029 | spin_lock(&req->ctx->completion_lock); | |
cc3cec83 | 7030 | io_put_kbuf_comp(req); |
8197b053 PB |
7031 | spin_unlock(&req->ctx->completion_lock); |
7032 | } | |
99bc4c38 | 7033 | |
0e1b6fe3 PB |
7034 | if (req->flags & REQ_F_NEED_CLEANUP) { |
7035 | switch (req->opcode) { | |
7036 | case IORING_OP_READV: | |
7037 | case IORING_OP_READ_FIXED: | |
7038 | case IORING_OP_READ: | |
7039 | case IORING_OP_WRITEV: | |
7040 | case IORING_OP_WRITE_FIXED: | |
e8c2bc1f JA |
7041 | case IORING_OP_WRITE: { |
7042 | struct io_async_rw *io = req->async_data; | |
1dacb4df PB |
7043 | |
7044 | kfree(io->free_iovec); | |
0e1b6fe3 | 7045 | break; |
e8c2bc1f | 7046 | } |
0e1b6fe3 | 7047 | case IORING_OP_RECVMSG: |
e8c2bc1f JA |
7048 | case IORING_OP_SENDMSG: { |
7049 | struct io_async_msghdr *io = req->async_data; | |
257e84a5 PB |
7050 | |
7051 | kfree(io->free_iov); | |
0e1b6fe3 | 7052 | break; |
e8c2bc1f | 7053 | } |
f3cd4850 JA |
7054 | case IORING_OP_OPENAT: |
7055 | case IORING_OP_OPENAT2: | |
7056 | if (req->open.filename) | |
7057 | putname(req->open.filename); | |
7058 | break; | |
80a261fd JA |
7059 | case IORING_OP_RENAMEAT: |
7060 | putname(req->rename.oldpath); | |
7061 | putname(req->rename.newpath); | |
7062 | break; | |
14a1143b JA |
7063 | case IORING_OP_UNLINKAT: |
7064 | putname(req->unlink.filename); | |
7065 | break; | |
e34a02dc DK |
7066 | case IORING_OP_MKDIRAT: |
7067 | putname(req->mkdir.filename); | |
7068 | break; | |
7a8721f8 DK |
7069 | case IORING_OP_SYMLINKAT: |
7070 | putname(req->symlink.oldpath); | |
7071 | putname(req->symlink.newpath); | |
7072 | break; | |
cf30da90 DK |
7073 | case IORING_OP_LINKAT: |
7074 | putname(req->hardlink.oldpath); | |
7075 | putname(req->hardlink.newpath); | |
7076 | break; | |
1b6fe6e0 SR |
7077 | case IORING_OP_STATX: |
7078 | if (req->statx.filename) | |
7079 | putname(req->statx.filename); | |
7080 | break; | |
0e1b6fe3 | 7081 | } |
99bc4c38 | 7082 | } |
75652a30 JA |
7083 | if ((req->flags & REQ_F_POLLED) && req->apoll) { |
7084 | kfree(req->apoll->double_poll); | |
7085 | kfree(req->apoll); | |
7086 | req->apoll = NULL; | |
7087 | } | |
c854357b | 7088 | if (req->flags & REQ_F_CREDS) |
b8e64b53 | 7089 | put_cred(req->creds); |
d886e185 PB |
7090 | if (req->flags & REQ_F_ASYNC_DATA) { |
7091 | kfree(req->async_data); | |
7092 | req->async_data = NULL; | |
7093 | } | |
c854357b | 7094 | req->flags &= ~IO_REQ_CLEAN_FLAGS; |
99bc4c38 PB |
7095 | } |
7096 | ||
6bf9c47a JA |
7097 | static bool io_assign_file(struct io_kiocb *req, unsigned int issue_flags) |
7098 | { | |
7099 | if (req->file || !io_op_defs[req->opcode].needs_file) | |
7100 | return true; | |
7101 | ||
7102 | if (req->flags & REQ_F_FIXED_FILE) | |
82733d16 | 7103 | req->file = io_file_get_fixed(req, req->fd, issue_flags); |
6bf9c47a | 7104 | else |
82733d16 | 7105 | req->file = io_file_get_normal(req, req->fd); |
6bf9c47a JA |
7106 | if (req->file) |
7107 | return true; | |
7108 | ||
7109 | req_set_fail(req); | |
7110 | req->result = -EBADF; | |
7111 | return false; | |
7112 | } | |
7113 | ||
889fca73 | 7114 | static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 7115 | { |
5730b27e | 7116 | const struct cred *creds = NULL; |
d625c6ee | 7117 | int ret; |
2b188cc1 | 7118 | |
70152140 JA |
7119 | if (unlikely(!io_assign_file(req, issue_flags))) |
7120 | return -EBADF; | |
7121 | ||
6878b40e | 7122 | if (unlikely((req->flags & REQ_F_CREDS) && req->creds != current_cred())) |
c10d1f98 | 7123 | creds = override_creds(req->creds); |
5730b27e | 7124 | |
5bd2182d PM |
7125 | if (!io_op_defs[req->opcode].audit_skip) |
7126 | audit_uring_entry(req->opcode); | |
7127 | ||
d625c6ee | 7128 | switch (req->opcode) { |
2b188cc1 | 7129 | case IORING_OP_NOP: |
889fca73 | 7130 | ret = io_nop(req, issue_flags); |
2b188cc1 JA |
7131 | break; |
7132 | case IORING_OP_READV: | |
edafccee | 7133 | case IORING_OP_READ_FIXED: |
3a6820f2 | 7134 | case IORING_OP_READ: |
889fca73 | 7135 | ret = io_read(req, issue_flags); |
edafccee | 7136 | break; |
3529d8c2 | 7137 | case IORING_OP_WRITEV: |
edafccee | 7138 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 7139 | case IORING_OP_WRITE: |
889fca73 | 7140 | ret = io_write(req, issue_flags); |
2b188cc1 | 7141 | break; |
c992fe29 | 7142 | case IORING_OP_FSYNC: |
45d189c6 | 7143 | ret = io_fsync(req, issue_flags); |
c992fe29 | 7144 | break; |
221c5eb2 | 7145 | case IORING_OP_POLL_ADD: |
61e98203 | 7146 | ret = io_poll_add(req, issue_flags); |
221c5eb2 JA |
7147 | break; |
7148 | case IORING_OP_POLL_REMOVE: | |
c5de0036 | 7149 | ret = io_poll_update(req, issue_flags); |
221c5eb2 | 7150 | break; |
5d17b4a4 | 7151 | case IORING_OP_SYNC_FILE_RANGE: |
45d189c6 | 7152 | ret = io_sync_file_range(req, issue_flags); |
5d17b4a4 | 7153 | break; |
0fa03c62 | 7154 | case IORING_OP_SENDMSG: |
889fca73 | 7155 | ret = io_sendmsg(req, issue_flags); |
062d04d7 | 7156 | break; |
fddaface | 7157 | case IORING_OP_SEND: |
889fca73 | 7158 | ret = io_send(req, issue_flags); |
0fa03c62 | 7159 | break; |
aa1fa28f | 7160 | case IORING_OP_RECVMSG: |
889fca73 | 7161 | ret = io_recvmsg(req, issue_flags); |
062d04d7 | 7162 | break; |
fddaface | 7163 | case IORING_OP_RECV: |
889fca73 | 7164 | ret = io_recv(req, issue_flags); |
aa1fa28f | 7165 | break; |
5262f567 | 7166 | case IORING_OP_TIMEOUT: |
61e98203 | 7167 | ret = io_timeout(req, issue_flags); |
5262f567 | 7168 | break; |
11365043 | 7169 | case IORING_OP_TIMEOUT_REMOVE: |
61e98203 | 7170 | ret = io_timeout_remove(req, issue_flags); |
11365043 | 7171 | break; |
17f2fe35 | 7172 | case IORING_OP_ACCEPT: |
889fca73 | 7173 | ret = io_accept(req, issue_flags); |
17f2fe35 | 7174 | break; |
f8e85cf2 | 7175 | case IORING_OP_CONNECT: |
889fca73 | 7176 | ret = io_connect(req, issue_flags); |
f8e85cf2 | 7177 | break; |
62755e35 | 7178 | case IORING_OP_ASYNC_CANCEL: |
61e98203 | 7179 | ret = io_async_cancel(req, issue_flags); |
62755e35 | 7180 | break; |
d63d1b5e | 7181 | case IORING_OP_FALLOCATE: |
45d189c6 | 7182 | ret = io_fallocate(req, issue_flags); |
d63d1b5e | 7183 | break; |
15b71abe | 7184 | case IORING_OP_OPENAT: |
45d189c6 | 7185 | ret = io_openat(req, issue_flags); |
15b71abe | 7186 | break; |
b5dba59e | 7187 | case IORING_OP_CLOSE: |
889fca73 | 7188 | ret = io_close(req, issue_flags); |
b5dba59e | 7189 | break; |
05f3fb3c | 7190 | case IORING_OP_FILES_UPDATE: |
889fca73 | 7191 | ret = io_files_update(req, issue_flags); |
05f3fb3c | 7192 | break; |
eddc7ef5 | 7193 | case IORING_OP_STATX: |
45d189c6 | 7194 | ret = io_statx(req, issue_flags); |
eddc7ef5 | 7195 | break; |
4840e418 | 7196 | case IORING_OP_FADVISE: |
45d189c6 | 7197 | ret = io_fadvise(req, issue_flags); |
4840e418 | 7198 | break; |
c1ca757b | 7199 | case IORING_OP_MADVISE: |
45d189c6 | 7200 | ret = io_madvise(req, issue_flags); |
c1ca757b | 7201 | break; |
cebdb986 | 7202 | case IORING_OP_OPENAT2: |
45d189c6 | 7203 | ret = io_openat2(req, issue_flags); |
cebdb986 | 7204 | break; |
3e4827b0 | 7205 | case IORING_OP_EPOLL_CTL: |
889fca73 | 7206 | ret = io_epoll_ctl(req, issue_flags); |
3e4827b0 | 7207 | break; |
7d67af2c | 7208 | case IORING_OP_SPLICE: |
45d189c6 | 7209 | ret = io_splice(req, issue_flags); |
7d67af2c | 7210 | break; |
ddf0322d | 7211 | case IORING_OP_PROVIDE_BUFFERS: |
889fca73 | 7212 | ret = io_provide_buffers(req, issue_flags); |
ddf0322d | 7213 | break; |
067524e9 | 7214 | case IORING_OP_REMOVE_BUFFERS: |
889fca73 | 7215 | ret = io_remove_buffers(req, issue_flags); |
3e4827b0 | 7216 | break; |
f2a8d5c7 | 7217 | case IORING_OP_TEE: |
45d189c6 | 7218 | ret = io_tee(req, issue_flags); |
f2a8d5c7 | 7219 | break; |
36f4fa68 | 7220 | case IORING_OP_SHUTDOWN: |
45d189c6 | 7221 | ret = io_shutdown(req, issue_flags); |
36f4fa68 | 7222 | break; |
80a261fd | 7223 | case IORING_OP_RENAMEAT: |
45d189c6 | 7224 | ret = io_renameat(req, issue_flags); |
80a261fd | 7225 | break; |
14a1143b | 7226 | case IORING_OP_UNLINKAT: |
45d189c6 | 7227 | ret = io_unlinkat(req, issue_flags); |
14a1143b | 7228 | break; |
e34a02dc DK |
7229 | case IORING_OP_MKDIRAT: |
7230 | ret = io_mkdirat(req, issue_flags); | |
7231 | break; | |
7a8721f8 DK |
7232 | case IORING_OP_SYMLINKAT: |
7233 | ret = io_symlinkat(req, issue_flags); | |
7234 | break; | |
cf30da90 DK |
7235 | case IORING_OP_LINKAT: |
7236 | ret = io_linkat(req, issue_flags); | |
7237 | break; | |
4f57f06c JA |
7238 | case IORING_OP_MSG_RING: |
7239 | ret = io_msg_ring(req, issue_flags); | |
7240 | break; | |
2b188cc1 JA |
7241 | default: |
7242 | ret = -EINVAL; | |
7243 | break; | |
7244 | } | |
7245 | ||
5bd2182d PM |
7246 | if (!io_op_defs[req->opcode].audit_skip) |
7247 | audit_uring_exit(!ret, ret); | |
7248 | ||
5730b27e JA |
7249 | if (creds) |
7250 | revert_creds(creds); | |
def596e9 JA |
7251 | if (ret) |
7252 | return ret; | |
b532576e | 7253 | /* If the op doesn't have a file, we're not polling for it */ |
9983028e | 7254 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && req->file) |
9882131c | 7255 | io_iopoll_req_issued(req, issue_flags); |
def596e9 JA |
7256 | |
7257 | return 0; | |
2b188cc1 JA |
7258 | } |
7259 | ||
ebc11b6c PB |
7260 | static struct io_wq_work *io_wq_free_work(struct io_wq_work *work) |
7261 | { | |
7262 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
7263 | ||
7264 | req = io_put_req_find_next(req); | |
7265 | return req ? &req->work : NULL; | |
7266 | } | |
7267 | ||
5280f7e5 | 7268 | static void io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
7269 | { |
7270 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6bf9c47a | 7271 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
d01905db PB |
7272 | unsigned int issue_flags = IO_URING_F_UNLOCKED; |
7273 | bool needs_poll = false; | |
6df1db6b | 7274 | struct io_kiocb *timeout; |
6bf9c47a | 7275 | int ret = 0, err = -ECANCELED; |
2b188cc1 | 7276 | |
48dcd38d PB |
7277 | /* one will be dropped by ->io_free_work() after returning to io-wq */ |
7278 | if (!(req->flags & REQ_F_REFCOUNT)) | |
7279 | __io_req_set_refcount(req, 2); | |
7280 | else | |
7281 | req_ref_get(req); | |
5d5901a3 | 7282 | |
6df1db6b PB |
7283 | timeout = io_prep_linked_timeout(req); |
7284 | if (timeout) | |
7285 | io_queue_linked_timeout(timeout); | |
d4c81f38 | 7286 | |
6bf9c47a | 7287 | |
dadebc35 | 7288 | /* either cancelled or io-wq is dying, so don't touch tctx->iowq */ |
d01905db | 7289 | if (work->flags & IO_WQ_WORK_CANCEL) { |
0f8da75b | 7290 | fail: |
6bf9c47a | 7291 | io_req_task_queue_fail(req, err); |
d01905db PB |
7292 | return; |
7293 | } | |
0f8da75b PB |
7294 | if (!io_assign_file(req, issue_flags)) { |
7295 | err = -EBADF; | |
7296 | work->flags |= IO_WQ_WORK_CANCEL; | |
7297 | goto fail; | |
7298 | } | |
31b51510 | 7299 | |
d01905db | 7300 | if (req->flags & REQ_F_FORCE_ASYNC) { |
afb7f56f PB |
7301 | bool opcode_poll = def->pollin || def->pollout; |
7302 | ||
7303 | if (opcode_poll && file_can_poll(req->file)) { | |
7304 | needs_poll = true; | |
d01905db | 7305 | issue_flags |= IO_URING_F_NONBLOCK; |
afb7f56f | 7306 | } |
561fb04a | 7307 | } |
31b51510 | 7308 | |
d01905db PB |
7309 | do { |
7310 | ret = io_issue_sqe(req, issue_flags); | |
7311 | if (ret != -EAGAIN) | |
7312 | break; | |
7313 | /* | |
7314 | * We can get EAGAIN for iopolled IO even though we're | |
7315 | * forcing a sync submission from here, since we can't | |
7316 | * wait for request slots on the block side. | |
7317 | */ | |
7318 | if (!needs_poll) { | |
7319 | cond_resched(); | |
7320 | continue; | |
90fa0288 HX |
7321 | } |
7322 | ||
4d9237e3 | 7323 | if (io_arm_poll_handler(req, issue_flags) == IO_APOLL_OK) |
d01905db PB |
7324 | return; |
7325 | /* aborted or ready, in either case retry blocking */ | |
7326 | needs_poll = false; | |
7327 | issue_flags &= ~IO_URING_F_NONBLOCK; | |
7328 | } while (1); | |
31b51510 | 7329 | |
a3df7698 | 7330 | /* avoid locking problems by failing it from a clean context */ |
5d5901a3 | 7331 | if (ret) |
a3df7698 | 7332 | io_req_task_queue_fail(req, ret); |
2b188cc1 JA |
7333 | } |
7334 | ||
aeca241b | 7335 | static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table, |
042b0d85 | 7336 | unsigned i) |
65e19f54 | 7337 | { |
042b0d85 | 7338 | return &table->files[i]; |
dafecf19 PB |
7339 | } |
7340 | ||
65e19f54 JA |
7341 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
7342 | int index) | |
7343 | { | |
aeca241b | 7344 | struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index); |
65e19f54 | 7345 | |
a04b0ac0 | 7346 | return (struct file *) (slot->file_ptr & FFS_MASK); |
65e19f54 JA |
7347 | } |
7348 | ||
a04b0ac0 | 7349 | static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file) |
9a321c98 PB |
7350 | { |
7351 | unsigned long file_ptr = (unsigned long) file; | |
7352 | ||
88459b50 | 7353 | file_ptr |= io_file_get_flags(file); |
a04b0ac0 | 7354 | file_slot->file_ptr = file_ptr; |
65e19f54 JA |
7355 | } |
7356 | ||
5106dd6e JA |
7357 | static inline struct file *io_file_get_fixed(struct io_kiocb *req, int fd, |
7358 | unsigned int issue_flags) | |
09bb8394 | 7359 | { |
5106dd6e JA |
7360 | struct io_ring_ctx *ctx = req->ctx; |
7361 | struct file *file = NULL; | |
ac177053 | 7362 | unsigned long file_ptr; |
09bb8394 | 7363 | |
5106dd6e JA |
7364 | if (issue_flags & IO_URING_F_UNLOCKED) |
7365 | mutex_lock(&ctx->uring_lock); | |
7366 | ||
ac177053 | 7367 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
5106dd6e | 7368 | goto out; |
ac177053 PB |
7369 | fd = array_index_nospec(fd, ctx->nr_user_files); |
7370 | file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr; | |
7371 | file = (struct file *) (file_ptr & FFS_MASK); | |
7372 | file_ptr &= ~FFS_MASK; | |
7373 | /* mask in overlapping REQ_F and FFS bits */ | |
35645ac3 | 7374 | req->flags |= (file_ptr << REQ_F_SUPPORT_NOWAIT_BIT); |
5106dd6e JA |
7375 | io_req_set_rsrc_node(req, ctx, 0); |
7376 | out: | |
7377 | if (issue_flags & IO_URING_F_UNLOCKED) | |
7378 | mutex_unlock(&ctx->uring_lock); | |
ac177053 PB |
7379 | return file; |
7380 | } | |
d44f554e | 7381 | |
d5361233 JA |
7382 | /* |
7383 | * Drop the file for requeue operations. Only used of req->file is the | |
7384 | * io_uring descriptor itself. | |
7385 | */ | |
7386 | static void io_drop_inflight_file(struct io_kiocb *req) | |
7387 | { | |
7388 | if (unlikely(req->flags & REQ_F_INFLIGHT)) { | |
7389 | fput(req->file); | |
7390 | req->file = NULL; | |
7391 | req->flags &= ~REQ_F_INFLIGHT; | |
7392 | } | |
7393 | } | |
7394 | ||
5106dd6e | 7395 | static struct file *io_file_get_normal(struct io_kiocb *req, int fd) |
ac177053 | 7396 | { |
62906e89 | 7397 | struct file *file = fget(fd); |
ac177053 | 7398 | |
5106dd6e | 7399 | trace_io_uring_file_get(req->ctx, req, req->user_data, fd); |
09bb8394 | 7400 | |
ac177053 | 7401 | /* we don't allow fixed io_uring files */ |
d5361233 JA |
7402 | if (file && file->f_op == &io_uring_fops) |
7403 | req->flags |= REQ_F_INFLIGHT; | |
8371adf5 | 7404 | return file; |
09bb8394 JA |
7405 | } |
7406 | ||
f237c30a | 7407 | static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked) |
89b263f6 JA |
7408 | { |
7409 | struct io_kiocb *prev = req->timeout.prev; | |
617a8948 | 7410 | int ret = -ENOENT; |
89b263f6 JA |
7411 | |
7412 | if (prev) { | |
617a8948 PB |
7413 | if (!(req->task->flags & PF_EXITING)) |
7414 | ret = io_try_cancel_userdata(req, prev->user_data); | |
505657bc | 7415 | io_req_complete_post(req, ret ?: -ETIME, 0); |
89b263f6 | 7416 | io_put_req(prev); |
89b263f6 JA |
7417 | } else { |
7418 | io_req_complete_post(req, -ETIME, 0); | |
7419 | } | |
7420 | } | |
7421 | ||
2665abfd | 7422 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 7423 | { |
ad8a48ac JA |
7424 | struct io_timeout_data *data = container_of(timer, |
7425 | struct io_timeout_data, timer); | |
90cd7e42 | 7426 | struct io_kiocb *prev, *req = data->req; |
2665abfd | 7427 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 7428 | unsigned long flags; |
2665abfd | 7429 | |
89b263f6 | 7430 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
90cd7e42 PB |
7431 | prev = req->timeout.head; |
7432 | req->timeout.head = NULL; | |
2665abfd JA |
7433 | |
7434 | /* | |
7435 | * We don't expect the list to be empty, that will only happen if we | |
7436 | * race with the completion of the linked work. | |
7437 | */ | |
447c19f3 | 7438 | if (prev) { |
f2f87370 | 7439 | io_remove_next_linked(prev); |
447c19f3 PB |
7440 | if (!req_ref_inc_not_zero(prev)) |
7441 | prev = NULL; | |
7442 | } | |
ef9dd637 | 7443 | list_del(&req->timeout.list); |
89b263f6 JA |
7444 | req->timeout.prev = prev; |
7445 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); | |
2665abfd | 7446 | |
89b263f6 | 7447 | req->io_task_work.func = io_req_task_link_timeout; |
4813c377 | 7448 | io_req_task_work_add(req, false); |
2665abfd JA |
7449 | return HRTIMER_NORESTART; |
7450 | } | |
7451 | ||
de968c18 | 7452 | static void io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 7453 | { |
de968c18 PB |
7454 | struct io_ring_ctx *ctx = req->ctx; |
7455 | ||
89b263f6 | 7456 | spin_lock_irq(&ctx->timeout_lock); |
76a46e06 | 7457 | /* |
f2f87370 PB |
7458 | * If the back reference is NULL, then our linked request finished |
7459 | * before we got a chance to setup the timer | |
76a46e06 | 7460 | */ |
90cd7e42 | 7461 | if (req->timeout.head) { |
e8c2bc1f | 7462 | struct io_timeout_data *data = req->async_data; |
94ae5e77 | 7463 | |
ad8a48ac JA |
7464 | data->timer.function = io_link_timeout_fn; |
7465 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
7466 | data->mode); | |
ef9dd637 | 7467 | list_add_tail(&req->timeout.list, &ctx->ltimeout_list); |
2665abfd | 7468 | } |
89b263f6 | 7469 | spin_unlock_irq(&ctx->timeout_lock); |
2665abfd | 7470 | /* drop submission reference */ |
76a46e06 JA |
7471 | io_put_req(req); |
7472 | } | |
2665abfd | 7473 | |
d475a9a6 PB |
7474 | static void io_queue_sqe_arm_apoll(struct io_kiocb *req) |
7475 | __must_hold(&req->ctx->uring_lock) | |
7476 | { | |
7477 | struct io_kiocb *linked_timeout = io_prep_linked_timeout(req); | |
7478 | ||
4d9237e3 | 7479 | switch (io_arm_poll_handler(req, 0)) { |
d475a9a6 | 7480 | case IO_APOLL_READY: |
d475a9a6 PB |
7481 | io_req_task_queue(req); |
7482 | break; | |
7483 | case IO_APOLL_ABORTED: | |
7484 | /* | |
7485 | * Queued up for async execution, worker will release | |
7486 | * submit reference when the iocb is actually submitted. | |
7487 | */ | |
7488 | io_queue_async_work(req, NULL); | |
7489 | break; | |
b1c62645 | 7490 | case IO_APOLL_OK: |
b1c62645 | 7491 | break; |
d475a9a6 PB |
7492 | } |
7493 | ||
7494 | if (linked_timeout) | |
7495 | io_queue_linked_timeout(linked_timeout); | |
7496 | } | |
7497 | ||
7498 | static inline void __io_queue_sqe(struct io_kiocb *req) | |
282cdc86 | 7499 | __must_hold(&req->ctx->uring_lock) |
2b188cc1 | 7500 | { |
906c6caa | 7501 | struct io_kiocb *linked_timeout; |
e0c5c576 | 7502 | int ret; |
2b188cc1 | 7503 | |
c5eef2b9 | 7504 | ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER); |
193155c8 | 7505 | |
fff4e40e PB |
7506 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
7507 | io_req_add_compl_list(req); | |
d9f9d284 | 7508 | return; |
fff4e40e | 7509 | } |
491381ce JA |
7510 | /* |
7511 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
7512 | * doesn't support non-blocking read/write attempts | |
7513 | */ | |
1840038e | 7514 | if (likely(!ret)) { |
906c6caa PB |
7515 | linked_timeout = io_prep_linked_timeout(req); |
7516 | if (linked_timeout) | |
7517 | io_queue_linked_timeout(linked_timeout); | |
1840038e | 7518 | } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
d475a9a6 | 7519 | io_queue_sqe_arm_apoll(req); |
0d63c148 | 7520 | } else { |
f41db273 | 7521 | io_req_complete_failed(req, ret); |
9e645e11 | 7522 | } |
2b188cc1 JA |
7523 | } |
7524 | ||
4652fe3f | 7525 | static void io_queue_sqe_fallback(struct io_kiocb *req) |
282cdc86 | 7526 | __must_hold(&req->ctx->uring_lock) |
4fe2c963 | 7527 | { |
4652fe3f | 7528 | if (req->flags & REQ_F_FAIL) { |
c6d3d9cb | 7529 | io_req_complete_fail_submit(req); |
e0eb71dc PB |
7530 | } else if (unlikely(req->ctx->drain_active)) { |
7531 | io_drain_req(req); | |
76cc33d7 PB |
7532 | } else { |
7533 | int ret = io_req_prep_async(req); | |
7534 | ||
7535 | if (unlikely(ret)) | |
7536 | io_req_complete_failed(req, ret); | |
7537 | else | |
f237c30a | 7538 | io_queue_async_work(req, NULL); |
ce35a47a | 7539 | } |
4fe2c963 JL |
7540 | } |
7541 | ||
4652fe3f PB |
7542 | static inline void io_queue_sqe(struct io_kiocb *req) |
7543 | __must_hold(&req->ctx->uring_lock) | |
7544 | { | |
7545 | if (likely(!(req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL)))) | |
7546 | __io_queue_sqe(req); | |
7547 | else | |
7548 | io_queue_sqe_fallback(req); | |
7549 | } | |
7550 | ||
b16fed66 PB |
7551 | /* |
7552 | * Check SQE restrictions (opcode and flags). | |
7553 | * | |
7554 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
7555 | */ | |
7556 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
7557 | struct io_kiocb *req, | |
7558 | unsigned int sqe_flags) | |
4fe2c963 | 7559 | { |
b16fed66 PB |
7560 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) |
7561 | return false; | |
7562 | ||
7563 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
7564 | ctx->restrictions.sqe_flags_required) | |
7565 | return false; | |
7566 | ||
7567 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
7568 | ctx->restrictions.sqe_flags_required)) | |
7569 | return false; | |
7570 | ||
7571 | return true; | |
4fe2c963 JL |
7572 | } |
7573 | ||
22b2ca31 PB |
7574 | static void io_init_req_drain(struct io_kiocb *req) |
7575 | { | |
7576 | struct io_ring_ctx *ctx = req->ctx; | |
7577 | struct io_kiocb *head = ctx->submit_state.link.head; | |
7578 | ||
7579 | ctx->drain_active = true; | |
7580 | if (head) { | |
7581 | /* | |
7582 | * If we need to drain a request in the middle of a link, drain | |
7583 | * the head request and the next request/link after the current | |
7584 | * link. Considering sequential execution of links, | |
b6c7db32 | 7585 | * REQ_F_IO_DRAIN will be maintained for every request of our |
22b2ca31 PB |
7586 | * link. |
7587 | */ | |
b6c7db32 | 7588 | head->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC; |
22b2ca31 PB |
7589 | ctx->drain_next = true; |
7590 | } | |
7591 | } | |
7592 | ||
b16fed66 PB |
7593 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
7594 | const struct io_uring_sqe *sqe) | |
282cdc86 | 7595 | __must_hold(&ctx->uring_lock) |
b16fed66 | 7596 | { |
b16fed66 | 7597 | unsigned int sqe_flags; |
fc0ae024 | 7598 | int personality; |
4a04d1d1 | 7599 | u8 opcode; |
b16fed66 | 7600 | |
864ea921 | 7601 | /* req is partially pre-initialised, see io_preinit_req() */ |
4a04d1d1 | 7602 | req->opcode = opcode = READ_ONCE(sqe->opcode); |
b16fed66 PB |
7603 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
7604 | req->flags = sqe_flags = READ_ONCE(sqe->flags); | |
7605 | req->user_data = READ_ONCE(sqe->user_data); | |
b16fed66 | 7606 | req->file = NULL; |
b16fed66 | 7607 | req->fixed_rsrc_refs = NULL; |
b16fed66 | 7608 | req->task = current; |
b16fed66 | 7609 | |
4a04d1d1 PB |
7610 | if (unlikely(opcode >= IORING_OP_LAST)) { |
7611 | req->opcode = 0; | |
b16fed66 | 7612 | return -EINVAL; |
4a04d1d1 | 7613 | } |
68fe256a PB |
7614 | if (unlikely(sqe_flags & ~SQE_COMMON_FLAGS)) { |
7615 | /* enforce forwards compatibility on users */ | |
7616 | if (sqe_flags & ~SQE_VALID_FLAGS) | |
7617 | return -EINVAL; | |
7618 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && | |
4a04d1d1 | 7619 | !io_op_defs[opcode].buffer_select) |
68fe256a | 7620 | return -EOPNOTSUPP; |
5562a8d7 PB |
7621 | if (sqe_flags & IOSQE_CQE_SKIP_SUCCESS) |
7622 | ctx->drain_disabled = true; | |
7623 | if (sqe_flags & IOSQE_IO_DRAIN) { | |
7624 | if (ctx->drain_disabled) | |
7625 | return -EOPNOTSUPP; | |
22b2ca31 | 7626 | io_init_req_drain(req); |
5562a8d7 | 7627 | } |
2a56a9bd PB |
7628 | } |
7629 | if (unlikely(ctx->restricted || ctx->drain_active || ctx->drain_next)) { | |
7630 | if (ctx->restricted && !io_check_restriction(ctx, req, sqe_flags)) | |
7631 | return -EACCES; | |
7632 | /* knock it to the slow queue path, will be drained there */ | |
7633 | if (ctx->drain_active) | |
7634 | req->flags |= REQ_F_FORCE_ASYNC; | |
7635 | /* if there is no link, we're at "next" request and need to drain */ | |
7636 | if (unlikely(ctx->drain_next) && !ctx->submit_state.link.head) { | |
7637 | ctx->drain_next = false; | |
7638 | ctx->drain_active = true; | |
b6c7db32 | 7639 | req->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC; |
2a56a9bd | 7640 | } |
68fe256a | 7641 | } |
b16fed66 | 7642 | |
4a04d1d1 | 7643 | if (io_op_defs[opcode].needs_file) { |
6d63416d PB |
7644 | struct io_submit_state *state = &ctx->submit_state; |
7645 | ||
82733d16 | 7646 | req->fd = READ_ONCE(sqe->fd); |
6bf9c47a | 7647 | |
6d63416d PB |
7648 | /* |
7649 | * Plug now if we have more than 2 IO left after this, and the | |
7650 | * target is potentially a read/write to block based storage. | |
7651 | */ | |
4a04d1d1 | 7652 | if (state->need_plug && io_op_defs[opcode].plug) { |
6d63416d PB |
7653 | state->plug_started = true; |
7654 | state->need_plug = false; | |
5ca7a8b3 | 7655 | blk_start_plug_nr_ios(&state->plug, state->submit_nr); |
6d63416d | 7656 | } |
b16fed66 | 7657 | } |
863e0560 | 7658 | |
003e8dcc JA |
7659 | personality = READ_ONCE(sqe->personality); |
7660 | if (personality) { | |
cdab10bf LT |
7661 | int ret; |
7662 | ||
c10d1f98 PB |
7663 | req->creds = xa_load(&ctx->personalities, personality); |
7664 | if (!req->creds) | |
003e8dcc | 7665 | return -EINVAL; |
c10d1f98 | 7666 | get_cred(req->creds); |
cdc1404a PM |
7667 | ret = security_uring_override_creds(req->creds); |
7668 | if (ret) { | |
7669 | put_cred(req->creds); | |
7670 | return ret; | |
7671 | } | |
b8e64b53 | 7672 | req->flags |= REQ_F_CREDS; |
003e8dcc | 7673 | } |
b16fed66 | 7674 | |
fc0ae024 | 7675 | return io_req_prep(req, sqe); |
b16fed66 PB |
7676 | } |
7677 | ||
a6b8cadc | 7678 | static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
a1ab7b35 | 7679 | const struct io_uring_sqe *sqe) |
282cdc86 | 7680 | __must_hold(&ctx->uring_lock) |
9e645e11 | 7681 | { |
a1ab7b35 | 7682 | struct io_submit_link *link = &ctx->submit_state.link; |
ef4ff581 | 7683 | int ret; |
9e645e11 | 7684 | |
a6b8cadc PB |
7685 | ret = io_init_req(ctx, req, sqe); |
7686 | if (unlikely(ret)) { | |
502c87d6 | 7687 | trace_io_uring_req_failed(sqe, ctx, req, ret); |
a87acfde | 7688 | |
a8295b98 | 7689 | /* fail even hard links since we don't submit */ |
de59bc10 | 7690 | if (link->head) { |
a8295b98 HX |
7691 | /* |
7692 | * we can judge a link req is failed or cancelled by if | |
7693 | * REQ_F_FAIL is set, but the head is an exception since | |
7694 | * it may be set REQ_F_FAIL because of other req's failure | |
7695 | * so let's leverage req->result to distinguish if a head | |
7696 | * is set REQ_F_FAIL because of its failure or other req's | |
7697 | * failure so that we can set the correct ret code for it. | |
7698 | * init result here to avoid affecting the normal path. | |
7699 | */ | |
7700 | if (!(link->head->flags & REQ_F_FAIL)) | |
7701 | req_fail_link_node(link->head, -ECANCELED); | |
7702 | } else if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { | |
7703 | /* | |
7704 | * the current req is a normal req, we should return | |
7705 | * error and thus break the submittion loop. | |
7706 | */ | |
7707 | io_req_complete_failed(req, ret); | |
7708 | return ret; | |
de59bc10 | 7709 | } |
a8295b98 | 7710 | req_fail_link_node(req, ret); |
a6b8cadc | 7711 | } |
441b8a78 | 7712 | |
be7053b7 | 7713 | /* don't need @sqe from now on */ |
502c87d6 | 7714 | trace_io_uring_submit_sqe(ctx, req, req->user_data, req->opcode, |
236daeae OL |
7715 | req->flags, true, |
7716 | ctx->flags & IORING_SETUP_SQPOLL); | |
a6b8cadc | 7717 | |
9e645e11 JA |
7718 | /* |
7719 | * If we already have a head request, queue this one for async | |
7720 | * submittal once the head completes. If we don't have a head but | |
7721 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
7722 | * submitted sync once the chain is complete. If none of those | |
7723 | * conditions are true (normal request), then just queue it. | |
7724 | */ | |
863e0560 PB |
7725 | if (link->head) { |
7726 | struct io_kiocb *head = link->head; | |
4e88d6e7 | 7727 | |
a8295b98 HX |
7728 | if (!(req->flags & REQ_F_FAIL)) { |
7729 | ret = io_req_prep_async(req); | |
7730 | if (unlikely(ret)) { | |
7731 | req_fail_link_node(req, ret); | |
7732 | if (!(head->flags & REQ_F_FAIL)) | |
7733 | req_fail_link_node(head, -ECANCELED); | |
7734 | } | |
7735 | } | |
9d76377f | 7736 | trace_io_uring_link(ctx, req, head); |
f2f87370 | 7737 | link->last->link = req; |
863e0560 | 7738 | link->last = req; |
32fe525b | 7739 | |
f15a3431 PB |
7740 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) |
7741 | return 0; | |
32fe525b | 7742 | /* last request of a link, enqueue the link */ |
f15a3431 PB |
7743 | link->head = NULL; |
7744 | req = head; | |
7745 | } else if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { | |
7746 | link->head = req; | |
7747 | link->last = req; | |
7748 | return 0; | |
9e645e11 | 7749 | } |
2e6e1fde | 7750 | |
f15a3431 | 7751 | io_queue_sqe(req); |
1d4240cc | 7752 | return 0; |
9e645e11 JA |
7753 | } |
7754 | ||
9a56a232 JA |
7755 | /* |
7756 | * Batched submission is done, ensure local IO is flushed out. | |
7757 | */ | |
553deffd | 7758 | static void io_submit_state_end(struct io_ring_ctx *ctx) |
9a56a232 | 7759 | { |
553deffd PB |
7760 | struct io_submit_state *state = &ctx->submit_state; |
7761 | ||
a1ab7b35 | 7762 | if (state->link.head) |
de59bc10 | 7763 | io_queue_sqe(state->link.head); |
553deffd | 7764 | /* flush only after queuing links as they can generate completions */ |
c450178d | 7765 | io_submit_flush_completions(ctx); |
27926b68 JA |
7766 | if (state->plug_started) |
7767 | blk_finish_plug(&state->plug); | |
9a56a232 JA |
7768 | } |
7769 | ||
7770 | /* | |
7771 | * Start submission side cache. | |
7772 | */ | |
7773 | static void io_submit_state_start(struct io_submit_state *state, | |
ba88ff11 | 7774 | unsigned int max_ios) |
9a56a232 | 7775 | { |
27926b68 | 7776 | state->plug_started = false; |
4b628aeb | 7777 | state->need_plug = max_ios > 2; |
5ca7a8b3 | 7778 | state->submit_nr = max_ios; |
a1ab7b35 PB |
7779 | /* set only head, no need to init link_last in advance */ |
7780 | state->link.head = NULL; | |
9a56a232 JA |
7781 | } |
7782 | ||
2b188cc1 JA |
7783 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
7784 | { | |
75b28aff | 7785 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 7786 | |
caf582c6 PB |
7787 | /* |
7788 | * Ensure any loads from the SQEs are done at this point, | |
7789 | * since once we write the new head, the application could | |
7790 | * write new data to them. | |
7791 | */ | |
7792 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
7793 | } |
7794 | ||
2b188cc1 | 7795 | /* |
dd9ae8a0 | 7796 | * Fetch an sqe, if one is available. Note this returns a pointer to memory |
2b188cc1 JA |
7797 | * that is mapped by userspace. This means that care needs to be taken to |
7798 | * ensure that reads are stable, as we cannot rely on userspace always | |
7799 | * being a good citizen. If members of the sqe are validated and then later | |
7800 | * used, it's important that those reads are done through READ_ONCE() to | |
7801 | * prevent a re-load down the line. | |
7802 | */ | |
709b302f | 7803 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 7804 | { |
ea5ab3b5 | 7805 | unsigned head, mask = ctx->sq_entries - 1; |
17d3aeb3 | 7806 | unsigned sq_idx = ctx->cached_sq_head++ & mask; |
2b188cc1 JA |
7807 | |
7808 | /* | |
7809 | * The cached sq head (or cq tail) serves two purposes: | |
7810 | * | |
7811 | * 1) allows us to batch the cost of updating the user visible | |
7812 | * head updates. | |
7813 | * 2) allows the kernel side to track the head on its own, even | |
7814 | * though the application is the one updating it. | |
7815 | */ | |
17d3aeb3 | 7816 | head = READ_ONCE(ctx->sq_array[sq_idx]); |
709b302f PB |
7817 | if (likely(head < ctx->sq_entries)) |
7818 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
7819 | |
7820 | /* drop invalid entries */ | |
15641e42 PB |
7821 | ctx->cq_extra--; |
7822 | WRITE_ONCE(ctx->rings->sq_dropped, | |
7823 | READ_ONCE(ctx->rings->sq_dropped) + 1); | |
709b302f PB |
7824 | return NULL; |
7825 | } | |
7826 | ||
0f212204 | 7827 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
282cdc86 | 7828 | __must_hold(&ctx->uring_lock) |
6c271ce2 | 7829 | { |
69629809 | 7830 | unsigned int entries = io_sqring_entries(ctx); |
46c4e16a | 7831 | int submitted = 0; |
6c271ce2 | 7832 | |
51d48dab | 7833 | if (unlikely(!entries)) |
69629809 | 7834 | return 0; |
ee7d46d9 | 7835 | /* make sure SQ entry isn't read before tail */ |
69629809 | 7836 | nr = min3(nr, ctx->sq_entries, entries); |
9a10867a | 7837 | io_get_task_refs(nr); |
6c271ce2 | 7838 | |
ba88ff11 | 7839 | io_submit_state_start(&ctx->submit_state, nr); |
69629809 | 7840 | do { |
3529d8c2 | 7841 | const struct io_uring_sqe *sqe; |
196be95c | 7842 | struct io_kiocb *req; |
fb5ccc98 | 7843 | |
a33ae9ce | 7844 | if (unlikely(!io_alloc_req_refill(ctx))) { |
196be95c PB |
7845 | if (!submitted) |
7846 | submitted = -EAGAIN; | |
fb5ccc98 | 7847 | break; |
196be95c | 7848 | } |
a33ae9ce | 7849 | req = io_alloc_req(ctx); |
4fccfcbb PB |
7850 | sqe = io_get_sqe(ctx); |
7851 | if (unlikely(!sqe)) { | |
c2b6c6bc | 7852 | wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list); |
4fccfcbb PB |
7853 | break; |
7854 | } | |
d3656344 JA |
7855 | /* will complete beyond this point, count as submitted */ |
7856 | submitted++; | |
bcbb7bf6 JA |
7857 | if (io_submit_sqe(ctx, req, sqe)) { |
7858 | /* | |
7859 | * Continue submitting even for sqe failure if the | |
7860 | * ring was setup with IORING_SETUP_SUBMIT_ALL | |
7861 | */ | |
7862 | if (!(ctx->flags & IORING_SETUP_SUBMIT_ALL)) | |
7863 | break; | |
7864 | } | |
69629809 | 7865 | } while (submitted < nr); |
6c271ce2 | 7866 | |
9466f437 PB |
7867 | if (unlikely(submitted != nr)) { |
7868 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; | |
d8a6df10 | 7869 | int unused = nr - ref_used; |
9466f437 | 7870 | |
09899b19 | 7871 | current->io_uring->cached_refs += unused; |
9466f437 | 7872 | } |
6c271ce2 | 7873 | |
553deffd | 7874 | io_submit_state_end(ctx); |
ae9428ca PB |
7875 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
7876 | io_commit_sqring(ctx); | |
7877 | ||
6c271ce2 JA |
7878 | return submitted; |
7879 | } | |
7880 | ||
e4b6d902 PB |
7881 | static inline bool io_sqd_events_pending(struct io_sq_data *sqd) |
7882 | { | |
7883 | return READ_ONCE(sqd->state); | |
7884 | } | |
7885 | ||
23b3628e XW |
7886 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
7887 | { | |
7888 | /* Tell userspace we may need a wakeup call */ | |
79ebeaee | 7889 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
7890 | WRITE_ONCE(ctx->rings->sq_flags, |
7891 | ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 7892 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
7893 | } |
7894 | ||
7895 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) | |
7896 | { | |
79ebeaee | 7897 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
7898 | WRITE_ONCE(ctx->rings->sq_flags, |
7899 | ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 7900 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
7901 | } |
7902 | ||
08369246 | 7903 | static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) |
6c271ce2 | 7904 | { |
c8d1ba58 | 7905 | unsigned int to_submit; |
bdcd3eab | 7906 | int ret = 0; |
6c271ce2 | 7907 | |
c8d1ba58 | 7908 | to_submit = io_sqring_entries(ctx); |
e95eee2d | 7909 | /* if we're handling multiple rings, cap submit size for fairness */ |
4ce8ad95 OL |
7910 | if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE) |
7911 | to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE; | |
e95eee2d | 7912 | |
5eef4e87 | 7913 | if (!wq_list_empty(&ctx->iopoll_list) || to_submit) { |
948e1947 PB |
7914 | const struct cred *creds = NULL; |
7915 | ||
7916 | if (ctx->sq_creds != current_cred()) | |
7917 | creds = override_creds(ctx->sq_creds); | |
a4c0b3de | 7918 | |
c8d1ba58 | 7919 | mutex_lock(&ctx->uring_lock); |
5eef4e87 | 7920 | if (!wq_list_empty(&ctx->iopoll_list)) |
5ba3c874 | 7921 | io_do_iopoll(ctx, true); |
906a3c6f | 7922 | |
3b763ba1 PB |
7923 | /* |
7924 | * Don't submit if refs are dying, good for io_uring_register(), | |
7925 | * but also it is relied upon by io_ring_exit_work() | |
7926 | */ | |
0298ef96 PB |
7927 | if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) && |
7928 | !(ctx->flags & IORING_SETUP_R_DISABLED)) | |
08369246 | 7929 | ret = io_submit_sqes(ctx, to_submit); |
c8d1ba58 | 7930 | mutex_unlock(&ctx->uring_lock); |
cb318216 | 7931 | |
acfb381d PB |
7932 | if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait)) |
7933 | wake_up(&ctx->sqo_sq_wait); | |
948e1947 PB |
7934 | if (creds) |
7935 | revert_creds(creds); | |
acfb381d | 7936 | } |
6c271ce2 | 7937 | |
08369246 XW |
7938 | return ret; |
7939 | } | |
6c271ce2 | 7940 | |
c072481d | 7941 | static __cold void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
08369246 XW |
7942 | { |
7943 | struct io_ring_ctx *ctx; | |
7944 | unsigned sq_thread_idle = 0; | |
6c271ce2 | 7945 | |
c9dca27d PB |
7946 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7947 | sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle); | |
08369246 | 7948 | sqd->sq_thread_idle = sq_thread_idle; |
c8d1ba58 | 7949 | } |
6c271ce2 | 7950 | |
e4b6d902 PB |
7951 | static bool io_sqd_handle_event(struct io_sq_data *sqd) |
7952 | { | |
7953 | bool did_sig = false; | |
7954 | struct ksignal ksig; | |
7955 | ||
7956 | if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) || | |
7957 | signal_pending(current)) { | |
7958 | mutex_unlock(&sqd->lock); | |
7959 | if (signal_pending(current)) | |
7960 | did_sig = get_signal(&ksig); | |
7961 | cond_resched(); | |
7962 | mutex_lock(&sqd->lock); | |
7963 | } | |
e4b6d902 PB |
7964 | return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
7965 | } | |
7966 | ||
c8d1ba58 JA |
7967 | static int io_sq_thread(void *data) |
7968 | { | |
69fb2131 JA |
7969 | struct io_sq_data *sqd = data; |
7970 | struct io_ring_ctx *ctx; | |
a0d9205f | 7971 | unsigned long timeout = 0; |
37d1e2e3 | 7972 | char buf[TASK_COMM_LEN]; |
08369246 | 7973 | DEFINE_WAIT(wait); |
6c271ce2 | 7974 | |
696ee88a | 7975 | snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid); |
37d1e2e3 | 7976 | set_task_comm(current, buf); |
37d1e2e3 JA |
7977 | |
7978 | if (sqd->sq_cpu != -1) | |
7979 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); | |
7980 | else | |
7981 | set_cpus_allowed_ptr(current, cpu_online_mask); | |
7982 | current->flags |= PF_NO_SETAFFINITY; | |
7983 | ||
5bd2182d PM |
7984 | audit_alloc_kernel(current); |
7985 | ||
09a6f4ef | 7986 | mutex_lock(&sqd->lock); |
e4b6d902 | 7987 | while (1) { |
1a924a80 | 7988 | bool cap_entries, sqt_spin = false; |
c1edbf5f | 7989 | |
e4b6d902 PB |
7990 | if (io_sqd_events_pending(sqd) || signal_pending(current)) { |
7991 | if (io_sqd_handle_event(sqd)) | |
c7d95613 | 7992 | break; |
08369246 XW |
7993 | timeout = jiffies + sqd->sq_thread_idle; |
7994 | } | |
e4b6d902 | 7995 | |
e95eee2d | 7996 | cap_entries = !list_is_singular(&sqd->ctx_list); |
69fb2131 | 7997 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
948e1947 | 7998 | int ret = __io_sq_thread(ctx, cap_entries); |
7c30f36a | 7999 | |
5eef4e87 | 8000 | if (!sqt_spin && (ret > 0 || !wq_list_empty(&ctx->iopoll_list))) |
08369246 | 8001 | sqt_spin = true; |
69fb2131 | 8002 | } |
dd432ea5 PB |
8003 | if (io_run_task_work()) |
8004 | sqt_spin = true; | |
6c271ce2 | 8005 | |
08369246 | 8006 | if (sqt_spin || !time_after(jiffies, timeout)) { |
c8d1ba58 | 8007 | cond_resched(); |
08369246 XW |
8008 | if (sqt_spin) |
8009 | timeout = jiffies + sqd->sq_thread_idle; | |
8010 | continue; | |
8011 | } | |
8012 | ||
08369246 | 8013 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); |
7f62d40d | 8014 | if (!io_sqd_events_pending(sqd) && !task_work_pending(current)) { |
1a924a80 PB |
8015 | bool needs_sched = true; |
8016 | ||
724cb4f9 | 8017 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
aaa9f0f4 PB |
8018 | io_ring_set_wakeup_flag(ctx); |
8019 | ||
724cb4f9 | 8020 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
5eef4e87 | 8021 | !wq_list_empty(&ctx->iopoll_list)) { |
724cb4f9 HX |
8022 | needs_sched = false; |
8023 | break; | |
8024 | } | |
649bb75d AK |
8025 | |
8026 | /* | |
8027 | * Ensure the store of the wakeup flag is not | |
8028 | * reordered with the load of the SQ tail | |
8029 | */ | |
8030 | smp_mb(); | |
8031 | ||
724cb4f9 HX |
8032 | if (io_sqring_entries(ctx)) { |
8033 | needs_sched = false; | |
8034 | break; | |
8035 | } | |
8036 | } | |
8037 | ||
8038 | if (needs_sched) { | |
8039 | mutex_unlock(&sqd->lock); | |
8040 | schedule(); | |
8041 | mutex_lock(&sqd->lock); | |
8042 | } | |
69fb2131 JA |
8043 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
8044 | io_ring_clear_wakeup_flag(ctx); | |
6c271ce2 | 8045 | } |
08369246 XW |
8046 | |
8047 | finish_wait(&sqd->wait, &wait); | |
8048 | timeout = jiffies + sqd->sq_thread_idle; | |
6c271ce2 | 8049 | } |
28cea78a | 8050 | |
78cc687b | 8051 | io_uring_cancel_generic(true, sqd); |
37d1e2e3 | 8052 | sqd->thread = NULL; |
05962f95 | 8053 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
5f3f26f9 | 8054 | io_ring_set_wakeup_flag(ctx); |
521d6a73 | 8055 | io_run_task_work(); |
734551df PB |
8056 | mutex_unlock(&sqd->lock); |
8057 | ||
5bd2182d PM |
8058 | audit_free(current); |
8059 | ||
37d1e2e3 JA |
8060 | complete(&sqd->exited); |
8061 | do_exit(0); | |
6c271ce2 JA |
8062 | } |
8063 | ||
bda52162 JA |
8064 | struct io_wait_queue { |
8065 | struct wait_queue_entry wq; | |
8066 | struct io_ring_ctx *ctx; | |
5fd46178 | 8067 | unsigned cq_tail; |
bda52162 JA |
8068 | unsigned nr_timeouts; |
8069 | }; | |
8070 | ||
6c503150 | 8071 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
bda52162 JA |
8072 | { |
8073 | struct io_ring_ctx *ctx = iowq->ctx; | |
5fd46178 | 8074 | int dist = ctx->cached_cq_tail - (int) iowq->cq_tail; |
bda52162 JA |
8075 | |
8076 | /* | |
d195a66e | 8077 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
8078 | * started waiting. For timeouts, we always want to return to userspace, |
8079 | * regardless of event count. | |
8080 | */ | |
5fd46178 | 8081 | return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
bda52162 JA |
8082 | } |
8083 | ||
8084 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
8085 | int wake_flags, void *key) | |
8086 | { | |
8087 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
8088 | wq); | |
8089 | ||
6c503150 PB |
8090 | /* |
8091 | * Cannot safely flush overflowed CQEs from here, ensure we wake up | |
8092 | * the task, and the next invocation will do it. | |
8093 | */ | |
5ed7a37d | 8094 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->check_cq_overflow)) |
6c503150 PB |
8095 | return autoremove_wake_function(curr, mode, wake_flags, key); |
8096 | return -1; | |
bda52162 JA |
8097 | } |
8098 | ||
af9c1a44 JA |
8099 | static int io_run_task_work_sig(void) |
8100 | { | |
8101 | if (io_run_task_work()) | |
8102 | return 1; | |
0b8cfa97 | 8103 | if (test_thread_flag(TIF_NOTIFY_SIGNAL)) |
792ee0f6 | 8104 | return -ERESTARTSYS; |
c5020bc8 OL |
8105 | if (task_sigpending(current)) |
8106 | return -EINTR; | |
8107 | return 0; | |
af9c1a44 JA |
8108 | } |
8109 | ||
eeb60b9a PB |
8110 | /* when returns >0, the caller should retry */ |
8111 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, | |
8112 | struct io_wait_queue *iowq, | |
22833966 | 8113 | ktime_t timeout) |
eeb60b9a PB |
8114 | { |
8115 | int ret; | |
8116 | ||
8117 | /* make sure we run task_work before checking for signals */ | |
8118 | ret = io_run_task_work_sig(); | |
8119 | if (ret || io_should_wake(iowq)) | |
8120 | return ret; | |
8121 | /* let the caller flush overflows, retry */ | |
5ed7a37d | 8122 | if (test_bit(0, &ctx->check_cq_overflow)) |
eeb60b9a PB |
8123 | return 1; |
8124 | ||
22833966 JA |
8125 | if (!schedule_hrtimeout(&timeout, HRTIMER_MODE_ABS)) |
8126 | return -ETIME; | |
8127 | return 1; | |
eeb60b9a PB |
8128 | } |
8129 | ||
2b188cc1 JA |
8130 | /* |
8131 | * Wait until events become available, if we don't already have some. The | |
8132 | * application must reap them itself, as they reside on the shared cq ring. | |
8133 | */ | |
8134 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
c73ebb68 HX |
8135 | const sigset_t __user *sig, size_t sigsz, |
8136 | struct __kernel_timespec __user *uts) | |
2b188cc1 | 8137 | { |
90291099 | 8138 | struct io_wait_queue iowq; |
75b28aff | 8139 | struct io_rings *rings = ctx->rings; |
22833966 | 8140 | ktime_t timeout = KTIME_MAX; |
c1d5a224 | 8141 | int ret; |
2b188cc1 | 8142 | |
b41e9852 | 8143 | do { |
90f67366 | 8144 | io_cqring_overflow_flush(ctx); |
6c503150 | 8145 | if (io_cqring_events(ctx) >= min_events) |
b41e9852 | 8146 | return 0; |
4c6e277c | 8147 | if (!io_run_task_work()) |
b41e9852 | 8148 | break; |
b41e9852 | 8149 | } while (1); |
2b188cc1 JA |
8150 | |
8151 | if (sig) { | |
9e75ad5d AB |
8152 | #ifdef CONFIG_COMPAT |
8153 | if (in_compat_syscall()) | |
8154 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 8155 | sigsz); |
9e75ad5d AB |
8156 | else |
8157 | #endif | |
b772434b | 8158 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 8159 | |
2b188cc1 JA |
8160 | if (ret) |
8161 | return ret; | |
8162 | } | |
8163 | ||
950e79dd OL |
8164 | if (uts) { |
8165 | struct timespec64 ts; | |
8166 | ||
8167 | if (get_timespec64(&ts, uts)) | |
8168 | return -EFAULT; | |
8169 | timeout = ktime_add_ns(timespec64_to_ktime(ts), ktime_get_ns()); | |
8170 | } | |
8171 | ||
90291099 PB |
8172 | init_waitqueue_func_entry(&iowq.wq, io_wake_function); |
8173 | iowq.wq.private = current; | |
8174 | INIT_LIST_HEAD(&iowq.wq.entry); | |
8175 | iowq.ctx = ctx; | |
bda52162 | 8176 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
5fd46178 | 8177 | iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events; |
90291099 | 8178 | |
c826bd7a | 8179 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 | 8180 | do { |
ca0a2651 | 8181 | /* if we can't even flush overflow, don't wait for more */ |
90f67366 | 8182 | if (!io_cqring_overflow_flush(ctx)) { |
ca0a2651 JA |
8183 | ret = -EBUSY; |
8184 | break; | |
8185 | } | |
311997b3 | 8186 | prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq, |
bda52162 | 8187 | TASK_INTERRUPTIBLE); |
22833966 | 8188 | ret = io_cqring_wait_schedule(ctx, &iowq, timeout); |
ca0a2651 | 8189 | cond_resched(); |
eeb60b9a | 8190 | } while (ret > 0); |
bda52162 | 8191 | |
b4f20bb4 | 8192 | finish_wait(&ctx->cq_wait, &iowq.wq); |
b7db41c9 | 8193 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 8194 | |
75b28aff | 8195 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
8196 | } |
8197 | ||
9123c8ff | 8198 | static void io_free_page_table(void **table, size_t size) |
05f3fb3c | 8199 | { |
9123c8ff | 8200 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); |
05f3fb3c | 8201 | |
846a4ef2 | 8202 | for (i = 0; i < nr_tables; i++) |
9123c8ff PB |
8203 | kfree(table[i]); |
8204 | kfree(table); | |
8205 | } | |
8206 | ||
c072481d | 8207 | static __cold void **io_alloc_page_table(size_t size) |
9123c8ff PB |
8208 | { |
8209 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); | |
8210 | size_t init_size = size; | |
8211 | void **table; | |
8212 | ||
0bea96f5 | 8213 | table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
8214 | if (!table) |
8215 | return NULL; | |
8216 | ||
8217 | for (i = 0; i < nr_tables; i++) { | |
27f6b318 | 8218 | unsigned int this_size = min_t(size_t, size, PAGE_SIZE); |
9123c8ff | 8219 | |
0bea96f5 | 8220 | table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
8221 | if (!table[i]) { |
8222 | io_free_page_table(table, init_size); | |
8223 | return NULL; | |
8224 | } | |
8225 | size -= this_size; | |
8226 | } | |
8227 | return table; | |
05f3fb3c JA |
8228 | } |
8229 | ||
28a9fe25 | 8230 | static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node) |
1642b445 | 8231 | { |
28a9fe25 PB |
8232 | percpu_ref_exit(&ref_node->refs); |
8233 | kfree(ref_node); | |
1642b445 PB |
8234 | } |
8235 | ||
c072481d | 8236 | static __cold void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
b9bd2bea PB |
8237 | { |
8238 | struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs); | |
8239 | struct io_ring_ctx *ctx = node->rsrc_data->ctx; | |
8240 | unsigned long flags; | |
8241 | bool first_add = false; | |
b36a2050 | 8242 | unsigned long delay = HZ; |
b9bd2bea PB |
8243 | |
8244 | spin_lock_irqsave(&ctx->rsrc_ref_lock, flags); | |
8245 | node->done = true; | |
8246 | ||
b36a2050 DY |
8247 | /* if we are mid-quiesce then do not delay */ |
8248 | if (node->rsrc_data->quiesce) | |
8249 | delay = 0; | |
8250 | ||
b9bd2bea PB |
8251 | while (!list_empty(&ctx->rsrc_ref_list)) { |
8252 | node = list_first_entry(&ctx->rsrc_ref_list, | |
8253 | struct io_rsrc_node, node); | |
8254 | /* recycle ref nodes in order */ | |
8255 | if (!node->done) | |
8256 | break; | |
8257 | list_del(&node->node); | |
8258 | first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist); | |
8259 | } | |
8260 | spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags); | |
8261 | ||
8262 | if (first_add) | |
b36a2050 | 8263 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, delay); |
b9bd2bea PB |
8264 | } |
8265 | ||
f6133fbd | 8266 | static struct io_rsrc_node *io_rsrc_node_alloc(void) |
b9bd2bea PB |
8267 | { |
8268 | struct io_rsrc_node *ref_node; | |
8269 | ||
8270 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); | |
8271 | if (!ref_node) | |
8272 | return NULL; | |
8273 | ||
8274 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, | |
8275 | 0, GFP_KERNEL)) { | |
8276 | kfree(ref_node); | |
8277 | return NULL; | |
8278 | } | |
8279 | INIT_LIST_HEAD(&ref_node->node); | |
8280 | INIT_LIST_HEAD(&ref_node->rsrc_list); | |
8281 | ref_node->done = false; | |
8282 | return ref_node; | |
8283 | } | |
8284 | ||
a7f0ed5a PB |
8285 | static void io_rsrc_node_switch(struct io_ring_ctx *ctx, |
8286 | struct io_rsrc_data *data_to_kill) | |
ab409402 | 8287 | __must_hold(&ctx->uring_lock) |
6b06314c | 8288 | { |
a7f0ed5a PB |
8289 | WARN_ON_ONCE(!ctx->rsrc_backup_node); |
8290 | WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node); | |
6b06314c | 8291 | |
ab409402 PB |
8292 | io_rsrc_refs_drop(ctx); |
8293 | ||
a7f0ed5a PB |
8294 | if (data_to_kill) { |
8295 | struct io_rsrc_node *rsrc_node = ctx->rsrc_node; | |
82fbcfa9 | 8296 | |
a7f0ed5a | 8297 | rsrc_node->rsrc_data = data_to_kill; |
4956b9ea | 8298 | spin_lock_irq(&ctx->rsrc_ref_lock); |
a7f0ed5a | 8299 | list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list); |
4956b9ea | 8300 | spin_unlock_irq(&ctx->rsrc_ref_lock); |
82fbcfa9 | 8301 | |
3e942498 | 8302 | atomic_inc(&data_to_kill->refs); |
a7f0ed5a PB |
8303 | percpu_ref_kill(&rsrc_node->refs); |
8304 | ctx->rsrc_node = NULL; | |
8305 | } | |
6b06314c | 8306 | |
a7f0ed5a PB |
8307 | if (!ctx->rsrc_node) { |
8308 | ctx->rsrc_node = ctx->rsrc_backup_node; | |
8309 | ctx->rsrc_backup_node = NULL; | |
8310 | } | |
8bad28d8 HX |
8311 | } |
8312 | ||
a7f0ed5a | 8313 | static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx) |
8dd03afe PB |
8314 | { |
8315 | if (ctx->rsrc_backup_node) | |
8316 | return 0; | |
f6133fbd | 8317 | ctx->rsrc_backup_node = io_rsrc_node_alloc(); |
8dd03afe | 8318 | return ctx->rsrc_backup_node ? 0 : -ENOMEM; |
8bad28d8 HX |
8319 | } |
8320 | ||
c072481d PB |
8321 | static __cold int io_rsrc_ref_quiesce(struct io_rsrc_data *data, |
8322 | struct io_ring_ctx *ctx) | |
8bad28d8 HX |
8323 | { |
8324 | int ret; | |
05589553 | 8325 | |
215c3902 | 8326 | /* As we may drop ->uring_lock, other task may have started quiesce */ |
8bad28d8 HX |
8327 | if (data->quiesce) |
8328 | return -ENXIO; | |
05589553 | 8329 | |
8bad28d8 | 8330 | data->quiesce = true; |
1ffc5422 | 8331 | do { |
a7f0ed5a | 8332 | ret = io_rsrc_node_switch_start(ctx); |
8dd03afe | 8333 | if (ret) |
f2303b1f | 8334 | break; |
a7f0ed5a | 8335 | io_rsrc_node_switch(ctx, data); |
f2303b1f | 8336 | |
3e942498 PB |
8337 | /* kill initial ref, already quiesced if zero */ |
8338 | if (atomic_dec_and_test(&data->refs)) | |
8339 | break; | |
c018db4a | 8340 | mutex_unlock(&ctx->uring_lock); |
8bad28d8 | 8341 | flush_delayed_work(&ctx->rsrc_put_work); |
1ffc5422 | 8342 | ret = wait_for_completion_interruptible(&data->done); |
c018db4a JA |
8343 | if (!ret) { |
8344 | mutex_lock(&ctx->uring_lock); | |
80912cef DY |
8345 | if (atomic_read(&data->refs) > 0) { |
8346 | /* | |
8347 | * it has been revived by another thread while | |
8348 | * we were unlocked | |
8349 | */ | |
8350 | mutex_unlock(&ctx->uring_lock); | |
8351 | } else { | |
8352 | break; | |
8353 | } | |
c018db4a | 8354 | } |
8bad28d8 | 8355 | |
3e942498 PB |
8356 | atomic_inc(&data->refs); |
8357 | /* wait for all works potentially completing data->done */ | |
8358 | flush_delayed_work(&ctx->rsrc_put_work); | |
cb5e1b81 | 8359 | reinit_completion(&data->done); |
8dd03afe | 8360 | |
1ffc5422 | 8361 | ret = io_run_task_work_sig(); |
8bad28d8 | 8362 | mutex_lock(&ctx->uring_lock); |
f2303b1f | 8363 | } while (ret >= 0); |
8bad28d8 | 8364 | data->quiesce = false; |
05f3fb3c | 8365 | |
8bad28d8 | 8366 | return ret; |
d7954b2b BM |
8367 | } |
8368 | ||
2d091d62 PB |
8369 | static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx) |
8370 | { | |
8371 | unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK; | |
8372 | unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT; | |
8373 | ||
8374 | return &data->tags[table_idx][off]; | |
8375 | } | |
8376 | ||
44b31f2f | 8377 | static void io_rsrc_data_free(struct io_rsrc_data *data) |
1ad555c6 | 8378 | { |
2d091d62 PB |
8379 | size_t size = data->nr * sizeof(data->tags[0][0]); |
8380 | ||
8381 | if (data->tags) | |
8382 | io_free_page_table((void **)data->tags, size); | |
44b31f2f PB |
8383 | kfree(data); |
8384 | } | |
8385 | ||
c072481d PB |
8386 | static __cold int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put, |
8387 | u64 __user *utags, unsigned nr, | |
8388 | struct io_rsrc_data **pdata) | |
1ad555c6 | 8389 | { |
b895c9a6 | 8390 | struct io_rsrc_data *data; |
2d091d62 | 8391 | int ret = -ENOMEM; |
d878c816 | 8392 | unsigned i; |
1ad555c6 BM |
8393 | |
8394 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
8395 | if (!data) | |
d878c816 | 8396 | return -ENOMEM; |
2d091d62 | 8397 | data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0])); |
b60c8dce | 8398 | if (!data->tags) { |
1ad555c6 | 8399 | kfree(data); |
d878c816 PB |
8400 | return -ENOMEM; |
8401 | } | |
2d091d62 PB |
8402 | |
8403 | data->nr = nr; | |
8404 | data->ctx = ctx; | |
8405 | data->do_put = do_put; | |
d878c816 | 8406 | if (utags) { |
2d091d62 | 8407 | ret = -EFAULT; |
d878c816 | 8408 | for (i = 0; i < nr; i++) { |
fdd1dc31 CIK |
8409 | u64 *tag_slot = io_get_tag_slot(data, i); |
8410 | ||
8411 | if (copy_from_user(tag_slot, &utags[i], | |
8412 | sizeof(*tag_slot))) | |
2d091d62 | 8413 | goto fail; |
d878c816 | 8414 | } |
1ad555c6 | 8415 | } |
b60c8dce | 8416 | |
3e942498 | 8417 | atomic_set(&data->refs, 1); |
1ad555c6 | 8418 | init_completion(&data->done); |
d878c816 PB |
8419 | *pdata = data; |
8420 | return 0; | |
2d091d62 PB |
8421 | fail: |
8422 | io_rsrc_data_free(data); | |
8423 | return ret; | |
1ad555c6 BM |
8424 | } |
8425 | ||
9123c8ff PB |
8426 | static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files) |
8427 | { | |
0bea96f5 PB |
8428 | table->files = kvcalloc(nr_files, sizeof(table->files[0]), |
8429 | GFP_KERNEL_ACCOUNT); | |
9123c8ff PB |
8430 | return !!table->files; |
8431 | } | |
8432 | ||
042b0d85 | 8433 | static void io_free_file_tables(struct io_file_table *table) |
9123c8ff | 8434 | { |
042b0d85 | 8435 | kvfree(table->files); |
9123c8ff PB |
8436 | table->files = NULL; |
8437 | } | |
8438 | ||
fff4db76 | 8439 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
1ad555c6 | 8440 | { |
1f59bc0f PB |
8441 | int i; |
8442 | ||
8443 | for (i = 0; i < ctx->nr_user_files; i++) { | |
8444 | struct file *file = io_file_from_index(ctx, i); | |
8445 | ||
8446 | if (!file || io_file_need_scm(file)) | |
8447 | continue; | |
8448 | io_fixed_file_slot(&ctx->file_table, i)->file_ptr = 0; | |
8449 | fput(file); | |
8450 | } | |
8451 | ||
fff4db76 PB |
8452 | #if defined(CONFIG_UNIX) |
8453 | if (ctx->ring_sock) { | |
8454 | struct sock *sock = ctx->ring_sock->sk; | |
8455 | struct sk_buff *skb; | |
8456 | ||
8457 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
8458 | kfree_skb(skb); | |
8459 | } | |
fff4db76 | 8460 | #endif |
042b0d85 | 8461 | io_free_file_tables(&ctx->file_table); |
44b31f2f | 8462 | io_rsrc_data_free(ctx->file_data); |
fff4db76 PB |
8463 | ctx->file_data = NULL; |
8464 | ctx->nr_user_files = 0; | |
1ad555c6 BM |
8465 | } |
8466 | ||
d7954b2b BM |
8467 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
8468 | { | |
d7954b2b BM |
8469 | int ret; |
8470 | ||
08480400 | 8471 | if (!ctx->file_data) |
d7954b2b | 8472 | return -ENXIO; |
08480400 PB |
8473 | ret = io_rsrc_ref_quiesce(ctx->file_data, ctx); |
8474 | if (!ret) | |
8475 | __io_sqe_files_unregister(ctx); | |
8476 | return ret; | |
6b06314c JA |
8477 | } |
8478 | ||
37d1e2e3 | 8479 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
09a6f4ef | 8480 | __releases(&sqd->lock) |
37d1e2e3 | 8481 | { |
521d6a73 PB |
8482 | WARN_ON_ONCE(sqd->thread == current); |
8483 | ||
9e138a48 PB |
8484 | /* |
8485 | * Do the dance but not conditional clear_bit() because it'd race with | |
8486 | * other threads incrementing park_pending and setting the bit. | |
8487 | */ | |
37d1e2e3 | 8488 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
9e138a48 PB |
8489 | if (atomic_dec_return(&sqd->park_pending)) |
8490 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); | |
09a6f4ef | 8491 | mutex_unlock(&sqd->lock); |
37d1e2e3 JA |
8492 | } |
8493 | ||
86e0d676 | 8494 | static void io_sq_thread_park(struct io_sq_data *sqd) |
09a6f4ef | 8495 | __acquires(&sqd->lock) |
37d1e2e3 | 8496 | { |
521d6a73 PB |
8497 | WARN_ON_ONCE(sqd->thread == current); |
8498 | ||
9e138a48 | 8499 | atomic_inc(&sqd->park_pending); |
86e0d676 | 8500 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
09a6f4ef | 8501 | mutex_lock(&sqd->lock); |
05962f95 | 8502 | if (sqd->thread) |
86e0d676 | 8503 | wake_up_process(sqd->thread); |
37d1e2e3 JA |
8504 | } |
8505 | ||
8506 | static void io_sq_thread_stop(struct io_sq_data *sqd) | |
8507 | { | |
521d6a73 | 8508 | WARN_ON_ONCE(sqd->thread == current); |
88885f66 | 8509 | WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)); |
521d6a73 | 8510 | |
05962f95 | 8511 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
88885f66 | 8512 | mutex_lock(&sqd->lock); |
e8f98f24 JA |
8513 | if (sqd->thread) |
8514 | wake_up_process(sqd->thread); | |
09a6f4ef | 8515 | mutex_unlock(&sqd->lock); |
05962f95 | 8516 | wait_for_completion(&sqd->exited); |
37d1e2e3 JA |
8517 | } |
8518 | ||
534ca6d6 | 8519 | static void io_put_sq_data(struct io_sq_data *sqd) |
6c271ce2 | 8520 | { |
534ca6d6 | 8521 | if (refcount_dec_and_test(&sqd->refs)) { |
9e138a48 PB |
8522 | WARN_ON_ONCE(atomic_read(&sqd->park_pending)); |
8523 | ||
37d1e2e3 JA |
8524 | io_sq_thread_stop(sqd); |
8525 | kfree(sqd); | |
8526 | } | |
8527 | } | |
8528 | ||
8529 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) | |
8530 | { | |
8531 | struct io_sq_data *sqd = ctx->sq_data; | |
8532 | ||
8533 | if (sqd) { | |
05962f95 | 8534 | io_sq_thread_park(sqd); |
521d6a73 | 8535 | list_del_init(&ctx->sqd_list); |
37d1e2e3 | 8536 | io_sqd_update_thread_idle(sqd); |
05962f95 | 8537 | io_sq_thread_unpark(sqd); |
37d1e2e3 JA |
8538 | |
8539 | io_put_sq_data(sqd); | |
8540 | ctx->sq_data = NULL; | |
534ca6d6 JA |
8541 | } |
8542 | } | |
8543 | ||
aa06165d JA |
8544 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
8545 | { | |
8546 | struct io_ring_ctx *ctx_attach; | |
8547 | struct io_sq_data *sqd; | |
8548 | struct fd f; | |
8549 | ||
8550 | f = fdget(p->wq_fd); | |
8551 | if (!f.file) | |
8552 | return ERR_PTR(-ENXIO); | |
8553 | if (f.file->f_op != &io_uring_fops) { | |
8554 | fdput(f); | |
8555 | return ERR_PTR(-EINVAL); | |
8556 | } | |
8557 | ||
8558 | ctx_attach = f.file->private_data; | |
8559 | sqd = ctx_attach->sq_data; | |
8560 | if (!sqd) { | |
8561 | fdput(f); | |
8562 | return ERR_PTR(-EINVAL); | |
8563 | } | |
5c2469e0 JA |
8564 | if (sqd->task_tgid != current->tgid) { |
8565 | fdput(f); | |
8566 | return ERR_PTR(-EPERM); | |
8567 | } | |
aa06165d JA |
8568 | |
8569 | refcount_inc(&sqd->refs); | |
8570 | fdput(f); | |
8571 | return sqd; | |
8572 | } | |
8573 | ||
26984fbf PB |
8574 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p, |
8575 | bool *attached) | |
534ca6d6 JA |
8576 | { |
8577 | struct io_sq_data *sqd; | |
8578 | ||
26984fbf | 8579 | *attached = false; |
5c2469e0 JA |
8580 | if (p->flags & IORING_SETUP_ATTACH_WQ) { |
8581 | sqd = io_attach_sq_data(p); | |
26984fbf PB |
8582 | if (!IS_ERR(sqd)) { |
8583 | *attached = true; | |
5c2469e0 | 8584 | return sqd; |
26984fbf | 8585 | } |
5c2469e0 JA |
8586 | /* fall through for EPERM case, setup new sqd/task */ |
8587 | if (PTR_ERR(sqd) != -EPERM) | |
8588 | return sqd; | |
8589 | } | |
aa06165d | 8590 | |
534ca6d6 JA |
8591 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
8592 | if (!sqd) | |
8593 | return ERR_PTR(-ENOMEM); | |
8594 | ||
9e138a48 | 8595 | atomic_set(&sqd->park_pending, 0); |
534ca6d6 | 8596 | refcount_set(&sqd->refs, 1); |
69fb2131 | 8597 | INIT_LIST_HEAD(&sqd->ctx_list); |
09a6f4ef | 8598 | mutex_init(&sqd->lock); |
534ca6d6 | 8599 | init_waitqueue_head(&sqd->wait); |
37d1e2e3 | 8600 | init_completion(&sqd->exited); |
534ca6d6 JA |
8601 | return sqd; |
8602 | } | |
8603 | ||
6b06314c | 8604 | #if defined(CONFIG_UNIX) |
6b06314c JA |
8605 | /* |
8606 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
8607 | * the io_uring can be safely unregistered on process exit, even if we have | |
1f59bc0f PB |
8608 | * loops in the file referencing. We account only files that can hold other |
8609 | * files because otherwise they can't form a loop and so are not interesting | |
8610 | * for GC. | |
6b06314c JA |
8611 | */ |
8612 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
8613 | { | |
8614 | struct sock *sk = ctx->ring_sock->sk; | |
8615 | struct scm_fp_list *fpl; | |
8616 | struct sk_buff *skb; | |
08a45173 | 8617 | int i, nr_files; |
6b06314c | 8618 | |
6b06314c JA |
8619 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
8620 | if (!fpl) | |
8621 | return -ENOMEM; | |
8622 | ||
8623 | skb = alloc_skb(0, GFP_KERNEL); | |
8624 | if (!skb) { | |
8625 | kfree(fpl); | |
8626 | return -ENOMEM; | |
8627 | } | |
8628 | ||
8629 | skb->sk = sk; | |
6b06314c | 8630 | |
08a45173 | 8631 | nr_files = 0; |
62e398be | 8632 | fpl->user = get_uid(current_user()); |
6b06314c | 8633 | for (i = 0; i < nr; i++) { |
65e19f54 JA |
8634 | struct file *file = io_file_from_index(ctx, i + offset); |
8635 | ||
1f59bc0f | 8636 | if (!file || !io_file_need_scm(file)) |
08a45173 | 8637 | continue; |
1f59bc0f | 8638 | |
65e19f54 | 8639 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
8640 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
8641 | nr_files++; | |
6b06314c JA |
8642 | } |
8643 | ||
08a45173 JA |
8644 | if (nr_files) { |
8645 | fpl->max = SCM_MAX_FD; | |
8646 | fpl->count = nr_files; | |
8647 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 8648 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
8649 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
8650 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 8651 | |
a07211e3 PB |
8652 | for (i = 0; i < nr; i++) { |
8653 | struct file *file = io_file_from_index(ctx, i + offset); | |
8654 | ||
1f59bc0f | 8655 | if (file && io_file_need_scm(file)) |
a07211e3 PB |
8656 | fput(file); |
8657 | } | |
08a45173 JA |
8658 | } else { |
8659 | kfree_skb(skb); | |
c86d18f4 | 8660 | free_uid(fpl->user); |
08a45173 JA |
8661 | kfree(fpl); |
8662 | } | |
6b06314c JA |
8663 | |
8664 | return 0; | |
8665 | } | |
6b06314c JA |
8666 | #endif |
8667 | ||
47e90392 | 8668 | static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
05f3fb3c | 8669 | { |
50238531 | 8670 | struct file *file = prsrc->file; |
05f3fb3c JA |
8671 | #if defined(CONFIG_UNIX) |
8672 | struct sock *sock = ctx->ring_sock->sk; | |
8673 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
8674 | struct sk_buff *skb; | |
8675 | int i; | |
8676 | ||
1f59bc0f PB |
8677 | if (!io_file_need_scm(file)) { |
8678 | fput(file); | |
8679 | return; | |
8680 | } | |
8681 | ||
05f3fb3c JA |
8682 | __skb_queue_head_init(&list); |
8683 | ||
8684 | /* | |
8685 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
8686 | * remove this entry and rearrange the file array. | |
8687 | */ | |
8688 | skb = skb_dequeue(head); | |
8689 | while (skb) { | |
8690 | struct scm_fp_list *fp; | |
8691 | ||
8692 | fp = UNIXCB(skb).fp; | |
8693 | for (i = 0; i < fp->count; i++) { | |
8694 | int left; | |
8695 | ||
8696 | if (fp->fp[i] != file) | |
8697 | continue; | |
8698 | ||
8699 | unix_notinflight(fp->user, fp->fp[i]); | |
8700 | left = fp->count - 1 - i; | |
8701 | if (left) { | |
8702 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
8703 | left * sizeof(struct file *)); | |
8704 | } | |
8705 | fp->count--; | |
8706 | if (!fp->count) { | |
8707 | kfree_skb(skb); | |
8708 | skb = NULL; | |
8709 | } else { | |
8710 | __skb_queue_tail(&list, skb); | |
8711 | } | |
8712 | fput(file); | |
8713 | file = NULL; | |
8714 | break; | |
8715 | } | |
8716 | ||
8717 | if (!file) | |
8718 | break; | |
8719 | ||
8720 | __skb_queue_tail(&list, skb); | |
8721 | ||
8722 | skb = skb_dequeue(head); | |
8723 | } | |
8724 | ||
8725 | if (skb_peek(&list)) { | |
8726 | spin_lock_irq(&head->lock); | |
8727 | while ((skb = __skb_dequeue(&list)) != NULL) | |
8728 | __skb_queue_tail(head, skb); | |
8729 | spin_unlock_irq(&head->lock); | |
8730 | } | |
8731 | #else | |
8732 | fput(file); | |
8733 | #endif | |
8734 | } | |
8735 | ||
b895c9a6 | 8736 | static void __io_rsrc_put_work(struct io_rsrc_node *ref_node) |
65e19f54 | 8737 | { |
b895c9a6 | 8738 | struct io_rsrc_data *rsrc_data = ref_node->rsrc_data; |
269bbe5f BM |
8739 | struct io_ring_ctx *ctx = rsrc_data->ctx; |
8740 | struct io_rsrc_put *prsrc, *tmp; | |
05589553 | 8741 | |
269bbe5f BM |
8742 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
8743 | list_del(&prsrc->list); | |
b60c8dce PB |
8744 | |
8745 | if (prsrc->tag) { | |
f8929630 PB |
8746 | if (ctx->flags & IORING_SETUP_IOPOLL) |
8747 | mutex_lock(&ctx->uring_lock); | |
b60c8dce | 8748 | |
79ebeaee | 8749 | spin_lock(&ctx->completion_lock); |
913a571a | 8750 | io_fill_cqe_aux(ctx, prsrc->tag, 0, 0); |
b60c8dce | 8751 | io_commit_cqring(ctx); |
79ebeaee | 8752 | spin_unlock(&ctx->completion_lock); |
b60c8dce | 8753 | io_cqring_ev_posted(ctx); |
f8929630 PB |
8754 | |
8755 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
8756 | mutex_unlock(&ctx->uring_lock); | |
b60c8dce PB |
8757 | } |
8758 | ||
40ae0ff7 | 8759 | rsrc_data->do_put(ctx, prsrc); |
269bbe5f | 8760 | kfree(prsrc); |
65e19f54 | 8761 | } |
05589553 | 8762 | |
28a9fe25 | 8763 | io_rsrc_node_destroy(ref_node); |
3e942498 PB |
8764 | if (atomic_dec_and_test(&rsrc_data->refs)) |
8765 | complete(&rsrc_data->done); | |
2faf852d | 8766 | } |
65e19f54 | 8767 | |
269bbe5f | 8768 | static void io_rsrc_put_work(struct work_struct *work) |
4a38aed2 JA |
8769 | { |
8770 | struct io_ring_ctx *ctx; | |
8771 | struct llist_node *node; | |
8772 | ||
269bbe5f BM |
8773 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
8774 | node = llist_del_all(&ctx->rsrc_put_llist); | |
4a38aed2 JA |
8775 | |
8776 | while (node) { | |
b895c9a6 | 8777 | struct io_rsrc_node *ref_node; |
4a38aed2 JA |
8778 | struct llist_node *next = node->next; |
8779 | ||
b895c9a6 | 8780 | ref_node = llist_entry(node, struct io_rsrc_node, llist); |
269bbe5f | 8781 | __io_rsrc_put_work(ref_node); |
4a38aed2 JA |
8782 | node = next; |
8783 | } | |
8784 | } | |
8785 | ||
a03a2a20 PB |
8786 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
8787 | int index); | |
8788 | ||
6b06314c | 8789 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
792e3582 | 8790 | unsigned nr_args, u64 __user *tags) |
6b06314c JA |
8791 | { |
8792 | __s32 __user *fds = (__s32 __user *) arg; | |
05f3fb3c | 8793 | struct file *file; |
f3baed39 | 8794 | int fd, ret; |
846a4ef2 | 8795 | unsigned i; |
6b06314c | 8796 | |
05f3fb3c | 8797 | if (ctx->file_data) |
6b06314c JA |
8798 | return -EBUSY; |
8799 | if (!nr_args) | |
8800 | return -EINVAL; | |
8801 | if (nr_args > IORING_MAX_FIXED_FILES) | |
8802 | return -EMFILE; | |
3a1b8a4e PB |
8803 | if (nr_args > rlimit(RLIMIT_NOFILE)) |
8804 | return -EMFILE; | |
a7f0ed5a | 8805 | ret = io_rsrc_node_switch_start(ctx); |
f3baed39 PB |
8806 | if (ret) |
8807 | return ret; | |
d878c816 PB |
8808 | ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args, |
8809 | &ctx->file_data); | |
8810 | if (ret) | |
8811 | return ret; | |
6b06314c | 8812 | |
a03a2a20 PB |
8813 | if (!io_alloc_file_tables(&ctx->file_table, nr_args)) { |
8814 | io_rsrc_data_free(ctx->file_data); | |
8815 | ctx->file_data = NULL; | |
8816 | return -ENOMEM; | |
8817 | } | |
65e19f54 | 8818 | |
08a45173 | 8819 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
a03a2a20 PB |
8820 | struct io_fixed_file *file_slot; |
8821 | ||
d878c816 | 8822 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
600cf3f8 | 8823 | ret = -EFAULT; |
a03a2a20 | 8824 | goto fail; |
600cf3f8 | 8825 | } |
08a45173 | 8826 | /* allow sparse sets */ |
792e3582 PB |
8827 | if (fd == -1) { |
8828 | ret = -EINVAL; | |
2d091d62 | 8829 | if (unlikely(*io_get_tag_slot(ctx->file_data, i))) |
a03a2a20 | 8830 | goto fail; |
08a45173 | 8831 | continue; |
792e3582 | 8832 | } |
6b06314c | 8833 | |
05f3fb3c | 8834 | file = fget(fd); |
6b06314c | 8835 | ret = -EBADF; |
792e3582 | 8836 | if (unlikely(!file)) |
a03a2a20 | 8837 | goto fail; |
05f3fb3c | 8838 | |
6b06314c JA |
8839 | /* |
8840 | * Don't allow io_uring instances to be registered. If UNIX | |
8841 | * isn't enabled, then this causes a reference cycle and this | |
8842 | * instance can never get freed. If UNIX is enabled we'll | |
8843 | * handle it just fine, but there's still no point in allowing | |
8844 | * a ring fd as it doesn't support regular read/write anyway. | |
8845 | */ | |
05f3fb3c JA |
8846 | if (file->f_op == &io_uring_fops) { |
8847 | fput(file); | |
a03a2a20 PB |
8848 | goto fail; |
8849 | } | |
8850 | file_slot = io_fixed_file_slot(&ctx->file_table, i); | |
8851 | io_fixed_file_set(file_slot, file); | |
8852 | ret = io_sqe_file_register(ctx, file, i); | |
8853 | if (ret) { | |
8854 | file_slot->file_ptr = 0; | |
8855 | fput(file); | |
8856 | goto fail; | |
6b06314c | 8857 | } |
05589553 | 8858 | } |
6b06314c | 8859 | |
a7f0ed5a | 8860 | io_rsrc_node_switch(ctx, NULL); |
a03a2a20 PB |
8861 | return 0; |
8862 | fail: | |
8863 | __io_sqe_files_unregister(ctx); | |
6b06314c JA |
8864 | return ret; |
8865 | } | |
8866 | ||
c3a31e60 JA |
8867 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
8868 | int index) | |
8869 | { | |
8870 | #if defined(CONFIG_UNIX) | |
8871 | struct sock *sock = ctx->ring_sock->sk; | |
8872 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
8873 | struct sk_buff *skb; | |
8874 | ||
1f59bc0f PB |
8875 | if (!io_file_need_scm(file)) |
8876 | return 0; | |
8877 | ||
c3a31e60 JA |
8878 | /* |
8879 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
8880 | * file set. If there's no room, fall back to allocating a new skb | |
8881 | * and filling it in. | |
8882 | */ | |
8883 | spin_lock_irq(&head->lock); | |
8884 | skb = skb_peek(head); | |
8885 | if (skb) { | |
8886 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
8887 | ||
8888 | if (fpl->count < SCM_MAX_FD) { | |
8889 | __skb_unlink(skb, head); | |
8890 | spin_unlock_irq(&head->lock); | |
8891 | fpl->fp[fpl->count] = get_file(file); | |
8892 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
8893 | fpl->count++; | |
8894 | spin_lock_irq(&head->lock); | |
8895 | __skb_queue_head(head, skb); | |
8896 | } else { | |
8897 | skb = NULL; | |
8898 | } | |
8899 | } | |
8900 | spin_unlock_irq(&head->lock); | |
8901 | ||
8902 | if (skb) { | |
8903 | fput(file); | |
8904 | return 0; | |
8905 | } | |
8906 | ||
8907 | return __io_sqe_files_scm(ctx, 1, index); | |
8908 | #else | |
8909 | return 0; | |
8910 | #endif | |
8911 | } | |
8912 | ||
9c7b0ba8 PB |
8913 | static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, |
8914 | struct io_rsrc_node *node, void *rsrc) | |
8915 | { | |
8f0a2480 | 8916 | u64 *tag_slot = io_get_tag_slot(data, idx); |
9c7b0ba8 PB |
8917 | struct io_rsrc_put *prsrc; |
8918 | ||
8919 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); | |
8920 | if (!prsrc) | |
8921 | return -ENOMEM; | |
8922 | ||
8f0a2480 PB |
8923 | prsrc->tag = *tag_slot; |
8924 | *tag_slot = 0; | |
9c7b0ba8 PB |
8925 | prsrc->rsrc = rsrc; |
8926 | list_add(&prsrc->list, &node->rsrc_list); | |
8927 | return 0; | |
8928 | } | |
8929 | ||
b9445598 PB |
8930 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
8931 | unsigned int issue_flags, u32 slot_index) | |
8932 | { | |
8933 | struct io_ring_ctx *ctx = req->ctx; | |
9c7b0ba8 | 8934 | bool needs_switch = false; |
b9445598 PB |
8935 | struct io_fixed_file *file_slot; |
8936 | int ret = -EBADF; | |
8937 | ||
f8929630 | 8938 | io_ring_submit_lock(ctx, issue_flags); |
b9445598 PB |
8939 | if (file->f_op == &io_uring_fops) |
8940 | goto err; | |
8941 | ret = -ENXIO; | |
8942 | if (!ctx->file_data) | |
8943 | goto err; | |
8944 | ret = -EINVAL; | |
8945 | if (slot_index >= ctx->nr_user_files) | |
8946 | goto err; | |
8947 | ||
8948 | slot_index = array_index_nospec(slot_index, ctx->nr_user_files); | |
8949 | file_slot = io_fixed_file_slot(&ctx->file_table, slot_index); | |
9c7b0ba8 PB |
8950 | |
8951 | if (file_slot->file_ptr) { | |
8952 | struct file *old_file; | |
8953 | ||
8954 | ret = io_rsrc_node_switch_start(ctx); | |
8955 | if (ret) | |
8956 | goto err; | |
8957 | ||
8958 | old_file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
8959 | ret = io_queue_rsrc_removal(ctx->file_data, slot_index, | |
8960 | ctx->rsrc_node, old_file); | |
8961 | if (ret) | |
8962 | goto err; | |
8963 | file_slot->file_ptr = 0; | |
8964 | needs_switch = true; | |
8965 | } | |
b9445598 PB |
8966 | |
8967 | *io_get_tag_slot(ctx->file_data, slot_index) = 0; | |
8968 | io_fixed_file_set(file_slot, file); | |
8969 | ret = io_sqe_file_register(ctx, file, slot_index); | |
8970 | if (ret) { | |
8971 | file_slot->file_ptr = 0; | |
8972 | goto err; | |
8973 | } | |
8974 | ||
8975 | ret = 0; | |
8976 | err: | |
9c7b0ba8 PB |
8977 | if (needs_switch) |
8978 | io_rsrc_node_switch(ctx, ctx->file_data); | |
f8929630 | 8979 | io_ring_submit_unlock(ctx, issue_flags); |
b9445598 PB |
8980 | if (ret) |
8981 | fput(file); | |
8982 | return ret; | |
8983 | } | |
8984 | ||
7df778be PB |
8985 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags) |
8986 | { | |
8987 | unsigned int offset = req->close.file_slot - 1; | |
8988 | struct io_ring_ctx *ctx = req->ctx; | |
8989 | struct io_fixed_file *file_slot; | |
8990 | struct file *file; | |
4cdd158b | 8991 | int ret; |
7df778be | 8992 | |
f8929630 | 8993 | io_ring_submit_lock(ctx, issue_flags); |
7df778be PB |
8994 | ret = -ENXIO; |
8995 | if (unlikely(!ctx->file_data)) | |
8996 | goto out; | |
8997 | ret = -EINVAL; | |
8998 | if (offset >= ctx->nr_user_files) | |
8999 | goto out; | |
9000 | ret = io_rsrc_node_switch_start(ctx); | |
9001 | if (ret) | |
9002 | goto out; | |
9003 | ||
4cdd158b PB |
9004 | offset = array_index_nospec(offset, ctx->nr_user_files); |
9005 | file_slot = io_fixed_file_slot(&ctx->file_table, offset); | |
7df778be PB |
9006 | ret = -EBADF; |
9007 | if (!file_slot->file_ptr) | |
9008 | goto out; | |
9009 | ||
9010 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
9011 | ret = io_queue_rsrc_removal(ctx->file_data, offset, ctx->rsrc_node, file); | |
9012 | if (ret) | |
9013 | goto out; | |
9014 | ||
9015 | file_slot->file_ptr = 0; | |
9016 | io_rsrc_node_switch(ctx, ctx->file_data); | |
9017 | ret = 0; | |
9018 | out: | |
f8929630 | 9019 | io_ring_submit_unlock(ctx, issue_flags); |
7df778be PB |
9020 | return ret; |
9021 | } | |
9022 | ||
05f3fb3c | 9023 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
c3bdad02 | 9024 | struct io_uring_rsrc_update2 *up, |
05f3fb3c JA |
9025 | unsigned nr_args) |
9026 | { | |
c3bdad02 | 9027 | u64 __user *tags = u64_to_user_ptr(up->tags); |
98f0b3b4 | 9028 | __s32 __user *fds = u64_to_user_ptr(up->data); |
b895c9a6 | 9029 | struct io_rsrc_data *data = ctx->file_data; |
a04b0ac0 PB |
9030 | struct io_fixed_file *file_slot; |
9031 | struct file *file; | |
98f0b3b4 PB |
9032 | int fd, i, err = 0; |
9033 | unsigned int done; | |
05589553 | 9034 | bool needs_switch = false; |
c3a31e60 | 9035 | |
98f0b3b4 PB |
9036 | if (!ctx->file_data) |
9037 | return -ENXIO; | |
9038 | if (up->offset + nr_args > ctx->nr_user_files) | |
c3a31e60 JA |
9039 | return -EINVAL; |
9040 | ||
67973b93 | 9041 | for (done = 0; done < nr_args; done++) { |
c3bdad02 PB |
9042 | u64 tag = 0; |
9043 | ||
9044 | if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) || | |
9045 | copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
c3a31e60 JA |
9046 | err = -EFAULT; |
9047 | break; | |
9048 | } | |
c3bdad02 PB |
9049 | if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) { |
9050 | err = -EINVAL; | |
9051 | break; | |
9052 | } | |
4e0377a1 | 9053 | if (fd == IORING_REGISTER_FILES_SKIP) |
9054 | continue; | |
9055 | ||
67973b93 | 9056 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
aeca241b | 9057 | file_slot = io_fixed_file_slot(&ctx->file_table, i); |
ea64ec02 | 9058 | |
a04b0ac0 PB |
9059 | if (file_slot->file_ptr) { |
9060 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
4cdd158b | 9061 | err = io_queue_rsrc_removal(data, i, ctx->rsrc_node, file); |
a5318d3c HD |
9062 | if (err) |
9063 | break; | |
a04b0ac0 | 9064 | file_slot->file_ptr = 0; |
05589553 | 9065 | needs_switch = true; |
c3a31e60 JA |
9066 | } |
9067 | if (fd != -1) { | |
c3a31e60 JA |
9068 | file = fget(fd); |
9069 | if (!file) { | |
9070 | err = -EBADF; | |
9071 | break; | |
9072 | } | |
9073 | /* | |
9074 | * Don't allow io_uring instances to be registered. If | |
9075 | * UNIX isn't enabled, then this causes a reference | |
9076 | * cycle and this instance can never get freed. If UNIX | |
9077 | * is enabled we'll handle it just fine, but there's | |
9078 | * still no point in allowing a ring fd as it doesn't | |
9079 | * support regular read/write anyway. | |
9080 | */ | |
9081 | if (file->f_op == &io_uring_fops) { | |
9082 | fput(file); | |
9083 | err = -EBADF; | |
9084 | break; | |
9085 | } | |
34bb7718 | 9086 | *io_get_tag_slot(data, i) = tag; |
9a321c98 | 9087 | io_fixed_file_set(file_slot, file); |
c3a31e60 | 9088 | err = io_sqe_file_register(ctx, file, i); |
f3bd9dae | 9089 | if (err) { |
a04b0ac0 | 9090 | file_slot->file_ptr = 0; |
f3bd9dae | 9091 | fput(file); |
c3a31e60 | 9092 | break; |
f3bd9dae | 9093 | } |
c3a31e60 | 9094 | } |
05f3fb3c JA |
9095 | } |
9096 | ||
a7f0ed5a PB |
9097 | if (needs_switch) |
9098 | io_rsrc_node_switch(ctx, data); | |
c3a31e60 JA |
9099 | return done ? done : err; |
9100 | } | |
05589553 | 9101 | |
685fe7fe JA |
9102 | static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx, |
9103 | struct task_struct *task) | |
24369c2e | 9104 | { |
e941894e | 9105 | struct io_wq_hash *hash; |
24369c2e | 9106 | struct io_wq_data data; |
24369c2e | 9107 | unsigned int concurrency; |
24369c2e | 9108 | |
362a9e65 | 9109 | mutex_lock(&ctx->uring_lock); |
e941894e JA |
9110 | hash = ctx->hash_map; |
9111 | if (!hash) { | |
9112 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); | |
362a9e65 YY |
9113 | if (!hash) { |
9114 | mutex_unlock(&ctx->uring_lock); | |
e941894e | 9115 | return ERR_PTR(-ENOMEM); |
362a9e65 | 9116 | } |
e941894e JA |
9117 | refcount_set(&hash->refs, 1); |
9118 | init_waitqueue_head(&hash->wait); | |
9119 | ctx->hash_map = hash; | |
24369c2e | 9120 | } |
362a9e65 | 9121 | mutex_unlock(&ctx->uring_lock); |
24369c2e | 9122 | |
e941894e | 9123 | data.hash = hash; |
685fe7fe | 9124 | data.task = task; |
ebc11b6c | 9125 | data.free_work = io_wq_free_work; |
f5fa38c5 | 9126 | data.do_work = io_wq_submit_work; |
24369c2e | 9127 | |
d25e3a3d JA |
9128 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
9129 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
24369c2e | 9130 | |
5aa75ed5 | 9131 | return io_wq_create(concurrency, &data); |
24369c2e PB |
9132 | } |
9133 | ||
c072481d PB |
9134 | static __cold int io_uring_alloc_task_context(struct task_struct *task, |
9135 | struct io_ring_ctx *ctx) | |
0f212204 JA |
9136 | { |
9137 | struct io_uring_task *tctx; | |
d8a6df10 | 9138 | int ret; |
0f212204 | 9139 | |
09899b19 | 9140 | tctx = kzalloc(sizeof(*tctx), GFP_KERNEL); |
0f212204 JA |
9141 | if (unlikely(!tctx)) |
9142 | return -ENOMEM; | |
9143 | ||
e7a6c00d JA |
9144 | tctx->registered_rings = kcalloc(IO_RINGFD_REG_MAX, |
9145 | sizeof(struct file *), GFP_KERNEL); | |
9146 | if (unlikely(!tctx->registered_rings)) { | |
9147 | kfree(tctx); | |
9148 | return -ENOMEM; | |
9149 | } | |
9150 | ||
d8a6df10 JA |
9151 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
9152 | if (unlikely(ret)) { | |
e7a6c00d | 9153 | kfree(tctx->registered_rings); |
d8a6df10 JA |
9154 | kfree(tctx); |
9155 | return ret; | |
9156 | } | |
9157 | ||
685fe7fe | 9158 | tctx->io_wq = io_init_wq_offload(ctx, task); |
5aa75ed5 JA |
9159 | if (IS_ERR(tctx->io_wq)) { |
9160 | ret = PTR_ERR(tctx->io_wq); | |
9161 | percpu_counter_destroy(&tctx->inflight); | |
e7a6c00d | 9162 | kfree(tctx->registered_rings); |
5aa75ed5 JA |
9163 | kfree(tctx); |
9164 | return ret; | |
9165 | } | |
9166 | ||
0f212204 JA |
9167 | xa_init(&tctx->xa); |
9168 | init_waitqueue_head(&tctx->wait); | |
fdaf083c | 9169 | atomic_set(&tctx->in_idle, 0); |
0f212204 | 9170 | task->io_uring = tctx; |
7cbf1722 JA |
9171 | spin_lock_init(&tctx->task_lock); |
9172 | INIT_WQ_LIST(&tctx->task_list); | |
4813c377 | 9173 | INIT_WQ_LIST(&tctx->prior_task_list); |
7cbf1722 | 9174 | init_task_work(&tctx->task_work, tctx_task_work); |
0f212204 JA |
9175 | return 0; |
9176 | } | |
9177 | ||
9178 | void __io_uring_free(struct task_struct *tsk) | |
9179 | { | |
9180 | struct io_uring_task *tctx = tsk->io_uring; | |
9181 | ||
9182 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); | |
ef8eaa4e | 9183 | WARN_ON_ONCE(tctx->io_wq); |
09899b19 | 9184 | WARN_ON_ONCE(tctx->cached_refs); |
ef8eaa4e | 9185 | |
e7a6c00d | 9186 | kfree(tctx->registered_rings); |
d8a6df10 | 9187 | percpu_counter_destroy(&tctx->inflight); |
0f212204 JA |
9188 | kfree(tctx); |
9189 | tsk->io_uring = NULL; | |
9190 | } | |
9191 | ||
c072481d PB |
9192 | static __cold int io_sq_offload_create(struct io_ring_ctx *ctx, |
9193 | struct io_uring_params *p) | |
2b188cc1 JA |
9194 | { |
9195 | int ret; | |
9196 | ||
d25e3a3d JA |
9197 | /* Retain compatibility with failing for an invalid attach attempt */ |
9198 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == | |
9199 | IORING_SETUP_ATTACH_WQ) { | |
9200 | struct fd f; | |
9201 | ||
9202 | f = fdget(p->wq_fd); | |
9203 | if (!f.file) | |
9204 | return -ENXIO; | |
0cc936f7 JA |
9205 | if (f.file->f_op != &io_uring_fops) { |
9206 | fdput(f); | |
f2a48dd0 | 9207 | return -EINVAL; |
0cc936f7 JA |
9208 | } |
9209 | fdput(f); | |
d25e3a3d | 9210 | } |
6c271ce2 | 9211 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
46fe18b1 | 9212 | struct task_struct *tsk; |
534ca6d6 | 9213 | struct io_sq_data *sqd; |
26984fbf | 9214 | bool attached; |
534ca6d6 | 9215 | |
cdc1404a PM |
9216 | ret = security_uring_sqpoll(); |
9217 | if (ret) | |
9218 | return ret; | |
9219 | ||
26984fbf | 9220 | sqd = io_get_sq_data(p, &attached); |
534ca6d6 JA |
9221 | if (IS_ERR(sqd)) { |
9222 | ret = PTR_ERR(sqd); | |
9223 | goto err; | |
9224 | } | |
69fb2131 | 9225 | |
7c30f36a | 9226 | ctx->sq_creds = get_current_cred(); |
534ca6d6 | 9227 | ctx->sq_data = sqd; |
917257da JA |
9228 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
9229 | if (!ctx->sq_thread_idle) | |
9230 | ctx->sq_thread_idle = HZ; | |
9231 | ||
78d7f6ba | 9232 | io_sq_thread_park(sqd); |
de75a3d3 PB |
9233 | list_add(&ctx->sqd_list, &sqd->ctx_list); |
9234 | io_sqd_update_thread_idle(sqd); | |
26984fbf | 9235 | /* don't attach to a dying SQPOLL thread, would be racy */ |
f2a48dd0 | 9236 | ret = (attached && !sqd->thread) ? -ENXIO : 0; |
78d7f6ba PB |
9237 | io_sq_thread_unpark(sqd); |
9238 | ||
de75a3d3 PB |
9239 | if (ret < 0) |
9240 | goto err; | |
9241 | if (attached) | |
5aa75ed5 | 9242 | return 0; |
aa06165d | 9243 | |
6c271ce2 | 9244 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 9245 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 9246 | |
917257da | 9247 | ret = -EINVAL; |
f2a48dd0 | 9248 | if (cpu >= nr_cpu_ids || !cpu_online(cpu)) |
e8f98f24 | 9249 | goto err_sqpoll; |
37d1e2e3 | 9250 | sqd->sq_cpu = cpu; |
6c271ce2 | 9251 | } else { |
37d1e2e3 | 9252 | sqd->sq_cpu = -1; |
6c271ce2 | 9253 | } |
37d1e2e3 JA |
9254 | |
9255 | sqd->task_pid = current->pid; | |
5c2469e0 | 9256 | sqd->task_tgid = current->tgid; |
46fe18b1 JA |
9257 | tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE); |
9258 | if (IS_ERR(tsk)) { | |
9259 | ret = PTR_ERR(tsk); | |
e8f98f24 | 9260 | goto err_sqpoll; |
6c271ce2 | 9261 | } |
97a73a0f | 9262 | |
46fe18b1 | 9263 | sqd->thread = tsk; |
97a73a0f | 9264 | ret = io_uring_alloc_task_context(tsk, ctx); |
46fe18b1 | 9265 | wake_up_new_task(tsk); |
0f212204 JA |
9266 | if (ret) |
9267 | goto err; | |
6c271ce2 JA |
9268 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
9269 | /* Can't have SQ_AFF without SQPOLL */ | |
9270 | ret = -EINVAL; | |
9271 | goto err; | |
9272 | } | |
9273 | ||
2b188cc1 | 9274 | return 0; |
f2a48dd0 PB |
9275 | err_sqpoll: |
9276 | complete(&ctx->sq_data->exited); | |
2b188cc1 | 9277 | err: |
37d1e2e3 | 9278 | io_sq_thread_finish(ctx); |
2b188cc1 JA |
9279 | return ret; |
9280 | } | |
9281 | ||
a087e2b5 BM |
9282 | static inline void __io_unaccount_mem(struct user_struct *user, |
9283 | unsigned long nr_pages) | |
2b188cc1 JA |
9284 | { |
9285 | atomic_long_sub(nr_pages, &user->locked_vm); | |
9286 | } | |
9287 | ||
a087e2b5 BM |
9288 | static inline int __io_account_mem(struct user_struct *user, |
9289 | unsigned long nr_pages) | |
2b188cc1 JA |
9290 | { |
9291 | unsigned long page_limit, cur_pages, new_pages; | |
9292 | ||
9293 | /* Don't allow more pages than we can safely lock */ | |
9294 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
9295 | ||
9296 | do { | |
9297 | cur_pages = atomic_long_read(&user->locked_vm); | |
9298 | new_pages = cur_pages + nr_pages; | |
9299 | if (new_pages > page_limit) | |
9300 | return -ENOMEM; | |
9301 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
9302 | new_pages) != cur_pages); | |
9303 | ||
9304 | return 0; | |
9305 | } | |
9306 | ||
26bfa89e | 9307 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 9308 | { |
62e398be | 9309 | if (ctx->user) |
a087e2b5 | 9310 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 9311 | |
26bfa89e JA |
9312 | if (ctx->mm_account) |
9313 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
9314 | } |
9315 | ||
26bfa89e | 9316 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 9317 | { |
30975825 BM |
9318 | int ret; |
9319 | ||
62e398be | 9320 | if (ctx->user) { |
30975825 BM |
9321 | ret = __io_account_mem(ctx->user, nr_pages); |
9322 | if (ret) | |
9323 | return ret; | |
9324 | } | |
9325 | ||
26bfa89e JA |
9326 | if (ctx->mm_account) |
9327 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
9328 | |
9329 | return 0; | |
9330 | } | |
9331 | ||
2b188cc1 JA |
9332 | static void io_mem_free(void *ptr) |
9333 | { | |
52e04ef4 MR |
9334 | struct page *page; |
9335 | ||
9336 | if (!ptr) | |
9337 | return; | |
2b188cc1 | 9338 | |
52e04ef4 | 9339 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
9340 | if (put_page_testzero(page)) |
9341 | free_compound_page(page); | |
9342 | } | |
9343 | ||
9344 | static void *io_mem_alloc(size_t size) | |
9345 | { | |
0a3f1e0b | 9346 | gfp_t gfp = GFP_KERNEL_ACCOUNT | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP; |
2b188cc1 | 9347 | |
0a3f1e0b | 9348 | return (void *) __get_free_pages(gfp, get_order(size)); |
2b188cc1 JA |
9349 | } |
9350 | ||
75b28aff HV |
9351 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
9352 | size_t *sq_offset) | |
9353 | { | |
9354 | struct io_rings *rings; | |
9355 | size_t off, sq_array_size; | |
9356 | ||
9357 | off = struct_size(rings, cqes, cq_entries); | |
9358 | if (off == SIZE_MAX) | |
9359 | return SIZE_MAX; | |
9360 | ||
9361 | #ifdef CONFIG_SMP | |
9362 | off = ALIGN(off, SMP_CACHE_BYTES); | |
9363 | if (off == 0) | |
9364 | return SIZE_MAX; | |
9365 | #endif | |
9366 | ||
b36200f5 DV |
9367 | if (sq_offset) |
9368 | *sq_offset = off; | |
9369 | ||
75b28aff HV |
9370 | sq_array_size = array_size(sizeof(u32), sq_entries); |
9371 | if (sq_array_size == SIZE_MAX) | |
9372 | return SIZE_MAX; | |
9373 | ||
9374 | if (check_add_overflow(off, sq_array_size, &off)) | |
9375 | return SIZE_MAX; | |
9376 | ||
75b28aff HV |
9377 | return off; |
9378 | } | |
9379 | ||
41edf1a5 | 9380 | static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot) |
7f61a1e9 | 9381 | { |
41edf1a5 | 9382 | struct io_mapped_ubuf *imu = *slot; |
7f61a1e9 PB |
9383 | unsigned int i; |
9384 | ||
6224843d PB |
9385 | if (imu != ctx->dummy_ubuf) { |
9386 | for (i = 0; i < imu->nr_bvecs; i++) | |
9387 | unpin_user_page(imu->bvec[i].bv_page); | |
9388 | if (imu->acct_pages) | |
9389 | io_unaccount_mem(ctx, imu->acct_pages); | |
9390 | kvfree(imu); | |
9391 | } | |
41edf1a5 | 9392 | *slot = NULL; |
7f61a1e9 PB |
9393 | } |
9394 | ||
bd54b6fe | 9395 | static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
edafccee | 9396 | { |
634d00df PB |
9397 | io_buffer_unmap(ctx, &prsrc->buf); |
9398 | prsrc->buf = NULL; | |
bd54b6fe | 9399 | } |
edafccee | 9400 | |
bd54b6fe BM |
9401 | static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
9402 | { | |
9403 | unsigned int i; | |
edafccee | 9404 | |
7f61a1e9 PB |
9405 | for (i = 0; i < ctx->nr_user_bufs; i++) |
9406 | io_buffer_unmap(ctx, &ctx->user_bufs[i]); | |
edafccee | 9407 | kfree(ctx->user_bufs); |
bb6659cc | 9408 | io_rsrc_data_free(ctx->buf_data); |
edafccee | 9409 | ctx->user_bufs = NULL; |
bd54b6fe | 9410 | ctx->buf_data = NULL; |
edafccee | 9411 | ctx->nr_user_bufs = 0; |
bd54b6fe BM |
9412 | } |
9413 | ||
0a96bbe4 | 9414 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
edafccee | 9415 | { |
bd54b6fe | 9416 | int ret; |
edafccee | 9417 | |
bd54b6fe | 9418 | if (!ctx->buf_data) |
edafccee JA |
9419 | return -ENXIO; |
9420 | ||
bd54b6fe BM |
9421 | ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx); |
9422 | if (!ret) | |
9423 | __io_sqe_buffers_unregister(ctx); | |
9424 | return ret; | |
edafccee JA |
9425 | } |
9426 | ||
9427 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
9428 | void __user *arg, unsigned index) | |
9429 | { | |
9430 | struct iovec __user *src; | |
9431 | ||
9432 | #ifdef CONFIG_COMPAT | |
9433 | if (ctx->compat) { | |
9434 | struct compat_iovec __user *ciovs; | |
9435 | struct compat_iovec ciov; | |
9436 | ||
9437 | ciovs = (struct compat_iovec __user *) arg; | |
9438 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
9439 | return -EFAULT; | |
9440 | ||
d55e5f5b | 9441 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
9442 | dst->iov_len = ciov.iov_len; |
9443 | return 0; | |
9444 | } | |
9445 | #endif | |
9446 | src = (struct iovec __user *) arg; | |
9447 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
9448 | return -EFAULT; | |
9449 | return 0; | |
9450 | } | |
9451 | ||
de293938 JA |
9452 | /* |
9453 | * Not super efficient, but this is just a registration time. And we do cache | |
9454 | * the last compound head, so generally we'll only do a full search if we don't | |
9455 | * match that one. | |
9456 | * | |
9457 | * We check if the given compound head page has already been accounted, to | |
9458 | * avoid double accounting it. This allows us to account the full size of the | |
9459 | * page, not just the constituent pages of a huge page. | |
9460 | */ | |
9461 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, | |
9462 | int nr_pages, struct page *hpage) | |
9463 | { | |
9464 | int i, j; | |
9465 | ||
9466 | /* check current page array */ | |
9467 | for (i = 0; i < nr_pages; i++) { | |
9468 | if (!PageCompound(pages[i])) | |
9469 | continue; | |
9470 | if (compound_head(pages[i]) == hpage) | |
9471 | return true; | |
9472 | } | |
9473 | ||
9474 | /* check previously registered pages */ | |
9475 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
41edf1a5 | 9476 | struct io_mapped_ubuf *imu = ctx->user_bufs[i]; |
de293938 JA |
9477 | |
9478 | for (j = 0; j < imu->nr_bvecs; j++) { | |
9479 | if (!PageCompound(imu->bvec[j].bv_page)) | |
9480 | continue; | |
9481 | if (compound_head(imu->bvec[j].bv_page) == hpage) | |
9482 | return true; | |
9483 | } | |
9484 | } | |
9485 | ||
9486 | return false; | |
9487 | } | |
9488 | ||
9489 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, | |
9490 | int nr_pages, struct io_mapped_ubuf *imu, | |
9491 | struct page **last_hpage) | |
9492 | { | |
9493 | int i, ret; | |
9494 | ||
216e5835 | 9495 | imu->acct_pages = 0; |
de293938 JA |
9496 | for (i = 0; i < nr_pages; i++) { |
9497 | if (!PageCompound(pages[i])) { | |
9498 | imu->acct_pages++; | |
9499 | } else { | |
9500 | struct page *hpage; | |
9501 | ||
9502 | hpage = compound_head(pages[i]); | |
9503 | if (hpage == *last_hpage) | |
9504 | continue; | |
9505 | *last_hpage = hpage; | |
9506 | if (headpage_already_acct(ctx, pages, i, hpage)) | |
9507 | continue; | |
9508 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; | |
9509 | } | |
9510 | } | |
9511 | ||
9512 | if (!imu->acct_pages) | |
9513 | return 0; | |
9514 | ||
26bfa89e | 9515 | ret = io_account_mem(ctx, imu->acct_pages); |
de293938 JA |
9516 | if (ret) |
9517 | imu->acct_pages = 0; | |
9518 | return ret; | |
9519 | } | |
9520 | ||
0a96bbe4 | 9521 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
41edf1a5 | 9522 | struct io_mapped_ubuf **pimu, |
0a96bbe4 | 9523 | struct page **last_hpage) |
edafccee | 9524 | { |
41edf1a5 | 9525 | struct io_mapped_ubuf *imu = NULL; |
edafccee JA |
9526 | struct vm_area_struct **vmas = NULL; |
9527 | struct page **pages = NULL; | |
0a96bbe4 BM |
9528 | unsigned long off, start, end, ubuf; |
9529 | size_t size; | |
9530 | int ret, pret, nr_pages, i; | |
9531 | ||
6224843d PB |
9532 | if (!iov->iov_base) { |
9533 | *pimu = ctx->dummy_ubuf; | |
9534 | return 0; | |
9535 | } | |
9536 | ||
0a96bbe4 BM |
9537 | ubuf = (unsigned long) iov->iov_base; |
9538 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
9539 | start = ubuf >> PAGE_SHIFT; | |
9540 | nr_pages = end - start; | |
9541 | ||
41edf1a5 | 9542 | *pimu = NULL; |
0a96bbe4 BM |
9543 | ret = -ENOMEM; |
9544 | ||
9545 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); | |
9546 | if (!pages) | |
9547 | goto done; | |
9548 | ||
9549 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), | |
9550 | GFP_KERNEL); | |
9551 | if (!vmas) | |
9552 | goto done; | |
edafccee | 9553 | |
41edf1a5 | 9554 | imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL); |
a2b4198c | 9555 | if (!imu) |
0a96bbe4 BM |
9556 | goto done; |
9557 | ||
9558 | ret = 0; | |
9559 | mmap_read_lock(current->mm); | |
9560 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, | |
9561 | pages, vmas); | |
9562 | if (pret == nr_pages) { | |
9563 | /* don't support file backed memory */ | |
9564 | for (i = 0; i < nr_pages; i++) { | |
9565 | struct vm_area_struct *vma = vmas[i]; | |
9566 | ||
40dad765 PB |
9567 | if (vma_is_shmem(vma)) |
9568 | continue; | |
0a96bbe4 BM |
9569 | if (vma->vm_file && |
9570 | !is_file_hugepages(vma->vm_file)) { | |
9571 | ret = -EOPNOTSUPP; | |
9572 | break; | |
9573 | } | |
9574 | } | |
9575 | } else { | |
9576 | ret = pret < 0 ? pret : -EFAULT; | |
9577 | } | |
9578 | mmap_read_unlock(current->mm); | |
9579 | if (ret) { | |
9580 | /* | |
9581 | * if we did partial map, or found file backed vmas, | |
9582 | * release any pages we did get | |
9583 | */ | |
9584 | if (pret > 0) | |
9585 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
9586 | goto done; |
9587 | } | |
9588 | ||
9589 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); | |
9590 | if (ret) { | |
9591 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
9592 | goto done; |
9593 | } | |
9594 | ||
9595 | off = ubuf & ~PAGE_MASK; | |
9596 | size = iov->iov_len; | |
9597 | for (i = 0; i < nr_pages; i++) { | |
9598 | size_t vec_len; | |
9599 | ||
9600 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
9601 | imu->bvec[i].bv_page = pages[i]; | |
9602 | imu->bvec[i].bv_len = vec_len; | |
9603 | imu->bvec[i].bv_offset = off; | |
9604 | off = 0; | |
9605 | size -= vec_len; | |
9606 | } | |
9607 | /* store original address for later verification */ | |
9608 | imu->ubuf = ubuf; | |
4751f53d | 9609 | imu->ubuf_end = ubuf + iov->iov_len; |
0a96bbe4 | 9610 | imu->nr_bvecs = nr_pages; |
41edf1a5 | 9611 | *pimu = imu; |
0a96bbe4 BM |
9612 | ret = 0; |
9613 | done: | |
41edf1a5 PB |
9614 | if (ret) |
9615 | kvfree(imu); | |
0a96bbe4 BM |
9616 | kvfree(pages); |
9617 | kvfree(vmas); | |
9618 | return ret; | |
9619 | } | |
9620 | ||
2b358604 | 9621 | static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args) |
0a96bbe4 | 9622 | { |
87094465 PB |
9623 | ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL); |
9624 | return ctx->user_bufs ? 0 : -ENOMEM; | |
2b358604 | 9625 | } |
edafccee | 9626 | |
2b358604 BM |
9627 | static int io_buffer_validate(struct iovec *iov) |
9628 | { | |
50e96989 PB |
9629 | unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1); |
9630 | ||
2b358604 BM |
9631 | /* |
9632 | * Don't impose further limits on the size and buffer | |
9633 | * constraints here, we'll -EINVAL later when IO is | |
9634 | * submitted if they are wrong. | |
9635 | */ | |
6224843d PB |
9636 | if (!iov->iov_base) |
9637 | return iov->iov_len ? -EFAULT : 0; | |
9638 | if (!iov->iov_len) | |
2b358604 | 9639 | return -EFAULT; |
edafccee | 9640 | |
2b358604 BM |
9641 | /* arbitrary limit, but we need something */ |
9642 | if (iov->iov_len > SZ_1G) | |
9643 | return -EFAULT; | |
edafccee | 9644 | |
50e96989 PB |
9645 | if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp)) |
9646 | return -EOVERFLOW; | |
9647 | ||
2b358604 BM |
9648 | return 0; |
9649 | } | |
edafccee | 9650 | |
2b358604 | 9651 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
634d00df | 9652 | unsigned int nr_args, u64 __user *tags) |
2b358604 | 9653 | { |
bd54b6fe BM |
9654 | struct page *last_hpage = NULL; |
9655 | struct io_rsrc_data *data; | |
2b358604 BM |
9656 | int i, ret; |
9657 | struct iovec iov; | |
edafccee | 9658 | |
87094465 PB |
9659 | if (ctx->user_bufs) |
9660 | return -EBUSY; | |
489809e2 | 9661 | if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS) |
87094465 | 9662 | return -EINVAL; |
bd54b6fe | 9663 | ret = io_rsrc_node_switch_start(ctx); |
2b358604 BM |
9664 | if (ret) |
9665 | return ret; | |
d878c816 PB |
9666 | ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data); |
9667 | if (ret) | |
9668 | return ret; | |
bd54b6fe BM |
9669 | ret = io_buffers_map_alloc(ctx, nr_args); |
9670 | if (ret) { | |
bb6659cc | 9671 | io_rsrc_data_free(data); |
bd54b6fe BM |
9672 | return ret; |
9673 | } | |
edafccee | 9674 | |
87094465 | 9675 | for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) { |
edafccee JA |
9676 | ret = io_copy_iov(ctx, &iov, arg, i); |
9677 | if (ret) | |
0a96bbe4 | 9678 | break; |
2b358604 BM |
9679 | ret = io_buffer_validate(&iov); |
9680 | if (ret) | |
0a96bbe4 | 9681 | break; |
2d091d62 | 9682 | if (!iov.iov_base && *io_get_tag_slot(data, i)) { |
cf3770e7 CIK |
9683 | ret = -EINVAL; |
9684 | break; | |
9685 | } | |
edafccee | 9686 | |
41edf1a5 PB |
9687 | ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i], |
9688 | &last_hpage); | |
0a96bbe4 BM |
9689 | if (ret) |
9690 | break; | |
edafccee | 9691 | } |
0a96bbe4 | 9692 | |
bd54b6fe | 9693 | WARN_ON_ONCE(ctx->buf_data); |
0a96bbe4 | 9694 | |
bd54b6fe BM |
9695 | ctx->buf_data = data; |
9696 | if (ret) | |
9697 | __io_sqe_buffers_unregister(ctx); | |
9698 | else | |
9699 | io_rsrc_node_switch(ctx, NULL); | |
edafccee JA |
9700 | return ret; |
9701 | } | |
9702 | ||
634d00df PB |
9703 | static int __io_sqe_buffers_update(struct io_ring_ctx *ctx, |
9704 | struct io_uring_rsrc_update2 *up, | |
9705 | unsigned int nr_args) | |
9706 | { | |
9707 | u64 __user *tags = u64_to_user_ptr(up->tags); | |
9708 | struct iovec iov, __user *iovs = u64_to_user_ptr(up->data); | |
634d00df PB |
9709 | struct page *last_hpage = NULL; |
9710 | bool needs_switch = false; | |
9711 | __u32 done; | |
9712 | int i, err; | |
9713 | ||
9714 | if (!ctx->buf_data) | |
9715 | return -ENXIO; | |
9716 | if (up->offset + nr_args > ctx->nr_user_bufs) | |
9717 | return -EINVAL; | |
9718 | ||
9719 | for (done = 0; done < nr_args; done++) { | |
0b8c0e7c PB |
9720 | struct io_mapped_ubuf *imu; |
9721 | int offset = up->offset + done; | |
634d00df PB |
9722 | u64 tag = 0; |
9723 | ||
9724 | err = io_copy_iov(ctx, &iov, iovs, done); | |
9725 | if (err) | |
9726 | break; | |
9727 | if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) { | |
9728 | err = -EFAULT; | |
9729 | break; | |
9730 | } | |
0b8c0e7c PB |
9731 | err = io_buffer_validate(&iov); |
9732 | if (err) | |
9733 | break; | |
cf3770e7 CIK |
9734 | if (!iov.iov_base && tag) { |
9735 | err = -EINVAL; | |
9736 | break; | |
9737 | } | |
0b8c0e7c PB |
9738 | err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage); |
9739 | if (err) | |
9740 | break; | |
634d00df | 9741 | |
0b8c0e7c | 9742 | i = array_index_nospec(offset, ctx->nr_user_bufs); |
6224843d | 9743 | if (ctx->user_bufs[i] != ctx->dummy_ubuf) { |
4cdd158b | 9744 | err = io_queue_rsrc_removal(ctx->buf_data, i, |
0b8c0e7c PB |
9745 | ctx->rsrc_node, ctx->user_bufs[i]); |
9746 | if (unlikely(err)) { | |
9747 | io_buffer_unmap(ctx, &imu); | |
634d00df | 9748 | break; |
0b8c0e7c | 9749 | } |
634d00df PB |
9750 | ctx->user_bufs[i] = NULL; |
9751 | needs_switch = true; | |
9752 | } | |
9753 | ||
0b8c0e7c | 9754 | ctx->user_bufs[i] = imu; |
2d091d62 | 9755 | *io_get_tag_slot(ctx->buf_data, offset) = tag; |
634d00df PB |
9756 | } |
9757 | ||
9758 | if (needs_switch) | |
9759 | io_rsrc_node_switch(ctx, ctx->buf_data); | |
9760 | return done ? done : err; | |
9761 | } | |
9762 | ||
c75312dd UA |
9763 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg, |
9764 | unsigned int eventfd_async) | |
9b402849 | 9765 | { |
77bc59b4 | 9766 | struct io_ev_fd *ev_fd; |
9b402849 | 9767 | __s32 __user *fds = arg; |
f0a4e62b | 9768 | int fd; |
9b402849 | 9769 | |
77bc59b4 UA |
9770 | ev_fd = rcu_dereference_protected(ctx->io_ev_fd, |
9771 | lockdep_is_held(&ctx->uring_lock)); | |
9772 | if (ev_fd) | |
9b402849 JA |
9773 | return -EBUSY; |
9774 | ||
9775 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
9776 | return -EFAULT; | |
9777 | ||
77bc59b4 UA |
9778 | ev_fd = kmalloc(sizeof(*ev_fd), GFP_KERNEL); |
9779 | if (!ev_fd) | |
9780 | return -ENOMEM; | |
fe7e3257 | 9781 | |
77bc59b4 UA |
9782 | ev_fd->cq_ev_fd = eventfd_ctx_fdget(fd); |
9783 | if (IS_ERR(ev_fd->cq_ev_fd)) { | |
f0a4e62b | 9784 | int ret = PTR_ERR(ev_fd->cq_ev_fd); |
77bc59b4 | 9785 | kfree(ev_fd); |
9b402849 JA |
9786 | return ret; |
9787 | } | |
c75312dd | 9788 | ev_fd->eventfd_async = eventfd_async; |
9aa8dfde | 9789 | ctx->has_evfd = true; |
77bc59b4 | 9790 | rcu_assign_pointer(ctx->io_ev_fd, ev_fd); |
f0a4e62b | 9791 | return 0; |
77bc59b4 UA |
9792 | } |
9793 | ||
9794 | static void io_eventfd_put(struct rcu_head *rcu) | |
9795 | { | |
9796 | struct io_ev_fd *ev_fd = container_of(rcu, struct io_ev_fd, rcu); | |
9797 | ||
9798 | eventfd_ctx_put(ev_fd->cq_ev_fd); | |
9799 | kfree(ev_fd); | |
9b402849 JA |
9800 | } |
9801 | ||
9802 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
9803 | { | |
77bc59b4 UA |
9804 | struct io_ev_fd *ev_fd; |
9805 | ||
9806 | ev_fd = rcu_dereference_protected(ctx->io_ev_fd, | |
9807 | lockdep_is_held(&ctx->uring_lock)); | |
9808 | if (ev_fd) { | |
9aa8dfde | 9809 | ctx->has_evfd = false; |
77bc59b4 UA |
9810 | rcu_assign_pointer(ctx->io_ev_fd, NULL); |
9811 | call_rcu(&ev_fd->rcu, io_eventfd_put); | |
9b402849 JA |
9812 | return 0; |
9813 | } | |
9814 | ||
9815 | return -ENXIO; | |
9816 | } | |
9817 | ||
5a2e745d JA |
9818 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
9819 | { | |
dbc7d452 JA |
9820 | int i; |
9821 | ||
9822 | for (i = 0; i < (1U << IO_BUFFERS_HASH_BITS); i++) { | |
9823 | struct list_head *list = &ctx->io_buffers[i]; | |
9e15c3a0 | 9824 | |
dbc7d452 JA |
9825 | while (!list_empty(list)) { |
9826 | struct io_buffer_list *bl; | |
9827 | ||
9828 | bl = list_first_entry(list, struct io_buffer_list, list); | |
9829 | __io_remove_buffers(ctx, bl, -1U); | |
9830 | list_del(&bl->list); | |
9831 | kfree(bl); | |
9832 | } | |
9833 | } | |
cc3cec83 JA |
9834 | |
9835 | while (!list_empty(&ctx->io_buffers_pages)) { | |
9836 | struct page *page; | |
9837 | ||
9838 | page = list_first_entry(&ctx->io_buffers_pages, struct page, lru); | |
9839 | list_del_init(&page->lru); | |
9840 | __free_page(page); | |
9841 | } | |
5a2e745d JA |
9842 | } |
9843 | ||
4010fec4 | 9844 | static void io_req_caches_free(struct io_ring_ctx *ctx) |
2b188cc1 | 9845 | { |
cd0ca2e0 | 9846 | struct io_submit_state *state = &ctx->submit_state; |
37f0e767 | 9847 | int nr = 0; |
bf019da7 | 9848 | |
9a4fdbd8 | 9849 | mutex_lock(&ctx->uring_lock); |
cd0ca2e0 | 9850 | io_flush_cached_locked_reqs(ctx, state); |
9a4fdbd8 | 9851 | |
c2b6c6bc PB |
9852 | while (state->free_list.next) { |
9853 | struct io_wq_work_node *node; | |
9854 | struct io_kiocb *req; | |
9a4fdbd8 | 9855 | |
c2b6c6bc PB |
9856 | node = wq_stack_extract(&state->free_list); |
9857 | req = container_of(node, struct io_kiocb, comp_list); | |
9858 | kmem_cache_free(req_cachep, req); | |
37f0e767 | 9859 | nr++; |
c2b6c6bc | 9860 | } |
37f0e767 PB |
9861 | if (nr) |
9862 | percpu_ref_put_many(&ctx->refs, nr); | |
9a4fdbd8 JA |
9863 | mutex_unlock(&ctx->uring_lock); |
9864 | } | |
9865 | ||
43597aac | 9866 | static void io_wait_rsrc_data(struct io_rsrc_data *data) |
2b188cc1 | 9867 | { |
43597aac | 9868 | if (data && !atomic_dec_and_test(&data->refs)) |
bd54b6fe | 9869 | wait_for_completion(&data->done); |
bd54b6fe | 9870 | } |
04fc6c80 | 9871 | |
4d9237e3 JA |
9872 | static void io_flush_apoll_cache(struct io_ring_ctx *ctx) |
9873 | { | |
9874 | struct async_poll *apoll; | |
9875 | ||
9876 | while (!list_empty(&ctx->apoll_cache)) { | |
9877 | apoll = list_first_entry(&ctx->apoll_cache, struct async_poll, | |
9878 | poll.wait.entry); | |
9879 | list_del(&apoll->poll.wait.entry); | |
9880 | kfree(apoll); | |
9881 | } | |
9882 | } | |
9883 | ||
c072481d | 9884 | static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx) |
2b188cc1 | 9885 | { |
37d1e2e3 | 9886 | io_sq_thread_finish(ctx); |
2aede0e4 | 9887 | |
37d1e2e3 | 9888 | if (ctx->mm_account) { |
2aede0e4 JA |
9889 | mmdrop(ctx->mm_account); |
9890 | ctx->mm_account = NULL; | |
30975825 | 9891 | } |
def596e9 | 9892 | |
ab409402 | 9893 | io_rsrc_refs_drop(ctx); |
43597aac PB |
9894 | /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */ |
9895 | io_wait_rsrc_data(ctx->buf_data); | |
9896 | io_wait_rsrc_data(ctx->file_data); | |
9897 | ||
8bad28d8 | 9898 | mutex_lock(&ctx->uring_lock); |
43597aac | 9899 | if (ctx->buf_data) |
bd54b6fe | 9900 | __io_sqe_buffers_unregister(ctx); |
43597aac | 9901 | if (ctx->file_data) |
08480400 | 9902 | __io_sqe_files_unregister(ctx); |
c4ea060e PB |
9903 | if (ctx->rings) |
9904 | __io_cqring_overflow_flush(ctx, true); | |
9b402849 | 9905 | io_eventfd_unregister(ctx); |
4d9237e3 | 9906 | io_flush_apoll_cache(ctx); |
77bc59b4 | 9907 | mutex_unlock(&ctx->uring_lock); |
5a2e745d | 9908 | io_destroy_buffers(ctx); |
07db298a PB |
9909 | if (ctx->sq_creds) |
9910 | put_cred(ctx->sq_creds); | |
def596e9 | 9911 | |
a7f0ed5a PB |
9912 | /* there are no registered resources left, nobody uses it */ |
9913 | if (ctx->rsrc_node) | |
9914 | io_rsrc_node_destroy(ctx->rsrc_node); | |
8dd03afe | 9915 | if (ctx->rsrc_backup_node) |
b895c9a6 | 9916 | io_rsrc_node_destroy(ctx->rsrc_backup_node); |
a7f0ed5a | 9917 | flush_delayed_work(&ctx->rsrc_put_work); |
756ab7c0 | 9918 | flush_delayed_work(&ctx->fallback_work); |
a7f0ed5a PB |
9919 | |
9920 | WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list)); | |
9921 | WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist)); | |
def596e9 | 9922 | |
2b188cc1 | 9923 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
9924 | if (ctx->ring_sock) { |
9925 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 9926 | sock_release(ctx->ring_sock); |
355e8d26 | 9927 | } |
2b188cc1 | 9928 | #endif |
ef9dd637 | 9929 | WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list)); |
2b188cc1 | 9930 | |
75b28aff | 9931 | io_mem_free(ctx->rings); |
2b188cc1 | 9932 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
9933 | |
9934 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 9935 | free_uid(ctx->user); |
4010fec4 | 9936 | io_req_caches_free(ctx); |
e941894e JA |
9937 | if (ctx->hash_map) |
9938 | io_wq_put_hash(ctx->hash_map); | |
78076bb6 | 9939 | kfree(ctx->cancel_hash); |
6224843d | 9940 | kfree(ctx->dummy_ubuf); |
dbc7d452 | 9941 | kfree(ctx->io_buffers); |
2b188cc1 JA |
9942 | kfree(ctx); |
9943 | } | |
9944 | ||
9945 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
9946 | { | |
9947 | struct io_ring_ctx *ctx = file->private_data; | |
9948 | __poll_t mask = 0; | |
9949 | ||
d60aa65b | 9950 | poll_wait(file, &ctx->cq_wait, wait); |
4f7067c3 SB |
9951 | /* |
9952 | * synchronizes with barrier from wq_has_sleeper call in | |
9953 | * io_commit_cqring | |
9954 | */ | |
2b188cc1 | 9955 | smp_rmb(); |
90554200 | 9956 | if (!io_sqring_full(ctx)) |
2b188cc1 | 9957 | mask |= EPOLLOUT | EPOLLWRNORM; |
ed670c3f HX |
9958 | |
9959 | /* | |
9960 | * Don't flush cqring overflow list here, just do a simple check. | |
9961 | * Otherwise there could possible be ABBA deadlock: | |
9962 | * CPU0 CPU1 | |
9963 | * ---- ---- | |
9964 | * lock(&ctx->uring_lock); | |
9965 | * lock(&ep->mtx); | |
9966 | * lock(&ctx->uring_lock); | |
9967 | * lock(&ep->mtx); | |
9968 | * | |
9969 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this | |
9970 | * pushs them to do the flush. | |
9971 | */ | |
5ed7a37d | 9972 | if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow)) |
2b188cc1 JA |
9973 | mask |= EPOLLIN | EPOLLRDNORM; |
9974 | ||
9975 | return mask; | |
9976 | } | |
9977 | ||
0bead8cd | 9978 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
071698e1 | 9979 | { |
4379bf8b | 9980 | const struct cred *creds; |
071698e1 | 9981 | |
61cf9370 | 9982 | creds = xa_erase(&ctx->personalities, id); |
4379bf8b JA |
9983 | if (creds) { |
9984 | put_cred(creds); | |
0bead8cd | 9985 | return 0; |
1e6fa521 | 9986 | } |
0bead8cd YD |
9987 | |
9988 | return -EINVAL; | |
9989 | } | |
9990 | ||
d56d938b PB |
9991 | struct io_tctx_exit { |
9992 | struct callback_head task_work; | |
9993 | struct completion completion; | |
baf186c4 | 9994 | struct io_ring_ctx *ctx; |
d56d938b PB |
9995 | }; |
9996 | ||
c072481d | 9997 | static __cold void io_tctx_exit_cb(struct callback_head *cb) |
d56d938b PB |
9998 | { |
9999 | struct io_uring_task *tctx = current->io_uring; | |
10000 | struct io_tctx_exit *work; | |
10001 | ||
10002 | work = container_of(cb, struct io_tctx_exit, task_work); | |
10003 | /* | |
10004 | * When @in_idle, we're in cancellation and it's racy to remove the | |
10005 | * node. It'll be removed by the end of cancellation, just ignore it. | |
10006 | */ | |
10007 | if (!atomic_read(&tctx->in_idle)) | |
eef51daa | 10008 | io_uring_del_tctx_node((unsigned long)work->ctx); |
d56d938b PB |
10009 | complete(&work->completion); |
10010 | } | |
10011 | ||
c072481d | 10012 | static __cold bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
28090c13 PB |
10013 | { |
10014 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
10015 | ||
10016 | return req->ctx == data; | |
10017 | } | |
10018 | ||
c072481d | 10019 | static __cold void io_ring_exit_work(struct work_struct *work) |
85faa7b8 | 10020 | { |
d56d938b | 10021 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work); |
b5bb3a24 | 10022 | unsigned long timeout = jiffies + HZ * 60 * 5; |
58d3be2c | 10023 | unsigned long interval = HZ / 20; |
d56d938b PB |
10024 | struct io_tctx_exit exit; |
10025 | struct io_tctx_node *node; | |
10026 | int ret; | |
85faa7b8 | 10027 | |
56952e91 JA |
10028 | /* |
10029 | * If we're doing polled IO and end up having requests being | |
10030 | * submitted async (out-of-line), then completions can come in while | |
10031 | * we're waiting for refs to drop. We need to reap these manually, | |
10032 | * as nobody else will be looking for them. | |
10033 | */ | |
b2edc0a7 | 10034 | do { |
3dd0c97a | 10035 | io_uring_try_cancel_requests(ctx, NULL, true); |
28090c13 PB |
10036 | if (ctx->sq_data) { |
10037 | struct io_sq_data *sqd = ctx->sq_data; | |
10038 | struct task_struct *tsk; | |
10039 | ||
10040 | io_sq_thread_park(sqd); | |
10041 | tsk = sqd->thread; | |
10042 | if (tsk && tsk->io_uring && tsk->io_uring->io_wq) | |
10043 | io_wq_cancel_cb(tsk->io_uring->io_wq, | |
10044 | io_cancel_ctx_cb, ctx, true); | |
10045 | io_sq_thread_unpark(sqd); | |
10046 | } | |
b5bb3a24 | 10047 | |
37f0e767 PB |
10048 | io_req_caches_free(ctx); |
10049 | ||
58d3be2c PB |
10050 | if (WARN_ON_ONCE(time_after(jiffies, timeout))) { |
10051 | /* there is little hope left, don't run it too often */ | |
10052 | interval = HZ * 60; | |
10053 | } | |
10054 | } while (!wait_for_completion_timeout(&ctx->ref_comp, interval)); | |
d56d938b | 10055 | |
7f00651a PB |
10056 | init_completion(&exit.completion); |
10057 | init_task_work(&exit.task_work, io_tctx_exit_cb); | |
10058 | exit.ctx = ctx; | |
89b5066e PB |
10059 | /* |
10060 | * Some may use context even when all refs and requests have been put, | |
10061 | * and they are free to do so while still holding uring_lock or | |
5b0a6acc | 10062 | * completion_lock, see io_req_task_submit(). Apart from other work, |
89b5066e PB |
10063 | * this lock/unlock section also waits them to finish. |
10064 | */ | |
d56d938b PB |
10065 | mutex_lock(&ctx->uring_lock); |
10066 | while (!list_empty(&ctx->tctx_list)) { | |
b5bb3a24 PB |
10067 | WARN_ON_ONCE(time_after(jiffies, timeout)); |
10068 | ||
d56d938b PB |
10069 | node = list_first_entry(&ctx->tctx_list, struct io_tctx_node, |
10070 | ctx_node); | |
7f00651a PB |
10071 | /* don't spin on a single task if cancellation failed */ |
10072 | list_rotate_left(&ctx->tctx_list); | |
d56d938b PB |
10073 | ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL); |
10074 | if (WARN_ON_ONCE(ret)) | |
10075 | continue; | |
d56d938b PB |
10076 | |
10077 | mutex_unlock(&ctx->uring_lock); | |
10078 | wait_for_completion(&exit.completion); | |
d56d938b PB |
10079 | mutex_lock(&ctx->uring_lock); |
10080 | } | |
10081 | mutex_unlock(&ctx->uring_lock); | |
79ebeaee JA |
10082 | spin_lock(&ctx->completion_lock); |
10083 | spin_unlock(&ctx->completion_lock); | |
d56d938b | 10084 | |
85faa7b8 JA |
10085 | io_ring_ctx_free(ctx); |
10086 | } | |
10087 | ||
80c4cbdb | 10088 | /* Returns true if we found and killed one or more timeouts */ |
c072481d PB |
10089 | static __cold bool io_kill_timeouts(struct io_ring_ctx *ctx, |
10090 | struct task_struct *tsk, bool cancel_all) | |
80c4cbdb PB |
10091 | { |
10092 | struct io_kiocb *req, *tmp; | |
10093 | int canceled = 0; | |
10094 | ||
79ebeaee JA |
10095 | spin_lock(&ctx->completion_lock); |
10096 | spin_lock_irq(&ctx->timeout_lock); | |
80c4cbdb | 10097 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
3dd0c97a | 10098 | if (io_match_task(req, tsk, cancel_all)) { |
80c4cbdb PB |
10099 | io_kill_timeout(req, -ECANCELED); |
10100 | canceled++; | |
10101 | } | |
10102 | } | |
79ebeaee | 10103 | spin_unlock_irq(&ctx->timeout_lock); |
60053be8 | 10104 | io_commit_cqring(ctx); |
79ebeaee | 10105 | spin_unlock(&ctx->completion_lock); |
80c4cbdb PB |
10106 | if (canceled != 0) |
10107 | io_cqring_ev_posted(ctx); | |
10108 | return canceled != 0; | |
10109 | } | |
10110 | ||
c072481d | 10111 | static __cold void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
2b188cc1 | 10112 | { |
61cf9370 MWO |
10113 | unsigned long index; |
10114 | struct creds *creds; | |
10115 | ||
2b188cc1 JA |
10116 | mutex_lock(&ctx->uring_lock); |
10117 | percpu_ref_kill(&ctx->refs); | |
634578f8 | 10118 | if (ctx->rings) |
6c2450ae | 10119 | __io_cqring_overflow_flush(ctx, true); |
61cf9370 MWO |
10120 | xa_for_each(&ctx->personalities, index, creds) |
10121 | io_unregister_personality(ctx, index); | |
2b188cc1 JA |
10122 | mutex_unlock(&ctx->uring_lock); |
10123 | ||
60053be8 PB |
10124 | /* failed during ring init, it couldn't have issued any requests */ |
10125 | if (ctx->rings) { | |
10126 | io_kill_timeouts(ctx, NULL, true); | |
10127 | io_poll_remove_all(ctx, NULL, true); | |
10128 | /* if we failed setting up the ctx, we might not have any rings */ | |
10129 | io_iopoll_try_reap_events(ctx); | |
10130 | } | |
309fc03a | 10131 | |
85faa7b8 | 10132 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
10133 | /* |
10134 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
10135 | * if we're exiting a ton of rings at the same time. It just adds | |
10136 | * noise and overhead, there's no discernable change in runtime | |
10137 | * over using system_wq. | |
10138 | */ | |
10139 | queue_work(system_unbound_wq, &ctx->exit_work); | |
2b188cc1 JA |
10140 | } |
10141 | ||
10142 | static int io_uring_release(struct inode *inode, struct file *file) | |
10143 | { | |
10144 | struct io_ring_ctx *ctx = file->private_data; | |
10145 | ||
10146 | file->private_data = NULL; | |
10147 | io_ring_ctx_wait_and_kill(ctx); | |
10148 | return 0; | |
10149 | } | |
10150 | ||
f6edbabb PB |
10151 | struct io_task_cancel { |
10152 | struct task_struct *task; | |
3dd0c97a | 10153 | bool all; |
f6edbabb | 10154 | }; |
f254ac04 | 10155 | |
f6edbabb | 10156 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
b711d4ea | 10157 | { |
9a472ef7 | 10158 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
f6edbabb | 10159 | struct io_task_cancel *cancel = data; |
9a472ef7 | 10160 | |
6af3f48b | 10161 | return io_match_task_safe(req, cancel->task, cancel->all); |
b711d4ea JA |
10162 | } |
10163 | ||
c072481d PB |
10164 | static __cold bool io_cancel_defer_files(struct io_ring_ctx *ctx, |
10165 | struct task_struct *task, | |
10166 | bool cancel_all) | |
b7ddce3c | 10167 | { |
e1915f76 | 10168 | struct io_defer_entry *de; |
b7ddce3c PB |
10169 | LIST_HEAD(list); |
10170 | ||
79ebeaee | 10171 | spin_lock(&ctx->completion_lock); |
b7ddce3c | 10172 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
6af3f48b | 10173 | if (io_match_task_safe(de->req, task, cancel_all)) { |
b7ddce3c PB |
10174 | list_cut_position(&list, &ctx->defer_list, &de->list); |
10175 | break; | |
10176 | } | |
10177 | } | |
79ebeaee | 10178 | spin_unlock(&ctx->completion_lock); |
e1915f76 PB |
10179 | if (list_empty(&list)) |
10180 | return false; | |
b7ddce3c PB |
10181 | |
10182 | while (!list_empty(&list)) { | |
10183 | de = list_first_entry(&list, struct io_defer_entry, list); | |
10184 | list_del_init(&de->list); | |
f41db273 | 10185 | io_req_complete_failed(de->req, -ECANCELED); |
b7ddce3c PB |
10186 | kfree(de); |
10187 | } | |
e1915f76 | 10188 | return true; |
b7ddce3c PB |
10189 | } |
10190 | ||
c072481d | 10191 | static __cold bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx) |
1b00764f PB |
10192 | { |
10193 | struct io_tctx_node *node; | |
10194 | enum io_wq_cancel cret; | |
10195 | bool ret = false; | |
10196 | ||
10197 | mutex_lock(&ctx->uring_lock); | |
10198 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
10199 | struct io_uring_task *tctx = node->task->io_uring; | |
10200 | ||
10201 | /* | |
10202 | * io_wq will stay alive while we hold uring_lock, because it's | |
10203 | * killed after ctx nodes, which requires to take the lock. | |
10204 | */ | |
10205 | if (!tctx || !tctx->io_wq) | |
10206 | continue; | |
10207 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true); | |
10208 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
10209 | } | |
10210 | mutex_unlock(&ctx->uring_lock); | |
10211 | ||
10212 | return ret; | |
10213 | } | |
10214 | ||
c072481d PB |
10215 | static __cold void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
10216 | struct task_struct *task, | |
10217 | bool cancel_all) | |
9936c7c2 | 10218 | { |
3dd0c97a | 10219 | struct io_task_cancel cancel = { .task = task, .all = cancel_all, }; |
1b00764f | 10220 | struct io_uring_task *tctx = task ? task->io_uring : NULL; |
9936c7c2 | 10221 | |
60053be8 PB |
10222 | /* failed during ring init, it couldn't have issued any requests */ |
10223 | if (!ctx->rings) | |
10224 | return; | |
10225 | ||
9936c7c2 PB |
10226 | while (1) { |
10227 | enum io_wq_cancel cret; | |
10228 | bool ret = false; | |
10229 | ||
1b00764f PB |
10230 | if (!task) { |
10231 | ret |= io_uring_try_cancel_iowq(ctx); | |
10232 | } else if (tctx && tctx->io_wq) { | |
10233 | /* | |
10234 | * Cancels requests of all rings, not only @ctx, but | |
10235 | * it's fine as the task is in exit/exec. | |
10236 | */ | |
5aa75ed5 | 10237 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, |
9936c7c2 PB |
10238 | &cancel, true); |
10239 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
10240 | } | |
10241 | ||
10242 | /* SQPOLL thread does its own polling */ | |
3dd0c97a | 10243 | if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) || |
d052d1d6 | 10244 | (ctx->sq_data && ctx->sq_data->thread == current)) { |
5eef4e87 | 10245 | while (!wq_list_empty(&ctx->iopoll_list)) { |
9936c7c2 PB |
10246 | io_iopoll_try_reap_events(ctx); |
10247 | ret = true; | |
10248 | } | |
10249 | } | |
10250 | ||
3dd0c97a PB |
10251 | ret |= io_cancel_defer_files(ctx, task, cancel_all); |
10252 | ret |= io_poll_remove_all(ctx, task, cancel_all); | |
10253 | ret |= io_kill_timeouts(ctx, task, cancel_all); | |
e5dc480d PB |
10254 | if (task) |
10255 | ret |= io_run_task_work(); | |
9936c7c2 PB |
10256 | if (!ret) |
10257 | break; | |
10258 | cond_resched(); | |
10259 | } | |
10260 | } | |
10261 | ||
eef51daa | 10262 | static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
0f212204 | 10263 | { |
236434c3 | 10264 | struct io_uring_task *tctx = current->io_uring; |
13bf43f5 | 10265 | struct io_tctx_node *node; |
a528b04e | 10266 | int ret; |
236434c3 MWO |
10267 | |
10268 | if (unlikely(!tctx)) { | |
5aa75ed5 | 10269 | ret = io_uring_alloc_task_context(current, ctx); |
0f212204 JA |
10270 | if (unlikely(ret)) |
10271 | return ret; | |
e139a1ec | 10272 | |
236434c3 | 10273 | tctx = current->io_uring; |
e139a1ec PB |
10274 | if (ctx->iowq_limits_set) { |
10275 | unsigned int limits[2] = { ctx->iowq_limits[0], | |
10276 | ctx->iowq_limits[1], }; | |
10277 | ||
10278 | ret = io_wq_max_workers(tctx->io_wq, limits); | |
10279 | if (ret) | |
10280 | return ret; | |
10281 | } | |
0f212204 | 10282 | } |
cf27f3b1 PB |
10283 | if (!xa_load(&tctx->xa, (unsigned long)ctx)) { |
10284 | node = kmalloc(sizeof(*node), GFP_KERNEL); | |
10285 | if (!node) | |
10286 | return -ENOMEM; | |
10287 | node->ctx = ctx; | |
10288 | node->task = current; | |
13bf43f5 | 10289 | |
cf27f3b1 PB |
10290 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx, |
10291 | node, GFP_KERNEL)); | |
10292 | if (ret) { | |
10293 | kfree(node); | |
10294 | return ret; | |
0f212204 | 10295 | } |
cf27f3b1 PB |
10296 | |
10297 | mutex_lock(&ctx->uring_lock); | |
10298 | list_add(&node->ctx_node, &ctx->tctx_list); | |
10299 | mutex_unlock(&ctx->uring_lock); | |
0f212204 | 10300 | } |
cf27f3b1 | 10301 | tctx->last = ctx; |
0f212204 JA |
10302 | return 0; |
10303 | } | |
10304 | ||
cf27f3b1 PB |
10305 | /* |
10306 | * Note that this task has used io_uring. We use it for cancelation purposes. | |
10307 | */ | |
eef51daa | 10308 | static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
cf27f3b1 PB |
10309 | { |
10310 | struct io_uring_task *tctx = current->io_uring; | |
10311 | ||
10312 | if (likely(tctx && tctx->last == ctx)) | |
10313 | return 0; | |
eef51daa | 10314 | return __io_uring_add_tctx_node(ctx); |
cf27f3b1 PB |
10315 | } |
10316 | ||
0f212204 JA |
10317 | /* |
10318 | * Remove this io_uring_file -> task mapping. | |
10319 | */ | |
c072481d | 10320 | static __cold void io_uring_del_tctx_node(unsigned long index) |
0f212204 JA |
10321 | { |
10322 | struct io_uring_task *tctx = current->io_uring; | |
13bf43f5 | 10323 | struct io_tctx_node *node; |
2941267b | 10324 | |
eebd2e37 PB |
10325 | if (!tctx) |
10326 | return; | |
13bf43f5 PB |
10327 | node = xa_erase(&tctx->xa, index); |
10328 | if (!node) | |
2941267b | 10329 | return; |
0f212204 | 10330 | |
13bf43f5 PB |
10331 | WARN_ON_ONCE(current != node->task); |
10332 | WARN_ON_ONCE(list_empty(&node->ctx_node)); | |
10333 | ||
10334 | mutex_lock(&node->ctx->uring_lock); | |
10335 | list_del(&node->ctx_node); | |
10336 | mutex_unlock(&node->ctx->uring_lock); | |
10337 | ||
baf186c4 | 10338 | if (tctx->last == node->ctx) |
0f212204 | 10339 | tctx->last = NULL; |
13bf43f5 | 10340 | kfree(node); |
0f212204 JA |
10341 | } |
10342 | ||
c072481d | 10343 | static __cold void io_uring_clean_tctx(struct io_uring_task *tctx) |
de7f1d9e | 10344 | { |
ba5ef6dc | 10345 | struct io_wq *wq = tctx->io_wq; |
13bf43f5 | 10346 | struct io_tctx_node *node; |
de7f1d9e PB |
10347 | unsigned long index; |
10348 | ||
8bab4c09 | 10349 | xa_for_each(&tctx->xa, index, node) { |
eef51daa | 10350 | io_uring_del_tctx_node(index); |
8bab4c09 JA |
10351 | cond_resched(); |
10352 | } | |
b16ef427 ME |
10353 | if (wq) { |
10354 | /* | |
f6f9b278 | 10355 | * Must be after io_uring_del_tctx_node() (removes nodes under |
b16ef427 ME |
10356 | * uring_lock) to avoid race with io_uring_try_cancel_iowq(). |
10357 | */ | |
ba5ef6dc | 10358 | io_wq_put_and_exit(wq); |
dadebc35 | 10359 | tctx->io_wq = NULL; |
b16ef427 | 10360 | } |
de7f1d9e PB |
10361 | } |
10362 | ||
3f48cf18 | 10363 | static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked) |
521d6a73 | 10364 | { |
3f48cf18 | 10365 | if (tracked) |
d5361233 | 10366 | return 0; |
521d6a73 PB |
10367 | return percpu_counter_sum(&tctx->inflight); |
10368 | } | |
10369 | ||
78cc687b PB |
10370 | /* |
10371 | * Find any io_uring ctx that this task has registered or done IO on, and cancel | |
78a78060 | 10372 | * requests. @sqd should be not-null IFF it's an SQPOLL thread cancellation. |
78cc687b | 10373 | */ |
c072481d PB |
10374 | static __cold void io_uring_cancel_generic(bool cancel_all, |
10375 | struct io_sq_data *sqd) | |
0e9ddb39 | 10376 | { |
521d6a73 | 10377 | struct io_uring_task *tctx = current->io_uring; |
734551df | 10378 | struct io_ring_ctx *ctx; |
0e9ddb39 PB |
10379 | s64 inflight; |
10380 | DEFINE_WAIT(wait); | |
fdaf083c | 10381 | |
78cc687b PB |
10382 | WARN_ON_ONCE(sqd && sqd->thread != current); |
10383 | ||
6d042ffb PO |
10384 | if (!current->io_uring) |
10385 | return; | |
17a91051 PB |
10386 | if (tctx->io_wq) |
10387 | io_wq_exit_start(tctx->io_wq); | |
10388 | ||
0e9ddb39 PB |
10389 | atomic_inc(&tctx->in_idle); |
10390 | do { | |
e9dbe221 | 10391 | io_uring_drop_tctx_refs(current); |
0e9ddb39 | 10392 | /* read completions before cancelations */ |
78cc687b | 10393 | inflight = tctx_inflight(tctx, !cancel_all); |
0e9ddb39 PB |
10394 | if (!inflight) |
10395 | break; | |
fdaf083c | 10396 | |
78cc687b PB |
10397 | if (!sqd) { |
10398 | struct io_tctx_node *node; | |
10399 | unsigned long index; | |
0f212204 | 10400 | |
78cc687b PB |
10401 | xa_for_each(&tctx->xa, index, node) { |
10402 | /* sqpoll task will cancel all its requests */ | |
10403 | if (node->ctx->sq_data) | |
10404 | continue; | |
10405 | io_uring_try_cancel_requests(node->ctx, current, | |
10406 | cancel_all); | |
10407 | } | |
10408 | } else { | |
10409 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) | |
10410 | io_uring_try_cancel_requests(ctx, current, | |
10411 | cancel_all); | |
10412 | } | |
17a91051 | 10413 | |
78a78060 JA |
10414 | prepare_to_wait(&tctx->wait, &wait, TASK_INTERRUPTIBLE); |
10415 | io_run_task_work(); | |
e9dbe221 | 10416 | io_uring_drop_tctx_refs(current); |
78a78060 | 10417 | |
0f212204 | 10418 | /* |
a1bb3cd5 PB |
10419 | * If we've seen completions, retry without waiting. This |
10420 | * avoids a race where a completion comes in before we did | |
10421 | * prepare_to_wait(). | |
0f212204 | 10422 | */ |
3dd0c97a | 10423 | if (inflight == tctx_inflight(tctx, !cancel_all)) |
a1bb3cd5 | 10424 | schedule(); |
f57555ed | 10425 | finish_wait(&tctx->wait, &wait); |
d8a6df10 | 10426 | } while (1); |
de7f1d9e | 10427 | |
8452d4a6 | 10428 | io_uring_clean_tctx(tctx); |
3dd0c97a | 10429 | if (cancel_all) { |
3cc7fdb9 PB |
10430 | /* |
10431 | * We shouldn't run task_works after cancel, so just leave | |
10432 | * ->in_idle set for normal exit. | |
10433 | */ | |
10434 | atomic_dec(&tctx->in_idle); | |
3f48cf18 PB |
10435 | /* for exec all current's requests should be gone, kill tctx */ |
10436 | __io_uring_free(current); | |
10437 | } | |
44e728b8 PB |
10438 | } |
10439 | ||
f552a27a | 10440 | void __io_uring_cancel(bool cancel_all) |
78cc687b | 10441 | { |
f552a27a | 10442 | io_uring_cancel_generic(cancel_all, NULL); |
78cc687b PB |
10443 | } |
10444 | ||
e7a6c00d JA |
10445 | void io_uring_unreg_ringfd(void) |
10446 | { | |
10447 | struct io_uring_task *tctx = current->io_uring; | |
10448 | int i; | |
10449 | ||
10450 | for (i = 0; i < IO_RINGFD_REG_MAX; i++) { | |
10451 | if (tctx->registered_rings[i]) { | |
10452 | fput(tctx->registered_rings[i]); | |
10453 | tctx->registered_rings[i] = NULL; | |
10454 | } | |
10455 | } | |
10456 | } | |
10457 | ||
10458 | static int io_ring_add_registered_fd(struct io_uring_task *tctx, int fd, | |
10459 | int start, int end) | |
10460 | { | |
10461 | struct file *file; | |
10462 | int offset; | |
10463 | ||
10464 | for (offset = start; offset < end; offset++) { | |
10465 | offset = array_index_nospec(offset, IO_RINGFD_REG_MAX); | |
10466 | if (tctx->registered_rings[offset]) | |
10467 | continue; | |
10468 | ||
10469 | file = fget(fd); | |
10470 | if (!file) { | |
10471 | return -EBADF; | |
10472 | } else if (file->f_op != &io_uring_fops) { | |
10473 | fput(file); | |
10474 | return -EOPNOTSUPP; | |
10475 | } | |
10476 | tctx->registered_rings[offset] = file; | |
10477 | return offset; | |
10478 | } | |
10479 | ||
10480 | return -EBUSY; | |
10481 | } | |
10482 | ||
10483 | /* | |
10484 | * Register a ring fd to avoid fdget/fdput for each io_uring_enter() | |
10485 | * invocation. User passes in an array of struct io_uring_rsrc_update | |
10486 | * with ->data set to the ring_fd, and ->offset given for the desired | |
10487 | * index. If no index is desired, application may set ->offset == -1U | |
10488 | * and we'll find an available index. Returns number of entries | |
10489 | * successfully processed, or < 0 on error if none were processed. | |
10490 | */ | |
10491 | static int io_ringfd_register(struct io_ring_ctx *ctx, void __user *__arg, | |
10492 | unsigned nr_args) | |
10493 | { | |
10494 | struct io_uring_rsrc_update __user *arg = __arg; | |
10495 | struct io_uring_rsrc_update reg; | |
10496 | struct io_uring_task *tctx; | |
10497 | int ret, i; | |
10498 | ||
10499 | if (!nr_args || nr_args > IO_RINGFD_REG_MAX) | |
10500 | return -EINVAL; | |
10501 | ||
10502 | mutex_unlock(&ctx->uring_lock); | |
10503 | ret = io_uring_add_tctx_node(ctx); | |
10504 | mutex_lock(&ctx->uring_lock); | |
10505 | if (ret) | |
10506 | return ret; | |
10507 | ||
10508 | tctx = current->io_uring; | |
10509 | for (i = 0; i < nr_args; i++) { | |
10510 | int start, end; | |
10511 | ||
10512 | if (copy_from_user(®, &arg[i], sizeof(reg))) { | |
10513 | ret = -EFAULT; | |
10514 | break; | |
10515 | } | |
10516 | ||
6fb53cf8 DY |
10517 | if (reg.resv) { |
10518 | ret = -EINVAL; | |
10519 | break; | |
10520 | } | |
10521 | ||
e7a6c00d JA |
10522 | if (reg.offset == -1U) { |
10523 | start = 0; | |
10524 | end = IO_RINGFD_REG_MAX; | |
10525 | } else { | |
10526 | if (reg.offset >= IO_RINGFD_REG_MAX) { | |
10527 | ret = -EINVAL; | |
10528 | break; | |
10529 | } | |
10530 | start = reg.offset; | |
10531 | end = start + 1; | |
10532 | } | |
10533 | ||
10534 | ret = io_ring_add_registered_fd(tctx, reg.data, start, end); | |
10535 | if (ret < 0) | |
10536 | break; | |
10537 | ||
10538 | reg.offset = ret; | |
10539 | if (copy_to_user(&arg[i], ®, sizeof(reg))) { | |
10540 | fput(tctx->registered_rings[reg.offset]); | |
10541 | tctx->registered_rings[reg.offset] = NULL; | |
10542 | ret = -EFAULT; | |
10543 | break; | |
10544 | } | |
10545 | } | |
10546 | ||
10547 | return i ? i : ret; | |
10548 | } | |
10549 | ||
10550 | static int io_ringfd_unregister(struct io_ring_ctx *ctx, void __user *__arg, | |
10551 | unsigned nr_args) | |
10552 | { | |
10553 | struct io_uring_rsrc_update __user *arg = __arg; | |
10554 | struct io_uring_task *tctx = current->io_uring; | |
10555 | struct io_uring_rsrc_update reg; | |
10556 | int ret = 0, i; | |
10557 | ||
10558 | if (!nr_args || nr_args > IO_RINGFD_REG_MAX) | |
10559 | return -EINVAL; | |
10560 | if (!tctx) | |
10561 | return 0; | |
10562 | ||
10563 | for (i = 0; i < nr_args; i++) { | |
10564 | if (copy_from_user(®, &arg[i], sizeof(reg))) { | |
10565 | ret = -EFAULT; | |
10566 | break; | |
10567 | } | |
6fb53cf8 | 10568 | if (reg.resv || reg.offset >= IO_RINGFD_REG_MAX) { |
e7a6c00d JA |
10569 | ret = -EINVAL; |
10570 | break; | |
10571 | } | |
10572 | ||
10573 | reg.offset = array_index_nospec(reg.offset, IO_RINGFD_REG_MAX); | |
10574 | if (tctx->registered_rings[reg.offset]) { | |
10575 | fput(tctx->registered_rings[reg.offset]); | |
10576 | tctx->registered_rings[reg.offset] = NULL; | |
10577 | } | |
10578 | } | |
10579 | ||
10580 | return i ? i : ret; | |
10581 | } | |
10582 | ||
6c5c240e RP |
10583 | static void *io_uring_validate_mmap_request(struct file *file, |
10584 | loff_t pgoff, size_t sz) | |
2b188cc1 | 10585 | { |
2b188cc1 | 10586 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 10587 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
10588 | struct page *page; |
10589 | void *ptr; | |
10590 | ||
10591 | switch (offset) { | |
10592 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
10593 | case IORING_OFF_CQ_RING: |
10594 | ptr = ctx->rings; | |
2b188cc1 JA |
10595 | break; |
10596 | case IORING_OFF_SQES: | |
10597 | ptr = ctx->sq_sqes; | |
10598 | break; | |
2b188cc1 | 10599 | default: |
6c5c240e | 10600 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
10601 | } |
10602 | ||
10603 | page = virt_to_head_page(ptr); | |
a50b854e | 10604 | if (sz > page_size(page)) |
6c5c240e RP |
10605 | return ERR_PTR(-EINVAL); |
10606 | ||
10607 | return ptr; | |
10608 | } | |
10609 | ||
10610 | #ifdef CONFIG_MMU | |
10611 | ||
c072481d | 10612 | static __cold int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
6c5c240e RP |
10613 | { |
10614 | size_t sz = vma->vm_end - vma->vm_start; | |
10615 | unsigned long pfn; | |
10616 | void *ptr; | |
10617 | ||
10618 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
10619 | if (IS_ERR(ptr)) | |
10620 | return PTR_ERR(ptr); | |
2b188cc1 JA |
10621 | |
10622 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
10623 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
10624 | } | |
10625 | ||
6c5c240e RP |
10626 | #else /* !CONFIG_MMU */ |
10627 | ||
10628 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
10629 | { | |
10630 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
10631 | } | |
10632 | ||
10633 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
10634 | { | |
10635 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
10636 | } | |
10637 | ||
10638 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
10639 | unsigned long addr, unsigned long len, | |
10640 | unsigned long pgoff, unsigned long flags) | |
10641 | { | |
10642 | void *ptr; | |
10643 | ||
10644 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
10645 | if (IS_ERR(ptr)) | |
10646 | return PTR_ERR(ptr); | |
10647 | ||
10648 | return (unsigned long) ptr; | |
10649 | } | |
10650 | ||
10651 | #endif /* !CONFIG_MMU */ | |
10652 | ||
d9d05217 | 10653 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
90554200 JA |
10654 | { |
10655 | DEFINE_WAIT(wait); | |
10656 | ||
10657 | do { | |
10658 | if (!io_sqring_full(ctx)) | |
10659 | break; | |
90554200 JA |
10660 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
10661 | ||
10662 | if (!io_sqring_full(ctx)) | |
10663 | break; | |
90554200 JA |
10664 | schedule(); |
10665 | } while (!signal_pending(current)); | |
10666 | ||
10667 | finish_wait(&ctx->sqo_sq_wait, &wait); | |
5199328a | 10668 | return 0; |
90554200 JA |
10669 | } |
10670 | ||
f81440d3 PB |
10671 | static int io_validate_ext_arg(unsigned flags, const void __user *argp, size_t argsz) |
10672 | { | |
10673 | if (flags & IORING_ENTER_EXT_ARG) { | |
10674 | struct io_uring_getevents_arg arg; | |
10675 | ||
10676 | if (argsz != sizeof(arg)) | |
10677 | return -EINVAL; | |
10678 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
10679 | return -EFAULT; | |
10680 | } | |
10681 | return 0; | |
10682 | } | |
10683 | ||
c73ebb68 HX |
10684 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
10685 | struct __kernel_timespec __user **ts, | |
10686 | const sigset_t __user **sig) | |
10687 | { | |
10688 | struct io_uring_getevents_arg arg; | |
10689 | ||
10690 | /* | |
10691 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer | |
10692 | * is just a pointer to the sigset_t. | |
10693 | */ | |
10694 | if (!(flags & IORING_ENTER_EXT_ARG)) { | |
10695 | *sig = (const sigset_t __user *) argp; | |
10696 | *ts = NULL; | |
10697 | return 0; | |
10698 | } | |
10699 | ||
10700 | /* | |
10701 | * EXT_ARG is set - ensure we agree on the size of it and copy in our | |
10702 | * timespec and sigset_t pointers if good. | |
10703 | */ | |
10704 | if (*argsz != sizeof(arg)) | |
10705 | return -EINVAL; | |
10706 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
10707 | return -EFAULT; | |
d2347b96 DY |
10708 | if (arg.pad) |
10709 | return -EINVAL; | |
c73ebb68 HX |
10710 | *sig = u64_to_user_ptr(arg.sigmask); |
10711 | *argsz = arg.sigmask_sz; | |
10712 | *ts = u64_to_user_ptr(arg.ts); | |
10713 | return 0; | |
10714 | } | |
10715 | ||
2b188cc1 | 10716 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
c73ebb68 HX |
10717 | u32, min_complete, u32, flags, const void __user *, argp, |
10718 | size_t, argsz) | |
2b188cc1 JA |
10719 | { |
10720 | struct io_ring_ctx *ctx; | |
2b188cc1 JA |
10721 | int submitted = 0; |
10722 | struct fd f; | |
33f993da | 10723 | long ret; |
2b188cc1 | 10724 | |
4c6e277c | 10725 | io_run_task_work(); |
b41e9852 | 10726 | |
33f993da | 10727 | if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
e7a6c00d JA |
10728 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG | |
10729 | IORING_ENTER_REGISTERED_RING))) | |
2b188cc1 JA |
10730 | return -EINVAL; |
10731 | ||
e7a6c00d JA |
10732 | /* |
10733 | * Ring fd has been registered via IORING_REGISTER_RING_FDS, we | |
10734 | * need only dereference our task private array to find it. | |
10735 | */ | |
10736 | if (flags & IORING_ENTER_REGISTERED_RING) { | |
10737 | struct io_uring_task *tctx = current->io_uring; | |
10738 | ||
10739 | if (!tctx || fd >= IO_RINGFD_REG_MAX) | |
10740 | return -EINVAL; | |
10741 | fd = array_index_nospec(fd, IO_RINGFD_REG_MAX); | |
10742 | f.file = tctx->registered_rings[fd]; | |
10743 | if (unlikely(!f.file)) | |
10744 | return -EBADF; | |
10745 | } else { | |
10746 | f = fdget(fd); | |
10747 | if (unlikely(!f.file)) | |
10748 | return -EBADF; | |
10749 | } | |
2b188cc1 JA |
10750 | |
10751 | ret = -EOPNOTSUPP; | |
33f993da | 10752 | if (unlikely(f.file->f_op != &io_uring_fops)) |
2b188cc1 JA |
10753 | goto out_fput; |
10754 | ||
10755 | ret = -ENXIO; | |
10756 | ctx = f.file->private_data; | |
33f993da | 10757 | if (unlikely(!percpu_ref_tryget(&ctx->refs))) |
2b188cc1 JA |
10758 | goto out_fput; |
10759 | ||
7e84e1c7 | 10760 | ret = -EBADFD; |
33f993da | 10761 | if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED)) |
7e84e1c7 SG |
10762 | goto out; |
10763 | ||
6c271ce2 JA |
10764 | /* |
10765 | * For SQ polling, the thread will do all submissions and completions. | |
10766 | * Just return the requested submit count, and wake the thread if | |
10767 | * we were asked to. | |
10768 | */ | |
b2a9eada | 10769 | ret = 0; |
6c271ce2 | 10770 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
90f67366 | 10771 | io_cqring_overflow_flush(ctx); |
89448c47 | 10772 | |
21f96522 JA |
10773 | if (unlikely(ctx->sq_data->thread == NULL)) { |
10774 | ret = -EOWNERDEAD; | |
04147488 | 10775 | goto out; |
21f96522 | 10776 | } |
6c271ce2 | 10777 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 10778 | wake_up(&ctx->sq_data->wait); |
d9d05217 PB |
10779 | if (flags & IORING_ENTER_SQ_WAIT) { |
10780 | ret = io_sqpoll_wait_sq(ctx); | |
10781 | if (ret) | |
10782 | goto out; | |
10783 | } | |
6c271ce2 | 10784 | submitted = to_submit; |
b2a9eada | 10785 | } else if (to_submit) { |
eef51daa | 10786 | ret = io_uring_add_tctx_node(ctx); |
0f212204 JA |
10787 | if (unlikely(ret)) |
10788 | goto out; | |
d487b43c | 10789 | |
2b188cc1 | 10790 | mutex_lock(&ctx->uring_lock); |
0f212204 | 10791 | submitted = io_submit_sqes(ctx, to_submit); |
d487b43c PB |
10792 | if (submitted != to_submit) { |
10793 | mutex_unlock(&ctx->uring_lock); | |
7c504e65 | 10794 | goto out; |
d487b43c PB |
10795 | } |
10796 | if ((flags & IORING_ENTER_GETEVENTS) && ctx->syscall_iopoll) | |
10797 | goto iopoll_locked; | |
10798 | mutex_unlock(&ctx->uring_lock); | |
2b188cc1 JA |
10799 | } |
10800 | if (flags & IORING_ENTER_GETEVENTS) { | |
773697b6 | 10801 | if (ctx->syscall_iopoll) { |
d487b43c PB |
10802 | /* |
10803 | * We disallow the app entering submit/complete with | |
10804 | * polling, but we still need to lock the ring to | |
10805 | * prevent racing with polled issue that got punted to | |
10806 | * a workqueue. | |
10807 | */ | |
10808 | mutex_lock(&ctx->uring_lock); | |
10809 | iopoll_locked: | |
f81440d3 | 10810 | ret = io_validate_ext_arg(flags, argp, argsz); |
d487b43c PB |
10811 | if (likely(!ret)) { |
10812 | min_complete = min(min_complete, ctx->cq_entries); | |
10813 | ret = io_iopoll_check(ctx, min_complete); | |
10814 | } | |
10815 | mutex_unlock(&ctx->uring_lock); | |
def596e9 | 10816 | } else { |
f81440d3 PB |
10817 | const sigset_t __user *sig; |
10818 | struct __kernel_timespec __user *ts; | |
10819 | ||
10820 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); | |
10821 | if (unlikely(ret)) | |
10822 | goto out; | |
d487b43c | 10823 | min_complete = min(min_complete, ctx->cq_entries); |
c73ebb68 | 10824 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
def596e9 | 10825 | } |
2b188cc1 JA |
10826 | } |
10827 | ||
7c504e65 | 10828 | out: |
6805b32e | 10829 | percpu_ref_put(&ctx->refs); |
2b188cc1 | 10830 | out_fput: |
e7a6c00d JA |
10831 | if (!(flags & IORING_ENTER_REGISTERED_RING)) |
10832 | fdput(f); | |
2b188cc1 JA |
10833 | return submitted ? submitted : ret; |
10834 | } | |
10835 | ||
bebdb65e | 10836 | #ifdef CONFIG_PROC_FS |
c072481d | 10837 | static __cold int io_uring_show_cred(struct seq_file *m, unsigned int id, |
61cf9370 | 10838 | const struct cred *cred) |
87ce955b | 10839 | { |
87ce955b JA |
10840 | struct user_namespace *uns = seq_user_ns(m); |
10841 | struct group_info *gi; | |
10842 | kernel_cap_t cap; | |
10843 | unsigned __capi; | |
10844 | int g; | |
10845 | ||
10846 | seq_printf(m, "%5d\n", id); | |
10847 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
10848 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
10849 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
10850 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
10851 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
10852 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
10853 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
10854 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
10855 | seq_puts(m, "\n\tGroups:\t"); | |
10856 | gi = cred->group_info; | |
10857 | for (g = 0; g < gi->ngroups; g++) { | |
10858 | seq_put_decimal_ull(m, g ? " " : "", | |
10859 | from_kgid_munged(uns, gi->gid[g])); | |
10860 | } | |
10861 | seq_puts(m, "\n\tCapEff:\t"); | |
10862 | cap = cred->cap_effective; | |
10863 | CAP_FOR_EACH_U32(__capi) | |
10864 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
10865 | seq_putc(m, '\n'); | |
10866 | return 0; | |
10867 | } | |
10868 | ||
c072481d PB |
10869 | static __cold void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, |
10870 | struct seq_file *m) | |
87ce955b | 10871 | { |
dbbe9c64 | 10872 | struct io_sq_data *sq = NULL; |
83f84356 HX |
10873 | struct io_overflow_cqe *ocqe; |
10874 | struct io_rings *r = ctx->rings; | |
10875 | unsigned int sq_mask = ctx->sq_entries - 1, cq_mask = ctx->cq_entries - 1; | |
83f84356 HX |
10876 | unsigned int sq_head = READ_ONCE(r->sq.head); |
10877 | unsigned int sq_tail = READ_ONCE(r->sq.tail); | |
10878 | unsigned int cq_head = READ_ONCE(r->cq.head); | |
10879 | unsigned int cq_tail = READ_ONCE(r->cq.tail); | |
f75d1183 | 10880 | unsigned int sq_entries, cq_entries; |
fad8e0de | 10881 | bool has_lock; |
83f84356 HX |
10882 | unsigned int i; |
10883 | ||
10884 | /* | |
10885 | * we may get imprecise sqe and cqe info if uring is actively running | |
10886 | * since we get cached_sq_head and cached_cq_tail without uring_lock | |
10887 | * and sq_tail and cq_head are changed by userspace. But it's ok since | |
10888 | * we usually use these info when it is stuck. | |
10889 | */ | |
c0235652 | 10890 | seq_printf(m, "SqMask:\t0x%x\n", sq_mask); |
f75d1183 JA |
10891 | seq_printf(m, "SqHead:\t%u\n", sq_head); |
10892 | seq_printf(m, "SqTail:\t%u\n", sq_tail); | |
10893 | seq_printf(m, "CachedSqHead:\t%u\n", ctx->cached_sq_head); | |
10894 | seq_printf(m, "CqMask:\t0x%x\n", cq_mask); | |
10895 | seq_printf(m, "CqHead:\t%u\n", cq_head); | |
10896 | seq_printf(m, "CqTail:\t%u\n", cq_tail); | |
10897 | seq_printf(m, "CachedCqTail:\t%u\n", ctx->cached_cq_tail); | |
10898 | seq_printf(m, "SQEs:\t%u\n", sq_tail - ctx->cached_sq_head); | |
10899 | sq_entries = min(sq_tail - sq_head, ctx->sq_entries); | |
10900 | for (i = 0; i < sq_entries; i++) { | |
10901 | unsigned int entry = i + sq_head; | |
10902 | unsigned int sq_idx = READ_ONCE(ctx->sq_array[entry & sq_mask]); | |
a1957780 | 10903 | struct io_uring_sqe *sqe; |
f75d1183 JA |
10904 | |
10905 | if (sq_idx > sq_mask) | |
10906 | continue; | |
10907 | sqe = &ctx->sq_sqes[sq_idx]; | |
10908 | seq_printf(m, "%5u: opcode:%d, fd:%d, flags:%x, user_data:%llu\n", | |
10909 | sq_idx, sqe->opcode, sqe->fd, sqe->flags, | |
10910 | sqe->user_data); | |
83f84356 | 10911 | } |
f75d1183 JA |
10912 | seq_printf(m, "CQEs:\t%u\n", cq_tail - cq_head); |
10913 | cq_entries = min(cq_tail - cq_head, ctx->cq_entries); | |
10914 | for (i = 0; i < cq_entries; i++) { | |
10915 | unsigned int entry = i + cq_head; | |
10916 | struct io_uring_cqe *cqe = &r->cqes[entry & cq_mask]; | |
83f84356 HX |
10917 | |
10918 | seq_printf(m, "%5u: user_data:%llu, res:%d, flag:%x\n", | |
f75d1183 JA |
10919 | entry & cq_mask, cqe->user_data, cqe->res, |
10920 | cqe->flags); | |
83f84356 | 10921 | } |
87ce955b | 10922 | |
fad8e0de JA |
10923 | /* |
10924 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, | |
10925 | * since fdinfo case grabs it in the opposite direction of normal use | |
10926 | * cases. If we fail to get the lock, we just don't iterate any | |
10927 | * structures that could be going away outside the io_uring mutex. | |
10928 | */ | |
10929 | has_lock = mutex_trylock(&ctx->uring_lock); | |
10930 | ||
5f3f26f9 | 10931 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) { |
dbbe9c64 | 10932 | sq = ctx->sq_data; |
5f3f26f9 JA |
10933 | if (!sq->thread) |
10934 | sq = NULL; | |
10935 | } | |
dbbe9c64 JQ |
10936 | |
10937 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); | |
10938 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); | |
87ce955b | 10939 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
fad8e0de | 10940 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
7b29f92d | 10941 | struct file *f = io_file_from_index(ctx, i); |
87ce955b | 10942 | |
87ce955b JA |
10943 | if (f) |
10944 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
10945 | else | |
10946 | seq_printf(m, "%5u: <none>\n", i); | |
10947 | } | |
10948 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
fad8e0de | 10949 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
41edf1a5 | 10950 | struct io_mapped_ubuf *buf = ctx->user_bufs[i]; |
4751f53d | 10951 | unsigned int len = buf->ubuf_end - buf->ubuf; |
87ce955b | 10952 | |
4751f53d | 10953 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len); |
87ce955b | 10954 | } |
61cf9370 MWO |
10955 | if (has_lock && !xa_empty(&ctx->personalities)) { |
10956 | unsigned long index; | |
10957 | const struct cred *cred; | |
10958 | ||
87ce955b | 10959 | seq_printf(m, "Personalities:\n"); |
61cf9370 MWO |
10960 | xa_for_each(&ctx->personalities, index, cred) |
10961 | io_uring_show_cred(m, index, cred); | |
87ce955b | 10962 | } |
83f84356 HX |
10963 | if (has_lock) |
10964 | mutex_unlock(&ctx->uring_lock); | |
10965 | ||
10966 | seq_puts(m, "PollList:\n"); | |
79ebeaee | 10967 | spin_lock(&ctx->completion_lock); |
d7718a9d JA |
10968 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
10969 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
10970 | struct io_kiocb *req; | |
10971 | ||
10972 | hlist_for_each_entry(req, list, hash_node) | |
10973 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
7f62d40d | 10974 | task_work_pending(req->task)); |
d7718a9d | 10975 | } |
83f84356 HX |
10976 | |
10977 | seq_puts(m, "CqOverflowList:\n"); | |
10978 | list_for_each_entry(ocqe, &ctx->cq_overflow_list, list) { | |
10979 | struct io_uring_cqe *cqe = &ocqe->cqe; | |
10980 | ||
10981 | seq_printf(m, " user_data=%llu, res=%d, flags=%x\n", | |
10982 | cqe->user_data, cqe->res, cqe->flags); | |
10983 | ||
10984 | } | |
10985 | ||
79ebeaee | 10986 | spin_unlock(&ctx->completion_lock); |
87ce955b JA |
10987 | } |
10988 | ||
c072481d | 10989 | static __cold void io_uring_show_fdinfo(struct seq_file *m, struct file *f) |
87ce955b JA |
10990 | { |
10991 | struct io_ring_ctx *ctx = f->private_data; | |
10992 | ||
10993 | if (percpu_ref_tryget(&ctx->refs)) { | |
10994 | __io_uring_show_fdinfo(ctx, m); | |
10995 | percpu_ref_put(&ctx->refs); | |
10996 | } | |
10997 | } | |
bebdb65e | 10998 | #endif |
87ce955b | 10999 | |
2b188cc1 JA |
11000 | static const struct file_operations io_uring_fops = { |
11001 | .release = io_uring_release, | |
11002 | .mmap = io_uring_mmap, | |
6c5c240e RP |
11003 | #ifndef CONFIG_MMU |
11004 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
11005 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
11006 | #endif | |
2b188cc1 | 11007 | .poll = io_uring_poll, |
bebdb65e | 11008 | #ifdef CONFIG_PROC_FS |
87ce955b | 11009 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 11010 | #endif |
2b188cc1 JA |
11011 | }; |
11012 | ||
c072481d PB |
11013 | static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
11014 | struct io_uring_params *p) | |
2b188cc1 | 11015 | { |
75b28aff HV |
11016 | struct io_rings *rings; |
11017 | size_t size, sq_array_offset; | |
2b188cc1 | 11018 | |
bd740481 JA |
11019 | /* make sure these are sane, as we already accounted them */ |
11020 | ctx->sq_entries = p->sq_entries; | |
11021 | ctx->cq_entries = p->cq_entries; | |
11022 | ||
75b28aff HV |
11023 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
11024 | if (size == SIZE_MAX) | |
11025 | return -EOVERFLOW; | |
11026 | ||
11027 | rings = io_mem_alloc(size); | |
11028 | if (!rings) | |
2b188cc1 JA |
11029 | return -ENOMEM; |
11030 | ||
75b28aff HV |
11031 | ctx->rings = rings; |
11032 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
11033 | rings->sq_ring_mask = p->sq_entries - 1; | |
11034 | rings->cq_ring_mask = p->cq_entries - 1; | |
11035 | rings->sq_ring_entries = p->sq_entries; | |
11036 | rings->cq_ring_entries = p->cq_entries; | |
2b188cc1 JA |
11037 | |
11038 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
11039 | if (size == SIZE_MAX) { |
11040 | io_mem_free(ctx->rings); | |
11041 | ctx->rings = NULL; | |
2b188cc1 | 11042 | return -EOVERFLOW; |
eb065d30 | 11043 | } |
2b188cc1 JA |
11044 | |
11045 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
11046 | if (!ctx->sq_sqes) { |
11047 | io_mem_free(ctx->rings); | |
11048 | ctx->rings = NULL; | |
2b188cc1 | 11049 | return -ENOMEM; |
eb065d30 | 11050 | } |
2b188cc1 | 11051 | |
2b188cc1 JA |
11052 | return 0; |
11053 | } | |
11054 | ||
9faadcc8 PB |
11055 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
11056 | { | |
11057 | int ret, fd; | |
11058 | ||
11059 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
11060 | if (fd < 0) | |
11061 | return fd; | |
11062 | ||
eef51daa | 11063 | ret = io_uring_add_tctx_node(ctx); |
9faadcc8 PB |
11064 | if (ret) { |
11065 | put_unused_fd(fd); | |
11066 | return ret; | |
11067 | } | |
11068 | fd_install(fd, file); | |
11069 | return fd; | |
11070 | } | |
11071 | ||
2b188cc1 JA |
11072 | /* |
11073 | * Allocate an anonymous fd, this is what constitutes the application | |
11074 | * visible backing of an io_uring instance. The application mmaps this | |
11075 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
11076 | * we have to tie this fd to a socket for file garbage collection purposes. | |
11077 | */ | |
9faadcc8 | 11078 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
2b188cc1 JA |
11079 | { |
11080 | struct file *file; | |
9faadcc8 | 11081 | #if defined(CONFIG_UNIX) |
2b188cc1 JA |
11082 | int ret; |
11083 | ||
2b188cc1 JA |
11084 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
11085 | &ctx->ring_sock); | |
11086 | if (ret) | |
9faadcc8 | 11087 | return ERR_PTR(ret); |
2b188cc1 JA |
11088 | #endif |
11089 | ||
91a9ab7c PM |
11090 | file = anon_inode_getfile_secure("[io_uring]", &io_uring_fops, ctx, |
11091 | O_RDWR | O_CLOEXEC, NULL); | |
2b188cc1 | 11092 | #if defined(CONFIG_UNIX) |
9faadcc8 PB |
11093 | if (IS_ERR(file)) { |
11094 | sock_release(ctx->ring_sock); | |
11095 | ctx->ring_sock = NULL; | |
11096 | } else { | |
11097 | ctx->ring_sock->file = file; | |
0f212204 | 11098 | } |
2b188cc1 | 11099 | #endif |
9faadcc8 | 11100 | return file; |
2b188cc1 JA |
11101 | } |
11102 | ||
c072481d PB |
11103 | static __cold int io_uring_create(unsigned entries, struct io_uring_params *p, |
11104 | struct io_uring_params __user *params) | |
2b188cc1 | 11105 | { |
2b188cc1 | 11106 | struct io_ring_ctx *ctx; |
9faadcc8 | 11107 | struct file *file; |
2b188cc1 JA |
11108 | int ret; |
11109 | ||
8110c1a6 | 11110 | if (!entries) |
2b188cc1 | 11111 | return -EINVAL; |
8110c1a6 JA |
11112 | if (entries > IORING_MAX_ENTRIES) { |
11113 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
11114 | return -EINVAL; | |
11115 | entries = IORING_MAX_ENTRIES; | |
11116 | } | |
2b188cc1 JA |
11117 | |
11118 | /* | |
11119 | * Use twice as many entries for the CQ ring. It's possible for the | |
11120 | * application to drive a higher depth than the size of the SQ ring, | |
11121 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
11122 | * some flexibility in overcommitting a bit. If the application has |
11123 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
11124 | * of CQ ring entries manually. | |
2b188cc1 JA |
11125 | */ |
11126 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
11127 | if (p->flags & IORING_SETUP_CQSIZE) { |
11128 | /* | |
11129 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
11130 | * to a power-of-two, if it isn't already. We do NOT impose | |
11131 | * any cq vs sq ring sizing. | |
11132 | */ | |
eb2667b3 | 11133 | if (!p->cq_entries) |
33a107f0 | 11134 | return -EINVAL; |
8110c1a6 JA |
11135 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
11136 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
11137 | return -EINVAL; | |
11138 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
11139 | } | |
eb2667b3 JQ |
11140 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
11141 | if (p->cq_entries < p->sq_entries) | |
11142 | return -EINVAL; | |
33a107f0 JA |
11143 | } else { |
11144 | p->cq_entries = 2 * p->sq_entries; | |
11145 | } | |
2b188cc1 | 11146 | |
2b188cc1 | 11147 | ctx = io_ring_ctx_alloc(p); |
62e398be | 11148 | if (!ctx) |
2b188cc1 | 11149 | return -ENOMEM; |
773697b6 PB |
11150 | |
11151 | /* | |
11152 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
11153 | * space applications don't need to do io completion events | |
11154 | * polling again, they can rely on io_sq_thread to do polling | |
11155 | * work, which can reduce cpu usage and uring_lock contention. | |
11156 | */ | |
11157 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
11158 | !(ctx->flags & IORING_SETUP_SQPOLL)) | |
11159 | ctx->syscall_iopoll = 1; | |
11160 | ||
2b188cc1 | 11161 | ctx->compat = in_compat_syscall(); |
62e398be JA |
11162 | if (!capable(CAP_IPC_LOCK)) |
11163 | ctx->user = get_uid(current_user()); | |
2aede0e4 JA |
11164 | |
11165 | /* | |
11166 | * This is just grabbed for accounting purposes. When a process exits, | |
11167 | * the mm is exited and dropped before the files, hence we need to hang | |
11168 | * on to this mm purely for the purposes of being able to unaccount | |
11169 | * memory (locked/pinned vm). It's not used for anything else. | |
11170 | */ | |
6b7898eb | 11171 | mmgrab(current->mm); |
2aede0e4 | 11172 | ctx->mm_account = current->mm; |
6b7898eb | 11173 | |
2b188cc1 JA |
11174 | ret = io_allocate_scq_urings(ctx, p); |
11175 | if (ret) | |
11176 | goto err; | |
11177 | ||
7e84e1c7 | 11178 | ret = io_sq_offload_create(ctx, p); |
2b188cc1 JA |
11179 | if (ret) |
11180 | goto err; | |
eae071c9 | 11181 | /* always set a rsrc node */ |
47b228ce PB |
11182 | ret = io_rsrc_node_switch_start(ctx); |
11183 | if (ret) | |
11184 | goto err; | |
eae071c9 | 11185 | io_rsrc_node_switch(ctx, NULL); |
2b188cc1 | 11186 | |
2b188cc1 | 11187 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
11188 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
11189 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
11190 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
11191 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
11192 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
11193 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
11194 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
11195 | |
11196 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
11197 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
11198 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
11199 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
11200 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
11201 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
11202 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 11203 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 11204 | |
7f13657d XW |
11205 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
11206 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 | 11207 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
c73ebb68 | 11208 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
9690557e | 11209 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS | |
c4212f3e JA |
11210 | IORING_FEAT_RSRC_TAGS | IORING_FEAT_CQE_SKIP | |
11211 | IORING_FEAT_LINKED_FILE; | |
7f13657d XW |
11212 | |
11213 | if (copy_to_user(params, p, sizeof(*p))) { | |
11214 | ret = -EFAULT; | |
11215 | goto err; | |
11216 | } | |
d1719f70 | 11217 | |
9faadcc8 PB |
11218 | file = io_uring_get_file(ctx); |
11219 | if (IS_ERR(file)) { | |
11220 | ret = PTR_ERR(file); | |
11221 | goto err; | |
11222 | } | |
11223 | ||
044c1ab3 JA |
11224 | /* |
11225 | * Install ring fd as the very last thing, so we don't risk someone | |
11226 | * having closed it before we finish setup | |
11227 | */ | |
9faadcc8 PB |
11228 | ret = io_uring_install_fd(ctx, file); |
11229 | if (ret < 0) { | |
11230 | /* fput will clean it up */ | |
11231 | fput(file); | |
11232 | return ret; | |
11233 | } | |
044c1ab3 | 11234 | |
c826bd7a | 11235 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
11236 | return ret; |
11237 | err: | |
11238 | io_ring_ctx_wait_and_kill(ctx); | |
11239 | return ret; | |
11240 | } | |
11241 | ||
11242 | /* | |
11243 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
11244 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
11245 | * params structure passed in. | |
11246 | */ | |
11247 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
11248 | { | |
11249 | struct io_uring_params p; | |
2b188cc1 JA |
11250 | int i; |
11251 | ||
11252 | if (copy_from_user(&p, params, sizeof(p))) | |
11253 | return -EFAULT; | |
11254 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
11255 | if (p.resv[i]) | |
11256 | return -EINVAL; | |
11257 | } | |
11258 | ||
6c271ce2 | 11259 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 11260 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 | 11261 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
bcbb7bf6 | 11262 | IORING_SETUP_R_DISABLED | IORING_SETUP_SUBMIT_ALL)) |
2b188cc1 JA |
11263 | return -EINVAL; |
11264 | ||
7f13657d | 11265 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
11266 | } |
11267 | ||
11268 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
11269 | struct io_uring_params __user *, params) | |
11270 | { | |
11271 | return io_uring_setup(entries, params); | |
11272 | } | |
11273 | ||
c072481d PB |
11274 | static __cold int io_probe(struct io_ring_ctx *ctx, void __user *arg, |
11275 | unsigned nr_args) | |
66f4af93 JA |
11276 | { |
11277 | struct io_uring_probe *p; | |
11278 | size_t size; | |
11279 | int i, ret; | |
11280 | ||
11281 | size = struct_size(p, ops, nr_args); | |
11282 | if (size == SIZE_MAX) | |
11283 | return -EOVERFLOW; | |
11284 | p = kzalloc(size, GFP_KERNEL); | |
11285 | if (!p) | |
11286 | return -ENOMEM; | |
11287 | ||
11288 | ret = -EFAULT; | |
11289 | if (copy_from_user(p, arg, size)) | |
11290 | goto out; | |
11291 | ret = -EINVAL; | |
11292 | if (memchr_inv(p, 0, size)) | |
11293 | goto out; | |
11294 | ||
11295 | p->last_op = IORING_OP_LAST - 1; | |
11296 | if (nr_args > IORING_OP_LAST) | |
11297 | nr_args = IORING_OP_LAST; | |
11298 | ||
11299 | for (i = 0; i < nr_args; i++) { | |
11300 | p->ops[i].op = i; | |
11301 | if (!io_op_defs[i].not_supported) | |
11302 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
11303 | } | |
11304 | p->ops_len = i; | |
11305 | ||
11306 | ret = 0; | |
11307 | if (copy_to_user(arg, p, size)) | |
11308 | ret = -EFAULT; | |
11309 | out: | |
11310 | kfree(p); | |
11311 | return ret; | |
11312 | } | |
11313 | ||
071698e1 JA |
11314 | static int io_register_personality(struct io_ring_ctx *ctx) |
11315 | { | |
4379bf8b | 11316 | const struct cred *creds; |
61cf9370 | 11317 | u32 id; |
1e6fa521 | 11318 | int ret; |
071698e1 | 11319 | |
4379bf8b | 11320 | creds = get_current_cred(); |
1e6fa521 | 11321 | |
61cf9370 MWO |
11322 | ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds, |
11323 | XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL); | |
a30f895a JA |
11324 | if (ret < 0) { |
11325 | put_cred(creds); | |
11326 | return ret; | |
11327 | } | |
11328 | return id; | |
071698e1 JA |
11329 | } |
11330 | ||
c072481d PB |
11331 | static __cold int io_register_restrictions(struct io_ring_ctx *ctx, |
11332 | void __user *arg, unsigned int nr_args) | |
21b55dbc SG |
11333 | { |
11334 | struct io_uring_restriction *res; | |
11335 | size_t size; | |
11336 | int i, ret; | |
11337 | ||
7e84e1c7 SG |
11338 | /* Restrictions allowed only if rings started disabled */ |
11339 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
11340 | return -EBADFD; | |
11341 | ||
21b55dbc | 11342 | /* We allow only a single restrictions registration */ |
7e84e1c7 | 11343 | if (ctx->restrictions.registered) |
21b55dbc SG |
11344 | return -EBUSY; |
11345 | ||
11346 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) | |
11347 | return -EINVAL; | |
11348 | ||
11349 | size = array_size(nr_args, sizeof(*res)); | |
11350 | if (size == SIZE_MAX) | |
11351 | return -EOVERFLOW; | |
11352 | ||
11353 | res = memdup_user(arg, size); | |
11354 | if (IS_ERR(res)) | |
11355 | return PTR_ERR(res); | |
11356 | ||
11357 | ret = 0; | |
11358 | ||
11359 | for (i = 0; i < nr_args; i++) { | |
11360 | switch (res[i].opcode) { | |
11361 | case IORING_RESTRICTION_REGISTER_OP: | |
11362 | if (res[i].register_op >= IORING_REGISTER_LAST) { | |
11363 | ret = -EINVAL; | |
11364 | goto out; | |
11365 | } | |
11366 | ||
11367 | __set_bit(res[i].register_op, | |
11368 | ctx->restrictions.register_op); | |
11369 | break; | |
11370 | case IORING_RESTRICTION_SQE_OP: | |
11371 | if (res[i].sqe_op >= IORING_OP_LAST) { | |
11372 | ret = -EINVAL; | |
11373 | goto out; | |
11374 | } | |
11375 | ||
11376 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); | |
11377 | break; | |
11378 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: | |
11379 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; | |
11380 | break; | |
11381 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: | |
11382 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; | |
11383 | break; | |
11384 | default: | |
11385 | ret = -EINVAL; | |
11386 | goto out; | |
11387 | } | |
11388 | } | |
11389 | ||
11390 | out: | |
11391 | /* Reset all restrictions if an error happened */ | |
11392 | if (ret != 0) | |
11393 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); | |
11394 | else | |
7e84e1c7 | 11395 | ctx->restrictions.registered = true; |
21b55dbc SG |
11396 | |
11397 | kfree(res); | |
11398 | return ret; | |
11399 | } | |
11400 | ||
7e84e1c7 SG |
11401 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
11402 | { | |
11403 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
11404 | return -EBADFD; | |
11405 | ||
11406 | if (ctx->restrictions.registered) | |
11407 | ctx->restricted = 1; | |
11408 | ||
0298ef96 PB |
11409 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
11410 | if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait)) | |
11411 | wake_up(&ctx->sq_data->wait); | |
7e84e1c7 SG |
11412 | return 0; |
11413 | } | |
11414 | ||
fdecb662 | 11415 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 11416 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 PB |
11417 | unsigned nr_args) |
11418 | { | |
11419 | __u32 tmp; | |
11420 | int err; | |
11421 | ||
11422 | if (check_add_overflow(up->offset, nr_args, &tmp)) | |
11423 | return -EOVERFLOW; | |
11424 | err = io_rsrc_node_switch_start(ctx); | |
11425 | if (err) | |
11426 | return err; | |
11427 | ||
fdecb662 PB |
11428 | switch (type) { |
11429 | case IORING_RSRC_FILE: | |
98f0b3b4 | 11430 | return __io_sqe_files_update(ctx, up, nr_args); |
634d00df PB |
11431 | case IORING_RSRC_BUFFER: |
11432 | return __io_sqe_buffers_update(ctx, up, nr_args); | |
98f0b3b4 PB |
11433 | } |
11434 | return -EINVAL; | |
11435 | } | |
11436 | ||
c3bdad02 PB |
11437 | static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg, |
11438 | unsigned nr_args) | |
98f0b3b4 | 11439 | { |
c3bdad02 | 11440 | struct io_uring_rsrc_update2 up; |
98f0b3b4 PB |
11441 | |
11442 | if (!nr_args) | |
11443 | return -EINVAL; | |
c3bdad02 PB |
11444 | memset(&up, 0, sizeof(up)); |
11445 | if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update))) | |
11446 | return -EFAULT; | |
d8a3ba9c | 11447 | if (up.resv || up.resv2) |
565c5e61 | 11448 | return -EINVAL; |
c3bdad02 PB |
11449 | return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args); |
11450 | } | |
11451 | ||
11452 | static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg, | |
992da01a | 11453 | unsigned size, unsigned type) |
c3bdad02 PB |
11454 | { |
11455 | struct io_uring_rsrc_update2 up; | |
11456 | ||
11457 | if (size != sizeof(up)) | |
11458 | return -EINVAL; | |
98f0b3b4 PB |
11459 | if (copy_from_user(&up, arg, sizeof(up))) |
11460 | return -EFAULT; | |
d8a3ba9c | 11461 | if (!up.nr || up.resv || up.resv2) |
98f0b3b4 | 11462 | return -EINVAL; |
992da01a | 11463 | return __io_register_rsrc_update(ctx, type, &up, up.nr); |
98f0b3b4 PB |
11464 | } |
11465 | ||
c072481d | 11466 | static __cold int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg, |
992da01a | 11467 | unsigned int size, unsigned int type) |
792e3582 PB |
11468 | { |
11469 | struct io_uring_rsrc_register rr; | |
11470 | ||
11471 | /* keep it extendible */ | |
11472 | if (size != sizeof(rr)) | |
11473 | return -EINVAL; | |
11474 | ||
11475 | memset(&rr, 0, sizeof(rr)); | |
11476 | if (copy_from_user(&rr, arg, size)) | |
11477 | return -EFAULT; | |
992da01a | 11478 | if (!rr.nr || rr.resv || rr.resv2) |
792e3582 PB |
11479 | return -EINVAL; |
11480 | ||
992da01a | 11481 | switch (type) { |
792e3582 PB |
11482 | case IORING_RSRC_FILE: |
11483 | return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data), | |
11484 | rr.nr, u64_to_user_ptr(rr.tags)); | |
634d00df PB |
11485 | case IORING_RSRC_BUFFER: |
11486 | return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data), | |
11487 | rr.nr, u64_to_user_ptr(rr.tags)); | |
792e3582 PB |
11488 | } |
11489 | return -EINVAL; | |
11490 | } | |
11491 | ||
c072481d PB |
11492 | static __cold int io_register_iowq_aff(struct io_ring_ctx *ctx, |
11493 | void __user *arg, unsigned len) | |
fe76421d JA |
11494 | { |
11495 | struct io_uring_task *tctx = current->io_uring; | |
11496 | cpumask_var_t new_mask; | |
11497 | int ret; | |
11498 | ||
11499 | if (!tctx || !tctx->io_wq) | |
11500 | return -EINVAL; | |
11501 | ||
11502 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) | |
11503 | return -ENOMEM; | |
11504 | ||
11505 | cpumask_clear(new_mask); | |
11506 | if (len > cpumask_size()) | |
11507 | len = cpumask_size(); | |
11508 | ||
0f5e4b83 ES |
11509 | if (in_compat_syscall()) { |
11510 | ret = compat_get_bitmap(cpumask_bits(new_mask), | |
11511 | (const compat_ulong_t __user *)arg, | |
11512 | len * 8 /* CHAR_BIT */); | |
11513 | } else { | |
11514 | ret = copy_from_user(new_mask, arg, len); | |
11515 | } | |
11516 | ||
11517 | if (ret) { | |
fe76421d JA |
11518 | free_cpumask_var(new_mask); |
11519 | return -EFAULT; | |
11520 | } | |
11521 | ||
11522 | ret = io_wq_cpu_affinity(tctx->io_wq, new_mask); | |
11523 | free_cpumask_var(new_mask); | |
11524 | return ret; | |
11525 | } | |
11526 | ||
c072481d | 11527 | static __cold int io_unregister_iowq_aff(struct io_ring_ctx *ctx) |
fe76421d JA |
11528 | { |
11529 | struct io_uring_task *tctx = current->io_uring; | |
11530 | ||
11531 | if (!tctx || !tctx->io_wq) | |
11532 | return -EINVAL; | |
11533 | ||
11534 | return io_wq_cpu_affinity(tctx->io_wq, NULL); | |
11535 | } | |
11536 | ||
c072481d PB |
11537 | static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx, |
11538 | void __user *arg) | |
b22fa62a | 11539 | __must_hold(&ctx->uring_lock) |
2e480058 | 11540 | { |
b22fa62a | 11541 | struct io_tctx_node *node; |
fa84693b JA |
11542 | struct io_uring_task *tctx = NULL; |
11543 | struct io_sq_data *sqd = NULL; | |
2e480058 JA |
11544 | __u32 new_count[2]; |
11545 | int i, ret; | |
11546 | ||
2e480058 JA |
11547 | if (copy_from_user(new_count, arg, sizeof(new_count))) |
11548 | return -EFAULT; | |
11549 | for (i = 0; i < ARRAY_SIZE(new_count); i++) | |
11550 | if (new_count[i] > INT_MAX) | |
11551 | return -EINVAL; | |
11552 | ||
fa84693b JA |
11553 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
11554 | sqd = ctx->sq_data; | |
11555 | if (sqd) { | |
009ad9f0 JA |
11556 | /* |
11557 | * Observe the correct sqd->lock -> ctx->uring_lock | |
11558 | * ordering. Fine to drop uring_lock here, we hold | |
11559 | * a ref to the ctx. | |
11560 | */ | |
41d3a6bd | 11561 | refcount_inc(&sqd->refs); |
009ad9f0 | 11562 | mutex_unlock(&ctx->uring_lock); |
fa84693b | 11563 | mutex_lock(&sqd->lock); |
009ad9f0 | 11564 | mutex_lock(&ctx->uring_lock); |
41d3a6bd JA |
11565 | if (sqd->thread) |
11566 | tctx = sqd->thread->io_uring; | |
fa84693b JA |
11567 | } |
11568 | } else { | |
11569 | tctx = current->io_uring; | |
11570 | } | |
11571 | ||
e139a1ec | 11572 | BUILD_BUG_ON(sizeof(new_count) != sizeof(ctx->iowq_limits)); |
fa84693b | 11573 | |
bad119b9 PB |
11574 | for (i = 0; i < ARRAY_SIZE(new_count); i++) |
11575 | if (new_count[i]) | |
11576 | ctx->iowq_limits[i] = new_count[i]; | |
e139a1ec PB |
11577 | ctx->iowq_limits_set = true; |
11578 | ||
e139a1ec PB |
11579 | if (tctx && tctx->io_wq) { |
11580 | ret = io_wq_max_workers(tctx->io_wq, new_count); | |
11581 | if (ret) | |
11582 | goto err; | |
11583 | } else { | |
11584 | memset(new_count, 0, sizeof(new_count)); | |
11585 | } | |
fa84693b | 11586 | |
41d3a6bd | 11587 | if (sqd) { |
fa84693b | 11588 | mutex_unlock(&sqd->lock); |
41d3a6bd JA |
11589 | io_put_sq_data(sqd); |
11590 | } | |
2e480058 JA |
11591 | |
11592 | if (copy_to_user(arg, new_count, sizeof(new_count))) | |
11593 | return -EFAULT; | |
11594 | ||
b22fa62a PB |
11595 | /* that's it for SQPOLL, only the SQPOLL task creates requests */ |
11596 | if (sqd) | |
11597 | return 0; | |
11598 | ||
11599 | /* now propagate the restriction to all registered users */ | |
11600 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
11601 | struct io_uring_task *tctx = node->task->io_uring; | |
11602 | ||
11603 | if (WARN_ON_ONCE(!tctx->io_wq)) | |
11604 | continue; | |
11605 | ||
11606 | for (i = 0; i < ARRAY_SIZE(new_count); i++) | |
11607 | new_count[i] = ctx->iowq_limits[i]; | |
11608 | /* ignore errors, it always returns zero anyway */ | |
11609 | (void)io_wq_max_workers(tctx->io_wq, new_count); | |
11610 | } | |
2e480058 | 11611 | return 0; |
fa84693b | 11612 | err: |
41d3a6bd | 11613 | if (sqd) { |
fa84693b | 11614 | mutex_unlock(&sqd->lock); |
41d3a6bd JA |
11615 | io_put_sq_data(sqd); |
11616 | } | |
fa84693b | 11617 | return ret; |
2e480058 JA |
11618 | } |
11619 | ||
edafccee JA |
11620 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
11621 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
11622 | __releases(ctx->uring_lock) |
11623 | __acquires(ctx->uring_lock) | |
edafccee JA |
11624 | { |
11625 | int ret; | |
11626 | ||
35fa71a0 JA |
11627 | /* |
11628 | * We're inside the ring mutex, if the ref is already dying, then | |
11629 | * someone else killed the ctx or is already going through | |
11630 | * io_uring_register(). | |
11631 | */ | |
11632 | if (percpu_ref_is_dying(&ctx->refs)) | |
11633 | return -ENXIO; | |
11634 | ||
75c4021a PB |
11635 | if (ctx->restricted) { |
11636 | if (opcode >= IORING_REGISTER_LAST) | |
11637 | return -EINVAL; | |
11638 | opcode = array_index_nospec(opcode, IORING_REGISTER_LAST); | |
11639 | if (!test_bit(opcode, ctx->restrictions.register_op)) | |
11640 | return -EACCES; | |
11641 | } | |
11642 | ||
edafccee JA |
11643 | switch (opcode) { |
11644 | case IORING_REGISTER_BUFFERS: | |
634d00df | 11645 | ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL); |
edafccee JA |
11646 | break; |
11647 | case IORING_UNREGISTER_BUFFERS: | |
11648 | ret = -EINVAL; | |
11649 | if (arg || nr_args) | |
11650 | break; | |
0a96bbe4 | 11651 | ret = io_sqe_buffers_unregister(ctx); |
edafccee | 11652 | break; |
6b06314c | 11653 | case IORING_REGISTER_FILES: |
792e3582 | 11654 | ret = io_sqe_files_register(ctx, arg, nr_args, NULL); |
6b06314c JA |
11655 | break; |
11656 | case IORING_UNREGISTER_FILES: | |
11657 | ret = -EINVAL; | |
11658 | if (arg || nr_args) | |
11659 | break; | |
11660 | ret = io_sqe_files_unregister(ctx); | |
11661 | break; | |
c3a31e60 | 11662 | case IORING_REGISTER_FILES_UPDATE: |
c3bdad02 | 11663 | ret = io_register_files_update(ctx, arg, nr_args); |
c3a31e60 | 11664 | break; |
9b402849 JA |
11665 | case IORING_REGISTER_EVENTFD: |
11666 | ret = -EINVAL; | |
11667 | if (nr_args != 1) | |
11668 | break; | |
c75312dd UA |
11669 | ret = io_eventfd_register(ctx, arg, 0); |
11670 | break; | |
11671 | case IORING_REGISTER_EVENTFD_ASYNC: | |
11672 | ret = -EINVAL; | |
11673 | if (nr_args != 1) | |
f2842ab5 | 11674 | break; |
c75312dd | 11675 | ret = io_eventfd_register(ctx, arg, 1); |
9b402849 JA |
11676 | break; |
11677 | case IORING_UNREGISTER_EVENTFD: | |
11678 | ret = -EINVAL; | |
11679 | if (arg || nr_args) | |
11680 | break; | |
11681 | ret = io_eventfd_unregister(ctx); | |
11682 | break; | |
66f4af93 JA |
11683 | case IORING_REGISTER_PROBE: |
11684 | ret = -EINVAL; | |
11685 | if (!arg || nr_args > 256) | |
11686 | break; | |
11687 | ret = io_probe(ctx, arg, nr_args); | |
11688 | break; | |
071698e1 JA |
11689 | case IORING_REGISTER_PERSONALITY: |
11690 | ret = -EINVAL; | |
11691 | if (arg || nr_args) | |
11692 | break; | |
11693 | ret = io_register_personality(ctx); | |
11694 | break; | |
11695 | case IORING_UNREGISTER_PERSONALITY: | |
11696 | ret = -EINVAL; | |
11697 | if (arg) | |
11698 | break; | |
11699 | ret = io_unregister_personality(ctx, nr_args); | |
11700 | break; | |
7e84e1c7 SG |
11701 | case IORING_REGISTER_ENABLE_RINGS: |
11702 | ret = -EINVAL; | |
11703 | if (arg || nr_args) | |
11704 | break; | |
11705 | ret = io_register_enable_rings(ctx); | |
11706 | break; | |
21b55dbc SG |
11707 | case IORING_REGISTER_RESTRICTIONS: |
11708 | ret = io_register_restrictions(ctx, arg, nr_args); | |
11709 | break; | |
992da01a PB |
11710 | case IORING_REGISTER_FILES2: |
11711 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE); | |
11712 | break; | |
11713 | case IORING_REGISTER_FILES_UPDATE2: | |
11714 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
11715 | IORING_RSRC_FILE); | |
11716 | break; | |
11717 | case IORING_REGISTER_BUFFERS2: | |
11718 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER); | |
792e3582 | 11719 | break; |
992da01a PB |
11720 | case IORING_REGISTER_BUFFERS_UPDATE: |
11721 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
11722 | IORING_RSRC_BUFFER); | |
c3bdad02 | 11723 | break; |
fe76421d JA |
11724 | case IORING_REGISTER_IOWQ_AFF: |
11725 | ret = -EINVAL; | |
11726 | if (!arg || !nr_args) | |
11727 | break; | |
11728 | ret = io_register_iowq_aff(ctx, arg, nr_args); | |
11729 | break; | |
11730 | case IORING_UNREGISTER_IOWQ_AFF: | |
11731 | ret = -EINVAL; | |
11732 | if (arg || nr_args) | |
11733 | break; | |
11734 | ret = io_unregister_iowq_aff(ctx); | |
11735 | break; | |
2e480058 JA |
11736 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
11737 | ret = -EINVAL; | |
11738 | if (!arg || nr_args != 2) | |
11739 | break; | |
11740 | ret = io_register_iowq_max_workers(ctx, arg); | |
11741 | break; | |
e7a6c00d JA |
11742 | case IORING_REGISTER_RING_FDS: |
11743 | ret = io_ringfd_register(ctx, arg, nr_args); | |
11744 | break; | |
11745 | case IORING_UNREGISTER_RING_FDS: | |
11746 | ret = io_ringfd_unregister(ctx, arg, nr_args); | |
11747 | break; | |
edafccee JA |
11748 | default: |
11749 | ret = -EINVAL; | |
11750 | break; | |
11751 | } | |
11752 | ||
edafccee JA |
11753 | return ret; |
11754 | } | |
11755 | ||
11756 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
11757 | void __user *, arg, unsigned int, nr_args) | |
11758 | { | |
11759 | struct io_ring_ctx *ctx; | |
11760 | long ret = -EBADF; | |
11761 | struct fd f; | |
11762 | ||
11763 | f = fdget(fd); | |
11764 | if (!f.file) | |
11765 | return -EBADF; | |
11766 | ||
11767 | ret = -EOPNOTSUPP; | |
11768 | if (f.file->f_op != &io_uring_fops) | |
11769 | goto out_fput; | |
11770 | ||
11771 | ctx = f.file->private_data; | |
11772 | ||
b6c23dd5 PB |
11773 | io_run_task_work(); |
11774 | ||
edafccee JA |
11775 | mutex_lock(&ctx->uring_lock); |
11776 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
11777 | mutex_unlock(&ctx->uring_lock); | |
2757be22 | 11778 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, ret); |
edafccee JA |
11779 | out_fput: |
11780 | fdput(f); | |
11781 | return ret; | |
11782 | } | |
11783 | ||
2b188cc1 JA |
11784 | static int __init io_uring_init(void) |
11785 | { | |
d7f62e82 SM |
11786 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
11787 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
11788 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
11789 | } while (0) | |
11790 | ||
11791 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
11792 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
11793 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
11794 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
11795 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
11796 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
11797 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
11798 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
11799 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
11800 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 11801 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
11802 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
11803 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
11804 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
11805 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
11806 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
11807 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
11808 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
11809 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
11810 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
11811 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
11812 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
11813 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
11814 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
11815 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
11816 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 11817 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
11818 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
11819 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
16340eab | 11820 | BUILD_BUG_SQE_ELEM(40, __u16, buf_group); |
d7f62e82 | 11821 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
7d67af2c | 11822 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
b9445598 | 11823 | BUILD_BUG_SQE_ELEM(44, __u32, file_index); |
d7f62e82 | 11824 | |
b0d658ec PB |
11825 | BUILD_BUG_ON(sizeof(struct io_uring_files_update) != |
11826 | sizeof(struct io_uring_rsrc_update)); | |
11827 | BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) > | |
11828 | sizeof(struct io_uring_rsrc_update2)); | |
90499ad0 PB |
11829 | |
11830 | /* ->buf_index is u16 */ | |
11831 | BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16)); | |
11832 | ||
b0d658ec PB |
11833 | /* should fit into one byte */ |
11834 | BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8)); | |
68fe256a PB |
11835 | BUILD_BUG_ON(SQE_COMMON_FLAGS >= (1 << 8)); |
11836 | BUILD_BUG_ON((SQE_VALID_FLAGS | SQE_COMMON_FLAGS) != SQE_VALID_FLAGS); | |
b0d658ec | 11837 | |
d3656344 | 11838 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
32c2d33e | 11839 | BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof(int)); |
16340eab | 11840 | |
91f245d5 JA |
11841 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
11842 | SLAB_ACCOUNT); | |
2b188cc1 JA |
11843 | return 0; |
11844 | }; | |
11845 | __initcall(io_uring_init); |