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; |
b52ecf8c | 359 | } ____cacheline_aligned_in_smp; |
2b188cc1 | 360 | |
7f1129d2 | 361 | /* submission data */ |
b52ecf8c | 362 | struct { |
0499e582 PB |
363 | struct mutex uring_lock; |
364 | ||
75b28aff HV |
365 | /* |
366 | * Ring buffer of indices into array of io_uring_sqe, which is | |
367 | * mmapped by the application using the IORING_OFF_SQES offset. | |
368 | * | |
369 | * This indirection could e.g. be used to assign fixed | |
370 | * io_uring_sqe entries to operations and only submit them to | |
371 | * the queue when needed. | |
372 | * | |
373 | * The kernel modifies neither the indices array nor the entries | |
374 | * array. | |
375 | */ | |
376 | u32 *sq_array; | |
c7af47cf | 377 | struct io_uring_sqe *sq_sqes; |
2b188cc1 JA |
378 | unsigned cached_sq_head; |
379 | unsigned sq_entries; | |
de0617e4 | 380 | struct list_head defer_list; |
7f1129d2 PB |
381 | |
382 | /* | |
383 | * Fixed resources fast path, should be accessed only under | |
384 | * uring_lock, and updated through io_uring_register(2) | |
385 | */ | |
386 | struct io_rsrc_node *rsrc_node; | |
ab409402 | 387 | int rsrc_cached_refs; |
7f1129d2 PB |
388 | struct io_file_table file_table; |
389 | unsigned nr_user_files; | |
390 | unsigned nr_user_bufs; | |
391 | struct io_mapped_ubuf **user_bufs; | |
392 | ||
393 | struct io_submit_state submit_state; | |
5262f567 | 394 | struct list_head timeout_list; |
ef9dd637 | 395 | struct list_head ltimeout_list; |
1d7bb1d5 | 396 | struct list_head cq_overflow_list; |
dbc7d452 | 397 | struct list_head *io_buffers; |
cc3cec83 | 398 | struct list_head io_buffers_cache; |
4d9237e3 | 399 | struct list_head apoll_cache; |
7f1129d2 PB |
400 | struct xarray personalities; |
401 | u32 pers_next; | |
402 | unsigned sq_thread_idle; | |
2b188cc1 JA |
403 | } ____cacheline_aligned_in_smp; |
404 | ||
d0acdee2 | 405 | /* IRQ completion list, under ->completion_lock */ |
c2b6c6bc | 406 | struct io_wq_work_list locked_free_list; |
d0acdee2 | 407 | unsigned int locked_free_nr; |
3c1a2ead | 408 | |
7c30f36a | 409 | const struct cred *sq_creds; /* cred used for __io_sq_thread() */ |
534ca6d6 JA |
410 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
411 | ||
90554200 | 412 | struct wait_queue_head sqo_sq_wait; |
69fb2131 | 413 | struct list_head sqd_list; |
75b28aff | 414 | |
5ed7a37d PB |
415 | unsigned long check_cq_overflow; |
416 | ||
206aefde JA |
417 | struct { |
418 | unsigned cached_cq_tail; | |
419 | unsigned cq_entries; | |
77bc59b4 | 420 | struct io_ev_fd __rcu *io_ev_fd; |
0499e582 PB |
421 | struct wait_queue_head cq_wait; |
422 | unsigned cq_extra; | |
423 | atomic_t cq_timeouts; | |
0499e582 | 424 | unsigned cq_last_tm_flush; |
206aefde | 425 | } ____cacheline_aligned_in_smp; |
2b188cc1 | 426 | |
2b188cc1 JA |
427 | struct { |
428 | spinlock_t completion_lock; | |
e94f141b | 429 | |
89850fce JA |
430 | spinlock_t timeout_lock; |
431 | ||
def596e9 | 432 | /* |
540e32a0 | 433 | * ->iopoll_list is protected by the ctx->uring_lock for |
def596e9 JA |
434 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
435 | * For SQPOLL, only the single threaded io_sq_thread() will | |
436 | * manipulate the list, hence no extra locking is needed there. | |
437 | */ | |
5eef4e87 | 438 | struct io_wq_work_list iopoll_list; |
78076bb6 JA |
439 | struct hlist_head *cancel_hash; |
440 | unsigned cancel_hash_bits; | |
915b3dde | 441 | bool poll_multi_queue; |
cc3cec83 JA |
442 | |
443 | struct list_head io_buffers_comp; | |
2b188cc1 | 444 | } ____cacheline_aligned_in_smp; |
85faa7b8 | 445 | |
21b55dbc | 446 | struct io_restriction restrictions; |
3c1a2ead | 447 | |
b13a8918 PB |
448 | /* slow path rsrc auxilary data, used by update/register */ |
449 | struct { | |
450 | struct io_rsrc_node *rsrc_backup_node; | |
451 | struct io_mapped_ubuf *dummy_ubuf; | |
452 | struct io_rsrc_data *file_data; | |
453 | struct io_rsrc_data *buf_data; | |
454 | ||
455 | struct delayed_work rsrc_put_work; | |
456 | struct llist_head rsrc_put_llist; | |
457 | struct list_head rsrc_ref_list; | |
458 | spinlock_t rsrc_ref_lock; | |
cc3cec83 JA |
459 | |
460 | struct list_head io_buffers_pages; | |
b13a8918 PB |
461 | }; |
462 | ||
3c1a2ead | 463 | /* Keep this last, we don't need it for the fast path */ |
b986af7e PB |
464 | struct { |
465 | #if defined(CONFIG_UNIX) | |
466 | struct socket *ring_sock; | |
467 | #endif | |
468 | /* hashed buffered write serialization */ | |
469 | struct io_wq_hash *hash_map; | |
470 | ||
471 | /* Only used for accounting purposes */ | |
472 | struct user_struct *user; | |
473 | struct mm_struct *mm_account; | |
474 | ||
475 | /* ctx exit and cancelation */ | |
9011bf9a PB |
476 | struct llist_head fallback_llist; |
477 | struct delayed_work fallback_work; | |
b986af7e PB |
478 | struct work_struct exit_work; |
479 | struct list_head tctx_list; | |
480 | struct completion ref_comp; | |
e139a1ec PB |
481 | u32 iowq_limits[2]; |
482 | bool iowq_limits_set; | |
b986af7e | 483 | }; |
2b188cc1 JA |
484 | }; |
485 | ||
e7a6c00d JA |
486 | /* |
487 | * Arbitrary limit, can be raised if need be | |
488 | */ | |
489 | #define IO_RINGFD_REG_MAX 16 | |
490 | ||
53e043b2 SM |
491 | struct io_uring_task { |
492 | /* submission side */ | |
09899b19 | 493 | int cached_refs; |
53e043b2 SM |
494 | struct xarray xa; |
495 | struct wait_queue_head wait; | |
ee53fb2b SM |
496 | const struct io_ring_ctx *last; |
497 | struct io_wq *io_wq; | |
53e043b2 SM |
498 | struct percpu_counter inflight; |
499 | atomic_t in_idle; | |
53e043b2 SM |
500 | |
501 | spinlock_t task_lock; | |
502 | struct io_wq_work_list task_list; | |
4813c377 | 503 | struct io_wq_work_list prior_task_list; |
53e043b2 | 504 | struct callback_head task_work; |
e7a6c00d | 505 | struct file **registered_rings; |
6294f368 | 506 | bool task_running; |
53e043b2 SM |
507 | }; |
508 | ||
09bb8394 JA |
509 | /* |
510 | * First field must be the file pointer in all the | |
511 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> | |
512 | */ | |
221c5eb2 JA |
513 | struct io_poll_iocb { |
514 | struct file *file; | |
018043be | 515 | struct wait_queue_head *head; |
221c5eb2 | 516 | __poll_t events; |
392edb45 | 517 | struct wait_queue_entry wait; |
221c5eb2 JA |
518 | }; |
519 | ||
9d805892 | 520 | struct io_poll_update { |
018043be | 521 | struct file *file; |
9d805892 PB |
522 | u64 old_user_data; |
523 | u64 new_user_data; | |
524 | __poll_t events; | |
b69de288 JA |
525 | bool update_events; |
526 | bool update_user_data; | |
018043be PB |
527 | }; |
528 | ||
b5dba59e JA |
529 | struct io_close { |
530 | struct file *file; | |
b5dba59e | 531 | int fd; |
7df778be | 532 | u32 file_slot; |
b5dba59e JA |
533 | }; |
534 | ||
ad8a48ac JA |
535 | struct io_timeout_data { |
536 | struct io_kiocb *req; | |
537 | struct hrtimer timer; | |
538 | struct timespec64 ts; | |
539 | enum hrtimer_mode mode; | |
50c1df2b | 540 | u32 flags; |
ad8a48ac JA |
541 | }; |
542 | ||
8ed8d3c3 JA |
543 | struct io_accept { |
544 | struct file *file; | |
545 | struct sockaddr __user *addr; | |
546 | int __user *addr_len; | |
547 | int flags; | |
aaa4db12 | 548 | u32 file_slot; |
09952e3e | 549 | unsigned long nofile; |
8ed8d3c3 JA |
550 | }; |
551 | ||
552 | struct io_sync { | |
553 | struct file *file; | |
554 | loff_t len; | |
555 | loff_t off; | |
556 | int flags; | |
d63d1b5e | 557 | int mode; |
8ed8d3c3 JA |
558 | }; |
559 | ||
fbf23849 JA |
560 | struct io_cancel { |
561 | struct file *file; | |
562 | u64 addr; | |
563 | }; | |
564 | ||
b29472ee JA |
565 | struct io_timeout { |
566 | struct file *file; | |
bfe68a22 PB |
567 | u32 off; |
568 | u32 target_seq; | |
135fcde8 | 569 | struct list_head list; |
90cd7e42 PB |
570 | /* head of the link, used by linked timeouts only */ |
571 | struct io_kiocb *head; | |
89b263f6 JA |
572 | /* for linked completions */ |
573 | struct io_kiocb *prev; | |
b29472ee JA |
574 | }; |
575 | ||
0bdf7a2d PB |
576 | struct io_timeout_rem { |
577 | struct file *file; | |
578 | u64 addr; | |
9c8e11b3 PB |
579 | |
580 | /* timeout update */ | |
581 | struct timespec64 ts; | |
582 | u32 flags; | |
f1042b6c | 583 | bool ltimeout; |
0bdf7a2d PB |
584 | }; |
585 | ||
9adbd45d JA |
586 | struct io_rw { |
587 | /* NOTE: kiocb has the file as the first member, so don't do it here */ | |
588 | struct kiocb kiocb; | |
589 | u64 addr; | |
584b0180 JA |
590 | u32 len; |
591 | u32 flags; | |
9adbd45d JA |
592 | }; |
593 | ||
3fbb51c1 JA |
594 | struct io_connect { |
595 | struct file *file; | |
596 | struct sockaddr __user *addr; | |
597 | int addr_len; | |
598 | }; | |
599 | ||
e47293fd JA |
600 | struct io_sr_msg { |
601 | struct file *file; | |
fddaface | 602 | union { |
4af3417a PB |
603 | struct compat_msghdr __user *umsg_compat; |
604 | struct user_msghdr __user *umsg; | |
605 | void __user *buf; | |
fddaface | 606 | }; |
e47293fd | 607 | int msg_flags; |
bcda7baa | 608 | int bgid; |
fddaface | 609 | size_t len; |
7ba89d2a | 610 | size_t done_io; |
e47293fd JA |
611 | }; |
612 | ||
15b71abe JA |
613 | struct io_open { |
614 | struct file *file; | |
615 | int dfd; | |
b9445598 | 616 | u32 file_slot; |
15b71abe | 617 | struct filename *filename; |
c12cedf2 | 618 | struct open_how how; |
4022e7af | 619 | unsigned long nofile; |
15b71abe JA |
620 | }; |
621 | ||
269bbe5f | 622 | struct io_rsrc_update { |
05f3fb3c JA |
623 | struct file *file; |
624 | u64 arg; | |
625 | u32 nr_args; | |
626 | u32 offset; | |
627 | }; | |
628 | ||
4840e418 JA |
629 | struct io_fadvise { |
630 | struct file *file; | |
631 | u64 offset; | |
632 | u32 len; | |
633 | u32 advice; | |
634 | }; | |
635 | ||
c1ca757b JA |
636 | struct io_madvise { |
637 | struct file *file; | |
638 | u64 addr; | |
639 | u32 len; | |
640 | u32 advice; | |
641 | }; | |
642 | ||
3e4827b0 JA |
643 | struct io_epoll { |
644 | struct file *file; | |
645 | int epfd; | |
646 | int op; | |
647 | int fd; | |
648 | struct epoll_event event; | |
e47293fd JA |
649 | }; |
650 | ||
7d67af2c PB |
651 | struct io_splice { |
652 | struct file *file_out; | |
7d67af2c PB |
653 | loff_t off_out; |
654 | loff_t off_in; | |
655 | u64 len; | |
a3e4bc23 | 656 | int splice_fd_in; |
7d67af2c PB |
657 | unsigned int flags; |
658 | }; | |
659 | ||
ddf0322d JA |
660 | struct io_provide_buf { |
661 | struct file *file; | |
662 | __u64 addr; | |
38134ada | 663 | __u32 len; |
ddf0322d JA |
664 | __u32 bgid; |
665 | __u16 nbufs; | |
666 | __u16 bid; | |
667 | }; | |
668 | ||
1d9e1288 BM |
669 | struct io_statx { |
670 | struct file *file; | |
671 | int dfd; | |
672 | unsigned int mask; | |
673 | unsigned int flags; | |
1b6fe6e0 | 674 | struct filename *filename; |
1d9e1288 BM |
675 | struct statx __user *buffer; |
676 | }; | |
677 | ||
36f4fa68 JA |
678 | struct io_shutdown { |
679 | struct file *file; | |
680 | int how; | |
681 | }; | |
682 | ||
80a261fd JA |
683 | struct io_rename { |
684 | struct file *file; | |
685 | int old_dfd; | |
686 | int new_dfd; | |
687 | struct filename *oldpath; | |
688 | struct filename *newpath; | |
689 | int flags; | |
690 | }; | |
691 | ||
14a1143b JA |
692 | struct io_unlink { |
693 | struct file *file; | |
694 | int dfd; | |
695 | int flags; | |
696 | struct filename *filename; | |
697 | }; | |
698 | ||
e34a02dc DK |
699 | struct io_mkdir { |
700 | struct file *file; | |
701 | int dfd; | |
702 | umode_t mode; | |
703 | struct filename *filename; | |
704 | }; | |
705 | ||
7a8721f8 DK |
706 | struct io_symlink { |
707 | struct file *file; | |
708 | int new_dfd; | |
709 | struct filename *oldpath; | |
710 | struct filename *newpath; | |
711 | }; | |
712 | ||
cf30da90 DK |
713 | struct io_hardlink { |
714 | struct file *file; | |
715 | int old_dfd; | |
716 | int new_dfd; | |
717 | struct filename *oldpath; | |
718 | struct filename *newpath; | |
719 | int flags; | |
720 | }; | |
721 | ||
4f57f06c JA |
722 | struct io_msg { |
723 | struct file *file; | |
724 | u64 user_data; | |
725 | u32 len; | |
726 | }; | |
727 | ||
f499a021 JA |
728 | struct io_async_connect { |
729 | struct sockaddr_storage address; | |
730 | }; | |
731 | ||
03b1230c JA |
732 | struct io_async_msghdr { |
733 | struct iovec fast_iov[UIO_FASTIOV]; | |
257e84a5 PB |
734 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
735 | struct iovec *free_iov; | |
03b1230c JA |
736 | struct sockaddr __user *uaddr; |
737 | struct msghdr msg; | |
b537916c | 738 | struct sockaddr_storage addr; |
03b1230c JA |
739 | }; |
740 | ||
538941e2 | 741 | struct io_rw_state { |
ff6165b2 | 742 | struct iov_iter iter; |
cd658695 | 743 | struct iov_iter_state iter_state; |
c88598a9 | 744 | struct iovec fast_iov[UIO_FASTIOV]; |
538941e2 PB |
745 | }; |
746 | ||
747 | struct io_async_rw { | |
748 | struct io_rw_state s; | |
749 | const struct iovec *free_iovec; | |
227c0c96 | 750 | size_t bytes_done; |
bcf5a063 | 751 | struct wait_page_queue wpq; |
f67676d1 JA |
752 | }; |
753 | ||
6b47ee6e PB |
754 | enum { |
755 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, | |
756 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, | |
757 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, | |
758 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, | |
759 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, | |
bcda7baa | 760 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
04c76b41 | 761 | REQ_F_CQE_SKIP_BIT = IOSQE_CQE_SKIP_SUCCESS_BIT, |
6b47ee6e | 762 | |
dddca226 | 763 | /* first byte is taken by user flags, shift it to not overlap */ |
93d2bcd2 | 764 | REQ_F_FAIL_BIT = 8, |
6b47ee6e PB |
765 | REQ_F_INFLIGHT_BIT, |
766 | REQ_F_CUR_POS_BIT, | |
767 | REQ_F_NOWAIT_BIT, | |
6b47ee6e | 768 | REQ_F_LINK_TIMEOUT_BIT, |
99bc4c38 | 769 | REQ_F_NEED_CLEANUP_BIT, |
d7718a9d | 770 | REQ_F_POLLED_BIT, |
bcda7baa | 771 | REQ_F_BUFFER_SELECTED_BIT, |
e342c807 | 772 | REQ_F_COMPLETE_INLINE_BIT, |
230d50d4 | 773 | REQ_F_REISSUE_BIT, |
b8e64b53 | 774 | REQ_F_CREDS_BIT, |
20e60a38 | 775 | REQ_F_REFCOUNT_BIT, |
4d13d1a4 | 776 | REQ_F_ARM_LTIMEOUT_BIT, |
d886e185 | 777 | REQ_F_ASYNC_DATA_BIT, |
04c76b41 | 778 | REQ_F_SKIP_LINK_CQES_BIT, |
91eac1c6 JA |
779 | REQ_F_SINGLE_POLL_BIT, |
780 | REQ_F_DOUBLE_POLL_BIT, | |
8a3e8ee5 | 781 | REQ_F_PARTIAL_IO_BIT, |
7b29f92d | 782 | /* keep async read/write and isreg together and in order */ |
35645ac3 | 783 | REQ_F_SUPPORT_NOWAIT_BIT, |
7b29f92d | 784 | REQ_F_ISREG_BIT, |
84557871 JA |
785 | |
786 | /* not a real bit, just to check we're not overflowing the space */ | |
787 | __REQ_F_LAST_BIT, | |
6b47ee6e PB |
788 | }; |
789 | ||
790 | enum { | |
791 | /* ctx owns file */ | |
792 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), | |
793 | /* drain existing IO first */ | |
794 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), | |
795 | /* linked sqes */ | |
796 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), | |
797 | /* doesn't sever on completion < 0 */ | |
798 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), | |
799 | /* IOSQE_ASYNC */ | |
800 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), | |
bcda7baa JA |
801 | /* IOSQE_BUFFER_SELECT */ |
802 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), | |
04c76b41 PB |
803 | /* IOSQE_CQE_SKIP_SUCCESS */ |
804 | REQ_F_CQE_SKIP = BIT(REQ_F_CQE_SKIP_BIT), | |
6b47ee6e | 805 | |
6b47ee6e | 806 | /* fail rest of links */ |
93d2bcd2 | 807 | REQ_F_FAIL = BIT(REQ_F_FAIL_BIT), |
b05a1bcd | 808 | /* on inflight list, should be cancelled and waited on exit reliably */ |
6b47ee6e PB |
809 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
810 | /* read/write uses file position */ | |
811 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), | |
812 | /* must not punt to workers */ | |
813 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), | |
900fad45 | 814 | /* has or had linked timeout */ |
6b47ee6e | 815 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
99bc4c38 PB |
816 | /* needs cleanup */ |
817 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), | |
d7718a9d JA |
818 | /* already went through poll handler */ |
819 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), | |
bcda7baa JA |
820 | /* buffer already selected */ |
821 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), | |
e342c807 PB |
822 | /* completion is deferred through io_comp_state */ |
823 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), | |
230d50d4 JA |
824 | /* caller should reissue async */ |
825 | REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT), | |
35645ac3 PB |
826 | /* supports async reads/writes */ |
827 | REQ_F_SUPPORT_NOWAIT = BIT(REQ_F_SUPPORT_NOWAIT_BIT), | |
7b29f92d JA |
828 | /* regular file */ |
829 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), | |
b8e64b53 PB |
830 | /* has creds assigned */ |
831 | REQ_F_CREDS = BIT(REQ_F_CREDS_BIT), | |
20e60a38 PB |
832 | /* skip refcounting if not set */ |
833 | REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT), | |
4d13d1a4 PB |
834 | /* there is a linked timeout that has to be armed */ |
835 | REQ_F_ARM_LTIMEOUT = BIT(REQ_F_ARM_LTIMEOUT_BIT), | |
d886e185 PB |
836 | /* ->async_data allocated */ |
837 | REQ_F_ASYNC_DATA = BIT(REQ_F_ASYNC_DATA_BIT), | |
04c76b41 PB |
838 | /* don't post CQEs while failing linked requests */ |
839 | REQ_F_SKIP_LINK_CQES = BIT(REQ_F_SKIP_LINK_CQES_BIT), | |
91eac1c6 JA |
840 | /* single poll may be active */ |
841 | REQ_F_SINGLE_POLL = BIT(REQ_F_SINGLE_POLL_BIT), | |
842 | /* double poll may active */ | |
843 | REQ_F_DOUBLE_POLL = BIT(REQ_F_DOUBLE_POLL_BIT), | |
8a3e8ee5 JA |
844 | /* request has already done partial IO */ |
845 | REQ_F_PARTIAL_IO = BIT(REQ_F_PARTIAL_IO_BIT), | |
d7718a9d JA |
846 | }; |
847 | ||
848 | struct async_poll { | |
849 | struct io_poll_iocb poll; | |
807abcb0 | 850 | struct io_poll_iocb *double_poll; |
6b47ee6e PB |
851 | }; |
852 | ||
f237c30a | 853 | typedef void (*io_req_tw_func_t)(struct io_kiocb *req, bool *locked); |
5b0a6acc | 854 | |
7cbf1722 | 855 | struct io_task_work { |
5b0a6acc PB |
856 | union { |
857 | struct io_wq_work_node node; | |
858 | struct llist_node fallback_node; | |
859 | }; | |
860 | io_req_tw_func_t func; | |
7cbf1722 JA |
861 | }; |
862 | ||
992da01a PB |
863 | enum { |
864 | IORING_RSRC_FILE = 0, | |
865 | IORING_RSRC_BUFFER = 1, | |
866 | }; | |
867 | ||
09bb8394 JA |
868 | /* |
869 | * NOTE! Each of the iocb union members has the file pointer | |
870 | * as the first entry in their struct definition. So you can | |
871 | * access the file pointer through any of the sub-structs, | |
63c36549 | 872 | * or directly as just 'file' in this struct. |
09bb8394 | 873 | */ |
2b188cc1 | 874 | struct io_kiocb { |
221c5eb2 | 875 | union { |
09bb8394 | 876 | struct file *file; |
9adbd45d | 877 | struct io_rw rw; |
221c5eb2 | 878 | struct io_poll_iocb poll; |
9d805892 | 879 | struct io_poll_update poll_update; |
8ed8d3c3 JA |
880 | struct io_accept accept; |
881 | struct io_sync sync; | |
fbf23849 | 882 | struct io_cancel cancel; |
b29472ee | 883 | struct io_timeout timeout; |
0bdf7a2d | 884 | struct io_timeout_rem timeout_rem; |
3fbb51c1 | 885 | struct io_connect connect; |
e47293fd | 886 | struct io_sr_msg sr_msg; |
15b71abe | 887 | struct io_open open; |
b5dba59e | 888 | struct io_close close; |
269bbe5f | 889 | struct io_rsrc_update rsrc_update; |
4840e418 | 890 | struct io_fadvise fadvise; |
c1ca757b | 891 | struct io_madvise madvise; |
3e4827b0 | 892 | struct io_epoll epoll; |
7d67af2c | 893 | struct io_splice splice; |
ddf0322d | 894 | struct io_provide_buf pbuf; |
1d9e1288 | 895 | struct io_statx statx; |
36f4fa68 | 896 | struct io_shutdown shutdown; |
80a261fd | 897 | struct io_rename rename; |
14a1143b | 898 | struct io_unlink unlink; |
e34a02dc | 899 | struct io_mkdir mkdir; |
7a8721f8 | 900 | struct io_symlink symlink; |
cf30da90 | 901 | struct io_hardlink hardlink; |
4f57f06c | 902 | struct io_msg msg; |
221c5eb2 | 903 | }; |
2b188cc1 | 904 | |
d625c6ee | 905 | u8 opcode; |
65a6543d XW |
906 | /* polled IO has completed */ |
907 | u8 iopoll_completed; | |
4f4eeba8 | 908 | u16 buf_index; |
d17e56eb PB |
909 | unsigned int flags; |
910 | ||
911 | u64 user_data; | |
9cf7c104 | 912 | u32 result; |
82733d16 JA |
913 | /* fd initially, then cflags for completion */ |
914 | union { | |
915 | u32 cflags; | |
916 | int fd; | |
917 | }; | |
4f4eeba8 | 918 | |
010e8e6b | 919 | struct io_ring_ctx *ctx; |
010e8e6b | 920 | struct task_struct *task; |
d7718a9d | 921 | |
269bbe5f | 922 | struct percpu_ref *fixed_rsrc_refs; |
d886e185 PB |
923 | /* store used ubuf, so we can prevent reloading */ |
924 | struct io_mapped_ubuf *imu; | |
fcb323cc | 925 | |
2804ecd8 JA |
926 | union { |
927 | /* used by request caches, completion batching and iopoll */ | |
928 | struct io_wq_work_node comp_list; | |
929 | /* cache ->apoll->events */ | |
930 | int apoll_events; | |
931 | }; | |
d17e56eb | 932 | atomic_t refs; |
521d61fc | 933 | atomic_t poll_refs; |
5b0a6acc | 934 | struct io_task_work io_task_work; |
010e8e6b PB |
935 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
936 | struct hlist_node hash_node; | |
7e3709d5 | 937 | /* internal polling, see IORING_FEAT_FAST_POLL */ |
010e8e6b | 938 | struct async_poll *apoll; |
d886e185 PB |
939 | /* opcode allocated if it needs to store data for async defer */ |
940 | void *async_data; | |
7e3709d5 | 941 | /* stores selected buf, valid IFF REQ_F_BUFFER_SELECTED is set */ |
30d51dd4 | 942 | struct io_buffer *kbuf; |
41cdcc22 | 943 | /* linked requests, IFF REQ_F_HARDLINK or REQ_F_LINK are set */ |
34d2bfe7 | 944 | struct io_kiocb *link; |
41cdcc22 | 945 | /* custom credentials, valid IFF REQ_F_CREDS is set */ |
521d61fc JA |
946 | const struct cred *creds; |
947 | struct io_wq_work work; | |
2b188cc1 | 948 | }; |
05589553 | 949 | |
13bf43f5 PB |
950 | struct io_tctx_node { |
951 | struct list_head ctx_node; | |
952 | struct task_struct *task; | |
13bf43f5 PB |
953 | struct io_ring_ctx *ctx; |
954 | }; | |
955 | ||
27dc8338 PB |
956 | struct io_defer_entry { |
957 | struct list_head list; | |
958 | struct io_kiocb *req; | |
9cf7c104 | 959 | u32 seq; |
2b188cc1 JA |
960 | }; |
961 | ||
d3656344 | 962 | struct io_op_def { |
d3656344 JA |
963 | /* needs req->file assigned */ |
964 | unsigned needs_file : 1; | |
6d63416d PB |
965 | /* should block plug */ |
966 | unsigned plug : 1; | |
d3656344 JA |
967 | /* hash wq insertion if file is a regular file */ |
968 | unsigned hash_reg_file : 1; | |
969 | /* unbound wq insertion if file is a non-regular file */ | |
970 | unsigned unbound_nonreg_file : 1; | |
8a72758c JA |
971 | /* set if opcode supports polled "wait" */ |
972 | unsigned pollin : 1; | |
973 | unsigned pollout : 1; | |
52dd8640 | 974 | unsigned poll_exclusive : 1; |
bcda7baa JA |
975 | /* op supports buffer selection */ |
976 | unsigned buffer_select : 1; | |
26f0505a PB |
977 | /* do prep async if is going to be punted */ |
978 | unsigned needs_async_setup : 1; | |
6d63416d PB |
979 | /* opcode is not supported by this kernel */ |
980 | unsigned not_supported : 1; | |
5bd2182d PM |
981 | /* skip auditing */ |
982 | unsigned audit_skip : 1; | |
e8c2bc1f JA |
983 | /* size of async data needed, if any */ |
984 | unsigned short async_size; | |
d3656344 JA |
985 | }; |
986 | ||
0918682b | 987 | static const struct io_op_def io_op_defs[] = { |
0463b6c5 PB |
988 | [IORING_OP_NOP] = {}, |
989 | [IORING_OP_READV] = { | |
d3656344 JA |
990 | .needs_file = 1, |
991 | .unbound_nonreg_file = 1, | |
8a72758c | 992 | .pollin = 1, |
4d954c25 | 993 | .buffer_select = 1, |
26f0505a | 994 | .needs_async_setup = 1, |
27926b68 | 995 | .plug = 1, |
5bd2182d | 996 | .audit_skip = 1, |
e8c2bc1f | 997 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 998 | }, |
0463b6c5 | 999 | [IORING_OP_WRITEV] = { |
d3656344 JA |
1000 | .needs_file = 1, |
1001 | .hash_reg_file = 1, | |
1002 | .unbound_nonreg_file = 1, | |
8a72758c | 1003 | .pollout = 1, |
26f0505a | 1004 | .needs_async_setup = 1, |
27926b68 | 1005 | .plug = 1, |
5bd2182d | 1006 | .audit_skip = 1, |
e8c2bc1f | 1007 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 1008 | }, |
0463b6c5 | 1009 | [IORING_OP_FSYNC] = { |
d3656344 | 1010 | .needs_file = 1, |
5bd2182d | 1011 | .audit_skip = 1, |
d3656344 | 1012 | }, |
0463b6c5 | 1013 | [IORING_OP_READ_FIXED] = { |
d3656344 JA |
1014 | .needs_file = 1, |
1015 | .unbound_nonreg_file = 1, | |
8a72758c | 1016 | .pollin = 1, |
27926b68 | 1017 | .plug = 1, |
5bd2182d | 1018 | .audit_skip = 1, |
e8c2bc1f | 1019 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 1020 | }, |
0463b6c5 | 1021 | [IORING_OP_WRITE_FIXED] = { |
d3656344 JA |
1022 | .needs_file = 1, |
1023 | .hash_reg_file = 1, | |
1024 | .unbound_nonreg_file = 1, | |
8a72758c | 1025 | .pollout = 1, |
27926b68 | 1026 | .plug = 1, |
5bd2182d | 1027 | .audit_skip = 1, |
e8c2bc1f | 1028 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 1029 | }, |
0463b6c5 | 1030 | [IORING_OP_POLL_ADD] = { |
d3656344 JA |
1031 | .needs_file = 1, |
1032 | .unbound_nonreg_file = 1, | |
5bd2182d PM |
1033 | .audit_skip = 1, |
1034 | }, | |
1035 | [IORING_OP_POLL_REMOVE] = { | |
1036 | .audit_skip = 1, | |
d3656344 | 1037 | }, |
0463b6c5 | 1038 | [IORING_OP_SYNC_FILE_RANGE] = { |
d3656344 | 1039 | .needs_file = 1, |
5bd2182d | 1040 | .audit_skip = 1, |
d3656344 | 1041 | }, |
0463b6c5 | 1042 | [IORING_OP_SENDMSG] = { |
d3656344 JA |
1043 | .needs_file = 1, |
1044 | .unbound_nonreg_file = 1, | |
8a72758c | 1045 | .pollout = 1, |
26f0505a | 1046 | .needs_async_setup = 1, |
e8c2bc1f | 1047 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 1048 | }, |
0463b6c5 | 1049 | [IORING_OP_RECVMSG] = { |
d3656344 JA |
1050 | .needs_file = 1, |
1051 | .unbound_nonreg_file = 1, | |
8a72758c | 1052 | .pollin = 1, |
52de1fe1 | 1053 | .buffer_select = 1, |
26f0505a | 1054 | .needs_async_setup = 1, |
e8c2bc1f | 1055 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 1056 | }, |
0463b6c5 | 1057 | [IORING_OP_TIMEOUT] = { |
5bd2182d | 1058 | .audit_skip = 1, |
e8c2bc1f | 1059 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 1060 | }, |
9c8e11b3 PB |
1061 | [IORING_OP_TIMEOUT_REMOVE] = { |
1062 | /* used by timeout updates' prep() */ | |
5bd2182d | 1063 | .audit_skip = 1, |
9c8e11b3 | 1064 | }, |
0463b6c5 | 1065 | [IORING_OP_ACCEPT] = { |
d3656344 JA |
1066 | .needs_file = 1, |
1067 | .unbound_nonreg_file = 1, | |
8a72758c | 1068 | .pollin = 1, |
52dd8640 | 1069 | .poll_exclusive = 1, |
d3656344 | 1070 | }, |
5bd2182d PM |
1071 | [IORING_OP_ASYNC_CANCEL] = { |
1072 | .audit_skip = 1, | |
1073 | }, | |
0463b6c5 | 1074 | [IORING_OP_LINK_TIMEOUT] = { |
5bd2182d | 1075 | .audit_skip = 1, |
e8c2bc1f | 1076 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 1077 | }, |
0463b6c5 | 1078 | [IORING_OP_CONNECT] = { |
d3656344 JA |
1079 | .needs_file = 1, |
1080 | .unbound_nonreg_file = 1, | |
8a72758c | 1081 | .pollout = 1, |
26f0505a | 1082 | .needs_async_setup = 1, |
e8c2bc1f | 1083 | .async_size = sizeof(struct io_async_connect), |
d3656344 | 1084 | }, |
0463b6c5 | 1085 | [IORING_OP_FALLOCATE] = { |
d3656344 | 1086 | .needs_file = 1, |
d3656344 | 1087 | }, |
44526bed JA |
1088 | [IORING_OP_OPENAT] = {}, |
1089 | [IORING_OP_CLOSE] = {}, | |
5bd2182d PM |
1090 | [IORING_OP_FILES_UPDATE] = { |
1091 | .audit_skip = 1, | |
1092 | }, | |
1093 | [IORING_OP_STATX] = { | |
1094 | .audit_skip = 1, | |
1095 | }, | |
0463b6c5 | 1096 | [IORING_OP_READ] = { |
3a6820f2 JA |
1097 | .needs_file = 1, |
1098 | .unbound_nonreg_file = 1, | |
8a72758c | 1099 | .pollin = 1, |
bcda7baa | 1100 | .buffer_select = 1, |
27926b68 | 1101 | .plug = 1, |
5bd2182d | 1102 | .audit_skip = 1, |
e8c2bc1f | 1103 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 1104 | }, |
0463b6c5 | 1105 | [IORING_OP_WRITE] = { |
3a6820f2 | 1106 | .needs_file = 1, |
7b3188e7 | 1107 | .hash_reg_file = 1, |
3a6820f2 | 1108 | .unbound_nonreg_file = 1, |
8a72758c | 1109 | .pollout = 1, |
27926b68 | 1110 | .plug = 1, |
5bd2182d | 1111 | .audit_skip = 1, |
e8c2bc1f | 1112 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 1113 | }, |
0463b6c5 | 1114 | [IORING_OP_FADVISE] = { |
4840e418 | 1115 | .needs_file = 1, |
5bd2182d | 1116 | .audit_skip = 1, |
c1ca757b | 1117 | }, |
44526bed | 1118 | [IORING_OP_MADVISE] = {}, |
0463b6c5 | 1119 | [IORING_OP_SEND] = { |
fddaface JA |
1120 | .needs_file = 1, |
1121 | .unbound_nonreg_file = 1, | |
8a72758c | 1122 | .pollout = 1, |
5bd2182d | 1123 | .audit_skip = 1, |
fddaface | 1124 | }, |
0463b6c5 | 1125 | [IORING_OP_RECV] = { |
fddaface JA |
1126 | .needs_file = 1, |
1127 | .unbound_nonreg_file = 1, | |
8a72758c | 1128 | .pollin = 1, |
bcda7baa | 1129 | .buffer_select = 1, |
5bd2182d | 1130 | .audit_skip = 1, |
fddaface | 1131 | }, |
0463b6c5 | 1132 | [IORING_OP_OPENAT2] = { |
cebdb986 | 1133 | }, |
3e4827b0 JA |
1134 | [IORING_OP_EPOLL_CTL] = { |
1135 | .unbound_nonreg_file = 1, | |
5bd2182d | 1136 | .audit_skip = 1, |
3e4827b0 | 1137 | }, |
7d67af2c PB |
1138 | [IORING_OP_SPLICE] = { |
1139 | .needs_file = 1, | |
1140 | .hash_reg_file = 1, | |
1141 | .unbound_nonreg_file = 1, | |
5bd2182d PM |
1142 | .audit_skip = 1, |
1143 | }, | |
1144 | [IORING_OP_PROVIDE_BUFFERS] = { | |
1145 | .audit_skip = 1, | |
1146 | }, | |
1147 | [IORING_OP_REMOVE_BUFFERS] = { | |
1148 | .audit_skip = 1, | |
ddf0322d | 1149 | }, |
f2a8d5c7 PB |
1150 | [IORING_OP_TEE] = { |
1151 | .needs_file = 1, | |
1152 | .hash_reg_file = 1, | |
1153 | .unbound_nonreg_file = 1, | |
5bd2182d | 1154 | .audit_skip = 1, |
f2a8d5c7 | 1155 | }, |
36f4fa68 JA |
1156 | [IORING_OP_SHUTDOWN] = { |
1157 | .needs_file = 1, | |
1158 | }, | |
44526bed JA |
1159 | [IORING_OP_RENAMEAT] = {}, |
1160 | [IORING_OP_UNLINKAT] = {}, | |
e34a02dc | 1161 | [IORING_OP_MKDIRAT] = {}, |
7a8721f8 | 1162 | [IORING_OP_SYMLINKAT] = {}, |
cf30da90 | 1163 | [IORING_OP_LINKAT] = {}, |
4f57f06c JA |
1164 | [IORING_OP_MSG_RING] = { |
1165 | .needs_file = 1, | |
1166 | }, | |
d3656344 JA |
1167 | }; |
1168 | ||
0756a869 PB |
1169 | /* requests with any of those set should undergo io_disarm_next() */ |
1170 | #define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL) | |
1171 | ||
7a612350 | 1172 | static bool io_disarm_next(struct io_kiocb *req); |
eef51daa | 1173 | static void io_uring_del_tctx_node(unsigned long index); |
9936c7c2 PB |
1174 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
1175 | struct task_struct *task, | |
3dd0c97a | 1176 | bool cancel_all); |
78cc687b | 1177 | static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd); |
1ffc5422 | 1178 | |
913a571a PB |
1179 | static void io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags); |
1180 | ||
ec9c02ad | 1181 | static void io_put_req(struct io_kiocb *req); |
91c2f697 | 1182 | static void io_put_req_deferred(struct io_kiocb *req); |
c7dae4ba | 1183 | static void io_dismantle_req(struct io_kiocb *req); |
94ae5e77 | 1184 | static void io_queue_linked_timeout(struct io_kiocb *req); |
fdecb662 | 1185 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 1186 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 | 1187 | unsigned nr_args); |
68fb8979 | 1188 | static void io_clean_op(struct io_kiocb *req); |
5106dd6e JA |
1189 | static inline struct file *io_file_get_fixed(struct io_kiocb *req, int fd, |
1190 | unsigned issue_flags); | |
1191 | static inline struct file *io_file_get_normal(struct io_kiocb *req, int fd); | |
d5361233 JA |
1192 | static void io_drop_inflight_file(struct io_kiocb *req); |
1193 | static bool io_assign_file(struct io_kiocb *req, unsigned int issue_flags); | |
c5eef2b9 | 1194 | static void __io_queue_sqe(struct io_kiocb *req); |
269bbe5f | 1195 | static void io_rsrc_put_work(struct work_struct *work); |
de0617e4 | 1196 | |
907d1df3 | 1197 | static void io_req_task_queue(struct io_kiocb *req); |
c450178d | 1198 | static void __io_submit_flush_completions(struct io_ring_ctx *ctx); |
179ae0d1 | 1199 | static int io_req_prep_async(struct io_kiocb *req); |
de0617e4 | 1200 | |
b9445598 PB |
1201 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
1202 | unsigned int issue_flags, u32 slot_index); | |
7df778be PB |
1203 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags); |
1204 | ||
f1042b6c | 1205 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer); |
9aa8dfde | 1206 | static void io_eventfd_signal(struct io_ring_ctx *ctx); |
b9445598 | 1207 | |
2b188cc1 JA |
1208 | static struct kmem_cache *req_cachep; |
1209 | ||
0918682b | 1210 | static const struct file_operations io_uring_fops; |
2b188cc1 JA |
1211 | |
1212 | struct sock *io_uring_get_socket(struct file *file) | |
1213 | { | |
1214 | #if defined(CONFIG_UNIX) | |
1215 | if (file->f_op == &io_uring_fops) { | |
1216 | struct io_ring_ctx *ctx = file->private_data; | |
1217 | ||
1218 | return ctx->ring_sock->sk; | |
1219 | } | |
1220 | #endif | |
1221 | return NULL; | |
1222 | } | |
1223 | EXPORT_SYMBOL(io_uring_get_socket); | |
1224 | ||
f237c30a PB |
1225 | static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked) |
1226 | { | |
1227 | if (!*locked) { | |
1228 | mutex_lock(&ctx->uring_lock); | |
1229 | *locked = true; | |
1230 | } | |
1231 | } | |
1232 | ||
f2f87370 PB |
1233 | #define io_for_each_link(pos, head) \ |
1234 | for (pos = (head); pos; pos = pos->link) | |
1235 | ||
21c843d5 PB |
1236 | /* |
1237 | * Shamelessly stolen from the mm implementation of page reference checking, | |
1238 | * see commit f958d7b528b1 for details. | |
1239 | */ | |
1240 | #define req_ref_zero_or_close_to_overflow(req) \ | |
1241 | ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u) | |
1242 | ||
1243 | static inline bool req_ref_inc_not_zero(struct io_kiocb *req) | |
1244 | { | |
20e60a38 | 1245 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1246 | return atomic_inc_not_zero(&req->refs); |
1247 | } | |
1248 | ||
21c843d5 PB |
1249 | static inline bool req_ref_put_and_test(struct io_kiocb *req) |
1250 | { | |
20e60a38 PB |
1251 | if (likely(!(req->flags & REQ_F_REFCOUNT))) |
1252 | return true; | |
1253 | ||
21c843d5 PB |
1254 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1255 | return atomic_dec_and_test(&req->refs); | |
1256 | } | |
1257 | ||
21c843d5 PB |
1258 | static inline void req_ref_get(struct io_kiocb *req) |
1259 | { | |
20e60a38 | 1260 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1261 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1262 | atomic_inc(&req->refs); | |
1263 | } | |
1264 | ||
c450178d PB |
1265 | static inline void io_submit_flush_completions(struct io_ring_ctx *ctx) |
1266 | { | |
6f33b0bc | 1267 | if (!wq_list_empty(&ctx->submit_state.compl_reqs)) |
c450178d PB |
1268 | __io_submit_flush_completions(ctx); |
1269 | } | |
1270 | ||
48dcd38d | 1271 | static inline void __io_req_set_refcount(struct io_kiocb *req, int nr) |
20e60a38 PB |
1272 | { |
1273 | if (!(req->flags & REQ_F_REFCOUNT)) { | |
1274 | req->flags |= REQ_F_REFCOUNT; | |
48dcd38d | 1275 | atomic_set(&req->refs, nr); |
20e60a38 PB |
1276 | } |
1277 | } | |
1278 | ||
48dcd38d PB |
1279 | static inline void io_req_set_refcount(struct io_kiocb *req) |
1280 | { | |
1281 | __io_req_set_refcount(req, 1); | |
1282 | } | |
1283 | ||
ab409402 PB |
1284 | #define IO_RSRC_REF_BATCH 100 |
1285 | ||
1286 | static inline void io_req_put_rsrc_locked(struct io_kiocb *req, | |
1287 | struct io_ring_ctx *ctx) | |
1288 | __must_hold(&ctx->uring_lock) | |
36f72fe2 | 1289 | { |
ab409402 PB |
1290 | struct percpu_ref *ref = req->fixed_rsrc_refs; |
1291 | ||
1292 | if (ref) { | |
1293 | if (ref == &ctx->rsrc_node->refs) | |
1294 | ctx->rsrc_cached_refs++; | |
1295 | else | |
1296 | percpu_ref_put(ref); | |
1297 | } | |
1298 | } | |
1299 | ||
1300 | static inline void io_req_put_rsrc(struct io_kiocb *req, struct io_ring_ctx *ctx) | |
1301 | { | |
1302 | if (req->fixed_rsrc_refs) | |
1303 | percpu_ref_put(req->fixed_rsrc_refs); | |
1304 | } | |
1305 | ||
1306 | static __cold void io_rsrc_refs_drop(struct io_ring_ctx *ctx) | |
1307 | __must_hold(&ctx->uring_lock) | |
1308 | { | |
1309 | if (ctx->rsrc_cached_refs) { | |
1310 | percpu_ref_put_many(&ctx->rsrc_node->refs, ctx->rsrc_cached_refs); | |
1311 | ctx->rsrc_cached_refs = 0; | |
1312 | } | |
1313 | } | |
1314 | ||
1315 | static void io_rsrc_refs_refill(struct io_ring_ctx *ctx) | |
1316 | __must_hold(&ctx->uring_lock) | |
1317 | { | |
1318 | ctx->rsrc_cached_refs += IO_RSRC_REF_BATCH; | |
1319 | percpu_ref_get_many(&ctx->rsrc_node->refs, IO_RSRC_REF_BATCH); | |
1320 | } | |
36f72fe2 | 1321 | |
a46be971 | 1322 | static inline void io_req_set_rsrc_node(struct io_kiocb *req, |
5106dd6e JA |
1323 | struct io_ring_ctx *ctx, |
1324 | unsigned int issue_flags) | |
36f72fe2 | 1325 | { |
269bbe5f | 1326 | if (!req->fixed_rsrc_refs) { |
a7f0ed5a | 1327 | req->fixed_rsrc_refs = &ctx->rsrc_node->refs; |
5106dd6e JA |
1328 | |
1329 | if (!(issue_flags & IO_URING_F_UNLOCKED)) { | |
1330 | lockdep_assert_held(&ctx->uring_lock); | |
1331 | ctx->rsrc_cached_refs--; | |
1332 | if (unlikely(ctx->rsrc_cached_refs < 0)) | |
1333 | io_rsrc_refs_refill(ctx); | |
1334 | } else { | |
1335 | percpu_ref_get(req->fixed_rsrc_refs); | |
1336 | } | |
36f72fe2 PB |
1337 | } |
1338 | } | |
1339 | ||
cc3cec83 | 1340 | static unsigned int __io_put_kbuf(struct io_kiocb *req, struct list_head *list) |
3648e526 | 1341 | { |
d1fd1c20 | 1342 | struct io_buffer *kbuf = req->kbuf; |
3648e526 HX |
1343 | unsigned int cflags; |
1344 | ||
cc3cec83 | 1345 | cflags = IORING_CQE_F_BUFFER | (kbuf->bid << IORING_CQE_BUFFER_SHIFT); |
3648e526 | 1346 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
cc3cec83 | 1347 | list_add(&kbuf->list, list); |
d1fd1c20 | 1348 | req->kbuf = NULL; |
3648e526 HX |
1349 | return cflags; |
1350 | } | |
1351 | ||
cc3cec83 | 1352 | static inline unsigned int io_put_kbuf_comp(struct io_kiocb *req) |
3648e526 | 1353 | { |
8197b053 PB |
1354 | lockdep_assert_held(&req->ctx->completion_lock); |
1355 | ||
3648e526 HX |
1356 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) |
1357 | return 0; | |
cc3cec83 JA |
1358 | return __io_put_kbuf(req, &req->ctx->io_buffers_comp); |
1359 | } | |
1360 | ||
1361 | static inline unsigned int io_put_kbuf(struct io_kiocb *req, | |
1362 | unsigned issue_flags) | |
1363 | { | |
1364 | unsigned int cflags; | |
1365 | ||
1366 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) | |
1367 | return 0; | |
1368 | ||
1369 | /* | |
1370 | * We can add this buffer back to two lists: | |
1371 | * | |
1372 | * 1) The io_buffers_cache list. This one is protected by the | |
1373 | * ctx->uring_lock. If we already hold this lock, add back to this | |
1374 | * list as we can grab it from issue as well. | |
1375 | * 2) The io_buffers_comp list. This one is protected by the | |
1376 | * ctx->completion_lock. | |
1377 | * | |
1378 | * We migrate buffers from the comp_list to the issue cache list | |
1379 | * when we need one. | |
1380 | */ | |
1381 | if (issue_flags & IO_URING_F_UNLOCKED) { | |
1382 | struct io_ring_ctx *ctx = req->ctx; | |
1383 | ||
1384 | spin_lock(&ctx->completion_lock); | |
1385 | cflags = __io_put_kbuf(req, &ctx->io_buffers_comp); | |
1386 | spin_unlock(&ctx->completion_lock); | |
1387 | } else { | |
ab0ac095 PB |
1388 | lockdep_assert_held(&req->ctx->uring_lock); |
1389 | ||
cc3cec83 JA |
1390 | cflags = __io_put_kbuf(req, &req->ctx->io_buffers_cache); |
1391 | } | |
1392 | ||
1393 | return cflags; | |
3648e526 HX |
1394 | } |
1395 | ||
dbc7d452 JA |
1396 | static struct io_buffer_list *io_buffer_get_list(struct io_ring_ctx *ctx, |
1397 | unsigned int bgid) | |
1398 | { | |
1399 | struct list_head *hash_list; | |
1400 | struct io_buffer_list *bl; | |
1401 | ||
1402 | hash_list = &ctx->io_buffers[hash_32(bgid, IO_BUFFERS_HASH_BITS)]; | |
1403 | list_for_each_entry(bl, hash_list, list) | |
1404 | if (bl->bgid == bgid || bgid == -1U) | |
1405 | return bl; | |
1406 | ||
1407 | return NULL; | |
1408 | } | |
1409 | ||
4d55f238 | 1410 | static void io_kbuf_recycle(struct io_kiocb *req, unsigned issue_flags) |
b1c62645 JA |
1411 | { |
1412 | struct io_ring_ctx *ctx = req->ctx; | |
dbc7d452 JA |
1413 | struct io_buffer_list *bl; |
1414 | struct io_buffer *buf; | |
b1c62645 JA |
1415 | |
1416 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) | |
1417 | return; | |
8a3e8ee5 JA |
1418 | /* don't recycle if we already did IO to this buffer */ |
1419 | if (req->flags & REQ_F_PARTIAL_IO) | |
1420 | return; | |
b1c62645 | 1421 | |
4d55f238 JA |
1422 | if (issue_flags & IO_URING_F_UNLOCKED) |
1423 | mutex_lock(&ctx->uring_lock); | |
b1c62645 JA |
1424 | |
1425 | lockdep_assert_held(&ctx->uring_lock); | |
1426 | ||
1427 | buf = req->kbuf; | |
dbc7d452 JA |
1428 | bl = io_buffer_get_list(ctx, buf->bgid); |
1429 | list_add(&buf->list, &bl->buf_list); | |
b1c62645 JA |
1430 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
1431 | req->kbuf = NULL; | |
4d55f238 JA |
1432 | |
1433 | if (issue_flags & IO_URING_F_UNLOCKED) | |
1434 | mutex_unlock(&ctx->uring_lock); | |
b1c62645 JA |
1435 | } |
1436 | ||
3dd0c97a PB |
1437 | static bool io_match_task(struct io_kiocb *head, struct task_struct *task, |
1438 | bool cancel_all) | |
6af3f48b | 1439 | __must_hold(&req->ctx->timeout_lock) |
08d23634 | 1440 | { |
68207680 | 1441 | if (task && head->task != task) |
08d23634 | 1442 | return false; |
d5361233 | 1443 | return cancel_all; |
6af3f48b PB |
1444 | } |
1445 | ||
1446 | /* | |
1447 | * As io_match_task() but protected against racing with linked timeouts. | |
1448 | * User must not hold timeout_lock. | |
1449 | */ | |
1450 | static bool io_match_task_safe(struct io_kiocb *head, struct task_struct *task, | |
1451 | bool cancel_all) | |
1452 | { | |
6af3f48b PB |
1453 | if (task && head->task != task) |
1454 | return false; | |
d5361233 | 1455 | return cancel_all; |
6af3f48b PB |
1456 | } |
1457 | ||
d886e185 PB |
1458 | static inline bool req_has_async_data(struct io_kiocb *req) |
1459 | { | |
1460 | return req->flags & REQ_F_ASYNC_DATA; | |
1461 | } | |
1462 | ||
93d2bcd2 | 1463 | static inline void req_set_fail(struct io_kiocb *req) |
c40f6379 | 1464 | { |
93d2bcd2 | 1465 | req->flags |= REQ_F_FAIL; |
04c76b41 PB |
1466 | if (req->flags & REQ_F_CQE_SKIP) { |
1467 | req->flags &= ~REQ_F_CQE_SKIP; | |
1468 | req->flags |= REQ_F_SKIP_LINK_CQES; | |
1469 | } | |
c40f6379 | 1470 | } |
4a38aed2 | 1471 | |
a8295b98 HX |
1472 | static inline void req_fail_link_node(struct io_kiocb *req, int res) |
1473 | { | |
1474 | req_set_fail(req); | |
1475 | req->result = res; | |
1476 | } | |
1477 | ||
c072481d | 1478 | static __cold void io_ring_ctx_ref_free(struct percpu_ref *ref) |
2b188cc1 JA |
1479 | { |
1480 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
1481 | ||
0f158b4c | 1482 | complete(&ctx->ref_comp); |
2b188cc1 JA |
1483 | } |
1484 | ||
8eb7e2d0 PB |
1485 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
1486 | { | |
1487 | return !req->timeout.off; | |
1488 | } | |
1489 | ||
c072481d | 1490 | static __cold void io_fallback_req_func(struct work_struct *work) |
f56165e6 PB |
1491 | { |
1492 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, | |
1493 | fallback_work.work); | |
1494 | struct llist_node *node = llist_del_all(&ctx->fallback_llist); | |
1495 | struct io_kiocb *req, *tmp; | |
f237c30a | 1496 | bool locked = false; |
f56165e6 PB |
1497 | |
1498 | percpu_ref_get(&ctx->refs); | |
1499 | llist_for_each_entry_safe(req, tmp, node, io_task_work.fallback_node) | |
f237c30a | 1500 | req->io_task_work.func(req, &locked); |
5636c00d | 1501 | |
f237c30a | 1502 | if (locked) { |
c450178d | 1503 | io_submit_flush_completions(ctx); |
f237c30a PB |
1504 | mutex_unlock(&ctx->uring_lock); |
1505 | } | |
f56165e6 PB |
1506 | percpu_ref_put(&ctx->refs); |
1507 | } | |
1508 | ||
c072481d | 1509 | static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
2b188cc1 JA |
1510 | { |
1511 | struct io_ring_ctx *ctx; | |
dbc7d452 | 1512 | int i, hash_bits; |
2b188cc1 JA |
1513 | |
1514 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1515 | if (!ctx) | |
1516 | return NULL; | |
1517 | ||
78076bb6 JA |
1518 | /* |
1519 | * Use 5 bits less than the max cq entries, that should give us around | |
1520 | * 32 entries per hash list if totally full and uniformly spread. | |
1521 | */ | |
1522 | hash_bits = ilog2(p->cq_entries); | |
1523 | hash_bits -= 5; | |
1524 | if (hash_bits <= 0) | |
1525 | hash_bits = 1; | |
1526 | ctx->cancel_hash_bits = hash_bits; | |
1527 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
1528 | GFP_KERNEL); | |
1529 | if (!ctx->cancel_hash) | |
1530 | goto err; | |
1531 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
1532 | ||
6224843d PB |
1533 | ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL); |
1534 | if (!ctx->dummy_ubuf) | |
1535 | goto err; | |
1536 | /* set invalid range, so io_import_fixed() fails meeting it */ | |
1537 | ctx->dummy_ubuf->ubuf = -1UL; | |
1538 | ||
dbc7d452 JA |
1539 | ctx->io_buffers = kcalloc(1U << IO_BUFFERS_HASH_BITS, |
1540 | sizeof(struct list_head), GFP_KERNEL); | |
1541 | if (!ctx->io_buffers) | |
1542 | goto err; | |
1543 | for (i = 0; i < (1U << IO_BUFFERS_HASH_BITS); i++) | |
1544 | INIT_LIST_HEAD(&ctx->io_buffers[i]); | |
1545 | ||
21482896 | 1546 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
1547 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
1548 | goto err; | |
2b188cc1 JA |
1549 | |
1550 | ctx->flags = p->flags; | |
90554200 | 1551 | init_waitqueue_head(&ctx->sqo_sq_wait); |
69fb2131 | 1552 | INIT_LIST_HEAD(&ctx->sqd_list); |
1d7bb1d5 | 1553 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
cc3cec83 | 1554 | INIT_LIST_HEAD(&ctx->io_buffers_cache); |
4d9237e3 | 1555 | INIT_LIST_HEAD(&ctx->apoll_cache); |
0f158b4c | 1556 | init_completion(&ctx->ref_comp); |
61cf9370 | 1557 | xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1); |
2b188cc1 | 1558 | mutex_init(&ctx->uring_lock); |
311997b3 | 1559 | init_waitqueue_head(&ctx->cq_wait); |
2b188cc1 | 1560 | spin_lock_init(&ctx->completion_lock); |
89850fce | 1561 | spin_lock_init(&ctx->timeout_lock); |
5eef4e87 | 1562 | INIT_WQ_LIST(&ctx->iopoll_list); |
cc3cec83 JA |
1563 | INIT_LIST_HEAD(&ctx->io_buffers_pages); |
1564 | INIT_LIST_HEAD(&ctx->io_buffers_comp); | |
de0617e4 | 1565 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 1566 | INIT_LIST_HEAD(&ctx->timeout_list); |
ef9dd637 | 1567 | INIT_LIST_HEAD(&ctx->ltimeout_list); |
d67d2263 BM |
1568 | spin_lock_init(&ctx->rsrc_ref_lock); |
1569 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); | |
269bbe5f BM |
1570 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
1571 | init_llist_head(&ctx->rsrc_put_llist); | |
13bf43f5 | 1572 | INIT_LIST_HEAD(&ctx->tctx_list); |
c2b6c6bc PB |
1573 | ctx->submit_state.free_list.next = NULL; |
1574 | INIT_WQ_LIST(&ctx->locked_free_list); | |
9011bf9a | 1575 | INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func); |
6f33b0bc | 1576 | INIT_WQ_LIST(&ctx->submit_state.compl_reqs); |
2b188cc1 | 1577 | return ctx; |
206aefde | 1578 | err: |
6224843d | 1579 | kfree(ctx->dummy_ubuf); |
78076bb6 | 1580 | kfree(ctx->cancel_hash); |
dbc7d452 | 1581 | kfree(ctx->io_buffers); |
206aefde JA |
1582 | kfree(ctx); |
1583 | return NULL; | |
2b188cc1 JA |
1584 | } |
1585 | ||
8f6ed49a PB |
1586 | static void io_account_cq_overflow(struct io_ring_ctx *ctx) |
1587 | { | |
1588 | struct io_rings *r = ctx->rings; | |
1589 | ||
1590 | WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1); | |
1591 | ctx->cq_extra--; | |
1592 | } | |
1593 | ||
9cf7c104 | 1594 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
7adf4eaf | 1595 | { |
2bc9930e JA |
1596 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
1597 | struct io_ring_ctx *ctx = req->ctx; | |
a197f664 | 1598 | |
8f6ed49a | 1599 | return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail; |
2bc9930e | 1600 | } |
de0617e4 | 1601 | |
9d858b21 | 1602 | return false; |
de0617e4 JA |
1603 | } |
1604 | ||
35645ac3 PB |
1605 | #define FFS_NOWAIT 0x1UL |
1606 | #define FFS_ISREG 0x2UL | |
1607 | #define FFS_MASK ~(FFS_NOWAIT|FFS_ISREG) | |
c97d8a0f PB |
1608 | |
1609 | static inline bool io_req_ffs_set(struct io_kiocb *req) | |
1610 | { | |
35645ac3 | 1611 | return req->flags & REQ_F_FIXED_FILE; |
c97d8a0f PB |
1612 | } |
1613 | ||
fd08e530 PB |
1614 | static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req) |
1615 | { | |
906c6caa PB |
1616 | if (WARN_ON_ONCE(!req->link)) |
1617 | return NULL; | |
1618 | ||
4d13d1a4 PB |
1619 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
1620 | req->flags |= REQ_F_LINK_TIMEOUT; | |
fd08e530 PB |
1621 | |
1622 | /* linked timeouts should have two refs once prep'ed */ | |
48dcd38d | 1623 | io_req_set_refcount(req); |
4d13d1a4 PB |
1624 | __io_req_set_refcount(req->link, 2); |
1625 | return req->link; | |
fd08e530 PB |
1626 | } |
1627 | ||
1628 | static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) | |
1629 | { | |
4d13d1a4 | 1630 | if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT))) |
fd08e530 PB |
1631 | return NULL; |
1632 | return __io_prep_linked_timeout(req); | |
1633 | } | |
1634 | ||
1e6fa521 JA |
1635 | static void io_prep_async_work(struct io_kiocb *req) |
1636 | { | |
1637 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
1e6fa521 JA |
1638 | struct io_ring_ctx *ctx = req->ctx; |
1639 | ||
b8e64b53 PB |
1640 | if (!(req->flags & REQ_F_CREDS)) { |
1641 | req->flags |= REQ_F_CREDS; | |
c10d1f98 | 1642 | req->creds = get_current_cred(); |
b8e64b53 | 1643 | } |
003e8dcc | 1644 | |
e1d675df PB |
1645 | req->work.list.next = NULL; |
1646 | req->work.flags = 0; | |
feaadc4f PB |
1647 | if (req->flags & REQ_F_FORCE_ASYNC) |
1648 | req->work.flags |= IO_WQ_WORK_CONCURRENT; | |
1649 | ||
1e6fa521 JA |
1650 | if (req->flags & REQ_F_ISREG) { |
1651 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) | |
1652 | io_wq_hash_work(&req->work, file_inode(req->file)); | |
4b982bd0 | 1653 | } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) { |
1e6fa521 JA |
1654 | if (def->unbound_nonreg_file) |
1655 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1656 | } | |
561fb04a | 1657 | } |
cccf0ee8 | 1658 | |
cbdcb435 | 1659 | static void io_prep_async_link(struct io_kiocb *req) |
561fb04a | 1660 | { |
cbdcb435 | 1661 | struct io_kiocb *cur; |
54a91f3b | 1662 | |
44eff40a PB |
1663 | if (req->flags & REQ_F_LINK_TIMEOUT) { |
1664 | struct io_ring_ctx *ctx = req->ctx; | |
1665 | ||
674ee8e1 | 1666 | spin_lock_irq(&ctx->timeout_lock); |
44eff40a PB |
1667 | io_for_each_link(cur, req) |
1668 | io_prep_async_work(cur); | |
674ee8e1 | 1669 | spin_unlock_irq(&ctx->timeout_lock); |
44eff40a PB |
1670 | } else { |
1671 | io_for_each_link(cur, req) | |
1672 | io_prep_async_work(cur); | |
1673 | } | |
561fb04a JA |
1674 | } |
1675 | ||
fff4e40e PB |
1676 | static inline void io_req_add_compl_list(struct io_kiocb *req) |
1677 | { | |
3d4aeb9f | 1678 | struct io_ring_ctx *ctx = req->ctx; |
33ce2aff | 1679 | struct io_submit_state *state = &ctx->submit_state; |
fff4e40e | 1680 | |
3d4aeb9f PB |
1681 | if (!(req->flags & REQ_F_CQE_SKIP)) |
1682 | ctx->submit_state.flush_cqes = true; | |
fff4e40e PB |
1683 | wq_list_add_tail(&req->comp_list, &state->compl_reqs); |
1684 | } | |
1685 | ||
00169246 | 1686 | static void io_queue_async_work(struct io_kiocb *req, bool *dont_use) |
561fb04a | 1687 | { |
a197f664 | 1688 | struct io_ring_ctx *ctx = req->ctx; |
cbdcb435 | 1689 | struct io_kiocb *link = io_prep_linked_timeout(req); |
5aa75ed5 | 1690 | struct io_uring_task *tctx = req->task->io_uring; |
561fb04a | 1691 | |
3bfe6106 JA |
1692 | BUG_ON(!tctx); |
1693 | BUG_ON(!tctx->io_wq); | |
561fb04a | 1694 | |
cbdcb435 PB |
1695 | /* init ->work of the whole link before punting */ |
1696 | io_prep_async_link(req); | |
991468dc JA |
1697 | |
1698 | /* | |
1699 | * Not expected to happen, but if we do have a bug where this _can_ | |
1700 | * happen, catch it here and ensure the request is marked as | |
1701 | * canceled. That will make io-wq go through the usual work cancel | |
1702 | * procedure rather than attempt to run this request (or create a new | |
1703 | * worker for it). | |
1704 | */ | |
1705 | if (WARN_ON_ONCE(!same_thread_group(req->task, current))) | |
1706 | req->work.flags |= IO_WQ_WORK_CANCEL; | |
1707 | ||
502c87d6 SR |
1708 | trace_io_uring_queue_async_work(ctx, req, req->user_data, req->opcode, req->flags, |
1709 | &req->work, io_wq_is_hashed(&req->work)); | |
ebf93667 | 1710 | io_wq_enqueue(tctx->io_wq, &req->work); |
7271ef3a JA |
1711 | if (link) |
1712 | io_queue_linked_timeout(link); | |
cbdcb435 PB |
1713 | } |
1714 | ||
1ee4160c | 1715 | static void io_kill_timeout(struct io_kiocb *req, int status) |
8c855885 | 1716 | __must_hold(&req->ctx->completion_lock) |
89850fce | 1717 | __must_hold(&req->ctx->timeout_lock) |
5262f567 | 1718 | { |
e8c2bc1f | 1719 | struct io_timeout_data *io = req->async_data; |
5262f567 | 1720 | |
fd9c7bc5 | 1721 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
2ae2eb9d PB |
1722 | if (status) |
1723 | req_set_fail(req); | |
01cec8c1 PB |
1724 | atomic_set(&req->ctx->cq_timeouts, |
1725 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
135fcde8 | 1726 | list_del_init(&req->timeout.list); |
913a571a | 1727 | io_fill_cqe_req(req, status, 0); |
91c2f697 | 1728 | io_put_req_deferred(req); |
5262f567 JA |
1729 | } |
1730 | } | |
1731 | ||
c072481d | 1732 | static __cold void io_queue_deferred(struct io_ring_ctx *ctx) |
de0617e4 | 1733 | { |
441b8a78 | 1734 | while (!list_empty(&ctx->defer_list)) { |
27dc8338 PB |
1735 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
1736 | struct io_defer_entry, list); | |
de0617e4 | 1737 | |
9cf7c104 | 1738 | if (req_need_defer(de->req, de->seq)) |
04518945 | 1739 | break; |
27dc8338 | 1740 | list_del_init(&de->list); |
907d1df3 | 1741 | io_req_task_queue(de->req); |
27dc8338 | 1742 | kfree(de); |
441b8a78 | 1743 | } |
04518945 PB |
1744 | } |
1745 | ||
c072481d | 1746 | static __cold void io_flush_timeouts(struct io_ring_ctx *ctx) |
89850fce | 1747 | __must_hold(&ctx->completion_lock) |
de0617e4 | 1748 | { |
441b8a78 | 1749 | u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
e677edbc | 1750 | struct io_kiocb *req, *tmp; |
f010505b | 1751 | |
79ebeaee | 1752 | spin_lock_irq(&ctx->timeout_lock); |
e677edbc | 1753 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
f010505b | 1754 | u32 events_needed, events_got; |
de0617e4 | 1755 | |
8eb7e2d0 | 1756 | if (io_is_timeout_noseq(req)) |
360428f8 | 1757 | break; |
f010505b MDG |
1758 | |
1759 | /* | |
1760 | * Since seq can easily wrap around over time, subtract | |
1761 | * the last seq at which timeouts were flushed before comparing. | |
1762 | * Assuming not more than 2^31-1 events have happened since, | |
1763 | * these subtractions won't have wrapped, so we can check if | |
1764 | * target is in [last_seq, current_seq] by comparing the two. | |
1765 | */ | |
1766 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; | |
1767 | events_got = seq - ctx->cq_last_tm_flush; | |
1768 | if (events_got < events_needed) | |
360428f8 | 1769 | break; |
bfe68a22 | 1770 | |
1ee4160c | 1771 | io_kill_timeout(req, 0); |
f18ee4cf | 1772 | } |
f010505b | 1773 | ctx->cq_last_tm_flush = seq; |
79ebeaee | 1774 | spin_unlock_irq(&ctx->timeout_lock); |
360428f8 | 1775 | } |
5262f567 | 1776 | |
9333f6b4 PB |
1777 | static inline void io_commit_cqring(struct io_ring_ctx *ctx) |
1778 | { | |
1779 | /* order cqe stores with ring update */ | |
1780 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); | |
1781 | } | |
1782 | ||
9aa8dfde | 1783 | static void __io_commit_cqring_flush(struct io_ring_ctx *ctx) |
360428f8 | 1784 | { |
9aa8dfde PB |
1785 | if (ctx->off_timeout_used || ctx->drain_active) { |
1786 | spin_lock(&ctx->completion_lock); | |
1787 | if (ctx->off_timeout_used) | |
1788 | io_flush_timeouts(ctx); | |
1789 | if (ctx->drain_active) | |
1790 | io_queue_deferred(ctx); | |
1791 | io_commit_cqring(ctx); | |
1792 | spin_unlock(&ctx->completion_lock); | |
1793 | } | |
1794 | if (ctx->has_evfd) | |
1795 | io_eventfd_signal(ctx); | |
de0617e4 JA |
1796 | } |
1797 | ||
90554200 JA |
1798 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
1799 | { | |
1800 | struct io_rings *r = ctx->rings; | |
1801 | ||
a566c556 | 1802 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries; |
90554200 JA |
1803 | } |
1804 | ||
888aae2e PB |
1805 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
1806 | { | |
1807 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); | |
1808 | } | |
1809 | ||
d068b506 | 1810 | static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx) |
2b188cc1 | 1811 | { |
75b28aff | 1812 | struct io_rings *rings = ctx->rings; |
ea5ab3b5 | 1813 | unsigned tail, mask = ctx->cq_entries - 1; |
2b188cc1 | 1814 | |
115e12e5 SB |
1815 | /* |
1816 | * writes to the cq entry need to come after reading head; the | |
1817 | * control dependency is enough as we're using WRITE_ONCE to | |
1818 | * fill the cq entry | |
1819 | */ | |
a566c556 | 1820 | if (__io_cqring_events(ctx) == ctx->cq_entries) |
2b188cc1 JA |
1821 | return NULL; |
1822 | ||
888aae2e | 1823 | tail = ctx->cached_cq_tail++; |
ea5ab3b5 | 1824 | return &rings->cqes[tail & mask]; |
2b188cc1 JA |
1825 | } |
1826 | ||
77bc59b4 | 1827 | static void io_eventfd_signal(struct io_ring_ctx *ctx) |
f2842ab5 | 1828 | { |
77bc59b4 UA |
1829 | struct io_ev_fd *ev_fd; |
1830 | ||
77bc59b4 UA |
1831 | rcu_read_lock(); |
1832 | /* | |
1833 | * rcu_dereference ctx->io_ev_fd once and use it for both for checking | |
1834 | * and eventfd_signal | |
1835 | */ | |
1836 | ev_fd = rcu_dereference(ctx->io_ev_fd); | |
1837 | ||
1838 | /* | |
1839 | * Check again if ev_fd exists incase an io_eventfd_unregister call | |
1840 | * completed between the NULL check of ctx->io_ev_fd at the start of | |
1841 | * the function and rcu_read_lock. | |
1842 | */ | |
1843 | if (unlikely(!ev_fd)) | |
1844 | goto out; | |
7e55a19c | 1845 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
77bc59b4 UA |
1846 | goto out; |
1847 | ||
c75312dd | 1848 | if (!ev_fd->eventfd_async || io_wq_current_is_worker()) |
77bc59b4 | 1849 | eventfd_signal(ev_fd->cq_ev_fd, 1); |
77bc59b4 UA |
1850 | out: |
1851 | rcu_read_unlock(); | |
f2842ab5 JA |
1852 | } |
1853 | ||
9aa8dfde PB |
1854 | static inline void io_cqring_wake(struct io_ring_ctx *ctx) |
1855 | { | |
1856 | /* | |
1857 | * wake_up_all() may seem excessive, but io_wake_function() and | |
1858 | * io_should_wake() handle the termination of the loop and only | |
1859 | * wake as many waiters as we need to. | |
1860 | */ | |
1861 | if (wq_has_sleeper(&ctx->cq_wait)) | |
1862 | wake_up_all(&ctx->cq_wait); | |
1863 | } | |
1864 | ||
2c5d763c JA |
1865 | /* |
1866 | * This should only get called when at least one event has been posted. | |
1867 | * Some applications rely on the eventfd notification count only changing | |
1868 | * IFF a new CQE has been added to the CQ ring. There's no depedency on | |
1869 | * 1:1 relationship between how many times this function is called (and | |
1870 | * hence the eventfd count) and number of CQEs posted to the CQ ring. | |
1871 | */ | |
66fc25ca | 1872 | static inline void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
1d7bb1d5 | 1873 | { |
9aa8dfde PB |
1874 | if (unlikely(ctx->off_timeout_used || ctx->drain_active || |
1875 | ctx->has_evfd)) | |
9333f6b4 PB |
1876 | __io_commit_cqring_flush(ctx); |
1877 | ||
9aa8dfde | 1878 | io_cqring_wake(ctx); |
1d7bb1d5 JA |
1879 | } |
1880 | ||
80c18e4a PB |
1881 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
1882 | { | |
9aa8dfde PB |
1883 | if (unlikely(ctx->off_timeout_used || ctx->drain_active || |
1884 | ctx->has_evfd)) | |
9333f6b4 PB |
1885 | __io_commit_cqring_flush(ctx); |
1886 | ||
9aa8dfde PB |
1887 | if (ctx->flags & IORING_SETUP_SQPOLL) |
1888 | io_cqring_wake(ctx); | |
80c18e4a PB |
1889 | } |
1890 | ||
c4a2ed72 | 1891 | /* Returns true if there are no backlogged entries after the flush */ |
6c2450ae | 1892 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force) |
1d7bb1d5 | 1893 | { |
b18032bb | 1894 | bool all_flushed, posted; |
1d7bb1d5 | 1895 | |
a566c556 | 1896 | if (!force && __io_cqring_events(ctx) == ctx->cq_entries) |
e23de15f | 1897 | return false; |
1d7bb1d5 | 1898 | |
b18032bb | 1899 | posted = false; |
79ebeaee | 1900 | spin_lock(&ctx->completion_lock); |
6c2450ae | 1901 | while (!list_empty(&ctx->cq_overflow_list)) { |
d068b506 | 1902 | struct io_uring_cqe *cqe = io_get_cqe(ctx); |
6c2450ae | 1903 | struct io_overflow_cqe *ocqe; |
e6c8aa9a | 1904 | |
1d7bb1d5 JA |
1905 | if (!cqe && !force) |
1906 | break; | |
6c2450ae PB |
1907 | ocqe = list_first_entry(&ctx->cq_overflow_list, |
1908 | struct io_overflow_cqe, list); | |
1909 | if (cqe) | |
1910 | memcpy(cqe, &ocqe->cqe, sizeof(*cqe)); | |
1911 | else | |
8f6ed49a PB |
1912 | io_account_cq_overflow(ctx); |
1913 | ||
b18032bb | 1914 | posted = true; |
6c2450ae PB |
1915 | list_del(&ocqe->list); |
1916 | kfree(ocqe); | |
1d7bb1d5 JA |
1917 | } |
1918 | ||
09e88404 PB |
1919 | all_flushed = list_empty(&ctx->cq_overflow_list); |
1920 | if (all_flushed) { | |
5ed7a37d | 1921 | clear_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
1922 | WRITE_ONCE(ctx->rings->sq_flags, |
1923 | ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW); | |
09e88404 | 1924 | } |
46930143 | 1925 | |
60053be8 | 1926 | io_commit_cqring(ctx); |
79ebeaee | 1927 | spin_unlock(&ctx->completion_lock); |
b18032bb JA |
1928 | if (posted) |
1929 | io_cqring_ev_posted(ctx); | |
09e88404 | 1930 | return all_flushed; |
1d7bb1d5 JA |
1931 | } |
1932 | ||
90f67366 | 1933 | static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx) |
6c503150 | 1934 | { |
ca0a2651 JA |
1935 | bool ret = true; |
1936 | ||
5ed7a37d | 1937 | if (test_bit(0, &ctx->check_cq_overflow)) { |
6c503150 PB |
1938 | /* iopoll syncs against uring_lock, not completion_lock */ |
1939 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1940 | mutex_lock(&ctx->uring_lock); | |
90f67366 | 1941 | ret = __io_cqring_overflow_flush(ctx, false); |
6c503150 PB |
1942 | if (ctx->flags & IORING_SETUP_IOPOLL) |
1943 | mutex_unlock(&ctx->uring_lock); | |
1944 | } | |
ca0a2651 JA |
1945 | |
1946 | return ret; | |
6c503150 PB |
1947 | } |
1948 | ||
6a290a14 PB |
1949 | /* must to be called somewhat shortly after putting a request */ |
1950 | static inline void io_put_task(struct task_struct *task, int nr) | |
1951 | { | |
1952 | struct io_uring_task *tctx = task->io_uring; | |
1953 | ||
e98e49b2 PB |
1954 | if (likely(task == current)) { |
1955 | tctx->cached_refs += nr; | |
1956 | } else { | |
1957 | percpu_counter_sub(&tctx->inflight, nr); | |
1958 | if (unlikely(atomic_read(&tctx->in_idle))) | |
1959 | wake_up(&tctx->wait); | |
1960 | put_task_struct_many(task, nr); | |
1961 | } | |
6a290a14 PB |
1962 | } |
1963 | ||
9a10867a PB |
1964 | static void io_task_refs_refill(struct io_uring_task *tctx) |
1965 | { | |
1966 | unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR; | |
1967 | ||
1968 | percpu_counter_add(&tctx->inflight, refill); | |
1969 | refcount_add(refill, ¤t->usage); | |
1970 | tctx->cached_refs += refill; | |
1971 | } | |
1972 | ||
1973 | static inline void io_get_task_refs(int nr) | |
1974 | { | |
1975 | struct io_uring_task *tctx = current->io_uring; | |
1976 | ||
1977 | tctx->cached_refs -= nr; | |
1978 | if (unlikely(tctx->cached_refs < 0)) | |
1979 | io_task_refs_refill(tctx); | |
1980 | } | |
1981 | ||
3cc7fdb9 PB |
1982 | static __cold void io_uring_drop_tctx_refs(struct task_struct *task) |
1983 | { | |
1984 | struct io_uring_task *tctx = task->io_uring; | |
1985 | unsigned int refs = tctx->cached_refs; | |
1986 | ||
1987 | if (refs) { | |
1988 | tctx->cached_refs = 0; | |
1989 | percpu_counter_sub(&tctx->inflight, refs); | |
1990 | put_task_struct_many(task, refs); | |
1991 | } | |
1992 | } | |
1993 | ||
d4d19c19 | 1994 | static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data, |
54daa9b2 | 1995 | s32 res, u32 cflags) |
2b188cc1 | 1996 | { |
cce4b8b0 | 1997 | struct io_overflow_cqe *ocqe; |
2b188cc1 | 1998 | |
cce4b8b0 PB |
1999 | ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT); |
2000 | if (!ocqe) { | |
2001 | /* | |
2002 | * If we're in ring overflow flush mode, or in task cancel mode, | |
2003 | * or cannot allocate an overflow entry, then we need to drop it | |
2004 | * on the floor. | |
2005 | */ | |
8f6ed49a | 2006 | io_account_cq_overflow(ctx); |
cce4b8b0 | 2007 | return false; |
2b188cc1 | 2008 | } |
cce4b8b0 | 2009 | if (list_empty(&ctx->cq_overflow_list)) { |
5ed7a37d | 2010 | set_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
2011 | WRITE_ONCE(ctx->rings->sq_flags, |
2012 | ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW); | |
2013 | ||
cce4b8b0 | 2014 | } |
d4d19c19 | 2015 | ocqe->cqe.user_data = user_data; |
cce4b8b0 PB |
2016 | ocqe->cqe.res = res; |
2017 | ocqe->cqe.flags = cflags; | |
2018 | list_add_tail(&ocqe->list, &ctx->cq_overflow_list); | |
2019 | return true; | |
2b188cc1 JA |
2020 | } |
2021 | ||
ae4da189 | 2022 | static inline bool __io_fill_cqe(struct io_ring_ctx *ctx, u64 user_data, |
913a571a | 2023 | s32 res, u32 cflags) |
2b188cc1 JA |
2024 | { |
2025 | struct io_uring_cqe *cqe; | |
2026 | ||
2027 | /* | |
2028 | * If we can't get a cq entry, userspace overflowed the | |
2029 | * submission (by quite a lot). Increment the overflow count in | |
2030 | * the ring. | |
2031 | */ | |
d068b506 | 2032 | cqe = io_get_cqe(ctx); |
1d7bb1d5 | 2033 | if (likely(cqe)) { |
d4d19c19 | 2034 | WRITE_ONCE(cqe->user_data, user_data); |
2b188cc1 | 2035 | WRITE_ONCE(cqe->res, res); |
bcda7baa | 2036 | WRITE_ONCE(cqe->flags, cflags); |
8d13326e | 2037 | return true; |
2b188cc1 | 2038 | } |
d4d19c19 | 2039 | return io_cqring_event_overflow(ctx, user_data, res, cflags); |
2b188cc1 JA |
2040 | } |
2041 | ||
ae4da189 | 2042 | static inline bool __io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags) |
d5ec1dfa | 2043 | { |
502c87d6 | 2044 | trace_io_uring_complete(req->ctx, req, req->user_data, res, cflags); |
ae4da189 | 2045 | return __io_fill_cqe(req->ctx, req->user_data, res, cflags); |
d5ec1dfa SR |
2046 | } |
2047 | ||
913a571a | 2048 | static noinline void io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags) |
bcda7baa | 2049 | { |
04c76b41 | 2050 | if (!(req->flags & REQ_F_CQE_SKIP)) |
ae4da189 | 2051 | __io_fill_cqe_req(req, res, cflags); |
bcda7baa JA |
2052 | } |
2053 | ||
913a571a PB |
2054 | static noinline bool io_fill_cqe_aux(struct io_ring_ctx *ctx, u64 user_data, |
2055 | s32 res, u32 cflags) | |
bcda7baa | 2056 | { |
913a571a | 2057 | ctx->cq_extra++; |
502c87d6 | 2058 | trace_io_uring_complete(ctx, NULL, user_data, res, cflags); |
ae4da189 | 2059 | return __io_fill_cqe(ctx, user_data, res, cflags); |
bcda7baa JA |
2060 | } |
2061 | ||
a37fae8a HX |
2062 | static void __io_req_complete_post(struct io_kiocb *req, s32 res, |
2063 | u32 cflags) | |
2b188cc1 | 2064 | { |
78e19bbe | 2065 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 | 2066 | |
04c76b41 | 2067 | if (!(req->flags & REQ_F_CQE_SKIP)) |
ae4da189 | 2068 | __io_fill_cqe_req(req, res, cflags); |
c7dae4ba JA |
2069 | /* |
2070 | * If we're the last reference to this request, add to our locked | |
2071 | * free_list cache. | |
2072 | */ | |
de9b4cca | 2073 | if (req_ref_put_and_test(req)) { |
7a612350 | 2074 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
0756a869 | 2075 | if (req->flags & IO_DISARM_MASK) |
7a612350 PB |
2076 | io_disarm_next(req); |
2077 | if (req->link) { | |
2078 | io_req_task_queue(req->link); | |
2079 | req->link = NULL; | |
2080 | } | |
2081 | } | |
ab409402 | 2082 | io_req_put_rsrc(req, ctx); |
8197b053 PB |
2083 | /* |
2084 | * Selected buffer deallocation in io_clean_op() assumes that | |
2085 | * we don't hold ->completion_lock. Clean them here to avoid | |
2086 | * deadlocks. | |
2087 | */ | |
2088 | io_put_kbuf_comp(req); | |
c7dae4ba JA |
2089 | io_dismantle_req(req); |
2090 | io_put_task(req->task, 1); | |
c2b6c6bc | 2091 | wq_list_add_head(&req->comp_list, &ctx->locked_free_list); |
d0acdee2 | 2092 | ctx->locked_free_nr++; |
180f829f | 2093 | } |
a37fae8a HX |
2094 | } |
2095 | ||
2096 | static void io_req_complete_post(struct io_kiocb *req, s32 res, | |
2097 | u32 cflags) | |
2098 | { | |
2099 | struct io_ring_ctx *ctx = req->ctx; | |
2100 | ||
2101 | spin_lock(&ctx->completion_lock); | |
2102 | __io_req_complete_post(req, res, cflags); | |
7a612350 | 2103 | io_commit_cqring(ctx); |
79ebeaee | 2104 | spin_unlock(&ctx->completion_lock); |
a3f34907 | 2105 | io_cqring_ev_posted(ctx); |
4e3d9ff9 JA |
2106 | } |
2107 | ||
54daa9b2 PB |
2108 | static inline void io_req_complete_state(struct io_kiocb *req, s32 res, |
2109 | u32 cflags) | |
229a7b63 | 2110 | { |
a38d68db | 2111 | req->result = res; |
d17e56eb | 2112 | req->cflags = cflags; |
e342c807 | 2113 | req->flags |= REQ_F_COMPLETE_INLINE; |
e1e16097 JA |
2114 | } |
2115 | ||
889fca73 | 2116 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
54daa9b2 | 2117 | s32 res, u32 cflags) |
bcda7baa | 2118 | { |
889fca73 PB |
2119 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
2120 | io_req_complete_state(req, res, cflags); | |
a38d68db | 2121 | else |
c7dae4ba | 2122 | io_req_complete_post(req, res, cflags); |
bcda7baa JA |
2123 | } |
2124 | ||
54daa9b2 | 2125 | static inline void io_req_complete(struct io_kiocb *req, s32 res) |
0ddf92e8 | 2126 | { |
889fca73 | 2127 | __io_req_complete(req, 0, res, 0); |
0ddf92e8 JA |
2128 | } |
2129 | ||
54daa9b2 | 2130 | static void io_req_complete_failed(struct io_kiocb *req, s32 res) |
f41db273 | 2131 | { |
93d2bcd2 | 2132 | req_set_fail(req); |
ab0ac095 | 2133 | io_req_complete_post(req, res, io_put_kbuf(req, IO_URING_F_UNLOCKED)); |
f41db273 PB |
2134 | } |
2135 | ||
c6d3d9cb PB |
2136 | static void io_req_complete_fail_submit(struct io_kiocb *req) |
2137 | { | |
2138 | /* | |
2139 | * We don't submit, fail them all, for that replace hardlinks with | |
2140 | * normal links. Extra REQ_F_LINK is tolerated. | |
2141 | */ | |
2142 | req->flags &= ~REQ_F_HARDLINK; | |
2143 | req->flags |= REQ_F_LINK; | |
2144 | io_req_complete_failed(req, req->result); | |
2145 | } | |
2146 | ||
864ea921 PB |
2147 | /* |
2148 | * Don't initialise the fields below on every allocation, but do that in | |
2149 | * advance and keep them valid across allocations. | |
2150 | */ | |
2151 | static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx) | |
2152 | { | |
2153 | req->ctx = ctx; | |
2154 | req->link = NULL; | |
2155 | req->async_data = NULL; | |
2156 | /* not necessary, but safer to zero */ | |
2157 | req->result = 0; | |
2158 | } | |
2159 | ||
dac7a098 | 2160 | static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx, |
cd0ca2e0 | 2161 | struct io_submit_state *state) |
dac7a098 | 2162 | { |
79ebeaee | 2163 | spin_lock(&ctx->completion_lock); |
c2b6c6bc | 2164 | wq_list_splice(&ctx->locked_free_list, &state->free_list); |
d0acdee2 | 2165 | ctx->locked_free_nr = 0; |
79ebeaee | 2166 | spin_unlock(&ctx->completion_lock); |
dac7a098 PB |
2167 | } |
2168 | ||
dd78f492 | 2169 | /* Returns true IFF there are requests in the cache */ |
c7dae4ba | 2170 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
0ddf92e8 | 2171 | { |
c7dae4ba | 2172 | struct io_submit_state *state = &ctx->submit_state; |
0ddf92e8 | 2173 | |
c7dae4ba JA |
2174 | /* |
2175 | * If we have more than a batch's worth of requests in our IRQ side | |
2176 | * locked cache, grab the lock and move them over to our submission | |
2177 | * side cache. | |
2178 | */ | |
d0acdee2 | 2179 | if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH) |
cd0ca2e0 | 2180 | io_flush_cached_locked_reqs(ctx, state); |
c2b6c6bc | 2181 | return !!state->free_list.next; |
0ddf92e8 JA |
2182 | } |
2183 | ||
5d5901a3 PB |
2184 | /* |
2185 | * A request might get retired back into the request caches even before opcode | |
2186 | * handlers and io_issue_sqe() are done with it, e.g. inline completion path. | |
2187 | * Because of that, io_alloc_req() should be called only under ->uring_lock | |
2188 | * and with extra caution to not get a request that is still worked on. | |
2189 | */ | |
c072481d | 2190 | static __cold bool __io_alloc_req_refill(struct io_ring_ctx *ctx) |
5d5901a3 | 2191 | __must_hold(&ctx->uring_lock) |
2b188cc1 | 2192 | { |
e5d1bc0a | 2193 | struct io_submit_state *state = &ctx->submit_state; |
864ea921 | 2194 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
3ab665b7 PB |
2195 | void *reqs[IO_REQ_ALLOC_BATCH]; |
2196 | struct io_kiocb *req; | |
864ea921 | 2197 | int ret, i; |
e5d1bc0a | 2198 | |
c2b6c6bc | 2199 | if (likely(state->free_list.next || io_flush_cached_reqs(ctx))) |
a33ae9ce | 2200 | return true; |
e5d1bc0a | 2201 | |
3ab665b7 | 2202 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, ARRAY_SIZE(reqs), reqs); |
fd6fab2c | 2203 | |
864ea921 PB |
2204 | /* |
2205 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
2206 | * retry single alloc to be on the safe side. | |
2207 | */ | |
2208 | if (unlikely(ret <= 0)) { | |
3ab665b7 PB |
2209 | reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
2210 | if (!reqs[0]) | |
a33ae9ce | 2211 | return false; |
864ea921 | 2212 | ret = 1; |
2b188cc1 | 2213 | } |
864ea921 | 2214 | |
37f0e767 | 2215 | percpu_ref_get_many(&ctx->refs, ret); |
3ab665b7 PB |
2216 | for (i = 0; i < ret; i++) { |
2217 | req = reqs[i]; | |
2218 | ||
2219 | io_preinit_req(req, ctx); | |
c2b6c6bc | 2220 | wq_stack_add_head(&req->comp_list, &state->free_list); |
3ab665b7 | 2221 | } |
a33ae9ce PB |
2222 | return true; |
2223 | } | |
2224 | ||
2225 | static inline bool io_alloc_req_refill(struct io_ring_ctx *ctx) | |
2226 | { | |
2227 | if (unlikely(!ctx->submit_state.free_list.next)) | |
2228 | return __io_alloc_req_refill(ctx); | |
2229 | return true; | |
2230 | } | |
2231 | ||
2232 | static inline struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) | |
2233 | { | |
2234 | struct io_wq_work_node *node; | |
2235 | ||
2236 | node = wq_stack_extract(&ctx->submit_state.free_list); | |
c2b6c6bc | 2237 | return container_of(node, struct io_kiocb, comp_list); |
2b188cc1 JA |
2238 | } |
2239 | ||
e1d767f0 | 2240 | static inline void io_put_file(struct file *file) |
8da11c19 | 2241 | { |
e1d767f0 | 2242 | if (file) |
8da11c19 PB |
2243 | fput(file); |
2244 | } | |
2245 | ||
6b639522 | 2246 | static inline void io_dismantle_req(struct io_kiocb *req) |
2b188cc1 | 2247 | { |
094bae49 | 2248 | unsigned int flags = req->flags; |
929a3af9 | 2249 | |
867f8fa5 | 2250 | if (unlikely(flags & IO_REQ_CLEAN_FLAGS)) |
3a0a6902 | 2251 | io_clean_op(req); |
e1d767f0 PB |
2252 | if (!(flags & REQ_F_FIXED_FILE)) |
2253 | io_put_file(req->file); | |
e65ef56d JA |
2254 | } |
2255 | ||
c072481d | 2256 | static __cold void __io_free_req(struct io_kiocb *req) |
c6ca97b3 | 2257 | { |
51a4cc11 | 2258 | struct io_ring_ctx *ctx = req->ctx; |
c6ca97b3 | 2259 | |
ab409402 | 2260 | io_req_put_rsrc(req, ctx); |
216578e5 | 2261 | io_dismantle_req(req); |
7c660731 | 2262 | io_put_task(req->task, 1); |
c6ca97b3 | 2263 | |
79ebeaee | 2264 | spin_lock(&ctx->completion_lock); |
c2b6c6bc | 2265 | wq_list_add_head(&req->comp_list, &ctx->locked_free_list); |
c34b025f | 2266 | ctx->locked_free_nr++; |
79ebeaee | 2267 | spin_unlock(&ctx->completion_lock); |
e65ef56d JA |
2268 | } |
2269 | ||
f2f87370 PB |
2270 | static inline void io_remove_next_linked(struct io_kiocb *req) |
2271 | { | |
2272 | struct io_kiocb *nxt = req->link; | |
2273 | ||
2274 | req->link = nxt->link; | |
2275 | nxt->link = NULL; | |
2276 | } | |
2277 | ||
33cc89a9 PB |
2278 | static bool io_kill_linked_timeout(struct io_kiocb *req) |
2279 | __must_hold(&req->ctx->completion_lock) | |
89b263f6 | 2280 | __must_hold(&req->ctx->timeout_lock) |
2665abfd | 2281 | { |
33cc89a9 | 2282 | struct io_kiocb *link = req->link; |
f2f87370 | 2283 | |
b97e736a | 2284 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
c9abd7ad | 2285 | struct io_timeout_data *io = link->async_data; |
7c86ffee | 2286 | |
f2f87370 | 2287 | io_remove_next_linked(req); |
90cd7e42 | 2288 | link->timeout.head = NULL; |
fd9c7bc5 | 2289 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
ef9dd637 | 2290 | list_del(&link->timeout.list); |
04c76b41 | 2291 | /* leave REQ_F_CQE_SKIP to io_fill_cqe_req */ |
913a571a | 2292 | io_fill_cqe_req(link, -ECANCELED, 0); |
91c2f697 | 2293 | io_put_req_deferred(link); |
d4729fbd | 2294 | return true; |
c9abd7ad PB |
2295 | } |
2296 | } | |
d4729fbd | 2297 | return false; |
7c86ffee PB |
2298 | } |
2299 | ||
d148ca4b | 2300 | static void io_fail_links(struct io_kiocb *req) |
33cc89a9 | 2301 | __must_hold(&req->ctx->completion_lock) |
9e645e11 | 2302 | { |
33cc89a9 | 2303 | struct io_kiocb *nxt, *link = req->link; |
04c76b41 | 2304 | bool ignore_cqes = req->flags & REQ_F_SKIP_LINK_CQES; |
9e645e11 | 2305 | |
f2f87370 | 2306 | req->link = NULL; |
f2f87370 | 2307 | while (link) { |
a8295b98 HX |
2308 | long res = -ECANCELED; |
2309 | ||
2310 | if (link->flags & REQ_F_FAIL) | |
2311 | res = link->result; | |
2312 | ||
f2f87370 PB |
2313 | nxt = link->link; |
2314 | link->link = NULL; | |
2665abfd | 2315 | |
502c87d6 SR |
2316 | trace_io_uring_fail_link(req->ctx, req, req->user_data, |
2317 | req->opcode, link); | |
2318 | ||
04c76b41 PB |
2319 | if (!ignore_cqes) { |
2320 | link->flags &= ~REQ_F_CQE_SKIP; | |
2321 | io_fill_cqe_req(link, res, 0); | |
2322 | } | |
91c2f697 | 2323 | io_put_req_deferred(link); |
f2f87370 | 2324 | link = nxt; |
9e645e11 | 2325 | } |
33cc89a9 | 2326 | } |
9e645e11 | 2327 | |
33cc89a9 PB |
2328 | static bool io_disarm_next(struct io_kiocb *req) |
2329 | __must_hold(&req->ctx->completion_lock) | |
2330 | { | |
2331 | bool posted = false; | |
2332 | ||
0756a869 PB |
2333 | if (req->flags & REQ_F_ARM_LTIMEOUT) { |
2334 | struct io_kiocb *link = req->link; | |
2335 | ||
906c6caa | 2336 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
0756a869 PB |
2337 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
2338 | io_remove_next_linked(req); | |
04c76b41 | 2339 | /* leave REQ_F_CQE_SKIP to io_fill_cqe_req */ |
913a571a | 2340 | io_fill_cqe_req(link, -ECANCELED, 0); |
0756a869 PB |
2341 | io_put_req_deferred(link); |
2342 | posted = true; | |
2343 | } | |
2344 | } else if (req->flags & REQ_F_LINK_TIMEOUT) { | |
89b263f6 JA |
2345 | struct io_ring_ctx *ctx = req->ctx; |
2346 | ||
2347 | spin_lock_irq(&ctx->timeout_lock); | |
33cc89a9 | 2348 | posted = io_kill_linked_timeout(req); |
89b263f6 JA |
2349 | spin_unlock_irq(&ctx->timeout_lock); |
2350 | } | |
93d2bcd2 | 2351 | if (unlikely((req->flags & REQ_F_FAIL) && |
e4335ed3 | 2352 | !(req->flags & REQ_F_HARDLINK))) { |
33cc89a9 PB |
2353 | posted |= (req->link != NULL); |
2354 | io_fail_links(req); | |
2355 | } | |
2356 | return posted; | |
9e645e11 JA |
2357 | } |
2358 | ||
d81499bf PB |
2359 | static void __io_req_find_next_prep(struct io_kiocb *req) |
2360 | { | |
2361 | struct io_ring_ctx *ctx = req->ctx; | |
2362 | bool posted; | |
2363 | ||
2364 | spin_lock(&ctx->completion_lock); | |
2365 | posted = io_disarm_next(req); | |
60053be8 | 2366 | io_commit_cqring(ctx); |
d81499bf PB |
2367 | spin_unlock(&ctx->completion_lock); |
2368 | if (posted) | |
2369 | io_cqring_ev_posted(ctx); | |
2370 | } | |
2371 | ||
2372 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) | |
c69f8dbe | 2373 | { |
33cc89a9 | 2374 | struct io_kiocb *nxt; |
944e58bf | 2375 | |
9e645e11 JA |
2376 | /* |
2377 | * If LINK is set, we have dependent requests in this chain. If we | |
2378 | * didn't fail this request, queue the first one up, moving any other | |
2379 | * dependencies to the next request. In case of failure, fail the rest | |
2380 | * of the chain. | |
2381 | */ | |
d81499bf PB |
2382 | if (unlikely(req->flags & IO_DISARM_MASK)) |
2383 | __io_req_find_next_prep(req); | |
33cc89a9 PB |
2384 | nxt = req->link; |
2385 | req->link = NULL; | |
2386 | return nxt; | |
4d7dd462 | 2387 | } |
9e645e11 | 2388 | |
f237c30a | 2389 | static void ctx_flush_and_put(struct io_ring_ctx *ctx, bool *locked) |
2c32395d PB |
2390 | { |
2391 | if (!ctx) | |
2392 | return; | |
f237c30a | 2393 | if (*locked) { |
c450178d | 2394 | io_submit_flush_completions(ctx); |
2c32395d | 2395 | mutex_unlock(&ctx->uring_lock); |
f237c30a | 2396 | *locked = false; |
2c32395d PB |
2397 | } |
2398 | percpu_ref_put(&ctx->refs); | |
2399 | } | |
2400 | ||
f28c240e HX |
2401 | static inline void ctx_commit_and_unlock(struct io_ring_ctx *ctx) |
2402 | { | |
2403 | io_commit_cqring(ctx); | |
2404 | spin_unlock(&ctx->completion_lock); | |
2405 | io_cqring_ev_posted(ctx); | |
2406 | } | |
2407 | ||
2408 | static void handle_prev_tw_list(struct io_wq_work_node *node, | |
2409 | struct io_ring_ctx **ctx, bool *uring_locked) | |
2410 | { | |
2411 | if (*ctx && !*uring_locked) | |
2412 | spin_lock(&(*ctx)->completion_lock); | |
2413 | ||
2414 | do { | |
2415 | struct io_wq_work_node *next = node->next; | |
2416 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2417 | io_task_work.node); | |
2418 | ||
34d2bfe7 JA |
2419 | prefetch(container_of(next, struct io_kiocb, io_task_work.node)); |
2420 | ||
f28c240e HX |
2421 | if (req->ctx != *ctx) { |
2422 | if (unlikely(!*uring_locked && *ctx)) | |
2423 | ctx_commit_and_unlock(*ctx); | |
2424 | ||
2425 | ctx_flush_and_put(*ctx, uring_locked); | |
2426 | *ctx = req->ctx; | |
2427 | /* if not contended, grab and improve batching */ | |
2428 | *uring_locked = mutex_trylock(&(*ctx)->uring_lock); | |
2429 | percpu_ref_get(&(*ctx)->refs); | |
2430 | if (unlikely(!*uring_locked)) | |
2431 | spin_lock(&(*ctx)->completion_lock); | |
2432 | } | |
2433 | if (likely(*uring_locked)) | |
2434 | req->io_task_work.func(req, uring_locked); | |
2435 | else | |
cc3cec83 JA |
2436 | __io_req_complete_post(req, req->result, |
2437 | io_put_kbuf_comp(req)); | |
f28c240e HX |
2438 | node = next; |
2439 | } while (node); | |
2440 | ||
2441 | if (unlikely(!*uring_locked)) | |
2442 | ctx_commit_and_unlock(*ctx); | |
2443 | } | |
2444 | ||
2445 | static void handle_tw_list(struct io_wq_work_node *node, | |
2446 | struct io_ring_ctx **ctx, bool *locked) | |
9f8d032a HX |
2447 | { |
2448 | do { | |
2449 | struct io_wq_work_node *next = node->next; | |
2450 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2451 | io_task_work.node); | |
2452 | ||
34d2bfe7 JA |
2453 | prefetch(container_of(next, struct io_kiocb, io_task_work.node)); |
2454 | ||
9f8d032a HX |
2455 | if (req->ctx != *ctx) { |
2456 | ctx_flush_and_put(*ctx, locked); | |
2457 | *ctx = req->ctx; | |
2458 | /* if not contended, grab and improve batching */ | |
2459 | *locked = mutex_trylock(&(*ctx)->uring_lock); | |
2460 | percpu_ref_get(&(*ctx)->refs); | |
2461 | } | |
2462 | req->io_task_work.func(req, locked); | |
2463 | node = next; | |
2464 | } while (node); | |
2465 | } | |
2466 | ||
7cbf1722 | 2467 | static void tctx_task_work(struct callback_head *cb) |
c40f6379 | 2468 | { |
f28c240e | 2469 | bool uring_locked = false; |
ebd0df2e | 2470 | struct io_ring_ctx *ctx = NULL; |
3f18407d PB |
2471 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, |
2472 | task_work); | |
c40f6379 | 2473 | |
16f72070 | 2474 | while (1) { |
f28c240e | 2475 | struct io_wq_work_node *node1, *node2; |
3f18407d PB |
2476 | |
2477 | spin_lock_irq(&tctx->task_lock); | |
f28c240e HX |
2478 | node1 = tctx->prior_task_list.first; |
2479 | node2 = tctx->task_list.first; | |
3f18407d | 2480 | INIT_WQ_LIST(&tctx->task_list); |
f28c240e HX |
2481 | INIT_WQ_LIST(&tctx->prior_task_list); |
2482 | if (!node2 && !node1) | |
6294f368 | 2483 | tctx->task_running = false; |
3f18407d | 2484 | spin_unlock_irq(&tctx->task_lock); |
f28c240e | 2485 | if (!node2 && !node1) |
6294f368 | 2486 | break; |
3f18407d | 2487 | |
f28c240e HX |
2488 | if (node1) |
2489 | handle_prev_tw_list(node1, &ctx, &uring_locked); | |
f28c240e HX |
2490 | if (node2) |
2491 | handle_tw_list(node2, &ctx, &uring_locked); | |
7cbf1722 | 2492 | cond_resched(); |
68ca8fc0 PB |
2493 | |
2494 | if (!tctx->task_list.first && | |
2495 | !tctx->prior_task_list.first && uring_locked) | |
2496 | io_submit_flush_completions(ctx); | |
3f18407d | 2497 | } |
ebd0df2e | 2498 | |
f28c240e | 2499 | ctx_flush_and_put(ctx, &uring_locked); |
3cc7fdb9 PB |
2500 | |
2501 | /* relaxed read is enough as only the task itself sets ->in_idle */ | |
2502 | if (unlikely(atomic_read(&tctx->in_idle))) | |
2503 | io_uring_drop_tctx_refs(current); | |
7cbf1722 JA |
2504 | } |
2505 | ||
4813c377 | 2506 | static void io_req_task_work_add(struct io_kiocb *req, bool priority) |
7cbf1722 | 2507 | { |
c15b79de | 2508 | struct task_struct *tsk = req->task; |
7cbf1722 | 2509 | struct io_uring_task *tctx = tsk->io_uring; |
c15b79de | 2510 | enum task_work_notify_mode notify; |
e09ee510 | 2511 | struct io_wq_work_node *node; |
0b81e80c | 2512 | unsigned long flags; |
6294f368 | 2513 | bool running; |
7cbf1722 JA |
2514 | |
2515 | WARN_ON_ONCE(!tctx); | |
2516 | ||
d5361233 JA |
2517 | io_drop_inflight_file(req); |
2518 | ||
0b81e80c | 2519 | spin_lock_irqsave(&tctx->task_lock, flags); |
4813c377 HX |
2520 | if (priority) |
2521 | wq_list_add_tail(&req->io_task_work.node, &tctx->prior_task_list); | |
2522 | else | |
2523 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); | |
6294f368 PB |
2524 | running = tctx->task_running; |
2525 | if (!running) | |
2526 | tctx->task_running = true; | |
0b81e80c | 2527 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
2528 | |
2529 | /* task_work already pending, we're done */ | |
6294f368 | 2530 | if (running) |
e09ee510 | 2531 | return; |
7cbf1722 | 2532 | |
c15b79de PB |
2533 | /* |
2534 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For | |
2535 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're | |
2536 | * processing task_work. There's no reliable way to tell if TWA_RESUME | |
2537 | * will do the job. | |
2538 | */ | |
2539 | notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL; | |
d97ec623 PB |
2540 | if (likely(!task_work_add(tsk, &tctx->task_work, notify))) { |
2541 | if (notify == TWA_NONE) | |
2542 | wake_up_process(tsk); | |
e09ee510 | 2543 | return; |
c15b79de | 2544 | } |
2215bed9 | 2545 | |
0b81e80c | 2546 | spin_lock_irqsave(&tctx->task_lock, flags); |
6294f368 | 2547 | tctx->task_running = false; |
4813c377 | 2548 | node = wq_list_merge(&tctx->prior_task_list, &tctx->task_list); |
0b81e80c | 2549 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 | 2550 | |
e09ee510 PB |
2551 | while (node) { |
2552 | req = container_of(node, struct io_kiocb, io_task_work.node); | |
2553 | node = node->next; | |
2554 | if (llist_add(&req->io_task_work.fallback_node, | |
2555 | &req->ctx->fallback_llist)) | |
2556 | schedule_delayed_work(&req->ctx->fallback_work, 1); | |
2557 | } | |
eab30c4d PB |
2558 | } |
2559 | ||
f237c30a | 2560 | static void io_req_task_cancel(struct io_kiocb *req, bool *locked) |
c40f6379 | 2561 | { |
87ceb6a6 | 2562 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 | 2563 | |
b18a1a45 | 2564 | /* not needed for normal modes, but SQPOLL depends on it */ |
f237c30a | 2565 | io_tw_lock(ctx, locked); |
2593553a | 2566 | io_req_complete_failed(req, req->result); |
c40f6379 JA |
2567 | } |
2568 | ||
f237c30a | 2569 | static void io_req_task_submit(struct io_kiocb *req, bool *locked) |
c40f6379 JA |
2570 | { |
2571 | struct io_ring_ctx *ctx = req->ctx; | |
2572 | ||
f237c30a | 2573 | io_tw_lock(ctx, locked); |
316319e8 | 2574 | /* req->task == current here, checking PF_EXITING is safe */ |
af066f31 | 2575 | if (likely(!(req->task->flags & PF_EXITING))) |
c5eef2b9 | 2576 | __io_queue_sqe(req); |
81b6d05c | 2577 | else |
2593553a | 2578 | io_req_complete_failed(req, -EFAULT); |
c40f6379 JA |
2579 | } |
2580 | ||
2c4b8eb6 | 2581 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
c40f6379 | 2582 | { |
2c4b8eb6 | 2583 | req->result = ret; |
5b0a6acc | 2584 | req->io_task_work.func = io_req_task_cancel; |
4813c377 | 2585 | io_req_task_work_add(req, false); |
c40f6379 JA |
2586 | } |
2587 | ||
2c4b8eb6 | 2588 | static void io_req_task_queue(struct io_kiocb *req) |
a3df7698 | 2589 | { |
5b0a6acc | 2590 | req->io_task_work.func = io_req_task_submit; |
4813c377 | 2591 | io_req_task_work_add(req, false); |
a3df7698 PB |
2592 | } |
2593 | ||
773af691 JA |
2594 | static void io_req_task_queue_reissue(struct io_kiocb *req) |
2595 | { | |
2596 | req->io_task_work.func = io_queue_async_work; | |
4813c377 | 2597 | io_req_task_work_add(req, false); |
773af691 JA |
2598 | } |
2599 | ||
57859f4d | 2600 | static void io_queue_next(struct io_kiocb *req) |
c69f8dbe | 2601 | { |
57859f4d | 2602 | struct io_kiocb *nxt = io_req_find_next(req); |
944e58bf | 2603 | |
57859f4d PB |
2604 | if (nxt) |
2605 | io_req_task_queue(nxt); | |
c69f8dbe JL |
2606 | } |
2607 | ||
c3524383 | 2608 | static void io_free_req(struct io_kiocb *req) |
7a743e22 | 2609 | { |
c3524383 PB |
2610 | io_queue_next(req); |
2611 | __io_free_req(req); | |
2612 | } | |
8766dd51 | 2613 | |
f237c30a PB |
2614 | static void io_free_req_work(struct io_kiocb *req, bool *locked) |
2615 | { | |
2616 | io_free_req(req); | |
2617 | } | |
2618 | ||
3aa83bfb | 2619 | static void io_free_batch_list(struct io_ring_ctx *ctx, |
1cce17ac | 2620 | struct io_wq_work_node *node) |
3aa83bfb | 2621 | __must_hold(&ctx->uring_lock) |
5af1d13e | 2622 | { |
d4b7a5ef | 2623 | struct task_struct *task = NULL; |
37f0e767 | 2624 | int task_refs = 0; |
5af1d13e | 2625 | |
3aa83bfb PB |
2626 | do { |
2627 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2628 | comp_list); | |
2d6500d4 | 2629 | |
a538be5b PB |
2630 | if (unlikely(req->flags & IO_REQ_CLEAN_SLOW_FLAGS)) { |
2631 | if (req->flags & REQ_F_REFCOUNT) { | |
2632 | node = req->comp_list.next; | |
2633 | if (!req_ref_put_and_test(req)) | |
2634 | continue; | |
2635 | } | |
b605a7fa PB |
2636 | if ((req->flags & REQ_F_POLLED) && req->apoll) { |
2637 | struct async_poll *apoll = req->apoll; | |
2638 | ||
2639 | if (apoll->double_poll) | |
2640 | kfree(apoll->double_poll); | |
2641 | list_add(&apoll->poll.wait.entry, | |
2642 | &ctx->apoll_cache); | |
2643 | req->flags &= ~REQ_F_POLLED; | |
2644 | } | |
57859f4d PB |
2645 | if (req->flags & (REQ_F_LINK|REQ_F_HARDLINK)) |
2646 | io_queue_next(req); | |
a538be5b PB |
2647 | if (unlikely(req->flags & IO_REQ_CLEAN_FLAGS)) |
2648 | io_clean_op(req); | |
c1e53a69 | 2649 | } |
a538be5b PB |
2650 | if (!(req->flags & REQ_F_FIXED_FILE)) |
2651 | io_put_file(req->file); | |
2d6500d4 | 2652 | |
ab409402 | 2653 | io_req_put_rsrc_locked(req, ctx); |
5af1d13e | 2654 | |
d4b7a5ef PB |
2655 | if (req->task != task) { |
2656 | if (task) | |
2657 | io_put_task(task, task_refs); | |
2658 | task = req->task; | |
2659 | task_refs = 0; | |
2660 | } | |
2661 | task_refs++; | |
c1e53a69 | 2662 | node = req->comp_list.next; |
d4b7a5ef | 2663 | wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list); |
3aa83bfb | 2664 | } while (node); |
d4b7a5ef | 2665 | |
d4b7a5ef PB |
2666 | if (task) |
2667 | io_put_task(task, task_refs); | |
7a743e22 PB |
2668 | } |
2669 | ||
c450178d | 2670 | static void __io_submit_flush_completions(struct io_ring_ctx *ctx) |
a141dd89 | 2671 | __must_hold(&ctx->uring_lock) |
905c172f | 2672 | { |
6f33b0bc | 2673 | struct io_wq_work_node *node, *prev; |
cd0ca2e0 | 2674 | struct io_submit_state *state = &ctx->submit_state; |
905c172f | 2675 | |
3d4aeb9f PB |
2676 | if (state->flush_cqes) { |
2677 | spin_lock(&ctx->completion_lock); | |
2678 | wq_list_for_each(node, prev, &state->compl_reqs) { | |
2679 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
6f33b0bc | 2680 | comp_list); |
5182ed2e | 2681 | |
3d4aeb9f | 2682 | if (!(req->flags & REQ_F_CQE_SKIP)) |
ae4da189 | 2683 | __io_fill_cqe_req(req, req->result, req->cflags); |
3d4aeb9f PB |
2684 | } |
2685 | ||
2686 | io_commit_cqring(ctx); | |
2687 | spin_unlock(&ctx->completion_lock); | |
2688 | io_cqring_ev_posted(ctx); | |
2689 | state->flush_cqes = false; | |
905c172f | 2690 | } |
5182ed2e | 2691 | |
1cce17ac | 2692 | io_free_batch_list(ctx, state->compl_reqs.first); |
6f33b0bc | 2693 | INIT_WQ_LIST(&state->compl_reqs); |
7a743e22 PB |
2694 | } |
2695 | ||
ba816ad6 JA |
2696 | /* |
2697 | * Drop reference to request, return next in chain (if there is one) if this | |
2698 | * was the last reference to this request. | |
2699 | */ | |
0d85035a | 2700 | static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
e65ef56d | 2701 | { |
9b5f7bd9 PB |
2702 | struct io_kiocb *nxt = NULL; |
2703 | ||
de9b4cca | 2704 | if (req_ref_put_and_test(req)) { |
7819a1f6 PB |
2705 | if (unlikely(req->flags & (REQ_F_LINK|REQ_F_HARDLINK))) |
2706 | nxt = io_req_find_next(req); | |
4d7dd462 | 2707 | __io_free_req(req); |
2a44f467 | 2708 | } |
9b5f7bd9 | 2709 | return nxt; |
2b188cc1 JA |
2710 | } |
2711 | ||
0d85035a | 2712 | static inline void io_put_req(struct io_kiocb *req) |
e65ef56d | 2713 | { |
de9b4cca | 2714 | if (req_ref_put_and_test(req)) |
e65ef56d | 2715 | io_free_req(req); |
2b188cc1 JA |
2716 | } |
2717 | ||
91c2f697 | 2718 | static inline void io_put_req_deferred(struct io_kiocb *req) |
216578e5 | 2719 | { |
91c2f697 | 2720 | if (req_ref_put_and_test(req)) { |
f237c30a | 2721 | req->io_task_work.func = io_free_req_work; |
4813c377 | 2722 | io_req_task_work_add(req, false); |
543af3a1 | 2723 | } |
216578e5 PB |
2724 | } |
2725 | ||
6c503150 | 2726 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
a3a0e43f JA |
2727 | { |
2728 | /* See comment at the top of this file */ | |
2729 | smp_rmb(); | |
e23de15f | 2730 | return __io_cqring_events(ctx); |
a3a0e43f JA |
2731 | } |
2732 | ||
fb5ccc98 PB |
2733 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
2734 | { | |
2735 | struct io_rings *rings = ctx->rings; | |
2736 | ||
2737 | /* make sure SQ entry isn't read before tail */ | |
2738 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
2739 | } | |
2740 | ||
4c6e277c JA |
2741 | static inline bool io_run_task_work(void) |
2742 | { | |
7f62d40d | 2743 | if (test_thread_flag(TIF_NOTIFY_SIGNAL) || task_work_pending(current)) { |
4c6e277c | 2744 | __set_current_state(TASK_RUNNING); |
7c5d8fa6 EB |
2745 | clear_notify_signal(); |
2746 | if (task_work_pending(current)) | |
2747 | task_work_run(); | |
4c6e277c JA |
2748 | return true; |
2749 | } | |
2750 | ||
2751 | return false; | |
bcda7baa JA |
2752 | } |
2753 | ||
5ba3c874 | 2754 | static int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin) |
def596e9 | 2755 | { |
5eef4e87 | 2756 | struct io_wq_work_node *pos, *start, *prev; |
d729cf9a | 2757 | unsigned int poll_flags = BLK_POLL_NOSLEEP; |
b688f11e | 2758 | DEFINE_IO_COMP_BATCH(iob); |
5ba3c874 | 2759 | int nr_events = 0; |
def596e9 JA |
2760 | |
2761 | /* | |
2762 | * Only spin for completions if we don't have multiple devices hanging | |
87a115fb | 2763 | * off our complete list. |
def596e9 | 2764 | */ |
87a115fb | 2765 | if (ctx->poll_multi_queue || force_nonspin) |
ef99b2d3 | 2766 | poll_flags |= BLK_POLL_ONESHOT; |
def596e9 | 2767 | |
5eef4e87 PB |
2768 | wq_list_for_each(pos, start, &ctx->iopoll_list) { |
2769 | struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); | |
9adbd45d | 2770 | struct kiocb *kiocb = &req->rw.kiocb; |
a2416e1e | 2771 | int ret; |
def596e9 JA |
2772 | |
2773 | /* | |
581f9810 BM |
2774 | * Move completed and retryable entries to our local lists. |
2775 | * If we find a request that requires polling, break out | |
2776 | * and complete those lists first, if we have entries there. | |
def596e9 | 2777 | */ |
e3f721e6 | 2778 | if (READ_ONCE(req->iopoll_completed)) |
def596e9 JA |
2779 | break; |
2780 | ||
b688f11e | 2781 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, &iob, poll_flags); |
a2416e1e PB |
2782 | if (unlikely(ret < 0)) |
2783 | return ret; | |
2784 | else if (ret) | |
ef99b2d3 | 2785 | poll_flags |= BLK_POLL_ONESHOT; |
def596e9 | 2786 | |
3aadc23e | 2787 | /* iopoll may have completed current req */ |
b688f11e JA |
2788 | if (!rq_list_empty(iob.req_list) || |
2789 | READ_ONCE(req->iopoll_completed)) | |
e3f721e6 | 2790 | break; |
def596e9 JA |
2791 | } |
2792 | ||
b688f11e JA |
2793 | if (!rq_list_empty(iob.req_list)) |
2794 | iob.complete(&iob); | |
5eef4e87 PB |
2795 | else if (!pos) |
2796 | return 0; | |
def596e9 | 2797 | |
5eef4e87 PB |
2798 | prev = start; |
2799 | wq_list_for_each_resume(pos, prev) { | |
2800 | struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); | |
2801 | ||
b3fa03fd PB |
2802 | /* order with io_complete_rw_iopoll(), e.g. ->result updates */ |
2803 | if (!smp_load_acquire(&req->iopoll_completed)) | |
e3f721e6 | 2804 | break; |
c0713540 | 2805 | nr_events++; |
83a13a41 PB |
2806 | if (unlikely(req->flags & REQ_F_CQE_SKIP)) |
2807 | continue; | |
ae4da189 | 2808 | __io_fill_cqe_req(req, req->result, io_put_kbuf(req, 0)); |
e3f721e6 | 2809 | } |
def596e9 | 2810 | |
f5ed3bcd PB |
2811 | if (unlikely(!nr_events)) |
2812 | return 0; | |
2813 | ||
2814 | io_commit_cqring(ctx); | |
2815 | io_cqring_ev_posted_iopoll(ctx); | |
1cce17ac | 2816 | pos = start ? start->next : ctx->iopoll_list.first; |
5eef4e87 | 2817 | wq_list_cut(&ctx->iopoll_list, prev, start); |
1cce17ac | 2818 | io_free_batch_list(ctx, pos); |
5ba3c874 | 2819 | return nr_events; |
def596e9 JA |
2820 | } |
2821 | ||
def596e9 JA |
2822 | /* |
2823 | * We can't just wait for polled events to come to us, we have to actively | |
2824 | * find and complete them. | |
2825 | */ | |
c072481d | 2826 | static __cold void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
def596e9 JA |
2827 | { |
2828 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
2829 | return; | |
2830 | ||
2831 | mutex_lock(&ctx->uring_lock); | |
5eef4e87 | 2832 | while (!wq_list_empty(&ctx->iopoll_list)) { |
b2edc0a7 | 2833 | /* let it sleep and repeat later if can't complete a request */ |
5ba3c874 | 2834 | if (io_do_iopoll(ctx, true) == 0) |
b2edc0a7 | 2835 | break; |
08f5439f JA |
2836 | /* |
2837 | * Ensure we allow local-to-the-cpu processing to take place, | |
2838 | * in this case we need to ensure that we reap all events. | |
3fcee5a6 | 2839 | * Also let task_work, etc. to progress by releasing the mutex |
08f5439f | 2840 | */ |
3fcee5a6 PB |
2841 | if (need_resched()) { |
2842 | mutex_unlock(&ctx->uring_lock); | |
2843 | cond_resched(); | |
2844 | mutex_lock(&ctx->uring_lock); | |
2845 | } | |
def596e9 JA |
2846 | } |
2847 | mutex_unlock(&ctx->uring_lock); | |
2848 | } | |
2849 | ||
7668b92a | 2850 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
def596e9 | 2851 | { |
7668b92a | 2852 | unsigned int nr_events = 0; |
e9979b36 | 2853 | int ret = 0; |
500f9fba | 2854 | |
c7849be9 XW |
2855 | /* |
2856 | * We disallow the app entering submit/complete with polling, but we | |
2857 | * still need to lock the ring to prevent racing with polled issue | |
2858 | * that got punted to a workqueue. | |
2859 | */ | |
2860 | mutex_lock(&ctx->uring_lock); | |
f39c8a5b PB |
2861 | /* |
2862 | * Don't enter poll loop if we already have events pending. | |
2863 | * If we do, we can potentially be spinning for commands that | |
2864 | * already triggered a CQE (eg in error). | |
2865 | */ | |
5ed7a37d | 2866 | if (test_bit(0, &ctx->check_cq_overflow)) |
f39c8a5b PB |
2867 | __io_cqring_overflow_flush(ctx, false); |
2868 | if (io_cqring_events(ctx)) | |
2869 | goto out; | |
def596e9 | 2870 | do { |
500f9fba JA |
2871 | /* |
2872 | * If a submit got punted to a workqueue, we can have the | |
2873 | * application entering polling for a command before it gets | |
2874 | * issued. That app will hold the uring_lock for the duration | |
2875 | * of the poll right here, so we need to take a breather every | |
2876 | * now and then to ensure that the issue has a chance to add | |
2877 | * the poll to the issued list. Otherwise we can spin here | |
2878 | * forever, while the workqueue is stuck trying to acquire the | |
2879 | * very same mutex. | |
2880 | */ | |
5eef4e87 | 2881 | if (wq_list_empty(&ctx->iopoll_list)) { |
8f487ef2 PB |
2882 | u32 tail = ctx->cached_cq_tail; |
2883 | ||
500f9fba | 2884 | mutex_unlock(&ctx->uring_lock); |
4c6e277c | 2885 | io_run_task_work(); |
500f9fba | 2886 | mutex_lock(&ctx->uring_lock); |
def596e9 | 2887 | |
8f487ef2 PB |
2888 | /* some requests don't go through iopoll_list */ |
2889 | if (tail != ctx->cached_cq_tail || | |
5eef4e87 | 2890 | wq_list_empty(&ctx->iopoll_list)) |
e9979b36 | 2891 | break; |
500f9fba | 2892 | } |
5ba3c874 PB |
2893 | ret = io_do_iopoll(ctx, !min); |
2894 | if (ret < 0) | |
2895 | break; | |
2896 | nr_events += ret; | |
2897 | ret = 0; | |
2898 | } while (nr_events < min && !need_resched()); | |
f39c8a5b | 2899 | out: |
500f9fba | 2900 | mutex_unlock(&ctx->uring_lock); |
def596e9 JA |
2901 | return ret; |
2902 | } | |
2903 | ||
491381ce | 2904 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 2905 | { |
491381ce JA |
2906 | /* |
2907 | * Tell lockdep we inherited freeze protection from submission | |
2908 | * thread. | |
2909 | */ | |
2910 | if (req->flags & REQ_F_ISREG) { | |
1c98679d | 2911 | struct super_block *sb = file_inode(req->file)->i_sb; |
2b188cc1 | 2912 | |
1c98679d PB |
2913 | __sb_writers_acquired(sb, SB_FREEZE_WRITE); |
2914 | sb_end_write(sb); | |
2b188cc1 JA |
2915 | } |
2916 | } | |
2917 | ||
b63534c4 | 2918 | #ifdef CONFIG_BLOCK |
dc2a6e9a | 2919 | static bool io_resubmit_prep(struct io_kiocb *req) |
b63534c4 | 2920 | { |
ab454438 | 2921 | struct io_async_rw *rw = req->async_data; |
b63534c4 | 2922 | |
d886e185 | 2923 | if (!req_has_async_data(req)) |
ab454438 | 2924 | return !io_req_prep_async(req); |
538941e2 | 2925 | iov_iter_restore(&rw->s.iter, &rw->s.iter_state); |
ab454438 | 2926 | return true; |
b63534c4 | 2927 | } |
b63534c4 | 2928 | |
3e6a0d3c | 2929 | static bool io_rw_should_reissue(struct io_kiocb *req) |
b63534c4 | 2930 | { |
355afaeb | 2931 | umode_t mode = file_inode(req->file)->i_mode; |
3e6a0d3c | 2932 | struct io_ring_ctx *ctx = req->ctx; |
b63534c4 | 2933 | |
355afaeb JA |
2934 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
2935 | return false; | |
3e6a0d3c JA |
2936 | if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() && |
2937 | !(ctx->flags & IORING_SETUP_IOPOLL))) | |
b63534c4 | 2938 | return false; |
7c977a58 JA |
2939 | /* |
2940 | * If ref is dying, we might be running poll reap from the exit work. | |
2941 | * Don't attempt to reissue from that path, just let it fail with | |
2942 | * -EAGAIN. | |
2943 | */ | |
3e6a0d3c JA |
2944 | if (percpu_ref_is_dying(&ctx->refs)) |
2945 | return false; | |
ef046888 JA |
2946 | /* |
2947 | * Play it safe and assume not safe to re-import and reissue if we're | |
2948 | * not in the original thread group (or in task context). | |
2949 | */ | |
2950 | if (!same_thread_group(req->task, current) || !in_task()) | |
2951 | return false; | |
3e6a0d3c JA |
2952 | return true; |
2953 | } | |
e82ad485 | 2954 | #else |
a1ff1e3f | 2955 | static bool io_resubmit_prep(struct io_kiocb *req) |
e82ad485 JA |
2956 | { |
2957 | return false; | |
2958 | } | |
e82ad485 | 2959 | static bool io_rw_should_reissue(struct io_kiocb *req) |
3e6a0d3c | 2960 | { |
b63534c4 JA |
2961 | return false; |
2962 | } | |
3e6a0d3c | 2963 | #endif |
b63534c4 | 2964 | |
8ef12efe | 2965 | static bool __io_complete_rw_common(struct io_kiocb *req, long res) |
a1d7c393 | 2966 | { |
f63cf519 | 2967 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) { |
b65c128f | 2968 | kiocb_end_write(req); |
f63cf519 JA |
2969 | fsnotify_modify(req->file); |
2970 | } else { | |
2971 | fsnotify_access(req->file); | |
2972 | } | |
258f3a7f | 2973 | if (unlikely(res != req->result)) { |
9532b99b PB |
2974 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && |
2975 | io_rw_should_reissue(req)) { | |
2976 | req->flags |= REQ_F_REISSUE; | |
8ef12efe | 2977 | return true; |
9532b99b | 2978 | } |
93d2bcd2 | 2979 | req_set_fail(req); |
8ef12efe | 2980 | req->result = res; |
9532b99b | 2981 | } |
8ef12efe JA |
2982 | return false; |
2983 | } | |
2984 | ||
cc8e9ba7 | 2985 | static inline void io_req_task_complete(struct io_kiocb *req, bool *locked) |
8ef12efe | 2986 | { |
54daa9b2 | 2987 | int res = req->result; |
126180b9 PB |
2988 | |
2989 | if (*locked) { | |
cc3cec83 | 2990 | io_req_complete_state(req, res, io_put_kbuf(req, 0)); |
fff4e40e | 2991 | io_req_add_compl_list(req); |
126180b9 | 2992 | } else { |
cc3cec83 JA |
2993 | io_req_complete_post(req, res, |
2994 | io_put_kbuf(req, IO_URING_F_UNLOCKED)); | |
126180b9 | 2995 | } |
8ef12efe JA |
2996 | } |
2997 | ||
00f6e68b | 2998 | static void __io_complete_rw(struct io_kiocb *req, long res, |
8ef12efe JA |
2999 | unsigned int issue_flags) |
3000 | { | |
3001 | if (__io_complete_rw_common(req, res)) | |
3002 | return; | |
cc3cec83 JA |
3003 | __io_req_complete(req, issue_flags, req->result, |
3004 | io_put_kbuf(req, issue_flags)); | |
ba816ad6 JA |
3005 | } |
3006 | ||
6b19b766 | 3007 | static void io_complete_rw(struct kiocb *kiocb, long res) |
ba816ad6 | 3008 | { |
9adbd45d | 3009 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 | 3010 | |
8ef12efe JA |
3011 | if (__io_complete_rw_common(req, res)) |
3012 | return; | |
3013 | req->result = res; | |
3014 | req->io_task_work.func = io_req_task_complete; | |
f28c240e | 3015 | io_req_task_work_add(req, !!(req->ctx->flags & IORING_SETUP_SQPOLL)); |
2b188cc1 JA |
3016 | } |
3017 | ||
6b19b766 | 3018 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res) |
def596e9 | 3019 | { |
9adbd45d | 3020 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 3021 | |
491381ce JA |
3022 | if (kiocb->ki_flags & IOCB_WRITE) |
3023 | kiocb_end_write(req); | |
9532b99b | 3024 | if (unlikely(res != req->result)) { |
b66ceaf3 PB |
3025 | if (res == -EAGAIN && io_rw_should_reissue(req)) { |
3026 | req->flags |= REQ_F_REISSUE; | |
3027 | return; | |
9532b99b | 3028 | } |
258f3a7f | 3029 | req->result = res; |
8c130827 | 3030 | } |
bbde017a | 3031 | |
b3fa03fd PB |
3032 | /* order with io_iopoll_complete() checking ->iopoll_completed */ |
3033 | smp_store_release(&req->iopoll_completed, 1); | |
def596e9 JA |
3034 | } |
3035 | ||
3036 | /* | |
3037 | * After the iocb has been issued, it's safe to be found on the poll list. | |
3038 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
f39c8a5b | 3039 | * find it from a io_do_iopoll() thread before the issuer is done |
def596e9 JA |
3040 | * accessing the kiocb cookie. |
3041 | */ | |
9882131c | 3042 | static void io_iopoll_req_issued(struct io_kiocb *req, unsigned int issue_flags) |
def596e9 JA |
3043 | { |
3044 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 3045 | const bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
cb3d8972 PB |
3046 | |
3047 | /* workqueue context doesn't hold uring_lock, grab it now */ | |
3b44b371 | 3048 | if (unlikely(needs_lock)) |
cb3d8972 | 3049 | mutex_lock(&ctx->uring_lock); |
def596e9 JA |
3050 | |
3051 | /* | |
3052 | * Track whether we have multiple files in our lists. This will impact | |
3053 | * how we do polling eventually, not spinning if we're on potentially | |
3054 | * different devices. | |
3055 | */ | |
5eef4e87 | 3056 | if (wq_list_empty(&ctx->iopoll_list)) { |
915b3dde HX |
3057 | ctx->poll_multi_queue = false; |
3058 | } else if (!ctx->poll_multi_queue) { | |
def596e9 JA |
3059 | struct io_kiocb *list_req; |
3060 | ||
5eef4e87 PB |
3061 | list_req = container_of(ctx->iopoll_list.first, struct io_kiocb, |
3062 | comp_list); | |
30da1b45 | 3063 | if (list_req->file != req->file) |
915b3dde | 3064 | ctx->poll_multi_queue = true; |
def596e9 JA |
3065 | } |
3066 | ||
3067 | /* | |
3068 | * For fast devices, IO may have already completed. If it has, add | |
3069 | * it to the front so we find it first. | |
3070 | */ | |
65a6543d | 3071 | if (READ_ONCE(req->iopoll_completed)) |
5eef4e87 | 3072 | wq_list_add_head(&req->comp_list, &ctx->iopoll_list); |
def596e9 | 3073 | else |
5eef4e87 | 3074 | wq_list_add_tail(&req->comp_list, &ctx->iopoll_list); |
bdcd3eab | 3075 | |
3b44b371 | 3076 | if (unlikely(needs_lock)) { |
cb3d8972 PB |
3077 | /* |
3078 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handle | |
3079 | * in sq thread task context or in io worker task context. If | |
3080 | * current task context is sq thread, we don't need to check | |
3081 | * whether should wake up sq thread. | |
3082 | */ | |
3083 | if ((ctx->flags & IORING_SETUP_SQPOLL) && | |
3084 | wq_has_sleeper(&ctx->sq_data->wait)) | |
3085 | wake_up(&ctx->sq_data->wait); | |
3086 | ||
3087 | mutex_unlock(&ctx->uring_lock); | |
3088 | } | |
def596e9 JA |
3089 | } |
3090 | ||
4503b767 JA |
3091 | static bool io_bdev_nowait(struct block_device *bdev) |
3092 | { | |
9ba0d0c8 | 3093 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
4503b767 JA |
3094 | } |
3095 | ||
2b188cc1 JA |
3096 | /* |
3097 | * If we tracked the file through the SCM inflight mechanism, we could support | |
3098 | * any file. For now, just ensure that anything potentially problematic is done | |
3099 | * inline. | |
3100 | */ | |
88459b50 | 3101 | static bool __io_file_supports_nowait(struct file *file, umode_t mode) |
2b188cc1 | 3102 | { |
4503b767 | 3103 | if (S_ISBLK(mode)) { |
4e7b5671 CH |
3104 | if (IS_ENABLED(CONFIG_BLOCK) && |
3105 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) | |
4503b767 JA |
3106 | return true; |
3107 | return false; | |
3108 | } | |
976517f1 | 3109 | if (S_ISSOCK(mode)) |
2b188cc1 | 3110 | return true; |
4503b767 | 3111 | if (S_ISREG(mode)) { |
4e7b5671 CH |
3112 | if (IS_ENABLED(CONFIG_BLOCK) && |
3113 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && | |
4503b767 JA |
3114 | file->f_op != &io_uring_fops) |
3115 | return true; | |
3116 | return false; | |
3117 | } | |
2b188cc1 | 3118 | |
c5b85625 JA |
3119 | /* any ->read/write should understand O_NONBLOCK */ |
3120 | if (file->f_flags & O_NONBLOCK) | |
3121 | return true; | |
35645ac3 | 3122 | return file->f_mode & FMODE_NOWAIT; |
2b188cc1 | 3123 | } |
c5b85625 | 3124 | |
88459b50 PB |
3125 | /* |
3126 | * If we tracked the file through the SCM inflight mechanism, we could support | |
3127 | * any file. For now, just ensure that anything potentially problematic is done | |
3128 | * inline. | |
3129 | */ | |
3130 | static unsigned int io_file_get_flags(struct file *file) | |
3131 | { | |
3132 | umode_t mode = file_inode(file)->i_mode; | |
3133 | unsigned int res = 0; | |
af197f50 | 3134 | |
88459b50 PB |
3135 | if (S_ISREG(mode)) |
3136 | res |= FFS_ISREG; | |
3137 | if (__io_file_supports_nowait(file, mode)) | |
3138 | res |= FFS_NOWAIT; | |
3139 | return res; | |
2b188cc1 JA |
3140 | } |
3141 | ||
35645ac3 | 3142 | static inline bool io_file_supports_nowait(struct io_kiocb *req) |
7b29f92d | 3143 | { |
88459b50 | 3144 | return req->flags & REQ_F_SUPPORT_NOWAIT; |
7b29f92d JA |
3145 | } |
3146 | ||
b9a6b8f9 | 3147 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2b188cc1 | 3148 | { |
9adbd45d | 3149 | struct kiocb *kiocb = &req->rw.kiocb; |
09bb8394 JA |
3150 | unsigned ioprio; |
3151 | int ret; | |
2b188cc1 | 3152 | |
2b188cc1 | 3153 | kiocb->ki_pos = READ_ONCE(sqe->off); |
9adbd45d | 3154 | |
fb27274a PB |
3155 | ioprio = READ_ONCE(sqe->ioprio); |
3156 | if (ioprio) { | |
3157 | ret = ioprio_check_cap(ioprio); | |
3158 | if (ret) | |
3159 | return ret; | |
3160 | ||
3161 | kiocb->ki_ioprio = ioprio; | |
3162 | } else { | |
3163 | kiocb->ki_ioprio = get_current_ioprio(); | |
eae071c9 PB |
3164 | } |
3165 | ||
578c0ee2 | 3166 | req->imu = NULL; |
3529d8c2 JA |
3167 | req->rw.addr = READ_ONCE(sqe->addr); |
3168 | req->rw.len = READ_ONCE(sqe->len); | |
584b0180 | 3169 | req->rw.flags = READ_ONCE(sqe->rw_flags); |
4f4eeba8 | 3170 | req->buf_index = READ_ONCE(sqe->buf_index); |
2b188cc1 | 3171 | return 0; |
2b188cc1 JA |
3172 | } |
3173 | ||
3174 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
3175 | { | |
3176 | switch (ret) { | |
3177 | case -EIOCBQUEUED: | |
3178 | break; | |
3179 | case -ERESTARTSYS: | |
3180 | case -ERESTARTNOINTR: | |
3181 | case -ERESTARTNOHAND: | |
3182 | case -ERESTART_RESTARTBLOCK: | |
3183 | /* | |
3184 | * We can't just restart the syscall, since previously | |
3185 | * submitted sqes may already be in progress. Just fail this | |
3186 | * IO with EINTR. | |
3187 | */ | |
3188 | ret = -EINTR; | |
df561f66 | 3189 | fallthrough; |
2b188cc1 | 3190 | default: |
6b19b766 | 3191 | kiocb->ki_complete(kiocb, ret); |
2b188cc1 JA |
3192 | } |
3193 | } | |
3194 | ||
b4aec400 | 3195 | static inline loff_t *io_kiocb_update_pos(struct io_kiocb *req) |
d34e1e5b DY |
3196 | { |
3197 | struct kiocb *kiocb = &req->rw.kiocb; | |
3198 | ||
6f83ab22 JA |
3199 | if (kiocb->ki_pos != -1) |
3200 | return &kiocb->ki_pos; | |
3201 | ||
3202 | if (!(req->file->f_mode & FMODE_STREAM)) { | |
3203 | req->flags |= REQ_F_CUR_POS; | |
3204 | kiocb->ki_pos = req->file->f_pos; | |
3205 | return &kiocb->ki_pos; | |
d34e1e5b | 3206 | } |
6f83ab22 JA |
3207 | |
3208 | kiocb->ki_pos = 0; | |
3209 | return NULL; | |
d34e1e5b DY |
3210 | } |
3211 | ||
2ea537ca | 3212 | static void kiocb_done(struct io_kiocb *req, ssize_t ret, |
889fca73 | 3213 | unsigned int issue_flags) |
ba816ad6 | 3214 | { |
e8c2bc1f | 3215 | struct io_async_rw *io = req->async_data; |
ba04291e | 3216 | |
227c0c96 | 3217 | /* add previously done IO, if any */ |
d886e185 | 3218 | if (req_has_async_data(req) && io->bytes_done > 0) { |
227c0c96 | 3219 | if (ret < 0) |
e8c2bc1f | 3220 | ret = io->bytes_done; |
227c0c96 | 3221 | else |
e8c2bc1f | 3222 | ret += io->bytes_done; |
227c0c96 JA |
3223 | } |
3224 | ||
ba04291e | 3225 | if (req->flags & REQ_F_CUR_POS) |
2ea537ca PB |
3226 | req->file->f_pos = req->rw.kiocb.ki_pos; |
3227 | if (ret >= 0 && (req->rw.kiocb.ki_complete == io_complete_rw)) | |
00f6e68b | 3228 | __io_complete_rw(req, ret, issue_flags); |
ba816ad6 | 3229 | else |
2ea537ca | 3230 | io_rw_done(&req->rw.kiocb, ret); |
97284637 | 3231 | |
b66ceaf3 | 3232 | if (req->flags & REQ_F_REISSUE) { |
97284637 | 3233 | req->flags &= ~REQ_F_REISSUE; |
b91ef187 | 3234 | if (io_resubmit_prep(req)) |
773af691 | 3235 | io_req_task_queue_reissue(req); |
b91ef187 PB |
3236 | else |
3237 | io_req_task_queue_fail(req, ret); | |
97284637 | 3238 | } |
ba816ad6 JA |
3239 | } |
3240 | ||
eae071c9 PB |
3241 | static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter, |
3242 | struct io_mapped_ubuf *imu) | |
edafccee | 3243 | { |
9adbd45d | 3244 | size_t len = req->rw.len; |
75769e3f | 3245 | u64 buf_end, buf_addr = req->rw.addr; |
edafccee | 3246 | size_t offset; |
edafccee | 3247 | |
75769e3f | 3248 | if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end))) |
edafccee JA |
3249 | return -EFAULT; |
3250 | /* not inside the mapped region */ | |
4751f53d | 3251 | if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end)) |
edafccee JA |
3252 | return -EFAULT; |
3253 | ||
3254 | /* | |
3255 | * May not be a start of buffer, set size appropriately | |
3256 | * and advance us to the beginning. | |
3257 | */ | |
3258 | offset = buf_addr - imu->ubuf; | |
3259 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
3260 | |
3261 | if (offset) { | |
3262 | /* | |
3263 | * Don't use iov_iter_advance() here, as it's really slow for | |
3264 | * using the latter parts of a big fixed buffer - it iterates | |
3265 | * over each segment manually. We can cheat a bit here, because | |
3266 | * we know that: | |
3267 | * | |
3268 | * 1) it's a BVEC iter, we set it up | |
3269 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
3270 | * first and last bvec | |
3271 | * | |
3272 | * So just find our index, and adjust the iterator afterwards. | |
3273 | * If the offset is within the first bvec (or the whole first | |
3274 | * bvec, just use iov_iter_advance(). This makes it easier | |
3275 | * since we can just skip the first segment, which may not | |
3276 | * be PAGE_SIZE aligned. | |
3277 | */ | |
3278 | const struct bio_vec *bvec = imu->bvec; | |
3279 | ||
3280 | if (offset <= bvec->bv_len) { | |
3281 | iov_iter_advance(iter, offset); | |
3282 | } else { | |
3283 | unsigned long seg_skip; | |
3284 | ||
3285 | /* skip first vec */ | |
3286 | offset -= bvec->bv_len; | |
3287 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
3288 | ||
3289 | iter->bvec = bvec + seg_skip; | |
3290 | iter->nr_segs -= seg_skip; | |
99c79f66 | 3291 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 3292 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
3293 | } |
3294 | } | |
3295 | ||
847595de | 3296 | return 0; |
edafccee JA |
3297 | } |
3298 | ||
5106dd6e JA |
3299 | static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter, |
3300 | unsigned int issue_flags) | |
eae071c9 | 3301 | { |
eae071c9 PB |
3302 | struct io_mapped_ubuf *imu = req->imu; |
3303 | u16 index, buf_index = req->buf_index; | |
3304 | ||
3305 | if (likely(!imu)) { | |
578c0ee2 PB |
3306 | struct io_ring_ctx *ctx = req->ctx; |
3307 | ||
eae071c9 PB |
3308 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
3309 | return -EFAULT; | |
5106dd6e | 3310 | io_req_set_rsrc_node(req, ctx, issue_flags); |
eae071c9 PB |
3311 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
3312 | imu = READ_ONCE(ctx->user_bufs[index]); | |
3313 | req->imu = imu; | |
3314 | } | |
3315 | return __io_import_fixed(req, rw, iter, imu); | |
3316 | } | |
3317 | ||
bcda7baa JA |
3318 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
3319 | { | |
3320 | if (needs_lock) | |
3321 | mutex_unlock(&ctx->uring_lock); | |
3322 | } | |
3323 | ||
3324 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) | |
3325 | { | |
3326 | /* | |
3327 | * "Normal" inline submissions always hold the uring_lock, since we | |
3328 | * grab it from the system call. Same is true for the SQPOLL offload. | |
3329 | * The only exception is when we've detached the request and issue it | |
3330 | * from an async worker thread, grab the lock for that case. | |
3331 | */ | |
3332 | if (needs_lock) | |
3333 | mutex_lock(&ctx->uring_lock); | |
3334 | } | |
3335 | ||
dbc7d452 JA |
3336 | static void io_buffer_add_list(struct io_ring_ctx *ctx, |
3337 | struct io_buffer_list *bl, unsigned int bgid) | |
3338 | { | |
3339 | struct list_head *list; | |
3340 | ||
3341 | list = &ctx->io_buffers[hash_32(bgid, IO_BUFFERS_HASH_BITS)]; | |
3342 | INIT_LIST_HEAD(&bl->buf_list); | |
3343 | bl->bgid = bgid; | |
3344 | list_add(&bl->list, list); | |
3345 | } | |
3346 | ||
bcda7baa | 3347 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, |
51aac424 | 3348 | int bgid, unsigned int issue_flags) |
bcda7baa | 3349 | { |
30d51dd4 | 3350 | struct io_buffer *kbuf = req->kbuf; |
3b44b371 | 3351 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
dbc7d452 JA |
3352 | struct io_ring_ctx *ctx = req->ctx; |
3353 | struct io_buffer_list *bl; | |
bcda7baa JA |
3354 | |
3355 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
3356 | return kbuf; | |
3357 | ||
dbc7d452 | 3358 | io_ring_submit_lock(ctx, needs_lock); |
bcda7baa | 3359 | |
dbc7d452 | 3360 | lockdep_assert_held(&ctx->uring_lock); |
bcda7baa | 3361 | |
dbc7d452 JA |
3362 | bl = io_buffer_get_list(ctx, bgid); |
3363 | if (bl && !list_empty(&bl->buf_list)) { | |
3364 | kbuf = list_first_entry(&bl->buf_list, struct io_buffer, list); | |
3365 | list_del(&kbuf->list); | |
bcda7baa JA |
3366 | if (*len > kbuf->len) |
3367 | *len = kbuf->len; | |
30d51dd4 PB |
3368 | req->flags |= REQ_F_BUFFER_SELECTED; |
3369 | req->kbuf = kbuf; | |
bcda7baa JA |
3370 | } else { |
3371 | kbuf = ERR_PTR(-ENOBUFS); | |
3372 | } | |
3373 | ||
3374 | io_ring_submit_unlock(req->ctx, needs_lock); | |
bcda7baa JA |
3375 | return kbuf; |
3376 | } | |
3377 | ||
4d954c25 | 3378 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
51aac424 | 3379 | unsigned int issue_flags) |
4d954c25 JA |
3380 | { |
3381 | struct io_buffer *kbuf; | |
4f4eeba8 | 3382 | u16 bgid; |
4d954c25 | 3383 | |
4f4eeba8 | 3384 | bgid = req->buf_index; |
51aac424 | 3385 | kbuf = io_buffer_select(req, len, bgid, issue_flags); |
4d954c25 JA |
3386 | if (IS_ERR(kbuf)) |
3387 | return kbuf; | |
4d954c25 JA |
3388 | return u64_to_user_ptr(kbuf->addr); |
3389 | } | |
3390 | ||
3391 | #ifdef CONFIG_COMPAT | |
3392 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, | |
51aac424 | 3393 | unsigned int issue_flags) |
4d954c25 JA |
3394 | { |
3395 | struct compat_iovec __user *uiov; | |
3396 | compat_ssize_t clen; | |
3397 | void __user *buf; | |
3398 | ssize_t len; | |
3399 | ||
3400 | uiov = u64_to_user_ptr(req->rw.addr); | |
3401 | if (!access_ok(uiov, sizeof(*uiov))) | |
3402 | return -EFAULT; | |
3403 | if (__get_user(clen, &uiov->iov_len)) | |
3404 | return -EFAULT; | |
3405 | if (clen < 0) | |
3406 | return -EINVAL; | |
3407 | ||
3408 | len = clen; | |
51aac424 | 3409 | buf = io_rw_buffer_select(req, &len, issue_flags); |
4d954c25 JA |
3410 | if (IS_ERR(buf)) |
3411 | return PTR_ERR(buf); | |
3412 | iov[0].iov_base = buf; | |
3413 | iov[0].iov_len = (compat_size_t) len; | |
3414 | return 0; | |
3415 | } | |
3416 | #endif | |
3417 | ||
3418 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
51aac424 | 3419 | unsigned int issue_flags) |
4d954c25 JA |
3420 | { |
3421 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); | |
3422 | void __user *buf; | |
3423 | ssize_t len; | |
3424 | ||
3425 | if (copy_from_user(iov, uiov, sizeof(*uiov))) | |
3426 | return -EFAULT; | |
3427 | ||
3428 | len = iov[0].iov_len; | |
3429 | if (len < 0) | |
3430 | return -EINVAL; | |
51aac424 | 3431 | buf = io_rw_buffer_select(req, &len, issue_flags); |
4d954c25 JA |
3432 | if (IS_ERR(buf)) |
3433 | return PTR_ERR(buf); | |
3434 | iov[0].iov_base = buf; | |
3435 | iov[0].iov_len = len; | |
3436 | return 0; | |
3437 | } | |
3438 | ||
3439 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
51aac424 | 3440 | unsigned int issue_flags) |
4d954c25 | 3441 | { |
dddb3e26 | 3442 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
30d51dd4 | 3443 | struct io_buffer *kbuf = req->kbuf; |
dddb3e26 | 3444 | |
dddb3e26 JA |
3445 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
3446 | iov[0].iov_len = kbuf->len; | |
4d954c25 | 3447 | return 0; |
dddb3e26 | 3448 | } |
dd201662 | 3449 | if (req->rw.len != 1) |
4d954c25 JA |
3450 | return -EINVAL; |
3451 | ||
3452 | #ifdef CONFIG_COMPAT | |
3453 | if (req->ctx->compat) | |
51aac424 | 3454 | return io_compat_import(req, iov, issue_flags); |
4d954c25 JA |
3455 | #endif |
3456 | ||
51aac424 | 3457 | return __io_iov_buffer_select(req, iov, issue_flags); |
4d954c25 JA |
3458 | } |
3459 | ||
caa8fe6e PB |
3460 | static struct iovec *__io_import_iovec(int rw, struct io_kiocb *req, |
3461 | struct io_rw_state *s, | |
3462 | unsigned int issue_flags) | |
2b188cc1 | 3463 | { |
5e49c973 | 3464 | struct iov_iter *iter = &s->iter; |
847595de | 3465 | u8 opcode = req->opcode; |
caa8fe6e | 3466 | struct iovec *iovec; |
d1d681b0 PB |
3467 | void __user *buf; |
3468 | size_t sqe_len; | |
4d954c25 | 3469 | ssize_t ret; |
edafccee | 3470 | |
f3251183 | 3471 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
5106dd6e | 3472 | ret = io_import_fixed(req, rw, iter, issue_flags); |
f3251183 PB |
3473 | if (ret) |
3474 | return ERR_PTR(ret); | |
3475 | return NULL; | |
3476 | } | |
2b188cc1 | 3477 | |
bcda7baa | 3478 | /* buffer index only valid with fixed read/write, or buffer select */ |
d1d681b0 | 3479 | if (unlikely(req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))) |
caa8fe6e | 3480 | return ERR_PTR(-EINVAL); |
9adbd45d | 3481 | |
d1d681b0 PB |
3482 | buf = u64_to_user_ptr(req->rw.addr); |
3483 | sqe_len = req->rw.len; | |
9adbd45d | 3484 | |
3a6820f2 | 3485 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
bcda7baa | 3486 | if (req->flags & REQ_F_BUFFER_SELECT) { |
51aac424 | 3487 | buf = io_rw_buffer_select(req, &sqe_len, issue_flags); |
867a23ea | 3488 | if (IS_ERR(buf)) |
898df244 | 3489 | return ERR_CAST(buf); |
3f9d6441 | 3490 | req->rw.len = sqe_len; |
bcda7baa JA |
3491 | } |
3492 | ||
5e49c973 | 3493 | ret = import_single_range(rw, buf, sqe_len, s->fast_iov, iter); |
f3251183 PB |
3494 | if (ret) |
3495 | return ERR_PTR(ret); | |
3496 | return NULL; | |
3a6820f2 JA |
3497 | } |
3498 | ||
caa8fe6e | 3499 | iovec = s->fast_iov; |
4d954c25 | 3500 | if (req->flags & REQ_F_BUFFER_SELECT) { |
caa8fe6e | 3501 | ret = io_iov_buffer_select(req, iovec, issue_flags); |
f3251183 PB |
3502 | if (ret) |
3503 | return ERR_PTR(ret); | |
3504 | iov_iter_init(iter, rw, iovec, 1, iovec->iov_len); | |
3505 | return NULL; | |
4d954c25 JA |
3506 | } |
3507 | ||
caa8fe6e | 3508 | ret = __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, &iovec, iter, |
89cd35c5 | 3509 | req->ctx->compat); |
caa8fe6e PB |
3510 | if (unlikely(ret < 0)) |
3511 | return ERR_PTR(ret); | |
3512 | return iovec; | |
2b188cc1 JA |
3513 | } |
3514 | ||
5e49c973 PB |
3515 | static inline int io_import_iovec(int rw, struct io_kiocb *req, |
3516 | struct iovec **iovec, struct io_rw_state *s, | |
3517 | unsigned int issue_flags) | |
3518 | { | |
caa8fe6e PB |
3519 | *iovec = __io_import_iovec(rw, req, s, issue_flags); |
3520 | if (unlikely(IS_ERR(*iovec))) | |
3521 | return PTR_ERR(*iovec); | |
5e49c973 | 3522 | |
5e49c973 | 3523 | iov_iter_save_state(&s->iter, &s->iter_state); |
caa8fe6e | 3524 | return 0; |
2b188cc1 JA |
3525 | } |
3526 | ||
0fef9483 JA |
3527 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
3528 | { | |
5b09e37e | 3529 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
0fef9483 JA |
3530 | } |
3531 | ||
31b51510 | 3532 | /* |
32960613 JA |
3533 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
3534 | * by looping over ->read() or ->write() manually. | |
31b51510 | 3535 | */ |
4017eb91 | 3536 | static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) |
32960613 | 3537 | { |
4017eb91 JA |
3538 | struct kiocb *kiocb = &req->rw.kiocb; |
3539 | struct file *file = req->file; | |
32960613 | 3540 | ssize_t ret = 0; |
af9c45ec | 3541 | loff_t *ppos; |
32960613 JA |
3542 | |
3543 | /* | |
3544 | * Don't support polled IO through this interface, and we can't | |
3545 | * support non-blocking either. For the latter, this just causes | |
3546 | * the kiocb to be handled from an async context. | |
3547 | */ | |
3548 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3549 | return -EOPNOTSUPP; | |
35645ac3 PB |
3550 | if ((kiocb->ki_flags & IOCB_NOWAIT) && |
3551 | !(kiocb->ki_filp->f_flags & O_NONBLOCK)) | |
32960613 JA |
3552 | return -EAGAIN; |
3553 | ||
af9c45ec DY |
3554 | ppos = io_kiocb_ppos(kiocb); |
3555 | ||
32960613 | 3556 | while (iov_iter_count(iter)) { |
311ae9e1 | 3557 | struct iovec iovec; |
32960613 JA |
3558 | ssize_t nr; |
3559 | ||
311ae9e1 PB |
3560 | if (!iov_iter_is_bvec(iter)) { |
3561 | iovec = iov_iter_iovec(iter); | |
3562 | } else { | |
4017eb91 JA |
3563 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
3564 | iovec.iov_len = req->rw.len; | |
311ae9e1 PB |
3565 | } |
3566 | ||
32960613 JA |
3567 | if (rw == READ) { |
3568 | nr = file->f_op->read(file, iovec.iov_base, | |
af9c45ec | 3569 | iovec.iov_len, ppos); |
32960613 JA |
3570 | } else { |
3571 | nr = file->f_op->write(file, iovec.iov_base, | |
af9c45ec | 3572 | iovec.iov_len, ppos); |
32960613 JA |
3573 | } |
3574 | ||
3575 | if (nr < 0) { | |
3576 | if (!ret) | |
3577 | ret = nr; | |
3578 | break; | |
3579 | } | |
5e929367 | 3580 | ret += nr; |
16c8d2df JA |
3581 | if (!iov_iter_is_bvec(iter)) { |
3582 | iov_iter_advance(iter, nr); | |
3583 | } else { | |
16c8d2df | 3584 | req->rw.addr += nr; |
5e929367 JA |
3585 | req->rw.len -= nr; |
3586 | if (!req->rw.len) | |
3587 | break; | |
16c8d2df | 3588 | } |
32960613 JA |
3589 | if (nr != iovec.iov_len) |
3590 | break; | |
32960613 JA |
3591 | } |
3592 | ||
3593 | return ret; | |
3594 | } | |
3595 | ||
ff6165b2 JA |
3596 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
3597 | const struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 3598 | { |
e8c2bc1f | 3599 | struct io_async_rw *rw = req->async_data; |
b64e3444 | 3600 | |
538941e2 | 3601 | memcpy(&rw->s.iter, iter, sizeof(*iter)); |
afb87658 | 3602 | rw->free_iovec = iovec; |
227c0c96 | 3603 | rw->bytes_done = 0; |
ff6165b2 | 3604 | /* can only be fixed buffers, no need to do anything */ |
9c3a205c | 3605 | if (iov_iter_is_bvec(iter)) |
ff6165b2 | 3606 | return; |
b64e3444 | 3607 | if (!iovec) { |
ff6165b2 JA |
3608 | unsigned iov_off = 0; |
3609 | ||
538941e2 | 3610 | rw->s.iter.iov = rw->s.fast_iov; |
ff6165b2 JA |
3611 | if (iter->iov != fast_iov) { |
3612 | iov_off = iter->iov - fast_iov; | |
538941e2 | 3613 | rw->s.iter.iov += iov_off; |
ff6165b2 | 3614 | } |
538941e2 PB |
3615 | if (rw->s.fast_iov != fast_iov) |
3616 | memcpy(rw->s.fast_iov + iov_off, fast_iov + iov_off, | |
45097dae | 3617 | sizeof(struct iovec) * iter->nr_segs); |
99bc4c38 PB |
3618 | } else { |
3619 | req->flags |= REQ_F_NEED_CLEANUP; | |
f67676d1 JA |
3620 | } |
3621 | } | |
3622 | ||
8d4af685 | 3623 | static inline bool io_alloc_async_data(struct io_kiocb *req) |
3d9932a8 | 3624 | { |
e8c2bc1f JA |
3625 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
3626 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); | |
d886e185 PB |
3627 | if (req->async_data) { |
3628 | req->flags |= REQ_F_ASYNC_DATA; | |
3629 | return false; | |
3630 | } | |
3631 | return true; | |
3d9932a8 XW |
3632 | } |
3633 | ||
ff6165b2 | 3634 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
c88598a9 | 3635 | struct io_rw_state *s, bool force) |
b7bb4f7d | 3636 | { |
26f0505a | 3637 | if (!force && !io_op_defs[req->opcode].needs_async_setup) |
74566df3 | 3638 | return 0; |
d886e185 | 3639 | if (!req_has_async_data(req)) { |
cd658695 JA |
3640 | struct io_async_rw *iorw; |
3641 | ||
6cb78689 | 3642 | if (io_alloc_async_data(req)) { |
6bf985dc | 3643 | kfree(iovec); |
5d204bcf | 3644 | return -ENOMEM; |
6bf985dc | 3645 | } |
b7bb4f7d | 3646 | |
c88598a9 | 3647 | io_req_map_rw(req, iovec, s->fast_iov, &s->iter); |
cd658695 JA |
3648 | iorw = req->async_data; |
3649 | /* we've copied and mapped the iter, ensure state is saved */ | |
538941e2 | 3650 | iov_iter_save_state(&iorw->s.iter, &iorw->s.iter_state); |
5d204bcf | 3651 | } |
b7bb4f7d | 3652 | return 0; |
f67676d1 JA |
3653 | } |
3654 | ||
73debe68 | 3655 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw) |
c3e330a4 | 3656 | { |
e8c2bc1f | 3657 | struct io_async_rw *iorw = req->async_data; |
5e49c973 | 3658 | struct iovec *iov; |
847595de | 3659 | int ret; |
c3e330a4 | 3660 | |
51aac424 | 3661 | /* submission path, ->uring_lock should already be taken */ |
3b44b371 | 3662 | ret = io_import_iovec(rw, req, &iov, &iorw->s, 0); |
c3e330a4 PB |
3663 | if (unlikely(ret < 0)) |
3664 | return ret; | |
3665 | ||
ab0b196c PB |
3666 | iorw->bytes_done = 0; |
3667 | iorw->free_iovec = iov; | |
3668 | if (iov) | |
3669 | req->flags |= REQ_F_NEED_CLEANUP; | |
c3e330a4 PB |
3670 | return 0; |
3671 | } | |
3672 | ||
c1dd91d1 | 3673 | /* |
ffdc8dab | 3674 | * This is our waitqueue callback handler, registered through __folio_lock_async() |
c1dd91d1 JA |
3675 | * when we initially tried to do the IO with the iocb armed our waitqueue. |
3676 | * This gets called when the page is unlocked, and we generally expect that to | |
3677 | * happen when the page IO is completed and the page is now uptodate. This will | |
3678 | * queue a task_work based retry of the operation, attempting to copy the data | |
3679 | * again. If the latter fails because the page was NOT uptodate, then we will | |
3680 | * do a thread based blocking retry of the operation. That's the unexpected | |
3681 | * slow path. | |
3682 | */ | |
bcf5a063 JA |
3683 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
3684 | int sync, void *arg) | |
3685 | { | |
3686 | struct wait_page_queue *wpq; | |
3687 | struct io_kiocb *req = wait->private; | |
bcf5a063 | 3688 | struct wait_page_key *key = arg; |
bcf5a063 JA |
3689 | |
3690 | wpq = container_of(wait, struct wait_page_queue, wait); | |
3691 | ||
cdc8fcb4 LT |
3692 | if (!wake_page_match(wpq, key)) |
3693 | return 0; | |
3694 | ||
c8d317aa | 3695 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
bcf5a063 | 3696 | list_del_init(&wait->entry); |
921b9054 | 3697 | io_req_task_queue(req); |
bcf5a063 JA |
3698 | return 1; |
3699 | } | |
3700 | ||
c1dd91d1 JA |
3701 | /* |
3702 | * This controls whether a given IO request should be armed for async page | |
3703 | * based retry. If we return false here, the request is handed to the async | |
3704 | * worker threads for retry. If we're doing buffered reads on a regular file, | |
3705 | * we prepare a private wait_page_queue entry and retry the operation. This | |
3706 | * will either succeed because the page is now uptodate and unlocked, or it | |
3707 | * will register a callback when the page is unlocked at IO completion. Through | |
3708 | * that callback, io_uring uses task_work to setup a retry of the operation. | |
3709 | * That retry will attempt the buffered read again. The retry will generally | |
3710 | * succeed, or in rare cases where it fails, we then fall back to using the | |
3711 | * async worker threads for a blocking retry. | |
3712 | */ | |
227c0c96 | 3713 | static bool io_rw_should_retry(struct io_kiocb *req) |
f67676d1 | 3714 | { |
e8c2bc1f JA |
3715 | struct io_async_rw *rw = req->async_data; |
3716 | struct wait_page_queue *wait = &rw->wpq; | |
bcf5a063 | 3717 | struct kiocb *kiocb = &req->rw.kiocb; |
f67676d1 | 3718 | |
bcf5a063 JA |
3719 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
3720 | if (req->flags & REQ_F_NOWAIT) | |
3721 | return false; | |
f67676d1 | 3722 | |
227c0c96 | 3723 | /* Only for buffered IO */ |
3b2a4439 | 3724 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
bcf5a063 | 3725 | return false; |
3b2a4439 | 3726 | |
bcf5a063 JA |
3727 | /* |
3728 | * just use poll if we can, and don't attempt if the fs doesn't | |
3729 | * support callback based unlocks | |
3730 | */ | |
3731 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) | |
3732 | return false; | |
f67676d1 | 3733 | |
3b2a4439 JA |
3734 | wait->wait.func = io_async_buf_func; |
3735 | wait->wait.private = req; | |
3736 | wait->wait.flags = 0; | |
3737 | INIT_LIST_HEAD(&wait->wait.entry); | |
3738 | kiocb->ki_flags |= IOCB_WAITQ; | |
c8d317aa | 3739 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3b2a4439 | 3740 | kiocb->ki_waitq = wait; |
3b2a4439 | 3741 | return true; |
bcf5a063 JA |
3742 | } |
3743 | ||
aeab9506 | 3744 | static inline int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) |
bcf5a063 | 3745 | { |
607b6fb8 | 3746 | if (likely(req->file->f_op->read_iter)) |
bcf5a063 | 3747 | return call_read_iter(req->file, &req->rw.kiocb, iter); |
2dd2111d | 3748 | else if (req->file->f_op->read) |
4017eb91 | 3749 | return loop_rw_iter(READ, req, iter); |
2dd2111d GH |
3750 | else |
3751 | return -EINVAL; | |
f67676d1 JA |
3752 | } |
3753 | ||
7db30437 ML |
3754 | static bool need_read_all(struct io_kiocb *req) |
3755 | { | |
3756 | return req->flags & REQ_F_ISREG || | |
3757 | S_ISBLK(file_inode(req->file)->i_mode); | |
3758 | } | |
3759 | ||
584b0180 JA |
3760 | static int io_rw_init_file(struct io_kiocb *req, fmode_t mode) |
3761 | { | |
3762 | struct kiocb *kiocb = &req->rw.kiocb; | |
3763 | struct io_ring_ctx *ctx = req->ctx; | |
3764 | struct file *file = req->file; | |
3765 | int ret; | |
3766 | ||
3767 | if (unlikely(!file || !(file->f_mode & mode))) | |
3768 | return -EBADF; | |
3769 | ||
3770 | if (!io_req_ffs_set(req)) | |
3771 | req->flags |= io_file_get_flags(file) << REQ_F_SUPPORT_NOWAIT_BIT; | |
3772 | ||
3773 | kiocb->ki_flags = iocb_flags(file); | |
3774 | ret = kiocb_set_rw_flags(kiocb, req->rw.flags); | |
3775 | if (unlikely(ret)) | |
3776 | return ret; | |
3777 | ||
3778 | /* | |
3779 | * If the file is marked O_NONBLOCK, still allow retry for it if it | |
3780 | * supports async. Otherwise it's impossible to use O_NONBLOCK files | |
3781 | * reliably. If not, or it IOCB_NOWAIT is set, don't retry. | |
3782 | */ | |
3783 | if ((kiocb->ki_flags & IOCB_NOWAIT) || | |
3784 | ((file->f_flags & O_NONBLOCK) && !io_file_supports_nowait(req))) | |
3785 | req->flags |= REQ_F_NOWAIT; | |
3786 | ||
3787 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
3788 | if (!(kiocb->ki_flags & IOCB_DIRECT) || !file->f_op->iopoll) | |
3789 | return -EOPNOTSUPP; | |
3790 | ||
3791 | kiocb->ki_flags |= IOCB_HIPRI | IOCB_ALLOC_CACHE; | |
3792 | kiocb->ki_complete = io_complete_rw_iopoll; | |
3793 | req->iopoll_completed = 0; | |
3794 | } else { | |
3795 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3796 | return -EINVAL; | |
3797 | kiocb->ki_complete = io_complete_rw; | |
3798 | } | |
3799 | ||
3800 | return 0; | |
3801 | } | |
3802 | ||
889fca73 | 3803 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 3804 | { |
607b6fb8 | 3805 | struct io_rw_state __s, *s = &__s; |
c88598a9 | 3806 | struct iovec *iovec; |
9adbd45d | 3807 | struct kiocb *kiocb = &req->rw.kiocb; |
45d189c6 | 3808 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
d886e185 | 3809 | struct io_async_rw *rw; |
cd658695 | 3810 | ssize_t ret, ret2; |
b4aec400 | 3811 | loff_t *ppos; |
ff6165b2 | 3812 | |
607b6fb8 PB |
3813 | if (!req_has_async_data(req)) { |
3814 | ret = io_import_iovec(READ, req, &iovec, s, issue_flags); | |
3815 | if (unlikely(ret < 0)) | |
3816 | return ret; | |
3817 | } else { | |
2be2eb02 JA |
3818 | /* |
3819 | * Safe and required to re-import if we're using provided | |
3820 | * buffers, as we dropped the selected one before retry. | |
3821 | */ | |
3822 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
3823 | ret = io_import_iovec(READ, req, &iovec, s, issue_flags); | |
3824 | if (unlikely(ret < 0)) | |
3825 | return ret; | |
3826 | } | |
3827 | ||
d886e185 | 3828 | rw = req->async_data; |
c88598a9 | 3829 | s = &rw->s; |
cd658695 JA |
3830 | /* |
3831 | * We come here from an earlier attempt, restore our state to | |
3832 | * match in case it doesn't. It's cheap enough that we don't | |
3833 | * need to make this conditional. | |
3834 | */ | |
c88598a9 | 3835 | iov_iter_restore(&s->iter, &s->iter_state); |
2846c481 | 3836 | iovec = NULL; |
2846c481 | 3837 | } |
584b0180 | 3838 | ret = io_rw_init_file(req, FMODE_READ); |
323b190b JA |
3839 | if (unlikely(ret)) { |
3840 | kfree(iovec); | |
584b0180 | 3841 | return ret; |
323b190b | 3842 | } |
c88598a9 | 3843 | req->result = iov_iter_count(&s->iter); |
2b188cc1 | 3844 | |
607b6fb8 PB |
3845 | if (force_nonblock) { |
3846 | /* If the file doesn't support async, just async punt */ | |
35645ac3 | 3847 | if (unlikely(!io_file_supports_nowait(req))) { |
607b6fb8 PB |
3848 | ret = io_setup_async_rw(req, iovec, s, true); |
3849 | return ret ?: -EAGAIN; | |
3850 | } | |
a88fc400 | 3851 | kiocb->ki_flags |= IOCB_NOWAIT; |
607b6fb8 PB |
3852 | } else { |
3853 | /* Ensure we clear previously set non-block flag */ | |
3854 | kiocb->ki_flags &= ~IOCB_NOWAIT; | |
6713e7a6 | 3855 | } |
9e645e11 | 3856 | |
b4aec400 | 3857 | ppos = io_kiocb_update_pos(req); |
d34e1e5b | 3858 | |
b4aec400 | 3859 | ret = rw_verify_area(READ, req->file, ppos, req->result); |
5ea5dd45 PB |
3860 | if (unlikely(ret)) { |
3861 | kfree(iovec); | |
3862 | return ret; | |
3863 | } | |
2b188cc1 | 3864 | |
c88598a9 | 3865 | ret = io_iter_do_read(req, &s->iter); |
32960613 | 3866 | |
230d50d4 | 3867 | if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) { |
6ad7f233 | 3868 | req->flags &= ~REQ_F_REISSUE; |
9af177ee JA |
3869 | /* if we can poll, just do that */ |
3870 | if (req->opcode == IORING_OP_READ && file_can_poll(req->file)) | |
3871 | return -EAGAIN; | |
eefdf30f JA |
3872 | /* IOPOLL retry should happen for io-wq threads */ |
3873 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
f91daf56 | 3874 | goto done; |
75c668cd PB |
3875 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3876 | if (req->flags & REQ_F_NOWAIT) | |
355afaeb | 3877 | goto done; |
f38c7e3a | 3878 | ret = 0; |
230d50d4 JA |
3879 | } else if (ret == -EIOCBQUEUED) { |
3880 | goto out_free; | |
f80a50a6 | 3881 | } else if (ret == req->result || ret <= 0 || !force_nonblock || |
7db30437 | 3882 | (req->flags & REQ_F_NOWAIT) || !need_read_all(req)) { |
7335e3bf | 3883 | /* read all, failed, already did sync or don't want to retry */ |
00d23d51 | 3884 | goto done; |
227c0c96 JA |
3885 | } |
3886 | ||
cd658695 JA |
3887 | /* |
3888 | * Don't depend on the iter state matching what was consumed, or being | |
3889 | * untouched in case of error. Restore it and we'll advance it | |
3890 | * manually if we need to. | |
3891 | */ | |
c88598a9 | 3892 | iov_iter_restore(&s->iter, &s->iter_state); |
cd658695 | 3893 | |
c88598a9 | 3894 | ret2 = io_setup_async_rw(req, iovec, s, true); |
6bf985dc PB |
3895 | if (ret2) |
3896 | return ret2; | |
3897 | ||
fe1cdd55 | 3898 | iovec = NULL; |
e8c2bc1f | 3899 | rw = req->async_data; |
c88598a9 | 3900 | s = &rw->s; |
cd658695 JA |
3901 | /* |
3902 | * Now use our persistent iterator and state, if we aren't already. | |
3903 | * We've restored and mapped the iter to match. | |
3904 | */ | |
227c0c96 | 3905 | |
b23df91b | 3906 | do { |
cd658695 JA |
3907 | /* |
3908 | * We end up here because of a partial read, either from | |
3909 | * above or inside this loop. Advance the iter by the bytes | |
3910 | * that were consumed. | |
3911 | */ | |
c88598a9 PB |
3912 | iov_iter_advance(&s->iter, ret); |
3913 | if (!iov_iter_count(&s->iter)) | |
cd658695 | 3914 | break; |
b23df91b | 3915 | rw->bytes_done += ret; |
c88598a9 | 3916 | iov_iter_save_state(&s->iter, &s->iter_state); |
cd658695 | 3917 | |
b23df91b PB |
3918 | /* if we can retry, do so with the callbacks armed */ |
3919 | if (!io_rw_should_retry(req)) { | |
3920 | kiocb->ki_flags &= ~IOCB_WAITQ; | |
3921 | return -EAGAIN; | |
3922 | } | |
3923 | ||
3924 | /* | |
3925 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If | |
3926 | * we get -EIOCBQUEUED, then we'll get a notification when the | |
3927 | * desired page gets unlocked. We can also get a partial read | |
3928 | * here, and if we do, then just retry at the new offset. | |
3929 | */ | |
c88598a9 | 3930 | ret = io_iter_do_read(req, &s->iter); |
b23df91b PB |
3931 | if (ret == -EIOCBQUEUED) |
3932 | return 0; | |
227c0c96 | 3933 | /* we got some bytes, but not all. retry. */ |
b5b0ecb7 | 3934 | kiocb->ki_flags &= ~IOCB_WAITQ; |
c88598a9 | 3935 | iov_iter_restore(&s->iter, &s->iter_state); |
cd658695 | 3936 | } while (ret > 0); |
227c0c96 | 3937 | done: |
2ea537ca | 3938 | kiocb_done(req, ret, issue_flags); |
fe1cdd55 PB |
3939 | out_free: |
3940 | /* it's faster to check here then delegate to kfree */ | |
3941 | if (iovec) | |
3942 | kfree(iovec); | |
5ea5dd45 | 3943 | return 0; |
2b188cc1 JA |
3944 | } |
3945 | ||
889fca73 | 3946 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 3947 | { |
607b6fb8 | 3948 | struct io_rw_state __s, *s = &__s; |
c88598a9 | 3949 | struct iovec *iovec; |
9adbd45d | 3950 | struct kiocb *kiocb = &req->rw.kiocb; |
45d189c6 | 3951 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
cd658695 | 3952 | ssize_t ret, ret2; |
b4aec400 | 3953 | loff_t *ppos; |
2b188cc1 | 3954 | |
607b6fb8 | 3955 | if (!req_has_async_data(req)) { |
5e49c973 PB |
3956 | ret = io_import_iovec(WRITE, req, &iovec, s, issue_flags); |
3957 | if (unlikely(ret < 0)) | |
2846c481 | 3958 | return ret; |
607b6fb8 PB |
3959 | } else { |
3960 | struct io_async_rw *rw = req->async_data; | |
3961 | ||
3962 | s = &rw->s; | |
3963 | iov_iter_restore(&s->iter, &s->iter_state); | |
2846c481 | 3964 | iovec = NULL; |
2846c481 | 3965 | } |
584b0180 | 3966 | ret = io_rw_init_file(req, FMODE_WRITE); |
323b190b JA |
3967 | if (unlikely(ret)) { |
3968 | kfree(iovec); | |
584b0180 | 3969 | return ret; |
323b190b | 3970 | } |
c88598a9 | 3971 | req->result = iov_iter_count(&s->iter); |
2b188cc1 | 3972 | |
607b6fb8 PB |
3973 | if (force_nonblock) { |
3974 | /* If the file doesn't support async, just async punt */ | |
35645ac3 | 3975 | if (unlikely(!io_file_supports_nowait(req))) |
607b6fb8 | 3976 | goto copy_iov; |
fd6c2e4c | 3977 | |
607b6fb8 PB |
3978 | /* file path doesn't support NOWAIT for non-direct_IO */ |
3979 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
3980 | (req->flags & REQ_F_ISREG)) | |
3981 | goto copy_iov; | |
31b51510 | 3982 | |
607b6fb8 PB |
3983 | kiocb->ki_flags |= IOCB_NOWAIT; |
3984 | } else { | |
3985 | /* Ensure we clear previously set non-block flag */ | |
3986 | kiocb->ki_flags &= ~IOCB_NOWAIT; | |
3987 | } | |
31b51510 | 3988 | |
b4aec400 | 3989 | ppos = io_kiocb_update_pos(req); |
d34e1e5b | 3990 | |
b4aec400 | 3991 | ret = rw_verify_area(WRITE, req->file, ppos, req->result); |
fa15bafb PB |
3992 | if (unlikely(ret)) |
3993 | goto out_free; | |
4ed734b0 | 3994 | |
fa15bafb PB |
3995 | /* |
3996 | * Open-code file_start_write here to grab freeze protection, | |
3997 | * which will be released by another thread in | |
3998 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
3999 | * released so that it doesn't complain about the held lock when | |
4000 | * we return to userspace. | |
4001 | */ | |
4002 | if (req->flags & REQ_F_ISREG) { | |
8a3c84b6 | 4003 | sb_start_write(file_inode(req->file)->i_sb); |
fa15bafb PB |
4004 | __sb_writers_release(file_inode(req->file)->i_sb, |
4005 | SB_FREEZE_WRITE); | |
4006 | } | |
4007 | kiocb->ki_flags |= IOCB_WRITE; | |
4ed734b0 | 4008 | |
35645ac3 | 4009 | if (likely(req->file->f_op->write_iter)) |
c88598a9 | 4010 | ret2 = call_write_iter(req->file, kiocb, &s->iter); |
2dd2111d | 4011 | else if (req->file->f_op->write) |
c88598a9 | 4012 | ret2 = loop_rw_iter(WRITE, req, &s->iter); |
2dd2111d GH |
4013 | else |
4014 | ret2 = -EINVAL; | |
4ed734b0 | 4015 | |
6ad7f233 PB |
4016 | if (req->flags & REQ_F_REISSUE) { |
4017 | req->flags &= ~REQ_F_REISSUE; | |
230d50d4 | 4018 | ret2 = -EAGAIN; |
6ad7f233 | 4019 | } |
230d50d4 | 4020 | |
fa15bafb PB |
4021 | /* |
4022 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just | |
4023 | * retry them without IOCB_NOWAIT. | |
4024 | */ | |
4025 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) | |
4026 | ret2 = -EAGAIN; | |
75c668cd PB |
4027 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
4028 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) | |
355afaeb | 4029 | goto done; |
fa15bafb | 4030 | if (!force_nonblock || ret2 != -EAGAIN) { |
eefdf30f | 4031 | /* IOPOLL retry should happen for io-wq threads */ |
b10841c9 | 4032 | if (ret2 == -EAGAIN && (req->ctx->flags & IORING_SETUP_IOPOLL)) |
eefdf30f | 4033 | goto copy_iov; |
355afaeb | 4034 | done: |
2ea537ca | 4035 | kiocb_done(req, ret2, issue_flags); |
fa15bafb | 4036 | } else { |
f67676d1 | 4037 | copy_iov: |
c88598a9 PB |
4038 | iov_iter_restore(&s->iter, &s->iter_state); |
4039 | ret = io_setup_async_rw(req, iovec, s, false); | |
6bf985dc | 4040 | return ret ?: -EAGAIN; |
2b188cc1 | 4041 | } |
31b51510 | 4042 | out_free: |
f261c168 | 4043 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 4044 | if (iovec) |
6f2cc166 | 4045 | kfree(iovec); |
2b188cc1 JA |
4046 | return ret; |
4047 | } | |
4048 | ||
80a261fd JA |
4049 | static int io_renameat_prep(struct io_kiocb *req, |
4050 | const struct io_uring_sqe *sqe) | |
4051 | { | |
4052 | struct io_rename *ren = &req->rename; | |
4053 | const char __user *oldf, *newf; | |
4054 | ||
ed7eb259 JA |
4055 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4056 | return -EINVAL; | |
26578cda | 4057 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
ed7eb259 | 4058 | return -EINVAL; |
80a261fd JA |
4059 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
4060 | return -EBADF; | |
4061 | ||
4062 | ren->old_dfd = READ_ONCE(sqe->fd); | |
4063 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4064 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
4065 | ren->new_dfd = READ_ONCE(sqe->len); | |
4066 | ren->flags = READ_ONCE(sqe->rename_flags); | |
4067 | ||
4068 | ren->oldpath = getname(oldf); | |
4069 | if (IS_ERR(ren->oldpath)) | |
4070 | return PTR_ERR(ren->oldpath); | |
4071 | ||
4072 | ren->newpath = getname(newf); | |
4073 | if (IS_ERR(ren->newpath)) { | |
4074 | putname(ren->oldpath); | |
4075 | return PTR_ERR(ren->newpath); | |
4076 | } | |
4077 | ||
4078 | req->flags |= REQ_F_NEED_CLEANUP; | |
4079 | return 0; | |
4080 | } | |
4081 | ||
45d189c6 | 4082 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
80a261fd JA |
4083 | { |
4084 | struct io_rename *ren = &req->rename; | |
4085 | int ret; | |
4086 | ||
45d189c6 | 4087 | if (issue_flags & IO_URING_F_NONBLOCK) |
80a261fd JA |
4088 | return -EAGAIN; |
4089 | ||
4090 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, | |
4091 | ren->newpath, ren->flags); | |
4092 | ||
4093 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4094 | if (ret < 0) | |
93d2bcd2 | 4095 | req_set_fail(req); |
80a261fd JA |
4096 | io_req_complete(req, ret); |
4097 | return 0; | |
4098 | } | |
4099 | ||
14a1143b JA |
4100 | static int io_unlinkat_prep(struct io_kiocb *req, |
4101 | const struct io_uring_sqe *sqe) | |
4102 | { | |
4103 | struct io_unlink *un = &req->unlink; | |
4104 | const char __user *fname; | |
4105 | ||
22634bc5 JA |
4106 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4107 | return -EINVAL; | |
26578cda PB |
4108 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
4109 | sqe->splice_fd_in) | |
22634bc5 | 4110 | return -EINVAL; |
14a1143b JA |
4111 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
4112 | return -EBADF; | |
4113 | ||
4114 | un->dfd = READ_ONCE(sqe->fd); | |
4115 | ||
4116 | un->flags = READ_ONCE(sqe->unlink_flags); | |
4117 | if (un->flags & ~AT_REMOVEDIR) | |
4118 | return -EINVAL; | |
4119 | ||
4120 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4121 | un->filename = getname(fname); | |
4122 | if (IS_ERR(un->filename)) | |
4123 | return PTR_ERR(un->filename); | |
4124 | ||
4125 | req->flags |= REQ_F_NEED_CLEANUP; | |
4126 | return 0; | |
4127 | } | |
4128 | ||
45d189c6 | 4129 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
14a1143b JA |
4130 | { |
4131 | struct io_unlink *un = &req->unlink; | |
4132 | int ret; | |
4133 | ||
45d189c6 | 4134 | if (issue_flags & IO_URING_F_NONBLOCK) |
14a1143b JA |
4135 | return -EAGAIN; |
4136 | ||
4137 | if (un->flags & AT_REMOVEDIR) | |
4138 | ret = do_rmdir(un->dfd, un->filename); | |
4139 | else | |
4140 | ret = do_unlinkat(un->dfd, un->filename); | |
4141 | ||
4142 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4143 | if (ret < 0) | |
93d2bcd2 | 4144 | req_set_fail(req); |
14a1143b JA |
4145 | io_req_complete(req, ret); |
4146 | return 0; | |
4147 | } | |
4148 | ||
e34a02dc DK |
4149 | static int io_mkdirat_prep(struct io_kiocb *req, |
4150 | const struct io_uring_sqe *sqe) | |
4151 | { | |
4152 | struct io_mkdir *mkd = &req->mkdir; | |
4153 | const char __user *fname; | |
4154 | ||
4155 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
4156 | return -EINVAL; | |
4157 | if (sqe->ioprio || sqe->off || sqe->rw_flags || sqe->buf_index || | |
4158 | sqe->splice_fd_in) | |
4159 | return -EINVAL; | |
4160 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
4161 | return -EBADF; | |
4162 | ||
4163 | mkd->dfd = READ_ONCE(sqe->fd); | |
4164 | mkd->mode = READ_ONCE(sqe->len); | |
4165 | ||
4166 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4167 | mkd->filename = getname(fname); | |
4168 | if (IS_ERR(mkd->filename)) | |
4169 | return PTR_ERR(mkd->filename); | |
4170 | ||
4171 | req->flags |= REQ_F_NEED_CLEANUP; | |
4172 | return 0; | |
4173 | } | |
4174 | ||
04f34081 | 4175 | static int io_mkdirat(struct io_kiocb *req, unsigned int issue_flags) |
e34a02dc DK |
4176 | { |
4177 | struct io_mkdir *mkd = &req->mkdir; | |
4178 | int ret; | |
4179 | ||
4180 | if (issue_flags & IO_URING_F_NONBLOCK) | |
4181 | return -EAGAIN; | |
4182 | ||
4183 | ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode); | |
4184 | ||
4185 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4186 | if (ret < 0) | |
4187 | req_set_fail(req); | |
4188 | io_req_complete(req, ret); | |
4189 | return 0; | |
4190 | } | |
4191 | ||
7a8721f8 DK |
4192 | static int io_symlinkat_prep(struct io_kiocb *req, |
4193 | const struct io_uring_sqe *sqe) | |
4194 | { | |
4195 | struct io_symlink *sl = &req->symlink; | |
4196 | const char __user *oldpath, *newpath; | |
4197 | ||
4198 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
4199 | return -EINVAL; | |
4200 | if (sqe->ioprio || sqe->len || sqe->rw_flags || sqe->buf_index || | |
4201 | sqe->splice_fd_in) | |
4202 | return -EINVAL; | |
4203 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
4204 | return -EBADF; | |
4205 | ||
4206 | sl->new_dfd = READ_ONCE(sqe->fd); | |
4207 | oldpath = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4208 | newpath = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
4209 | ||
4210 | sl->oldpath = getname(oldpath); | |
4211 | if (IS_ERR(sl->oldpath)) | |
4212 | return PTR_ERR(sl->oldpath); | |
4213 | ||
4214 | sl->newpath = getname(newpath); | |
4215 | if (IS_ERR(sl->newpath)) { | |
4216 | putname(sl->oldpath); | |
4217 | return PTR_ERR(sl->newpath); | |
4218 | } | |
4219 | ||
4220 | req->flags |= REQ_F_NEED_CLEANUP; | |
4221 | return 0; | |
4222 | } | |
4223 | ||
04f34081 | 4224 | static int io_symlinkat(struct io_kiocb *req, unsigned int issue_flags) |
7a8721f8 DK |
4225 | { |
4226 | struct io_symlink *sl = &req->symlink; | |
4227 | int ret; | |
4228 | ||
4229 | if (issue_flags & IO_URING_F_NONBLOCK) | |
4230 | return -EAGAIN; | |
4231 | ||
4232 | ret = do_symlinkat(sl->oldpath, sl->new_dfd, sl->newpath); | |
4233 | ||
4234 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4235 | if (ret < 0) | |
4236 | req_set_fail(req); | |
4237 | io_req_complete(req, ret); | |
4238 | return 0; | |
4239 | } | |
4240 | ||
cf30da90 DK |
4241 | static int io_linkat_prep(struct io_kiocb *req, |
4242 | const struct io_uring_sqe *sqe) | |
4243 | { | |
4244 | struct io_hardlink *lnk = &req->hardlink; | |
4245 | const char __user *oldf, *newf; | |
4246 | ||
4247 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
4248 | return -EINVAL; | |
4249 | if (sqe->ioprio || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in) | |
4250 | return -EINVAL; | |
4251 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
4252 | return -EBADF; | |
4253 | ||
4254 | lnk->old_dfd = READ_ONCE(sqe->fd); | |
4255 | lnk->new_dfd = READ_ONCE(sqe->len); | |
4256 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4257 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
4258 | lnk->flags = READ_ONCE(sqe->hardlink_flags); | |
4259 | ||
4260 | lnk->oldpath = getname(oldf); | |
4261 | if (IS_ERR(lnk->oldpath)) | |
4262 | return PTR_ERR(lnk->oldpath); | |
4263 | ||
4264 | lnk->newpath = getname(newf); | |
4265 | if (IS_ERR(lnk->newpath)) { | |
4266 | putname(lnk->oldpath); | |
4267 | return PTR_ERR(lnk->newpath); | |
4268 | } | |
4269 | ||
4270 | req->flags |= REQ_F_NEED_CLEANUP; | |
4271 | return 0; | |
4272 | } | |
4273 | ||
04f34081 | 4274 | static int io_linkat(struct io_kiocb *req, unsigned int issue_flags) |
cf30da90 DK |
4275 | { |
4276 | struct io_hardlink *lnk = &req->hardlink; | |
4277 | int ret; | |
4278 | ||
4279 | if (issue_flags & IO_URING_F_NONBLOCK) | |
4280 | return -EAGAIN; | |
4281 | ||
4282 | ret = do_linkat(lnk->old_dfd, lnk->oldpath, lnk->new_dfd, | |
4283 | lnk->newpath, lnk->flags); | |
4284 | ||
4285 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4286 | if (ret < 0) | |
4287 | req_set_fail(req); | |
4288 | io_req_complete(req, ret); | |
4289 | return 0; | |
4290 | } | |
4291 | ||
36f4fa68 JA |
4292 | static int io_shutdown_prep(struct io_kiocb *req, |
4293 | const struct io_uring_sqe *sqe) | |
4294 | { | |
4295 | #if defined(CONFIG_NET) | |
4296 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
4297 | return -EINVAL; | |
26578cda PB |
4298 | if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || |
4299 | sqe->buf_index || sqe->splice_fd_in)) | |
36f4fa68 JA |
4300 | return -EINVAL; |
4301 | ||
4302 | req->shutdown.how = READ_ONCE(sqe->len); | |
4303 | return 0; | |
4304 | #else | |
4305 | return -EOPNOTSUPP; | |
4306 | #endif | |
4307 | } | |
4308 | ||
45d189c6 | 4309 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
36f4fa68 JA |
4310 | { |
4311 | #if defined(CONFIG_NET) | |
4312 | struct socket *sock; | |
4313 | int ret; | |
4314 | ||
45d189c6 | 4315 | if (issue_flags & IO_URING_F_NONBLOCK) |
36f4fa68 JA |
4316 | return -EAGAIN; |
4317 | ||
48aba79b | 4318 | sock = sock_from_file(req->file); |
36f4fa68 | 4319 | if (unlikely(!sock)) |
48aba79b | 4320 | return -ENOTSOCK; |
36f4fa68 JA |
4321 | |
4322 | ret = __sys_shutdown_sock(sock, req->shutdown.how); | |
a146468d | 4323 | if (ret < 0) |
93d2bcd2 | 4324 | req_set_fail(req); |
36f4fa68 JA |
4325 | io_req_complete(req, ret); |
4326 | return 0; | |
4327 | #else | |
4328 | return -EOPNOTSUPP; | |
4329 | #endif | |
4330 | } | |
4331 | ||
f2a8d5c7 PB |
4332 | static int __io_splice_prep(struct io_kiocb *req, |
4333 | const struct io_uring_sqe *sqe) | |
7d67af2c | 4334 | { |
fe7e3257 | 4335 | struct io_splice *sp = &req->splice; |
7d67af2c | 4336 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
7d67af2c | 4337 | |
3232dd02 PB |
4338 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4339 | return -EINVAL; | |
7d67af2c | 4340 | |
7d67af2c PB |
4341 | sp->len = READ_ONCE(sqe->len); |
4342 | sp->flags = READ_ONCE(sqe->splice_flags); | |
7d67af2c PB |
4343 | if (unlikely(sp->flags & ~valid_flags)) |
4344 | return -EINVAL; | |
a3e4bc23 | 4345 | sp->splice_fd_in = READ_ONCE(sqe->splice_fd_in); |
7d67af2c PB |
4346 | return 0; |
4347 | } | |
4348 | ||
f2a8d5c7 PB |
4349 | static int io_tee_prep(struct io_kiocb *req, |
4350 | const struct io_uring_sqe *sqe) | |
4351 | { | |
4352 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) | |
4353 | return -EINVAL; | |
4354 | return __io_splice_prep(req, sqe); | |
4355 | } | |
4356 | ||
45d189c6 | 4357 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
f2a8d5c7 PB |
4358 | { |
4359 | struct io_splice *sp = &req->splice; | |
f2a8d5c7 PB |
4360 | struct file *out = sp->file_out; |
4361 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
a3e4bc23 | 4362 | struct file *in; |
f2a8d5c7 PB |
4363 | long ret = 0; |
4364 | ||
45d189c6 | 4365 | if (issue_flags & IO_URING_F_NONBLOCK) |
f2a8d5c7 | 4366 | return -EAGAIN; |
a3e4bc23 | 4367 | |
5106dd6e | 4368 | if (sp->flags & SPLICE_F_FD_IN_FIXED) |
e9419766 | 4369 | in = io_file_get_fixed(req, sp->splice_fd_in, issue_flags); |
5106dd6e JA |
4370 | else |
4371 | in = io_file_get_normal(req, sp->splice_fd_in); | |
a3e4bc23 JA |
4372 | if (!in) { |
4373 | ret = -EBADF; | |
4374 | goto done; | |
4375 | } | |
4376 | ||
f2a8d5c7 PB |
4377 | if (sp->len) |
4378 | ret = do_tee(in, out, sp->len, flags); | |
4379 | ||
e1d767f0 PB |
4380 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
4381 | io_put_file(in); | |
a3e4bc23 | 4382 | done: |
f2a8d5c7 | 4383 | if (ret != sp->len) |
93d2bcd2 | 4384 | req_set_fail(req); |
e1e16097 | 4385 | io_req_complete(req, ret); |
f2a8d5c7 PB |
4386 | return 0; |
4387 | } | |
4388 | ||
4389 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4390 | { | |
fe7e3257 | 4391 | struct io_splice *sp = &req->splice; |
f2a8d5c7 PB |
4392 | |
4393 | sp->off_in = READ_ONCE(sqe->splice_off_in); | |
4394 | sp->off_out = READ_ONCE(sqe->off); | |
4395 | return __io_splice_prep(req, sqe); | |
4396 | } | |
4397 | ||
45d189c6 | 4398 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
7d67af2c PB |
4399 | { |
4400 | struct io_splice *sp = &req->splice; | |
7d67af2c PB |
4401 | struct file *out = sp->file_out; |
4402 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
4403 | loff_t *poff_in, *poff_out; | |
a3e4bc23 | 4404 | struct file *in; |
c9687426 | 4405 | long ret = 0; |
7d67af2c | 4406 | |
45d189c6 | 4407 | if (issue_flags & IO_URING_F_NONBLOCK) |
2fb3e822 | 4408 | return -EAGAIN; |
7d67af2c | 4409 | |
5106dd6e | 4410 | if (sp->flags & SPLICE_F_FD_IN_FIXED) |
e9419766 | 4411 | in = io_file_get_fixed(req, sp->splice_fd_in, issue_flags); |
5106dd6e JA |
4412 | else |
4413 | in = io_file_get_normal(req, sp->splice_fd_in); | |
a3e4bc23 JA |
4414 | if (!in) { |
4415 | ret = -EBADF; | |
4416 | goto done; | |
4417 | } | |
4418 | ||
7d67af2c PB |
4419 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; |
4420 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; | |
c9687426 | 4421 | |
948a7749 | 4422 | if (sp->len) |
c9687426 | 4423 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
7d67af2c | 4424 | |
e1d767f0 PB |
4425 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
4426 | io_put_file(in); | |
a3e4bc23 | 4427 | done: |
7d67af2c | 4428 | if (ret != sp->len) |
93d2bcd2 | 4429 | req_set_fail(req); |
e1e16097 | 4430 | io_req_complete(req, ret); |
7d67af2c PB |
4431 | return 0; |
4432 | } | |
4433 | ||
2b188cc1 JA |
4434 | /* |
4435 | * IORING_OP_NOP just posts a completion event, nothing else. | |
4436 | */ | |
889fca73 | 4437 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
4438 | { |
4439 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 4440 | |
def596e9 JA |
4441 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4442 | return -EINVAL; | |
4443 | ||
889fca73 | 4444 | __io_req_complete(req, issue_flags, 0, 0); |
2b188cc1 JA |
4445 | return 0; |
4446 | } | |
4447 | ||
4f57f06c JA |
4448 | static int io_msg_ring_prep(struct io_kiocb *req, |
4449 | const struct io_uring_sqe *sqe) | |
4450 | { | |
f3b6a41e JA |
4451 | if (unlikely(sqe->addr || sqe->ioprio || sqe->rw_flags || |
4452 | sqe->splice_fd_in || sqe->buf_index || sqe->personality)) | |
4f57f06c JA |
4453 | return -EINVAL; |
4454 | ||
4f57f06c JA |
4455 | req->msg.user_data = READ_ONCE(sqe->off); |
4456 | req->msg.len = READ_ONCE(sqe->len); | |
4457 | return 0; | |
4458 | } | |
4459 | ||
4460 | static int io_msg_ring(struct io_kiocb *req, unsigned int issue_flags) | |
4461 | { | |
4462 | struct io_ring_ctx *target_ctx; | |
4463 | struct io_msg *msg = &req->msg; | |
4f57f06c | 4464 | bool filled; |
3f1d52ab | 4465 | int ret; |
4f57f06c | 4466 | |
3f1d52ab JA |
4467 | ret = -EBADFD; |
4468 | if (req->file->f_op != &io_uring_fops) | |
4469 | goto done; | |
4f57f06c | 4470 | |
3f1d52ab | 4471 | ret = -EOVERFLOW; |
4f57f06c JA |
4472 | target_ctx = req->file->private_data; |
4473 | ||
4474 | spin_lock(&target_ctx->completion_lock); | |
7ef66d18 | 4475 | filled = io_fill_cqe_aux(target_ctx, msg->user_data, msg->len, 0); |
4f57f06c JA |
4476 | io_commit_cqring(target_ctx); |
4477 | spin_unlock(&target_ctx->completion_lock); | |
4478 | ||
4479 | if (filled) { | |
4480 | io_cqring_ev_posted(target_ctx); | |
4481 | ret = 0; | |
4482 | } | |
4483 | ||
3f1d52ab | 4484 | done: |
9666d420 JA |
4485 | if (ret < 0) |
4486 | req_set_fail(req); | |
4f57f06c JA |
4487 | __io_req_complete(req, issue_flags, ret, 0); |
4488 | return 0; | |
4489 | } | |
4490 | ||
1155c76a | 4491 | static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 4492 | { |
6b06314c | 4493 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 4494 | |
6b06314c | 4495 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 4496 | return -EINVAL; |
26578cda PB |
4497 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
4498 | sqe->splice_fd_in)) | |
c992fe29 CH |
4499 | return -EINVAL; |
4500 | ||
8ed8d3c3 JA |
4501 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
4502 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
4503 | return -EINVAL; | |
4504 | ||
4505 | req->sync.off = READ_ONCE(sqe->off); | |
4506 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
4507 | return 0; |
4508 | } | |
4509 | ||
45d189c6 | 4510 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4511 | { |
8ed8d3c3 | 4512 | loff_t end = req->sync.off + req->sync.len; |
8ed8d3c3 JA |
4513 | int ret; |
4514 | ||
ac45abc0 | 4515 | /* fsync always requires a blocking context */ |
45d189c6 | 4516 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4517 | return -EAGAIN; |
4518 | ||
9adbd45d | 4519 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
4520 | end > 0 ? end : LLONG_MAX, |
4521 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
4522 | if (ret < 0) | |
93d2bcd2 | 4523 | req_set_fail(req); |
e1e16097 | 4524 | io_req_complete(req, ret); |
c992fe29 CH |
4525 | return 0; |
4526 | } | |
4527 | ||
d63d1b5e JA |
4528 | static int io_fallocate_prep(struct io_kiocb *req, |
4529 | const struct io_uring_sqe *sqe) | |
4530 | { | |
26578cda PB |
4531 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags || |
4532 | sqe->splice_fd_in) | |
d63d1b5e | 4533 | return -EINVAL; |
3232dd02 PB |
4534 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4535 | return -EINVAL; | |
d63d1b5e JA |
4536 | |
4537 | req->sync.off = READ_ONCE(sqe->off); | |
4538 | req->sync.len = READ_ONCE(sqe->addr); | |
4539 | req->sync.mode = READ_ONCE(sqe->len); | |
4540 | return 0; | |
4541 | } | |
4542 | ||
45d189c6 | 4543 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
5d17b4a4 | 4544 | { |
ac45abc0 PB |
4545 | int ret; |
4546 | ||
d63d1b5e | 4547 | /* fallocate always requiring blocking context */ |
45d189c6 | 4548 | if (issue_flags & IO_URING_F_NONBLOCK) |
5d17b4a4 | 4549 | return -EAGAIN; |
ac45abc0 PB |
4550 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
4551 | req->sync.len); | |
ac45abc0 | 4552 | if (ret < 0) |
93d2bcd2 | 4553 | req_set_fail(req); |
f63cf519 JA |
4554 | else |
4555 | fsnotify_modify(req->file); | |
e1e16097 | 4556 | io_req_complete(req, ret); |
5d17b4a4 JA |
4557 | return 0; |
4558 | } | |
4559 | ||
ec65fea5 | 4560 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 4561 | { |
f8748881 | 4562 | const char __user *fname; |
15b71abe | 4563 | int ret; |
b7bb4f7d | 4564 | |
d3fddf6d PB |
4565 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4566 | return -EINVAL; | |
b9445598 | 4567 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
15b71abe | 4568 | return -EINVAL; |
ec65fea5 | 4569 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
cf3040ca | 4570 | return -EBADF; |
03b1230c | 4571 | |
ec65fea5 PB |
4572 | /* open.how should be already initialised */ |
4573 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
08a1d26e | 4574 | req->open.how.flags |= O_LARGEFILE; |
3529d8c2 | 4575 | |
25e72d10 PB |
4576 | req->open.dfd = READ_ONCE(sqe->fd); |
4577 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
f8748881 | 4578 | req->open.filename = getname(fname); |
15b71abe JA |
4579 | if (IS_ERR(req->open.filename)) { |
4580 | ret = PTR_ERR(req->open.filename); | |
4581 | req->open.filename = NULL; | |
4582 | return ret; | |
4583 | } | |
b9445598 PB |
4584 | |
4585 | req->open.file_slot = READ_ONCE(sqe->file_index); | |
4586 | if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC)) | |
4587 | return -EINVAL; | |
4588 | ||
4022e7af | 4589 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
8fef80bf | 4590 | req->flags |= REQ_F_NEED_CLEANUP; |
15b71abe | 4591 | return 0; |
03b1230c JA |
4592 | } |
4593 | ||
ec65fea5 PB |
4594 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4595 | { | |
d3fddf6d PB |
4596 | u64 mode = READ_ONCE(sqe->len); |
4597 | u64 flags = READ_ONCE(sqe->open_flags); | |
ec65fea5 | 4598 | |
ec65fea5 PB |
4599 | req->open.how = build_open_how(flags, mode); |
4600 | return __io_openat_prep(req, sqe); | |
4601 | } | |
4602 | ||
cebdb986 | 4603 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 4604 | { |
cebdb986 | 4605 | struct open_how __user *how; |
cebdb986 | 4606 | size_t len; |
0fa03c62 JA |
4607 | int ret; |
4608 | ||
cebdb986 JA |
4609 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4610 | len = READ_ONCE(sqe->len); | |
cebdb986 JA |
4611 | if (len < OPEN_HOW_SIZE_VER0) |
4612 | return -EINVAL; | |
3529d8c2 | 4613 | |
cebdb986 JA |
4614 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
4615 | len); | |
4616 | if (ret) | |
4617 | return ret; | |
3529d8c2 | 4618 | |
ec65fea5 | 4619 | return __io_openat_prep(req, sqe); |
cebdb986 JA |
4620 | } |
4621 | ||
45d189c6 | 4622 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
15b71abe JA |
4623 | { |
4624 | struct open_flags op; | |
15b71abe | 4625 | struct file *file; |
b9445598 PB |
4626 | bool resolve_nonblock, nonblock_set; |
4627 | bool fixed = !!req->open.file_slot; | |
15b71abe JA |
4628 | int ret; |
4629 | ||
cebdb986 | 4630 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
4631 | if (ret) |
4632 | goto err; | |
3a81fd02 JA |
4633 | nonblock_set = op.open_flag & O_NONBLOCK; |
4634 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; | |
45d189c6 | 4635 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3a81fd02 JA |
4636 | /* |
4637 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, | |
4638 | * it'll always -EAGAIN | |
4639 | */ | |
4640 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) | |
4641 | return -EAGAIN; | |
4642 | op.lookup_flags |= LOOKUP_CACHED; | |
4643 | op.open_flag |= O_NONBLOCK; | |
4644 | } | |
15b71abe | 4645 | |
b9445598 PB |
4646 | if (!fixed) { |
4647 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); | |
4648 | if (ret < 0) | |
4649 | goto err; | |
4650 | } | |
15b71abe JA |
4651 | |
4652 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
12dcb58a | 4653 | if (IS_ERR(file)) { |
944d1444 | 4654 | /* |
12dcb58a PB |
4655 | * We could hang on to this 'fd' on retrying, but seems like |
4656 | * marginal gain for something that is now known to be a slower | |
4657 | * path. So just put it, and we'll get a new one when we retry. | |
944d1444 | 4658 | */ |
b9445598 PB |
4659 | if (!fixed) |
4660 | put_unused_fd(ret); | |
3a81fd02 | 4661 | |
15b71abe | 4662 | ret = PTR_ERR(file); |
12dcb58a PB |
4663 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
4664 | if (ret == -EAGAIN && | |
4665 | (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK))) | |
4666 | return -EAGAIN; | |
4667 | goto err; | |
15b71abe | 4668 | } |
12dcb58a PB |
4669 | |
4670 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) | |
4671 | file->f_flags &= ~O_NONBLOCK; | |
4672 | fsnotify_open(file); | |
b9445598 PB |
4673 | |
4674 | if (!fixed) | |
4675 | fd_install(ret, file); | |
4676 | else | |
4677 | ret = io_install_fixed_file(req, file, issue_flags, | |
4678 | req->open.file_slot - 1); | |
15b71abe JA |
4679 | err: |
4680 | putname(req->open.filename); | |
8fef80bf | 4681 | req->flags &= ~REQ_F_NEED_CLEANUP; |
15b71abe | 4682 | if (ret < 0) |
93d2bcd2 | 4683 | req_set_fail(req); |
0bdf3398 | 4684 | __io_req_complete(req, issue_flags, ret, 0); |
15b71abe JA |
4685 | return 0; |
4686 | } | |
4687 | ||
45d189c6 | 4688 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
cebdb986 | 4689 | { |
e45cff58 | 4690 | return io_openat2(req, issue_flags); |
cebdb986 JA |
4691 | } |
4692 | ||
067524e9 JA |
4693 | static int io_remove_buffers_prep(struct io_kiocb *req, |
4694 | const struct io_uring_sqe *sqe) | |
4695 | { | |
4696 | struct io_provide_buf *p = &req->pbuf; | |
4697 | u64 tmp; | |
4698 | ||
26578cda PB |
4699 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off || |
4700 | sqe->splice_fd_in) | |
067524e9 JA |
4701 | return -EINVAL; |
4702 | ||
4703 | tmp = READ_ONCE(sqe->fd); | |
4704 | if (!tmp || tmp > USHRT_MAX) | |
4705 | return -EINVAL; | |
4706 | ||
4707 | memset(p, 0, sizeof(*p)); | |
4708 | p->nbufs = tmp; | |
4709 | p->bgid = READ_ONCE(sqe->buf_group); | |
4710 | return 0; | |
4711 | } | |
4712 | ||
dbc7d452 JA |
4713 | static int __io_remove_buffers(struct io_ring_ctx *ctx, |
4714 | struct io_buffer_list *bl, unsigned nbufs) | |
067524e9 JA |
4715 | { |
4716 | unsigned i = 0; | |
4717 | ||
4718 | /* shouldn't happen */ | |
4719 | if (!nbufs) | |
4720 | return 0; | |
4721 | ||
4722 | /* the head kbuf is the list itself */ | |
dbc7d452 | 4723 | while (!list_empty(&bl->buf_list)) { |
067524e9 JA |
4724 | struct io_buffer *nxt; |
4725 | ||
dbc7d452 | 4726 | nxt = list_first_entry(&bl->buf_list, struct io_buffer, list); |
067524e9 | 4727 | list_del(&nxt->list); |
067524e9 JA |
4728 | if (++i == nbufs) |
4729 | return i; | |
1d0254e6 | 4730 | cond_resched(); |
067524e9 JA |
4731 | } |
4732 | i++; | |
067524e9 JA |
4733 | |
4734 | return i; | |
4735 | } | |
4736 | ||
889fca73 | 4737 | static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags) |
067524e9 JA |
4738 | { |
4739 | struct io_provide_buf *p = &req->pbuf; | |
4740 | struct io_ring_ctx *ctx = req->ctx; | |
dbc7d452 | 4741 | struct io_buffer_list *bl; |
067524e9 | 4742 | int ret = 0; |
3b44b371 | 4743 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
067524e9 | 4744 | |
3b44b371 | 4745 | io_ring_submit_lock(ctx, needs_lock); |
067524e9 JA |
4746 | |
4747 | lockdep_assert_held(&ctx->uring_lock); | |
4748 | ||
4749 | ret = -ENOENT; | |
dbc7d452 JA |
4750 | bl = io_buffer_get_list(ctx, p->bgid); |
4751 | if (bl) | |
4752 | ret = __io_remove_buffers(ctx, bl, p->nbufs); | |
067524e9 | 4753 | if (ret < 0) |
93d2bcd2 | 4754 | req_set_fail(req); |
067524e9 | 4755 | |
9fb8cb49 PB |
4756 | /* complete before unlock, IOPOLL may need the lock */ |
4757 | __io_req_complete(req, issue_flags, ret, 0); | |
3b44b371 | 4758 | io_ring_submit_unlock(ctx, needs_lock); |
067524e9 JA |
4759 | return 0; |
4760 | } | |
4761 | ||
ddf0322d JA |
4762 | static int io_provide_buffers_prep(struct io_kiocb *req, |
4763 | const struct io_uring_sqe *sqe) | |
4764 | { | |
38134ada | 4765 | unsigned long size, tmp_check; |
ddf0322d JA |
4766 | struct io_provide_buf *p = &req->pbuf; |
4767 | u64 tmp; | |
4768 | ||
26578cda | 4769 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
ddf0322d JA |
4770 | return -EINVAL; |
4771 | ||
4772 | tmp = READ_ONCE(sqe->fd); | |
4773 | if (!tmp || tmp > USHRT_MAX) | |
4774 | return -E2BIG; | |
4775 | p->nbufs = tmp; | |
4776 | p->addr = READ_ONCE(sqe->addr); | |
4777 | p->len = READ_ONCE(sqe->len); | |
4778 | ||
38134ada PB |
4779 | if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs, |
4780 | &size)) | |
4781 | return -EOVERFLOW; | |
4782 | if (check_add_overflow((unsigned long)p->addr, size, &tmp_check)) | |
4783 | return -EOVERFLOW; | |
4784 | ||
d81269fe PB |
4785 | size = (unsigned long)p->len * p->nbufs; |
4786 | if (!access_ok(u64_to_user_ptr(p->addr), size)) | |
ddf0322d JA |
4787 | return -EFAULT; |
4788 | ||
4789 | p->bgid = READ_ONCE(sqe->buf_group); | |
4790 | tmp = READ_ONCE(sqe->off); | |
4791 | if (tmp > USHRT_MAX) | |
4792 | return -E2BIG; | |
4793 | p->bid = tmp; | |
4794 | return 0; | |
4795 | } | |
4796 | ||
cc3cec83 JA |
4797 | static int io_refill_buffer_cache(struct io_ring_ctx *ctx) |
4798 | { | |
4799 | struct io_buffer *buf; | |
4800 | struct page *page; | |
4801 | int bufs_in_page; | |
4802 | ||
4803 | /* | |
4804 | * Completions that don't happen inline (eg not under uring_lock) will | |
4805 | * add to ->io_buffers_comp. If we don't have any free buffers, check | |
4806 | * the completion list and splice those entries first. | |
4807 | */ | |
4808 | if (!list_empty_careful(&ctx->io_buffers_comp)) { | |
4809 | spin_lock(&ctx->completion_lock); | |
4810 | if (!list_empty(&ctx->io_buffers_comp)) { | |
4811 | list_splice_init(&ctx->io_buffers_comp, | |
4812 | &ctx->io_buffers_cache); | |
4813 | spin_unlock(&ctx->completion_lock); | |
4814 | return 0; | |
4815 | } | |
4816 | spin_unlock(&ctx->completion_lock); | |
4817 | } | |
4818 | ||
4819 | /* | |
4820 | * No free buffers and no completion entries either. Allocate a new | |
4821 | * page worth of buffer entries and add those to our freelist. | |
4822 | */ | |
4823 | page = alloc_page(GFP_KERNEL_ACCOUNT); | |
4824 | if (!page) | |
4825 | return -ENOMEM; | |
4826 | ||
4827 | list_add(&page->lru, &ctx->io_buffers_pages); | |
4828 | ||
4829 | buf = page_address(page); | |
4830 | bufs_in_page = PAGE_SIZE / sizeof(*buf); | |
4831 | while (bufs_in_page) { | |
4832 | list_add_tail(&buf->list, &ctx->io_buffers_cache); | |
4833 | buf++; | |
4834 | bufs_in_page--; | |
4835 | } | |
4836 | ||
4837 | return 0; | |
4838 | } | |
4839 | ||
4840 | static int io_add_buffers(struct io_ring_ctx *ctx, struct io_provide_buf *pbuf, | |
dbc7d452 | 4841 | struct io_buffer_list *bl) |
ddf0322d JA |
4842 | { |
4843 | struct io_buffer *buf; | |
4844 | u64 addr = pbuf->addr; | |
4845 | int i, bid = pbuf->bid; | |
4846 | ||
4847 | for (i = 0; i < pbuf->nbufs; i++) { | |
cc3cec83 JA |
4848 | if (list_empty(&ctx->io_buffers_cache) && |
4849 | io_refill_buffer_cache(ctx)) | |
ddf0322d | 4850 | break; |
cc3cec83 JA |
4851 | buf = list_first_entry(&ctx->io_buffers_cache, struct io_buffer, |
4852 | list); | |
dbc7d452 | 4853 | list_move_tail(&buf->list, &bl->buf_list); |
ddf0322d | 4854 | buf->addr = addr; |
d1f82808 | 4855 | buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT); |
ddf0322d | 4856 | buf->bid = bid; |
b1c62645 | 4857 | buf->bgid = pbuf->bgid; |
ddf0322d JA |
4858 | addr += pbuf->len; |
4859 | bid++; | |
f240762f | 4860 | cond_resched(); |
ddf0322d JA |
4861 | } |
4862 | ||
dbc7d452 | 4863 | return i ? 0 : -ENOMEM; |
ddf0322d JA |
4864 | } |
4865 | ||
889fca73 | 4866 | static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags) |
ddf0322d JA |
4867 | { |
4868 | struct io_provide_buf *p = &req->pbuf; | |
4869 | struct io_ring_ctx *ctx = req->ctx; | |
dbc7d452 | 4870 | struct io_buffer_list *bl; |
ddf0322d | 4871 | int ret = 0; |
3b44b371 | 4872 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
ddf0322d | 4873 | |
3b44b371 | 4874 | io_ring_submit_lock(ctx, needs_lock); |
ddf0322d JA |
4875 | |
4876 | lockdep_assert_held(&ctx->uring_lock); | |
4877 | ||
dbc7d452 JA |
4878 | bl = io_buffer_get_list(ctx, p->bgid); |
4879 | if (unlikely(!bl)) { | |
4880 | bl = kmalloc(sizeof(*bl), GFP_KERNEL); | |
4881 | if (!bl) { | |
4882 | ret = -ENOMEM; | |
4883 | goto err; | |
4884 | } | |
4885 | io_buffer_add_list(ctx, bl, p->bgid); | |
ddf0322d | 4886 | } |
dbc7d452 JA |
4887 | |
4888 | ret = io_add_buffers(ctx, p, bl); | |
4889 | err: | |
ddf0322d | 4890 | if (ret < 0) |
93d2bcd2 | 4891 | req_set_fail(req); |
9fb8cb49 PB |
4892 | /* complete before unlock, IOPOLL may need the lock */ |
4893 | __io_req_complete(req, issue_flags, ret, 0); | |
3b44b371 | 4894 | io_ring_submit_unlock(ctx, needs_lock); |
ddf0322d | 4895 | return 0; |
cebdb986 JA |
4896 | } |
4897 | ||
3e4827b0 JA |
4898 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
4899 | const struct io_uring_sqe *sqe) | |
4900 | { | |
4901 | #if defined(CONFIG_EPOLL) | |
26578cda | 4902 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
3e4827b0 | 4903 | return -EINVAL; |
2d74d042 | 4904 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4905 | return -EINVAL; |
3e4827b0 JA |
4906 | |
4907 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
4908 | req->epoll.op = READ_ONCE(sqe->len); | |
4909 | req->epoll.fd = READ_ONCE(sqe->off); | |
4910 | ||
4911 | if (ep_op_has_event(req->epoll.op)) { | |
4912 | struct epoll_event __user *ev; | |
4913 | ||
4914 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4915 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
4916 | return -EFAULT; | |
4917 | } | |
4918 | ||
4919 | return 0; | |
4920 | #else | |
4921 | return -EOPNOTSUPP; | |
4922 | #endif | |
4923 | } | |
4924 | ||
889fca73 | 4925 | static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags) |
3e4827b0 JA |
4926 | { |
4927 | #if defined(CONFIG_EPOLL) | |
4928 | struct io_epoll *ie = &req->epoll; | |
4929 | int ret; | |
45d189c6 | 4930 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
3e4827b0 JA |
4931 | |
4932 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
4933 | if (force_nonblock && ret == -EAGAIN) | |
4934 | return -EAGAIN; | |
4935 | ||
4936 | if (ret < 0) | |
93d2bcd2 | 4937 | req_set_fail(req); |
889fca73 | 4938 | __io_req_complete(req, issue_flags, ret, 0); |
3e4827b0 JA |
4939 | return 0; |
4940 | #else | |
4941 | return -EOPNOTSUPP; | |
4942 | #endif | |
4943 | } | |
4944 | ||
c1ca757b JA |
4945 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4946 | { | |
4947 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
26578cda | 4948 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in) |
c1ca757b | 4949 | return -EINVAL; |
3232dd02 PB |
4950 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4951 | return -EINVAL; | |
c1ca757b JA |
4952 | |
4953 | req->madvise.addr = READ_ONCE(sqe->addr); | |
4954 | req->madvise.len = READ_ONCE(sqe->len); | |
4955 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4956 | return 0; | |
4957 | #else | |
4958 | return -EOPNOTSUPP; | |
4959 | #endif | |
4960 | } | |
4961 | ||
45d189c6 | 4962 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
c1ca757b JA |
4963 | { |
4964 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4965 | struct io_madvise *ma = &req->madvise; | |
4966 | int ret; | |
4967 | ||
45d189c6 | 4968 | if (issue_flags & IO_URING_F_NONBLOCK) |
c1ca757b JA |
4969 | return -EAGAIN; |
4970 | ||
0726b01e | 4971 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
c1ca757b | 4972 | if (ret < 0) |
93d2bcd2 | 4973 | req_set_fail(req); |
e1e16097 | 4974 | io_req_complete(req, ret); |
c1ca757b JA |
4975 | return 0; |
4976 | #else | |
4977 | return -EOPNOTSUPP; | |
4978 | #endif | |
4979 | } | |
4980 | ||
4840e418 JA |
4981 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4982 | { | |
26578cda | 4983 | if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in) |
4840e418 | 4984 | return -EINVAL; |
3232dd02 PB |
4985 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4986 | return -EINVAL; | |
4840e418 JA |
4987 | |
4988 | req->fadvise.offset = READ_ONCE(sqe->off); | |
4989 | req->fadvise.len = READ_ONCE(sqe->len); | |
4990 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4991 | return 0; | |
4992 | } | |
4993 | ||
45d189c6 | 4994 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
4840e418 JA |
4995 | { |
4996 | struct io_fadvise *fa = &req->fadvise; | |
4997 | int ret; | |
4998 | ||
45d189c6 | 4999 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3e69426d JA |
5000 | switch (fa->advice) { |
5001 | case POSIX_FADV_NORMAL: | |
5002 | case POSIX_FADV_RANDOM: | |
5003 | case POSIX_FADV_SEQUENTIAL: | |
5004 | break; | |
5005 | default: | |
5006 | return -EAGAIN; | |
5007 | } | |
5008 | } | |
4840e418 JA |
5009 | |
5010 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
5011 | if (ret < 0) | |
93d2bcd2 | 5012 | req_set_fail(req); |
0bdf3398 | 5013 | __io_req_complete(req, issue_flags, ret, 0); |
4840e418 JA |
5014 | return 0; |
5015 | } | |
5016 | ||
eddc7ef5 JA |
5017 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5018 | { | |
1b6fe6e0 SR |
5019 | const char __user *path; |
5020 | ||
2d74d042 | 5021 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 5022 | return -EINVAL; |
26578cda | 5023 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
eddc7ef5 | 5024 | return -EINVAL; |
9c280f90 | 5025 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 5026 | return -EBADF; |
eddc7ef5 | 5027 | |
1d9e1288 BM |
5028 | req->statx.dfd = READ_ONCE(sqe->fd); |
5029 | req->statx.mask = READ_ONCE(sqe->len); | |
1b6fe6e0 | 5030 | path = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
1d9e1288 BM |
5031 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
5032 | req->statx.flags = READ_ONCE(sqe->statx_flags); | |
eddc7ef5 | 5033 | |
1b6fe6e0 SR |
5034 | req->statx.filename = getname_flags(path, |
5035 | getname_statx_lookup_flags(req->statx.flags), | |
5036 | NULL); | |
5037 | ||
5038 | if (IS_ERR(req->statx.filename)) { | |
5039 | int ret = PTR_ERR(req->statx.filename); | |
5040 | ||
5041 | req->statx.filename = NULL; | |
5042 | return ret; | |
5043 | } | |
5044 | ||
5045 | req->flags |= REQ_F_NEED_CLEANUP; | |
eddc7ef5 JA |
5046 | return 0; |
5047 | } | |
5048 | ||
45d189c6 | 5049 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
eddc7ef5 | 5050 | { |
1d9e1288 | 5051 | struct io_statx *ctx = &req->statx; |
eddc7ef5 JA |
5052 | int ret; |
5053 | ||
59d70013 | 5054 | if (issue_flags & IO_URING_F_NONBLOCK) |
eddc7ef5 JA |
5055 | return -EAGAIN; |
5056 | ||
e62753e4 BM |
5057 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
5058 | ctx->buffer); | |
eddc7ef5 | 5059 | |
eddc7ef5 | 5060 | if (ret < 0) |
93d2bcd2 | 5061 | req_set_fail(req); |
e1e16097 | 5062 | io_req_complete(req, ret); |
eddc7ef5 JA |
5063 | return 0; |
5064 | } | |
5065 | ||
b5dba59e JA |
5066 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5067 | { | |
14587a46 | 5068 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 5069 | return -EINVAL; |
b5dba59e | 5070 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
7df778be | 5071 | sqe->rw_flags || sqe->buf_index) |
b5dba59e | 5072 | return -EINVAL; |
9c280f90 | 5073 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 5074 | return -EBADF; |
b5dba59e JA |
5075 | |
5076 | req->close.fd = READ_ONCE(sqe->fd); | |
7df778be PB |
5077 | req->close.file_slot = READ_ONCE(sqe->file_index); |
5078 | if (req->close.file_slot && req->close.fd) | |
5079 | return -EINVAL; | |
5080 | ||
b5dba59e | 5081 | return 0; |
b5dba59e JA |
5082 | } |
5083 | ||
889fca73 | 5084 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
b5dba59e | 5085 | { |
9eac1904 | 5086 | struct files_struct *files = current->files; |
3af73b28 | 5087 | struct io_close *close = &req->close; |
9eac1904 | 5088 | struct fdtable *fdt; |
a1fde923 PB |
5089 | struct file *file = NULL; |
5090 | int ret = -EBADF; | |
b5dba59e | 5091 | |
7df778be PB |
5092 | if (req->close.file_slot) { |
5093 | ret = io_close_fixed(req, issue_flags); | |
5094 | goto err; | |
5095 | } | |
5096 | ||
9eac1904 JA |
5097 | spin_lock(&files->file_lock); |
5098 | fdt = files_fdtable(files); | |
5099 | if (close->fd >= fdt->max_fds) { | |
5100 | spin_unlock(&files->file_lock); | |
5101 | goto err; | |
5102 | } | |
5103 | file = fdt->fd[close->fd]; | |
a1fde923 | 5104 | if (!file || file->f_op == &io_uring_fops) { |
9eac1904 JA |
5105 | spin_unlock(&files->file_lock); |
5106 | file = NULL; | |
5107 | goto err; | |
3af73b28 | 5108 | } |
b5dba59e JA |
5109 | |
5110 | /* if the file has a flush method, be safe and punt to async */ | |
45d189c6 | 5111 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
9eac1904 | 5112 | spin_unlock(&files->file_lock); |
0bf0eefd | 5113 | return -EAGAIN; |
a2100672 | 5114 | } |
b5dba59e | 5115 | |
9eac1904 JA |
5116 | ret = __close_fd_get_file(close->fd, &file); |
5117 | spin_unlock(&files->file_lock); | |
5118 | if (ret < 0) { | |
5119 | if (ret == -ENOENT) | |
5120 | ret = -EBADF; | |
5121 | goto err; | |
5122 | } | |
5123 | ||
3af73b28 | 5124 | /* No ->flush() or already async, safely close from here */ |
9eac1904 JA |
5125 | ret = filp_close(file, current->files); |
5126 | err: | |
3af73b28 | 5127 | if (ret < 0) |
93d2bcd2 | 5128 | req_set_fail(req); |
9eac1904 JA |
5129 | if (file) |
5130 | fput(file); | |
889fca73 | 5131 | __io_req_complete(req, issue_flags, ret, 0); |
1a417f4e | 5132 | return 0; |
b5dba59e JA |
5133 | } |
5134 | ||
1155c76a | 5135 | static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
5136 | { |
5137 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 | 5138 | |
5d17b4a4 JA |
5139 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
5140 | return -EINVAL; | |
26578cda PB |
5141 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
5142 | sqe->splice_fd_in)) | |
5d17b4a4 JA |
5143 | return -EINVAL; |
5144 | ||
8ed8d3c3 JA |
5145 | req->sync.off = READ_ONCE(sqe->off); |
5146 | req->sync.len = READ_ONCE(sqe->len); | |
5147 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
5148 | return 0; |
5149 | } | |
5150 | ||
45d189c6 | 5151 | static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 5152 | { |
8ed8d3c3 JA |
5153 | int ret; |
5154 | ||
ac45abc0 | 5155 | /* sync_file_range always requires a blocking context */ |
45d189c6 | 5156 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
5157 | return -EAGAIN; |
5158 | ||
9adbd45d | 5159 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
5160 | req->sync.flags); |
5161 | if (ret < 0) | |
93d2bcd2 | 5162 | req_set_fail(req); |
e1e16097 | 5163 | io_req_complete(req, ret); |
5d17b4a4 JA |
5164 | return 0; |
5165 | } | |
5166 | ||
469956e8 | 5167 | #if defined(CONFIG_NET) |
02d27d89 PB |
5168 | static int io_setup_async_msg(struct io_kiocb *req, |
5169 | struct io_async_msghdr *kmsg) | |
5170 | { | |
e8c2bc1f JA |
5171 | struct io_async_msghdr *async_msg = req->async_data; |
5172 | ||
5173 | if (async_msg) | |
02d27d89 | 5174 | return -EAGAIN; |
e8c2bc1f | 5175 | if (io_alloc_async_data(req)) { |
257e84a5 | 5176 | kfree(kmsg->free_iov); |
02d27d89 PB |
5177 | return -ENOMEM; |
5178 | } | |
e8c2bc1f | 5179 | async_msg = req->async_data; |
02d27d89 | 5180 | req->flags |= REQ_F_NEED_CLEANUP; |
e8c2bc1f | 5181 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
2a780802 | 5182 | async_msg->msg.msg_name = &async_msg->addr; |
257e84a5 PB |
5183 | /* if were using fast_iov, set it to the new one */ |
5184 | if (!async_msg->free_iov) | |
5185 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; | |
5186 | ||
02d27d89 PB |
5187 | return -EAGAIN; |
5188 | } | |
5189 | ||
2ae523ed PB |
5190 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
5191 | struct io_async_msghdr *iomsg) | |
5192 | { | |
2ae523ed | 5193 | iomsg->msg.msg_name = &iomsg->addr; |
257e84a5 | 5194 | iomsg->free_iov = iomsg->fast_iov; |
2ae523ed | 5195 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
257e84a5 | 5196 | req->sr_msg.msg_flags, &iomsg->free_iov); |
2ae523ed PB |
5197 | } |
5198 | ||
93642ef8 PB |
5199 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
5200 | { | |
5201 | int ret; | |
5202 | ||
93642ef8 PB |
5203 | ret = io_sendmsg_copy_hdr(req, req->async_data); |
5204 | if (!ret) | |
5205 | req->flags |= REQ_F_NEED_CLEANUP; | |
5206 | return ret; | |
5207 | } | |
5208 | ||
3529d8c2 | 5209 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 5210 | { |
e47293fd | 5211 | struct io_sr_msg *sr = &req->sr_msg; |
03b1230c | 5212 | |
d2b6f48b PB |
5213 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5214 | return -EINVAL; | |
5215 | ||
270a5940 | 5216 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
fddaface | 5217 | sr->len = READ_ONCE(sqe->len); |
04411806 PB |
5218 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
5219 | if (sr->msg_flags & MSG_DONTWAIT) | |
5220 | req->flags |= REQ_F_NOWAIT; | |
3529d8c2 | 5221 | |
d8768362 JA |
5222 | #ifdef CONFIG_COMPAT |
5223 | if (req->ctx->compat) | |
5224 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
5225 | #endif | |
93642ef8 | 5226 | return 0; |
03b1230c JA |
5227 | } |
5228 | ||
889fca73 | 5229 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 5230 | { |
6b754c8b | 5231 | struct io_async_msghdr iomsg, *kmsg; |
0fa03c62 | 5232 | struct socket *sock; |
7a7cacba | 5233 | unsigned flags; |
0031275d | 5234 | int min_ret = 0; |
0fa03c62 JA |
5235 | int ret; |
5236 | ||
dba4a925 | 5237 | sock = sock_from_file(req->file); |
7a7cacba | 5238 | if (unlikely(!sock)) |
dba4a925 | 5239 | return -ENOTSOCK; |
3529d8c2 | 5240 | |
d886e185 PB |
5241 | if (req_has_async_data(req)) { |
5242 | kmsg = req->async_data; | |
5243 | } else { | |
7a7cacba PB |
5244 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
5245 | if (ret) | |
5246 | return ret; | |
5247 | kmsg = &iomsg; | |
0fa03c62 | 5248 | } |
0fa03c62 | 5249 | |
04411806 PB |
5250 | flags = req->sr_msg.msg_flags; |
5251 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 5252 | flags |= MSG_DONTWAIT; |
0031275d SM |
5253 | if (flags & MSG_WAITALL) |
5254 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
5255 | ||
7a7cacba | 5256 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
0fa03c62 | 5257 | |
7297ce3d PB |
5258 | if (ret < min_ret) { |
5259 | if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK)) | |
5260 | return io_setup_async_msg(req, kmsg); | |
5261 | if (ret == -ERESTARTSYS) | |
5262 | ret = -EINTR; | |
5263 | req_set_fail(req); | |
5264 | } | |
257e84a5 PB |
5265 | /* fast path, check for non-NULL to avoid function call */ |
5266 | if (kmsg->free_iov) | |
5267 | kfree(kmsg->free_iov); | |
99bc4c38 | 5268 | req->flags &= ~REQ_F_NEED_CLEANUP; |
889fca73 | 5269 | __io_req_complete(req, issue_flags, ret, 0); |
5d17b4a4 | 5270 | return 0; |
03b1230c | 5271 | } |
aa1fa28f | 5272 | |
889fca73 | 5273 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 5274 | { |
7a7cacba PB |
5275 | struct io_sr_msg *sr = &req->sr_msg; |
5276 | struct msghdr msg; | |
5277 | struct iovec iov; | |
fddaface | 5278 | struct socket *sock; |
7a7cacba | 5279 | unsigned flags; |
0031275d | 5280 | int min_ret = 0; |
fddaface JA |
5281 | int ret; |
5282 | ||
dba4a925 | 5283 | sock = sock_from_file(req->file); |
7a7cacba | 5284 | if (unlikely(!sock)) |
dba4a925 | 5285 | return -ENOTSOCK; |
fddaface | 5286 | |
7a7cacba PB |
5287 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
5288 | if (unlikely(ret)) | |
14db8411 | 5289 | return ret; |
fddaface | 5290 | |
7a7cacba PB |
5291 | msg.msg_name = NULL; |
5292 | msg.msg_control = NULL; | |
5293 | msg.msg_controllen = 0; | |
5294 | msg.msg_namelen = 0; | |
fddaface | 5295 | |
04411806 PB |
5296 | flags = req->sr_msg.msg_flags; |
5297 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 5298 | flags |= MSG_DONTWAIT; |
0031275d SM |
5299 | if (flags & MSG_WAITALL) |
5300 | min_ret = iov_iter_count(&msg.msg_iter); | |
5301 | ||
7a7cacba PB |
5302 | msg.msg_flags = flags; |
5303 | ret = sock_sendmsg(sock, &msg); | |
7297ce3d PB |
5304 | if (ret < min_ret) { |
5305 | if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK)) | |
5306 | return -EAGAIN; | |
5307 | if (ret == -ERESTARTSYS) | |
5308 | ret = -EINTR; | |
93d2bcd2 | 5309 | req_set_fail(req); |
7297ce3d | 5310 | } |
889fca73 | 5311 | __io_req_complete(req, issue_flags, ret, 0); |
fddaface | 5312 | return 0; |
fddaface JA |
5313 | } |
5314 | ||
1400e697 PB |
5315 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
5316 | struct io_async_msghdr *iomsg) | |
52de1fe1 JA |
5317 | { |
5318 | struct io_sr_msg *sr = &req->sr_msg; | |
5319 | struct iovec __user *uiov; | |
5320 | size_t iov_len; | |
5321 | int ret; | |
5322 | ||
1400e697 PB |
5323 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
5324 | &iomsg->uaddr, &uiov, &iov_len); | |
52de1fe1 JA |
5325 | if (ret) |
5326 | return ret; | |
5327 | ||
5328 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
5329 | if (iov_len > 1) | |
5330 | return -EINVAL; | |
5476dfed | 5331 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
52de1fe1 | 5332 | return -EFAULT; |
5476dfed | 5333 | sr->len = iomsg->fast_iov[0].iov_len; |
257e84a5 | 5334 | iomsg->free_iov = NULL; |
52de1fe1 | 5335 | } else { |
257e84a5 | 5336 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 5337 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
257e84a5 | 5338 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
89cd35c5 | 5339 | false); |
52de1fe1 JA |
5340 | if (ret > 0) |
5341 | ret = 0; | |
5342 | } | |
5343 | ||
5344 | return ret; | |
5345 | } | |
5346 | ||
5347 | #ifdef CONFIG_COMPAT | |
5348 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, | |
1400e697 | 5349 | struct io_async_msghdr *iomsg) |
52de1fe1 | 5350 | { |
52de1fe1 JA |
5351 | struct io_sr_msg *sr = &req->sr_msg; |
5352 | struct compat_iovec __user *uiov; | |
5353 | compat_uptr_t ptr; | |
5354 | compat_size_t len; | |
5355 | int ret; | |
5356 | ||
4af3417a PB |
5357 | ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr, |
5358 | &ptr, &len); | |
52de1fe1 JA |
5359 | if (ret) |
5360 | return ret; | |
5361 | ||
5362 | uiov = compat_ptr(ptr); | |
5363 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
5364 | compat_ssize_t clen; | |
5365 | ||
5366 | if (len > 1) | |
5367 | return -EINVAL; | |
5368 | if (!access_ok(uiov, sizeof(*uiov))) | |
5369 | return -EFAULT; | |
5370 | if (__get_user(clen, &uiov->iov_len)) | |
5371 | return -EFAULT; | |
5372 | if (clen < 0) | |
5373 | return -EINVAL; | |
2d280bc8 | 5374 | sr->len = clen; |
257e84a5 | 5375 | iomsg->free_iov = NULL; |
52de1fe1 | 5376 | } else { |
257e84a5 | 5377 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 5378 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
257e84a5 | 5379 | UIO_FASTIOV, &iomsg->free_iov, |
89cd35c5 | 5380 | &iomsg->msg.msg_iter, true); |
52de1fe1 JA |
5381 | if (ret < 0) |
5382 | return ret; | |
5383 | } | |
5384 | ||
5385 | return 0; | |
5386 | } | |
5387 | #endif | |
5388 | ||
1400e697 PB |
5389 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
5390 | struct io_async_msghdr *iomsg) | |
52de1fe1 | 5391 | { |
1400e697 | 5392 | iomsg->msg.msg_name = &iomsg->addr; |
52de1fe1 JA |
5393 | |
5394 | #ifdef CONFIG_COMPAT | |
5395 | if (req->ctx->compat) | |
1400e697 | 5396 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
fddaface | 5397 | #endif |
52de1fe1 | 5398 | |
1400e697 | 5399 | return __io_recvmsg_copy_hdr(req, iomsg); |
52de1fe1 JA |
5400 | } |
5401 | ||
bcda7baa | 5402 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
51aac424 | 5403 | unsigned int issue_flags) |
bcda7baa JA |
5404 | { |
5405 | struct io_sr_msg *sr = &req->sr_msg; | |
bcda7baa | 5406 | |
51aac424 | 5407 | return io_buffer_select(req, &sr->len, sr->bgid, issue_flags); |
fddaface JA |
5408 | } |
5409 | ||
93642ef8 | 5410 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
aa1fa28f | 5411 | { |
99bc4c38 | 5412 | int ret; |
3529d8c2 | 5413 | |
93642ef8 PB |
5414 | ret = io_recvmsg_copy_hdr(req, req->async_data); |
5415 | if (!ret) | |
5416 | req->flags |= REQ_F_NEED_CLEANUP; | |
5417 | return ret; | |
5418 | } | |
5419 | ||
5420 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
5421 | { | |
5422 | struct io_sr_msg *sr = &req->sr_msg; | |
5423 | ||
d2b6f48b PB |
5424 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5425 | return -EINVAL; | |
5426 | ||
270a5940 | 5427 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
0b7b21e4 | 5428 | sr->len = READ_ONCE(sqe->len); |
bcda7baa | 5429 | sr->bgid = READ_ONCE(sqe->buf_group); |
04411806 PB |
5430 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
5431 | if (sr->msg_flags & MSG_DONTWAIT) | |
5432 | req->flags |= REQ_F_NOWAIT; | |
06b76d44 | 5433 | |
d8768362 JA |
5434 | #ifdef CONFIG_COMPAT |
5435 | if (req->ctx->compat) | |
5436 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
5437 | #endif | |
7ba89d2a | 5438 | sr->done_io = 0; |
93642ef8 | 5439 | return 0; |
aa1fa28f JA |
5440 | } |
5441 | ||
7ba89d2a JA |
5442 | static bool io_net_retry(struct socket *sock, int flags) |
5443 | { | |
5444 | if (!(flags & MSG_WAITALL)) | |
5445 | return false; | |
5446 | return sock->type == SOCK_STREAM || sock->type == SOCK_SEQPACKET; | |
5447 | } | |
5448 | ||
889fca73 | 5449 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 5450 | { |
6b754c8b | 5451 | struct io_async_msghdr iomsg, *kmsg; |
7ba89d2a | 5452 | struct io_sr_msg *sr = &req->sr_msg; |
03b1230c | 5453 | struct socket *sock; |
7fbb1b54 | 5454 | struct io_buffer *kbuf; |
7a7cacba | 5455 | unsigned flags; |
d1fd1c20 | 5456 | int ret, min_ret = 0; |
45d189c6 | 5457 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
03b1230c | 5458 | |
dba4a925 | 5459 | sock = sock_from_file(req->file); |
7a7cacba | 5460 | if (unlikely(!sock)) |
dba4a925 | 5461 | return -ENOTSOCK; |
3529d8c2 | 5462 | |
d886e185 PB |
5463 | if (req_has_async_data(req)) { |
5464 | kmsg = req->async_data; | |
5465 | } else { | |
7a7cacba PB |
5466 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
5467 | if (ret) | |
681fda8d | 5468 | return ret; |
7a7cacba PB |
5469 | kmsg = &iomsg; |
5470 | } | |
03b1230c | 5471 | |
bc02ef33 | 5472 | if (req->flags & REQ_F_BUFFER_SELECT) { |
51aac424 | 5473 | kbuf = io_recv_buffer_select(req, issue_flags); |
bc02ef33 | 5474 | if (IS_ERR(kbuf)) |
52de1fe1 | 5475 | return PTR_ERR(kbuf); |
7a7cacba | 5476 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
5476dfed PB |
5477 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
5478 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, | |
7a7cacba PB |
5479 | 1, req->sr_msg.len); |
5480 | } | |
52de1fe1 | 5481 | |
04411806 PB |
5482 | flags = req->sr_msg.msg_flags; |
5483 | if (force_nonblock) | |
7a7cacba | 5484 | flags |= MSG_DONTWAIT; |
0031275d SM |
5485 | if (flags & MSG_WAITALL) |
5486 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
5487 | ||
7a7cacba PB |
5488 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
5489 | kmsg->uaddr, flags); | |
7297ce3d PB |
5490 | if (ret < min_ret) { |
5491 | if (ret == -EAGAIN && force_nonblock) | |
5492 | return io_setup_async_msg(req, kmsg); | |
5493 | if (ret == -ERESTARTSYS) | |
5494 | ret = -EINTR; | |
7ba89d2a JA |
5495 | if (ret > 0 && io_net_retry(sock, flags)) { |
5496 | sr->done_io += ret; | |
8a3e8ee5 | 5497 | req->flags |= REQ_F_PARTIAL_IO; |
7ba89d2a JA |
5498 | return io_setup_async_msg(req, kmsg); |
5499 | } | |
7297ce3d PB |
5500 | req_set_fail(req); |
5501 | } else if ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) { | |
5502 | req_set_fail(req); | |
5503 | } | |
03b1230c | 5504 | |
257e84a5 PB |
5505 | /* fast path, check for non-NULL to avoid function call */ |
5506 | if (kmsg->free_iov) | |
5507 | kfree(kmsg->free_iov); | |
99bc4c38 | 5508 | req->flags &= ~REQ_F_NEED_CLEANUP; |
7ba89d2a JA |
5509 | if (ret >= 0) |
5510 | ret += sr->done_io; | |
5511 | else if (sr->done_io) | |
5512 | ret = sr->done_io; | |
cc3cec83 | 5513 | __io_req_complete(req, issue_flags, ret, io_put_kbuf(req, issue_flags)); |
03b1230c | 5514 | return 0; |
0fa03c62 | 5515 | } |
5d17b4a4 | 5516 | |
889fca73 | 5517 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 5518 | { |
6b754c8b | 5519 | struct io_buffer *kbuf; |
7a7cacba PB |
5520 | struct io_sr_msg *sr = &req->sr_msg; |
5521 | struct msghdr msg; | |
5522 | void __user *buf = sr->buf; | |
fddaface | 5523 | struct socket *sock; |
7a7cacba PB |
5524 | struct iovec iov; |
5525 | unsigned flags; | |
d1fd1c20 | 5526 | int ret, min_ret = 0; |
45d189c6 | 5527 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
fddaface | 5528 | |
dba4a925 | 5529 | sock = sock_from_file(req->file); |
7a7cacba | 5530 | if (unlikely(!sock)) |
dba4a925 | 5531 | return -ENOTSOCK; |
fddaface | 5532 | |
bc02ef33 | 5533 | if (req->flags & REQ_F_BUFFER_SELECT) { |
51aac424 | 5534 | kbuf = io_recv_buffer_select(req, issue_flags); |
bcda7baa JA |
5535 | if (IS_ERR(kbuf)) |
5536 | return PTR_ERR(kbuf); | |
7a7cacba | 5537 | buf = u64_to_user_ptr(kbuf->addr); |
bc02ef33 | 5538 | } |
bcda7baa | 5539 | |
7a7cacba | 5540 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
14c32eee PB |
5541 | if (unlikely(ret)) |
5542 | goto out_free; | |
fddaface | 5543 | |
7a7cacba PB |
5544 | msg.msg_name = NULL; |
5545 | msg.msg_control = NULL; | |
5546 | msg.msg_controllen = 0; | |
5547 | msg.msg_namelen = 0; | |
5548 | msg.msg_iocb = NULL; | |
5549 | msg.msg_flags = 0; | |
fddaface | 5550 | |
04411806 PB |
5551 | flags = req->sr_msg.msg_flags; |
5552 | if (force_nonblock) | |
7a7cacba | 5553 | flags |= MSG_DONTWAIT; |
0031275d SM |
5554 | if (flags & MSG_WAITALL) |
5555 | min_ret = iov_iter_count(&msg.msg_iter); | |
5556 | ||
7a7cacba | 5557 | ret = sock_recvmsg(sock, &msg, flags); |
7297ce3d PB |
5558 | if (ret < min_ret) { |
5559 | if (ret == -EAGAIN && force_nonblock) | |
5560 | return -EAGAIN; | |
5561 | if (ret == -ERESTARTSYS) | |
5562 | ret = -EINTR; | |
7ba89d2a JA |
5563 | if (ret > 0 && io_net_retry(sock, flags)) { |
5564 | sr->len -= ret; | |
5565 | sr->buf += ret; | |
5566 | sr->done_io += ret; | |
8a3e8ee5 | 5567 | req->flags |= REQ_F_PARTIAL_IO; |
7ba89d2a JA |
5568 | return -EAGAIN; |
5569 | } | |
7297ce3d PB |
5570 | req_set_fail(req); |
5571 | } else if ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) { | |
0d7c1153 | 5572 | out_free: |
93d2bcd2 | 5573 | req_set_fail(req); |
7297ce3d | 5574 | } |
cc3cec83 | 5575 | |
7ba89d2a JA |
5576 | if (ret >= 0) |
5577 | ret += sr->done_io; | |
5578 | else if (sr->done_io) | |
5579 | ret = sr->done_io; | |
cc3cec83 | 5580 | __io_req_complete(req, issue_flags, ret, io_put_kbuf(req, issue_flags)); |
fddaface | 5581 | return 0; |
fddaface JA |
5582 | } |
5583 | ||
3529d8c2 | 5584 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 | 5585 | { |
8ed8d3c3 JA |
5586 | struct io_accept *accept = &req->accept; |
5587 | ||
14587a46 | 5588 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
17f2fe35 | 5589 | return -EINVAL; |
aaa4db12 | 5590 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
5591 | return -EINVAL; |
5592 | ||
d55e5f5b JA |
5593 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5594 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 5595 | accept->flags = READ_ONCE(sqe->accept_flags); |
09952e3e | 5596 | accept->nofile = rlimit(RLIMIT_NOFILE); |
a7083ad5 | 5597 | |
aaa4db12 | 5598 | accept->file_slot = READ_ONCE(sqe->file_index); |
adf3a9e9 | 5599 | if (accept->file_slot && (accept->flags & SOCK_CLOEXEC)) |
aaa4db12 | 5600 | return -EINVAL; |
a7083ad5 PB |
5601 | if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) |
5602 | return -EINVAL; | |
5603 | if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK)) | |
5604 | accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK; | |
8ed8d3c3 | 5605 | return 0; |
8ed8d3c3 | 5606 | } |
17f2fe35 | 5607 | |
889fca73 | 5608 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 JA |
5609 | { |
5610 | struct io_accept *accept = &req->accept; | |
45d189c6 | 5611 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ac45abc0 | 5612 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
aaa4db12 | 5613 | bool fixed = !!accept->file_slot; |
a7083ad5 PB |
5614 | struct file *file; |
5615 | int ret, fd; | |
8ed8d3c3 | 5616 | |
aaa4db12 PB |
5617 | if (!fixed) { |
5618 | fd = __get_unused_fd_flags(accept->flags, accept->nofile); | |
5619 | if (unlikely(fd < 0)) | |
5620 | return fd; | |
5621 | } | |
a7083ad5 PB |
5622 | file = do_accept(req->file, file_flags, accept->addr, accept->addr_len, |
5623 | accept->flags); | |
5624 | if (IS_ERR(file)) { | |
aaa4db12 PB |
5625 | if (!fixed) |
5626 | put_unused_fd(fd); | |
a7083ad5 PB |
5627 | ret = PTR_ERR(file); |
5628 | if (ret == -EAGAIN && force_nonblock) | |
5629 | return -EAGAIN; | |
ac45abc0 PB |
5630 | if (ret == -ERESTARTSYS) |
5631 | ret = -EINTR; | |
93d2bcd2 | 5632 | req_set_fail(req); |
aaa4db12 | 5633 | } else if (!fixed) { |
a7083ad5 PB |
5634 | fd_install(fd, file); |
5635 | ret = fd; | |
aaa4db12 PB |
5636 | } else { |
5637 | ret = io_install_fixed_file(req, file, issue_flags, | |
5638 | accept->file_slot - 1); | |
ac45abc0 | 5639 | } |
889fca73 | 5640 | __io_req_complete(req, issue_flags, ret, 0); |
17f2fe35 | 5641 | return 0; |
8ed8d3c3 JA |
5642 | } |
5643 | ||
93642ef8 PB |
5644 | static int io_connect_prep_async(struct io_kiocb *req) |
5645 | { | |
5646 | struct io_async_connect *io = req->async_data; | |
5647 | struct io_connect *conn = &req->connect; | |
5648 | ||
5649 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); | |
5650 | } | |
5651 | ||
3529d8c2 | 5652 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 | 5653 | { |
3529d8c2 | 5654 | struct io_connect *conn = &req->connect; |
f499a021 | 5655 | |
14587a46 | 5656 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3fbb51c1 | 5657 | return -EINVAL; |
26578cda PB |
5658 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags || |
5659 | sqe->splice_fd_in) | |
3fbb51c1 JA |
5660 | return -EINVAL; |
5661 | ||
3529d8c2 JA |
5662 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5663 | conn->addr_len = READ_ONCE(sqe->addr2); | |
93642ef8 | 5664 | return 0; |
f499a021 JA |
5665 | } |
5666 | ||
889fca73 | 5667 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
f8e85cf2 | 5668 | { |
e8c2bc1f | 5669 | struct io_async_connect __io, *io; |
f8e85cf2 | 5670 | unsigned file_flags; |
3fbb51c1 | 5671 | int ret; |
45d189c6 | 5672 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
f8e85cf2 | 5673 | |
d886e185 | 5674 | if (req_has_async_data(req)) { |
e8c2bc1f | 5675 | io = req->async_data; |
f499a021 | 5676 | } else { |
3529d8c2 JA |
5677 | ret = move_addr_to_kernel(req->connect.addr, |
5678 | req->connect.addr_len, | |
e8c2bc1f | 5679 | &__io.address); |
f499a021 JA |
5680 | if (ret) |
5681 | goto out; | |
5682 | io = &__io; | |
5683 | } | |
5684 | ||
3fbb51c1 JA |
5685 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
5686 | ||
e8c2bc1f | 5687 | ret = __sys_connect_file(req->file, &io->address, |
3fbb51c1 | 5688 | req->connect.addr_len, file_flags); |
87f80d62 | 5689 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
d886e185 | 5690 | if (req_has_async_data(req)) |
b7bb4f7d | 5691 | return -EAGAIN; |
e8c2bc1f | 5692 | if (io_alloc_async_data(req)) { |
f499a021 JA |
5693 | ret = -ENOMEM; |
5694 | goto out; | |
5695 | } | |
e8c2bc1f | 5696 | memcpy(req->async_data, &__io, sizeof(__io)); |
f8e85cf2 | 5697 | return -EAGAIN; |
f499a021 | 5698 | } |
f8e85cf2 JA |
5699 | if (ret == -ERESTARTSYS) |
5700 | ret = -EINTR; | |
f499a021 | 5701 | out: |
4e88d6e7 | 5702 | if (ret < 0) |
93d2bcd2 | 5703 | req_set_fail(req); |
889fca73 | 5704 | __io_req_complete(req, issue_flags, ret, 0); |
f8e85cf2 | 5705 | return 0; |
469956e8 Y |
5706 | } |
5707 | #else /* !CONFIG_NET */ | |
99a10081 JA |
5708 | #define IO_NETOP_FN(op) \ |
5709 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ | |
5710 | { \ | |
5711 | return -EOPNOTSUPP; \ | |
5712 | } | |
5713 | ||
5714 | #define IO_NETOP_PREP(op) \ | |
5715 | IO_NETOP_FN(op) \ | |
5716 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ | |
5717 | { \ | |
5718 | return -EOPNOTSUPP; \ | |
5719 | } \ | |
5720 | ||
5721 | #define IO_NETOP_PREP_ASYNC(op) \ | |
5722 | IO_NETOP_PREP(op) \ | |
5723 | static int io_##op##_prep_async(struct io_kiocb *req) \ | |
5724 | { \ | |
5725 | return -EOPNOTSUPP; \ | |
5726 | } | |
5727 | ||
5728 | IO_NETOP_PREP_ASYNC(sendmsg); | |
5729 | IO_NETOP_PREP_ASYNC(recvmsg); | |
5730 | IO_NETOP_PREP_ASYNC(connect); | |
5731 | IO_NETOP_PREP(accept); | |
5732 | IO_NETOP_FN(send); | |
5733 | IO_NETOP_FN(recv); | |
469956e8 | 5734 | #endif /* CONFIG_NET */ |
f8e85cf2 | 5735 | |
d7718a9d JA |
5736 | struct io_poll_table { |
5737 | struct poll_table_struct pt; | |
5738 | struct io_kiocb *req; | |
68b11e8b | 5739 | int nr_entries; |
d7718a9d JA |
5740 | int error; |
5741 | }; | |
ce593a6c | 5742 | |
aa43477b | 5743 | #define IO_POLL_CANCEL_FLAG BIT(31) |
e2c0cb7c | 5744 | #define IO_POLL_REF_MASK GENMASK(30, 0) |
6d816e08 | 5745 | |
aa43477b PB |
5746 | /* |
5747 | * If refs part of ->poll_refs (see IO_POLL_REF_MASK) is 0, it's free. We can | |
5748 | * bump it and acquire ownership. It's disallowed to modify requests while not | |
5749 | * owning it, that prevents from races for enqueueing task_work's and b/w | |
5750 | * arming poll and wakeups. | |
5751 | */ | |
5752 | static inline bool io_poll_get_ownership(struct io_kiocb *req) | |
5753 | { | |
5754 | return !(atomic_fetch_inc(&req->poll_refs) & IO_POLL_REF_MASK); | |
d7718a9d JA |
5755 | } |
5756 | ||
aa43477b | 5757 | static void io_poll_mark_cancelled(struct io_kiocb *req) |
74ce6ce4 | 5758 | { |
aa43477b | 5759 | atomic_or(IO_POLL_CANCEL_FLAG, &req->poll_refs); |
74ce6ce4 JA |
5760 | } |
5761 | ||
d4e7cd36 | 5762 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
18bceab1 | 5763 | { |
e8c2bc1f | 5764 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
d4e7cd36 | 5765 | if (req->opcode == IORING_OP_POLL_ADD) |
e8c2bc1f | 5766 | return req->async_data; |
d4e7cd36 JA |
5767 | return req->apoll->double_poll; |
5768 | } | |
5769 | ||
5770 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) | |
5771 | { | |
5772 | if (req->opcode == IORING_OP_POLL_ADD) | |
5773 | return &req->poll; | |
5774 | return &req->apoll->poll; | |
5775 | } | |
5776 | ||
5641897a | 5777 | static void io_poll_req_insert(struct io_kiocb *req) |
d4e7cd36 | 5778 | { |
5641897a PB |
5779 | struct io_ring_ctx *ctx = req->ctx; |
5780 | struct hlist_head *list; | |
18bceab1 | 5781 | |
5641897a PB |
5782 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
5783 | hlist_add_head(&req->hash_node, list); | |
18bceab1 JA |
5784 | } |
5785 | ||
5641897a PB |
5786 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, |
5787 | wait_queue_func_t wake_func) | |
18bceab1 | 5788 | { |
5641897a | 5789 | poll->head = NULL; |
5641897a PB |
5790 | #define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP) |
5791 | /* mask in events that we always want/need */ | |
5792 | poll->events = events | IO_POLL_UNMASK; | |
5793 | INIT_LIST_HEAD(&poll->wait.entry); | |
5794 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
18bceab1 JA |
5795 | } |
5796 | ||
aa43477b | 5797 | static inline void io_poll_remove_entry(struct io_poll_iocb *poll) |
18bceab1 | 5798 | { |
791f3465 | 5799 | struct wait_queue_head *head = smp_load_acquire(&poll->head); |
18bceab1 | 5800 | |
791f3465 PB |
5801 | if (head) { |
5802 | spin_lock_irq(&head->lock); | |
5803 | list_del_init(&poll->wait.entry); | |
5804 | poll->head = NULL; | |
5805 | spin_unlock_irq(&head->lock); | |
5806 | } | |
aa43477b | 5807 | } |
18bceab1 | 5808 | |
aa43477b PB |
5809 | static void io_poll_remove_entries(struct io_kiocb *req) |
5810 | { | |
91eac1c6 JA |
5811 | /* |
5812 | * Nothing to do if neither of those flags are set. Avoid dipping | |
5813 | * into the poll/apoll/double cachelines if we can. | |
5814 | */ | |
5815 | if (!(req->flags & (REQ_F_SINGLE_POLL | REQ_F_DOUBLE_POLL))) | |
5816 | return; | |
18bceab1 | 5817 | |
791f3465 PB |
5818 | /* |
5819 | * While we hold the waitqueue lock and the waitqueue is nonempty, | |
5820 | * wake_up_pollfree() will wait for us. However, taking the waitqueue | |
5821 | * lock in the first place can race with the waitqueue being freed. | |
5822 | * | |
5823 | * We solve this as eventpoll does: by taking advantage of the fact that | |
5824 | * all users of wake_up_pollfree() will RCU-delay the actual free. If | |
5825 | * we enter rcu_read_lock() and see that the pointer to the queue is | |
5826 | * non-NULL, we can then lock it without the memory being freed out from | |
5827 | * under us. | |
5828 | * | |
5829 | * Keep holding rcu_read_lock() as long as we hold the queue lock, in | |
5830 | * case the caller deletes the entry from the queue, leaving it empty. | |
5831 | * In that case, only RCU prevents the queue memory from being freed. | |
5832 | */ | |
5833 | rcu_read_lock(); | |
91eac1c6 JA |
5834 | if (req->flags & REQ_F_SINGLE_POLL) |
5835 | io_poll_remove_entry(io_poll_get_single(req)); | |
5836 | if (req->flags & REQ_F_DOUBLE_POLL) | |
5837 | io_poll_remove_entry(io_poll_get_double(req)); | |
791f3465 | 5838 | rcu_read_unlock(); |
18bceab1 JA |
5839 | } |
5840 | ||
aa43477b PB |
5841 | /* |
5842 | * All poll tw should go through this. Checks for poll events, manages | |
5843 | * references, does rewait, etc. | |
5844 | * | |
5845 | * Returns a negative error on failure. >0 when no action require, which is | |
5846 | * either spurious wakeup or multishot CQE is served. 0 when it's done with | |
5847 | * the request, then the mask is stored in req->result. | |
5848 | */ | |
5106dd6e | 5849 | static int io_poll_check_events(struct io_kiocb *req, bool locked) |
18bceab1 | 5850 | { |
74ce6ce4 | 5851 | struct io_ring_ctx *ctx = req->ctx; |
aa43477b | 5852 | int v; |
18bceab1 | 5853 | |
316319e8 | 5854 | /* req->task == current here, checking PF_EXITING is safe */ |
e09ee510 | 5855 | if (unlikely(req->task->flags & PF_EXITING)) |
aa43477b | 5856 | io_poll_mark_cancelled(req); |
18bceab1 | 5857 | |
aa43477b PB |
5858 | do { |
5859 | v = atomic_read(&req->poll_refs); | |
74ce6ce4 | 5860 | |
aa43477b PB |
5861 | /* tw handler should be the owner, and so have some references */ |
5862 | if (WARN_ON_ONCE(!(v & IO_POLL_REF_MASK))) | |
5863 | return 0; | |
5864 | if (v & IO_POLL_CANCEL_FLAG) | |
5865 | return -ECANCELED; | |
8706e04e | 5866 | |
aa43477b | 5867 | if (!req->result) { |
2804ecd8 | 5868 | struct poll_table_struct pt = { ._key = req->apoll_events }; |
cce64ef0 | 5869 | unsigned flags = locked ? 0 : IO_URING_F_UNLOCKED; |
18bceab1 | 5870 | |
cce64ef0 | 5871 | if (unlikely(!io_assign_file(req, flags))) |
7179c3ce PB |
5872 | return -EBADF; |
5873 | req->result = vfs_poll(req->file, &pt) & req->apoll_events; | |
c8b5e260 | 5874 | } |
74ce6ce4 | 5875 | |
aa43477b | 5876 | /* multishot, just fill an CQE and proceed */ |
2804ecd8 JA |
5877 | if (req->result && !(req->apoll_events & EPOLLONESHOT)) { |
5878 | __poll_t mask = mangle_poll(req->result & req->apoll_events); | |
aa43477b | 5879 | bool filled; |
18bceab1 | 5880 | |
aa43477b PB |
5881 | spin_lock(&ctx->completion_lock); |
5882 | filled = io_fill_cqe_aux(ctx, req->user_data, mask, | |
5883 | IORING_CQE_F_MORE); | |
5884 | io_commit_cqring(ctx); | |
5885 | spin_unlock(&ctx->completion_lock); | |
5886 | if (unlikely(!filled)) | |
5887 | return -ECANCELED; | |
5888 | io_cqring_ev_posted(ctx); | |
5889 | } else if (req->result) { | |
5890 | return 0; | |
5891 | } | |
18bceab1 | 5892 | |
aa43477b PB |
5893 | /* |
5894 | * Release all references, retry if someone tried to restart | |
5895 | * task_work while we were executing it. | |
5896 | */ | |
5897 | } while (atomic_sub_return(v & IO_POLL_REF_MASK, &req->poll_refs)); | |
18bceab1 | 5898 | |
18bceab1 JA |
5899 | return 1; |
5900 | } | |
5901 | ||
aa43477b | 5902 | static void io_poll_task_func(struct io_kiocb *req, bool *locked) |
18bceab1 | 5903 | { |
18bceab1 | 5904 | struct io_ring_ctx *ctx = req->ctx; |
aa43477b | 5905 | int ret; |
18bceab1 | 5906 | |
5106dd6e | 5907 | ret = io_poll_check_events(req, *locked); |
aa43477b PB |
5908 | if (ret > 0) |
5909 | return; | |
5910 | ||
5911 | if (!ret) { | |
5912 | req->result = mangle_poll(req->result & req->poll.events); | |
e27414be | 5913 | } else { |
aa43477b PB |
5914 | req->result = ret; |
5915 | req_set_fail(req); | |
a62682f9 | 5916 | } |
aa43477b PB |
5917 | |
5918 | io_poll_remove_entries(req); | |
5919 | spin_lock(&ctx->completion_lock); | |
5920 | hash_del(&req->hash_node); | |
5921 | __io_req_complete_post(req, req->result, 0); | |
5922 | io_commit_cqring(ctx); | |
5923 | spin_unlock(&ctx->completion_lock); | |
5924 | io_cqring_ev_posted(ctx); | |
18bceab1 JA |
5925 | } |
5926 | ||
aa43477b | 5927 | static void io_apoll_task_func(struct io_kiocb *req, bool *locked) |
18bceab1 JA |
5928 | { |
5929 | struct io_ring_ctx *ctx = req->ctx; | |
aa43477b | 5930 | int ret; |
18bceab1 | 5931 | |
5106dd6e | 5932 | ret = io_poll_check_events(req, *locked); |
aa43477b PB |
5933 | if (ret > 0) |
5934 | return; | |
18bceab1 | 5935 | |
aa43477b PB |
5936 | io_poll_remove_entries(req); |
5937 | spin_lock(&ctx->completion_lock); | |
5938 | hash_del(&req->hash_node); | |
5939 | spin_unlock(&ctx->completion_lock); | |
18bceab1 | 5940 | |
aa43477b PB |
5941 | if (!ret) |
5942 | io_req_task_submit(req, locked); | |
5943 | else | |
5944 | io_req_complete_failed(req, ret); | |
18bceab1 JA |
5945 | } |
5946 | ||
81459350 | 5947 | static void __io_poll_execute(struct io_kiocb *req, int mask, int events) |
aa43477b PB |
5948 | { |
5949 | req->result = mask; | |
81459350 JA |
5950 | /* |
5951 | * This is useful for poll that is armed on behalf of another | |
5952 | * request, and where the wakeup path could be on a different | |
5953 | * CPU. We want to avoid pulling in req->apoll->events for that | |
5954 | * case. | |
5955 | */ | |
2804ecd8 | 5956 | req->apoll_events = events; |
aa43477b PB |
5957 | if (req->opcode == IORING_OP_POLL_ADD) |
5958 | req->io_task_work.func = io_poll_task_func; | |
5959 | else | |
5960 | req->io_task_work.func = io_apoll_task_func; | |
5961 | ||
502c87d6 | 5962 | trace_io_uring_task_add(req->ctx, req, req->user_data, req->opcode, mask); |
aa43477b PB |
5963 | io_req_task_work_add(req, false); |
5964 | } | |
5965 | ||
81459350 | 5966 | static inline void io_poll_execute(struct io_kiocb *req, int res, int events) |
aa43477b PB |
5967 | { |
5968 | if (io_poll_get_ownership(req)) | |
81459350 | 5969 | __io_poll_execute(req, res, events); |
aa43477b PB |
5970 | } |
5971 | ||
5972 | static void io_poll_cancel_req(struct io_kiocb *req) | |
5973 | { | |
5974 | io_poll_mark_cancelled(req); | |
5975 | /* kick tw, which should complete the request */ | |
81459350 | 5976 | io_poll_execute(req, 0, 0); |
aa43477b PB |
5977 | } |
5978 | ||
d89a4fac JA |
5979 | #define wqe_to_req(wait) ((void *)((unsigned long) (wait)->private & ~1)) |
5980 | #define wqe_is_double(wait) ((unsigned long) (wait)->private & 1) | |
5981 | ||
aa43477b PB |
5982 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
5983 | void *key) | |
18bceab1 | 5984 | { |
d89a4fac | 5985 | struct io_kiocb *req = wqe_to_req(wait); |
aa43477b PB |
5986 | struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb, |
5987 | wait); | |
18bceab1 JA |
5988 | __poll_t mask = key_to_poll(key); |
5989 | ||
791f3465 PB |
5990 | if (unlikely(mask & POLLFREE)) { |
5991 | io_poll_mark_cancelled(req); | |
5992 | /* we have to kick tw in case it's not already */ | |
81459350 | 5993 | io_poll_execute(req, 0, poll->events); |
791f3465 PB |
5994 | |
5995 | /* | |
5996 | * If the waitqueue is being freed early but someone is already | |
5997 | * holds ownership over it, we have to tear down the request as | |
5998 | * best we can. That means immediately removing the request from | |
5999 | * its waitqueue and preventing all further accesses to the | |
6000 | * waitqueue via the request. | |
6001 | */ | |
6002 | list_del_init(&poll->wait.entry); | |
6003 | ||
6004 | /* | |
6005 | * Careful: this *must* be the last step, since as soon | |
6006 | * as req->head is NULL'ed out, the request can be | |
6007 | * completed and freed, since aio_poll_complete_work() | |
6008 | * will no longer need to take the waitqueue lock. | |
6009 | */ | |
6010 | smp_store_release(&poll->head, NULL); | |
6011 | return 1; | |
6012 | } | |
6013 | ||
aa43477b | 6014 | /* for instances that support it check for an event match first */ |
18bceab1 JA |
6015 | if (mask && !(mask & poll->events)) |
6016 | return 0; | |
6017 | ||
eb0089d6 PB |
6018 | if (io_poll_get_ownership(req)) { |
6019 | /* optional, saves extra locking for removal in tw handler */ | |
6020 | if (mask && poll->events & EPOLLONESHOT) { | |
6021 | list_del_init(&poll->wait.entry); | |
6022 | poll->head = NULL; | |
d89a4fac JA |
6023 | if (wqe_is_double(wait)) |
6024 | req->flags &= ~REQ_F_DOUBLE_POLL; | |
6025 | else | |
6026 | req->flags &= ~REQ_F_SINGLE_POLL; | |
eb0089d6 | 6027 | } |
81459350 | 6028 | __io_poll_execute(req, mask, poll->events); |
eb0089d6 | 6029 | } |
18bceab1 | 6030 | return 1; |
18bceab1 JA |
6031 | } |
6032 | ||
6033 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, | |
807abcb0 JA |
6034 | struct wait_queue_head *head, |
6035 | struct io_poll_iocb **poll_ptr) | |
18bceab1 JA |
6036 | { |
6037 | struct io_kiocb *req = pt->req; | |
d89a4fac | 6038 | unsigned long wqe_private = (unsigned long) req; |
18bceab1 JA |
6039 | |
6040 | /* | |
68b11e8b PB |
6041 | * The file being polled uses multiple waitqueues for poll handling |
6042 | * (e.g. one for read, one for write). Setup a separate io_poll_iocb | |
6043 | * if this happens. | |
18bceab1 | 6044 | */ |
68b11e8b | 6045 | if (unlikely(pt->nr_entries)) { |
aa43477b | 6046 | struct io_poll_iocb *first = poll; |
58852d4d | 6047 | |
23a65db8 | 6048 | /* double add on the same waitqueue head, ignore */ |
aa43477b | 6049 | if (first->head == head) |
23a65db8 | 6050 | return; |
18bceab1 | 6051 | /* already have a 2nd entry, fail a third attempt */ |
807abcb0 | 6052 | if (*poll_ptr) { |
23a65db8 PB |
6053 | if ((*poll_ptr)->head == head) |
6054 | return; | |
18bceab1 JA |
6055 | pt->error = -EINVAL; |
6056 | return; | |
6057 | } | |
aa43477b | 6058 | |
18bceab1 JA |
6059 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
6060 | if (!poll) { | |
6061 | pt->error = -ENOMEM; | |
6062 | return; | |
6063 | } | |
d89a4fac JA |
6064 | /* mark as double wq entry */ |
6065 | wqe_private |= 1; | |
91eac1c6 | 6066 | req->flags |= REQ_F_DOUBLE_POLL; |
aa43477b | 6067 | io_init_poll_iocb(poll, first->events, first->wait.func); |
807abcb0 | 6068 | *poll_ptr = poll; |
d886e185 PB |
6069 | if (req->opcode == IORING_OP_POLL_ADD) |
6070 | req->flags |= REQ_F_ASYNC_DATA; | |
18bceab1 JA |
6071 | } |
6072 | ||
91eac1c6 | 6073 | req->flags |= REQ_F_SINGLE_POLL; |
68b11e8b | 6074 | pt->nr_entries++; |
18bceab1 | 6075 | poll->head = head; |
d89a4fac | 6076 | poll->wait.private = (void *) wqe_private; |
a31eb4a2 JX |
6077 | |
6078 | if (poll->events & EPOLLEXCLUSIVE) | |
6079 | add_wait_queue_exclusive(head, &poll->wait); | |
6080 | else | |
6081 | add_wait_queue(head, &poll->wait); | |
18bceab1 JA |
6082 | } |
6083 | ||
aa43477b | 6084 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
18bceab1 JA |
6085 | struct poll_table_struct *p) |
6086 | { | |
6087 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
d7718a9d | 6088 | |
aa43477b PB |
6089 | __io_queue_proc(&pt->req->poll, pt, head, |
6090 | (struct io_poll_iocb **) &pt->req->async_data); | |
d7718a9d JA |
6091 | } |
6092 | ||
aa43477b PB |
6093 | static int __io_arm_poll_handler(struct io_kiocb *req, |
6094 | struct io_poll_iocb *poll, | |
6095 | struct io_poll_table *ipt, __poll_t mask) | |
d7718a9d JA |
6096 | { |
6097 | struct io_ring_ctx *ctx = req->ctx; | |
aa43477b | 6098 | int v; |
d7718a9d | 6099 | |
4d52f338 | 6100 | INIT_HLIST_NODE(&req->hash_node); |
aa43477b | 6101 | io_init_poll_iocb(poll, mask, io_poll_wake); |
b90cd197 | 6102 | poll->file = req->file; |
d7718a9d JA |
6103 | |
6104 | ipt->pt._key = mask; | |
6105 | ipt->req = req; | |
68b11e8b PB |
6106 | ipt->error = 0; |
6107 | ipt->nr_entries = 0; | |
d7718a9d | 6108 | |
aa43477b PB |
6109 | /* |
6110 | * Take the ownership to delay any tw execution up until we're done | |
6111 | * with poll arming. see io_poll_get_ownership(). | |
6112 | */ | |
6113 | atomic_set(&req->poll_refs, 1); | |
d7718a9d | 6114 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
aa43477b PB |
6115 | |
6116 | if (mask && (poll->events & EPOLLONESHOT)) { | |
6117 | io_poll_remove_entries(req); | |
6118 | /* no one else has access to the req, forget about the ref */ | |
6119 | return mask; | |
6120 | } | |
6121 | if (!mask && unlikely(ipt->error || !ipt->nr_entries)) { | |
6122 | io_poll_remove_entries(req); | |
6123 | if (!ipt->error) | |
6124 | ipt->error = -EINVAL; | |
6125 | return 0; | |
6126 | } | |
d7718a9d | 6127 | |
79ebeaee | 6128 | spin_lock(&ctx->completion_lock); |
aa43477b PB |
6129 | io_poll_req_insert(req); |
6130 | spin_unlock(&ctx->completion_lock); | |
6131 | ||
6132 | if (mask) { | |
6133 | /* can't multishot if failed, just queue the event we've got */ | |
6134 | if (unlikely(ipt->error || !ipt->nr_entries)) | |
6135 | poll->events |= EPOLLONESHOT; | |
81459350 | 6136 | __io_poll_execute(req, mask, poll->events); |
aa43477b | 6137 | return 0; |
d7718a9d JA |
6138 | } |
6139 | ||
aa43477b PB |
6140 | /* |
6141 | * Release ownership. If someone tried to queue a tw while it was | |
6142 | * locked, kick it off for them. | |
6143 | */ | |
6144 | v = atomic_dec_return(&req->poll_refs); | |
6145 | if (unlikely(v & IO_POLL_REF_MASK)) | |
81459350 | 6146 | __io_poll_execute(req, 0, poll->events); |
aa43477b PB |
6147 | return 0; |
6148 | } | |
6149 | ||
6150 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
6151 | struct poll_table_struct *p) | |
6152 | { | |
6153 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
6154 | struct async_poll *apoll = pt->req->apoll; | |
6155 | ||
6156 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); | |
d7718a9d JA |
6157 | } |
6158 | ||
59b735ae OL |
6159 | enum { |
6160 | IO_APOLL_OK, | |
6161 | IO_APOLL_ABORTED, | |
6162 | IO_APOLL_READY | |
6163 | }; | |
6164 | ||
4d9237e3 | 6165 | static int io_arm_poll_handler(struct io_kiocb *req, unsigned issue_flags) |
d7718a9d JA |
6166 | { |
6167 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
6168 | struct io_ring_ctx *ctx = req->ctx; | |
6169 | struct async_poll *apoll; | |
6170 | struct io_poll_table ipt; | |
aa43477b PB |
6171 | __poll_t mask = EPOLLONESHOT | POLLERR | POLLPRI; |
6172 | int ret; | |
d7718a9d | 6173 | |
b2d9c3da PB |
6174 | if (!def->pollin && !def->pollout) |
6175 | return IO_APOLL_ABORTED; | |
658d0a40 PB |
6176 | if (!file_can_poll(req->file) || (req->flags & REQ_F_POLLED)) |
6177 | return IO_APOLL_ABORTED; | |
b2d9c3da PB |
6178 | |
6179 | if (def->pollin) { | |
b2d9c3da PB |
6180 | mask |= POLLIN | POLLRDNORM; |
6181 | ||
6182 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ | |
6183 | if ((req->opcode == IORING_OP_RECVMSG) && | |
6184 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) | |
6185 | mask &= ~POLLIN; | |
6186 | } else { | |
b2d9c3da PB |
6187 | mask |= POLLOUT | POLLWRNORM; |
6188 | } | |
52dd8640 DY |
6189 | if (def->poll_exclusive) |
6190 | mask |= EPOLLEXCLUSIVE; | |
4d9237e3 JA |
6191 | if (!(issue_flags & IO_URING_F_UNLOCKED) && |
6192 | !list_empty(&ctx->apoll_cache)) { | |
6193 | apoll = list_first_entry(&ctx->apoll_cache, struct async_poll, | |
6194 | poll.wait.entry); | |
6195 | list_del_init(&apoll->poll.wait.entry); | |
6196 | } else { | |
6197 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); | |
6198 | if (unlikely(!apoll)) | |
6199 | return IO_APOLL_ABORTED; | |
6200 | } | |
807abcb0 | 6201 | apoll->double_poll = NULL; |
d7718a9d | 6202 | req->apoll = apoll; |
b2d9c3da | 6203 | req->flags |= REQ_F_POLLED; |
d7718a9d JA |
6204 | ipt.pt._qproc = io_async_queue_proc; |
6205 | ||
4d55f238 | 6206 | io_kbuf_recycle(req, issue_flags); |
abdad709 | 6207 | |
aa43477b | 6208 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask); |
41a5169c HX |
6209 | if (ret || ipt.error) |
6210 | return ret ? IO_APOLL_READY : IO_APOLL_ABORTED; | |
6211 | ||
502c87d6 | 6212 | trace_io_uring_poll_arm(ctx, req, req->user_data, req->opcode, |
236daeae | 6213 | mask, apoll->poll.events); |
59b735ae | 6214 | return IO_APOLL_OK; |
d7718a9d JA |
6215 | } |
6216 | ||
76e1b642 JA |
6217 | /* |
6218 | * Returns true if we found and killed one or more poll requests | |
6219 | */ | |
c072481d PB |
6220 | static __cold bool io_poll_remove_all(struct io_ring_ctx *ctx, |
6221 | struct task_struct *tsk, bool cancel_all) | |
221c5eb2 | 6222 | { |
78076bb6 | 6223 | struct hlist_node *tmp; |
221c5eb2 | 6224 | struct io_kiocb *req; |
aa43477b PB |
6225 | bool found = false; |
6226 | int i; | |
221c5eb2 | 6227 | |
79ebeaee | 6228 | spin_lock(&ctx->completion_lock); |
78076bb6 JA |
6229 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
6230 | struct hlist_head *list; | |
6231 | ||
6232 | list = &ctx->cancel_hash[i]; | |
f3606e3a | 6233 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
42a7b4ed | 6234 | if (io_match_task_safe(req, tsk, cancel_all)) { |
61bc84c4 | 6235 | hlist_del_init(&req->hash_node); |
aa43477b PB |
6236 | io_poll_cancel_req(req); |
6237 | found = true; | |
6238 | } | |
f3606e3a | 6239 | } |
221c5eb2 | 6240 | } |
79ebeaee | 6241 | spin_unlock(&ctx->completion_lock); |
aa43477b | 6242 | return found; |
221c5eb2 JA |
6243 | } |
6244 | ||
9ba5fac8 PB |
6245 | static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr, |
6246 | bool poll_only) | |
e07785b0 | 6247 | __must_hold(&ctx->completion_lock) |
47f46768 | 6248 | { |
78076bb6 | 6249 | struct hlist_head *list; |
47f46768 JA |
6250 | struct io_kiocb *req; |
6251 | ||
78076bb6 JA |
6252 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
6253 | hlist_for_each_entry(req, list, hash_node) { | |
b41e9852 JA |
6254 | if (sqe_addr != req->user_data) |
6255 | continue; | |
9ba5fac8 PB |
6256 | if (poll_only && req->opcode != IORING_OP_POLL_ADD) |
6257 | continue; | |
b2cb805f | 6258 | return req; |
47f46768 | 6259 | } |
b2cb805f JA |
6260 | return NULL; |
6261 | } | |
6262 | ||
aa43477b PB |
6263 | static bool io_poll_disarm(struct io_kiocb *req) |
6264 | __must_hold(&ctx->completion_lock) | |
6265 | { | |
6266 | if (!io_poll_get_ownership(req)) | |
6267 | return false; | |
6268 | io_poll_remove_entries(req); | |
6269 | hash_del(&req->hash_node); | |
6270 | return true; | |
6271 | } | |
6272 | ||
9ba5fac8 PB |
6273 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr, |
6274 | bool poll_only) | |
e07785b0 | 6275 | __must_hold(&ctx->completion_lock) |
b2cb805f | 6276 | { |
aa43477b | 6277 | struct io_kiocb *req = io_poll_find(ctx, sqe_addr, poll_only); |
b2cb805f | 6278 | |
b2cb805f JA |
6279 | if (!req) |
6280 | return -ENOENT; | |
aa43477b PB |
6281 | io_poll_cancel_req(req); |
6282 | return 0; | |
47f46768 JA |
6283 | } |
6284 | ||
9096af3e PB |
6285 | static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe, |
6286 | unsigned int flags) | |
6287 | { | |
6288 | u32 events; | |
47f46768 | 6289 | |
9096af3e PB |
6290 | events = READ_ONCE(sqe->poll32_events); |
6291 | #ifdef __BIG_ENDIAN | |
6292 | events = swahw32(events); | |
6293 | #endif | |
6294 | if (!(flags & IORING_POLL_ADD_MULTI)) | |
6295 | events |= EPOLLONESHOT; | |
6296 | return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT)); | |
47f46768 JA |
6297 | } |
6298 | ||
c5de0036 | 6299 | static int io_poll_update_prep(struct io_kiocb *req, |
3529d8c2 | 6300 | const struct io_uring_sqe *sqe) |
0969e783 | 6301 | { |
c5de0036 PB |
6302 | struct io_poll_update *upd = &req->poll_update; |
6303 | u32 flags; | |
6304 | ||
0969e783 JA |
6305 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
6306 | return -EINVAL; | |
26578cda | 6307 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
c5de0036 PB |
6308 | return -EINVAL; |
6309 | flags = READ_ONCE(sqe->len); | |
6310 | if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA | | |
6311 | IORING_POLL_ADD_MULTI)) | |
6312 | return -EINVAL; | |
6313 | /* meaningless without update */ | |
6314 | if (flags == IORING_POLL_ADD_MULTI) | |
0969e783 JA |
6315 | return -EINVAL; |
6316 | ||
c5de0036 PB |
6317 | upd->old_user_data = READ_ONCE(sqe->addr); |
6318 | upd->update_events = flags & IORING_POLL_UPDATE_EVENTS; | |
6319 | upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA; | |
221c5eb2 | 6320 | |
c5de0036 PB |
6321 | upd->new_user_data = READ_ONCE(sqe->off); |
6322 | if (!upd->update_user_data && upd->new_user_data) | |
6323 | return -EINVAL; | |
6324 | if (upd->update_events) | |
6325 | upd->events = io_poll_parse_events(sqe, flags); | |
6326 | else if (sqe->poll32_events) | |
6327 | return -EINVAL; | |
221c5eb2 | 6328 | |
221c5eb2 JA |
6329 | return 0; |
6330 | } | |
6331 | ||
3529d8c2 | 6332 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
6333 | { |
6334 | struct io_poll_iocb *poll = &req->poll; | |
c5de0036 | 6335 | u32 flags; |
221c5eb2 JA |
6336 | |
6337 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
6338 | return -EINVAL; | |
c5de0036 | 6339 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr) |
88e41cf9 JA |
6340 | return -EINVAL; |
6341 | flags = READ_ONCE(sqe->len); | |
c5de0036 | 6342 | if (flags & ~IORING_POLL_ADD_MULTI) |
221c5eb2 | 6343 | return -EINVAL; |
04c76b41 PB |
6344 | if ((flags & IORING_POLL_ADD_MULTI) && (req->flags & REQ_F_CQE_SKIP)) |
6345 | return -EINVAL; | |
221c5eb2 | 6346 | |
48dcd38d | 6347 | io_req_set_refcount(req); |
2804ecd8 | 6348 | req->apoll_events = poll->events = io_poll_parse_events(sqe, flags); |
0969e783 JA |
6349 | return 0; |
6350 | } | |
6351 | ||
61e98203 | 6352 | static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags) |
0969e783 JA |
6353 | { |
6354 | struct io_poll_iocb *poll = &req->poll; | |
0969e783 | 6355 | struct io_poll_table ipt; |
aa43477b | 6356 | int ret; |
0969e783 | 6357 | |
d7718a9d | 6358 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 6359 | |
aa43477b PB |
6360 | ret = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events); |
6361 | ret = ret ?: ipt.error; | |
6362 | if (ret) | |
6363 | __io_req_complete(req, issue_flags, ret, 0); | |
6364 | return 0; | |
221c5eb2 JA |
6365 | } |
6366 | ||
c5de0036 | 6367 | static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags) |
b69de288 JA |
6368 | { |
6369 | struct io_ring_ctx *ctx = req->ctx; | |
6370 | struct io_kiocb *preq; | |
2bbb146d | 6371 | int ret2, ret = 0; |
cc8e9ba7 | 6372 | bool locked; |
b69de288 | 6373 | |
79ebeaee | 6374 | spin_lock(&ctx->completion_lock); |
9ba5fac8 | 6375 | preq = io_poll_find(ctx, req->poll_update.old_user_data, true); |
aa43477b | 6376 | if (!preq || !io_poll_disarm(preq)) { |
79ebeaee | 6377 | spin_unlock(&ctx->completion_lock); |
aa43477b | 6378 | ret = preq ? -EALREADY : -ENOENT; |
2bbb146d | 6379 | goto out; |
b69de288 | 6380 | } |
79ebeaee | 6381 | spin_unlock(&ctx->completion_lock); |
cb3b200e | 6382 | |
2bbb146d PB |
6383 | if (req->poll_update.update_events || req->poll_update.update_user_data) { |
6384 | /* only mask one event flags, keep behavior flags */ | |
6385 | if (req->poll_update.update_events) { | |
6386 | preq->poll.events &= ~0xffff; | |
6387 | preq->poll.events |= req->poll_update.events & 0xffff; | |
6388 | preq->poll.events |= IO_POLL_UNMASK; | |
cb3b200e | 6389 | } |
2bbb146d PB |
6390 | if (req->poll_update.update_user_data) |
6391 | preq->user_data = req->poll_update.new_user_data; | |
b69de288 | 6392 | |
2bbb146d PB |
6393 | ret2 = io_poll_add(preq, issue_flags); |
6394 | /* successfully updated, don't complete poll request */ | |
6395 | if (!ret2) | |
6396 | goto out; | |
b69de288 | 6397 | } |
6224590d | 6398 | |
2bbb146d | 6399 | req_set_fail(preq); |
cc8e9ba7 PB |
6400 | preq->result = -ECANCELED; |
6401 | locked = !(issue_flags & IO_URING_F_UNLOCKED); | |
6402 | io_req_task_complete(preq, &locked); | |
2bbb146d PB |
6403 | out: |
6404 | if (ret < 0) | |
6224590d | 6405 | req_set_fail(req); |
2bbb146d | 6406 | /* complete update request, we're done with it */ |
cc8e9ba7 | 6407 | __io_req_complete(req, issue_flags, ret, 0); |
b69de288 | 6408 | return 0; |
89850fce JA |
6409 | } |
6410 | ||
5262f567 JA |
6411 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
6412 | { | |
ad8a48ac JA |
6413 | struct io_timeout_data *data = container_of(timer, |
6414 | struct io_timeout_data, timer); | |
6415 | struct io_kiocb *req = data->req; | |
6416 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
6417 | unsigned long flags; |
6418 | ||
89850fce | 6419 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
a71976f3 | 6420 | list_del_init(&req->timeout.list); |
01cec8c1 PB |
6421 | atomic_set(&req->ctx->cq_timeouts, |
6422 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
89850fce | 6423 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); |
01cec8c1 | 6424 | |
a90c8bf6 PB |
6425 | if (!(data->flags & IORING_TIMEOUT_ETIME_SUCCESS)) |
6426 | req_set_fail(req); | |
6427 | ||
6428 | req->result = -ETIME; | |
6429 | req->io_task_work.func = io_req_task_complete; | |
4813c377 | 6430 | io_req_task_work_add(req, false); |
5262f567 JA |
6431 | return HRTIMER_NORESTART; |
6432 | } | |
6433 | ||
fbd15848 PB |
6434 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
6435 | __u64 user_data) | |
89850fce | 6436 | __must_hold(&ctx->timeout_lock) |
f254ac04 | 6437 | { |
fbd15848 | 6438 | struct io_timeout_data *io; |
47f46768 | 6439 | struct io_kiocb *req; |
fd9c7bc5 | 6440 | bool found = false; |
f254ac04 | 6441 | |
135fcde8 | 6442 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
fd9c7bc5 PB |
6443 | found = user_data == req->user_data; |
6444 | if (found) | |
47f46768 | 6445 | break; |
47f46768 | 6446 | } |
fd9c7bc5 PB |
6447 | if (!found) |
6448 | return ERR_PTR(-ENOENT); | |
fbd15848 PB |
6449 | |
6450 | io = req->async_data; | |
fd9c7bc5 | 6451 | if (hrtimer_try_to_cancel(&io->timer) == -1) |
fbd15848 | 6452 | return ERR_PTR(-EALREADY); |
a71976f3 | 6453 | list_del_init(&req->timeout.list); |
fbd15848 PB |
6454 | return req; |
6455 | } | |
47f46768 | 6456 | |
fbd15848 | 6457 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
ec3c3d0f | 6458 | __must_hold(&ctx->completion_lock) |
89850fce | 6459 | __must_hold(&ctx->timeout_lock) |
fbd15848 PB |
6460 | { |
6461 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); | |
6462 | ||
6463 | if (IS_ERR(req)) | |
6464 | return PTR_ERR(req); | |
6695490d | 6465 | io_req_task_queue_fail(req, -ECANCELED); |
f254ac04 JA |
6466 | return 0; |
6467 | } | |
6468 | ||
50c1df2b JA |
6469 | static clockid_t io_timeout_get_clock(struct io_timeout_data *data) |
6470 | { | |
6471 | switch (data->flags & IORING_TIMEOUT_CLOCK_MASK) { | |
6472 | case IORING_TIMEOUT_BOOTTIME: | |
6473 | return CLOCK_BOOTTIME; | |
6474 | case IORING_TIMEOUT_REALTIME: | |
6475 | return CLOCK_REALTIME; | |
6476 | default: | |
6477 | /* can't happen, vetted at prep time */ | |
6478 | WARN_ON_ONCE(1); | |
6479 | fallthrough; | |
6480 | case 0: | |
6481 | return CLOCK_MONOTONIC; | |
6482 | } | |
6483 | } | |
6484 | ||
f1042b6c PB |
6485 | static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
6486 | struct timespec64 *ts, enum hrtimer_mode mode) | |
6487 | __must_hold(&ctx->timeout_lock) | |
6488 | { | |
6489 | struct io_timeout_data *io; | |
6490 | struct io_kiocb *req; | |
6491 | bool found = false; | |
6492 | ||
6493 | list_for_each_entry(req, &ctx->ltimeout_list, timeout.list) { | |
6494 | found = user_data == req->user_data; | |
6495 | if (found) | |
6496 | break; | |
6497 | } | |
6498 | if (!found) | |
6499 | return -ENOENT; | |
6500 | ||
6501 | io = req->async_data; | |
6502 | if (hrtimer_try_to_cancel(&io->timer) == -1) | |
6503 | return -EALREADY; | |
6504 | hrtimer_init(&io->timer, io_timeout_get_clock(io), mode); | |
6505 | io->timer.function = io_link_timeout_fn; | |
6506 | hrtimer_start(&io->timer, timespec64_to_ktime(*ts), mode); | |
6507 | return 0; | |
6508 | } | |
6509 | ||
9c8e11b3 PB |
6510 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
6511 | struct timespec64 *ts, enum hrtimer_mode mode) | |
89850fce | 6512 | __must_hold(&ctx->timeout_lock) |
47f46768 | 6513 | { |
9c8e11b3 PB |
6514 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
6515 | struct io_timeout_data *data; | |
47f46768 | 6516 | |
9c8e11b3 PB |
6517 | if (IS_ERR(req)) |
6518 | return PTR_ERR(req); | |
47f46768 | 6519 | |
9c8e11b3 PB |
6520 | req->timeout.off = 0; /* noseq */ |
6521 | data = req->async_data; | |
6522 | list_add_tail(&req->timeout.list, &ctx->timeout_list); | |
50c1df2b | 6523 | hrtimer_init(&data->timer, io_timeout_get_clock(data), mode); |
9c8e11b3 PB |
6524 | data->timer.function = io_timeout_fn; |
6525 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); | |
6526 | return 0; | |
47f46768 JA |
6527 | } |
6528 | ||
3529d8c2 JA |
6529 | static int io_timeout_remove_prep(struct io_kiocb *req, |
6530 | const struct io_uring_sqe *sqe) | |
b29472ee | 6531 | { |
9c8e11b3 PB |
6532 | struct io_timeout_rem *tr = &req->timeout_rem; |
6533 | ||
b29472ee JA |
6534 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
6535 | return -EINVAL; | |
61710e43 DA |
6536 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6537 | return -EINVAL; | |
26578cda | 6538 | if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in) |
b29472ee JA |
6539 | return -EINVAL; |
6540 | ||
f1042b6c | 6541 | tr->ltimeout = false; |
9c8e11b3 PB |
6542 | tr->addr = READ_ONCE(sqe->addr); |
6543 | tr->flags = READ_ONCE(sqe->timeout_flags); | |
f1042b6c PB |
6544 | if (tr->flags & IORING_TIMEOUT_UPDATE_MASK) { |
6545 | if (hweight32(tr->flags & IORING_TIMEOUT_CLOCK_MASK) > 1) | |
6546 | return -EINVAL; | |
6547 | if (tr->flags & IORING_LINK_TIMEOUT_UPDATE) | |
6548 | tr->ltimeout = true; | |
6549 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE_MASK|IORING_TIMEOUT_ABS)) | |
9c8e11b3 PB |
6550 | return -EINVAL; |
6551 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) | |
6552 | return -EFAULT; | |
2087009c YB |
6553 | if (tr->ts.tv_sec < 0 || tr->ts.tv_nsec < 0) |
6554 | return -EINVAL; | |
9c8e11b3 PB |
6555 | } else if (tr->flags) { |
6556 | /* timeout removal doesn't support flags */ | |
b29472ee | 6557 | return -EINVAL; |
9c8e11b3 | 6558 | } |
b29472ee | 6559 | |
b29472ee JA |
6560 | return 0; |
6561 | } | |
6562 | ||
8662daec PB |
6563 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
6564 | { | |
6565 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS | |
6566 | : HRTIMER_MODE_REL; | |
6567 | } | |
6568 | ||
11365043 JA |
6569 | /* |
6570 | * Remove or update an existing timeout command | |
6571 | */ | |
61e98203 | 6572 | static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags) |
11365043 | 6573 | { |
9c8e11b3 | 6574 | struct io_timeout_rem *tr = &req->timeout_rem; |
11365043 | 6575 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 | 6576 | int ret; |
11365043 | 6577 | |
ec3c3d0f PB |
6578 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) { |
6579 | spin_lock(&ctx->completion_lock); | |
6580 | spin_lock_irq(&ctx->timeout_lock); | |
9c8e11b3 | 6581 | ret = io_timeout_cancel(ctx, tr->addr); |
ec3c3d0f PB |
6582 | spin_unlock_irq(&ctx->timeout_lock); |
6583 | spin_unlock(&ctx->completion_lock); | |
6584 | } else { | |
f1042b6c PB |
6585 | enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags); |
6586 | ||
ec3c3d0f | 6587 | spin_lock_irq(&ctx->timeout_lock); |
f1042b6c PB |
6588 | if (tr->ltimeout) |
6589 | ret = io_linked_timeout_update(ctx, tr->addr, &tr->ts, mode); | |
6590 | else | |
6591 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode); | |
ec3c3d0f PB |
6592 | spin_unlock_irq(&ctx->timeout_lock); |
6593 | } | |
11365043 | 6594 | |
4e88d6e7 | 6595 | if (ret < 0) |
93d2bcd2 | 6596 | req_set_fail(req); |
505657bc | 6597 | io_req_complete_post(req, ret, 0); |
11365043 | 6598 | return 0; |
5262f567 JA |
6599 | } |
6600 | ||
3529d8c2 | 6601 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 6602 | bool is_timeout_link) |
5262f567 | 6603 | { |
ad8a48ac | 6604 | struct io_timeout_data *data; |
a41525ab | 6605 | unsigned flags; |
56080b02 | 6606 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 6607 | |
ad8a48ac | 6608 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 6609 | return -EINVAL; |
26578cda PB |
6610 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1 || |
6611 | sqe->splice_fd_in) | |
a41525ab | 6612 | return -EINVAL; |
56080b02 | 6613 | if (off && is_timeout_link) |
2d28390a | 6614 | return -EINVAL; |
a41525ab | 6615 | flags = READ_ONCE(sqe->timeout_flags); |
6224590d PB |
6616 | if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK | |
6617 | IORING_TIMEOUT_ETIME_SUCCESS)) | |
50c1df2b JA |
6618 | return -EINVAL; |
6619 | /* more than one clock specified is invalid, obviously */ | |
6620 | if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1) | |
5262f567 | 6621 | return -EINVAL; |
bdf20073 | 6622 | |
ef9dd637 | 6623 | INIT_LIST_HEAD(&req->timeout.list); |
bfe68a22 | 6624 | req->timeout.off = off; |
f18ee4cf PB |
6625 | if (unlikely(off && !req->ctx->off_timeout_used)) |
6626 | req->ctx->off_timeout_used = true; | |
26a61679 | 6627 | |
d6a644a7 PB |
6628 | if (WARN_ON_ONCE(req_has_async_data(req))) |
6629 | return -EFAULT; | |
6630 | if (io_alloc_async_data(req)) | |
26a61679 JA |
6631 | return -ENOMEM; |
6632 | ||
e8c2bc1f | 6633 | data = req->async_data; |
ad8a48ac | 6634 | data->req = req; |
50c1df2b | 6635 | data->flags = flags; |
ad8a48ac JA |
6636 | |
6637 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
6638 | return -EFAULT; |
6639 | ||
f6223ff7 YB |
6640 | if (data->ts.tv_sec < 0 || data->ts.tv_nsec < 0) |
6641 | return -EINVAL; | |
6642 | ||
e677edbc | 6643 | INIT_LIST_HEAD(&req->timeout.list); |
8662daec | 6644 | data->mode = io_translate_timeout_mode(flags); |
50c1df2b | 6645 | hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode); |
b97e736a PB |
6646 | |
6647 | if (is_timeout_link) { | |
6648 | struct io_submit_link *link = &req->ctx->submit_state.link; | |
6649 | ||
6650 | if (!link->head) | |
6651 | return -EINVAL; | |
6652 | if (link->last->opcode == IORING_OP_LINK_TIMEOUT) | |
6653 | return -EINVAL; | |
4d13d1a4 PB |
6654 | req->timeout.head = link->last; |
6655 | link->last->flags |= REQ_F_ARM_LTIMEOUT; | |
b97e736a | 6656 | } |
ad8a48ac JA |
6657 | return 0; |
6658 | } | |
6659 | ||
61e98203 | 6660 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
ad8a48ac | 6661 | { |
ad8a48ac | 6662 | struct io_ring_ctx *ctx = req->ctx; |
e8c2bc1f | 6663 | struct io_timeout_data *data = req->async_data; |
ad8a48ac | 6664 | struct list_head *entry; |
bfe68a22 | 6665 | u32 tail, off = req->timeout.off; |
ad8a48ac | 6666 | |
89850fce | 6667 | spin_lock_irq(&ctx->timeout_lock); |
93bd25bb | 6668 | |
5262f567 JA |
6669 | /* |
6670 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
6671 | * timeout event to be satisfied. If it isn't set, then this is |
6672 | * a pure timeout request, sequence isn't used. | |
5262f567 | 6673 | */ |
8eb7e2d0 | 6674 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
6675 | entry = ctx->timeout_list.prev; |
6676 | goto add; | |
6677 | } | |
5262f567 | 6678 | |
bfe68a22 PB |
6679 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
6680 | req->timeout.target_seq = tail + off; | |
5262f567 | 6681 | |
f010505b MDG |
6682 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
6683 | * This is safe because ->completion_lock is held, and submissions | |
6684 | * and completions are never mixed in the same ->completion_lock section. | |
6685 | */ | |
6686 | ctx->cq_last_tm_flush = tail; | |
6687 | ||
5262f567 JA |
6688 | /* |
6689 | * Insertion sort, ensuring the first entry in the list is always | |
6690 | * the one we need first. | |
6691 | */ | |
5262f567 | 6692 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
6693 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
6694 | timeout.list); | |
5262f567 | 6695 | |
8eb7e2d0 | 6696 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 6697 | continue; |
bfe68a22 PB |
6698 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
6699 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
6700 | break; |
6701 | } | |
93bd25bb | 6702 | add: |
135fcde8 | 6703 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
6704 | data->timer.function = io_timeout_fn; |
6705 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
89850fce | 6706 | spin_unlock_irq(&ctx->timeout_lock); |
5262f567 JA |
6707 | return 0; |
6708 | } | |
5262f567 | 6709 | |
f458dd84 PB |
6710 | struct io_cancel_data { |
6711 | struct io_ring_ctx *ctx; | |
6712 | u64 user_data; | |
6713 | }; | |
6714 | ||
62755e35 JA |
6715 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
6716 | { | |
6717 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
f458dd84 | 6718 | struct io_cancel_data *cd = data; |
62755e35 | 6719 | |
f458dd84 | 6720 | return req->ctx == cd->ctx && req->user_data == cd->user_data; |
62755e35 JA |
6721 | } |
6722 | ||
f458dd84 PB |
6723 | static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data, |
6724 | struct io_ring_ctx *ctx) | |
62755e35 | 6725 | { |
f458dd84 | 6726 | struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, }; |
62755e35 | 6727 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
6728 | int ret = 0; |
6729 | ||
f458dd84 | 6730 | if (!tctx || !tctx->io_wq) |
5aa75ed5 JA |
6731 | return -ENOENT; |
6732 | ||
f458dd84 | 6733 | cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false); |
62755e35 JA |
6734 | switch (cancel_ret) { |
6735 | case IO_WQ_CANCEL_OK: | |
6736 | ret = 0; | |
6737 | break; | |
6738 | case IO_WQ_CANCEL_RUNNING: | |
6739 | ret = -EALREADY; | |
6740 | break; | |
6741 | case IO_WQ_CANCEL_NOTFOUND: | |
6742 | ret = -ENOENT; | |
6743 | break; | |
6744 | } | |
6745 | ||
e977d6d3 JA |
6746 | return ret; |
6747 | } | |
6748 | ||
8cb01fac | 6749 | static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr) |
47f46768 | 6750 | { |
8cb01fac | 6751 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 JA |
6752 | int ret; |
6753 | ||
dadebc35 | 6754 | WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current); |
8cb01fac | 6755 | |
f458dd84 | 6756 | ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx); |
ccbf7261 JA |
6757 | /* |
6758 | * Fall-through even for -EALREADY, as we may have poll armed | |
6759 | * that need unarming. | |
6760 | */ | |
6761 | if (!ret) | |
6762 | return 0; | |
505657bc PB |
6763 | |
6764 | spin_lock(&ctx->completion_lock); | |
ccbf7261 JA |
6765 | ret = io_poll_cancel(ctx, sqe_addr, false); |
6766 | if (ret != -ENOENT) | |
6767 | goto out; | |
6768 | ||
79ebeaee | 6769 | spin_lock_irq(&ctx->timeout_lock); |
47f46768 | 6770 | ret = io_timeout_cancel(ctx, sqe_addr); |
79ebeaee | 6771 | spin_unlock_irq(&ctx->timeout_lock); |
505657bc PB |
6772 | out: |
6773 | spin_unlock(&ctx->completion_lock); | |
6774 | return ret; | |
47f46768 JA |
6775 | } |
6776 | ||
3529d8c2 JA |
6777 | static int io_async_cancel_prep(struct io_kiocb *req, |
6778 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 6779 | { |
fbf23849 | 6780 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 6781 | return -EINVAL; |
61710e43 DA |
6782 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6783 | return -EINVAL; | |
26578cda PB |
6784 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags || |
6785 | sqe->splice_fd_in) | |
e977d6d3 JA |
6786 | return -EINVAL; |
6787 | ||
fbf23849 JA |
6788 | req->cancel.addr = READ_ONCE(sqe->addr); |
6789 | return 0; | |
6790 | } | |
6791 | ||
61e98203 | 6792 | static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6793 | { |
6794 | struct io_ring_ctx *ctx = req->ctx; | |
58f99373 | 6795 | u64 sqe_addr = req->cancel.addr; |
3b44b371 | 6796 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
58f99373 PB |
6797 | struct io_tctx_node *node; |
6798 | int ret; | |
6799 | ||
8cb01fac | 6800 | ret = io_try_cancel_userdata(req, sqe_addr); |
58f99373 PB |
6801 | if (ret != -ENOENT) |
6802 | goto done; | |
58f99373 PB |
6803 | |
6804 | /* slow path, try all io-wq's */ | |
3b44b371 | 6805 | io_ring_submit_lock(ctx, needs_lock); |
58f99373 PB |
6806 | ret = -ENOENT; |
6807 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
6808 | struct io_uring_task *tctx = node->task->io_uring; | |
fbf23849 | 6809 | |
58f99373 PB |
6810 | ret = io_async_cancel_one(tctx, req->cancel.addr, ctx); |
6811 | if (ret != -ENOENT) | |
6812 | break; | |
6813 | } | |
3b44b371 | 6814 | io_ring_submit_unlock(ctx, needs_lock); |
58f99373 | 6815 | done: |
58f99373 | 6816 | if (ret < 0) |
93d2bcd2 | 6817 | req_set_fail(req); |
505657bc | 6818 | io_req_complete_post(req, ret, 0); |
5262f567 JA |
6819 | return 0; |
6820 | } | |
6821 | ||
269bbe5f | 6822 | static int io_rsrc_update_prep(struct io_kiocb *req, |
05f3fb3c JA |
6823 | const struct io_uring_sqe *sqe) |
6824 | { | |
61710e43 DA |
6825 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6826 | return -EINVAL; | |
26578cda | 6827 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
05f3fb3c JA |
6828 | return -EINVAL; |
6829 | ||
269bbe5f BM |
6830 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
6831 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); | |
6832 | if (!req->rsrc_update.nr_args) | |
05f3fb3c | 6833 | return -EINVAL; |
269bbe5f | 6834 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
05f3fb3c JA |
6835 | return 0; |
6836 | } | |
6837 | ||
889fca73 | 6838 | static int io_files_update(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6839 | { |
6840 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 6841 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
c3bdad02 | 6842 | struct io_uring_rsrc_update2 up; |
05f3fb3c | 6843 | int ret; |
fbf23849 | 6844 | |
269bbe5f BM |
6845 | up.offset = req->rsrc_update.offset; |
6846 | up.data = req->rsrc_update.arg; | |
c3bdad02 PB |
6847 | up.nr = 0; |
6848 | up.tags = 0; | |
615cee49 | 6849 | up.resv = 0; |
d8a3ba9c | 6850 | up.resv2 = 0; |
05f3fb3c | 6851 | |
3b44b371 | 6852 | io_ring_submit_lock(ctx, needs_lock); |
fdecb662 | 6853 | ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE, |
98f0b3b4 | 6854 | &up, req->rsrc_update.nr_args); |
3b44b371 | 6855 | io_ring_submit_unlock(ctx, needs_lock); |
05f3fb3c JA |
6856 | |
6857 | if (ret < 0) | |
93d2bcd2 | 6858 | req_set_fail(req); |
889fca73 | 6859 | __io_req_complete(req, issue_flags, ret, 0); |
5262f567 JA |
6860 | return 0; |
6861 | } | |
6862 | ||
bfe76559 | 6863 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 6864 | { |
d625c6ee | 6865 | switch (req->opcode) { |
e781573e | 6866 | case IORING_OP_NOP: |
bfe76559 | 6867 | return 0; |
f67676d1 JA |
6868 | case IORING_OP_READV: |
6869 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 6870 | case IORING_OP_READ: |
f67676d1 JA |
6871 | case IORING_OP_WRITEV: |
6872 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 6873 | case IORING_OP_WRITE: |
584b0180 | 6874 | return io_prep_rw(req, sqe); |
0969e783 | 6875 | case IORING_OP_POLL_ADD: |
bfe76559 | 6876 | return io_poll_add_prep(req, sqe); |
0969e783 | 6877 | case IORING_OP_POLL_REMOVE: |
c5de0036 | 6878 | return io_poll_update_prep(req, sqe); |
8ed8d3c3 | 6879 | case IORING_OP_FSYNC: |
1155c76a | 6880 | return io_fsync_prep(req, sqe); |
8ed8d3c3 | 6881 | case IORING_OP_SYNC_FILE_RANGE: |
1155c76a | 6882 | return io_sfr_prep(req, sqe); |
03b1230c | 6883 | case IORING_OP_SENDMSG: |
fddaface | 6884 | case IORING_OP_SEND: |
bfe76559 | 6885 | return io_sendmsg_prep(req, sqe); |
03b1230c | 6886 | case IORING_OP_RECVMSG: |
fddaface | 6887 | case IORING_OP_RECV: |
bfe76559 | 6888 | return io_recvmsg_prep(req, sqe); |
f499a021 | 6889 | case IORING_OP_CONNECT: |
bfe76559 | 6890 | return io_connect_prep(req, sqe); |
2d28390a | 6891 | case IORING_OP_TIMEOUT: |
bfe76559 | 6892 | return io_timeout_prep(req, sqe, false); |
b29472ee | 6893 | case IORING_OP_TIMEOUT_REMOVE: |
bfe76559 | 6894 | return io_timeout_remove_prep(req, sqe); |
fbf23849 | 6895 | case IORING_OP_ASYNC_CANCEL: |
bfe76559 | 6896 | return io_async_cancel_prep(req, sqe); |
2d28390a | 6897 | case IORING_OP_LINK_TIMEOUT: |
bfe76559 | 6898 | return io_timeout_prep(req, sqe, true); |
8ed8d3c3 | 6899 | case IORING_OP_ACCEPT: |
bfe76559 | 6900 | return io_accept_prep(req, sqe); |
d63d1b5e | 6901 | case IORING_OP_FALLOCATE: |
bfe76559 | 6902 | return io_fallocate_prep(req, sqe); |
15b71abe | 6903 | case IORING_OP_OPENAT: |
bfe76559 | 6904 | return io_openat_prep(req, sqe); |
b5dba59e | 6905 | case IORING_OP_CLOSE: |
bfe76559 | 6906 | return io_close_prep(req, sqe); |
05f3fb3c | 6907 | case IORING_OP_FILES_UPDATE: |
269bbe5f | 6908 | return io_rsrc_update_prep(req, sqe); |
eddc7ef5 | 6909 | case IORING_OP_STATX: |
bfe76559 | 6910 | return io_statx_prep(req, sqe); |
4840e418 | 6911 | case IORING_OP_FADVISE: |
bfe76559 | 6912 | return io_fadvise_prep(req, sqe); |
c1ca757b | 6913 | case IORING_OP_MADVISE: |
bfe76559 | 6914 | return io_madvise_prep(req, sqe); |
cebdb986 | 6915 | case IORING_OP_OPENAT2: |
bfe76559 | 6916 | return io_openat2_prep(req, sqe); |
3e4827b0 | 6917 | case IORING_OP_EPOLL_CTL: |
bfe76559 | 6918 | return io_epoll_ctl_prep(req, sqe); |
7d67af2c | 6919 | case IORING_OP_SPLICE: |
bfe76559 | 6920 | return io_splice_prep(req, sqe); |
ddf0322d | 6921 | case IORING_OP_PROVIDE_BUFFERS: |
bfe76559 | 6922 | return io_provide_buffers_prep(req, sqe); |
067524e9 | 6923 | case IORING_OP_REMOVE_BUFFERS: |
bfe76559 | 6924 | return io_remove_buffers_prep(req, sqe); |
f2a8d5c7 | 6925 | case IORING_OP_TEE: |
bfe76559 | 6926 | return io_tee_prep(req, sqe); |
36f4fa68 JA |
6927 | case IORING_OP_SHUTDOWN: |
6928 | return io_shutdown_prep(req, sqe); | |
80a261fd JA |
6929 | case IORING_OP_RENAMEAT: |
6930 | return io_renameat_prep(req, sqe); | |
14a1143b JA |
6931 | case IORING_OP_UNLINKAT: |
6932 | return io_unlinkat_prep(req, sqe); | |
e34a02dc DK |
6933 | case IORING_OP_MKDIRAT: |
6934 | return io_mkdirat_prep(req, sqe); | |
7a8721f8 DK |
6935 | case IORING_OP_SYMLINKAT: |
6936 | return io_symlinkat_prep(req, sqe); | |
cf30da90 DK |
6937 | case IORING_OP_LINKAT: |
6938 | return io_linkat_prep(req, sqe); | |
4f57f06c JA |
6939 | case IORING_OP_MSG_RING: |
6940 | return io_msg_ring_prep(req, sqe); | |
f67676d1 JA |
6941 | } |
6942 | ||
bfe76559 PB |
6943 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
6944 | req->opcode); | |
bd54b6fe | 6945 | return -EINVAL; |
bfe76559 PB |
6946 | } |
6947 | ||
93642ef8 | 6948 | static int io_req_prep_async(struct io_kiocb *req) |
bfe76559 | 6949 | { |
b7e298d2 PB |
6950 | if (!io_op_defs[req->opcode].needs_async_setup) |
6951 | return 0; | |
d886e185 | 6952 | if (WARN_ON_ONCE(req_has_async_data(req))) |
b7e298d2 PB |
6953 | return -EFAULT; |
6954 | if (io_alloc_async_data(req)) | |
6955 | return -EAGAIN; | |
6956 | ||
93642ef8 PB |
6957 | switch (req->opcode) { |
6958 | case IORING_OP_READV: | |
93642ef8 PB |
6959 | return io_rw_prep_async(req, READ); |
6960 | case IORING_OP_WRITEV: | |
93642ef8 PB |
6961 | return io_rw_prep_async(req, WRITE); |
6962 | case IORING_OP_SENDMSG: | |
93642ef8 PB |
6963 | return io_sendmsg_prep_async(req); |
6964 | case IORING_OP_RECVMSG: | |
93642ef8 PB |
6965 | return io_recvmsg_prep_async(req); |
6966 | case IORING_OP_CONNECT: | |
6967 | return io_connect_prep_async(req); | |
6968 | } | |
b7e298d2 PB |
6969 | printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n", |
6970 | req->opcode); | |
6971 | return -EFAULT; | |
f67676d1 JA |
6972 | } |
6973 | ||
9cf7c104 PB |
6974 | static u32 io_get_sequence(struct io_kiocb *req) |
6975 | { | |
a3dbdf54 | 6976 | u32 seq = req->ctx->cached_sq_head; |
9cf7c104 | 6977 | |
a3dbdf54 PB |
6978 | /* need original cached_sq_head, but it was increased for each req */ |
6979 | io_for_each_link(req, req) | |
6980 | seq--; | |
6981 | return seq; | |
9cf7c104 PB |
6982 | } |
6983 | ||
c072481d | 6984 | static __cold void io_drain_req(struct io_kiocb *req) |
de0617e4 | 6985 | { |
a197f664 | 6986 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 6987 | struct io_defer_entry *de; |
f67676d1 | 6988 | int ret; |
e0eb71dc | 6989 | u32 seq = io_get_sequence(req); |
3c19966d | 6990 | |
9d858b21 | 6991 | /* Still need defer if there is pending req in defer list. */ |
e302f104 | 6992 | spin_lock(&ctx->completion_lock); |
5e371265 | 6993 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) { |
e302f104 | 6994 | spin_unlock(&ctx->completion_lock); |
e0eb71dc | 6995 | queue: |
10c66904 | 6996 | ctx->drain_active = false; |
e0eb71dc PB |
6997 | io_req_task_queue(req); |
6998 | return; | |
10c66904 | 6999 | } |
e302f104 | 7000 | spin_unlock(&ctx->completion_lock); |
9cf7c104 | 7001 | |
b7e298d2 | 7002 | ret = io_req_prep_async(req); |
e0eb71dc PB |
7003 | if (ret) { |
7004 | fail: | |
7005 | io_req_complete_failed(req, ret); | |
7006 | return; | |
7007 | } | |
cbdcb435 | 7008 | io_prep_async_link(req); |
27dc8338 | 7009 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
76cc33d7 | 7010 | if (!de) { |
1b48773f | 7011 | ret = -ENOMEM; |
e0eb71dc | 7012 | goto fail; |
76cc33d7 | 7013 | } |
2d28390a | 7014 | |
79ebeaee | 7015 | spin_lock(&ctx->completion_lock); |
9cf7c104 | 7016 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
79ebeaee | 7017 | spin_unlock(&ctx->completion_lock); |
27dc8338 | 7018 | kfree(de); |
e0eb71dc | 7019 | goto queue; |
de0617e4 JA |
7020 | } |
7021 | ||
502c87d6 | 7022 | trace_io_uring_defer(ctx, req, req->user_data, req->opcode); |
27dc8338 | 7023 | de->req = req; |
9cf7c104 | 7024 | de->seq = seq; |
27dc8338 | 7025 | list_add_tail(&de->list, &ctx->defer_list); |
79ebeaee | 7026 | spin_unlock(&ctx->completion_lock); |
de0617e4 JA |
7027 | } |
7028 | ||
68fb8979 | 7029 | static void io_clean_op(struct io_kiocb *req) |
99bc4c38 | 7030 | { |
8197b053 PB |
7031 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
7032 | spin_lock(&req->ctx->completion_lock); | |
cc3cec83 | 7033 | io_put_kbuf_comp(req); |
8197b053 PB |
7034 | spin_unlock(&req->ctx->completion_lock); |
7035 | } | |
99bc4c38 | 7036 | |
0e1b6fe3 PB |
7037 | if (req->flags & REQ_F_NEED_CLEANUP) { |
7038 | switch (req->opcode) { | |
7039 | case IORING_OP_READV: | |
7040 | case IORING_OP_READ_FIXED: | |
7041 | case IORING_OP_READ: | |
7042 | case IORING_OP_WRITEV: | |
7043 | case IORING_OP_WRITE_FIXED: | |
e8c2bc1f JA |
7044 | case IORING_OP_WRITE: { |
7045 | struct io_async_rw *io = req->async_data; | |
1dacb4df PB |
7046 | |
7047 | kfree(io->free_iovec); | |
0e1b6fe3 | 7048 | break; |
e8c2bc1f | 7049 | } |
0e1b6fe3 | 7050 | case IORING_OP_RECVMSG: |
e8c2bc1f JA |
7051 | case IORING_OP_SENDMSG: { |
7052 | struct io_async_msghdr *io = req->async_data; | |
257e84a5 PB |
7053 | |
7054 | kfree(io->free_iov); | |
0e1b6fe3 | 7055 | break; |
e8c2bc1f | 7056 | } |
f3cd4850 JA |
7057 | case IORING_OP_OPENAT: |
7058 | case IORING_OP_OPENAT2: | |
7059 | if (req->open.filename) | |
7060 | putname(req->open.filename); | |
7061 | break; | |
80a261fd JA |
7062 | case IORING_OP_RENAMEAT: |
7063 | putname(req->rename.oldpath); | |
7064 | putname(req->rename.newpath); | |
7065 | break; | |
14a1143b JA |
7066 | case IORING_OP_UNLINKAT: |
7067 | putname(req->unlink.filename); | |
7068 | break; | |
e34a02dc DK |
7069 | case IORING_OP_MKDIRAT: |
7070 | putname(req->mkdir.filename); | |
7071 | break; | |
7a8721f8 DK |
7072 | case IORING_OP_SYMLINKAT: |
7073 | putname(req->symlink.oldpath); | |
7074 | putname(req->symlink.newpath); | |
7075 | break; | |
cf30da90 DK |
7076 | case IORING_OP_LINKAT: |
7077 | putname(req->hardlink.oldpath); | |
7078 | putname(req->hardlink.newpath); | |
7079 | break; | |
1b6fe6e0 SR |
7080 | case IORING_OP_STATX: |
7081 | if (req->statx.filename) | |
7082 | putname(req->statx.filename); | |
7083 | break; | |
0e1b6fe3 | 7084 | } |
99bc4c38 | 7085 | } |
75652a30 JA |
7086 | if ((req->flags & REQ_F_POLLED) && req->apoll) { |
7087 | kfree(req->apoll->double_poll); | |
7088 | kfree(req->apoll); | |
7089 | req->apoll = NULL; | |
7090 | } | |
c854357b | 7091 | if (req->flags & REQ_F_CREDS) |
b8e64b53 | 7092 | put_cred(req->creds); |
d886e185 PB |
7093 | if (req->flags & REQ_F_ASYNC_DATA) { |
7094 | kfree(req->async_data); | |
7095 | req->async_data = NULL; | |
7096 | } | |
c854357b | 7097 | req->flags &= ~IO_REQ_CLEAN_FLAGS; |
99bc4c38 PB |
7098 | } |
7099 | ||
6bf9c47a JA |
7100 | static bool io_assign_file(struct io_kiocb *req, unsigned int issue_flags) |
7101 | { | |
7102 | if (req->file || !io_op_defs[req->opcode].needs_file) | |
7103 | return true; | |
7104 | ||
7105 | if (req->flags & REQ_F_FIXED_FILE) | |
82733d16 | 7106 | req->file = io_file_get_fixed(req, req->fd, issue_flags); |
6bf9c47a | 7107 | else |
82733d16 | 7108 | req->file = io_file_get_normal(req, req->fd); |
6bf9c47a JA |
7109 | if (req->file) |
7110 | return true; | |
7111 | ||
7112 | req_set_fail(req); | |
7113 | req->result = -EBADF; | |
7114 | return false; | |
7115 | } | |
7116 | ||
889fca73 | 7117 | static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 7118 | { |
5730b27e | 7119 | const struct cred *creds = NULL; |
d625c6ee | 7120 | int ret; |
2b188cc1 | 7121 | |
70152140 JA |
7122 | if (unlikely(!io_assign_file(req, issue_flags))) |
7123 | return -EBADF; | |
7124 | ||
6878b40e | 7125 | if (unlikely((req->flags & REQ_F_CREDS) && req->creds != current_cred())) |
c10d1f98 | 7126 | creds = override_creds(req->creds); |
5730b27e | 7127 | |
5bd2182d PM |
7128 | if (!io_op_defs[req->opcode].audit_skip) |
7129 | audit_uring_entry(req->opcode); | |
7130 | ||
d625c6ee | 7131 | switch (req->opcode) { |
2b188cc1 | 7132 | case IORING_OP_NOP: |
889fca73 | 7133 | ret = io_nop(req, issue_flags); |
2b188cc1 JA |
7134 | break; |
7135 | case IORING_OP_READV: | |
edafccee | 7136 | case IORING_OP_READ_FIXED: |
3a6820f2 | 7137 | case IORING_OP_READ: |
889fca73 | 7138 | ret = io_read(req, issue_flags); |
edafccee | 7139 | break; |
3529d8c2 | 7140 | case IORING_OP_WRITEV: |
edafccee | 7141 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 7142 | case IORING_OP_WRITE: |
889fca73 | 7143 | ret = io_write(req, issue_flags); |
2b188cc1 | 7144 | break; |
c992fe29 | 7145 | case IORING_OP_FSYNC: |
45d189c6 | 7146 | ret = io_fsync(req, issue_flags); |
c992fe29 | 7147 | break; |
221c5eb2 | 7148 | case IORING_OP_POLL_ADD: |
61e98203 | 7149 | ret = io_poll_add(req, issue_flags); |
221c5eb2 JA |
7150 | break; |
7151 | case IORING_OP_POLL_REMOVE: | |
c5de0036 | 7152 | ret = io_poll_update(req, issue_flags); |
221c5eb2 | 7153 | break; |
5d17b4a4 | 7154 | case IORING_OP_SYNC_FILE_RANGE: |
45d189c6 | 7155 | ret = io_sync_file_range(req, issue_flags); |
5d17b4a4 | 7156 | break; |
0fa03c62 | 7157 | case IORING_OP_SENDMSG: |
889fca73 | 7158 | ret = io_sendmsg(req, issue_flags); |
062d04d7 | 7159 | break; |
fddaface | 7160 | case IORING_OP_SEND: |
889fca73 | 7161 | ret = io_send(req, issue_flags); |
0fa03c62 | 7162 | break; |
aa1fa28f | 7163 | case IORING_OP_RECVMSG: |
889fca73 | 7164 | ret = io_recvmsg(req, issue_flags); |
062d04d7 | 7165 | break; |
fddaface | 7166 | case IORING_OP_RECV: |
889fca73 | 7167 | ret = io_recv(req, issue_flags); |
aa1fa28f | 7168 | break; |
5262f567 | 7169 | case IORING_OP_TIMEOUT: |
61e98203 | 7170 | ret = io_timeout(req, issue_flags); |
5262f567 | 7171 | break; |
11365043 | 7172 | case IORING_OP_TIMEOUT_REMOVE: |
61e98203 | 7173 | ret = io_timeout_remove(req, issue_flags); |
11365043 | 7174 | break; |
17f2fe35 | 7175 | case IORING_OP_ACCEPT: |
889fca73 | 7176 | ret = io_accept(req, issue_flags); |
17f2fe35 | 7177 | break; |
f8e85cf2 | 7178 | case IORING_OP_CONNECT: |
889fca73 | 7179 | ret = io_connect(req, issue_flags); |
f8e85cf2 | 7180 | break; |
62755e35 | 7181 | case IORING_OP_ASYNC_CANCEL: |
61e98203 | 7182 | ret = io_async_cancel(req, issue_flags); |
62755e35 | 7183 | break; |
d63d1b5e | 7184 | case IORING_OP_FALLOCATE: |
45d189c6 | 7185 | ret = io_fallocate(req, issue_flags); |
d63d1b5e | 7186 | break; |
15b71abe | 7187 | case IORING_OP_OPENAT: |
45d189c6 | 7188 | ret = io_openat(req, issue_flags); |
15b71abe | 7189 | break; |
b5dba59e | 7190 | case IORING_OP_CLOSE: |
889fca73 | 7191 | ret = io_close(req, issue_flags); |
b5dba59e | 7192 | break; |
05f3fb3c | 7193 | case IORING_OP_FILES_UPDATE: |
889fca73 | 7194 | ret = io_files_update(req, issue_flags); |
05f3fb3c | 7195 | break; |
eddc7ef5 | 7196 | case IORING_OP_STATX: |
45d189c6 | 7197 | ret = io_statx(req, issue_flags); |
eddc7ef5 | 7198 | break; |
4840e418 | 7199 | case IORING_OP_FADVISE: |
45d189c6 | 7200 | ret = io_fadvise(req, issue_flags); |
4840e418 | 7201 | break; |
c1ca757b | 7202 | case IORING_OP_MADVISE: |
45d189c6 | 7203 | ret = io_madvise(req, issue_flags); |
c1ca757b | 7204 | break; |
cebdb986 | 7205 | case IORING_OP_OPENAT2: |
45d189c6 | 7206 | ret = io_openat2(req, issue_flags); |
cebdb986 | 7207 | break; |
3e4827b0 | 7208 | case IORING_OP_EPOLL_CTL: |
889fca73 | 7209 | ret = io_epoll_ctl(req, issue_flags); |
3e4827b0 | 7210 | break; |
7d67af2c | 7211 | case IORING_OP_SPLICE: |
45d189c6 | 7212 | ret = io_splice(req, issue_flags); |
7d67af2c | 7213 | break; |
ddf0322d | 7214 | case IORING_OP_PROVIDE_BUFFERS: |
889fca73 | 7215 | ret = io_provide_buffers(req, issue_flags); |
ddf0322d | 7216 | break; |
067524e9 | 7217 | case IORING_OP_REMOVE_BUFFERS: |
889fca73 | 7218 | ret = io_remove_buffers(req, issue_flags); |
3e4827b0 | 7219 | break; |
f2a8d5c7 | 7220 | case IORING_OP_TEE: |
45d189c6 | 7221 | ret = io_tee(req, issue_flags); |
f2a8d5c7 | 7222 | break; |
36f4fa68 | 7223 | case IORING_OP_SHUTDOWN: |
45d189c6 | 7224 | ret = io_shutdown(req, issue_flags); |
36f4fa68 | 7225 | break; |
80a261fd | 7226 | case IORING_OP_RENAMEAT: |
45d189c6 | 7227 | ret = io_renameat(req, issue_flags); |
80a261fd | 7228 | break; |
14a1143b | 7229 | case IORING_OP_UNLINKAT: |
45d189c6 | 7230 | ret = io_unlinkat(req, issue_flags); |
14a1143b | 7231 | break; |
e34a02dc DK |
7232 | case IORING_OP_MKDIRAT: |
7233 | ret = io_mkdirat(req, issue_flags); | |
7234 | break; | |
7a8721f8 DK |
7235 | case IORING_OP_SYMLINKAT: |
7236 | ret = io_symlinkat(req, issue_flags); | |
7237 | break; | |
cf30da90 DK |
7238 | case IORING_OP_LINKAT: |
7239 | ret = io_linkat(req, issue_flags); | |
7240 | break; | |
4f57f06c JA |
7241 | case IORING_OP_MSG_RING: |
7242 | ret = io_msg_ring(req, issue_flags); | |
7243 | break; | |
2b188cc1 JA |
7244 | default: |
7245 | ret = -EINVAL; | |
7246 | break; | |
7247 | } | |
7248 | ||
5bd2182d PM |
7249 | if (!io_op_defs[req->opcode].audit_skip) |
7250 | audit_uring_exit(!ret, ret); | |
7251 | ||
5730b27e JA |
7252 | if (creds) |
7253 | revert_creds(creds); | |
def596e9 JA |
7254 | if (ret) |
7255 | return ret; | |
b532576e | 7256 | /* If the op doesn't have a file, we're not polling for it */ |
9983028e | 7257 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && req->file) |
9882131c | 7258 | io_iopoll_req_issued(req, issue_flags); |
def596e9 JA |
7259 | |
7260 | return 0; | |
2b188cc1 JA |
7261 | } |
7262 | ||
ebc11b6c PB |
7263 | static struct io_wq_work *io_wq_free_work(struct io_wq_work *work) |
7264 | { | |
7265 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
7266 | ||
7267 | req = io_put_req_find_next(req); | |
7268 | return req ? &req->work : NULL; | |
7269 | } | |
7270 | ||
5280f7e5 | 7271 | static void io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
7272 | { |
7273 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6bf9c47a | 7274 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
d01905db PB |
7275 | unsigned int issue_flags = IO_URING_F_UNLOCKED; |
7276 | bool needs_poll = false; | |
6df1db6b | 7277 | struct io_kiocb *timeout; |
6bf9c47a | 7278 | int ret = 0, err = -ECANCELED; |
2b188cc1 | 7279 | |
48dcd38d PB |
7280 | /* one will be dropped by ->io_free_work() after returning to io-wq */ |
7281 | if (!(req->flags & REQ_F_REFCOUNT)) | |
7282 | __io_req_set_refcount(req, 2); | |
7283 | else | |
7284 | req_ref_get(req); | |
5d5901a3 | 7285 | |
6df1db6b PB |
7286 | timeout = io_prep_linked_timeout(req); |
7287 | if (timeout) | |
7288 | io_queue_linked_timeout(timeout); | |
d4c81f38 | 7289 | |
6bf9c47a | 7290 | |
dadebc35 | 7291 | /* either cancelled or io-wq is dying, so don't touch tctx->iowq */ |
d01905db | 7292 | if (work->flags & IO_WQ_WORK_CANCEL) { |
0f8da75b | 7293 | fail: |
6bf9c47a | 7294 | io_req_task_queue_fail(req, err); |
d01905db PB |
7295 | return; |
7296 | } | |
0f8da75b PB |
7297 | if (!io_assign_file(req, issue_flags)) { |
7298 | err = -EBADF; | |
7299 | work->flags |= IO_WQ_WORK_CANCEL; | |
7300 | goto fail; | |
7301 | } | |
31b51510 | 7302 | |
d01905db | 7303 | if (req->flags & REQ_F_FORCE_ASYNC) { |
afb7f56f PB |
7304 | bool opcode_poll = def->pollin || def->pollout; |
7305 | ||
7306 | if (opcode_poll && file_can_poll(req->file)) { | |
7307 | needs_poll = true; | |
d01905db | 7308 | issue_flags |= IO_URING_F_NONBLOCK; |
afb7f56f | 7309 | } |
561fb04a | 7310 | } |
31b51510 | 7311 | |
d01905db PB |
7312 | do { |
7313 | ret = io_issue_sqe(req, issue_flags); | |
7314 | if (ret != -EAGAIN) | |
7315 | break; | |
7316 | /* | |
7317 | * We can get EAGAIN for iopolled IO even though we're | |
7318 | * forcing a sync submission from here, since we can't | |
7319 | * wait for request slots on the block side. | |
7320 | */ | |
7321 | if (!needs_poll) { | |
7322 | cond_resched(); | |
7323 | continue; | |
90fa0288 HX |
7324 | } |
7325 | ||
4d9237e3 | 7326 | if (io_arm_poll_handler(req, issue_flags) == IO_APOLL_OK) |
d01905db PB |
7327 | return; |
7328 | /* aborted or ready, in either case retry blocking */ | |
7329 | needs_poll = false; | |
7330 | issue_flags &= ~IO_URING_F_NONBLOCK; | |
7331 | } while (1); | |
31b51510 | 7332 | |
a3df7698 | 7333 | /* avoid locking problems by failing it from a clean context */ |
5d5901a3 | 7334 | if (ret) |
a3df7698 | 7335 | io_req_task_queue_fail(req, ret); |
2b188cc1 JA |
7336 | } |
7337 | ||
aeca241b | 7338 | static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table, |
042b0d85 | 7339 | unsigned i) |
65e19f54 | 7340 | { |
042b0d85 | 7341 | return &table->files[i]; |
dafecf19 PB |
7342 | } |
7343 | ||
65e19f54 JA |
7344 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
7345 | int index) | |
7346 | { | |
aeca241b | 7347 | struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index); |
65e19f54 | 7348 | |
a04b0ac0 | 7349 | return (struct file *) (slot->file_ptr & FFS_MASK); |
65e19f54 JA |
7350 | } |
7351 | ||
a04b0ac0 | 7352 | static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file) |
9a321c98 PB |
7353 | { |
7354 | unsigned long file_ptr = (unsigned long) file; | |
7355 | ||
88459b50 | 7356 | file_ptr |= io_file_get_flags(file); |
a04b0ac0 | 7357 | file_slot->file_ptr = file_ptr; |
65e19f54 JA |
7358 | } |
7359 | ||
5106dd6e JA |
7360 | static inline struct file *io_file_get_fixed(struct io_kiocb *req, int fd, |
7361 | unsigned int issue_flags) | |
09bb8394 | 7362 | { |
5106dd6e JA |
7363 | struct io_ring_ctx *ctx = req->ctx; |
7364 | struct file *file = NULL; | |
ac177053 | 7365 | unsigned long file_ptr; |
09bb8394 | 7366 | |
5106dd6e JA |
7367 | if (issue_flags & IO_URING_F_UNLOCKED) |
7368 | mutex_lock(&ctx->uring_lock); | |
7369 | ||
ac177053 | 7370 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
5106dd6e | 7371 | goto out; |
ac177053 PB |
7372 | fd = array_index_nospec(fd, ctx->nr_user_files); |
7373 | file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr; | |
7374 | file = (struct file *) (file_ptr & FFS_MASK); | |
7375 | file_ptr &= ~FFS_MASK; | |
7376 | /* mask in overlapping REQ_F and FFS bits */ | |
35645ac3 | 7377 | req->flags |= (file_ptr << REQ_F_SUPPORT_NOWAIT_BIT); |
5106dd6e JA |
7378 | io_req_set_rsrc_node(req, ctx, 0); |
7379 | out: | |
7380 | if (issue_flags & IO_URING_F_UNLOCKED) | |
7381 | mutex_unlock(&ctx->uring_lock); | |
ac177053 PB |
7382 | return file; |
7383 | } | |
d44f554e | 7384 | |
d5361233 JA |
7385 | /* |
7386 | * Drop the file for requeue operations. Only used of req->file is the | |
7387 | * io_uring descriptor itself. | |
7388 | */ | |
7389 | static void io_drop_inflight_file(struct io_kiocb *req) | |
7390 | { | |
7391 | if (unlikely(req->flags & REQ_F_INFLIGHT)) { | |
7392 | fput(req->file); | |
7393 | req->file = NULL; | |
7394 | req->flags &= ~REQ_F_INFLIGHT; | |
7395 | } | |
7396 | } | |
7397 | ||
5106dd6e | 7398 | static struct file *io_file_get_normal(struct io_kiocb *req, int fd) |
ac177053 | 7399 | { |
62906e89 | 7400 | struct file *file = fget(fd); |
ac177053 | 7401 | |
5106dd6e | 7402 | trace_io_uring_file_get(req->ctx, req, req->user_data, fd); |
09bb8394 | 7403 | |
ac177053 | 7404 | /* we don't allow fixed io_uring files */ |
d5361233 JA |
7405 | if (file && file->f_op == &io_uring_fops) |
7406 | req->flags |= REQ_F_INFLIGHT; | |
8371adf5 | 7407 | return file; |
09bb8394 JA |
7408 | } |
7409 | ||
f237c30a | 7410 | static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked) |
89b263f6 JA |
7411 | { |
7412 | struct io_kiocb *prev = req->timeout.prev; | |
617a8948 | 7413 | int ret = -ENOENT; |
89b263f6 JA |
7414 | |
7415 | if (prev) { | |
617a8948 PB |
7416 | if (!(req->task->flags & PF_EXITING)) |
7417 | ret = io_try_cancel_userdata(req, prev->user_data); | |
505657bc | 7418 | io_req_complete_post(req, ret ?: -ETIME, 0); |
89b263f6 | 7419 | io_put_req(prev); |
89b263f6 JA |
7420 | } else { |
7421 | io_req_complete_post(req, -ETIME, 0); | |
7422 | } | |
7423 | } | |
7424 | ||
2665abfd | 7425 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 7426 | { |
ad8a48ac JA |
7427 | struct io_timeout_data *data = container_of(timer, |
7428 | struct io_timeout_data, timer); | |
90cd7e42 | 7429 | struct io_kiocb *prev, *req = data->req; |
2665abfd | 7430 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 7431 | unsigned long flags; |
2665abfd | 7432 | |
89b263f6 | 7433 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
90cd7e42 PB |
7434 | prev = req->timeout.head; |
7435 | req->timeout.head = NULL; | |
2665abfd JA |
7436 | |
7437 | /* | |
7438 | * We don't expect the list to be empty, that will only happen if we | |
7439 | * race with the completion of the linked work. | |
7440 | */ | |
447c19f3 | 7441 | if (prev) { |
f2f87370 | 7442 | io_remove_next_linked(prev); |
447c19f3 PB |
7443 | if (!req_ref_inc_not_zero(prev)) |
7444 | prev = NULL; | |
7445 | } | |
ef9dd637 | 7446 | list_del(&req->timeout.list); |
89b263f6 JA |
7447 | req->timeout.prev = prev; |
7448 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); | |
2665abfd | 7449 | |
89b263f6 | 7450 | req->io_task_work.func = io_req_task_link_timeout; |
4813c377 | 7451 | io_req_task_work_add(req, false); |
2665abfd JA |
7452 | return HRTIMER_NORESTART; |
7453 | } | |
7454 | ||
de968c18 | 7455 | static void io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 7456 | { |
de968c18 PB |
7457 | struct io_ring_ctx *ctx = req->ctx; |
7458 | ||
89b263f6 | 7459 | spin_lock_irq(&ctx->timeout_lock); |
76a46e06 | 7460 | /* |
f2f87370 PB |
7461 | * If the back reference is NULL, then our linked request finished |
7462 | * before we got a chance to setup the timer | |
76a46e06 | 7463 | */ |
90cd7e42 | 7464 | if (req->timeout.head) { |
e8c2bc1f | 7465 | struct io_timeout_data *data = req->async_data; |
94ae5e77 | 7466 | |
ad8a48ac JA |
7467 | data->timer.function = io_link_timeout_fn; |
7468 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
7469 | data->mode); | |
ef9dd637 | 7470 | list_add_tail(&req->timeout.list, &ctx->ltimeout_list); |
2665abfd | 7471 | } |
89b263f6 | 7472 | spin_unlock_irq(&ctx->timeout_lock); |
2665abfd | 7473 | /* drop submission reference */ |
76a46e06 JA |
7474 | io_put_req(req); |
7475 | } | |
2665abfd | 7476 | |
d475a9a6 PB |
7477 | static void io_queue_sqe_arm_apoll(struct io_kiocb *req) |
7478 | __must_hold(&req->ctx->uring_lock) | |
7479 | { | |
7480 | struct io_kiocb *linked_timeout = io_prep_linked_timeout(req); | |
7481 | ||
4d9237e3 | 7482 | switch (io_arm_poll_handler(req, 0)) { |
d475a9a6 | 7483 | case IO_APOLL_READY: |
d475a9a6 PB |
7484 | io_req_task_queue(req); |
7485 | break; | |
7486 | case IO_APOLL_ABORTED: | |
7487 | /* | |
7488 | * Queued up for async execution, worker will release | |
7489 | * submit reference when the iocb is actually submitted. | |
7490 | */ | |
7491 | io_queue_async_work(req, NULL); | |
7492 | break; | |
b1c62645 | 7493 | case IO_APOLL_OK: |
b1c62645 | 7494 | break; |
d475a9a6 PB |
7495 | } |
7496 | ||
7497 | if (linked_timeout) | |
7498 | io_queue_linked_timeout(linked_timeout); | |
7499 | } | |
7500 | ||
7501 | static inline void __io_queue_sqe(struct io_kiocb *req) | |
282cdc86 | 7502 | __must_hold(&req->ctx->uring_lock) |
2b188cc1 | 7503 | { |
906c6caa | 7504 | struct io_kiocb *linked_timeout; |
e0c5c576 | 7505 | int ret; |
2b188cc1 | 7506 | |
c5eef2b9 | 7507 | ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER); |
193155c8 | 7508 | |
fff4e40e PB |
7509 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
7510 | io_req_add_compl_list(req); | |
d9f9d284 | 7511 | return; |
fff4e40e | 7512 | } |
491381ce JA |
7513 | /* |
7514 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
7515 | * doesn't support non-blocking read/write attempts | |
7516 | */ | |
1840038e | 7517 | if (likely(!ret)) { |
906c6caa PB |
7518 | linked_timeout = io_prep_linked_timeout(req); |
7519 | if (linked_timeout) | |
7520 | io_queue_linked_timeout(linked_timeout); | |
1840038e | 7521 | } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
d475a9a6 | 7522 | io_queue_sqe_arm_apoll(req); |
0d63c148 | 7523 | } else { |
f41db273 | 7524 | io_req_complete_failed(req, ret); |
9e645e11 | 7525 | } |
2b188cc1 JA |
7526 | } |
7527 | ||
4652fe3f | 7528 | static void io_queue_sqe_fallback(struct io_kiocb *req) |
282cdc86 | 7529 | __must_hold(&req->ctx->uring_lock) |
4fe2c963 | 7530 | { |
4652fe3f | 7531 | if (req->flags & REQ_F_FAIL) { |
c6d3d9cb | 7532 | io_req_complete_fail_submit(req); |
e0eb71dc PB |
7533 | } else if (unlikely(req->ctx->drain_active)) { |
7534 | io_drain_req(req); | |
76cc33d7 PB |
7535 | } else { |
7536 | int ret = io_req_prep_async(req); | |
7537 | ||
7538 | if (unlikely(ret)) | |
7539 | io_req_complete_failed(req, ret); | |
7540 | else | |
f237c30a | 7541 | io_queue_async_work(req, NULL); |
ce35a47a | 7542 | } |
4fe2c963 JL |
7543 | } |
7544 | ||
4652fe3f PB |
7545 | static inline void io_queue_sqe(struct io_kiocb *req) |
7546 | __must_hold(&req->ctx->uring_lock) | |
7547 | { | |
7548 | if (likely(!(req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL)))) | |
7549 | __io_queue_sqe(req); | |
7550 | else | |
7551 | io_queue_sqe_fallback(req); | |
7552 | } | |
7553 | ||
b16fed66 PB |
7554 | /* |
7555 | * Check SQE restrictions (opcode and flags). | |
7556 | * | |
7557 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
7558 | */ | |
7559 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
7560 | struct io_kiocb *req, | |
7561 | unsigned int sqe_flags) | |
4fe2c963 | 7562 | { |
b16fed66 PB |
7563 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) |
7564 | return false; | |
7565 | ||
7566 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
7567 | ctx->restrictions.sqe_flags_required) | |
7568 | return false; | |
7569 | ||
7570 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
7571 | ctx->restrictions.sqe_flags_required)) | |
7572 | return false; | |
7573 | ||
7574 | return true; | |
4fe2c963 JL |
7575 | } |
7576 | ||
22b2ca31 PB |
7577 | static void io_init_req_drain(struct io_kiocb *req) |
7578 | { | |
7579 | struct io_ring_ctx *ctx = req->ctx; | |
7580 | struct io_kiocb *head = ctx->submit_state.link.head; | |
7581 | ||
7582 | ctx->drain_active = true; | |
7583 | if (head) { | |
7584 | /* | |
7585 | * If we need to drain a request in the middle of a link, drain | |
7586 | * the head request and the next request/link after the current | |
7587 | * link. Considering sequential execution of links, | |
b6c7db32 | 7588 | * REQ_F_IO_DRAIN will be maintained for every request of our |
22b2ca31 PB |
7589 | * link. |
7590 | */ | |
b6c7db32 | 7591 | head->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC; |
22b2ca31 PB |
7592 | ctx->drain_next = true; |
7593 | } | |
7594 | } | |
7595 | ||
b16fed66 PB |
7596 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
7597 | const struct io_uring_sqe *sqe) | |
282cdc86 | 7598 | __must_hold(&ctx->uring_lock) |
b16fed66 | 7599 | { |
b16fed66 | 7600 | unsigned int sqe_flags; |
fc0ae024 | 7601 | int personality; |
4a04d1d1 | 7602 | u8 opcode; |
b16fed66 | 7603 | |
864ea921 | 7604 | /* req is partially pre-initialised, see io_preinit_req() */ |
4a04d1d1 | 7605 | req->opcode = opcode = READ_ONCE(sqe->opcode); |
b16fed66 PB |
7606 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
7607 | req->flags = sqe_flags = READ_ONCE(sqe->flags); | |
7608 | req->user_data = READ_ONCE(sqe->user_data); | |
b16fed66 | 7609 | req->file = NULL; |
b16fed66 | 7610 | req->fixed_rsrc_refs = NULL; |
b16fed66 | 7611 | req->task = current; |
b16fed66 | 7612 | |
4a04d1d1 PB |
7613 | if (unlikely(opcode >= IORING_OP_LAST)) { |
7614 | req->opcode = 0; | |
b16fed66 | 7615 | return -EINVAL; |
4a04d1d1 | 7616 | } |
68fe256a PB |
7617 | if (unlikely(sqe_flags & ~SQE_COMMON_FLAGS)) { |
7618 | /* enforce forwards compatibility on users */ | |
7619 | if (sqe_flags & ~SQE_VALID_FLAGS) | |
7620 | return -EINVAL; | |
7621 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && | |
4a04d1d1 | 7622 | !io_op_defs[opcode].buffer_select) |
68fe256a | 7623 | return -EOPNOTSUPP; |
5562a8d7 PB |
7624 | if (sqe_flags & IOSQE_CQE_SKIP_SUCCESS) |
7625 | ctx->drain_disabled = true; | |
7626 | if (sqe_flags & IOSQE_IO_DRAIN) { | |
7627 | if (ctx->drain_disabled) | |
7628 | return -EOPNOTSUPP; | |
22b2ca31 | 7629 | io_init_req_drain(req); |
5562a8d7 | 7630 | } |
2a56a9bd PB |
7631 | } |
7632 | if (unlikely(ctx->restricted || ctx->drain_active || ctx->drain_next)) { | |
7633 | if (ctx->restricted && !io_check_restriction(ctx, req, sqe_flags)) | |
7634 | return -EACCES; | |
7635 | /* knock it to the slow queue path, will be drained there */ | |
7636 | if (ctx->drain_active) | |
7637 | req->flags |= REQ_F_FORCE_ASYNC; | |
7638 | /* if there is no link, we're at "next" request and need to drain */ | |
7639 | if (unlikely(ctx->drain_next) && !ctx->submit_state.link.head) { | |
7640 | ctx->drain_next = false; | |
7641 | ctx->drain_active = true; | |
b6c7db32 | 7642 | req->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC; |
2a56a9bd | 7643 | } |
68fe256a | 7644 | } |
b16fed66 | 7645 | |
4a04d1d1 | 7646 | if (io_op_defs[opcode].needs_file) { |
6d63416d PB |
7647 | struct io_submit_state *state = &ctx->submit_state; |
7648 | ||
82733d16 | 7649 | req->fd = READ_ONCE(sqe->fd); |
6bf9c47a | 7650 | |
6d63416d PB |
7651 | /* |
7652 | * Plug now if we have more than 2 IO left after this, and the | |
7653 | * target is potentially a read/write to block based storage. | |
7654 | */ | |
4a04d1d1 | 7655 | if (state->need_plug && io_op_defs[opcode].plug) { |
6d63416d PB |
7656 | state->plug_started = true; |
7657 | state->need_plug = false; | |
5ca7a8b3 | 7658 | blk_start_plug_nr_ios(&state->plug, state->submit_nr); |
6d63416d | 7659 | } |
b16fed66 | 7660 | } |
863e0560 | 7661 | |
003e8dcc JA |
7662 | personality = READ_ONCE(sqe->personality); |
7663 | if (personality) { | |
cdab10bf LT |
7664 | int ret; |
7665 | ||
c10d1f98 PB |
7666 | req->creds = xa_load(&ctx->personalities, personality); |
7667 | if (!req->creds) | |
003e8dcc | 7668 | return -EINVAL; |
c10d1f98 | 7669 | get_cred(req->creds); |
cdc1404a PM |
7670 | ret = security_uring_override_creds(req->creds); |
7671 | if (ret) { | |
7672 | put_cred(req->creds); | |
7673 | return ret; | |
7674 | } | |
b8e64b53 | 7675 | req->flags |= REQ_F_CREDS; |
003e8dcc | 7676 | } |
b16fed66 | 7677 | |
fc0ae024 | 7678 | return io_req_prep(req, sqe); |
b16fed66 PB |
7679 | } |
7680 | ||
a6b8cadc | 7681 | static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
a1ab7b35 | 7682 | const struct io_uring_sqe *sqe) |
282cdc86 | 7683 | __must_hold(&ctx->uring_lock) |
9e645e11 | 7684 | { |
a1ab7b35 | 7685 | struct io_submit_link *link = &ctx->submit_state.link; |
ef4ff581 | 7686 | int ret; |
9e645e11 | 7687 | |
a6b8cadc PB |
7688 | ret = io_init_req(ctx, req, sqe); |
7689 | if (unlikely(ret)) { | |
502c87d6 | 7690 | trace_io_uring_req_failed(sqe, ctx, req, ret); |
a87acfde | 7691 | |
a8295b98 | 7692 | /* fail even hard links since we don't submit */ |
de59bc10 | 7693 | if (link->head) { |
a8295b98 HX |
7694 | /* |
7695 | * we can judge a link req is failed or cancelled by if | |
7696 | * REQ_F_FAIL is set, but the head is an exception since | |
7697 | * it may be set REQ_F_FAIL because of other req's failure | |
7698 | * so let's leverage req->result to distinguish if a head | |
7699 | * is set REQ_F_FAIL because of its failure or other req's | |
7700 | * failure so that we can set the correct ret code for it. | |
7701 | * init result here to avoid affecting the normal path. | |
7702 | */ | |
7703 | if (!(link->head->flags & REQ_F_FAIL)) | |
7704 | req_fail_link_node(link->head, -ECANCELED); | |
7705 | } else if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { | |
7706 | /* | |
7707 | * the current req is a normal req, we should return | |
7708 | * error and thus break the submittion loop. | |
7709 | */ | |
7710 | io_req_complete_failed(req, ret); | |
7711 | return ret; | |
de59bc10 | 7712 | } |
a8295b98 | 7713 | req_fail_link_node(req, ret); |
a6b8cadc | 7714 | } |
441b8a78 | 7715 | |
be7053b7 | 7716 | /* don't need @sqe from now on */ |
502c87d6 | 7717 | trace_io_uring_submit_sqe(ctx, req, req->user_data, req->opcode, |
236daeae OL |
7718 | req->flags, true, |
7719 | ctx->flags & IORING_SETUP_SQPOLL); | |
a6b8cadc | 7720 | |
9e645e11 JA |
7721 | /* |
7722 | * If we already have a head request, queue this one for async | |
7723 | * submittal once the head completes. If we don't have a head but | |
7724 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
7725 | * submitted sync once the chain is complete. If none of those | |
7726 | * conditions are true (normal request), then just queue it. | |
7727 | */ | |
863e0560 PB |
7728 | if (link->head) { |
7729 | struct io_kiocb *head = link->head; | |
4e88d6e7 | 7730 | |
a8295b98 HX |
7731 | if (!(req->flags & REQ_F_FAIL)) { |
7732 | ret = io_req_prep_async(req); | |
7733 | if (unlikely(ret)) { | |
7734 | req_fail_link_node(req, ret); | |
7735 | if (!(head->flags & REQ_F_FAIL)) | |
7736 | req_fail_link_node(head, -ECANCELED); | |
7737 | } | |
7738 | } | |
9d76377f | 7739 | trace_io_uring_link(ctx, req, head); |
f2f87370 | 7740 | link->last->link = req; |
863e0560 | 7741 | link->last = req; |
32fe525b | 7742 | |
f15a3431 PB |
7743 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) |
7744 | return 0; | |
32fe525b | 7745 | /* last request of a link, enqueue the link */ |
f15a3431 PB |
7746 | link->head = NULL; |
7747 | req = head; | |
7748 | } else if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { | |
7749 | link->head = req; | |
7750 | link->last = req; | |
7751 | return 0; | |
9e645e11 | 7752 | } |
2e6e1fde | 7753 | |
f15a3431 | 7754 | io_queue_sqe(req); |
1d4240cc | 7755 | return 0; |
9e645e11 JA |
7756 | } |
7757 | ||
9a56a232 JA |
7758 | /* |
7759 | * Batched submission is done, ensure local IO is flushed out. | |
7760 | */ | |
553deffd | 7761 | static void io_submit_state_end(struct io_ring_ctx *ctx) |
9a56a232 | 7762 | { |
553deffd PB |
7763 | struct io_submit_state *state = &ctx->submit_state; |
7764 | ||
a1ab7b35 | 7765 | if (state->link.head) |
de59bc10 | 7766 | io_queue_sqe(state->link.head); |
553deffd | 7767 | /* flush only after queuing links as they can generate completions */ |
c450178d | 7768 | io_submit_flush_completions(ctx); |
27926b68 JA |
7769 | if (state->plug_started) |
7770 | blk_finish_plug(&state->plug); | |
9a56a232 JA |
7771 | } |
7772 | ||
7773 | /* | |
7774 | * Start submission side cache. | |
7775 | */ | |
7776 | static void io_submit_state_start(struct io_submit_state *state, | |
ba88ff11 | 7777 | unsigned int max_ios) |
9a56a232 | 7778 | { |
27926b68 | 7779 | state->plug_started = false; |
4b628aeb | 7780 | state->need_plug = max_ios > 2; |
5ca7a8b3 | 7781 | state->submit_nr = max_ios; |
a1ab7b35 PB |
7782 | /* set only head, no need to init link_last in advance */ |
7783 | state->link.head = NULL; | |
9a56a232 JA |
7784 | } |
7785 | ||
2b188cc1 JA |
7786 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
7787 | { | |
75b28aff | 7788 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 7789 | |
caf582c6 PB |
7790 | /* |
7791 | * Ensure any loads from the SQEs are done at this point, | |
7792 | * since once we write the new head, the application could | |
7793 | * write new data to them. | |
7794 | */ | |
7795 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
7796 | } |
7797 | ||
2b188cc1 | 7798 | /* |
dd9ae8a0 | 7799 | * Fetch an sqe, if one is available. Note this returns a pointer to memory |
2b188cc1 JA |
7800 | * that is mapped by userspace. This means that care needs to be taken to |
7801 | * ensure that reads are stable, as we cannot rely on userspace always | |
7802 | * being a good citizen. If members of the sqe are validated and then later | |
7803 | * used, it's important that those reads are done through READ_ONCE() to | |
7804 | * prevent a re-load down the line. | |
7805 | */ | |
709b302f | 7806 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 7807 | { |
ea5ab3b5 | 7808 | unsigned head, mask = ctx->sq_entries - 1; |
17d3aeb3 | 7809 | unsigned sq_idx = ctx->cached_sq_head++ & mask; |
2b188cc1 JA |
7810 | |
7811 | /* | |
7812 | * The cached sq head (or cq tail) serves two purposes: | |
7813 | * | |
7814 | * 1) allows us to batch the cost of updating the user visible | |
7815 | * head updates. | |
7816 | * 2) allows the kernel side to track the head on its own, even | |
7817 | * though the application is the one updating it. | |
7818 | */ | |
17d3aeb3 | 7819 | head = READ_ONCE(ctx->sq_array[sq_idx]); |
709b302f PB |
7820 | if (likely(head < ctx->sq_entries)) |
7821 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
7822 | |
7823 | /* drop invalid entries */ | |
15641e42 PB |
7824 | ctx->cq_extra--; |
7825 | WRITE_ONCE(ctx->rings->sq_dropped, | |
7826 | READ_ONCE(ctx->rings->sq_dropped) + 1); | |
709b302f PB |
7827 | return NULL; |
7828 | } | |
7829 | ||
0f212204 | 7830 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
282cdc86 | 7831 | __must_hold(&ctx->uring_lock) |
6c271ce2 | 7832 | { |
69629809 | 7833 | unsigned int entries = io_sqring_entries(ctx); |
46c4e16a | 7834 | int submitted = 0; |
6c271ce2 | 7835 | |
51d48dab | 7836 | if (unlikely(!entries)) |
69629809 | 7837 | return 0; |
ee7d46d9 | 7838 | /* make sure SQ entry isn't read before tail */ |
69629809 | 7839 | nr = min3(nr, ctx->sq_entries, entries); |
9a10867a | 7840 | io_get_task_refs(nr); |
6c271ce2 | 7841 | |
ba88ff11 | 7842 | io_submit_state_start(&ctx->submit_state, nr); |
69629809 | 7843 | do { |
3529d8c2 | 7844 | const struct io_uring_sqe *sqe; |
196be95c | 7845 | struct io_kiocb *req; |
fb5ccc98 | 7846 | |
a33ae9ce | 7847 | if (unlikely(!io_alloc_req_refill(ctx))) { |
196be95c PB |
7848 | if (!submitted) |
7849 | submitted = -EAGAIN; | |
fb5ccc98 | 7850 | break; |
196be95c | 7851 | } |
a33ae9ce | 7852 | req = io_alloc_req(ctx); |
4fccfcbb PB |
7853 | sqe = io_get_sqe(ctx); |
7854 | if (unlikely(!sqe)) { | |
c2b6c6bc | 7855 | wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list); |
4fccfcbb PB |
7856 | break; |
7857 | } | |
d3656344 JA |
7858 | /* will complete beyond this point, count as submitted */ |
7859 | submitted++; | |
bcbb7bf6 JA |
7860 | if (io_submit_sqe(ctx, req, sqe)) { |
7861 | /* | |
7862 | * Continue submitting even for sqe failure if the | |
7863 | * ring was setup with IORING_SETUP_SUBMIT_ALL | |
7864 | */ | |
7865 | if (!(ctx->flags & IORING_SETUP_SUBMIT_ALL)) | |
7866 | break; | |
7867 | } | |
69629809 | 7868 | } while (submitted < nr); |
6c271ce2 | 7869 | |
9466f437 PB |
7870 | if (unlikely(submitted != nr)) { |
7871 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; | |
d8a6df10 | 7872 | int unused = nr - ref_used; |
9466f437 | 7873 | |
09899b19 | 7874 | current->io_uring->cached_refs += unused; |
9466f437 | 7875 | } |
6c271ce2 | 7876 | |
553deffd | 7877 | io_submit_state_end(ctx); |
ae9428ca PB |
7878 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
7879 | io_commit_sqring(ctx); | |
7880 | ||
6c271ce2 JA |
7881 | return submitted; |
7882 | } | |
7883 | ||
e4b6d902 PB |
7884 | static inline bool io_sqd_events_pending(struct io_sq_data *sqd) |
7885 | { | |
7886 | return READ_ONCE(sqd->state); | |
7887 | } | |
7888 | ||
23b3628e XW |
7889 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
7890 | { | |
7891 | /* Tell userspace we may need a wakeup call */ | |
79ebeaee | 7892 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
7893 | WRITE_ONCE(ctx->rings->sq_flags, |
7894 | ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 7895 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
7896 | } |
7897 | ||
7898 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) | |
7899 | { | |
79ebeaee | 7900 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
7901 | WRITE_ONCE(ctx->rings->sq_flags, |
7902 | ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 7903 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
7904 | } |
7905 | ||
08369246 | 7906 | static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) |
6c271ce2 | 7907 | { |
c8d1ba58 | 7908 | unsigned int to_submit; |
bdcd3eab | 7909 | int ret = 0; |
6c271ce2 | 7910 | |
c8d1ba58 | 7911 | to_submit = io_sqring_entries(ctx); |
e95eee2d | 7912 | /* if we're handling multiple rings, cap submit size for fairness */ |
4ce8ad95 OL |
7913 | if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE) |
7914 | to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE; | |
e95eee2d | 7915 | |
5eef4e87 | 7916 | if (!wq_list_empty(&ctx->iopoll_list) || to_submit) { |
948e1947 PB |
7917 | const struct cred *creds = NULL; |
7918 | ||
7919 | if (ctx->sq_creds != current_cred()) | |
7920 | creds = override_creds(ctx->sq_creds); | |
a4c0b3de | 7921 | |
c8d1ba58 | 7922 | mutex_lock(&ctx->uring_lock); |
5eef4e87 | 7923 | if (!wq_list_empty(&ctx->iopoll_list)) |
5ba3c874 | 7924 | io_do_iopoll(ctx, true); |
906a3c6f | 7925 | |
3b763ba1 PB |
7926 | /* |
7927 | * Don't submit if refs are dying, good for io_uring_register(), | |
7928 | * but also it is relied upon by io_ring_exit_work() | |
7929 | */ | |
0298ef96 PB |
7930 | if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) && |
7931 | !(ctx->flags & IORING_SETUP_R_DISABLED)) | |
08369246 | 7932 | ret = io_submit_sqes(ctx, to_submit); |
c8d1ba58 | 7933 | mutex_unlock(&ctx->uring_lock); |
cb318216 | 7934 | |
acfb381d PB |
7935 | if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait)) |
7936 | wake_up(&ctx->sqo_sq_wait); | |
948e1947 PB |
7937 | if (creds) |
7938 | revert_creds(creds); | |
acfb381d | 7939 | } |
6c271ce2 | 7940 | |
08369246 XW |
7941 | return ret; |
7942 | } | |
6c271ce2 | 7943 | |
c072481d | 7944 | static __cold void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
08369246 XW |
7945 | { |
7946 | struct io_ring_ctx *ctx; | |
7947 | unsigned sq_thread_idle = 0; | |
6c271ce2 | 7948 | |
c9dca27d PB |
7949 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7950 | sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle); | |
08369246 | 7951 | sqd->sq_thread_idle = sq_thread_idle; |
c8d1ba58 | 7952 | } |
6c271ce2 | 7953 | |
e4b6d902 PB |
7954 | static bool io_sqd_handle_event(struct io_sq_data *sqd) |
7955 | { | |
7956 | bool did_sig = false; | |
7957 | struct ksignal ksig; | |
7958 | ||
7959 | if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) || | |
7960 | signal_pending(current)) { | |
7961 | mutex_unlock(&sqd->lock); | |
7962 | if (signal_pending(current)) | |
7963 | did_sig = get_signal(&ksig); | |
7964 | cond_resched(); | |
7965 | mutex_lock(&sqd->lock); | |
7966 | } | |
e4b6d902 PB |
7967 | return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
7968 | } | |
7969 | ||
c8d1ba58 JA |
7970 | static int io_sq_thread(void *data) |
7971 | { | |
69fb2131 JA |
7972 | struct io_sq_data *sqd = data; |
7973 | struct io_ring_ctx *ctx; | |
a0d9205f | 7974 | unsigned long timeout = 0; |
37d1e2e3 | 7975 | char buf[TASK_COMM_LEN]; |
08369246 | 7976 | DEFINE_WAIT(wait); |
6c271ce2 | 7977 | |
696ee88a | 7978 | snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid); |
37d1e2e3 | 7979 | set_task_comm(current, buf); |
37d1e2e3 JA |
7980 | |
7981 | if (sqd->sq_cpu != -1) | |
7982 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); | |
7983 | else | |
7984 | set_cpus_allowed_ptr(current, cpu_online_mask); | |
7985 | current->flags |= PF_NO_SETAFFINITY; | |
7986 | ||
5bd2182d PM |
7987 | audit_alloc_kernel(current); |
7988 | ||
09a6f4ef | 7989 | mutex_lock(&sqd->lock); |
e4b6d902 | 7990 | while (1) { |
1a924a80 | 7991 | bool cap_entries, sqt_spin = false; |
c1edbf5f | 7992 | |
e4b6d902 PB |
7993 | if (io_sqd_events_pending(sqd) || signal_pending(current)) { |
7994 | if (io_sqd_handle_event(sqd)) | |
c7d95613 | 7995 | break; |
08369246 XW |
7996 | timeout = jiffies + sqd->sq_thread_idle; |
7997 | } | |
e4b6d902 | 7998 | |
e95eee2d | 7999 | cap_entries = !list_is_singular(&sqd->ctx_list); |
69fb2131 | 8000 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
948e1947 | 8001 | int ret = __io_sq_thread(ctx, cap_entries); |
7c30f36a | 8002 | |
5eef4e87 | 8003 | if (!sqt_spin && (ret > 0 || !wq_list_empty(&ctx->iopoll_list))) |
08369246 | 8004 | sqt_spin = true; |
69fb2131 | 8005 | } |
dd432ea5 PB |
8006 | if (io_run_task_work()) |
8007 | sqt_spin = true; | |
6c271ce2 | 8008 | |
08369246 | 8009 | if (sqt_spin || !time_after(jiffies, timeout)) { |
c8d1ba58 | 8010 | cond_resched(); |
08369246 XW |
8011 | if (sqt_spin) |
8012 | timeout = jiffies + sqd->sq_thread_idle; | |
8013 | continue; | |
8014 | } | |
8015 | ||
08369246 | 8016 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); |
7f62d40d | 8017 | if (!io_sqd_events_pending(sqd) && !task_work_pending(current)) { |
1a924a80 PB |
8018 | bool needs_sched = true; |
8019 | ||
724cb4f9 | 8020 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
aaa9f0f4 PB |
8021 | io_ring_set_wakeup_flag(ctx); |
8022 | ||
724cb4f9 | 8023 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
5eef4e87 | 8024 | !wq_list_empty(&ctx->iopoll_list)) { |
724cb4f9 HX |
8025 | needs_sched = false; |
8026 | break; | |
8027 | } | |
649bb75d AK |
8028 | |
8029 | /* | |
8030 | * Ensure the store of the wakeup flag is not | |
8031 | * reordered with the load of the SQ tail | |
8032 | */ | |
8033 | smp_mb(); | |
8034 | ||
724cb4f9 HX |
8035 | if (io_sqring_entries(ctx)) { |
8036 | needs_sched = false; | |
8037 | break; | |
8038 | } | |
8039 | } | |
8040 | ||
8041 | if (needs_sched) { | |
8042 | mutex_unlock(&sqd->lock); | |
8043 | schedule(); | |
8044 | mutex_lock(&sqd->lock); | |
8045 | } | |
69fb2131 JA |
8046 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
8047 | io_ring_clear_wakeup_flag(ctx); | |
6c271ce2 | 8048 | } |
08369246 XW |
8049 | |
8050 | finish_wait(&sqd->wait, &wait); | |
8051 | timeout = jiffies + sqd->sq_thread_idle; | |
6c271ce2 | 8052 | } |
28cea78a | 8053 | |
78cc687b | 8054 | io_uring_cancel_generic(true, sqd); |
37d1e2e3 | 8055 | sqd->thread = NULL; |
05962f95 | 8056 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
5f3f26f9 | 8057 | io_ring_set_wakeup_flag(ctx); |
521d6a73 | 8058 | io_run_task_work(); |
734551df PB |
8059 | mutex_unlock(&sqd->lock); |
8060 | ||
5bd2182d PM |
8061 | audit_free(current); |
8062 | ||
37d1e2e3 JA |
8063 | complete(&sqd->exited); |
8064 | do_exit(0); | |
6c271ce2 JA |
8065 | } |
8066 | ||
bda52162 JA |
8067 | struct io_wait_queue { |
8068 | struct wait_queue_entry wq; | |
8069 | struct io_ring_ctx *ctx; | |
5fd46178 | 8070 | unsigned cq_tail; |
bda52162 JA |
8071 | unsigned nr_timeouts; |
8072 | }; | |
8073 | ||
6c503150 | 8074 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
bda52162 JA |
8075 | { |
8076 | struct io_ring_ctx *ctx = iowq->ctx; | |
5fd46178 | 8077 | int dist = ctx->cached_cq_tail - (int) iowq->cq_tail; |
bda52162 JA |
8078 | |
8079 | /* | |
d195a66e | 8080 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
8081 | * started waiting. For timeouts, we always want to return to userspace, |
8082 | * regardless of event count. | |
8083 | */ | |
5fd46178 | 8084 | return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
bda52162 JA |
8085 | } |
8086 | ||
8087 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
8088 | int wake_flags, void *key) | |
8089 | { | |
8090 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
8091 | wq); | |
8092 | ||
6c503150 PB |
8093 | /* |
8094 | * Cannot safely flush overflowed CQEs from here, ensure we wake up | |
8095 | * the task, and the next invocation will do it. | |
8096 | */ | |
5ed7a37d | 8097 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->check_cq_overflow)) |
6c503150 PB |
8098 | return autoremove_wake_function(curr, mode, wake_flags, key); |
8099 | return -1; | |
bda52162 JA |
8100 | } |
8101 | ||
af9c1a44 JA |
8102 | static int io_run_task_work_sig(void) |
8103 | { | |
8104 | if (io_run_task_work()) | |
8105 | return 1; | |
0b8cfa97 | 8106 | if (test_thread_flag(TIF_NOTIFY_SIGNAL)) |
792ee0f6 | 8107 | return -ERESTARTSYS; |
c5020bc8 OL |
8108 | if (task_sigpending(current)) |
8109 | return -EINTR; | |
8110 | return 0; | |
af9c1a44 JA |
8111 | } |
8112 | ||
eeb60b9a PB |
8113 | /* when returns >0, the caller should retry */ |
8114 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, | |
8115 | struct io_wait_queue *iowq, | |
22833966 | 8116 | ktime_t timeout) |
eeb60b9a PB |
8117 | { |
8118 | int ret; | |
8119 | ||
8120 | /* make sure we run task_work before checking for signals */ | |
8121 | ret = io_run_task_work_sig(); | |
8122 | if (ret || io_should_wake(iowq)) | |
8123 | return ret; | |
8124 | /* let the caller flush overflows, retry */ | |
5ed7a37d | 8125 | if (test_bit(0, &ctx->check_cq_overflow)) |
eeb60b9a PB |
8126 | return 1; |
8127 | ||
22833966 JA |
8128 | if (!schedule_hrtimeout(&timeout, HRTIMER_MODE_ABS)) |
8129 | return -ETIME; | |
8130 | return 1; | |
eeb60b9a PB |
8131 | } |
8132 | ||
2b188cc1 JA |
8133 | /* |
8134 | * Wait until events become available, if we don't already have some. The | |
8135 | * application must reap them itself, as they reside on the shared cq ring. | |
8136 | */ | |
8137 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
c73ebb68 HX |
8138 | const sigset_t __user *sig, size_t sigsz, |
8139 | struct __kernel_timespec __user *uts) | |
2b188cc1 | 8140 | { |
90291099 | 8141 | struct io_wait_queue iowq; |
75b28aff | 8142 | struct io_rings *rings = ctx->rings; |
22833966 | 8143 | ktime_t timeout = KTIME_MAX; |
c1d5a224 | 8144 | int ret; |
2b188cc1 | 8145 | |
b41e9852 | 8146 | do { |
90f67366 | 8147 | io_cqring_overflow_flush(ctx); |
6c503150 | 8148 | if (io_cqring_events(ctx) >= min_events) |
b41e9852 | 8149 | return 0; |
4c6e277c | 8150 | if (!io_run_task_work()) |
b41e9852 | 8151 | break; |
b41e9852 | 8152 | } while (1); |
2b188cc1 JA |
8153 | |
8154 | if (sig) { | |
9e75ad5d AB |
8155 | #ifdef CONFIG_COMPAT |
8156 | if (in_compat_syscall()) | |
8157 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 8158 | sigsz); |
9e75ad5d AB |
8159 | else |
8160 | #endif | |
b772434b | 8161 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 8162 | |
2b188cc1 JA |
8163 | if (ret) |
8164 | return ret; | |
8165 | } | |
8166 | ||
950e79dd OL |
8167 | if (uts) { |
8168 | struct timespec64 ts; | |
8169 | ||
8170 | if (get_timespec64(&ts, uts)) | |
8171 | return -EFAULT; | |
8172 | timeout = ktime_add_ns(timespec64_to_ktime(ts), ktime_get_ns()); | |
8173 | } | |
8174 | ||
90291099 PB |
8175 | init_waitqueue_func_entry(&iowq.wq, io_wake_function); |
8176 | iowq.wq.private = current; | |
8177 | INIT_LIST_HEAD(&iowq.wq.entry); | |
8178 | iowq.ctx = ctx; | |
bda52162 | 8179 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
5fd46178 | 8180 | iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events; |
90291099 | 8181 | |
c826bd7a | 8182 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 | 8183 | do { |
ca0a2651 | 8184 | /* if we can't even flush overflow, don't wait for more */ |
90f67366 | 8185 | if (!io_cqring_overflow_flush(ctx)) { |
ca0a2651 JA |
8186 | ret = -EBUSY; |
8187 | break; | |
8188 | } | |
311997b3 | 8189 | prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq, |
bda52162 | 8190 | TASK_INTERRUPTIBLE); |
22833966 | 8191 | ret = io_cqring_wait_schedule(ctx, &iowq, timeout); |
311997b3 | 8192 | finish_wait(&ctx->cq_wait, &iowq.wq); |
ca0a2651 | 8193 | cond_resched(); |
eeb60b9a | 8194 | } while (ret > 0); |
bda52162 | 8195 | |
b7db41c9 | 8196 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 8197 | |
75b28aff | 8198 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
8199 | } |
8200 | ||
9123c8ff | 8201 | static void io_free_page_table(void **table, size_t size) |
05f3fb3c | 8202 | { |
9123c8ff | 8203 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); |
05f3fb3c | 8204 | |
846a4ef2 | 8205 | for (i = 0; i < nr_tables; i++) |
9123c8ff PB |
8206 | kfree(table[i]); |
8207 | kfree(table); | |
8208 | } | |
8209 | ||
c072481d | 8210 | static __cold void **io_alloc_page_table(size_t size) |
9123c8ff PB |
8211 | { |
8212 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); | |
8213 | size_t init_size = size; | |
8214 | void **table; | |
8215 | ||
0bea96f5 | 8216 | table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
8217 | if (!table) |
8218 | return NULL; | |
8219 | ||
8220 | for (i = 0; i < nr_tables; i++) { | |
27f6b318 | 8221 | unsigned int this_size = min_t(size_t, size, PAGE_SIZE); |
9123c8ff | 8222 | |
0bea96f5 | 8223 | table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
8224 | if (!table[i]) { |
8225 | io_free_page_table(table, init_size); | |
8226 | return NULL; | |
8227 | } | |
8228 | size -= this_size; | |
8229 | } | |
8230 | return table; | |
05f3fb3c JA |
8231 | } |
8232 | ||
28a9fe25 | 8233 | static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node) |
1642b445 | 8234 | { |
28a9fe25 PB |
8235 | percpu_ref_exit(&ref_node->refs); |
8236 | kfree(ref_node); | |
1642b445 PB |
8237 | } |
8238 | ||
c072481d | 8239 | static __cold void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
b9bd2bea PB |
8240 | { |
8241 | struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs); | |
8242 | struct io_ring_ctx *ctx = node->rsrc_data->ctx; | |
8243 | unsigned long flags; | |
8244 | bool first_add = false; | |
b36a2050 | 8245 | unsigned long delay = HZ; |
b9bd2bea PB |
8246 | |
8247 | spin_lock_irqsave(&ctx->rsrc_ref_lock, flags); | |
8248 | node->done = true; | |
8249 | ||
b36a2050 DY |
8250 | /* if we are mid-quiesce then do not delay */ |
8251 | if (node->rsrc_data->quiesce) | |
8252 | delay = 0; | |
8253 | ||
b9bd2bea PB |
8254 | while (!list_empty(&ctx->rsrc_ref_list)) { |
8255 | node = list_first_entry(&ctx->rsrc_ref_list, | |
8256 | struct io_rsrc_node, node); | |
8257 | /* recycle ref nodes in order */ | |
8258 | if (!node->done) | |
8259 | break; | |
8260 | list_del(&node->node); | |
8261 | first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist); | |
8262 | } | |
8263 | spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags); | |
8264 | ||
8265 | if (first_add) | |
b36a2050 | 8266 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, delay); |
b9bd2bea PB |
8267 | } |
8268 | ||
f6133fbd | 8269 | static struct io_rsrc_node *io_rsrc_node_alloc(void) |
b9bd2bea PB |
8270 | { |
8271 | struct io_rsrc_node *ref_node; | |
8272 | ||
8273 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); | |
8274 | if (!ref_node) | |
8275 | return NULL; | |
8276 | ||
8277 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, | |
8278 | 0, GFP_KERNEL)) { | |
8279 | kfree(ref_node); | |
8280 | return NULL; | |
8281 | } | |
8282 | INIT_LIST_HEAD(&ref_node->node); | |
8283 | INIT_LIST_HEAD(&ref_node->rsrc_list); | |
8284 | ref_node->done = false; | |
8285 | return ref_node; | |
8286 | } | |
8287 | ||
a7f0ed5a PB |
8288 | static void io_rsrc_node_switch(struct io_ring_ctx *ctx, |
8289 | struct io_rsrc_data *data_to_kill) | |
ab409402 | 8290 | __must_hold(&ctx->uring_lock) |
6b06314c | 8291 | { |
a7f0ed5a PB |
8292 | WARN_ON_ONCE(!ctx->rsrc_backup_node); |
8293 | WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node); | |
6b06314c | 8294 | |
ab409402 PB |
8295 | io_rsrc_refs_drop(ctx); |
8296 | ||
a7f0ed5a PB |
8297 | if (data_to_kill) { |
8298 | struct io_rsrc_node *rsrc_node = ctx->rsrc_node; | |
82fbcfa9 | 8299 | |
a7f0ed5a | 8300 | rsrc_node->rsrc_data = data_to_kill; |
4956b9ea | 8301 | spin_lock_irq(&ctx->rsrc_ref_lock); |
a7f0ed5a | 8302 | list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list); |
4956b9ea | 8303 | spin_unlock_irq(&ctx->rsrc_ref_lock); |
82fbcfa9 | 8304 | |
3e942498 | 8305 | atomic_inc(&data_to_kill->refs); |
a7f0ed5a PB |
8306 | percpu_ref_kill(&rsrc_node->refs); |
8307 | ctx->rsrc_node = NULL; | |
8308 | } | |
6b06314c | 8309 | |
a7f0ed5a PB |
8310 | if (!ctx->rsrc_node) { |
8311 | ctx->rsrc_node = ctx->rsrc_backup_node; | |
8312 | ctx->rsrc_backup_node = NULL; | |
8313 | } | |
8bad28d8 HX |
8314 | } |
8315 | ||
a7f0ed5a | 8316 | static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx) |
8dd03afe PB |
8317 | { |
8318 | if (ctx->rsrc_backup_node) | |
8319 | return 0; | |
f6133fbd | 8320 | ctx->rsrc_backup_node = io_rsrc_node_alloc(); |
8dd03afe | 8321 | return ctx->rsrc_backup_node ? 0 : -ENOMEM; |
8bad28d8 HX |
8322 | } |
8323 | ||
c072481d PB |
8324 | static __cold int io_rsrc_ref_quiesce(struct io_rsrc_data *data, |
8325 | struct io_ring_ctx *ctx) | |
8bad28d8 HX |
8326 | { |
8327 | int ret; | |
05589553 | 8328 | |
215c3902 | 8329 | /* As we may drop ->uring_lock, other task may have started quiesce */ |
8bad28d8 HX |
8330 | if (data->quiesce) |
8331 | return -ENXIO; | |
05589553 | 8332 | |
8bad28d8 | 8333 | data->quiesce = true; |
1ffc5422 | 8334 | do { |
a7f0ed5a | 8335 | ret = io_rsrc_node_switch_start(ctx); |
8dd03afe | 8336 | if (ret) |
f2303b1f | 8337 | break; |
a7f0ed5a | 8338 | io_rsrc_node_switch(ctx, data); |
f2303b1f | 8339 | |
3e942498 PB |
8340 | /* kill initial ref, already quiesced if zero */ |
8341 | if (atomic_dec_and_test(&data->refs)) | |
8342 | break; | |
c018db4a | 8343 | mutex_unlock(&ctx->uring_lock); |
8bad28d8 | 8344 | flush_delayed_work(&ctx->rsrc_put_work); |
1ffc5422 | 8345 | ret = wait_for_completion_interruptible(&data->done); |
c018db4a JA |
8346 | if (!ret) { |
8347 | mutex_lock(&ctx->uring_lock); | |
80912cef DY |
8348 | if (atomic_read(&data->refs) > 0) { |
8349 | /* | |
8350 | * it has been revived by another thread while | |
8351 | * we were unlocked | |
8352 | */ | |
8353 | mutex_unlock(&ctx->uring_lock); | |
8354 | } else { | |
8355 | break; | |
8356 | } | |
c018db4a | 8357 | } |
8bad28d8 | 8358 | |
3e942498 PB |
8359 | atomic_inc(&data->refs); |
8360 | /* wait for all works potentially completing data->done */ | |
8361 | flush_delayed_work(&ctx->rsrc_put_work); | |
cb5e1b81 | 8362 | reinit_completion(&data->done); |
8dd03afe | 8363 | |
1ffc5422 | 8364 | ret = io_run_task_work_sig(); |
8bad28d8 | 8365 | mutex_lock(&ctx->uring_lock); |
f2303b1f | 8366 | } while (ret >= 0); |
8bad28d8 | 8367 | data->quiesce = false; |
05f3fb3c | 8368 | |
8bad28d8 | 8369 | return ret; |
d7954b2b BM |
8370 | } |
8371 | ||
2d091d62 PB |
8372 | static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx) |
8373 | { | |
8374 | unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK; | |
8375 | unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT; | |
8376 | ||
8377 | return &data->tags[table_idx][off]; | |
8378 | } | |
8379 | ||
44b31f2f | 8380 | static void io_rsrc_data_free(struct io_rsrc_data *data) |
1ad555c6 | 8381 | { |
2d091d62 PB |
8382 | size_t size = data->nr * sizeof(data->tags[0][0]); |
8383 | ||
8384 | if (data->tags) | |
8385 | io_free_page_table((void **)data->tags, size); | |
44b31f2f PB |
8386 | kfree(data); |
8387 | } | |
8388 | ||
c072481d PB |
8389 | static __cold int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put, |
8390 | u64 __user *utags, unsigned nr, | |
8391 | struct io_rsrc_data **pdata) | |
1ad555c6 | 8392 | { |
b895c9a6 | 8393 | struct io_rsrc_data *data; |
2d091d62 | 8394 | int ret = -ENOMEM; |
d878c816 | 8395 | unsigned i; |
1ad555c6 BM |
8396 | |
8397 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
8398 | if (!data) | |
d878c816 | 8399 | return -ENOMEM; |
2d091d62 | 8400 | data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0])); |
b60c8dce | 8401 | if (!data->tags) { |
1ad555c6 | 8402 | kfree(data); |
d878c816 PB |
8403 | return -ENOMEM; |
8404 | } | |
2d091d62 PB |
8405 | |
8406 | data->nr = nr; | |
8407 | data->ctx = ctx; | |
8408 | data->do_put = do_put; | |
d878c816 | 8409 | if (utags) { |
2d091d62 | 8410 | ret = -EFAULT; |
d878c816 | 8411 | for (i = 0; i < nr; i++) { |
fdd1dc31 CIK |
8412 | u64 *tag_slot = io_get_tag_slot(data, i); |
8413 | ||
8414 | if (copy_from_user(tag_slot, &utags[i], | |
8415 | sizeof(*tag_slot))) | |
2d091d62 | 8416 | goto fail; |
d878c816 | 8417 | } |
1ad555c6 | 8418 | } |
b60c8dce | 8419 | |
3e942498 | 8420 | atomic_set(&data->refs, 1); |
1ad555c6 | 8421 | init_completion(&data->done); |
d878c816 PB |
8422 | *pdata = data; |
8423 | return 0; | |
2d091d62 PB |
8424 | fail: |
8425 | io_rsrc_data_free(data); | |
8426 | return ret; | |
1ad555c6 BM |
8427 | } |
8428 | ||
9123c8ff PB |
8429 | static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files) |
8430 | { | |
0bea96f5 PB |
8431 | table->files = kvcalloc(nr_files, sizeof(table->files[0]), |
8432 | GFP_KERNEL_ACCOUNT); | |
9123c8ff PB |
8433 | return !!table->files; |
8434 | } | |
8435 | ||
042b0d85 | 8436 | static void io_free_file_tables(struct io_file_table *table) |
9123c8ff | 8437 | { |
042b0d85 | 8438 | kvfree(table->files); |
9123c8ff PB |
8439 | table->files = NULL; |
8440 | } | |
8441 | ||
fff4db76 | 8442 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
1ad555c6 | 8443 | { |
fff4db76 PB |
8444 | #if defined(CONFIG_UNIX) |
8445 | if (ctx->ring_sock) { | |
8446 | struct sock *sock = ctx->ring_sock->sk; | |
8447 | struct sk_buff *skb; | |
8448 | ||
8449 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
8450 | kfree_skb(skb); | |
8451 | } | |
8452 | #else | |
8453 | int i; | |
8454 | ||
8455 | for (i = 0; i < ctx->nr_user_files; i++) { | |
8456 | struct file *file; | |
8457 | ||
8458 | file = io_file_from_index(ctx, i); | |
8459 | if (file) | |
8460 | fput(file); | |
8461 | } | |
8462 | #endif | |
042b0d85 | 8463 | io_free_file_tables(&ctx->file_table); |
44b31f2f | 8464 | io_rsrc_data_free(ctx->file_data); |
fff4db76 PB |
8465 | ctx->file_data = NULL; |
8466 | ctx->nr_user_files = 0; | |
1ad555c6 BM |
8467 | } |
8468 | ||
d7954b2b BM |
8469 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
8470 | { | |
d7954b2b BM |
8471 | int ret; |
8472 | ||
08480400 | 8473 | if (!ctx->file_data) |
d7954b2b | 8474 | return -ENXIO; |
08480400 PB |
8475 | ret = io_rsrc_ref_quiesce(ctx->file_data, ctx); |
8476 | if (!ret) | |
8477 | __io_sqe_files_unregister(ctx); | |
8478 | return ret; | |
6b06314c JA |
8479 | } |
8480 | ||
37d1e2e3 | 8481 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
09a6f4ef | 8482 | __releases(&sqd->lock) |
37d1e2e3 | 8483 | { |
521d6a73 PB |
8484 | WARN_ON_ONCE(sqd->thread == current); |
8485 | ||
9e138a48 PB |
8486 | /* |
8487 | * Do the dance but not conditional clear_bit() because it'd race with | |
8488 | * other threads incrementing park_pending and setting the bit. | |
8489 | */ | |
37d1e2e3 | 8490 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
9e138a48 PB |
8491 | if (atomic_dec_return(&sqd->park_pending)) |
8492 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); | |
09a6f4ef | 8493 | mutex_unlock(&sqd->lock); |
37d1e2e3 JA |
8494 | } |
8495 | ||
86e0d676 | 8496 | static void io_sq_thread_park(struct io_sq_data *sqd) |
09a6f4ef | 8497 | __acquires(&sqd->lock) |
37d1e2e3 | 8498 | { |
521d6a73 PB |
8499 | WARN_ON_ONCE(sqd->thread == current); |
8500 | ||
9e138a48 | 8501 | atomic_inc(&sqd->park_pending); |
86e0d676 | 8502 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
09a6f4ef | 8503 | mutex_lock(&sqd->lock); |
05962f95 | 8504 | if (sqd->thread) |
86e0d676 | 8505 | wake_up_process(sqd->thread); |
37d1e2e3 JA |
8506 | } |
8507 | ||
8508 | static void io_sq_thread_stop(struct io_sq_data *sqd) | |
8509 | { | |
521d6a73 | 8510 | WARN_ON_ONCE(sqd->thread == current); |
88885f66 | 8511 | WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)); |
521d6a73 | 8512 | |
05962f95 | 8513 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
88885f66 | 8514 | mutex_lock(&sqd->lock); |
e8f98f24 JA |
8515 | if (sqd->thread) |
8516 | wake_up_process(sqd->thread); | |
09a6f4ef | 8517 | mutex_unlock(&sqd->lock); |
05962f95 | 8518 | wait_for_completion(&sqd->exited); |
37d1e2e3 JA |
8519 | } |
8520 | ||
534ca6d6 | 8521 | static void io_put_sq_data(struct io_sq_data *sqd) |
6c271ce2 | 8522 | { |
534ca6d6 | 8523 | if (refcount_dec_and_test(&sqd->refs)) { |
9e138a48 PB |
8524 | WARN_ON_ONCE(atomic_read(&sqd->park_pending)); |
8525 | ||
37d1e2e3 JA |
8526 | io_sq_thread_stop(sqd); |
8527 | kfree(sqd); | |
8528 | } | |
8529 | } | |
8530 | ||
8531 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) | |
8532 | { | |
8533 | struct io_sq_data *sqd = ctx->sq_data; | |
8534 | ||
8535 | if (sqd) { | |
05962f95 | 8536 | io_sq_thread_park(sqd); |
521d6a73 | 8537 | list_del_init(&ctx->sqd_list); |
37d1e2e3 | 8538 | io_sqd_update_thread_idle(sqd); |
05962f95 | 8539 | io_sq_thread_unpark(sqd); |
37d1e2e3 JA |
8540 | |
8541 | io_put_sq_data(sqd); | |
8542 | ctx->sq_data = NULL; | |
534ca6d6 JA |
8543 | } |
8544 | } | |
8545 | ||
aa06165d JA |
8546 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
8547 | { | |
8548 | struct io_ring_ctx *ctx_attach; | |
8549 | struct io_sq_data *sqd; | |
8550 | struct fd f; | |
8551 | ||
8552 | f = fdget(p->wq_fd); | |
8553 | if (!f.file) | |
8554 | return ERR_PTR(-ENXIO); | |
8555 | if (f.file->f_op != &io_uring_fops) { | |
8556 | fdput(f); | |
8557 | return ERR_PTR(-EINVAL); | |
8558 | } | |
8559 | ||
8560 | ctx_attach = f.file->private_data; | |
8561 | sqd = ctx_attach->sq_data; | |
8562 | if (!sqd) { | |
8563 | fdput(f); | |
8564 | return ERR_PTR(-EINVAL); | |
8565 | } | |
5c2469e0 JA |
8566 | if (sqd->task_tgid != current->tgid) { |
8567 | fdput(f); | |
8568 | return ERR_PTR(-EPERM); | |
8569 | } | |
aa06165d JA |
8570 | |
8571 | refcount_inc(&sqd->refs); | |
8572 | fdput(f); | |
8573 | return sqd; | |
8574 | } | |
8575 | ||
26984fbf PB |
8576 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p, |
8577 | bool *attached) | |
534ca6d6 JA |
8578 | { |
8579 | struct io_sq_data *sqd; | |
8580 | ||
26984fbf | 8581 | *attached = false; |
5c2469e0 JA |
8582 | if (p->flags & IORING_SETUP_ATTACH_WQ) { |
8583 | sqd = io_attach_sq_data(p); | |
26984fbf PB |
8584 | if (!IS_ERR(sqd)) { |
8585 | *attached = true; | |
5c2469e0 | 8586 | return sqd; |
26984fbf | 8587 | } |
5c2469e0 JA |
8588 | /* fall through for EPERM case, setup new sqd/task */ |
8589 | if (PTR_ERR(sqd) != -EPERM) | |
8590 | return sqd; | |
8591 | } | |
aa06165d | 8592 | |
534ca6d6 JA |
8593 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
8594 | if (!sqd) | |
8595 | return ERR_PTR(-ENOMEM); | |
8596 | ||
9e138a48 | 8597 | atomic_set(&sqd->park_pending, 0); |
534ca6d6 | 8598 | refcount_set(&sqd->refs, 1); |
69fb2131 | 8599 | INIT_LIST_HEAD(&sqd->ctx_list); |
09a6f4ef | 8600 | mutex_init(&sqd->lock); |
534ca6d6 | 8601 | init_waitqueue_head(&sqd->wait); |
37d1e2e3 | 8602 | init_completion(&sqd->exited); |
534ca6d6 JA |
8603 | return sqd; |
8604 | } | |
8605 | ||
6b06314c | 8606 | #if defined(CONFIG_UNIX) |
6b06314c JA |
8607 | /* |
8608 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
8609 | * the io_uring can be safely unregistered on process exit, even if we have | |
8610 | * loops in the file referencing. | |
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 | ||
8636 | if (!file) | |
08a45173 | 8637 | continue; |
65e19f54 | 8638 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
8639 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
8640 | nr_files++; | |
6b06314c JA |
8641 | } |
8642 | ||
08a45173 JA |
8643 | if (nr_files) { |
8644 | fpl->max = SCM_MAX_FD; | |
8645 | fpl->count = nr_files; | |
8646 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 8647 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
8648 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
8649 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 8650 | |
a07211e3 PB |
8651 | for (i = 0; i < nr; i++) { |
8652 | struct file *file = io_file_from_index(ctx, i + offset); | |
8653 | ||
8654 | if (file) | |
8655 | fput(file); | |
8656 | } | |
08a45173 JA |
8657 | } else { |
8658 | kfree_skb(skb); | |
c86d18f4 | 8659 | free_uid(fpl->user); |
08a45173 JA |
8660 | kfree(fpl); |
8661 | } | |
6b06314c JA |
8662 | |
8663 | return 0; | |
8664 | } | |
8665 | ||
8666 | /* | |
8667 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
8668 | * causes regular reference counting to break down. We rely on the UNIX | |
8669 | * garbage collection to take care of this problem for us. | |
8670 | */ | |
8671 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
8672 | { | |
8673 | unsigned left, total; | |
8674 | int ret = 0; | |
8675 | ||
8676 | total = 0; | |
8677 | left = ctx->nr_user_files; | |
8678 | while (left) { | |
8679 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
8680 | |
8681 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
8682 | if (ret) | |
8683 | break; | |
8684 | left -= this_files; | |
8685 | total += this_files; | |
8686 | } | |
8687 | ||
8688 | if (!ret) | |
8689 | return 0; | |
8690 | ||
8691 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
8692 | struct file *file = io_file_from_index(ctx, total); |
8693 | ||
8694 | if (file) | |
8695 | fput(file); | |
6b06314c JA |
8696 | total++; |
8697 | } | |
8698 | ||
8699 | return ret; | |
8700 | } | |
8701 | #else | |
8702 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
8703 | { | |
8704 | return 0; | |
8705 | } | |
8706 | #endif | |
8707 | ||
47e90392 | 8708 | static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
05f3fb3c | 8709 | { |
50238531 | 8710 | struct file *file = prsrc->file; |
05f3fb3c JA |
8711 | #if defined(CONFIG_UNIX) |
8712 | struct sock *sock = ctx->ring_sock->sk; | |
8713 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
8714 | struct sk_buff *skb; | |
8715 | int i; | |
8716 | ||
8717 | __skb_queue_head_init(&list); | |
8718 | ||
8719 | /* | |
8720 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
8721 | * remove this entry and rearrange the file array. | |
8722 | */ | |
8723 | skb = skb_dequeue(head); | |
8724 | while (skb) { | |
8725 | struct scm_fp_list *fp; | |
8726 | ||
8727 | fp = UNIXCB(skb).fp; | |
8728 | for (i = 0; i < fp->count; i++) { | |
8729 | int left; | |
8730 | ||
8731 | if (fp->fp[i] != file) | |
8732 | continue; | |
8733 | ||
8734 | unix_notinflight(fp->user, fp->fp[i]); | |
8735 | left = fp->count - 1 - i; | |
8736 | if (left) { | |
8737 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
8738 | left * sizeof(struct file *)); | |
8739 | } | |
8740 | fp->count--; | |
8741 | if (!fp->count) { | |
8742 | kfree_skb(skb); | |
8743 | skb = NULL; | |
8744 | } else { | |
8745 | __skb_queue_tail(&list, skb); | |
8746 | } | |
8747 | fput(file); | |
8748 | file = NULL; | |
8749 | break; | |
8750 | } | |
8751 | ||
8752 | if (!file) | |
8753 | break; | |
8754 | ||
8755 | __skb_queue_tail(&list, skb); | |
8756 | ||
8757 | skb = skb_dequeue(head); | |
8758 | } | |
8759 | ||
8760 | if (skb_peek(&list)) { | |
8761 | spin_lock_irq(&head->lock); | |
8762 | while ((skb = __skb_dequeue(&list)) != NULL) | |
8763 | __skb_queue_tail(head, skb); | |
8764 | spin_unlock_irq(&head->lock); | |
8765 | } | |
8766 | #else | |
8767 | fput(file); | |
8768 | #endif | |
8769 | } | |
8770 | ||
b895c9a6 | 8771 | static void __io_rsrc_put_work(struct io_rsrc_node *ref_node) |
65e19f54 | 8772 | { |
b895c9a6 | 8773 | struct io_rsrc_data *rsrc_data = ref_node->rsrc_data; |
269bbe5f BM |
8774 | struct io_ring_ctx *ctx = rsrc_data->ctx; |
8775 | struct io_rsrc_put *prsrc, *tmp; | |
05589553 | 8776 | |
269bbe5f BM |
8777 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
8778 | list_del(&prsrc->list); | |
b60c8dce PB |
8779 | |
8780 | if (prsrc->tag) { | |
8781 | bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL; | |
b60c8dce PB |
8782 | |
8783 | io_ring_submit_lock(ctx, lock_ring); | |
79ebeaee | 8784 | spin_lock(&ctx->completion_lock); |
913a571a | 8785 | io_fill_cqe_aux(ctx, prsrc->tag, 0, 0); |
b60c8dce | 8786 | io_commit_cqring(ctx); |
79ebeaee | 8787 | spin_unlock(&ctx->completion_lock); |
b60c8dce PB |
8788 | io_cqring_ev_posted(ctx); |
8789 | io_ring_submit_unlock(ctx, lock_ring); | |
8790 | } | |
8791 | ||
40ae0ff7 | 8792 | rsrc_data->do_put(ctx, prsrc); |
269bbe5f | 8793 | kfree(prsrc); |
65e19f54 | 8794 | } |
05589553 | 8795 | |
28a9fe25 | 8796 | io_rsrc_node_destroy(ref_node); |
3e942498 PB |
8797 | if (atomic_dec_and_test(&rsrc_data->refs)) |
8798 | complete(&rsrc_data->done); | |
2faf852d | 8799 | } |
65e19f54 | 8800 | |
269bbe5f | 8801 | static void io_rsrc_put_work(struct work_struct *work) |
4a38aed2 JA |
8802 | { |
8803 | struct io_ring_ctx *ctx; | |
8804 | struct llist_node *node; | |
8805 | ||
269bbe5f BM |
8806 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
8807 | node = llist_del_all(&ctx->rsrc_put_llist); | |
4a38aed2 JA |
8808 | |
8809 | while (node) { | |
b895c9a6 | 8810 | struct io_rsrc_node *ref_node; |
4a38aed2 JA |
8811 | struct llist_node *next = node->next; |
8812 | ||
b895c9a6 | 8813 | ref_node = llist_entry(node, struct io_rsrc_node, llist); |
269bbe5f | 8814 | __io_rsrc_put_work(ref_node); |
4a38aed2 JA |
8815 | node = next; |
8816 | } | |
8817 | } | |
8818 | ||
6b06314c | 8819 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
792e3582 | 8820 | unsigned nr_args, u64 __user *tags) |
6b06314c JA |
8821 | { |
8822 | __s32 __user *fds = (__s32 __user *) arg; | |
05f3fb3c | 8823 | struct file *file; |
f3baed39 | 8824 | int fd, ret; |
846a4ef2 | 8825 | unsigned i; |
6b06314c | 8826 | |
05f3fb3c | 8827 | if (ctx->file_data) |
6b06314c JA |
8828 | return -EBUSY; |
8829 | if (!nr_args) | |
8830 | return -EINVAL; | |
8831 | if (nr_args > IORING_MAX_FIXED_FILES) | |
8832 | return -EMFILE; | |
3a1b8a4e PB |
8833 | if (nr_args > rlimit(RLIMIT_NOFILE)) |
8834 | return -EMFILE; | |
a7f0ed5a | 8835 | ret = io_rsrc_node_switch_start(ctx); |
f3baed39 PB |
8836 | if (ret) |
8837 | return ret; | |
d878c816 PB |
8838 | ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args, |
8839 | &ctx->file_data); | |
8840 | if (ret) | |
8841 | return ret; | |
6b06314c | 8842 | |
f3baed39 | 8843 | ret = -ENOMEM; |
aeca241b | 8844 | if (!io_alloc_file_tables(&ctx->file_table, nr_args)) |
1ad555c6 | 8845 | goto out_free; |
65e19f54 | 8846 | |
08a45173 | 8847 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
d878c816 | 8848 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
600cf3f8 PB |
8849 | ret = -EFAULT; |
8850 | goto out_fput; | |
8851 | } | |
08a45173 | 8852 | /* allow sparse sets */ |
792e3582 PB |
8853 | if (fd == -1) { |
8854 | ret = -EINVAL; | |
2d091d62 | 8855 | if (unlikely(*io_get_tag_slot(ctx->file_data, i))) |
792e3582 | 8856 | goto out_fput; |
08a45173 | 8857 | continue; |
792e3582 | 8858 | } |
6b06314c | 8859 | |
05f3fb3c | 8860 | file = fget(fd); |
6b06314c | 8861 | ret = -EBADF; |
792e3582 | 8862 | if (unlikely(!file)) |
600cf3f8 | 8863 | goto out_fput; |
05f3fb3c | 8864 | |
6b06314c JA |
8865 | /* |
8866 | * Don't allow io_uring instances to be registered. If UNIX | |
8867 | * isn't enabled, then this causes a reference cycle and this | |
8868 | * instance can never get freed. If UNIX is enabled we'll | |
8869 | * handle it just fine, but there's still no point in allowing | |
8870 | * a ring fd as it doesn't support regular read/write anyway. | |
8871 | */ | |
05f3fb3c JA |
8872 | if (file->f_op == &io_uring_fops) { |
8873 | fput(file); | |
600cf3f8 | 8874 | goto out_fput; |
6b06314c | 8875 | } |
aeca241b | 8876 | io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file); |
6b06314c JA |
8877 | } |
8878 | ||
6b06314c | 8879 | ret = io_sqe_files_scm(ctx); |
05589553 | 8880 | if (ret) { |
08480400 | 8881 | __io_sqe_files_unregister(ctx); |
05589553 XW |
8882 | return ret; |
8883 | } | |
6b06314c | 8884 | |
a7f0ed5a | 8885 | io_rsrc_node_switch(ctx, NULL); |
6b06314c | 8886 | return ret; |
600cf3f8 PB |
8887 | out_fput: |
8888 | for (i = 0; i < ctx->nr_user_files; i++) { | |
8889 | file = io_file_from_index(ctx, i); | |
8890 | if (file) | |
8891 | fput(file); | |
8892 | } | |
042b0d85 | 8893 | io_free_file_tables(&ctx->file_table); |
600cf3f8 | 8894 | ctx->nr_user_files = 0; |
600cf3f8 | 8895 | out_free: |
44b31f2f | 8896 | io_rsrc_data_free(ctx->file_data); |
55cbc256 | 8897 | ctx->file_data = NULL; |
6b06314c JA |
8898 | return ret; |
8899 | } | |
8900 | ||
c3a31e60 JA |
8901 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
8902 | int index) | |
8903 | { | |
8904 | #if defined(CONFIG_UNIX) | |
8905 | struct sock *sock = ctx->ring_sock->sk; | |
8906 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
8907 | struct sk_buff *skb; | |
8908 | ||
8909 | /* | |
8910 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
8911 | * file set. If there's no room, fall back to allocating a new skb | |
8912 | * and filling it in. | |
8913 | */ | |
8914 | spin_lock_irq(&head->lock); | |
8915 | skb = skb_peek(head); | |
8916 | if (skb) { | |
8917 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
8918 | ||
8919 | if (fpl->count < SCM_MAX_FD) { | |
8920 | __skb_unlink(skb, head); | |
8921 | spin_unlock_irq(&head->lock); | |
8922 | fpl->fp[fpl->count] = get_file(file); | |
8923 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
8924 | fpl->count++; | |
8925 | spin_lock_irq(&head->lock); | |
8926 | __skb_queue_head(head, skb); | |
8927 | } else { | |
8928 | skb = NULL; | |
8929 | } | |
8930 | } | |
8931 | spin_unlock_irq(&head->lock); | |
8932 | ||
8933 | if (skb) { | |
8934 | fput(file); | |
8935 | return 0; | |
8936 | } | |
8937 | ||
8938 | return __io_sqe_files_scm(ctx, 1, index); | |
8939 | #else | |
8940 | return 0; | |
8941 | #endif | |
8942 | } | |
8943 | ||
9c7b0ba8 PB |
8944 | static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, |
8945 | struct io_rsrc_node *node, void *rsrc) | |
8946 | { | |
8f0a2480 | 8947 | u64 *tag_slot = io_get_tag_slot(data, idx); |
9c7b0ba8 PB |
8948 | struct io_rsrc_put *prsrc; |
8949 | ||
8950 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); | |
8951 | if (!prsrc) | |
8952 | return -ENOMEM; | |
8953 | ||
8f0a2480 PB |
8954 | prsrc->tag = *tag_slot; |
8955 | *tag_slot = 0; | |
9c7b0ba8 PB |
8956 | prsrc->rsrc = rsrc; |
8957 | list_add(&prsrc->list, &node->rsrc_list); | |
8958 | return 0; | |
8959 | } | |
8960 | ||
b9445598 PB |
8961 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
8962 | unsigned int issue_flags, u32 slot_index) | |
8963 | { | |
8964 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 8965 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
9c7b0ba8 | 8966 | bool needs_switch = false; |
b9445598 PB |
8967 | struct io_fixed_file *file_slot; |
8968 | int ret = -EBADF; | |
8969 | ||
3b44b371 | 8970 | io_ring_submit_lock(ctx, needs_lock); |
b9445598 PB |
8971 | if (file->f_op == &io_uring_fops) |
8972 | goto err; | |
8973 | ret = -ENXIO; | |
8974 | if (!ctx->file_data) | |
8975 | goto err; | |
8976 | ret = -EINVAL; | |
8977 | if (slot_index >= ctx->nr_user_files) | |
8978 | goto err; | |
8979 | ||
8980 | slot_index = array_index_nospec(slot_index, ctx->nr_user_files); | |
8981 | file_slot = io_fixed_file_slot(&ctx->file_table, slot_index); | |
9c7b0ba8 PB |
8982 | |
8983 | if (file_slot->file_ptr) { | |
8984 | struct file *old_file; | |
8985 | ||
8986 | ret = io_rsrc_node_switch_start(ctx); | |
8987 | if (ret) | |
8988 | goto err; | |
8989 | ||
8990 | old_file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
8991 | ret = io_queue_rsrc_removal(ctx->file_data, slot_index, | |
8992 | ctx->rsrc_node, old_file); | |
8993 | if (ret) | |
8994 | goto err; | |
8995 | file_slot->file_ptr = 0; | |
8996 | needs_switch = true; | |
8997 | } | |
b9445598 PB |
8998 | |
8999 | *io_get_tag_slot(ctx->file_data, slot_index) = 0; | |
9000 | io_fixed_file_set(file_slot, file); | |
9001 | ret = io_sqe_file_register(ctx, file, slot_index); | |
9002 | if (ret) { | |
9003 | file_slot->file_ptr = 0; | |
9004 | goto err; | |
9005 | } | |
9006 | ||
9007 | ret = 0; | |
9008 | err: | |
9c7b0ba8 PB |
9009 | if (needs_switch) |
9010 | io_rsrc_node_switch(ctx, ctx->file_data); | |
3b44b371 | 9011 | io_ring_submit_unlock(ctx, needs_lock); |
b9445598 PB |
9012 | if (ret) |
9013 | fput(file); | |
9014 | return ret; | |
9015 | } | |
9016 | ||
7df778be PB |
9017 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags) |
9018 | { | |
9019 | unsigned int offset = req->close.file_slot - 1; | |
9020 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 9021 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
7df778be PB |
9022 | struct io_fixed_file *file_slot; |
9023 | struct file *file; | |
4cdd158b | 9024 | int ret; |
7df778be | 9025 | |
3b44b371 | 9026 | io_ring_submit_lock(ctx, needs_lock); |
7df778be PB |
9027 | ret = -ENXIO; |
9028 | if (unlikely(!ctx->file_data)) | |
9029 | goto out; | |
9030 | ret = -EINVAL; | |
9031 | if (offset >= ctx->nr_user_files) | |
9032 | goto out; | |
9033 | ret = io_rsrc_node_switch_start(ctx); | |
9034 | if (ret) | |
9035 | goto out; | |
9036 | ||
4cdd158b PB |
9037 | offset = array_index_nospec(offset, ctx->nr_user_files); |
9038 | file_slot = io_fixed_file_slot(&ctx->file_table, offset); | |
7df778be PB |
9039 | ret = -EBADF; |
9040 | if (!file_slot->file_ptr) | |
9041 | goto out; | |
9042 | ||
9043 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
9044 | ret = io_queue_rsrc_removal(ctx->file_data, offset, ctx->rsrc_node, file); | |
9045 | if (ret) | |
9046 | goto out; | |
9047 | ||
9048 | file_slot->file_ptr = 0; | |
9049 | io_rsrc_node_switch(ctx, ctx->file_data); | |
9050 | ret = 0; | |
9051 | out: | |
3b44b371 | 9052 | io_ring_submit_unlock(ctx, needs_lock); |
7df778be PB |
9053 | return ret; |
9054 | } | |
9055 | ||
05f3fb3c | 9056 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
c3bdad02 | 9057 | struct io_uring_rsrc_update2 *up, |
05f3fb3c JA |
9058 | unsigned nr_args) |
9059 | { | |
c3bdad02 | 9060 | u64 __user *tags = u64_to_user_ptr(up->tags); |
98f0b3b4 | 9061 | __s32 __user *fds = u64_to_user_ptr(up->data); |
b895c9a6 | 9062 | struct io_rsrc_data *data = ctx->file_data; |
a04b0ac0 PB |
9063 | struct io_fixed_file *file_slot; |
9064 | struct file *file; | |
98f0b3b4 PB |
9065 | int fd, i, err = 0; |
9066 | unsigned int done; | |
05589553 | 9067 | bool needs_switch = false; |
c3a31e60 | 9068 | |
98f0b3b4 PB |
9069 | if (!ctx->file_data) |
9070 | return -ENXIO; | |
9071 | if (up->offset + nr_args > ctx->nr_user_files) | |
c3a31e60 JA |
9072 | return -EINVAL; |
9073 | ||
67973b93 | 9074 | for (done = 0; done < nr_args; done++) { |
c3bdad02 PB |
9075 | u64 tag = 0; |
9076 | ||
9077 | if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) || | |
9078 | copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
c3a31e60 JA |
9079 | err = -EFAULT; |
9080 | break; | |
9081 | } | |
c3bdad02 PB |
9082 | if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) { |
9083 | err = -EINVAL; | |
9084 | break; | |
9085 | } | |
4e0377a1 | 9086 | if (fd == IORING_REGISTER_FILES_SKIP) |
9087 | continue; | |
9088 | ||
67973b93 | 9089 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
aeca241b | 9090 | file_slot = io_fixed_file_slot(&ctx->file_table, i); |
ea64ec02 | 9091 | |
a04b0ac0 PB |
9092 | if (file_slot->file_ptr) { |
9093 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
4cdd158b | 9094 | err = io_queue_rsrc_removal(data, i, ctx->rsrc_node, file); |
a5318d3c HD |
9095 | if (err) |
9096 | break; | |
a04b0ac0 | 9097 | file_slot->file_ptr = 0; |
05589553 | 9098 | needs_switch = true; |
c3a31e60 JA |
9099 | } |
9100 | if (fd != -1) { | |
c3a31e60 JA |
9101 | file = fget(fd); |
9102 | if (!file) { | |
9103 | err = -EBADF; | |
9104 | break; | |
9105 | } | |
9106 | /* | |
9107 | * Don't allow io_uring instances to be registered. If | |
9108 | * UNIX isn't enabled, then this causes a reference | |
9109 | * cycle and this instance can never get freed. If UNIX | |
9110 | * is enabled we'll handle it just fine, but there's | |
9111 | * still no point in allowing a ring fd as it doesn't | |
9112 | * support regular read/write anyway. | |
9113 | */ | |
9114 | if (file->f_op == &io_uring_fops) { | |
9115 | fput(file); | |
9116 | err = -EBADF; | |
9117 | break; | |
9118 | } | |
34bb7718 | 9119 | *io_get_tag_slot(data, i) = tag; |
9a321c98 | 9120 | io_fixed_file_set(file_slot, file); |
c3a31e60 | 9121 | err = io_sqe_file_register(ctx, file, i); |
f3bd9dae | 9122 | if (err) { |
a04b0ac0 | 9123 | file_slot->file_ptr = 0; |
f3bd9dae | 9124 | fput(file); |
c3a31e60 | 9125 | break; |
f3bd9dae | 9126 | } |
c3a31e60 | 9127 | } |
05f3fb3c JA |
9128 | } |
9129 | ||
a7f0ed5a PB |
9130 | if (needs_switch) |
9131 | io_rsrc_node_switch(ctx, data); | |
c3a31e60 JA |
9132 | return done ? done : err; |
9133 | } | |
05589553 | 9134 | |
685fe7fe JA |
9135 | static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx, |
9136 | struct task_struct *task) | |
24369c2e | 9137 | { |
e941894e | 9138 | struct io_wq_hash *hash; |
24369c2e | 9139 | struct io_wq_data data; |
24369c2e | 9140 | unsigned int concurrency; |
24369c2e | 9141 | |
362a9e65 | 9142 | mutex_lock(&ctx->uring_lock); |
e941894e JA |
9143 | hash = ctx->hash_map; |
9144 | if (!hash) { | |
9145 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); | |
362a9e65 YY |
9146 | if (!hash) { |
9147 | mutex_unlock(&ctx->uring_lock); | |
e941894e | 9148 | return ERR_PTR(-ENOMEM); |
362a9e65 | 9149 | } |
e941894e JA |
9150 | refcount_set(&hash->refs, 1); |
9151 | init_waitqueue_head(&hash->wait); | |
9152 | ctx->hash_map = hash; | |
24369c2e | 9153 | } |
362a9e65 | 9154 | mutex_unlock(&ctx->uring_lock); |
24369c2e | 9155 | |
e941894e | 9156 | data.hash = hash; |
685fe7fe | 9157 | data.task = task; |
ebc11b6c | 9158 | data.free_work = io_wq_free_work; |
f5fa38c5 | 9159 | data.do_work = io_wq_submit_work; |
24369c2e | 9160 | |
d25e3a3d JA |
9161 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
9162 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
24369c2e | 9163 | |
5aa75ed5 | 9164 | return io_wq_create(concurrency, &data); |
24369c2e PB |
9165 | } |
9166 | ||
c072481d PB |
9167 | static __cold int io_uring_alloc_task_context(struct task_struct *task, |
9168 | struct io_ring_ctx *ctx) | |
0f212204 JA |
9169 | { |
9170 | struct io_uring_task *tctx; | |
d8a6df10 | 9171 | int ret; |
0f212204 | 9172 | |
09899b19 | 9173 | tctx = kzalloc(sizeof(*tctx), GFP_KERNEL); |
0f212204 JA |
9174 | if (unlikely(!tctx)) |
9175 | return -ENOMEM; | |
9176 | ||
e7a6c00d JA |
9177 | tctx->registered_rings = kcalloc(IO_RINGFD_REG_MAX, |
9178 | sizeof(struct file *), GFP_KERNEL); | |
9179 | if (unlikely(!tctx->registered_rings)) { | |
9180 | kfree(tctx); | |
9181 | return -ENOMEM; | |
9182 | } | |
9183 | ||
d8a6df10 JA |
9184 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
9185 | if (unlikely(ret)) { | |
e7a6c00d | 9186 | kfree(tctx->registered_rings); |
d8a6df10 JA |
9187 | kfree(tctx); |
9188 | return ret; | |
9189 | } | |
9190 | ||
685fe7fe | 9191 | tctx->io_wq = io_init_wq_offload(ctx, task); |
5aa75ed5 JA |
9192 | if (IS_ERR(tctx->io_wq)) { |
9193 | ret = PTR_ERR(tctx->io_wq); | |
9194 | percpu_counter_destroy(&tctx->inflight); | |
e7a6c00d | 9195 | kfree(tctx->registered_rings); |
5aa75ed5 JA |
9196 | kfree(tctx); |
9197 | return ret; | |
9198 | } | |
9199 | ||
0f212204 JA |
9200 | xa_init(&tctx->xa); |
9201 | init_waitqueue_head(&tctx->wait); | |
fdaf083c | 9202 | atomic_set(&tctx->in_idle, 0); |
0f212204 | 9203 | task->io_uring = tctx; |
7cbf1722 JA |
9204 | spin_lock_init(&tctx->task_lock); |
9205 | INIT_WQ_LIST(&tctx->task_list); | |
4813c377 | 9206 | INIT_WQ_LIST(&tctx->prior_task_list); |
7cbf1722 | 9207 | init_task_work(&tctx->task_work, tctx_task_work); |
0f212204 JA |
9208 | return 0; |
9209 | } | |
9210 | ||
9211 | void __io_uring_free(struct task_struct *tsk) | |
9212 | { | |
9213 | struct io_uring_task *tctx = tsk->io_uring; | |
9214 | ||
9215 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); | |
ef8eaa4e | 9216 | WARN_ON_ONCE(tctx->io_wq); |
09899b19 | 9217 | WARN_ON_ONCE(tctx->cached_refs); |
ef8eaa4e | 9218 | |
e7a6c00d | 9219 | kfree(tctx->registered_rings); |
d8a6df10 | 9220 | percpu_counter_destroy(&tctx->inflight); |
0f212204 JA |
9221 | kfree(tctx); |
9222 | tsk->io_uring = NULL; | |
9223 | } | |
9224 | ||
c072481d PB |
9225 | static __cold int io_sq_offload_create(struct io_ring_ctx *ctx, |
9226 | struct io_uring_params *p) | |
2b188cc1 JA |
9227 | { |
9228 | int ret; | |
9229 | ||
d25e3a3d JA |
9230 | /* Retain compatibility with failing for an invalid attach attempt */ |
9231 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == | |
9232 | IORING_SETUP_ATTACH_WQ) { | |
9233 | struct fd f; | |
9234 | ||
9235 | f = fdget(p->wq_fd); | |
9236 | if (!f.file) | |
9237 | return -ENXIO; | |
0cc936f7 JA |
9238 | if (f.file->f_op != &io_uring_fops) { |
9239 | fdput(f); | |
f2a48dd0 | 9240 | return -EINVAL; |
0cc936f7 JA |
9241 | } |
9242 | fdput(f); | |
d25e3a3d | 9243 | } |
6c271ce2 | 9244 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
46fe18b1 | 9245 | struct task_struct *tsk; |
534ca6d6 | 9246 | struct io_sq_data *sqd; |
26984fbf | 9247 | bool attached; |
534ca6d6 | 9248 | |
cdc1404a PM |
9249 | ret = security_uring_sqpoll(); |
9250 | if (ret) | |
9251 | return ret; | |
9252 | ||
26984fbf | 9253 | sqd = io_get_sq_data(p, &attached); |
534ca6d6 JA |
9254 | if (IS_ERR(sqd)) { |
9255 | ret = PTR_ERR(sqd); | |
9256 | goto err; | |
9257 | } | |
69fb2131 | 9258 | |
7c30f36a | 9259 | ctx->sq_creds = get_current_cred(); |
534ca6d6 | 9260 | ctx->sq_data = sqd; |
917257da JA |
9261 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
9262 | if (!ctx->sq_thread_idle) | |
9263 | ctx->sq_thread_idle = HZ; | |
9264 | ||
78d7f6ba | 9265 | io_sq_thread_park(sqd); |
de75a3d3 PB |
9266 | list_add(&ctx->sqd_list, &sqd->ctx_list); |
9267 | io_sqd_update_thread_idle(sqd); | |
26984fbf | 9268 | /* don't attach to a dying SQPOLL thread, would be racy */ |
f2a48dd0 | 9269 | ret = (attached && !sqd->thread) ? -ENXIO : 0; |
78d7f6ba PB |
9270 | io_sq_thread_unpark(sqd); |
9271 | ||
de75a3d3 PB |
9272 | if (ret < 0) |
9273 | goto err; | |
9274 | if (attached) | |
5aa75ed5 | 9275 | return 0; |
aa06165d | 9276 | |
6c271ce2 | 9277 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 9278 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 9279 | |
917257da | 9280 | ret = -EINVAL; |
f2a48dd0 | 9281 | if (cpu >= nr_cpu_ids || !cpu_online(cpu)) |
e8f98f24 | 9282 | goto err_sqpoll; |
37d1e2e3 | 9283 | sqd->sq_cpu = cpu; |
6c271ce2 | 9284 | } else { |
37d1e2e3 | 9285 | sqd->sq_cpu = -1; |
6c271ce2 | 9286 | } |
37d1e2e3 JA |
9287 | |
9288 | sqd->task_pid = current->pid; | |
5c2469e0 | 9289 | sqd->task_tgid = current->tgid; |
46fe18b1 JA |
9290 | tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE); |
9291 | if (IS_ERR(tsk)) { | |
9292 | ret = PTR_ERR(tsk); | |
e8f98f24 | 9293 | goto err_sqpoll; |
6c271ce2 | 9294 | } |
97a73a0f | 9295 | |
46fe18b1 | 9296 | sqd->thread = tsk; |
97a73a0f | 9297 | ret = io_uring_alloc_task_context(tsk, ctx); |
46fe18b1 | 9298 | wake_up_new_task(tsk); |
0f212204 JA |
9299 | if (ret) |
9300 | goto err; | |
6c271ce2 JA |
9301 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
9302 | /* Can't have SQ_AFF without SQPOLL */ | |
9303 | ret = -EINVAL; | |
9304 | goto err; | |
9305 | } | |
9306 | ||
2b188cc1 | 9307 | return 0; |
f2a48dd0 PB |
9308 | err_sqpoll: |
9309 | complete(&ctx->sq_data->exited); | |
2b188cc1 | 9310 | err: |
37d1e2e3 | 9311 | io_sq_thread_finish(ctx); |
2b188cc1 JA |
9312 | return ret; |
9313 | } | |
9314 | ||
a087e2b5 BM |
9315 | static inline void __io_unaccount_mem(struct user_struct *user, |
9316 | unsigned long nr_pages) | |
2b188cc1 JA |
9317 | { |
9318 | atomic_long_sub(nr_pages, &user->locked_vm); | |
9319 | } | |
9320 | ||
a087e2b5 BM |
9321 | static inline int __io_account_mem(struct user_struct *user, |
9322 | unsigned long nr_pages) | |
2b188cc1 JA |
9323 | { |
9324 | unsigned long page_limit, cur_pages, new_pages; | |
9325 | ||
9326 | /* Don't allow more pages than we can safely lock */ | |
9327 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
9328 | ||
9329 | do { | |
9330 | cur_pages = atomic_long_read(&user->locked_vm); | |
9331 | new_pages = cur_pages + nr_pages; | |
9332 | if (new_pages > page_limit) | |
9333 | return -ENOMEM; | |
9334 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
9335 | new_pages) != cur_pages); | |
9336 | ||
9337 | return 0; | |
9338 | } | |
9339 | ||
26bfa89e | 9340 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 9341 | { |
62e398be | 9342 | if (ctx->user) |
a087e2b5 | 9343 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 9344 | |
26bfa89e JA |
9345 | if (ctx->mm_account) |
9346 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
9347 | } |
9348 | ||
26bfa89e | 9349 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 9350 | { |
30975825 BM |
9351 | int ret; |
9352 | ||
62e398be | 9353 | if (ctx->user) { |
30975825 BM |
9354 | ret = __io_account_mem(ctx->user, nr_pages); |
9355 | if (ret) | |
9356 | return ret; | |
9357 | } | |
9358 | ||
26bfa89e JA |
9359 | if (ctx->mm_account) |
9360 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
9361 | |
9362 | return 0; | |
9363 | } | |
9364 | ||
2b188cc1 JA |
9365 | static void io_mem_free(void *ptr) |
9366 | { | |
52e04ef4 MR |
9367 | struct page *page; |
9368 | ||
9369 | if (!ptr) | |
9370 | return; | |
2b188cc1 | 9371 | |
52e04ef4 | 9372 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
9373 | if (put_page_testzero(page)) |
9374 | free_compound_page(page); | |
9375 | } | |
9376 | ||
9377 | static void *io_mem_alloc(size_t size) | |
9378 | { | |
0a3f1e0b | 9379 | gfp_t gfp = GFP_KERNEL_ACCOUNT | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP; |
2b188cc1 | 9380 | |
0a3f1e0b | 9381 | return (void *) __get_free_pages(gfp, get_order(size)); |
2b188cc1 JA |
9382 | } |
9383 | ||
75b28aff HV |
9384 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
9385 | size_t *sq_offset) | |
9386 | { | |
9387 | struct io_rings *rings; | |
9388 | size_t off, sq_array_size; | |
9389 | ||
9390 | off = struct_size(rings, cqes, cq_entries); | |
9391 | if (off == SIZE_MAX) | |
9392 | return SIZE_MAX; | |
9393 | ||
9394 | #ifdef CONFIG_SMP | |
9395 | off = ALIGN(off, SMP_CACHE_BYTES); | |
9396 | if (off == 0) | |
9397 | return SIZE_MAX; | |
9398 | #endif | |
9399 | ||
b36200f5 DV |
9400 | if (sq_offset) |
9401 | *sq_offset = off; | |
9402 | ||
75b28aff HV |
9403 | sq_array_size = array_size(sizeof(u32), sq_entries); |
9404 | if (sq_array_size == SIZE_MAX) | |
9405 | return SIZE_MAX; | |
9406 | ||
9407 | if (check_add_overflow(off, sq_array_size, &off)) | |
9408 | return SIZE_MAX; | |
9409 | ||
75b28aff HV |
9410 | return off; |
9411 | } | |
9412 | ||
41edf1a5 | 9413 | static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot) |
7f61a1e9 | 9414 | { |
41edf1a5 | 9415 | struct io_mapped_ubuf *imu = *slot; |
7f61a1e9 PB |
9416 | unsigned int i; |
9417 | ||
6224843d PB |
9418 | if (imu != ctx->dummy_ubuf) { |
9419 | for (i = 0; i < imu->nr_bvecs; i++) | |
9420 | unpin_user_page(imu->bvec[i].bv_page); | |
9421 | if (imu->acct_pages) | |
9422 | io_unaccount_mem(ctx, imu->acct_pages); | |
9423 | kvfree(imu); | |
9424 | } | |
41edf1a5 | 9425 | *slot = NULL; |
7f61a1e9 PB |
9426 | } |
9427 | ||
bd54b6fe | 9428 | static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
edafccee | 9429 | { |
634d00df PB |
9430 | io_buffer_unmap(ctx, &prsrc->buf); |
9431 | prsrc->buf = NULL; | |
bd54b6fe | 9432 | } |
edafccee | 9433 | |
bd54b6fe BM |
9434 | static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
9435 | { | |
9436 | unsigned int i; | |
edafccee | 9437 | |
7f61a1e9 PB |
9438 | for (i = 0; i < ctx->nr_user_bufs; i++) |
9439 | io_buffer_unmap(ctx, &ctx->user_bufs[i]); | |
edafccee | 9440 | kfree(ctx->user_bufs); |
bb6659cc | 9441 | io_rsrc_data_free(ctx->buf_data); |
edafccee | 9442 | ctx->user_bufs = NULL; |
bd54b6fe | 9443 | ctx->buf_data = NULL; |
edafccee | 9444 | ctx->nr_user_bufs = 0; |
bd54b6fe BM |
9445 | } |
9446 | ||
0a96bbe4 | 9447 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
edafccee | 9448 | { |
bd54b6fe | 9449 | int ret; |
edafccee | 9450 | |
bd54b6fe | 9451 | if (!ctx->buf_data) |
edafccee JA |
9452 | return -ENXIO; |
9453 | ||
bd54b6fe BM |
9454 | ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx); |
9455 | if (!ret) | |
9456 | __io_sqe_buffers_unregister(ctx); | |
9457 | return ret; | |
edafccee JA |
9458 | } |
9459 | ||
9460 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
9461 | void __user *arg, unsigned index) | |
9462 | { | |
9463 | struct iovec __user *src; | |
9464 | ||
9465 | #ifdef CONFIG_COMPAT | |
9466 | if (ctx->compat) { | |
9467 | struct compat_iovec __user *ciovs; | |
9468 | struct compat_iovec ciov; | |
9469 | ||
9470 | ciovs = (struct compat_iovec __user *) arg; | |
9471 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
9472 | return -EFAULT; | |
9473 | ||
d55e5f5b | 9474 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
9475 | dst->iov_len = ciov.iov_len; |
9476 | return 0; | |
9477 | } | |
9478 | #endif | |
9479 | src = (struct iovec __user *) arg; | |
9480 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
9481 | return -EFAULT; | |
9482 | return 0; | |
9483 | } | |
9484 | ||
de293938 JA |
9485 | /* |
9486 | * Not super efficient, but this is just a registration time. And we do cache | |
9487 | * the last compound head, so generally we'll only do a full search if we don't | |
9488 | * match that one. | |
9489 | * | |
9490 | * We check if the given compound head page has already been accounted, to | |
9491 | * avoid double accounting it. This allows us to account the full size of the | |
9492 | * page, not just the constituent pages of a huge page. | |
9493 | */ | |
9494 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, | |
9495 | int nr_pages, struct page *hpage) | |
9496 | { | |
9497 | int i, j; | |
9498 | ||
9499 | /* check current page array */ | |
9500 | for (i = 0; i < nr_pages; i++) { | |
9501 | if (!PageCompound(pages[i])) | |
9502 | continue; | |
9503 | if (compound_head(pages[i]) == hpage) | |
9504 | return true; | |
9505 | } | |
9506 | ||
9507 | /* check previously registered pages */ | |
9508 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
41edf1a5 | 9509 | struct io_mapped_ubuf *imu = ctx->user_bufs[i]; |
de293938 JA |
9510 | |
9511 | for (j = 0; j < imu->nr_bvecs; j++) { | |
9512 | if (!PageCompound(imu->bvec[j].bv_page)) | |
9513 | continue; | |
9514 | if (compound_head(imu->bvec[j].bv_page) == hpage) | |
9515 | return true; | |
9516 | } | |
9517 | } | |
9518 | ||
9519 | return false; | |
9520 | } | |
9521 | ||
9522 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, | |
9523 | int nr_pages, struct io_mapped_ubuf *imu, | |
9524 | struct page **last_hpage) | |
9525 | { | |
9526 | int i, ret; | |
9527 | ||
216e5835 | 9528 | imu->acct_pages = 0; |
de293938 JA |
9529 | for (i = 0; i < nr_pages; i++) { |
9530 | if (!PageCompound(pages[i])) { | |
9531 | imu->acct_pages++; | |
9532 | } else { | |
9533 | struct page *hpage; | |
9534 | ||
9535 | hpage = compound_head(pages[i]); | |
9536 | if (hpage == *last_hpage) | |
9537 | continue; | |
9538 | *last_hpage = hpage; | |
9539 | if (headpage_already_acct(ctx, pages, i, hpage)) | |
9540 | continue; | |
9541 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; | |
9542 | } | |
9543 | } | |
9544 | ||
9545 | if (!imu->acct_pages) | |
9546 | return 0; | |
9547 | ||
26bfa89e | 9548 | ret = io_account_mem(ctx, imu->acct_pages); |
de293938 JA |
9549 | if (ret) |
9550 | imu->acct_pages = 0; | |
9551 | return ret; | |
9552 | } | |
9553 | ||
0a96bbe4 | 9554 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
41edf1a5 | 9555 | struct io_mapped_ubuf **pimu, |
0a96bbe4 | 9556 | struct page **last_hpage) |
edafccee | 9557 | { |
41edf1a5 | 9558 | struct io_mapped_ubuf *imu = NULL; |
edafccee JA |
9559 | struct vm_area_struct **vmas = NULL; |
9560 | struct page **pages = NULL; | |
0a96bbe4 BM |
9561 | unsigned long off, start, end, ubuf; |
9562 | size_t size; | |
9563 | int ret, pret, nr_pages, i; | |
9564 | ||
6224843d PB |
9565 | if (!iov->iov_base) { |
9566 | *pimu = ctx->dummy_ubuf; | |
9567 | return 0; | |
9568 | } | |
9569 | ||
0a96bbe4 BM |
9570 | ubuf = (unsigned long) iov->iov_base; |
9571 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
9572 | start = ubuf >> PAGE_SHIFT; | |
9573 | nr_pages = end - start; | |
9574 | ||
41edf1a5 | 9575 | *pimu = NULL; |
0a96bbe4 BM |
9576 | ret = -ENOMEM; |
9577 | ||
9578 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); | |
9579 | if (!pages) | |
9580 | goto done; | |
9581 | ||
9582 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), | |
9583 | GFP_KERNEL); | |
9584 | if (!vmas) | |
9585 | goto done; | |
edafccee | 9586 | |
41edf1a5 | 9587 | imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL); |
a2b4198c | 9588 | if (!imu) |
0a96bbe4 BM |
9589 | goto done; |
9590 | ||
9591 | ret = 0; | |
9592 | mmap_read_lock(current->mm); | |
9593 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, | |
9594 | pages, vmas); | |
9595 | if (pret == nr_pages) { | |
9596 | /* don't support file backed memory */ | |
9597 | for (i = 0; i < nr_pages; i++) { | |
9598 | struct vm_area_struct *vma = vmas[i]; | |
9599 | ||
40dad765 PB |
9600 | if (vma_is_shmem(vma)) |
9601 | continue; | |
0a96bbe4 BM |
9602 | if (vma->vm_file && |
9603 | !is_file_hugepages(vma->vm_file)) { | |
9604 | ret = -EOPNOTSUPP; | |
9605 | break; | |
9606 | } | |
9607 | } | |
9608 | } else { | |
9609 | ret = pret < 0 ? pret : -EFAULT; | |
9610 | } | |
9611 | mmap_read_unlock(current->mm); | |
9612 | if (ret) { | |
9613 | /* | |
9614 | * if we did partial map, or found file backed vmas, | |
9615 | * release any pages we did get | |
9616 | */ | |
9617 | if (pret > 0) | |
9618 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
9619 | goto done; |
9620 | } | |
9621 | ||
9622 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); | |
9623 | if (ret) { | |
9624 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
9625 | goto done; |
9626 | } | |
9627 | ||
9628 | off = ubuf & ~PAGE_MASK; | |
9629 | size = iov->iov_len; | |
9630 | for (i = 0; i < nr_pages; i++) { | |
9631 | size_t vec_len; | |
9632 | ||
9633 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
9634 | imu->bvec[i].bv_page = pages[i]; | |
9635 | imu->bvec[i].bv_len = vec_len; | |
9636 | imu->bvec[i].bv_offset = off; | |
9637 | off = 0; | |
9638 | size -= vec_len; | |
9639 | } | |
9640 | /* store original address for later verification */ | |
9641 | imu->ubuf = ubuf; | |
4751f53d | 9642 | imu->ubuf_end = ubuf + iov->iov_len; |
0a96bbe4 | 9643 | imu->nr_bvecs = nr_pages; |
41edf1a5 | 9644 | *pimu = imu; |
0a96bbe4 BM |
9645 | ret = 0; |
9646 | done: | |
41edf1a5 PB |
9647 | if (ret) |
9648 | kvfree(imu); | |
0a96bbe4 BM |
9649 | kvfree(pages); |
9650 | kvfree(vmas); | |
9651 | return ret; | |
9652 | } | |
9653 | ||
2b358604 | 9654 | static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args) |
0a96bbe4 | 9655 | { |
87094465 PB |
9656 | ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL); |
9657 | return ctx->user_bufs ? 0 : -ENOMEM; | |
2b358604 | 9658 | } |
edafccee | 9659 | |
2b358604 BM |
9660 | static int io_buffer_validate(struct iovec *iov) |
9661 | { | |
50e96989 PB |
9662 | unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1); |
9663 | ||
2b358604 BM |
9664 | /* |
9665 | * Don't impose further limits on the size and buffer | |
9666 | * constraints here, we'll -EINVAL later when IO is | |
9667 | * submitted if they are wrong. | |
9668 | */ | |
6224843d PB |
9669 | if (!iov->iov_base) |
9670 | return iov->iov_len ? -EFAULT : 0; | |
9671 | if (!iov->iov_len) | |
2b358604 | 9672 | return -EFAULT; |
edafccee | 9673 | |
2b358604 BM |
9674 | /* arbitrary limit, but we need something */ |
9675 | if (iov->iov_len > SZ_1G) | |
9676 | return -EFAULT; | |
edafccee | 9677 | |
50e96989 PB |
9678 | if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp)) |
9679 | return -EOVERFLOW; | |
9680 | ||
2b358604 BM |
9681 | return 0; |
9682 | } | |
edafccee | 9683 | |
2b358604 | 9684 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
634d00df | 9685 | unsigned int nr_args, u64 __user *tags) |
2b358604 | 9686 | { |
bd54b6fe BM |
9687 | struct page *last_hpage = NULL; |
9688 | struct io_rsrc_data *data; | |
2b358604 BM |
9689 | int i, ret; |
9690 | struct iovec iov; | |
edafccee | 9691 | |
87094465 PB |
9692 | if (ctx->user_bufs) |
9693 | return -EBUSY; | |
489809e2 | 9694 | if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS) |
87094465 | 9695 | return -EINVAL; |
bd54b6fe | 9696 | ret = io_rsrc_node_switch_start(ctx); |
2b358604 BM |
9697 | if (ret) |
9698 | return ret; | |
d878c816 PB |
9699 | ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data); |
9700 | if (ret) | |
9701 | return ret; | |
bd54b6fe BM |
9702 | ret = io_buffers_map_alloc(ctx, nr_args); |
9703 | if (ret) { | |
bb6659cc | 9704 | io_rsrc_data_free(data); |
bd54b6fe BM |
9705 | return ret; |
9706 | } | |
edafccee | 9707 | |
87094465 | 9708 | for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) { |
edafccee JA |
9709 | ret = io_copy_iov(ctx, &iov, arg, i); |
9710 | if (ret) | |
0a96bbe4 | 9711 | break; |
2b358604 BM |
9712 | ret = io_buffer_validate(&iov); |
9713 | if (ret) | |
0a96bbe4 | 9714 | break; |
2d091d62 | 9715 | if (!iov.iov_base && *io_get_tag_slot(data, i)) { |
cf3770e7 CIK |
9716 | ret = -EINVAL; |
9717 | break; | |
9718 | } | |
edafccee | 9719 | |
41edf1a5 PB |
9720 | ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i], |
9721 | &last_hpage); | |
0a96bbe4 BM |
9722 | if (ret) |
9723 | break; | |
edafccee | 9724 | } |
0a96bbe4 | 9725 | |
bd54b6fe | 9726 | WARN_ON_ONCE(ctx->buf_data); |
0a96bbe4 | 9727 | |
bd54b6fe BM |
9728 | ctx->buf_data = data; |
9729 | if (ret) | |
9730 | __io_sqe_buffers_unregister(ctx); | |
9731 | else | |
9732 | io_rsrc_node_switch(ctx, NULL); | |
edafccee JA |
9733 | return ret; |
9734 | } | |
9735 | ||
634d00df PB |
9736 | static int __io_sqe_buffers_update(struct io_ring_ctx *ctx, |
9737 | struct io_uring_rsrc_update2 *up, | |
9738 | unsigned int nr_args) | |
9739 | { | |
9740 | u64 __user *tags = u64_to_user_ptr(up->tags); | |
9741 | struct iovec iov, __user *iovs = u64_to_user_ptr(up->data); | |
634d00df PB |
9742 | struct page *last_hpage = NULL; |
9743 | bool needs_switch = false; | |
9744 | __u32 done; | |
9745 | int i, err; | |
9746 | ||
9747 | if (!ctx->buf_data) | |
9748 | return -ENXIO; | |
9749 | if (up->offset + nr_args > ctx->nr_user_bufs) | |
9750 | return -EINVAL; | |
9751 | ||
9752 | for (done = 0; done < nr_args; done++) { | |
0b8c0e7c PB |
9753 | struct io_mapped_ubuf *imu; |
9754 | int offset = up->offset + done; | |
634d00df PB |
9755 | u64 tag = 0; |
9756 | ||
9757 | err = io_copy_iov(ctx, &iov, iovs, done); | |
9758 | if (err) | |
9759 | break; | |
9760 | if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) { | |
9761 | err = -EFAULT; | |
9762 | break; | |
9763 | } | |
0b8c0e7c PB |
9764 | err = io_buffer_validate(&iov); |
9765 | if (err) | |
9766 | break; | |
cf3770e7 CIK |
9767 | if (!iov.iov_base && tag) { |
9768 | err = -EINVAL; | |
9769 | break; | |
9770 | } | |
0b8c0e7c PB |
9771 | err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage); |
9772 | if (err) | |
9773 | break; | |
634d00df | 9774 | |
0b8c0e7c | 9775 | i = array_index_nospec(offset, ctx->nr_user_bufs); |
6224843d | 9776 | if (ctx->user_bufs[i] != ctx->dummy_ubuf) { |
4cdd158b | 9777 | err = io_queue_rsrc_removal(ctx->buf_data, i, |
0b8c0e7c PB |
9778 | ctx->rsrc_node, ctx->user_bufs[i]); |
9779 | if (unlikely(err)) { | |
9780 | io_buffer_unmap(ctx, &imu); | |
634d00df | 9781 | break; |
0b8c0e7c | 9782 | } |
634d00df PB |
9783 | ctx->user_bufs[i] = NULL; |
9784 | needs_switch = true; | |
9785 | } | |
9786 | ||
0b8c0e7c | 9787 | ctx->user_bufs[i] = imu; |
2d091d62 | 9788 | *io_get_tag_slot(ctx->buf_data, offset) = tag; |
634d00df PB |
9789 | } |
9790 | ||
9791 | if (needs_switch) | |
9792 | io_rsrc_node_switch(ctx, ctx->buf_data); | |
9793 | return done ? done : err; | |
9794 | } | |
9795 | ||
c75312dd UA |
9796 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg, |
9797 | unsigned int eventfd_async) | |
9b402849 | 9798 | { |
77bc59b4 | 9799 | struct io_ev_fd *ev_fd; |
9b402849 | 9800 | __s32 __user *fds = arg; |
f0a4e62b | 9801 | int fd; |
9b402849 | 9802 | |
77bc59b4 UA |
9803 | ev_fd = rcu_dereference_protected(ctx->io_ev_fd, |
9804 | lockdep_is_held(&ctx->uring_lock)); | |
9805 | if (ev_fd) | |
9b402849 JA |
9806 | return -EBUSY; |
9807 | ||
9808 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
9809 | return -EFAULT; | |
9810 | ||
77bc59b4 UA |
9811 | ev_fd = kmalloc(sizeof(*ev_fd), GFP_KERNEL); |
9812 | if (!ev_fd) | |
9813 | return -ENOMEM; | |
fe7e3257 | 9814 | |
77bc59b4 UA |
9815 | ev_fd->cq_ev_fd = eventfd_ctx_fdget(fd); |
9816 | if (IS_ERR(ev_fd->cq_ev_fd)) { | |
f0a4e62b | 9817 | int ret = PTR_ERR(ev_fd->cq_ev_fd); |
77bc59b4 | 9818 | kfree(ev_fd); |
9b402849 JA |
9819 | return ret; |
9820 | } | |
c75312dd | 9821 | ev_fd->eventfd_async = eventfd_async; |
9aa8dfde | 9822 | ctx->has_evfd = true; |
77bc59b4 | 9823 | rcu_assign_pointer(ctx->io_ev_fd, ev_fd); |
f0a4e62b | 9824 | return 0; |
77bc59b4 UA |
9825 | } |
9826 | ||
9827 | static void io_eventfd_put(struct rcu_head *rcu) | |
9828 | { | |
9829 | struct io_ev_fd *ev_fd = container_of(rcu, struct io_ev_fd, rcu); | |
9830 | ||
9831 | eventfd_ctx_put(ev_fd->cq_ev_fd); | |
9832 | kfree(ev_fd); | |
9b402849 JA |
9833 | } |
9834 | ||
9835 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
9836 | { | |
77bc59b4 UA |
9837 | struct io_ev_fd *ev_fd; |
9838 | ||
9839 | ev_fd = rcu_dereference_protected(ctx->io_ev_fd, | |
9840 | lockdep_is_held(&ctx->uring_lock)); | |
9841 | if (ev_fd) { | |
9aa8dfde | 9842 | ctx->has_evfd = false; |
77bc59b4 UA |
9843 | rcu_assign_pointer(ctx->io_ev_fd, NULL); |
9844 | call_rcu(&ev_fd->rcu, io_eventfd_put); | |
9b402849 JA |
9845 | return 0; |
9846 | } | |
9847 | ||
9848 | return -ENXIO; | |
9849 | } | |
9850 | ||
5a2e745d JA |
9851 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
9852 | { | |
dbc7d452 JA |
9853 | int i; |
9854 | ||
9855 | for (i = 0; i < (1U << IO_BUFFERS_HASH_BITS); i++) { | |
9856 | struct list_head *list = &ctx->io_buffers[i]; | |
9e15c3a0 | 9857 | |
dbc7d452 JA |
9858 | while (!list_empty(list)) { |
9859 | struct io_buffer_list *bl; | |
9860 | ||
9861 | bl = list_first_entry(list, struct io_buffer_list, list); | |
9862 | __io_remove_buffers(ctx, bl, -1U); | |
9863 | list_del(&bl->list); | |
9864 | kfree(bl); | |
9865 | } | |
9866 | } | |
cc3cec83 JA |
9867 | |
9868 | while (!list_empty(&ctx->io_buffers_pages)) { | |
9869 | struct page *page; | |
9870 | ||
9871 | page = list_first_entry(&ctx->io_buffers_pages, struct page, lru); | |
9872 | list_del_init(&page->lru); | |
9873 | __free_page(page); | |
9874 | } | |
5a2e745d JA |
9875 | } |
9876 | ||
4010fec4 | 9877 | static void io_req_caches_free(struct io_ring_ctx *ctx) |
2b188cc1 | 9878 | { |
cd0ca2e0 | 9879 | struct io_submit_state *state = &ctx->submit_state; |
37f0e767 | 9880 | int nr = 0; |
bf019da7 | 9881 | |
9a4fdbd8 | 9882 | mutex_lock(&ctx->uring_lock); |
cd0ca2e0 | 9883 | io_flush_cached_locked_reqs(ctx, state); |
9a4fdbd8 | 9884 | |
c2b6c6bc PB |
9885 | while (state->free_list.next) { |
9886 | struct io_wq_work_node *node; | |
9887 | struct io_kiocb *req; | |
9a4fdbd8 | 9888 | |
c2b6c6bc PB |
9889 | node = wq_stack_extract(&state->free_list); |
9890 | req = container_of(node, struct io_kiocb, comp_list); | |
9891 | kmem_cache_free(req_cachep, req); | |
37f0e767 | 9892 | nr++; |
c2b6c6bc | 9893 | } |
37f0e767 PB |
9894 | if (nr) |
9895 | percpu_ref_put_many(&ctx->refs, nr); | |
9a4fdbd8 JA |
9896 | mutex_unlock(&ctx->uring_lock); |
9897 | } | |
9898 | ||
43597aac | 9899 | static void io_wait_rsrc_data(struct io_rsrc_data *data) |
2b188cc1 | 9900 | { |
43597aac | 9901 | if (data && !atomic_dec_and_test(&data->refs)) |
bd54b6fe | 9902 | wait_for_completion(&data->done); |
bd54b6fe | 9903 | } |
04fc6c80 | 9904 | |
4d9237e3 JA |
9905 | static void io_flush_apoll_cache(struct io_ring_ctx *ctx) |
9906 | { | |
9907 | struct async_poll *apoll; | |
9908 | ||
9909 | while (!list_empty(&ctx->apoll_cache)) { | |
9910 | apoll = list_first_entry(&ctx->apoll_cache, struct async_poll, | |
9911 | poll.wait.entry); | |
9912 | list_del(&apoll->poll.wait.entry); | |
9913 | kfree(apoll); | |
9914 | } | |
9915 | } | |
9916 | ||
c072481d | 9917 | static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx) |
2b188cc1 | 9918 | { |
37d1e2e3 | 9919 | io_sq_thread_finish(ctx); |
2aede0e4 | 9920 | |
37d1e2e3 | 9921 | if (ctx->mm_account) { |
2aede0e4 JA |
9922 | mmdrop(ctx->mm_account); |
9923 | ctx->mm_account = NULL; | |
30975825 | 9924 | } |
def596e9 | 9925 | |
ab409402 | 9926 | io_rsrc_refs_drop(ctx); |
43597aac PB |
9927 | /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */ |
9928 | io_wait_rsrc_data(ctx->buf_data); | |
9929 | io_wait_rsrc_data(ctx->file_data); | |
9930 | ||
8bad28d8 | 9931 | mutex_lock(&ctx->uring_lock); |
43597aac | 9932 | if (ctx->buf_data) |
bd54b6fe | 9933 | __io_sqe_buffers_unregister(ctx); |
43597aac | 9934 | if (ctx->file_data) |
08480400 | 9935 | __io_sqe_files_unregister(ctx); |
c4ea060e PB |
9936 | if (ctx->rings) |
9937 | __io_cqring_overflow_flush(ctx, true); | |
9b402849 | 9938 | io_eventfd_unregister(ctx); |
4d9237e3 | 9939 | io_flush_apoll_cache(ctx); |
77bc59b4 | 9940 | mutex_unlock(&ctx->uring_lock); |
5a2e745d | 9941 | io_destroy_buffers(ctx); |
07db298a PB |
9942 | if (ctx->sq_creds) |
9943 | put_cred(ctx->sq_creds); | |
def596e9 | 9944 | |
a7f0ed5a PB |
9945 | /* there are no registered resources left, nobody uses it */ |
9946 | if (ctx->rsrc_node) | |
9947 | io_rsrc_node_destroy(ctx->rsrc_node); | |
8dd03afe | 9948 | if (ctx->rsrc_backup_node) |
b895c9a6 | 9949 | io_rsrc_node_destroy(ctx->rsrc_backup_node); |
a7f0ed5a | 9950 | flush_delayed_work(&ctx->rsrc_put_work); |
756ab7c0 | 9951 | flush_delayed_work(&ctx->fallback_work); |
a7f0ed5a PB |
9952 | |
9953 | WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list)); | |
9954 | WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist)); | |
def596e9 | 9955 | |
2b188cc1 | 9956 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
9957 | if (ctx->ring_sock) { |
9958 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 9959 | sock_release(ctx->ring_sock); |
355e8d26 | 9960 | } |
2b188cc1 | 9961 | #endif |
ef9dd637 | 9962 | WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list)); |
2b188cc1 | 9963 | |
75b28aff | 9964 | io_mem_free(ctx->rings); |
2b188cc1 | 9965 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
9966 | |
9967 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 9968 | free_uid(ctx->user); |
4010fec4 | 9969 | io_req_caches_free(ctx); |
e941894e JA |
9970 | if (ctx->hash_map) |
9971 | io_wq_put_hash(ctx->hash_map); | |
78076bb6 | 9972 | kfree(ctx->cancel_hash); |
6224843d | 9973 | kfree(ctx->dummy_ubuf); |
dbc7d452 | 9974 | kfree(ctx->io_buffers); |
2b188cc1 JA |
9975 | kfree(ctx); |
9976 | } | |
9977 | ||
9978 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
9979 | { | |
9980 | struct io_ring_ctx *ctx = file->private_data; | |
9981 | __poll_t mask = 0; | |
9982 | ||
d60aa65b | 9983 | poll_wait(file, &ctx->cq_wait, wait); |
4f7067c3 SB |
9984 | /* |
9985 | * synchronizes with barrier from wq_has_sleeper call in | |
9986 | * io_commit_cqring | |
9987 | */ | |
2b188cc1 | 9988 | smp_rmb(); |
90554200 | 9989 | if (!io_sqring_full(ctx)) |
2b188cc1 | 9990 | mask |= EPOLLOUT | EPOLLWRNORM; |
ed670c3f HX |
9991 | |
9992 | /* | |
9993 | * Don't flush cqring overflow list here, just do a simple check. | |
9994 | * Otherwise there could possible be ABBA deadlock: | |
9995 | * CPU0 CPU1 | |
9996 | * ---- ---- | |
9997 | * lock(&ctx->uring_lock); | |
9998 | * lock(&ep->mtx); | |
9999 | * lock(&ctx->uring_lock); | |
10000 | * lock(&ep->mtx); | |
10001 | * | |
10002 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this | |
10003 | * pushs them to do the flush. | |
10004 | */ | |
5ed7a37d | 10005 | if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow)) |
2b188cc1 JA |
10006 | mask |= EPOLLIN | EPOLLRDNORM; |
10007 | ||
10008 | return mask; | |
10009 | } | |
10010 | ||
0bead8cd | 10011 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
071698e1 | 10012 | { |
4379bf8b | 10013 | const struct cred *creds; |
071698e1 | 10014 | |
61cf9370 | 10015 | creds = xa_erase(&ctx->personalities, id); |
4379bf8b JA |
10016 | if (creds) { |
10017 | put_cred(creds); | |
0bead8cd | 10018 | return 0; |
1e6fa521 | 10019 | } |
0bead8cd YD |
10020 | |
10021 | return -EINVAL; | |
10022 | } | |
10023 | ||
d56d938b PB |
10024 | struct io_tctx_exit { |
10025 | struct callback_head task_work; | |
10026 | struct completion completion; | |
baf186c4 | 10027 | struct io_ring_ctx *ctx; |
d56d938b PB |
10028 | }; |
10029 | ||
c072481d | 10030 | static __cold void io_tctx_exit_cb(struct callback_head *cb) |
d56d938b PB |
10031 | { |
10032 | struct io_uring_task *tctx = current->io_uring; | |
10033 | struct io_tctx_exit *work; | |
10034 | ||
10035 | work = container_of(cb, struct io_tctx_exit, task_work); | |
10036 | /* | |
10037 | * When @in_idle, we're in cancellation and it's racy to remove the | |
10038 | * node. It'll be removed by the end of cancellation, just ignore it. | |
10039 | */ | |
10040 | if (!atomic_read(&tctx->in_idle)) | |
eef51daa | 10041 | io_uring_del_tctx_node((unsigned long)work->ctx); |
d56d938b PB |
10042 | complete(&work->completion); |
10043 | } | |
10044 | ||
c072481d | 10045 | static __cold bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
28090c13 PB |
10046 | { |
10047 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
10048 | ||
10049 | return req->ctx == data; | |
10050 | } | |
10051 | ||
c072481d | 10052 | static __cold void io_ring_exit_work(struct work_struct *work) |
85faa7b8 | 10053 | { |
d56d938b | 10054 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work); |
b5bb3a24 | 10055 | unsigned long timeout = jiffies + HZ * 60 * 5; |
58d3be2c | 10056 | unsigned long interval = HZ / 20; |
d56d938b PB |
10057 | struct io_tctx_exit exit; |
10058 | struct io_tctx_node *node; | |
10059 | int ret; | |
85faa7b8 | 10060 | |
56952e91 JA |
10061 | /* |
10062 | * If we're doing polled IO and end up having requests being | |
10063 | * submitted async (out-of-line), then completions can come in while | |
10064 | * we're waiting for refs to drop. We need to reap these manually, | |
10065 | * as nobody else will be looking for them. | |
10066 | */ | |
b2edc0a7 | 10067 | do { |
3dd0c97a | 10068 | io_uring_try_cancel_requests(ctx, NULL, true); |
28090c13 PB |
10069 | if (ctx->sq_data) { |
10070 | struct io_sq_data *sqd = ctx->sq_data; | |
10071 | struct task_struct *tsk; | |
10072 | ||
10073 | io_sq_thread_park(sqd); | |
10074 | tsk = sqd->thread; | |
10075 | if (tsk && tsk->io_uring && tsk->io_uring->io_wq) | |
10076 | io_wq_cancel_cb(tsk->io_uring->io_wq, | |
10077 | io_cancel_ctx_cb, ctx, true); | |
10078 | io_sq_thread_unpark(sqd); | |
10079 | } | |
b5bb3a24 | 10080 | |
37f0e767 PB |
10081 | io_req_caches_free(ctx); |
10082 | ||
58d3be2c PB |
10083 | if (WARN_ON_ONCE(time_after(jiffies, timeout))) { |
10084 | /* there is little hope left, don't run it too often */ | |
10085 | interval = HZ * 60; | |
10086 | } | |
10087 | } while (!wait_for_completion_timeout(&ctx->ref_comp, interval)); | |
d56d938b | 10088 | |
7f00651a PB |
10089 | init_completion(&exit.completion); |
10090 | init_task_work(&exit.task_work, io_tctx_exit_cb); | |
10091 | exit.ctx = ctx; | |
89b5066e PB |
10092 | /* |
10093 | * Some may use context even when all refs and requests have been put, | |
10094 | * and they are free to do so while still holding uring_lock or | |
5b0a6acc | 10095 | * completion_lock, see io_req_task_submit(). Apart from other work, |
89b5066e PB |
10096 | * this lock/unlock section also waits them to finish. |
10097 | */ | |
d56d938b PB |
10098 | mutex_lock(&ctx->uring_lock); |
10099 | while (!list_empty(&ctx->tctx_list)) { | |
b5bb3a24 PB |
10100 | WARN_ON_ONCE(time_after(jiffies, timeout)); |
10101 | ||
d56d938b PB |
10102 | node = list_first_entry(&ctx->tctx_list, struct io_tctx_node, |
10103 | ctx_node); | |
7f00651a PB |
10104 | /* don't spin on a single task if cancellation failed */ |
10105 | list_rotate_left(&ctx->tctx_list); | |
d56d938b PB |
10106 | ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL); |
10107 | if (WARN_ON_ONCE(ret)) | |
10108 | continue; | |
d56d938b PB |
10109 | |
10110 | mutex_unlock(&ctx->uring_lock); | |
10111 | wait_for_completion(&exit.completion); | |
d56d938b PB |
10112 | mutex_lock(&ctx->uring_lock); |
10113 | } | |
10114 | mutex_unlock(&ctx->uring_lock); | |
79ebeaee JA |
10115 | spin_lock(&ctx->completion_lock); |
10116 | spin_unlock(&ctx->completion_lock); | |
d56d938b | 10117 | |
85faa7b8 JA |
10118 | io_ring_ctx_free(ctx); |
10119 | } | |
10120 | ||
80c4cbdb | 10121 | /* Returns true if we found and killed one or more timeouts */ |
c072481d PB |
10122 | static __cold bool io_kill_timeouts(struct io_ring_ctx *ctx, |
10123 | struct task_struct *tsk, bool cancel_all) | |
80c4cbdb PB |
10124 | { |
10125 | struct io_kiocb *req, *tmp; | |
10126 | int canceled = 0; | |
10127 | ||
79ebeaee JA |
10128 | spin_lock(&ctx->completion_lock); |
10129 | spin_lock_irq(&ctx->timeout_lock); | |
80c4cbdb | 10130 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
3dd0c97a | 10131 | if (io_match_task(req, tsk, cancel_all)) { |
80c4cbdb PB |
10132 | io_kill_timeout(req, -ECANCELED); |
10133 | canceled++; | |
10134 | } | |
10135 | } | |
79ebeaee | 10136 | spin_unlock_irq(&ctx->timeout_lock); |
60053be8 | 10137 | io_commit_cqring(ctx); |
79ebeaee | 10138 | spin_unlock(&ctx->completion_lock); |
80c4cbdb PB |
10139 | if (canceled != 0) |
10140 | io_cqring_ev_posted(ctx); | |
10141 | return canceled != 0; | |
10142 | } | |
10143 | ||
c072481d | 10144 | static __cold void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
2b188cc1 | 10145 | { |
61cf9370 MWO |
10146 | unsigned long index; |
10147 | struct creds *creds; | |
10148 | ||
2b188cc1 JA |
10149 | mutex_lock(&ctx->uring_lock); |
10150 | percpu_ref_kill(&ctx->refs); | |
634578f8 | 10151 | if (ctx->rings) |
6c2450ae | 10152 | __io_cqring_overflow_flush(ctx, true); |
61cf9370 MWO |
10153 | xa_for_each(&ctx->personalities, index, creds) |
10154 | io_unregister_personality(ctx, index); | |
2b188cc1 JA |
10155 | mutex_unlock(&ctx->uring_lock); |
10156 | ||
60053be8 PB |
10157 | /* failed during ring init, it couldn't have issued any requests */ |
10158 | if (ctx->rings) { | |
10159 | io_kill_timeouts(ctx, NULL, true); | |
10160 | io_poll_remove_all(ctx, NULL, true); | |
10161 | /* if we failed setting up the ctx, we might not have any rings */ | |
10162 | io_iopoll_try_reap_events(ctx); | |
10163 | } | |
309fc03a | 10164 | |
85faa7b8 | 10165 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
10166 | /* |
10167 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
10168 | * if we're exiting a ton of rings at the same time. It just adds | |
10169 | * noise and overhead, there's no discernable change in runtime | |
10170 | * over using system_wq. | |
10171 | */ | |
10172 | queue_work(system_unbound_wq, &ctx->exit_work); | |
2b188cc1 JA |
10173 | } |
10174 | ||
10175 | static int io_uring_release(struct inode *inode, struct file *file) | |
10176 | { | |
10177 | struct io_ring_ctx *ctx = file->private_data; | |
10178 | ||
10179 | file->private_data = NULL; | |
10180 | io_ring_ctx_wait_and_kill(ctx); | |
10181 | return 0; | |
10182 | } | |
10183 | ||
f6edbabb PB |
10184 | struct io_task_cancel { |
10185 | struct task_struct *task; | |
3dd0c97a | 10186 | bool all; |
f6edbabb | 10187 | }; |
f254ac04 | 10188 | |
f6edbabb | 10189 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
b711d4ea | 10190 | { |
9a472ef7 | 10191 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
f6edbabb | 10192 | struct io_task_cancel *cancel = data; |
9a472ef7 | 10193 | |
6af3f48b | 10194 | return io_match_task_safe(req, cancel->task, cancel->all); |
b711d4ea JA |
10195 | } |
10196 | ||
c072481d PB |
10197 | static __cold bool io_cancel_defer_files(struct io_ring_ctx *ctx, |
10198 | struct task_struct *task, | |
10199 | bool cancel_all) | |
b7ddce3c | 10200 | { |
e1915f76 | 10201 | struct io_defer_entry *de; |
b7ddce3c PB |
10202 | LIST_HEAD(list); |
10203 | ||
79ebeaee | 10204 | spin_lock(&ctx->completion_lock); |
b7ddce3c | 10205 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
6af3f48b | 10206 | if (io_match_task_safe(de->req, task, cancel_all)) { |
b7ddce3c PB |
10207 | list_cut_position(&list, &ctx->defer_list, &de->list); |
10208 | break; | |
10209 | } | |
10210 | } | |
79ebeaee | 10211 | spin_unlock(&ctx->completion_lock); |
e1915f76 PB |
10212 | if (list_empty(&list)) |
10213 | return false; | |
b7ddce3c PB |
10214 | |
10215 | while (!list_empty(&list)) { | |
10216 | de = list_first_entry(&list, struct io_defer_entry, list); | |
10217 | list_del_init(&de->list); | |
f41db273 | 10218 | io_req_complete_failed(de->req, -ECANCELED); |
b7ddce3c PB |
10219 | kfree(de); |
10220 | } | |
e1915f76 | 10221 | return true; |
b7ddce3c PB |
10222 | } |
10223 | ||
c072481d | 10224 | static __cold bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx) |
1b00764f PB |
10225 | { |
10226 | struct io_tctx_node *node; | |
10227 | enum io_wq_cancel cret; | |
10228 | bool ret = false; | |
10229 | ||
10230 | mutex_lock(&ctx->uring_lock); | |
10231 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
10232 | struct io_uring_task *tctx = node->task->io_uring; | |
10233 | ||
10234 | /* | |
10235 | * io_wq will stay alive while we hold uring_lock, because it's | |
10236 | * killed after ctx nodes, which requires to take the lock. | |
10237 | */ | |
10238 | if (!tctx || !tctx->io_wq) | |
10239 | continue; | |
10240 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true); | |
10241 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
10242 | } | |
10243 | mutex_unlock(&ctx->uring_lock); | |
10244 | ||
10245 | return ret; | |
10246 | } | |
10247 | ||
c072481d PB |
10248 | static __cold void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
10249 | struct task_struct *task, | |
10250 | bool cancel_all) | |
9936c7c2 | 10251 | { |
3dd0c97a | 10252 | struct io_task_cancel cancel = { .task = task, .all = cancel_all, }; |
1b00764f | 10253 | struct io_uring_task *tctx = task ? task->io_uring : NULL; |
9936c7c2 | 10254 | |
60053be8 PB |
10255 | /* failed during ring init, it couldn't have issued any requests */ |
10256 | if (!ctx->rings) | |
10257 | return; | |
10258 | ||
9936c7c2 PB |
10259 | while (1) { |
10260 | enum io_wq_cancel cret; | |
10261 | bool ret = false; | |
10262 | ||
1b00764f PB |
10263 | if (!task) { |
10264 | ret |= io_uring_try_cancel_iowq(ctx); | |
10265 | } else if (tctx && tctx->io_wq) { | |
10266 | /* | |
10267 | * Cancels requests of all rings, not only @ctx, but | |
10268 | * it's fine as the task is in exit/exec. | |
10269 | */ | |
5aa75ed5 | 10270 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, |
9936c7c2 PB |
10271 | &cancel, true); |
10272 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
10273 | } | |
10274 | ||
10275 | /* SQPOLL thread does its own polling */ | |
3dd0c97a | 10276 | if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) || |
d052d1d6 | 10277 | (ctx->sq_data && ctx->sq_data->thread == current)) { |
5eef4e87 | 10278 | while (!wq_list_empty(&ctx->iopoll_list)) { |
9936c7c2 PB |
10279 | io_iopoll_try_reap_events(ctx); |
10280 | ret = true; | |
10281 | } | |
10282 | } | |
10283 | ||
3dd0c97a PB |
10284 | ret |= io_cancel_defer_files(ctx, task, cancel_all); |
10285 | ret |= io_poll_remove_all(ctx, task, cancel_all); | |
10286 | ret |= io_kill_timeouts(ctx, task, cancel_all); | |
e5dc480d PB |
10287 | if (task) |
10288 | ret |= io_run_task_work(); | |
9936c7c2 PB |
10289 | if (!ret) |
10290 | break; | |
10291 | cond_resched(); | |
10292 | } | |
10293 | } | |
10294 | ||
eef51daa | 10295 | static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
0f212204 | 10296 | { |
236434c3 | 10297 | struct io_uring_task *tctx = current->io_uring; |
13bf43f5 | 10298 | struct io_tctx_node *node; |
a528b04e | 10299 | int ret; |
236434c3 MWO |
10300 | |
10301 | if (unlikely(!tctx)) { | |
5aa75ed5 | 10302 | ret = io_uring_alloc_task_context(current, ctx); |
0f212204 JA |
10303 | if (unlikely(ret)) |
10304 | return ret; | |
e139a1ec | 10305 | |
236434c3 | 10306 | tctx = current->io_uring; |
e139a1ec PB |
10307 | if (ctx->iowq_limits_set) { |
10308 | unsigned int limits[2] = { ctx->iowq_limits[0], | |
10309 | ctx->iowq_limits[1], }; | |
10310 | ||
10311 | ret = io_wq_max_workers(tctx->io_wq, limits); | |
10312 | if (ret) | |
10313 | return ret; | |
10314 | } | |
0f212204 | 10315 | } |
cf27f3b1 PB |
10316 | if (!xa_load(&tctx->xa, (unsigned long)ctx)) { |
10317 | node = kmalloc(sizeof(*node), GFP_KERNEL); | |
10318 | if (!node) | |
10319 | return -ENOMEM; | |
10320 | node->ctx = ctx; | |
10321 | node->task = current; | |
13bf43f5 | 10322 | |
cf27f3b1 PB |
10323 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx, |
10324 | node, GFP_KERNEL)); | |
10325 | if (ret) { | |
10326 | kfree(node); | |
10327 | return ret; | |
0f212204 | 10328 | } |
cf27f3b1 PB |
10329 | |
10330 | mutex_lock(&ctx->uring_lock); | |
10331 | list_add(&node->ctx_node, &ctx->tctx_list); | |
10332 | mutex_unlock(&ctx->uring_lock); | |
0f212204 | 10333 | } |
cf27f3b1 | 10334 | tctx->last = ctx; |
0f212204 JA |
10335 | return 0; |
10336 | } | |
10337 | ||
cf27f3b1 PB |
10338 | /* |
10339 | * Note that this task has used io_uring. We use it for cancelation purposes. | |
10340 | */ | |
eef51daa | 10341 | static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
cf27f3b1 PB |
10342 | { |
10343 | struct io_uring_task *tctx = current->io_uring; | |
10344 | ||
10345 | if (likely(tctx && tctx->last == ctx)) | |
10346 | return 0; | |
eef51daa | 10347 | return __io_uring_add_tctx_node(ctx); |
cf27f3b1 PB |
10348 | } |
10349 | ||
0f212204 JA |
10350 | /* |
10351 | * Remove this io_uring_file -> task mapping. | |
10352 | */ | |
c072481d | 10353 | static __cold void io_uring_del_tctx_node(unsigned long index) |
0f212204 JA |
10354 | { |
10355 | struct io_uring_task *tctx = current->io_uring; | |
13bf43f5 | 10356 | struct io_tctx_node *node; |
2941267b | 10357 | |
eebd2e37 PB |
10358 | if (!tctx) |
10359 | return; | |
13bf43f5 PB |
10360 | node = xa_erase(&tctx->xa, index); |
10361 | if (!node) | |
2941267b | 10362 | return; |
0f212204 | 10363 | |
13bf43f5 PB |
10364 | WARN_ON_ONCE(current != node->task); |
10365 | WARN_ON_ONCE(list_empty(&node->ctx_node)); | |
10366 | ||
10367 | mutex_lock(&node->ctx->uring_lock); | |
10368 | list_del(&node->ctx_node); | |
10369 | mutex_unlock(&node->ctx->uring_lock); | |
10370 | ||
baf186c4 | 10371 | if (tctx->last == node->ctx) |
0f212204 | 10372 | tctx->last = NULL; |
13bf43f5 | 10373 | kfree(node); |
0f212204 JA |
10374 | } |
10375 | ||
c072481d | 10376 | static __cold void io_uring_clean_tctx(struct io_uring_task *tctx) |
de7f1d9e | 10377 | { |
ba5ef6dc | 10378 | struct io_wq *wq = tctx->io_wq; |
13bf43f5 | 10379 | struct io_tctx_node *node; |
de7f1d9e PB |
10380 | unsigned long index; |
10381 | ||
8bab4c09 | 10382 | xa_for_each(&tctx->xa, index, node) { |
eef51daa | 10383 | io_uring_del_tctx_node(index); |
8bab4c09 JA |
10384 | cond_resched(); |
10385 | } | |
b16ef427 ME |
10386 | if (wq) { |
10387 | /* | |
f6f9b278 | 10388 | * Must be after io_uring_del_tctx_node() (removes nodes under |
b16ef427 ME |
10389 | * uring_lock) to avoid race with io_uring_try_cancel_iowq(). |
10390 | */ | |
ba5ef6dc | 10391 | io_wq_put_and_exit(wq); |
dadebc35 | 10392 | tctx->io_wq = NULL; |
b16ef427 | 10393 | } |
de7f1d9e PB |
10394 | } |
10395 | ||
3f48cf18 | 10396 | static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked) |
521d6a73 | 10397 | { |
3f48cf18 | 10398 | if (tracked) |
d5361233 | 10399 | return 0; |
521d6a73 PB |
10400 | return percpu_counter_sum(&tctx->inflight); |
10401 | } | |
10402 | ||
78cc687b PB |
10403 | /* |
10404 | * Find any io_uring ctx that this task has registered or done IO on, and cancel | |
78a78060 | 10405 | * requests. @sqd should be not-null IFF it's an SQPOLL thread cancellation. |
78cc687b | 10406 | */ |
c072481d PB |
10407 | static __cold void io_uring_cancel_generic(bool cancel_all, |
10408 | struct io_sq_data *sqd) | |
0e9ddb39 | 10409 | { |
521d6a73 | 10410 | struct io_uring_task *tctx = current->io_uring; |
734551df | 10411 | struct io_ring_ctx *ctx; |
0e9ddb39 PB |
10412 | s64 inflight; |
10413 | DEFINE_WAIT(wait); | |
fdaf083c | 10414 | |
78cc687b PB |
10415 | WARN_ON_ONCE(sqd && sqd->thread != current); |
10416 | ||
6d042ffb PO |
10417 | if (!current->io_uring) |
10418 | return; | |
17a91051 PB |
10419 | if (tctx->io_wq) |
10420 | io_wq_exit_start(tctx->io_wq); | |
10421 | ||
0e9ddb39 PB |
10422 | atomic_inc(&tctx->in_idle); |
10423 | do { | |
e9dbe221 | 10424 | io_uring_drop_tctx_refs(current); |
0e9ddb39 | 10425 | /* read completions before cancelations */ |
78cc687b | 10426 | inflight = tctx_inflight(tctx, !cancel_all); |
0e9ddb39 PB |
10427 | if (!inflight) |
10428 | break; | |
fdaf083c | 10429 | |
78cc687b PB |
10430 | if (!sqd) { |
10431 | struct io_tctx_node *node; | |
10432 | unsigned long index; | |
0f212204 | 10433 | |
78cc687b PB |
10434 | xa_for_each(&tctx->xa, index, node) { |
10435 | /* sqpoll task will cancel all its requests */ | |
10436 | if (node->ctx->sq_data) | |
10437 | continue; | |
10438 | io_uring_try_cancel_requests(node->ctx, current, | |
10439 | cancel_all); | |
10440 | } | |
10441 | } else { | |
10442 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) | |
10443 | io_uring_try_cancel_requests(ctx, current, | |
10444 | cancel_all); | |
10445 | } | |
17a91051 | 10446 | |
78a78060 JA |
10447 | prepare_to_wait(&tctx->wait, &wait, TASK_INTERRUPTIBLE); |
10448 | io_run_task_work(); | |
e9dbe221 | 10449 | io_uring_drop_tctx_refs(current); |
78a78060 | 10450 | |
0f212204 | 10451 | /* |
a1bb3cd5 PB |
10452 | * If we've seen completions, retry without waiting. This |
10453 | * avoids a race where a completion comes in before we did | |
10454 | * prepare_to_wait(). | |
0f212204 | 10455 | */ |
3dd0c97a | 10456 | if (inflight == tctx_inflight(tctx, !cancel_all)) |
a1bb3cd5 | 10457 | schedule(); |
f57555ed | 10458 | finish_wait(&tctx->wait, &wait); |
d8a6df10 | 10459 | } while (1); |
de7f1d9e | 10460 | |
8452d4a6 | 10461 | io_uring_clean_tctx(tctx); |
3dd0c97a | 10462 | if (cancel_all) { |
3cc7fdb9 PB |
10463 | /* |
10464 | * We shouldn't run task_works after cancel, so just leave | |
10465 | * ->in_idle set for normal exit. | |
10466 | */ | |
10467 | atomic_dec(&tctx->in_idle); | |
3f48cf18 PB |
10468 | /* for exec all current's requests should be gone, kill tctx */ |
10469 | __io_uring_free(current); | |
10470 | } | |
44e728b8 PB |
10471 | } |
10472 | ||
f552a27a | 10473 | void __io_uring_cancel(bool cancel_all) |
78cc687b | 10474 | { |
f552a27a | 10475 | io_uring_cancel_generic(cancel_all, NULL); |
78cc687b PB |
10476 | } |
10477 | ||
e7a6c00d JA |
10478 | void io_uring_unreg_ringfd(void) |
10479 | { | |
10480 | struct io_uring_task *tctx = current->io_uring; | |
10481 | int i; | |
10482 | ||
10483 | for (i = 0; i < IO_RINGFD_REG_MAX; i++) { | |
10484 | if (tctx->registered_rings[i]) { | |
10485 | fput(tctx->registered_rings[i]); | |
10486 | tctx->registered_rings[i] = NULL; | |
10487 | } | |
10488 | } | |
10489 | } | |
10490 | ||
10491 | static int io_ring_add_registered_fd(struct io_uring_task *tctx, int fd, | |
10492 | int start, int end) | |
10493 | { | |
10494 | struct file *file; | |
10495 | int offset; | |
10496 | ||
10497 | for (offset = start; offset < end; offset++) { | |
10498 | offset = array_index_nospec(offset, IO_RINGFD_REG_MAX); | |
10499 | if (tctx->registered_rings[offset]) | |
10500 | continue; | |
10501 | ||
10502 | file = fget(fd); | |
10503 | if (!file) { | |
10504 | return -EBADF; | |
10505 | } else if (file->f_op != &io_uring_fops) { | |
10506 | fput(file); | |
10507 | return -EOPNOTSUPP; | |
10508 | } | |
10509 | tctx->registered_rings[offset] = file; | |
10510 | return offset; | |
10511 | } | |
10512 | ||
10513 | return -EBUSY; | |
10514 | } | |
10515 | ||
10516 | /* | |
10517 | * Register a ring fd to avoid fdget/fdput for each io_uring_enter() | |
10518 | * invocation. User passes in an array of struct io_uring_rsrc_update | |
10519 | * with ->data set to the ring_fd, and ->offset given for the desired | |
10520 | * index. If no index is desired, application may set ->offset == -1U | |
10521 | * and we'll find an available index. Returns number of entries | |
10522 | * successfully processed, or < 0 on error if none were processed. | |
10523 | */ | |
10524 | static int io_ringfd_register(struct io_ring_ctx *ctx, void __user *__arg, | |
10525 | unsigned nr_args) | |
10526 | { | |
10527 | struct io_uring_rsrc_update __user *arg = __arg; | |
10528 | struct io_uring_rsrc_update reg; | |
10529 | struct io_uring_task *tctx; | |
10530 | int ret, i; | |
10531 | ||
10532 | if (!nr_args || nr_args > IO_RINGFD_REG_MAX) | |
10533 | return -EINVAL; | |
10534 | ||
10535 | mutex_unlock(&ctx->uring_lock); | |
10536 | ret = io_uring_add_tctx_node(ctx); | |
10537 | mutex_lock(&ctx->uring_lock); | |
10538 | if (ret) | |
10539 | return ret; | |
10540 | ||
10541 | tctx = current->io_uring; | |
10542 | for (i = 0; i < nr_args; i++) { | |
10543 | int start, end; | |
10544 | ||
10545 | if (copy_from_user(®, &arg[i], sizeof(reg))) { | |
10546 | ret = -EFAULT; | |
10547 | break; | |
10548 | } | |
10549 | ||
6fb53cf8 DY |
10550 | if (reg.resv) { |
10551 | ret = -EINVAL; | |
10552 | break; | |
10553 | } | |
10554 | ||
e7a6c00d JA |
10555 | if (reg.offset == -1U) { |
10556 | start = 0; | |
10557 | end = IO_RINGFD_REG_MAX; | |
10558 | } else { | |
10559 | if (reg.offset >= IO_RINGFD_REG_MAX) { | |
10560 | ret = -EINVAL; | |
10561 | break; | |
10562 | } | |
10563 | start = reg.offset; | |
10564 | end = start + 1; | |
10565 | } | |
10566 | ||
10567 | ret = io_ring_add_registered_fd(tctx, reg.data, start, end); | |
10568 | if (ret < 0) | |
10569 | break; | |
10570 | ||
10571 | reg.offset = ret; | |
10572 | if (copy_to_user(&arg[i], ®, sizeof(reg))) { | |
10573 | fput(tctx->registered_rings[reg.offset]); | |
10574 | tctx->registered_rings[reg.offset] = NULL; | |
10575 | ret = -EFAULT; | |
10576 | break; | |
10577 | } | |
10578 | } | |
10579 | ||
10580 | return i ? i : ret; | |
10581 | } | |
10582 | ||
10583 | static int io_ringfd_unregister(struct io_ring_ctx *ctx, void __user *__arg, | |
10584 | unsigned nr_args) | |
10585 | { | |
10586 | struct io_uring_rsrc_update __user *arg = __arg; | |
10587 | struct io_uring_task *tctx = current->io_uring; | |
10588 | struct io_uring_rsrc_update reg; | |
10589 | int ret = 0, i; | |
10590 | ||
10591 | if (!nr_args || nr_args > IO_RINGFD_REG_MAX) | |
10592 | return -EINVAL; | |
10593 | if (!tctx) | |
10594 | return 0; | |
10595 | ||
10596 | for (i = 0; i < nr_args; i++) { | |
10597 | if (copy_from_user(®, &arg[i], sizeof(reg))) { | |
10598 | ret = -EFAULT; | |
10599 | break; | |
10600 | } | |
6fb53cf8 | 10601 | if (reg.resv || reg.offset >= IO_RINGFD_REG_MAX) { |
e7a6c00d JA |
10602 | ret = -EINVAL; |
10603 | break; | |
10604 | } | |
10605 | ||
10606 | reg.offset = array_index_nospec(reg.offset, IO_RINGFD_REG_MAX); | |
10607 | if (tctx->registered_rings[reg.offset]) { | |
10608 | fput(tctx->registered_rings[reg.offset]); | |
10609 | tctx->registered_rings[reg.offset] = NULL; | |
10610 | } | |
10611 | } | |
10612 | ||
10613 | return i ? i : ret; | |
10614 | } | |
10615 | ||
6c5c240e RP |
10616 | static void *io_uring_validate_mmap_request(struct file *file, |
10617 | loff_t pgoff, size_t sz) | |
2b188cc1 | 10618 | { |
2b188cc1 | 10619 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 10620 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
10621 | struct page *page; |
10622 | void *ptr; | |
10623 | ||
10624 | switch (offset) { | |
10625 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
10626 | case IORING_OFF_CQ_RING: |
10627 | ptr = ctx->rings; | |
2b188cc1 JA |
10628 | break; |
10629 | case IORING_OFF_SQES: | |
10630 | ptr = ctx->sq_sqes; | |
10631 | break; | |
2b188cc1 | 10632 | default: |
6c5c240e | 10633 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
10634 | } |
10635 | ||
10636 | page = virt_to_head_page(ptr); | |
a50b854e | 10637 | if (sz > page_size(page)) |
6c5c240e RP |
10638 | return ERR_PTR(-EINVAL); |
10639 | ||
10640 | return ptr; | |
10641 | } | |
10642 | ||
10643 | #ifdef CONFIG_MMU | |
10644 | ||
c072481d | 10645 | static __cold int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
6c5c240e RP |
10646 | { |
10647 | size_t sz = vma->vm_end - vma->vm_start; | |
10648 | unsigned long pfn; | |
10649 | void *ptr; | |
10650 | ||
10651 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
10652 | if (IS_ERR(ptr)) | |
10653 | return PTR_ERR(ptr); | |
2b188cc1 JA |
10654 | |
10655 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
10656 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
10657 | } | |
10658 | ||
6c5c240e RP |
10659 | #else /* !CONFIG_MMU */ |
10660 | ||
10661 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
10662 | { | |
10663 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
10664 | } | |
10665 | ||
10666 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
10667 | { | |
10668 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
10669 | } | |
10670 | ||
10671 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
10672 | unsigned long addr, unsigned long len, | |
10673 | unsigned long pgoff, unsigned long flags) | |
10674 | { | |
10675 | void *ptr; | |
10676 | ||
10677 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
10678 | if (IS_ERR(ptr)) | |
10679 | return PTR_ERR(ptr); | |
10680 | ||
10681 | return (unsigned long) ptr; | |
10682 | } | |
10683 | ||
10684 | #endif /* !CONFIG_MMU */ | |
10685 | ||
d9d05217 | 10686 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
90554200 JA |
10687 | { |
10688 | DEFINE_WAIT(wait); | |
10689 | ||
10690 | do { | |
10691 | if (!io_sqring_full(ctx)) | |
10692 | break; | |
90554200 JA |
10693 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
10694 | ||
10695 | if (!io_sqring_full(ctx)) | |
10696 | break; | |
90554200 JA |
10697 | schedule(); |
10698 | } while (!signal_pending(current)); | |
10699 | ||
10700 | finish_wait(&ctx->sqo_sq_wait, &wait); | |
5199328a | 10701 | return 0; |
90554200 JA |
10702 | } |
10703 | ||
c73ebb68 HX |
10704 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
10705 | struct __kernel_timespec __user **ts, | |
10706 | const sigset_t __user **sig) | |
10707 | { | |
10708 | struct io_uring_getevents_arg arg; | |
10709 | ||
10710 | /* | |
10711 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer | |
10712 | * is just a pointer to the sigset_t. | |
10713 | */ | |
10714 | if (!(flags & IORING_ENTER_EXT_ARG)) { | |
10715 | *sig = (const sigset_t __user *) argp; | |
10716 | *ts = NULL; | |
10717 | return 0; | |
10718 | } | |
10719 | ||
10720 | /* | |
10721 | * EXT_ARG is set - ensure we agree on the size of it and copy in our | |
10722 | * timespec and sigset_t pointers if good. | |
10723 | */ | |
10724 | if (*argsz != sizeof(arg)) | |
10725 | return -EINVAL; | |
10726 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
10727 | return -EFAULT; | |
d2347b96 DY |
10728 | if (arg.pad) |
10729 | return -EINVAL; | |
c73ebb68 HX |
10730 | *sig = u64_to_user_ptr(arg.sigmask); |
10731 | *argsz = arg.sigmask_sz; | |
10732 | *ts = u64_to_user_ptr(arg.ts); | |
10733 | return 0; | |
10734 | } | |
10735 | ||
2b188cc1 | 10736 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
c73ebb68 HX |
10737 | u32, min_complete, u32, flags, const void __user *, argp, |
10738 | size_t, argsz) | |
2b188cc1 JA |
10739 | { |
10740 | struct io_ring_ctx *ctx; | |
2b188cc1 JA |
10741 | int submitted = 0; |
10742 | struct fd f; | |
33f993da | 10743 | long ret; |
2b188cc1 | 10744 | |
4c6e277c | 10745 | io_run_task_work(); |
b41e9852 | 10746 | |
33f993da | 10747 | if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
e7a6c00d JA |
10748 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG | |
10749 | IORING_ENTER_REGISTERED_RING))) | |
2b188cc1 JA |
10750 | return -EINVAL; |
10751 | ||
e7a6c00d JA |
10752 | /* |
10753 | * Ring fd has been registered via IORING_REGISTER_RING_FDS, we | |
10754 | * need only dereference our task private array to find it. | |
10755 | */ | |
10756 | if (flags & IORING_ENTER_REGISTERED_RING) { | |
10757 | struct io_uring_task *tctx = current->io_uring; | |
10758 | ||
10759 | if (!tctx || fd >= IO_RINGFD_REG_MAX) | |
10760 | return -EINVAL; | |
10761 | fd = array_index_nospec(fd, IO_RINGFD_REG_MAX); | |
10762 | f.file = tctx->registered_rings[fd]; | |
10763 | if (unlikely(!f.file)) | |
10764 | return -EBADF; | |
10765 | } else { | |
10766 | f = fdget(fd); | |
10767 | if (unlikely(!f.file)) | |
10768 | return -EBADF; | |
10769 | } | |
2b188cc1 JA |
10770 | |
10771 | ret = -EOPNOTSUPP; | |
33f993da | 10772 | if (unlikely(f.file->f_op != &io_uring_fops)) |
2b188cc1 JA |
10773 | goto out_fput; |
10774 | ||
10775 | ret = -ENXIO; | |
10776 | ctx = f.file->private_data; | |
33f993da | 10777 | if (unlikely(!percpu_ref_tryget(&ctx->refs))) |
2b188cc1 JA |
10778 | goto out_fput; |
10779 | ||
7e84e1c7 | 10780 | ret = -EBADFD; |
33f993da | 10781 | if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED)) |
7e84e1c7 SG |
10782 | goto out; |
10783 | ||
6c271ce2 JA |
10784 | /* |
10785 | * For SQ polling, the thread will do all submissions and completions. | |
10786 | * Just return the requested submit count, and wake the thread if | |
10787 | * we were asked to. | |
10788 | */ | |
b2a9eada | 10789 | ret = 0; |
6c271ce2 | 10790 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
90f67366 | 10791 | io_cqring_overflow_flush(ctx); |
89448c47 | 10792 | |
21f96522 JA |
10793 | if (unlikely(ctx->sq_data->thread == NULL)) { |
10794 | ret = -EOWNERDEAD; | |
04147488 | 10795 | goto out; |
21f96522 | 10796 | } |
6c271ce2 | 10797 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 10798 | wake_up(&ctx->sq_data->wait); |
d9d05217 PB |
10799 | if (flags & IORING_ENTER_SQ_WAIT) { |
10800 | ret = io_sqpoll_wait_sq(ctx); | |
10801 | if (ret) | |
10802 | goto out; | |
10803 | } | |
6c271ce2 | 10804 | submitted = to_submit; |
b2a9eada | 10805 | } else if (to_submit) { |
eef51daa | 10806 | ret = io_uring_add_tctx_node(ctx); |
0f212204 JA |
10807 | if (unlikely(ret)) |
10808 | goto out; | |
2b188cc1 | 10809 | mutex_lock(&ctx->uring_lock); |
0f212204 | 10810 | submitted = io_submit_sqes(ctx, to_submit); |
2b188cc1 | 10811 | mutex_unlock(&ctx->uring_lock); |
7c504e65 PB |
10812 | |
10813 | if (submitted != to_submit) | |
10814 | goto out; | |
2b188cc1 JA |
10815 | } |
10816 | if (flags & IORING_ENTER_GETEVENTS) { | |
c73ebb68 HX |
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; | |
10823 | ||
2b188cc1 JA |
10824 | min_complete = min(min_complete, ctx->cq_entries); |
10825 | ||
32b2244a XW |
10826 | /* |
10827 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
10828 | * space applications don't need to do io completion events | |
10829 | * polling again, they can rely on io_sq_thread to do polling | |
10830 | * work, which can reduce cpu usage and uring_lock contention. | |
10831 | */ | |
10832 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
10833 | !(ctx->flags & IORING_SETUP_SQPOLL)) { | |
7668b92a | 10834 | ret = io_iopoll_check(ctx, min_complete); |
def596e9 | 10835 | } else { |
c73ebb68 | 10836 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
def596e9 | 10837 | } |
2b188cc1 JA |
10838 | } |
10839 | ||
7c504e65 | 10840 | out: |
6805b32e | 10841 | percpu_ref_put(&ctx->refs); |
2b188cc1 | 10842 | out_fput: |
e7a6c00d JA |
10843 | if (!(flags & IORING_ENTER_REGISTERED_RING)) |
10844 | fdput(f); | |
2b188cc1 JA |
10845 | return submitted ? submitted : ret; |
10846 | } | |
10847 | ||
bebdb65e | 10848 | #ifdef CONFIG_PROC_FS |
c072481d | 10849 | static __cold int io_uring_show_cred(struct seq_file *m, unsigned int id, |
61cf9370 | 10850 | const struct cred *cred) |
87ce955b | 10851 | { |
87ce955b JA |
10852 | struct user_namespace *uns = seq_user_ns(m); |
10853 | struct group_info *gi; | |
10854 | kernel_cap_t cap; | |
10855 | unsigned __capi; | |
10856 | int g; | |
10857 | ||
10858 | seq_printf(m, "%5d\n", id); | |
10859 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
10860 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
10861 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
10862 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
10863 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
10864 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
10865 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
10866 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
10867 | seq_puts(m, "\n\tGroups:\t"); | |
10868 | gi = cred->group_info; | |
10869 | for (g = 0; g < gi->ngroups; g++) { | |
10870 | seq_put_decimal_ull(m, g ? " " : "", | |
10871 | from_kgid_munged(uns, gi->gid[g])); | |
10872 | } | |
10873 | seq_puts(m, "\n\tCapEff:\t"); | |
10874 | cap = cred->cap_effective; | |
10875 | CAP_FOR_EACH_U32(__capi) | |
10876 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
10877 | seq_putc(m, '\n'); | |
10878 | return 0; | |
10879 | } | |
10880 | ||
c072481d PB |
10881 | static __cold void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, |
10882 | struct seq_file *m) | |
87ce955b | 10883 | { |
dbbe9c64 | 10884 | struct io_sq_data *sq = NULL; |
83f84356 HX |
10885 | struct io_overflow_cqe *ocqe; |
10886 | struct io_rings *r = ctx->rings; | |
10887 | unsigned int sq_mask = ctx->sq_entries - 1, cq_mask = ctx->cq_entries - 1; | |
83f84356 HX |
10888 | unsigned int sq_head = READ_ONCE(r->sq.head); |
10889 | unsigned int sq_tail = READ_ONCE(r->sq.tail); | |
10890 | unsigned int cq_head = READ_ONCE(r->cq.head); | |
10891 | unsigned int cq_tail = READ_ONCE(r->cq.tail); | |
f75d1183 | 10892 | unsigned int sq_entries, cq_entries; |
fad8e0de | 10893 | bool has_lock; |
83f84356 HX |
10894 | unsigned int i; |
10895 | ||
10896 | /* | |
10897 | * we may get imprecise sqe and cqe info if uring is actively running | |
10898 | * since we get cached_sq_head and cached_cq_tail without uring_lock | |
10899 | * and sq_tail and cq_head are changed by userspace. But it's ok since | |
10900 | * we usually use these info when it is stuck. | |
10901 | */ | |
c0235652 | 10902 | seq_printf(m, "SqMask:\t0x%x\n", sq_mask); |
f75d1183 JA |
10903 | seq_printf(m, "SqHead:\t%u\n", sq_head); |
10904 | seq_printf(m, "SqTail:\t%u\n", sq_tail); | |
10905 | seq_printf(m, "CachedSqHead:\t%u\n", ctx->cached_sq_head); | |
10906 | seq_printf(m, "CqMask:\t0x%x\n", cq_mask); | |
10907 | seq_printf(m, "CqHead:\t%u\n", cq_head); | |
10908 | seq_printf(m, "CqTail:\t%u\n", cq_tail); | |
10909 | seq_printf(m, "CachedCqTail:\t%u\n", ctx->cached_cq_tail); | |
10910 | seq_printf(m, "SQEs:\t%u\n", sq_tail - ctx->cached_sq_head); | |
10911 | sq_entries = min(sq_tail - sq_head, ctx->sq_entries); | |
10912 | for (i = 0; i < sq_entries; i++) { | |
10913 | unsigned int entry = i + sq_head; | |
10914 | unsigned int sq_idx = READ_ONCE(ctx->sq_array[entry & sq_mask]); | |
a1957780 | 10915 | struct io_uring_sqe *sqe; |
f75d1183 JA |
10916 | |
10917 | if (sq_idx > sq_mask) | |
10918 | continue; | |
10919 | sqe = &ctx->sq_sqes[sq_idx]; | |
10920 | seq_printf(m, "%5u: opcode:%d, fd:%d, flags:%x, user_data:%llu\n", | |
10921 | sq_idx, sqe->opcode, sqe->fd, sqe->flags, | |
10922 | sqe->user_data); | |
83f84356 | 10923 | } |
f75d1183 JA |
10924 | seq_printf(m, "CQEs:\t%u\n", cq_tail - cq_head); |
10925 | cq_entries = min(cq_tail - cq_head, ctx->cq_entries); | |
10926 | for (i = 0; i < cq_entries; i++) { | |
10927 | unsigned int entry = i + cq_head; | |
10928 | struct io_uring_cqe *cqe = &r->cqes[entry & cq_mask]; | |
83f84356 HX |
10929 | |
10930 | seq_printf(m, "%5u: user_data:%llu, res:%d, flag:%x\n", | |
f75d1183 JA |
10931 | entry & cq_mask, cqe->user_data, cqe->res, |
10932 | cqe->flags); | |
83f84356 | 10933 | } |
87ce955b | 10934 | |
fad8e0de JA |
10935 | /* |
10936 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, | |
10937 | * since fdinfo case grabs it in the opposite direction of normal use | |
10938 | * cases. If we fail to get the lock, we just don't iterate any | |
10939 | * structures that could be going away outside the io_uring mutex. | |
10940 | */ | |
10941 | has_lock = mutex_trylock(&ctx->uring_lock); | |
10942 | ||
5f3f26f9 | 10943 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) { |
dbbe9c64 | 10944 | sq = ctx->sq_data; |
5f3f26f9 JA |
10945 | if (!sq->thread) |
10946 | sq = NULL; | |
10947 | } | |
dbbe9c64 JQ |
10948 | |
10949 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); | |
10950 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); | |
87ce955b | 10951 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
fad8e0de | 10952 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
7b29f92d | 10953 | struct file *f = io_file_from_index(ctx, i); |
87ce955b | 10954 | |
87ce955b JA |
10955 | if (f) |
10956 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
10957 | else | |
10958 | seq_printf(m, "%5u: <none>\n", i); | |
10959 | } | |
10960 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
fad8e0de | 10961 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
41edf1a5 | 10962 | struct io_mapped_ubuf *buf = ctx->user_bufs[i]; |
4751f53d | 10963 | unsigned int len = buf->ubuf_end - buf->ubuf; |
87ce955b | 10964 | |
4751f53d | 10965 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len); |
87ce955b | 10966 | } |
61cf9370 MWO |
10967 | if (has_lock && !xa_empty(&ctx->personalities)) { |
10968 | unsigned long index; | |
10969 | const struct cred *cred; | |
10970 | ||
87ce955b | 10971 | seq_printf(m, "Personalities:\n"); |
61cf9370 MWO |
10972 | xa_for_each(&ctx->personalities, index, cred) |
10973 | io_uring_show_cred(m, index, cred); | |
87ce955b | 10974 | } |
83f84356 HX |
10975 | if (has_lock) |
10976 | mutex_unlock(&ctx->uring_lock); | |
10977 | ||
10978 | seq_puts(m, "PollList:\n"); | |
79ebeaee | 10979 | spin_lock(&ctx->completion_lock); |
d7718a9d JA |
10980 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
10981 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
10982 | struct io_kiocb *req; | |
10983 | ||
10984 | hlist_for_each_entry(req, list, hash_node) | |
10985 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
7f62d40d | 10986 | task_work_pending(req->task)); |
d7718a9d | 10987 | } |
83f84356 HX |
10988 | |
10989 | seq_puts(m, "CqOverflowList:\n"); | |
10990 | list_for_each_entry(ocqe, &ctx->cq_overflow_list, list) { | |
10991 | struct io_uring_cqe *cqe = &ocqe->cqe; | |
10992 | ||
10993 | seq_printf(m, " user_data=%llu, res=%d, flags=%x\n", | |
10994 | cqe->user_data, cqe->res, cqe->flags); | |
10995 | ||
10996 | } | |
10997 | ||
79ebeaee | 10998 | spin_unlock(&ctx->completion_lock); |
87ce955b JA |
10999 | } |
11000 | ||
c072481d | 11001 | static __cold void io_uring_show_fdinfo(struct seq_file *m, struct file *f) |
87ce955b JA |
11002 | { |
11003 | struct io_ring_ctx *ctx = f->private_data; | |
11004 | ||
11005 | if (percpu_ref_tryget(&ctx->refs)) { | |
11006 | __io_uring_show_fdinfo(ctx, m); | |
11007 | percpu_ref_put(&ctx->refs); | |
11008 | } | |
11009 | } | |
bebdb65e | 11010 | #endif |
87ce955b | 11011 | |
2b188cc1 JA |
11012 | static const struct file_operations io_uring_fops = { |
11013 | .release = io_uring_release, | |
11014 | .mmap = io_uring_mmap, | |
6c5c240e RP |
11015 | #ifndef CONFIG_MMU |
11016 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
11017 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
11018 | #endif | |
2b188cc1 | 11019 | .poll = io_uring_poll, |
bebdb65e | 11020 | #ifdef CONFIG_PROC_FS |
87ce955b | 11021 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 11022 | #endif |
2b188cc1 JA |
11023 | }; |
11024 | ||
c072481d PB |
11025 | static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
11026 | struct io_uring_params *p) | |
2b188cc1 | 11027 | { |
75b28aff HV |
11028 | struct io_rings *rings; |
11029 | size_t size, sq_array_offset; | |
2b188cc1 | 11030 | |
bd740481 JA |
11031 | /* make sure these are sane, as we already accounted them */ |
11032 | ctx->sq_entries = p->sq_entries; | |
11033 | ctx->cq_entries = p->cq_entries; | |
11034 | ||
75b28aff HV |
11035 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
11036 | if (size == SIZE_MAX) | |
11037 | return -EOVERFLOW; | |
11038 | ||
11039 | rings = io_mem_alloc(size); | |
11040 | if (!rings) | |
2b188cc1 JA |
11041 | return -ENOMEM; |
11042 | ||
75b28aff HV |
11043 | ctx->rings = rings; |
11044 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
11045 | rings->sq_ring_mask = p->sq_entries - 1; | |
11046 | rings->cq_ring_mask = p->cq_entries - 1; | |
11047 | rings->sq_ring_entries = p->sq_entries; | |
11048 | rings->cq_ring_entries = p->cq_entries; | |
2b188cc1 JA |
11049 | |
11050 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
11051 | if (size == SIZE_MAX) { |
11052 | io_mem_free(ctx->rings); | |
11053 | ctx->rings = NULL; | |
2b188cc1 | 11054 | return -EOVERFLOW; |
eb065d30 | 11055 | } |
2b188cc1 JA |
11056 | |
11057 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
11058 | if (!ctx->sq_sqes) { |
11059 | io_mem_free(ctx->rings); | |
11060 | ctx->rings = NULL; | |
2b188cc1 | 11061 | return -ENOMEM; |
eb065d30 | 11062 | } |
2b188cc1 | 11063 | |
2b188cc1 JA |
11064 | return 0; |
11065 | } | |
11066 | ||
9faadcc8 PB |
11067 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
11068 | { | |
11069 | int ret, fd; | |
11070 | ||
11071 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
11072 | if (fd < 0) | |
11073 | return fd; | |
11074 | ||
eef51daa | 11075 | ret = io_uring_add_tctx_node(ctx); |
9faadcc8 PB |
11076 | if (ret) { |
11077 | put_unused_fd(fd); | |
11078 | return ret; | |
11079 | } | |
11080 | fd_install(fd, file); | |
11081 | return fd; | |
11082 | } | |
11083 | ||
2b188cc1 JA |
11084 | /* |
11085 | * Allocate an anonymous fd, this is what constitutes the application | |
11086 | * visible backing of an io_uring instance. The application mmaps this | |
11087 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
11088 | * we have to tie this fd to a socket for file garbage collection purposes. | |
11089 | */ | |
9faadcc8 | 11090 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
2b188cc1 JA |
11091 | { |
11092 | struct file *file; | |
9faadcc8 | 11093 | #if defined(CONFIG_UNIX) |
2b188cc1 JA |
11094 | int ret; |
11095 | ||
2b188cc1 JA |
11096 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
11097 | &ctx->ring_sock); | |
11098 | if (ret) | |
9faadcc8 | 11099 | return ERR_PTR(ret); |
2b188cc1 JA |
11100 | #endif |
11101 | ||
91a9ab7c PM |
11102 | file = anon_inode_getfile_secure("[io_uring]", &io_uring_fops, ctx, |
11103 | O_RDWR | O_CLOEXEC, NULL); | |
2b188cc1 | 11104 | #if defined(CONFIG_UNIX) |
9faadcc8 PB |
11105 | if (IS_ERR(file)) { |
11106 | sock_release(ctx->ring_sock); | |
11107 | ctx->ring_sock = NULL; | |
11108 | } else { | |
11109 | ctx->ring_sock->file = file; | |
0f212204 | 11110 | } |
2b188cc1 | 11111 | #endif |
9faadcc8 | 11112 | return file; |
2b188cc1 JA |
11113 | } |
11114 | ||
c072481d PB |
11115 | static __cold int io_uring_create(unsigned entries, struct io_uring_params *p, |
11116 | struct io_uring_params __user *params) | |
2b188cc1 | 11117 | { |
2b188cc1 | 11118 | struct io_ring_ctx *ctx; |
9faadcc8 | 11119 | struct file *file; |
2b188cc1 JA |
11120 | int ret; |
11121 | ||
8110c1a6 | 11122 | if (!entries) |
2b188cc1 | 11123 | return -EINVAL; |
8110c1a6 JA |
11124 | if (entries > IORING_MAX_ENTRIES) { |
11125 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
11126 | return -EINVAL; | |
11127 | entries = IORING_MAX_ENTRIES; | |
11128 | } | |
2b188cc1 JA |
11129 | |
11130 | /* | |
11131 | * Use twice as many entries for the CQ ring. It's possible for the | |
11132 | * application to drive a higher depth than the size of the SQ ring, | |
11133 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
11134 | * some flexibility in overcommitting a bit. If the application has |
11135 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
11136 | * of CQ ring entries manually. | |
2b188cc1 JA |
11137 | */ |
11138 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
11139 | if (p->flags & IORING_SETUP_CQSIZE) { |
11140 | /* | |
11141 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
11142 | * to a power-of-two, if it isn't already. We do NOT impose | |
11143 | * any cq vs sq ring sizing. | |
11144 | */ | |
eb2667b3 | 11145 | if (!p->cq_entries) |
33a107f0 | 11146 | return -EINVAL; |
8110c1a6 JA |
11147 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
11148 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
11149 | return -EINVAL; | |
11150 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
11151 | } | |
eb2667b3 JQ |
11152 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
11153 | if (p->cq_entries < p->sq_entries) | |
11154 | return -EINVAL; | |
33a107f0 JA |
11155 | } else { |
11156 | p->cq_entries = 2 * p->sq_entries; | |
11157 | } | |
2b188cc1 | 11158 | |
2b188cc1 | 11159 | ctx = io_ring_ctx_alloc(p); |
62e398be | 11160 | if (!ctx) |
2b188cc1 | 11161 | return -ENOMEM; |
2b188cc1 | 11162 | ctx->compat = in_compat_syscall(); |
62e398be JA |
11163 | if (!capable(CAP_IPC_LOCK)) |
11164 | ctx->user = get_uid(current_user()); | |
2aede0e4 JA |
11165 | |
11166 | /* | |
11167 | * This is just grabbed for accounting purposes. When a process exits, | |
11168 | * the mm is exited and dropped before the files, hence we need to hang | |
11169 | * on to this mm purely for the purposes of being able to unaccount | |
11170 | * memory (locked/pinned vm). It's not used for anything else. | |
11171 | */ | |
6b7898eb | 11172 | mmgrab(current->mm); |
2aede0e4 | 11173 | ctx->mm_account = current->mm; |
6b7898eb | 11174 | |
2b188cc1 JA |
11175 | ret = io_allocate_scq_urings(ctx, p); |
11176 | if (ret) | |
11177 | goto err; | |
11178 | ||
7e84e1c7 | 11179 | ret = io_sq_offload_create(ctx, p); |
2b188cc1 JA |
11180 | if (ret) |
11181 | goto err; | |
eae071c9 | 11182 | /* always set a rsrc node */ |
47b228ce PB |
11183 | ret = io_rsrc_node_switch_start(ctx); |
11184 | if (ret) | |
11185 | goto err; | |
eae071c9 | 11186 | io_rsrc_node_switch(ctx, NULL); |
2b188cc1 | 11187 | |
2b188cc1 | 11188 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
11189 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
11190 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
11191 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
11192 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
11193 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
11194 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
11195 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
11196 | |
11197 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
11198 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
11199 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
11200 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
11201 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
11202 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
11203 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 11204 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 11205 | |
7f13657d XW |
11206 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
11207 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 | 11208 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
c73ebb68 | 11209 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
9690557e | 11210 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS | |
c4212f3e JA |
11211 | IORING_FEAT_RSRC_TAGS | IORING_FEAT_CQE_SKIP | |
11212 | IORING_FEAT_LINKED_FILE; | |
7f13657d XW |
11213 | |
11214 | if (copy_to_user(params, p, sizeof(*p))) { | |
11215 | ret = -EFAULT; | |
11216 | goto err; | |
11217 | } | |
d1719f70 | 11218 | |
9faadcc8 PB |
11219 | file = io_uring_get_file(ctx); |
11220 | if (IS_ERR(file)) { | |
11221 | ret = PTR_ERR(file); | |
11222 | goto err; | |
11223 | } | |
11224 | ||
044c1ab3 JA |
11225 | /* |
11226 | * Install ring fd as the very last thing, so we don't risk someone | |
11227 | * having closed it before we finish setup | |
11228 | */ | |
9faadcc8 PB |
11229 | ret = io_uring_install_fd(ctx, file); |
11230 | if (ret < 0) { | |
11231 | /* fput will clean it up */ | |
11232 | fput(file); | |
11233 | return ret; | |
11234 | } | |
044c1ab3 | 11235 | |
c826bd7a | 11236 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
11237 | return ret; |
11238 | err: | |
11239 | io_ring_ctx_wait_and_kill(ctx); | |
11240 | return ret; | |
11241 | } | |
11242 | ||
11243 | /* | |
11244 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
11245 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
11246 | * params structure passed in. | |
11247 | */ | |
11248 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
11249 | { | |
11250 | struct io_uring_params p; | |
2b188cc1 JA |
11251 | int i; |
11252 | ||
11253 | if (copy_from_user(&p, params, sizeof(p))) | |
11254 | return -EFAULT; | |
11255 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
11256 | if (p.resv[i]) | |
11257 | return -EINVAL; | |
11258 | } | |
11259 | ||
6c271ce2 | 11260 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 11261 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 | 11262 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
bcbb7bf6 | 11263 | IORING_SETUP_R_DISABLED | IORING_SETUP_SUBMIT_ALL)) |
2b188cc1 JA |
11264 | return -EINVAL; |
11265 | ||
7f13657d | 11266 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
11267 | } |
11268 | ||
11269 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
11270 | struct io_uring_params __user *, params) | |
11271 | { | |
11272 | return io_uring_setup(entries, params); | |
11273 | } | |
11274 | ||
c072481d PB |
11275 | static __cold int io_probe(struct io_ring_ctx *ctx, void __user *arg, |
11276 | unsigned nr_args) | |
66f4af93 JA |
11277 | { |
11278 | struct io_uring_probe *p; | |
11279 | size_t size; | |
11280 | int i, ret; | |
11281 | ||
11282 | size = struct_size(p, ops, nr_args); | |
11283 | if (size == SIZE_MAX) | |
11284 | return -EOVERFLOW; | |
11285 | p = kzalloc(size, GFP_KERNEL); | |
11286 | if (!p) | |
11287 | return -ENOMEM; | |
11288 | ||
11289 | ret = -EFAULT; | |
11290 | if (copy_from_user(p, arg, size)) | |
11291 | goto out; | |
11292 | ret = -EINVAL; | |
11293 | if (memchr_inv(p, 0, size)) | |
11294 | goto out; | |
11295 | ||
11296 | p->last_op = IORING_OP_LAST - 1; | |
11297 | if (nr_args > IORING_OP_LAST) | |
11298 | nr_args = IORING_OP_LAST; | |
11299 | ||
11300 | for (i = 0; i < nr_args; i++) { | |
11301 | p->ops[i].op = i; | |
11302 | if (!io_op_defs[i].not_supported) | |
11303 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
11304 | } | |
11305 | p->ops_len = i; | |
11306 | ||
11307 | ret = 0; | |
11308 | if (copy_to_user(arg, p, size)) | |
11309 | ret = -EFAULT; | |
11310 | out: | |
11311 | kfree(p); | |
11312 | return ret; | |
11313 | } | |
11314 | ||
071698e1 JA |
11315 | static int io_register_personality(struct io_ring_ctx *ctx) |
11316 | { | |
4379bf8b | 11317 | const struct cred *creds; |
61cf9370 | 11318 | u32 id; |
1e6fa521 | 11319 | int ret; |
071698e1 | 11320 | |
4379bf8b | 11321 | creds = get_current_cred(); |
1e6fa521 | 11322 | |
61cf9370 MWO |
11323 | ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds, |
11324 | XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL); | |
a30f895a JA |
11325 | if (ret < 0) { |
11326 | put_cred(creds); | |
11327 | return ret; | |
11328 | } | |
11329 | return id; | |
071698e1 JA |
11330 | } |
11331 | ||
c072481d PB |
11332 | static __cold int io_register_restrictions(struct io_ring_ctx *ctx, |
11333 | void __user *arg, unsigned int nr_args) | |
21b55dbc SG |
11334 | { |
11335 | struct io_uring_restriction *res; | |
11336 | size_t size; | |
11337 | int i, ret; | |
11338 | ||
7e84e1c7 SG |
11339 | /* Restrictions allowed only if rings started disabled */ |
11340 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
11341 | return -EBADFD; | |
11342 | ||
21b55dbc | 11343 | /* We allow only a single restrictions registration */ |
7e84e1c7 | 11344 | if (ctx->restrictions.registered) |
21b55dbc SG |
11345 | return -EBUSY; |
11346 | ||
11347 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) | |
11348 | return -EINVAL; | |
11349 | ||
11350 | size = array_size(nr_args, sizeof(*res)); | |
11351 | if (size == SIZE_MAX) | |
11352 | return -EOVERFLOW; | |
11353 | ||
11354 | res = memdup_user(arg, size); | |
11355 | if (IS_ERR(res)) | |
11356 | return PTR_ERR(res); | |
11357 | ||
11358 | ret = 0; | |
11359 | ||
11360 | for (i = 0; i < nr_args; i++) { | |
11361 | switch (res[i].opcode) { | |
11362 | case IORING_RESTRICTION_REGISTER_OP: | |
11363 | if (res[i].register_op >= IORING_REGISTER_LAST) { | |
11364 | ret = -EINVAL; | |
11365 | goto out; | |
11366 | } | |
11367 | ||
11368 | __set_bit(res[i].register_op, | |
11369 | ctx->restrictions.register_op); | |
11370 | break; | |
11371 | case IORING_RESTRICTION_SQE_OP: | |
11372 | if (res[i].sqe_op >= IORING_OP_LAST) { | |
11373 | ret = -EINVAL; | |
11374 | goto out; | |
11375 | } | |
11376 | ||
11377 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); | |
11378 | break; | |
11379 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: | |
11380 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; | |
11381 | break; | |
11382 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: | |
11383 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; | |
11384 | break; | |
11385 | default: | |
11386 | ret = -EINVAL; | |
11387 | goto out; | |
11388 | } | |
11389 | } | |
11390 | ||
11391 | out: | |
11392 | /* Reset all restrictions if an error happened */ | |
11393 | if (ret != 0) | |
11394 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); | |
11395 | else | |
7e84e1c7 | 11396 | ctx->restrictions.registered = true; |
21b55dbc SG |
11397 | |
11398 | kfree(res); | |
11399 | return ret; | |
11400 | } | |
11401 | ||
7e84e1c7 SG |
11402 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
11403 | { | |
11404 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
11405 | return -EBADFD; | |
11406 | ||
11407 | if (ctx->restrictions.registered) | |
11408 | ctx->restricted = 1; | |
11409 | ||
0298ef96 PB |
11410 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
11411 | if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait)) | |
11412 | wake_up(&ctx->sq_data->wait); | |
7e84e1c7 SG |
11413 | return 0; |
11414 | } | |
11415 | ||
fdecb662 | 11416 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 11417 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 PB |
11418 | unsigned nr_args) |
11419 | { | |
11420 | __u32 tmp; | |
11421 | int err; | |
11422 | ||
11423 | if (check_add_overflow(up->offset, nr_args, &tmp)) | |
11424 | return -EOVERFLOW; | |
11425 | err = io_rsrc_node_switch_start(ctx); | |
11426 | if (err) | |
11427 | return err; | |
11428 | ||
fdecb662 PB |
11429 | switch (type) { |
11430 | case IORING_RSRC_FILE: | |
98f0b3b4 | 11431 | return __io_sqe_files_update(ctx, up, nr_args); |
634d00df PB |
11432 | case IORING_RSRC_BUFFER: |
11433 | return __io_sqe_buffers_update(ctx, up, nr_args); | |
98f0b3b4 PB |
11434 | } |
11435 | return -EINVAL; | |
11436 | } | |
11437 | ||
c3bdad02 PB |
11438 | static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg, |
11439 | unsigned nr_args) | |
98f0b3b4 | 11440 | { |
c3bdad02 | 11441 | struct io_uring_rsrc_update2 up; |
98f0b3b4 PB |
11442 | |
11443 | if (!nr_args) | |
11444 | return -EINVAL; | |
c3bdad02 PB |
11445 | memset(&up, 0, sizeof(up)); |
11446 | if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update))) | |
11447 | return -EFAULT; | |
d8a3ba9c | 11448 | if (up.resv || up.resv2) |
565c5e61 | 11449 | return -EINVAL; |
c3bdad02 PB |
11450 | return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args); |
11451 | } | |
11452 | ||
11453 | static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg, | |
992da01a | 11454 | unsigned size, unsigned type) |
c3bdad02 PB |
11455 | { |
11456 | struct io_uring_rsrc_update2 up; | |
11457 | ||
11458 | if (size != sizeof(up)) | |
11459 | return -EINVAL; | |
98f0b3b4 PB |
11460 | if (copy_from_user(&up, arg, sizeof(up))) |
11461 | return -EFAULT; | |
d8a3ba9c | 11462 | if (!up.nr || up.resv || up.resv2) |
98f0b3b4 | 11463 | return -EINVAL; |
992da01a | 11464 | return __io_register_rsrc_update(ctx, type, &up, up.nr); |
98f0b3b4 PB |
11465 | } |
11466 | ||
c072481d | 11467 | static __cold int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg, |
992da01a | 11468 | unsigned int size, unsigned int type) |
792e3582 PB |
11469 | { |
11470 | struct io_uring_rsrc_register rr; | |
11471 | ||
11472 | /* keep it extendible */ | |
11473 | if (size != sizeof(rr)) | |
11474 | return -EINVAL; | |
11475 | ||
11476 | memset(&rr, 0, sizeof(rr)); | |
11477 | if (copy_from_user(&rr, arg, size)) | |
11478 | return -EFAULT; | |
992da01a | 11479 | if (!rr.nr || rr.resv || rr.resv2) |
792e3582 PB |
11480 | return -EINVAL; |
11481 | ||
992da01a | 11482 | switch (type) { |
792e3582 PB |
11483 | case IORING_RSRC_FILE: |
11484 | return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data), | |
11485 | rr.nr, u64_to_user_ptr(rr.tags)); | |
634d00df PB |
11486 | case IORING_RSRC_BUFFER: |
11487 | return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data), | |
11488 | rr.nr, u64_to_user_ptr(rr.tags)); | |
792e3582 PB |
11489 | } |
11490 | return -EINVAL; | |
11491 | } | |
11492 | ||
c072481d PB |
11493 | static __cold int io_register_iowq_aff(struct io_ring_ctx *ctx, |
11494 | void __user *arg, unsigned len) | |
fe76421d JA |
11495 | { |
11496 | struct io_uring_task *tctx = current->io_uring; | |
11497 | cpumask_var_t new_mask; | |
11498 | int ret; | |
11499 | ||
11500 | if (!tctx || !tctx->io_wq) | |
11501 | return -EINVAL; | |
11502 | ||
11503 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) | |
11504 | return -ENOMEM; | |
11505 | ||
11506 | cpumask_clear(new_mask); | |
11507 | if (len > cpumask_size()) | |
11508 | len = cpumask_size(); | |
11509 | ||
0f5e4b83 ES |
11510 | if (in_compat_syscall()) { |
11511 | ret = compat_get_bitmap(cpumask_bits(new_mask), | |
11512 | (const compat_ulong_t __user *)arg, | |
11513 | len * 8 /* CHAR_BIT */); | |
11514 | } else { | |
11515 | ret = copy_from_user(new_mask, arg, len); | |
11516 | } | |
11517 | ||
11518 | if (ret) { | |
fe76421d JA |
11519 | free_cpumask_var(new_mask); |
11520 | return -EFAULT; | |
11521 | } | |
11522 | ||
11523 | ret = io_wq_cpu_affinity(tctx->io_wq, new_mask); | |
11524 | free_cpumask_var(new_mask); | |
11525 | return ret; | |
11526 | } | |
11527 | ||
c072481d | 11528 | static __cold int io_unregister_iowq_aff(struct io_ring_ctx *ctx) |
fe76421d JA |
11529 | { |
11530 | struct io_uring_task *tctx = current->io_uring; | |
11531 | ||
11532 | if (!tctx || !tctx->io_wq) | |
11533 | return -EINVAL; | |
11534 | ||
11535 | return io_wq_cpu_affinity(tctx->io_wq, NULL); | |
11536 | } | |
11537 | ||
c072481d PB |
11538 | static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx, |
11539 | void __user *arg) | |
b22fa62a | 11540 | __must_hold(&ctx->uring_lock) |
2e480058 | 11541 | { |
b22fa62a | 11542 | struct io_tctx_node *node; |
fa84693b JA |
11543 | struct io_uring_task *tctx = NULL; |
11544 | struct io_sq_data *sqd = NULL; | |
2e480058 JA |
11545 | __u32 new_count[2]; |
11546 | int i, ret; | |
11547 | ||
2e480058 JA |
11548 | if (copy_from_user(new_count, arg, sizeof(new_count))) |
11549 | return -EFAULT; | |
11550 | for (i = 0; i < ARRAY_SIZE(new_count); i++) | |
11551 | if (new_count[i] > INT_MAX) | |
11552 | return -EINVAL; | |
11553 | ||
fa84693b JA |
11554 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
11555 | sqd = ctx->sq_data; | |
11556 | if (sqd) { | |
009ad9f0 JA |
11557 | /* |
11558 | * Observe the correct sqd->lock -> ctx->uring_lock | |
11559 | * ordering. Fine to drop uring_lock here, we hold | |
11560 | * a ref to the ctx. | |
11561 | */ | |
41d3a6bd | 11562 | refcount_inc(&sqd->refs); |
009ad9f0 | 11563 | mutex_unlock(&ctx->uring_lock); |
fa84693b | 11564 | mutex_lock(&sqd->lock); |
009ad9f0 | 11565 | mutex_lock(&ctx->uring_lock); |
41d3a6bd JA |
11566 | if (sqd->thread) |
11567 | tctx = sqd->thread->io_uring; | |
fa84693b JA |
11568 | } |
11569 | } else { | |
11570 | tctx = current->io_uring; | |
11571 | } | |
11572 | ||
e139a1ec | 11573 | BUILD_BUG_ON(sizeof(new_count) != sizeof(ctx->iowq_limits)); |
fa84693b | 11574 | |
bad119b9 PB |
11575 | for (i = 0; i < ARRAY_SIZE(new_count); i++) |
11576 | if (new_count[i]) | |
11577 | ctx->iowq_limits[i] = new_count[i]; | |
e139a1ec PB |
11578 | ctx->iowq_limits_set = true; |
11579 | ||
e139a1ec PB |
11580 | if (tctx && tctx->io_wq) { |
11581 | ret = io_wq_max_workers(tctx->io_wq, new_count); | |
11582 | if (ret) | |
11583 | goto err; | |
11584 | } else { | |
11585 | memset(new_count, 0, sizeof(new_count)); | |
11586 | } | |
fa84693b | 11587 | |
41d3a6bd | 11588 | if (sqd) { |
fa84693b | 11589 | mutex_unlock(&sqd->lock); |
41d3a6bd JA |
11590 | io_put_sq_data(sqd); |
11591 | } | |
2e480058 JA |
11592 | |
11593 | if (copy_to_user(arg, new_count, sizeof(new_count))) | |
11594 | return -EFAULT; | |
11595 | ||
b22fa62a PB |
11596 | /* that's it for SQPOLL, only the SQPOLL task creates requests */ |
11597 | if (sqd) | |
11598 | return 0; | |
11599 | ||
11600 | /* now propagate the restriction to all registered users */ | |
11601 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
11602 | struct io_uring_task *tctx = node->task->io_uring; | |
11603 | ||
11604 | if (WARN_ON_ONCE(!tctx->io_wq)) | |
11605 | continue; | |
11606 | ||
11607 | for (i = 0; i < ARRAY_SIZE(new_count); i++) | |
11608 | new_count[i] = ctx->iowq_limits[i]; | |
11609 | /* ignore errors, it always returns zero anyway */ | |
11610 | (void)io_wq_max_workers(tctx->io_wq, new_count); | |
11611 | } | |
2e480058 | 11612 | return 0; |
fa84693b | 11613 | err: |
41d3a6bd | 11614 | if (sqd) { |
fa84693b | 11615 | mutex_unlock(&sqd->lock); |
41d3a6bd JA |
11616 | io_put_sq_data(sqd); |
11617 | } | |
fa84693b | 11618 | return ret; |
2e480058 JA |
11619 | } |
11620 | ||
edafccee JA |
11621 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
11622 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
11623 | __releases(ctx->uring_lock) |
11624 | __acquires(ctx->uring_lock) | |
edafccee JA |
11625 | { |
11626 | int ret; | |
11627 | ||
35fa71a0 JA |
11628 | /* |
11629 | * We're inside the ring mutex, if the ref is already dying, then | |
11630 | * someone else killed the ctx or is already going through | |
11631 | * io_uring_register(). | |
11632 | */ | |
11633 | if (percpu_ref_is_dying(&ctx->refs)) | |
11634 | return -ENXIO; | |
11635 | ||
75c4021a PB |
11636 | if (ctx->restricted) { |
11637 | if (opcode >= IORING_REGISTER_LAST) | |
11638 | return -EINVAL; | |
11639 | opcode = array_index_nospec(opcode, IORING_REGISTER_LAST); | |
11640 | if (!test_bit(opcode, ctx->restrictions.register_op)) | |
11641 | return -EACCES; | |
11642 | } | |
11643 | ||
edafccee JA |
11644 | switch (opcode) { |
11645 | case IORING_REGISTER_BUFFERS: | |
634d00df | 11646 | ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL); |
edafccee JA |
11647 | break; |
11648 | case IORING_UNREGISTER_BUFFERS: | |
11649 | ret = -EINVAL; | |
11650 | if (arg || nr_args) | |
11651 | break; | |
0a96bbe4 | 11652 | ret = io_sqe_buffers_unregister(ctx); |
edafccee | 11653 | break; |
6b06314c | 11654 | case IORING_REGISTER_FILES: |
792e3582 | 11655 | ret = io_sqe_files_register(ctx, arg, nr_args, NULL); |
6b06314c JA |
11656 | break; |
11657 | case IORING_UNREGISTER_FILES: | |
11658 | ret = -EINVAL; | |
11659 | if (arg || nr_args) | |
11660 | break; | |
11661 | ret = io_sqe_files_unregister(ctx); | |
11662 | break; | |
c3a31e60 | 11663 | case IORING_REGISTER_FILES_UPDATE: |
c3bdad02 | 11664 | ret = io_register_files_update(ctx, arg, nr_args); |
c3a31e60 | 11665 | break; |
9b402849 JA |
11666 | case IORING_REGISTER_EVENTFD: |
11667 | ret = -EINVAL; | |
11668 | if (nr_args != 1) | |
11669 | break; | |
c75312dd UA |
11670 | ret = io_eventfd_register(ctx, arg, 0); |
11671 | break; | |
11672 | case IORING_REGISTER_EVENTFD_ASYNC: | |
11673 | ret = -EINVAL; | |
11674 | if (nr_args != 1) | |
f2842ab5 | 11675 | break; |
c75312dd | 11676 | ret = io_eventfd_register(ctx, arg, 1); |
9b402849 JA |
11677 | break; |
11678 | case IORING_UNREGISTER_EVENTFD: | |
11679 | ret = -EINVAL; | |
11680 | if (arg || nr_args) | |
11681 | break; | |
11682 | ret = io_eventfd_unregister(ctx); | |
11683 | break; | |
66f4af93 JA |
11684 | case IORING_REGISTER_PROBE: |
11685 | ret = -EINVAL; | |
11686 | if (!arg || nr_args > 256) | |
11687 | break; | |
11688 | ret = io_probe(ctx, arg, nr_args); | |
11689 | break; | |
071698e1 JA |
11690 | case IORING_REGISTER_PERSONALITY: |
11691 | ret = -EINVAL; | |
11692 | if (arg || nr_args) | |
11693 | break; | |
11694 | ret = io_register_personality(ctx); | |
11695 | break; | |
11696 | case IORING_UNREGISTER_PERSONALITY: | |
11697 | ret = -EINVAL; | |
11698 | if (arg) | |
11699 | break; | |
11700 | ret = io_unregister_personality(ctx, nr_args); | |
11701 | break; | |
7e84e1c7 SG |
11702 | case IORING_REGISTER_ENABLE_RINGS: |
11703 | ret = -EINVAL; | |
11704 | if (arg || nr_args) | |
11705 | break; | |
11706 | ret = io_register_enable_rings(ctx); | |
11707 | break; | |
21b55dbc SG |
11708 | case IORING_REGISTER_RESTRICTIONS: |
11709 | ret = io_register_restrictions(ctx, arg, nr_args); | |
11710 | break; | |
992da01a PB |
11711 | case IORING_REGISTER_FILES2: |
11712 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE); | |
11713 | break; | |
11714 | case IORING_REGISTER_FILES_UPDATE2: | |
11715 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
11716 | IORING_RSRC_FILE); | |
11717 | break; | |
11718 | case IORING_REGISTER_BUFFERS2: | |
11719 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER); | |
792e3582 | 11720 | break; |
992da01a PB |
11721 | case IORING_REGISTER_BUFFERS_UPDATE: |
11722 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
11723 | IORING_RSRC_BUFFER); | |
c3bdad02 | 11724 | break; |
fe76421d JA |
11725 | case IORING_REGISTER_IOWQ_AFF: |
11726 | ret = -EINVAL; | |
11727 | if (!arg || !nr_args) | |
11728 | break; | |
11729 | ret = io_register_iowq_aff(ctx, arg, nr_args); | |
11730 | break; | |
11731 | case IORING_UNREGISTER_IOWQ_AFF: | |
11732 | ret = -EINVAL; | |
11733 | if (arg || nr_args) | |
11734 | break; | |
11735 | ret = io_unregister_iowq_aff(ctx); | |
11736 | break; | |
2e480058 JA |
11737 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
11738 | ret = -EINVAL; | |
11739 | if (!arg || nr_args != 2) | |
11740 | break; | |
11741 | ret = io_register_iowq_max_workers(ctx, arg); | |
11742 | break; | |
e7a6c00d JA |
11743 | case IORING_REGISTER_RING_FDS: |
11744 | ret = io_ringfd_register(ctx, arg, nr_args); | |
11745 | break; | |
11746 | case IORING_UNREGISTER_RING_FDS: | |
11747 | ret = io_ringfd_unregister(ctx, arg, nr_args); | |
11748 | break; | |
edafccee JA |
11749 | default: |
11750 | ret = -EINVAL; | |
11751 | break; | |
11752 | } | |
11753 | ||
edafccee JA |
11754 | return ret; |
11755 | } | |
11756 | ||
11757 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
11758 | void __user *, arg, unsigned int, nr_args) | |
11759 | { | |
11760 | struct io_ring_ctx *ctx; | |
11761 | long ret = -EBADF; | |
11762 | struct fd f; | |
11763 | ||
11764 | f = fdget(fd); | |
11765 | if (!f.file) | |
11766 | return -EBADF; | |
11767 | ||
11768 | ret = -EOPNOTSUPP; | |
11769 | if (f.file->f_op != &io_uring_fops) | |
11770 | goto out_fput; | |
11771 | ||
11772 | ctx = f.file->private_data; | |
11773 | ||
b6c23dd5 PB |
11774 | io_run_task_work(); |
11775 | ||
edafccee JA |
11776 | mutex_lock(&ctx->uring_lock); |
11777 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
11778 | mutex_unlock(&ctx->uring_lock); | |
2757be22 | 11779 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, ret); |
edafccee JA |
11780 | out_fput: |
11781 | fdput(f); | |
11782 | return ret; | |
11783 | } | |
11784 | ||
2b188cc1 JA |
11785 | static int __init io_uring_init(void) |
11786 | { | |
d7f62e82 SM |
11787 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
11788 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
11789 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
11790 | } while (0) | |
11791 | ||
11792 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
11793 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
11794 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
11795 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
11796 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
11797 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
11798 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
11799 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
11800 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
11801 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 11802 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
11803 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
11804 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
11805 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
11806 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
11807 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
11808 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
11809 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
11810 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
11811 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
11812 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
11813 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
11814 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
11815 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
11816 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
11817 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 11818 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
11819 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
11820 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
16340eab | 11821 | BUILD_BUG_SQE_ELEM(40, __u16, buf_group); |
d7f62e82 | 11822 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
7d67af2c | 11823 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
b9445598 | 11824 | BUILD_BUG_SQE_ELEM(44, __u32, file_index); |
d7f62e82 | 11825 | |
b0d658ec PB |
11826 | BUILD_BUG_ON(sizeof(struct io_uring_files_update) != |
11827 | sizeof(struct io_uring_rsrc_update)); | |
11828 | BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) > | |
11829 | sizeof(struct io_uring_rsrc_update2)); | |
90499ad0 PB |
11830 | |
11831 | /* ->buf_index is u16 */ | |
11832 | BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16)); | |
11833 | ||
b0d658ec PB |
11834 | /* should fit into one byte */ |
11835 | BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8)); | |
68fe256a PB |
11836 | BUILD_BUG_ON(SQE_COMMON_FLAGS >= (1 << 8)); |
11837 | BUILD_BUG_ON((SQE_VALID_FLAGS | SQE_COMMON_FLAGS) != SQE_VALID_FLAGS); | |
b0d658ec | 11838 | |
d3656344 | 11839 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
32c2d33e | 11840 | BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof(int)); |
16340eab | 11841 | |
91f245d5 JA |
11842 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
11843 | SLAB_ACCOUNT); | |
2b188cc1 JA |
11844 | return 0; |
11845 | }; | |
11846 | __initcall(io_uring_init); |