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> |
adc8682e | 66 | #include <net/busy_poll.h> |
2b188cc1 JA |
67 | #include <linux/anon_inodes.h> |
68 | #include <linux/sched/mm.h> | |
69 | #include <linux/uaccess.h> | |
70 | #include <linux/nospec.h> | |
edafccee JA |
71 | #include <linux/sizes.h> |
72 | #include <linux/hugetlb.h> | |
aa4c3967 | 73 | #include <linux/highmem.h> |
15b71abe JA |
74 | #include <linux/namei.h> |
75 | #include <linux/fsnotify.h> | |
4840e418 | 76 | #include <linux/fadvise.h> |
3e4827b0 | 77 | #include <linux/eventpoll.h> |
7d67af2c | 78 | #include <linux/splice.h> |
b41e9852 | 79 | #include <linux/task_work.h> |
bcf5a063 | 80 | #include <linux/pagemap.h> |
0f212204 | 81 | #include <linux/io_uring.h> |
ef98eb04 | 82 | #include <linux/tracehook.h> |
5bd2182d | 83 | #include <linux/audit.h> |
cdc1404a | 84 | #include <linux/security.h> |
2b188cc1 | 85 | |
c826bd7a DD |
86 | #define CREATE_TRACE_POINTS |
87 | #include <trace/events/io_uring.h> | |
88 | ||
2b188cc1 JA |
89 | #include <uapi/linux/io_uring.h> |
90 | ||
91 | #include "internal.h" | |
561fb04a | 92 | #include "io-wq.h" |
2b188cc1 | 93 | |
5277deaa | 94 | #define IORING_MAX_ENTRIES 32768 |
33a107f0 | 95 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
4ce8ad95 | 96 | #define IORING_SQPOLL_CAP_ENTRIES_VALUE 8 |
65e19f54 | 97 | |
187f08c1 | 98 | /* only define max */ |
042b0d85 | 99 | #define IORING_MAX_FIXED_FILES (1U << 15) |
21b55dbc SG |
100 | #define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \ |
101 | IORING_REGISTER_LAST + IORING_OP_LAST) | |
2b188cc1 | 102 | |
187f08c1 | 103 | #define IO_RSRC_TAG_TABLE_SHIFT (PAGE_SHIFT - 3) |
2d091d62 PB |
104 | #define IO_RSRC_TAG_TABLE_MAX (1U << IO_RSRC_TAG_TABLE_SHIFT) |
105 | #define IO_RSRC_TAG_TABLE_MASK (IO_RSRC_TAG_TABLE_MAX - 1) | |
106 | ||
489809e2 PB |
107 | #define IORING_MAX_REG_BUFFERS (1U << 14) |
108 | ||
68fe256a PB |
109 | #define SQE_COMMON_FLAGS (IOSQE_FIXED_FILE | IOSQE_IO_LINK | \ |
110 | IOSQE_IO_HARDLINK | IOSQE_ASYNC) | |
111 | ||
5562a8d7 PB |
112 | #define SQE_VALID_FLAGS (SQE_COMMON_FLAGS | IOSQE_BUFFER_SELECT | \ |
113 | IOSQE_IO_DRAIN | IOSQE_CQE_SKIP_SUCCESS) | |
68fe256a | 114 | |
c854357b | 115 | #define IO_REQ_CLEAN_FLAGS (REQ_F_BUFFER_SELECTED | REQ_F_NEED_CLEANUP | \ |
d886e185 PB |
116 | REQ_F_POLLED | REQ_F_INFLIGHT | REQ_F_CREDS | \ |
117 | REQ_F_ASYNC_DATA) | |
b16fed66 | 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 | ||
5a2e745d JA |
267 | struct io_buffer { |
268 | struct list_head list; | |
269 | __u64 addr; | |
d1f82808 | 270 | __u32 len; |
5a2e745d | 271 | __u16 bid; |
b1c62645 | 272 | __u16 bgid; |
5a2e745d JA |
273 | }; |
274 | ||
21b55dbc SG |
275 | struct io_restriction { |
276 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); | |
277 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); | |
278 | u8 sqe_flags_allowed; | |
279 | u8 sqe_flags_required; | |
7e84e1c7 | 280 | bool registered; |
21b55dbc SG |
281 | }; |
282 | ||
37d1e2e3 JA |
283 | enum { |
284 | IO_SQ_THREAD_SHOULD_STOP = 0, | |
285 | IO_SQ_THREAD_SHOULD_PARK, | |
286 | }; | |
287 | ||
534ca6d6 JA |
288 | struct io_sq_data { |
289 | refcount_t refs; | |
9e138a48 | 290 | atomic_t park_pending; |
09a6f4ef | 291 | struct mutex lock; |
69fb2131 JA |
292 | |
293 | /* ctx's that are using this sqd */ | |
294 | struct list_head ctx_list; | |
69fb2131 | 295 | |
534ca6d6 JA |
296 | struct task_struct *thread; |
297 | struct wait_queue_head wait; | |
08369246 XW |
298 | |
299 | unsigned sq_thread_idle; | |
37d1e2e3 JA |
300 | int sq_cpu; |
301 | pid_t task_pid; | |
5c2469e0 | 302 | pid_t task_tgid; |
37d1e2e3 JA |
303 | |
304 | unsigned long state; | |
37d1e2e3 | 305 | struct completion exited; |
534ca6d6 JA |
306 | }; |
307 | ||
6dd0be1e | 308 | #define IO_COMPL_BATCH 32 |
6ff119a6 | 309 | #define IO_REQ_CACHE_SIZE 32 |
bf019da7 | 310 | #define IO_REQ_ALLOC_BATCH 8 |
258b29a9 | 311 | |
a1ab7b35 PB |
312 | struct io_submit_link { |
313 | struct io_kiocb *head; | |
314 | struct io_kiocb *last; | |
315 | }; | |
316 | ||
258b29a9 | 317 | struct io_submit_state { |
5a158c6b PB |
318 | /* inline/task_work completion list, under ->uring_lock */ |
319 | struct io_wq_work_node free_list; | |
320 | /* batch completion logic */ | |
321 | struct io_wq_work_list compl_reqs; | |
a1ab7b35 | 322 | struct io_submit_link link; |
258b29a9 | 323 | |
258b29a9 | 324 | bool plug_started; |
4b628aeb | 325 | bool need_plug; |
3d4aeb9f | 326 | bool flush_cqes; |
5ca7a8b3 | 327 | unsigned short submit_nr; |
5a158c6b | 328 | struct blk_plug plug; |
258b29a9 PB |
329 | }; |
330 | ||
77bc59b4 UA |
331 | struct io_ev_fd { |
332 | struct eventfd_ctx *cq_ev_fd; | |
c75312dd | 333 | unsigned int eventfd_async: 1; |
77bc59b4 UA |
334 | struct rcu_head rcu; |
335 | }; | |
336 | ||
2b188cc1 | 337 | struct io_ring_ctx { |
b52ecf8c | 338 | /* const or read-mostly hot data */ |
2b188cc1 JA |
339 | struct { |
340 | struct percpu_ref refs; | |
2b188cc1 | 341 | |
b52ecf8c | 342 | struct io_rings *rings; |
2b188cc1 | 343 | unsigned int flags; |
e1d85334 | 344 | unsigned int compat: 1; |
e1d85334 | 345 | unsigned int drain_next: 1; |
21b55dbc | 346 | unsigned int restricted: 1; |
f18ee4cf | 347 | unsigned int off_timeout_used: 1; |
10c66904 | 348 | unsigned int drain_active: 1; |
5562a8d7 | 349 | unsigned int drain_disabled: 1; |
b52ecf8c | 350 | } ____cacheline_aligned_in_smp; |
2b188cc1 | 351 | |
7f1129d2 | 352 | /* submission data */ |
b52ecf8c | 353 | struct { |
0499e582 PB |
354 | struct mutex uring_lock; |
355 | ||
75b28aff HV |
356 | /* |
357 | * Ring buffer of indices into array of io_uring_sqe, which is | |
358 | * mmapped by the application using the IORING_OFF_SQES offset. | |
359 | * | |
360 | * This indirection could e.g. be used to assign fixed | |
361 | * io_uring_sqe entries to operations and only submit them to | |
362 | * the queue when needed. | |
363 | * | |
364 | * The kernel modifies neither the indices array nor the entries | |
365 | * array. | |
366 | */ | |
367 | u32 *sq_array; | |
c7af47cf | 368 | struct io_uring_sqe *sq_sqes; |
2b188cc1 JA |
369 | unsigned cached_sq_head; |
370 | unsigned sq_entries; | |
de0617e4 | 371 | struct list_head defer_list; |
7f1129d2 PB |
372 | |
373 | /* | |
374 | * Fixed resources fast path, should be accessed only under | |
375 | * uring_lock, and updated through io_uring_register(2) | |
376 | */ | |
377 | struct io_rsrc_node *rsrc_node; | |
ab409402 | 378 | int rsrc_cached_refs; |
7f1129d2 PB |
379 | struct io_file_table file_table; |
380 | unsigned nr_user_files; | |
381 | unsigned nr_user_bufs; | |
382 | struct io_mapped_ubuf **user_bufs; | |
383 | ||
384 | struct io_submit_state submit_state; | |
5262f567 | 385 | struct list_head timeout_list; |
ef9dd637 | 386 | struct list_head ltimeout_list; |
1d7bb1d5 | 387 | struct list_head cq_overflow_list; |
7f1129d2 | 388 | struct xarray io_buffers; |
cc3cec83 | 389 | struct list_head io_buffers_cache; |
4d9237e3 | 390 | struct list_head apoll_cache; |
7f1129d2 PB |
391 | struct xarray personalities; |
392 | u32 pers_next; | |
393 | unsigned sq_thread_idle; | |
2b188cc1 JA |
394 | } ____cacheline_aligned_in_smp; |
395 | ||
d0acdee2 | 396 | /* IRQ completion list, under ->completion_lock */ |
c2b6c6bc | 397 | struct io_wq_work_list locked_free_list; |
d0acdee2 | 398 | unsigned int locked_free_nr; |
3c1a2ead | 399 | |
7c30f36a | 400 | const struct cred *sq_creds; /* cred used for __io_sq_thread() */ |
534ca6d6 JA |
401 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
402 | ||
90554200 | 403 | struct wait_queue_head sqo_sq_wait; |
69fb2131 | 404 | struct list_head sqd_list; |
75b28aff | 405 | |
5ed7a37d | 406 | unsigned long check_cq_overflow; |
adc8682e OL |
407 | #ifdef CONFIG_NET_RX_BUSY_POLL |
408 | /* used to track busy poll napi_id */ | |
409 | struct list_head napi_list; | |
410 | spinlock_t napi_lock; /* napi_list lock */ | |
411 | #endif | |
5ed7a37d | 412 | |
206aefde JA |
413 | struct { |
414 | unsigned cached_cq_tail; | |
415 | unsigned cq_entries; | |
77bc59b4 | 416 | struct io_ev_fd __rcu *io_ev_fd; |
0499e582 PB |
417 | struct wait_queue_head cq_wait; |
418 | unsigned cq_extra; | |
419 | atomic_t cq_timeouts; | |
0499e582 | 420 | unsigned cq_last_tm_flush; |
206aefde | 421 | } ____cacheline_aligned_in_smp; |
2b188cc1 | 422 | |
2b188cc1 JA |
423 | struct { |
424 | spinlock_t completion_lock; | |
e94f141b | 425 | |
89850fce JA |
426 | spinlock_t timeout_lock; |
427 | ||
def596e9 | 428 | /* |
540e32a0 | 429 | * ->iopoll_list is protected by the ctx->uring_lock for |
def596e9 JA |
430 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
431 | * For SQPOLL, only the single threaded io_sq_thread() will | |
432 | * manipulate the list, hence no extra locking is needed there. | |
433 | */ | |
5eef4e87 | 434 | struct io_wq_work_list iopoll_list; |
78076bb6 JA |
435 | struct hlist_head *cancel_hash; |
436 | unsigned cancel_hash_bits; | |
915b3dde | 437 | bool poll_multi_queue; |
cc3cec83 JA |
438 | |
439 | struct list_head io_buffers_comp; | |
2b188cc1 | 440 | } ____cacheline_aligned_in_smp; |
85faa7b8 | 441 | |
21b55dbc | 442 | struct io_restriction restrictions; |
3c1a2ead | 443 | |
b13a8918 PB |
444 | /* slow path rsrc auxilary data, used by update/register */ |
445 | struct { | |
446 | struct io_rsrc_node *rsrc_backup_node; | |
447 | struct io_mapped_ubuf *dummy_ubuf; | |
448 | struct io_rsrc_data *file_data; | |
449 | struct io_rsrc_data *buf_data; | |
450 | ||
451 | struct delayed_work rsrc_put_work; | |
452 | struct llist_head rsrc_put_llist; | |
453 | struct list_head rsrc_ref_list; | |
454 | spinlock_t rsrc_ref_lock; | |
cc3cec83 JA |
455 | |
456 | struct list_head io_buffers_pages; | |
b13a8918 PB |
457 | }; |
458 | ||
3c1a2ead | 459 | /* Keep this last, we don't need it for the fast path */ |
b986af7e PB |
460 | struct { |
461 | #if defined(CONFIG_UNIX) | |
462 | struct socket *ring_sock; | |
463 | #endif | |
464 | /* hashed buffered write serialization */ | |
465 | struct io_wq_hash *hash_map; | |
466 | ||
467 | /* Only used for accounting purposes */ | |
468 | struct user_struct *user; | |
469 | struct mm_struct *mm_account; | |
470 | ||
471 | /* ctx exit and cancelation */ | |
9011bf9a PB |
472 | struct llist_head fallback_llist; |
473 | struct delayed_work fallback_work; | |
b986af7e PB |
474 | struct work_struct exit_work; |
475 | struct list_head tctx_list; | |
476 | struct completion ref_comp; | |
e139a1ec PB |
477 | u32 iowq_limits[2]; |
478 | bool iowq_limits_set; | |
b986af7e | 479 | }; |
2b188cc1 JA |
480 | }; |
481 | ||
e7a6c00d JA |
482 | /* |
483 | * Arbitrary limit, can be raised if need be | |
484 | */ | |
485 | #define IO_RINGFD_REG_MAX 16 | |
486 | ||
53e043b2 SM |
487 | struct io_uring_task { |
488 | /* submission side */ | |
09899b19 | 489 | int cached_refs; |
53e043b2 SM |
490 | struct xarray xa; |
491 | struct wait_queue_head wait; | |
ee53fb2b SM |
492 | const struct io_ring_ctx *last; |
493 | struct io_wq *io_wq; | |
53e043b2 | 494 | struct percpu_counter inflight; |
b303fe2e | 495 | atomic_t inflight_tracked; |
53e043b2 | 496 | atomic_t in_idle; |
53e043b2 SM |
497 | |
498 | spinlock_t task_lock; | |
499 | struct io_wq_work_list task_list; | |
4813c377 | 500 | struct io_wq_work_list prior_task_list; |
53e043b2 | 501 | struct callback_head task_work; |
e7a6c00d | 502 | struct file **registered_rings; |
6294f368 | 503 | bool task_running; |
53e043b2 SM |
504 | }; |
505 | ||
09bb8394 JA |
506 | /* |
507 | * First field must be the file pointer in all the | |
508 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> | |
509 | */ | |
221c5eb2 JA |
510 | struct io_poll_iocb { |
511 | struct file *file; | |
018043be | 512 | struct wait_queue_head *head; |
221c5eb2 | 513 | __poll_t events; |
392edb45 | 514 | struct wait_queue_entry wait; |
221c5eb2 JA |
515 | }; |
516 | ||
9d805892 | 517 | struct io_poll_update { |
018043be | 518 | struct file *file; |
9d805892 PB |
519 | u64 old_user_data; |
520 | u64 new_user_data; | |
521 | __poll_t events; | |
b69de288 JA |
522 | bool update_events; |
523 | bool update_user_data; | |
018043be PB |
524 | }; |
525 | ||
b5dba59e JA |
526 | struct io_close { |
527 | struct file *file; | |
b5dba59e | 528 | int fd; |
7df778be | 529 | u32 file_slot; |
b5dba59e JA |
530 | }; |
531 | ||
ad8a48ac JA |
532 | struct io_timeout_data { |
533 | struct io_kiocb *req; | |
534 | struct hrtimer timer; | |
535 | struct timespec64 ts; | |
536 | enum hrtimer_mode mode; | |
50c1df2b | 537 | u32 flags; |
ad8a48ac JA |
538 | }; |
539 | ||
8ed8d3c3 JA |
540 | struct io_accept { |
541 | struct file *file; | |
542 | struct sockaddr __user *addr; | |
543 | int __user *addr_len; | |
544 | int flags; | |
aaa4db12 | 545 | u32 file_slot; |
09952e3e | 546 | unsigned long nofile; |
8ed8d3c3 JA |
547 | }; |
548 | ||
549 | struct io_sync { | |
550 | struct file *file; | |
551 | loff_t len; | |
552 | loff_t off; | |
553 | int flags; | |
d63d1b5e | 554 | int mode; |
8ed8d3c3 JA |
555 | }; |
556 | ||
fbf23849 JA |
557 | struct io_cancel { |
558 | struct file *file; | |
559 | u64 addr; | |
560 | }; | |
561 | ||
b29472ee JA |
562 | struct io_timeout { |
563 | struct file *file; | |
bfe68a22 PB |
564 | u32 off; |
565 | u32 target_seq; | |
135fcde8 | 566 | struct list_head list; |
90cd7e42 PB |
567 | /* head of the link, used by linked timeouts only */ |
568 | struct io_kiocb *head; | |
89b263f6 JA |
569 | /* for linked completions */ |
570 | struct io_kiocb *prev; | |
b29472ee JA |
571 | }; |
572 | ||
0bdf7a2d PB |
573 | struct io_timeout_rem { |
574 | struct file *file; | |
575 | u64 addr; | |
9c8e11b3 PB |
576 | |
577 | /* timeout update */ | |
578 | struct timespec64 ts; | |
579 | u32 flags; | |
f1042b6c | 580 | bool ltimeout; |
0bdf7a2d PB |
581 | }; |
582 | ||
9adbd45d JA |
583 | struct io_rw { |
584 | /* NOTE: kiocb has the file as the first member, so don't do it here */ | |
585 | struct kiocb kiocb; | |
586 | u64 addr; | |
587 | u64 len; | |
588 | }; | |
589 | ||
3fbb51c1 JA |
590 | struct io_connect { |
591 | struct file *file; | |
592 | struct sockaddr __user *addr; | |
593 | int addr_len; | |
594 | }; | |
595 | ||
e47293fd JA |
596 | struct io_sr_msg { |
597 | struct file *file; | |
fddaface | 598 | union { |
4af3417a PB |
599 | struct compat_msghdr __user *umsg_compat; |
600 | struct user_msghdr __user *umsg; | |
601 | void __user *buf; | |
fddaface | 602 | }; |
e47293fd | 603 | int msg_flags; |
bcda7baa | 604 | int bgid; |
fddaface | 605 | size_t len; |
e47293fd JA |
606 | }; |
607 | ||
15b71abe JA |
608 | struct io_open { |
609 | struct file *file; | |
610 | int dfd; | |
b9445598 | 611 | u32 file_slot; |
15b71abe | 612 | struct filename *filename; |
c12cedf2 | 613 | struct open_how how; |
4022e7af | 614 | unsigned long nofile; |
15b71abe JA |
615 | }; |
616 | ||
269bbe5f | 617 | struct io_rsrc_update { |
05f3fb3c JA |
618 | struct file *file; |
619 | u64 arg; | |
620 | u32 nr_args; | |
621 | u32 offset; | |
622 | }; | |
623 | ||
4840e418 JA |
624 | struct io_fadvise { |
625 | struct file *file; | |
626 | u64 offset; | |
627 | u32 len; | |
628 | u32 advice; | |
629 | }; | |
630 | ||
c1ca757b JA |
631 | struct io_madvise { |
632 | struct file *file; | |
633 | u64 addr; | |
634 | u32 len; | |
635 | u32 advice; | |
636 | }; | |
637 | ||
3e4827b0 JA |
638 | struct io_epoll { |
639 | struct file *file; | |
640 | int epfd; | |
641 | int op; | |
642 | int fd; | |
643 | struct epoll_event event; | |
e47293fd JA |
644 | }; |
645 | ||
7d67af2c PB |
646 | struct io_splice { |
647 | struct file *file_out; | |
648 | struct file *file_in; | |
649 | loff_t off_out; | |
650 | loff_t off_in; | |
651 | u64 len; | |
652 | unsigned int flags; | |
653 | }; | |
654 | ||
ddf0322d JA |
655 | struct io_provide_buf { |
656 | struct file *file; | |
657 | __u64 addr; | |
38134ada | 658 | __u32 len; |
ddf0322d JA |
659 | __u32 bgid; |
660 | __u16 nbufs; | |
661 | __u16 bid; | |
662 | }; | |
663 | ||
1d9e1288 BM |
664 | struct io_statx { |
665 | struct file *file; | |
666 | int dfd; | |
667 | unsigned int mask; | |
668 | unsigned int flags; | |
e62753e4 | 669 | const char __user *filename; |
1d9e1288 BM |
670 | struct statx __user *buffer; |
671 | }; | |
672 | ||
36f4fa68 JA |
673 | struct io_shutdown { |
674 | struct file *file; | |
675 | int how; | |
676 | }; | |
677 | ||
80a261fd JA |
678 | struct io_rename { |
679 | struct file *file; | |
680 | int old_dfd; | |
681 | int new_dfd; | |
682 | struct filename *oldpath; | |
683 | struct filename *newpath; | |
684 | int flags; | |
685 | }; | |
686 | ||
14a1143b JA |
687 | struct io_unlink { |
688 | struct file *file; | |
689 | int dfd; | |
690 | int flags; | |
691 | struct filename *filename; | |
692 | }; | |
693 | ||
e34a02dc DK |
694 | struct io_mkdir { |
695 | struct file *file; | |
696 | int dfd; | |
697 | umode_t mode; | |
698 | struct filename *filename; | |
699 | }; | |
700 | ||
7a8721f8 DK |
701 | struct io_symlink { |
702 | struct file *file; | |
703 | int new_dfd; | |
704 | struct filename *oldpath; | |
705 | struct filename *newpath; | |
706 | }; | |
707 | ||
cf30da90 DK |
708 | struct io_hardlink { |
709 | struct file *file; | |
710 | int old_dfd; | |
711 | int new_dfd; | |
712 | struct filename *oldpath; | |
713 | struct filename *newpath; | |
714 | int flags; | |
715 | }; | |
716 | ||
4f57f06c JA |
717 | struct io_msg { |
718 | struct file *file; | |
719 | u64 user_data; | |
720 | u32 len; | |
721 | }; | |
722 | ||
f499a021 JA |
723 | struct io_async_connect { |
724 | struct sockaddr_storage address; | |
725 | }; | |
726 | ||
03b1230c JA |
727 | struct io_async_msghdr { |
728 | struct iovec fast_iov[UIO_FASTIOV]; | |
257e84a5 PB |
729 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
730 | struct iovec *free_iov; | |
03b1230c JA |
731 | struct sockaddr __user *uaddr; |
732 | struct msghdr msg; | |
b537916c | 733 | struct sockaddr_storage addr; |
03b1230c JA |
734 | }; |
735 | ||
538941e2 | 736 | struct io_rw_state { |
ff6165b2 | 737 | struct iov_iter iter; |
cd658695 | 738 | struct iov_iter_state iter_state; |
c88598a9 | 739 | struct iovec fast_iov[UIO_FASTIOV]; |
538941e2 PB |
740 | }; |
741 | ||
742 | struct io_async_rw { | |
743 | struct io_rw_state s; | |
744 | const struct iovec *free_iovec; | |
227c0c96 | 745 | size_t bytes_done; |
bcf5a063 | 746 | struct wait_page_queue wpq; |
f67676d1 JA |
747 | }; |
748 | ||
6b47ee6e PB |
749 | enum { |
750 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, | |
751 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, | |
752 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, | |
753 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, | |
754 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, | |
bcda7baa | 755 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
04c76b41 | 756 | REQ_F_CQE_SKIP_BIT = IOSQE_CQE_SKIP_SUCCESS_BIT, |
6b47ee6e | 757 | |
dddca226 | 758 | /* first byte is taken by user flags, shift it to not overlap */ |
93d2bcd2 | 759 | REQ_F_FAIL_BIT = 8, |
6b47ee6e PB |
760 | REQ_F_INFLIGHT_BIT, |
761 | REQ_F_CUR_POS_BIT, | |
762 | REQ_F_NOWAIT_BIT, | |
6b47ee6e | 763 | REQ_F_LINK_TIMEOUT_BIT, |
99bc4c38 | 764 | REQ_F_NEED_CLEANUP_BIT, |
d7718a9d | 765 | REQ_F_POLLED_BIT, |
bcda7baa | 766 | REQ_F_BUFFER_SELECTED_BIT, |
e342c807 | 767 | REQ_F_COMPLETE_INLINE_BIT, |
230d50d4 | 768 | REQ_F_REISSUE_BIT, |
b8e64b53 | 769 | REQ_F_CREDS_BIT, |
20e60a38 | 770 | REQ_F_REFCOUNT_BIT, |
4d13d1a4 | 771 | REQ_F_ARM_LTIMEOUT_BIT, |
d886e185 | 772 | REQ_F_ASYNC_DATA_BIT, |
04c76b41 | 773 | REQ_F_SKIP_LINK_CQES_BIT, |
91eac1c6 JA |
774 | REQ_F_SINGLE_POLL_BIT, |
775 | REQ_F_DOUBLE_POLL_BIT, | |
7b29f92d | 776 | /* keep async read/write and isreg together and in order */ |
35645ac3 | 777 | REQ_F_SUPPORT_NOWAIT_BIT, |
7b29f92d | 778 | REQ_F_ISREG_BIT, |
84557871 JA |
779 | |
780 | /* not a real bit, just to check we're not overflowing the space */ | |
781 | __REQ_F_LAST_BIT, | |
6b47ee6e PB |
782 | }; |
783 | ||
784 | enum { | |
785 | /* ctx owns file */ | |
786 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), | |
787 | /* drain existing IO first */ | |
788 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), | |
789 | /* linked sqes */ | |
790 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), | |
791 | /* doesn't sever on completion < 0 */ | |
792 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), | |
793 | /* IOSQE_ASYNC */ | |
794 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), | |
bcda7baa JA |
795 | /* IOSQE_BUFFER_SELECT */ |
796 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), | |
04c76b41 PB |
797 | /* IOSQE_CQE_SKIP_SUCCESS */ |
798 | REQ_F_CQE_SKIP = BIT(REQ_F_CQE_SKIP_BIT), | |
6b47ee6e | 799 | |
6b47ee6e | 800 | /* fail rest of links */ |
93d2bcd2 | 801 | REQ_F_FAIL = BIT(REQ_F_FAIL_BIT), |
b05a1bcd | 802 | /* on inflight list, should be cancelled and waited on exit reliably */ |
6b47ee6e PB |
803 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
804 | /* read/write uses file position */ | |
805 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), | |
806 | /* must not punt to workers */ | |
807 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), | |
900fad45 | 808 | /* has or had linked timeout */ |
6b47ee6e | 809 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
99bc4c38 PB |
810 | /* needs cleanup */ |
811 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), | |
d7718a9d JA |
812 | /* already went through poll handler */ |
813 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), | |
bcda7baa JA |
814 | /* buffer already selected */ |
815 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), | |
e342c807 PB |
816 | /* completion is deferred through io_comp_state */ |
817 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), | |
230d50d4 JA |
818 | /* caller should reissue async */ |
819 | REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT), | |
35645ac3 PB |
820 | /* supports async reads/writes */ |
821 | REQ_F_SUPPORT_NOWAIT = BIT(REQ_F_SUPPORT_NOWAIT_BIT), | |
7b29f92d JA |
822 | /* regular file */ |
823 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), | |
b8e64b53 PB |
824 | /* has creds assigned */ |
825 | REQ_F_CREDS = BIT(REQ_F_CREDS_BIT), | |
20e60a38 PB |
826 | /* skip refcounting if not set */ |
827 | REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT), | |
4d13d1a4 PB |
828 | /* there is a linked timeout that has to be armed */ |
829 | REQ_F_ARM_LTIMEOUT = BIT(REQ_F_ARM_LTIMEOUT_BIT), | |
d886e185 PB |
830 | /* ->async_data allocated */ |
831 | REQ_F_ASYNC_DATA = BIT(REQ_F_ASYNC_DATA_BIT), | |
04c76b41 PB |
832 | /* don't post CQEs while failing linked requests */ |
833 | REQ_F_SKIP_LINK_CQES = BIT(REQ_F_SKIP_LINK_CQES_BIT), | |
91eac1c6 JA |
834 | /* single poll may be active */ |
835 | REQ_F_SINGLE_POLL = BIT(REQ_F_SINGLE_POLL_BIT), | |
836 | /* double poll may active */ | |
837 | REQ_F_DOUBLE_POLL = BIT(REQ_F_DOUBLE_POLL_BIT), | |
d7718a9d JA |
838 | }; |
839 | ||
840 | struct async_poll { | |
841 | struct io_poll_iocb poll; | |
807abcb0 | 842 | struct io_poll_iocb *double_poll; |
6b47ee6e PB |
843 | }; |
844 | ||
f237c30a | 845 | typedef void (*io_req_tw_func_t)(struct io_kiocb *req, bool *locked); |
5b0a6acc | 846 | |
7cbf1722 | 847 | struct io_task_work { |
5b0a6acc PB |
848 | union { |
849 | struct io_wq_work_node node; | |
850 | struct llist_node fallback_node; | |
851 | }; | |
852 | io_req_tw_func_t func; | |
7cbf1722 JA |
853 | }; |
854 | ||
992da01a PB |
855 | enum { |
856 | IORING_RSRC_FILE = 0, | |
857 | IORING_RSRC_BUFFER = 1, | |
858 | }; | |
859 | ||
09bb8394 JA |
860 | /* |
861 | * NOTE! Each of the iocb union members has the file pointer | |
862 | * as the first entry in their struct definition. So you can | |
863 | * access the file pointer through any of the sub-structs, | |
63c36549 | 864 | * or directly as just 'file' in this struct. |
09bb8394 | 865 | */ |
2b188cc1 | 866 | struct io_kiocb { |
221c5eb2 | 867 | union { |
09bb8394 | 868 | struct file *file; |
9adbd45d | 869 | struct io_rw rw; |
221c5eb2 | 870 | struct io_poll_iocb poll; |
9d805892 | 871 | struct io_poll_update poll_update; |
8ed8d3c3 JA |
872 | struct io_accept accept; |
873 | struct io_sync sync; | |
fbf23849 | 874 | struct io_cancel cancel; |
b29472ee | 875 | struct io_timeout timeout; |
0bdf7a2d | 876 | struct io_timeout_rem timeout_rem; |
3fbb51c1 | 877 | struct io_connect connect; |
e47293fd | 878 | struct io_sr_msg sr_msg; |
15b71abe | 879 | struct io_open open; |
b5dba59e | 880 | struct io_close close; |
269bbe5f | 881 | struct io_rsrc_update rsrc_update; |
4840e418 | 882 | struct io_fadvise fadvise; |
c1ca757b | 883 | struct io_madvise madvise; |
3e4827b0 | 884 | struct io_epoll epoll; |
7d67af2c | 885 | struct io_splice splice; |
ddf0322d | 886 | struct io_provide_buf pbuf; |
1d9e1288 | 887 | struct io_statx statx; |
36f4fa68 | 888 | struct io_shutdown shutdown; |
80a261fd | 889 | struct io_rename rename; |
14a1143b | 890 | struct io_unlink unlink; |
e34a02dc | 891 | struct io_mkdir mkdir; |
7a8721f8 | 892 | struct io_symlink symlink; |
cf30da90 | 893 | struct io_hardlink hardlink; |
4f57f06c | 894 | struct io_msg msg; |
221c5eb2 | 895 | }; |
2b188cc1 | 896 | |
d625c6ee | 897 | u8 opcode; |
65a6543d XW |
898 | /* polled IO has completed */ |
899 | u8 iopoll_completed; | |
4f4eeba8 | 900 | u16 buf_index; |
d17e56eb PB |
901 | unsigned int flags; |
902 | ||
903 | u64 user_data; | |
9cf7c104 | 904 | u32 result; |
d17e56eb | 905 | u32 cflags; |
4f4eeba8 | 906 | |
010e8e6b | 907 | struct io_ring_ctx *ctx; |
010e8e6b | 908 | struct task_struct *task; |
d7718a9d | 909 | |
269bbe5f | 910 | struct percpu_ref *fixed_rsrc_refs; |
d886e185 PB |
911 | /* store used ubuf, so we can prevent reloading */ |
912 | struct io_mapped_ubuf *imu; | |
fcb323cc | 913 | |
7e3709d5 | 914 | /* used by request caches, completion batching and iopoll */ |
ef05d9eb | 915 | struct io_wq_work_node comp_list; |
d17e56eb | 916 | atomic_t refs; |
521d61fc | 917 | atomic_t poll_refs; |
7e3709d5 | 918 | struct io_kiocb *link; |
5b0a6acc | 919 | struct io_task_work io_task_work; |
010e8e6b PB |
920 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
921 | struct hlist_node hash_node; | |
7e3709d5 | 922 | /* internal polling, see IORING_FEAT_FAST_POLL */ |
010e8e6b | 923 | struct async_poll *apoll; |
d886e185 PB |
924 | /* opcode allocated if it needs to store data for async defer */ |
925 | void *async_data; | |
7e3709d5 | 926 | /* custom credentials, valid IFF REQ_F_CREDS is set */ |
7e3709d5 | 927 | /* stores selected buf, valid IFF REQ_F_BUFFER_SELECTED is set */ |
30d51dd4 | 928 | struct io_buffer *kbuf; |
521d61fc JA |
929 | const struct cred *creds; |
930 | struct io_wq_work work; | |
2b188cc1 | 931 | }; |
05589553 | 932 | |
13bf43f5 PB |
933 | struct io_tctx_node { |
934 | struct list_head ctx_node; | |
935 | struct task_struct *task; | |
13bf43f5 PB |
936 | struct io_ring_ctx *ctx; |
937 | }; | |
938 | ||
27dc8338 PB |
939 | struct io_defer_entry { |
940 | struct list_head list; | |
941 | struct io_kiocb *req; | |
9cf7c104 | 942 | u32 seq; |
2b188cc1 JA |
943 | }; |
944 | ||
d3656344 | 945 | struct io_op_def { |
d3656344 JA |
946 | /* needs req->file assigned */ |
947 | unsigned needs_file : 1; | |
6d63416d PB |
948 | /* should block plug */ |
949 | unsigned plug : 1; | |
d3656344 JA |
950 | /* hash wq insertion if file is a regular file */ |
951 | unsigned hash_reg_file : 1; | |
952 | /* unbound wq insertion if file is a non-regular file */ | |
953 | unsigned unbound_nonreg_file : 1; | |
8a72758c JA |
954 | /* set if opcode supports polled "wait" */ |
955 | unsigned pollin : 1; | |
956 | unsigned pollout : 1; | |
bcda7baa JA |
957 | /* op supports buffer selection */ |
958 | unsigned buffer_select : 1; | |
26f0505a PB |
959 | /* do prep async if is going to be punted */ |
960 | unsigned needs_async_setup : 1; | |
6d63416d PB |
961 | /* opcode is not supported by this kernel */ |
962 | unsigned not_supported : 1; | |
5bd2182d PM |
963 | /* skip auditing */ |
964 | unsigned audit_skip : 1; | |
e8c2bc1f JA |
965 | /* size of async data needed, if any */ |
966 | unsigned short async_size; | |
d3656344 JA |
967 | }; |
968 | ||
0918682b | 969 | static const struct io_op_def io_op_defs[] = { |
0463b6c5 PB |
970 | [IORING_OP_NOP] = {}, |
971 | [IORING_OP_READV] = { | |
d3656344 JA |
972 | .needs_file = 1, |
973 | .unbound_nonreg_file = 1, | |
8a72758c | 974 | .pollin = 1, |
4d954c25 | 975 | .buffer_select = 1, |
26f0505a | 976 | .needs_async_setup = 1, |
27926b68 | 977 | .plug = 1, |
5bd2182d | 978 | .audit_skip = 1, |
e8c2bc1f | 979 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 980 | }, |
0463b6c5 | 981 | [IORING_OP_WRITEV] = { |
d3656344 JA |
982 | .needs_file = 1, |
983 | .hash_reg_file = 1, | |
984 | .unbound_nonreg_file = 1, | |
8a72758c | 985 | .pollout = 1, |
26f0505a | 986 | .needs_async_setup = 1, |
27926b68 | 987 | .plug = 1, |
5bd2182d | 988 | .audit_skip = 1, |
e8c2bc1f | 989 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 990 | }, |
0463b6c5 | 991 | [IORING_OP_FSYNC] = { |
d3656344 | 992 | .needs_file = 1, |
5bd2182d | 993 | .audit_skip = 1, |
d3656344 | 994 | }, |
0463b6c5 | 995 | [IORING_OP_READ_FIXED] = { |
d3656344 JA |
996 | .needs_file = 1, |
997 | .unbound_nonreg_file = 1, | |
8a72758c | 998 | .pollin = 1, |
27926b68 | 999 | .plug = 1, |
5bd2182d | 1000 | .audit_skip = 1, |
e8c2bc1f | 1001 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 1002 | }, |
0463b6c5 | 1003 | [IORING_OP_WRITE_FIXED] = { |
d3656344 JA |
1004 | .needs_file = 1, |
1005 | .hash_reg_file = 1, | |
1006 | .unbound_nonreg_file = 1, | |
8a72758c | 1007 | .pollout = 1, |
27926b68 | 1008 | .plug = 1, |
5bd2182d | 1009 | .audit_skip = 1, |
e8c2bc1f | 1010 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 1011 | }, |
0463b6c5 | 1012 | [IORING_OP_POLL_ADD] = { |
d3656344 JA |
1013 | .needs_file = 1, |
1014 | .unbound_nonreg_file = 1, | |
5bd2182d PM |
1015 | .audit_skip = 1, |
1016 | }, | |
1017 | [IORING_OP_POLL_REMOVE] = { | |
1018 | .audit_skip = 1, | |
d3656344 | 1019 | }, |
0463b6c5 | 1020 | [IORING_OP_SYNC_FILE_RANGE] = { |
d3656344 | 1021 | .needs_file = 1, |
5bd2182d | 1022 | .audit_skip = 1, |
d3656344 | 1023 | }, |
0463b6c5 | 1024 | [IORING_OP_SENDMSG] = { |
d3656344 JA |
1025 | .needs_file = 1, |
1026 | .unbound_nonreg_file = 1, | |
8a72758c | 1027 | .pollout = 1, |
26f0505a | 1028 | .needs_async_setup = 1, |
e8c2bc1f | 1029 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 1030 | }, |
0463b6c5 | 1031 | [IORING_OP_RECVMSG] = { |
d3656344 JA |
1032 | .needs_file = 1, |
1033 | .unbound_nonreg_file = 1, | |
8a72758c | 1034 | .pollin = 1, |
52de1fe1 | 1035 | .buffer_select = 1, |
26f0505a | 1036 | .needs_async_setup = 1, |
e8c2bc1f | 1037 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 1038 | }, |
0463b6c5 | 1039 | [IORING_OP_TIMEOUT] = { |
5bd2182d | 1040 | .audit_skip = 1, |
e8c2bc1f | 1041 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 1042 | }, |
9c8e11b3 PB |
1043 | [IORING_OP_TIMEOUT_REMOVE] = { |
1044 | /* used by timeout updates' prep() */ | |
5bd2182d | 1045 | .audit_skip = 1, |
9c8e11b3 | 1046 | }, |
0463b6c5 | 1047 | [IORING_OP_ACCEPT] = { |
d3656344 JA |
1048 | .needs_file = 1, |
1049 | .unbound_nonreg_file = 1, | |
8a72758c | 1050 | .pollin = 1, |
d3656344 | 1051 | }, |
5bd2182d PM |
1052 | [IORING_OP_ASYNC_CANCEL] = { |
1053 | .audit_skip = 1, | |
1054 | }, | |
0463b6c5 | 1055 | [IORING_OP_LINK_TIMEOUT] = { |
5bd2182d | 1056 | .audit_skip = 1, |
e8c2bc1f | 1057 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 1058 | }, |
0463b6c5 | 1059 | [IORING_OP_CONNECT] = { |
d3656344 JA |
1060 | .needs_file = 1, |
1061 | .unbound_nonreg_file = 1, | |
8a72758c | 1062 | .pollout = 1, |
26f0505a | 1063 | .needs_async_setup = 1, |
e8c2bc1f | 1064 | .async_size = sizeof(struct io_async_connect), |
d3656344 | 1065 | }, |
0463b6c5 | 1066 | [IORING_OP_FALLOCATE] = { |
d3656344 | 1067 | .needs_file = 1, |
d3656344 | 1068 | }, |
44526bed JA |
1069 | [IORING_OP_OPENAT] = {}, |
1070 | [IORING_OP_CLOSE] = {}, | |
5bd2182d PM |
1071 | [IORING_OP_FILES_UPDATE] = { |
1072 | .audit_skip = 1, | |
1073 | }, | |
1074 | [IORING_OP_STATX] = { | |
1075 | .audit_skip = 1, | |
1076 | }, | |
0463b6c5 | 1077 | [IORING_OP_READ] = { |
3a6820f2 JA |
1078 | .needs_file = 1, |
1079 | .unbound_nonreg_file = 1, | |
8a72758c | 1080 | .pollin = 1, |
bcda7baa | 1081 | .buffer_select = 1, |
27926b68 | 1082 | .plug = 1, |
5bd2182d | 1083 | .audit_skip = 1, |
e8c2bc1f | 1084 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 1085 | }, |
0463b6c5 | 1086 | [IORING_OP_WRITE] = { |
3a6820f2 | 1087 | .needs_file = 1, |
7b3188e7 | 1088 | .hash_reg_file = 1, |
3a6820f2 | 1089 | .unbound_nonreg_file = 1, |
8a72758c | 1090 | .pollout = 1, |
27926b68 | 1091 | .plug = 1, |
5bd2182d | 1092 | .audit_skip = 1, |
e8c2bc1f | 1093 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 1094 | }, |
0463b6c5 | 1095 | [IORING_OP_FADVISE] = { |
4840e418 | 1096 | .needs_file = 1, |
5bd2182d | 1097 | .audit_skip = 1, |
c1ca757b | 1098 | }, |
44526bed | 1099 | [IORING_OP_MADVISE] = {}, |
0463b6c5 | 1100 | [IORING_OP_SEND] = { |
fddaface JA |
1101 | .needs_file = 1, |
1102 | .unbound_nonreg_file = 1, | |
8a72758c | 1103 | .pollout = 1, |
5bd2182d | 1104 | .audit_skip = 1, |
fddaface | 1105 | }, |
0463b6c5 | 1106 | [IORING_OP_RECV] = { |
fddaface JA |
1107 | .needs_file = 1, |
1108 | .unbound_nonreg_file = 1, | |
8a72758c | 1109 | .pollin = 1, |
bcda7baa | 1110 | .buffer_select = 1, |
5bd2182d | 1111 | .audit_skip = 1, |
fddaface | 1112 | }, |
0463b6c5 | 1113 | [IORING_OP_OPENAT2] = { |
cebdb986 | 1114 | }, |
3e4827b0 JA |
1115 | [IORING_OP_EPOLL_CTL] = { |
1116 | .unbound_nonreg_file = 1, | |
5bd2182d | 1117 | .audit_skip = 1, |
3e4827b0 | 1118 | }, |
7d67af2c PB |
1119 | [IORING_OP_SPLICE] = { |
1120 | .needs_file = 1, | |
1121 | .hash_reg_file = 1, | |
1122 | .unbound_nonreg_file = 1, | |
5bd2182d PM |
1123 | .audit_skip = 1, |
1124 | }, | |
1125 | [IORING_OP_PROVIDE_BUFFERS] = { | |
1126 | .audit_skip = 1, | |
1127 | }, | |
1128 | [IORING_OP_REMOVE_BUFFERS] = { | |
1129 | .audit_skip = 1, | |
ddf0322d | 1130 | }, |
f2a8d5c7 PB |
1131 | [IORING_OP_TEE] = { |
1132 | .needs_file = 1, | |
1133 | .hash_reg_file = 1, | |
1134 | .unbound_nonreg_file = 1, | |
5bd2182d | 1135 | .audit_skip = 1, |
f2a8d5c7 | 1136 | }, |
36f4fa68 JA |
1137 | [IORING_OP_SHUTDOWN] = { |
1138 | .needs_file = 1, | |
1139 | }, | |
44526bed JA |
1140 | [IORING_OP_RENAMEAT] = {}, |
1141 | [IORING_OP_UNLINKAT] = {}, | |
e34a02dc | 1142 | [IORING_OP_MKDIRAT] = {}, |
7a8721f8 | 1143 | [IORING_OP_SYMLINKAT] = {}, |
cf30da90 | 1144 | [IORING_OP_LINKAT] = {}, |
4f57f06c JA |
1145 | [IORING_OP_MSG_RING] = { |
1146 | .needs_file = 1, | |
1147 | }, | |
d3656344 JA |
1148 | }; |
1149 | ||
0756a869 PB |
1150 | /* requests with any of those set should undergo io_disarm_next() */ |
1151 | #define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL) | |
1152 | ||
7a612350 | 1153 | static bool io_disarm_next(struct io_kiocb *req); |
eef51daa | 1154 | static void io_uring_del_tctx_node(unsigned long index); |
9936c7c2 PB |
1155 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
1156 | struct task_struct *task, | |
3dd0c97a | 1157 | bool cancel_all); |
78cc687b | 1158 | static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd); |
1ffc5422 | 1159 | |
913a571a PB |
1160 | static void io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags); |
1161 | ||
ec9c02ad | 1162 | static void io_put_req(struct io_kiocb *req); |
91c2f697 | 1163 | static void io_put_req_deferred(struct io_kiocb *req); |
c7dae4ba | 1164 | static void io_dismantle_req(struct io_kiocb *req); |
94ae5e77 | 1165 | static void io_queue_linked_timeout(struct io_kiocb *req); |
fdecb662 | 1166 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 1167 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 | 1168 | unsigned nr_args); |
68fb8979 | 1169 | static void io_clean_op(struct io_kiocb *req); |
ac177053 | 1170 | static struct file *io_file_get(struct io_ring_ctx *ctx, |
8371adf5 | 1171 | struct io_kiocb *req, int fd, bool fixed); |
c5eef2b9 | 1172 | static void __io_queue_sqe(struct io_kiocb *req); |
269bbe5f | 1173 | static void io_rsrc_put_work(struct work_struct *work); |
de0617e4 | 1174 | |
907d1df3 | 1175 | static void io_req_task_queue(struct io_kiocb *req); |
c450178d | 1176 | static void __io_submit_flush_completions(struct io_ring_ctx *ctx); |
179ae0d1 | 1177 | static int io_req_prep_async(struct io_kiocb *req); |
de0617e4 | 1178 | |
b9445598 PB |
1179 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
1180 | unsigned int issue_flags, u32 slot_index); | |
7df778be PB |
1181 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags); |
1182 | ||
f1042b6c | 1183 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer); |
b9445598 | 1184 | |
2b188cc1 JA |
1185 | static struct kmem_cache *req_cachep; |
1186 | ||
0918682b | 1187 | static const struct file_operations io_uring_fops; |
2b188cc1 JA |
1188 | |
1189 | struct sock *io_uring_get_socket(struct file *file) | |
1190 | { | |
1191 | #if defined(CONFIG_UNIX) | |
1192 | if (file->f_op == &io_uring_fops) { | |
1193 | struct io_ring_ctx *ctx = file->private_data; | |
1194 | ||
1195 | return ctx->ring_sock->sk; | |
1196 | } | |
1197 | #endif | |
1198 | return NULL; | |
1199 | } | |
1200 | EXPORT_SYMBOL(io_uring_get_socket); | |
1201 | ||
f237c30a PB |
1202 | static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked) |
1203 | { | |
1204 | if (!*locked) { | |
1205 | mutex_lock(&ctx->uring_lock); | |
1206 | *locked = true; | |
1207 | } | |
1208 | } | |
1209 | ||
f2f87370 PB |
1210 | #define io_for_each_link(pos, head) \ |
1211 | for (pos = (head); pos; pos = pos->link) | |
1212 | ||
21c843d5 PB |
1213 | /* |
1214 | * Shamelessly stolen from the mm implementation of page reference checking, | |
1215 | * see commit f958d7b528b1 for details. | |
1216 | */ | |
1217 | #define req_ref_zero_or_close_to_overflow(req) \ | |
1218 | ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u) | |
1219 | ||
1220 | static inline bool req_ref_inc_not_zero(struct io_kiocb *req) | |
1221 | { | |
20e60a38 | 1222 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1223 | return atomic_inc_not_zero(&req->refs); |
1224 | } | |
1225 | ||
21c843d5 PB |
1226 | static inline bool req_ref_put_and_test(struct io_kiocb *req) |
1227 | { | |
20e60a38 PB |
1228 | if (likely(!(req->flags & REQ_F_REFCOUNT))) |
1229 | return true; | |
1230 | ||
21c843d5 PB |
1231 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1232 | return atomic_dec_and_test(&req->refs); | |
1233 | } | |
1234 | ||
21c843d5 PB |
1235 | static inline void req_ref_get(struct io_kiocb *req) |
1236 | { | |
20e60a38 | 1237 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1238 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1239 | atomic_inc(&req->refs); | |
1240 | } | |
1241 | ||
c450178d PB |
1242 | static inline void io_submit_flush_completions(struct io_ring_ctx *ctx) |
1243 | { | |
6f33b0bc | 1244 | if (!wq_list_empty(&ctx->submit_state.compl_reqs)) |
c450178d PB |
1245 | __io_submit_flush_completions(ctx); |
1246 | } | |
1247 | ||
48dcd38d | 1248 | static inline void __io_req_set_refcount(struct io_kiocb *req, int nr) |
20e60a38 PB |
1249 | { |
1250 | if (!(req->flags & REQ_F_REFCOUNT)) { | |
1251 | req->flags |= REQ_F_REFCOUNT; | |
48dcd38d | 1252 | atomic_set(&req->refs, nr); |
20e60a38 PB |
1253 | } |
1254 | } | |
1255 | ||
48dcd38d PB |
1256 | static inline void io_req_set_refcount(struct io_kiocb *req) |
1257 | { | |
1258 | __io_req_set_refcount(req, 1); | |
1259 | } | |
1260 | ||
ab409402 PB |
1261 | #define IO_RSRC_REF_BATCH 100 |
1262 | ||
1263 | static inline void io_req_put_rsrc_locked(struct io_kiocb *req, | |
1264 | struct io_ring_ctx *ctx) | |
1265 | __must_hold(&ctx->uring_lock) | |
36f72fe2 | 1266 | { |
ab409402 PB |
1267 | struct percpu_ref *ref = req->fixed_rsrc_refs; |
1268 | ||
1269 | if (ref) { | |
1270 | if (ref == &ctx->rsrc_node->refs) | |
1271 | ctx->rsrc_cached_refs++; | |
1272 | else | |
1273 | percpu_ref_put(ref); | |
1274 | } | |
1275 | } | |
1276 | ||
1277 | static inline void io_req_put_rsrc(struct io_kiocb *req, struct io_ring_ctx *ctx) | |
1278 | { | |
1279 | if (req->fixed_rsrc_refs) | |
1280 | percpu_ref_put(req->fixed_rsrc_refs); | |
1281 | } | |
1282 | ||
1283 | static __cold void io_rsrc_refs_drop(struct io_ring_ctx *ctx) | |
1284 | __must_hold(&ctx->uring_lock) | |
1285 | { | |
1286 | if (ctx->rsrc_cached_refs) { | |
1287 | percpu_ref_put_many(&ctx->rsrc_node->refs, ctx->rsrc_cached_refs); | |
1288 | ctx->rsrc_cached_refs = 0; | |
1289 | } | |
1290 | } | |
1291 | ||
1292 | static void io_rsrc_refs_refill(struct io_ring_ctx *ctx) | |
1293 | __must_hold(&ctx->uring_lock) | |
1294 | { | |
1295 | ctx->rsrc_cached_refs += IO_RSRC_REF_BATCH; | |
1296 | percpu_ref_get_many(&ctx->rsrc_node->refs, IO_RSRC_REF_BATCH); | |
1297 | } | |
36f72fe2 | 1298 | |
a46be971 PB |
1299 | static inline void io_req_set_rsrc_node(struct io_kiocb *req, |
1300 | struct io_ring_ctx *ctx) | |
36f72fe2 | 1301 | { |
269bbe5f | 1302 | if (!req->fixed_rsrc_refs) { |
a7f0ed5a | 1303 | req->fixed_rsrc_refs = &ctx->rsrc_node->refs; |
ab409402 PB |
1304 | ctx->rsrc_cached_refs--; |
1305 | if (unlikely(ctx->rsrc_cached_refs < 0)) | |
1306 | io_rsrc_refs_refill(ctx); | |
36f72fe2 PB |
1307 | } |
1308 | } | |
1309 | ||
cc3cec83 | 1310 | static unsigned int __io_put_kbuf(struct io_kiocb *req, struct list_head *list) |
3648e526 | 1311 | { |
d1fd1c20 | 1312 | struct io_buffer *kbuf = req->kbuf; |
3648e526 HX |
1313 | unsigned int cflags; |
1314 | ||
cc3cec83 | 1315 | cflags = IORING_CQE_F_BUFFER | (kbuf->bid << IORING_CQE_BUFFER_SHIFT); |
3648e526 | 1316 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
cc3cec83 | 1317 | list_add(&kbuf->list, list); |
d1fd1c20 | 1318 | req->kbuf = NULL; |
3648e526 HX |
1319 | return cflags; |
1320 | } | |
1321 | ||
cc3cec83 | 1322 | static inline unsigned int io_put_kbuf_comp(struct io_kiocb *req) |
3648e526 HX |
1323 | { |
1324 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) | |
1325 | return 0; | |
cc3cec83 JA |
1326 | return __io_put_kbuf(req, &req->ctx->io_buffers_comp); |
1327 | } | |
1328 | ||
1329 | static inline unsigned int io_put_kbuf(struct io_kiocb *req, | |
1330 | unsigned issue_flags) | |
1331 | { | |
1332 | unsigned int cflags; | |
1333 | ||
1334 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) | |
1335 | return 0; | |
1336 | ||
1337 | /* | |
1338 | * We can add this buffer back to two lists: | |
1339 | * | |
1340 | * 1) The io_buffers_cache list. This one is protected by the | |
1341 | * ctx->uring_lock. If we already hold this lock, add back to this | |
1342 | * list as we can grab it from issue as well. | |
1343 | * 2) The io_buffers_comp list. This one is protected by the | |
1344 | * ctx->completion_lock. | |
1345 | * | |
1346 | * We migrate buffers from the comp_list to the issue cache list | |
1347 | * when we need one. | |
1348 | */ | |
1349 | if (issue_flags & IO_URING_F_UNLOCKED) { | |
1350 | struct io_ring_ctx *ctx = req->ctx; | |
1351 | ||
1352 | spin_lock(&ctx->completion_lock); | |
1353 | cflags = __io_put_kbuf(req, &ctx->io_buffers_comp); | |
1354 | spin_unlock(&ctx->completion_lock); | |
1355 | } else { | |
1356 | cflags = __io_put_kbuf(req, &req->ctx->io_buffers_cache); | |
1357 | } | |
1358 | ||
1359 | return cflags; | |
3648e526 HX |
1360 | } |
1361 | ||
b1c62645 JA |
1362 | static void io_kbuf_recycle(struct io_kiocb *req) |
1363 | { | |
1364 | struct io_ring_ctx *ctx = req->ctx; | |
1365 | struct io_buffer *head, *buf; | |
1366 | ||
1367 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) | |
1368 | return; | |
1369 | ||
1370 | lockdep_assert_held(&ctx->uring_lock); | |
1371 | ||
1372 | buf = req->kbuf; | |
1373 | ||
1374 | head = xa_load(&ctx->io_buffers, buf->bgid); | |
1375 | if (head) { | |
1376 | list_add(&buf->list, &head->list); | |
1377 | } else { | |
1378 | int ret; | |
1379 | ||
1380 | INIT_LIST_HEAD(&buf->list); | |
1381 | ||
1382 | /* if we fail, just leave buffer attached */ | |
1383 | ret = xa_insert(&ctx->io_buffers, buf->bgid, buf, GFP_KERNEL); | |
1384 | if (unlikely(ret < 0)) | |
1385 | return; | |
1386 | } | |
1387 | ||
1388 | req->flags &= ~REQ_F_BUFFER_SELECTED; | |
1389 | req->kbuf = NULL; | |
1390 | } | |
1391 | ||
3dd0c97a PB |
1392 | static bool io_match_task(struct io_kiocb *head, struct task_struct *task, |
1393 | bool cancel_all) | |
6af3f48b | 1394 | __must_hold(&req->ctx->timeout_lock) |
08d23634 PB |
1395 | { |
1396 | struct io_kiocb *req; | |
1397 | ||
68207680 | 1398 | if (task && head->task != task) |
08d23634 | 1399 | return false; |
3dd0c97a | 1400 | if (cancel_all) |
08d23634 PB |
1401 | return true; |
1402 | ||
1403 | io_for_each_link(req, head) { | |
b05a1bcd | 1404 | if (req->flags & REQ_F_INFLIGHT) |
02a13674 | 1405 | return true; |
08d23634 PB |
1406 | } |
1407 | return false; | |
1408 | } | |
1409 | ||
6af3f48b PB |
1410 | static bool io_match_linked(struct io_kiocb *head) |
1411 | { | |
1412 | struct io_kiocb *req; | |
1413 | ||
1414 | io_for_each_link(req, head) { | |
1415 | if (req->flags & REQ_F_INFLIGHT) | |
1416 | return true; | |
1417 | } | |
1418 | return false; | |
1419 | } | |
1420 | ||
1421 | /* | |
1422 | * As io_match_task() but protected against racing with linked timeouts. | |
1423 | * User must not hold timeout_lock. | |
1424 | */ | |
1425 | static bool io_match_task_safe(struct io_kiocb *head, struct task_struct *task, | |
1426 | bool cancel_all) | |
1427 | { | |
1428 | bool matched; | |
1429 | ||
1430 | if (task && head->task != task) | |
1431 | return false; | |
1432 | if (cancel_all) | |
1433 | return true; | |
1434 | ||
1435 | if (head->flags & REQ_F_LINK_TIMEOUT) { | |
1436 | struct io_ring_ctx *ctx = head->ctx; | |
1437 | ||
1438 | /* protect against races with linked timeouts */ | |
1439 | spin_lock_irq(&ctx->timeout_lock); | |
1440 | matched = io_match_linked(head); | |
1441 | spin_unlock_irq(&ctx->timeout_lock); | |
1442 | } else { | |
1443 | matched = io_match_linked(head); | |
1444 | } | |
1445 | return matched; | |
1446 | } | |
1447 | ||
d886e185 PB |
1448 | static inline bool req_has_async_data(struct io_kiocb *req) |
1449 | { | |
1450 | return req->flags & REQ_F_ASYNC_DATA; | |
1451 | } | |
1452 | ||
93d2bcd2 | 1453 | static inline void req_set_fail(struct io_kiocb *req) |
c40f6379 | 1454 | { |
93d2bcd2 | 1455 | req->flags |= REQ_F_FAIL; |
04c76b41 PB |
1456 | if (req->flags & REQ_F_CQE_SKIP) { |
1457 | req->flags &= ~REQ_F_CQE_SKIP; | |
1458 | req->flags |= REQ_F_SKIP_LINK_CQES; | |
1459 | } | |
c40f6379 | 1460 | } |
4a38aed2 | 1461 | |
a8295b98 HX |
1462 | static inline void req_fail_link_node(struct io_kiocb *req, int res) |
1463 | { | |
1464 | req_set_fail(req); | |
1465 | req->result = res; | |
1466 | } | |
1467 | ||
c072481d | 1468 | static __cold void io_ring_ctx_ref_free(struct percpu_ref *ref) |
2b188cc1 JA |
1469 | { |
1470 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
1471 | ||
0f158b4c | 1472 | complete(&ctx->ref_comp); |
2b188cc1 JA |
1473 | } |
1474 | ||
8eb7e2d0 PB |
1475 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
1476 | { | |
1477 | return !req->timeout.off; | |
1478 | } | |
1479 | ||
c072481d | 1480 | static __cold void io_fallback_req_func(struct work_struct *work) |
f56165e6 PB |
1481 | { |
1482 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, | |
1483 | fallback_work.work); | |
1484 | struct llist_node *node = llist_del_all(&ctx->fallback_llist); | |
1485 | struct io_kiocb *req, *tmp; | |
f237c30a | 1486 | bool locked = false; |
f56165e6 PB |
1487 | |
1488 | percpu_ref_get(&ctx->refs); | |
1489 | llist_for_each_entry_safe(req, tmp, node, io_task_work.fallback_node) | |
f237c30a | 1490 | req->io_task_work.func(req, &locked); |
5636c00d | 1491 | |
f237c30a | 1492 | if (locked) { |
c450178d | 1493 | io_submit_flush_completions(ctx); |
f237c30a PB |
1494 | mutex_unlock(&ctx->uring_lock); |
1495 | } | |
f56165e6 PB |
1496 | percpu_ref_put(&ctx->refs); |
1497 | } | |
1498 | ||
c072481d | 1499 | static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
2b188cc1 JA |
1500 | { |
1501 | struct io_ring_ctx *ctx; | |
78076bb6 | 1502 | int hash_bits; |
2b188cc1 JA |
1503 | |
1504 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1505 | if (!ctx) | |
1506 | return NULL; | |
1507 | ||
78076bb6 JA |
1508 | /* |
1509 | * Use 5 bits less than the max cq entries, that should give us around | |
1510 | * 32 entries per hash list if totally full and uniformly spread. | |
1511 | */ | |
1512 | hash_bits = ilog2(p->cq_entries); | |
1513 | hash_bits -= 5; | |
1514 | if (hash_bits <= 0) | |
1515 | hash_bits = 1; | |
1516 | ctx->cancel_hash_bits = hash_bits; | |
1517 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
1518 | GFP_KERNEL); | |
1519 | if (!ctx->cancel_hash) | |
1520 | goto err; | |
1521 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
1522 | ||
6224843d PB |
1523 | ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL); |
1524 | if (!ctx->dummy_ubuf) | |
1525 | goto err; | |
1526 | /* set invalid range, so io_import_fixed() fails meeting it */ | |
1527 | ctx->dummy_ubuf->ubuf = -1UL; | |
1528 | ||
21482896 | 1529 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
1530 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
1531 | goto err; | |
2b188cc1 JA |
1532 | |
1533 | ctx->flags = p->flags; | |
90554200 | 1534 | init_waitqueue_head(&ctx->sqo_sq_wait); |
69fb2131 | 1535 | INIT_LIST_HEAD(&ctx->sqd_list); |
1d7bb1d5 | 1536 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
cc3cec83 | 1537 | INIT_LIST_HEAD(&ctx->io_buffers_cache); |
4d9237e3 | 1538 | INIT_LIST_HEAD(&ctx->apoll_cache); |
0f158b4c | 1539 | init_completion(&ctx->ref_comp); |
9e15c3a0 | 1540 | xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1); |
61cf9370 | 1541 | xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1); |
2b188cc1 | 1542 | mutex_init(&ctx->uring_lock); |
311997b3 | 1543 | init_waitqueue_head(&ctx->cq_wait); |
2b188cc1 | 1544 | spin_lock_init(&ctx->completion_lock); |
89850fce | 1545 | spin_lock_init(&ctx->timeout_lock); |
5eef4e87 | 1546 | INIT_WQ_LIST(&ctx->iopoll_list); |
cc3cec83 JA |
1547 | INIT_LIST_HEAD(&ctx->io_buffers_pages); |
1548 | INIT_LIST_HEAD(&ctx->io_buffers_comp); | |
de0617e4 | 1549 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 1550 | INIT_LIST_HEAD(&ctx->timeout_list); |
ef9dd637 | 1551 | INIT_LIST_HEAD(&ctx->ltimeout_list); |
d67d2263 BM |
1552 | spin_lock_init(&ctx->rsrc_ref_lock); |
1553 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); | |
269bbe5f BM |
1554 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
1555 | init_llist_head(&ctx->rsrc_put_llist); | |
13bf43f5 | 1556 | INIT_LIST_HEAD(&ctx->tctx_list); |
c2b6c6bc PB |
1557 | ctx->submit_state.free_list.next = NULL; |
1558 | INIT_WQ_LIST(&ctx->locked_free_list); | |
9011bf9a | 1559 | INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func); |
6f33b0bc | 1560 | INIT_WQ_LIST(&ctx->submit_state.compl_reqs); |
adc8682e OL |
1561 | #ifdef CONFIG_NET_RX_BUSY_POLL |
1562 | INIT_LIST_HEAD(&ctx->napi_list); | |
1563 | spin_lock_init(&ctx->napi_lock); | |
1564 | #endif | |
2b188cc1 | 1565 | return ctx; |
206aefde | 1566 | err: |
6224843d | 1567 | kfree(ctx->dummy_ubuf); |
78076bb6 | 1568 | kfree(ctx->cancel_hash); |
206aefde JA |
1569 | kfree(ctx); |
1570 | return NULL; | |
2b188cc1 JA |
1571 | } |
1572 | ||
8f6ed49a PB |
1573 | static void io_account_cq_overflow(struct io_ring_ctx *ctx) |
1574 | { | |
1575 | struct io_rings *r = ctx->rings; | |
1576 | ||
1577 | WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1); | |
1578 | ctx->cq_extra--; | |
1579 | } | |
1580 | ||
9cf7c104 | 1581 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
7adf4eaf | 1582 | { |
2bc9930e JA |
1583 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
1584 | struct io_ring_ctx *ctx = req->ctx; | |
a197f664 | 1585 | |
8f6ed49a | 1586 | return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail; |
2bc9930e | 1587 | } |
de0617e4 | 1588 | |
9d858b21 | 1589 | return false; |
de0617e4 JA |
1590 | } |
1591 | ||
35645ac3 PB |
1592 | #define FFS_NOWAIT 0x1UL |
1593 | #define FFS_ISREG 0x2UL | |
1594 | #define FFS_MASK ~(FFS_NOWAIT|FFS_ISREG) | |
c97d8a0f PB |
1595 | |
1596 | static inline bool io_req_ffs_set(struct io_kiocb *req) | |
1597 | { | |
35645ac3 | 1598 | return req->flags & REQ_F_FIXED_FILE; |
c97d8a0f PB |
1599 | } |
1600 | ||
c072481d | 1601 | static inline void io_req_track_inflight(struct io_kiocb *req) |
ce3d5aae | 1602 | { |
ce3d5aae | 1603 | if (!(req->flags & REQ_F_INFLIGHT)) { |
ce3d5aae | 1604 | req->flags |= REQ_F_INFLIGHT; |
b303fe2e | 1605 | atomic_inc(¤t->io_uring->inflight_tracked); |
ce3d5aae PB |
1606 | } |
1607 | } | |
1608 | ||
fd08e530 PB |
1609 | static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req) |
1610 | { | |
906c6caa PB |
1611 | if (WARN_ON_ONCE(!req->link)) |
1612 | return NULL; | |
1613 | ||
4d13d1a4 PB |
1614 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
1615 | req->flags |= REQ_F_LINK_TIMEOUT; | |
fd08e530 PB |
1616 | |
1617 | /* linked timeouts should have two refs once prep'ed */ | |
48dcd38d | 1618 | io_req_set_refcount(req); |
4d13d1a4 PB |
1619 | __io_req_set_refcount(req->link, 2); |
1620 | return req->link; | |
fd08e530 PB |
1621 | } |
1622 | ||
1623 | static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) | |
1624 | { | |
4d13d1a4 | 1625 | if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT))) |
fd08e530 PB |
1626 | return NULL; |
1627 | return __io_prep_linked_timeout(req); | |
1628 | } | |
1629 | ||
1e6fa521 JA |
1630 | static void io_prep_async_work(struct io_kiocb *req) |
1631 | { | |
1632 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
1e6fa521 JA |
1633 | struct io_ring_ctx *ctx = req->ctx; |
1634 | ||
b8e64b53 PB |
1635 | if (!(req->flags & REQ_F_CREDS)) { |
1636 | req->flags |= REQ_F_CREDS; | |
c10d1f98 | 1637 | req->creds = get_current_cred(); |
b8e64b53 | 1638 | } |
003e8dcc | 1639 | |
e1d675df PB |
1640 | req->work.list.next = NULL; |
1641 | req->work.flags = 0; | |
feaadc4f PB |
1642 | if (req->flags & REQ_F_FORCE_ASYNC) |
1643 | req->work.flags |= IO_WQ_WORK_CONCURRENT; | |
1644 | ||
1e6fa521 JA |
1645 | if (req->flags & REQ_F_ISREG) { |
1646 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) | |
1647 | io_wq_hash_work(&req->work, file_inode(req->file)); | |
4b982bd0 | 1648 | } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) { |
1e6fa521 JA |
1649 | if (def->unbound_nonreg_file) |
1650 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1651 | } | |
e1d675df PB |
1652 | |
1653 | switch (req->opcode) { | |
1654 | case IORING_OP_SPLICE: | |
1655 | case IORING_OP_TEE: | |
e1d675df PB |
1656 | if (!S_ISREG(file_inode(req->splice.file_in)->i_mode)) |
1657 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1658 | break; | |
1659 | } | |
561fb04a | 1660 | } |
cccf0ee8 | 1661 | |
cbdcb435 | 1662 | static void io_prep_async_link(struct io_kiocb *req) |
561fb04a | 1663 | { |
cbdcb435 | 1664 | struct io_kiocb *cur; |
54a91f3b | 1665 | |
44eff40a PB |
1666 | if (req->flags & REQ_F_LINK_TIMEOUT) { |
1667 | struct io_ring_ctx *ctx = req->ctx; | |
1668 | ||
674ee8e1 | 1669 | spin_lock_irq(&ctx->timeout_lock); |
44eff40a PB |
1670 | io_for_each_link(cur, req) |
1671 | io_prep_async_work(cur); | |
674ee8e1 | 1672 | spin_unlock_irq(&ctx->timeout_lock); |
44eff40a PB |
1673 | } else { |
1674 | io_for_each_link(cur, req) | |
1675 | io_prep_async_work(cur); | |
1676 | } | |
561fb04a JA |
1677 | } |
1678 | ||
fff4e40e PB |
1679 | static inline void io_req_add_compl_list(struct io_kiocb *req) |
1680 | { | |
3d4aeb9f | 1681 | struct io_ring_ctx *ctx = req->ctx; |
33ce2aff | 1682 | struct io_submit_state *state = &ctx->submit_state; |
fff4e40e | 1683 | |
3d4aeb9f PB |
1684 | if (!(req->flags & REQ_F_CQE_SKIP)) |
1685 | ctx->submit_state.flush_cqes = true; | |
fff4e40e PB |
1686 | wq_list_add_tail(&req->comp_list, &state->compl_reqs); |
1687 | } | |
1688 | ||
00169246 | 1689 | static void io_queue_async_work(struct io_kiocb *req, bool *dont_use) |
561fb04a | 1690 | { |
a197f664 | 1691 | struct io_ring_ctx *ctx = req->ctx; |
cbdcb435 | 1692 | struct io_kiocb *link = io_prep_linked_timeout(req); |
5aa75ed5 | 1693 | struct io_uring_task *tctx = req->task->io_uring; |
561fb04a | 1694 | |
3bfe6106 JA |
1695 | BUG_ON(!tctx); |
1696 | BUG_ON(!tctx->io_wq); | |
561fb04a | 1697 | |
cbdcb435 PB |
1698 | /* init ->work of the whole link before punting */ |
1699 | io_prep_async_link(req); | |
991468dc JA |
1700 | |
1701 | /* | |
1702 | * Not expected to happen, but if we do have a bug where this _can_ | |
1703 | * happen, catch it here and ensure the request is marked as | |
1704 | * canceled. That will make io-wq go through the usual work cancel | |
1705 | * procedure rather than attempt to run this request (or create a new | |
1706 | * worker for it). | |
1707 | */ | |
1708 | if (WARN_ON_ONCE(!same_thread_group(req->task, current))) | |
1709 | req->work.flags |= IO_WQ_WORK_CANCEL; | |
1710 | ||
502c87d6 SR |
1711 | trace_io_uring_queue_async_work(ctx, req, req->user_data, req->opcode, req->flags, |
1712 | &req->work, io_wq_is_hashed(&req->work)); | |
ebf93667 | 1713 | io_wq_enqueue(tctx->io_wq, &req->work); |
7271ef3a JA |
1714 | if (link) |
1715 | io_queue_linked_timeout(link); | |
cbdcb435 PB |
1716 | } |
1717 | ||
1ee4160c | 1718 | static void io_kill_timeout(struct io_kiocb *req, int status) |
8c855885 | 1719 | __must_hold(&req->ctx->completion_lock) |
89850fce | 1720 | __must_hold(&req->ctx->timeout_lock) |
5262f567 | 1721 | { |
e8c2bc1f | 1722 | struct io_timeout_data *io = req->async_data; |
5262f567 | 1723 | |
fd9c7bc5 | 1724 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
2ae2eb9d PB |
1725 | if (status) |
1726 | req_set_fail(req); | |
01cec8c1 PB |
1727 | atomic_set(&req->ctx->cq_timeouts, |
1728 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
135fcde8 | 1729 | list_del_init(&req->timeout.list); |
913a571a | 1730 | io_fill_cqe_req(req, status, 0); |
91c2f697 | 1731 | io_put_req_deferred(req); |
5262f567 JA |
1732 | } |
1733 | } | |
1734 | ||
c072481d | 1735 | static __cold void io_queue_deferred(struct io_ring_ctx *ctx) |
de0617e4 | 1736 | { |
441b8a78 | 1737 | while (!list_empty(&ctx->defer_list)) { |
27dc8338 PB |
1738 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
1739 | struct io_defer_entry, list); | |
de0617e4 | 1740 | |
9cf7c104 | 1741 | if (req_need_defer(de->req, de->seq)) |
04518945 | 1742 | break; |
27dc8338 | 1743 | list_del_init(&de->list); |
907d1df3 | 1744 | io_req_task_queue(de->req); |
27dc8338 | 1745 | kfree(de); |
441b8a78 | 1746 | } |
04518945 PB |
1747 | } |
1748 | ||
c072481d | 1749 | static __cold void io_flush_timeouts(struct io_ring_ctx *ctx) |
89850fce | 1750 | __must_hold(&ctx->completion_lock) |
de0617e4 | 1751 | { |
441b8a78 | 1752 | u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
f010505b | 1753 | |
79ebeaee | 1754 | spin_lock_irq(&ctx->timeout_lock); |
f18ee4cf | 1755 | while (!list_empty(&ctx->timeout_list)) { |
f010505b | 1756 | u32 events_needed, events_got; |
360428f8 | 1757 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
135fcde8 | 1758 | struct io_kiocb, timeout.list); |
de0617e4 | 1759 | |
8eb7e2d0 | 1760 | if (io_is_timeout_noseq(req)) |
360428f8 | 1761 | break; |
f010505b MDG |
1762 | |
1763 | /* | |
1764 | * Since seq can easily wrap around over time, subtract | |
1765 | * the last seq at which timeouts were flushed before comparing. | |
1766 | * Assuming not more than 2^31-1 events have happened since, | |
1767 | * these subtractions won't have wrapped, so we can check if | |
1768 | * target is in [last_seq, current_seq] by comparing the two. | |
1769 | */ | |
1770 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; | |
1771 | events_got = seq - ctx->cq_last_tm_flush; | |
1772 | if (events_got < events_needed) | |
360428f8 | 1773 | break; |
bfe68a22 | 1774 | |
135fcde8 | 1775 | list_del_init(&req->timeout.list); |
1ee4160c | 1776 | io_kill_timeout(req, 0); |
f18ee4cf | 1777 | } |
f010505b | 1778 | ctx->cq_last_tm_flush = seq; |
79ebeaee | 1779 | spin_unlock_irq(&ctx->timeout_lock); |
360428f8 | 1780 | } |
5262f567 | 1781 | |
c072481d | 1782 | static __cold void __io_commit_cqring_flush(struct io_ring_ctx *ctx) |
360428f8 | 1783 | { |
2335f6f5 PB |
1784 | if (ctx->off_timeout_used) |
1785 | io_flush_timeouts(ctx); | |
1786 | if (ctx->drain_active) | |
1787 | io_queue_deferred(ctx); | |
1788 | } | |
1789 | ||
1790 | static inline void io_commit_cqring(struct io_ring_ctx *ctx) | |
1791 | { | |
1792 | if (unlikely(ctx->off_timeout_used || ctx->drain_active)) | |
1793 | __io_commit_cqring_flush(ctx); | |
ec30e04b PB |
1794 | /* order cqe stores with ring update */ |
1795 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); | |
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 | ||
1831 | /* Return quickly if ctx->io_ev_fd doesn't exist */ | |
1832 | if (likely(!rcu_dereference_raw(ctx->io_ev_fd))) | |
1833 | return; | |
1834 | ||
1835 | rcu_read_lock(); | |
1836 | /* | |
1837 | * rcu_dereference ctx->io_ev_fd once and use it for both for checking | |
1838 | * and eventfd_signal | |
1839 | */ | |
1840 | ev_fd = rcu_dereference(ctx->io_ev_fd); | |
1841 | ||
1842 | /* | |
1843 | * Check again if ev_fd exists incase an io_eventfd_unregister call | |
1844 | * completed between the NULL check of ctx->io_ev_fd at the start of | |
1845 | * the function and rcu_read_lock. | |
1846 | */ | |
1847 | if (unlikely(!ev_fd)) | |
1848 | goto out; | |
7e55a19c | 1849 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
77bc59b4 UA |
1850 | goto out; |
1851 | ||
c75312dd | 1852 | if (!ev_fd->eventfd_async || io_wq_current_is_worker()) |
77bc59b4 UA |
1853 | eventfd_signal(ev_fd->cq_ev_fd, 1); |
1854 | ||
1855 | out: | |
1856 | rcu_read_unlock(); | |
f2842ab5 JA |
1857 | } |
1858 | ||
2c5d763c JA |
1859 | /* |
1860 | * This should only get called when at least one event has been posted. | |
1861 | * Some applications rely on the eventfd notification count only changing | |
1862 | * IFF a new CQE has been added to the CQ ring. There's no depedency on | |
1863 | * 1:1 relationship between how many times this function is called (and | |
1864 | * hence the eventfd count) and number of CQEs posted to the CQ ring. | |
1865 | */ | |
b41e9852 | 1866 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
1d7bb1d5 | 1867 | { |
5fd46178 JA |
1868 | /* |
1869 | * wake_up_all() may seem excessive, but io_wake_function() and | |
1870 | * io_should_wake() handle the termination of the loop and only | |
1871 | * wake as many waiters as we need to. | |
1872 | */ | |
1873 | if (wq_has_sleeper(&ctx->cq_wait)) | |
1874 | wake_up_all(&ctx->cq_wait); | |
77bc59b4 | 1875 | io_eventfd_signal(ctx); |
1d7bb1d5 JA |
1876 | } |
1877 | ||
80c18e4a PB |
1878 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
1879 | { | |
c57a91fb PB |
1880 | /* see waitqueue_active() comment */ |
1881 | smp_mb(); | |
1882 | ||
80c18e4a | 1883 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
c57a91fb | 1884 | if (waitqueue_active(&ctx->cq_wait)) |
5fd46178 | 1885 | wake_up_all(&ctx->cq_wait); |
80c18e4a | 1886 | } |
77bc59b4 | 1887 | io_eventfd_signal(ctx); |
80c18e4a PB |
1888 | } |
1889 | ||
c4a2ed72 | 1890 | /* Returns true if there are no backlogged entries after the flush */ |
6c2450ae | 1891 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force) |
1d7bb1d5 | 1892 | { |
b18032bb | 1893 | bool all_flushed, posted; |
1d7bb1d5 | 1894 | |
a566c556 | 1895 | if (!force && __io_cqring_events(ctx) == ctx->cq_entries) |
e23de15f | 1896 | return false; |
1d7bb1d5 | 1897 | |
b18032bb | 1898 | posted = false; |
79ebeaee | 1899 | spin_lock(&ctx->completion_lock); |
6c2450ae | 1900 | while (!list_empty(&ctx->cq_overflow_list)) { |
d068b506 | 1901 | struct io_uring_cqe *cqe = io_get_cqe(ctx); |
6c2450ae | 1902 | struct io_overflow_cqe *ocqe; |
e6c8aa9a | 1903 | |
1d7bb1d5 JA |
1904 | if (!cqe && !force) |
1905 | break; | |
6c2450ae PB |
1906 | ocqe = list_first_entry(&ctx->cq_overflow_list, |
1907 | struct io_overflow_cqe, list); | |
1908 | if (cqe) | |
1909 | memcpy(cqe, &ocqe->cqe, sizeof(*cqe)); | |
1910 | else | |
8f6ed49a PB |
1911 | io_account_cq_overflow(ctx); |
1912 | ||
b18032bb | 1913 | posted = true; |
6c2450ae PB |
1914 | list_del(&ocqe->list); |
1915 | kfree(ocqe); | |
1d7bb1d5 JA |
1916 | } |
1917 | ||
09e88404 PB |
1918 | all_flushed = list_empty(&ctx->cq_overflow_list); |
1919 | if (all_flushed) { | |
5ed7a37d | 1920 | clear_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
1921 | WRITE_ONCE(ctx->rings->sq_flags, |
1922 | ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW); | |
09e88404 | 1923 | } |
46930143 | 1924 | |
b18032bb JA |
1925 | if (posted) |
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); |
c7dae4ba JA |
2083 | io_dismantle_req(req); |
2084 | io_put_task(req->task, 1); | |
c2b6c6bc | 2085 | wq_list_add_head(&req->comp_list, &ctx->locked_free_list); |
d0acdee2 | 2086 | ctx->locked_free_nr++; |
180f829f | 2087 | } |
a37fae8a HX |
2088 | } |
2089 | ||
2090 | static void io_req_complete_post(struct io_kiocb *req, s32 res, | |
2091 | u32 cflags) | |
2092 | { | |
2093 | struct io_ring_ctx *ctx = req->ctx; | |
2094 | ||
2095 | spin_lock(&ctx->completion_lock); | |
2096 | __io_req_complete_post(req, res, cflags); | |
7a612350 | 2097 | io_commit_cqring(ctx); |
79ebeaee | 2098 | spin_unlock(&ctx->completion_lock); |
a3f34907 | 2099 | io_cqring_ev_posted(ctx); |
4e3d9ff9 JA |
2100 | } |
2101 | ||
54daa9b2 PB |
2102 | static inline void io_req_complete_state(struct io_kiocb *req, s32 res, |
2103 | u32 cflags) | |
229a7b63 | 2104 | { |
a38d68db | 2105 | req->result = res; |
d17e56eb | 2106 | req->cflags = cflags; |
e342c807 | 2107 | req->flags |= REQ_F_COMPLETE_INLINE; |
e1e16097 JA |
2108 | } |
2109 | ||
889fca73 | 2110 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
54daa9b2 | 2111 | s32 res, u32 cflags) |
bcda7baa | 2112 | { |
889fca73 PB |
2113 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
2114 | io_req_complete_state(req, res, cflags); | |
a38d68db | 2115 | else |
c7dae4ba | 2116 | io_req_complete_post(req, res, cflags); |
bcda7baa JA |
2117 | } |
2118 | ||
54daa9b2 | 2119 | static inline void io_req_complete(struct io_kiocb *req, s32 res) |
0ddf92e8 | 2120 | { |
889fca73 | 2121 | __io_req_complete(req, 0, res, 0); |
0ddf92e8 JA |
2122 | } |
2123 | ||
54daa9b2 | 2124 | static void io_req_complete_failed(struct io_kiocb *req, s32 res) |
f41db273 | 2125 | { |
93d2bcd2 | 2126 | req_set_fail(req); |
3b2b78a8 | 2127 | io_req_complete_post(req, res, io_put_kbuf(req, 0)); |
f41db273 PB |
2128 | } |
2129 | ||
c6d3d9cb PB |
2130 | static void io_req_complete_fail_submit(struct io_kiocb *req) |
2131 | { | |
2132 | /* | |
2133 | * We don't submit, fail them all, for that replace hardlinks with | |
2134 | * normal links. Extra REQ_F_LINK is tolerated. | |
2135 | */ | |
2136 | req->flags &= ~REQ_F_HARDLINK; | |
2137 | req->flags |= REQ_F_LINK; | |
2138 | io_req_complete_failed(req, req->result); | |
2139 | } | |
2140 | ||
864ea921 PB |
2141 | /* |
2142 | * Don't initialise the fields below on every allocation, but do that in | |
2143 | * advance and keep them valid across allocations. | |
2144 | */ | |
2145 | static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx) | |
2146 | { | |
2147 | req->ctx = ctx; | |
2148 | req->link = NULL; | |
2149 | req->async_data = NULL; | |
2150 | /* not necessary, but safer to zero */ | |
2151 | req->result = 0; | |
2152 | } | |
2153 | ||
dac7a098 | 2154 | static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx, |
cd0ca2e0 | 2155 | struct io_submit_state *state) |
dac7a098 | 2156 | { |
79ebeaee | 2157 | spin_lock(&ctx->completion_lock); |
c2b6c6bc | 2158 | wq_list_splice(&ctx->locked_free_list, &state->free_list); |
d0acdee2 | 2159 | ctx->locked_free_nr = 0; |
79ebeaee | 2160 | spin_unlock(&ctx->completion_lock); |
dac7a098 PB |
2161 | } |
2162 | ||
dd78f492 | 2163 | /* Returns true IFF there are requests in the cache */ |
c7dae4ba | 2164 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
0ddf92e8 | 2165 | { |
c7dae4ba | 2166 | struct io_submit_state *state = &ctx->submit_state; |
0ddf92e8 | 2167 | |
c7dae4ba JA |
2168 | /* |
2169 | * If we have more than a batch's worth of requests in our IRQ side | |
2170 | * locked cache, grab the lock and move them over to our submission | |
2171 | * side cache. | |
2172 | */ | |
d0acdee2 | 2173 | if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH) |
cd0ca2e0 | 2174 | io_flush_cached_locked_reqs(ctx, state); |
c2b6c6bc | 2175 | return !!state->free_list.next; |
0ddf92e8 JA |
2176 | } |
2177 | ||
5d5901a3 PB |
2178 | /* |
2179 | * A request might get retired back into the request caches even before opcode | |
2180 | * handlers and io_issue_sqe() are done with it, e.g. inline completion path. | |
2181 | * Because of that, io_alloc_req() should be called only under ->uring_lock | |
2182 | * and with extra caution to not get a request that is still worked on. | |
2183 | */ | |
c072481d | 2184 | static __cold bool __io_alloc_req_refill(struct io_ring_ctx *ctx) |
5d5901a3 | 2185 | __must_hold(&ctx->uring_lock) |
2b188cc1 | 2186 | { |
e5d1bc0a | 2187 | struct io_submit_state *state = &ctx->submit_state; |
864ea921 | 2188 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
3ab665b7 PB |
2189 | void *reqs[IO_REQ_ALLOC_BATCH]; |
2190 | struct io_kiocb *req; | |
864ea921 | 2191 | int ret, i; |
e5d1bc0a | 2192 | |
c2b6c6bc | 2193 | if (likely(state->free_list.next || io_flush_cached_reqs(ctx))) |
a33ae9ce | 2194 | return true; |
e5d1bc0a | 2195 | |
3ab665b7 | 2196 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, ARRAY_SIZE(reqs), reqs); |
fd6fab2c | 2197 | |
864ea921 PB |
2198 | /* |
2199 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
2200 | * retry single alloc to be on the safe side. | |
2201 | */ | |
2202 | if (unlikely(ret <= 0)) { | |
3ab665b7 PB |
2203 | reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
2204 | if (!reqs[0]) | |
a33ae9ce | 2205 | return false; |
864ea921 | 2206 | ret = 1; |
2b188cc1 | 2207 | } |
864ea921 | 2208 | |
37f0e767 | 2209 | percpu_ref_get_many(&ctx->refs, ret); |
3ab665b7 PB |
2210 | for (i = 0; i < ret; i++) { |
2211 | req = reqs[i]; | |
2212 | ||
2213 | io_preinit_req(req, ctx); | |
c2b6c6bc | 2214 | wq_stack_add_head(&req->comp_list, &state->free_list); |
3ab665b7 | 2215 | } |
a33ae9ce PB |
2216 | return true; |
2217 | } | |
2218 | ||
2219 | static inline bool io_alloc_req_refill(struct io_ring_ctx *ctx) | |
2220 | { | |
2221 | if (unlikely(!ctx->submit_state.free_list.next)) | |
2222 | return __io_alloc_req_refill(ctx); | |
2223 | return true; | |
2224 | } | |
2225 | ||
2226 | static inline struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) | |
2227 | { | |
2228 | struct io_wq_work_node *node; | |
2229 | ||
2230 | node = wq_stack_extract(&ctx->submit_state.free_list); | |
c2b6c6bc | 2231 | return container_of(node, struct io_kiocb, comp_list); |
2b188cc1 JA |
2232 | } |
2233 | ||
e1d767f0 | 2234 | static inline void io_put_file(struct file *file) |
8da11c19 | 2235 | { |
e1d767f0 | 2236 | if (file) |
8da11c19 PB |
2237 | fput(file); |
2238 | } | |
2239 | ||
6b639522 | 2240 | static inline void io_dismantle_req(struct io_kiocb *req) |
2b188cc1 | 2241 | { |
094bae49 | 2242 | unsigned int flags = req->flags; |
929a3af9 | 2243 | |
867f8fa5 | 2244 | if (unlikely(flags & IO_REQ_CLEAN_FLAGS)) |
3a0a6902 | 2245 | io_clean_op(req); |
e1d767f0 PB |
2246 | if (!(flags & REQ_F_FIXED_FILE)) |
2247 | io_put_file(req->file); | |
e65ef56d JA |
2248 | } |
2249 | ||
c072481d | 2250 | static __cold void __io_free_req(struct io_kiocb *req) |
c6ca97b3 | 2251 | { |
51a4cc11 | 2252 | struct io_ring_ctx *ctx = req->ctx; |
c6ca97b3 | 2253 | |
ab409402 | 2254 | io_req_put_rsrc(req, ctx); |
216578e5 | 2255 | io_dismantle_req(req); |
7c660731 | 2256 | io_put_task(req->task, 1); |
c6ca97b3 | 2257 | |
79ebeaee | 2258 | spin_lock(&ctx->completion_lock); |
c2b6c6bc | 2259 | wq_list_add_head(&req->comp_list, &ctx->locked_free_list); |
c34b025f | 2260 | ctx->locked_free_nr++; |
79ebeaee | 2261 | spin_unlock(&ctx->completion_lock); |
e65ef56d JA |
2262 | } |
2263 | ||
f2f87370 PB |
2264 | static inline void io_remove_next_linked(struct io_kiocb *req) |
2265 | { | |
2266 | struct io_kiocb *nxt = req->link; | |
2267 | ||
2268 | req->link = nxt->link; | |
2269 | nxt->link = NULL; | |
2270 | } | |
2271 | ||
33cc89a9 PB |
2272 | static bool io_kill_linked_timeout(struct io_kiocb *req) |
2273 | __must_hold(&req->ctx->completion_lock) | |
89b263f6 | 2274 | __must_hold(&req->ctx->timeout_lock) |
2665abfd | 2275 | { |
33cc89a9 | 2276 | struct io_kiocb *link = req->link; |
f2f87370 | 2277 | |
b97e736a | 2278 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
c9abd7ad | 2279 | struct io_timeout_data *io = link->async_data; |
7c86ffee | 2280 | |
f2f87370 | 2281 | io_remove_next_linked(req); |
90cd7e42 | 2282 | link->timeout.head = NULL; |
fd9c7bc5 | 2283 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
ef9dd637 | 2284 | list_del(&link->timeout.list); |
04c76b41 | 2285 | /* leave REQ_F_CQE_SKIP to io_fill_cqe_req */ |
913a571a | 2286 | io_fill_cqe_req(link, -ECANCELED, 0); |
91c2f697 | 2287 | io_put_req_deferred(link); |
d4729fbd | 2288 | return true; |
c9abd7ad PB |
2289 | } |
2290 | } | |
d4729fbd | 2291 | return false; |
7c86ffee PB |
2292 | } |
2293 | ||
d148ca4b | 2294 | static void io_fail_links(struct io_kiocb *req) |
33cc89a9 | 2295 | __must_hold(&req->ctx->completion_lock) |
9e645e11 | 2296 | { |
33cc89a9 | 2297 | struct io_kiocb *nxt, *link = req->link; |
04c76b41 | 2298 | bool ignore_cqes = req->flags & REQ_F_SKIP_LINK_CQES; |
9e645e11 | 2299 | |
f2f87370 | 2300 | req->link = NULL; |
f2f87370 | 2301 | while (link) { |
a8295b98 HX |
2302 | long res = -ECANCELED; |
2303 | ||
2304 | if (link->flags & REQ_F_FAIL) | |
2305 | res = link->result; | |
2306 | ||
f2f87370 PB |
2307 | nxt = link->link; |
2308 | link->link = NULL; | |
2665abfd | 2309 | |
502c87d6 SR |
2310 | trace_io_uring_fail_link(req->ctx, req, req->user_data, |
2311 | req->opcode, link); | |
2312 | ||
04c76b41 PB |
2313 | if (!ignore_cqes) { |
2314 | link->flags &= ~REQ_F_CQE_SKIP; | |
2315 | io_fill_cqe_req(link, res, 0); | |
2316 | } | |
91c2f697 | 2317 | io_put_req_deferred(link); |
f2f87370 | 2318 | link = nxt; |
9e645e11 | 2319 | } |
33cc89a9 | 2320 | } |
9e645e11 | 2321 | |
33cc89a9 PB |
2322 | static bool io_disarm_next(struct io_kiocb *req) |
2323 | __must_hold(&req->ctx->completion_lock) | |
2324 | { | |
2325 | bool posted = false; | |
2326 | ||
0756a869 PB |
2327 | if (req->flags & REQ_F_ARM_LTIMEOUT) { |
2328 | struct io_kiocb *link = req->link; | |
2329 | ||
906c6caa | 2330 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
0756a869 PB |
2331 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
2332 | io_remove_next_linked(req); | |
04c76b41 | 2333 | /* leave REQ_F_CQE_SKIP to io_fill_cqe_req */ |
913a571a | 2334 | io_fill_cqe_req(link, -ECANCELED, 0); |
0756a869 PB |
2335 | io_put_req_deferred(link); |
2336 | posted = true; | |
2337 | } | |
2338 | } else if (req->flags & REQ_F_LINK_TIMEOUT) { | |
89b263f6 JA |
2339 | struct io_ring_ctx *ctx = req->ctx; |
2340 | ||
2341 | spin_lock_irq(&ctx->timeout_lock); | |
33cc89a9 | 2342 | posted = io_kill_linked_timeout(req); |
89b263f6 JA |
2343 | spin_unlock_irq(&ctx->timeout_lock); |
2344 | } | |
93d2bcd2 | 2345 | if (unlikely((req->flags & REQ_F_FAIL) && |
e4335ed3 | 2346 | !(req->flags & REQ_F_HARDLINK))) { |
33cc89a9 PB |
2347 | posted |= (req->link != NULL); |
2348 | io_fail_links(req); | |
2349 | } | |
2350 | return posted; | |
9e645e11 JA |
2351 | } |
2352 | ||
d81499bf PB |
2353 | static void __io_req_find_next_prep(struct io_kiocb *req) |
2354 | { | |
2355 | struct io_ring_ctx *ctx = req->ctx; | |
2356 | bool posted; | |
2357 | ||
2358 | spin_lock(&ctx->completion_lock); | |
2359 | posted = io_disarm_next(req); | |
2360 | if (posted) | |
33ce2aff | 2361 | io_commit_cqring(ctx); |
d81499bf PB |
2362 | spin_unlock(&ctx->completion_lock); |
2363 | if (posted) | |
2364 | io_cqring_ev_posted(ctx); | |
2365 | } | |
2366 | ||
2367 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) | |
c69f8dbe | 2368 | { |
33cc89a9 | 2369 | struct io_kiocb *nxt; |
944e58bf | 2370 | |
d81499bf PB |
2371 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
2372 | return NULL; | |
9e645e11 JA |
2373 | /* |
2374 | * If LINK is set, we have dependent requests in this chain. If we | |
2375 | * didn't fail this request, queue the first one up, moving any other | |
2376 | * dependencies to the next request. In case of failure, fail the rest | |
2377 | * of the chain. | |
2378 | */ | |
d81499bf PB |
2379 | if (unlikely(req->flags & IO_DISARM_MASK)) |
2380 | __io_req_find_next_prep(req); | |
33cc89a9 PB |
2381 | nxt = req->link; |
2382 | req->link = NULL; | |
2383 | return nxt; | |
4d7dd462 | 2384 | } |
9e645e11 | 2385 | |
f237c30a | 2386 | static void ctx_flush_and_put(struct io_ring_ctx *ctx, bool *locked) |
2c32395d PB |
2387 | { |
2388 | if (!ctx) | |
2389 | return; | |
f237c30a | 2390 | if (*locked) { |
c450178d | 2391 | io_submit_flush_completions(ctx); |
2c32395d | 2392 | mutex_unlock(&ctx->uring_lock); |
f237c30a | 2393 | *locked = false; |
2c32395d PB |
2394 | } |
2395 | percpu_ref_put(&ctx->refs); | |
2396 | } | |
2397 | ||
f28c240e HX |
2398 | static inline void ctx_commit_and_unlock(struct io_ring_ctx *ctx) |
2399 | { | |
2400 | io_commit_cqring(ctx); | |
2401 | spin_unlock(&ctx->completion_lock); | |
2402 | io_cqring_ev_posted(ctx); | |
2403 | } | |
2404 | ||
2405 | static void handle_prev_tw_list(struct io_wq_work_node *node, | |
2406 | struct io_ring_ctx **ctx, bool *uring_locked) | |
2407 | { | |
2408 | if (*ctx && !*uring_locked) | |
2409 | spin_lock(&(*ctx)->completion_lock); | |
2410 | ||
2411 | do { | |
2412 | struct io_wq_work_node *next = node->next; | |
2413 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2414 | io_task_work.node); | |
2415 | ||
2416 | if (req->ctx != *ctx) { | |
2417 | if (unlikely(!*uring_locked && *ctx)) | |
2418 | ctx_commit_and_unlock(*ctx); | |
2419 | ||
2420 | ctx_flush_and_put(*ctx, uring_locked); | |
2421 | *ctx = req->ctx; | |
2422 | /* if not contended, grab and improve batching */ | |
2423 | *uring_locked = mutex_trylock(&(*ctx)->uring_lock); | |
2424 | percpu_ref_get(&(*ctx)->refs); | |
2425 | if (unlikely(!*uring_locked)) | |
2426 | spin_lock(&(*ctx)->completion_lock); | |
2427 | } | |
2428 | if (likely(*uring_locked)) | |
2429 | req->io_task_work.func(req, uring_locked); | |
2430 | else | |
cc3cec83 JA |
2431 | __io_req_complete_post(req, req->result, |
2432 | io_put_kbuf_comp(req)); | |
f28c240e HX |
2433 | node = next; |
2434 | } while (node); | |
2435 | ||
2436 | if (unlikely(!*uring_locked)) | |
2437 | ctx_commit_and_unlock(*ctx); | |
2438 | } | |
2439 | ||
2440 | static void handle_tw_list(struct io_wq_work_node *node, | |
2441 | struct io_ring_ctx **ctx, bool *locked) | |
9f8d032a HX |
2442 | { |
2443 | do { | |
2444 | struct io_wq_work_node *next = node->next; | |
2445 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2446 | io_task_work.node); | |
2447 | ||
2448 | if (req->ctx != *ctx) { | |
2449 | ctx_flush_and_put(*ctx, locked); | |
2450 | *ctx = req->ctx; | |
2451 | /* if not contended, grab and improve batching */ | |
2452 | *locked = mutex_trylock(&(*ctx)->uring_lock); | |
2453 | percpu_ref_get(&(*ctx)->refs); | |
2454 | } | |
2455 | req->io_task_work.func(req, locked); | |
2456 | node = next; | |
2457 | } while (node); | |
2458 | } | |
2459 | ||
7cbf1722 | 2460 | static void tctx_task_work(struct callback_head *cb) |
c40f6379 | 2461 | { |
f28c240e | 2462 | bool uring_locked = false; |
ebd0df2e | 2463 | struct io_ring_ctx *ctx = NULL; |
3f18407d PB |
2464 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, |
2465 | task_work); | |
c40f6379 | 2466 | |
16f72070 | 2467 | while (1) { |
f28c240e | 2468 | struct io_wq_work_node *node1, *node2; |
3f18407d | 2469 | |
f28c240e HX |
2470 | if (!tctx->task_list.first && |
2471 | !tctx->prior_task_list.first && uring_locked) | |
8d4ad41e PB |
2472 | io_submit_flush_completions(ctx); |
2473 | ||
3f18407d | 2474 | spin_lock_irq(&tctx->task_lock); |
f28c240e HX |
2475 | node1 = tctx->prior_task_list.first; |
2476 | node2 = tctx->task_list.first; | |
3f18407d | 2477 | INIT_WQ_LIST(&tctx->task_list); |
f28c240e HX |
2478 | INIT_WQ_LIST(&tctx->prior_task_list); |
2479 | if (!node2 && !node1) | |
6294f368 | 2480 | tctx->task_running = false; |
3f18407d | 2481 | spin_unlock_irq(&tctx->task_lock); |
f28c240e | 2482 | if (!node2 && !node1) |
6294f368 | 2483 | break; |
3f18407d | 2484 | |
f28c240e HX |
2485 | if (node1) |
2486 | handle_prev_tw_list(node1, &ctx, &uring_locked); | |
6294f368 | 2487 | |
f28c240e HX |
2488 | if (node2) |
2489 | handle_tw_list(node2, &ctx, &uring_locked); | |
7cbf1722 | 2490 | cond_resched(); |
3f18407d | 2491 | } |
ebd0df2e | 2492 | |
f28c240e | 2493 | ctx_flush_and_put(ctx, &uring_locked); |
3cc7fdb9 PB |
2494 | |
2495 | /* relaxed read is enough as only the task itself sets ->in_idle */ | |
2496 | if (unlikely(atomic_read(&tctx->in_idle))) | |
2497 | io_uring_drop_tctx_refs(current); | |
7cbf1722 JA |
2498 | } |
2499 | ||
4813c377 | 2500 | static void io_req_task_work_add(struct io_kiocb *req, bool priority) |
7cbf1722 | 2501 | { |
c15b79de | 2502 | struct task_struct *tsk = req->task; |
7cbf1722 | 2503 | struct io_uring_task *tctx = tsk->io_uring; |
c15b79de | 2504 | enum task_work_notify_mode notify; |
e09ee510 | 2505 | struct io_wq_work_node *node; |
0b81e80c | 2506 | unsigned long flags; |
6294f368 | 2507 | bool running; |
7cbf1722 JA |
2508 | |
2509 | WARN_ON_ONCE(!tctx); | |
2510 | ||
0b81e80c | 2511 | spin_lock_irqsave(&tctx->task_lock, flags); |
4813c377 HX |
2512 | if (priority) |
2513 | wq_list_add_tail(&req->io_task_work.node, &tctx->prior_task_list); | |
2514 | else | |
2515 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); | |
6294f368 PB |
2516 | running = tctx->task_running; |
2517 | if (!running) | |
2518 | tctx->task_running = true; | |
0b81e80c | 2519 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
2520 | |
2521 | /* task_work already pending, we're done */ | |
6294f368 | 2522 | if (running) |
e09ee510 | 2523 | return; |
7cbf1722 | 2524 | |
c15b79de PB |
2525 | /* |
2526 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For | |
2527 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're | |
2528 | * processing task_work. There's no reliable way to tell if TWA_RESUME | |
2529 | * will do the job. | |
2530 | */ | |
2531 | notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL; | |
d97ec623 PB |
2532 | if (likely(!task_work_add(tsk, &tctx->task_work, notify))) { |
2533 | if (notify == TWA_NONE) | |
2534 | wake_up_process(tsk); | |
e09ee510 | 2535 | return; |
c15b79de | 2536 | } |
2215bed9 | 2537 | |
0b81e80c | 2538 | spin_lock_irqsave(&tctx->task_lock, flags); |
6294f368 | 2539 | tctx->task_running = false; |
4813c377 | 2540 | node = wq_list_merge(&tctx->prior_task_list, &tctx->task_list); |
0b81e80c | 2541 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 | 2542 | |
e09ee510 PB |
2543 | while (node) { |
2544 | req = container_of(node, struct io_kiocb, io_task_work.node); | |
2545 | node = node->next; | |
2546 | if (llist_add(&req->io_task_work.fallback_node, | |
2547 | &req->ctx->fallback_llist)) | |
2548 | schedule_delayed_work(&req->ctx->fallback_work, 1); | |
2549 | } | |
eab30c4d PB |
2550 | } |
2551 | ||
f237c30a | 2552 | static void io_req_task_cancel(struct io_kiocb *req, bool *locked) |
c40f6379 | 2553 | { |
87ceb6a6 | 2554 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 | 2555 | |
b18a1a45 | 2556 | /* not needed for normal modes, but SQPOLL depends on it */ |
f237c30a | 2557 | io_tw_lock(ctx, locked); |
2593553a | 2558 | io_req_complete_failed(req, req->result); |
c40f6379 JA |
2559 | } |
2560 | ||
f237c30a | 2561 | static void io_req_task_submit(struct io_kiocb *req, bool *locked) |
c40f6379 JA |
2562 | { |
2563 | struct io_ring_ctx *ctx = req->ctx; | |
2564 | ||
f237c30a | 2565 | io_tw_lock(ctx, locked); |
316319e8 | 2566 | /* req->task == current here, checking PF_EXITING is safe */ |
af066f31 | 2567 | if (likely(!(req->task->flags & PF_EXITING))) |
c5eef2b9 | 2568 | __io_queue_sqe(req); |
81b6d05c | 2569 | else |
2593553a | 2570 | io_req_complete_failed(req, -EFAULT); |
c40f6379 JA |
2571 | } |
2572 | ||
2c4b8eb6 | 2573 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
c40f6379 | 2574 | { |
2c4b8eb6 | 2575 | req->result = ret; |
5b0a6acc | 2576 | req->io_task_work.func = io_req_task_cancel; |
4813c377 | 2577 | io_req_task_work_add(req, false); |
c40f6379 JA |
2578 | } |
2579 | ||
2c4b8eb6 | 2580 | static void io_req_task_queue(struct io_kiocb *req) |
a3df7698 | 2581 | { |
5b0a6acc | 2582 | req->io_task_work.func = io_req_task_submit; |
4813c377 | 2583 | io_req_task_work_add(req, false); |
a3df7698 PB |
2584 | } |
2585 | ||
773af691 JA |
2586 | static void io_req_task_queue_reissue(struct io_kiocb *req) |
2587 | { | |
2588 | req->io_task_work.func = io_queue_async_work; | |
4813c377 | 2589 | io_req_task_work_add(req, false); |
773af691 JA |
2590 | } |
2591 | ||
f2f87370 | 2592 | static inline void io_queue_next(struct io_kiocb *req) |
c69f8dbe | 2593 | { |
9b5f7bd9 | 2594 | struct io_kiocb *nxt = io_req_find_next(req); |
944e58bf PB |
2595 | |
2596 | if (nxt) | |
906a8c3f | 2597 | io_req_task_queue(nxt); |
c69f8dbe JL |
2598 | } |
2599 | ||
c3524383 | 2600 | static void io_free_req(struct io_kiocb *req) |
7a743e22 | 2601 | { |
c3524383 PB |
2602 | io_queue_next(req); |
2603 | __io_free_req(req); | |
2604 | } | |
8766dd51 | 2605 | |
f237c30a PB |
2606 | static void io_free_req_work(struct io_kiocb *req, bool *locked) |
2607 | { | |
2608 | io_free_req(req); | |
2609 | } | |
2610 | ||
3aa83bfb | 2611 | static void io_free_batch_list(struct io_ring_ctx *ctx, |
1cce17ac | 2612 | struct io_wq_work_node *node) |
3aa83bfb | 2613 | __must_hold(&ctx->uring_lock) |
5af1d13e | 2614 | { |
d4b7a5ef | 2615 | struct task_struct *task = NULL; |
37f0e767 | 2616 | int task_refs = 0; |
5af1d13e | 2617 | |
3aa83bfb PB |
2618 | do { |
2619 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2620 | comp_list); | |
2d6500d4 | 2621 | |
def77acf | 2622 | if (unlikely(req->flags & REQ_F_REFCOUNT)) { |
c1e53a69 | 2623 | node = req->comp_list.next; |
def77acf PB |
2624 | if (!req_ref_put_and_test(req)) |
2625 | continue; | |
c1e53a69 | 2626 | } |
2d6500d4 | 2627 | |
ab409402 | 2628 | io_req_put_rsrc_locked(req, ctx); |
d4b7a5ef PB |
2629 | io_queue_next(req); |
2630 | io_dismantle_req(req); | |
5af1d13e | 2631 | |
d4b7a5ef PB |
2632 | if (req->task != task) { |
2633 | if (task) | |
2634 | io_put_task(task, task_refs); | |
2635 | task = req->task; | |
2636 | task_refs = 0; | |
2637 | } | |
2638 | task_refs++; | |
c1e53a69 | 2639 | node = req->comp_list.next; |
d4b7a5ef | 2640 | wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list); |
3aa83bfb | 2641 | } while (node); |
d4b7a5ef | 2642 | |
d4b7a5ef PB |
2643 | if (task) |
2644 | io_put_task(task, task_refs); | |
7a743e22 PB |
2645 | } |
2646 | ||
c450178d | 2647 | static void __io_submit_flush_completions(struct io_ring_ctx *ctx) |
a141dd89 | 2648 | __must_hold(&ctx->uring_lock) |
905c172f | 2649 | { |
6f33b0bc | 2650 | struct io_wq_work_node *node, *prev; |
cd0ca2e0 | 2651 | struct io_submit_state *state = &ctx->submit_state; |
905c172f | 2652 | |
3d4aeb9f PB |
2653 | if (state->flush_cqes) { |
2654 | spin_lock(&ctx->completion_lock); | |
2655 | wq_list_for_each(node, prev, &state->compl_reqs) { | |
2656 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
6f33b0bc | 2657 | comp_list); |
5182ed2e | 2658 | |
3d4aeb9f | 2659 | if (!(req->flags & REQ_F_CQE_SKIP)) |
ae4da189 | 2660 | __io_fill_cqe_req(req, req->result, req->cflags); |
4d9237e3 JA |
2661 | if ((req->flags & REQ_F_POLLED) && req->apoll) { |
2662 | struct async_poll *apoll = req->apoll; | |
2663 | ||
2664 | if (apoll->double_poll) | |
2665 | kfree(apoll->double_poll); | |
2666 | list_add(&apoll->poll.wait.entry, | |
2667 | &ctx->apoll_cache); | |
2668 | req->flags &= ~REQ_F_POLLED; | |
2669 | } | |
3d4aeb9f PB |
2670 | } |
2671 | ||
2672 | io_commit_cqring(ctx); | |
2673 | spin_unlock(&ctx->completion_lock); | |
2674 | io_cqring_ev_posted(ctx); | |
2675 | state->flush_cqes = false; | |
905c172f | 2676 | } |
5182ed2e | 2677 | |
1cce17ac | 2678 | io_free_batch_list(ctx, state->compl_reqs.first); |
6f33b0bc | 2679 | INIT_WQ_LIST(&state->compl_reqs); |
7a743e22 PB |
2680 | } |
2681 | ||
ba816ad6 JA |
2682 | /* |
2683 | * Drop reference to request, return next in chain (if there is one) if this | |
2684 | * was the last reference to this request. | |
2685 | */ | |
0d85035a | 2686 | static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
e65ef56d | 2687 | { |
9b5f7bd9 PB |
2688 | struct io_kiocb *nxt = NULL; |
2689 | ||
de9b4cca | 2690 | if (req_ref_put_and_test(req)) { |
9b5f7bd9 | 2691 | nxt = io_req_find_next(req); |
4d7dd462 | 2692 | __io_free_req(req); |
2a44f467 | 2693 | } |
9b5f7bd9 | 2694 | return nxt; |
2b188cc1 JA |
2695 | } |
2696 | ||
0d85035a | 2697 | static inline void io_put_req(struct io_kiocb *req) |
e65ef56d | 2698 | { |
de9b4cca | 2699 | if (req_ref_put_and_test(req)) |
e65ef56d | 2700 | io_free_req(req); |
2b188cc1 JA |
2701 | } |
2702 | ||
91c2f697 | 2703 | static inline void io_put_req_deferred(struct io_kiocb *req) |
216578e5 | 2704 | { |
91c2f697 | 2705 | if (req_ref_put_and_test(req)) { |
f237c30a | 2706 | req->io_task_work.func = io_free_req_work; |
4813c377 | 2707 | io_req_task_work_add(req, false); |
543af3a1 | 2708 | } |
216578e5 PB |
2709 | } |
2710 | ||
6c503150 | 2711 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
a3a0e43f JA |
2712 | { |
2713 | /* See comment at the top of this file */ | |
2714 | smp_rmb(); | |
e23de15f | 2715 | return __io_cqring_events(ctx); |
a3a0e43f JA |
2716 | } |
2717 | ||
fb5ccc98 PB |
2718 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
2719 | { | |
2720 | struct io_rings *rings = ctx->rings; | |
2721 | ||
2722 | /* make sure SQ entry isn't read before tail */ | |
2723 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
2724 | } | |
2725 | ||
4c6e277c JA |
2726 | static inline bool io_run_task_work(void) |
2727 | { | |
ef98eb04 | 2728 | if (test_thread_flag(TIF_NOTIFY_SIGNAL) || current->task_works) { |
4c6e277c | 2729 | __set_current_state(TASK_RUNNING); |
ef98eb04 | 2730 | tracehook_notify_signal(); |
4c6e277c JA |
2731 | return true; |
2732 | } | |
2733 | ||
2734 | return false; | |
bcda7baa JA |
2735 | } |
2736 | ||
5ba3c874 | 2737 | static int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin) |
def596e9 | 2738 | { |
5eef4e87 | 2739 | struct io_wq_work_node *pos, *start, *prev; |
d729cf9a | 2740 | unsigned int poll_flags = BLK_POLL_NOSLEEP; |
b688f11e | 2741 | DEFINE_IO_COMP_BATCH(iob); |
5ba3c874 | 2742 | int nr_events = 0; |
def596e9 JA |
2743 | |
2744 | /* | |
2745 | * Only spin for completions if we don't have multiple devices hanging | |
87a115fb | 2746 | * off our complete list. |
def596e9 | 2747 | */ |
87a115fb | 2748 | if (ctx->poll_multi_queue || force_nonspin) |
ef99b2d3 | 2749 | poll_flags |= BLK_POLL_ONESHOT; |
def596e9 | 2750 | |
5eef4e87 PB |
2751 | wq_list_for_each(pos, start, &ctx->iopoll_list) { |
2752 | struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); | |
9adbd45d | 2753 | struct kiocb *kiocb = &req->rw.kiocb; |
a2416e1e | 2754 | int ret; |
def596e9 JA |
2755 | |
2756 | /* | |
581f9810 BM |
2757 | * Move completed and retryable entries to our local lists. |
2758 | * If we find a request that requires polling, break out | |
2759 | * and complete those lists first, if we have entries there. | |
def596e9 | 2760 | */ |
e3f721e6 | 2761 | if (READ_ONCE(req->iopoll_completed)) |
def596e9 JA |
2762 | break; |
2763 | ||
b688f11e | 2764 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, &iob, poll_flags); |
a2416e1e PB |
2765 | if (unlikely(ret < 0)) |
2766 | return ret; | |
2767 | else if (ret) | |
ef99b2d3 | 2768 | poll_flags |= BLK_POLL_ONESHOT; |
def596e9 | 2769 | |
3aadc23e | 2770 | /* iopoll may have completed current req */ |
b688f11e JA |
2771 | if (!rq_list_empty(iob.req_list) || |
2772 | READ_ONCE(req->iopoll_completed)) | |
e3f721e6 | 2773 | break; |
def596e9 JA |
2774 | } |
2775 | ||
b688f11e JA |
2776 | if (!rq_list_empty(iob.req_list)) |
2777 | iob.complete(&iob); | |
5eef4e87 PB |
2778 | else if (!pos) |
2779 | return 0; | |
def596e9 | 2780 | |
5eef4e87 PB |
2781 | prev = start; |
2782 | wq_list_for_each_resume(pos, prev) { | |
2783 | struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); | |
2784 | ||
b3fa03fd PB |
2785 | /* order with io_complete_rw_iopoll(), e.g. ->result updates */ |
2786 | if (!smp_load_acquire(&req->iopoll_completed)) | |
e3f721e6 | 2787 | break; |
83a13a41 PB |
2788 | if (unlikely(req->flags & REQ_F_CQE_SKIP)) |
2789 | continue; | |
d1fd1c20 | 2790 | |
ae4da189 | 2791 | __io_fill_cqe_req(req, req->result, io_put_kbuf(req, 0)); |
e3f721e6 PB |
2792 | nr_events++; |
2793 | } | |
def596e9 | 2794 | |
f5ed3bcd PB |
2795 | if (unlikely(!nr_events)) |
2796 | return 0; | |
2797 | ||
2798 | io_commit_cqring(ctx); | |
2799 | io_cqring_ev_posted_iopoll(ctx); | |
1cce17ac | 2800 | pos = start ? start->next : ctx->iopoll_list.first; |
5eef4e87 | 2801 | wq_list_cut(&ctx->iopoll_list, prev, start); |
1cce17ac | 2802 | io_free_batch_list(ctx, pos); |
5ba3c874 | 2803 | return nr_events; |
def596e9 JA |
2804 | } |
2805 | ||
def596e9 JA |
2806 | /* |
2807 | * We can't just wait for polled events to come to us, we have to actively | |
2808 | * find and complete them. | |
2809 | */ | |
c072481d | 2810 | static __cold void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
def596e9 JA |
2811 | { |
2812 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
2813 | return; | |
2814 | ||
2815 | mutex_lock(&ctx->uring_lock); | |
5eef4e87 | 2816 | while (!wq_list_empty(&ctx->iopoll_list)) { |
b2edc0a7 | 2817 | /* let it sleep and repeat later if can't complete a request */ |
5ba3c874 | 2818 | if (io_do_iopoll(ctx, true) == 0) |
b2edc0a7 | 2819 | break; |
08f5439f JA |
2820 | /* |
2821 | * Ensure we allow local-to-the-cpu processing to take place, | |
2822 | * in this case we need to ensure that we reap all events. | |
3fcee5a6 | 2823 | * Also let task_work, etc. to progress by releasing the mutex |
08f5439f | 2824 | */ |
3fcee5a6 PB |
2825 | if (need_resched()) { |
2826 | mutex_unlock(&ctx->uring_lock); | |
2827 | cond_resched(); | |
2828 | mutex_lock(&ctx->uring_lock); | |
2829 | } | |
def596e9 JA |
2830 | } |
2831 | mutex_unlock(&ctx->uring_lock); | |
2832 | } | |
2833 | ||
7668b92a | 2834 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
def596e9 | 2835 | { |
7668b92a | 2836 | unsigned int nr_events = 0; |
e9979b36 | 2837 | int ret = 0; |
500f9fba | 2838 | |
c7849be9 XW |
2839 | /* |
2840 | * We disallow the app entering submit/complete with polling, but we | |
2841 | * still need to lock the ring to prevent racing with polled issue | |
2842 | * that got punted to a workqueue. | |
2843 | */ | |
2844 | mutex_lock(&ctx->uring_lock); | |
f39c8a5b PB |
2845 | /* |
2846 | * Don't enter poll loop if we already have events pending. | |
2847 | * If we do, we can potentially be spinning for commands that | |
2848 | * already triggered a CQE (eg in error). | |
2849 | */ | |
5ed7a37d | 2850 | if (test_bit(0, &ctx->check_cq_overflow)) |
f39c8a5b PB |
2851 | __io_cqring_overflow_flush(ctx, false); |
2852 | if (io_cqring_events(ctx)) | |
2853 | goto out; | |
def596e9 | 2854 | do { |
500f9fba JA |
2855 | /* |
2856 | * If a submit got punted to a workqueue, we can have the | |
2857 | * application entering polling for a command before it gets | |
2858 | * issued. That app will hold the uring_lock for the duration | |
2859 | * of the poll right here, so we need to take a breather every | |
2860 | * now and then to ensure that the issue has a chance to add | |
2861 | * the poll to the issued list. Otherwise we can spin here | |
2862 | * forever, while the workqueue is stuck trying to acquire the | |
2863 | * very same mutex. | |
2864 | */ | |
5eef4e87 | 2865 | if (wq_list_empty(&ctx->iopoll_list)) { |
8f487ef2 PB |
2866 | u32 tail = ctx->cached_cq_tail; |
2867 | ||
500f9fba | 2868 | mutex_unlock(&ctx->uring_lock); |
4c6e277c | 2869 | io_run_task_work(); |
500f9fba | 2870 | mutex_lock(&ctx->uring_lock); |
def596e9 | 2871 | |
8f487ef2 PB |
2872 | /* some requests don't go through iopoll_list */ |
2873 | if (tail != ctx->cached_cq_tail || | |
5eef4e87 | 2874 | wq_list_empty(&ctx->iopoll_list)) |
e9979b36 | 2875 | break; |
500f9fba | 2876 | } |
5ba3c874 PB |
2877 | ret = io_do_iopoll(ctx, !min); |
2878 | if (ret < 0) | |
2879 | break; | |
2880 | nr_events += ret; | |
2881 | ret = 0; | |
2882 | } while (nr_events < min && !need_resched()); | |
f39c8a5b | 2883 | out: |
500f9fba | 2884 | mutex_unlock(&ctx->uring_lock); |
def596e9 JA |
2885 | return ret; |
2886 | } | |
2887 | ||
491381ce | 2888 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 2889 | { |
491381ce JA |
2890 | /* |
2891 | * Tell lockdep we inherited freeze protection from submission | |
2892 | * thread. | |
2893 | */ | |
2894 | if (req->flags & REQ_F_ISREG) { | |
1c98679d | 2895 | struct super_block *sb = file_inode(req->file)->i_sb; |
2b188cc1 | 2896 | |
1c98679d PB |
2897 | __sb_writers_acquired(sb, SB_FREEZE_WRITE); |
2898 | sb_end_write(sb); | |
2b188cc1 JA |
2899 | } |
2900 | } | |
2901 | ||
b63534c4 | 2902 | #ifdef CONFIG_BLOCK |
dc2a6e9a | 2903 | static bool io_resubmit_prep(struct io_kiocb *req) |
b63534c4 | 2904 | { |
ab454438 | 2905 | struct io_async_rw *rw = req->async_data; |
b63534c4 | 2906 | |
d886e185 | 2907 | if (!req_has_async_data(req)) |
ab454438 | 2908 | return !io_req_prep_async(req); |
538941e2 | 2909 | iov_iter_restore(&rw->s.iter, &rw->s.iter_state); |
ab454438 | 2910 | return true; |
b63534c4 | 2911 | } |
b63534c4 | 2912 | |
3e6a0d3c | 2913 | static bool io_rw_should_reissue(struct io_kiocb *req) |
b63534c4 | 2914 | { |
355afaeb | 2915 | umode_t mode = file_inode(req->file)->i_mode; |
3e6a0d3c | 2916 | struct io_ring_ctx *ctx = req->ctx; |
b63534c4 | 2917 | |
355afaeb JA |
2918 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
2919 | return false; | |
3e6a0d3c JA |
2920 | if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() && |
2921 | !(ctx->flags & IORING_SETUP_IOPOLL))) | |
b63534c4 | 2922 | return false; |
7c977a58 JA |
2923 | /* |
2924 | * If ref is dying, we might be running poll reap from the exit work. | |
2925 | * Don't attempt to reissue from that path, just let it fail with | |
2926 | * -EAGAIN. | |
2927 | */ | |
3e6a0d3c JA |
2928 | if (percpu_ref_is_dying(&ctx->refs)) |
2929 | return false; | |
ef046888 JA |
2930 | /* |
2931 | * Play it safe and assume not safe to re-import and reissue if we're | |
2932 | * not in the original thread group (or in task context). | |
2933 | */ | |
2934 | if (!same_thread_group(req->task, current) || !in_task()) | |
2935 | return false; | |
3e6a0d3c JA |
2936 | return true; |
2937 | } | |
e82ad485 | 2938 | #else |
a1ff1e3f | 2939 | static bool io_resubmit_prep(struct io_kiocb *req) |
e82ad485 JA |
2940 | { |
2941 | return false; | |
2942 | } | |
e82ad485 | 2943 | static bool io_rw_should_reissue(struct io_kiocb *req) |
3e6a0d3c | 2944 | { |
b63534c4 JA |
2945 | return false; |
2946 | } | |
3e6a0d3c | 2947 | #endif |
b63534c4 | 2948 | |
8ef12efe | 2949 | static bool __io_complete_rw_common(struct io_kiocb *req, long res) |
a1d7c393 | 2950 | { |
b65c128f PB |
2951 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) |
2952 | kiocb_end_write(req); | |
258f3a7f | 2953 | if (unlikely(res != req->result)) { |
9532b99b PB |
2954 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && |
2955 | io_rw_should_reissue(req)) { | |
2956 | req->flags |= REQ_F_REISSUE; | |
8ef12efe | 2957 | return true; |
9532b99b | 2958 | } |
93d2bcd2 | 2959 | req_set_fail(req); |
8ef12efe | 2960 | req->result = res; |
9532b99b | 2961 | } |
8ef12efe JA |
2962 | return false; |
2963 | } | |
2964 | ||
cc8e9ba7 | 2965 | static inline void io_req_task_complete(struct io_kiocb *req, bool *locked) |
8ef12efe | 2966 | { |
54daa9b2 | 2967 | int res = req->result; |
126180b9 PB |
2968 | |
2969 | if (*locked) { | |
cc3cec83 | 2970 | io_req_complete_state(req, res, io_put_kbuf(req, 0)); |
fff4e40e | 2971 | io_req_add_compl_list(req); |
126180b9 | 2972 | } else { |
cc3cec83 JA |
2973 | io_req_complete_post(req, res, |
2974 | io_put_kbuf(req, IO_URING_F_UNLOCKED)); | |
126180b9 | 2975 | } |
8ef12efe JA |
2976 | } |
2977 | ||
00f6e68b | 2978 | static void __io_complete_rw(struct io_kiocb *req, long res, |
8ef12efe JA |
2979 | unsigned int issue_flags) |
2980 | { | |
2981 | if (__io_complete_rw_common(req, res)) | |
2982 | return; | |
cc3cec83 JA |
2983 | __io_req_complete(req, issue_flags, req->result, |
2984 | io_put_kbuf(req, issue_flags)); | |
ba816ad6 JA |
2985 | } |
2986 | ||
6b19b766 | 2987 | static void io_complete_rw(struct kiocb *kiocb, long res) |
ba816ad6 | 2988 | { |
9adbd45d | 2989 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 | 2990 | |
8ef12efe JA |
2991 | if (__io_complete_rw_common(req, res)) |
2992 | return; | |
2993 | req->result = res; | |
2994 | req->io_task_work.func = io_req_task_complete; | |
f28c240e | 2995 | io_req_task_work_add(req, !!(req->ctx->flags & IORING_SETUP_SQPOLL)); |
2b188cc1 JA |
2996 | } |
2997 | ||
6b19b766 | 2998 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res) |
def596e9 | 2999 | { |
9adbd45d | 3000 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 3001 | |
491381ce JA |
3002 | if (kiocb->ki_flags & IOCB_WRITE) |
3003 | kiocb_end_write(req); | |
9532b99b | 3004 | if (unlikely(res != req->result)) { |
b66ceaf3 PB |
3005 | if (res == -EAGAIN && io_rw_should_reissue(req)) { |
3006 | req->flags |= REQ_F_REISSUE; | |
3007 | return; | |
9532b99b | 3008 | } |
258f3a7f | 3009 | req->result = res; |
8c130827 | 3010 | } |
bbde017a | 3011 | |
b3fa03fd PB |
3012 | /* order with io_iopoll_complete() checking ->iopoll_completed */ |
3013 | smp_store_release(&req->iopoll_completed, 1); | |
def596e9 JA |
3014 | } |
3015 | ||
3016 | /* | |
3017 | * After the iocb has been issued, it's safe to be found on the poll list. | |
3018 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
f39c8a5b | 3019 | * find it from a io_do_iopoll() thread before the issuer is done |
def596e9 JA |
3020 | * accessing the kiocb cookie. |
3021 | */ | |
9882131c | 3022 | static void io_iopoll_req_issued(struct io_kiocb *req, unsigned int issue_flags) |
def596e9 JA |
3023 | { |
3024 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 3025 | const bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
cb3d8972 PB |
3026 | |
3027 | /* workqueue context doesn't hold uring_lock, grab it now */ | |
3b44b371 | 3028 | if (unlikely(needs_lock)) |
cb3d8972 | 3029 | mutex_lock(&ctx->uring_lock); |
def596e9 JA |
3030 | |
3031 | /* | |
3032 | * Track whether we have multiple files in our lists. This will impact | |
3033 | * how we do polling eventually, not spinning if we're on potentially | |
3034 | * different devices. | |
3035 | */ | |
5eef4e87 | 3036 | if (wq_list_empty(&ctx->iopoll_list)) { |
915b3dde HX |
3037 | ctx->poll_multi_queue = false; |
3038 | } else if (!ctx->poll_multi_queue) { | |
def596e9 JA |
3039 | struct io_kiocb *list_req; |
3040 | ||
5eef4e87 PB |
3041 | list_req = container_of(ctx->iopoll_list.first, struct io_kiocb, |
3042 | comp_list); | |
30da1b45 | 3043 | if (list_req->file != req->file) |
915b3dde | 3044 | ctx->poll_multi_queue = true; |
def596e9 JA |
3045 | } |
3046 | ||
3047 | /* | |
3048 | * For fast devices, IO may have already completed. If it has, add | |
3049 | * it to the front so we find it first. | |
3050 | */ | |
65a6543d | 3051 | if (READ_ONCE(req->iopoll_completed)) |
5eef4e87 | 3052 | wq_list_add_head(&req->comp_list, &ctx->iopoll_list); |
def596e9 | 3053 | else |
5eef4e87 | 3054 | wq_list_add_tail(&req->comp_list, &ctx->iopoll_list); |
bdcd3eab | 3055 | |
3b44b371 | 3056 | if (unlikely(needs_lock)) { |
cb3d8972 PB |
3057 | /* |
3058 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handle | |
3059 | * in sq thread task context or in io worker task context. If | |
3060 | * current task context is sq thread, we don't need to check | |
3061 | * whether should wake up sq thread. | |
3062 | */ | |
3063 | if ((ctx->flags & IORING_SETUP_SQPOLL) && | |
3064 | wq_has_sleeper(&ctx->sq_data->wait)) | |
3065 | wake_up(&ctx->sq_data->wait); | |
3066 | ||
3067 | mutex_unlock(&ctx->uring_lock); | |
3068 | } | |
def596e9 JA |
3069 | } |
3070 | ||
4503b767 JA |
3071 | static bool io_bdev_nowait(struct block_device *bdev) |
3072 | { | |
9ba0d0c8 | 3073 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
4503b767 JA |
3074 | } |
3075 | ||
2b188cc1 JA |
3076 | /* |
3077 | * If we tracked the file through the SCM inflight mechanism, we could support | |
3078 | * any file. For now, just ensure that anything potentially problematic is done | |
3079 | * inline. | |
3080 | */ | |
88459b50 | 3081 | static bool __io_file_supports_nowait(struct file *file, umode_t mode) |
2b188cc1 | 3082 | { |
4503b767 | 3083 | if (S_ISBLK(mode)) { |
4e7b5671 CH |
3084 | if (IS_ENABLED(CONFIG_BLOCK) && |
3085 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) | |
4503b767 JA |
3086 | return true; |
3087 | return false; | |
3088 | } | |
976517f1 | 3089 | if (S_ISSOCK(mode)) |
2b188cc1 | 3090 | return true; |
4503b767 | 3091 | if (S_ISREG(mode)) { |
4e7b5671 CH |
3092 | if (IS_ENABLED(CONFIG_BLOCK) && |
3093 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && | |
4503b767 JA |
3094 | file->f_op != &io_uring_fops) |
3095 | return true; | |
3096 | return false; | |
3097 | } | |
2b188cc1 | 3098 | |
c5b85625 JA |
3099 | /* any ->read/write should understand O_NONBLOCK */ |
3100 | if (file->f_flags & O_NONBLOCK) | |
3101 | return true; | |
35645ac3 | 3102 | return file->f_mode & FMODE_NOWAIT; |
2b188cc1 | 3103 | } |
c5b85625 | 3104 | |
88459b50 PB |
3105 | /* |
3106 | * If we tracked the file through the SCM inflight mechanism, we could support | |
3107 | * any file. For now, just ensure that anything potentially problematic is done | |
3108 | * inline. | |
3109 | */ | |
3110 | static unsigned int io_file_get_flags(struct file *file) | |
3111 | { | |
3112 | umode_t mode = file_inode(file)->i_mode; | |
3113 | unsigned int res = 0; | |
af197f50 | 3114 | |
88459b50 PB |
3115 | if (S_ISREG(mode)) |
3116 | res |= FFS_ISREG; | |
3117 | if (__io_file_supports_nowait(file, mode)) | |
3118 | res |= FFS_NOWAIT; | |
3119 | return res; | |
2b188cc1 JA |
3120 | } |
3121 | ||
35645ac3 | 3122 | static inline bool io_file_supports_nowait(struct io_kiocb *req) |
7b29f92d | 3123 | { |
88459b50 | 3124 | return req->flags & REQ_F_SUPPORT_NOWAIT; |
7b29f92d JA |
3125 | } |
3126 | ||
b9a6b8f9 | 3127 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2b188cc1 | 3128 | { |
def596e9 | 3129 | struct io_ring_ctx *ctx = req->ctx; |
9adbd45d | 3130 | struct kiocb *kiocb = &req->rw.kiocb; |
75c668cd | 3131 | struct file *file = req->file; |
09bb8394 JA |
3132 | unsigned ioprio; |
3133 | int ret; | |
2b188cc1 | 3134 | |
88459b50 PB |
3135 | if (!io_req_ffs_set(req)) |
3136 | req->flags |= io_file_get_flags(file) << REQ_F_SUPPORT_NOWAIT_BIT; | |
491381ce | 3137 | |
2b188cc1 | 3138 | kiocb->ki_pos = READ_ONCE(sqe->off); |
5cb03d63 | 3139 | kiocb->ki_flags = iocb_flags(file); |
3e577dcd PB |
3140 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
3141 | if (unlikely(ret)) | |
3142 | return ret; | |
2b188cc1 | 3143 | |
5d329e12 JA |
3144 | /* |
3145 | * If the file is marked O_NONBLOCK, still allow retry for it if it | |
3146 | * supports async. Otherwise it's impossible to use O_NONBLOCK files | |
3147 | * reliably. If not, or it IOCB_NOWAIT is set, don't retry. | |
3148 | */ | |
3149 | if ((kiocb->ki_flags & IOCB_NOWAIT) || | |
35645ac3 | 3150 | ((file->f_flags & O_NONBLOCK) && !io_file_supports_nowait(req))) |
75c668cd PB |
3151 | req->flags |= REQ_F_NOWAIT; |
3152 | ||
def596e9 | 3153 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
5cb03d63 | 3154 | if (!(kiocb->ki_flags & IOCB_DIRECT) || !file->f_op->iopoll) |
09bb8394 | 3155 | return -EOPNOTSUPP; |
2b188cc1 | 3156 | |
394918eb | 3157 | kiocb->ki_flags |= IOCB_HIPRI | IOCB_ALLOC_CACHE; |
def596e9 | 3158 | kiocb->ki_complete = io_complete_rw_iopoll; |
65a6543d | 3159 | req->iopoll_completed = 0; |
def596e9 | 3160 | } else { |
09bb8394 JA |
3161 | if (kiocb->ki_flags & IOCB_HIPRI) |
3162 | return -EINVAL; | |
def596e9 JA |
3163 | kiocb->ki_complete = io_complete_rw; |
3164 | } | |
9adbd45d | 3165 | |
fb27274a PB |
3166 | ioprio = READ_ONCE(sqe->ioprio); |
3167 | if (ioprio) { | |
3168 | ret = ioprio_check_cap(ioprio); | |
3169 | if (ret) | |
3170 | return ret; | |
3171 | ||
3172 | kiocb->ki_ioprio = ioprio; | |
3173 | } else { | |
3174 | kiocb->ki_ioprio = get_current_ioprio(); | |
eae071c9 PB |
3175 | } |
3176 | ||
578c0ee2 | 3177 | req->imu = NULL; |
3529d8c2 JA |
3178 | req->rw.addr = READ_ONCE(sqe->addr); |
3179 | req->rw.len = READ_ONCE(sqe->len); | |
4f4eeba8 | 3180 | req->buf_index = READ_ONCE(sqe->buf_index); |
2b188cc1 | 3181 | return 0; |
2b188cc1 JA |
3182 | } |
3183 | ||
3184 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
3185 | { | |
3186 | switch (ret) { | |
3187 | case -EIOCBQUEUED: | |
3188 | break; | |
3189 | case -ERESTARTSYS: | |
3190 | case -ERESTARTNOINTR: | |
3191 | case -ERESTARTNOHAND: | |
3192 | case -ERESTART_RESTARTBLOCK: | |
3193 | /* | |
3194 | * We can't just restart the syscall, since previously | |
3195 | * submitted sqes may already be in progress. Just fail this | |
3196 | * IO with EINTR. | |
3197 | */ | |
3198 | ret = -EINTR; | |
df561f66 | 3199 | fallthrough; |
2b188cc1 | 3200 | default: |
6b19b766 | 3201 | kiocb->ki_complete(kiocb, ret); |
2b188cc1 JA |
3202 | } |
3203 | } | |
3204 | ||
b4aec400 | 3205 | static inline loff_t *io_kiocb_update_pos(struct io_kiocb *req) |
d34e1e5b DY |
3206 | { |
3207 | struct kiocb *kiocb = &req->rw.kiocb; | |
b4aec400 | 3208 | bool is_stream = req->file->f_mode & FMODE_STREAM; |
d34e1e5b DY |
3209 | |
3210 | if (kiocb->ki_pos == -1) { | |
b4aec400 | 3211 | if (!is_stream) { |
d34e1e5b DY |
3212 | req->flags |= REQ_F_CUR_POS; |
3213 | kiocb->ki_pos = req->file->f_pos; | |
b4aec400 | 3214 | return &kiocb->ki_pos; |
d34e1e5b DY |
3215 | } else { |
3216 | kiocb->ki_pos = 0; | |
b4aec400 | 3217 | return NULL; |
d34e1e5b DY |
3218 | } |
3219 | } | |
b4aec400 | 3220 | return is_stream ? NULL : &kiocb->ki_pos; |
d34e1e5b DY |
3221 | } |
3222 | ||
2ea537ca | 3223 | static void kiocb_done(struct io_kiocb *req, ssize_t ret, |
889fca73 | 3224 | unsigned int issue_flags) |
ba816ad6 | 3225 | { |
e8c2bc1f | 3226 | struct io_async_rw *io = req->async_data; |
ba04291e | 3227 | |
227c0c96 | 3228 | /* add previously done IO, if any */ |
d886e185 | 3229 | if (req_has_async_data(req) && io->bytes_done > 0) { |
227c0c96 | 3230 | if (ret < 0) |
e8c2bc1f | 3231 | ret = io->bytes_done; |
227c0c96 | 3232 | else |
e8c2bc1f | 3233 | ret += io->bytes_done; |
227c0c96 JA |
3234 | } |
3235 | ||
ba04291e | 3236 | if (req->flags & REQ_F_CUR_POS) |
2ea537ca PB |
3237 | req->file->f_pos = req->rw.kiocb.ki_pos; |
3238 | if (ret >= 0 && (req->rw.kiocb.ki_complete == io_complete_rw)) | |
00f6e68b | 3239 | __io_complete_rw(req, ret, issue_flags); |
ba816ad6 | 3240 | else |
2ea537ca | 3241 | io_rw_done(&req->rw.kiocb, ret); |
97284637 | 3242 | |
b66ceaf3 | 3243 | if (req->flags & REQ_F_REISSUE) { |
97284637 | 3244 | req->flags &= ~REQ_F_REISSUE; |
a7be7c23 | 3245 | if (io_resubmit_prep(req)) { |
773af691 | 3246 | io_req_task_queue_reissue(req); |
8c130827 | 3247 | } else { |
93d2bcd2 | 3248 | req_set_fail(req); |
06bdea20 PB |
3249 | req->result = ret; |
3250 | req->io_task_work.func = io_req_task_complete; | |
4813c377 | 3251 | io_req_task_work_add(req, false); |
97284637 PB |
3252 | } |
3253 | } | |
ba816ad6 JA |
3254 | } |
3255 | ||
eae071c9 PB |
3256 | static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter, |
3257 | struct io_mapped_ubuf *imu) | |
edafccee | 3258 | { |
9adbd45d | 3259 | size_t len = req->rw.len; |
75769e3f | 3260 | u64 buf_end, buf_addr = req->rw.addr; |
edafccee | 3261 | size_t offset; |
edafccee | 3262 | |
75769e3f | 3263 | if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end))) |
edafccee JA |
3264 | return -EFAULT; |
3265 | /* not inside the mapped region */ | |
4751f53d | 3266 | if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end)) |
edafccee JA |
3267 | return -EFAULT; |
3268 | ||
3269 | /* | |
3270 | * May not be a start of buffer, set size appropriately | |
3271 | * and advance us to the beginning. | |
3272 | */ | |
3273 | offset = buf_addr - imu->ubuf; | |
3274 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
3275 | |
3276 | if (offset) { | |
3277 | /* | |
3278 | * Don't use iov_iter_advance() here, as it's really slow for | |
3279 | * using the latter parts of a big fixed buffer - it iterates | |
3280 | * over each segment manually. We can cheat a bit here, because | |
3281 | * we know that: | |
3282 | * | |
3283 | * 1) it's a BVEC iter, we set it up | |
3284 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
3285 | * first and last bvec | |
3286 | * | |
3287 | * So just find our index, and adjust the iterator afterwards. | |
3288 | * If the offset is within the first bvec (or the whole first | |
3289 | * bvec, just use iov_iter_advance(). This makes it easier | |
3290 | * since we can just skip the first segment, which may not | |
3291 | * be PAGE_SIZE aligned. | |
3292 | */ | |
3293 | const struct bio_vec *bvec = imu->bvec; | |
3294 | ||
3295 | if (offset <= bvec->bv_len) { | |
3296 | iov_iter_advance(iter, offset); | |
3297 | } else { | |
3298 | unsigned long seg_skip; | |
3299 | ||
3300 | /* skip first vec */ | |
3301 | offset -= bvec->bv_len; | |
3302 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
3303 | ||
3304 | iter->bvec = bvec + seg_skip; | |
3305 | iter->nr_segs -= seg_skip; | |
99c79f66 | 3306 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 3307 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
3308 | } |
3309 | } | |
3310 | ||
847595de | 3311 | return 0; |
edafccee JA |
3312 | } |
3313 | ||
eae071c9 PB |
3314 | static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter) |
3315 | { | |
eae071c9 PB |
3316 | struct io_mapped_ubuf *imu = req->imu; |
3317 | u16 index, buf_index = req->buf_index; | |
3318 | ||
3319 | if (likely(!imu)) { | |
578c0ee2 PB |
3320 | struct io_ring_ctx *ctx = req->ctx; |
3321 | ||
eae071c9 PB |
3322 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
3323 | return -EFAULT; | |
578c0ee2 | 3324 | io_req_set_rsrc_node(req, ctx); |
eae071c9 PB |
3325 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
3326 | imu = READ_ONCE(ctx->user_bufs[index]); | |
3327 | req->imu = imu; | |
3328 | } | |
3329 | return __io_import_fixed(req, rw, iter, imu); | |
3330 | } | |
3331 | ||
bcda7baa JA |
3332 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
3333 | { | |
3334 | if (needs_lock) | |
3335 | mutex_unlock(&ctx->uring_lock); | |
3336 | } | |
3337 | ||
3338 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) | |
3339 | { | |
3340 | /* | |
3341 | * "Normal" inline submissions always hold the uring_lock, since we | |
3342 | * grab it from the system call. Same is true for the SQPOLL offload. | |
3343 | * The only exception is when we've detached the request and issue it | |
3344 | * from an async worker thread, grab the lock for that case. | |
3345 | */ | |
3346 | if (needs_lock) | |
3347 | mutex_lock(&ctx->uring_lock); | |
3348 | } | |
3349 | ||
3350 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, | |
51aac424 | 3351 | int bgid, unsigned int issue_flags) |
bcda7baa | 3352 | { |
30d51dd4 | 3353 | struct io_buffer *kbuf = req->kbuf; |
bcda7baa | 3354 | struct io_buffer *head; |
3b44b371 | 3355 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
bcda7baa JA |
3356 | |
3357 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
3358 | return kbuf; | |
3359 | ||
3360 | io_ring_submit_lock(req->ctx, needs_lock); | |
3361 | ||
3362 | lockdep_assert_held(&req->ctx->uring_lock); | |
3363 | ||
9e15c3a0 | 3364 | head = xa_load(&req->ctx->io_buffers, bgid); |
bcda7baa JA |
3365 | if (head) { |
3366 | if (!list_empty(&head->list)) { | |
3367 | kbuf = list_last_entry(&head->list, struct io_buffer, | |
3368 | list); | |
3369 | list_del(&kbuf->list); | |
3370 | } else { | |
3371 | kbuf = head; | |
9e15c3a0 | 3372 | xa_erase(&req->ctx->io_buffers, bgid); |
bcda7baa JA |
3373 | } |
3374 | if (*len > kbuf->len) | |
3375 | *len = kbuf->len; | |
30d51dd4 PB |
3376 | req->flags |= REQ_F_BUFFER_SELECTED; |
3377 | req->kbuf = kbuf; | |
bcda7baa JA |
3378 | } else { |
3379 | kbuf = ERR_PTR(-ENOBUFS); | |
3380 | } | |
3381 | ||
3382 | io_ring_submit_unlock(req->ctx, needs_lock); | |
bcda7baa JA |
3383 | return kbuf; |
3384 | } | |
3385 | ||
4d954c25 | 3386 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
51aac424 | 3387 | unsigned int issue_flags) |
4d954c25 JA |
3388 | { |
3389 | struct io_buffer *kbuf; | |
4f4eeba8 | 3390 | u16 bgid; |
4d954c25 | 3391 | |
4f4eeba8 | 3392 | bgid = req->buf_index; |
51aac424 | 3393 | kbuf = io_buffer_select(req, len, bgid, issue_flags); |
4d954c25 JA |
3394 | if (IS_ERR(kbuf)) |
3395 | return kbuf; | |
4d954c25 JA |
3396 | return u64_to_user_ptr(kbuf->addr); |
3397 | } | |
3398 | ||
3399 | #ifdef CONFIG_COMPAT | |
3400 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, | |
51aac424 | 3401 | unsigned int issue_flags) |
4d954c25 JA |
3402 | { |
3403 | struct compat_iovec __user *uiov; | |
3404 | compat_ssize_t clen; | |
3405 | void __user *buf; | |
3406 | ssize_t len; | |
3407 | ||
3408 | uiov = u64_to_user_ptr(req->rw.addr); | |
3409 | if (!access_ok(uiov, sizeof(*uiov))) | |
3410 | return -EFAULT; | |
3411 | if (__get_user(clen, &uiov->iov_len)) | |
3412 | return -EFAULT; | |
3413 | if (clen < 0) | |
3414 | return -EINVAL; | |
3415 | ||
3416 | len = clen; | |
51aac424 | 3417 | buf = io_rw_buffer_select(req, &len, issue_flags); |
4d954c25 JA |
3418 | if (IS_ERR(buf)) |
3419 | return PTR_ERR(buf); | |
3420 | iov[0].iov_base = buf; | |
3421 | iov[0].iov_len = (compat_size_t) len; | |
3422 | return 0; | |
3423 | } | |
3424 | #endif | |
3425 | ||
3426 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
51aac424 | 3427 | unsigned int issue_flags) |
4d954c25 JA |
3428 | { |
3429 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); | |
3430 | void __user *buf; | |
3431 | ssize_t len; | |
3432 | ||
3433 | if (copy_from_user(iov, uiov, sizeof(*uiov))) | |
3434 | return -EFAULT; | |
3435 | ||
3436 | len = iov[0].iov_len; | |
3437 | if (len < 0) | |
3438 | return -EINVAL; | |
51aac424 | 3439 | buf = io_rw_buffer_select(req, &len, issue_flags); |
4d954c25 JA |
3440 | if (IS_ERR(buf)) |
3441 | return PTR_ERR(buf); | |
3442 | iov[0].iov_base = buf; | |
3443 | iov[0].iov_len = len; | |
3444 | return 0; | |
3445 | } | |
3446 | ||
3447 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
51aac424 | 3448 | unsigned int issue_flags) |
4d954c25 | 3449 | { |
dddb3e26 | 3450 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
30d51dd4 | 3451 | struct io_buffer *kbuf = req->kbuf; |
dddb3e26 | 3452 | |
dddb3e26 JA |
3453 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
3454 | iov[0].iov_len = kbuf->len; | |
4d954c25 | 3455 | return 0; |
dddb3e26 | 3456 | } |
dd201662 | 3457 | if (req->rw.len != 1) |
4d954c25 JA |
3458 | return -EINVAL; |
3459 | ||
3460 | #ifdef CONFIG_COMPAT | |
3461 | if (req->ctx->compat) | |
51aac424 | 3462 | return io_compat_import(req, iov, issue_flags); |
4d954c25 JA |
3463 | #endif |
3464 | ||
51aac424 | 3465 | return __io_iov_buffer_select(req, iov, issue_flags); |
4d954c25 JA |
3466 | } |
3467 | ||
caa8fe6e PB |
3468 | static struct iovec *__io_import_iovec(int rw, struct io_kiocb *req, |
3469 | struct io_rw_state *s, | |
3470 | unsigned int issue_flags) | |
2b188cc1 | 3471 | { |
5e49c973 | 3472 | struct iov_iter *iter = &s->iter; |
847595de | 3473 | u8 opcode = req->opcode; |
caa8fe6e | 3474 | struct iovec *iovec; |
d1d681b0 PB |
3475 | void __user *buf; |
3476 | size_t sqe_len; | |
4d954c25 | 3477 | ssize_t ret; |
edafccee | 3478 | |
f3251183 PB |
3479 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
3480 | ret = io_import_fixed(req, rw, iter); | |
3481 | if (ret) | |
3482 | return ERR_PTR(ret); | |
3483 | return NULL; | |
3484 | } | |
2b188cc1 | 3485 | |
bcda7baa | 3486 | /* buffer index only valid with fixed read/write, or buffer select */ |
d1d681b0 | 3487 | if (unlikely(req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))) |
caa8fe6e | 3488 | return ERR_PTR(-EINVAL); |
9adbd45d | 3489 | |
d1d681b0 PB |
3490 | buf = u64_to_user_ptr(req->rw.addr); |
3491 | sqe_len = req->rw.len; | |
9adbd45d | 3492 | |
3a6820f2 | 3493 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
bcda7baa | 3494 | if (req->flags & REQ_F_BUFFER_SELECT) { |
51aac424 | 3495 | buf = io_rw_buffer_select(req, &sqe_len, issue_flags); |
867a23ea | 3496 | if (IS_ERR(buf)) |
898df244 | 3497 | return ERR_CAST(buf); |
3f9d6441 | 3498 | req->rw.len = sqe_len; |
bcda7baa JA |
3499 | } |
3500 | ||
5e49c973 | 3501 | ret = import_single_range(rw, buf, sqe_len, s->fast_iov, iter); |
f3251183 PB |
3502 | if (ret) |
3503 | return ERR_PTR(ret); | |
3504 | return NULL; | |
3a6820f2 JA |
3505 | } |
3506 | ||
caa8fe6e | 3507 | iovec = s->fast_iov; |
4d954c25 | 3508 | if (req->flags & REQ_F_BUFFER_SELECT) { |
caa8fe6e | 3509 | ret = io_iov_buffer_select(req, iovec, issue_flags); |
f3251183 PB |
3510 | if (ret) |
3511 | return ERR_PTR(ret); | |
3512 | iov_iter_init(iter, rw, iovec, 1, iovec->iov_len); | |
3513 | return NULL; | |
4d954c25 JA |
3514 | } |
3515 | ||
caa8fe6e | 3516 | ret = __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, &iovec, iter, |
89cd35c5 | 3517 | req->ctx->compat); |
caa8fe6e PB |
3518 | if (unlikely(ret < 0)) |
3519 | return ERR_PTR(ret); | |
3520 | return iovec; | |
2b188cc1 JA |
3521 | } |
3522 | ||
5e49c973 PB |
3523 | static inline int io_import_iovec(int rw, struct io_kiocb *req, |
3524 | struct iovec **iovec, struct io_rw_state *s, | |
3525 | unsigned int issue_flags) | |
3526 | { | |
caa8fe6e PB |
3527 | *iovec = __io_import_iovec(rw, req, s, issue_flags); |
3528 | if (unlikely(IS_ERR(*iovec))) | |
3529 | return PTR_ERR(*iovec); | |
5e49c973 | 3530 | |
5e49c973 | 3531 | iov_iter_save_state(&s->iter, &s->iter_state); |
caa8fe6e | 3532 | return 0; |
2b188cc1 JA |
3533 | } |
3534 | ||
0fef9483 JA |
3535 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
3536 | { | |
5b09e37e | 3537 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
0fef9483 JA |
3538 | } |
3539 | ||
31b51510 | 3540 | /* |
32960613 JA |
3541 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
3542 | * by looping over ->read() or ->write() manually. | |
31b51510 | 3543 | */ |
4017eb91 | 3544 | static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) |
32960613 | 3545 | { |
4017eb91 JA |
3546 | struct kiocb *kiocb = &req->rw.kiocb; |
3547 | struct file *file = req->file; | |
32960613 | 3548 | ssize_t ret = 0; |
af9c45ec | 3549 | loff_t *ppos; |
32960613 JA |
3550 | |
3551 | /* | |
3552 | * Don't support polled IO through this interface, and we can't | |
3553 | * support non-blocking either. For the latter, this just causes | |
3554 | * the kiocb to be handled from an async context. | |
3555 | */ | |
3556 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3557 | return -EOPNOTSUPP; | |
35645ac3 PB |
3558 | if ((kiocb->ki_flags & IOCB_NOWAIT) && |
3559 | !(kiocb->ki_filp->f_flags & O_NONBLOCK)) | |
32960613 JA |
3560 | return -EAGAIN; |
3561 | ||
af9c45ec DY |
3562 | ppos = io_kiocb_ppos(kiocb); |
3563 | ||
32960613 | 3564 | while (iov_iter_count(iter)) { |
311ae9e1 | 3565 | struct iovec iovec; |
32960613 JA |
3566 | ssize_t nr; |
3567 | ||
311ae9e1 PB |
3568 | if (!iov_iter_is_bvec(iter)) { |
3569 | iovec = iov_iter_iovec(iter); | |
3570 | } else { | |
4017eb91 JA |
3571 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
3572 | iovec.iov_len = req->rw.len; | |
311ae9e1 PB |
3573 | } |
3574 | ||
32960613 JA |
3575 | if (rw == READ) { |
3576 | nr = file->f_op->read(file, iovec.iov_base, | |
af9c45ec | 3577 | iovec.iov_len, ppos); |
32960613 JA |
3578 | } else { |
3579 | nr = file->f_op->write(file, iovec.iov_base, | |
af9c45ec | 3580 | iovec.iov_len, ppos); |
32960613 JA |
3581 | } |
3582 | ||
3583 | if (nr < 0) { | |
3584 | if (!ret) | |
3585 | ret = nr; | |
3586 | break; | |
3587 | } | |
16c8d2df JA |
3588 | if (!iov_iter_is_bvec(iter)) { |
3589 | iov_iter_advance(iter, nr); | |
3590 | } else { | |
3591 | req->rw.len -= nr; | |
3592 | req->rw.addr += nr; | |
3593 | } | |
32960613 JA |
3594 | ret += nr; |
3595 | if (nr != iovec.iov_len) | |
3596 | break; | |
32960613 JA |
3597 | } |
3598 | ||
3599 | return ret; | |
3600 | } | |
3601 | ||
ff6165b2 JA |
3602 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
3603 | const struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 3604 | { |
e8c2bc1f | 3605 | struct io_async_rw *rw = req->async_data; |
b64e3444 | 3606 | |
538941e2 | 3607 | memcpy(&rw->s.iter, iter, sizeof(*iter)); |
afb87658 | 3608 | rw->free_iovec = iovec; |
227c0c96 | 3609 | rw->bytes_done = 0; |
ff6165b2 | 3610 | /* can only be fixed buffers, no need to do anything */ |
9c3a205c | 3611 | if (iov_iter_is_bvec(iter)) |
ff6165b2 | 3612 | return; |
b64e3444 | 3613 | if (!iovec) { |
ff6165b2 JA |
3614 | unsigned iov_off = 0; |
3615 | ||
538941e2 | 3616 | rw->s.iter.iov = rw->s.fast_iov; |
ff6165b2 JA |
3617 | if (iter->iov != fast_iov) { |
3618 | iov_off = iter->iov - fast_iov; | |
538941e2 | 3619 | rw->s.iter.iov += iov_off; |
ff6165b2 | 3620 | } |
538941e2 PB |
3621 | if (rw->s.fast_iov != fast_iov) |
3622 | memcpy(rw->s.fast_iov + iov_off, fast_iov + iov_off, | |
45097dae | 3623 | sizeof(struct iovec) * iter->nr_segs); |
99bc4c38 PB |
3624 | } else { |
3625 | req->flags |= REQ_F_NEED_CLEANUP; | |
f67676d1 JA |
3626 | } |
3627 | } | |
3628 | ||
8d4af685 | 3629 | static inline bool io_alloc_async_data(struct io_kiocb *req) |
3d9932a8 | 3630 | { |
e8c2bc1f JA |
3631 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
3632 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); | |
d886e185 PB |
3633 | if (req->async_data) { |
3634 | req->flags |= REQ_F_ASYNC_DATA; | |
3635 | return false; | |
3636 | } | |
3637 | return true; | |
3d9932a8 XW |
3638 | } |
3639 | ||
ff6165b2 | 3640 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
c88598a9 | 3641 | struct io_rw_state *s, bool force) |
b7bb4f7d | 3642 | { |
26f0505a | 3643 | if (!force && !io_op_defs[req->opcode].needs_async_setup) |
74566df3 | 3644 | return 0; |
d886e185 | 3645 | if (!req_has_async_data(req)) { |
cd658695 JA |
3646 | struct io_async_rw *iorw; |
3647 | ||
6cb78689 | 3648 | if (io_alloc_async_data(req)) { |
6bf985dc | 3649 | kfree(iovec); |
5d204bcf | 3650 | return -ENOMEM; |
6bf985dc | 3651 | } |
b7bb4f7d | 3652 | |
c88598a9 | 3653 | io_req_map_rw(req, iovec, s->fast_iov, &s->iter); |
cd658695 JA |
3654 | iorw = req->async_data; |
3655 | /* we've copied and mapped the iter, ensure state is saved */ | |
538941e2 | 3656 | iov_iter_save_state(&iorw->s.iter, &iorw->s.iter_state); |
5d204bcf | 3657 | } |
b7bb4f7d | 3658 | return 0; |
f67676d1 JA |
3659 | } |
3660 | ||
73debe68 | 3661 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw) |
c3e330a4 | 3662 | { |
e8c2bc1f | 3663 | struct io_async_rw *iorw = req->async_data; |
5e49c973 | 3664 | struct iovec *iov; |
847595de | 3665 | int ret; |
c3e330a4 | 3666 | |
51aac424 | 3667 | /* submission path, ->uring_lock should already be taken */ |
3b44b371 | 3668 | ret = io_import_iovec(rw, req, &iov, &iorw->s, 0); |
c3e330a4 PB |
3669 | if (unlikely(ret < 0)) |
3670 | return ret; | |
3671 | ||
ab0b196c PB |
3672 | iorw->bytes_done = 0; |
3673 | iorw->free_iovec = iov; | |
3674 | if (iov) | |
3675 | req->flags |= REQ_F_NEED_CLEANUP; | |
c3e330a4 PB |
3676 | return 0; |
3677 | } | |
3678 | ||
73debe68 | 3679 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3680 | { |
3529d8c2 JA |
3681 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
3682 | return -EBADF; | |
b9a6b8f9 | 3683 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3684 | } |
3685 | ||
c1dd91d1 | 3686 | /* |
ffdc8dab | 3687 | * This is our waitqueue callback handler, registered through __folio_lock_async() |
c1dd91d1 JA |
3688 | * when we initially tried to do the IO with the iocb armed our waitqueue. |
3689 | * This gets called when the page is unlocked, and we generally expect that to | |
3690 | * happen when the page IO is completed and the page is now uptodate. This will | |
3691 | * queue a task_work based retry of the operation, attempting to copy the data | |
3692 | * again. If the latter fails because the page was NOT uptodate, then we will | |
3693 | * do a thread based blocking retry of the operation. That's the unexpected | |
3694 | * slow path. | |
3695 | */ | |
bcf5a063 JA |
3696 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
3697 | int sync, void *arg) | |
3698 | { | |
3699 | struct wait_page_queue *wpq; | |
3700 | struct io_kiocb *req = wait->private; | |
bcf5a063 | 3701 | struct wait_page_key *key = arg; |
bcf5a063 JA |
3702 | |
3703 | wpq = container_of(wait, struct wait_page_queue, wait); | |
3704 | ||
cdc8fcb4 LT |
3705 | if (!wake_page_match(wpq, key)) |
3706 | return 0; | |
3707 | ||
c8d317aa | 3708 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
bcf5a063 | 3709 | list_del_init(&wait->entry); |
921b9054 | 3710 | io_req_task_queue(req); |
bcf5a063 JA |
3711 | return 1; |
3712 | } | |
3713 | ||
c1dd91d1 JA |
3714 | /* |
3715 | * This controls whether a given IO request should be armed for async page | |
3716 | * based retry. If we return false here, the request is handed to the async | |
3717 | * worker threads for retry. If we're doing buffered reads on a regular file, | |
3718 | * we prepare a private wait_page_queue entry and retry the operation. This | |
3719 | * will either succeed because the page is now uptodate and unlocked, or it | |
3720 | * will register a callback when the page is unlocked at IO completion. Through | |
3721 | * that callback, io_uring uses task_work to setup a retry of the operation. | |
3722 | * That retry will attempt the buffered read again. The retry will generally | |
3723 | * succeed, or in rare cases where it fails, we then fall back to using the | |
3724 | * async worker threads for a blocking retry. | |
3725 | */ | |
227c0c96 | 3726 | static bool io_rw_should_retry(struct io_kiocb *req) |
f67676d1 | 3727 | { |
e8c2bc1f JA |
3728 | struct io_async_rw *rw = req->async_data; |
3729 | struct wait_page_queue *wait = &rw->wpq; | |
bcf5a063 | 3730 | struct kiocb *kiocb = &req->rw.kiocb; |
f67676d1 | 3731 | |
bcf5a063 JA |
3732 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
3733 | if (req->flags & REQ_F_NOWAIT) | |
3734 | return false; | |
f67676d1 | 3735 | |
227c0c96 | 3736 | /* Only for buffered IO */ |
3b2a4439 | 3737 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
bcf5a063 | 3738 | return false; |
3b2a4439 | 3739 | |
bcf5a063 JA |
3740 | /* |
3741 | * just use poll if we can, and don't attempt if the fs doesn't | |
3742 | * support callback based unlocks | |
3743 | */ | |
3744 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) | |
3745 | return false; | |
f67676d1 | 3746 | |
3b2a4439 JA |
3747 | wait->wait.func = io_async_buf_func; |
3748 | wait->wait.private = req; | |
3749 | wait->wait.flags = 0; | |
3750 | INIT_LIST_HEAD(&wait->wait.entry); | |
3751 | kiocb->ki_flags |= IOCB_WAITQ; | |
c8d317aa | 3752 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3b2a4439 | 3753 | kiocb->ki_waitq = wait; |
3b2a4439 | 3754 | return true; |
bcf5a063 JA |
3755 | } |
3756 | ||
aeab9506 | 3757 | static inline int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) |
bcf5a063 | 3758 | { |
607b6fb8 | 3759 | if (likely(req->file->f_op->read_iter)) |
bcf5a063 | 3760 | return call_read_iter(req->file, &req->rw.kiocb, iter); |
2dd2111d | 3761 | else if (req->file->f_op->read) |
4017eb91 | 3762 | return loop_rw_iter(READ, req, iter); |
2dd2111d GH |
3763 | else |
3764 | return -EINVAL; | |
f67676d1 JA |
3765 | } |
3766 | ||
7db30437 ML |
3767 | static bool need_read_all(struct io_kiocb *req) |
3768 | { | |
3769 | return req->flags & REQ_F_ISREG || | |
3770 | S_ISBLK(file_inode(req->file)->i_mode); | |
3771 | } | |
3772 | ||
889fca73 | 3773 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 3774 | { |
607b6fb8 | 3775 | struct io_rw_state __s, *s = &__s; |
c88598a9 | 3776 | struct iovec *iovec; |
9adbd45d | 3777 | struct kiocb *kiocb = &req->rw.kiocb; |
45d189c6 | 3778 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
d886e185 | 3779 | struct io_async_rw *rw; |
cd658695 | 3780 | ssize_t ret, ret2; |
b4aec400 | 3781 | loff_t *ppos; |
ff6165b2 | 3782 | |
607b6fb8 PB |
3783 | if (!req_has_async_data(req)) { |
3784 | ret = io_import_iovec(READ, req, &iovec, s, issue_flags); | |
3785 | if (unlikely(ret < 0)) | |
3786 | return ret; | |
3787 | } else { | |
2be2eb02 JA |
3788 | /* |
3789 | * Safe and required to re-import if we're using provided | |
3790 | * buffers, as we dropped the selected one before retry. | |
3791 | */ | |
3792 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
3793 | ret = io_import_iovec(READ, req, &iovec, s, issue_flags); | |
3794 | if (unlikely(ret < 0)) | |
3795 | return ret; | |
3796 | } | |
3797 | ||
d886e185 | 3798 | rw = req->async_data; |
c88598a9 | 3799 | s = &rw->s; |
cd658695 JA |
3800 | /* |
3801 | * We come here from an earlier attempt, restore our state to | |
3802 | * match in case it doesn't. It's cheap enough that we don't | |
3803 | * need to make this conditional. | |
3804 | */ | |
c88598a9 | 3805 | iov_iter_restore(&s->iter, &s->iter_state); |
2846c481 | 3806 | iovec = NULL; |
2846c481 | 3807 | } |
c88598a9 | 3808 | req->result = iov_iter_count(&s->iter); |
2b188cc1 | 3809 | |
607b6fb8 PB |
3810 | if (force_nonblock) { |
3811 | /* If the file doesn't support async, just async punt */ | |
35645ac3 | 3812 | if (unlikely(!io_file_supports_nowait(req))) { |
607b6fb8 PB |
3813 | ret = io_setup_async_rw(req, iovec, s, true); |
3814 | return ret ?: -EAGAIN; | |
3815 | } | |
a88fc400 | 3816 | kiocb->ki_flags |= IOCB_NOWAIT; |
607b6fb8 PB |
3817 | } else { |
3818 | /* Ensure we clear previously set non-block flag */ | |
3819 | kiocb->ki_flags &= ~IOCB_NOWAIT; | |
6713e7a6 | 3820 | } |
9e645e11 | 3821 | |
b4aec400 | 3822 | ppos = io_kiocb_update_pos(req); |
d34e1e5b | 3823 | |
b4aec400 | 3824 | ret = rw_verify_area(READ, req->file, ppos, req->result); |
5ea5dd45 PB |
3825 | if (unlikely(ret)) { |
3826 | kfree(iovec); | |
3827 | return ret; | |
3828 | } | |
2b188cc1 | 3829 | |
c88598a9 | 3830 | ret = io_iter_do_read(req, &s->iter); |
32960613 | 3831 | |
230d50d4 | 3832 | if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) { |
6ad7f233 | 3833 | req->flags &= ~REQ_F_REISSUE; |
9af177ee JA |
3834 | /* if we can poll, just do that */ |
3835 | if (req->opcode == IORING_OP_READ && file_can_poll(req->file)) | |
3836 | return -EAGAIN; | |
eefdf30f JA |
3837 | /* IOPOLL retry should happen for io-wq threads */ |
3838 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
f91daf56 | 3839 | goto done; |
75c668cd PB |
3840 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3841 | if (req->flags & REQ_F_NOWAIT) | |
355afaeb | 3842 | goto done; |
f38c7e3a | 3843 | ret = 0; |
230d50d4 JA |
3844 | } else if (ret == -EIOCBQUEUED) { |
3845 | goto out_free; | |
f80a50a6 | 3846 | } else if (ret == req->result || ret <= 0 || !force_nonblock || |
7db30437 | 3847 | (req->flags & REQ_F_NOWAIT) || !need_read_all(req)) { |
7335e3bf | 3848 | /* read all, failed, already did sync or don't want to retry */ |
00d23d51 | 3849 | goto done; |
227c0c96 JA |
3850 | } |
3851 | ||
cd658695 JA |
3852 | /* |
3853 | * Don't depend on the iter state matching what was consumed, or being | |
3854 | * untouched in case of error. Restore it and we'll advance it | |
3855 | * manually if we need to. | |
3856 | */ | |
c88598a9 | 3857 | iov_iter_restore(&s->iter, &s->iter_state); |
cd658695 | 3858 | |
c88598a9 | 3859 | ret2 = io_setup_async_rw(req, iovec, s, true); |
6bf985dc PB |
3860 | if (ret2) |
3861 | return ret2; | |
3862 | ||
fe1cdd55 | 3863 | iovec = NULL; |
e8c2bc1f | 3864 | rw = req->async_data; |
c88598a9 | 3865 | s = &rw->s; |
cd658695 JA |
3866 | /* |
3867 | * Now use our persistent iterator and state, if we aren't already. | |
3868 | * We've restored and mapped the iter to match. | |
3869 | */ | |
227c0c96 | 3870 | |
b23df91b | 3871 | do { |
cd658695 JA |
3872 | /* |
3873 | * We end up here because of a partial read, either from | |
3874 | * above or inside this loop. Advance the iter by the bytes | |
3875 | * that were consumed. | |
3876 | */ | |
c88598a9 PB |
3877 | iov_iter_advance(&s->iter, ret); |
3878 | if (!iov_iter_count(&s->iter)) | |
cd658695 | 3879 | break; |
b23df91b | 3880 | rw->bytes_done += ret; |
c88598a9 | 3881 | iov_iter_save_state(&s->iter, &s->iter_state); |
cd658695 | 3882 | |
b23df91b PB |
3883 | /* if we can retry, do so with the callbacks armed */ |
3884 | if (!io_rw_should_retry(req)) { | |
3885 | kiocb->ki_flags &= ~IOCB_WAITQ; | |
3886 | return -EAGAIN; | |
3887 | } | |
3888 | ||
3889 | /* | |
3890 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If | |
3891 | * we get -EIOCBQUEUED, then we'll get a notification when the | |
3892 | * desired page gets unlocked. We can also get a partial read | |
3893 | * here, and if we do, then just retry at the new offset. | |
3894 | */ | |
c88598a9 | 3895 | ret = io_iter_do_read(req, &s->iter); |
b23df91b PB |
3896 | if (ret == -EIOCBQUEUED) |
3897 | return 0; | |
227c0c96 | 3898 | /* we got some bytes, but not all. retry. */ |
b5b0ecb7 | 3899 | kiocb->ki_flags &= ~IOCB_WAITQ; |
c88598a9 | 3900 | iov_iter_restore(&s->iter, &s->iter_state); |
cd658695 | 3901 | } while (ret > 0); |
227c0c96 | 3902 | done: |
2ea537ca | 3903 | kiocb_done(req, ret, issue_flags); |
fe1cdd55 PB |
3904 | out_free: |
3905 | /* it's faster to check here then delegate to kfree */ | |
3906 | if (iovec) | |
3907 | kfree(iovec); | |
5ea5dd45 | 3908 | return 0; |
2b188cc1 JA |
3909 | } |
3910 | ||
73debe68 | 3911 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3912 | { |
3529d8c2 JA |
3913 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
3914 | return -EBADF; | |
3884b83d | 3915 | req->rw.kiocb.ki_hint = ki_hint_validate(file_write_hint(req->file)); |
b9a6b8f9 | 3916 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3917 | } |
3918 | ||
889fca73 | 3919 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 3920 | { |
607b6fb8 | 3921 | struct io_rw_state __s, *s = &__s; |
c88598a9 | 3922 | struct iovec *iovec; |
9adbd45d | 3923 | struct kiocb *kiocb = &req->rw.kiocb; |
45d189c6 | 3924 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
cd658695 | 3925 | ssize_t ret, ret2; |
b4aec400 | 3926 | loff_t *ppos; |
2b188cc1 | 3927 | |
607b6fb8 | 3928 | if (!req_has_async_data(req)) { |
5e49c973 PB |
3929 | ret = io_import_iovec(WRITE, req, &iovec, s, issue_flags); |
3930 | if (unlikely(ret < 0)) | |
2846c481 | 3931 | return ret; |
607b6fb8 PB |
3932 | } else { |
3933 | struct io_async_rw *rw = req->async_data; | |
3934 | ||
3935 | s = &rw->s; | |
3936 | iov_iter_restore(&s->iter, &s->iter_state); | |
2846c481 | 3937 | iovec = NULL; |
2846c481 | 3938 | } |
c88598a9 | 3939 | req->result = iov_iter_count(&s->iter); |
2b188cc1 | 3940 | |
607b6fb8 PB |
3941 | if (force_nonblock) { |
3942 | /* If the file doesn't support async, just async punt */ | |
35645ac3 | 3943 | if (unlikely(!io_file_supports_nowait(req))) |
607b6fb8 | 3944 | goto copy_iov; |
fd6c2e4c | 3945 | |
607b6fb8 PB |
3946 | /* file path doesn't support NOWAIT for non-direct_IO */ |
3947 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
3948 | (req->flags & REQ_F_ISREG)) | |
3949 | goto copy_iov; | |
31b51510 | 3950 | |
607b6fb8 PB |
3951 | kiocb->ki_flags |= IOCB_NOWAIT; |
3952 | } else { | |
3953 | /* Ensure we clear previously set non-block flag */ | |
3954 | kiocb->ki_flags &= ~IOCB_NOWAIT; | |
3955 | } | |
31b51510 | 3956 | |
b4aec400 | 3957 | ppos = io_kiocb_update_pos(req); |
d34e1e5b | 3958 | |
b4aec400 | 3959 | ret = rw_verify_area(WRITE, req->file, ppos, req->result); |
fa15bafb PB |
3960 | if (unlikely(ret)) |
3961 | goto out_free; | |
4ed734b0 | 3962 | |
fa15bafb PB |
3963 | /* |
3964 | * Open-code file_start_write here to grab freeze protection, | |
3965 | * which will be released by another thread in | |
3966 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
3967 | * released so that it doesn't complain about the held lock when | |
3968 | * we return to userspace. | |
3969 | */ | |
3970 | if (req->flags & REQ_F_ISREG) { | |
8a3c84b6 | 3971 | sb_start_write(file_inode(req->file)->i_sb); |
fa15bafb PB |
3972 | __sb_writers_release(file_inode(req->file)->i_sb, |
3973 | SB_FREEZE_WRITE); | |
3974 | } | |
3975 | kiocb->ki_flags |= IOCB_WRITE; | |
4ed734b0 | 3976 | |
35645ac3 | 3977 | if (likely(req->file->f_op->write_iter)) |
c88598a9 | 3978 | ret2 = call_write_iter(req->file, kiocb, &s->iter); |
2dd2111d | 3979 | else if (req->file->f_op->write) |
c88598a9 | 3980 | ret2 = loop_rw_iter(WRITE, req, &s->iter); |
2dd2111d GH |
3981 | else |
3982 | ret2 = -EINVAL; | |
4ed734b0 | 3983 | |
6ad7f233 PB |
3984 | if (req->flags & REQ_F_REISSUE) { |
3985 | req->flags &= ~REQ_F_REISSUE; | |
230d50d4 | 3986 | ret2 = -EAGAIN; |
6ad7f233 | 3987 | } |
230d50d4 | 3988 | |
fa15bafb PB |
3989 | /* |
3990 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just | |
3991 | * retry them without IOCB_NOWAIT. | |
3992 | */ | |
3993 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) | |
3994 | ret2 = -EAGAIN; | |
75c668cd PB |
3995 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3996 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) | |
355afaeb | 3997 | goto done; |
fa15bafb | 3998 | if (!force_nonblock || ret2 != -EAGAIN) { |
eefdf30f | 3999 | /* IOPOLL retry should happen for io-wq threads */ |
b10841c9 | 4000 | if (ret2 == -EAGAIN && (req->ctx->flags & IORING_SETUP_IOPOLL)) |
eefdf30f | 4001 | goto copy_iov; |
355afaeb | 4002 | done: |
2ea537ca | 4003 | kiocb_done(req, ret2, issue_flags); |
fa15bafb | 4004 | } else { |
f67676d1 | 4005 | copy_iov: |
c88598a9 PB |
4006 | iov_iter_restore(&s->iter, &s->iter_state); |
4007 | ret = io_setup_async_rw(req, iovec, s, false); | |
6bf985dc | 4008 | return ret ?: -EAGAIN; |
2b188cc1 | 4009 | } |
31b51510 | 4010 | out_free: |
f261c168 | 4011 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 4012 | if (iovec) |
6f2cc166 | 4013 | kfree(iovec); |
2b188cc1 JA |
4014 | return ret; |
4015 | } | |
4016 | ||
80a261fd JA |
4017 | static int io_renameat_prep(struct io_kiocb *req, |
4018 | const struct io_uring_sqe *sqe) | |
4019 | { | |
4020 | struct io_rename *ren = &req->rename; | |
4021 | const char __user *oldf, *newf; | |
4022 | ||
ed7eb259 JA |
4023 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4024 | return -EINVAL; | |
26578cda | 4025 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
ed7eb259 | 4026 | return -EINVAL; |
80a261fd JA |
4027 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
4028 | return -EBADF; | |
4029 | ||
4030 | ren->old_dfd = READ_ONCE(sqe->fd); | |
4031 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4032 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
4033 | ren->new_dfd = READ_ONCE(sqe->len); | |
4034 | ren->flags = READ_ONCE(sqe->rename_flags); | |
4035 | ||
4036 | ren->oldpath = getname(oldf); | |
4037 | if (IS_ERR(ren->oldpath)) | |
4038 | return PTR_ERR(ren->oldpath); | |
4039 | ||
4040 | ren->newpath = getname(newf); | |
4041 | if (IS_ERR(ren->newpath)) { | |
4042 | putname(ren->oldpath); | |
4043 | return PTR_ERR(ren->newpath); | |
4044 | } | |
4045 | ||
4046 | req->flags |= REQ_F_NEED_CLEANUP; | |
4047 | return 0; | |
4048 | } | |
4049 | ||
45d189c6 | 4050 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
80a261fd JA |
4051 | { |
4052 | struct io_rename *ren = &req->rename; | |
4053 | int ret; | |
4054 | ||
45d189c6 | 4055 | if (issue_flags & IO_URING_F_NONBLOCK) |
80a261fd JA |
4056 | return -EAGAIN; |
4057 | ||
4058 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, | |
4059 | ren->newpath, ren->flags); | |
4060 | ||
4061 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4062 | if (ret < 0) | |
93d2bcd2 | 4063 | req_set_fail(req); |
80a261fd JA |
4064 | io_req_complete(req, ret); |
4065 | return 0; | |
4066 | } | |
4067 | ||
14a1143b JA |
4068 | static int io_unlinkat_prep(struct io_kiocb *req, |
4069 | const struct io_uring_sqe *sqe) | |
4070 | { | |
4071 | struct io_unlink *un = &req->unlink; | |
4072 | const char __user *fname; | |
4073 | ||
22634bc5 JA |
4074 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4075 | return -EINVAL; | |
26578cda PB |
4076 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
4077 | sqe->splice_fd_in) | |
22634bc5 | 4078 | return -EINVAL; |
14a1143b JA |
4079 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
4080 | return -EBADF; | |
4081 | ||
4082 | un->dfd = READ_ONCE(sqe->fd); | |
4083 | ||
4084 | un->flags = READ_ONCE(sqe->unlink_flags); | |
4085 | if (un->flags & ~AT_REMOVEDIR) | |
4086 | return -EINVAL; | |
4087 | ||
4088 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4089 | un->filename = getname(fname); | |
4090 | if (IS_ERR(un->filename)) | |
4091 | return PTR_ERR(un->filename); | |
4092 | ||
4093 | req->flags |= REQ_F_NEED_CLEANUP; | |
4094 | return 0; | |
4095 | } | |
4096 | ||
45d189c6 | 4097 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
14a1143b JA |
4098 | { |
4099 | struct io_unlink *un = &req->unlink; | |
4100 | int ret; | |
4101 | ||
45d189c6 | 4102 | if (issue_flags & IO_URING_F_NONBLOCK) |
14a1143b JA |
4103 | return -EAGAIN; |
4104 | ||
4105 | if (un->flags & AT_REMOVEDIR) | |
4106 | ret = do_rmdir(un->dfd, un->filename); | |
4107 | else | |
4108 | ret = do_unlinkat(un->dfd, un->filename); | |
4109 | ||
4110 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4111 | if (ret < 0) | |
93d2bcd2 | 4112 | req_set_fail(req); |
14a1143b JA |
4113 | io_req_complete(req, ret); |
4114 | return 0; | |
4115 | } | |
4116 | ||
e34a02dc DK |
4117 | static int io_mkdirat_prep(struct io_kiocb *req, |
4118 | const struct io_uring_sqe *sqe) | |
4119 | { | |
4120 | struct io_mkdir *mkd = &req->mkdir; | |
4121 | const char __user *fname; | |
4122 | ||
4123 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
4124 | return -EINVAL; | |
4125 | if (sqe->ioprio || sqe->off || sqe->rw_flags || sqe->buf_index || | |
4126 | sqe->splice_fd_in) | |
4127 | return -EINVAL; | |
4128 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
4129 | return -EBADF; | |
4130 | ||
4131 | mkd->dfd = READ_ONCE(sqe->fd); | |
4132 | mkd->mode = READ_ONCE(sqe->len); | |
4133 | ||
4134 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4135 | mkd->filename = getname(fname); | |
4136 | if (IS_ERR(mkd->filename)) | |
4137 | return PTR_ERR(mkd->filename); | |
4138 | ||
4139 | req->flags |= REQ_F_NEED_CLEANUP; | |
4140 | return 0; | |
4141 | } | |
4142 | ||
04f34081 | 4143 | static int io_mkdirat(struct io_kiocb *req, unsigned int issue_flags) |
e34a02dc DK |
4144 | { |
4145 | struct io_mkdir *mkd = &req->mkdir; | |
4146 | int ret; | |
4147 | ||
4148 | if (issue_flags & IO_URING_F_NONBLOCK) | |
4149 | return -EAGAIN; | |
4150 | ||
4151 | ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode); | |
4152 | ||
4153 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4154 | if (ret < 0) | |
4155 | req_set_fail(req); | |
4156 | io_req_complete(req, ret); | |
4157 | return 0; | |
4158 | } | |
4159 | ||
7a8721f8 DK |
4160 | static int io_symlinkat_prep(struct io_kiocb *req, |
4161 | const struct io_uring_sqe *sqe) | |
4162 | { | |
4163 | struct io_symlink *sl = &req->symlink; | |
4164 | const char __user *oldpath, *newpath; | |
4165 | ||
4166 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
4167 | return -EINVAL; | |
4168 | if (sqe->ioprio || sqe->len || sqe->rw_flags || sqe->buf_index || | |
4169 | sqe->splice_fd_in) | |
4170 | return -EINVAL; | |
4171 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
4172 | return -EBADF; | |
4173 | ||
4174 | sl->new_dfd = READ_ONCE(sqe->fd); | |
4175 | oldpath = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4176 | newpath = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
4177 | ||
4178 | sl->oldpath = getname(oldpath); | |
4179 | if (IS_ERR(sl->oldpath)) | |
4180 | return PTR_ERR(sl->oldpath); | |
4181 | ||
4182 | sl->newpath = getname(newpath); | |
4183 | if (IS_ERR(sl->newpath)) { | |
4184 | putname(sl->oldpath); | |
4185 | return PTR_ERR(sl->newpath); | |
4186 | } | |
4187 | ||
4188 | req->flags |= REQ_F_NEED_CLEANUP; | |
4189 | return 0; | |
4190 | } | |
4191 | ||
04f34081 | 4192 | static int io_symlinkat(struct io_kiocb *req, unsigned int issue_flags) |
7a8721f8 DK |
4193 | { |
4194 | struct io_symlink *sl = &req->symlink; | |
4195 | int ret; | |
4196 | ||
4197 | if (issue_flags & IO_URING_F_NONBLOCK) | |
4198 | return -EAGAIN; | |
4199 | ||
4200 | ret = do_symlinkat(sl->oldpath, sl->new_dfd, sl->newpath); | |
4201 | ||
4202 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4203 | if (ret < 0) | |
4204 | req_set_fail(req); | |
4205 | io_req_complete(req, ret); | |
4206 | return 0; | |
4207 | } | |
4208 | ||
cf30da90 DK |
4209 | static int io_linkat_prep(struct io_kiocb *req, |
4210 | const struct io_uring_sqe *sqe) | |
4211 | { | |
4212 | struct io_hardlink *lnk = &req->hardlink; | |
4213 | const char __user *oldf, *newf; | |
4214 | ||
4215 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
4216 | return -EINVAL; | |
4217 | if (sqe->ioprio || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in) | |
4218 | return -EINVAL; | |
4219 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
4220 | return -EBADF; | |
4221 | ||
4222 | lnk->old_dfd = READ_ONCE(sqe->fd); | |
4223 | lnk->new_dfd = READ_ONCE(sqe->len); | |
4224 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4225 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
4226 | lnk->flags = READ_ONCE(sqe->hardlink_flags); | |
4227 | ||
4228 | lnk->oldpath = getname(oldf); | |
4229 | if (IS_ERR(lnk->oldpath)) | |
4230 | return PTR_ERR(lnk->oldpath); | |
4231 | ||
4232 | lnk->newpath = getname(newf); | |
4233 | if (IS_ERR(lnk->newpath)) { | |
4234 | putname(lnk->oldpath); | |
4235 | return PTR_ERR(lnk->newpath); | |
4236 | } | |
4237 | ||
4238 | req->flags |= REQ_F_NEED_CLEANUP; | |
4239 | return 0; | |
4240 | } | |
4241 | ||
04f34081 | 4242 | static int io_linkat(struct io_kiocb *req, unsigned int issue_flags) |
cf30da90 DK |
4243 | { |
4244 | struct io_hardlink *lnk = &req->hardlink; | |
4245 | int ret; | |
4246 | ||
4247 | if (issue_flags & IO_URING_F_NONBLOCK) | |
4248 | return -EAGAIN; | |
4249 | ||
4250 | ret = do_linkat(lnk->old_dfd, lnk->oldpath, lnk->new_dfd, | |
4251 | lnk->newpath, lnk->flags); | |
4252 | ||
4253 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4254 | if (ret < 0) | |
4255 | req_set_fail(req); | |
4256 | io_req_complete(req, ret); | |
4257 | return 0; | |
4258 | } | |
4259 | ||
36f4fa68 JA |
4260 | static int io_shutdown_prep(struct io_kiocb *req, |
4261 | const struct io_uring_sqe *sqe) | |
4262 | { | |
4263 | #if defined(CONFIG_NET) | |
4264 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
4265 | return -EINVAL; | |
26578cda PB |
4266 | if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || |
4267 | sqe->buf_index || sqe->splice_fd_in)) | |
36f4fa68 JA |
4268 | return -EINVAL; |
4269 | ||
4270 | req->shutdown.how = READ_ONCE(sqe->len); | |
4271 | return 0; | |
4272 | #else | |
4273 | return -EOPNOTSUPP; | |
4274 | #endif | |
4275 | } | |
4276 | ||
45d189c6 | 4277 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
36f4fa68 JA |
4278 | { |
4279 | #if defined(CONFIG_NET) | |
4280 | struct socket *sock; | |
4281 | int ret; | |
4282 | ||
45d189c6 | 4283 | if (issue_flags & IO_URING_F_NONBLOCK) |
36f4fa68 JA |
4284 | return -EAGAIN; |
4285 | ||
48aba79b | 4286 | sock = sock_from_file(req->file); |
36f4fa68 | 4287 | if (unlikely(!sock)) |
48aba79b | 4288 | return -ENOTSOCK; |
36f4fa68 JA |
4289 | |
4290 | ret = __sys_shutdown_sock(sock, req->shutdown.how); | |
a146468d | 4291 | if (ret < 0) |
93d2bcd2 | 4292 | req_set_fail(req); |
36f4fa68 JA |
4293 | io_req_complete(req, ret); |
4294 | return 0; | |
4295 | #else | |
4296 | return -EOPNOTSUPP; | |
4297 | #endif | |
4298 | } | |
4299 | ||
f2a8d5c7 PB |
4300 | static int __io_splice_prep(struct io_kiocb *req, |
4301 | const struct io_uring_sqe *sqe) | |
7d67af2c | 4302 | { |
fe7e3257 | 4303 | struct io_splice *sp = &req->splice; |
7d67af2c | 4304 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
7d67af2c | 4305 | |
3232dd02 PB |
4306 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4307 | return -EINVAL; | |
7d67af2c PB |
4308 | |
4309 | sp->file_in = NULL; | |
7d67af2c PB |
4310 | sp->len = READ_ONCE(sqe->len); |
4311 | sp->flags = READ_ONCE(sqe->splice_flags); | |
4312 | ||
4313 | if (unlikely(sp->flags & ~valid_flags)) | |
4314 | return -EINVAL; | |
4315 | ||
62906e89 | 4316 | sp->file_in = io_file_get(req->ctx, req, READ_ONCE(sqe->splice_fd_in), |
8371adf5 PB |
4317 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
4318 | if (!sp->file_in) | |
4319 | return -EBADF; | |
7d67af2c | 4320 | req->flags |= REQ_F_NEED_CLEANUP; |
7d67af2c PB |
4321 | return 0; |
4322 | } | |
4323 | ||
f2a8d5c7 PB |
4324 | static int io_tee_prep(struct io_kiocb *req, |
4325 | const struct io_uring_sqe *sqe) | |
4326 | { | |
4327 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) | |
4328 | return -EINVAL; | |
4329 | return __io_splice_prep(req, sqe); | |
4330 | } | |
4331 | ||
45d189c6 | 4332 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
f2a8d5c7 PB |
4333 | { |
4334 | struct io_splice *sp = &req->splice; | |
4335 | struct file *in = sp->file_in; | |
4336 | struct file *out = sp->file_out; | |
4337 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
4338 | long ret = 0; | |
4339 | ||
45d189c6 | 4340 | if (issue_flags & IO_URING_F_NONBLOCK) |
f2a8d5c7 PB |
4341 | return -EAGAIN; |
4342 | if (sp->len) | |
4343 | ret = do_tee(in, out, sp->len, flags); | |
4344 | ||
e1d767f0 PB |
4345 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
4346 | io_put_file(in); | |
f2a8d5c7 PB |
4347 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4348 | ||
f2a8d5c7 | 4349 | if (ret != sp->len) |
93d2bcd2 | 4350 | req_set_fail(req); |
e1e16097 | 4351 | io_req_complete(req, ret); |
f2a8d5c7 PB |
4352 | return 0; |
4353 | } | |
4354 | ||
4355 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4356 | { | |
fe7e3257 | 4357 | struct io_splice *sp = &req->splice; |
f2a8d5c7 PB |
4358 | |
4359 | sp->off_in = READ_ONCE(sqe->splice_off_in); | |
4360 | sp->off_out = READ_ONCE(sqe->off); | |
4361 | return __io_splice_prep(req, sqe); | |
4362 | } | |
4363 | ||
45d189c6 | 4364 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
7d67af2c PB |
4365 | { |
4366 | struct io_splice *sp = &req->splice; | |
4367 | struct file *in = sp->file_in; | |
4368 | struct file *out = sp->file_out; | |
4369 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
4370 | loff_t *poff_in, *poff_out; | |
c9687426 | 4371 | long ret = 0; |
7d67af2c | 4372 | |
45d189c6 | 4373 | if (issue_flags & IO_URING_F_NONBLOCK) |
2fb3e822 | 4374 | return -EAGAIN; |
7d67af2c PB |
4375 | |
4376 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; | |
4377 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; | |
c9687426 | 4378 | |
948a7749 | 4379 | if (sp->len) |
c9687426 | 4380 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
7d67af2c | 4381 | |
e1d767f0 PB |
4382 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
4383 | io_put_file(in); | |
7d67af2c PB |
4384 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4385 | ||
7d67af2c | 4386 | if (ret != sp->len) |
93d2bcd2 | 4387 | req_set_fail(req); |
e1e16097 | 4388 | io_req_complete(req, ret); |
7d67af2c PB |
4389 | return 0; |
4390 | } | |
4391 | ||
2b188cc1 JA |
4392 | /* |
4393 | * IORING_OP_NOP just posts a completion event, nothing else. | |
4394 | */ | |
889fca73 | 4395 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
4396 | { |
4397 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 4398 | |
def596e9 JA |
4399 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4400 | return -EINVAL; | |
4401 | ||
889fca73 | 4402 | __io_req_complete(req, issue_flags, 0, 0); |
2b188cc1 JA |
4403 | return 0; |
4404 | } | |
4405 | ||
4f57f06c JA |
4406 | static int io_msg_ring_prep(struct io_kiocb *req, |
4407 | const struct io_uring_sqe *sqe) | |
4408 | { | |
f3b6a41e JA |
4409 | if (unlikely(sqe->addr || sqe->ioprio || sqe->rw_flags || |
4410 | sqe->splice_fd_in || sqe->buf_index || sqe->personality)) | |
4f57f06c JA |
4411 | return -EINVAL; |
4412 | ||
4413 | if (req->file->f_op != &io_uring_fops) | |
4414 | return -EBADFD; | |
4415 | ||
4416 | req->msg.user_data = READ_ONCE(sqe->off); | |
4417 | req->msg.len = READ_ONCE(sqe->len); | |
4418 | return 0; | |
4419 | } | |
4420 | ||
4421 | static int io_msg_ring(struct io_kiocb *req, unsigned int issue_flags) | |
4422 | { | |
4423 | struct io_ring_ctx *target_ctx; | |
4424 | struct io_msg *msg = &req->msg; | |
4425 | int ret = -EOVERFLOW; | |
4426 | bool filled; | |
4427 | ||
4428 | target_ctx = req->file->private_data; | |
4429 | ||
4430 | spin_lock(&target_ctx->completion_lock); | |
4431 | filled = io_fill_cqe_aux(target_ctx, msg->user_data, msg->len, | |
4432 | IORING_CQE_F_MSG); | |
4433 | io_commit_cqring(target_ctx); | |
4434 | spin_unlock(&target_ctx->completion_lock); | |
4435 | ||
4436 | if (filled) { | |
4437 | io_cqring_ev_posted(target_ctx); | |
4438 | ret = 0; | |
4439 | } | |
4440 | ||
4441 | __io_req_complete(req, issue_flags, ret, 0); | |
4442 | return 0; | |
4443 | } | |
4444 | ||
1155c76a | 4445 | static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 4446 | { |
6b06314c | 4447 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 4448 | |
09bb8394 JA |
4449 | if (!req->file) |
4450 | return -EBADF; | |
c992fe29 | 4451 | |
6b06314c | 4452 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 4453 | return -EINVAL; |
26578cda PB |
4454 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
4455 | sqe->splice_fd_in)) | |
c992fe29 CH |
4456 | return -EINVAL; |
4457 | ||
8ed8d3c3 JA |
4458 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
4459 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
4460 | return -EINVAL; | |
4461 | ||
4462 | req->sync.off = READ_ONCE(sqe->off); | |
4463 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
4464 | return 0; |
4465 | } | |
4466 | ||
45d189c6 | 4467 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4468 | { |
8ed8d3c3 | 4469 | loff_t end = req->sync.off + req->sync.len; |
8ed8d3c3 JA |
4470 | int ret; |
4471 | ||
ac45abc0 | 4472 | /* fsync always requires a blocking context */ |
45d189c6 | 4473 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4474 | return -EAGAIN; |
4475 | ||
9adbd45d | 4476 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
4477 | end > 0 ? end : LLONG_MAX, |
4478 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
4479 | if (ret < 0) | |
93d2bcd2 | 4480 | req_set_fail(req); |
e1e16097 | 4481 | io_req_complete(req, ret); |
c992fe29 CH |
4482 | return 0; |
4483 | } | |
4484 | ||
d63d1b5e JA |
4485 | static int io_fallocate_prep(struct io_kiocb *req, |
4486 | const struct io_uring_sqe *sqe) | |
4487 | { | |
26578cda PB |
4488 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags || |
4489 | sqe->splice_fd_in) | |
d63d1b5e | 4490 | return -EINVAL; |
3232dd02 PB |
4491 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4492 | return -EINVAL; | |
d63d1b5e JA |
4493 | |
4494 | req->sync.off = READ_ONCE(sqe->off); | |
4495 | req->sync.len = READ_ONCE(sqe->addr); | |
4496 | req->sync.mode = READ_ONCE(sqe->len); | |
4497 | return 0; | |
4498 | } | |
4499 | ||
45d189c6 | 4500 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
5d17b4a4 | 4501 | { |
ac45abc0 PB |
4502 | int ret; |
4503 | ||
d63d1b5e | 4504 | /* fallocate always requiring blocking context */ |
45d189c6 | 4505 | if (issue_flags & IO_URING_F_NONBLOCK) |
5d17b4a4 | 4506 | return -EAGAIN; |
ac45abc0 PB |
4507 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
4508 | req->sync.len); | |
ac45abc0 | 4509 | if (ret < 0) |
93d2bcd2 | 4510 | req_set_fail(req); |
e1e16097 | 4511 | io_req_complete(req, ret); |
5d17b4a4 JA |
4512 | return 0; |
4513 | } | |
4514 | ||
ec65fea5 | 4515 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 4516 | { |
f8748881 | 4517 | const char __user *fname; |
15b71abe | 4518 | int ret; |
b7bb4f7d | 4519 | |
d3fddf6d PB |
4520 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4521 | return -EINVAL; | |
b9445598 | 4522 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
15b71abe | 4523 | return -EINVAL; |
ec65fea5 | 4524 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
cf3040ca | 4525 | return -EBADF; |
03b1230c | 4526 | |
ec65fea5 PB |
4527 | /* open.how should be already initialised */ |
4528 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
08a1d26e | 4529 | req->open.how.flags |= O_LARGEFILE; |
3529d8c2 | 4530 | |
25e72d10 PB |
4531 | req->open.dfd = READ_ONCE(sqe->fd); |
4532 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
f8748881 | 4533 | req->open.filename = getname(fname); |
15b71abe JA |
4534 | if (IS_ERR(req->open.filename)) { |
4535 | ret = PTR_ERR(req->open.filename); | |
4536 | req->open.filename = NULL; | |
4537 | return ret; | |
4538 | } | |
b9445598 PB |
4539 | |
4540 | req->open.file_slot = READ_ONCE(sqe->file_index); | |
4541 | if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC)) | |
4542 | return -EINVAL; | |
4543 | ||
4022e7af | 4544 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
8fef80bf | 4545 | req->flags |= REQ_F_NEED_CLEANUP; |
15b71abe | 4546 | return 0; |
03b1230c JA |
4547 | } |
4548 | ||
ec65fea5 PB |
4549 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4550 | { | |
d3fddf6d PB |
4551 | u64 mode = READ_ONCE(sqe->len); |
4552 | u64 flags = READ_ONCE(sqe->open_flags); | |
ec65fea5 | 4553 | |
ec65fea5 PB |
4554 | req->open.how = build_open_how(flags, mode); |
4555 | return __io_openat_prep(req, sqe); | |
4556 | } | |
4557 | ||
cebdb986 | 4558 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 4559 | { |
cebdb986 | 4560 | struct open_how __user *how; |
cebdb986 | 4561 | size_t len; |
0fa03c62 JA |
4562 | int ret; |
4563 | ||
cebdb986 JA |
4564 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4565 | len = READ_ONCE(sqe->len); | |
cebdb986 JA |
4566 | if (len < OPEN_HOW_SIZE_VER0) |
4567 | return -EINVAL; | |
3529d8c2 | 4568 | |
cebdb986 JA |
4569 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
4570 | len); | |
4571 | if (ret) | |
4572 | return ret; | |
3529d8c2 | 4573 | |
ec65fea5 | 4574 | return __io_openat_prep(req, sqe); |
cebdb986 JA |
4575 | } |
4576 | ||
45d189c6 | 4577 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
15b71abe JA |
4578 | { |
4579 | struct open_flags op; | |
15b71abe | 4580 | struct file *file; |
b9445598 PB |
4581 | bool resolve_nonblock, nonblock_set; |
4582 | bool fixed = !!req->open.file_slot; | |
15b71abe JA |
4583 | int ret; |
4584 | ||
cebdb986 | 4585 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
4586 | if (ret) |
4587 | goto err; | |
3a81fd02 JA |
4588 | nonblock_set = op.open_flag & O_NONBLOCK; |
4589 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; | |
45d189c6 | 4590 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3a81fd02 JA |
4591 | /* |
4592 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, | |
4593 | * it'll always -EAGAIN | |
4594 | */ | |
4595 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) | |
4596 | return -EAGAIN; | |
4597 | op.lookup_flags |= LOOKUP_CACHED; | |
4598 | op.open_flag |= O_NONBLOCK; | |
4599 | } | |
15b71abe | 4600 | |
b9445598 PB |
4601 | if (!fixed) { |
4602 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); | |
4603 | if (ret < 0) | |
4604 | goto err; | |
4605 | } | |
15b71abe JA |
4606 | |
4607 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
12dcb58a | 4608 | if (IS_ERR(file)) { |
944d1444 | 4609 | /* |
12dcb58a PB |
4610 | * We could hang on to this 'fd' on retrying, but seems like |
4611 | * marginal gain for something that is now known to be a slower | |
4612 | * path. So just put it, and we'll get a new one when we retry. | |
944d1444 | 4613 | */ |
b9445598 PB |
4614 | if (!fixed) |
4615 | put_unused_fd(ret); | |
3a81fd02 | 4616 | |
15b71abe | 4617 | ret = PTR_ERR(file); |
12dcb58a PB |
4618 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
4619 | if (ret == -EAGAIN && | |
4620 | (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK))) | |
4621 | return -EAGAIN; | |
4622 | goto err; | |
15b71abe | 4623 | } |
12dcb58a PB |
4624 | |
4625 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) | |
4626 | file->f_flags &= ~O_NONBLOCK; | |
4627 | fsnotify_open(file); | |
b9445598 PB |
4628 | |
4629 | if (!fixed) | |
4630 | fd_install(ret, file); | |
4631 | else | |
4632 | ret = io_install_fixed_file(req, file, issue_flags, | |
4633 | req->open.file_slot - 1); | |
15b71abe JA |
4634 | err: |
4635 | putname(req->open.filename); | |
8fef80bf | 4636 | req->flags &= ~REQ_F_NEED_CLEANUP; |
15b71abe | 4637 | if (ret < 0) |
93d2bcd2 | 4638 | req_set_fail(req); |
0bdf3398 | 4639 | __io_req_complete(req, issue_flags, ret, 0); |
15b71abe JA |
4640 | return 0; |
4641 | } | |
4642 | ||
45d189c6 | 4643 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
cebdb986 | 4644 | { |
e45cff58 | 4645 | return io_openat2(req, issue_flags); |
cebdb986 JA |
4646 | } |
4647 | ||
067524e9 JA |
4648 | static int io_remove_buffers_prep(struct io_kiocb *req, |
4649 | const struct io_uring_sqe *sqe) | |
4650 | { | |
4651 | struct io_provide_buf *p = &req->pbuf; | |
4652 | u64 tmp; | |
4653 | ||
26578cda PB |
4654 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off || |
4655 | sqe->splice_fd_in) | |
067524e9 JA |
4656 | return -EINVAL; |
4657 | ||
4658 | tmp = READ_ONCE(sqe->fd); | |
4659 | if (!tmp || tmp > USHRT_MAX) | |
4660 | return -EINVAL; | |
4661 | ||
4662 | memset(p, 0, sizeof(*p)); | |
4663 | p->nbufs = tmp; | |
4664 | p->bgid = READ_ONCE(sqe->buf_group); | |
4665 | return 0; | |
4666 | } | |
4667 | ||
4668 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, | |
4669 | int bgid, unsigned nbufs) | |
4670 | { | |
4671 | unsigned i = 0; | |
4672 | ||
4673 | /* shouldn't happen */ | |
4674 | if (!nbufs) | |
4675 | return 0; | |
4676 | ||
4677 | /* the head kbuf is the list itself */ | |
4678 | while (!list_empty(&buf->list)) { | |
4679 | struct io_buffer *nxt; | |
4680 | ||
4681 | nxt = list_first_entry(&buf->list, struct io_buffer, list); | |
4682 | list_del(&nxt->list); | |
067524e9 JA |
4683 | if (++i == nbufs) |
4684 | return i; | |
1d0254e6 | 4685 | cond_resched(); |
067524e9 JA |
4686 | } |
4687 | i++; | |
9e15c3a0 | 4688 | xa_erase(&ctx->io_buffers, bgid); |
067524e9 JA |
4689 | |
4690 | return i; | |
4691 | } | |
4692 | ||
889fca73 | 4693 | static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags) |
067524e9 JA |
4694 | { |
4695 | struct io_provide_buf *p = &req->pbuf; | |
4696 | struct io_ring_ctx *ctx = req->ctx; | |
4697 | struct io_buffer *head; | |
4698 | int ret = 0; | |
3b44b371 | 4699 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
067524e9 | 4700 | |
3b44b371 | 4701 | io_ring_submit_lock(ctx, needs_lock); |
067524e9 JA |
4702 | |
4703 | lockdep_assert_held(&ctx->uring_lock); | |
4704 | ||
4705 | ret = -ENOENT; | |
9e15c3a0 | 4706 | head = xa_load(&ctx->io_buffers, p->bgid); |
067524e9 JA |
4707 | if (head) |
4708 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); | |
067524e9 | 4709 | if (ret < 0) |
93d2bcd2 | 4710 | req_set_fail(req); |
067524e9 | 4711 | |
9fb8cb49 PB |
4712 | /* complete before unlock, IOPOLL may need the lock */ |
4713 | __io_req_complete(req, issue_flags, ret, 0); | |
3b44b371 | 4714 | io_ring_submit_unlock(ctx, needs_lock); |
067524e9 JA |
4715 | return 0; |
4716 | } | |
4717 | ||
ddf0322d JA |
4718 | static int io_provide_buffers_prep(struct io_kiocb *req, |
4719 | const struct io_uring_sqe *sqe) | |
4720 | { | |
38134ada | 4721 | unsigned long size, tmp_check; |
ddf0322d JA |
4722 | struct io_provide_buf *p = &req->pbuf; |
4723 | u64 tmp; | |
4724 | ||
26578cda | 4725 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
ddf0322d JA |
4726 | return -EINVAL; |
4727 | ||
4728 | tmp = READ_ONCE(sqe->fd); | |
4729 | if (!tmp || tmp > USHRT_MAX) | |
4730 | return -E2BIG; | |
4731 | p->nbufs = tmp; | |
4732 | p->addr = READ_ONCE(sqe->addr); | |
4733 | p->len = READ_ONCE(sqe->len); | |
4734 | ||
38134ada PB |
4735 | if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs, |
4736 | &size)) | |
4737 | return -EOVERFLOW; | |
4738 | if (check_add_overflow((unsigned long)p->addr, size, &tmp_check)) | |
4739 | return -EOVERFLOW; | |
4740 | ||
d81269fe PB |
4741 | size = (unsigned long)p->len * p->nbufs; |
4742 | if (!access_ok(u64_to_user_ptr(p->addr), size)) | |
ddf0322d JA |
4743 | return -EFAULT; |
4744 | ||
4745 | p->bgid = READ_ONCE(sqe->buf_group); | |
4746 | tmp = READ_ONCE(sqe->off); | |
4747 | if (tmp > USHRT_MAX) | |
4748 | return -E2BIG; | |
4749 | p->bid = tmp; | |
4750 | return 0; | |
4751 | } | |
4752 | ||
cc3cec83 JA |
4753 | static int io_refill_buffer_cache(struct io_ring_ctx *ctx) |
4754 | { | |
4755 | struct io_buffer *buf; | |
4756 | struct page *page; | |
4757 | int bufs_in_page; | |
4758 | ||
4759 | /* | |
4760 | * Completions that don't happen inline (eg not under uring_lock) will | |
4761 | * add to ->io_buffers_comp. If we don't have any free buffers, check | |
4762 | * the completion list and splice those entries first. | |
4763 | */ | |
4764 | if (!list_empty_careful(&ctx->io_buffers_comp)) { | |
4765 | spin_lock(&ctx->completion_lock); | |
4766 | if (!list_empty(&ctx->io_buffers_comp)) { | |
4767 | list_splice_init(&ctx->io_buffers_comp, | |
4768 | &ctx->io_buffers_cache); | |
4769 | spin_unlock(&ctx->completion_lock); | |
4770 | return 0; | |
4771 | } | |
4772 | spin_unlock(&ctx->completion_lock); | |
4773 | } | |
4774 | ||
4775 | /* | |
4776 | * No free buffers and no completion entries either. Allocate a new | |
4777 | * page worth of buffer entries and add those to our freelist. | |
4778 | */ | |
4779 | page = alloc_page(GFP_KERNEL_ACCOUNT); | |
4780 | if (!page) | |
4781 | return -ENOMEM; | |
4782 | ||
4783 | list_add(&page->lru, &ctx->io_buffers_pages); | |
4784 | ||
4785 | buf = page_address(page); | |
4786 | bufs_in_page = PAGE_SIZE / sizeof(*buf); | |
4787 | while (bufs_in_page) { | |
4788 | list_add_tail(&buf->list, &ctx->io_buffers_cache); | |
4789 | buf++; | |
4790 | bufs_in_page--; | |
4791 | } | |
4792 | ||
4793 | return 0; | |
4794 | } | |
4795 | ||
4796 | static int io_add_buffers(struct io_ring_ctx *ctx, struct io_provide_buf *pbuf, | |
4797 | struct io_buffer **head) | |
ddf0322d JA |
4798 | { |
4799 | struct io_buffer *buf; | |
4800 | u64 addr = pbuf->addr; | |
4801 | int i, bid = pbuf->bid; | |
4802 | ||
4803 | for (i = 0; i < pbuf->nbufs; i++) { | |
cc3cec83 JA |
4804 | if (list_empty(&ctx->io_buffers_cache) && |
4805 | io_refill_buffer_cache(ctx)) | |
ddf0322d | 4806 | break; |
cc3cec83 JA |
4807 | buf = list_first_entry(&ctx->io_buffers_cache, struct io_buffer, |
4808 | list); | |
4809 | list_del(&buf->list); | |
ddf0322d | 4810 | buf->addr = addr; |
d1f82808 | 4811 | buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT); |
ddf0322d | 4812 | buf->bid = bid; |
b1c62645 | 4813 | buf->bgid = pbuf->bgid; |
ddf0322d JA |
4814 | addr += pbuf->len; |
4815 | bid++; | |
4816 | if (!*head) { | |
4817 | INIT_LIST_HEAD(&buf->list); | |
4818 | *head = buf; | |
4819 | } else { | |
4820 | list_add_tail(&buf->list, &(*head)->list); | |
4821 | } | |
f240762f | 4822 | cond_resched(); |
ddf0322d JA |
4823 | } |
4824 | ||
4825 | return i ? i : -ENOMEM; | |
4826 | } | |
4827 | ||
889fca73 | 4828 | static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags) |
ddf0322d JA |
4829 | { |
4830 | struct io_provide_buf *p = &req->pbuf; | |
4831 | struct io_ring_ctx *ctx = req->ctx; | |
4832 | struct io_buffer *head, *list; | |
4833 | int ret = 0; | |
3b44b371 | 4834 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
ddf0322d | 4835 | |
3b44b371 | 4836 | io_ring_submit_lock(ctx, needs_lock); |
ddf0322d JA |
4837 | |
4838 | lockdep_assert_held(&ctx->uring_lock); | |
4839 | ||
9e15c3a0 | 4840 | list = head = xa_load(&ctx->io_buffers, p->bgid); |
ddf0322d | 4841 | |
cc3cec83 | 4842 | ret = io_add_buffers(ctx, p, &head); |
9e15c3a0 JA |
4843 | if (ret >= 0 && !list) { |
4844 | ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL); | |
4845 | if (ret < 0) | |
067524e9 | 4846 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
ddf0322d | 4847 | } |
ddf0322d | 4848 | if (ret < 0) |
93d2bcd2 | 4849 | req_set_fail(req); |
9fb8cb49 PB |
4850 | /* complete before unlock, IOPOLL may need the lock */ |
4851 | __io_req_complete(req, issue_flags, ret, 0); | |
3b44b371 | 4852 | io_ring_submit_unlock(ctx, needs_lock); |
ddf0322d | 4853 | return 0; |
cebdb986 JA |
4854 | } |
4855 | ||
3e4827b0 JA |
4856 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
4857 | const struct io_uring_sqe *sqe) | |
4858 | { | |
4859 | #if defined(CONFIG_EPOLL) | |
26578cda | 4860 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
3e4827b0 | 4861 | return -EINVAL; |
2d74d042 | 4862 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4863 | return -EINVAL; |
3e4827b0 JA |
4864 | |
4865 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
4866 | req->epoll.op = READ_ONCE(sqe->len); | |
4867 | req->epoll.fd = READ_ONCE(sqe->off); | |
4868 | ||
4869 | if (ep_op_has_event(req->epoll.op)) { | |
4870 | struct epoll_event __user *ev; | |
4871 | ||
4872 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4873 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
4874 | return -EFAULT; | |
4875 | } | |
4876 | ||
4877 | return 0; | |
4878 | #else | |
4879 | return -EOPNOTSUPP; | |
4880 | #endif | |
4881 | } | |
4882 | ||
889fca73 | 4883 | static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags) |
3e4827b0 JA |
4884 | { |
4885 | #if defined(CONFIG_EPOLL) | |
4886 | struct io_epoll *ie = &req->epoll; | |
4887 | int ret; | |
45d189c6 | 4888 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
3e4827b0 JA |
4889 | |
4890 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
4891 | if (force_nonblock && ret == -EAGAIN) | |
4892 | return -EAGAIN; | |
4893 | ||
4894 | if (ret < 0) | |
93d2bcd2 | 4895 | req_set_fail(req); |
889fca73 | 4896 | __io_req_complete(req, issue_flags, ret, 0); |
3e4827b0 JA |
4897 | return 0; |
4898 | #else | |
4899 | return -EOPNOTSUPP; | |
4900 | #endif | |
4901 | } | |
4902 | ||
c1ca757b JA |
4903 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4904 | { | |
4905 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
26578cda | 4906 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in) |
c1ca757b | 4907 | return -EINVAL; |
3232dd02 PB |
4908 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4909 | return -EINVAL; | |
c1ca757b JA |
4910 | |
4911 | req->madvise.addr = READ_ONCE(sqe->addr); | |
4912 | req->madvise.len = READ_ONCE(sqe->len); | |
4913 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4914 | return 0; | |
4915 | #else | |
4916 | return -EOPNOTSUPP; | |
4917 | #endif | |
4918 | } | |
4919 | ||
45d189c6 | 4920 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
c1ca757b JA |
4921 | { |
4922 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4923 | struct io_madvise *ma = &req->madvise; | |
4924 | int ret; | |
4925 | ||
45d189c6 | 4926 | if (issue_flags & IO_URING_F_NONBLOCK) |
c1ca757b JA |
4927 | return -EAGAIN; |
4928 | ||
0726b01e | 4929 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
c1ca757b | 4930 | if (ret < 0) |
93d2bcd2 | 4931 | req_set_fail(req); |
e1e16097 | 4932 | io_req_complete(req, ret); |
c1ca757b JA |
4933 | return 0; |
4934 | #else | |
4935 | return -EOPNOTSUPP; | |
4936 | #endif | |
4937 | } | |
4938 | ||
4840e418 JA |
4939 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4940 | { | |
26578cda | 4941 | if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in) |
4840e418 | 4942 | return -EINVAL; |
3232dd02 PB |
4943 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4944 | return -EINVAL; | |
4840e418 JA |
4945 | |
4946 | req->fadvise.offset = READ_ONCE(sqe->off); | |
4947 | req->fadvise.len = READ_ONCE(sqe->len); | |
4948 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4949 | return 0; | |
4950 | } | |
4951 | ||
45d189c6 | 4952 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
4840e418 JA |
4953 | { |
4954 | struct io_fadvise *fa = &req->fadvise; | |
4955 | int ret; | |
4956 | ||
45d189c6 | 4957 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3e69426d JA |
4958 | switch (fa->advice) { |
4959 | case POSIX_FADV_NORMAL: | |
4960 | case POSIX_FADV_RANDOM: | |
4961 | case POSIX_FADV_SEQUENTIAL: | |
4962 | break; | |
4963 | default: | |
4964 | return -EAGAIN; | |
4965 | } | |
4966 | } | |
4840e418 JA |
4967 | |
4968 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
4969 | if (ret < 0) | |
93d2bcd2 | 4970 | req_set_fail(req); |
0bdf3398 | 4971 | __io_req_complete(req, issue_flags, ret, 0); |
4840e418 JA |
4972 | return 0; |
4973 | } | |
4974 | ||
eddc7ef5 JA |
4975 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4976 | { | |
2d74d042 | 4977 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4978 | return -EINVAL; |
26578cda | 4979 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
eddc7ef5 | 4980 | return -EINVAL; |
9c280f90 | 4981 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4982 | return -EBADF; |
eddc7ef5 | 4983 | |
1d9e1288 BM |
4984 | req->statx.dfd = READ_ONCE(sqe->fd); |
4985 | req->statx.mask = READ_ONCE(sqe->len); | |
e62753e4 | 4986 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
1d9e1288 BM |
4987 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4988 | req->statx.flags = READ_ONCE(sqe->statx_flags); | |
eddc7ef5 JA |
4989 | |
4990 | return 0; | |
4991 | } | |
4992 | ||
45d189c6 | 4993 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
eddc7ef5 | 4994 | { |
1d9e1288 | 4995 | struct io_statx *ctx = &req->statx; |
eddc7ef5 JA |
4996 | int ret; |
4997 | ||
59d70013 | 4998 | if (issue_flags & IO_URING_F_NONBLOCK) |
eddc7ef5 JA |
4999 | return -EAGAIN; |
5000 | ||
e62753e4 BM |
5001 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
5002 | ctx->buffer); | |
eddc7ef5 | 5003 | |
eddc7ef5 | 5004 | if (ret < 0) |
93d2bcd2 | 5005 | req_set_fail(req); |
e1e16097 | 5006 | io_req_complete(req, ret); |
eddc7ef5 JA |
5007 | return 0; |
5008 | } | |
5009 | ||
b5dba59e JA |
5010 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5011 | { | |
14587a46 | 5012 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 5013 | return -EINVAL; |
b5dba59e | 5014 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
7df778be | 5015 | sqe->rw_flags || sqe->buf_index) |
b5dba59e | 5016 | return -EINVAL; |
9c280f90 | 5017 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 5018 | return -EBADF; |
b5dba59e JA |
5019 | |
5020 | req->close.fd = READ_ONCE(sqe->fd); | |
7df778be PB |
5021 | req->close.file_slot = READ_ONCE(sqe->file_index); |
5022 | if (req->close.file_slot && req->close.fd) | |
5023 | return -EINVAL; | |
5024 | ||
b5dba59e | 5025 | return 0; |
b5dba59e JA |
5026 | } |
5027 | ||
889fca73 | 5028 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
b5dba59e | 5029 | { |
9eac1904 | 5030 | struct files_struct *files = current->files; |
3af73b28 | 5031 | struct io_close *close = &req->close; |
9eac1904 | 5032 | struct fdtable *fdt; |
a1fde923 PB |
5033 | struct file *file = NULL; |
5034 | int ret = -EBADF; | |
b5dba59e | 5035 | |
7df778be PB |
5036 | if (req->close.file_slot) { |
5037 | ret = io_close_fixed(req, issue_flags); | |
5038 | goto err; | |
5039 | } | |
5040 | ||
9eac1904 JA |
5041 | spin_lock(&files->file_lock); |
5042 | fdt = files_fdtable(files); | |
5043 | if (close->fd >= fdt->max_fds) { | |
5044 | spin_unlock(&files->file_lock); | |
5045 | goto err; | |
5046 | } | |
5047 | file = fdt->fd[close->fd]; | |
a1fde923 | 5048 | if (!file || file->f_op == &io_uring_fops) { |
9eac1904 JA |
5049 | spin_unlock(&files->file_lock); |
5050 | file = NULL; | |
5051 | goto err; | |
3af73b28 | 5052 | } |
b5dba59e JA |
5053 | |
5054 | /* if the file has a flush method, be safe and punt to async */ | |
45d189c6 | 5055 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
9eac1904 | 5056 | spin_unlock(&files->file_lock); |
0bf0eefd | 5057 | return -EAGAIN; |
a2100672 | 5058 | } |
b5dba59e | 5059 | |
9eac1904 JA |
5060 | ret = __close_fd_get_file(close->fd, &file); |
5061 | spin_unlock(&files->file_lock); | |
5062 | if (ret < 0) { | |
5063 | if (ret == -ENOENT) | |
5064 | ret = -EBADF; | |
5065 | goto err; | |
5066 | } | |
5067 | ||
3af73b28 | 5068 | /* No ->flush() or already async, safely close from here */ |
9eac1904 JA |
5069 | ret = filp_close(file, current->files); |
5070 | err: | |
3af73b28 | 5071 | if (ret < 0) |
93d2bcd2 | 5072 | req_set_fail(req); |
9eac1904 JA |
5073 | if (file) |
5074 | fput(file); | |
889fca73 | 5075 | __io_req_complete(req, issue_flags, ret, 0); |
1a417f4e | 5076 | return 0; |
b5dba59e JA |
5077 | } |
5078 | ||
1155c76a | 5079 | static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
5080 | { |
5081 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 | 5082 | |
5d17b4a4 JA |
5083 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
5084 | return -EINVAL; | |
26578cda PB |
5085 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
5086 | sqe->splice_fd_in)) | |
5d17b4a4 JA |
5087 | return -EINVAL; |
5088 | ||
8ed8d3c3 JA |
5089 | req->sync.off = READ_ONCE(sqe->off); |
5090 | req->sync.len = READ_ONCE(sqe->len); | |
5091 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
5092 | return 0; |
5093 | } | |
5094 | ||
45d189c6 | 5095 | static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 5096 | { |
8ed8d3c3 JA |
5097 | int ret; |
5098 | ||
ac45abc0 | 5099 | /* sync_file_range always requires a blocking context */ |
45d189c6 | 5100 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
5101 | return -EAGAIN; |
5102 | ||
9adbd45d | 5103 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
5104 | req->sync.flags); |
5105 | if (ret < 0) | |
93d2bcd2 | 5106 | req_set_fail(req); |
e1e16097 | 5107 | io_req_complete(req, ret); |
5d17b4a4 JA |
5108 | return 0; |
5109 | } | |
5110 | ||
469956e8 | 5111 | #if defined(CONFIG_NET) |
02d27d89 PB |
5112 | static int io_setup_async_msg(struct io_kiocb *req, |
5113 | struct io_async_msghdr *kmsg) | |
5114 | { | |
e8c2bc1f JA |
5115 | struct io_async_msghdr *async_msg = req->async_data; |
5116 | ||
5117 | if (async_msg) | |
02d27d89 | 5118 | return -EAGAIN; |
e8c2bc1f | 5119 | if (io_alloc_async_data(req)) { |
257e84a5 | 5120 | kfree(kmsg->free_iov); |
02d27d89 PB |
5121 | return -ENOMEM; |
5122 | } | |
e8c2bc1f | 5123 | async_msg = req->async_data; |
02d27d89 | 5124 | req->flags |= REQ_F_NEED_CLEANUP; |
e8c2bc1f | 5125 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
2a780802 | 5126 | async_msg->msg.msg_name = &async_msg->addr; |
257e84a5 PB |
5127 | /* if were using fast_iov, set it to the new one */ |
5128 | if (!async_msg->free_iov) | |
5129 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; | |
5130 | ||
02d27d89 PB |
5131 | return -EAGAIN; |
5132 | } | |
5133 | ||
2ae523ed PB |
5134 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
5135 | struct io_async_msghdr *iomsg) | |
5136 | { | |
2ae523ed | 5137 | iomsg->msg.msg_name = &iomsg->addr; |
257e84a5 | 5138 | iomsg->free_iov = iomsg->fast_iov; |
2ae523ed | 5139 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
257e84a5 | 5140 | req->sr_msg.msg_flags, &iomsg->free_iov); |
2ae523ed PB |
5141 | } |
5142 | ||
93642ef8 PB |
5143 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
5144 | { | |
5145 | int ret; | |
5146 | ||
93642ef8 PB |
5147 | ret = io_sendmsg_copy_hdr(req, req->async_data); |
5148 | if (!ret) | |
5149 | req->flags |= REQ_F_NEED_CLEANUP; | |
5150 | return ret; | |
5151 | } | |
5152 | ||
3529d8c2 | 5153 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 5154 | { |
e47293fd | 5155 | struct io_sr_msg *sr = &req->sr_msg; |
03b1230c | 5156 | |
d2b6f48b PB |
5157 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5158 | return -EINVAL; | |
5159 | ||
270a5940 | 5160 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
fddaface | 5161 | sr->len = READ_ONCE(sqe->len); |
04411806 PB |
5162 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
5163 | if (sr->msg_flags & MSG_DONTWAIT) | |
5164 | req->flags |= REQ_F_NOWAIT; | |
3529d8c2 | 5165 | |
d8768362 JA |
5166 | #ifdef CONFIG_COMPAT |
5167 | if (req->ctx->compat) | |
5168 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
5169 | #endif | |
93642ef8 | 5170 | return 0; |
03b1230c JA |
5171 | } |
5172 | ||
889fca73 | 5173 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 5174 | { |
6b754c8b | 5175 | struct io_async_msghdr iomsg, *kmsg; |
0fa03c62 | 5176 | struct socket *sock; |
7a7cacba | 5177 | unsigned flags; |
0031275d | 5178 | int min_ret = 0; |
0fa03c62 JA |
5179 | int ret; |
5180 | ||
dba4a925 | 5181 | sock = sock_from_file(req->file); |
7a7cacba | 5182 | if (unlikely(!sock)) |
dba4a925 | 5183 | return -ENOTSOCK; |
3529d8c2 | 5184 | |
d886e185 PB |
5185 | if (req_has_async_data(req)) { |
5186 | kmsg = req->async_data; | |
5187 | } else { | |
7a7cacba PB |
5188 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
5189 | if (ret) | |
5190 | return ret; | |
5191 | kmsg = &iomsg; | |
0fa03c62 | 5192 | } |
0fa03c62 | 5193 | |
04411806 PB |
5194 | flags = req->sr_msg.msg_flags; |
5195 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 5196 | flags |= MSG_DONTWAIT; |
0031275d SM |
5197 | if (flags & MSG_WAITALL) |
5198 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
5199 | ||
7a7cacba | 5200 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
0fa03c62 | 5201 | |
7297ce3d PB |
5202 | if (ret < min_ret) { |
5203 | if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK)) | |
5204 | return io_setup_async_msg(req, kmsg); | |
5205 | if (ret == -ERESTARTSYS) | |
5206 | ret = -EINTR; | |
5207 | req_set_fail(req); | |
5208 | } | |
257e84a5 PB |
5209 | /* fast path, check for non-NULL to avoid function call */ |
5210 | if (kmsg->free_iov) | |
5211 | kfree(kmsg->free_iov); | |
99bc4c38 | 5212 | req->flags &= ~REQ_F_NEED_CLEANUP; |
889fca73 | 5213 | __io_req_complete(req, issue_flags, ret, 0); |
5d17b4a4 | 5214 | return 0; |
03b1230c | 5215 | } |
aa1fa28f | 5216 | |
889fca73 | 5217 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 5218 | { |
7a7cacba PB |
5219 | struct io_sr_msg *sr = &req->sr_msg; |
5220 | struct msghdr msg; | |
5221 | struct iovec iov; | |
fddaface | 5222 | struct socket *sock; |
7a7cacba | 5223 | unsigned flags; |
0031275d | 5224 | int min_ret = 0; |
fddaface JA |
5225 | int ret; |
5226 | ||
dba4a925 | 5227 | sock = sock_from_file(req->file); |
7a7cacba | 5228 | if (unlikely(!sock)) |
dba4a925 | 5229 | return -ENOTSOCK; |
fddaface | 5230 | |
7a7cacba PB |
5231 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
5232 | if (unlikely(ret)) | |
14db8411 | 5233 | return ret; |
fddaface | 5234 | |
7a7cacba PB |
5235 | msg.msg_name = NULL; |
5236 | msg.msg_control = NULL; | |
5237 | msg.msg_controllen = 0; | |
5238 | msg.msg_namelen = 0; | |
fddaface | 5239 | |
04411806 PB |
5240 | flags = req->sr_msg.msg_flags; |
5241 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 5242 | flags |= MSG_DONTWAIT; |
0031275d SM |
5243 | if (flags & MSG_WAITALL) |
5244 | min_ret = iov_iter_count(&msg.msg_iter); | |
5245 | ||
7a7cacba PB |
5246 | msg.msg_flags = flags; |
5247 | ret = sock_sendmsg(sock, &msg); | |
7297ce3d PB |
5248 | if (ret < min_ret) { |
5249 | if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK)) | |
5250 | return -EAGAIN; | |
5251 | if (ret == -ERESTARTSYS) | |
5252 | ret = -EINTR; | |
93d2bcd2 | 5253 | req_set_fail(req); |
7297ce3d | 5254 | } |
889fca73 | 5255 | __io_req_complete(req, issue_flags, ret, 0); |
fddaface | 5256 | return 0; |
fddaface JA |
5257 | } |
5258 | ||
1400e697 PB |
5259 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
5260 | struct io_async_msghdr *iomsg) | |
52de1fe1 JA |
5261 | { |
5262 | struct io_sr_msg *sr = &req->sr_msg; | |
5263 | struct iovec __user *uiov; | |
5264 | size_t iov_len; | |
5265 | int ret; | |
5266 | ||
1400e697 PB |
5267 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
5268 | &iomsg->uaddr, &uiov, &iov_len); | |
52de1fe1 JA |
5269 | if (ret) |
5270 | return ret; | |
5271 | ||
5272 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
5273 | if (iov_len > 1) | |
5274 | return -EINVAL; | |
5476dfed | 5275 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
52de1fe1 | 5276 | return -EFAULT; |
5476dfed | 5277 | sr->len = iomsg->fast_iov[0].iov_len; |
257e84a5 | 5278 | iomsg->free_iov = NULL; |
52de1fe1 | 5279 | } else { |
257e84a5 | 5280 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 5281 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
257e84a5 | 5282 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
89cd35c5 | 5283 | false); |
52de1fe1 JA |
5284 | if (ret > 0) |
5285 | ret = 0; | |
5286 | } | |
5287 | ||
5288 | return ret; | |
5289 | } | |
5290 | ||
5291 | #ifdef CONFIG_COMPAT | |
5292 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, | |
1400e697 | 5293 | struct io_async_msghdr *iomsg) |
52de1fe1 | 5294 | { |
52de1fe1 JA |
5295 | struct io_sr_msg *sr = &req->sr_msg; |
5296 | struct compat_iovec __user *uiov; | |
5297 | compat_uptr_t ptr; | |
5298 | compat_size_t len; | |
5299 | int ret; | |
5300 | ||
4af3417a PB |
5301 | ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr, |
5302 | &ptr, &len); | |
52de1fe1 JA |
5303 | if (ret) |
5304 | return ret; | |
5305 | ||
5306 | uiov = compat_ptr(ptr); | |
5307 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
5308 | compat_ssize_t clen; | |
5309 | ||
5310 | if (len > 1) | |
5311 | return -EINVAL; | |
5312 | if (!access_ok(uiov, sizeof(*uiov))) | |
5313 | return -EFAULT; | |
5314 | if (__get_user(clen, &uiov->iov_len)) | |
5315 | return -EFAULT; | |
5316 | if (clen < 0) | |
5317 | return -EINVAL; | |
2d280bc8 | 5318 | sr->len = clen; |
257e84a5 | 5319 | iomsg->free_iov = NULL; |
52de1fe1 | 5320 | } else { |
257e84a5 | 5321 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 5322 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
257e84a5 | 5323 | UIO_FASTIOV, &iomsg->free_iov, |
89cd35c5 | 5324 | &iomsg->msg.msg_iter, true); |
52de1fe1 JA |
5325 | if (ret < 0) |
5326 | return ret; | |
5327 | } | |
5328 | ||
5329 | return 0; | |
5330 | } | |
5331 | #endif | |
5332 | ||
1400e697 PB |
5333 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
5334 | struct io_async_msghdr *iomsg) | |
52de1fe1 | 5335 | { |
1400e697 | 5336 | iomsg->msg.msg_name = &iomsg->addr; |
52de1fe1 JA |
5337 | |
5338 | #ifdef CONFIG_COMPAT | |
5339 | if (req->ctx->compat) | |
1400e697 | 5340 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
fddaface | 5341 | #endif |
52de1fe1 | 5342 | |
1400e697 | 5343 | return __io_recvmsg_copy_hdr(req, iomsg); |
52de1fe1 JA |
5344 | } |
5345 | ||
bcda7baa | 5346 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
51aac424 | 5347 | unsigned int issue_flags) |
bcda7baa JA |
5348 | { |
5349 | struct io_sr_msg *sr = &req->sr_msg; | |
bcda7baa | 5350 | |
51aac424 | 5351 | return io_buffer_select(req, &sr->len, sr->bgid, issue_flags); |
fddaface JA |
5352 | } |
5353 | ||
93642ef8 | 5354 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
aa1fa28f | 5355 | { |
99bc4c38 | 5356 | int ret; |
3529d8c2 | 5357 | |
93642ef8 PB |
5358 | ret = io_recvmsg_copy_hdr(req, req->async_data); |
5359 | if (!ret) | |
5360 | req->flags |= REQ_F_NEED_CLEANUP; | |
5361 | return ret; | |
5362 | } | |
5363 | ||
5364 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
5365 | { | |
5366 | struct io_sr_msg *sr = &req->sr_msg; | |
5367 | ||
d2b6f48b PB |
5368 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5369 | return -EINVAL; | |
5370 | ||
270a5940 | 5371 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
0b7b21e4 | 5372 | sr->len = READ_ONCE(sqe->len); |
bcda7baa | 5373 | sr->bgid = READ_ONCE(sqe->buf_group); |
04411806 PB |
5374 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
5375 | if (sr->msg_flags & MSG_DONTWAIT) | |
5376 | req->flags |= REQ_F_NOWAIT; | |
06b76d44 | 5377 | |
d8768362 JA |
5378 | #ifdef CONFIG_COMPAT |
5379 | if (req->ctx->compat) | |
5380 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
5381 | #endif | |
93642ef8 | 5382 | return 0; |
aa1fa28f JA |
5383 | } |
5384 | ||
889fca73 | 5385 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 5386 | { |
6b754c8b | 5387 | struct io_async_msghdr iomsg, *kmsg; |
03b1230c | 5388 | struct socket *sock; |
7fbb1b54 | 5389 | struct io_buffer *kbuf; |
7a7cacba | 5390 | unsigned flags; |
d1fd1c20 | 5391 | int ret, min_ret = 0; |
45d189c6 | 5392 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
03b1230c | 5393 | |
dba4a925 | 5394 | sock = sock_from_file(req->file); |
7a7cacba | 5395 | if (unlikely(!sock)) |
dba4a925 | 5396 | return -ENOTSOCK; |
3529d8c2 | 5397 | |
d886e185 PB |
5398 | if (req_has_async_data(req)) { |
5399 | kmsg = req->async_data; | |
5400 | } else { | |
7a7cacba PB |
5401 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
5402 | if (ret) | |
681fda8d | 5403 | return ret; |
7a7cacba PB |
5404 | kmsg = &iomsg; |
5405 | } | |
03b1230c | 5406 | |
bc02ef33 | 5407 | if (req->flags & REQ_F_BUFFER_SELECT) { |
51aac424 | 5408 | kbuf = io_recv_buffer_select(req, issue_flags); |
bc02ef33 | 5409 | if (IS_ERR(kbuf)) |
52de1fe1 | 5410 | return PTR_ERR(kbuf); |
7a7cacba | 5411 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
5476dfed PB |
5412 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
5413 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, | |
7a7cacba PB |
5414 | 1, req->sr_msg.len); |
5415 | } | |
52de1fe1 | 5416 | |
04411806 PB |
5417 | flags = req->sr_msg.msg_flags; |
5418 | if (force_nonblock) | |
7a7cacba | 5419 | flags |= MSG_DONTWAIT; |
0031275d SM |
5420 | if (flags & MSG_WAITALL) |
5421 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
5422 | ||
7a7cacba PB |
5423 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
5424 | kmsg->uaddr, flags); | |
7297ce3d PB |
5425 | if (ret < min_ret) { |
5426 | if (ret == -EAGAIN && force_nonblock) | |
5427 | return io_setup_async_msg(req, kmsg); | |
5428 | if (ret == -ERESTARTSYS) | |
5429 | ret = -EINTR; | |
5430 | req_set_fail(req); | |
5431 | } else if ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) { | |
5432 | req_set_fail(req); | |
5433 | } | |
03b1230c | 5434 | |
257e84a5 PB |
5435 | /* fast path, check for non-NULL to avoid function call */ |
5436 | if (kmsg->free_iov) | |
5437 | kfree(kmsg->free_iov); | |
99bc4c38 | 5438 | req->flags &= ~REQ_F_NEED_CLEANUP; |
cc3cec83 | 5439 | __io_req_complete(req, issue_flags, ret, io_put_kbuf(req, issue_flags)); |
03b1230c | 5440 | return 0; |
0fa03c62 | 5441 | } |
5d17b4a4 | 5442 | |
889fca73 | 5443 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 5444 | { |
6b754c8b | 5445 | struct io_buffer *kbuf; |
7a7cacba PB |
5446 | struct io_sr_msg *sr = &req->sr_msg; |
5447 | struct msghdr msg; | |
5448 | void __user *buf = sr->buf; | |
fddaface | 5449 | struct socket *sock; |
7a7cacba PB |
5450 | struct iovec iov; |
5451 | unsigned flags; | |
d1fd1c20 | 5452 | int ret, min_ret = 0; |
45d189c6 | 5453 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
fddaface | 5454 | |
dba4a925 | 5455 | sock = sock_from_file(req->file); |
7a7cacba | 5456 | if (unlikely(!sock)) |
dba4a925 | 5457 | return -ENOTSOCK; |
fddaface | 5458 | |
bc02ef33 | 5459 | if (req->flags & REQ_F_BUFFER_SELECT) { |
51aac424 | 5460 | kbuf = io_recv_buffer_select(req, issue_flags); |
bcda7baa JA |
5461 | if (IS_ERR(kbuf)) |
5462 | return PTR_ERR(kbuf); | |
7a7cacba | 5463 | buf = u64_to_user_ptr(kbuf->addr); |
bc02ef33 | 5464 | } |
bcda7baa | 5465 | |
7a7cacba | 5466 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
14c32eee PB |
5467 | if (unlikely(ret)) |
5468 | goto out_free; | |
fddaface | 5469 | |
7a7cacba PB |
5470 | msg.msg_name = NULL; |
5471 | msg.msg_control = NULL; | |
5472 | msg.msg_controllen = 0; | |
5473 | msg.msg_namelen = 0; | |
5474 | msg.msg_iocb = NULL; | |
5475 | msg.msg_flags = 0; | |
fddaface | 5476 | |
04411806 PB |
5477 | flags = req->sr_msg.msg_flags; |
5478 | if (force_nonblock) | |
7a7cacba | 5479 | flags |= MSG_DONTWAIT; |
0031275d SM |
5480 | if (flags & MSG_WAITALL) |
5481 | min_ret = iov_iter_count(&msg.msg_iter); | |
5482 | ||
7a7cacba | 5483 | ret = sock_recvmsg(sock, &msg, flags); |
7297ce3d PB |
5484 | if (ret < min_ret) { |
5485 | if (ret == -EAGAIN && force_nonblock) | |
5486 | return -EAGAIN; | |
5487 | if (ret == -ERESTARTSYS) | |
5488 | ret = -EINTR; | |
5489 | req_set_fail(req); | |
5490 | } else if ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) { | |
0d7c1153 | 5491 | out_free: |
93d2bcd2 | 5492 | req_set_fail(req); |
7297ce3d | 5493 | } |
cc3cec83 JA |
5494 | |
5495 | __io_req_complete(req, issue_flags, ret, io_put_kbuf(req, issue_flags)); | |
fddaface | 5496 | return 0; |
fddaface JA |
5497 | } |
5498 | ||
3529d8c2 | 5499 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 | 5500 | { |
8ed8d3c3 JA |
5501 | struct io_accept *accept = &req->accept; |
5502 | ||
14587a46 | 5503 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
17f2fe35 | 5504 | return -EINVAL; |
aaa4db12 | 5505 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
5506 | return -EINVAL; |
5507 | ||
d55e5f5b JA |
5508 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5509 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 5510 | accept->flags = READ_ONCE(sqe->accept_flags); |
09952e3e | 5511 | accept->nofile = rlimit(RLIMIT_NOFILE); |
a7083ad5 | 5512 | |
aaa4db12 PB |
5513 | accept->file_slot = READ_ONCE(sqe->file_index); |
5514 | if (accept->file_slot && ((req->open.how.flags & O_CLOEXEC) || | |
5515 | (accept->flags & SOCK_CLOEXEC))) | |
5516 | return -EINVAL; | |
a7083ad5 PB |
5517 | if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) |
5518 | return -EINVAL; | |
5519 | if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK)) | |
5520 | accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK; | |
8ed8d3c3 | 5521 | return 0; |
8ed8d3c3 | 5522 | } |
17f2fe35 | 5523 | |
889fca73 | 5524 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 JA |
5525 | { |
5526 | struct io_accept *accept = &req->accept; | |
45d189c6 | 5527 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ac45abc0 | 5528 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
aaa4db12 | 5529 | bool fixed = !!accept->file_slot; |
a7083ad5 PB |
5530 | struct file *file; |
5531 | int ret, fd; | |
8ed8d3c3 | 5532 | |
e697deed JX |
5533 | if (req->file->f_flags & O_NONBLOCK) |
5534 | req->flags |= REQ_F_NOWAIT; | |
5535 | ||
aaa4db12 PB |
5536 | if (!fixed) { |
5537 | fd = __get_unused_fd_flags(accept->flags, accept->nofile); | |
5538 | if (unlikely(fd < 0)) | |
5539 | return fd; | |
5540 | } | |
a7083ad5 PB |
5541 | file = do_accept(req->file, file_flags, accept->addr, accept->addr_len, |
5542 | accept->flags); | |
5543 | if (IS_ERR(file)) { | |
aaa4db12 PB |
5544 | if (!fixed) |
5545 | put_unused_fd(fd); | |
a7083ad5 PB |
5546 | ret = PTR_ERR(file); |
5547 | if (ret == -EAGAIN && force_nonblock) | |
5548 | return -EAGAIN; | |
ac45abc0 PB |
5549 | if (ret == -ERESTARTSYS) |
5550 | ret = -EINTR; | |
93d2bcd2 | 5551 | req_set_fail(req); |
aaa4db12 | 5552 | } else if (!fixed) { |
a7083ad5 PB |
5553 | fd_install(fd, file); |
5554 | ret = fd; | |
aaa4db12 PB |
5555 | } else { |
5556 | ret = io_install_fixed_file(req, file, issue_flags, | |
5557 | accept->file_slot - 1); | |
ac45abc0 | 5558 | } |
889fca73 | 5559 | __io_req_complete(req, issue_flags, ret, 0); |
17f2fe35 | 5560 | return 0; |
8ed8d3c3 JA |
5561 | } |
5562 | ||
93642ef8 PB |
5563 | static int io_connect_prep_async(struct io_kiocb *req) |
5564 | { | |
5565 | struct io_async_connect *io = req->async_data; | |
5566 | struct io_connect *conn = &req->connect; | |
5567 | ||
5568 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); | |
5569 | } | |
5570 | ||
3529d8c2 | 5571 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 | 5572 | { |
3529d8c2 | 5573 | struct io_connect *conn = &req->connect; |
f499a021 | 5574 | |
14587a46 | 5575 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3fbb51c1 | 5576 | return -EINVAL; |
26578cda PB |
5577 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags || |
5578 | sqe->splice_fd_in) | |
3fbb51c1 JA |
5579 | return -EINVAL; |
5580 | ||
3529d8c2 JA |
5581 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5582 | conn->addr_len = READ_ONCE(sqe->addr2); | |
93642ef8 | 5583 | return 0; |
f499a021 JA |
5584 | } |
5585 | ||
889fca73 | 5586 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
f8e85cf2 | 5587 | { |
e8c2bc1f | 5588 | struct io_async_connect __io, *io; |
f8e85cf2 | 5589 | unsigned file_flags; |
3fbb51c1 | 5590 | int ret; |
45d189c6 | 5591 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
f8e85cf2 | 5592 | |
d886e185 | 5593 | if (req_has_async_data(req)) { |
e8c2bc1f | 5594 | io = req->async_data; |
f499a021 | 5595 | } else { |
3529d8c2 JA |
5596 | ret = move_addr_to_kernel(req->connect.addr, |
5597 | req->connect.addr_len, | |
e8c2bc1f | 5598 | &__io.address); |
f499a021 JA |
5599 | if (ret) |
5600 | goto out; | |
5601 | io = &__io; | |
5602 | } | |
5603 | ||
3fbb51c1 JA |
5604 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
5605 | ||
e8c2bc1f | 5606 | ret = __sys_connect_file(req->file, &io->address, |
3fbb51c1 | 5607 | req->connect.addr_len, file_flags); |
87f80d62 | 5608 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
d886e185 | 5609 | if (req_has_async_data(req)) |
b7bb4f7d | 5610 | return -EAGAIN; |
e8c2bc1f | 5611 | if (io_alloc_async_data(req)) { |
f499a021 JA |
5612 | ret = -ENOMEM; |
5613 | goto out; | |
5614 | } | |
e8c2bc1f | 5615 | memcpy(req->async_data, &__io, sizeof(__io)); |
f8e85cf2 | 5616 | return -EAGAIN; |
f499a021 | 5617 | } |
f8e85cf2 JA |
5618 | if (ret == -ERESTARTSYS) |
5619 | ret = -EINTR; | |
f499a021 | 5620 | out: |
4e88d6e7 | 5621 | if (ret < 0) |
93d2bcd2 | 5622 | req_set_fail(req); |
889fca73 | 5623 | __io_req_complete(req, issue_flags, ret, 0); |
f8e85cf2 | 5624 | return 0; |
469956e8 Y |
5625 | } |
5626 | #else /* !CONFIG_NET */ | |
99a10081 JA |
5627 | #define IO_NETOP_FN(op) \ |
5628 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ | |
5629 | { \ | |
5630 | return -EOPNOTSUPP; \ | |
5631 | } | |
5632 | ||
5633 | #define IO_NETOP_PREP(op) \ | |
5634 | IO_NETOP_FN(op) \ | |
5635 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ | |
5636 | { \ | |
5637 | return -EOPNOTSUPP; \ | |
5638 | } \ | |
5639 | ||
5640 | #define IO_NETOP_PREP_ASYNC(op) \ | |
5641 | IO_NETOP_PREP(op) \ | |
5642 | static int io_##op##_prep_async(struct io_kiocb *req) \ | |
5643 | { \ | |
5644 | return -EOPNOTSUPP; \ | |
5645 | } | |
5646 | ||
5647 | IO_NETOP_PREP_ASYNC(sendmsg); | |
5648 | IO_NETOP_PREP_ASYNC(recvmsg); | |
5649 | IO_NETOP_PREP_ASYNC(connect); | |
5650 | IO_NETOP_PREP(accept); | |
5651 | IO_NETOP_FN(send); | |
5652 | IO_NETOP_FN(recv); | |
469956e8 | 5653 | #endif /* CONFIG_NET */ |
f8e85cf2 | 5654 | |
adc8682e OL |
5655 | #ifdef CONFIG_NET_RX_BUSY_POLL |
5656 | ||
5657 | #define NAPI_TIMEOUT (60 * SEC_CONVERSION) | |
5658 | ||
5659 | struct napi_entry { | |
5660 | struct list_head list; | |
5661 | unsigned int napi_id; | |
5662 | unsigned long timeout; | |
5663 | }; | |
5664 | ||
5665 | /* | |
5666 | * Add busy poll NAPI ID from sk. | |
5667 | */ | |
5668 | static void io_add_napi(struct file *file, struct io_ring_ctx *ctx) | |
5669 | { | |
5670 | unsigned int napi_id; | |
5671 | struct socket *sock; | |
5672 | struct sock *sk; | |
5673 | struct napi_entry *ne; | |
5674 | ||
5675 | if (!net_busy_loop_on()) | |
5676 | return; | |
5677 | ||
5678 | sock = sock_from_file(file); | |
5679 | if (!sock) | |
5680 | return; | |
5681 | ||
5682 | sk = sock->sk; | |
5683 | if (!sk) | |
5684 | return; | |
5685 | ||
5686 | napi_id = READ_ONCE(sk->sk_napi_id); | |
5687 | ||
5688 | /* Non-NAPI IDs can be rejected */ | |
5689 | if (napi_id < MIN_NAPI_ID) | |
5690 | return; | |
5691 | ||
5692 | spin_lock(&ctx->napi_lock); | |
5693 | list_for_each_entry(ne, &ctx->napi_list, list) { | |
5694 | if (ne->napi_id == napi_id) { | |
5695 | ne->timeout = jiffies + NAPI_TIMEOUT; | |
5696 | goto out; | |
5697 | } | |
5698 | } | |
5699 | ||
5700 | ne = kmalloc(sizeof(*ne), GFP_NOWAIT); | |
5701 | if (!ne) | |
5702 | goto out; | |
5703 | ||
5704 | ne->napi_id = napi_id; | |
5705 | ne->timeout = jiffies + NAPI_TIMEOUT; | |
5706 | list_add_tail(&ne->list, &ctx->napi_list); | |
5707 | out: | |
5708 | spin_unlock(&ctx->napi_lock); | |
5709 | } | |
5710 | ||
5711 | static inline void io_check_napi_entry_timeout(struct napi_entry *ne) | |
5712 | { | |
5713 | if (time_after(jiffies, ne->timeout)) { | |
5714 | list_del(&ne->list); | |
5715 | kfree(ne); | |
5716 | } | |
5717 | } | |
5718 | ||
5719 | /* | |
5720 | * Busy poll if globally on and supporting sockets found | |
5721 | */ | |
5722 | static bool io_napi_busy_loop(struct list_head *napi_list) | |
5723 | { | |
5724 | struct napi_entry *ne, *n; | |
5725 | ||
5726 | list_for_each_entry_safe(ne, n, napi_list, list) { | |
5727 | napi_busy_loop(ne->napi_id, NULL, NULL, true, | |
5728 | BUSY_POLL_BUDGET); | |
5729 | io_check_napi_entry_timeout(ne); | |
5730 | } | |
5731 | return !list_empty(napi_list); | |
5732 | } | |
5733 | ||
5734 | static void io_free_napi_list(struct io_ring_ctx *ctx) | |
5735 | { | |
5736 | spin_lock(&ctx->napi_lock); | |
5737 | while (!list_empty(&ctx->napi_list)) { | |
5738 | struct napi_entry *ne = | |
5739 | list_first_entry(&ctx->napi_list, struct napi_entry, | |
5740 | list); | |
5741 | ||
5742 | list_del(&ne->list); | |
5743 | kfree(ne); | |
5744 | } | |
5745 | spin_unlock(&ctx->napi_lock); | |
5746 | } | |
5747 | #else | |
5748 | static inline void io_add_napi(struct file *file, struct io_ring_ctx *ctx) | |
5749 | { | |
5750 | } | |
5751 | ||
5752 | static inline void io_free_napi_list(struct io_ring_ctx *ctx) | |
5753 | { | |
5754 | } | |
5755 | #endif /* CONFIG_NET_RX_BUSY_POLL */ | |
5756 | ||
d7718a9d JA |
5757 | struct io_poll_table { |
5758 | struct poll_table_struct pt; | |
5759 | struct io_kiocb *req; | |
68b11e8b | 5760 | int nr_entries; |
d7718a9d JA |
5761 | int error; |
5762 | }; | |
ce593a6c | 5763 | |
aa43477b PB |
5764 | #define IO_POLL_CANCEL_FLAG BIT(31) |
5765 | #define IO_POLL_REF_MASK ((1u << 20)-1) | |
6d816e08 | 5766 | |
aa43477b PB |
5767 | /* |
5768 | * If refs part of ->poll_refs (see IO_POLL_REF_MASK) is 0, it's free. We can | |
5769 | * bump it and acquire ownership. It's disallowed to modify requests while not | |
5770 | * owning it, that prevents from races for enqueueing task_work's and b/w | |
5771 | * arming poll and wakeups. | |
5772 | */ | |
5773 | static inline bool io_poll_get_ownership(struct io_kiocb *req) | |
5774 | { | |
5775 | return !(atomic_fetch_inc(&req->poll_refs) & IO_POLL_REF_MASK); | |
d7718a9d JA |
5776 | } |
5777 | ||
aa43477b | 5778 | static void io_poll_mark_cancelled(struct io_kiocb *req) |
74ce6ce4 | 5779 | { |
aa43477b | 5780 | atomic_or(IO_POLL_CANCEL_FLAG, &req->poll_refs); |
74ce6ce4 JA |
5781 | } |
5782 | ||
d4e7cd36 | 5783 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
18bceab1 | 5784 | { |
e8c2bc1f | 5785 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
d4e7cd36 | 5786 | if (req->opcode == IORING_OP_POLL_ADD) |
e8c2bc1f | 5787 | return req->async_data; |
d4e7cd36 JA |
5788 | return req->apoll->double_poll; |
5789 | } | |
5790 | ||
5791 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) | |
5792 | { | |
5793 | if (req->opcode == IORING_OP_POLL_ADD) | |
5794 | return &req->poll; | |
5795 | return &req->apoll->poll; | |
5796 | } | |
5797 | ||
5641897a | 5798 | static void io_poll_req_insert(struct io_kiocb *req) |
d4e7cd36 | 5799 | { |
5641897a PB |
5800 | struct io_ring_ctx *ctx = req->ctx; |
5801 | struct hlist_head *list; | |
18bceab1 | 5802 | |
5641897a PB |
5803 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
5804 | hlist_add_head(&req->hash_node, list); | |
18bceab1 JA |
5805 | } |
5806 | ||
5641897a PB |
5807 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, |
5808 | wait_queue_func_t wake_func) | |
18bceab1 | 5809 | { |
5641897a | 5810 | poll->head = NULL; |
5641897a PB |
5811 | #define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP) |
5812 | /* mask in events that we always want/need */ | |
5813 | poll->events = events | IO_POLL_UNMASK; | |
5814 | INIT_LIST_HEAD(&poll->wait.entry); | |
5815 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
18bceab1 JA |
5816 | } |
5817 | ||
aa43477b | 5818 | static inline void io_poll_remove_entry(struct io_poll_iocb *poll) |
18bceab1 | 5819 | { |
791f3465 | 5820 | struct wait_queue_head *head = smp_load_acquire(&poll->head); |
18bceab1 | 5821 | |
791f3465 PB |
5822 | if (head) { |
5823 | spin_lock_irq(&head->lock); | |
5824 | list_del_init(&poll->wait.entry); | |
5825 | poll->head = NULL; | |
5826 | spin_unlock_irq(&head->lock); | |
5827 | } | |
aa43477b | 5828 | } |
18bceab1 | 5829 | |
aa43477b PB |
5830 | static void io_poll_remove_entries(struct io_kiocb *req) |
5831 | { | |
91eac1c6 JA |
5832 | /* |
5833 | * Nothing to do if neither of those flags are set. Avoid dipping | |
5834 | * into the poll/apoll/double cachelines if we can. | |
5835 | */ | |
5836 | if (!(req->flags & (REQ_F_SINGLE_POLL | REQ_F_DOUBLE_POLL))) | |
5837 | return; | |
18bceab1 | 5838 | |
791f3465 PB |
5839 | /* |
5840 | * While we hold the waitqueue lock and the waitqueue is nonempty, | |
5841 | * wake_up_pollfree() will wait for us. However, taking the waitqueue | |
5842 | * lock in the first place can race with the waitqueue being freed. | |
5843 | * | |
5844 | * We solve this as eventpoll does: by taking advantage of the fact that | |
5845 | * all users of wake_up_pollfree() will RCU-delay the actual free. If | |
5846 | * we enter rcu_read_lock() and see that the pointer to the queue is | |
5847 | * non-NULL, we can then lock it without the memory being freed out from | |
5848 | * under us. | |
5849 | * | |
5850 | * Keep holding rcu_read_lock() as long as we hold the queue lock, in | |
5851 | * case the caller deletes the entry from the queue, leaving it empty. | |
5852 | * In that case, only RCU prevents the queue memory from being freed. | |
5853 | */ | |
5854 | rcu_read_lock(); | |
91eac1c6 JA |
5855 | if (req->flags & REQ_F_SINGLE_POLL) |
5856 | io_poll_remove_entry(io_poll_get_single(req)); | |
5857 | if (req->flags & REQ_F_DOUBLE_POLL) | |
5858 | io_poll_remove_entry(io_poll_get_double(req)); | |
791f3465 | 5859 | rcu_read_unlock(); |
18bceab1 JA |
5860 | } |
5861 | ||
aa43477b PB |
5862 | /* |
5863 | * All poll tw should go through this. Checks for poll events, manages | |
5864 | * references, does rewait, etc. | |
5865 | * | |
5866 | * Returns a negative error on failure. >0 when no action require, which is | |
5867 | * either spurious wakeup or multishot CQE is served. 0 when it's done with | |
5868 | * the request, then the mask is stored in req->result. | |
5869 | */ | |
5870 | static int io_poll_check_events(struct io_kiocb *req) | |
18bceab1 | 5871 | { |
74ce6ce4 | 5872 | struct io_ring_ctx *ctx = req->ctx; |
d4e7cd36 | 5873 | struct io_poll_iocb *poll = io_poll_get_single(req); |
aa43477b | 5874 | int v; |
18bceab1 | 5875 | |
316319e8 | 5876 | /* req->task == current here, checking PF_EXITING is safe */ |
e09ee510 | 5877 | if (unlikely(req->task->flags & PF_EXITING)) |
aa43477b | 5878 | io_poll_mark_cancelled(req); |
18bceab1 | 5879 | |
aa43477b PB |
5880 | do { |
5881 | v = atomic_read(&req->poll_refs); | |
74ce6ce4 | 5882 | |
aa43477b PB |
5883 | /* tw handler should be the owner, and so have some references */ |
5884 | if (WARN_ON_ONCE(!(v & IO_POLL_REF_MASK))) | |
5885 | return 0; | |
5886 | if (v & IO_POLL_CANCEL_FLAG) | |
5887 | return -ECANCELED; | |
8706e04e | 5888 | |
aa43477b | 5889 | if (!req->result) { |
81459350 | 5890 | struct poll_table_struct pt = { ._key = req->cflags }; |
18bceab1 | 5891 | |
81459350 | 5892 | req->result = vfs_poll(req->file, &pt) & req->cflags; |
c8b5e260 | 5893 | } |
74ce6ce4 | 5894 | |
aa43477b | 5895 | /* multishot, just fill an CQE and proceed */ |
81459350 | 5896 | if (req->result && !(req->cflags & EPOLLONESHOT)) { |
aa43477b PB |
5897 | __poll_t mask = mangle_poll(req->result & poll->events); |
5898 | bool filled; | |
18bceab1 | 5899 | |
aa43477b PB |
5900 | spin_lock(&ctx->completion_lock); |
5901 | filled = io_fill_cqe_aux(ctx, req->user_data, mask, | |
5902 | IORING_CQE_F_MORE); | |
5903 | io_commit_cqring(ctx); | |
5904 | spin_unlock(&ctx->completion_lock); | |
5905 | if (unlikely(!filled)) | |
5906 | return -ECANCELED; | |
5907 | io_cqring_ev_posted(ctx); | |
adc8682e | 5908 | io_add_napi(req->file, ctx); |
aa43477b PB |
5909 | } else if (req->result) { |
5910 | return 0; | |
5911 | } | |
18bceab1 | 5912 | |
aa43477b PB |
5913 | /* |
5914 | * Release all references, retry if someone tried to restart | |
5915 | * task_work while we were executing it. | |
5916 | */ | |
5917 | } while (atomic_sub_return(v & IO_POLL_REF_MASK, &req->poll_refs)); | |
18bceab1 | 5918 | |
18bceab1 JA |
5919 | return 1; |
5920 | } | |
5921 | ||
aa43477b | 5922 | static void io_poll_task_func(struct io_kiocb *req, bool *locked) |
18bceab1 | 5923 | { |
18bceab1 | 5924 | struct io_ring_ctx *ctx = req->ctx; |
aa43477b | 5925 | int ret; |
18bceab1 | 5926 | |
aa43477b PB |
5927 | ret = io_poll_check_events(req); |
5928 | if (ret > 0) | |
5929 | return; | |
5930 | ||
5931 | if (!ret) { | |
5932 | req->result = mangle_poll(req->result & req->poll.events); | |
e27414be | 5933 | } else { |
aa43477b PB |
5934 | req->result = ret; |
5935 | req_set_fail(req); | |
a62682f9 | 5936 | } |
aa43477b PB |
5937 | |
5938 | io_poll_remove_entries(req); | |
5939 | spin_lock(&ctx->completion_lock); | |
5940 | hash_del(&req->hash_node); | |
5941 | __io_req_complete_post(req, req->result, 0); | |
5942 | io_commit_cqring(ctx); | |
5943 | spin_unlock(&ctx->completion_lock); | |
5944 | io_cqring_ev_posted(ctx); | |
18bceab1 JA |
5945 | } |
5946 | ||
aa43477b | 5947 | static void io_apoll_task_func(struct io_kiocb *req, bool *locked) |
18bceab1 JA |
5948 | { |
5949 | struct io_ring_ctx *ctx = req->ctx; | |
aa43477b | 5950 | int ret; |
18bceab1 | 5951 | |
aa43477b PB |
5952 | ret = io_poll_check_events(req); |
5953 | if (ret > 0) | |
5954 | return; | |
18bceab1 | 5955 | |
aa43477b PB |
5956 | io_poll_remove_entries(req); |
5957 | spin_lock(&ctx->completion_lock); | |
5958 | hash_del(&req->hash_node); | |
5959 | spin_unlock(&ctx->completion_lock); | |
18bceab1 | 5960 | |
aa43477b PB |
5961 | if (!ret) |
5962 | io_req_task_submit(req, locked); | |
5963 | else | |
5964 | io_req_complete_failed(req, ret); | |
18bceab1 JA |
5965 | } |
5966 | ||
81459350 | 5967 | static void __io_poll_execute(struct io_kiocb *req, int mask, int events) |
aa43477b PB |
5968 | { |
5969 | req->result = mask; | |
81459350 JA |
5970 | /* |
5971 | * This is useful for poll that is armed on behalf of another | |
5972 | * request, and where the wakeup path could be on a different | |
5973 | * CPU. We want to avoid pulling in req->apoll->events for that | |
5974 | * case. | |
5975 | */ | |
5976 | req->cflags = events; | |
aa43477b PB |
5977 | if (req->opcode == IORING_OP_POLL_ADD) |
5978 | req->io_task_work.func = io_poll_task_func; | |
5979 | else | |
5980 | req->io_task_work.func = io_apoll_task_func; | |
5981 | ||
502c87d6 | 5982 | trace_io_uring_task_add(req->ctx, req, req->user_data, req->opcode, mask); |
aa43477b PB |
5983 | io_req_task_work_add(req, false); |
5984 | } | |
5985 | ||
81459350 | 5986 | static inline void io_poll_execute(struct io_kiocb *req, int res, int events) |
aa43477b PB |
5987 | { |
5988 | if (io_poll_get_ownership(req)) | |
81459350 | 5989 | __io_poll_execute(req, res, events); |
aa43477b PB |
5990 | } |
5991 | ||
5992 | static void io_poll_cancel_req(struct io_kiocb *req) | |
5993 | { | |
5994 | io_poll_mark_cancelled(req); | |
5995 | /* kick tw, which should complete the request */ | |
81459350 | 5996 | io_poll_execute(req, 0, 0); |
aa43477b PB |
5997 | } |
5998 | ||
5999 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, | |
6000 | void *key) | |
18bceab1 JA |
6001 | { |
6002 | struct io_kiocb *req = wait->private; | |
aa43477b PB |
6003 | struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb, |
6004 | wait); | |
18bceab1 JA |
6005 | __poll_t mask = key_to_poll(key); |
6006 | ||
791f3465 PB |
6007 | if (unlikely(mask & POLLFREE)) { |
6008 | io_poll_mark_cancelled(req); | |
6009 | /* we have to kick tw in case it's not already */ | |
81459350 | 6010 | io_poll_execute(req, 0, poll->events); |
791f3465 PB |
6011 | |
6012 | /* | |
6013 | * If the waitqueue is being freed early but someone is already | |
6014 | * holds ownership over it, we have to tear down the request as | |
6015 | * best we can. That means immediately removing the request from | |
6016 | * its waitqueue and preventing all further accesses to the | |
6017 | * waitqueue via the request. | |
6018 | */ | |
6019 | list_del_init(&poll->wait.entry); | |
6020 | ||
6021 | /* | |
6022 | * Careful: this *must* be the last step, since as soon | |
6023 | * as req->head is NULL'ed out, the request can be | |
6024 | * completed and freed, since aio_poll_complete_work() | |
6025 | * will no longer need to take the waitqueue lock. | |
6026 | */ | |
6027 | smp_store_release(&poll->head, NULL); | |
6028 | return 1; | |
6029 | } | |
6030 | ||
aa43477b | 6031 | /* for instances that support it check for an event match first */ |
18bceab1 JA |
6032 | if (mask && !(mask & poll->events)) |
6033 | return 0; | |
6034 | ||
eb0089d6 PB |
6035 | if (io_poll_get_ownership(req)) { |
6036 | /* optional, saves extra locking for removal in tw handler */ | |
6037 | if (mask && poll->events & EPOLLONESHOT) { | |
6038 | list_del_init(&poll->wait.entry); | |
6039 | poll->head = NULL; | |
91eac1c6 | 6040 | req->flags &= ~REQ_F_SINGLE_POLL; |
eb0089d6 | 6041 | } |
81459350 | 6042 | __io_poll_execute(req, mask, poll->events); |
eb0089d6 | 6043 | } |
18bceab1 | 6044 | return 1; |
18bceab1 JA |
6045 | } |
6046 | ||
6047 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, | |
807abcb0 JA |
6048 | struct wait_queue_head *head, |
6049 | struct io_poll_iocb **poll_ptr) | |
18bceab1 JA |
6050 | { |
6051 | struct io_kiocb *req = pt->req; | |
6052 | ||
6053 | /* | |
68b11e8b PB |
6054 | * The file being polled uses multiple waitqueues for poll handling |
6055 | * (e.g. one for read, one for write). Setup a separate io_poll_iocb | |
6056 | * if this happens. | |
18bceab1 | 6057 | */ |
68b11e8b | 6058 | if (unlikely(pt->nr_entries)) { |
aa43477b | 6059 | struct io_poll_iocb *first = poll; |
58852d4d | 6060 | |
23a65db8 | 6061 | /* double add on the same waitqueue head, ignore */ |
aa43477b | 6062 | if (first->head == head) |
23a65db8 | 6063 | return; |
18bceab1 | 6064 | /* already have a 2nd entry, fail a third attempt */ |
807abcb0 | 6065 | if (*poll_ptr) { |
23a65db8 PB |
6066 | if ((*poll_ptr)->head == head) |
6067 | return; | |
18bceab1 JA |
6068 | pt->error = -EINVAL; |
6069 | return; | |
6070 | } | |
aa43477b | 6071 | |
18bceab1 JA |
6072 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
6073 | if (!poll) { | |
6074 | pt->error = -ENOMEM; | |
6075 | return; | |
6076 | } | |
91eac1c6 | 6077 | req->flags |= REQ_F_DOUBLE_POLL; |
aa43477b | 6078 | io_init_poll_iocb(poll, first->events, first->wait.func); |
807abcb0 | 6079 | *poll_ptr = poll; |
d886e185 PB |
6080 | if (req->opcode == IORING_OP_POLL_ADD) |
6081 | req->flags |= REQ_F_ASYNC_DATA; | |
18bceab1 JA |
6082 | } |
6083 | ||
91eac1c6 | 6084 | req->flags |= REQ_F_SINGLE_POLL; |
68b11e8b | 6085 | pt->nr_entries++; |
18bceab1 | 6086 | poll->head = head; |
aa43477b | 6087 | poll->wait.private = req; |
a31eb4a2 JX |
6088 | |
6089 | if (poll->events & EPOLLEXCLUSIVE) | |
6090 | add_wait_queue_exclusive(head, &poll->wait); | |
6091 | else | |
6092 | add_wait_queue(head, &poll->wait); | |
18bceab1 JA |
6093 | } |
6094 | ||
aa43477b | 6095 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
18bceab1 JA |
6096 | struct poll_table_struct *p) |
6097 | { | |
6098 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
d7718a9d | 6099 | |
aa43477b PB |
6100 | __io_queue_proc(&pt->req->poll, pt, head, |
6101 | (struct io_poll_iocb **) &pt->req->async_data); | |
d7718a9d JA |
6102 | } |
6103 | ||
aa43477b PB |
6104 | static int __io_arm_poll_handler(struct io_kiocb *req, |
6105 | struct io_poll_iocb *poll, | |
6106 | struct io_poll_table *ipt, __poll_t mask) | |
d7718a9d JA |
6107 | { |
6108 | struct io_ring_ctx *ctx = req->ctx; | |
aa43477b | 6109 | int v; |
d7718a9d | 6110 | |
4d52f338 | 6111 | INIT_HLIST_NODE(&req->hash_node); |
aa43477b | 6112 | io_init_poll_iocb(poll, mask, io_poll_wake); |
b90cd197 | 6113 | poll->file = req->file; |
18bceab1 | 6114 | poll->wait.private = req; |
d7718a9d JA |
6115 | |
6116 | ipt->pt._key = mask; | |
6117 | ipt->req = req; | |
68b11e8b PB |
6118 | ipt->error = 0; |
6119 | ipt->nr_entries = 0; | |
d7718a9d | 6120 | |
aa43477b PB |
6121 | /* |
6122 | * Take the ownership to delay any tw execution up until we're done | |
6123 | * with poll arming. see io_poll_get_ownership(). | |
6124 | */ | |
6125 | atomic_set(&req->poll_refs, 1); | |
d7718a9d | 6126 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
aa43477b PB |
6127 | |
6128 | if (mask && (poll->events & EPOLLONESHOT)) { | |
6129 | io_poll_remove_entries(req); | |
6130 | /* no one else has access to the req, forget about the ref */ | |
6131 | return mask; | |
6132 | } | |
6133 | if (!mask && unlikely(ipt->error || !ipt->nr_entries)) { | |
6134 | io_poll_remove_entries(req); | |
6135 | if (!ipt->error) | |
6136 | ipt->error = -EINVAL; | |
6137 | return 0; | |
6138 | } | |
d7718a9d | 6139 | |
79ebeaee | 6140 | spin_lock(&ctx->completion_lock); |
aa43477b PB |
6141 | io_poll_req_insert(req); |
6142 | spin_unlock(&ctx->completion_lock); | |
6143 | ||
6144 | if (mask) { | |
6145 | /* can't multishot if failed, just queue the event we've got */ | |
6146 | if (unlikely(ipt->error || !ipt->nr_entries)) | |
6147 | poll->events |= EPOLLONESHOT; | |
81459350 | 6148 | __io_poll_execute(req, mask, poll->events); |
aa43477b | 6149 | return 0; |
d7718a9d | 6150 | } |
adc8682e | 6151 | io_add_napi(req->file, req->ctx); |
d7718a9d | 6152 | |
aa43477b PB |
6153 | /* |
6154 | * Release ownership. If someone tried to queue a tw while it was | |
6155 | * locked, kick it off for them. | |
6156 | */ | |
6157 | v = atomic_dec_return(&req->poll_refs); | |
6158 | if (unlikely(v & IO_POLL_REF_MASK)) | |
81459350 | 6159 | __io_poll_execute(req, 0, poll->events); |
aa43477b PB |
6160 | return 0; |
6161 | } | |
6162 | ||
6163 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
6164 | struct poll_table_struct *p) | |
6165 | { | |
6166 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
6167 | struct async_poll *apoll = pt->req->apoll; | |
6168 | ||
6169 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); | |
d7718a9d JA |
6170 | } |
6171 | ||
59b735ae OL |
6172 | enum { |
6173 | IO_APOLL_OK, | |
6174 | IO_APOLL_ABORTED, | |
6175 | IO_APOLL_READY | |
6176 | }; | |
6177 | ||
4d9237e3 | 6178 | static int io_arm_poll_handler(struct io_kiocb *req, unsigned issue_flags) |
d7718a9d JA |
6179 | { |
6180 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
6181 | struct io_ring_ctx *ctx = req->ctx; | |
6182 | struct async_poll *apoll; | |
6183 | struct io_poll_table ipt; | |
aa43477b PB |
6184 | __poll_t mask = EPOLLONESHOT | POLLERR | POLLPRI; |
6185 | int ret; | |
d7718a9d | 6186 | |
b2d9c3da PB |
6187 | if (!def->pollin && !def->pollout) |
6188 | return IO_APOLL_ABORTED; | |
658d0a40 PB |
6189 | if (!file_can_poll(req->file) || (req->flags & REQ_F_POLLED)) |
6190 | return IO_APOLL_ABORTED; | |
b2d9c3da PB |
6191 | |
6192 | if (def->pollin) { | |
b2d9c3da PB |
6193 | mask |= POLLIN | POLLRDNORM; |
6194 | ||
6195 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ | |
6196 | if ((req->opcode == IORING_OP_RECVMSG) && | |
6197 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) | |
6198 | mask &= ~POLLIN; | |
6199 | } else { | |
b2d9c3da PB |
6200 | mask |= POLLOUT | POLLWRNORM; |
6201 | } | |
6202 | ||
4d9237e3 JA |
6203 | if (!(issue_flags & IO_URING_F_UNLOCKED) && |
6204 | !list_empty(&ctx->apoll_cache)) { | |
6205 | apoll = list_first_entry(&ctx->apoll_cache, struct async_poll, | |
6206 | poll.wait.entry); | |
6207 | list_del_init(&apoll->poll.wait.entry); | |
6208 | } else { | |
6209 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); | |
6210 | if (unlikely(!apoll)) | |
6211 | return IO_APOLL_ABORTED; | |
6212 | } | |
807abcb0 | 6213 | apoll->double_poll = NULL; |
d7718a9d | 6214 | req->apoll = apoll; |
b2d9c3da | 6215 | req->flags |= REQ_F_POLLED; |
d7718a9d JA |
6216 | ipt.pt._qproc = io_async_queue_proc; |
6217 | ||
aa43477b | 6218 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask); |
41a5169c HX |
6219 | if (ret || ipt.error) |
6220 | return ret ? IO_APOLL_READY : IO_APOLL_ABORTED; | |
6221 | ||
502c87d6 | 6222 | trace_io_uring_poll_arm(ctx, req, req->user_data, req->opcode, |
236daeae | 6223 | mask, apoll->poll.events); |
59b735ae | 6224 | return IO_APOLL_OK; |
d7718a9d JA |
6225 | } |
6226 | ||
76e1b642 JA |
6227 | /* |
6228 | * Returns true if we found and killed one or more poll requests | |
6229 | */ | |
c072481d PB |
6230 | static __cold bool io_poll_remove_all(struct io_ring_ctx *ctx, |
6231 | struct task_struct *tsk, bool cancel_all) | |
221c5eb2 | 6232 | { |
78076bb6 | 6233 | struct hlist_node *tmp; |
221c5eb2 | 6234 | struct io_kiocb *req; |
aa43477b PB |
6235 | bool found = false; |
6236 | int i; | |
221c5eb2 | 6237 | |
79ebeaee | 6238 | spin_lock(&ctx->completion_lock); |
78076bb6 JA |
6239 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
6240 | struct hlist_head *list; | |
6241 | ||
6242 | list = &ctx->cancel_hash[i]; | |
f3606e3a | 6243 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
42a7b4ed | 6244 | if (io_match_task_safe(req, tsk, cancel_all)) { |
aa43477b PB |
6245 | io_poll_cancel_req(req); |
6246 | found = true; | |
6247 | } | |
f3606e3a | 6248 | } |
221c5eb2 | 6249 | } |
79ebeaee | 6250 | spin_unlock(&ctx->completion_lock); |
aa43477b | 6251 | return found; |
221c5eb2 JA |
6252 | } |
6253 | ||
9ba5fac8 PB |
6254 | static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr, |
6255 | bool poll_only) | |
e07785b0 | 6256 | __must_hold(&ctx->completion_lock) |
47f46768 | 6257 | { |
78076bb6 | 6258 | struct hlist_head *list; |
47f46768 JA |
6259 | struct io_kiocb *req; |
6260 | ||
78076bb6 JA |
6261 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
6262 | hlist_for_each_entry(req, list, hash_node) { | |
b41e9852 JA |
6263 | if (sqe_addr != req->user_data) |
6264 | continue; | |
9ba5fac8 PB |
6265 | if (poll_only && req->opcode != IORING_OP_POLL_ADD) |
6266 | continue; | |
b2cb805f | 6267 | return req; |
47f46768 | 6268 | } |
b2cb805f JA |
6269 | return NULL; |
6270 | } | |
6271 | ||
aa43477b PB |
6272 | static bool io_poll_disarm(struct io_kiocb *req) |
6273 | __must_hold(&ctx->completion_lock) | |
6274 | { | |
6275 | if (!io_poll_get_ownership(req)) | |
6276 | return false; | |
6277 | io_poll_remove_entries(req); | |
6278 | hash_del(&req->hash_node); | |
6279 | return true; | |
6280 | } | |
6281 | ||
9ba5fac8 PB |
6282 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr, |
6283 | bool poll_only) | |
e07785b0 | 6284 | __must_hold(&ctx->completion_lock) |
b2cb805f | 6285 | { |
aa43477b | 6286 | struct io_kiocb *req = io_poll_find(ctx, sqe_addr, poll_only); |
b2cb805f | 6287 | |
b2cb805f JA |
6288 | if (!req) |
6289 | return -ENOENT; | |
aa43477b PB |
6290 | io_poll_cancel_req(req); |
6291 | return 0; | |
47f46768 JA |
6292 | } |
6293 | ||
9096af3e PB |
6294 | static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe, |
6295 | unsigned int flags) | |
6296 | { | |
6297 | u32 events; | |
47f46768 | 6298 | |
9096af3e PB |
6299 | events = READ_ONCE(sqe->poll32_events); |
6300 | #ifdef __BIG_ENDIAN | |
6301 | events = swahw32(events); | |
6302 | #endif | |
6303 | if (!(flags & IORING_POLL_ADD_MULTI)) | |
6304 | events |= EPOLLONESHOT; | |
6305 | return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT)); | |
47f46768 JA |
6306 | } |
6307 | ||
c5de0036 | 6308 | static int io_poll_update_prep(struct io_kiocb *req, |
3529d8c2 | 6309 | const struct io_uring_sqe *sqe) |
0969e783 | 6310 | { |
c5de0036 PB |
6311 | struct io_poll_update *upd = &req->poll_update; |
6312 | u32 flags; | |
6313 | ||
0969e783 JA |
6314 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
6315 | return -EINVAL; | |
26578cda | 6316 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
c5de0036 PB |
6317 | return -EINVAL; |
6318 | flags = READ_ONCE(sqe->len); | |
6319 | if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA | | |
6320 | IORING_POLL_ADD_MULTI)) | |
6321 | return -EINVAL; | |
6322 | /* meaningless without update */ | |
6323 | if (flags == IORING_POLL_ADD_MULTI) | |
0969e783 JA |
6324 | return -EINVAL; |
6325 | ||
c5de0036 PB |
6326 | upd->old_user_data = READ_ONCE(sqe->addr); |
6327 | upd->update_events = flags & IORING_POLL_UPDATE_EVENTS; | |
6328 | upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA; | |
221c5eb2 | 6329 | |
c5de0036 PB |
6330 | upd->new_user_data = READ_ONCE(sqe->off); |
6331 | if (!upd->update_user_data && upd->new_user_data) | |
6332 | return -EINVAL; | |
6333 | if (upd->update_events) | |
6334 | upd->events = io_poll_parse_events(sqe, flags); | |
6335 | else if (sqe->poll32_events) | |
6336 | return -EINVAL; | |
221c5eb2 | 6337 | |
221c5eb2 JA |
6338 | return 0; |
6339 | } | |
6340 | ||
3529d8c2 | 6341 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
6342 | { |
6343 | struct io_poll_iocb *poll = &req->poll; | |
c5de0036 | 6344 | u32 flags; |
221c5eb2 JA |
6345 | |
6346 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
6347 | return -EINVAL; | |
c5de0036 | 6348 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr) |
88e41cf9 JA |
6349 | return -EINVAL; |
6350 | flags = READ_ONCE(sqe->len); | |
c5de0036 | 6351 | if (flags & ~IORING_POLL_ADD_MULTI) |
221c5eb2 | 6352 | return -EINVAL; |
04c76b41 PB |
6353 | if ((flags & IORING_POLL_ADD_MULTI) && (req->flags & REQ_F_CQE_SKIP)) |
6354 | return -EINVAL; | |
221c5eb2 | 6355 | |
48dcd38d | 6356 | io_req_set_refcount(req); |
81459350 | 6357 | req->cflags = poll->events = io_poll_parse_events(sqe, flags); |
0969e783 JA |
6358 | return 0; |
6359 | } | |
6360 | ||
61e98203 | 6361 | static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags) |
0969e783 JA |
6362 | { |
6363 | struct io_poll_iocb *poll = &req->poll; | |
0969e783 | 6364 | struct io_poll_table ipt; |
aa43477b | 6365 | int ret; |
0969e783 | 6366 | |
d7718a9d | 6367 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 6368 | |
aa43477b PB |
6369 | ret = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events); |
6370 | ret = ret ?: ipt.error; | |
6371 | if (ret) | |
6372 | __io_req_complete(req, issue_flags, ret, 0); | |
6373 | return 0; | |
221c5eb2 JA |
6374 | } |
6375 | ||
c5de0036 | 6376 | static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags) |
b69de288 JA |
6377 | { |
6378 | struct io_ring_ctx *ctx = req->ctx; | |
6379 | struct io_kiocb *preq; | |
2bbb146d | 6380 | int ret2, ret = 0; |
cc8e9ba7 | 6381 | bool locked; |
b69de288 | 6382 | |
79ebeaee | 6383 | spin_lock(&ctx->completion_lock); |
9ba5fac8 | 6384 | preq = io_poll_find(ctx, req->poll_update.old_user_data, true); |
aa43477b | 6385 | if (!preq || !io_poll_disarm(preq)) { |
79ebeaee | 6386 | spin_unlock(&ctx->completion_lock); |
aa43477b | 6387 | ret = preq ? -EALREADY : -ENOENT; |
2bbb146d | 6388 | goto out; |
b69de288 | 6389 | } |
79ebeaee | 6390 | spin_unlock(&ctx->completion_lock); |
cb3b200e | 6391 | |
2bbb146d PB |
6392 | if (req->poll_update.update_events || req->poll_update.update_user_data) { |
6393 | /* only mask one event flags, keep behavior flags */ | |
6394 | if (req->poll_update.update_events) { | |
6395 | preq->poll.events &= ~0xffff; | |
6396 | preq->poll.events |= req->poll_update.events & 0xffff; | |
6397 | preq->poll.events |= IO_POLL_UNMASK; | |
cb3b200e | 6398 | } |
2bbb146d PB |
6399 | if (req->poll_update.update_user_data) |
6400 | preq->user_data = req->poll_update.new_user_data; | |
b69de288 | 6401 | |
2bbb146d PB |
6402 | ret2 = io_poll_add(preq, issue_flags); |
6403 | /* successfully updated, don't complete poll request */ | |
6404 | if (!ret2) | |
6405 | goto out; | |
b69de288 | 6406 | } |
6224590d | 6407 | |
2bbb146d | 6408 | req_set_fail(preq); |
cc8e9ba7 PB |
6409 | preq->result = -ECANCELED; |
6410 | locked = !(issue_flags & IO_URING_F_UNLOCKED); | |
6411 | io_req_task_complete(preq, &locked); | |
2bbb146d PB |
6412 | out: |
6413 | if (ret < 0) | |
6224590d | 6414 | req_set_fail(req); |
2bbb146d | 6415 | /* complete update request, we're done with it */ |
cc8e9ba7 | 6416 | __io_req_complete(req, issue_flags, ret, 0); |
b69de288 | 6417 | return 0; |
89850fce JA |
6418 | } |
6419 | ||
5262f567 JA |
6420 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
6421 | { | |
ad8a48ac JA |
6422 | struct io_timeout_data *data = container_of(timer, |
6423 | struct io_timeout_data, timer); | |
6424 | struct io_kiocb *req = data->req; | |
6425 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
6426 | unsigned long flags; |
6427 | ||
89850fce | 6428 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
a71976f3 | 6429 | list_del_init(&req->timeout.list); |
01cec8c1 PB |
6430 | atomic_set(&req->ctx->cq_timeouts, |
6431 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
89850fce | 6432 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); |
01cec8c1 | 6433 | |
a90c8bf6 PB |
6434 | if (!(data->flags & IORING_TIMEOUT_ETIME_SUCCESS)) |
6435 | req_set_fail(req); | |
6436 | ||
6437 | req->result = -ETIME; | |
6438 | req->io_task_work.func = io_req_task_complete; | |
4813c377 | 6439 | io_req_task_work_add(req, false); |
5262f567 JA |
6440 | return HRTIMER_NORESTART; |
6441 | } | |
6442 | ||
fbd15848 PB |
6443 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
6444 | __u64 user_data) | |
89850fce | 6445 | __must_hold(&ctx->timeout_lock) |
f254ac04 | 6446 | { |
fbd15848 | 6447 | struct io_timeout_data *io; |
47f46768 | 6448 | struct io_kiocb *req; |
fd9c7bc5 | 6449 | bool found = false; |
f254ac04 | 6450 | |
135fcde8 | 6451 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
fd9c7bc5 PB |
6452 | found = user_data == req->user_data; |
6453 | if (found) | |
47f46768 | 6454 | break; |
47f46768 | 6455 | } |
fd9c7bc5 PB |
6456 | if (!found) |
6457 | return ERR_PTR(-ENOENT); | |
fbd15848 PB |
6458 | |
6459 | io = req->async_data; | |
fd9c7bc5 | 6460 | if (hrtimer_try_to_cancel(&io->timer) == -1) |
fbd15848 | 6461 | return ERR_PTR(-EALREADY); |
a71976f3 | 6462 | list_del_init(&req->timeout.list); |
fbd15848 PB |
6463 | return req; |
6464 | } | |
47f46768 | 6465 | |
fbd15848 | 6466 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
ec3c3d0f | 6467 | __must_hold(&ctx->completion_lock) |
89850fce | 6468 | __must_hold(&ctx->timeout_lock) |
fbd15848 PB |
6469 | { |
6470 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); | |
6471 | ||
6472 | if (IS_ERR(req)) | |
6473 | return PTR_ERR(req); | |
6695490d | 6474 | io_req_task_queue_fail(req, -ECANCELED); |
f254ac04 JA |
6475 | return 0; |
6476 | } | |
6477 | ||
50c1df2b JA |
6478 | static clockid_t io_timeout_get_clock(struct io_timeout_data *data) |
6479 | { | |
6480 | switch (data->flags & IORING_TIMEOUT_CLOCK_MASK) { | |
6481 | case IORING_TIMEOUT_BOOTTIME: | |
6482 | return CLOCK_BOOTTIME; | |
6483 | case IORING_TIMEOUT_REALTIME: | |
6484 | return CLOCK_REALTIME; | |
6485 | default: | |
6486 | /* can't happen, vetted at prep time */ | |
6487 | WARN_ON_ONCE(1); | |
6488 | fallthrough; | |
6489 | case 0: | |
6490 | return CLOCK_MONOTONIC; | |
6491 | } | |
6492 | } | |
6493 | ||
f1042b6c PB |
6494 | static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
6495 | struct timespec64 *ts, enum hrtimer_mode mode) | |
6496 | __must_hold(&ctx->timeout_lock) | |
6497 | { | |
6498 | struct io_timeout_data *io; | |
6499 | struct io_kiocb *req; | |
6500 | bool found = false; | |
6501 | ||
6502 | list_for_each_entry(req, &ctx->ltimeout_list, timeout.list) { | |
6503 | found = user_data == req->user_data; | |
6504 | if (found) | |
6505 | break; | |
6506 | } | |
6507 | if (!found) | |
6508 | return -ENOENT; | |
6509 | ||
6510 | io = req->async_data; | |
6511 | if (hrtimer_try_to_cancel(&io->timer) == -1) | |
6512 | return -EALREADY; | |
6513 | hrtimer_init(&io->timer, io_timeout_get_clock(io), mode); | |
6514 | io->timer.function = io_link_timeout_fn; | |
6515 | hrtimer_start(&io->timer, timespec64_to_ktime(*ts), mode); | |
6516 | return 0; | |
6517 | } | |
6518 | ||
9c8e11b3 PB |
6519 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
6520 | struct timespec64 *ts, enum hrtimer_mode mode) | |
89850fce | 6521 | __must_hold(&ctx->timeout_lock) |
47f46768 | 6522 | { |
9c8e11b3 PB |
6523 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
6524 | struct io_timeout_data *data; | |
47f46768 | 6525 | |
9c8e11b3 PB |
6526 | if (IS_ERR(req)) |
6527 | return PTR_ERR(req); | |
47f46768 | 6528 | |
9c8e11b3 PB |
6529 | req->timeout.off = 0; /* noseq */ |
6530 | data = req->async_data; | |
6531 | list_add_tail(&req->timeout.list, &ctx->timeout_list); | |
50c1df2b | 6532 | hrtimer_init(&data->timer, io_timeout_get_clock(data), mode); |
9c8e11b3 PB |
6533 | data->timer.function = io_timeout_fn; |
6534 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); | |
6535 | return 0; | |
47f46768 JA |
6536 | } |
6537 | ||
3529d8c2 JA |
6538 | static int io_timeout_remove_prep(struct io_kiocb *req, |
6539 | const struct io_uring_sqe *sqe) | |
b29472ee | 6540 | { |
9c8e11b3 PB |
6541 | struct io_timeout_rem *tr = &req->timeout_rem; |
6542 | ||
b29472ee JA |
6543 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
6544 | return -EINVAL; | |
61710e43 DA |
6545 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6546 | return -EINVAL; | |
26578cda | 6547 | if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in) |
b29472ee JA |
6548 | return -EINVAL; |
6549 | ||
f1042b6c | 6550 | tr->ltimeout = false; |
9c8e11b3 PB |
6551 | tr->addr = READ_ONCE(sqe->addr); |
6552 | tr->flags = READ_ONCE(sqe->timeout_flags); | |
f1042b6c PB |
6553 | if (tr->flags & IORING_TIMEOUT_UPDATE_MASK) { |
6554 | if (hweight32(tr->flags & IORING_TIMEOUT_CLOCK_MASK) > 1) | |
6555 | return -EINVAL; | |
6556 | if (tr->flags & IORING_LINK_TIMEOUT_UPDATE) | |
6557 | tr->ltimeout = true; | |
6558 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE_MASK|IORING_TIMEOUT_ABS)) | |
9c8e11b3 PB |
6559 | return -EINVAL; |
6560 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) | |
6561 | return -EFAULT; | |
2087009c YB |
6562 | if (tr->ts.tv_sec < 0 || tr->ts.tv_nsec < 0) |
6563 | return -EINVAL; | |
9c8e11b3 PB |
6564 | } else if (tr->flags) { |
6565 | /* timeout removal doesn't support flags */ | |
b29472ee | 6566 | return -EINVAL; |
9c8e11b3 | 6567 | } |
b29472ee | 6568 | |
b29472ee JA |
6569 | return 0; |
6570 | } | |
6571 | ||
8662daec PB |
6572 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
6573 | { | |
6574 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS | |
6575 | : HRTIMER_MODE_REL; | |
6576 | } | |
6577 | ||
11365043 JA |
6578 | /* |
6579 | * Remove or update an existing timeout command | |
6580 | */ | |
61e98203 | 6581 | static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags) |
11365043 | 6582 | { |
9c8e11b3 | 6583 | struct io_timeout_rem *tr = &req->timeout_rem; |
11365043 | 6584 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 | 6585 | int ret; |
11365043 | 6586 | |
ec3c3d0f PB |
6587 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) { |
6588 | spin_lock(&ctx->completion_lock); | |
6589 | spin_lock_irq(&ctx->timeout_lock); | |
9c8e11b3 | 6590 | ret = io_timeout_cancel(ctx, tr->addr); |
ec3c3d0f PB |
6591 | spin_unlock_irq(&ctx->timeout_lock); |
6592 | spin_unlock(&ctx->completion_lock); | |
6593 | } else { | |
f1042b6c PB |
6594 | enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags); |
6595 | ||
ec3c3d0f | 6596 | spin_lock_irq(&ctx->timeout_lock); |
f1042b6c PB |
6597 | if (tr->ltimeout) |
6598 | ret = io_linked_timeout_update(ctx, tr->addr, &tr->ts, mode); | |
6599 | else | |
6600 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode); | |
ec3c3d0f PB |
6601 | spin_unlock_irq(&ctx->timeout_lock); |
6602 | } | |
11365043 | 6603 | |
4e88d6e7 | 6604 | if (ret < 0) |
93d2bcd2 | 6605 | req_set_fail(req); |
505657bc | 6606 | io_req_complete_post(req, ret, 0); |
11365043 | 6607 | return 0; |
5262f567 JA |
6608 | } |
6609 | ||
3529d8c2 | 6610 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 6611 | bool is_timeout_link) |
5262f567 | 6612 | { |
ad8a48ac | 6613 | struct io_timeout_data *data; |
a41525ab | 6614 | unsigned flags; |
56080b02 | 6615 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 6616 | |
ad8a48ac | 6617 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 6618 | return -EINVAL; |
26578cda PB |
6619 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1 || |
6620 | sqe->splice_fd_in) | |
a41525ab | 6621 | return -EINVAL; |
56080b02 | 6622 | if (off && is_timeout_link) |
2d28390a | 6623 | return -EINVAL; |
a41525ab | 6624 | flags = READ_ONCE(sqe->timeout_flags); |
6224590d PB |
6625 | if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK | |
6626 | IORING_TIMEOUT_ETIME_SUCCESS)) | |
50c1df2b JA |
6627 | return -EINVAL; |
6628 | /* more than one clock specified is invalid, obviously */ | |
6629 | if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1) | |
5262f567 | 6630 | return -EINVAL; |
bdf20073 | 6631 | |
ef9dd637 | 6632 | INIT_LIST_HEAD(&req->timeout.list); |
bfe68a22 | 6633 | req->timeout.off = off; |
f18ee4cf PB |
6634 | if (unlikely(off && !req->ctx->off_timeout_used)) |
6635 | req->ctx->off_timeout_used = true; | |
26a61679 | 6636 | |
d6a644a7 PB |
6637 | if (WARN_ON_ONCE(req_has_async_data(req))) |
6638 | return -EFAULT; | |
6639 | if (io_alloc_async_data(req)) | |
26a61679 JA |
6640 | return -ENOMEM; |
6641 | ||
e8c2bc1f | 6642 | data = req->async_data; |
ad8a48ac | 6643 | data->req = req; |
50c1df2b | 6644 | data->flags = flags; |
ad8a48ac JA |
6645 | |
6646 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
6647 | return -EFAULT; |
6648 | ||
f6223ff7 YB |
6649 | if (data->ts.tv_sec < 0 || data->ts.tv_nsec < 0) |
6650 | return -EINVAL; | |
6651 | ||
8662daec | 6652 | data->mode = io_translate_timeout_mode(flags); |
50c1df2b | 6653 | hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode); |
b97e736a PB |
6654 | |
6655 | if (is_timeout_link) { | |
6656 | struct io_submit_link *link = &req->ctx->submit_state.link; | |
6657 | ||
6658 | if (!link->head) | |
6659 | return -EINVAL; | |
6660 | if (link->last->opcode == IORING_OP_LINK_TIMEOUT) | |
6661 | return -EINVAL; | |
4d13d1a4 PB |
6662 | req->timeout.head = link->last; |
6663 | link->last->flags |= REQ_F_ARM_LTIMEOUT; | |
b97e736a | 6664 | } |
ad8a48ac JA |
6665 | return 0; |
6666 | } | |
6667 | ||
61e98203 | 6668 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
ad8a48ac | 6669 | { |
ad8a48ac | 6670 | struct io_ring_ctx *ctx = req->ctx; |
e8c2bc1f | 6671 | struct io_timeout_data *data = req->async_data; |
ad8a48ac | 6672 | struct list_head *entry; |
bfe68a22 | 6673 | u32 tail, off = req->timeout.off; |
ad8a48ac | 6674 | |
89850fce | 6675 | spin_lock_irq(&ctx->timeout_lock); |
93bd25bb | 6676 | |
5262f567 JA |
6677 | /* |
6678 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
6679 | * timeout event to be satisfied. If it isn't set, then this is |
6680 | * a pure timeout request, sequence isn't used. | |
5262f567 | 6681 | */ |
8eb7e2d0 | 6682 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
6683 | entry = ctx->timeout_list.prev; |
6684 | goto add; | |
6685 | } | |
5262f567 | 6686 | |
bfe68a22 PB |
6687 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
6688 | req->timeout.target_seq = tail + off; | |
5262f567 | 6689 | |
f010505b MDG |
6690 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
6691 | * This is safe because ->completion_lock is held, and submissions | |
6692 | * and completions are never mixed in the same ->completion_lock section. | |
6693 | */ | |
6694 | ctx->cq_last_tm_flush = tail; | |
6695 | ||
5262f567 JA |
6696 | /* |
6697 | * Insertion sort, ensuring the first entry in the list is always | |
6698 | * the one we need first. | |
6699 | */ | |
5262f567 | 6700 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
6701 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
6702 | timeout.list); | |
5262f567 | 6703 | |
8eb7e2d0 | 6704 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 6705 | continue; |
bfe68a22 PB |
6706 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
6707 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
6708 | break; |
6709 | } | |
93bd25bb | 6710 | add: |
135fcde8 | 6711 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
6712 | data->timer.function = io_timeout_fn; |
6713 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
89850fce | 6714 | spin_unlock_irq(&ctx->timeout_lock); |
5262f567 JA |
6715 | return 0; |
6716 | } | |
5262f567 | 6717 | |
f458dd84 PB |
6718 | struct io_cancel_data { |
6719 | struct io_ring_ctx *ctx; | |
6720 | u64 user_data; | |
6721 | }; | |
6722 | ||
62755e35 JA |
6723 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
6724 | { | |
6725 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
f458dd84 | 6726 | struct io_cancel_data *cd = data; |
62755e35 | 6727 | |
f458dd84 | 6728 | return req->ctx == cd->ctx && req->user_data == cd->user_data; |
62755e35 JA |
6729 | } |
6730 | ||
f458dd84 PB |
6731 | static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data, |
6732 | struct io_ring_ctx *ctx) | |
62755e35 | 6733 | { |
f458dd84 | 6734 | struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, }; |
62755e35 | 6735 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
6736 | int ret = 0; |
6737 | ||
f458dd84 | 6738 | if (!tctx || !tctx->io_wq) |
5aa75ed5 JA |
6739 | return -ENOENT; |
6740 | ||
f458dd84 | 6741 | cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false); |
62755e35 JA |
6742 | switch (cancel_ret) { |
6743 | case IO_WQ_CANCEL_OK: | |
6744 | ret = 0; | |
6745 | break; | |
6746 | case IO_WQ_CANCEL_RUNNING: | |
6747 | ret = -EALREADY; | |
6748 | break; | |
6749 | case IO_WQ_CANCEL_NOTFOUND: | |
6750 | ret = -ENOENT; | |
6751 | break; | |
6752 | } | |
6753 | ||
e977d6d3 JA |
6754 | return ret; |
6755 | } | |
6756 | ||
8cb01fac | 6757 | static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr) |
47f46768 | 6758 | { |
8cb01fac | 6759 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 JA |
6760 | int ret; |
6761 | ||
dadebc35 | 6762 | WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current); |
8cb01fac | 6763 | |
f458dd84 | 6764 | ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx); |
ccbf7261 JA |
6765 | /* |
6766 | * Fall-through even for -EALREADY, as we may have poll armed | |
6767 | * that need unarming. | |
6768 | */ | |
6769 | if (!ret) | |
6770 | return 0; | |
505657bc PB |
6771 | |
6772 | spin_lock(&ctx->completion_lock); | |
ccbf7261 JA |
6773 | ret = io_poll_cancel(ctx, sqe_addr, false); |
6774 | if (ret != -ENOENT) | |
6775 | goto out; | |
6776 | ||
79ebeaee | 6777 | spin_lock_irq(&ctx->timeout_lock); |
47f46768 | 6778 | ret = io_timeout_cancel(ctx, sqe_addr); |
79ebeaee | 6779 | spin_unlock_irq(&ctx->timeout_lock); |
505657bc PB |
6780 | out: |
6781 | spin_unlock(&ctx->completion_lock); | |
6782 | return ret; | |
47f46768 JA |
6783 | } |
6784 | ||
3529d8c2 JA |
6785 | static int io_async_cancel_prep(struct io_kiocb *req, |
6786 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 6787 | { |
fbf23849 | 6788 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 6789 | return -EINVAL; |
61710e43 DA |
6790 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6791 | return -EINVAL; | |
26578cda PB |
6792 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags || |
6793 | sqe->splice_fd_in) | |
e977d6d3 JA |
6794 | return -EINVAL; |
6795 | ||
fbf23849 JA |
6796 | req->cancel.addr = READ_ONCE(sqe->addr); |
6797 | return 0; | |
6798 | } | |
6799 | ||
61e98203 | 6800 | static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6801 | { |
6802 | struct io_ring_ctx *ctx = req->ctx; | |
58f99373 | 6803 | u64 sqe_addr = req->cancel.addr; |
3b44b371 | 6804 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
58f99373 PB |
6805 | struct io_tctx_node *node; |
6806 | int ret; | |
6807 | ||
8cb01fac | 6808 | ret = io_try_cancel_userdata(req, sqe_addr); |
58f99373 PB |
6809 | if (ret != -ENOENT) |
6810 | goto done; | |
58f99373 PB |
6811 | |
6812 | /* slow path, try all io-wq's */ | |
3b44b371 | 6813 | io_ring_submit_lock(ctx, needs_lock); |
58f99373 PB |
6814 | ret = -ENOENT; |
6815 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
6816 | struct io_uring_task *tctx = node->task->io_uring; | |
fbf23849 | 6817 | |
58f99373 PB |
6818 | ret = io_async_cancel_one(tctx, req->cancel.addr, ctx); |
6819 | if (ret != -ENOENT) | |
6820 | break; | |
6821 | } | |
3b44b371 | 6822 | io_ring_submit_unlock(ctx, needs_lock); |
58f99373 | 6823 | done: |
58f99373 | 6824 | if (ret < 0) |
93d2bcd2 | 6825 | req_set_fail(req); |
505657bc | 6826 | io_req_complete_post(req, ret, 0); |
5262f567 JA |
6827 | return 0; |
6828 | } | |
6829 | ||
269bbe5f | 6830 | static int io_rsrc_update_prep(struct io_kiocb *req, |
05f3fb3c JA |
6831 | const struct io_uring_sqe *sqe) |
6832 | { | |
61710e43 DA |
6833 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6834 | return -EINVAL; | |
26578cda | 6835 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
05f3fb3c JA |
6836 | return -EINVAL; |
6837 | ||
269bbe5f BM |
6838 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
6839 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); | |
6840 | if (!req->rsrc_update.nr_args) | |
05f3fb3c | 6841 | return -EINVAL; |
269bbe5f | 6842 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
05f3fb3c JA |
6843 | return 0; |
6844 | } | |
6845 | ||
889fca73 | 6846 | static int io_files_update(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6847 | { |
6848 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 6849 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
c3bdad02 | 6850 | struct io_uring_rsrc_update2 up; |
05f3fb3c | 6851 | int ret; |
fbf23849 | 6852 | |
269bbe5f BM |
6853 | up.offset = req->rsrc_update.offset; |
6854 | up.data = req->rsrc_update.arg; | |
c3bdad02 PB |
6855 | up.nr = 0; |
6856 | up.tags = 0; | |
615cee49 | 6857 | up.resv = 0; |
05f3fb3c | 6858 | |
3b44b371 | 6859 | io_ring_submit_lock(ctx, needs_lock); |
fdecb662 | 6860 | ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE, |
98f0b3b4 | 6861 | &up, req->rsrc_update.nr_args); |
3b44b371 | 6862 | io_ring_submit_unlock(ctx, needs_lock); |
05f3fb3c JA |
6863 | |
6864 | if (ret < 0) | |
93d2bcd2 | 6865 | req_set_fail(req); |
889fca73 | 6866 | __io_req_complete(req, issue_flags, ret, 0); |
5262f567 JA |
6867 | return 0; |
6868 | } | |
6869 | ||
bfe76559 | 6870 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 6871 | { |
d625c6ee | 6872 | switch (req->opcode) { |
e781573e | 6873 | case IORING_OP_NOP: |
bfe76559 | 6874 | return 0; |
f67676d1 JA |
6875 | case IORING_OP_READV: |
6876 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 6877 | case IORING_OP_READ: |
bfe76559 | 6878 | return io_read_prep(req, sqe); |
f67676d1 JA |
6879 | case IORING_OP_WRITEV: |
6880 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 6881 | case IORING_OP_WRITE: |
bfe76559 | 6882 | return io_write_prep(req, sqe); |
0969e783 | 6883 | case IORING_OP_POLL_ADD: |
bfe76559 | 6884 | return io_poll_add_prep(req, sqe); |
0969e783 | 6885 | case IORING_OP_POLL_REMOVE: |
c5de0036 | 6886 | return io_poll_update_prep(req, sqe); |
8ed8d3c3 | 6887 | case IORING_OP_FSYNC: |
1155c76a | 6888 | return io_fsync_prep(req, sqe); |
8ed8d3c3 | 6889 | case IORING_OP_SYNC_FILE_RANGE: |
1155c76a | 6890 | return io_sfr_prep(req, sqe); |
03b1230c | 6891 | case IORING_OP_SENDMSG: |
fddaface | 6892 | case IORING_OP_SEND: |
bfe76559 | 6893 | return io_sendmsg_prep(req, sqe); |
03b1230c | 6894 | case IORING_OP_RECVMSG: |
fddaface | 6895 | case IORING_OP_RECV: |
bfe76559 | 6896 | return io_recvmsg_prep(req, sqe); |
f499a021 | 6897 | case IORING_OP_CONNECT: |
bfe76559 | 6898 | return io_connect_prep(req, sqe); |
2d28390a | 6899 | case IORING_OP_TIMEOUT: |
bfe76559 | 6900 | return io_timeout_prep(req, sqe, false); |
b29472ee | 6901 | case IORING_OP_TIMEOUT_REMOVE: |
bfe76559 | 6902 | return io_timeout_remove_prep(req, sqe); |
fbf23849 | 6903 | case IORING_OP_ASYNC_CANCEL: |
bfe76559 | 6904 | return io_async_cancel_prep(req, sqe); |
2d28390a | 6905 | case IORING_OP_LINK_TIMEOUT: |
bfe76559 | 6906 | return io_timeout_prep(req, sqe, true); |
8ed8d3c3 | 6907 | case IORING_OP_ACCEPT: |
bfe76559 | 6908 | return io_accept_prep(req, sqe); |
d63d1b5e | 6909 | case IORING_OP_FALLOCATE: |
bfe76559 | 6910 | return io_fallocate_prep(req, sqe); |
15b71abe | 6911 | case IORING_OP_OPENAT: |
bfe76559 | 6912 | return io_openat_prep(req, sqe); |
b5dba59e | 6913 | case IORING_OP_CLOSE: |
bfe76559 | 6914 | return io_close_prep(req, sqe); |
05f3fb3c | 6915 | case IORING_OP_FILES_UPDATE: |
269bbe5f | 6916 | return io_rsrc_update_prep(req, sqe); |
eddc7ef5 | 6917 | case IORING_OP_STATX: |
bfe76559 | 6918 | return io_statx_prep(req, sqe); |
4840e418 | 6919 | case IORING_OP_FADVISE: |
bfe76559 | 6920 | return io_fadvise_prep(req, sqe); |
c1ca757b | 6921 | case IORING_OP_MADVISE: |
bfe76559 | 6922 | return io_madvise_prep(req, sqe); |
cebdb986 | 6923 | case IORING_OP_OPENAT2: |
bfe76559 | 6924 | return io_openat2_prep(req, sqe); |
3e4827b0 | 6925 | case IORING_OP_EPOLL_CTL: |
bfe76559 | 6926 | return io_epoll_ctl_prep(req, sqe); |
7d67af2c | 6927 | case IORING_OP_SPLICE: |
bfe76559 | 6928 | return io_splice_prep(req, sqe); |
ddf0322d | 6929 | case IORING_OP_PROVIDE_BUFFERS: |
bfe76559 | 6930 | return io_provide_buffers_prep(req, sqe); |
067524e9 | 6931 | case IORING_OP_REMOVE_BUFFERS: |
bfe76559 | 6932 | return io_remove_buffers_prep(req, sqe); |
f2a8d5c7 | 6933 | case IORING_OP_TEE: |
bfe76559 | 6934 | return io_tee_prep(req, sqe); |
36f4fa68 JA |
6935 | case IORING_OP_SHUTDOWN: |
6936 | return io_shutdown_prep(req, sqe); | |
80a261fd JA |
6937 | case IORING_OP_RENAMEAT: |
6938 | return io_renameat_prep(req, sqe); | |
14a1143b JA |
6939 | case IORING_OP_UNLINKAT: |
6940 | return io_unlinkat_prep(req, sqe); | |
e34a02dc DK |
6941 | case IORING_OP_MKDIRAT: |
6942 | return io_mkdirat_prep(req, sqe); | |
7a8721f8 DK |
6943 | case IORING_OP_SYMLINKAT: |
6944 | return io_symlinkat_prep(req, sqe); | |
cf30da90 DK |
6945 | case IORING_OP_LINKAT: |
6946 | return io_linkat_prep(req, sqe); | |
4f57f06c JA |
6947 | case IORING_OP_MSG_RING: |
6948 | return io_msg_ring_prep(req, sqe); | |
f67676d1 JA |
6949 | } |
6950 | ||
bfe76559 PB |
6951 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
6952 | req->opcode); | |
bd54b6fe | 6953 | return -EINVAL; |
bfe76559 PB |
6954 | } |
6955 | ||
93642ef8 | 6956 | static int io_req_prep_async(struct io_kiocb *req) |
bfe76559 | 6957 | { |
b7e298d2 PB |
6958 | if (!io_op_defs[req->opcode].needs_async_setup) |
6959 | return 0; | |
d886e185 | 6960 | if (WARN_ON_ONCE(req_has_async_data(req))) |
b7e298d2 PB |
6961 | return -EFAULT; |
6962 | if (io_alloc_async_data(req)) | |
6963 | return -EAGAIN; | |
6964 | ||
93642ef8 PB |
6965 | switch (req->opcode) { |
6966 | case IORING_OP_READV: | |
93642ef8 PB |
6967 | return io_rw_prep_async(req, READ); |
6968 | case IORING_OP_WRITEV: | |
93642ef8 PB |
6969 | return io_rw_prep_async(req, WRITE); |
6970 | case IORING_OP_SENDMSG: | |
93642ef8 PB |
6971 | return io_sendmsg_prep_async(req); |
6972 | case IORING_OP_RECVMSG: | |
93642ef8 PB |
6973 | return io_recvmsg_prep_async(req); |
6974 | case IORING_OP_CONNECT: | |
6975 | return io_connect_prep_async(req); | |
6976 | } | |
b7e298d2 PB |
6977 | printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n", |
6978 | req->opcode); | |
6979 | return -EFAULT; | |
f67676d1 JA |
6980 | } |
6981 | ||
9cf7c104 PB |
6982 | static u32 io_get_sequence(struct io_kiocb *req) |
6983 | { | |
a3dbdf54 | 6984 | u32 seq = req->ctx->cached_sq_head; |
9cf7c104 | 6985 | |
a3dbdf54 PB |
6986 | /* need original cached_sq_head, but it was increased for each req */ |
6987 | io_for_each_link(req, req) | |
6988 | seq--; | |
6989 | return seq; | |
9cf7c104 PB |
6990 | } |
6991 | ||
c072481d | 6992 | static __cold void io_drain_req(struct io_kiocb *req) |
de0617e4 | 6993 | { |
a197f664 | 6994 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 6995 | struct io_defer_entry *de; |
f67676d1 | 6996 | int ret; |
e0eb71dc | 6997 | u32 seq = io_get_sequence(req); |
3c19966d | 6998 | |
9d858b21 | 6999 | /* Still need defer if there is pending req in defer list. */ |
e302f104 | 7000 | spin_lock(&ctx->completion_lock); |
5e371265 | 7001 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) { |
e302f104 | 7002 | spin_unlock(&ctx->completion_lock); |
e0eb71dc | 7003 | queue: |
10c66904 | 7004 | ctx->drain_active = false; |
e0eb71dc PB |
7005 | io_req_task_queue(req); |
7006 | return; | |
10c66904 | 7007 | } |
e302f104 | 7008 | spin_unlock(&ctx->completion_lock); |
9cf7c104 | 7009 | |
b7e298d2 | 7010 | ret = io_req_prep_async(req); |
e0eb71dc PB |
7011 | if (ret) { |
7012 | fail: | |
7013 | io_req_complete_failed(req, ret); | |
7014 | return; | |
7015 | } | |
cbdcb435 | 7016 | io_prep_async_link(req); |
27dc8338 | 7017 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
76cc33d7 | 7018 | if (!de) { |
1b48773f | 7019 | ret = -ENOMEM; |
e0eb71dc | 7020 | goto fail; |
76cc33d7 | 7021 | } |
2d28390a | 7022 | |
79ebeaee | 7023 | spin_lock(&ctx->completion_lock); |
9cf7c104 | 7024 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
79ebeaee | 7025 | spin_unlock(&ctx->completion_lock); |
27dc8338 | 7026 | kfree(de); |
e0eb71dc | 7027 | goto queue; |
de0617e4 JA |
7028 | } |
7029 | ||
502c87d6 | 7030 | trace_io_uring_defer(ctx, req, req->user_data, req->opcode); |
27dc8338 | 7031 | de->req = req; |
9cf7c104 | 7032 | de->seq = seq; |
27dc8338 | 7033 | list_add_tail(&de->list, &ctx->defer_list); |
79ebeaee | 7034 | spin_unlock(&ctx->completion_lock); |
de0617e4 JA |
7035 | } |
7036 | ||
68fb8979 | 7037 | static void io_clean_op(struct io_kiocb *req) |
99bc4c38 | 7038 | { |
d1fd1c20 | 7039 | if (req->flags & REQ_F_BUFFER_SELECTED) |
cc3cec83 | 7040 | io_put_kbuf_comp(req); |
99bc4c38 | 7041 | |
0e1b6fe3 PB |
7042 | if (req->flags & REQ_F_NEED_CLEANUP) { |
7043 | switch (req->opcode) { | |
7044 | case IORING_OP_READV: | |
7045 | case IORING_OP_READ_FIXED: | |
7046 | case IORING_OP_READ: | |
7047 | case IORING_OP_WRITEV: | |
7048 | case IORING_OP_WRITE_FIXED: | |
e8c2bc1f JA |
7049 | case IORING_OP_WRITE: { |
7050 | struct io_async_rw *io = req->async_data; | |
1dacb4df PB |
7051 | |
7052 | kfree(io->free_iovec); | |
0e1b6fe3 | 7053 | break; |
e8c2bc1f | 7054 | } |
0e1b6fe3 | 7055 | case IORING_OP_RECVMSG: |
e8c2bc1f JA |
7056 | case IORING_OP_SENDMSG: { |
7057 | struct io_async_msghdr *io = req->async_data; | |
257e84a5 PB |
7058 | |
7059 | kfree(io->free_iov); | |
0e1b6fe3 | 7060 | break; |
e8c2bc1f | 7061 | } |
0e1b6fe3 PB |
7062 | case IORING_OP_SPLICE: |
7063 | case IORING_OP_TEE: | |
e1d767f0 PB |
7064 | if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED)) |
7065 | io_put_file(req->splice.file_in); | |
0e1b6fe3 | 7066 | break; |
f3cd4850 JA |
7067 | case IORING_OP_OPENAT: |
7068 | case IORING_OP_OPENAT2: | |
7069 | if (req->open.filename) | |
7070 | putname(req->open.filename); | |
7071 | break; | |
80a261fd JA |
7072 | case IORING_OP_RENAMEAT: |
7073 | putname(req->rename.oldpath); | |
7074 | putname(req->rename.newpath); | |
7075 | break; | |
14a1143b JA |
7076 | case IORING_OP_UNLINKAT: |
7077 | putname(req->unlink.filename); | |
7078 | break; | |
e34a02dc DK |
7079 | case IORING_OP_MKDIRAT: |
7080 | putname(req->mkdir.filename); | |
7081 | break; | |
7a8721f8 DK |
7082 | case IORING_OP_SYMLINKAT: |
7083 | putname(req->symlink.oldpath); | |
7084 | putname(req->symlink.newpath); | |
7085 | break; | |
cf30da90 DK |
7086 | case IORING_OP_LINKAT: |
7087 | putname(req->hardlink.oldpath); | |
7088 | putname(req->hardlink.newpath); | |
7089 | break; | |
0e1b6fe3 | 7090 | } |
99bc4c38 | 7091 | } |
75652a30 JA |
7092 | if ((req->flags & REQ_F_POLLED) && req->apoll) { |
7093 | kfree(req->apoll->double_poll); | |
7094 | kfree(req->apoll); | |
7095 | req->apoll = NULL; | |
7096 | } | |
3a0a6902 PB |
7097 | if (req->flags & REQ_F_INFLIGHT) { |
7098 | struct io_uring_task *tctx = req->task->io_uring; | |
7099 | ||
7100 | atomic_dec(&tctx->inflight_tracked); | |
3a0a6902 | 7101 | } |
c854357b | 7102 | if (req->flags & REQ_F_CREDS) |
b8e64b53 | 7103 | put_cred(req->creds); |
d886e185 PB |
7104 | if (req->flags & REQ_F_ASYNC_DATA) { |
7105 | kfree(req->async_data); | |
7106 | req->async_data = NULL; | |
7107 | } | |
c854357b | 7108 | req->flags &= ~IO_REQ_CLEAN_FLAGS; |
99bc4c38 PB |
7109 | } |
7110 | ||
889fca73 | 7111 | static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 7112 | { |
5730b27e | 7113 | const struct cred *creds = NULL; |
d625c6ee | 7114 | int ret; |
2b188cc1 | 7115 | |
6878b40e | 7116 | if (unlikely((req->flags & REQ_F_CREDS) && req->creds != current_cred())) |
c10d1f98 | 7117 | creds = override_creds(req->creds); |
5730b27e | 7118 | |
5bd2182d PM |
7119 | if (!io_op_defs[req->opcode].audit_skip) |
7120 | audit_uring_entry(req->opcode); | |
7121 | ||
d625c6ee | 7122 | switch (req->opcode) { |
2b188cc1 | 7123 | case IORING_OP_NOP: |
889fca73 | 7124 | ret = io_nop(req, issue_flags); |
2b188cc1 JA |
7125 | break; |
7126 | case IORING_OP_READV: | |
edafccee | 7127 | case IORING_OP_READ_FIXED: |
3a6820f2 | 7128 | case IORING_OP_READ: |
889fca73 | 7129 | ret = io_read(req, issue_flags); |
edafccee | 7130 | break; |
3529d8c2 | 7131 | case IORING_OP_WRITEV: |
edafccee | 7132 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 7133 | case IORING_OP_WRITE: |
889fca73 | 7134 | ret = io_write(req, issue_flags); |
2b188cc1 | 7135 | break; |
c992fe29 | 7136 | case IORING_OP_FSYNC: |
45d189c6 | 7137 | ret = io_fsync(req, issue_flags); |
c992fe29 | 7138 | break; |
221c5eb2 | 7139 | case IORING_OP_POLL_ADD: |
61e98203 | 7140 | ret = io_poll_add(req, issue_flags); |
221c5eb2 JA |
7141 | break; |
7142 | case IORING_OP_POLL_REMOVE: | |
c5de0036 | 7143 | ret = io_poll_update(req, issue_flags); |
221c5eb2 | 7144 | break; |
5d17b4a4 | 7145 | case IORING_OP_SYNC_FILE_RANGE: |
45d189c6 | 7146 | ret = io_sync_file_range(req, issue_flags); |
5d17b4a4 | 7147 | break; |
0fa03c62 | 7148 | case IORING_OP_SENDMSG: |
889fca73 | 7149 | ret = io_sendmsg(req, issue_flags); |
062d04d7 | 7150 | break; |
fddaface | 7151 | case IORING_OP_SEND: |
889fca73 | 7152 | ret = io_send(req, issue_flags); |
0fa03c62 | 7153 | break; |
aa1fa28f | 7154 | case IORING_OP_RECVMSG: |
889fca73 | 7155 | ret = io_recvmsg(req, issue_flags); |
062d04d7 | 7156 | break; |
fddaface | 7157 | case IORING_OP_RECV: |
889fca73 | 7158 | ret = io_recv(req, issue_flags); |
aa1fa28f | 7159 | break; |
5262f567 | 7160 | case IORING_OP_TIMEOUT: |
61e98203 | 7161 | ret = io_timeout(req, issue_flags); |
5262f567 | 7162 | break; |
11365043 | 7163 | case IORING_OP_TIMEOUT_REMOVE: |
61e98203 | 7164 | ret = io_timeout_remove(req, issue_flags); |
11365043 | 7165 | break; |
17f2fe35 | 7166 | case IORING_OP_ACCEPT: |
889fca73 | 7167 | ret = io_accept(req, issue_flags); |
17f2fe35 | 7168 | break; |
f8e85cf2 | 7169 | case IORING_OP_CONNECT: |
889fca73 | 7170 | ret = io_connect(req, issue_flags); |
f8e85cf2 | 7171 | break; |
62755e35 | 7172 | case IORING_OP_ASYNC_CANCEL: |
61e98203 | 7173 | ret = io_async_cancel(req, issue_flags); |
62755e35 | 7174 | break; |
d63d1b5e | 7175 | case IORING_OP_FALLOCATE: |
45d189c6 | 7176 | ret = io_fallocate(req, issue_flags); |
d63d1b5e | 7177 | break; |
15b71abe | 7178 | case IORING_OP_OPENAT: |
45d189c6 | 7179 | ret = io_openat(req, issue_flags); |
15b71abe | 7180 | break; |
b5dba59e | 7181 | case IORING_OP_CLOSE: |
889fca73 | 7182 | ret = io_close(req, issue_flags); |
b5dba59e | 7183 | break; |
05f3fb3c | 7184 | case IORING_OP_FILES_UPDATE: |
889fca73 | 7185 | ret = io_files_update(req, issue_flags); |
05f3fb3c | 7186 | break; |
eddc7ef5 | 7187 | case IORING_OP_STATX: |
45d189c6 | 7188 | ret = io_statx(req, issue_flags); |
eddc7ef5 | 7189 | break; |
4840e418 | 7190 | case IORING_OP_FADVISE: |
45d189c6 | 7191 | ret = io_fadvise(req, issue_flags); |
4840e418 | 7192 | break; |
c1ca757b | 7193 | case IORING_OP_MADVISE: |
45d189c6 | 7194 | ret = io_madvise(req, issue_flags); |
c1ca757b | 7195 | break; |
cebdb986 | 7196 | case IORING_OP_OPENAT2: |
45d189c6 | 7197 | ret = io_openat2(req, issue_flags); |
cebdb986 | 7198 | break; |
3e4827b0 | 7199 | case IORING_OP_EPOLL_CTL: |
889fca73 | 7200 | ret = io_epoll_ctl(req, issue_flags); |
3e4827b0 | 7201 | break; |
7d67af2c | 7202 | case IORING_OP_SPLICE: |
45d189c6 | 7203 | ret = io_splice(req, issue_flags); |
7d67af2c | 7204 | break; |
ddf0322d | 7205 | case IORING_OP_PROVIDE_BUFFERS: |
889fca73 | 7206 | ret = io_provide_buffers(req, issue_flags); |
ddf0322d | 7207 | break; |
067524e9 | 7208 | case IORING_OP_REMOVE_BUFFERS: |
889fca73 | 7209 | ret = io_remove_buffers(req, issue_flags); |
3e4827b0 | 7210 | break; |
f2a8d5c7 | 7211 | case IORING_OP_TEE: |
45d189c6 | 7212 | ret = io_tee(req, issue_flags); |
f2a8d5c7 | 7213 | break; |
36f4fa68 | 7214 | case IORING_OP_SHUTDOWN: |
45d189c6 | 7215 | ret = io_shutdown(req, issue_flags); |
36f4fa68 | 7216 | break; |
80a261fd | 7217 | case IORING_OP_RENAMEAT: |
45d189c6 | 7218 | ret = io_renameat(req, issue_flags); |
80a261fd | 7219 | break; |
14a1143b | 7220 | case IORING_OP_UNLINKAT: |
45d189c6 | 7221 | ret = io_unlinkat(req, issue_flags); |
14a1143b | 7222 | break; |
e34a02dc DK |
7223 | case IORING_OP_MKDIRAT: |
7224 | ret = io_mkdirat(req, issue_flags); | |
7225 | break; | |
7a8721f8 DK |
7226 | case IORING_OP_SYMLINKAT: |
7227 | ret = io_symlinkat(req, issue_flags); | |
7228 | break; | |
cf30da90 DK |
7229 | case IORING_OP_LINKAT: |
7230 | ret = io_linkat(req, issue_flags); | |
7231 | break; | |
4f57f06c JA |
7232 | case IORING_OP_MSG_RING: |
7233 | ret = io_msg_ring(req, issue_flags); | |
7234 | break; | |
2b188cc1 JA |
7235 | default: |
7236 | ret = -EINVAL; | |
7237 | break; | |
7238 | } | |
7239 | ||
5bd2182d PM |
7240 | if (!io_op_defs[req->opcode].audit_skip) |
7241 | audit_uring_exit(!ret, ret); | |
7242 | ||
5730b27e JA |
7243 | if (creds) |
7244 | revert_creds(creds); | |
def596e9 JA |
7245 | if (ret) |
7246 | return ret; | |
b532576e | 7247 | /* If the op doesn't have a file, we're not polling for it */ |
9983028e | 7248 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && req->file) |
9882131c | 7249 | io_iopoll_req_issued(req, issue_flags); |
def596e9 JA |
7250 | |
7251 | return 0; | |
2b188cc1 JA |
7252 | } |
7253 | ||
ebc11b6c PB |
7254 | static struct io_wq_work *io_wq_free_work(struct io_wq_work *work) |
7255 | { | |
7256 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
7257 | ||
7258 | req = io_put_req_find_next(req); | |
7259 | return req ? &req->work : NULL; | |
7260 | } | |
7261 | ||
5280f7e5 | 7262 | static void io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
7263 | { |
7264 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
d01905db PB |
7265 | unsigned int issue_flags = IO_URING_F_UNLOCKED; |
7266 | bool needs_poll = false; | |
6df1db6b | 7267 | struct io_kiocb *timeout; |
561fb04a | 7268 | int ret = 0; |
2b188cc1 | 7269 | |
48dcd38d PB |
7270 | /* one will be dropped by ->io_free_work() after returning to io-wq */ |
7271 | if (!(req->flags & REQ_F_REFCOUNT)) | |
7272 | __io_req_set_refcount(req, 2); | |
7273 | else | |
7274 | req_ref_get(req); | |
5d5901a3 | 7275 | |
6df1db6b PB |
7276 | timeout = io_prep_linked_timeout(req); |
7277 | if (timeout) | |
7278 | io_queue_linked_timeout(timeout); | |
d4c81f38 | 7279 | |
dadebc35 | 7280 | /* either cancelled or io-wq is dying, so don't touch tctx->iowq */ |
d01905db PB |
7281 | if (work->flags & IO_WQ_WORK_CANCEL) { |
7282 | io_req_task_queue_fail(req, -ECANCELED); | |
7283 | return; | |
7284 | } | |
31b51510 | 7285 | |
d01905db | 7286 | if (req->flags & REQ_F_FORCE_ASYNC) { |
afb7f56f PB |
7287 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
7288 | bool opcode_poll = def->pollin || def->pollout; | |
7289 | ||
7290 | if (opcode_poll && file_can_poll(req->file)) { | |
7291 | needs_poll = true; | |
d01905db | 7292 | issue_flags |= IO_URING_F_NONBLOCK; |
afb7f56f | 7293 | } |
561fb04a | 7294 | } |
31b51510 | 7295 | |
d01905db PB |
7296 | do { |
7297 | ret = io_issue_sqe(req, issue_flags); | |
7298 | if (ret != -EAGAIN) | |
7299 | break; | |
7300 | /* | |
7301 | * We can get EAGAIN for iopolled IO even though we're | |
7302 | * forcing a sync submission from here, since we can't | |
7303 | * wait for request slots on the block side. | |
7304 | */ | |
7305 | if (!needs_poll) { | |
7306 | cond_resched(); | |
7307 | continue; | |
90fa0288 HX |
7308 | } |
7309 | ||
4d9237e3 | 7310 | if (io_arm_poll_handler(req, issue_flags) == IO_APOLL_OK) |
d01905db PB |
7311 | return; |
7312 | /* aborted or ready, in either case retry blocking */ | |
7313 | needs_poll = false; | |
7314 | issue_flags &= ~IO_URING_F_NONBLOCK; | |
7315 | } while (1); | |
31b51510 | 7316 | |
a3df7698 | 7317 | /* avoid locking problems by failing it from a clean context */ |
5d5901a3 | 7318 | if (ret) |
a3df7698 | 7319 | io_req_task_queue_fail(req, ret); |
2b188cc1 JA |
7320 | } |
7321 | ||
aeca241b | 7322 | static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table, |
042b0d85 | 7323 | unsigned i) |
65e19f54 | 7324 | { |
042b0d85 | 7325 | return &table->files[i]; |
dafecf19 PB |
7326 | } |
7327 | ||
65e19f54 JA |
7328 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
7329 | int index) | |
7330 | { | |
aeca241b | 7331 | struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index); |
65e19f54 | 7332 | |
a04b0ac0 | 7333 | return (struct file *) (slot->file_ptr & FFS_MASK); |
65e19f54 JA |
7334 | } |
7335 | ||
a04b0ac0 | 7336 | static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file) |
9a321c98 PB |
7337 | { |
7338 | unsigned long file_ptr = (unsigned long) file; | |
7339 | ||
88459b50 | 7340 | file_ptr |= io_file_get_flags(file); |
a04b0ac0 | 7341 | file_slot->file_ptr = file_ptr; |
65e19f54 JA |
7342 | } |
7343 | ||
ac177053 PB |
7344 | static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx, |
7345 | struct io_kiocb *req, int fd) | |
09bb8394 | 7346 | { |
8da11c19 | 7347 | struct file *file; |
ac177053 | 7348 | unsigned long file_ptr; |
09bb8394 | 7349 | |
ac177053 PB |
7350 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
7351 | return NULL; | |
7352 | fd = array_index_nospec(fd, ctx->nr_user_files); | |
7353 | file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr; | |
7354 | file = (struct file *) (file_ptr & FFS_MASK); | |
7355 | file_ptr &= ~FFS_MASK; | |
7356 | /* mask in overlapping REQ_F and FFS bits */ | |
35645ac3 | 7357 | req->flags |= (file_ptr << REQ_F_SUPPORT_NOWAIT_BIT); |
a46be971 | 7358 | io_req_set_rsrc_node(req, ctx); |
ac177053 PB |
7359 | return file; |
7360 | } | |
d44f554e | 7361 | |
ac177053 | 7362 | static struct file *io_file_get_normal(struct io_ring_ctx *ctx, |
ac177053 PB |
7363 | struct io_kiocb *req, int fd) |
7364 | { | |
62906e89 | 7365 | struct file *file = fget(fd); |
ac177053 | 7366 | |
502c87d6 | 7367 | trace_io_uring_file_get(ctx, req, req->user_data, fd); |
09bb8394 | 7368 | |
ac177053 PB |
7369 | /* we don't allow fixed io_uring files */ |
7370 | if (file && unlikely(file->f_op == &io_uring_fops)) | |
7371 | io_req_track_inflight(req); | |
8371adf5 | 7372 | return file; |
09bb8394 JA |
7373 | } |
7374 | ||
ac177053 | 7375 | static inline struct file *io_file_get(struct io_ring_ctx *ctx, |
ac177053 PB |
7376 | struct io_kiocb *req, int fd, bool fixed) |
7377 | { | |
7378 | if (fixed) | |
7379 | return io_file_get_fixed(ctx, req, fd); | |
7380 | else | |
62906e89 | 7381 | return io_file_get_normal(ctx, req, fd); |
ac177053 PB |
7382 | } |
7383 | ||
f237c30a | 7384 | static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked) |
89b263f6 JA |
7385 | { |
7386 | struct io_kiocb *prev = req->timeout.prev; | |
617a8948 | 7387 | int ret = -ENOENT; |
89b263f6 JA |
7388 | |
7389 | if (prev) { | |
617a8948 PB |
7390 | if (!(req->task->flags & PF_EXITING)) |
7391 | ret = io_try_cancel_userdata(req, prev->user_data); | |
505657bc | 7392 | io_req_complete_post(req, ret ?: -ETIME, 0); |
89b263f6 | 7393 | io_put_req(prev); |
89b263f6 JA |
7394 | } else { |
7395 | io_req_complete_post(req, -ETIME, 0); | |
7396 | } | |
7397 | } | |
7398 | ||
2665abfd | 7399 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 7400 | { |
ad8a48ac JA |
7401 | struct io_timeout_data *data = container_of(timer, |
7402 | struct io_timeout_data, timer); | |
90cd7e42 | 7403 | struct io_kiocb *prev, *req = data->req; |
2665abfd | 7404 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 7405 | unsigned long flags; |
2665abfd | 7406 | |
89b263f6 | 7407 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
90cd7e42 PB |
7408 | prev = req->timeout.head; |
7409 | req->timeout.head = NULL; | |
2665abfd JA |
7410 | |
7411 | /* | |
7412 | * We don't expect the list to be empty, that will only happen if we | |
7413 | * race with the completion of the linked work. | |
7414 | */ | |
447c19f3 | 7415 | if (prev) { |
f2f87370 | 7416 | io_remove_next_linked(prev); |
447c19f3 PB |
7417 | if (!req_ref_inc_not_zero(prev)) |
7418 | prev = NULL; | |
7419 | } | |
ef9dd637 | 7420 | list_del(&req->timeout.list); |
89b263f6 JA |
7421 | req->timeout.prev = prev; |
7422 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); | |
2665abfd | 7423 | |
89b263f6 | 7424 | req->io_task_work.func = io_req_task_link_timeout; |
4813c377 | 7425 | io_req_task_work_add(req, false); |
2665abfd JA |
7426 | return HRTIMER_NORESTART; |
7427 | } | |
7428 | ||
de968c18 | 7429 | static void io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 7430 | { |
de968c18 PB |
7431 | struct io_ring_ctx *ctx = req->ctx; |
7432 | ||
89b263f6 | 7433 | spin_lock_irq(&ctx->timeout_lock); |
76a46e06 | 7434 | /* |
f2f87370 PB |
7435 | * If the back reference is NULL, then our linked request finished |
7436 | * before we got a chance to setup the timer | |
76a46e06 | 7437 | */ |
90cd7e42 | 7438 | if (req->timeout.head) { |
e8c2bc1f | 7439 | struct io_timeout_data *data = req->async_data; |
94ae5e77 | 7440 | |
ad8a48ac JA |
7441 | data->timer.function = io_link_timeout_fn; |
7442 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
7443 | data->mode); | |
ef9dd637 | 7444 | list_add_tail(&req->timeout.list, &ctx->ltimeout_list); |
2665abfd | 7445 | } |
89b263f6 | 7446 | spin_unlock_irq(&ctx->timeout_lock); |
2665abfd | 7447 | /* drop submission reference */ |
76a46e06 JA |
7448 | io_put_req(req); |
7449 | } | |
2665abfd | 7450 | |
d475a9a6 PB |
7451 | static void io_queue_sqe_arm_apoll(struct io_kiocb *req) |
7452 | __must_hold(&req->ctx->uring_lock) | |
7453 | { | |
7454 | struct io_kiocb *linked_timeout = io_prep_linked_timeout(req); | |
7455 | ||
4d9237e3 | 7456 | switch (io_arm_poll_handler(req, 0)) { |
d475a9a6 | 7457 | case IO_APOLL_READY: |
d475a9a6 PB |
7458 | io_req_task_queue(req); |
7459 | break; | |
7460 | case IO_APOLL_ABORTED: | |
7461 | /* | |
7462 | * Queued up for async execution, worker will release | |
7463 | * submit reference when the iocb is actually submitted. | |
7464 | */ | |
b1c62645 | 7465 | io_kbuf_recycle(req); |
d475a9a6 PB |
7466 | io_queue_async_work(req, NULL); |
7467 | break; | |
b1c62645 JA |
7468 | case IO_APOLL_OK: |
7469 | io_kbuf_recycle(req); | |
7470 | break; | |
d475a9a6 PB |
7471 | } |
7472 | ||
7473 | if (linked_timeout) | |
7474 | io_queue_linked_timeout(linked_timeout); | |
7475 | } | |
7476 | ||
7477 | static inline void __io_queue_sqe(struct io_kiocb *req) | |
282cdc86 | 7478 | __must_hold(&req->ctx->uring_lock) |
2b188cc1 | 7479 | { |
906c6caa | 7480 | struct io_kiocb *linked_timeout; |
e0c5c576 | 7481 | int ret; |
2b188cc1 | 7482 | |
c5eef2b9 | 7483 | ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER); |
193155c8 | 7484 | |
fff4e40e PB |
7485 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
7486 | io_req_add_compl_list(req); | |
d9f9d284 | 7487 | return; |
fff4e40e | 7488 | } |
491381ce JA |
7489 | /* |
7490 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
7491 | * doesn't support non-blocking read/write attempts | |
7492 | */ | |
1840038e | 7493 | if (likely(!ret)) { |
906c6caa PB |
7494 | linked_timeout = io_prep_linked_timeout(req); |
7495 | if (linked_timeout) | |
7496 | io_queue_linked_timeout(linked_timeout); | |
1840038e | 7497 | } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
d475a9a6 | 7498 | io_queue_sqe_arm_apoll(req); |
0d63c148 | 7499 | } else { |
f41db273 | 7500 | io_req_complete_failed(req, ret); |
9e645e11 | 7501 | } |
2b188cc1 JA |
7502 | } |
7503 | ||
4652fe3f | 7504 | static void io_queue_sqe_fallback(struct io_kiocb *req) |
282cdc86 | 7505 | __must_hold(&req->ctx->uring_lock) |
4fe2c963 | 7506 | { |
4652fe3f | 7507 | if (req->flags & REQ_F_FAIL) { |
c6d3d9cb | 7508 | io_req_complete_fail_submit(req); |
e0eb71dc PB |
7509 | } else if (unlikely(req->ctx->drain_active)) { |
7510 | io_drain_req(req); | |
76cc33d7 PB |
7511 | } else { |
7512 | int ret = io_req_prep_async(req); | |
7513 | ||
7514 | if (unlikely(ret)) | |
7515 | io_req_complete_failed(req, ret); | |
7516 | else | |
f237c30a | 7517 | io_queue_async_work(req, NULL); |
ce35a47a | 7518 | } |
4fe2c963 JL |
7519 | } |
7520 | ||
4652fe3f PB |
7521 | static inline void io_queue_sqe(struct io_kiocb *req) |
7522 | __must_hold(&req->ctx->uring_lock) | |
7523 | { | |
7524 | if (likely(!(req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL)))) | |
7525 | __io_queue_sqe(req); | |
7526 | else | |
7527 | io_queue_sqe_fallback(req); | |
7528 | } | |
7529 | ||
b16fed66 PB |
7530 | /* |
7531 | * Check SQE restrictions (opcode and flags). | |
7532 | * | |
7533 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
7534 | */ | |
7535 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
7536 | struct io_kiocb *req, | |
7537 | unsigned int sqe_flags) | |
4fe2c963 | 7538 | { |
b16fed66 PB |
7539 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) |
7540 | return false; | |
7541 | ||
7542 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
7543 | ctx->restrictions.sqe_flags_required) | |
7544 | return false; | |
7545 | ||
7546 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
7547 | ctx->restrictions.sqe_flags_required)) | |
7548 | return false; | |
7549 | ||
7550 | return true; | |
4fe2c963 JL |
7551 | } |
7552 | ||
22b2ca31 PB |
7553 | static void io_init_req_drain(struct io_kiocb *req) |
7554 | { | |
7555 | struct io_ring_ctx *ctx = req->ctx; | |
7556 | struct io_kiocb *head = ctx->submit_state.link.head; | |
7557 | ||
7558 | ctx->drain_active = true; | |
7559 | if (head) { | |
7560 | /* | |
7561 | * If we need to drain a request in the middle of a link, drain | |
7562 | * the head request and the next request/link after the current | |
7563 | * link. Considering sequential execution of links, | |
b6c7db32 | 7564 | * REQ_F_IO_DRAIN will be maintained for every request of our |
22b2ca31 PB |
7565 | * link. |
7566 | */ | |
b6c7db32 | 7567 | head->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC; |
22b2ca31 PB |
7568 | ctx->drain_next = true; |
7569 | } | |
7570 | } | |
7571 | ||
b16fed66 PB |
7572 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
7573 | const struct io_uring_sqe *sqe) | |
282cdc86 | 7574 | __must_hold(&ctx->uring_lock) |
b16fed66 | 7575 | { |
b16fed66 | 7576 | unsigned int sqe_flags; |
fc0ae024 | 7577 | int personality; |
4a04d1d1 | 7578 | u8 opcode; |
b16fed66 | 7579 | |
864ea921 | 7580 | /* req is partially pre-initialised, see io_preinit_req() */ |
4a04d1d1 | 7581 | req->opcode = opcode = READ_ONCE(sqe->opcode); |
b16fed66 PB |
7582 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
7583 | req->flags = sqe_flags = READ_ONCE(sqe->flags); | |
7584 | req->user_data = READ_ONCE(sqe->user_data); | |
b16fed66 | 7585 | req->file = NULL; |
b16fed66 | 7586 | req->fixed_rsrc_refs = NULL; |
b16fed66 | 7587 | req->task = current; |
b16fed66 | 7588 | |
4a04d1d1 PB |
7589 | if (unlikely(opcode >= IORING_OP_LAST)) { |
7590 | req->opcode = 0; | |
b16fed66 | 7591 | return -EINVAL; |
4a04d1d1 | 7592 | } |
68fe256a PB |
7593 | if (unlikely(sqe_flags & ~SQE_COMMON_FLAGS)) { |
7594 | /* enforce forwards compatibility on users */ | |
7595 | if (sqe_flags & ~SQE_VALID_FLAGS) | |
7596 | return -EINVAL; | |
7597 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && | |
4a04d1d1 | 7598 | !io_op_defs[opcode].buffer_select) |
68fe256a | 7599 | return -EOPNOTSUPP; |
5562a8d7 PB |
7600 | if (sqe_flags & IOSQE_CQE_SKIP_SUCCESS) |
7601 | ctx->drain_disabled = true; | |
7602 | if (sqe_flags & IOSQE_IO_DRAIN) { | |
7603 | if (ctx->drain_disabled) | |
7604 | return -EOPNOTSUPP; | |
22b2ca31 | 7605 | io_init_req_drain(req); |
5562a8d7 | 7606 | } |
2a56a9bd PB |
7607 | } |
7608 | if (unlikely(ctx->restricted || ctx->drain_active || ctx->drain_next)) { | |
7609 | if (ctx->restricted && !io_check_restriction(ctx, req, sqe_flags)) | |
7610 | return -EACCES; | |
7611 | /* knock it to the slow queue path, will be drained there */ | |
7612 | if (ctx->drain_active) | |
7613 | req->flags |= REQ_F_FORCE_ASYNC; | |
7614 | /* if there is no link, we're at "next" request and need to drain */ | |
7615 | if (unlikely(ctx->drain_next) && !ctx->submit_state.link.head) { | |
7616 | ctx->drain_next = false; | |
7617 | ctx->drain_active = true; | |
b6c7db32 | 7618 | req->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC; |
2a56a9bd | 7619 | } |
68fe256a | 7620 | } |
b16fed66 | 7621 | |
4a04d1d1 | 7622 | if (io_op_defs[opcode].needs_file) { |
6d63416d PB |
7623 | struct io_submit_state *state = &ctx->submit_state; |
7624 | ||
7625 | /* | |
7626 | * Plug now if we have more than 2 IO left after this, and the | |
7627 | * target is potentially a read/write to block based storage. | |
7628 | */ | |
4a04d1d1 | 7629 | if (state->need_plug && io_op_defs[opcode].plug) { |
6d63416d PB |
7630 | state->plug_started = true; |
7631 | state->need_plug = false; | |
5ca7a8b3 | 7632 | blk_start_plug_nr_ios(&state->plug, state->submit_nr); |
6d63416d PB |
7633 | } |
7634 | ||
62906e89 | 7635 | req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd), |
ac177053 | 7636 | (sqe_flags & IOSQE_FIXED_FILE)); |
b16fed66 | 7637 | if (unlikely(!req->file)) |
fc0ae024 | 7638 | return -EBADF; |
b16fed66 | 7639 | } |
863e0560 | 7640 | |
003e8dcc JA |
7641 | personality = READ_ONCE(sqe->personality); |
7642 | if (personality) { | |
cdab10bf LT |
7643 | int ret; |
7644 | ||
c10d1f98 PB |
7645 | req->creds = xa_load(&ctx->personalities, personality); |
7646 | if (!req->creds) | |
003e8dcc | 7647 | return -EINVAL; |
c10d1f98 | 7648 | get_cred(req->creds); |
cdc1404a PM |
7649 | ret = security_uring_override_creds(req->creds); |
7650 | if (ret) { | |
7651 | put_cred(req->creds); | |
7652 | return ret; | |
7653 | } | |
b8e64b53 | 7654 | req->flags |= REQ_F_CREDS; |
003e8dcc | 7655 | } |
b16fed66 | 7656 | |
fc0ae024 | 7657 | return io_req_prep(req, sqe); |
b16fed66 PB |
7658 | } |
7659 | ||
a6b8cadc | 7660 | static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
a1ab7b35 | 7661 | const struct io_uring_sqe *sqe) |
282cdc86 | 7662 | __must_hold(&ctx->uring_lock) |
9e645e11 | 7663 | { |
a1ab7b35 | 7664 | struct io_submit_link *link = &ctx->submit_state.link; |
ef4ff581 | 7665 | int ret; |
9e645e11 | 7666 | |
a6b8cadc PB |
7667 | ret = io_init_req(ctx, req, sqe); |
7668 | if (unlikely(ret)) { | |
502c87d6 | 7669 | trace_io_uring_req_failed(sqe, ctx, req, ret); |
a87acfde | 7670 | |
a8295b98 | 7671 | /* fail even hard links since we don't submit */ |
de59bc10 | 7672 | if (link->head) { |
a8295b98 HX |
7673 | /* |
7674 | * we can judge a link req is failed or cancelled by if | |
7675 | * REQ_F_FAIL is set, but the head is an exception since | |
7676 | * it may be set REQ_F_FAIL because of other req's failure | |
7677 | * so let's leverage req->result to distinguish if a head | |
7678 | * is set REQ_F_FAIL because of its failure or other req's | |
7679 | * failure so that we can set the correct ret code for it. | |
7680 | * init result here to avoid affecting the normal path. | |
7681 | */ | |
7682 | if (!(link->head->flags & REQ_F_FAIL)) | |
7683 | req_fail_link_node(link->head, -ECANCELED); | |
7684 | } else if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { | |
7685 | /* | |
7686 | * the current req is a normal req, we should return | |
7687 | * error and thus break the submittion loop. | |
7688 | */ | |
7689 | io_req_complete_failed(req, ret); | |
7690 | return ret; | |
de59bc10 | 7691 | } |
a8295b98 | 7692 | req_fail_link_node(req, ret); |
a6b8cadc | 7693 | } |
441b8a78 | 7694 | |
be7053b7 | 7695 | /* don't need @sqe from now on */ |
502c87d6 | 7696 | trace_io_uring_submit_sqe(ctx, req, req->user_data, req->opcode, |
236daeae OL |
7697 | req->flags, true, |
7698 | ctx->flags & IORING_SETUP_SQPOLL); | |
a6b8cadc | 7699 | |
9e645e11 JA |
7700 | /* |
7701 | * If we already have a head request, queue this one for async | |
7702 | * submittal once the head completes. If we don't have a head but | |
7703 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
7704 | * submitted sync once the chain is complete. If none of those | |
7705 | * conditions are true (normal request), then just queue it. | |
7706 | */ | |
863e0560 PB |
7707 | if (link->head) { |
7708 | struct io_kiocb *head = link->head; | |
4e88d6e7 | 7709 | |
a8295b98 HX |
7710 | if (!(req->flags & REQ_F_FAIL)) { |
7711 | ret = io_req_prep_async(req); | |
7712 | if (unlikely(ret)) { | |
7713 | req_fail_link_node(req, ret); | |
7714 | if (!(head->flags & REQ_F_FAIL)) | |
7715 | req_fail_link_node(head, -ECANCELED); | |
7716 | } | |
7717 | } | |
9d76377f | 7718 | trace_io_uring_link(ctx, req, head); |
f2f87370 | 7719 | link->last->link = req; |
863e0560 | 7720 | link->last = req; |
32fe525b | 7721 | |
f15a3431 PB |
7722 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) |
7723 | return 0; | |
32fe525b | 7724 | /* last request of a link, enqueue the link */ |
f15a3431 PB |
7725 | link->head = NULL; |
7726 | req = head; | |
7727 | } else if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { | |
7728 | link->head = req; | |
7729 | link->last = req; | |
7730 | return 0; | |
9e645e11 | 7731 | } |
2e6e1fde | 7732 | |
f15a3431 | 7733 | io_queue_sqe(req); |
1d4240cc | 7734 | return 0; |
9e645e11 JA |
7735 | } |
7736 | ||
9a56a232 JA |
7737 | /* |
7738 | * Batched submission is done, ensure local IO is flushed out. | |
7739 | */ | |
553deffd | 7740 | static void io_submit_state_end(struct io_ring_ctx *ctx) |
9a56a232 | 7741 | { |
553deffd PB |
7742 | struct io_submit_state *state = &ctx->submit_state; |
7743 | ||
a1ab7b35 | 7744 | if (state->link.head) |
de59bc10 | 7745 | io_queue_sqe(state->link.head); |
553deffd | 7746 | /* flush only after queuing links as they can generate completions */ |
c450178d | 7747 | io_submit_flush_completions(ctx); |
27926b68 JA |
7748 | if (state->plug_started) |
7749 | blk_finish_plug(&state->plug); | |
9a56a232 JA |
7750 | } |
7751 | ||
7752 | /* | |
7753 | * Start submission side cache. | |
7754 | */ | |
7755 | static void io_submit_state_start(struct io_submit_state *state, | |
ba88ff11 | 7756 | unsigned int max_ios) |
9a56a232 | 7757 | { |
27926b68 | 7758 | state->plug_started = false; |
4b628aeb | 7759 | state->need_plug = max_ios > 2; |
5ca7a8b3 | 7760 | state->submit_nr = max_ios; |
a1ab7b35 PB |
7761 | /* set only head, no need to init link_last in advance */ |
7762 | state->link.head = NULL; | |
9a56a232 JA |
7763 | } |
7764 | ||
2b188cc1 JA |
7765 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
7766 | { | |
75b28aff | 7767 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 7768 | |
caf582c6 PB |
7769 | /* |
7770 | * Ensure any loads from the SQEs are done at this point, | |
7771 | * since once we write the new head, the application could | |
7772 | * write new data to them. | |
7773 | */ | |
7774 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
7775 | } |
7776 | ||
2b188cc1 | 7777 | /* |
dd9ae8a0 | 7778 | * Fetch an sqe, if one is available. Note this returns a pointer to memory |
2b188cc1 JA |
7779 | * that is mapped by userspace. This means that care needs to be taken to |
7780 | * ensure that reads are stable, as we cannot rely on userspace always | |
7781 | * being a good citizen. If members of the sqe are validated and then later | |
7782 | * used, it's important that those reads are done through READ_ONCE() to | |
7783 | * prevent a re-load down the line. | |
7784 | */ | |
709b302f | 7785 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 7786 | { |
ea5ab3b5 | 7787 | unsigned head, mask = ctx->sq_entries - 1; |
17d3aeb3 | 7788 | unsigned sq_idx = ctx->cached_sq_head++ & mask; |
2b188cc1 JA |
7789 | |
7790 | /* | |
7791 | * The cached sq head (or cq tail) serves two purposes: | |
7792 | * | |
7793 | * 1) allows us to batch the cost of updating the user visible | |
7794 | * head updates. | |
7795 | * 2) allows the kernel side to track the head on its own, even | |
7796 | * though the application is the one updating it. | |
7797 | */ | |
17d3aeb3 | 7798 | head = READ_ONCE(ctx->sq_array[sq_idx]); |
709b302f PB |
7799 | if (likely(head < ctx->sq_entries)) |
7800 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
7801 | |
7802 | /* drop invalid entries */ | |
15641e42 PB |
7803 | ctx->cq_extra--; |
7804 | WRITE_ONCE(ctx->rings->sq_dropped, | |
7805 | READ_ONCE(ctx->rings->sq_dropped) + 1); | |
709b302f PB |
7806 | return NULL; |
7807 | } | |
7808 | ||
0f212204 | 7809 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
282cdc86 | 7810 | __must_hold(&ctx->uring_lock) |
6c271ce2 | 7811 | { |
69629809 | 7812 | unsigned int entries = io_sqring_entries(ctx); |
46c4e16a | 7813 | int submitted = 0; |
6c271ce2 | 7814 | |
51d48dab | 7815 | if (unlikely(!entries)) |
69629809 | 7816 | return 0; |
ee7d46d9 | 7817 | /* make sure SQ entry isn't read before tail */ |
69629809 | 7818 | nr = min3(nr, ctx->sq_entries, entries); |
9a10867a | 7819 | io_get_task_refs(nr); |
6c271ce2 | 7820 | |
ba88ff11 | 7821 | io_submit_state_start(&ctx->submit_state, nr); |
69629809 | 7822 | do { |
3529d8c2 | 7823 | const struct io_uring_sqe *sqe; |
196be95c | 7824 | struct io_kiocb *req; |
fb5ccc98 | 7825 | |
a33ae9ce | 7826 | if (unlikely(!io_alloc_req_refill(ctx))) { |
196be95c PB |
7827 | if (!submitted) |
7828 | submitted = -EAGAIN; | |
fb5ccc98 | 7829 | break; |
196be95c | 7830 | } |
a33ae9ce | 7831 | req = io_alloc_req(ctx); |
4fccfcbb PB |
7832 | sqe = io_get_sqe(ctx); |
7833 | if (unlikely(!sqe)) { | |
c2b6c6bc | 7834 | wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list); |
4fccfcbb PB |
7835 | break; |
7836 | } | |
d3656344 JA |
7837 | /* will complete beyond this point, count as submitted */ |
7838 | submitted++; | |
bcbb7bf6 JA |
7839 | if (io_submit_sqe(ctx, req, sqe)) { |
7840 | /* | |
7841 | * Continue submitting even for sqe failure if the | |
7842 | * ring was setup with IORING_SETUP_SUBMIT_ALL | |
7843 | */ | |
7844 | if (!(ctx->flags & IORING_SETUP_SUBMIT_ALL)) | |
7845 | break; | |
7846 | } | |
69629809 | 7847 | } while (submitted < nr); |
6c271ce2 | 7848 | |
9466f437 PB |
7849 | if (unlikely(submitted != nr)) { |
7850 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; | |
d8a6df10 | 7851 | int unused = nr - ref_used; |
9466f437 | 7852 | |
09899b19 | 7853 | current->io_uring->cached_refs += unused; |
9466f437 | 7854 | } |
6c271ce2 | 7855 | |
553deffd | 7856 | io_submit_state_end(ctx); |
ae9428ca PB |
7857 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
7858 | io_commit_sqring(ctx); | |
7859 | ||
6c271ce2 JA |
7860 | return submitted; |
7861 | } | |
7862 | ||
e4b6d902 PB |
7863 | static inline bool io_sqd_events_pending(struct io_sq_data *sqd) |
7864 | { | |
7865 | return READ_ONCE(sqd->state); | |
7866 | } | |
7867 | ||
23b3628e XW |
7868 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
7869 | { | |
7870 | /* Tell userspace we may need a wakeup call */ | |
79ebeaee | 7871 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
7872 | WRITE_ONCE(ctx->rings->sq_flags, |
7873 | ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 7874 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
7875 | } |
7876 | ||
7877 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) | |
7878 | { | |
79ebeaee | 7879 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
7880 | WRITE_ONCE(ctx->rings->sq_flags, |
7881 | ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 7882 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
7883 | } |
7884 | ||
08369246 | 7885 | static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) |
6c271ce2 | 7886 | { |
c8d1ba58 | 7887 | unsigned int to_submit; |
bdcd3eab | 7888 | int ret = 0; |
6c271ce2 | 7889 | |
c8d1ba58 | 7890 | to_submit = io_sqring_entries(ctx); |
e95eee2d | 7891 | /* if we're handling multiple rings, cap submit size for fairness */ |
4ce8ad95 OL |
7892 | if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE) |
7893 | to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE; | |
e95eee2d | 7894 | |
5eef4e87 | 7895 | if (!wq_list_empty(&ctx->iopoll_list) || to_submit) { |
948e1947 PB |
7896 | const struct cred *creds = NULL; |
7897 | ||
7898 | if (ctx->sq_creds != current_cred()) | |
7899 | creds = override_creds(ctx->sq_creds); | |
a4c0b3de | 7900 | |
c8d1ba58 | 7901 | mutex_lock(&ctx->uring_lock); |
5eef4e87 | 7902 | if (!wq_list_empty(&ctx->iopoll_list)) |
5ba3c874 | 7903 | io_do_iopoll(ctx, true); |
906a3c6f | 7904 | |
3b763ba1 PB |
7905 | /* |
7906 | * Don't submit if refs are dying, good for io_uring_register(), | |
7907 | * but also it is relied upon by io_ring_exit_work() | |
7908 | */ | |
0298ef96 PB |
7909 | if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) && |
7910 | !(ctx->flags & IORING_SETUP_R_DISABLED)) | |
08369246 | 7911 | ret = io_submit_sqes(ctx, to_submit); |
c8d1ba58 | 7912 | mutex_unlock(&ctx->uring_lock); |
adc8682e OL |
7913 | #ifdef CONFIG_NET_RX_BUSY_POLL |
7914 | spin_lock(&ctx->napi_lock); | |
7915 | if (!list_empty(&ctx->napi_list) && | |
7916 | io_napi_busy_loop(&ctx->napi_list)) | |
7917 | ++ret; | |
7918 | spin_unlock(&ctx->napi_lock); | |
7919 | #endif | |
acfb381d PB |
7920 | if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait)) |
7921 | wake_up(&ctx->sqo_sq_wait); | |
948e1947 PB |
7922 | if (creds) |
7923 | revert_creds(creds); | |
acfb381d | 7924 | } |
6c271ce2 | 7925 | |
08369246 XW |
7926 | return ret; |
7927 | } | |
6c271ce2 | 7928 | |
c072481d | 7929 | static __cold void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
08369246 XW |
7930 | { |
7931 | struct io_ring_ctx *ctx; | |
7932 | unsigned sq_thread_idle = 0; | |
6c271ce2 | 7933 | |
c9dca27d PB |
7934 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7935 | sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle); | |
08369246 | 7936 | sqd->sq_thread_idle = sq_thread_idle; |
c8d1ba58 | 7937 | } |
6c271ce2 | 7938 | |
e4b6d902 PB |
7939 | static bool io_sqd_handle_event(struct io_sq_data *sqd) |
7940 | { | |
7941 | bool did_sig = false; | |
7942 | struct ksignal ksig; | |
7943 | ||
7944 | if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) || | |
7945 | signal_pending(current)) { | |
7946 | mutex_unlock(&sqd->lock); | |
7947 | if (signal_pending(current)) | |
7948 | did_sig = get_signal(&ksig); | |
7949 | cond_resched(); | |
7950 | mutex_lock(&sqd->lock); | |
7951 | } | |
e4b6d902 PB |
7952 | return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
7953 | } | |
7954 | ||
c8d1ba58 JA |
7955 | static int io_sq_thread(void *data) |
7956 | { | |
69fb2131 JA |
7957 | struct io_sq_data *sqd = data; |
7958 | struct io_ring_ctx *ctx; | |
a0d9205f | 7959 | unsigned long timeout = 0; |
37d1e2e3 | 7960 | char buf[TASK_COMM_LEN]; |
08369246 | 7961 | DEFINE_WAIT(wait); |
6c271ce2 | 7962 | |
696ee88a | 7963 | snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid); |
37d1e2e3 | 7964 | set_task_comm(current, buf); |
37d1e2e3 JA |
7965 | |
7966 | if (sqd->sq_cpu != -1) | |
7967 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); | |
7968 | else | |
7969 | set_cpus_allowed_ptr(current, cpu_online_mask); | |
7970 | current->flags |= PF_NO_SETAFFINITY; | |
7971 | ||
5bd2182d PM |
7972 | audit_alloc_kernel(current); |
7973 | ||
09a6f4ef | 7974 | mutex_lock(&sqd->lock); |
e4b6d902 | 7975 | while (1) { |
1a924a80 | 7976 | bool cap_entries, sqt_spin = false; |
c1edbf5f | 7977 | |
e4b6d902 PB |
7978 | if (io_sqd_events_pending(sqd) || signal_pending(current)) { |
7979 | if (io_sqd_handle_event(sqd)) | |
c7d95613 | 7980 | break; |
08369246 XW |
7981 | timeout = jiffies + sqd->sq_thread_idle; |
7982 | } | |
e4b6d902 | 7983 | |
e95eee2d | 7984 | cap_entries = !list_is_singular(&sqd->ctx_list); |
69fb2131 | 7985 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
948e1947 | 7986 | int ret = __io_sq_thread(ctx, cap_entries); |
7c30f36a | 7987 | |
5eef4e87 | 7988 | if (!sqt_spin && (ret > 0 || !wq_list_empty(&ctx->iopoll_list))) |
08369246 | 7989 | sqt_spin = true; |
69fb2131 | 7990 | } |
dd432ea5 PB |
7991 | if (io_run_task_work()) |
7992 | sqt_spin = true; | |
6c271ce2 | 7993 | |
08369246 | 7994 | if (sqt_spin || !time_after(jiffies, timeout)) { |
c8d1ba58 | 7995 | cond_resched(); |
08369246 XW |
7996 | if (sqt_spin) |
7997 | timeout = jiffies + sqd->sq_thread_idle; | |
7998 | continue; | |
7999 | } | |
8000 | ||
08369246 | 8001 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); |
dd432ea5 | 8002 | if (!io_sqd_events_pending(sqd) && !current->task_works) { |
1a924a80 PB |
8003 | bool needs_sched = true; |
8004 | ||
724cb4f9 | 8005 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
aaa9f0f4 PB |
8006 | io_ring_set_wakeup_flag(ctx); |
8007 | ||
724cb4f9 | 8008 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
5eef4e87 | 8009 | !wq_list_empty(&ctx->iopoll_list)) { |
724cb4f9 HX |
8010 | needs_sched = false; |
8011 | break; | |
8012 | } | |
8013 | if (io_sqring_entries(ctx)) { | |
8014 | needs_sched = false; | |
8015 | break; | |
8016 | } | |
8017 | } | |
8018 | ||
8019 | if (needs_sched) { | |
8020 | mutex_unlock(&sqd->lock); | |
8021 | schedule(); | |
8022 | mutex_lock(&sqd->lock); | |
8023 | } | |
69fb2131 JA |
8024 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
8025 | io_ring_clear_wakeup_flag(ctx); | |
6c271ce2 | 8026 | } |
08369246 XW |
8027 | |
8028 | finish_wait(&sqd->wait, &wait); | |
8029 | timeout = jiffies + sqd->sq_thread_idle; | |
6c271ce2 | 8030 | } |
28cea78a | 8031 | |
78cc687b | 8032 | io_uring_cancel_generic(true, sqd); |
37d1e2e3 | 8033 | sqd->thread = NULL; |
05962f95 | 8034 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
5f3f26f9 | 8035 | io_ring_set_wakeup_flag(ctx); |
521d6a73 | 8036 | io_run_task_work(); |
734551df PB |
8037 | mutex_unlock(&sqd->lock); |
8038 | ||
5bd2182d PM |
8039 | audit_free(current); |
8040 | ||
37d1e2e3 JA |
8041 | complete(&sqd->exited); |
8042 | do_exit(0); | |
6c271ce2 JA |
8043 | } |
8044 | ||
bda52162 JA |
8045 | struct io_wait_queue { |
8046 | struct wait_queue_entry wq; | |
8047 | struct io_ring_ctx *ctx; | |
5fd46178 | 8048 | unsigned cq_tail; |
bda52162 | 8049 | unsigned nr_timeouts; |
adc8682e OL |
8050 | #ifdef CONFIG_NET_RX_BUSY_POLL |
8051 | unsigned busy_poll_to; | |
8052 | #endif | |
bda52162 JA |
8053 | }; |
8054 | ||
6c503150 | 8055 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
bda52162 JA |
8056 | { |
8057 | struct io_ring_ctx *ctx = iowq->ctx; | |
5fd46178 | 8058 | int dist = ctx->cached_cq_tail - (int) iowq->cq_tail; |
bda52162 JA |
8059 | |
8060 | /* | |
d195a66e | 8061 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
8062 | * started waiting. For timeouts, we always want to return to userspace, |
8063 | * regardless of event count. | |
8064 | */ | |
5fd46178 | 8065 | return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
bda52162 JA |
8066 | } |
8067 | ||
8068 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
8069 | int wake_flags, void *key) | |
8070 | { | |
8071 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
8072 | wq); | |
8073 | ||
6c503150 PB |
8074 | /* |
8075 | * Cannot safely flush overflowed CQEs from here, ensure we wake up | |
8076 | * the task, and the next invocation will do it. | |
8077 | */ | |
5ed7a37d | 8078 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->check_cq_overflow)) |
6c503150 PB |
8079 | return autoremove_wake_function(curr, mode, wake_flags, key); |
8080 | return -1; | |
bda52162 JA |
8081 | } |
8082 | ||
af9c1a44 JA |
8083 | static int io_run_task_work_sig(void) |
8084 | { | |
8085 | if (io_run_task_work()) | |
8086 | return 1; | |
0b8cfa97 | 8087 | if (test_thread_flag(TIF_NOTIFY_SIGNAL)) |
792ee0f6 | 8088 | return -ERESTARTSYS; |
c5020bc8 OL |
8089 | if (task_sigpending(current)) |
8090 | return -EINTR; | |
8091 | return 0; | |
af9c1a44 JA |
8092 | } |
8093 | ||
eeb60b9a PB |
8094 | /* when returns >0, the caller should retry */ |
8095 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, | |
8096 | struct io_wait_queue *iowq, | |
22833966 | 8097 | ktime_t timeout) |
eeb60b9a PB |
8098 | { |
8099 | int ret; | |
8100 | ||
8101 | /* make sure we run task_work before checking for signals */ | |
8102 | ret = io_run_task_work_sig(); | |
8103 | if (ret || io_should_wake(iowq)) | |
8104 | return ret; | |
8105 | /* let the caller flush overflows, retry */ | |
5ed7a37d | 8106 | if (test_bit(0, &ctx->check_cq_overflow)) |
eeb60b9a PB |
8107 | return 1; |
8108 | ||
22833966 JA |
8109 | if (!schedule_hrtimeout(&timeout, HRTIMER_MODE_ABS)) |
8110 | return -ETIME; | |
8111 | return 1; | |
eeb60b9a PB |
8112 | } |
8113 | ||
adc8682e OL |
8114 | #ifdef CONFIG_NET_RX_BUSY_POLL |
8115 | static void io_adjust_busy_loop_timeout(struct timespec64 *ts, | |
8116 | struct io_wait_queue *iowq) | |
8117 | { | |
8118 | unsigned busy_poll_to = READ_ONCE(sysctl_net_busy_poll); | |
8119 | struct timespec64 pollto = ns_to_timespec64(1000 * (s64)busy_poll_to); | |
8120 | ||
8121 | if (timespec64_compare(ts, &pollto) > 0) { | |
8122 | *ts = timespec64_sub(*ts, pollto); | |
8123 | iowq->busy_poll_to = busy_poll_to; | |
8124 | } else { | |
8125 | u64 to = timespec64_to_ns(ts); | |
8126 | ||
8127 | do_div(to, 1000); | |
8128 | iowq->busy_poll_to = to; | |
8129 | ts->tv_sec = 0; | |
8130 | ts->tv_nsec = 0; | |
8131 | } | |
8132 | } | |
8133 | ||
8134 | static inline bool io_busy_loop_timeout(unsigned long start_time, | |
8135 | unsigned long bp_usec) | |
8136 | { | |
8137 | if (bp_usec) { | |
8138 | unsigned long end_time = start_time + bp_usec; | |
8139 | unsigned long now = busy_loop_current_time(); | |
8140 | ||
8141 | return time_after(now, end_time); | |
8142 | } | |
8143 | return true; | |
8144 | } | |
8145 | ||
8146 | static bool io_busy_loop_end(void *p, unsigned long start_time) | |
8147 | { | |
8148 | struct io_wait_queue *iowq = p; | |
8149 | ||
8150 | return signal_pending(current) || | |
8151 | io_should_wake(iowq) || | |
8152 | io_busy_loop_timeout(start_time, iowq->busy_poll_to); | |
8153 | } | |
8154 | ||
8155 | static void io_blocking_napi_busy_loop(struct list_head *napi_list, | |
8156 | struct io_wait_queue *iowq) | |
8157 | { | |
8158 | unsigned long start_time = | |
8159 | list_is_singular(napi_list) ? 0 : | |
8160 | busy_loop_current_time(); | |
8161 | ||
8162 | do { | |
8163 | if (list_is_singular(napi_list)) { | |
8164 | struct napi_entry *ne = | |
8165 | list_first_entry(napi_list, | |
8166 | struct napi_entry, list); | |
8167 | ||
8168 | napi_busy_loop(ne->napi_id, io_busy_loop_end, iowq, | |
8169 | true, BUSY_POLL_BUDGET); | |
8170 | io_check_napi_entry_timeout(ne); | |
8171 | break; | |
8172 | } | |
8173 | } while (io_napi_busy_loop(napi_list) && | |
8174 | !io_busy_loop_end(iowq, start_time)); | |
8175 | } | |
8176 | ||
8177 | static void io_putback_napi_list(struct io_ring_ctx *ctx, | |
8178 | struct list_head *napi_list) | |
8179 | { | |
8180 | struct napi_entry *cne, *lne; | |
8181 | ||
8182 | spin_lock(&ctx->napi_lock); | |
8183 | list_for_each_entry(cne, &ctx->napi_list, list) | |
8184 | list_for_each_entry(lne, napi_list, list) | |
8185 | if (cne->napi_id == lne->napi_id) { | |
8186 | list_del(&lne->list); | |
8187 | kfree(lne); | |
8188 | break; | |
8189 | } | |
8190 | list_splice(napi_list, &ctx->napi_list); | |
8191 | spin_unlock(&ctx->napi_lock); | |
8192 | } | |
8193 | #endif /* CONFIG_NET_RX_BUSY_POLL */ | |
8194 | ||
2b188cc1 JA |
8195 | /* |
8196 | * Wait until events become available, if we don't already have some. The | |
8197 | * application must reap them itself, as they reside on the shared cq ring. | |
8198 | */ | |
8199 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
c73ebb68 HX |
8200 | const sigset_t __user *sig, size_t sigsz, |
8201 | struct __kernel_timespec __user *uts) | |
2b188cc1 | 8202 | { |
90291099 | 8203 | struct io_wait_queue iowq; |
75b28aff | 8204 | struct io_rings *rings = ctx->rings; |
22833966 | 8205 | ktime_t timeout = KTIME_MAX; |
c1d5a224 | 8206 | int ret; |
adc8682e OL |
8207 | #ifdef CONFIG_NET_RX_BUSY_POLL |
8208 | LIST_HEAD(local_napi_list); | |
8209 | #endif | |
2b188cc1 | 8210 | |
b41e9852 | 8211 | do { |
90f67366 | 8212 | io_cqring_overflow_flush(ctx); |
6c503150 | 8213 | if (io_cqring_events(ctx) >= min_events) |
b41e9852 | 8214 | return 0; |
4c6e277c | 8215 | if (!io_run_task_work()) |
b41e9852 | 8216 | break; |
b41e9852 | 8217 | } while (1); |
2b188cc1 JA |
8218 | |
8219 | if (sig) { | |
9e75ad5d AB |
8220 | #ifdef CONFIG_COMPAT |
8221 | if (in_compat_syscall()) | |
8222 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 8223 | sigsz); |
9e75ad5d AB |
8224 | else |
8225 | #endif | |
b772434b | 8226 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 8227 | |
2b188cc1 JA |
8228 | if (ret) |
8229 | return ret; | |
8230 | } | |
8231 | ||
adc8682e OL |
8232 | #ifdef CONFIG_NET_RX_BUSY_POLL |
8233 | iowq.busy_poll_to = 0; | |
8234 | if (!(ctx->flags & IORING_SETUP_SQPOLL)) { | |
8235 | spin_lock(&ctx->napi_lock); | |
8236 | list_splice_init(&ctx->napi_list, &local_napi_list); | |
8237 | spin_unlock(&ctx->napi_lock); | |
8238 | } | |
8239 | #endif | |
950e79dd OL |
8240 | if (uts) { |
8241 | struct timespec64 ts; | |
8242 | ||
8243 | if (get_timespec64(&ts, uts)) | |
8244 | return -EFAULT; | |
adc8682e OL |
8245 | #ifdef CONFIG_NET_RX_BUSY_POLL |
8246 | if (!list_empty(&local_napi_list)) | |
8247 | io_adjust_busy_loop_timeout(&ts, &iowq); | |
8248 | #endif | |
950e79dd OL |
8249 | timeout = ktime_add_ns(timespec64_to_ktime(ts), ktime_get_ns()); |
8250 | } | |
adc8682e OL |
8251 | #ifdef CONFIG_NET_RX_BUSY_POLL |
8252 | else if (!list_empty(&local_napi_list)) | |
8253 | iowq.busy_poll_to = READ_ONCE(sysctl_net_busy_poll); | |
8254 | #endif | |
950e79dd | 8255 | |
90291099 PB |
8256 | init_waitqueue_func_entry(&iowq.wq, io_wake_function); |
8257 | iowq.wq.private = current; | |
8258 | INIT_LIST_HEAD(&iowq.wq.entry); | |
8259 | iowq.ctx = ctx; | |
bda52162 | 8260 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
5fd46178 | 8261 | iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events; |
90291099 | 8262 | |
c826bd7a | 8263 | trace_io_uring_cqring_wait(ctx, min_events); |
adc8682e OL |
8264 | #ifdef CONFIG_NET_RX_BUSY_POLL |
8265 | if (iowq.busy_poll_to) | |
8266 | io_blocking_napi_busy_loop(&local_napi_list, &iowq); | |
8267 | if (!list_empty(&local_napi_list)) | |
8268 | io_putback_napi_list(ctx, &local_napi_list); | |
8269 | #endif | |
bda52162 | 8270 | do { |
ca0a2651 | 8271 | /* if we can't even flush overflow, don't wait for more */ |
90f67366 | 8272 | if (!io_cqring_overflow_flush(ctx)) { |
ca0a2651 JA |
8273 | ret = -EBUSY; |
8274 | break; | |
8275 | } | |
311997b3 | 8276 | prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq, |
bda52162 | 8277 | TASK_INTERRUPTIBLE); |
22833966 | 8278 | ret = io_cqring_wait_schedule(ctx, &iowq, timeout); |
311997b3 | 8279 | finish_wait(&ctx->cq_wait, &iowq.wq); |
ca0a2651 | 8280 | cond_resched(); |
eeb60b9a | 8281 | } while (ret > 0); |
bda52162 | 8282 | |
b7db41c9 | 8283 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 8284 | |
75b28aff | 8285 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
8286 | } |
8287 | ||
9123c8ff | 8288 | static void io_free_page_table(void **table, size_t size) |
05f3fb3c | 8289 | { |
9123c8ff | 8290 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); |
05f3fb3c | 8291 | |
846a4ef2 | 8292 | for (i = 0; i < nr_tables; i++) |
9123c8ff PB |
8293 | kfree(table[i]); |
8294 | kfree(table); | |
8295 | } | |
8296 | ||
c072481d | 8297 | static __cold void **io_alloc_page_table(size_t size) |
9123c8ff PB |
8298 | { |
8299 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); | |
8300 | size_t init_size = size; | |
8301 | void **table; | |
8302 | ||
0bea96f5 | 8303 | table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
8304 | if (!table) |
8305 | return NULL; | |
8306 | ||
8307 | for (i = 0; i < nr_tables; i++) { | |
27f6b318 | 8308 | unsigned int this_size = min_t(size_t, size, PAGE_SIZE); |
9123c8ff | 8309 | |
0bea96f5 | 8310 | table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
8311 | if (!table[i]) { |
8312 | io_free_page_table(table, init_size); | |
8313 | return NULL; | |
8314 | } | |
8315 | size -= this_size; | |
8316 | } | |
8317 | return table; | |
05f3fb3c JA |
8318 | } |
8319 | ||
28a9fe25 | 8320 | static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node) |
1642b445 | 8321 | { |
28a9fe25 PB |
8322 | percpu_ref_exit(&ref_node->refs); |
8323 | kfree(ref_node); | |
1642b445 PB |
8324 | } |
8325 | ||
c072481d | 8326 | static __cold void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
b9bd2bea PB |
8327 | { |
8328 | struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs); | |
8329 | struct io_ring_ctx *ctx = node->rsrc_data->ctx; | |
8330 | unsigned long flags; | |
8331 | bool first_add = false; | |
b36a2050 | 8332 | unsigned long delay = HZ; |
b9bd2bea PB |
8333 | |
8334 | spin_lock_irqsave(&ctx->rsrc_ref_lock, flags); | |
8335 | node->done = true; | |
8336 | ||
b36a2050 DY |
8337 | /* if we are mid-quiesce then do not delay */ |
8338 | if (node->rsrc_data->quiesce) | |
8339 | delay = 0; | |
8340 | ||
b9bd2bea PB |
8341 | while (!list_empty(&ctx->rsrc_ref_list)) { |
8342 | node = list_first_entry(&ctx->rsrc_ref_list, | |
8343 | struct io_rsrc_node, node); | |
8344 | /* recycle ref nodes in order */ | |
8345 | if (!node->done) | |
8346 | break; | |
8347 | list_del(&node->node); | |
8348 | first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist); | |
8349 | } | |
8350 | spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags); | |
8351 | ||
8352 | if (first_add) | |
b36a2050 | 8353 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, delay); |
b9bd2bea PB |
8354 | } |
8355 | ||
f6133fbd | 8356 | static struct io_rsrc_node *io_rsrc_node_alloc(void) |
b9bd2bea PB |
8357 | { |
8358 | struct io_rsrc_node *ref_node; | |
8359 | ||
8360 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); | |
8361 | if (!ref_node) | |
8362 | return NULL; | |
8363 | ||
8364 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, | |
8365 | 0, GFP_KERNEL)) { | |
8366 | kfree(ref_node); | |
8367 | return NULL; | |
8368 | } | |
8369 | INIT_LIST_HEAD(&ref_node->node); | |
8370 | INIT_LIST_HEAD(&ref_node->rsrc_list); | |
8371 | ref_node->done = false; | |
8372 | return ref_node; | |
8373 | } | |
8374 | ||
a7f0ed5a PB |
8375 | static void io_rsrc_node_switch(struct io_ring_ctx *ctx, |
8376 | struct io_rsrc_data *data_to_kill) | |
ab409402 | 8377 | __must_hold(&ctx->uring_lock) |
6b06314c | 8378 | { |
a7f0ed5a PB |
8379 | WARN_ON_ONCE(!ctx->rsrc_backup_node); |
8380 | WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node); | |
6b06314c | 8381 | |
ab409402 PB |
8382 | io_rsrc_refs_drop(ctx); |
8383 | ||
a7f0ed5a PB |
8384 | if (data_to_kill) { |
8385 | struct io_rsrc_node *rsrc_node = ctx->rsrc_node; | |
82fbcfa9 | 8386 | |
a7f0ed5a | 8387 | rsrc_node->rsrc_data = data_to_kill; |
4956b9ea | 8388 | spin_lock_irq(&ctx->rsrc_ref_lock); |
a7f0ed5a | 8389 | list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list); |
4956b9ea | 8390 | spin_unlock_irq(&ctx->rsrc_ref_lock); |
82fbcfa9 | 8391 | |
3e942498 | 8392 | atomic_inc(&data_to_kill->refs); |
a7f0ed5a PB |
8393 | percpu_ref_kill(&rsrc_node->refs); |
8394 | ctx->rsrc_node = NULL; | |
8395 | } | |
6b06314c | 8396 | |
a7f0ed5a PB |
8397 | if (!ctx->rsrc_node) { |
8398 | ctx->rsrc_node = ctx->rsrc_backup_node; | |
8399 | ctx->rsrc_backup_node = NULL; | |
8400 | } | |
8bad28d8 HX |
8401 | } |
8402 | ||
a7f0ed5a | 8403 | static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx) |
8dd03afe PB |
8404 | { |
8405 | if (ctx->rsrc_backup_node) | |
8406 | return 0; | |
f6133fbd | 8407 | ctx->rsrc_backup_node = io_rsrc_node_alloc(); |
8dd03afe | 8408 | return ctx->rsrc_backup_node ? 0 : -ENOMEM; |
8bad28d8 HX |
8409 | } |
8410 | ||
c072481d PB |
8411 | static __cold int io_rsrc_ref_quiesce(struct io_rsrc_data *data, |
8412 | struct io_ring_ctx *ctx) | |
8bad28d8 HX |
8413 | { |
8414 | int ret; | |
05589553 | 8415 | |
215c3902 | 8416 | /* As we may drop ->uring_lock, other task may have started quiesce */ |
8bad28d8 HX |
8417 | if (data->quiesce) |
8418 | return -ENXIO; | |
05589553 | 8419 | |
8bad28d8 | 8420 | data->quiesce = true; |
1ffc5422 | 8421 | do { |
a7f0ed5a | 8422 | ret = io_rsrc_node_switch_start(ctx); |
8dd03afe | 8423 | if (ret) |
f2303b1f | 8424 | break; |
a7f0ed5a | 8425 | io_rsrc_node_switch(ctx, data); |
f2303b1f | 8426 | |
3e942498 PB |
8427 | /* kill initial ref, already quiesced if zero */ |
8428 | if (atomic_dec_and_test(&data->refs)) | |
8429 | break; | |
c018db4a | 8430 | mutex_unlock(&ctx->uring_lock); |
8bad28d8 | 8431 | flush_delayed_work(&ctx->rsrc_put_work); |
1ffc5422 | 8432 | ret = wait_for_completion_interruptible(&data->done); |
c018db4a JA |
8433 | if (!ret) { |
8434 | mutex_lock(&ctx->uring_lock); | |
80912cef DY |
8435 | if (atomic_read(&data->refs) > 0) { |
8436 | /* | |
8437 | * it has been revived by another thread while | |
8438 | * we were unlocked | |
8439 | */ | |
8440 | mutex_unlock(&ctx->uring_lock); | |
8441 | } else { | |
8442 | break; | |
8443 | } | |
c018db4a | 8444 | } |
8bad28d8 | 8445 | |
3e942498 PB |
8446 | atomic_inc(&data->refs); |
8447 | /* wait for all works potentially completing data->done */ | |
8448 | flush_delayed_work(&ctx->rsrc_put_work); | |
cb5e1b81 | 8449 | reinit_completion(&data->done); |
8dd03afe | 8450 | |
1ffc5422 | 8451 | ret = io_run_task_work_sig(); |
8bad28d8 | 8452 | mutex_lock(&ctx->uring_lock); |
f2303b1f | 8453 | } while (ret >= 0); |
8bad28d8 | 8454 | data->quiesce = false; |
05f3fb3c | 8455 | |
8bad28d8 | 8456 | return ret; |
d7954b2b BM |
8457 | } |
8458 | ||
2d091d62 PB |
8459 | static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx) |
8460 | { | |
8461 | unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK; | |
8462 | unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT; | |
8463 | ||
8464 | return &data->tags[table_idx][off]; | |
8465 | } | |
8466 | ||
44b31f2f | 8467 | static void io_rsrc_data_free(struct io_rsrc_data *data) |
1ad555c6 | 8468 | { |
2d091d62 PB |
8469 | size_t size = data->nr * sizeof(data->tags[0][0]); |
8470 | ||
8471 | if (data->tags) | |
8472 | io_free_page_table((void **)data->tags, size); | |
44b31f2f PB |
8473 | kfree(data); |
8474 | } | |
8475 | ||
c072481d PB |
8476 | static __cold int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put, |
8477 | u64 __user *utags, unsigned nr, | |
8478 | struct io_rsrc_data **pdata) | |
1ad555c6 | 8479 | { |
b895c9a6 | 8480 | struct io_rsrc_data *data; |
2d091d62 | 8481 | int ret = -ENOMEM; |
d878c816 | 8482 | unsigned i; |
1ad555c6 BM |
8483 | |
8484 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
8485 | if (!data) | |
d878c816 | 8486 | return -ENOMEM; |
2d091d62 | 8487 | data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0])); |
b60c8dce | 8488 | if (!data->tags) { |
1ad555c6 | 8489 | kfree(data); |
d878c816 PB |
8490 | return -ENOMEM; |
8491 | } | |
2d091d62 PB |
8492 | |
8493 | data->nr = nr; | |
8494 | data->ctx = ctx; | |
8495 | data->do_put = do_put; | |
d878c816 | 8496 | if (utags) { |
2d091d62 | 8497 | ret = -EFAULT; |
d878c816 | 8498 | for (i = 0; i < nr; i++) { |
fdd1dc31 CIK |
8499 | u64 *tag_slot = io_get_tag_slot(data, i); |
8500 | ||
8501 | if (copy_from_user(tag_slot, &utags[i], | |
8502 | sizeof(*tag_slot))) | |
2d091d62 | 8503 | goto fail; |
d878c816 | 8504 | } |
1ad555c6 | 8505 | } |
b60c8dce | 8506 | |
3e942498 | 8507 | atomic_set(&data->refs, 1); |
1ad555c6 | 8508 | init_completion(&data->done); |
d878c816 PB |
8509 | *pdata = data; |
8510 | return 0; | |
2d091d62 PB |
8511 | fail: |
8512 | io_rsrc_data_free(data); | |
8513 | return ret; | |
1ad555c6 BM |
8514 | } |
8515 | ||
9123c8ff PB |
8516 | static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files) |
8517 | { | |
0bea96f5 PB |
8518 | table->files = kvcalloc(nr_files, sizeof(table->files[0]), |
8519 | GFP_KERNEL_ACCOUNT); | |
9123c8ff PB |
8520 | return !!table->files; |
8521 | } | |
8522 | ||
042b0d85 | 8523 | static void io_free_file_tables(struct io_file_table *table) |
9123c8ff | 8524 | { |
042b0d85 | 8525 | kvfree(table->files); |
9123c8ff PB |
8526 | table->files = NULL; |
8527 | } | |
8528 | ||
fff4db76 | 8529 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
1ad555c6 | 8530 | { |
fff4db76 PB |
8531 | #if defined(CONFIG_UNIX) |
8532 | if (ctx->ring_sock) { | |
8533 | struct sock *sock = ctx->ring_sock->sk; | |
8534 | struct sk_buff *skb; | |
8535 | ||
8536 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
8537 | kfree_skb(skb); | |
8538 | } | |
8539 | #else | |
8540 | int i; | |
8541 | ||
8542 | for (i = 0; i < ctx->nr_user_files; i++) { | |
8543 | struct file *file; | |
8544 | ||
8545 | file = io_file_from_index(ctx, i); | |
8546 | if (file) | |
8547 | fput(file); | |
8548 | } | |
8549 | #endif | |
042b0d85 | 8550 | io_free_file_tables(&ctx->file_table); |
44b31f2f | 8551 | io_rsrc_data_free(ctx->file_data); |
fff4db76 PB |
8552 | ctx->file_data = NULL; |
8553 | ctx->nr_user_files = 0; | |
1ad555c6 BM |
8554 | } |
8555 | ||
d7954b2b BM |
8556 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
8557 | { | |
d7954b2b BM |
8558 | int ret; |
8559 | ||
08480400 | 8560 | if (!ctx->file_data) |
d7954b2b | 8561 | return -ENXIO; |
08480400 PB |
8562 | ret = io_rsrc_ref_quiesce(ctx->file_data, ctx); |
8563 | if (!ret) | |
8564 | __io_sqe_files_unregister(ctx); | |
8565 | return ret; | |
6b06314c JA |
8566 | } |
8567 | ||
37d1e2e3 | 8568 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
09a6f4ef | 8569 | __releases(&sqd->lock) |
37d1e2e3 | 8570 | { |
521d6a73 PB |
8571 | WARN_ON_ONCE(sqd->thread == current); |
8572 | ||
9e138a48 PB |
8573 | /* |
8574 | * Do the dance but not conditional clear_bit() because it'd race with | |
8575 | * other threads incrementing park_pending and setting the bit. | |
8576 | */ | |
37d1e2e3 | 8577 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
9e138a48 PB |
8578 | if (atomic_dec_return(&sqd->park_pending)) |
8579 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); | |
09a6f4ef | 8580 | mutex_unlock(&sqd->lock); |
37d1e2e3 JA |
8581 | } |
8582 | ||
86e0d676 | 8583 | static void io_sq_thread_park(struct io_sq_data *sqd) |
09a6f4ef | 8584 | __acquires(&sqd->lock) |
37d1e2e3 | 8585 | { |
521d6a73 PB |
8586 | WARN_ON_ONCE(sqd->thread == current); |
8587 | ||
9e138a48 | 8588 | atomic_inc(&sqd->park_pending); |
86e0d676 | 8589 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
09a6f4ef | 8590 | mutex_lock(&sqd->lock); |
05962f95 | 8591 | if (sqd->thread) |
86e0d676 | 8592 | wake_up_process(sqd->thread); |
37d1e2e3 JA |
8593 | } |
8594 | ||
8595 | static void io_sq_thread_stop(struct io_sq_data *sqd) | |
8596 | { | |
521d6a73 | 8597 | WARN_ON_ONCE(sqd->thread == current); |
88885f66 | 8598 | WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)); |
521d6a73 | 8599 | |
05962f95 | 8600 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
88885f66 | 8601 | mutex_lock(&sqd->lock); |
e8f98f24 JA |
8602 | if (sqd->thread) |
8603 | wake_up_process(sqd->thread); | |
09a6f4ef | 8604 | mutex_unlock(&sqd->lock); |
05962f95 | 8605 | wait_for_completion(&sqd->exited); |
37d1e2e3 JA |
8606 | } |
8607 | ||
534ca6d6 | 8608 | static void io_put_sq_data(struct io_sq_data *sqd) |
6c271ce2 | 8609 | { |
534ca6d6 | 8610 | if (refcount_dec_and_test(&sqd->refs)) { |
9e138a48 PB |
8611 | WARN_ON_ONCE(atomic_read(&sqd->park_pending)); |
8612 | ||
37d1e2e3 JA |
8613 | io_sq_thread_stop(sqd); |
8614 | kfree(sqd); | |
8615 | } | |
8616 | } | |
8617 | ||
8618 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) | |
8619 | { | |
8620 | struct io_sq_data *sqd = ctx->sq_data; | |
8621 | ||
8622 | if (sqd) { | |
05962f95 | 8623 | io_sq_thread_park(sqd); |
521d6a73 | 8624 | list_del_init(&ctx->sqd_list); |
37d1e2e3 | 8625 | io_sqd_update_thread_idle(sqd); |
05962f95 | 8626 | io_sq_thread_unpark(sqd); |
37d1e2e3 JA |
8627 | |
8628 | io_put_sq_data(sqd); | |
8629 | ctx->sq_data = NULL; | |
534ca6d6 JA |
8630 | } |
8631 | } | |
8632 | ||
aa06165d JA |
8633 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
8634 | { | |
8635 | struct io_ring_ctx *ctx_attach; | |
8636 | struct io_sq_data *sqd; | |
8637 | struct fd f; | |
8638 | ||
8639 | f = fdget(p->wq_fd); | |
8640 | if (!f.file) | |
8641 | return ERR_PTR(-ENXIO); | |
8642 | if (f.file->f_op != &io_uring_fops) { | |
8643 | fdput(f); | |
8644 | return ERR_PTR(-EINVAL); | |
8645 | } | |
8646 | ||
8647 | ctx_attach = f.file->private_data; | |
8648 | sqd = ctx_attach->sq_data; | |
8649 | if (!sqd) { | |
8650 | fdput(f); | |
8651 | return ERR_PTR(-EINVAL); | |
8652 | } | |
5c2469e0 JA |
8653 | if (sqd->task_tgid != current->tgid) { |
8654 | fdput(f); | |
8655 | return ERR_PTR(-EPERM); | |
8656 | } | |
aa06165d JA |
8657 | |
8658 | refcount_inc(&sqd->refs); | |
8659 | fdput(f); | |
8660 | return sqd; | |
8661 | } | |
8662 | ||
26984fbf PB |
8663 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p, |
8664 | bool *attached) | |
534ca6d6 JA |
8665 | { |
8666 | struct io_sq_data *sqd; | |
8667 | ||
26984fbf | 8668 | *attached = false; |
5c2469e0 JA |
8669 | if (p->flags & IORING_SETUP_ATTACH_WQ) { |
8670 | sqd = io_attach_sq_data(p); | |
26984fbf PB |
8671 | if (!IS_ERR(sqd)) { |
8672 | *attached = true; | |
5c2469e0 | 8673 | return sqd; |
26984fbf | 8674 | } |
5c2469e0 JA |
8675 | /* fall through for EPERM case, setup new sqd/task */ |
8676 | if (PTR_ERR(sqd) != -EPERM) | |
8677 | return sqd; | |
8678 | } | |
aa06165d | 8679 | |
534ca6d6 JA |
8680 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
8681 | if (!sqd) | |
8682 | return ERR_PTR(-ENOMEM); | |
8683 | ||
9e138a48 | 8684 | atomic_set(&sqd->park_pending, 0); |
534ca6d6 | 8685 | refcount_set(&sqd->refs, 1); |
69fb2131 | 8686 | INIT_LIST_HEAD(&sqd->ctx_list); |
09a6f4ef | 8687 | mutex_init(&sqd->lock); |
534ca6d6 | 8688 | init_waitqueue_head(&sqd->wait); |
37d1e2e3 | 8689 | init_completion(&sqd->exited); |
534ca6d6 JA |
8690 | return sqd; |
8691 | } | |
8692 | ||
6b06314c | 8693 | #if defined(CONFIG_UNIX) |
6b06314c JA |
8694 | /* |
8695 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
8696 | * the io_uring can be safely unregistered on process exit, even if we have | |
8697 | * loops in the file referencing. | |
8698 | */ | |
8699 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
8700 | { | |
8701 | struct sock *sk = ctx->ring_sock->sk; | |
8702 | struct scm_fp_list *fpl; | |
8703 | struct sk_buff *skb; | |
08a45173 | 8704 | int i, nr_files; |
6b06314c | 8705 | |
6b06314c JA |
8706 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
8707 | if (!fpl) | |
8708 | return -ENOMEM; | |
8709 | ||
8710 | skb = alloc_skb(0, GFP_KERNEL); | |
8711 | if (!skb) { | |
8712 | kfree(fpl); | |
8713 | return -ENOMEM; | |
8714 | } | |
8715 | ||
8716 | skb->sk = sk; | |
6b06314c | 8717 | |
08a45173 | 8718 | nr_files = 0; |
62e398be | 8719 | fpl->user = get_uid(current_user()); |
6b06314c | 8720 | for (i = 0; i < nr; i++) { |
65e19f54 JA |
8721 | struct file *file = io_file_from_index(ctx, i + offset); |
8722 | ||
8723 | if (!file) | |
08a45173 | 8724 | continue; |
65e19f54 | 8725 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
8726 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
8727 | nr_files++; | |
6b06314c JA |
8728 | } |
8729 | ||
08a45173 JA |
8730 | if (nr_files) { |
8731 | fpl->max = SCM_MAX_FD; | |
8732 | fpl->count = nr_files; | |
8733 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 8734 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
8735 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
8736 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 8737 | |
08a45173 JA |
8738 | for (i = 0; i < nr_files; i++) |
8739 | fput(fpl->fp[i]); | |
8740 | } else { | |
8741 | kfree_skb(skb); | |
8742 | kfree(fpl); | |
8743 | } | |
6b06314c JA |
8744 | |
8745 | return 0; | |
8746 | } | |
8747 | ||
8748 | /* | |
8749 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
8750 | * causes regular reference counting to break down. We rely on the UNIX | |
8751 | * garbage collection to take care of this problem for us. | |
8752 | */ | |
8753 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
8754 | { | |
8755 | unsigned left, total; | |
8756 | int ret = 0; | |
8757 | ||
8758 | total = 0; | |
8759 | left = ctx->nr_user_files; | |
8760 | while (left) { | |
8761 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
8762 | |
8763 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
8764 | if (ret) | |
8765 | break; | |
8766 | left -= this_files; | |
8767 | total += this_files; | |
8768 | } | |
8769 | ||
8770 | if (!ret) | |
8771 | return 0; | |
8772 | ||
8773 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
8774 | struct file *file = io_file_from_index(ctx, total); |
8775 | ||
8776 | if (file) | |
8777 | fput(file); | |
6b06314c JA |
8778 | total++; |
8779 | } | |
8780 | ||
8781 | return ret; | |
8782 | } | |
8783 | #else | |
8784 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
8785 | { | |
8786 | return 0; | |
8787 | } | |
8788 | #endif | |
8789 | ||
47e90392 | 8790 | static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
05f3fb3c | 8791 | { |
50238531 | 8792 | struct file *file = prsrc->file; |
05f3fb3c JA |
8793 | #if defined(CONFIG_UNIX) |
8794 | struct sock *sock = ctx->ring_sock->sk; | |
8795 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
8796 | struct sk_buff *skb; | |
8797 | int i; | |
8798 | ||
8799 | __skb_queue_head_init(&list); | |
8800 | ||
8801 | /* | |
8802 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
8803 | * remove this entry and rearrange the file array. | |
8804 | */ | |
8805 | skb = skb_dequeue(head); | |
8806 | while (skb) { | |
8807 | struct scm_fp_list *fp; | |
8808 | ||
8809 | fp = UNIXCB(skb).fp; | |
8810 | for (i = 0; i < fp->count; i++) { | |
8811 | int left; | |
8812 | ||
8813 | if (fp->fp[i] != file) | |
8814 | continue; | |
8815 | ||
8816 | unix_notinflight(fp->user, fp->fp[i]); | |
8817 | left = fp->count - 1 - i; | |
8818 | if (left) { | |
8819 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
8820 | left * sizeof(struct file *)); | |
8821 | } | |
8822 | fp->count--; | |
8823 | if (!fp->count) { | |
8824 | kfree_skb(skb); | |
8825 | skb = NULL; | |
8826 | } else { | |
8827 | __skb_queue_tail(&list, skb); | |
8828 | } | |
8829 | fput(file); | |
8830 | file = NULL; | |
8831 | break; | |
8832 | } | |
8833 | ||
8834 | if (!file) | |
8835 | break; | |
8836 | ||
8837 | __skb_queue_tail(&list, skb); | |
8838 | ||
8839 | skb = skb_dequeue(head); | |
8840 | } | |
8841 | ||
8842 | if (skb_peek(&list)) { | |
8843 | spin_lock_irq(&head->lock); | |
8844 | while ((skb = __skb_dequeue(&list)) != NULL) | |
8845 | __skb_queue_tail(head, skb); | |
8846 | spin_unlock_irq(&head->lock); | |
8847 | } | |
8848 | #else | |
8849 | fput(file); | |
8850 | #endif | |
8851 | } | |
8852 | ||
b895c9a6 | 8853 | static void __io_rsrc_put_work(struct io_rsrc_node *ref_node) |
65e19f54 | 8854 | { |
b895c9a6 | 8855 | struct io_rsrc_data *rsrc_data = ref_node->rsrc_data; |
269bbe5f BM |
8856 | struct io_ring_ctx *ctx = rsrc_data->ctx; |
8857 | struct io_rsrc_put *prsrc, *tmp; | |
05589553 | 8858 | |
269bbe5f BM |
8859 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
8860 | list_del(&prsrc->list); | |
b60c8dce PB |
8861 | |
8862 | if (prsrc->tag) { | |
8863 | bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL; | |
b60c8dce PB |
8864 | |
8865 | io_ring_submit_lock(ctx, lock_ring); | |
79ebeaee | 8866 | spin_lock(&ctx->completion_lock); |
913a571a | 8867 | io_fill_cqe_aux(ctx, prsrc->tag, 0, 0); |
b60c8dce | 8868 | io_commit_cqring(ctx); |
79ebeaee | 8869 | spin_unlock(&ctx->completion_lock); |
b60c8dce PB |
8870 | io_cqring_ev_posted(ctx); |
8871 | io_ring_submit_unlock(ctx, lock_ring); | |
8872 | } | |
8873 | ||
40ae0ff7 | 8874 | rsrc_data->do_put(ctx, prsrc); |
269bbe5f | 8875 | kfree(prsrc); |
65e19f54 | 8876 | } |
05589553 | 8877 | |
28a9fe25 | 8878 | io_rsrc_node_destroy(ref_node); |
3e942498 PB |
8879 | if (atomic_dec_and_test(&rsrc_data->refs)) |
8880 | complete(&rsrc_data->done); | |
2faf852d | 8881 | } |
65e19f54 | 8882 | |
269bbe5f | 8883 | static void io_rsrc_put_work(struct work_struct *work) |
4a38aed2 JA |
8884 | { |
8885 | struct io_ring_ctx *ctx; | |
8886 | struct llist_node *node; | |
8887 | ||
269bbe5f BM |
8888 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
8889 | node = llist_del_all(&ctx->rsrc_put_llist); | |
4a38aed2 JA |
8890 | |
8891 | while (node) { | |
b895c9a6 | 8892 | struct io_rsrc_node *ref_node; |
4a38aed2 JA |
8893 | struct llist_node *next = node->next; |
8894 | ||
b895c9a6 | 8895 | ref_node = llist_entry(node, struct io_rsrc_node, llist); |
269bbe5f | 8896 | __io_rsrc_put_work(ref_node); |
4a38aed2 JA |
8897 | node = next; |
8898 | } | |
8899 | } | |
8900 | ||
6b06314c | 8901 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
792e3582 | 8902 | unsigned nr_args, u64 __user *tags) |
6b06314c JA |
8903 | { |
8904 | __s32 __user *fds = (__s32 __user *) arg; | |
05f3fb3c | 8905 | struct file *file; |
f3baed39 | 8906 | int fd, ret; |
846a4ef2 | 8907 | unsigned i; |
6b06314c | 8908 | |
05f3fb3c | 8909 | if (ctx->file_data) |
6b06314c JA |
8910 | return -EBUSY; |
8911 | if (!nr_args) | |
8912 | return -EINVAL; | |
8913 | if (nr_args > IORING_MAX_FIXED_FILES) | |
8914 | return -EMFILE; | |
3a1b8a4e PB |
8915 | if (nr_args > rlimit(RLIMIT_NOFILE)) |
8916 | return -EMFILE; | |
a7f0ed5a | 8917 | ret = io_rsrc_node_switch_start(ctx); |
f3baed39 PB |
8918 | if (ret) |
8919 | return ret; | |
d878c816 PB |
8920 | ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args, |
8921 | &ctx->file_data); | |
8922 | if (ret) | |
8923 | return ret; | |
6b06314c | 8924 | |
f3baed39 | 8925 | ret = -ENOMEM; |
aeca241b | 8926 | if (!io_alloc_file_tables(&ctx->file_table, nr_args)) |
1ad555c6 | 8927 | goto out_free; |
65e19f54 | 8928 | |
08a45173 | 8929 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
d878c816 | 8930 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
600cf3f8 PB |
8931 | ret = -EFAULT; |
8932 | goto out_fput; | |
8933 | } | |
08a45173 | 8934 | /* allow sparse sets */ |
792e3582 PB |
8935 | if (fd == -1) { |
8936 | ret = -EINVAL; | |
2d091d62 | 8937 | if (unlikely(*io_get_tag_slot(ctx->file_data, i))) |
792e3582 | 8938 | goto out_fput; |
08a45173 | 8939 | continue; |
792e3582 | 8940 | } |
6b06314c | 8941 | |
05f3fb3c | 8942 | file = fget(fd); |
6b06314c | 8943 | ret = -EBADF; |
792e3582 | 8944 | if (unlikely(!file)) |
600cf3f8 | 8945 | goto out_fput; |
05f3fb3c | 8946 | |
6b06314c JA |
8947 | /* |
8948 | * Don't allow io_uring instances to be registered. If UNIX | |
8949 | * isn't enabled, then this causes a reference cycle and this | |
8950 | * instance can never get freed. If UNIX is enabled we'll | |
8951 | * handle it just fine, but there's still no point in allowing | |
8952 | * a ring fd as it doesn't support regular read/write anyway. | |
8953 | */ | |
05f3fb3c JA |
8954 | if (file->f_op == &io_uring_fops) { |
8955 | fput(file); | |
600cf3f8 | 8956 | goto out_fput; |
6b06314c | 8957 | } |
aeca241b | 8958 | io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file); |
6b06314c JA |
8959 | } |
8960 | ||
6b06314c | 8961 | ret = io_sqe_files_scm(ctx); |
05589553 | 8962 | if (ret) { |
08480400 | 8963 | __io_sqe_files_unregister(ctx); |
05589553 XW |
8964 | return ret; |
8965 | } | |
6b06314c | 8966 | |
a7f0ed5a | 8967 | io_rsrc_node_switch(ctx, NULL); |
6b06314c | 8968 | return ret; |
600cf3f8 PB |
8969 | out_fput: |
8970 | for (i = 0; i < ctx->nr_user_files; i++) { | |
8971 | file = io_file_from_index(ctx, i); | |
8972 | if (file) | |
8973 | fput(file); | |
8974 | } | |
042b0d85 | 8975 | io_free_file_tables(&ctx->file_table); |
600cf3f8 | 8976 | ctx->nr_user_files = 0; |
600cf3f8 | 8977 | out_free: |
44b31f2f | 8978 | io_rsrc_data_free(ctx->file_data); |
55cbc256 | 8979 | ctx->file_data = NULL; |
6b06314c JA |
8980 | return ret; |
8981 | } | |
8982 | ||
c3a31e60 JA |
8983 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
8984 | int index) | |
8985 | { | |
8986 | #if defined(CONFIG_UNIX) | |
8987 | struct sock *sock = ctx->ring_sock->sk; | |
8988 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
8989 | struct sk_buff *skb; | |
8990 | ||
8991 | /* | |
8992 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
8993 | * file set. If there's no room, fall back to allocating a new skb | |
8994 | * and filling it in. | |
8995 | */ | |
8996 | spin_lock_irq(&head->lock); | |
8997 | skb = skb_peek(head); | |
8998 | if (skb) { | |
8999 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
9000 | ||
9001 | if (fpl->count < SCM_MAX_FD) { | |
9002 | __skb_unlink(skb, head); | |
9003 | spin_unlock_irq(&head->lock); | |
9004 | fpl->fp[fpl->count] = get_file(file); | |
9005 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
9006 | fpl->count++; | |
9007 | spin_lock_irq(&head->lock); | |
9008 | __skb_queue_head(head, skb); | |
9009 | } else { | |
9010 | skb = NULL; | |
9011 | } | |
9012 | } | |
9013 | spin_unlock_irq(&head->lock); | |
9014 | ||
9015 | if (skb) { | |
9016 | fput(file); | |
9017 | return 0; | |
9018 | } | |
9019 | ||
9020 | return __io_sqe_files_scm(ctx, 1, index); | |
9021 | #else | |
9022 | return 0; | |
9023 | #endif | |
9024 | } | |
9025 | ||
9c7b0ba8 PB |
9026 | static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, |
9027 | struct io_rsrc_node *node, void *rsrc) | |
9028 | { | |
9029 | struct io_rsrc_put *prsrc; | |
9030 | ||
9031 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); | |
9032 | if (!prsrc) | |
9033 | return -ENOMEM; | |
9034 | ||
9035 | prsrc->tag = *io_get_tag_slot(data, idx); | |
9036 | prsrc->rsrc = rsrc; | |
9037 | list_add(&prsrc->list, &node->rsrc_list); | |
9038 | return 0; | |
9039 | } | |
9040 | ||
b9445598 PB |
9041 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
9042 | unsigned int issue_flags, u32 slot_index) | |
9043 | { | |
9044 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 9045 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
9c7b0ba8 | 9046 | bool needs_switch = false; |
b9445598 PB |
9047 | struct io_fixed_file *file_slot; |
9048 | int ret = -EBADF; | |
9049 | ||
3b44b371 | 9050 | io_ring_submit_lock(ctx, needs_lock); |
b9445598 PB |
9051 | if (file->f_op == &io_uring_fops) |
9052 | goto err; | |
9053 | ret = -ENXIO; | |
9054 | if (!ctx->file_data) | |
9055 | goto err; | |
9056 | ret = -EINVAL; | |
9057 | if (slot_index >= ctx->nr_user_files) | |
9058 | goto err; | |
9059 | ||
9060 | slot_index = array_index_nospec(slot_index, ctx->nr_user_files); | |
9061 | file_slot = io_fixed_file_slot(&ctx->file_table, slot_index); | |
9c7b0ba8 PB |
9062 | |
9063 | if (file_slot->file_ptr) { | |
9064 | struct file *old_file; | |
9065 | ||
9066 | ret = io_rsrc_node_switch_start(ctx); | |
9067 | if (ret) | |
9068 | goto err; | |
9069 | ||
9070 | old_file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
9071 | ret = io_queue_rsrc_removal(ctx->file_data, slot_index, | |
9072 | ctx->rsrc_node, old_file); | |
9073 | if (ret) | |
9074 | goto err; | |
9075 | file_slot->file_ptr = 0; | |
9076 | needs_switch = true; | |
9077 | } | |
b9445598 PB |
9078 | |
9079 | *io_get_tag_slot(ctx->file_data, slot_index) = 0; | |
9080 | io_fixed_file_set(file_slot, file); | |
9081 | ret = io_sqe_file_register(ctx, file, slot_index); | |
9082 | if (ret) { | |
9083 | file_slot->file_ptr = 0; | |
9084 | goto err; | |
9085 | } | |
9086 | ||
9087 | ret = 0; | |
9088 | err: | |
9c7b0ba8 PB |
9089 | if (needs_switch) |
9090 | io_rsrc_node_switch(ctx, ctx->file_data); | |
3b44b371 | 9091 | io_ring_submit_unlock(ctx, needs_lock); |
b9445598 PB |
9092 | if (ret) |
9093 | fput(file); | |
9094 | return ret; | |
9095 | } | |
9096 | ||
7df778be PB |
9097 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags) |
9098 | { | |
9099 | unsigned int offset = req->close.file_slot - 1; | |
9100 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 9101 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
7df778be PB |
9102 | struct io_fixed_file *file_slot; |
9103 | struct file *file; | |
9104 | int ret, i; | |
9105 | ||
3b44b371 | 9106 | io_ring_submit_lock(ctx, needs_lock); |
7df778be PB |
9107 | ret = -ENXIO; |
9108 | if (unlikely(!ctx->file_data)) | |
9109 | goto out; | |
9110 | ret = -EINVAL; | |
9111 | if (offset >= ctx->nr_user_files) | |
9112 | goto out; | |
9113 | ret = io_rsrc_node_switch_start(ctx); | |
9114 | if (ret) | |
9115 | goto out; | |
9116 | ||
9117 | i = array_index_nospec(offset, ctx->nr_user_files); | |
9118 | file_slot = io_fixed_file_slot(&ctx->file_table, i); | |
9119 | ret = -EBADF; | |
9120 | if (!file_slot->file_ptr) | |
9121 | goto out; | |
9122 | ||
9123 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
9124 | ret = io_queue_rsrc_removal(ctx->file_data, offset, ctx->rsrc_node, file); | |
9125 | if (ret) | |
9126 | goto out; | |
9127 | ||
9128 | file_slot->file_ptr = 0; | |
9129 | io_rsrc_node_switch(ctx, ctx->file_data); | |
9130 | ret = 0; | |
9131 | out: | |
3b44b371 | 9132 | io_ring_submit_unlock(ctx, needs_lock); |
7df778be PB |
9133 | return ret; |
9134 | } | |
9135 | ||
05f3fb3c | 9136 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
c3bdad02 | 9137 | struct io_uring_rsrc_update2 *up, |
05f3fb3c JA |
9138 | unsigned nr_args) |
9139 | { | |
c3bdad02 | 9140 | u64 __user *tags = u64_to_user_ptr(up->tags); |
98f0b3b4 | 9141 | __s32 __user *fds = u64_to_user_ptr(up->data); |
b895c9a6 | 9142 | struct io_rsrc_data *data = ctx->file_data; |
a04b0ac0 PB |
9143 | struct io_fixed_file *file_slot; |
9144 | struct file *file; | |
98f0b3b4 PB |
9145 | int fd, i, err = 0; |
9146 | unsigned int done; | |
05589553 | 9147 | bool needs_switch = false; |
c3a31e60 | 9148 | |
98f0b3b4 PB |
9149 | if (!ctx->file_data) |
9150 | return -ENXIO; | |
9151 | if (up->offset + nr_args > ctx->nr_user_files) | |
c3a31e60 JA |
9152 | return -EINVAL; |
9153 | ||
67973b93 | 9154 | for (done = 0; done < nr_args; done++) { |
c3bdad02 PB |
9155 | u64 tag = 0; |
9156 | ||
9157 | if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) || | |
9158 | copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
c3a31e60 JA |
9159 | err = -EFAULT; |
9160 | break; | |
9161 | } | |
c3bdad02 PB |
9162 | if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) { |
9163 | err = -EINVAL; | |
9164 | break; | |
9165 | } | |
4e0377a1 | 9166 | if (fd == IORING_REGISTER_FILES_SKIP) |
9167 | continue; | |
9168 | ||
67973b93 | 9169 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
aeca241b | 9170 | file_slot = io_fixed_file_slot(&ctx->file_table, i); |
ea64ec02 | 9171 | |
a04b0ac0 PB |
9172 | if (file_slot->file_ptr) { |
9173 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
b60c8dce PB |
9174 | err = io_queue_rsrc_removal(data, up->offset + done, |
9175 | ctx->rsrc_node, file); | |
a5318d3c HD |
9176 | if (err) |
9177 | break; | |
a04b0ac0 | 9178 | file_slot->file_ptr = 0; |
05589553 | 9179 | needs_switch = true; |
c3a31e60 JA |
9180 | } |
9181 | if (fd != -1) { | |
c3a31e60 JA |
9182 | file = fget(fd); |
9183 | if (!file) { | |
9184 | err = -EBADF; | |
9185 | break; | |
9186 | } | |
9187 | /* | |
9188 | * Don't allow io_uring instances to be registered. If | |
9189 | * UNIX isn't enabled, then this causes a reference | |
9190 | * cycle and this instance can never get freed. If UNIX | |
9191 | * is enabled we'll handle it just fine, but there's | |
9192 | * still no point in allowing a ring fd as it doesn't | |
9193 | * support regular read/write anyway. | |
9194 | */ | |
9195 | if (file->f_op == &io_uring_fops) { | |
9196 | fput(file); | |
9197 | err = -EBADF; | |
9198 | break; | |
9199 | } | |
2d091d62 | 9200 | *io_get_tag_slot(data, up->offset + done) = tag; |
9a321c98 | 9201 | io_fixed_file_set(file_slot, file); |
c3a31e60 | 9202 | err = io_sqe_file_register(ctx, file, i); |
f3bd9dae | 9203 | if (err) { |
a04b0ac0 | 9204 | file_slot->file_ptr = 0; |
f3bd9dae | 9205 | fput(file); |
c3a31e60 | 9206 | break; |
f3bd9dae | 9207 | } |
c3a31e60 | 9208 | } |
05f3fb3c JA |
9209 | } |
9210 | ||
a7f0ed5a PB |
9211 | if (needs_switch) |
9212 | io_rsrc_node_switch(ctx, data); | |
c3a31e60 JA |
9213 | return done ? done : err; |
9214 | } | |
05589553 | 9215 | |
685fe7fe JA |
9216 | static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx, |
9217 | struct task_struct *task) | |
24369c2e | 9218 | { |
e941894e | 9219 | struct io_wq_hash *hash; |
24369c2e | 9220 | struct io_wq_data data; |
24369c2e | 9221 | unsigned int concurrency; |
24369c2e | 9222 | |
362a9e65 | 9223 | mutex_lock(&ctx->uring_lock); |
e941894e JA |
9224 | hash = ctx->hash_map; |
9225 | if (!hash) { | |
9226 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); | |
362a9e65 YY |
9227 | if (!hash) { |
9228 | mutex_unlock(&ctx->uring_lock); | |
e941894e | 9229 | return ERR_PTR(-ENOMEM); |
362a9e65 | 9230 | } |
e941894e JA |
9231 | refcount_set(&hash->refs, 1); |
9232 | init_waitqueue_head(&hash->wait); | |
9233 | ctx->hash_map = hash; | |
24369c2e | 9234 | } |
362a9e65 | 9235 | mutex_unlock(&ctx->uring_lock); |
24369c2e | 9236 | |
e941894e | 9237 | data.hash = hash; |
685fe7fe | 9238 | data.task = task; |
ebc11b6c | 9239 | data.free_work = io_wq_free_work; |
f5fa38c5 | 9240 | data.do_work = io_wq_submit_work; |
24369c2e | 9241 | |
d25e3a3d JA |
9242 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
9243 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
24369c2e | 9244 | |
5aa75ed5 | 9245 | return io_wq_create(concurrency, &data); |
24369c2e PB |
9246 | } |
9247 | ||
c072481d PB |
9248 | static __cold int io_uring_alloc_task_context(struct task_struct *task, |
9249 | struct io_ring_ctx *ctx) | |
0f212204 JA |
9250 | { |
9251 | struct io_uring_task *tctx; | |
d8a6df10 | 9252 | int ret; |
0f212204 | 9253 | |
09899b19 | 9254 | tctx = kzalloc(sizeof(*tctx), GFP_KERNEL); |
0f212204 JA |
9255 | if (unlikely(!tctx)) |
9256 | return -ENOMEM; | |
9257 | ||
e7a6c00d JA |
9258 | tctx->registered_rings = kcalloc(IO_RINGFD_REG_MAX, |
9259 | sizeof(struct file *), GFP_KERNEL); | |
9260 | if (unlikely(!tctx->registered_rings)) { | |
9261 | kfree(tctx); | |
9262 | return -ENOMEM; | |
9263 | } | |
9264 | ||
d8a6df10 JA |
9265 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
9266 | if (unlikely(ret)) { | |
e7a6c00d | 9267 | kfree(tctx->registered_rings); |
d8a6df10 JA |
9268 | kfree(tctx); |
9269 | return ret; | |
9270 | } | |
9271 | ||
685fe7fe | 9272 | tctx->io_wq = io_init_wq_offload(ctx, task); |
5aa75ed5 JA |
9273 | if (IS_ERR(tctx->io_wq)) { |
9274 | ret = PTR_ERR(tctx->io_wq); | |
9275 | percpu_counter_destroy(&tctx->inflight); | |
e7a6c00d | 9276 | kfree(tctx->registered_rings); |
5aa75ed5 JA |
9277 | kfree(tctx); |
9278 | return ret; | |
9279 | } | |
9280 | ||
0f212204 JA |
9281 | xa_init(&tctx->xa); |
9282 | init_waitqueue_head(&tctx->wait); | |
fdaf083c | 9283 | atomic_set(&tctx->in_idle, 0); |
b303fe2e | 9284 | atomic_set(&tctx->inflight_tracked, 0); |
0f212204 | 9285 | task->io_uring = tctx; |
7cbf1722 JA |
9286 | spin_lock_init(&tctx->task_lock); |
9287 | INIT_WQ_LIST(&tctx->task_list); | |
4813c377 | 9288 | INIT_WQ_LIST(&tctx->prior_task_list); |
7cbf1722 | 9289 | init_task_work(&tctx->task_work, tctx_task_work); |
0f212204 JA |
9290 | return 0; |
9291 | } | |
9292 | ||
9293 | void __io_uring_free(struct task_struct *tsk) | |
9294 | { | |
9295 | struct io_uring_task *tctx = tsk->io_uring; | |
9296 | ||
9297 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); | |
ef8eaa4e | 9298 | WARN_ON_ONCE(tctx->io_wq); |
09899b19 | 9299 | WARN_ON_ONCE(tctx->cached_refs); |
ef8eaa4e | 9300 | |
e7a6c00d | 9301 | kfree(tctx->registered_rings); |
d8a6df10 | 9302 | percpu_counter_destroy(&tctx->inflight); |
0f212204 JA |
9303 | kfree(tctx); |
9304 | tsk->io_uring = NULL; | |
9305 | } | |
9306 | ||
c072481d PB |
9307 | static __cold int io_sq_offload_create(struct io_ring_ctx *ctx, |
9308 | struct io_uring_params *p) | |
2b188cc1 JA |
9309 | { |
9310 | int ret; | |
9311 | ||
d25e3a3d JA |
9312 | /* Retain compatibility with failing for an invalid attach attempt */ |
9313 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == | |
9314 | IORING_SETUP_ATTACH_WQ) { | |
9315 | struct fd f; | |
9316 | ||
9317 | f = fdget(p->wq_fd); | |
9318 | if (!f.file) | |
9319 | return -ENXIO; | |
0cc936f7 JA |
9320 | if (f.file->f_op != &io_uring_fops) { |
9321 | fdput(f); | |
f2a48dd0 | 9322 | return -EINVAL; |
0cc936f7 JA |
9323 | } |
9324 | fdput(f); | |
d25e3a3d | 9325 | } |
6c271ce2 | 9326 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
46fe18b1 | 9327 | struct task_struct *tsk; |
534ca6d6 | 9328 | struct io_sq_data *sqd; |
26984fbf | 9329 | bool attached; |
534ca6d6 | 9330 | |
cdc1404a PM |
9331 | ret = security_uring_sqpoll(); |
9332 | if (ret) | |
9333 | return ret; | |
9334 | ||
26984fbf | 9335 | sqd = io_get_sq_data(p, &attached); |
534ca6d6 JA |
9336 | if (IS_ERR(sqd)) { |
9337 | ret = PTR_ERR(sqd); | |
9338 | goto err; | |
9339 | } | |
69fb2131 | 9340 | |
7c30f36a | 9341 | ctx->sq_creds = get_current_cred(); |
534ca6d6 | 9342 | ctx->sq_data = sqd; |
917257da JA |
9343 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
9344 | if (!ctx->sq_thread_idle) | |
9345 | ctx->sq_thread_idle = HZ; | |
9346 | ||
78d7f6ba | 9347 | io_sq_thread_park(sqd); |
de75a3d3 PB |
9348 | list_add(&ctx->sqd_list, &sqd->ctx_list); |
9349 | io_sqd_update_thread_idle(sqd); | |
26984fbf | 9350 | /* don't attach to a dying SQPOLL thread, would be racy */ |
f2a48dd0 | 9351 | ret = (attached && !sqd->thread) ? -ENXIO : 0; |
78d7f6ba PB |
9352 | io_sq_thread_unpark(sqd); |
9353 | ||
de75a3d3 PB |
9354 | if (ret < 0) |
9355 | goto err; | |
9356 | if (attached) | |
5aa75ed5 | 9357 | return 0; |
aa06165d | 9358 | |
6c271ce2 | 9359 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 9360 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 9361 | |
917257da | 9362 | ret = -EINVAL; |
f2a48dd0 | 9363 | if (cpu >= nr_cpu_ids || !cpu_online(cpu)) |
e8f98f24 | 9364 | goto err_sqpoll; |
37d1e2e3 | 9365 | sqd->sq_cpu = cpu; |
6c271ce2 | 9366 | } else { |
37d1e2e3 | 9367 | sqd->sq_cpu = -1; |
6c271ce2 | 9368 | } |
37d1e2e3 JA |
9369 | |
9370 | sqd->task_pid = current->pid; | |
5c2469e0 | 9371 | sqd->task_tgid = current->tgid; |
46fe18b1 JA |
9372 | tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE); |
9373 | if (IS_ERR(tsk)) { | |
9374 | ret = PTR_ERR(tsk); | |
e8f98f24 | 9375 | goto err_sqpoll; |
6c271ce2 | 9376 | } |
97a73a0f | 9377 | |
46fe18b1 | 9378 | sqd->thread = tsk; |
97a73a0f | 9379 | ret = io_uring_alloc_task_context(tsk, ctx); |
46fe18b1 | 9380 | wake_up_new_task(tsk); |
0f212204 JA |
9381 | if (ret) |
9382 | goto err; | |
6c271ce2 JA |
9383 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
9384 | /* Can't have SQ_AFF without SQPOLL */ | |
9385 | ret = -EINVAL; | |
9386 | goto err; | |
9387 | } | |
9388 | ||
2b188cc1 | 9389 | return 0; |
f2a48dd0 PB |
9390 | err_sqpoll: |
9391 | complete(&ctx->sq_data->exited); | |
2b188cc1 | 9392 | err: |
37d1e2e3 | 9393 | io_sq_thread_finish(ctx); |
2b188cc1 JA |
9394 | return ret; |
9395 | } | |
9396 | ||
a087e2b5 BM |
9397 | static inline void __io_unaccount_mem(struct user_struct *user, |
9398 | unsigned long nr_pages) | |
2b188cc1 JA |
9399 | { |
9400 | atomic_long_sub(nr_pages, &user->locked_vm); | |
9401 | } | |
9402 | ||
a087e2b5 BM |
9403 | static inline int __io_account_mem(struct user_struct *user, |
9404 | unsigned long nr_pages) | |
2b188cc1 JA |
9405 | { |
9406 | unsigned long page_limit, cur_pages, new_pages; | |
9407 | ||
9408 | /* Don't allow more pages than we can safely lock */ | |
9409 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
9410 | ||
9411 | do { | |
9412 | cur_pages = atomic_long_read(&user->locked_vm); | |
9413 | new_pages = cur_pages + nr_pages; | |
9414 | if (new_pages > page_limit) | |
9415 | return -ENOMEM; | |
9416 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
9417 | new_pages) != cur_pages); | |
9418 | ||
9419 | return 0; | |
9420 | } | |
9421 | ||
26bfa89e | 9422 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 9423 | { |
62e398be | 9424 | if (ctx->user) |
a087e2b5 | 9425 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 9426 | |
26bfa89e JA |
9427 | if (ctx->mm_account) |
9428 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
9429 | } |
9430 | ||
26bfa89e | 9431 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 9432 | { |
30975825 BM |
9433 | int ret; |
9434 | ||
62e398be | 9435 | if (ctx->user) { |
30975825 BM |
9436 | ret = __io_account_mem(ctx->user, nr_pages); |
9437 | if (ret) | |
9438 | return ret; | |
9439 | } | |
9440 | ||
26bfa89e JA |
9441 | if (ctx->mm_account) |
9442 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
9443 | |
9444 | return 0; | |
9445 | } | |
9446 | ||
2b188cc1 JA |
9447 | static void io_mem_free(void *ptr) |
9448 | { | |
52e04ef4 MR |
9449 | struct page *page; |
9450 | ||
9451 | if (!ptr) | |
9452 | return; | |
2b188cc1 | 9453 | |
52e04ef4 | 9454 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
9455 | if (put_page_testzero(page)) |
9456 | free_compound_page(page); | |
9457 | } | |
9458 | ||
9459 | static void *io_mem_alloc(size_t size) | |
9460 | { | |
0a3f1e0b | 9461 | gfp_t gfp = GFP_KERNEL_ACCOUNT | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP; |
2b188cc1 | 9462 | |
0a3f1e0b | 9463 | return (void *) __get_free_pages(gfp, get_order(size)); |
2b188cc1 JA |
9464 | } |
9465 | ||
75b28aff HV |
9466 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
9467 | size_t *sq_offset) | |
9468 | { | |
9469 | struct io_rings *rings; | |
9470 | size_t off, sq_array_size; | |
9471 | ||
9472 | off = struct_size(rings, cqes, cq_entries); | |
9473 | if (off == SIZE_MAX) | |
9474 | return SIZE_MAX; | |
9475 | ||
9476 | #ifdef CONFIG_SMP | |
9477 | off = ALIGN(off, SMP_CACHE_BYTES); | |
9478 | if (off == 0) | |
9479 | return SIZE_MAX; | |
9480 | #endif | |
9481 | ||
b36200f5 DV |
9482 | if (sq_offset) |
9483 | *sq_offset = off; | |
9484 | ||
75b28aff HV |
9485 | sq_array_size = array_size(sizeof(u32), sq_entries); |
9486 | if (sq_array_size == SIZE_MAX) | |
9487 | return SIZE_MAX; | |
9488 | ||
9489 | if (check_add_overflow(off, sq_array_size, &off)) | |
9490 | return SIZE_MAX; | |
9491 | ||
75b28aff HV |
9492 | return off; |
9493 | } | |
9494 | ||
41edf1a5 | 9495 | static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot) |
7f61a1e9 | 9496 | { |
41edf1a5 | 9497 | struct io_mapped_ubuf *imu = *slot; |
7f61a1e9 PB |
9498 | unsigned int i; |
9499 | ||
6224843d PB |
9500 | if (imu != ctx->dummy_ubuf) { |
9501 | for (i = 0; i < imu->nr_bvecs; i++) | |
9502 | unpin_user_page(imu->bvec[i].bv_page); | |
9503 | if (imu->acct_pages) | |
9504 | io_unaccount_mem(ctx, imu->acct_pages); | |
9505 | kvfree(imu); | |
9506 | } | |
41edf1a5 | 9507 | *slot = NULL; |
7f61a1e9 PB |
9508 | } |
9509 | ||
bd54b6fe | 9510 | static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
edafccee | 9511 | { |
634d00df PB |
9512 | io_buffer_unmap(ctx, &prsrc->buf); |
9513 | prsrc->buf = NULL; | |
bd54b6fe | 9514 | } |
edafccee | 9515 | |
bd54b6fe BM |
9516 | static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
9517 | { | |
9518 | unsigned int i; | |
edafccee | 9519 | |
7f61a1e9 PB |
9520 | for (i = 0; i < ctx->nr_user_bufs; i++) |
9521 | io_buffer_unmap(ctx, &ctx->user_bufs[i]); | |
edafccee | 9522 | kfree(ctx->user_bufs); |
bb6659cc | 9523 | io_rsrc_data_free(ctx->buf_data); |
edafccee | 9524 | ctx->user_bufs = NULL; |
bd54b6fe | 9525 | ctx->buf_data = NULL; |
edafccee | 9526 | ctx->nr_user_bufs = 0; |
bd54b6fe BM |
9527 | } |
9528 | ||
0a96bbe4 | 9529 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
edafccee | 9530 | { |
bd54b6fe | 9531 | int ret; |
edafccee | 9532 | |
bd54b6fe | 9533 | if (!ctx->buf_data) |
edafccee JA |
9534 | return -ENXIO; |
9535 | ||
bd54b6fe BM |
9536 | ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx); |
9537 | if (!ret) | |
9538 | __io_sqe_buffers_unregister(ctx); | |
9539 | return ret; | |
edafccee JA |
9540 | } |
9541 | ||
9542 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
9543 | void __user *arg, unsigned index) | |
9544 | { | |
9545 | struct iovec __user *src; | |
9546 | ||
9547 | #ifdef CONFIG_COMPAT | |
9548 | if (ctx->compat) { | |
9549 | struct compat_iovec __user *ciovs; | |
9550 | struct compat_iovec ciov; | |
9551 | ||
9552 | ciovs = (struct compat_iovec __user *) arg; | |
9553 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
9554 | return -EFAULT; | |
9555 | ||
d55e5f5b | 9556 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
9557 | dst->iov_len = ciov.iov_len; |
9558 | return 0; | |
9559 | } | |
9560 | #endif | |
9561 | src = (struct iovec __user *) arg; | |
9562 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
9563 | return -EFAULT; | |
9564 | return 0; | |
9565 | } | |
9566 | ||
de293938 JA |
9567 | /* |
9568 | * Not super efficient, but this is just a registration time. And we do cache | |
9569 | * the last compound head, so generally we'll only do a full search if we don't | |
9570 | * match that one. | |
9571 | * | |
9572 | * We check if the given compound head page has already been accounted, to | |
9573 | * avoid double accounting it. This allows us to account the full size of the | |
9574 | * page, not just the constituent pages of a huge page. | |
9575 | */ | |
9576 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, | |
9577 | int nr_pages, struct page *hpage) | |
9578 | { | |
9579 | int i, j; | |
9580 | ||
9581 | /* check current page array */ | |
9582 | for (i = 0; i < nr_pages; i++) { | |
9583 | if (!PageCompound(pages[i])) | |
9584 | continue; | |
9585 | if (compound_head(pages[i]) == hpage) | |
9586 | return true; | |
9587 | } | |
9588 | ||
9589 | /* check previously registered pages */ | |
9590 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
41edf1a5 | 9591 | struct io_mapped_ubuf *imu = ctx->user_bufs[i]; |
de293938 JA |
9592 | |
9593 | for (j = 0; j < imu->nr_bvecs; j++) { | |
9594 | if (!PageCompound(imu->bvec[j].bv_page)) | |
9595 | continue; | |
9596 | if (compound_head(imu->bvec[j].bv_page) == hpage) | |
9597 | return true; | |
9598 | } | |
9599 | } | |
9600 | ||
9601 | return false; | |
9602 | } | |
9603 | ||
9604 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, | |
9605 | int nr_pages, struct io_mapped_ubuf *imu, | |
9606 | struct page **last_hpage) | |
9607 | { | |
9608 | int i, ret; | |
9609 | ||
216e5835 | 9610 | imu->acct_pages = 0; |
de293938 JA |
9611 | for (i = 0; i < nr_pages; i++) { |
9612 | if (!PageCompound(pages[i])) { | |
9613 | imu->acct_pages++; | |
9614 | } else { | |
9615 | struct page *hpage; | |
9616 | ||
9617 | hpage = compound_head(pages[i]); | |
9618 | if (hpage == *last_hpage) | |
9619 | continue; | |
9620 | *last_hpage = hpage; | |
9621 | if (headpage_already_acct(ctx, pages, i, hpage)) | |
9622 | continue; | |
9623 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; | |
9624 | } | |
9625 | } | |
9626 | ||
9627 | if (!imu->acct_pages) | |
9628 | return 0; | |
9629 | ||
26bfa89e | 9630 | ret = io_account_mem(ctx, imu->acct_pages); |
de293938 JA |
9631 | if (ret) |
9632 | imu->acct_pages = 0; | |
9633 | return ret; | |
9634 | } | |
9635 | ||
0a96bbe4 | 9636 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
41edf1a5 | 9637 | struct io_mapped_ubuf **pimu, |
0a96bbe4 | 9638 | struct page **last_hpage) |
edafccee | 9639 | { |
41edf1a5 | 9640 | struct io_mapped_ubuf *imu = NULL; |
edafccee JA |
9641 | struct vm_area_struct **vmas = NULL; |
9642 | struct page **pages = NULL; | |
0a96bbe4 BM |
9643 | unsigned long off, start, end, ubuf; |
9644 | size_t size; | |
9645 | int ret, pret, nr_pages, i; | |
9646 | ||
6224843d PB |
9647 | if (!iov->iov_base) { |
9648 | *pimu = ctx->dummy_ubuf; | |
9649 | return 0; | |
9650 | } | |
9651 | ||
0a96bbe4 BM |
9652 | ubuf = (unsigned long) iov->iov_base; |
9653 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
9654 | start = ubuf >> PAGE_SHIFT; | |
9655 | nr_pages = end - start; | |
9656 | ||
41edf1a5 | 9657 | *pimu = NULL; |
0a96bbe4 BM |
9658 | ret = -ENOMEM; |
9659 | ||
9660 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); | |
9661 | if (!pages) | |
9662 | goto done; | |
9663 | ||
9664 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), | |
9665 | GFP_KERNEL); | |
9666 | if (!vmas) | |
9667 | goto done; | |
edafccee | 9668 | |
41edf1a5 | 9669 | imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL); |
a2b4198c | 9670 | if (!imu) |
0a96bbe4 BM |
9671 | goto done; |
9672 | ||
9673 | ret = 0; | |
9674 | mmap_read_lock(current->mm); | |
9675 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, | |
9676 | pages, vmas); | |
9677 | if (pret == nr_pages) { | |
9678 | /* don't support file backed memory */ | |
9679 | for (i = 0; i < nr_pages; i++) { | |
9680 | struct vm_area_struct *vma = vmas[i]; | |
9681 | ||
40dad765 PB |
9682 | if (vma_is_shmem(vma)) |
9683 | continue; | |
0a96bbe4 BM |
9684 | if (vma->vm_file && |
9685 | !is_file_hugepages(vma->vm_file)) { | |
9686 | ret = -EOPNOTSUPP; | |
9687 | break; | |
9688 | } | |
9689 | } | |
9690 | } else { | |
9691 | ret = pret < 0 ? pret : -EFAULT; | |
9692 | } | |
9693 | mmap_read_unlock(current->mm); | |
9694 | if (ret) { | |
9695 | /* | |
9696 | * if we did partial map, or found file backed vmas, | |
9697 | * release any pages we did get | |
9698 | */ | |
9699 | if (pret > 0) | |
9700 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
9701 | goto done; |
9702 | } | |
9703 | ||
9704 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); | |
9705 | if (ret) { | |
9706 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
9707 | goto done; |
9708 | } | |
9709 | ||
9710 | off = ubuf & ~PAGE_MASK; | |
9711 | size = iov->iov_len; | |
9712 | for (i = 0; i < nr_pages; i++) { | |
9713 | size_t vec_len; | |
9714 | ||
9715 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
9716 | imu->bvec[i].bv_page = pages[i]; | |
9717 | imu->bvec[i].bv_len = vec_len; | |
9718 | imu->bvec[i].bv_offset = off; | |
9719 | off = 0; | |
9720 | size -= vec_len; | |
9721 | } | |
9722 | /* store original address for later verification */ | |
9723 | imu->ubuf = ubuf; | |
4751f53d | 9724 | imu->ubuf_end = ubuf + iov->iov_len; |
0a96bbe4 | 9725 | imu->nr_bvecs = nr_pages; |
41edf1a5 | 9726 | *pimu = imu; |
0a96bbe4 BM |
9727 | ret = 0; |
9728 | done: | |
41edf1a5 PB |
9729 | if (ret) |
9730 | kvfree(imu); | |
0a96bbe4 BM |
9731 | kvfree(pages); |
9732 | kvfree(vmas); | |
9733 | return ret; | |
9734 | } | |
9735 | ||
2b358604 | 9736 | static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args) |
0a96bbe4 | 9737 | { |
87094465 PB |
9738 | ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL); |
9739 | return ctx->user_bufs ? 0 : -ENOMEM; | |
2b358604 | 9740 | } |
edafccee | 9741 | |
2b358604 BM |
9742 | static int io_buffer_validate(struct iovec *iov) |
9743 | { | |
50e96989 PB |
9744 | unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1); |
9745 | ||
2b358604 BM |
9746 | /* |
9747 | * Don't impose further limits on the size and buffer | |
9748 | * constraints here, we'll -EINVAL later when IO is | |
9749 | * submitted if they are wrong. | |
9750 | */ | |
6224843d PB |
9751 | if (!iov->iov_base) |
9752 | return iov->iov_len ? -EFAULT : 0; | |
9753 | if (!iov->iov_len) | |
2b358604 | 9754 | return -EFAULT; |
edafccee | 9755 | |
2b358604 BM |
9756 | /* arbitrary limit, but we need something */ |
9757 | if (iov->iov_len > SZ_1G) | |
9758 | return -EFAULT; | |
edafccee | 9759 | |
50e96989 PB |
9760 | if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp)) |
9761 | return -EOVERFLOW; | |
9762 | ||
2b358604 BM |
9763 | return 0; |
9764 | } | |
edafccee | 9765 | |
2b358604 | 9766 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
634d00df | 9767 | unsigned int nr_args, u64 __user *tags) |
2b358604 | 9768 | { |
bd54b6fe BM |
9769 | struct page *last_hpage = NULL; |
9770 | struct io_rsrc_data *data; | |
2b358604 BM |
9771 | int i, ret; |
9772 | struct iovec iov; | |
edafccee | 9773 | |
87094465 PB |
9774 | if (ctx->user_bufs) |
9775 | return -EBUSY; | |
489809e2 | 9776 | if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS) |
87094465 | 9777 | return -EINVAL; |
bd54b6fe | 9778 | ret = io_rsrc_node_switch_start(ctx); |
2b358604 BM |
9779 | if (ret) |
9780 | return ret; | |
d878c816 PB |
9781 | ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data); |
9782 | if (ret) | |
9783 | return ret; | |
bd54b6fe BM |
9784 | ret = io_buffers_map_alloc(ctx, nr_args); |
9785 | if (ret) { | |
bb6659cc | 9786 | io_rsrc_data_free(data); |
bd54b6fe BM |
9787 | return ret; |
9788 | } | |
edafccee | 9789 | |
87094465 | 9790 | for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) { |
edafccee JA |
9791 | ret = io_copy_iov(ctx, &iov, arg, i); |
9792 | if (ret) | |
0a96bbe4 | 9793 | break; |
2b358604 BM |
9794 | ret = io_buffer_validate(&iov); |
9795 | if (ret) | |
0a96bbe4 | 9796 | break; |
2d091d62 | 9797 | if (!iov.iov_base && *io_get_tag_slot(data, i)) { |
cf3770e7 CIK |
9798 | ret = -EINVAL; |
9799 | break; | |
9800 | } | |
edafccee | 9801 | |
41edf1a5 PB |
9802 | ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i], |
9803 | &last_hpage); | |
0a96bbe4 BM |
9804 | if (ret) |
9805 | break; | |
edafccee | 9806 | } |
0a96bbe4 | 9807 | |
bd54b6fe | 9808 | WARN_ON_ONCE(ctx->buf_data); |
0a96bbe4 | 9809 | |
bd54b6fe BM |
9810 | ctx->buf_data = data; |
9811 | if (ret) | |
9812 | __io_sqe_buffers_unregister(ctx); | |
9813 | else | |
9814 | io_rsrc_node_switch(ctx, NULL); | |
edafccee JA |
9815 | return ret; |
9816 | } | |
9817 | ||
634d00df PB |
9818 | static int __io_sqe_buffers_update(struct io_ring_ctx *ctx, |
9819 | struct io_uring_rsrc_update2 *up, | |
9820 | unsigned int nr_args) | |
9821 | { | |
9822 | u64 __user *tags = u64_to_user_ptr(up->tags); | |
9823 | struct iovec iov, __user *iovs = u64_to_user_ptr(up->data); | |
634d00df PB |
9824 | struct page *last_hpage = NULL; |
9825 | bool needs_switch = false; | |
9826 | __u32 done; | |
9827 | int i, err; | |
9828 | ||
9829 | if (!ctx->buf_data) | |
9830 | return -ENXIO; | |
9831 | if (up->offset + nr_args > ctx->nr_user_bufs) | |
9832 | return -EINVAL; | |
9833 | ||
9834 | for (done = 0; done < nr_args; done++) { | |
0b8c0e7c PB |
9835 | struct io_mapped_ubuf *imu; |
9836 | int offset = up->offset + done; | |
634d00df PB |
9837 | u64 tag = 0; |
9838 | ||
9839 | err = io_copy_iov(ctx, &iov, iovs, done); | |
9840 | if (err) | |
9841 | break; | |
9842 | if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) { | |
9843 | err = -EFAULT; | |
9844 | break; | |
9845 | } | |
0b8c0e7c PB |
9846 | err = io_buffer_validate(&iov); |
9847 | if (err) | |
9848 | break; | |
cf3770e7 CIK |
9849 | if (!iov.iov_base && tag) { |
9850 | err = -EINVAL; | |
9851 | break; | |
9852 | } | |
0b8c0e7c PB |
9853 | err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage); |
9854 | if (err) | |
9855 | break; | |
634d00df | 9856 | |
0b8c0e7c | 9857 | i = array_index_nospec(offset, ctx->nr_user_bufs); |
6224843d | 9858 | if (ctx->user_bufs[i] != ctx->dummy_ubuf) { |
0b8c0e7c PB |
9859 | err = io_queue_rsrc_removal(ctx->buf_data, offset, |
9860 | ctx->rsrc_node, ctx->user_bufs[i]); | |
9861 | if (unlikely(err)) { | |
9862 | io_buffer_unmap(ctx, &imu); | |
634d00df | 9863 | break; |
0b8c0e7c | 9864 | } |
634d00df PB |
9865 | ctx->user_bufs[i] = NULL; |
9866 | needs_switch = true; | |
9867 | } | |
9868 | ||
0b8c0e7c | 9869 | ctx->user_bufs[i] = imu; |
2d091d62 | 9870 | *io_get_tag_slot(ctx->buf_data, offset) = tag; |
634d00df PB |
9871 | } |
9872 | ||
9873 | if (needs_switch) | |
9874 | io_rsrc_node_switch(ctx, ctx->buf_data); | |
9875 | return done ? done : err; | |
9876 | } | |
9877 | ||
c75312dd UA |
9878 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg, |
9879 | unsigned int eventfd_async) | |
9b402849 | 9880 | { |
77bc59b4 | 9881 | struct io_ev_fd *ev_fd; |
9b402849 | 9882 | __s32 __user *fds = arg; |
f0a4e62b | 9883 | int fd; |
9b402849 | 9884 | |
77bc59b4 UA |
9885 | ev_fd = rcu_dereference_protected(ctx->io_ev_fd, |
9886 | lockdep_is_held(&ctx->uring_lock)); | |
9887 | if (ev_fd) | |
9b402849 JA |
9888 | return -EBUSY; |
9889 | ||
9890 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
9891 | return -EFAULT; | |
9892 | ||
77bc59b4 UA |
9893 | ev_fd = kmalloc(sizeof(*ev_fd), GFP_KERNEL); |
9894 | if (!ev_fd) | |
9895 | return -ENOMEM; | |
fe7e3257 | 9896 | |
77bc59b4 UA |
9897 | ev_fd->cq_ev_fd = eventfd_ctx_fdget(fd); |
9898 | if (IS_ERR(ev_fd->cq_ev_fd)) { | |
f0a4e62b | 9899 | int ret = PTR_ERR(ev_fd->cq_ev_fd); |
77bc59b4 | 9900 | kfree(ev_fd); |
9b402849 JA |
9901 | return ret; |
9902 | } | |
c75312dd | 9903 | ev_fd->eventfd_async = eventfd_async; |
9b402849 | 9904 | |
77bc59b4 | 9905 | rcu_assign_pointer(ctx->io_ev_fd, ev_fd); |
f0a4e62b | 9906 | return 0; |
77bc59b4 UA |
9907 | } |
9908 | ||
9909 | static void io_eventfd_put(struct rcu_head *rcu) | |
9910 | { | |
9911 | struct io_ev_fd *ev_fd = container_of(rcu, struct io_ev_fd, rcu); | |
9912 | ||
9913 | eventfd_ctx_put(ev_fd->cq_ev_fd); | |
9914 | kfree(ev_fd); | |
9b402849 JA |
9915 | } |
9916 | ||
9917 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
9918 | { | |
77bc59b4 UA |
9919 | struct io_ev_fd *ev_fd; |
9920 | ||
9921 | ev_fd = rcu_dereference_protected(ctx->io_ev_fd, | |
9922 | lockdep_is_held(&ctx->uring_lock)); | |
9923 | if (ev_fd) { | |
9924 | rcu_assign_pointer(ctx->io_ev_fd, NULL); | |
9925 | call_rcu(&ev_fd->rcu, io_eventfd_put); | |
9b402849 JA |
9926 | return 0; |
9927 | } | |
9928 | ||
9929 | return -ENXIO; | |
9930 | } | |
9931 | ||
5a2e745d JA |
9932 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
9933 | { | |
9e15c3a0 JA |
9934 | struct io_buffer *buf; |
9935 | unsigned long index; | |
9936 | ||
1d0254e6 | 9937 | xa_for_each(&ctx->io_buffers, index, buf) |
9e15c3a0 | 9938 | __io_remove_buffers(ctx, buf, index, -1U); |
cc3cec83 JA |
9939 | |
9940 | while (!list_empty(&ctx->io_buffers_pages)) { | |
9941 | struct page *page; | |
9942 | ||
9943 | page = list_first_entry(&ctx->io_buffers_pages, struct page, lru); | |
9944 | list_del_init(&page->lru); | |
9945 | __free_page(page); | |
9946 | } | |
5a2e745d JA |
9947 | } |
9948 | ||
4010fec4 | 9949 | static void io_req_caches_free(struct io_ring_ctx *ctx) |
2b188cc1 | 9950 | { |
cd0ca2e0 | 9951 | struct io_submit_state *state = &ctx->submit_state; |
37f0e767 | 9952 | int nr = 0; |
bf019da7 | 9953 | |
9a4fdbd8 | 9954 | mutex_lock(&ctx->uring_lock); |
cd0ca2e0 | 9955 | io_flush_cached_locked_reqs(ctx, state); |
9a4fdbd8 | 9956 | |
c2b6c6bc PB |
9957 | while (state->free_list.next) { |
9958 | struct io_wq_work_node *node; | |
9959 | struct io_kiocb *req; | |
9a4fdbd8 | 9960 | |
c2b6c6bc PB |
9961 | node = wq_stack_extract(&state->free_list); |
9962 | req = container_of(node, struct io_kiocb, comp_list); | |
9963 | kmem_cache_free(req_cachep, req); | |
37f0e767 | 9964 | nr++; |
c2b6c6bc | 9965 | } |
37f0e767 PB |
9966 | if (nr) |
9967 | percpu_ref_put_many(&ctx->refs, nr); | |
9a4fdbd8 JA |
9968 | mutex_unlock(&ctx->uring_lock); |
9969 | } | |
9970 | ||
43597aac | 9971 | static void io_wait_rsrc_data(struct io_rsrc_data *data) |
2b188cc1 | 9972 | { |
43597aac | 9973 | if (data && !atomic_dec_and_test(&data->refs)) |
bd54b6fe | 9974 | wait_for_completion(&data->done); |
bd54b6fe | 9975 | } |
04fc6c80 | 9976 | |
4d9237e3 JA |
9977 | static void io_flush_apoll_cache(struct io_ring_ctx *ctx) |
9978 | { | |
9979 | struct async_poll *apoll; | |
9980 | ||
9981 | while (!list_empty(&ctx->apoll_cache)) { | |
9982 | apoll = list_first_entry(&ctx->apoll_cache, struct async_poll, | |
9983 | poll.wait.entry); | |
9984 | list_del(&apoll->poll.wait.entry); | |
9985 | kfree(apoll); | |
9986 | } | |
9987 | } | |
9988 | ||
c072481d | 9989 | static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx) |
2b188cc1 | 9990 | { |
37d1e2e3 | 9991 | io_sq_thread_finish(ctx); |
2aede0e4 | 9992 | |
37d1e2e3 | 9993 | if (ctx->mm_account) { |
2aede0e4 JA |
9994 | mmdrop(ctx->mm_account); |
9995 | ctx->mm_account = NULL; | |
30975825 | 9996 | } |
def596e9 | 9997 | |
ab409402 | 9998 | io_rsrc_refs_drop(ctx); |
43597aac PB |
9999 | /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */ |
10000 | io_wait_rsrc_data(ctx->buf_data); | |
10001 | io_wait_rsrc_data(ctx->file_data); | |
10002 | ||
8bad28d8 | 10003 | mutex_lock(&ctx->uring_lock); |
43597aac | 10004 | if (ctx->buf_data) |
bd54b6fe | 10005 | __io_sqe_buffers_unregister(ctx); |
43597aac | 10006 | if (ctx->file_data) |
08480400 | 10007 | __io_sqe_files_unregister(ctx); |
c4ea060e PB |
10008 | if (ctx->rings) |
10009 | __io_cqring_overflow_flush(ctx, true); | |
9b402849 | 10010 | io_eventfd_unregister(ctx); |
4d9237e3 | 10011 | io_flush_apoll_cache(ctx); |
77bc59b4 | 10012 | mutex_unlock(&ctx->uring_lock); |
5a2e745d | 10013 | io_destroy_buffers(ctx); |
07db298a PB |
10014 | if (ctx->sq_creds) |
10015 | put_cred(ctx->sq_creds); | |
def596e9 | 10016 | |
a7f0ed5a PB |
10017 | /* there are no registered resources left, nobody uses it */ |
10018 | if (ctx->rsrc_node) | |
10019 | io_rsrc_node_destroy(ctx->rsrc_node); | |
8dd03afe | 10020 | if (ctx->rsrc_backup_node) |
b895c9a6 | 10021 | io_rsrc_node_destroy(ctx->rsrc_backup_node); |
a7f0ed5a | 10022 | flush_delayed_work(&ctx->rsrc_put_work); |
756ab7c0 | 10023 | flush_delayed_work(&ctx->fallback_work); |
a7f0ed5a PB |
10024 | |
10025 | WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list)); | |
10026 | WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist)); | |
def596e9 | 10027 | |
2b188cc1 | 10028 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
10029 | if (ctx->ring_sock) { |
10030 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 10031 | sock_release(ctx->ring_sock); |
355e8d26 | 10032 | } |
2b188cc1 | 10033 | #endif |
ef9dd637 | 10034 | WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list)); |
2b188cc1 | 10035 | |
75b28aff | 10036 | io_mem_free(ctx->rings); |
2b188cc1 | 10037 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
10038 | |
10039 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 10040 | free_uid(ctx->user); |
4010fec4 | 10041 | io_req_caches_free(ctx); |
e941894e JA |
10042 | if (ctx->hash_map) |
10043 | io_wq_put_hash(ctx->hash_map); | |
adc8682e | 10044 | io_free_napi_list(ctx); |
78076bb6 | 10045 | kfree(ctx->cancel_hash); |
6224843d | 10046 | kfree(ctx->dummy_ubuf); |
2b188cc1 JA |
10047 | kfree(ctx); |
10048 | } | |
10049 | ||
10050 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
10051 | { | |
10052 | struct io_ring_ctx *ctx = file->private_data; | |
10053 | __poll_t mask = 0; | |
10054 | ||
d60aa65b | 10055 | poll_wait(file, &ctx->cq_wait, wait); |
4f7067c3 SB |
10056 | /* |
10057 | * synchronizes with barrier from wq_has_sleeper call in | |
10058 | * io_commit_cqring | |
10059 | */ | |
2b188cc1 | 10060 | smp_rmb(); |
90554200 | 10061 | if (!io_sqring_full(ctx)) |
2b188cc1 | 10062 | mask |= EPOLLOUT | EPOLLWRNORM; |
ed670c3f HX |
10063 | |
10064 | /* | |
10065 | * Don't flush cqring overflow list here, just do a simple check. | |
10066 | * Otherwise there could possible be ABBA deadlock: | |
10067 | * CPU0 CPU1 | |
10068 | * ---- ---- | |
10069 | * lock(&ctx->uring_lock); | |
10070 | * lock(&ep->mtx); | |
10071 | * lock(&ctx->uring_lock); | |
10072 | * lock(&ep->mtx); | |
10073 | * | |
10074 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this | |
10075 | * pushs them to do the flush. | |
10076 | */ | |
5ed7a37d | 10077 | if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow)) |
2b188cc1 JA |
10078 | mask |= EPOLLIN | EPOLLRDNORM; |
10079 | ||
10080 | return mask; | |
10081 | } | |
10082 | ||
0bead8cd | 10083 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
071698e1 | 10084 | { |
4379bf8b | 10085 | const struct cred *creds; |
071698e1 | 10086 | |
61cf9370 | 10087 | creds = xa_erase(&ctx->personalities, id); |
4379bf8b JA |
10088 | if (creds) { |
10089 | put_cred(creds); | |
0bead8cd | 10090 | return 0; |
1e6fa521 | 10091 | } |
0bead8cd YD |
10092 | |
10093 | return -EINVAL; | |
10094 | } | |
10095 | ||
d56d938b PB |
10096 | struct io_tctx_exit { |
10097 | struct callback_head task_work; | |
10098 | struct completion completion; | |
baf186c4 | 10099 | struct io_ring_ctx *ctx; |
d56d938b PB |
10100 | }; |
10101 | ||
c072481d | 10102 | static __cold void io_tctx_exit_cb(struct callback_head *cb) |
d56d938b PB |
10103 | { |
10104 | struct io_uring_task *tctx = current->io_uring; | |
10105 | struct io_tctx_exit *work; | |
10106 | ||
10107 | work = container_of(cb, struct io_tctx_exit, task_work); | |
10108 | /* | |
10109 | * When @in_idle, we're in cancellation and it's racy to remove the | |
10110 | * node. It'll be removed by the end of cancellation, just ignore it. | |
10111 | */ | |
10112 | if (!atomic_read(&tctx->in_idle)) | |
eef51daa | 10113 | io_uring_del_tctx_node((unsigned long)work->ctx); |
d56d938b PB |
10114 | complete(&work->completion); |
10115 | } | |
10116 | ||
c072481d | 10117 | static __cold bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
28090c13 PB |
10118 | { |
10119 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
10120 | ||
10121 | return req->ctx == data; | |
10122 | } | |
10123 | ||
c072481d | 10124 | static __cold void io_ring_exit_work(struct work_struct *work) |
85faa7b8 | 10125 | { |
d56d938b | 10126 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work); |
b5bb3a24 | 10127 | unsigned long timeout = jiffies + HZ * 60 * 5; |
58d3be2c | 10128 | unsigned long interval = HZ / 20; |
d56d938b PB |
10129 | struct io_tctx_exit exit; |
10130 | struct io_tctx_node *node; | |
10131 | int ret; | |
85faa7b8 | 10132 | |
56952e91 JA |
10133 | /* |
10134 | * If we're doing polled IO and end up having requests being | |
10135 | * submitted async (out-of-line), then completions can come in while | |
10136 | * we're waiting for refs to drop. We need to reap these manually, | |
10137 | * as nobody else will be looking for them. | |
10138 | */ | |
b2edc0a7 | 10139 | do { |
3dd0c97a | 10140 | io_uring_try_cancel_requests(ctx, NULL, true); |
28090c13 PB |
10141 | if (ctx->sq_data) { |
10142 | struct io_sq_data *sqd = ctx->sq_data; | |
10143 | struct task_struct *tsk; | |
10144 | ||
10145 | io_sq_thread_park(sqd); | |
10146 | tsk = sqd->thread; | |
10147 | if (tsk && tsk->io_uring && tsk->io_uring->io_wq) | |
10148 | io_wq_cancel_cb(tsk->io_uring->io_wq, | |
10149 | io_cancel_ctx_cb, ctx, true); | |
10150 | io_sq_thread_unpark(sqd); | |
10151 | } | |
b5bb3a24 | 10152 | |
37f0e767 PB |
10153 | io_req_caches_free(ctx); |
10154 | ||
58d3be2c PB |
10155 | if (WARN_ON_ONCE(time_after(jiffies, timeout))) { |
10156 | /* there is little hope left, don't run it too often */ | |
10157 | interval = HZ * 60; | |
10158 | } | |
10159 | } while (!wait_for_completion_timeout(&ctx->ref_comp, interval)); | |
d56d938b | 10160 | |
7f00651a PB |
10161 | init_completion(&exit.completion); |
10162 | init_task_work(&exit.task_work, io_tctx_exit_cb); | |
10163 | exit.ctx = ctx; | |
89b5066e PB |
10164 | /* |
10165 | * Some may use context even when all refs and requests have been put, | |
10166 | * and they are free to do so while still holding uring_lock or | |
5b0a6acc | 10167 | * completion_lock, see io_req_task_submit(). Apart from other work, |
89b5066e PB |
10168 | * this lock/unlock section also waits them to finish. |
10169 | */ | |
d56d938b PB |
10170 | mutex_lock(&ctx->uring_lock); |
10171 | while (!list_empty(&ctx->tctx_list)) { | |
b5bb3a24 PB |
10172 | WARN_ON_ONCE(time_after(jiffies, timeout)); |
10173 | ||
d56d938b PB |
10174 | node = list_first_entry(&ctx->tctx_list, struct io_tctx_node, |
10175 | ctx_node); | |
7f00651a PB |
10176 | /* don't spin on a single task if cancellation failed */ |
10177 | list_rotate_left(&ctx->tctx_list); | |
d56d938b PB |
10178 | ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL); |
10179 | if (WARN_ON_ONCE(ret)) | |
10180 | continue; | |
d56d938b PB |
10181 | |
10182 | mutex_unlock(&ctx->uring_lock); | |
10183 | wait_for_completion(&exit.completion); | |
d56d938b PB |
10184 | mutex_lock(&ctx->uring_lock); |
10185 | } | |
10186 | mutex_unlock(&ctx->uring_lock); | |
79ebeaee JA |
10187 | spin_lock(&ctx->completion_lock); |
10188 | spin_unlock(&ctx->completion_lock); | |
d56d938b | 10189 | |
85faa7b8 JA |
10190 | io_ring_ctx_free(ctx); |
10191 | } | |
10192 | ||
80c4cbdb | 10193 | /* Returns true if we found and killed one or more timeouts */ |
c072481d PB |
10194 | static __cold bool io_kill_timeouts(struct io_ring_ctx *ctx, |
10195 | struct task_struct *tsk, bool cancel_all) | |
80c4cbdb PB |
10196 | { |
10197 | struct io_kiocb *req, *tmp; | |
10198 | int canceled = 0; | |
10199 | ||
79ebeaee JA |
10200 | spin_lock(&ctx->completion_lock); |
10201 | spin_lock_irq(&ctx->timeout_lock); | |
80c4cbdb | 10202 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
3dd0c97a | 10203 | if (io_match_task(req, tsk, cancel_all)) { |
80c4cbdb PB |
10204 | io_kill_timeout(req, -ECANCELED); |
10205 | canceled++; | |
10206 | } | |
10207 | } | |
79ebeaee | 10208 | spin_unlock_irq(&ctx->timeout_lock); |
51520426 PB |
10209 | if (canceled != 0) |
10210 | io_commit_cqring(ctx); | |
79ebeaee | 10211 | spin_unlock(&ctx->completion_lock); |
80c4cbdb PB |
10212 | if (canceled != 0) |
10213 | io_cqring_ev_posted(ctx); | |
10214 | return canceled != 0; | |
10215 | } | |
10216 | ||
c072481d | 10217 | static __cold void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
2b188cc1 | 10218 | { |
61cf9370 MWO |
10219 | unsigned long index; |
10220 | struct creds *creds; | |
10221 | ||
2b188cc1 JA |
10222 | mutex_lock(&ctx->uring_lock); |
10223 | percpu_ref_kill(&ctx->refs); | |
634578f8 | 10224 | if (ctx->rings) |
6c2450ae | 10225 | __io_cqring_overflow_flush(ctx, true); |
61cf9370 MWO |
10226 | xa_for_each(&ctx->personalities, index, creds) |
10227 | io_unregister_personality(ctx, index); | |
2b188cc1 JA |
10228 | mutex_unlock(&ctx->uring_lock); |
10229 | ||
3dd0c97a PB |
10230 | io_kill_timeouts(ctx, NULL, true); |
10231 | io_poll_remove_all(ctx, NULL, true); | |
561fb04a | 10232 | |
15dff286 | 10233 | /* if we failed setting up the ctx, we might not have any rings */ |
b2edc0a7 | 10234 | io_iopoll_try_reap_events(ctx); |
309fc03a | 10235 | |
85faa7b8 | 10236 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
10237 | /* |
10238 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
10239 | * if we're exiting a ton of rings at the same time. It just adds | |
10240 | * noise and overhead, there's no discernable change in runtime | |
10241 | * over using system_wq. | |
10242 | */ | |
10243 | queue_work(system_unbound_wq, &ctx->exit_work); | |
2b188cc1 JA |
10244 | } |
10245 | ||
10246 | static int io_uring_release(struct inode *inode, struct file *file) | |
10247 | { | |
10248 | struct io_ring_ctx *ctx = file->private_data; | |
10249 | ||
10250 | file->private_data = NULL; | |
10251 | io_ring_ctx_wait_and_kill(ctx); | |
10252 | return 0; | |
10253 | } | |
10254 | ||
f6edbabb PB |
10255 | struct io_task_cancel { |
10256 | struct task_struct *task; | |
3dd0c97a | 10257 | bool all; |
f6edbabb | 10258 | }; |
f254ac04 | 10259 | |
f6edbabb | 10260 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
b711d4ea | 10261 | { |
9a472ef7 | 10262 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
f6edbabb | 10263 | struct io_task_cancel *cancel = data; |
9a472ef7 | 10264 | |
6af3f48b | 10265 | return io_match_task_safe(req, cancel->task, cancel->all); |
b711d4ea JA |
10266 | } |
10267 | ||
c072481d PB |
10268 | static __cold bool io_cancel_defer_files(struct io_ring_ctx *ctx, |
10269 | struct task_struct *task, | |
10270 | bool cancel_all) | |
b7ddce3c | 10271 | { |
e1915f76 | 10272 | struct io_defer_entry *de; |
b7ddce3c PB |
10273 | LIST_HEAD(list); |
10274 | ||
79ebeaee | 10275 | spin_lock(&ctx->completion_lock); |
b7ddce3c | 10276 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
6af3f48b | 10277 | if (io_match_task_safe(de->req, task, cancel_all)) { |
b7ddce3c PB |
10278 | list_cut_position(&list, &ctx->defer_list, &de->list); |
10279 | break; | |
10280 | } | |
10281 | } | |
79ebeaee | 10282 | spin_unlock(&ctx->completion_lock); |
e1915f76 PB |
10283 | if (list_empty(&list)) |
10284 | return false; | |
b7ddce3c PB |
10285 | |
10286 | while (!list_empty(&list)) { | |
10287 | de = list_first_entry(&list, struct io_defer_entry, list); | |
10288 | list_del_init(&de->list); | |
f41db273 | 10289 | io_req_complete_failed(de->req, -ECANCELED); |
b7ddce3c PB |
10290 | kfree(de); |
10291 | } | |
e1915f76 | 10292 | return true; |
b7ddce3c PB |
10293 | } |
10294 | ||
c072481d | 10295 | static __cold bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx) |
1b00764f PB |
10296 | { |
10297 | struct io_tctx_node *node; | |
10298 | enum io_wq_cancel cret; | |
10299 | bool ret = false; | |
10300 | ||
10301 | mutex_lock(&ctx->uring_lock); | |
10302 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
10303 | struct io_uring_task *tctx = node->task->io_uring; | |
10304 | ||
10305 | /* | |
10306 | * io_wq will stay alive while we hold uring_lock, because it's | |
10307 | * killed after ctx nodes, which requires to take the lock. | |
10308 | */ | |
10309 | if (!tctx || !tctx->io_wq) | |
10310 | continue; | |
10311 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true); | |
10312 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
10313 | } | |
10314 | mutex_unlock(&ctx->uring_lock); | |
10315 | ||
10316 | return ret; | |
10317 | } | |
10318 | ||
c072481d PB |
10319 | static __cold void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
10320 | struct task_struct *task, | |
10321 | bool cancel_all) | |
9936c7c2 | 10322 | { |
3dd0c97a | 10323 | struct io_task_cancel cancel = { .task = task, .all = cancel_all, }; |
1b00764f | 10324 | struct io_uring_task *tctx = task ? task->io_uring : NULL; |
9936c7c2 PB |
10325 | |
10326 | while (1) { | |
10327 | enum io_wq_cancel cret; | |
10328 | bool ret = false; | |
10329 | ||
1b00764f PB |
10330 | if (!task) { |
10331 | ret |= io_uring_try_cancel_iowq(ctx); | |
10332 | } else if (tctx && tctx->io_wq) { | |
10333 | /* | |
10334 | * Cancels requests of all rings, not only @ctx, but | |
10335 | * it's fine as the task is in exit/exec. | |
10336 | */ | |
5aa75ed5 | 10337 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, |
9936c7c2 PB |
10338 | &cancel, true); |
10339 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
10340 | } | |
10341 | ||
10342 | /* SQPOLL thread does its own polling */ | |
3dd0c97a | 10343 | if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) || |
d052d1d6 | 10344 | (ctx->sq_data && ctx->sq_data->thread == current)) { |
5eef4e87 | 10345 | while (!wq_list_empty(&ctx->iopoll_list)) { |
9936c7c2 PB |
10346 | io_iopoll_try_reap_events(ctx); |
10347 | ret = true; | |
10348 | } | |
10349 | } | |
10350 | ||
3dd0c97a PB |
10351 | ret |= io_cancel_defer_files(ctx, task, cancel_all); |
10352 | ret |= io_poll_remove_all(ctx, task, cancel_all); | |
10353 | ret |= io_kill_timeouts(ctx, task, cancel_all); | |
e5dc480d PB |
10354 | if (task) |
10355 | ret |= io_run_task_work(); | |
9936c7c2 PB |
10356 | if (!ret) |
10357 | break; | |
10358 | cond_resched(); | |
10359 | } | |
10360 | } | |
10361 | ||
eef51daa | 10362 | static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
0f212204 | 10363 | { |
236434c3 | 10364 | struct io_uring_task *tctx = current->io_uring; |
13bf43f5 | 10365 | struct io_tctx_node *node; |
a528b04e | 10366 | int ret; |
236434c3 MWO |
10367 | |
10368 | if (unlikely(!tctx)) { | |
5aa75ed5 | 10369 | ret = io_uring_alloc_task_context(current, ctx); |
0f212204 JA |
10370 | if (unlikely(ret)) |
10371 | return ret; | |
e139a1ec | 10372 | |
236434c3 | 10373 | tctx = current->io_uring; |
e139a1ec PB |
10374 | if (ctx->iowq_limits_set) { |
10375 | unsigned int limits[2] = { ctx->iowq_limits[0], | |
10376 | ctx->iowq_limits[1], }; | |
10377 | ||
10378 | ret = io_wq_max_workers(tctx->io_wq, limits); | |
10379 | if (ret) | |
10380 | return ret; | |
10381 | } | |
0f212204 | 10382 | } |
cf27f3b1 PB |
10383 | if (!xa_load(&tctx->xa, (unsigned long)ctx)) { |
10384 | node = kmalloc(sizeof(*node), GFP_KERNEL); | |
10385 | if (!node) | |
10386 | return -ENOMEM; | |
10387 | node->ctx = ctx; | |
10388 | node->task = current; | |
13bf43f5 | 10389 | |
cf27f3b1 PB |
10390 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx, |
10391 | node, GFP_KERNEL)); | |
10392 | if (ret) { | |
10393 | kfree(node); | |
10394 | return ret; | |
0f212204 | 10395 | } |
cf27f3b1 PB |
10396 | |
10397 | mutex_lock(&ctx->uring_lock); | |
10398 | list_add(&node->ctx_node, &ctx->tctx_list); | |
10399 | mutex_unlock(&ctx->uring_lock); | |
0f212204 | 10400 | } |
cf27f3b1 | 10401 | tctx->last = ctx; |
0f212204 JA |
10402 | return 0; |
10403 | } | |
10404 | ||
cf27f3b1 PB |
10405 | /* |
10406 | * Note that this task has used io_uring. We use it for cancelation purposes. | |
10407 | */ | |
eef51daa | 10408 | static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
cf27f3b1 PB |
10409 | { |
10410 | struct io_uring_task *tctx = current->io_uring; | |
10411 | ||
10412 | if (likely(tctx && tctx->last == ctx)) | |
10413 | return 0; | |
eef51daa | 10414 | return __io_uring_add_tctx_node(ctx); |
cf27f3b1 PB |
10415 | } |
10416 | ||
0f212204 JA |
10417 | /* |
10418 | * Remove this io_uring_file -> task mapping. | |
10419 | */ | |
c072481d | 10420 | static __cold void io_uring_del_tctx_node(unsigned long index) |
0f212204 JA |
10421 | { |
10422 | struct io_uring_task *tctx = current->io_uring; | |
13bf43f5 | 10423 | struct io_tctx_node *node; |
2941267b | 10424 | |
eebd2e37 PB |
10425 | if (!tctx) |
10426 | return; | |
13bf43f5 PB |
10427 | node = xa_erase(&tctx->xa, index); |
10428 | if (!node) | |
2941267b | 10429 | return; |
0f212204 | 10430 | |
13bf43f5 PB |
10431 | WARN_ON_ONCE(current != node->task); |
10432 | WARN_ON_ONCE(list_empty(&node->ctx_node)); | |
10433 | ||
10434 | mutex_lock(&node->ctx->uring_lock); | |
10435 | list_del(&node->ctx_node); | |
10436 | mutex_unlock(&node->ctx->uring_lock); | |
10437 | ||
baf186c4 | 10438 | if (tctx->last == node->ctx) |
0f212204 | 10439 | tctx->last = NULL; |
13bf43f5 | 10440 | kfree(node); |
0f212204 JA |
10441 | } |
10442 | ||
c072481d | 10443 | static __cold void io_uring_clean_tctx(struct io_uring_task *tctx) |
de7f1d9e | 10444 | { |
ba5ef6dc | 10445 | struct io_wq *wq = tctx->io_wq; |
13bf43f5 | 10446 | struct io_tctx_node *node; |
de7f1d9e PB |
10447 | unsigned long index; |
10448 | ||
8bab4c09 | 10449 | xa_for_each(&tctx->xa, index, node) { |
eef51daa | 10450 | io_uring_del_tctx_node(index); |
8bab4c09 JA |
10451 | cond_resched(); |
10452 | } | |
b16ef427 ME |
10453 | if (wq) { |
10454 | /* | |
f6f9b278 | 10455 | * Must be after io_uring_del_tctx_node() (removes nodes under |
b16ef427 ME |
10456 | * uring_lock) to avoid race with io_uring_try_cancel_iowq(). |
10457 | */ | |
ba5ef6dc | 10458 | io_wq_put_and_exit(wq); |
dadebc35 | 10459 | tctx->io_wq = NULL; |
b16ef427 | 10460 | } |
de7f1d9e PB |
10461 | } |
10462 | ||
3f48cf18 | 10463 | static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked) |
521d6a73 | 10464 | { |
3f48cf18 PB |
10465 | if (tracked) |
10466 | return atomic_read(&tctx->inflight_tracked); | |
521d6a73 PB |
10467 | return percpu_counter_sum(&tctx->inflight); |
10468 | } | |
10469 | ||
78cc687b PB |
10470 | /* |
10471 | * Find any io_uring ctx that this task has registered or done IO on, and cancel | |
78a78060 | 10472 | * requests. @sqd should be not-null IFF it's an SQPOLL thread cancellation. |
78cc687b | 10473 | */ |
c072481d PB |
10474 | static __cold void io_uring_cancel_generic(bool cancel_all, |
10475 | struct io_sq_data *sqd) | |
0e9ddb39 | 10476 | { |
521d6a73 | 10477 | struct io_uring_task *tctx = current->io_uring; |
734551df | 10478 | struct io_ring_ctx *ctx; |
0e9ddb39 PB |
10479 | s64 inflight; |
10480 | DEFINE_WAIT(wait); | |
fdaf083c | 10481 | |
78cc687b PB |
10482 | WARN_ON_ONCE(sqd && sqd->thread != current); |
10483 | ||
6d042ffb PO |
10484 | if (!current->io_uring) |
10485 | return; | |
17a91051 PB |
10486 | if (tctx->io_wq) |
10487 | io_wq_exit_start(tctx->io_wq); | |
10488 | ||
0e9ddb39 PB |
10489 | atomic_inc(&tctx->in_idle); |
10490 | do { | |
e9dbe221 | 10491 | io_uring_drop_tctx_refs(current); |
0e9ddb39 | 10492 | /* read completions before cancelations */ |
78cc687b | 10493 | inflight = tctx_inflight(tctx, !cancel_all); |
0e9ddb39 PB |
10494 | if (!inflight) |
10495 | break; | |
fdaf083c | 10496 | |
78cc687b PB |
10497 | if (!sqd) { |
10498 | struct io_tctx_node *node; | |
10499 | unsigned long index; | |
0f212204 | 10500 | |
78cc687b PB |
10501 | xa_for_each(&tctx->xa, index, node) { |
10502 | /* sqpoll task will cancel all its requests */ | |
10503 | if (node->ctx->sq_data) | |
10504 | continue; | |
10505 | io_uring_try_cancel_requests(node->ctx, current, | |
10506 | cancel_all); | |
10507 | } | |
10508 | } else { | |
10509 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) | |
10510 | io_uring_try_cancel_requests(ctx, current, | |
10511 | cancel_all); | |
10512 | } | |
17a91051 | 10513 | |
78a78060 JA |
10514 | prepare_to_wait(&tctx->wait, &wait, TASK_INTERRUPTIBLE); |
10515 | io_run_task_work(); | |
e9dbe221 | 10516 | io_uring_drop_tctx_refs(current); |
78a78060 | 10517 | |
0f212204 | 10518 | /* |
a1bb3cd5 PB |
10519 | * If we've seen completions, retry without waiting. This |
10520 | * avoids a race where a completion comes in before we did | |
10521 | * prepare_to_wait(). | |
0f212204 | 10522 | */ |
3dd0c97a | 10523 | if (inflight == tctx_inflight(tctx, !cancel_all)) |
a1bb3cd5 | 10524 | schedule(); |
f57555ed | 10525 | finish_wait(&tctx->wait, &wait); |
d8a6df10 | 10526 | } while (1); |
de7f1d9e | 10527 | |
8452d4a6 | 10528 | io_uring_clean_tctx(tctx); |
3dd0c97a | 10529 | if (cancel_all) { |
3cc7fdb9 PB |
10530 | /* |
10531 | * We shouldn't run task_works after cancel, so just leave | |
10532 | * ->in_idle set for normal exit. | |
10533 | */ | |
10534 | atomic_dec(&tctx->in_idle); | |
3f48cf18 PB |
10535 | /* for exec all current's requests should be gone, kill tctx */ |
10536 | __io_uring_free(current); | |
10537 | } | |
44e728b8 PB |
10538 | } |
10539 | ||
f552a27a | 10540 | void __io_uring_cancel(bool cancel_all) |
78cc687b | 10541 | { |
f552a27a | 10542 | io_uring_cancel_generic(cancel_all, NULL); |
78cc687b PB |
10543 | } |
10544 | ||
e7a6c00d JA |
10545 | void io_uring_unreg_ringfd(void) |
10546 | { | |
10547 | struct io_uring_task *tctx = current->io_uring; | |
10548 | int i; | |
10549 | ||
10550 | for (i = 0; i < IO_RINGFD_REG_MAX; i++) { | |
10551 | if (tctx->registered_rings[i]) { | |
10552 | fput(tctx->registered_rings[i]); | |
10553 | tctx->registered_rings[i] = NULL; | |
10554 | } | |
10555 | } | |
10556 | } | |
10557 | ||
10558 | static int io_ring_add_registered_fd(struct io_uring_task *tctx, int fd, | |
10559 | int start, int end) | |
10560 | { | |
10561 | struct file *file; | |
10562 | int offset; | |
10563 | ||
10564 | for (offset = start; offset < end; offset++) { | |
10565 | offset = array_index_nospec(offset, IO_RINGFD_REG_MAX); | |
10566 | if (tctx->registered_rings[offset]) | |
10567 | continue; | |
10568 | ||
10569 | file = fget(fd); | |
10570 | if (!file) { | |
10571 | return -EBADF; | |
10572 | } else if (file->f_op != &io_uring_fops) { | |
10573 | fput(file); | |
10574 | return -EOPNOTSUPP; | |
10575 | } | |
10576 | tctx->registered_rings[offset] = file; | |
10577 | return offset; | |
10578 | } | |
10579 | ||
10580 | return -EBUSY; | |
10581 | } | |
10582 | ||
10583 | /* | |
10584 | * Register a ring fd to avoid fdget/fdput for each io_uring_enter() | |
10585 | * invocation. User passes in an array of struct io_uring_rsrc_update | |
10586 | * with ->data set to the ring_fd, and ->offset given for the desired | |
10587 | * index. If no index is desired, application may set ->offset == -1U | |
10588 | * and we'll find an available index. Returns number of entries | |
10589 | * successfully processed, or < 0 on error if none were processed. | |
10590 | */ | |
10591 | static int io_ringfd_register(struct io_ring_ctx *ctx, void __user *__arg, | |
10592 | unsigned nr_args) | |
10593 | { | |
10594 | struct io_uring_rsrc_update __user *arg = __arg; | |
10595 | struct io_uring_rsrc_update reg; | |
10596 | struct io_uring_task *tctx; | |
10597 | int ret, i; | |
10598 | ||
10599 | if (!nr_args || nr_args > IO_RINGFD_REG_MAX) | |
10600 | return -EINVAL; | |
10601 | ||
10602 | mutex_unlock(&ctx->uring_lock); | |
10603 | ret = io_uring_add_tctx_node(ctx); | |
10604 | mutex_lock(&ctx->uring_lock); | |
10605 | if (ret) | |
10606 | return ret; | |
10607 | ||
10608 | tctx = current->io_uring; | |
10609 | for (i = 0; i < nr_args; i++) { | |
10610 | int start, end; | |
10611 | ||
10612 | if (copy_from_user(®, &arg[i], sizeof(reg))) { | |
10613 | ret = -EFAULT; | |
10614 | break; | |
10615 | } | |
10616 | ||
10617 | if (reg.offset == -1U) { | |
10618 | start = 0; | |
10619 | end = IO_RINGFD_REG_MAX; | |
10620 | } else { | |
10621 | if (reg.offset >= IO_RINGFD_REG_MAX) { | |
10622 | ret = -EINVAL; | |
10623 | break; | |
10624 | } | |
10625 | start = reg.offset; | |
10626 | end = start + 1; | |
10627 | } | |
10628 | ||
10629 | ret = io_ring_add_registered_fd(tctx, reg.data, start, end); | |
10630 | if (ret < 0) | |
10631 | break; | |
10632 | ||
10633 | reg.offset = ret; | |
10634 | if (copy_to_user(&arg[i], ®, sizeof(reg))) { | |
10635 | fput(tctx->registered_rings[reg.offset]); | |
10636 | tctx->registered_rings[reg.offset] = NULL; | |
10637 | ret = -EFAULT; | |
10638 | break; | |
10639 | } | |
10640 | } | |
10641 | ||
10642 | return i ? i : ret; | |
10643 | } | |
10644 | ||
10645 | static int io_ringfd_unregister(struct io_ring_ctx *ctx, void __user *__arg, | |
10646 | unsigned nr_args) | |
10647 | { | |
10648 | struct io_uring_rsrc_update __user *arg = __arg; | |
10649 | struct io_uring_task *tctx = current->io_uring; | |
10650 | struct io_uring_rsrc_update reg; | |
10651 | int ret = 0, i; | |
10652 | ||
10653 | if (!nr_args || nr_args > IO_RINGFD_REG_MAX) | |
10654 | return -EINVAL; | |
10655 | if (!tctx) | |
10656 | return 0; | |
10657 | ||
10658 | for (i = 0; i < nr_args; i++) { | |
10659 | if (copy_from_user(®, &arg[i], sizeof(reg))) { | |
10660 | ret = -EFAULT; | |
10661 | break; | |
10662 | } | |
10663 | if (reg.offset >= IO_RINGFD_REG_MAX) { | |
10664 | ret = -EINVAL; | |
10665 | break; | |
10666 | } | |
10667 | ||
10668 | reg.offset = array_index_nospec(reg.offset, IO_RINGFD_REG_MAX); | |
10669 | if (tctx->registered_rings[reg.offset]) { | |
10670 | fput(tctx->registered_rings[reg.offset]); | |
10671 | tctx->registered_rings[reg.offset] = NULL; | |
10672 | } | |
10673 | } | |
10674 | ||
10675 | return i ? i : ret; | |
10676 | } | |
10677 | ||
6c5c240e RP |
10678 | static void *io_uring_validate_mmap_request(struct file *file, |
10679 | loff_t pgoff, size_t sz) | |
2b188cc1 | 10680 | { |
2b188cc1 | 10681 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 10682 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
10683 | struct page *page; |
10684 | void *ptr; | |
10685 | ||
10686 | switch (offset) { | |
10687 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
10688 | case IORING_OFF_CQ_RING: |
10689 | ptr = ctx->rings; | |
2b188cc1 JA |
10690 | break; |
10691 | case IORING_OFF_SQES: | |
10692 | ptr = ctx->sq_sqes; | |
10693 | break; | |
2b188cc1 | 10694 | default: |
6c5c240e | 10695 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
10696 | } |
10697 | ||
10698 | page = virt_to_head_page(ptr); | |
a50b854e | 10699 | if (sz > page_size(page)) |
6c5c240e RP |
10700 | return ERR_PTR(-EINVAL); |
10701 | ||
10702 | return ptr; | |
10703 | } | |
10704 | ||
10705 | #ifdef CONFIG_MMU | |
10706 | ||
c072481d | 10707 | static __cold int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
6c5c240e RP |
10708 | { |
10709 | size_t sz = vma->vm_end - vma->vm_start; | |
10710 | unsigned long pfn; | |
10711 | void *ptr; | |
10712 | ||
10713 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
10714 | if (IS_ERR(ptr)) | |
10715 | return PTR_ERR(ptr); | |
2b188cc1 JA |
10716 | |
10717 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
10718 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
10719 | } | |
10720 | ||
6c5c240e RP |
10721 | #else /* !CONFIG_MMU */ |
10722 | ||
10723 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
10724 | { | |
10725 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
10726 | } | |
10727 | ||
10728 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
10729 | { | |
10730 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
10731 | } | |
10732 | ||
10733 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
10734 | unsigned long addr, unsigned long len, | |
10735 | unsigned long pgoff, unsigned long flags) | |
10736 | { | |
10737 | void *ptr; | |
10738 | ||
10739 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
10740 | if (IS_ERR(ptr)) | |
10741 | return PTR_ERR(ptr); | |
10742 | ||
10743 | return (unsigned long) ptr; | |
10744 | } | |
10745 | ||
10746 | #endif /* !CONFIG_MMU */ | |
10747 | ||
d9d05217 | 10748 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
90554200 JA |
10749 | { |
10750 | DEFINE_WAIT(wait); | |
10751 | ||
10752 | do { | |
10753 | if (!io_sqring_full(ctx)) | |
10754 | break; | |
90554200 JA |
10755 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
10756 | ||
10757 | if (!io_sqring_full(ctx)) | |
10758 | break; | |
90554200 JA |
10759 | schedule(); |
10760 | } while (!signal_pending(current)); | |
10761 | ||
10762 | finish_wait(&ctx->sqo_sq_wait, &wait); | |
5199328a | 10763 | return 0; |
90554200 JA |
10764 | } |
10765 | ||
c73ebb68 HX |
10766 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
10767 | struct __kernel_timespec __user **ts, | |
10768 | const sigset_t __user **sig) | |
10769 | { | |
10770 | struct io_uring_getevents_arg arg; | |
10771 | ||
10772 | /* | |
10773 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer | |
10774 | * is just a pointer to the sigset_t. | |
10775 | */ | |
10776 | if (!(flags & IORING_ENTER_EXT_ARG)) { | |
10777 | *sig = (const sigset_t __user *) argp; | |
10778 | *ts = NULL; | |
10779 | return 0; | |
10780 | } | |
10781 | ||
10782 | /* | |
10783 | * EXT_ARG is set - ensure we agree on the size of it and copy in our | |
10784 | * timespec and sigset_t pointers if good. | |
10785 | */ | |
10786 | if (*argsz != sizeof(arg)) | |
10787 | return -EINVAL; | |
10788 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
10789 | return -EFAULT; | |
10790 | *sig = u64_to_user_ptr(arg.sigmask); | |
10791 | *argsz = arg.sigmask_sz; | |
10792 | *ts = u64_to_user_ptr(arg.ts); | |
10793 | return 0; | |
10794 | } | |
10795 | ||
2b188cc1 | 10796 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
c73ebb68 HX |
10797 | u32, min_complete, u32, flags, const void __user *, argp, |
10798 | size_t, argsz) | |
2b188cc1 JA |
10799 | { |
10800 | struct io_ring_ctx *ctx; | |
2b188cc1 JA |
10801 | int submitted = 0; |
10802 | struct fd f; | |
33f993da | 10803 | long ret; |
2b188cc1 | 10804 | |
4c6e277c | 10805 | io_run_task_work(); |
b41e9852 | 10806 | |
33f993da | 10807 | if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
e7a6c00d JA |
10808 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG | |
10809 | IORING_ENTER_REGISTERED_RING))) | |
2b188cc1 JA |
10810 | return -EINVAL; |
10811 | ||
e7a6c00d JA |
10812 | /* |
10813 | * Ring fd has been registered via IORING_REGISTER_RING_FDS, we | |
10814 | * need only dereference our task private array to find it. | |
10815 | */ | |
10816 | if (flags & IORING_ENTER_REGISTERED_RING) { | |
10817 | struct io_uring_task *tctx = current->io_uring; | |
10818 | ||
10819 | if (!tctx || fd >= IO_RINGFD_REG_MAX) | |
10820 | return -EINVAL; | |
10821 | fd = array_index_nospec(fd, IO_RINGFD_REG_MAX); | |
10822 | f.file = tctx->registered_rings[fd]; | |
10823 | if (unlikely(!f.file)) | |
10824 | return -EBADF; | |
10825 | } else { | |
10826 | f = fdget(fd); | |
10827 | if (unlikely(!f.file)) | |
10828 | return -EBADF; | |
10829 | } | |
2b188cc1 JA |
10830 | |
10831 | ret = -EOPNOTSUPP; | |
33f993da | 10832 | if (unlikely(f.file->f_op != &io_uring_fops)) |
2b188cc1 JA |
10833 | goto out_fput; |
10834 | ||
10835 | ret = -ENXIO; | |
10836 | ctx = f.file->private_data; | |
33f993da | 10837 | if (unlikely(!percpu_ref_tryget(&ctx->refs))) |
2b188cc1 JA |
10838 | goto out_fput; |
10839 | ||
7e84e1c7 | 10840 | ret = -EBADFD; |
33f993da | 10841 | if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED)) |
7e84e1c7 SG |
10842 | goto out; |
10843 | ||
6c271ce2 JA |
10844 | /* |
10845 | * For SQ polling, the thread will do all submissions and completions. | |
10846 | * Just return the requested submit count, and wake the thread if | |
10847 | * we were asked to. | |
10848 | */ | |
b2a9eada | 10849 | ret = 0; |
6c271ce2 | 10850 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
90f67366 | 10851 | io_cqring_overflow_flush(ctx); |
89448c47 | 10852 | |
21f96522 JA |
10853 | if (unlikely(ctx->sq_data->thread == NULL)) { |
10854 | ret = -EOWNERDEAD; | |
04147488 | 10855 | goto out; |
21f96522 | 10856 | } |
6c271ce2 | 10857 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 10858 | wake_up(&ctx->sq_data->wait); |
d9d05217 PB |
10859 | if (flags & IORING_ENTER_SQ_WAIT) { |
10860 | ret = io_sqpoll_wait_sq(ctx); | |
10861 | if (ret) | |
10862 | goto out; | |
10863 | } | |
6c271ce2 | 10864 | submitted = to_submit; |
b2a9eada | 10865 | } else if (to_submit) { |
eef51daa | 10866 | ret = io_uring_add_tctx_node(ctx); |
0f212204 JA |
10867 | if (unlikely(ret)) |
10868 | goto out; | |
2b188cc1 | 10869 | mutex_lock(&ctx->uring_lock); |
0f212204 | 10870 | submitted = io_submit_sqes(ctx, to_submit); |
2b188cc1 | 10871 | mutex_unlock(&ctx->uring_lock); |
7c504e65 PB |
10872 | |
10873 | if (submitted != to_submit) | |
10874 | goto out; | |
2b188cc1 JA |
10875 | } |
10876 | if (flags & IORING_ENTER_GETEVENTS) { | |
c73ebb68 HX |
10877 | const sigset_t __user *sig; |
10878 | struct __kernel_timespec __user *ts; | |
10879 | ||
10880 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); | |
10881 | if (unlikely(ret)) | |
10882 | goto out; | |
10883 | ||
2b188cc1 JA |
10884 | min_complete = min(min_complete, ctx->cq_entries); |
10885 | ||
32b2244a XW |
10886 | /* |
10887 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
10888 | * space applications don't need to do io completion events | |
10889 | * polling again, they can rely on io_sq_thread to do polling | |
10890 | * work, which can reduce cpu usage and uring_lock contention. | |
10891 | */ | |
10892 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
10893 | !(ctx->flags & IORING_SETUP_SQPOLL)) { | |
7668b92a | 10894 | ret = io_iopoll_check(ctx, min_complete); |
def596e9 | 10895 | } else { |
c73ebb68 | 10896 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
def596e9 | 10897 | } |
2b188cc1 JA |
10898 | } |
10899 | ||
7c504e65 | 10900 | out: |
6805b32e | 10901 | percpu_ref_put(&ctx->refs); |
2b188cc1 | 10902 | out_fput: |
e7a6c00d JA |
10903 | if (!(flags & IORING_ENTER_REGISTERED_RING)) |
10904 | fdput(f); | |
2b188cc1 JA |
10905 | return submitted ? submitted : ret; |
10906 | } | |
10907 | ||
bebdb65e | 10908 | #ifdef CONFIG_PROC_FS |
c072481d | 10909 | static __cold int io_uring_show_cred(struct seq_file *m, unsigned int id, |
61cf9370 | 10910 | const struct cred *cred) |
87ce955b | 10911 | { |
87ce955b JA |
10912 | struct user_namespace *uns = seq_user_ns(m); |
10913 | struct group_info *gi; | |
10914 | kernel_cap_t cap; | |
10915 | unsigned __capi; | |
10916 | int g; | |
10917 | ||
10918 | seq_printf(m, "%5d\n", id); | |
10919 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
10920 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
10921 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
10922 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
10923 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
10924 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
10925 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
10926 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
10927 | seq_puts(m, "\n\tGroups:\t"); | |
10928 | gi = cred->group_info; | |
10929 | for (g = 0; g < gi->ngroups; g++) { | |
10930 | seq_put_decimal_ull(m, g ? " " : "", | |
10931 | from_kgid_munged(uns, gi->gid[g])); | |
10932 | } | |
10933 | seq_puts(m, "\n\tCapEff:\t"); | |
10934 | cap = cred->cap_effective; | |
10935 | CAP_FOR_EACH_U32(__capi) | |
10936 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
10937 | seq_putc(m, '\n'); | |
10938 | return 0; | |
10939 | } | |
10940 | ||
c072481d PB |
10941 | static __cold void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, |
10942 | struct seq_file *m) | |
87ce955b | 10943 | { |
dbbe9c64 | 10944 | struct io_sq_data *sq = NULL; |
83f84356 HX |
10945 | struct io_overflow_cqe *ocqe; |
10946 | struct io_rings *r = ctx->rings; | |
10947 | unsigned int sq_mask = ctx->sq_entries - 1, cq_mask = ctx->cq_entries - 1; | |
83f84356 HX |
10948 | unsigned int sq_head = READ_ONCE(r->sq.head); |
10949 | unsigned int sq_tail = READ_ONCE(r->sq.tail); | |
10950 | unsigned int cq_head = READ_ONCE(r->cq.head); | |
10951 | unsigned int cq_tail = READ_ONCE(r->cq.tail); | |
f75d1183 | 10952 | unsigned int sq_entries, cq_entries; |
fad8e0de | 10953 | bool has_lock; |
83f84356 HX |
10954 | unsigned int i; |
10955 | ||
10956 | /* | |
10957 | * we may get imprecise sqe and cqe info if uring is actively running | |
10958 | * since we get cached_sq_head and cached_cq_tail without uring_lock | |
10959 | * and sq_tail and cq_head are changed by userspace. But it's ok since | |
10960 | * we usually use these info when it is stuck. | |
10961 | */ | |
c0235652 | 10962 | seq_printf(m, "SqMask:\t0x%x\n", sq_mask); |
f75d1183 JA |
10963 | seq_printf(m, "SqHead:\t%u\n", sq_head); |
10964 | seq_printf(m, "SqTail:\t%u\n", sq_tail); | |
10965 | seq_printf(m, "CachedSqHead:\t%u\n", ctx->cached_sq_head); | |
10966 | seq_printf(m, "CqMask:\t0x%x\n", cq_mask); | |
10967 | seq_printf(m, "CqHead:\t%u\n", cq_head); | |
10968 | seq_printf(m, "CqTail:\t%u\n", cq_tail); | |
10969 | seq_printf(m, "CachedCqTail:\t%u\n", ctx->cached_cq_tail); | |
10970 | seq_printf(m, "SQEs:\t%u\n", sq_tail - ctx->cached_sq_head); | |
10971 | sq_entries = min(sq_tail - sq_head, ctx->sq_entries); | |
10972 | for (i = 0; i < sq_entries; i++) { | |
10973 | unsigned int entry = i + sq_head; | |
10974 | unsigned int sq_idx = READ_ONCE(ctx->sq_array[entry & sq_mask]); | |
a1957780 | 10975 | struct io_uring_sqe *sqe; |
f75d1183 JA |
10976 | |
10977 | if (sq_idx > sq_mask) | |
10978 | continue; | |
10979 | sqe = &ctx->sq_sqes[sq_idx]; | |
10980 | seq_printf(m, "%5u: opcode:%d, fd:%d, flags:%x, user_data:%llu\n", | |
10981 | sq_idx, sqe->opcode, sqe->fd, sqe->flags, | |
10982 | sqe->user_data); | |
83f84356 | 10983 | } |
f75d1183 JA |
10984 | seq_printf(m, "CQEs:\t%u\n", cq_tail - cq_head); |
10985 | cq_entries = min(cq_tail - cq_head, ctx->cq_entries); | |
10986 | for (i = 0; i < cq_entries; i++) { | |
10987 | unsigned int entry = i + cq_head; | |
10988 | struct io_uring_cqe *cqe = &r->cqes[entry & cq_mask]; | |
83f84356 HX |
10989 | |
10990 | seq_printf(m, "%5u: user_data:%llu, res:%d, flag:%x\n", | |
f75d1183 JA |
10991 | entry & cq_mask, cqe->user_data, cqe->res, |
10992 | cqe->flags); | |
83f84356 | 10993 | } |
87ce955b | 10994 | |
fad8e0de JA |
10995 | /* |
10996 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, | |
10997 | * since fdinfo case grabs it in the opposite direction of normal use | |
10998 | * cases. If we fail to get the lock, we just don't iterate any | |
10999 | * structures that could be going away outside the io_uring mutex. | |
11000 | */ | |
11001 | has_lock = mutex_trylock(&ctx->uring_lock); | |
11002 | ||
5f3f26f9 | 11003 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) { |
dbbe9c64 | 11004 | sq = ctx->sq_data; |
5f3f26f9 JA |
11005 | if (!sq->thread) |
11006 | sq = NULL; | |
11007 | } | |
dbbe9c64 JQ |
11008 | |
11009 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); | |
11010 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); | |
87ce955b | 11011 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
fad8e0de | 11012 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
7b29f92d | 11013 | struct file *f = io_file_from_index(ctx, i); |
87ce955b | 11014 | |
87ce955b JA |
11015 | if (f) |
11016 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
11017 | else | |
11018 | seq_printf(m, "%5u: <none>\n", i); | |
11019 | } | |
11020 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
fad8e0de | 11021 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
41edf1a5 | 11022 | struct io_mapped_ubuf *buf = ctx->user_bufs[i]; |
4751f53d | 11023 | unsigned int len = buf->ubuf_end - buf->ubuf; |
87ce955b | 11024 | |
4751f53d | 11025 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len); |
87ce955b | 11026 | } |
61cf9370 MWO |
11027 | if (has_lock && !xa_empty(&ctx->personalities)) { |
11028 | unsigned long index; | |
11029 | const struct cred *cred; | |
11030 | ||
87ce955b | 11031 | seq_printf(m, "Personalities:\n"); |
61cf9370 MWO |
11032 | xa_for_each(&ctx->personalities, index, cred) |
11033 | io_uring_show_cred(m, index, cred); | |
87ce955b | 11034 | } |
83f84356 HX |
11035 | if (has_lock) |
11036 | mutex_unlock(&ctx->uring_lock); | |
11037 | ||
11038 | seq_puts(m, "PollList:\n"); | |
79ebeaee | 11039 | spin_lock(&ctx->completion_lock); |
d7718a9d JA |
11040 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
11041 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
11042 | struct io_kiocb *req; | |
11043 | ||
11044 | hlist_for_each_entry(req, list, hash_node) | |
11045 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
11046 | req->task->task_works != NULL); | |
11047 | } | |
83f84356 HX |
11048 | |
11049 | seq_puts(m, "CqOverflowList:\n"); | |
11050 | list_for_each_entry(ocqe, &ctx->cq_overflow_list, list) { | |
11051 | struct io_uring_cqe *cqe = &ocqe->cqe; | |
11052 | ||
11053 | seq_printf(m, " user_data=%llu, res=%d, flags=%x\n", | |
11054 | cqe->user_data, cqe->res, cqe->flags); | |
11055 | ||
11056 | } | |
11057 | ||
79ebeaee | 11058 | spin_unlock(&ctx->completion_lock); |
87ce955b JA |
11059 | } |
11060 | ||
c072481d | 11061 | static __cold void io_uring_show_fdinfo(struct seq_file *m, struct file *f) |
87ce955b JA |
11062 | { |
11063 | struct io_ring_ctx *ctx = f->private_data; | |
11064 | ||
11065 | if (percpu_ref_tryget(&ctx->refs)) { | |
11066 | __io_uring_show_fdinfo(ctx, m); | |
11067 | percpu_ref_put(&ctx->refs); | |
11068 | } | |
11069 | } | |
bebdb65e | 11070 | #endif |
87ce955b | 11071 | |
2b188cc1 JA |
11072 | static const struct file_operations io_uring_fops = { |
11073 | .release = io_uring_release, | |
11074 | .mmap = io_uring_mmap, | |
6c5c240e RP |
11075 | #ifndef CONFIG_MMU |
11076 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
11077 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
11078 | #endif | |
2b188cc1 | 11079 | .poll = io_uring_poll, |
bebdb65e | 11080 | #ifdef CONFIG_PROC_FS |
87ce955b | 11081 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 11082 | #endif |
2b188cc1 JA |
11083 | }; |
11084 | ||
c072481d PB |
11085 | static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
11086 | struct io_uring_params *p) | |
2b188cc1 | 11087 | { |
75b28aff HV |
11088 | struct io_rings *rings; |
11089 | size_t size, sq_array_offset; | |
2b188cc1 | 11090 | |
bd740481 JA |
11091 | /* make sure these are sane, as we already accounted them */ |
11092 | ctx->sq_entries = p->sq_entries; | |
11093 | ctx->cq_entries = p->cq_entries; | |
11094 | ||
75b28aff HV |
11095 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
11096 | if (size == SIZE_MAX) | |
11097 | return -EOVERFLOW; | |
11098 | ||
11099 | rings = io_mem_alloc(size); | |
11100 | if (!rings) | |
2b188cc1 JA |
11101 | return -ENOMEM; |
11102 | ||
75b28aff HV |
11103 | ctx->rings = rings; |
11104 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
11105 | rings->sq_ring_mask = p->sq_entries - 1; | |
11106 | rings->cq_ring_mask = p->cq_entries - 1; | |
11107 | rings->sq_ring_entries = p->sq_entries; | |
11108 | rings->cq_ring_entries = p->cq_entries; | |
2b188cc1 JA |
11109 | |
11110 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
11111 | if (size == SIZE_MAX) { |
11112 | io_mem_free(ctx->rings); | |
11113 | ctx->rings = NULL; | |
2b188cc1 | 11114 | return -EOVERFLOW; |
eb065d30 | 11115 | } |
2b188cc1 JA |
11116 | |
11117 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
11118 | if (!ctx->sq_sqes) { |
11119 | io_mem_free(ctx->rings); | |
11120 | ctx->rings = NULL; | |
2b188cc1 | 11121 | return -ENOMEM; |
eb065d30 | 11122 | } |
2b188cc1 | 11123 | |
2b188cc1 JA |
11124 | return 0; |
11125 | } | |
11126 | ||
9faadcc8 PB |
11127 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
11128 | { | |
11129 | int ret, fd; | |
11130 | ||
11131 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
11132 | if (fd < 0) | |
11133 | return fd; | |
11134 | ||
eef51daa | 11135 | ret = io_uring_add_tctx_node(ctx); |
9faadcc8 PB |
11136 | if (ret) { |
11137 | put_unused_fd(fd); | |
11138 | return ret; | |
11139 | } | |
11140 | fd_install(fd, file); | |
11141 | return fd; | |
11142 | } | |
11143 | ||
2b188cc1 JA |
11144 | /* |
11145 | * Allocate an anonymous fd, this is what constitutes the application | |
11146 | * visible backing of an io_uring instance. The application mmaps this | |
11147 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
11148 | * we have to tie this fd to a socket for file garbage collection purposes. | |
11149 | */ | |
9faadcc8 | 11150 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
2b188cc1 JA |
11151 | { |
11152 | struct file *file; | |
9faadcc8 | 11153 | #if defined(CONFIG_UNIX) |
2b188cc1 JA |
11154 | int ret; |
11155 | ||
2b188cc1 JA |
11156 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
11157 | &ctx->ring_sock); | |
11158 | if (ret) | |
9faadcc8 | 11159 | return ERR_PTR(ret); |
2b188cc1 JA |
11160 | #endif |
11161 | ||
91a9ab7c PM |
11162 | file = anon_inode_getfile_secure("[io_uring]", &io_uring_fops, ctx, |
11163 | O_RDWR | O_CLOEXEC, NULL); | |
2b188cc1 | 11164 | #if defined(CONFIG_UNIX) |
9faadcc8 PB |
11165 | if (IS_ERR(file)) { |
11166 | sock_release(ctx->ring_sock); | |
11167 | ctx->ring_sock = NULL; | |
11168 | } else { | |
11169 | ctx->ring_sock->file = file; | |
0f212204 | 11170 | } |
2b188cc1 | 11171 | #endif |
9faadcc8 | 11172 | return file; |
2b188cc1 JA |
11173 | } |
11174 | ||
c072481d PB |
11175 | static __cold int io_uring_create(unsigned entries, struct io_uring_params *p, |
11176 | struct io_uring_params __user *params) | |
2b188cc1 | 11177 | { |
2b188cc1 | 11178 | struct io_ring_ctx *ctx; |
9faadcc8 | 11179 | struct file *file; |
2b188cc1 JA |
11180 | int ret; |
11181 | ||
8110c1a6 | 11182 | if (!entries) |
2b188cc1 | 11183 | return -EINVAL; |
8110c1a6 JA |
11184 | if (entries > IORING_MAX_ENTRIES) { |
11185 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
11186 | return -EINVAL; | |
11187 | entries = IORING_MAX_ENTRIES; | |
11188 | } | |
2b188cc1 JA |
11189 | |
11190 | /* | |
11191 | * Use twice as many entries for the CQ ring. It's possible for the | |
11192 | * application to drive a higher depth than the size of the SQ ring, | |
11193 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
11194 | * some flexibility in overcommitting a bit. If the application has |
11195 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
11196 | * of CQ ring entries manually. | |
2b188cc1 JA |
11197 | */ |
11198 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
11199 | if (p->flags & IORING_SETUP_CQSIZE) { |
11200 | /* | |
11201 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
11202 | * to a power-of-two, if it isn't already. We do NOT impose | |
11203 | * any cq vs sq ring sizing. | |
11204 | */ | |
eb2667b3 | 11205 | if (!p->cq_entries) |
33a107f0 | 11206 | return -EINVAL; |
8110c1a6 JA |
11207 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
11208 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
11209 | return -EINVAL; | |
11210 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
11211 | } | |
eb2667b3 JQ |
11212 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
11213 | if (p->cq_entries < p->sq_entries) | |
11214 | return -EINVAL; | |
33a107f0 JA |
11215 | } else { |
11216 | p->cq_entries = 2 * p->sq_entries; | |
11217 | } | |
2b188cc1 | 11218 | |
2b188cc1 | 11219 | ctx = io_ring_ctx_alloc(p); |
62e398be | 11220 | if (!ctx) |
2b188cc1 | 11221 | return -ENOMEM; |
2b188cc1 | 11222 | ctx->compat = in_compat_syscall(); |
62e398be JA |
11223 | if (!capable(CAP_IPC_LOCK)) |
11224 | ctx->user = get_uid(current_user()); | |
2aede0e4 JA |
11225 | |
11226 | /* | |
11227 | * This is just grabbed for accounting purposes. When a process exits, | |
11228 | * the mm is exited and dropped before the files, hence we need to hang | |
11229 | * on to this mm purely for the purposes of being able to unaccount | |
11230 | * memory (locked/pinned vm). It's not used for anything else. | |
11231 | */ | |
6b7898eb | 11232 | mmgrab(current->mm); |
2aede0e4 | 11233 | ctx->mm_account = current->mm; |
6b7898eb | 11234 | |
2b188cc1 JA |
11235 | ret = io_allocate_scq_urings(ctx, p); |
11236 | if (ret) | |
11237 | goto err; | |
11238 | ||
7e84e1c7 | 11239 | ret = io_sq_offload_create(ctx, p); |
2b188cc1 JA |
11240 | if (ret) |
11241 | goto err; | |
eae071c9 | 11242 | /* always set a rsrc node */ |
47b228ce PB |
11243 | ret = io_rsrc_node_switch_start(ctx); |
11244 | if (ret) | |
11245 | goto err; | |
eae071c9 | 11246 | io_rsrc_node_switch(ctx, NULL); |
2b188cc1 | 11247 | |
2b188cc1 | 11248 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
11249 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
11250 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
11251 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
11252 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
11253 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
11254 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
11255 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
11256 | |
11257 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
11258 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
11259 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
11260 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
11261 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
11262 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
11263 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 11264 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 11265 | |
7f13657d XW |
11266 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
11267 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 | 11268 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
c73ebb68 | 11269 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
9690557e | 11270 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS | |
04c76b41 | 11271 | IORING_FEAT_RSRC_TAGS | IORING_FEAT_CQE_SKIP; |
7f13657d XW |
11272 | |
11273 | if (copy_to_user(params, p, sizeof(*p))) { | |
11274 | ret = -EFAULT; | |
11275 | goto err; | |
11276 | } | |
d1719f70 | 11277 | |
9faadcc8 PB |
11278 | file = io_uring_get_file(ctx); |
11279 | if (IS_ERR(file)) { | |
11280 | ret = PTR_ERR(file); | |
11281 | goto err; | |
11282 | } | |
11283 | ||
044c1ab3 JA |
11284 | /* |
11285 | * Install ring fd as the very last thing, so we don't risk someone | |
11286 | * having closed it before we finish setup | |
11287 | */ | |
9faadcc8 PB |
11288 | ret = io_uring_install_fd(ctx, file); |
11289 | if (ret < 0) { | |
11290 | /* fput will clean it up */ | |
11291 | fput(file); | |
11292 | return ret; | |
11293 | } | |
044c1ab3 | 11294 | |
c826bd7a | 11295 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
11296 | return ret; |
11297 | err: | |
11298 | io_ring_ctx_wait_and_kill(ctx); | |
11299 | return ret; | |
11300 | } | |
11301 | ||
11302 | /* | |
11303 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
11304 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
11305 | * params structure passed in. | |
11306 | */ | |
11307 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
11308 | { | |
11309 | struct io_uring_params p; | |
2b188cc1 JA |
11310 | int i; |
11311 | ||
11312 | if (copy_from_user(&p, params, sizeof(p))) | |
11313 | return -EFAULT; | |
11314 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
11315 | if (p.resv[i]) | |
11316 | return -EINVAL; | |
11317 | } | |
11318 | ||
6c271ce2 | 11319 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 11320 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 | 11321 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
bcbb7bf6 | 11322 | IORING_SETUP_R_DISABLED | IORING_SETUP_SUBMIT_ALL)) |
2b188cc1 JA |
11323 | return -EINVAL; |
11324 | ||
7f13657d | 11325 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
11326 | } |
11327 | ||
11328 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
11329 | struct io_uring_params __user *, params) | |
11330 | { | |
11331 | return io_uring_setup(entries, params); | |
11332 | } | |
11333 | ||
c072481d PB |
11334 | static __cold int io_probe(struct io_ring_ctx *ctx, void __user *arg, |
11335 | unsigned nr_args) | |
66f4af93 JA |
11336 | { |
11337 | struct io_uring_probe *p; | |
11338 | size_t size; | |
11339 | int i, ret; | |
11340 | ||
11341 | size = struct_size(p, ops, nr_args); | |
11342 | if (size == SIZE_MAX) | |
11343 | return -EOVERFLOW; | |
11344 | p = kzalloc(size, GFP_KERNEL); | |
11345 | if (!p) | |
11346 | return -ENOMEM; | |
11347 | ||
11348 | ret = -EFAULT; | |
11349 | if (copy_from_user(p, arg, size)) | |
11350 | goto out; | |
11351 | ret = -EINVAL; | |
11352 | if (memchr_inv(p, 0, size)) | |
11353 | goto out; | |
11354 | ||
11355 | p->last_op = IORING_OP_LAST - 1; | |
11356 | if (nr_args > IORING_OP_LAST) | |
11357 | nr_args = IORING_OP_LAST; | |
11358 | ||
11359 | for (i = 0; i < nr_args; i++) { | |
11360 | p->ops[i].op = i; | |
11361 | if (!io_op_defs[i].not_supported) | |
11362 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
11363 | } | |
11364 | p->ops_len = i; | |
11365 | ||
11366 | ret = 0; | |
11367 | if (copy_to_user(arg, p, size)) | |
11368 | ret = -EFAULT; | |
11369 | out: | |
11370 | kfree(p); | |
11371 | return ret; | |
11372 | } | |
11373 | ||
071698e1 JA |
11374 | static int io_register_personality(struct io_ring_ctx *ctx) |
11375 | { | |
4379bf8b | 11376 | const struct cred *creds; |
61cf9370 | 11377 | u32 id; |
1e6fa521 | 11378 | int ret; |
071698e1 | 11379 | |
4379bf8b | 11380 | creds = get_current_cred(); |
1e6fa521 | 11381 | |
61cf9370 MWO |
11382 | ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds, |
11383 | XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL); | |
a30f895a JA |
11384 | if (ret < 0) { |
11385 | put_cred(creds); | |
11386 | return ret; | |
11387 | } | |
11388 | return id; | |
071698e1 JA |
11389 | } |
11390 | ||
c072481d PB |
11391 | static __cold int io_register_restrictions(struct io_ring_ctx *ctx, |
11392 | void __user *arg, unsigned int nr_args) | |
21b55dbc SG |
11393 | { |
11394 | struct io_uring_restriction *res; | |
11395 | size_t size; | |
11396 | int i, ret; | |
11397 | ||
7e84e1c7 SG |
11398 | /* Restrictions allowed only if rings started disabled */ |
11399 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
11400 | return -EBADFD; | |
11401 | ||
21b55dbc | 11402 | /* We allow only a single restrictions registration */ |
7e84e1c7 | 11403 | if (ctx->restrictions.registered) |
21b55dbc SG |
11404 | return -EBUSY; |
11405 | ||
11406 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) | |
11407 | return -EINVAL; | |
11408 | ||
11409 | size = array_size(nr_args, sizeof(*res)); | |
11410 | if (size == SIZE_MAX) | |
11411 | return -EOVERFLOW; | |
11412 | ||
11413 | res = memdup_user(arg, size); | |
11414 | if (IS_ERR(res)) | |
11415 | return PTR_ERR(res); | |
11416 | ||
11417 | ret = 0; | |
11418 | ||
11419 | for (i = 0; i < nr_args; i++) { | |
11420 | switch (res[i].opcode) { | |
11421 | case IORING_RESTRICTION_REGISTER_OP: | |
11422 | if (res[i].register_op >= IORING_REGISTER_LAST) { | |
11423 | ret = -EINVAL; | |
11424 | goto out; | |
11425 | } | |
11426 | ||
11427 | __set_bit(res[i].register_op, | |
11428 | ctx->restrictions.register_op); | |
11429 | break; | |
11430 | case IORING_RESTRICTION_SQE_OP: | |
11431 | if (res[i].sqe_op >= IORING_OP_LAST) { | |
11432 | ret = -EINVAL; | |
11433 | goto out; | |
11434 | } | |
11435 | ||
11436 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); | |
11437 | break; | |
11438 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: | |
11439 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; | |
11440 | break; | |
11441 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: | |
11442 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; | |
11443 | break; | |
11444 | default: | |
11445 | ret = -EINVAL; | |
11446 | goto out; | |
11447 | } | |
11448 | } | |
11449 | ||
11450 | out: | |
11451 | /* Reset all restrictions if an error happened */ | |
11452 | if (ret != 0) | |
11453 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); | |
11454 | else | |
7e84e1c7 | 11455 | ctx->restrictions.registered = true; |
21b55dbc SG |
11456 | |
11457 | kfree(res); | |
11458 | return ret; | |
11459 | } | |
11460 | ||
7e84e1c7 SG |
11461 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
11462 | { | |
11463 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
11464 | return -EBADFD; | |
11465 | ||
11466 | if (ctx->restrictions.registered) | |
11467 | ctx->restricted = 1; | |
11468 | ||
0298ef96 PB |
11469 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
11470 | if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait)) | |
11471 | wake_up(&ctx->sq_data->wait); | |
7e84e1c7 SG |
11472 | return 0; |
11473 | } | |
11474 | ||
fdecb662 | 11475 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 11476 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 PB |
11477 | unsigned nr_args) |
11478 | { | |
11479 | __u32 tmp; | |
11480 | int err; | |
11481 | ||
c3bdad02 PB |
11482 | if (up->resv) |
11483 | return -EINVAL; | |
98f0b3b4 PB |
11484 | if (check_add_overflow(up->offset, nr_args, &tmp)) |
11485 | return -EOVERFLOW; | |
11486 | err = io_rsrc_node_switch_start(ctx); | |
11487 | if (err) | |
11488 | return err; | |
11489 | ||
fdecb662 PB |
11490 | switch (type) { |
11491 | case IORING_RSRC_FILE: | |
98f0b3b4 | 11492 | return __io_sqe_files_update(ctx, up, nr_args); |
634d00df PB |
11493 | case IORING_RSRC_BUFFER: |
11494 | return __io_sqe_buffers_update(ctx, up, nr_args); | |
98f0b3b4 PB |
11495 | } |
11496 | return -EINVAL; | |
11497 | } | |
11498 | ||
c3bdad02 PB |
11499 | static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg, |
11500 | unsigned nr_args) | |
98f0b3b4 | 11501 | { |
c3bdad02 | 11502 | struct io_uring_rsrc_update2 up; |
98f0b3b4 PB |
11503 | |
11504 | if (!nr_args) | |
11505 | return -EINVAL; | |
c3bdad02 PB |
11506 | memset(&up, 0, sizeof(up)); |
11507 | if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update))) | |
11508 | return -EFAULT; | |
11509 | return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args); | |
11510 | } | |
11511 | ||
11512 | static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg, | |
992da01a | 11513 | unsigned size, unsigned type) |
c3bdad02 PB |
11514 | { |
11515 | struct io_uring_rsrc_update2 up; | |
11516 | ||
11517 | if (size != sizeof(up)) | |
11518 | return -EINVAL; | |
98f0b3b4 PB |
11519 | if (copy_from_user(&up, arg, sizeof(up))) |
11520 | return -EFAULT; | |
992da01a | 11521 | if (!up.nr || up.resv) |
98f0b3b4 | 11522 | return -EINVAL; |
992da01a | 11523 | return __io_register_rsrc_update(ctx, type, &up, up.nr); |
98f0b3b4 PB |
11524 | } |
11525 | ||
c072481d | 11526 | static __cold int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg, |
992da01a | 11527 | unsigned int size, unsigned int type) |
792e3582 PB |
11528 | { |
11529 | struct io_uring_rsrc_register rr; | |
11530 | ||
11531 | /* keep it extendible */ | |
11532 | if (size != sizeof(rr)) | |
11533 | return -EINVAL; | |
11534 | ||
11535 | memset(&rr, 0, sizeof(rr)); | |
11536 | if (copy_from_user(&rr, arg, size)) | |
11537 | return -EFAULT; | |
992da01a | 11538 | if (!rr.nr || rr.resv || rr.resv2) |
792e3582 PB |
11539 | return -EINVAL; |
11540 | ||
992da01a | 11541 | switch (type) { |
792e3582 PB |
11542 | case IORING_RSRC_FILE: |
11543 | return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data), | |
11544 | rr.nr, u64_to_user_ptr(rr.tags)); | |
634d00df PB |
11545 | case IORING_RSRC_BUFFER: |
11546 | return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data), | |
11547 | rr.nr, u64_to_user_ptr(rr.tags)); | |
792e3582 PB |
11548 | } |
11549 | return -EINVAL; | |
11550 | } | |
11551 | ||
c072481d PB |
11552 | static __cold int io_register_iowq_aff(struct io_ring_ctx *ctx, |
11553 | void __user *arg, unsigned len) | |
fe76421d JA |
11554 | { |
11555 | struct io_uring_task *tctx = current->io_uring; | |
11556 | cpumask_var_t new_mask; | |
11557 | int ret; | |
11558 | ||
11559 | if (!tctx || !tctx->io_wq) | |
11560 | return -EINVAL; | |
11561 | ||
11562 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) | |
11563 | return -ENOMEM; | |
11564 | ||
11565 | cpumask_clear(new_mask); | |
11566 | if (len > cpumask_size()) | |
11567 | len = cpumask_size(); | |
11568 | ||
11569 | if (copy_from_user(new_mask, arg, len)) { | |
11570 | free_cpumask_var(new_mask); | |
11571 | return -EFAULT; | |
11572 | } | |
11573 | ||
11574 | ret = io_wq_cpu_affinity(tctx->io_wq, new_mask); | |
11575 | free_cpumask_var(new_mask); | |
11576 | return ret; | |
11577 | } | |
11578 | ||
c072481d | 11579 | static __cold int io_unregister_iowq_aff(struct io_ring_ctx *ctx) |
fe76421d JA |
11580 | { |
11581 | struct io_uring_task *tctx = current->io_uring; | |
11582 | ||
11583 | if (!tctx || !tctx->io_wq) | |
11584 | return -EINVAL; | |
11585 | ||
11586 | return io_wq_cpu_affinity(tctx->io_wq, NULL); | |
11587 | } | |
11588 | ||
c072481d PB |
11589 | static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx, |
11590 | void __user *arg) | |
b22fa62a | 11591 | __must_hold(&ctx->uring_lock) |
2e480058 | 11592 | { |
b22fa62a | 11593 | struct io_tctx_node *node; |
fa84693b JA |
11594 | struct io_uring_task *tctx = NULL; |
11595 | struct io_sq_data *sqd = NULL; | |
2e480058 JA |
11596 | __u32 new_count[2]; |
11597 | int i, ret; | |
11598 | ||
2e480058 JA |
11599 | if (copy_from_user(new_count, arg, sizeof(new_count))) |
11600 | return -EFAULT; | |
11601 | for (i = 0; i < ARRAY_SIZE(new_count); i++) | |
11602 | if (new_count[i] > INT_MAX) | |
11603 | return -EINVAL; | |
11604 | ||
fa84693b JA |
11605 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
11606 | sqd = ctx->sq_data; | |
11607 | if (sqd) { | |
009ad9f0 JA |
11608 | /* |
11609 | * Observe the correct sqd->lock -> ctx->uring_lock | |
11610 | * ordering. Fine to drop uring_lock here, we hold | |
11611 | * a ref to the ctx. | |
11612 | */ | |
41d3a6bd | 11613 | refcount_inc(&sqd->refs); |
009ad9f0 | 11614 | mutex_unlock(&ctx->uring_lock); |
fa84693b | 11615 | mutex_lock(&sqd->lock); |
009ad9f0 | 11616 | mutex_lock(&ctx->uring_lock); |
41d3a6bd JA |
11617 | if (sqd->thread) |
11618 | tctx = sqd->thread->io_uring; | |
fa84693b JA |
11619 | } |
11620 | } else { | |
11621 | tctx = current->io_uring; | |
11622 | } | |
11623 | ||
e139a1ec | 11624 | BUILD_BUG_ON(sizeof(new_count) != sizeof(ctx->iowq_limits)); |
fa84693b | 11625 | |
bad119b9 PB |
11626 | for (i = 0; i < ARRAY_SIZE(new_count); i++) |
11627 | if (new_count[i]) | |
11628 | ctx->iowq_limits[i] = new_count[i]; | |
e139a1ec PB |
11629 | ctx->iowq_limits_set = true; |
11630 | ||
e139a1ec PB |
11631 | if (tctx && tctx->io_wq) { |
11632 | ret = io_wq_max_workers(tctx->io_wq, new_count); | |
11633 | if (ret) | |
11634 | goto err; | |
11635 | } else { | |
11636 | memset(new_count, 0, sizeof(new_count)); | |
11637 | } | |
fa84693b | 11638 | |
41d3a6bd | 11639 | if (sqd) { |
fa84693b | 11640 | mutex_unlock(&sqd->lock); |
41d3a6bd JA |
11641 | io_put_sq_data(sqd); |
11642 | } | |
2e480058 JA |
11643 | |
11644 | if (copy_to_user(arg, new_count, sizeof(new_count))) | |
11645 | return -EFAULT; | |
11646 | ||
b22fa62a PB |
11647 | /* that's it for SQPOLL, only the SQPOLL task creates requests */ |
11648 | if (sqd) | |
11649 | return 0; | |
11650 | ||
11651 | /* now propagate the restriction to all registered users */ | |
11652 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
11653 | struct io_uring_task *tctx = node->task->io_uring; | |
11654 | ||
11655 | if (WARN_ON_ONCE(!tctx->io_wq)) | |
11656 | continue; | |
11657 | ||
11658 | for (i = 0; i < ARRAY_SIZE(new_count); i++) | |
11659 | new_count[i] = ctx->iowq_limits[i]; | |
11660 | /* ignore errors, it always returns zero anyway */ | |
11661 | (void)io_wq_max_workers(tctx->io_wq, new_count); | |
11662 | } | |
2e480058 | 11663 | return 0; |
fa84693b | 11664 | err: |
41d3a6bd | 11665 | if (sqd) { |
fa84693b | 11666 | mutex_unlock(&sqd->lock); |
41d3a6bd JA |
11667 | io_put_sq_data(sqd); |
11668 | } | |
fa84693b | 11669 | return ret; |
2e480058 JA |
11670 | } |
11671 | ||
edafccee JA |
11672 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
11673 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
11674 | __releases(ctx->uring_lock) |
11675 | __acquires(ctx->uring_lock) | |
edafccee JA |
11676 | { |
11677 | int ret; | |
11678 | ||
35fa71a0 JA |
11679 | /* |
11680 | * We're inside the ring mutex, if the ref is already dying, then | |
11681 | * someone else killed the ctx or is already going through | |
11682 | * io_uring_register(). | |
11683 | */ | |
11684 | if (percpu_ref_is_dying(&ctx->refs)) | |
11685 | return -ENXIO; | |
11686 | ||
75c4021a PB |
11687 | if (ctx->restricted) { |
11688 | if (opcode >= IORING_REGISTER_LAST) | |
11689 | return -EINVAL; | |
11690 | opcode = array_index_nospec(opcode, IORING_REGISTER_LAST); | |
11691 | if (!test_bit(opcode, ctx->restrictions.register_op)) | |
11692 | return -EACCES; | |
11693 | } | |
11694 | ||
edafccee JA |
11695 | switch (opcode) { |
11696 | case IORING_REGISTER_BUFFERS: | |
634d00df | 11697 | ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL); |
edafccee JA |
11698 | break; |
11699 | case IORING_UNREGISTER_BUFFERS: | |
11700 | ret = -EINVAL; | |
11701 | if (arg || nr_args) | |
11702 | break; | |
0a96bbe4 | 11703 | ret = io_sqe_buffers_unregister(ctx); |
edafccee | 11704 | break; |
6b06314c | 11705 | case IORING_REGISTER_FILES: |
792e3582 | 11706 | ret = io_sqe_files_register(ctx, arg, nr_args, NULL); |
6b06314c JA |
11707 | break; |
11708 | case IORING_UNREGISTER_FILES: | |
11709 | ret = -EINVAL; | |
11710 | if (arg || nr_args) | |
11711 | break; | |
11712 | ret = io_sqe_files_unregister(ctx); | |
11713 | break; | |
c3a31e60 | 11714 | case IORING_REGISTER_FILES_UPDATE: |
c3bdad02 | 11715 | ret = io_register_files_update(ctx, arg, nr_args); |
c3a31e60 | 11716 | break; |
9b402849 JA |
11717 | case IORING_REGISTER_EVENTFD: |
11718 | ret = -EINVAL; | |
11719 | if (nr_args != 1) | |
11720 | break; | |
c75312dd UA |
11721 | ret = io_eventfd_register(ctx, arg, 0); |
11722 | break; | |
11723 | case IORING_REGISTER_EVENTFD_ASYNC: | |
11724 | ret = -EINVAL; | |
11725 | if (nr_args != 1) | |
f2842ab5 | 11726 | break; |
c75312dd | 11727 | ret = io_eventfd_register(ctx, arg, 1); |
9b402849 JA |
11728 | break; |
11729 | case IORING_UNREGISTER_EVENTFD: | |
11730 | ret = -EINVAL; | |
11731 | if (arg || nr_args) | |
11732 | break; | |
11733 | ret = io_eventfd_unregister(ctx); | |
11734 | break; | |
66f4af93 JA |
11735 | case IORING_REGISTER_PROBE: |
11736 | ret = -EINVAL; | |
11737 | if (!arg || nr_args > 256) | |
11738 | break; | |
11739 | ret = io_probe(ctx, arg, nr_args); | |
11740 | break; | |
071698e1 JA |
11741 | case IORING_REGISTER_PERSONALITY: |
11742 | ret = -EINVAL; | |
11743 | if (arg || nr_args) | |
11744 | break; | |
11745 | ret = io_register_personality(ctx); | |
11746 | break; | |
11747 | case IORING_UNREGISTER_PERSONALITY: | |
11748 | ret = -EINVAL; | |
11749 | if (arg) | |
11750 | break; | |
11751 | ret = io_unregister_personality(ctx, nr_args); | |
11752 | break; | |
7e84e1c7 SG |
11753 | case IORING_REGISTER_ENABLE_RINGS: |
11754 | ret = -EINVAL; | |
11755 | if (arg || nr_args) | |
11756 | break; | |
11757 | ret = io_register_enable_rings(ctx); | |
11758 | break; | |
21b55dbc SG |
11759 | case IORING_REGISTER_RESTRICTIONS: |
11760 | ret = io_register_restrictions(ctx, arg, nr_args); | |
11761 | break; | |
992da01a PB |
11762 | case IORING_REGISTER_FILES2: |
11763 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE); | |
11764 | break; | |
11765 | case IORING_REGISTER_FILES_UPDATE2: | |
11766 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
11767 | IORING_RSRC_FILE); | |
11768 | break; | |
11769 | case IORING_REGISTER_BUFFERS2: | |
11770 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER); | |
792e3582 | 11771 | break; |
992da01a PB |
11772 | case IORING_REGISTER_BUFFERS_UPDATE: |
11773 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
11774 | IORING_RSRC_BUFFER); | |
c3bdad02 | 11775 | break; |
fe76421d JA |
11776 | case IORING_REGISTER_IOWQ_AFF: |
11777 | ret = -EINVAL; | |
11778 | if (!arg || !nr_args) | |
11779 | break; | |
11780 | ret = io_register_iowq_aff(ctx, arg, nr_args); | |
11781 | break; | |
11782 | case IORING_UNREGISTER_IOWQ_AFF: | |
11783 | ret = -EINVAL; | |
11784 | if (arg || nr_args) | |
11785 | break; | |
11786 | ret = io_unregister_iowq_aff(ctx); | |
11787 | break; | |
2e480058 JA |
11788 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
11789 | ret = -EINVAL; | |
11790 | if (!arg || nr_args != 2) | |
11791 | break; | |
11792 | ret = io_register_iowq_max_workers(ctx, arg); | |
11793 | break; | |
e7a6c00d JA |
11794 | case IORING_REGISTER_RING_FDS: |
11795 | ret = io_ringfd_register(ctx, arg, nr_args); | |
11796 | break; | |
11797 | case IORING_UNREGISTER_RING_FDS: | |
11798 | ret = io_ringfd_unregister(ctx, arg, nr_args); | |
11799 | break; | |
edafccee JA |
11800 | default: |
11801 | ret = -EINVAL; | |
11802 | break; | |
11803 | } | |
11804 | ||
edafccee JA |
11805 | return ret; |
11806 | } | |
11807 | ||
11808 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
11809 | void __user *, arg, unsigned int, nr_args) | |
11810 | { | |
11811 | struct io_ring_ctx *ctx; | |
11812 | long ret = -EBADF; | |
11813 | struct fd f; | |
11814 | ||
11815 | f = fdget(fd); | |
11816 | if (!f.file) | |
11817 | return -EBADF; | |
11818 | ||
11819 | ret = -EOPNOTSUPP; | |
11820 | if (f.file->f_op != &io_uring_fops) | |
11821 | goto out_fput; | |
11822 | ||
11823 | ctx = f.file->private_data; | |
11824 | ||
b6c23dd5 PB |
11825 | io_run_task_work(); |
11826 | ||
edafccee JA |
11827 | mutex_lock(&ctx->uring_lock); |
11828 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
11829 | mutex_unlock(&ctx->uring_lock); | |
2757be22 | 11830 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, ret); |
edafccee JA |
11831 | out_fput: |
11832 | fdput(f); | |
11833 | return ret; | |
11834 | } | |
11835 | ||
2b188cc1 JA |
11836 | static int __init io_uring_init(void) |
11837 | { | |
d7f62e82 SM |
11838 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
11839 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
11840 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
11841 | } while (0) | |
11842 | ||
11843 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
11844 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
11845 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
11846 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
11847 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
11848 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
11849 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
11850 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
11851 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
11852 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 11853 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
11854 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
11855 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
11856 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
11857 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
11858 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
11859 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
11860 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
11861 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
11862 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
11863 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
11864 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
11865 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
11866 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
11867 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
11868 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 11869 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
11870 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
11871 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
16340eab | 11872 | BUILD_BUG_SQE_ELEM(40, __u16, buf_group); |
d7f62e82 | 11873 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
7d67af2c | 11874 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
b9445598 | 11875 | BUILD_BUG_SQE_ELEM(44, __u32, file_index); |
d7f62e82 | 11876 | |
b0d658ec PB |
11877 | BUILD_BUG_ON(sizeof(struct io_uring_files_update) != |
11878 | sizeof(struct io_uring_rsrc_update)); | |
11879 | BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) > | |
11880 | sizeof(struct io_uring_rsrc_update2)); | |
90499ad0 PB |
11881 | |
11882 | /* ->buf_index is u16 */ | |
11883 | BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16)); | |
11884 | ||
b0d658ec PB |
11885 | /* should fit into one byte */ |
11886 | BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8)); | |
68fe256a PB |
11887 | BUILD_BUG_ON(SQE_COMMON_FLAGS >= (1 << 8)); |
11888 | BUILD_BUG_ON((SQE_VALID_FLAGS | SQE_COMMON_FLAGS) != SQE_VALID_FLAGS); | |
b0d658ec | 11889 | |
d3656344 | 11890 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
32c2d33e | 11891 | BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof(int)); |
16340eab | 11892 | |
91f245d5 JA |
11893 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
11894 | SLAB_ACCOUNT); | |
2b188cc1 JA |
11895 | return 0; |
11896 | }; | |
11897 | __initcall(io_uring_init); |