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 JA |
271 | __u16 bid; |
272 | }; | |
273 | ||
21b55dbc SG |
274 | struct io_restriction { |
275 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); | |
276 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); | |
277 | u8 sqe_flags_allowed; | |
278 | u8 sqe_flags_required; | |
7e84e1c7 | 279 | bool registered; |
21b55dbc SG |
280 | }; |
281 | ||
37d1e2e3 JA |
282 | enum { |
283 | IO_SQ_THREAD_SHOULD_STOP = 0, | |
284 | IO_SQ_THREAD_SHOULD_PARK, | |
285 | }; | |
286 | ||
534ca6d6 JA |
287 | struct io_sq_data { |
288 | refcount_t refs; | |
9e138a48 | 289 | atomic_t park_pending; |
09a6f4ef | 290 | struct mutex lock; |
69fb2131 JA |
291 | |
292 | /* ctx's that are using this sqd */ | |
293 | struct list_head ctx_list; | |
69fb2131 | 294 | |
534ca6d6 JA |
295 | struct task_struct *thread; |
296 | struct wait_queue_head wait; | |
08369246 XW |
297 | |
298 | unsigned sq_thread_idle; | |
37d1e2e3 JA |
299 | int sq_cpu; |
300 | pid_t task_pid; | |
5c2469e0 | 301 | pid_t task_tgid; |
37d1e2e3 JA |
302 | |
303 | unsigned long state; | |
37d1e2e3 | 304 | struct completion exited; |
534ca6d6 JA |
305 | }; |
306 | ||
6dd0be1e | 307 | #define IO_COMPL_BATCH 32 |
6ff119a6 | 308 | #define IO_REQ_CACHE_SIZE 32 |
bf019da7 | 309 | #define IO_REQ_ALLOC_BATCH 8 |
258b29a9 | 310 | |
a1ab7b35 PB |
311 | struct io_submit_link { |
312 | struct io_kiocb *head; | |
313 | struct io_kiocb *last; | |
314 | }; | |
315 | ||
258b29a9 | 316 | struct io_submit_state { |
5a158c6b PB |
317 | /* inline/task_work completion list, under ->uring_lock */ |
318 | struct io_wq_work_node free_list; | |
319 | /* batch completion logic */ | |
320 | struct io_wq_work_list compl_reqs; | |
a1ab7b35 | 321 | struct io_submit_link link; |
258b29a9 | 322 | |
258b29a9 | 323 | bool plug_started; |
4b628aeb | 324 | bool need_plug; |
3d4aeb9f | 325 | bool flush_cqes; |
5ca7a8b3 | 326 | unsigned short submit_nr; |
5a158c6b | 327 | struct blk_plug plug; |
258b29a9 PB |
328 | }; |
329 | ||
77bc59b4 UA |
330 | struct io_ev_fd { |
331 | struct eventfd_ctx *cq_ev_fd; | |
c75312dd | 332 | unsigned int eventfd_async: 1; |
77bc59b4 UA |
333 | struct rcu_head rcu; |
334 | }; | |
335 | ||
2b188cc1 | 336 | struct io_ring_ctx { |
b52ecf8c | 337 | /* const or read-mostly hot data */ |
2b188cc1 JA |
338 | struct { |
339 | struct percpu_ref refs; | |
2b188cc1 | 340 | |
b52ecf8c | 341 | struct io_rings *rings; |
2b188cc1 | 342 | unsigned int flags; |
e1d85334 | 343 | unsigned int compat: 1; |
e1d85334 | 344 | unsigned int drain_next: 1; |
21b55dbc | 345 | unsigned int restricted: 1; |
f18ee4cf | 346 | unsigned int off_timeout_used: 1; |
10c66904 | 347 | unsigned int drain_active: 1; |
5562a8d7 | 348 | unsigned int drain_disabled: 1; |
b52ecf8c | 349 | } ____cacheline_aligned_in_smp; |
2b188cc1 | 350 | |
7f1129d2 | 351 | /* submission data */ |
b52ecf8c | 352 | struct { |
0499e582 PB |
353 | struct mutex uring_lock; |
354 | ||
75b28aff HV |
355 | /* |
356 | * Ring buffer of indices into array of io_uring_sqe, which is | |
357 | * mmapped by the application using the IORING_OFF_SQES offset. | |
358 | * | |
359 | * This indirection could e.g. be used to assign fixed | |
360 | * io_uring_sqe entries to operations and only submit them to | |
361 | * the queue when needed. | |
362 | * | |
363 | * The kernel modifies neither the indices array nor the entries | |
364 | * array. | |
365 | */ | |
366 | u32 *sq_array; | |
c7af47cf | 367 | struct io_uring_sqe *sq_sqes; |
2b188cc1 JA |
368 | unsigned cached_sq_head; |
369 | unsigned sq_entries; | |
de0617e4 | 370 | struct list_head defer_list; |
7f1129d2 PB |
371 | |
372 | /* | |
373 | * Fixed resources fast path, should be accessed only under | |
374 | * uring_lock, and updated through io_uring_register(2) | |
375 | */ | |
376 | struct io_rsrc_node *rsrc_node; | |
ab409402 | 377 | int rsrc_cached_refs; |
7f1129d2 PB |
378 | struct io_file_table file_table; |
379 | unsigned nr_user_files; | |
380 | unsigned nr_user_bufs; | |
381 | struct io_mapped_ubuf **user_bufs; | |
382 | ||
383 | struct io_submit_state submit_state; | |
5262f567 | 384 | struct list_head timeout_list; |
ef9dd637 | 385 | struct list_head ltimeout_list; |
1d7bb1d5 | 386 | struct list_head cq_overflow_list; |
7f1129d2 | 387 | struct xarray io_buffers; |
cc3cec83 | 388 | struct list_head io_buffers_cache; |
7f1129d2 PB |
389 | struct xarray personalities; |
390 | u32 pers_next; | |
391 | unsigned sq_thread_idle; | |
2b188cc1 JA |
392 | } ____cacheline_aligned_in_smp; |
393 | ||
d0acdee2 | 394 | /* IRQ completion list, under ->completion_lock */ |
c2b6c6bc | 395 | struct io_wq_work_list locked_free_list; |
d0acdee2 | 396 | unsigned int locked_free_nr; |
3c1a2ead | 397 | |
7c30f36a | 398 | const struct cred *sq_creds; /* cred used for __io_sq_thread() */ |
534ca6d6 JA |
399 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
400 | ||
90554200 | 401 | struct wait_queue_head sqo_sq_wait; |
69fb2131 | 402 | struct list_head sqd_list; |
75b28aff | 403 | |
5ed7a37d | 404 | unsigned long check_cq_overflow; |
adc8682e OL |
405 | #ifdef CONFIG_NET_RX_BUSY_POLL |
406 | /* used to track busy poll napi_id */ | |
407 | struct list_head napi_list; | |
408 | spinlock_t napi_lock; /* napi_list lock */ | |
409 | #endif | |
5ed7a37d | 410 | |
206aefde JA |
411 | struct { |
412 | unsigned cached_cq_tail; | |
413 | unsigned cq_entries; | |
77bc59b4 | 414 | struct io_ev_fd __rcu *io_ev_fd; |
0499e582 PB |
415 | struct wait_queue_head cq_wait; |
416 | unsigned cq_extra; | |
417 | atomic_t cq_timeouts; | |
0499e582 | 418 | unsigned cq_last_tm_flush; |
206aefde | 419 | } ____cacheline_aligned_in_smp; |
2b188cc1 | 420 | |
2b188cc1 JA |
421 | struct { |
422 | spinlock_t completion_lock; | |
e94f141b | 423 | |
89850fce JA |
424 | spinlock_t timeout_lock; |
425 | ||
def596e9 | 426 | /* |
540e32a0 | 427 | * ->iopoll_list is protected by the ctx->uring_lock for |
def596e9 JA |
428 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
429 | * For SQPOLL, only the single threaded io_sq_thread() will | |
430 | * manipulate the list, hence no extra locking is needed there. | |
431 | */ | |
5eef4e87 | 432 | struct io_wq_work_list iopoll_list; |
78076bb6 JA |
433 | struct hlist_head *cancel_hash; |
434 | unsigned cancel_hash_bits; | |
915b3dde | 435 | bool poll_multi_queue; |
cc3cec83 JA |
436 | |
437 | struct list_head io_buffers_comp; | |
2b188cc1 | 438 | } ____cacheline_aligned_in_smp; |
85faa7b8 | 439 | |
21b55dbc | 440 | struct io_restriction restrictions; |
3c1a2ead | 441 | |
b13a8918 PB |
442 | /* slow path rsrc auxilary data, used by update/register */ |
443 | struct { | |
444 | struct io_rsrc_node *rsrc_backup_node; | |
445 | struct io_mapped_ubuf *dummy_ubuf; | |
446 | struct io_rsrc_data *file_data; | |
447 | struct io_rsrc_data *buf_data; | |
448 | ||
449 | struct delayed_work rsrc_put_work; | |
450 | struct llist_head rsrc_put_llist; | |
451 | struct list_head rsrc_ref_list; | |
452 | spinlock_t rsrc_ref_lock; | |
cc3cec83 JA |
453 | |
454 | struct list_head io_buffers_pages; | |
b13a8918 PB |
455 | }; |
456 | ||
3c1a2ead | 457 | /* Keep this last, we don't need it for the fast path */ |
b986af7e PB |
458 | struct { |
459 | #if defined(CONFIG_UNIX) | |
460 | struct socket *ring_sock; | |
461 | #endif | |
462 | /* hashed buffered write serialization */ | |
463 | struct io_wq_hash *hash_map; | |
464 | ||
465 | /* Only used for accounting purposes */ | |
466 | struct user_struct *user; | |
467 | struct mm_struct *mm_account; | |
468 | ||
469 | /* ctx exit and cancelation */ | |
9011bf9a PB |
470 | struct llist_head fallback_llist; |
471 | struct delayed_work fallback_work; | |
b986af7e PB |
472 | struct work_struct exit_work; |
473 | struct list_head tctx_list; | |
474 | struct completion ref_comp; | |
e139a1ec PB |
475 | u32 iowq_limits[2]; |
476 | bool iowq_limits_set; | |
b986af7e | 477 | }; |
2b188cc1 JA |
478 | }; |
479 | ||
e7a6c00d JA |
480 | /* |
481 | * Arbitrary limit, can be raised if need be | |
482 | */ | |
483 | #define IO_RINGFD_REG_MAX 16 | |
484 | ||
53e043b2 SM |
485 | struct io_uring_task { |
486 | /* submission side */ | |
09899b19 | 487 | int cached_refs; |
53e043b2 SM |
488 | struct xarray xa; |
489 | struct wait_queue_head wait; | |
ee53fb2b SM |
490 | const struct io_ring_ctx *last; |
491 | struct io_wq *io_wq; | |
53e043b2 | 492 | struct percpu_counter inflight; |
b303fe2e | 493 | atomic_t inflight_tracked; |
53e043b2 | 494 | atomic_t in_idle; |
53e043b2 SM |
495 | |
496 | spinlock_t task_lock; | |
497 | struct io_wq_work_list task_list; | |
4813c377 | 498 | struct io_wq_work_list prior_task_list; |
53e043b2 | 499 | struct callback_head task_work; |
e7a6c00d | 500 | struct file **registered_rings; |
6294f368 | 501 | bool task_running; |
53e043b2 SM |
502 | }; |
503 | ||
09bb8394 JA |
504 | /* |
505 | * First field must be the file pointer in all the | |
506 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> | |
507 | */ | |
221c5eb2 JA |
508 | struct io_poll_iocb { |
509 | struct file *file; | |
018043be | 510 | struct wait_queue_head *head; |
221c5eb2 | 511 | __poll_t events; |
392edb45 | 512 | struct wait_queue_entry wait; |
221c5eb2 JA |
513 | }; |
514 | ||
9d805892 | 515 | struct io_poll_update { |
018043be | 516 | struct file *file; |
9d805892 PB |
517 | u64 old_user_data; |
518 | u64 new_user_data; | |
519 | __poll_t events; | |
b69de288 JA |
520 | bool update_events; |
521 | bool update_user_data; | |
018043be PB |
522 | }; |
523 | ||
b5dba59e JA |
524 | struct io_close { |
525 | struct file *file; | |
b5dba59e | 526 | int fd; |
7df778be | 527 | u32 file_slot; |
b5dba59e JA |
528 | }; |
529 | ||
ad8a48ac JA |
530 | struct io_timeout_data { |
531 | struct io_kiocb *req; | |
532 | struct hrtimer timer; | |
533 | struct timespec64 ts; | |
534 | enum hrtimer_mode mode; | |
50c1df2b | 535 | u32 flags; |
ad8a48ac JA |
536 | }; |
537 | ||
8ed8d3c3 JA |
538 | struct io_accept { |
539 | struct file *file; | |
540 | struct sockaddr __user *addr; | |
541 | int __user *addr_len; | |
542 | int flags; | |
aaa4db12 | 543 | u32 file_slot; |
09952e3e | 544 | unsigned long nofile; |
8ed8d3c3 JA |
545 | }; |
546 | ||
547 | struct io_sync { | |
548 | struct file *file; | |
549 | loff_t len; | |
550 | loff_t off; | |
551 | int flags; | |
d63d1b5e | 552 | int mode; |
8ed8d3c3 JA |
553 | }; |
554 | ||
fbf23849 JA |
555 | struct io_cancel { |
556 | struct file *file; | |
557 | u64 addr; | |
558 | }; | |
559 | ||
b29472ee JA |
560 | struct io_timeout { |
561 | struct file *file; | |
bfe68a22 PB |
562 | u32 off; |
563 | u32 target_seq; | |
135fcde8 | 564 | struct list_head list; |
90cd7e42 PB |
565 | /* head of the link, used by linked timeouts only */ |
566 | struct io_kiocb *head; | |
89b263f6 JA |
567 | /* for linked completions */ |
568 | struct io_kiocb *prev; | |
b29472ee JA |
569 | }; |
570 | ||
0bdf7a2d PB |
571 | struct io_timeout_rem { |
572 | struct file *file; | |
573 | u64 addr; | |
9c8e11b3 PB |
574 | |
575 | /* timeout update */ | |
576 | struct timespec64 ts; | |
577 | u32 flags; | |
f1042b6c | 578 | bool ltimeout; |
0bdf7a2d PB |
579 | }; |
580 | ||
9adbd45d JA |
581 | struct io_rw { |
582 | /* NOTE: kiocb has the file as the first member, so don't do it here */ | |
583 | struct kiocb kiocb; | |
584 | u64 addr; | |
585 | u64 len; | |
586 | }; | |
587 | ||
3fbb51c1 JA |
588 | struct io_connect { |
589 | struct file *file; | |
590 | struct sockaddr __user *addr; | |
591 | int addr_len; | |
592 | }; | |
593 | ||
e47293fd JA |
594 | struct io_sr_msg { |
595 | struct file *file; | |
fddaface | 596 | union { |
4af3417a PB |
597 | struct compat_msghdr __user *umsg_compat; |
598 | struct user_msghdr __user *umsg; | |
599 | void __user *buf; | |
fddaface | 600 | }; |
e47293fd | 601 | int msg_flags; |
bcda7baa | 602 | int bgid; |
fddaface | 603 | size_t len; |
e47293fd JA |
604 | }; |
605 | ||
15b71abe JA |
606 | struct io_open { |
607 | struct file *file; | |
608 | int dfd; | |
b9445598 | 609 | u32 file_slot; |
15b71abe | 610 | struct filename *filename; |
c12cedf2 | 611 | struct open_how how; |
4022e7af | 612 | unsigned long nofile; |
15b71abe JA |
613 | }; |
614 | ||
269bbe5f | 615 | struct io_rsrc_update { |
05f3fb3c JA |
616 | struct file *file; |
617 | u64 arg; | |
618 | u32 nr_args; | |
619 | u32 offset; | |
620 | }; | |
621 | ||
4840e418 JA |
622 | struct io_fadvise { |
623 | struct file *file; | |
624 | u64 offset; | |
625 | u32 len; | |
626 | u32 advice; | |
627 | }; | |
628 | ||
c1ca757b JA |
629 | struct io_madvise { |
630 | struct file *file; | |
631 | u64 addr; | |
632 | u32 len; | |
633 | u32 advice; | |
634 | }; | |
635 | ||
3e4827b0 JA |
636 | struct io_epoll { |
637 | struct file *file; | |
638 | int epfd; | |
639 | int op; | |
640 | int fd; | |
641 | struct epoll_event event; | |
e47293fd JA |
642 | }; |
643 | ||
7d67af2c PB |
644 | struct io_splice { |
645 | struct file *file_out; | |
646 | struct file *file_in; | |
647 | loff_t off_out; | |
648 | loff_t off_in; | |
649 | u64 len; | |
650 | unsigned int flags; | |
651 | }; | |
652 | ||
ddf0322d JA |
653 | struct io_provide_buf { |
654 | struct file *file; | |
655 | __u64 addr; | |
38134ada | 656 | __u32 len; |
ddf0322d JA |
657 | __u32 bgid; |
658 | __u16 nbufs; | |
659 | __u16 bid; | |
660 | }; | |
661 | ||
1d9e1288 BM |
662 | struct io_statx { |
663 | struct file *file; | |
664 | int dfd; | |
665 | unsigned int mask; | |
666 | unsigned int flags; | |
e62753e4 | 667 | const char __user *filename; |
1d9e1288 BM |
668 | struct statx __user *buffer; |
669 | }; | |
670 | ||
36f4fa68 JA |
671 | struct io_shutdown { |
672 | struct file *file; | |
673 | int how; | |
674 | }; | |
675 | ||
80a261fd JA |
676 | struct io_rename { |
677 | struct file *file; | |
678 | int old_dfd; | |
679 | int new_dfd; | |
680 | struct filename *oldpath; | |
681 | struct filename *newpath; | |
682 | int flags; | |
683 | }; | |
684 | ||
14a1143b JA |
685 | struct io_unlink { |
686 | struct file *file; | |
687 | int dfd; | |
688 | int flags; | |
689 | struct filename *filename; | |
690 | }; | |
691 | ||
e34a02dc DK |
692 | struct io_mkdir { |
693 | struct file *file; | |
694 | int dfd; | |
695 | umode_t mode; | |
696 | struct filename *filename; | |
697 | }; | |
698 | ||
7a8721f8 DK |
699 | struct io_symlink { |
700 | struct file *file; | |
701 | int new_dfd; | |
702 | struct filename *oldpath; | |
703 | struct filename *newpath; | |
704 | }; | |
705 | ||
cf30da90 DK |
706 | struct io_hardlink { |
707 | struct file *file; | |
708 | int old_dfd; | |
709 | int new_dfd; | |
710 | struct filename *oldpath; | |
711 | struct filename *newpath; | |
712 | int flags; | |
713 | }; | |
714 | ||
4f57f06c JA |
715 | struct io_msg { |
716 | struct file *file; | |
717 | u64 user_data; | |
718 | u32 len; | |
719 | }; | |
720 | ||
f499a021 JA |
721 | struct io_async_connect { |
722 | struct sockaddr_storage address; | |
723 | }; | |
724 | ||
03b1230c JA |
725 | struct io_async_msghdr { |
726 | struct iovec fast_iov[UIO_FASTIOV]; | |
257e84a5 PB |
727 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
728 | struct iovec *free_iov; | |
03b1230c JA |
729 | struct sockaddr __user *uaddr; |
730 | struct msghdr msg; | |
b537916c | 731 | struct sockaddr_storage addr; |
03b1230c JA |
732 | }; |
733 | ||
538941e2 | 734 | struct io_rw_state { |
ff6165b2 | 735 | struct iov_iter iter; |
cd658695 | 736 | struct iov_iter_state iter_state; |
c88598a9 | 737 | struct iovec fast_iov[UIO_FASTIOV]; |
538941e2 PB |
738 | }; |
739 | ||
740 | struct io_async_rw { | |
741 | struct io_rw_state s; | |
742 | const struct iovec *free_iovec; | |
227c0c96 | 743 | size_t bytes_done; |
bcf5a063 | 744 | struct wait_page_queue wpq; |
f67676d1 JA |
745 | }; |
746 | ||
6b47ee6e PB |
747 | enum { |
748 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, | |
749 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, | |
750 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, | |
751 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, | |
752 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, | |
bcda7baa | 753 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
04c76b41 | 754 | REQ_F_CQE_SKIP_BIT = IOSQE_CQE_SKIP_SUCCESS_BIT, |
6b47ee6e | 755 | |
dddca226 | 756 | /* first byte is taken by user flags, shift it to not overlap */ |
93d2bcd2 | 757 | REQ_F_FAIL_BIT = 8, |
6b47ee6e PB |
758 | REQ_F_INFLIGHT_BIT, |
759 | REQ_F_CUR_POS_BIT, | |
760 | REQ_F_NOWAIT_BIT, | |
6b47ee6e | 761 | REQ_F_LINK_TIMEOUT_BIT, |
99bc4c38 | 762 | REQ_F_NEED_CLEANUP_BIT, |
d7718a9d | 763 | REQ_F_POLLED_BIT, |
bcda7baa | 764 | REQ_F_BUFFER_SELECTED_BIT, |
e342c807 | 765 | REQ_F_COMPLETE_INLINE_BIT, |
230d50d4 | 766 | REQ_F_REISSUE_BIT, |
b8e64b53 | 767 | REQ_F_CREDS_BIT, |
20e60a38 | 768 | REQ_F_REFCOUNT_BIT, |
4d13d1a4 | 769 | REQ_F_ARM_LTIMEOUT_BIT, |
d886e185 | 770 | REQ_F_ASYNC_DATA_BIT, |
04c76b41 | 771 | REQ_F_SKIP_LINK_CQES_BIT, |
7b29f92d | 772 | /* keep async read/write and isreg together and in order */ |
35645ac3 | 773 | REQ_F_SUPPORT_NOWAIT_BIT, |
7b29f92d | 774 | REQ_F_ISREG_BIT, |
84557871 JA |
775 | |
776 | /* not a real bit, just to check we're not overflowing the space */ | |
777 | __REQ_F_LAST_BIT, | |
6b47ee6e PB |
778 | }; |
779 | ||
780 | enum { | |
781 | /* ctx owns file */ | |
782 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), | |
783 | /* drain existing IO first */ | |
784 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), | |
785 | /* linked sqes */ | |
786 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), | |
787 | /* doesn't sever on completion < 0 */ | |
788 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), | |
789 | /* IOSQE_ASYNC */ | |
790 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), | |
bcda7baa JA |
791 | /* IOSQE_BUFFER_SELECT */ |
792 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), | |
04c76b41 PB |
793 | /* IOSQE_CQE_SKIP_SUCCESS */ |
794 | REQ_F_CQE_SKIP = BIT(REQ_F_CQE_SKIP_BIT), | |
6b47ee6e | 795 | |
6b47ee6e | 796 | /* fail rest of links */ |
93d2bcd2 | 797 | REQ_F_FAIL = BIT(REQ_F_FAIL_BIT), |
b05a1bcd | 798 | /* on inflight list, should be cancelled and waited on exit reliably */ |
6b47ee6e PB |
799 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
800 | /* read/write uses file position */ | |
801 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), | |
802 | /* must not punt to workers */ | |
803 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), | |
900fad45 | 804 | /* has or had linked timeout */ |
6b47ee6e | 805 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
99bc4c38 PB |
806 | /* needs cleanup */ |
807 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), | |
d7718a9d JA |
808 | /* already went through poll handler */ |
809 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), | |
bcda7baa JA |
810 | /* buffer already selected */ |
811 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), | |
e342c807 PB |
812 | /* completion is deferred through io_comp_state */ |
813 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), | |
230d50d4 JA |
814 | /* caller should reissue async */ |
815 | REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT), | |
35645ac3 PB |
816 | /* supports async reads/writes */ |
817 | REQ_F_SUPPORT_NOWAIT = BIT(REQ_F_SUPPORT_NOWAIT_BIT), | |
7b29f92d JA |
818 | /* regular file */ |
819 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), | |
b8e64b53 PB |
820 | /* has creds assigned */ |
821 | REQ_F_CREDS = BIT(REQ_F_CREDS_BIT), | |
20e60a38 PB |
822 | /* skip refcounting if not set */ |
823 | REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT), | |
4d13d1a4 PB |
824 | /* there is a linked timeout that has to be armed */ |
825 | REQ_F_ARM_LTIMEOUT = BIT(REQ_F_ARM_LTIMEOUT_BIT), | |
d886e185 PB |
826 | /* ->async_data allocated */ |
827 | REQ_F_ASYNC_DATA = BIT(REQ_F_ASYNC_DATA_BIT), | |
04c76b41 PB |
828 | /* don't post CQEs while failing linked requests */ |
829 | REQ_F_SKIP_LINK_CQES = BIT(REQ_F_SKIP_LINK_CQES_BIT), | |
d7718a9d JA |
830 | }; |
831 | ||
832 | struct async_poll { | |
833 | struct io_poll_iocb poll; | |
807abcb0 | 834 | struct io_poll_iocb *double_poll; |
6b47ee6e PB |
835 | }; |
836 | ||
f237c30a | 837 | typedef void (*io_req_tw_func_t)(struct io_kiocb *req, bool *locked); |
5b0a6acc | 838 | |
7cbf1722 | 839 | struct io_task_work { |
5b0a6acc PB |
840 | union { |
841 | struct io_wq_work_node node; | |
842 | struct llist_node fallback_node; | |
843 | }; | |
844 | io_req_tw_func_t func; | |
7cbf1722 JA |
845 | }; |
846 | ||
992da01a PB |
847 | enum { |
848 | IORING_RSRC_FILE = 0, | |
849 | IORING_RSRC_BUFFER = 1, | |
850 | }; | |
851 | ||
09bb8394 JA |
852 | /* |
853 | * NOTE! Each of the iocb union members has the file pointer | |
854 | * as the first entry in their struct definition. So you can | |
855 | * access the file pointer through any of the sub-structs, | |
63c36549 | 856 | * or directly as just 'file' in this struct. |
09bb8394 | 857 | */ |
2b188cc1 | 858 | struct io_kiocb { |
221c5eb2 | 859 | union { |
09bb8394 | 860 | struct file *file; |
9adbd45d | 861 | struct io_rw rw; |
221c5eb2 | 862 | struct io_poll_iocb poll; |
9d805892 | 863 | struct io_poll_update poll_update; |
8ed8d3c3 JA |
864 | struct io_accept accept; |
865 | struct io_sync sync; | |
fbf23849 | 866 | struct io_cancel cancel; |
b29472ee | 867 | struct io_timeout timeout; |
0bdf7a2d | 868 | struct io_timeout_rem timeout_rem; |
3fbb51c1 | 869 | struct io_connect connect; |
e47293fd | 870 | struct io_sr_msg sr_msg; |
15b71abe | 871 | struct io_open open; |
b5dba59e | 872 | struct io_close close; |
269bbe5f | 873 | struct io_rsrc_update rsrc_update; |
4840e418 | 874 | struct io_fadvise fadvise; |
c1ca757b | 875 | struct io_madvise madvise; |
3e4827b0 | 876 | struct io_epoll epoll; |
7d67af2c | 877 | struct io_splice splice; |
ddf0322d | 878 | struct io_provide_buf pbuf; |
1d9e1288 | 879 | struct io_statx statx; |
36f4fa68 | 880 | struct io_shutdown shutdown; |
80a261fd | 881 | struct io_rename rename; |
14a1143b | 882 | struct io_unlink unlink; |
e34a02dc | 883 | struct io_mkdir mkdir; |
7a8721f8 | 884 | struct io_symlink symlink; |
cf30da90 | 885 | struct io_hardlink hardlink; |
4f57f06c | 886 | struct io_msg msg; |
221c5eb2 | 887 | }; |
2b188cc1 | 888 | |
d625c6ee | 889 | u8 opcode; |
65a6543d XW |
890 | /* polled IO has completed */ |
891 | u8 iopoll_completed; | |
4f4eeba8 | 892 | u16 buf_index; |
d17e56eb PB |
893 | unsigned int flags; |
894 | ||
895 | u64 user_data; | |
9cf7c104 | 896 | u32 result; |
d17e56eb | 897 | u32 cflags; |
4f4eeba8 | 898 | |
010e8e6b | 899 | struct io_ring_ctx *ctx; |
010e8e6b | 900 | struct task_struct *task; |
d7718a9d | 901 | |
269bbe5f | 902 | struct percpu_ref *fixed_rsrc_refs; |
d886e185 PB |
903 | /* store used ubuf, so we can prevent reloading */ |
904 | struct io_mapped_ubuf *imu; | |
fcb323cc | 905 | |
7e3709d5 | 906 | /* used by request caches, completion batching and iopoll */ |
ef05d9eb | 907 | struct io_wq_work_node comp_list; |
d17e56eb | 908 | atomic_t refs; |
7e3709d5 | 909 | struct io_kiocb *link; |
5b0a6acc | 910 | struct io_task_work io_task_work; |
010e8e6b PB |
911 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
912 | struct hlist_node hash_node; | |
7e3709d5 | 913 | /* internal polling, see IORING_FEAT_FAST_POLL */ |
010e8e6b | 914 | struct async_poll *apoll; |
d886e185 PB |
915 | /* opcode allocated if it needs to store data for async defer */ |
916 | void *async_data; | |
010e8e6b | 917 | struct io_wq_work work; |
7e3709d5 | 918 | /* custom credentials, valid IFF REQ_F_CREDS is set */ |
fe7e3257 | 919 | const struct cred *creds; |
7e3709d5 | 920 | /* stores selected buf, valid IFF REQ_F_BUFFER_SELECTED is set */ |
30d51dd4 | 921 | struct io_buffer *kbuf; |
aa43477b | 922 | atomic_t poll_refs; |
2b188cc1 | 923 | }; |
05589553 | 924 | |
13bf43f5 PB |
925 | struct io_tctx_node { |
926 | struct list_head ctx_node; | |
927 | struct task_struct *task; | |
13bf43f5 PB |
928 | struct io_ring_ctx *ctx; |
929 | }; | |
930 | ||
27dc8338 PB |
931 | struct io_defer_entry { |
932 | struct list_head list; | |
933 | struct io_kiocb *req; | |
9cf7c104 | 934 | u32 seq; |
2b188cc1 JA |
935 | }; |
936 | ||
d3656344 | 937 | struct io_op_def { |
d3656344 JA |
938 | /* needs req->file assigned */ |
939 | unsigned needs_file : 1; | |
6d63416d PB |
940 | /* should block plug */ |
941 | unsigned plug : 1; | |
d3656344 JA |
942 | /* hash wq insertion if file is a regular file */ |
943 | unsigned hash_reg_file : 1; | |
944 | /* unbound wq insertion if file is a non-regular file */ | |
945 | unsigned unbound_nonreg_file : 1; | |
8a72758c JA |
946 | /* set if opcode supports polled "wait" */ |
947 | unsigned pollin : 1; | |
948 | unsigned pollout : 1; | |
bcda7baa JA |
949 | /* op supports buffer selection */ |
950 | unsigned buffer_select : 1; | |
26f0505a PB |
951 | /* do prep async if is going to be punted */ |
952 | unsigned needs_async_setup : 1; | |
6d63416d PB |
953 | /* opcode is not supported by this kernel */ |
954 | unsigned not_supported : 1; | |
5bd2182d PM |
955 | /* skip auditing */ |
956 | unsigned audit_skip : 1; | |
e8c2bc1f JA |
957 | /* size of async data needed, if any */ |
958 | unsigned short async_size; | |
d3656344 JA |
959 | }; |
960 | ||
0918682b | 961 | static const struct io_op_def io_op_defs[] = { |
0463b6c5 PB |
962 | [IORING_OP_NOP] = {}, |
963 | [IORING_OP_READV] = { | |
d3656344 JA |
964 | .needs_file = 1, |
965 | .unbound_nonreg_file = 1, | |
8a72758c | 966 | .pollin = 1, |
4d954c25 | 967 | .buffer_select = 1, |
26f0505a | 968 | .needs_async_setup = 1, |
27926b68 | 969 | .plug = 1, |
5bd2182d | 970 | .audit_skip = 1, |
e8c2bc1f | 971 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 972 | }, |
0463b6c5 | 973 | [IORING_OP_WRITEV] = { |
d3656344 JA |
974 | .needs_file = 1, |
975 | .hash_reg_file = 1, | |
976 | .unbound_nonreg_file = 1, | |
8a72758c | 977 | .pollout = 1, |
26f0505a | 978 | .needs_async_setup = 1, |
27926b68 | 979 | .plug = 1, |
5bd2182d | 980 | .audit_skip = 1, |
e8c2bc1f | 981 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 982 | }, |
0463b6c5 | 983 | [IORING_OP_FSYNC] = { |
d3656344 | 984 | .needs_file = 1, |
5bd2182d | 985 | .audit_skip = 1, |
d3656344 | 986 | }, |
0463b6c5 | 987 | [IORING_OP_READ_FIXED] = { |
d3656344 JA |
988 | .needs_file = 1, |
989 | .unbound_nonreg_file = 1, | |
8a72758c | 990 | .pollin = 1, |
27926b68 | 991 | .plug = 1, |
5bd2182d | 992 | .audit_skip = 1, |
e8c2bc1f | 993 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 994 | }, |
0463b6c5 | 995 | [IORING_OP_WRITE_FIXED] = { |
d3656344 JA |
996 | .needs_file = 1, |
997 | .hash_reg_file = 1, | |
998 | .unbound_nonreg_file = 1, | |
8a72758c | 999 | .pollout = 1, |
27926b68 | 1000 | .plug = 1, |
5bd2182d | 1001 | .audit_skip = 1, |
e8c2bc1f | 1002 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 1003 | }, |
0463b6c5 | 1004 | [IORING_OP_POLL_ADD] = { |
d3656344 JA |
1005 | .needs_file = 1, |
1006 | .unbound_nonreg_file = 1, | |
5bd2182d PM |
1007 | .audit_skip = 1, |
1008 | }, | |
1009 | [IORING_OP_POLL_REMOVE] = { | |
1010 | .audit_skip = 1, | |
d3656344 | 1011 | }, |
0463b6c5 | 1012 | [IORING_OP_SYNC_FILE_RANGE] = { |
d3656344 | 1013 | .needs_file = 1, |
5bd2182d | 1014 | .audit_skip = 1, |
d3656344 | 1015 | }, |
0463b6c5 | 1016 | [IORING_OP_SENDMSG] = { |
d3656344 JA |
1017 | .needs_file = 1, |
1018 | .unbound_nonreg_file = 1, | |
8a72758c | 1019 | .pollout = 1, |
26f0505a | 1020 | .needs_async_setup = 1, |
e8c2bc1f | 1021 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 1022 | }, |
0463b6c5 | 1023 | [IORING_OP_RECVMSG] = { |
d3656344 JA |
1024 | .needs_file = 1, |
1025 | .unbound_nonreg_file = 1, | |
8a72758c | 1026 | .pollin = 1, |
52de1fe1 | 1027 | .buffer_select = 1, |
26f0505a | 1028 | .needs_async_setup = 1, |
e8c2bc1f | 1029 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 1030 | }, |
0463b6c5 | 1031 | [IORING_OP_TIMEOUT] = { |
5bd2182d | 1032 | .audit_skip = 1, |
e8c2bc1f | 1033 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 1034 | }, |
9c8e11b3 PB |
1035 | [IORING_OP_TIMEOUT_REMOVE] = { |
1036 | /* used by timeout updates' prep() */ | |
5bd2182d | 1037 | .audit_skip = 1, |
9c8e11b3 | 1038 | }, |
0463b6c5 | 1039 | [IORING_OP_ACCEPT] = { |
d3656344 JA |
1040 | .needs_file = 1, |
1041 | .unbound_nonreg_file = 1, | |
8a72758c | 1042 | .pollin = 1, |
d3656344 | 1043 | }, |
5bd2182d PM |
1044 | [IORING_OP_ASYNC_CANCEL] = { |
1045 | .audit_skip = 1, | |
1046 | }, | |
0463b6c5 | 1047 | [IORING_OP_LINK_TIMEOUT] = { |
5bd2182d | 1048 | .audit_skip = 1, |
e8c2bc1f | 1049 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 1050 | }, |
0463b6c5 | 1051 | [IORING_OP_CONNECT] = { |
d3656344 JA |
1052 | .needs_file = 1, |
1053 | .unbound_nonreg_file = 1, | |
8a72758c | 1054 | .pollout = 1, |
26f0505a | 1055 | .needs_async_setup = 1, |
e8c2bc1f | 1056 | .async_size = sizeof(struct io_async_connect), |
d3656344 | 1057 | }, |
0463b6c5 | 1058 | [IORING_OP_FALLOCATE] = { |
d3656344 | 1059 | .needs_file = 1, |
d3656344 | 1060 | }, |
44526bed JA |
1061 | [IORING_OP_OPENAT] = {}, |
1062 | [IORING_OP_CLOSE] = {}, | |
5bd2182d PM |
1063 | [IORING_OP_FILES_UPDATE] = { |
1064 | .audit_skip = 1, | |
1065 | }, | |
1066 | [IORING_OP_STATX] = { | |
1067 | .audit_skip = 1, | |
1068 | }, | |
0463b6c5 | 1069 | [IORING_OP_READ] = { |
3a6820f2 JA |
1070 | .needs_file = 1, |
1071 | .unbound_nonreg_file = 1, | |
8a72758c | 1072 | .pollin = 1, |
bcda7baa | 1073 | .buffer_select = 1, |
27926b68 | 1074 | .plug = 1, |
5bd2182d | 1075 | .audit_skip = 1, |
e8c2bc1f | 1076 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 1077 | }, |
0463b6c5 | 1078 | [IORING_OP_WRITE] = { |
3a6820f2 | 1079 | .needs_file = 1, |
7b3188e7 | 1080 | .hash_reg_file = 1, |
3a6820f2 | 1081 | .unbound_nonreg_file = 1, |
8a72758c | 1082 | .pollout = 1, |
27926b68 | 1083 | .plug = 1, |
5bd2182d | 1084 | .audit_skip = 1, |
e8c2bc1f | 1085 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 1086 | }, |
0463b6c5 | 1087 | [IORING_OP_FADVISE] = { |
4840e418 | 1088 | .needs_file = 1, |
5bd2182d | 1089 | .audit_skip = 1, |
c1ca757b | 1090 | }, |
44526bed | 1091 | [IORING_OP_MADVISE] = {}, |
0463b6c5 | 1092 | [IORING_OP_SEND] = { |
fddaface JA |
1093 | .needs_file = 1, |
1094 | .unbound_nonreg_file = 1, | |
8a72758c | 1095 | .pollout = 1, |
5bd2182d | 1096 | .audit_skip = 1, |
fddaface | 1097 | }, |
0463b6c5 | 1098 | [IORING_OP_RECV] = { |
fddaface JA |
1099 | .needs_file = 1, |
1100 | .unbound_nonreg_file = 1, | |
8a72758c | 1101 | .pollin = 1, |
bcda7baa | 1102 | .buffer_select = 1, |
5bd2182d | 1103 | .audit_skip = 1, |
fddaface | 1104 | }, |
0463b6c5 | 1105 | [IORING_OP_OPENAT2] = { |
cebdb986 | 1106 | }, |
3e4827b0 JA |
1107 | [IORING_OP_EPOLL_CTL] = { |
1108 | .unbound_nonreg_file = 1, | |
5bd2182d | 1109 | .audit_skip = 1, |
3e4827b0 | 1110 | }, |
7d67af2c PB |
1111 | [IORING_OP_SPLICE] = { |
1112 | .needs_file = 1, | |
1113 | .hash_reg_file = 1, | |
1114 | .unbound_nonreg_file = 1, | |
5bd2182d PM |
1115 | .audit_skip = 1, |
1116 | }, | |
1117 | [IORING_OP_PROVIDE_BUFFERS] = { | |
1118 | .audit_skip = 1, | |
1119 | }, | |
1120 | [IORING_OP_REMOVE_BUFFERS] = { | |
1121 | .audit_skip = 1, | |
ddf0322d | 1122 | }, |
f2a8d5c7 PB |
1123 | [IORING_OP_TEE] = { |
1124 | .needs_file = 1, | |
1125 | .hash_reg_file = 1, | |
1126 | .unbound_nonreg_file = 1, | |
5bd2182d | 1127 | .audit_skip = 1, |
f2a8d5c7 | 1128 | }, |
36f4fa68 JA |
1129 | [IORING_OP_SHUTDOWN] = { |
1130 | .needs_file = 1, | |
1131 | }, | |
44526bed JA |
1132 | [IORING_OP_RENAMEAT] = {}, |
1133 | [IORING_OP_UNLINKAT] = {}, | |
e34a02dc | 1134 | [IORING_OP_MKDIRAT] = {}, |
7a8721f8 | 1135 | [IORING_OP_SYMLINKAT] = {}, |
cf30da90 | 1136 | [IORING_OP_LINKAT] = {}, |
4f57f06c JA |
1137 | [IORING_OP_MSG_RING] = { |
1138 | .needs_file = 1, | |
1139 | }, | |
d3656344 JA |
1140 | }; |
1141 | ||
0756a869 PB |
1142 | /* requests with any of those set should undergo io_disarm_next() */ |
1143 | #define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL) | |
1144 | ||
7a612350 | 1145 | static bool io_disarm_next(struct io_kiocb *req); |
eef51daa | 1146 | static void io_uring_del_tctx_node(unsigned long index); |
9936c7c2 PB |
1147 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
1148 | struct task_struct *task, | |
3dd0c97a | 1149 | bool cancel_all); |
78cc687b | 1150 | static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd); |
1ffc5422 | 1151 | |
913a571a PB |
1152 | static void io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags); |
1153 | ||
ec9c02ad | 1154 | static void io_put_req(struct io_kiocb *req); |
91c2f697 | 1155 | static void io_put_req_deferred(struct io_kiocb *req); |
c7dae4ba | 1156 | static void io_dismantle_req(struct io_kiocb *req); |
94ae5e77 | 1157 | static void io_queue_linked_timeout(struct io_kiocb *req); |
fdecb662 | 1158 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 1159 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 | 1160 | unsigned nr_args); |
68fb8979 | 1161 | static void io_clean_op(struct io_kiocb *req); |
ac177053 | 1162 | static struct file *io_file_get(struct io_ring_ctx *ctx, |
8371adf5 | 1163 | struct io_kiocb *req, int fd, bool fixed); |
c5eef2b9 | 1164 | static void __io_queue_sqe(struct io_kiocb *req); |
269bbe5f | 1165 | static void io_rsrc_put_work(struct work_struct *work); |
de0617e4 | 1166 | |
907d1df3 | 1167 | static void io_req_task_queue(struct io_kiocb *req); |
c450178d | 1168 | static void __io_submit_flush_completions(struct io_ring_ctx *ctx); |
179ae0d1 | 1169 | static int io_req_prep_async(struct io_kiocb *req); |
de0617e4 | 1170 | |
b9445598 PB |
1171 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
1172 | unsigned int issue_flags, u32 slot_index); | |
7df778be PB |
1173 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags); |
1174 | ||
f1042b6c | 1175 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer); |
b9445598 | 1176 | |
2b188cc1 JA |
1177 | static struct kmem_cache *req_cachep; |
1178 | ||
0918682b | 1179 | static const struct file_operations io_uring_fops; |
2b188cc1 JA |
1180 | |
1181 | struct sock *io_uring_get_socket(struct file *file) | |
1182 | { | |
1183 | #if defined(CONFIG_UNIX) | |
1184 | if (file->f_op == &io_uring_fops) { | |
1185 | struct io_ring_ctx *ctx = file->private_data; | |
1186 | ||
1187 | return ctx->ring_sock->sk; | |
1188 | } | |
1189 | #endif | |
1190 | return NULL; | |
1191 | } | |
1192 | EXPORT_SYMBOL(io_uring_get_socket); | |
1193 | ||
f237c30a PB |
1194 | static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked) |
1195 | { | |
1196 | if (!*locked) { | |
1197 | mutex_lock(&ctx->uring_lock); | |
1198 | *locked = true; | |
1199 | } | |
1200 | } | |
1201 | ||
f2f87370 PB |
1202 | #define io_for_each_link(pos, head) \ |
1203 | for (pos = (head); pos; pos = pos->link) | |
1204 | ||
21c843d5 PB |
1205 | /* |
1206 | * Shamelessly stolen from the mm implementation of page reference checking, | |
1207 | * see commit f958d7b528b1 for details. | |
1208 | */ | |
1209 | #define req_ref_zero_or_close_to_overflow(req) \ | |
1210 | ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u) | |
1211 | ||
1212 | static inline bool req_ref_inc_not_zero(struct io_kiocb *req) | |
1213 | { | |
20e60a38 | 1214 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1215 | return atomic_inc_not_zero(&req->refs); |
1216 | } | |
1217 | ||
21c843d5 PB |
1218 | static inline bool req_ref_put_and_test(struct io_kiocb *req) |
1219 | { | |
20e60a38 PB |
1220 | if (likely(!(req->flags & REQ_F_REFCOUNT))) |
1221 | return true; | |
1222 | ||
21c843d5 PB |
1223 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1224 | return atomic_dec_and_test(&req->refs); | |
1225 | } | |
1226 | ||
21c843d5 PB |
1227 | static inline void req_ref_get(struct io_kiocb *req) |
1228 | { | |
20e60a38 | 1229 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1230 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1231 | atomic_inc(&req->refs); | |
1232 | } | |
1233 | ||
c450178d PB |
1234 | static inline void io_submit_flush_completions(struct io_ring_ctx *ctx) |
1235 | { | |
6f33b0bc | 1236 | if (!wq_list_empty(&ctx->submit_state.compl_reqs)) |
c450178d PB |
1237 | __io_submit_flush_completions(ctx); |
1238 | } | |
1239 | ||
48dcd38d | 1240 | static inline void __io_req_set_refcount(struct io_kiocb *req, int nr) |
20e60a38 PB |
1241 | { |
1242 | if (!(req->flags & REQ_F_REFCOUNT)) { | |
1243 | req->flags |= REQ_F_REFCOUNT; | |
48dcd38d | 1244 | atomic_set(&req->refs, nr); |
20e60a38 PB |
1245 | } |
1246 | } | |
1247 | ||
48dcd38d PB |
1248 | static inline void io_req_set_refcount(struct io_kiocb *req) |
1249 | { | |
1250 | __io_req_set_refcount(req, 1); | |
1251 | } | |
1252 | ||
ab409402 PB |
1253 | #define IO_RSRC_REF_BATCH 100 |
1254 | ||
1255 | static inline void io_req_put_rsrc_locked(struct io_kiocb *req, | |
1256 | struct io_ring_ctx *ctx) | |
1257 | __must_hold(&ctx->uring_lock) | |
36f72fe2 | 1258 | { |
ab409402 PB |
1259 | struct percpu_ref *ref = req->fixed_rsrc_refs; |
1260 | ||
1261 | if (ref) { | |
1262 | if (ref == &ctx->rsrc_node->refs) | |
1263 | ctx->rsrc_cached_refs++; | |
1264 | else | |
1265 | percpu_ref_put(ref); | |
1266 | } | |
1267 | } | |
1268 | ||
1269 | static inline void io_req_put_rsrc(struct io_kiocb *req, struct io_ring_ctx *ctx) | |
1270 | { | |
1271 | if (req->fixed_rsrc_refs) | |
1272 | percpu_ref_put(req->fixed_rsrc_refs); | |
1273 | } | |
1274 | ||
1275 | static __cold void io_rsrc_refs_drop(struct io_ring_ctx *ctx) | |
1276 | __must_hold(&ctx->uring_lock) | |
1277 | { | |
1278 | if (ctx->rsrc_cached_refs) { | |
1279 | percpu_ref_put_many(&ctx->rsrc_node->refs, ctx->rsrc_cached_refs); | |
1280 | ctx->rsrc_cached_refs = 0; | |
1281 | } | |
1282 | } | |
1283 | ||
1284 | static void io_rsrc_refs_refill(struct io_ring_ctx *ctx) | |
1285 | __must_hold(&ctx->uring_lock) | |
1286 | { | |
1287 | ctx->rsrc_cached_refs += IO_RSRC_REF_BATCH; | |
1288 | percpu_ref_get_many(&ctx->rsrc_node->refs, IO_RSRC_REF_BATCH); | |
1289 | } | |
36f72fe2 | 1290 | |
a46be971 PB |
1291 | static inline void io_req_set_rsrc_node(struct io_kiocb *req, |
1292 | struct io_ring_ctx *ctx) | |
36f72fe2 | 1293 | { |
269bbe5f | 1294 | if (!req->fixed_rsrc_refs) { |
a7f0ed5a | 1295 | req->fixed_rsrc_refs = &ctx->rsrc_node->refs; |
ab409402 PB |
1296 | ctx->rsrc_cached_refs--; |
1297 | if (unlikely(ctx->rsrc_cached_refs < 0)) | |
1298 | io_rsrc_refs_refill(ctx); | |
36f72fe2 PB |
1299 | } |
1300 | } | |
1301 | ||
cc3cec83 | 1302 | static unsigned int __io_put_kbuf(struct io_kiocb *req, struct list_head *list) |
3648e526 | 1303 | { |
d1fd1c20 | 1304 | struct io_buffer *kbuf = req->kbuf; |
3648e526 HX |
1305 | unsigned int cflags; |
1306 | ||
cc3cec83 | 1307 | cflags = IORING_CQE_F_BUFFER | (kbuf->bid << IORING_CQE_BUFFER_SHIFT); |
3648e526 | 1308 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
cc3cec83 | 1309 | list_add(&kbuf->list, list); |
d1fd1c20 | 1310 | req->kbuf = NULL; |
3648e526 HX |
1311 | return cflags; |
1312 | } | |
1313 | ||
cc3cec83 | 1314 | static inline unsigned int io_put_kbuf_comp(struct io_kiocb *req) |
3648e526 HX |
1315 | { |
1316 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) | |
1317 | return 0; | |
cc3cec83 JA |
1318 | return __io_put_kbuf(req, &req->ctx->io_buffers_comp); |
1319 | } | |
1320 | ||
1321 | static inline unsigned int io_put_kbuf(struct io_kiocb *req, | |
1322 | unsigned issue_flags) | |
1323 | { | |
1324 | unsigned int cflags; | |
1325 | ||
1326 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) | |
1327 | return 0; | |
1328 | ||
1329 | /* | |
1330 | * We can add this buffer back to two lists: | |
1331 | * | |
1332 | * 1) The io_buffers_cache list. This one is protected by the | |
1333 | * ctx->uring_lock. If we already hold this lock, add back to this | |
1334 | * list as we can grab it from issue as well. | |
1335 | * 2) The io_buffers_comp list. This one is protected by the | |
1336 | * ctx->completion_lock. | |
1337 | * | |
1338 | * We migrate buffers from the comp_list to the issue cache list | |
1339 | * when we need one. | |
1340 | */ | |
1341 | if (issue_flags & IO_URING_F_UNLOCKED) { | |
1342 | struct io_ring_ctx *ctx = req->ctx; | |
1343 | ||
1344 | spin_lock(&ctx->completion_lock); | |
1345 | cflags = __io_put_kbuf(req, &ctx->io_buffers_comp); | |
1346 | spin_unlock(&ctx->completion_lock); | |
1347 | } else { | |
1348 | cflags = __io_put_kbuf(req, &req->ctx->io_buffers_cache); | |
1349 | } | |
1350 | ||
1351 | return cflags; | |
3648e526 HX |
1352 | } |
1353 | ||
3dd0c97a PB |
1354 | static bool io_match_task(struct io_kiocb *head, struct task_struct *task, |
1355 | bool cancel_all) | |
6af3f48b | 1356 | __must_hold(&req->ctx->timeout_lock) |
08d23634 PB |
1357 | { |
1358 | struct io_kiocb *req; | |
1359 | ||
68207680 | 1360 | if (task && head->task != task) |
08d23634 | 1361 | return false; |
3dd0c97a | 1362 | if (cancel_all) |
08d23634 PB |
1363 | return true; |
1364 | ||
1365 | io_for_each_link(req, head) { | |
b05a1bcd | 1366 | if (req->flags & REQ_F_INFLIGHT) |
02a13674 | 1367 | return true; |
08d23634 PB |
1368 | } |
1369 | return false; | |
1370 | } | |
1371 | ||
6af3f48b PB |
1372 | static bool io_match_linked(struct io_kiocb *head) |
1373 | { | |
1374 | struct io_kiocb *req; | |
1375 | ||
1376 | io_for_each_link(req, head) { | |
1377 | if (req->flags & REQ_F_INFLIGHT) | |
1378 | return true; | |
1379 | } | |
1380 | return false; | |
1381 | } | |
1382 | ||
1383 | /* | |
1384 | * As io_match_task() but protected against racing with linked timeouts. | |
1385 | * User must not hold timeout_lock. | |
1386 | */ | |
1387 | static bool io_match_task_safe(struct io_kiocb *head, struct task_struct *task, | |
1388 | bool cancel_all) | |
1389 | { | |
1390 | bool matched; | |
1391 | ||
1392 | if (task && head->task != task) | |
1393 | return false; | |
1394 | if (cancel_all) | |
1395 | return true; | |
1396 | ||
1397 | if (head->flags & REQ_F_LINK_TIMEOUT) { | |
1398 | struct io_ring_ctx *ctx = head->ctx; | |
1399 | ||
1400 | /* protect against races with linked timeouts */ | |
1401 | spin_lock_irq(&ctx->timeout_lock); | |
1402 | matched = io_match_linked(head); | |
1403 | spin_unlock_irq(&ctx->timeout_lock); | |
1404 | } else { | |
1405 | matched = io_match_linked(head); | |
1406 | } | |
1407 | return matched; | |
1408 | } | |
1409 | ||
d886e185 PB |
1410 | static inline bool req_has_async_data(struct io_kiocb *req) |
1411 | { | |
1412 | return req->flags & REQ_F_ASYNC_DATA; | |
1413 | } | |
1414 | ||
93d2bcd2 | 1415 | static inline void req_set_fail(struct io_kiocb *req) |
c40f6379 | 1416 | { |
93d2bcd2 | 1417 | req->flags |= REQ_F_FAIL; |
04c76b41 PB |
1418 | if (req->flags & REQ_F_CQE_SKIP) { |
1419 | req->flags &= ~REQ_F_CQE_SKIP; | |
1420 | req->flags |= REQ_F_SKIP_LINK_CQES; | |
1421 | } | |
c40f6379 | 1422 | } |
4a38aed2 | 1423 | |
a8295b98 HX |
1424 | static inline void req_fail_link_node(struct io_kiocb *req, int res) |
1425 | { | |
1426 | req_set_fail(req); | |
1427 | req->result = res; | |
1428 | } | |
1429 | ||
c072481d | 1430 | static __cold void io_ring_ctx_ref_free(struct percpu_ref *ref) |
2b188cc1 JA |
1431 | { |
1432 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
1433 | ||
0f158b4c | 1434 | complete(&ctx->ref_comp); |
2b188cc1 JA |
1435 | } |
1436 | ||
8eb7e2d0 PB |
1437 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
1438 | { | |
1439 | return !req->timeout.off; | |
1440 | } | |
1441 | ||
c072481d | 1442 | static __cold void io_fallback_req_func(struct work_struct *work) |
f56165e6 PB |
1443 | { |
1444 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, | |
1445 | fallback_work.work); | |
1446 | struct llist_node *node = llist_del_all(&ctx->fallback_llist); | |
1447 | struct io_kiocb *req, *tmp; | |
f237c30a | 1448 | bool locked = false; |
f56165e6 PB |
1449 | |
1450 | percpu_ref_get(&ctx->refs); | |
1451 | llist_for_each_entry_safe(req, tmp, node, io_task_work.fallback_node) | |
f237c30a | 1452 | req->io_task_work.func(req, &locked); |
5636c00d | 1453 | |
f237c30a | 1454 | if (locked) { |
c450178d | 1455 | io_submit_flush_completions(ctx); |
f237c30a PB |
1456 | mutex_unlock(&ctx->uring_lock); |
1457 | } | |
f56165e6 PB |
1458 | percpu_ref_put(&ctx->refs); |
1459 | } | |
1460 | ||
c072481d | 1461 | static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
2b188cc1 JA |
1462 | { |
1463 | struct io_ring_ctx *ctx; | |
78076bb6 | 1464 | int hash_bits; |
2b188cc1 JA |
1465 | |
1466 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1467 | if (!ctx) | |
1468 | return NULL; | |
1469 | ||
78076bb6 JA |
1470 | /* |
1471 | * Use 5 bits less than the max cq entries, that should give us around | |
1472 | * 32 entries per hash list if totally full and uniformly spread. | |
1473 | */ | |
1474 | hash_bits = ilog2(p->cq_entries); | |
1475 | hash_bits -= 5; | |
1476 | if (hash_bits <= 0) | |
1477 | hash_bits = 1; | |
1478 | ctx->cancel_hash_bits = hash_bits; | |
1479 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
1480 | GFP_KERNEL); | |
1481 | if (!ctx->cancel_hash) | |
1482 | goto err; | |
1483 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
1484 | ||
6224843d PB |
1485 | ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL); |
1486 | if (!ctx->dummy_ubuf) | |
1487 | goto err; | |
1488 | /* set invalid range, so io_import_fixed() fails meeting it */ | |
1489 | ctx->dummy_ubuf->ubuf = -1UL; | |
1490 | ||
21482896 | 1491 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
1492 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
1493 | goto err; | |
2b188cc1 JA |
1494 | |
1495 | ctx->flags = p->flags; | |
90554200 | 1496 | init_waitqueue_head(&ctx->sqo_sq_wait); |
69fb2131 | 1497 | INIT_LIST_HEAD(&ctx->sqd_list); |
1d7bb1d5 | 1498 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
cc3cec83 | 1499 | INIT_LIST_HEAD(&ctx->io_buffers_cache); |
0f158b4c | 1500 | init_completion(&ctx->ref_comp); |
9e15c3a0 | 1501 | xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1); |
61cf9370 | 1502 | xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1); |
2b188cc1 | 1503 | mutex_init(&ctx->uring_lock); |
311997b3 | 1504 | init_waitqueue_head(&ctx->cq_wait); |
2b188cc1 | 1505 | spin_lock_init(&ctx->completion_lock); |
89850fce | 1506 | spin_lock_init(&ctx->timeout_lock); |
5eef4e87 | 1507 | INIT_WQ_LIST(&ctx->iopoll_list); |
cc3cec83 JA |
1508 | INIT_LIST_HEAD(&ctx->io_buffers_pages); |
1509 | INIT_LIST_HEAD(&ctx->io_buffers_comp); | |
de0617e4 | 1510 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 1511 | INIT_LIST_HEAD(&ctx->timeout_list); |
ef9dd637 | 1512 | INIT_LIST_HEAD(&ctx->ltimeout_list); |
d67d2263 BM |
1513 | spin_lock_init(&ctx->rsrc_ref_lock); |
1514 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); | |
269bbe5f BM |
1515 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
1516 | init_llist_head(&ctx->rsrc_put_llist); | |
13bf43f5 | 1517 | INIT_LIST_HEAD(&ctx->tctx_list); |
c2b6c6bc PB |
1518 | ctx->submit_state.free_list.next = NULL; |
1519 | INIT_WQ_LIST(&ctx->locked_free_list); | |
9011bf9a | 1520 | INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func); |
6f33b0bc | 1521 | INIT_WQ_LIST(&ctx->submit_state.compl_reqs); |
adc8682e OL |
1522 | #ifdef CONFIG_NET_RX_BUSY_POLL |
1523 | INIT_LIST_HEAD(&ctx->napi_list); | |
1524 | spin_lock_init(&ctx->napi_lock); | |
1525 | #endif | |
2b188cc1 | 1526 | return ctx; |
206aefde | 1527 | err: |
6224843d | 1528 | kfree(ctx->dummy_ubuf); |
78076bb6 | 1529 | kfree(ctx->cancel_hash); |
206aefde JA |
1530 | kfree(ctx); |
1531 | return NULL; | |
2b188cc1 JA |
1532 | } |
1533 | ||
8f6ed49a PB |
1534 | static void io_account_cq_overflow(struct io_ring_ctx *ctx) |
1535 | { | |
1536 | struct io_rings *r = ctx->rings; | |
1537 | ||
1538 | WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1); | |
1539 | ctx->cq_extra--; | |
1540 | } | |
1541 | ||
9cf7c104 | 1542 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
7adf4eaf | 1543 | { |
2bc9930e JA |
1544 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
1545 | struct io_ring_ctx *ctx = req->ctx; | |
a197f664 | 1546 | |
8f6ed49a | 1547 | return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail; |
2bc9930e | 1548 | } |
de0617e4 | 1549 | |
9d858b21 | 1550 | return false; |
de0617e4 JA |
1551 | } |
1552 | ||
35645ac3 PB |
1553 | #define FFS_NOWAIT 0x1UL |
1554 | #define FFS_ISREG 0x2UL | |
1555 | #define FFS_MASK ~(FFS_NOWAIT|FFS_ISREG) | |
c97d8a0f PB |
1556 | |
1557 | static inline bool io_req_ffs_set(struct io_kiocb *req) | |
1558 | { | |
35645ac3 | 1559 | return req->flags & REQ_F_FIXED_FILE; |
c97d8a0f PB |
1560 | } |
1561 | ||
c072481d | 1562 | static inline void io_req_track_inflight(struct io_kiocb *req) |
ce3d5aae | 1563 | { |
ce3d5aae | 1564 | if (!(req->flags & REQ_F_INFLIGHT)) { |
ce3d5aae | 1565 | req->flags |= REQ_F_INFLIGHT; |
b303fe2e | 1566 | atomic_inc(¤t->io_uring->inflight_tracked); |
ce3d5aae PB |
1567 | } |
1568 | } | |
1569 | ||
fd08e530 PB |
1570 | static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req) |
1571 | { | |
906c6caa PB |
1572 | if (WARN_ON_ONCE(!req->link)) |
1573 | return NULL; | |
1574 | ||
4d13d1a4 PB |
1575 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
1576 | req->flags |= REQ_F_LINK_TIMEOUT; | |
fd08e530 PB |
1577 | |
1578 | /* linked timeouts should have two refs once prep'ed */ | |
48dcd38d | 1579 | io_req_set_refcount(req); |
4d13d1a4 PB |
1580 | __io_req_set_refcount(req->link, 2); |
1581 | return req->link; | |
fd08e530 PB |
1582 | } |
1583 | ||
1584 | static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) | |
1585 | { | |
4d13d1a4 | 1586 | if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT))) |
fd08e530 PB |
1587 | return NULL; |
1588 | return __io_prep_linked_timeout(req); | |
1589 | } | |
1590 | ||
1e6fa521 JA |
1591 | static void io_prep_async_work(struct io_kiocb *req) |
1592 | { | |
1593 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
1e6fa521 JA |
1594 | struct io_ring_ctx *ctx = req->ctx; |
1595 | ||
b8e64b53 PB |
1596 | if (!(req->flags & REQ_F_CREDS)) { |
1597 | req->flags |= REQ_F_CREDS; | |
c10d1f98 | 1598 | req->creds = get_current_cred(); |
b8e64b53 | 1599 | } |
003e8dcc | 1600 | |
e1d675df PB |
1601 | req->work.list.next = NULL; |
1602 | req->work.flags = 0; | |
feaadc4f PB |
1603 | if (req->flags & REQ_F_FORCE_ASYNC) |
1604 | req->work.flags |= IO_WQ_WORK_CONCURRENT; | |
1605 | ||
1e6fa521 JA |
1606 | if (req->flags & REQ_F_ISREG) { |
1607 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) | |
1608 | io_wq_hash_work(&req->work, file_inode(req->file)); | |
4b982bd0 | 1609 | } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) { |
1e6fa521 JA |
1610 | if (def->unbound_nonreg_file) |
1611 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1612 | } | |
e1d675df PB |
1613 | |
1614 | switch (req->opcode) { | |
1615 | case IORING_OP_SPLICE: | |
1616 | case IORING_OP_TEE: | |
e1d675df PB |
1617 | if (!S_ISREG(file_inode(req->splice.file_in)->i_mode)) |
1618 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1619 | break; | |
1620 | } | |
561fb04a | 1621 | } |
cccf0ee8 | 1622 | |
cbdcb435 | 1623 | static void io_prep_async_link(struct io_kiocb *req) |
561fb04a | 1624 | { |
cbdcb435 | 1625 | struct io_kiocb *cur; |
54a91f3b | 1626 | |
44eff40a PB |
1627 | if (req->flags & REQ_F_LINK_TIMEOUT) { |
1628 | struct io_ring_ctx *ctx = req->ctx; | |
1629 | ||
674ee8e1 | 1630 | spin_lock_irq(&ctx->timeout_lock); |
44eff40a PB |
1631 | io_for_each_link(cur, req) |
1632 | io_prep_async_work(cur); | |
674ee8e1 | 1633 | spin_unlock_irq(&ctx->timeout_lock); |
44eff40a PB |
1634 | } else { |
1635 | io_for_each_link(cur, req) | |
1636 | io_prep_async_work(cur); | |
1637 | } | |
561fb04a JA |
1638 | } |
1639 | ||
fff4e40e PB |
1640 | static inline void io_req_add_compl_list(struct io_kiocb *req) |
1641 | { | |
3d4aeb9f | 1642 | struct io_ring_ctx *ctx = req->ctx; |
33ce2aff | 1643 | struct io_submit_state *state = &ctx->submit_state; |
fff4e40e | 1644 | |
3d4aeb9f PB |
1645 | if (!(req->flags & REQ_F_CQE_SKIP)) |
1646 | ctx->submit_state.flush_cqes = true; | |
fff4e40e PB |
1647 | wq_list_add_tail(&req->comp_list, &state->compl_reqs); |
1648 | } | |
1649 | ||
00169246 | 1650 | static void io_queue_async_work(struct io_kiocb *req, bool *dont_use) |
561fb04a | 1651 | { |
a197f664 | 1652 | struct io_ring_ctx *ctx = req->ctx; |
cbdcb435 | 1653 | struct io_kiocb *link = io_prep_linked_timeout(req); |
5aa75ed5 | 1654 | struct io_uring_task *tctx = req->task->io_uring; |
561fb04a | 1655 | |
3bfe6106 JA |
1656 | BUG_ON(!tctx); |
1657 | BUG_ON(!tctx->io_wq); | |
561fb04a | 1658 | |
cbdcb435 PB |
1659 | /* init ->work of the whole link before punting */ |
1660 | io_prep_async_link(req); | |
991468dc JA |
1661 | |
1662 | /* | |
1663 | * Not expected to happen, but if we do have a bug where this _can_ | |
1664 | * happen, catch it here and ensure the request is marked as | |
1665 | * canceled. That will make io-wq go through the usual work cancel | |
1666 | * procedure rather than attempt to run this request (or create a new | |
1667 | * worker for it). | |
1668 | */ | |
1669 | if (WARN_ON_ONCE(!same_thread_group(req->task, current))) | |
1670 | req->work.flags |= IO_WQ_WORK_CANCEL; | |
1671 | ||
502c87d6 SR |
1672 | trace_io_uring_queue_async_work(ctx, req, req->user_data, req->opcode, req->flags, |
1673 | &req->work, io_wq_is_hashed(&req->work)); | |
ebf93667 | 1674 | io_wq_enqueue(tctx->io_wq, &req->work); |
7271ef3a JA |
1675 | if (link) |
1676 | io_queue_linked_timeout(link); | |
cbdcb435 PB |
1677 | } |
1678 | ||
1ee4160c | 1679 | static void io_kill_timeout(struct io_kiocb *req, int status) |
8c855885 | 1680 | __must_hold(&req->ctx->completion_lock) |
89850fce | 1681 | __must_hold(&req->ctx->timeout_lock) |
5262f567 | 1682 | { |
e8c2bc1f | 1683 | struct io_timeout_data *io = req->async_data; |
5262f567 | 1684 | |
fd9c7bc5 | 1685 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
2ae2eb9d PB |
1686 | if (status) |
1687 | req_set_fail(req); | |
01cec8c1 PB |
1688 | atomic_set(&req->ctx->cq_timeouts, |
1689 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
135fcde8 | 1690 | list_del_init(&req->timeout.list); |
913a571a | 1691 | io_fill_cqe_req(req, status, 0); |
91c2f697 | 1692 | io_put_req_deferred(req); |
5262f567 JA |
1693 | } |
1694 | } | |
1695 | ||
c072481d | 1696 | static __cold void io_queue_deferred(struct io_ring_ctx *ctx) |
de0617e4 | 1697 | { |
441b8a78 | 1698 | while (!list_empty(&ctx->defer_list)) { |
27dc8338 PB |
1699 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
1700 | struct io_defer_entry, list); | |
de0617e4 | 1701 | |
9cf7c104 | 1702 | if (req_need_defer(de->req, de->seq)) |
04518945 | 1703 | break; |
27dc8338 | 1704 | list_del_init(&de->list); |
907d1df3 | 1705 | io_req_task_queue(de->req); |
27dc8338 | 1706 | kfree(de); |
441b8a78 | 1707 | } |
04518945 PB |
1708 | } |
1709 | ||
c072481d | 1710 | static __cold void io_flush_timeouts(struct io_ring_ctx *ctx) |
89850fce | 1711 | __must_hold(&ctx->completion_lock) |
de0617e4 | 1712 | { |
441b8a78 | 1713 | u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
f010505b | 1714 | |
79ebeaee | 1715 | spin_lock_irq(&ctx->timeout_lock); |
f18ee4cf | 1716 | while (!list_empty(&ctx->timeout_list)) { |
f010505b | 1717 | u32 events_needed, events_got; |
360428f8 | 1718 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
135fcde8 | 1719 | struct io_kiocb, timeout.list); |
de0617e4 | 1720 | |
8eb7e2d0 | 1721 | if (io_is_timeout_noseq(req)) |
360428f8 | 1722 | break; |
f010505b MDG |
1723 | |
1724 | /* | |
1725 | * Since seq can easily wrap around over time, subtract | |
1726 | * the last seq at which timeouts were flushed before comparing. | |
1727 | * Assuming not more than 2^31-1 events have happened since, | |
1728 | * these subtractions won't have wrapped, so we can check if | |
1729 | * target is in [last_seq, current_seq] by comparing the two. | |
1730 | */ | |
1731 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; | |
1732 | events_got = seq - ctx->cq_last_tm_flush; | |
1733 | if (events_got < events_needed) | |
360428f8 | 1734 | break; |
bfe68a22 | 1735 | |
135fcde8 | 1736 | list_del_init(&req->timeout.list); |
1ee4160c | 1737 | io_kill_timeout(req, 0); |
f18ee4cf | 1738 | } |
f010505b | 1739 | ctx->cq_last_tm_flush = seq; |
79ebeaee | 1740 | spin_unlock_irq(&ctx->timeout_lock); |
360428f8 | 1741 | } |
5262f567 | 1742 | |
c072481d | 1743 | static __cold void __io_commit_cqring_flush(struct io_ring_ctx *ctx) |
360428f8 | 1744 | { |
2335f6f5 PB |
1745 | if (ctx->off_timeout_used) |
1746 | io_flush_timeouts(ctx); | |
1747 | if (ctx->drain_active) | |
1748 | io_queue_deferred(ctx); | |
1749 | } | |
1750 | ||
1751 | static inline void io_commit_cqring(struct io_ring_ctx *ctx) | |
1752 | { | |
1753 | if (unlikely(ctx->off_timeout_used || ctx->drain_active)) | |
1754 | __io_commit_cqring_flush(ctx); | |
ec30e04b PB |
1755 | /* order cqe stores with ring update */ |
1756 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); | |
de0617e4 JA |
1757 | } |
1758 | ||
90554200 JA |
1759 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
1760 | { | |
1761 | struct io_rings *r = ctx->rings; | |
1762 | ||
a566c556 | 1763 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries; |
90554200 JA |
1764 | } |
1765 | ||
888aae2e PB |
1766 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
1767 | { | |
1768 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); | |
1769 | } | |
1770 | ||
d068b506 | 1771 | static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx) |
2b188cc1 | 1772 | { |
75b28aff | 1773 | struct io_rings *rings = ctx->rings; |
ea5ab3b5 | 1774 | unsigned tail, mask = ctx->cq_entries - 1; |
2b188cc1 | 1775 | |
115e12e5 SB |
1776 | /* |
1777 | * writes to the cq entry need to come after reading head; the | |
1778 | * control dependency is enough as we're using WRITE_ONCE to | |
1779 | * fill the cq entry | |
1780 | */ | |
a566c556 | 1781 | if (__io_cqring_events(ctx) == ctx->cq_entries) |
2b188cc1 JA |
1782 | return NULL; |
1783 | ||
888aae2e | 1784 | tail = ctx->cached_cq_tail++; |
ea5ab3b5 | 1785 | return &rings->cqes[tail & mask]; |
2b188cc1 JA |
1786 | } |
1787 | ||
77bc59b4 | 1788 | static void io_eventfd_signal(struct io_ring_ctx *ctx) |
f2842ab5 | 1789 | { |
77bc59b4 UA |
1790 | struct io_ev_fd *ev_fd; |
1791 | ||
1792 | /* Return quickly if ctx->io_ev_fd doesn't exist */ | |
1793 | if (likely(!rcu_dereference_raw(ctx->io_ev_fd))) | |
1794 | return; | |
1795 | ||
1796 | rcu_read_lock(); | |
1797 | /* | |
1798 | * rcu_dereference ctx->io_ev_fd once and use it for both for checking | |
1799 | * and eventfd_signal | |
1800 | */ | |
1801 | ev_fd = rcu_dereference(ctx->io_ev_fd); | |
1802 | ||
1803 | /* | |
1804 | * Check again if ev_fd exists incase an io_eventfd_unregister call | |
1805 | * completed between the NULL check of ctx->io_ev_fd at the start of | |
1806 | * the function and rcu_read_lock. | |
1807 | */ | |
1808 | if (unlikely(!ev_fd)) | |
1809 | goto out; | |
7e55a19c | 1810 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
77bc59b4 UA |
1811 | goto out; |
1812 | ||
c75312dd | 1813 | if (!ev_fd->eventfd_async || io_wq_current_is_worker()) |
77bc59b4 UA |
1814 | eventfd_signal(ev_fd->cq_ev_fd, 1); |
1815 | ||
1816 | out: | |
1817 | rcu_read_unlock(); | |
f2842ab5 JA |
1818 | } |
1819 | ||
2c5d763c JA |
1820 | /* |
1821 | * This should only get called when at least one event has been posted. | |
1822 | * Some applications rely on the eventfd notification count only changing | |
1823 | * IFF a new CQE has been added to the CQ ring. There's no depedency on | |
1824 | * 1:1 relationship between how many times this function is called (and | |
1825 | * hence the eventfd count) and number of CQEs posted to the CQ ring. | |
1826 | */ | |
b41e9852 | 1827 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
1d7bb1d5 | 1828 | { |
5fd46178 JA |
1829 | /* |
1830 | * wake_up_all() may seem excessive, but io_wake_function() and | |
1831 | * io_should_wake() handle the termination of the loop and only | |
1832 | * wake as many waiters as we need to. | |
1833 | */ | |
1834 | if (wq_has_sleeper(&ctx->cq_wait)) | |
1835 | wake_up_all(&ctx->cq_wait); | |
77bc59b4 | 1836 | io_eventfd_signal(ctx); |
1d7bb1d5 JA |
1837 | } |
1838 | ||
80c18e4a PB |
1839 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
1840 | { | |
c57a91fb PB |
1841 | /* see waitqueue_active() comment */ |
1842 | smp_mb(); | |
1843 | ||
80c18e4a | 1844 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
c57a91fb | 1845 | if (waitqueue_active(&ctx->cq_wait)) |
5fd46178 | 1846 | wake_up_all(&ctx->cq_wait); |
80c18e4a | 1847 | } |
77bc59b4 | 1848 | io_eventfd_signal(ctx); |
80c18e4a PB |
1849 | } |
1850 | ||
c4a2ed72 | 1851 | /* Returns true if there are no backlogged entries after the flush */ |
6c2450ae | 1852 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force) |
1d7bb1d5 | 1853 | { |
b18032bb | 1854 | bool all_flushed, posted; |
1d7bb1d5 | 1855 | |
a566c556 | 1856 | if (!force && __io_cqring_events(ctx) == ctx->cq_entries) |
e23de15f | 1857 | return false; |
1d7bb1d5 | 1858 | |
b18032bb | 1859 | posted = false; |
79ebeaee | 1860 | spin_lock(&ctx->completion_lock); |
6c2450ae | 1861 | while (!list_empty(&ctx->cq_overflow_list)) { |
d068b506 | 1862 | struct io_uring_cqe *cqe = io_get_cqe(ctx); |
6c2450ae | 1863 | struct io_overflow_cqe *ocqe; |
e6c8aa9a | 1864 | |
1d7bb1d5 JA |
1865 | if (!cqe && !force) |
1866 | break; | |
6c2450ae PB |
1867 | ocqe = list_first_entry(&ctx->cq_overflow_list, |
1868 | struct io_overflow_cqe, list); | |
1869 | if (cqe) | |
1870 | memcpy(cqe, &ocqe->cqe, sizeof(*cqe)); | |
1871 | else | |
8f6ed49a PB |
1872 | io_account_cq_overflow(ctx); |
1873 | ||
b18032bb | 1874 | posted = true; |
6c2450ae PB |
1875 | list_del(&ocqe->list); |
1876 | kfree(ocqe); | |
1d7bb1d5 JA |
1877 | } |
1878 | ||
09e88404 PB |
1879 | all_flushed = list_empty(&ctx->cq_overflow_list); |
1880 | if (all_flushed) { | |
5ed7a37d | 1881 | clear_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
1882 | WRITE_ONCE(ctx->rings->sq_flags, |
1883 | ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW); | |
09e88404 | 1884 | } |
46930143 | 1885 | |
b18032bb JA |
1886 | if (posted) |
1887 | io_commit_cqring(ctx); | |
79ebeaee | 1888 | spin_unlock(&ctx->completion_lock); |
b18032bb JA |
1889 | if (posted) |
1890 | io_cqring_ev_posted(ctx); | |
09e88404 | 1891 | return all_flushed; |
1d7bb1d5 JA |
1892 | } |
1893 | ||
90f67366 | 1894 | static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx) |
6c503150 | 1895 | { |
ca0a2651 JA |
1896 | bool ret = true; |
1897 | ||
5ed7a37d | 1898 | if (test_bit(0, &ctx->check_cq_overflow)) { |
6c503150 PB |
1899 | /* iopoll syncs against uring_lock, not completion_lock */ |
1900 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1901 | mutex_lock(&ctx->uring_lock); | |
90f67366 | 1902 | ret = __io_cqring_overflow_flush(ctx, false); |
6c503150 PB |
1903 | if (ctx->flags & IORING_SETUP_IOPOLL) |
1904 | mutex_unlock(&ctx->uring_lock); | |
1905 | } | |
ca0a2651 JA |
1906 | |
1907 | return ret; | |
6c503150 PB |
1908 | } |
1909 | ||
6a290a14 PB |
1910 | /* must to be called somewhat shortly after putting a request */ |
1911 | static inline void io_put_task(struct task_struct *task, int nr) | |
1912 | { | |
1913 | struct io_uring_task *tctx = task->io_uring; | |
1914 | ||
e98e49b2 PB |
1915 | if (likely(task == current)) { |
1916 | tctx->cached_refs += nr; | |
1917 | } else { | |
1918 | percpu_counter_sub(&tctx->inflight, nr); | |
1919 | if (unlikely(atomic_read(&tctx->in_idle))) | |
1920 | wake_up(&tctx->wait); | |
1921 | put_task_struct_many(task, nr); | |
1922 | } | |
6a290a14 PB |
1923 | } |
1924 | ||
9a10867a PB |
1925 | static void io_task_refs_refill(struct io_uring_task *tctx) |
1926 | { | |
1927 | unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR; | |
1928 | ||
1929 | percpu_counter_add(&tctx->inflight, refill); | |
1930 | refcount_add(refill, ¤t->usage); | |
1931 | tctx->cached_refs += refill; | |
1932 | } | |
1933 | ||
1934 | static inline void io_get_task_refs(int nr) | |
1935 | { | |
1936 | struct io_uring_task *tctx = current->io_uring; | |
1937 | ||
1938 | tctx->cached_refs -= nr; | |
1939 | if (unlikely(tctx->cached_refs < 0)) | |
1940 | io_task_refs_refill(tctx); | |
1941 | } | |
1942 | ||
3cc7fdb9 PB |
1943 | static __cold void io_uring_drop_tctx_refs(struct task_struct *task) |
1944 | { | |
1945 | struct io_uring_task *tctx = task->io_uring; | |
1946 | unsigned int refs = tctx->cached_refs; | |
1947 | ||
1948 | if (refs) { | |
1949 | tctx->cached_refs = 0; | |
1950 | percpu_counter_sub(&tctx->inflight, refs); | |
1951 | put_task_struct_many(task, refs); | |
1952 | } | |
1953 | } | |
1954 | ||
d4d19c19 | 1955 | static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data, |
54daa9b2 | 1956 | s32 res, u32 cflags) |
2b188cc1 | 1957 | { |
cce4b8b0 | 1958 | struct io_overflow_cqe *ocqe; |
2b188cc1 | 1959 | |
cce4b8b0 PB |
1960 | ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT); |
1961 | if (!ocqe) { | |
1962 | /* | |
1963 | * If we're in ring overflow flush mode, or in task cancel mode, | |
1964 | * or cannot allocate an overflow entry, then we need to drop it | |
1965 | * on the floor. | |
1966 | */ | |
8f6ed49a | 1967 | io_account_cq_overflow(ctx); |
cce4b8b0 | 1968 | return false; |
2b188cc1 | 1969 | } |
cce4b8b0 | 1970 | if (list_empty(&ctx->cq_overflow_list)) { |
5ed7a37d | 1971 | set_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
1972 | WRITE_ONCE(ctx->rings->sq_flags, |
1973 | ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW); | |
1974 | ||
cce4b8b0 | 1975 | } |
d4d19c19 | 1976 | ocqe->cqe.user_data = user_data; |
cce4b8b0 PB |
1977 | ocqe->cqe.res = res; |
1978 | ocqe->cqe.flags = cflags; | |
1979 | list_add_tail(&ocqe->list, &ctx->cq_overflow_list); | |
1980 | return true; | |
2b188cc1 JA |
1981 | } |
1982 | ||
d5ec1dfa | 1983 | static inline bool __fill_cqe(struct io_ring_ctx *ctx, u64 user_data, |
913a571a | 1984 | s32 res, u32 cflags) |
2b188cc1 JA |
1985 | { |
1986 | struct io_uring_cqe *cqe; | |
1987 | ||
1988 | /* | |
1989 | * If we can't get a cq entry, userspace overflowed the | |
1990 | * submission (by quite a lot). Increment the overflow count in | |
1991 | * the ring. | |
1992 | */ | |
d068b506 | 1993 | cqe = io_get_cqe(ctx); |
1d7bb1d5 | 1994 | if (likely(cqe)) { |
d4d19c19 | 1995 | WRITE_ONCE(cqe->user_data, user_data); |
2b188cc1 | 1996 | WRITE_ONCE(cqe->res, res); |
bcda7baa | 1997 | WRITE_ONCE(cqe->flags, cflags); |
8d13326e | 1998 | return true; |
2b188cc1 | 1999 | } |
d4d19c19 | 2000 | return io_cqring_event_overflow(ctx, user_data, res, cflags); |
2b188cc1 JA |
2001 | } |
2002 | ||
d5ec1dfa SR |
2003 | static inline bool __io_fill_cqe(struct io_kiocb *req, s32 res, u32 cflags) |
2004 | { | |
502c87d6 | 2005 | trace_io_uring_complete(req->ctx, req, req->user_data, res, cflags); |
d5ec1dfa SR |
2006 | return __fill_cqe(req->ctx, req->user_data, res, cflags); |
2007 | } | |
2008 | ||
913a571a | 2009 | static noinline void io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags) |
bcda7baa | 2010 | { |
04c76b41 | 2011 | if (!(req->flags & REQ_F_CQE_SKIP)) |
d5ec1dfa | 2012 | __io_fill_cqe(req, res, cflags); |
bcda7baa JA |
2013 | } |
2014 | ||
913a571a PB |
2015 | static noinline bool io_fill_cqe_aux(struct io_ring_ctx *ctx, u64 user_data, |
2016 | s32 res, u32 cflags) | |
bcda7baa | 2017 | { |
913a571a | 2018 | ctx->cq_extra++; |
502c87d6 | 2019 | trace_io_uring_complete(ctx, NULL, user_data, res, cflags); |
d5ec1dfa | 2020 | return __fill_cqe(ctx, user_data, res, cflags); |
bcda7baa JA |
2021 | } |
2022 | ||
a37fae8a HX |
2023 | static void __io_req_complete_post(struct io_kiocb *req, s32 res, |
2024 | u32 cflags) | |
2b188cc1 | 2025 | { |
78e19bbe | 2026 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 | 2027 | |
04c76b41 | 2028 | if (!(req->flags & REQ_F_CQE_SKIP)) |
d5ec1dfa | 2029 | __io_fill_cqe(req, res, cflags); |
c7dae4ba JA |
2030 | /* |
2031 | * If we're the last reference to this request, add to our locked | |
2032 | * free_list cache. | |
2033 | */ | |
de9b4cca | 2034 | if (req_ref_put_and_test(req)) { |
7a612350 | 2035 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
0756a869 | 2036 | if (req->flags & IO_DISARM_MASK) |
7a612350 PB |
2037 | io_disarm_next(req); |
2038 | if (req->link) { | |
2039 | io_req_task_queue(req->link); | |
2040 | req->link = NULL; | |
2041 | } | |
2042 | } | |
ab409402 | 2043 | io_req_put_rsrc(req, ctx); |
c7dae4ba JA |
2044 | io_dismantle_req(req); |
2045 | io_put_task(req->task, 1); | |
c2b6c6bc | 2046 | wq_list_add_head(&req->comp_list, &ctx->locked_free_list); |
d0acdee2 | 2047 | ctx->locked_free_nr++; |
180f829f | 2048 | } |
a37fae8a HX |
2049 | } |
2050 | ||
2051 | static void io_req_complete_post(struct io_kiocb *req, s32 res, | |
2052 | u32 cflags) | |
2053 | { | |
2054 | struct io_ring_ctx *ctx = req->ctx; | |
2055 | ||
2056 | spin_lock(&ctx->completion_lock); | |
2057 | __io_req_complete_post(req, res, cflags); | |
7a612350 | 2058 | io_commit_cqring(ctx); |
79ebeaee | 2059 | spin_unlock(&ctx->completion_lock); |
a3f34907 | 2060 | io_cqring_ev_posted(ctx); |
4e3d9ff9 JA |
2061 | } |
2062 | ||
54daa9b2 PB |
2063 | static inline void io_req_complete_state(struct io_kiocb *req, s32 res, |
2064 | u32 cflags) | |
229a7b63 | 2065 | { |
a38d68db | 2066 | req->result = res; |
d17e56eb | 2067 | req->cflags = cflags; |
e342c807 | 2068 | req->flags |= REQ_F_COMPLETE_INLINE; |
e1e16097 JA |
2069 | } |
2070 | ||
889fca73 | 2071 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
54daa9b2 | 2072 | s32 res, u32 cflags) |
bcda7baa | 2073 | { |
889fca73 PB |
2074 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
2075 | io_req_complete_state(req, res, cflags); | |
a38d68db | 2076 | else |
c7dae4ba | 2077 | io_req_complete_post(req, res, cflags); |
bcda7baa JA |
2078 | } |
2079 | ||
54daa9b2 | 2080 | static inline void io_req_complete(struct io_kiocb *req, s32 res) |
0ddf92e8 | 2081 | { |
889fca73 | 2082 | __io_req_complete(req, 0, res, 0); |
0ddf92e8 JA |
2083 | } |
2084 | ||
54daa9b2 | 2085 | static void io_req_complete_failed(struct io_kiocb *req, s32 res) |
f41db273 | 2086 | { |
93d2bcd2 | 2087 | req_set_fail(req); |
f41db273 PB |
2088 | io_req_complete_post(req, res, 0); |
2089 | } | |
2090 | ||
c6d3d9cb PB |
2091 | static void io_req_complete_fail_submit(struct io_kiocb *req) |
2092 | { | |
2093 | /* | |
2094 | * We don't submit, fail them all, for that replace hardlinks with | |
2095 | * normal links. Extra REQ_F_LINK is tolerated. | |
2096 | */ | |
2097 | req->flags &= ~REQ_F_HARDLINK; | |
2098 | req->flags |= REQ_F_LINK; | |
2099 | io_req_complete_failed(req, req->result); | |
2100 | } | |
2101 | ||
864ea921 PB |
2102 | /* |
2103 | * Don't initialise the fields below on every allocation, but do that in | |
2104 | * advance and keep them valid across allocations. | |
2105 | */ | |
2106 | static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx) | |
2107 | { | |
2108 | req->ctx = ctx; | |
2109 | req->link = NULL; | |
2110 | req->async_data = NULL; | |
2111 | /* not necessary, but safer to zero */ | |
2112 | req->result = 0; | |
2113 | } | |
2114 | ||
dac7a098 | 2115 | static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx, |
cd0ca2e0 | 2116 | struct io_submit_state *state) |
dac7a098 | 2117 | { |
79ebeaee | 2118 | spin_lock(&ctx->completion_lock); |
c2b6c6bc | 2119 | wq_list_splice(&ctx->locked_free_list, &state->free_list); |
d0acdee2 | 2120 | ctx->locked_free_nr = 0; |
79ebeaee | 2121 | spin_unlock(&ctx->completion_lock); |
dac7a098 PB |
2122 | } |
2123 | ||
dd78f492 | 2124 | /* Returns true IFF there are requests in the cache */ |
c7dae4ba | 2125 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
0ddf92e8 | 2126 | { |
c7dae4ba | 2127 | struct io_submit_state *state = &ctx->submit_state; |
0ddf92e8 | 2128 | |
c7dae4ba JA |
2129 | /* |
2130 | * If we have more than a batch's worth of requests in our IRQ side | |
2131 | * locked cache, grab the lock and move them over to our submission | |
2132 | * side cache. | |
2133 | */ | |
d0acdee2 | 2134 | if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH) |
cd0ca2e0 | 2135 | io_flush_cached_locked_reqs(ctx, state); |
c2b6c6bc | 2136 | return !!state->free_list.next; |
0ddf92e8 JA |
2137 | } |
2138 | ||
5d5901a3 PB |
2139 | /* |
2140 | * A request might get retired back into the request caches even before opcode | |
2141 | * handlers and io_issue_sqe() are done with it, e.g. inline completion path. | |
2142 | * Because of that, io_alloc_req() should be called only under ->uring_lock | |
2143 | * and with extra caution to not get a request that is still worked on. | |
2144 | */ | |
c072481d | 2145 | static __cold bool __io_alloc_req_refill(struct io_ring_ctx *ctx) |
5d5901a3 | 2146 | __must_hold(&ctx->uring_lock) |
2b188cc1 | 2147 | { |
e5d1bc0a | 2148 | struct io_submit_state *state = &ctx->submit_state; |
864ea921 | 2149 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
3ab665b7 PB |
2150 | void *reqs[IO_REQ_ALLOC_BATCH]; |
2151 | struct io_kiocb *req; | |
864ea921 | 2152 | int ret, i; |
e5d1bc0a | 2153 | |
c2b6c6bc | 2154 | if (likely(state->free_list.next || io_flush_cached_reqs(ctx))) |
a33ae9ce | 2155 | return true; |
e5d1bc0a | 2156 | |
3ab665b7 | 2157 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, ARRAY_SIZE(reqs), reqs); |
fd6fab2c | 2158 | |
864ea921 PB |
2159 | /* |
2160 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
2161 | * retry single alloc to be on the safe side. | |
2162 | */ | |
2163 | if (unlikely(ret <= 0)) { | |
3ab665b7 PB |
2164 | reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
2165 | if (!reqs[0]) | |
a33ae9ce | 2166 | return false; |
864ea921 | 2167 | ret = 1; |
2b188cc1 | 2168 | } |
864ea921 | 2169 | |
37f0e767 | 2170 | percpu_ref_get_many(&ctx->refs, ret); |
3ab665b7 PB |
2171 | for (i = 0; i < ret; i++) { |
2172 | req = reqs[i]; | |
2173 | ||
2174 | io_preinit_req(req, ctx); | |
c2b6c6bc | 2175 | wq_stack_add_head(&req->comp_list, &state->free_list); |
3ab665b7 | 2176 | } |
a33ae9ce PB |
2177 | return true; |
2178 | } | |
2179 | ||
2180 | static inline bool io_alloc_req_refill(struct io_ring_ctx *ctx) | |
2181 | { | |
2182 | if (unlikely(!ctx->submit_state.free_list.next)) | |
2183 | return __io_alloc_req_refill(ctx); | |
2184 | return true; | |
2185 | } | |
2186 | ||
2187 | static inline struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) | |
2188 | { | |
2189 | struct io_wq_work_node *node; | |
2190 | ||
2191 | node = wq_stack_extract(&ctx->submit_state.free_list); | |
c2b6c6bc | 2192 | return container_of(node, struct io_kiocb, comp_list); |
2b188cc1 JA |
2193 | } |
2194 | ||
e1d767f0 | 2195 | static inline void io_put_file(struct file *file) |
8da11c19 | 2196 | { |
e1d767f0 | 2197 | if (file) |
8da11c19 PB |
2198 | fput(file); |
2199 | } | |
2200 | ||
6b639522 | 2201 | static inline void io_dismantle_req(struct io_kiocb *req) |
2b188cc1 | 2202 | { |
094bae49 | 2203 | unsigned int flags = req->flags; |
929a3af9 | 2204 | |
867f8fa5 | 2205 | if (unlikely(flags & IO_REQ_CLEAN_FLAGS)) |
3a0a6902 | 2206 | io_clean_op(req); |
e1d767f0 PB |
2207 | if (!(flags & REQ_F_FIXED_FILE)) |
2208 | io_put_file(req->file); | |
e65ef56d JA |
2209 | } |
2210 | ||
c072481d | 2211 | static __cold void __io_free_req(struct io_kiocb *req) |
c6ca97b3 | 2212 | { |
51a4cc11 | 2213 | struct io_ring_ctx *ctx = req->ctx; |
c6ca97b3 | 2214 | |
ab409402 | 2215 | io_req_put_rsrc(req, ctx); |
216578e5 | 2216 | io_dismantle_req(req); |
7c660731 | 2217 | io_put_task(req->task, 1); |
c6ca97b3 | 2218 | |
79ebeaee | 2219 | spin_lock(&ctx->completion_lock); |
c2b6c6bc | 2220 | wq_list_add_head(&req->comp_list, &ctx->locked_free_list); |
c34b025f | 2221 | ctx->locked_free_nr++; |
79ebeaee | 2222 | spin_unlock(&ctx->completion_lock); |
e65ef56d JA |
2223 | } |
2224 | ||
f2f87370 PB |
2225 | static inline void io_remove_next_linked(struct io_kiocb *req) |
2226 | { | |
2227 | struct io_kiocb *nxt = req->link; | |
2228 | ||
2229 | req->link = nxt->link; | |
2230 | nxt->link = NULL; | |
2231 | } | |
2232 | ||
33cc89a9 PB |
2233 | static bool io_kill_linked_timeout(struct io_kiocb *req) |
2234 | __must_hold(&req->ctx->completion_lock) | |
89b263f6 | 2235 | __must_hold(&req->ctx->timeout_lock) |
2665abfd | 2236 | { |
33cc89a9 | 2237 | struct io_kiocb *link = req->link; |
f2f87370 | 2238 | |
b97e736a | 2239 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
c9abd7ad | 2240 | struct io_timeout_data *io = link->async_data; |
7c86ffee | 2241 | |
f2f87370 | 2242 | io_remove_next_linked(req); |
90cd7e42 | 2243 | link->timeout.head = NULL; |
fd9c7bc5 | 2244 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
ef9dd637 | 2245 | list_del(&link->timeout.list); |
04c76b41 | 2246 | /* leave REQ_F_CQE_SKIP to io_fill_cqe_req */ |
913a571a | 2247 | io_fill_cqe_req(link, -ECANCELED, 0); |
91c2f697 | 2248 | io_put_req_deferred(link); |
d4729fbd | 2249 | return true; |
c9abd7ad PB |
2250 | } |
2251 | } | |
d4729fbd | 2252 | return false; |
7c86ffee PB |
2253 | } |
2254 | ||
d148ca4b | 2255 | static void io_fail_links(struct io_kiocb *req) |
33cc89a9 | 2256 | __must_hold(&req->ctx->completion_lock) |
9e645e11 | 2257 | { |
33cc89a9 | 2258 | struct io_kiocb *nxt, *link = req->link; |
04c76b41 | 2259 | bool ignore_cqes = req->flags & REQ_F_SKIP_LINK_CQES; |
9e645e11 | 2260 | |
f2f87370 | 2261 | req->link = NULL; |
f2f87370 | 2262 | while (link) { |
a8295b98 HX |
2263 | long res = -ECANCELED; |
2264 | ||
2265 | if (link->flags & REQ_F_FAIL) | |
2266 | res = link->result; | |
2267 | ||
f2f87370 PB |
2268 | nxt = link->link; |
2269 | link->link = NULL; | |
2665abfd | 2270 | |
502c87d6 SR |
2271 | trace_io_uring_fail_link(req->ctx, req, req->user_data, |
2272 | req->opcode, link); | |
2273 | ||
04c76b41 PB |
2274 | if (!ignore_cqes) { |
2275 | link->flags &= ~REQ_F_CQE_SKIP; | |
2276 | io_fill_cqe_req(link, res, 0); | |
2277 | } | |
91c2f697 | 2278 | io_put_req_deferred(link); |
f2f87370 | 2279 | link = nxt; |
9e645e11 | 2280 | } |
33cc89a9 | 2281 | } |
9e645e11 | 2282 | |
33cc89a9 PB |
2283 | static bool io_disarm_next(struct io_kiocb *req) |
2284 | __must_hold(&req->ctx->completion_lock) | |
2285 | { | |
2286 | bool posted = false; | |
2287 | ||
0756a869 PB |
2288 | if (req->flags & REQ_F_ARM_LTIMEOUT) { |
2289 | struct io_kiocb *link = req->link; | |
2290 | ||
906c6caa | 2291 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
0756a869 PB |
2292 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
2293 | io_remove_next_linked(req); | |
04c76b41 | 2294 | /* leave REQ_F_CQE_SKIP to io_fill_cqe_req */ |
913a571a | 2295 | io_fill_cqe_req(link, -ECANCELED, 0); |
0756a869 PB |
2296 | io_put_req_deferred(link); |
2297 | posted = true; | |
2298 | } | |
2299 | } else if (req->flags & REQ_F_LINK_TIMEOUT) { | |
89b263f6 JA |
2300 | struct io_ring_ctx *ctx = req->ctx; |
2301 | ||
2302 | spin_lock_irq(&ctx->timeout_lock); | |
33cc89a9 | 2303 | posted = io_kill_linked_timeout(req); |
89b263f6 JA |
2304 | spin_unlock_irq(&ctx->timeout_lock); |
2305 | } | |
93d2bcd2 | 2306 | if (unlikely((req->flags & REQ_F_FAIL) && |
e4335ed3 | 2307 | !(req->flags & REQ_F_HARDLINK))) { |
33cc89a9 PB |
2308 | posted |= (req->link != NULL); |
2309 | io_fail_links(req); | |
2310 | } | |
2311 | return posted; | |
9e645e11 JA |
2312 | } |
2313 | ||
d81499bf PB |
2314 | static void __io_req_find_next_prep(struct io_kiocb *req) |
2315 | { | |
2316 | struct io_ring_ctx *ctx = req->ctx; | |
2317 | bool posted; | |
2318 | ||
2319 | spin_lock(&ctx->completion_lock); | |
2320 | posted = io_disarm_next(req); | |
2321 | if (posted) | |
33ce2aff | 2322 | io_commit_cqring(ctx); |
d81499bf PB |
2323 | spin_unlock(&ctx->completion_lock); |
2324 | if (posted) | |
2325 | io_cqring_ev_posted(ctx); | |
2326 | } | |
2327 | ||
2328 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) | |
c69f8dbe | 2329 | { |
33cc89a9 | 2330 | struct io_kiocb *nxt; |
944e58bf | 2331 | |
d81499bf PB |
2332 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
2333 | return NULL; | |
9e645e11 JA |
2334 | /* |
2335 | * If LINK is set, we have dependent requests in this chain. If we | |
2336 | * didn't fail this request, queue the first one up, moving any other | |
2337 | * dependencies to the next request. In case of failure, fail the rest | |
2338 | * of the chain. | |
2339 | */ | |
d81499bf PB |
2340 | if (unlikely(req->flags & IO_DISARM_MASK)) |
2341 | __io_req_find_next_prep(req); | |
33cc89a9 PB |
2342 | nxt = req->link; |
2343 | req->link = NULL; | |
2344 | return nxt; | |
4d7dd462 | 2345 | } |
9e645e11 | 2346 | |
f237c30a | 2347 | static void ctx_flush_and_put(struct io_ring_ctx *ctx, bool *locked) |
2c32395d PB |
2348 | { |
2349 | if (!ctx) | |
2350 | return; | |
f237c30a | 2351 | if (*locked) { |
c450178d | 2352 | io_submit_flush_completions(ctx); |
2c32395d | 2353 | mutex_unlock(&ctx->uring_lock); |
f237c30a | 2354 | *locked = false; |
2c32395d PB |
2355 | } |
2356 | percpu_ref_put(&ctx->refs); | |
2357 | } | |
2358 | ||
f28c240e HX |
2359 | static inline void ctx_commit_and_unlock(struct io_ring_ctx *ctx) |
2360 | { | |
2361 | io_commit_cqring(ctx); | |
2362 | spin_unlock(&ctx->completion_lock); | |
2363 | io_cqring_ev_posted(ctx); | |
2364 | } | |
2365 | ||
2366 | static void handle_prev_tw_list(struct io_wq_work_node *node, | |
2367 | struct io_ring_ctx **ctx, bool *uring_locked) | |
2368 | { | |
2369 | if (*ctx && !*uring_locked) | |
2370 | spin_lock(&(*ctx)->completion_lock); | |
2371 | ||
2372 | do { | |
2373 | struct io_wq_work_node *next = node->next; | |
2374 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2375 | io_task_work.node); | |
2376 | ||
2377 | if (req->ctx != *ctx) { | |
2378 | if (unlikely(!*uring_locked && *ctx)) | |
2379 | ctx_commit_and_unlock(*ctx); | |
2380 | ||
2381 | ctx_flush_and_put(*ctx, uring_locked); | |
2382 | *ctx = req->ctx; | |
2383 | /* if not contended, grab and improve batching */ | |
2384 | *uring_locked = mutex_trylock(&(*ctx)->uring_lock); | |
2385 | percpu_ref_get(&(*ctx)->refs); | |
2386 | if (unlikely(!*uring_locked)) | |
2387 | spin_lock(&(*ctx)->completion_lock); | |
2388 | } | |
2389 | if (likely(*uring_locked)) | |
2390 | req->io_task_work.func(req, uring_locked); | |
2391 | else | |
cc3cec83 JA |
2392 | __io_req_complete_post(req, req->result, |
2393 | io_put_kbuf_comp(req)); | |
f28c240e HX |
2394 | node = next; |
2395 | } while (node); | |
2396 | ||
2397 | if (unlikely(!*uring_locked)) | |
2398 | ctx_commit_and_unlock(*ctx); | |
2399 | } | |
2400 | ||
2401 | static void handle_tw_list(struct io_wq_work_node *node, | |
2402 | struct io_ring_ctx **ctx, bool *locked) | |
9f8d032a HX |
2403 | { |
2404 | do { | |
2405 | struct io_wq_work_node *next = node->next; | |
2406 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2407 | io_task_work.node); | |
2408 | ||
2409 | if (req->ctx != *ctx) { | |
2410 | ctx_flush_and_put(*ctx, locked); | |
2411 | *ctx = req->ctx; | |
2412 | /* if not contended, grab and improve batching */ | |
2413 | *locked = mutex_trylock(&(*ctx)->uring_lock); | |
2414 | percpu_ref_get(&(*ctx)->refs); | |
2415 | } | |
2416 | req->io_task_work.func(req, locked); | |
2417 | node = next; | |
2418 | } while (node); | |
2419 | } | |
2420 | ||
7cbf1722 | 2421 | static void tctx_task_work(struct callback_head *cb) |
c40f6379 | 2422 | { |
f28c240e | 2423 | bool uring_locked = false; |
ebd0df2e | 2424 | struct io_ring_ctx *ctx = NULL; |
3f18407d PB |
2425 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, |
2426 | task_work); | |
c40f6379 | 2427 | |
16f72070 | 2428 | while (1) { |
f28c240e | 2429 | struct io_wq_work_node *node1, *node2; |
3f18407d | 2430 | |
f28c240e HX |
2431 | if (!tctx->task_list.first && |
2432 | !tctx->prior_task_list.first && uring_locked) | |
8d4ad41e PB |
2433 | io_submit_flush_completions(ctx); |
2434 | ||
3f18407d | 2435 | spin_lock_irq(&tctx->task_lock); |
f28c240e HX |
2436 | node1 = tctx->prior_task_list.first; |
2437 | node2 = tctx->task_list.first; | |
3f18407d | 2438 | INIT_WQ_LIST(&tctx->task_list); |
f28c240e HX |
2439 | INIT_WQ_LIST(&tctx->prior_task_list); |
2440 | if (!node2 && !node1) | |
6294f368 | 2441 | tctx->task_running = false; |
3f18407d | 2442 | spin_unlock_irq(&tctx->task_lock); |
f28c240e | 2443 | if (!node2 && !node1) |
6294f368 | 2444 | break; |
3f18407d | 2445 | |
f28c240e HX |
2446 | if (node1) |
2447 | handle_prev_tw_list(node1, &ctx, &uring_locked); | |
6294f368 | 2448 | |
f28c240e HX |
2449 | if (node2) |
2450 | handle_tw_list(node2, &ctx, &uring_locked); | |
7cbf1722 | 2451 | cond_resched(); |
3f18407d | 2452 | } |
ebd0df2e | 2453 | |
f28c240e | 2454 | ctx_flush_and_put(ctx, &uring_locked); |
3cc7fdb9 PB |
2455 | |
2456 | /* relaxed read is enough as only the task itself sets ->in_idle */ | |
2457 | if (unlikely(atomic_read(&tctx->in_idle))) | |
2458 | io_uring_drop_tctx_refs(current); | |
7cbf1722 JA |
2459 | } |
2460 | ||
4813c377 | 2461 | static void io_req_task_work_add(struct io_kiocb *req, bool priority) |
7cbf1722 | 2462 | { |
c15b79de | 2463 | struct task_struct *tsk = req->task; |
7cbf1722 | 2464 | struct io_uring_task *tctx = tsk->io_uring; |
c15b79de | 2465 | enum task_work_notify_mode notify; |
e09ee510 | 2466 | struct io_wq_work_node *node; |
0b81e80c | 2467 | unsigned long flags; |
6294f368 | 2468 | bool running; |
7cbf1722 JA |
2469 | |
2470 | WARN_ON_ONCE(!tctx); | |
2471 | ||
0b81e80c | 2472 | spin_lock_irqsave(&tctx->task_lock, flags); |
4813c377 HX |
2473 | if (priority) |
2474 | wq_list_add_tail(&req->io_task_work.node, &tctx->prior_task_list); | |
2475 | else | |
2476 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); | |
6294f368 PB |
2477 | running = tctx->task_running; |
2478 | if (!running) | |
2479 | tctx->task_running = true; | |
0b81e80c | 2480 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
2481 | |
2482 | /* task_work already pending, we're done */ | |
6294f368 | 2483 | if (running) |
e09ee510 | 2484 | return; |
7cbf1722 | 2485 | |
c15b79de PB |
2486 | /* |
2487 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For | |
2488 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're | |
2489 | * processing task_work. There's no reliable way to tell if TWA_RESUME | |
2490 | * will do the job. | |
2491 | */ | |
2492 | notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL; | |
d97ec623 PB |
2493 | if (likely(!task_work_add(tsk, &tctx->task_work, notify))) { |
2494 | if (notify == TWA_NONE) | |
2495 | wake_up_process(tsk); | |
e09ee510 | 2496 | return; |
c15b79de | 2497 | } |
2215bed9 | 2498 | |
0b81e80c | 2499 | spin_lock_irqsave(&tctx->task_lock, flags); |
6294f368 | 2500 | tctx->task_running = false; |
4813c377 | 2501 | node = wq_list_merge(&tctx->prior_task_list, &tctx->task_list); |
0b81e80c | 2502 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 | 2503 | |
e09ee510 PB |
2504 | while (node) { |
2505 | req = container_of(node, struct io_kiocb, io_task_work.node); | |
2506 | node = node->next; | |
2507 | if (llist_add(&req->io_task_work.fallback_node, | |
2508 | &req->ctx->fallback_llist)) | |
2509 | schedule_delayed_work(&req->ctx->fallback_work, 1); | |
2510 | } | |
eab30c4d PB |
2511 | } |
2512 | ||
f237c30a | 2513 | static void io_req_task_cancel(struct io_kiocb *req, bool *locked) |
c40f6379 | 2514 | { |
87ceb6a6 | 2515 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 | 2516 | |
b18a1a45 | 2517 | /* not needed for normal modes, but SQPOLL depends on it */ |
f237c30a | 2518 | io_tw_lock(ctx, locked); |
2593553a | 2519 | io_req_complete_failed(req, req->result); |
c40f6379 JA |
2520 | } |
2521 | ||
f237c30a | 2522 | static void io_req_task_submit(struct io_kiocb *req, bool *locked) |
c40f6379 JA |
2523 | { |
2524 | struct io_ring_ctx *ctx = req->ctx; | |
2525 | ||
f237c30a | 2526 | io_tw_lock(ctx, locked); |
316319e8 | 2527 | /* req->task == current here, checking PF_EXITING is safe */ |
af066f31 | 2528 | if (likely(!(req->task->flags & PF_EXITING))) |
c5eef2b9 | 2529 | __io_queue_sqe(req); |
81b6d05c | 2530 | else |
2593553a | 2531 | io_req_complete_failed(req, -EFAULT); |
c40f6379 JA |
2532 | } |
2533 | ||
2c4b8eb6 | 2534 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
c40f6379 | 2535 | { |
2c4b8eb6 | 2536 | req->result = ret; |
5b0a6acc | 2537 | req->io_task_work.func = io_req_task_cancel; |
4813c377 | 2538 | io_req_task_work_add(req, false); |
c40f6379 JA |
2539 | } |
2540 | ||
2c4b8eb6 | 2541 | static void io_req_task_queue(struct io_kiocb *req) |
a3df7698 | 2542 | { |
5b0a6acc | 2543 | req->io_task_work.func = io_req_task_submit; |
4813c377 | 2544 | io_req_task_work_add(req, false); |
a3df7698 PB |
2545 | } |
2546 | ||
773af691 JA |
2547 | static void io_req_task_queue_reissue(struct io_kiocb *req) |
2548 | { | |
2549 | req->io_task_work.func = io_queue_async_work; | |
4813c377 | 2550 | io_req_task_work_add(req, false); |
773af691 JA |
2551 | } |
2552 | ||
f2f87370 | 2553 | static inline void io_queue_next(struct io_kiocb *req) |
c69f8dbe | 2554 | { |
9b5f7bd9 | 2555 | struct io_kiocb *nxt = io_req_find_next(req); |
944e58bf PB |
2556 | |
2557 | if (nxt) | |
906a8c3f | 2558 | io_req_task_queue(nxt); |
c69f8dbe JL |
2559 | } |
2560 | ||
c3524383 | 2561 | static void io_free_req(struct io_kiocb *req) |
7a743e22 | 2562 | { |
c3524383 PB |
2563 | io_queue_next(req); |
2564 | __io_free_req(req); | |
2565 | } | |
8766dd51 | 2566 | |
f237c30a PB |
2567 | static void io_free_req_work(struct io_kiocb *req, bool *locked) |
2568 | { | |
2569 | io_free_req(req); | |
2570 | } | |
2571 | ||
3aa83bfb | 2572 | static void io_free_batch_list(struct io_ring_ctx *ctx, |
1cce17ac | 2573 | struct io_wq_work_node *node) |
3aa83bfb | 2574 | __must_hold(&ctx->uring_lock) |
5af1d13e | 2575 | { |
d4b7a5ef | 2576 | struct task_struct *task = NULL; |
37f0e767 | 2577 | int task_refs = 0; |
5af1d13e | 2578 | |
3aa83bfb PB |
2579 | do { |
2580 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2581 | comp_list); | |
2d6500d4 | 2582 | |
def77acf | 2583 | if (unlikely(req->flags & REQ_F_REFCOUNT)) { |
c1e53a69 | 2584 | node = req->comp_list.next; |
def77acf PB |
2585 | if (!req_ref_put_and_test(req)) |
2586 | continue; | |
c1e53a69 | 2587 | } |
2d6500d4 | 2588 | |
ab409402 | 2589 | io_req_put_rsrc_locked(req, ctx); |
d4b7a5ef PB |
2590 | io_queue_next(req); |
2591 | io_dismantle_req(req); | |
5af1d13e | 2592 | |
d4b7a5ef PB |
2593 | if (req->task != task) { |
2594 | if (task) | |
2595 | io_put_task(task, task_refs); | |
2596 | task = req->task; | |
2597 | task_refs = 0; | |
2598 | } | |
2599 | task_refs++; | |
c1e53a69 | 2600 | node = req->comp_list.next; |
d4b7a5ef | 2601 | wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list); |
3aa83bfb | 2602 | } while (node); |
d4b7a5ef | 2603 | |
d4b7a5ef PB |
2604 | if (task) |
2605 | io_put_task(task, task_refs); | |
7a743e22 PB |
2606 | } |
2607 | ||
c450178d | 2608 | static void __io_submit_flush_completions(struct io_ring_ctx *ctx) |
a141dd89 | 2609 | __must_hold(&ctx->uring_lock) |
905c172f | 2610 | { |
6f33b0bc | 2611 | struct io_wq_work_node *node, *prev; |
cd0ca2e0 | 2612 | struct io_submit_state *state = &ctx->submit_state; |
905c172f | 2613 | |
3d4aeb9f PB |
2614 | if (state->flush_cqes) { |
2615 | spin_lock(&ctx->completion_lock); | |
2616 | wq_list_for_each(node, prev, &state->compl_reqs) { | |
2617 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
6f33b0bc | 2618 | comp_list); |
5182ed2e | 2619 | |
3d4aeb9f | 2620 | if (!(req->flags & REQ_F_CQE_SKIP)) |
d5ec1dfa | 2621 | __io_fill_cqe(req, req->result, req->cflags); |
3d4aeb9f PB |
2622 | } |
2623 | ||
2624 | io_commit_cqring(ctx); | |
2625 | spin_unlock(&ctx->completion_lock); | |
2626 | io_cqring_ev_posted(ctx); | |
2627 | state->flush_cqes = false; | |
905c172f | 2628 | } |
5182ed2e | 2629 | |
1cce17ac | 2630 | io_free_batch_list(ctx, state->compl_reqs.first); |
6f33b0bc | 2631 | INIT_WQ_LIST(&state->compl_reqs); |
7a743e22 PB |
2632 | } |
2633 | ||
ba816ad6 JA |
2634 | /* |
2635 | * Drop reference to request, return next in chain (if there is one) if this | |
2636 | * was the last reference to this request. | |
2637 | */ | |
0d85035a | 2638 | static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
e65ef56d | 2639 | { |
9b5f7bd9 PB |
2640 | struct io_kiocb *nxt = NULL; |
2641 | ||
de9b4cca | 2642 | if (req_ref_put_and_test(req)) { |
9b5f7bd9 | 2643 | nxt = io_req_find_next(req); |
4d7dd462 | 2644 | __io_free_req(req); |
2a44f467 | 2645 | } |
9b5f7bd9 | 2646 | return nxt; |
2b188cc1 JA |
2647 | } |
2648 | ||
0d85035a | 2649 | static inline void io_put_req(struct io_kiocb *req) |
e65ef56d | 2650 | { |
de9b4cca | 2651 | if (req_ref_put_and_test(req)) |
e65ef56d | 2652 | io_free_req(req); |
2b188cc1 JA |
2653 | } |
2654 | ||
91c2f697 | 2655 | static inline void io_put_req_deferred(struct io_kiocb *req) |
216578e5 | 2656 | { |
91c2f697 | 2657 | if (req_ref_put_and_test(req)) { |
f237c30a | 2658 | req->io_task_work.func = io_free_req_work; |
4813c377 | 2659 | io_req_task_work_add(req, false); |
543af3a1 | 2660 | } |
216578e5 PB |
2661 | } |
2662 | ||
6c503150 | 2663 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
a3a0e43f JA |
2664 | { |
2665 | /* See comment at the top of this file */ | |
2666 | smp_rmb(); | |
e23de15f | 2667 | return __io_cqring_events(ctx); |
a3a0e43f JA |
2668 | } |
2669 | ||
fb5ccc98 PB |
2670 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
2671 | { | |
2672 | struct io_rings *rings = ctx->rings; | |
2673 | ||
2674 | /* make sure SQ entry isn't read before tail */ | |
2675 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
2676 | } | |
2677 | ||
4c6e277c JA |
2678 | static inline bool io_run_task_work(void) |
2679 | { | |
ef98eb04 | 2680 | if (test_thread_flag(TIF_NOTIFY_SIGNAL) || current->task_works) { |
4c6e277c | 2681 | __set_current_state(TASK_RUNNING); |
ef98eb04 | 2682 | tracehook_notify_signal(); |
4c6e277c JA |
2683 | return true; |
2684 | } | |
2685 | ||
2686 | return false; | |
bcda7baa JA |
2687 | } |
2688 | ||
5ba3c874 | 2689 | static int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin) |
def596e9 | 2690 | { |
5eef4e87 | 2691 | struct io_wq_work_node *pos, *start, *prev; |
d729cf9a | 2692 | unsigned int poll_flags = BLK_POLL_NOSLEEP; |
b688f11e | 2693 | DEFINE_IO_COMP_BATCH(iob); |
5ba3c874 | 2694 | int nr_events = 0; |
def596e9 JA |
2695 | |
2696 | /* | |
2697 | * Only spin for completions if we don't have multiple devices hanging | |
87a115fb | 2698 | * off our complete list. |
def596e9 | 2699 | */ |
87a115fb | 2700 | if (ctx->poll_multi_queue || force_nonspin) |
ef99b2d3 | 2701 | poll_flags |= BLK_POLL_ONESHOT; |
def596e9 | 2702 | |
5eef4e87 PB |
2703 | wq_list_for_each(pos, start, &ctx->iopoll_list) { |
2704 | struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); | |
9adbd45d | 2705 | struct kiocb *kiocb = &req->rw.kiocb; |
a2416e1e | 2706 | int ret; |
def596e9 JA |
2707 | |
2708 | /* | |
581f9810 BM |
2709 | * Move completed and retryable entries to our local lists. |
2710 | * If we find a request that requires polling, break out | |
2711 | * and complete those lists first, if we have entries there. | |
def596e9 | 2712 | */ |
e3f721e6 | 2713 | if (READ_ONCE(req->iopoll_completed)) |
def596e9 JA |
2714 | break; |
2715 | ||
b688f11e | 2716 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, &iob, poll_flags); |
a2416e1e PB |
2717 | if (unlikely(ret < 0)) |
2718 | return ret; | |
2719 | else if (ret) | |
ef99b2d3 | 2720 | poll_flags |= BLK_POLL_ONESHOT; |
def596e9 | 2721 | |
3aadc23e | 2722 | /* iopoll may have completed current req */ |
b688f11e JA |
2723 | if (!rq_list_empty(iob.req_list) || |
2724 | READ_ONCE(req->iopoll_completed)) | |
e3f721e6 | 2725 | break; |
def596e9 JA |
2726 | } |
2727 | ||
b688f11e JA |
2728 | if (!rq_list_empty(iob.req_list)) |
2729 | iob.complete(&iob); | |
5eef4e87 PB |
2730 | else if (!pos) |
2731 | return 0; | |
def596e9 | 2732 | |
5eef4e87 PB |
2733 | prev = start; |
2734 | wq_list_for_each_resume(pos, prev) { | |
2735 | struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); | |
2736 | ||
b3fa03fd PB |
2737 | /* order with io_complete_rw_iopoll(), e.g. ->result updates */ |
2738 | if (!smp_load_acquire(&req->iopoll_completed)) | |
e3f721e6 | 2739 | break; |
83a13a41 PB |
2740 | if (unlikely(req->flags & REQ_F_CQE_SKIP)) |
2741 | continue; | |
d1fd1c20 | 2742 | |
cc3cec83 | 2743 | __io_fill_cqe(req, req->result, io_put_kbuf(req, 0)); |
e3f721e6 PB |
2744 | nr_events++; |
2745 | } | |
def596e9 | 2746 | |
f5ed3bcd PB |
2747 | if (unlikely(!nr_events)) |
2748 | return 0; | |
2749 | ||
2750 | io_commit_cqring(ctx); | |
2751 | io_cqring_ev_posted_iopoll(ctx); | |
1cce17ac | 2752 | pos = start ? start->next : ctx->iopoll_list.first; |
5eef4e87 | 2753 | wq_list_cut(&ctx->iopoll_list, prev, start); |
1cce17ac | 2754 | io_free_batch_list(ctx, pos); |
5ba3c874 | 2755 | return nr_events; |
def596e9 JA |
2756 | } |
2757 | ||
def596e9 JA |
2758 | /* |
2759 | * We can't just wait for polled events to come to us, we have to actively | |
2760 | * find and complete them. | |
2761 | */ | |
c072481d | 2762 | static __cold void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
def596e9 JA |
2763 | { |
2764 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
2765 | return; | |
2766 | ||
2767 | mutex_lock(&ctx->uring_lock); | |
5eef4e87 | 2768 | while (!wq_list_empty(&ctx->iopoll_list)) { |
b2edc0a7 | 2769 | /* let it sleep and repeat later if can't complete a request */ |
5ba3c874 | 2770 | if (io_do_iopoll(ctx, true) == 0) |
b2edc0a7 | 2771 | break; |
08f5439f JA |
2772 | /* |
2773 | * Ensure we allow local-to-the-cpu processing to take place, | |
2774 | * in this case we need to ensure that we reap all events. | |
3fcee5a6 | 2775 | * Also let task_work, etc. to progress by releasing the mutex |
08f5439f | 2776 | */ |
3fcee5a6 PB |
2777 | if (need_resched()) { |
2778 | mutex_unlock(&ctx->uring_lock); | |
2779 | cond_resched(); | |
2780 | mutex_lock(&ctx->uring_lock); | |
2781 | } | |
def596e9 JA |
2782 | } |
2783 | mutex_unlock(&ctx->uring_lock); | |
2784 | } | |
2785 | ||
7668b92a | 2786 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
def596e9 | 2787 | { |
7668b92a | 2788 | unsigned int nr_events = 0; |
e9979b36 | 2789 | int ret = 0; |
500f9fba | 2790 | |
c7849be9 XW |
2791 | /* |
2792 | * We disallow the app entering submit/complete with polling, but we | |
2793 | * still need to lock the ring to prevent racing with polled issue | |
2794 | * that got punted to a workqueue. | |
2795 | */ | |
2796 | mutex_lock(&ctx->uring_lock); | |
f39c8a5b PB |
2797 | /* |
2798 | * Don't enter poll loop if we already have events pending. | |
2799 | * If we do, we can potentially be spinning for commands that | |
2800 | * already triggered a CQE (eg in error). | |
2801 | */ | |
5ed7a37d | 2802 | if (test_bit(0, &ctx->check_cq_overflow)) |
f39c8a5b PB |
2803 | __io_cqring_overflow_flush(ctx, false); |
2804 | if (io_cqring_events(ctx)) | |
2805 | goto out; | |
def596e9 | 2806 | do { |
500f9fba JA |
2807 | /* |
2808 | * If a submit got punted to a workqueue, we can have the | |
2809 | * application entering polling for a command before it gets | |
2810 | * issued. That app will hold the uring_lock for the duration | |
2811 | * of the poll right here, so we need to take a breather every | |
2812 | * now and then to ensure that the issue has a chance to add | |
2813 | * the poll to the issued list. Otherwise we can spin here | |
2814 | * forever, while the workqueue is stuck trying to acquire the | |
2815 | * very same mutex. | |
2816 | */ | |
5eef4e87 | 2817 | if (wq_list_empty(&ctx->iopoll_list)) { |
8f487ef2 PB |
2818 | u32 tail = ctx->cached_cq_tail; |
2819 | ||
500f9fba | 2820 | mutex_unlock(&ctx->uring_lock); |
4c6e277c | 2821 | io_run_task_work(); |
500f9fba | 2822 | mutex_lock(&ctx->uring_lock); |
def596e9 | 2823 | |
8f487ef2 PB |
2824 | /* some requests don't go through iopoll_list */ |
2825 | if (tail != ctx->cached_cq_tail || | |
5eef4e87 | 2826 | wq_list_empty(&ctx->iopoll_list)) |
e9979b36 | 2827 | break; |
500f9fba | 2828 | } |
5ba3c874 PB |
2829 | ret = io_do_iopoll(ctx, !min); |
2830 | if (ret < 0) | |
2831 | break; | |
2832 | nr_events += ret; | |
2833 | ret = 0; | |
2834 | } while (nr_events < min && !need_resched()); | |
f39c8a5b | 2835 | out: |
500f9fba | 2836 | mutex_unlock(&ctx->uring_lock); |
def596e9 JA |
2837 | return ret; |
2838 | } | |
2839 | ||
491381ce | 2840 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 2841 | { |
491381ce JA |
2842 | /* |
2843 | * Tell lockdep we inherited freeze protection from submission | |
2844 | * thread. | |
2845 | */ | |
2846 | if (req->flags & REQ_F_ISREG) { | |
1c98679d | 2847 | struct super_block *sb = file_inode(req->file)->i_sb; |
2b188cc1 | 2848 | |
1c98679d PB |
2849 | __sb_writers_acquired(sb, SB_FREEZE_WRITE); |
2850 | sb_end_write(sb); | |
2b188cc1 JA |
2851 | } |
2852 | } | |
2853 | ||
b63534c4 | 2854 | #ifdef CONFIG_BLOCK |
dc2a6e9a | 2855 | static bool io_resubmit_prep(struct io_kiocb *req) |
b63534c4 | 2856 | { |
ab454438 | 2857 | struct io_async_rw *rw = req->async_data; |
b63534c4 | 2858 | |
d886e185 | 2859 | if (!req_has_async_data(req)) |
ab454438 | 2860 | return !io_req_prep_async(req); |
538941e2 | 2861 | iov_iter_restore(&rw->s.iter, &rw->s.iter_state); |
ab454438 | 2862 | return true; |
b63534c4 | 2863 | } |
b63534c4 | 2864 | |
3e6a0d3c | 2865 | static bool io_rw_should_reissue(struct io_kiocb *req) |
b63534c4 | 2866 | { |
355afaeb | 2867 | umode_t mode = file_inode(req->file)->i_mode; |
3e6a0d3c | 2868 | struct io_ring_ctx *ctx = req->ctx; |
b63534c4 | 2869 | |
355afaeb JA |
2870 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
2871 | return false; | |
3e6a0d3c JA |
2872 | if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() && |
2873 | !(ctx->flags & IORING_SETUP_IOPOLL))) | |
b63534c4 | 2874 | return false; |
7c977a58 JA |
2875 | /* |
2876 | * If ref is dying, we might be running poll reap from the exit work. | |
2877 | * Don't attempt to reissue from that path, just let it fail with | |
2878 | * -EAGAIN. | |
2879 | */ | |
3e6a0d3c JA |
2880 | if (percpu_ref_is_dying(&ctx->refs)) |
2881 | return false; | |
ef046888 JA |
2882 | /* |
2883 | * Play it safe and assume not safe to re-import and reissue if we're | |
2884 | * not in the original thread group (or in task context). | |
2885 | */ | |
2886 | if (!same_thread_group(req->task, current) || !in_task()) | |
2887 | return false; | |
3e6a0d3c JA |
2888 | return true; |
2889 | } | |
e82ad485 | 2890 | #else |
a1ff1e3f | 2891 | static bool io_resubmit_prep(struct io_kiocb *req) |
e82ad485 JA |
2892 | { |
2893 | return false; | |
2894 | } | |
e82ad485 | 2895 | static bool io_rw_should_reissue(struct io_kiocb *req) |
3e6a0d3c | 2896 | { |
b63534c4 JA |
2897 | return false; |
2898 | } | |
3e6a0d3c | 2899 | #endif |
b63534c4 | 2900 | |
8ef12efe | 2901 | static bool __io_complete_rw_common(struct io_kiocb *req, long res) |
a1d7c393 | 2902 | { |
b65c128f PB |
2903 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) |
2904 | kiocb_end_write(req); | |
258f3a7f | 2905 | if (unlikely(res != req->result)) { |
9532b99b PB |
2906 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && |
2907 | io_rw_should_reissue(req)) { | |
2908 | req->flags |= REQ_F_REISSUE; | |
8ef12efe | 2909 | return true; |
9532b99b | 2910 | } |
93d2bcd2 | 2911 | req_set_fail(req); |
8ef12efe | 2912 | req->result = res; |
9532b99b | 2913 | } |
8ef12efe JA |
2914 | return false; |
2915 | } | |
2916 | ||
cc8e9ba7 | 2917 | static inline void io_req_task_complete(struct io_kiocb *req, bool *locked) |
8ef12efe | 2918 | { |
54daa9b2 | 2919 | int res = req->result; |
126180b9 PB |
2920 | |
2921 | if (*locked) { | |
cc3cec83 | 2922 | io_req_complete_state(req, res, io_put_kbuf(req, 0)); |
fff4e40e | 2923 | io_req_add_compl_list(req); |
126180b9 | 2924 | } else { |
cc3cec83 JA |
2925 | io_req_complete_post(req, res, |
2926 | io_put_kbuf(req, IO_URING_F_UNLOCKED)); | |
126180b9 | 2927 | } |
8ef12efe JA |
2928 | } |
2929 | ||
00f6e68b | 2930 | static void __io_complete_rw(struct io_kiocb *req, long res, |
8ef12efe JA |
2931 | unsigned int issue_flags) |
2932 | { | |
2933 | if (__io_complete_rw_common(req, res)) | |
2934 | return; | |
cc3cec83 JA |
2935 | __io_req_complete(req, issue_flags, req->result, |
2936 | io_put_kbuf(req, issue_flags)); | |
ba816ad6 JA |
2937 | } |
2938 | ||
6b19b766 | 2939 | static void io_complete_rw(struct kiocb *kiocb, long res) |
ba816ad6 | 2940 | { |
9adbd45d | 2941 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 | 2942 | |
8ef12efe JA |
2943 | if (__io_complete_rw_common(req, res)) |
2944 | return; | |
2945 | req->result = res; | |
2946 | req->io_task_work.func = io_req_task_complete; | |
f28c240e | 2947 | io_req_task_work_add(req, !!(req->ctx->flags & IORING_SETUP_SQPOLL)); |
2b188cc1 JA |
2948 | } |
2949 | ||
6b19b766 | 2950 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res) |
def596e9 | 2951 | { |
9adbd45d | 2952 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 2953 | |
491381ce JA |
2954 | if (kiocb->ki_flags & IOCB_WRITE) |
2955 | kiocb_end_write(req); | |
9532b99b | 2956 | if (unlikely(res != req->result)) { |
b66ceaf3 PB |
2957 | if (res == -EAGAIN && io_rw_should_reissue(req)) { |
2958 | req->flags |= REQ_F_REISSUE; | |
2959 | return; | |
9532b99b | 2960 | } |
258f3a7f | 2961 | req->result = res; |
8c130827 | 2962 | } |
bbde017a | 2963 | |
b3fa03fd PB |
2964 | /* order with io_iopoll_complete() checking ->iopoll_completed */ |
2965 | smp_store_release(&req->iopoll_completed, 1); | |
def596e9 JA |
2966 | } |
2967 | ||
2968 | /* | |
2969 | * After the iocb has been issued, it's safe to be found on the poll list. | |
2970 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
f39c8a5b | 2971 | * find it from a io_do_iopoll() thread before the issuer is done |
def596e9 JA |
2972 | * accessing the kiocb cookie. |
2973 | */ | |
9882131c | 2974 | static void io_iopoll_req_issued(struct io_kiocb *req, unsigned int issue_flags) |
def596e9 JA |
2975 | { |
2976 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 2977 | const bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
cb3d8972 PB |
2978 | |
2979 | /* workqueue context doesn't hold uring_lock, grab it now */ | |
3b44b371 | 2980 | if (unlikely(needs_lock)) |
cb3d8972 | 2981 | mutex_lock(&ctx->uring_lock); |
def596e9 JA |
2982 | |
2983 | /* | |
2984 | * Track whether we have multiple files in our lists. This will impact | |
2985 | * how we do polling eventually, not spinning if we're on potentially | |
2986 | * different devices. | |
2987 | */ | |
5eef4e87 | 2988 | if (wq_list_empty(&ctx->iopoll_list)) { |
915b3dde HX |
2989 | ctx->poll_multi_queue = false; |
2990 | } else if (!ctx->poll_multi_queue) { | |
def596e9 JA |
2991 | struct io_kiocb *list_req; |
2992 | ||
5eef4e87 PB |
2993 | list_req = container_of(ctx->iopoll_list.first, struct io_kiocb, |
2994 | comp_list); | |
30da1b45 | 2995 | if (list_req->file != req->file) |
915b3dde | 2996 | ctx->poll_multi_queue = true; |
def596e9 JA |
2997 | } |
2998 | ||
2999 | /* | |
3000 | * For fast devices, IO may have already completed. If it has, add | |
3001 | * it to the front so we find it first. | |
3002 | */ | |
65a6543d | 3003 | if (READ_ONCE(req->iopoll_completed)) |
5eef4e87 | 3004 | wq_list_add_head(&req->comp_list, &ctx->iopoll_list); |
def596e9 | 3005 | else |
5eef4e87 | 3006 | wq_list_add_tail(&req->comp_list, &ctx->iopoll_list); |
bdcd3eab | 3007 | |
3b44b371 | 3008 | if (unlikely(needs_lock)) { |
cb3d8972 PB |
3009 | /* |
3010 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handle | |
3011 | * in sq thread task context or in io worker task context. If | |
3012 | * current task context is sq thread, we don't need to check | |
3013 | * whether should wake up sq thread. | |
3014 | */ | |
3015 | if ((ctx->flags & IORING_SETUP_SQPOLL) && | |
3016 | wq_has_sleeper(&ctx->sq_data->wait)) | |
3017 | wake_up(&ctx->sq_data->wait); | |
3018 | ||
3019 | mutex_unlock(&ctx->uring_lock); | |
3020 | } | |
def596e9 JA |
3021 | } |
3022 | ||
4503b767 JA |
3023 | static bool io_bdev_nowait(struct block_device *bdev) |
3024 | { | |
9ba0d0c8 | 3025 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
4503b767 JA |
3026 | } |
3027 | ||
2b188cc1 JA |
3028 | /* |
3029 | * If we tracked the file through the SCM inflight mechanism, we could support | |
3030 | * any file. For now, just ensure that anything potentially problematic is done | |
3031 | * inline. | |
3032 | */ | |
88459b50 | 3033 | static bool __io_file_supports_nowait(struct file *file, umode_t mode) |
2b188cc1 | 3034 | { |
4503b767 | 3035 | if (S_ISBLK(mode)) { |
4e7b5671 CH |
3036 | if (IS_ENABLED(CONFIG_BLOCK) && |
3037 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) | |
4503b767 JA |
3038 | return true; |
3039 | return false; | |
3040 | } | |
976517f1 | 3041 | if (S_ISSOCK(mode)) |
2b188cc1 | 3042 | return true; |
4503b767 | 3043 | if (S_ISREG(mode)) { |
4e7b5671 CH |
3044 | if (IS_ENABLED(CONFIG_BLOCK) && |
3045 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && | |
4503b767 JA |
3046 | file->f_op != &io_uring_fops) |
3047 | return true; | |
3048 | return false; | |
3049 | } | |
2b188cc1 | 3050 | |
c5b85625 JA |
3051 | /* any ->read/write should understand O_NONBLOCK */ |
3052 | if (file->f_flags & O_NONBLOCK) | |
3053 | return true; | |
35645ac3 | 3054 | return file->f_mode & FMODE_NOWAIT; |
2b188cc1 | 3055 | } |
c5b85625 | 3056 | |
88459b50 PB |
3057 | /* |
3058 | * If we tracked the file through the SCM inflight mechanism, we could support | |
3059 | * any file. For now, just ensure that anything potentially problematic is done | |
3060 | * inline. | |
3061 | */ | |
3062 | static unsigned int io_file_get_flags(struct file *file) | |
3063 | { | |
3064 | umode_t mode = file_inode(file)->i_mode; | |
3065 | unsigned int res = 0; | |
af197f50 | 3066 | |
88459b50 PB |
3067 | if (S_ISREG(mode)) |
3068 | res |= FFS_ISREG; | |
3069 | if (__io_file_supports_nowait(file, mode)) | |
3070 | res |= FFS_NOWAIT; | |
3071 | return res; | |
2b188cc1 JA |
3072 | } |
3073 | ||
35645ac3 | 3074 | static inline bool io_file_supports_nowait(struct io_kiocb *req) |
7b29f92d | 3075 | { |
88459b50 | 3076 | return req->flags & REQ_F_SUPPORT_NOWAIT; |
7b29f92d JA |
3077 | } |
3078 | ||
b9a6b8f9 | 3079 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2b188cc1 | 3080 | { |
def596e9 | 3081 | struct io_ring_ctx *ctx = req->ctx; |
9adbd45d | 3082 | struct kiocb *kiocb = &req->rw.kiocb; |
75c668cd | 3083 | struct file *file = req->file; |
09bb8394 JA |
3084 | unsigned ioprio; |
3085 | int ret; | |
2b188cc1 | 3086 | |
88459b50 PB |
3087 | if (!io_req_ffs_set(req)) |
3088 | req->flags |= io_file_get_flags(file) << REQ_F_SUPPORT_NOWAIT_BIT; | |
491381ce | 3089 | |
2b188cc1 | 3090 | kiocb->ki_pos = READ_ONCE(sqe->off); |
5cb03d63 | 3091 | kiocb->ki_flags = iocb_flags(file); |
3e577dcd PB |
3092 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
3093 | if (unlikely(ret)) | |
3094 | return ret; | |
2b188cc1 | 3095 | |
5d329e12 JA |
3096 | /* |
3097 | * If the file is marked O_NONBLOCK, still allow retry for it if it | |
3098 | * supports async. Otherwise it's impossible to use O_NONBLOCK files | |
3099 | * reliably. If not, or it IOCB_NOWAIT is set, don't retry. | |
3100 | */ | |
3101 | if ((kiocb->ki_flags & IOCB_NOWAIT) || | |
35645ac3 | 3102 | ((file->f_flags & O_NONBLOCK) && !io_file_supports_nowait(req))) |
75c668cd PB |
3103 | req->flags |= REQ_F_NOWAIT; |
3104 | ||
def596e9 | 3105 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
5cb03d63 | 3106 | if (!(kiocb->ki_flags & IOCB_DIRECT) || !file->f_op->iopoll) |
09bb8394 | 3107 | return -EOPNOTSUPP; |
2b188cc1 | 3108 | |
394918eb | 3109 | kiocb->ki_flags |= IOCB_HIPRI | IOCB_ALLOC_CACHE; |
def596e9 | 3110 | kiocb->ki_complete = io_complete_rw_iopoll; |
65a6543d | 3111 | req->iopoll_completed = 0; |
def596e9 | 3112 | } else { |
09bb8394 JA |
3113 | if (kiocb->ki_flags & IOCB_HIPRI) |
3114 | return -EINVAL; | |
def596e9 JA |
3115 | kiocb->ki_complete = io_complete_rw; |
3116 | } | |
9adbd45d | 3117 | |
fb27274a PB |
3118 | ioprio = READ_ONCE(sqe->ioprio); |
3119 | if (ioprio) { | |
3120 | ret = ioprio_check_cap(ioprio); | |
3121 | if (ret) | |
3122 | return ret; | |
3123 | ||
3124 | kiocb->ki_ioprio = ioprio; | |
3125 | } else { | |
3126 | kiocb->ki_ioprio = get_current_ioprio(); | |
eae071c9 PB |
3127 | } |
3128 | ||
578c0ee2 | 3129 | req->imu = NULL; |
3529d8c2 JA |
3130 | req->rw.addr = READ_ONCE(sqe->addr); |
3131 | req->rw.len = READ_ONCE(sqe->len); | |
4f4eeba8 | 3132 | req->buf_index = READ_ONCE(sqe->buf_index); |
2b188cc1 | 3133 | return 0; |
2b188cc1 JA |
3134 | } |
3135 | ||
3136 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
3137 | { | |
3138 | switch (ret) { | |
3139 | case -EIOCBQUEUED: | |
3140 | break; | |
3141 | case -ERESTARTSYS: | |
3142 | case -ERESTARTNOINTR: | |
3143 | case -ERESTARTNOHAND: | |
3144 | case -ERESTART_RESTARTBLOCK: | |
3145 | /* | |
3146 | * We can't just restart the syscall, since previously | |
3147 | * submitted sqes may already be in progress. Just fail this | |
3148 | * IO with EINTR. | |
3149 | */ | |
3150 | ret = -EINTR; | |
df561f66 | 3151 | fallthrough; |
2b188cc1 | 3152 | default: |
6b19b766 | 3153 | kiocb->ki_complete(kiocb, ret); |
2b188cc1 JA |
3154 | } |
3155 | } | |
3156 | ||
b4aec400 | 3157 | static inline loff_t *io_kiocb_update_pos(struct io_kiocb *req) |
d34e1e5b DY |
3158 | { |
3159 | struct kiocb *kiocb = &req->rw.kiocb; | |
b4aec400 | 3160 | bool is_stream = req->file->f_mode & FMODE_STREAM; |
d34e1e5b DY |
3161 | |
3162 | if (kiocb->ki_pos == -1) { | |
b4aec400 | 3163 | if (!is_stream) { |
d34e1e5b DY |
3164 | req->flags |= REQ_F_CUR_POS; |
3165 | kiocb->ki_pos = req->file->f_pos; | |
b4aec400 | 3166 | return &kiocb->ki_pos; |
d34e1e5b DY |
3167 | } else { |
3168 | kiocb->ki_pos = 0; | |
b4aec400 | 3169 | return NULL; |
d34e1e5b DY |
3170 | } |
3171 | } | |
b4aec400 | 3172 | return is_stream ? NULL : &kiocb->ki_pos; |
d34e1e5b DY |
3173 | } |
3174 | ||
2ea537ca | 3175 | static void kiocb_done(struct io_kiocb *req, ssize_t ret, |
889fca73 | 3176 | unsigned int issue_flags) |
ba816ad6 | 3177 | { |
e8c2bc1f | 3178 | struct io_async_rw *io = req->async_data; |
ba04291e | 3179 | |
227c0c96 | 3180 | /* add previously done IO, if any */ |
d886e185 | 3181 | if (req_has_async_data(req) && io->bytes_done > 0) { |
227c0c96 | 3182 | if (ret < 0) |
e8c2bc1f | 3183 | ret = io->bytes_done; |
227c0c96 | 3184 | else |
e8c2bc1f | 3185 | ret += io->bytes_done; |
227c0c96 JA |
3186 | } |
3187 | ||
ba04291e | 3188 | if (req->flags & REQ_F_CUR_POS) |
2ea537ca PB |
3189 | req->file->f_pos = req->rw.kiocb.ki_pos; |
3190 | if (ret >= 0 && (req->rw.kiocb.ki_complete == io_complete_rw)) | |
00f6e68b | 3191 | __io_complete_rw(req, ret, issue_flags); |
ba816ad6 | 3192 | else |
2ea537ca | 3193 | io_rw_done(&req->rw.kiocb, ret); |
97284637 | 3194 | |
b66ceaf3 | 3195 | if (req->flags & REQ_F_REISSUE) { |
97284637 | 3196 | req->flags &= ~REQ_F_REISSUE; |
a7be7c23 | 3197 | if (io_resubmit_prep(req)) { |
773af691 | 3198 | io_req_task_queue_reissue(req); |
8c130827 | 3199 | } else { |
93d2bcd2 | 3200 | req_set_fail(req); |
06bdea20 PB |
3201 | req->result = ret; |
3202 | req->io_task_work.func = io_req_task_complete; | |
4813c377 | 3203 | io_req_task_work_add(req, false); |
97284637 PB |
3204 | } |
3205 | } | |
ba816ad6 JA |
3206 | } |
3207 | ||
eae071c9 PB |
3208 | static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter, |
3209 | struct io_mapped_ubuf *imu) | |
edafccee | 3210 | { |
9adbd45d | 3211 | size_t len = req->rw.len; |
75769e3f | 3212 | u64 buf_end, buf_addr = req->rw.addr; |
edafccee | 3213 | size_t offset; |
edafccee | 3214 | |
75769e3f | 3215 | if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end))) |
edafccee JA |
3216 | return -EFAULT; |
3217 | /* not inside the mapped region */ | |
4751f53d | 3218 | if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end)) |
edafccee JA |
3219 | return -EFAULT; |
3220 | ||
3221 | /* | |
3222 | * May not be a start of buffer, set size appropriately | |
3223 | * and advance us to the beginning. | |
3224 | */ | |
3225 | offset = buf_addr - imu->ubuf; | |
3226 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
3227 | |
3228 | if (offset) { | |
3229 | /* | |
3230 | * Don't use iov_iter_advance() here, as it's really slow for | |
3231 | * using the latter parts of a big fixed buffer - it iterates | |
3232 | * over each segment manually. We can cheat a bit here, because | |
3233 | * we know that: | |
3234 | * | |
3235 | * 1) it's a BVEC iter, we set it up | |
3236 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
3237 | * first and last bvec | |
3238 | * | |
3239 | * So just find our index, and adjust the iterator afterwards. | |
3240 | * If the offset is within the first bvec (or the whole first | |
3241 | * bvec, just use iov_iter_advance(). This makes it easier | |
3242 | * since we can just skip the first segment, which may not | |
3243 | * be PAGE_SIZE aligned. | |
3244 | */ | |
3245 | const struct bio_vec *bvec = imu->bvec; | |
3246 | ||
3247 | if (offset <= bvec->bv_len) { | |
3248 | iov_iter_advance(iter, offset); | |
3249 | } else { | |
3250 | unsigned long seg_skip; | |
3251 | ||
3252 | /* skip first vec */ | |
3253 | offset -= bvec->bv_len; | |
3254 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
3255 | ||
3256 | iter->bvec = bvec + seg_skip; | |
3257 | iter->nr_segs -= seg_skip; | |
99c79f66 | 3258 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 3259 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
3260 | } |
3261 | } | |
3262 | ||
847595de | 3263 | return 0; |
edafccee JA |
3264 | } |
3265 | ||
eae071c9 PB |
3266 | static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter) |
3267 | { | |
eae071c9 PB |
3268 | struct io_mapped_ubuf *imu = req->imu; |
3269 | u16 index, buf_index = req->buf_index; | |
3270 | ||
3271 | if (likely(!imu)) { | |
578c0ee2 PB |
3272 | struct io_ring_ctx *ctx = req->ctx; |
3273 | ||
eae071c9 PB |
3274 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
3275 | return -EFAULT; | |
578c0ee2 | 3276 | io_req_set_rsrc_node(req, ctx); |
eae071c9 PB |
3277 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
3278 | imu = READ_ONCE(ctx->user_bufs[index]); | |
3279 | req->imu = imu; | |
3280 | } | |
3281 | return __io_import_fixed(req, rw, iter, imu); | |
3282 | } | |
3283 | ||
bcda7baa JA |
3284 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
3285 | { | |
3286 | if (needs_lock) | |
3287 | mutex_unlock(&ctx->uring_lock); | |
3288 | } | |
3289 | ||
3290 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) | |
3291 | { | |
3292 | /* | |
3293 | * "Normal" inline submissions always hold the uring_lock, since we | |
3294 | * grab it from the system call. Same is true for the SQPOLL offload. | |
3295 | * The only exception is when we've detached the request and issue it | |
3296 | * from an async worker thread, grab the lock for that case. | |
3297 | */ | |
3298 | if (needs_lock) | |
3299 | mutex_lock(&ctx->uring_lock); | |
3300 | } | |
3301 | ||
3302 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, | |
51aac424 | 3303 | int bgid, unsigned int issue_flags) |
bcda7baa | 3304 | { |
30d51dd4 | 3305 | struct io_buffer *kbuf = req->kbuf; |
bcda7baa | 3306 | struct io_buffer *head; |
3b44b371 | 3307 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
bcda7baa JA |
3308 | |
3309 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
3310 | return kbuf; | |
3311 | ||
3312 | io_ring_submit_lock(req->ctx, needs_lock); | |
3313 | ||
3314 | lockdep_assert_held(&req->ctx->uring_lock); | |
3315 | ||
9e15c3a0 | 3316 | head = xa_load(&req->ctx->io_buffers, bgid); |
bcda7baa JA |
3317 | if (head) { |
3318 | if (!list_empty(&head->list)) { | |
3319 | kbuf = list_last_entry(&head->list, struct io_buffer, | |
3320 | list); | |
3321 | list_del(&kbuf->list); | |
3322 | } else { | |
3323 | kbuf = head; | |
9e15c3a0 | 3324 | xa_erase(&req->ctx->io_buffers, bgid); |
bcda7baa JA |
3325 | } |
3326 | if (*len > kbuf->len) | |
3327 | *len = kbuf->len; | |
30d51dd4 PB |
3328 | req->flags |= REQ_F_BUFFER_SELECTED; |
3329 | req->kbuf = kbuf; | |
bcda7baa JA |
3330 | } else { |
3331 | kbuf = ERR_PTR(-ENOBUFS); | |
3332 | } | |
3333 | ||
3334 | io_ring_submit_unlock(req->ctx, needs_lock); | |
bcda7baa JA |
3335 | return kbuf; |
3336 | } | |
3337 | ||
4d954c25 | 3338 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
51aac424 | 3339 | unsigned int issue_flags) |
4d954c25 JA |
3340 | { |
3341 | struct io_buffer *kbuf; | |
4f4eeba8 | 3342 | u16 bgid; |
4d954c25 | 3343 | |
4f4eeba8 | 3344 | bgid = req->buf_index; |
51aac424 | 3345 | kbuf = io_buffer_select(req, len, bgid, issue_flags); |
4d954c25 JA |
3346 | if (IS_ERR(kbuf)) |
3347 | return kbuf; | |
4d954c25 JA |
3348 | return u64_to_user_ptr(kbuf->addr); |
3349 | } | |
3350 | ||
3351 | #ifdef CONFIG_COMPAT | |
3352 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, | |
51aac424 | 3353 | unsigned int issue_flags) |
4d954c25 JA |
3354 | { |
3355 | struct compat_iovec __user *uiov; | |
3356 | compat_ssize_t clen; | |
3357 | void __user *buf; | |
3358 | ssize_t len; | |
3359 | ||
3360 | uiov = u64_to_user_ptr(req->rw.addr); | |
3361 | if (!access_ok(uiov, sizeof(*uiov))) | |
3362 | return -EFAULT; | |
3363 | if (__get_user(clen, &uiov->iov_len)) | |
3364 | return -EFAULT; | |
3365 | if (clen < 0) | |
3366 | return -EINVAL; | |
3367 | ||
3368 | len = clen; | |
51aac424 | 3369 | buf = io_rw_buffer_select(req, &len, issue_flags); |
4d954c25 JA |
3370 | if (IS_ERR(buf)) |
3371 | return PTR_ERR(buf); | |
3372 | iov[0].iov_base = buf; | |
3373 | iov[0].iov_len = (compat_size_t) len; | |
3374 | return 0; | |
3375 | } | |
3376 | #endif | |
3377 | ||
3378 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
51aac424 | 3379 | unsigned int issue_flags) |
4d954c25 JA |
3380 | { |
3381 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); | |
3382 | void __user *buf; | |
3383 | ssize_t len; | |
3384 | ||
3385 | if (copy_from_user(iov, uiov, sizeof(*uiov))) | |
3386 | return -EFAULT; | |
3387 | ||
3388 | len = iov[0].iov_len; | |
3389 | if (len < 0) | |
3390 | return -EINVAL; | |
51aac424 | 3391 | buf = io_rw_buffer_select(req, &len, issue_flags); |
4d954c25 JA |
3392 | if (IS_ERR(buf)) |
3393 | return PTR_ERR(buf); | |
3394 | iov[0].iov_base = buf; | |
3395 | iov[0].iov_len = len; | |
3396 | return 0; | |
3397 | } | |
3398 | ||
3399 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
51aac424 | 3400 | unsigned int issue_flags) |
4d954c25 | 3401 | { |
dddb3e26 | 3402 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
30d51dd4 | 3403 | struct io_buffer *kbuf = req->kbuf; |
dddb3e26 | 3404 | |
dddb3e26 JA |
3405 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
3406 | iov[0].iov_len = kbuf->len; | |
4d954c25 | 3407 | return 0; |
dddb3e26 | 3408 | } |
dd201662 | 3409 | if (req->rw.len != 1) |
4d954c25 JA |
3410 | return -EINVAL; |
3411 | ||
3412 | #ifdef CONFIG_COMPAT | |
3413 | if (req->ctx->compat) | |
51aac424 | 3414 | return io_compat_import(req, iov, issue_flags); |
4d954c25 JA |
3415 | #endif |
3416 | ||
51aac424 | 3417 | return __io_iov_buffer_select(req, iov, issue_flags); |
4d954c25 JA |
3418 | } |
3419 | ||
caa8fe6e PB |
3420 | static struct iovec *__io_import_iovec(int rw, struct io_kiocb *req, |
3421 | struct io_rw_state *s, | |
3422 | unsigned int issue_flags) | |
2b188cc1 | 3423 | { |
5e49c973 | 3424 | struct iov_iter *iter = &s->iter; |
847595de | 3425 | u8 opcode = req->opcode; |
caa8fe6e | 3426 | struct iovec *iovec; |
d1d681b0 PB |
3427 | void __user *buf; |
3428 | size_t sqe_len; | |
4d954c25 | 3429 | ssize_t ret; |
edafccee | 3430 | |
f3251183 PB |
3431 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
3432 | ret = io_import_fixed(req, rw, iter); | |
3433 | if (ret) | |
3434 | return ERR_PTR(ret); | |
3435 | return NULL; | |
3436 | } | |
2b188cc1 | 3437 | |
bcda7baa | 3438 | /* buffer index only valid with fixed read/write, or buffer select */ |
d1d681b0 | 3439 | if (unlikely(req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))) |
caa8fe6e | 3440 | return ERR_PTR(-EINVAL); |
9adbd45d | 3441 | |
d1d681b0 PB |
3442 | buf = u64_to_user_ptr(req->rw.addr); |
3443 | sqe_len = req->rw.len; | |
9adbd45d | 3444 | |
3a6820f2 | 3445 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
bcda7baa | 3446 | if (req->flags & REQ_F_BUFFER_SELECT) { |
51aac424 | 3447 | buf = io_rw_buffer_select(req, &sqe_len, issue_flags); |
867a23ea | 3448 | if (IS_ERR(buf)) |
898df244 | 3449 | return ERR_CAST(buf); |
3f9d6441 | 3450 | req->rw.len = sqe_len; |
bcda7baa JA |
3451 | } |
3452 | ||
5e49c973 | 3453 | ret = import_single_range(rw, buf, sqe_len, s->fast_iov, iter); |
f3251183 PB |
3454 | if (ret) |
3455 | return ERR_PTR(ret); | |
3456 | return NULL; | |
3a6820f2 JA |
3457 | } |
3458 | ||
caa8fe6e | 3459 | iovec = s->fast_iov; |
4d954c25 | 3460 | if (req->flags & REQ_F_BUFFER_SELECT) { |
caa8fe6e | 3461 | ret = io_iov_buffer_select(req, iovec, issue_flags); |
f3251183 PB |
3462 | if (ret) |
3463 | return ERR_PTR(ret); | |
3464 | iov_iter_init(iter, rw, iovec, 1, iovec->iov_len); | |
3465 | return NULL; | |
4d954c25 JA |
3466 | } |
3467 | ||
caa8fe6e | 3468 | ret = __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, &iovec, iter, |
89cd35c5 | 3469 | req->ctx->compat); |
caa8fe6e PB |
3470 | if (unlikely(ret < 0)) |
3471 | return ERR_PTR(ret); | |
3472 | return iovec; | |
2b188cc1 JA |
3473 | } |
3474 | ||
5e49c973 PB |
3475 | static inline int io_import_iovec(int rw, struct io_kiocb *req, |
3476 | struct iovec **iovec, struct io_rw_state *s, | |
3477 | unsigned int issue_flags) | |
3478 | { | |
caa8fe6e PB |
3479 | *iovec = __io_import_iovec(rw, req, s, issue_flags); |
3480 | if (unlikely(IS_ERR(*iovec))) | |
3481 | return PTR_ERR(*iovec); | |
5e49c973 | 3482 | |
5e49c973 | 3483 | iov_iter_save_state(&s->iter, &s->iter_state); |
caa8fe6e | 3484 | return 0; |
2b188cc1 JA |
3485 | } |
3486 | ||
0fef9483 JA |
3487 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
3488 | { | |
5b09e37e | 3489 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
0fef9483 JA |
3490 | } |
3491 | ||
31b51510 | 3492 | /* |
32960613 JA |
3493 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
3494 | * by looping over ->read() or ->write() manually. | |
31b51510 | 3495 | */ |
4017eb91 | 3496 | static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) |
32960613 | 3497 | { |
4017eb91 JA |
3498 | struct kiocb *kiocb = &req->rw.kiocb; |
3499 | struct file *file = req->file; | |
32960613 | 3500 | ssize_t ret = 0; |
af9c45ec | 3501 | loff_t *ppos; |
32960613 JA |
3502 | |
3503 | /* | |
3504 | * Don't support polled IO through this interface, and we can't | |
3505 | * support non-blocking either. For the latter, this just causes | |
3506 | * the kiocb to be handled from an async context. | |
3507 | */ | |
3508 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3509 | return -EOPNOTSUPP; | |
35645ac3 PB |
3510 | if ((kiocb->ki_flags & IOCB_NOWAIT) && |
3511 | !(kiocb->ki_filp->f_flags & O_NONBLOCK)) | |
32960613 JA |
3512 | return -EAGAIN; |
3513 | ||
af9c45ec DY |
3514 | ppos = io_kiocb_ppos(kiocb); |
3515 | ||
32960613 | 3516 | while (iov_iter_count(iter)) { |
311ae9e1 | 3517 | struct iovec iovec; |
32960613 JA |
3518 | ssize_t nr; |
3519 | ||
311ae9e1 PB |
3520 | if (!iov_iter_is_bvec(iter)) { |
3521 | iovec = iov_iter_iovec(iter); | |
3522 | } else { | |
4017eb91 JA |
3523 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
3524 | iovec.iov_len = req->rw.len; | |
311ae9e1 PB |
3525 | } |
3526 | ||
32960613 JA |
3527 | if (rw == READ) { |
3528 | nr = file->f_op->read(file, iovec.iov_base, | |
af9c45ec | 3529 | iovec.iov_len, ppos); |
32960613 JA |
3530 | } else { |
3531 | nr = file->f_op->write(file, iovec.iov_base, | |
af9c45ec | 3532 | iovec.iov_len, ppos); |
32960613 JA |
3533 | } |
3534 | ||
3535 | if (nr < 0) { | |
3536 | if (!ret) | |
3537 | ret = nr; | |
3538 | break; | |
3539 | } | |
16c8d2df JA |
3540 | if (!iov_iter_is_bvec(iter)) { |
3541 | iov_iter_advance(iter, nr); | |
3542 | } else { | |
3543 | req->rw.len -= nr; | |
3544 | req->rw.addr += nr; | |
3545 | } | |
32960613 JA |
3546 | ret += nr; |
3547 | if (nr != iovec.iov_len) | |
3548 | break; | |
32960613 JA |
3549 | } |
3550 | ||
3551 | return ret; | |
3552 | } | |
3553 | ||
ff6165b2 JA |
3554 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
3555 | const struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 3556 | { |
e8c2bc1f | 3557 | struct io_async_rw *rw = req->async_data; |
b64e3444 | 3558 | |
538941e2 | 3559 | memcpy(&rw->s.iter, iter, sizeof(*iter)); |
afb87658 | 3560 | rw->free_iovec = iovec; |
227c0c96 | 3561 | rw->bytes_done = 0; |
ff6165b2 | 3562 | /* can only be fixed buffers, no need to do anything */ |
9c3a205c | 3563 | if (iov_iter_is_bvec(iter)) |
ff6165b2 | 3564 | return; |
b64e3444 | 3565 | if (!iovec) { |
ff6165b2 JA |
3566 | unsigned iov_off = 0; |
3567 | ||
538941e2 | 3568 | rw->s.iter.iov = rw->s.fast_iov; |
ff6165b2 JA |
3569 | if (iter->iov != fast_iov) { |
3570 | iov_off = iter->iov - fast_iov; | |
538941e2 | 3571 | rw->s.iter.iov += iov_off; |
ff6165b2 | 3572 | } |
538941e2 PB |
3573 | if (rw->s.fast_iov != fast_iov) |
3574 | memcpy(rw->s.fast_iov + iov_off, fast_iov + iov_off, | |
45097dae | 3575 | sizeof(struct iovec) * iter->nr_segs); |
99bc4c38 PB |
3576 | } else { |
3577 | req->flags |= REQ_F_NEED_CLEANUP; | |
f67676d1 JA |
3578 | } |
3579 | } | |
3580 | ||
8d4af685 | 3581 | static inline bool io_alloc_async_data(struct io_kiocb *req) |
3d9932a8 | 3582 | { |
e8c2bc1f JA |
3583 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
3584 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); | |
d886e185 PB |
3585 | if (req->async_data) { |
3586 | req->flags |= REQ_F_ASYNC_DATA; | |
3587 | return false; | |
3588 | } | |
3589 | return true; | |
3d9932a8 XW |
3590 | } |
3591 | ||
ff6165b2 | 3592 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
c88598a9 | 3593 | struct io_rw_state *s, bool force) |
b7bb4f7d | 3594 | { |
26f0505a | 3595 | if (!force && !io_op_defs[req->opcode].needs_async_setup) |
74566df3 | 3596 | return 0; |
d886e185 | 3597 | if (!req_has_async_data(req)) { |
cd658695 JA |
3598 | struct io_async_rw *iorw; |
3599 | ||
6cb78689 | 3600 | if (io_alloc_async_data(req)) { |
6bf985dc | 3601 | kfree(iovec); |
5d204bcf | 3602 | return -ENOMEM; |
6bf985dc | 3603 | } |
b7bb4f7d | 3604 | |
c88598a9 | 3605 | io_req_map_rw(req, iovec, s->fast_iov, &s->iter); |
cd658695 JA |
3606 | iorw = req->async_data; |
3607 | /* we've copied and mapped the iter, ensure state is saved */ | |
538941e2 | 3608 | iov_iter_save_state(&iorw->s.iter, &iorw->s.iter_state); |
5d204bcf | 3609 | } |
b7bb4f7d | 3610 | return 0; |
f67676d1 JA |
3611 | } |
3612 | ||
73debe68 | 3613 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw) |
c3e330a4 | 3614 | { |
e8c2bc1f | 3615 | struct io_async_rw *iorw = req->async_data; |
5e49c973 | 3616 | struct iovec *iov; |
847595de | 3617 | int ret; |
c3e330a4 | 3618 | |
51aac424 | 3619 | /* submission path, ->uring_lock should already be taken */ |
3b44b371 | 3620 | ret = io_import_iovec(rw, req, &iov, &iorw->s, 0); |
c3e330a4 PB |
3621 | if (unlikely(ret < 0)) |
3622 | return ret; | |
3623 | ||
ab0b196c PB |
3624 | iorw->bytes_done = 0; |
3625 | iorw->free_iovec = iov; | |
3626 | if (iov) | |
3627 | req->flags |= REQ_F_NEED_CLEANUP; | |
c3e330a4 PB |
3628 | return 0; |
3629 | } | |
3630 | ||
73debe68 | 3631 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3632 | { |
3529d8c2 JA |
3633 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
3634 | return -EBADF; | |
b9a6b8f9 | 3635 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3636 | } |
3637 | ||
c1dd91d1 | 3638 | /* |
ffdc8dab | 3639 | * This is our waitqueue callback handler, registered through __folio_lock_async() |
c1dd91d1 JA |
3640 | * when we initially tried to do the IO with the iocb armed our waitqueue. |
3641 | * This gets called when the page is unlocked, and we generally expect that to | |
3642 | * happen when the page IO is completed and the page is now uptodate. This will | |
3643 | * queue a task_work based retry of the operation, attempting to copy the data | |
3644 | * again. If the latter fails because the page was NOT uptodate, then we will | |
3645 | * do a thread based blocking retry of the operation. That's the unexpected | |
3646 | * slow path. | |
3647 | */ | |
bcf5a063 JA |
3648 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
3649 | int sync, void *arg) | |
3650 | { | |
3651 | struct wait_page_queue *wpq; | |
3652 | struct io_kiocb *req = wait->private; | |
bcf5a063 | 3653 | struct wait_page_key *key = arg; |
bcf5a063 JA |
3654 | |
3655 | wpq = container_of(wait, struct wait_page_queue, wait); | |
3656 | ||
cdc8fcb4 LT |
3657 | if (!wake_page_match(wpq, key)) |
3658 | return 0; | |
3659 | ||
c8d317aa | 3660 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
bcf5a063 | 3661 | list_del_init(&wait->entry); |
921b9054 | 3662 | io_req_task_queue(req); |
bcf5a063 JA |
3663 | return 1; |
3664 | } | |
3665 | ||
c1dd91d1 JA |
3666 | /* |
3667 | * This controls whether a given IO request should be armed for async page | |
3668 | * based retry. If we return false here, the request is handed to the async | |
3669 | * worker threads for retry. If we're doing buffered reads on a regular file, | |
3670 | * we prepare a private wait_page_queue entry and retry the operation. This | |
3671 | * will either succeed because the page is now uptodate and unlocked, or it | |
3672 | * will register a callback when the page is unlocked at IO completion. Through | |
3673 | * that callback, io_uring uses task_work to setup a retry of the operation. | |
3674 | * That retry will attempt the buffered read again. The retry will generally | |
3675 | * succeed, or in rare cases where it fails, we then fall back to using the | |
3676 | * async worker threads for a blocking retry. | |
3677 | */ | |
227c0c96 | 3678 | static bool io_rw_should_retry(struct io_kiocb *req) |
f67676d1 | 3679 | { |
e8c2bc1f JA |
3680 | struct io_async_rw *rw = req->async_data; |
3681 | struct wait_page_queue *wait = &rw->wpq; | |
bcf5a063 | 3682 | struct kiocb *kiocb = &req->rw.kiocb; |
f67676d1 | 3683 | |
bcf5a063 JA |
3684 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
3685 | if (req->flags & REQ_F_NOWAIT) | |
3686 | return false; | |
f67676d1 | 3687 | |
227c0c96 | 3688 | /* Only for buffered IO */ |
3b2a4439 | 3689 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
bcf5a063 | 3690 | return false; |
3b2a4439 | 3691 | |
bcf5a063 JA |
3692 | /* |
3693 | * just use poll if we can, and don't attempt if the fs doesn't | |
3694 | * support callback based unlocks | |
3695 | */ | |
3696 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) | |
3697 | return false; | |
f67676d1 | 3698 | |
3b2a4439 JA |
3699 | wait->wait.func = io_async_buf_func; |
3700 | wait->wait.private = req; | |
3701 | wait->wait.flags = 0; | |
3702 | INIT_LIST_HEAD(&wait->wait.entry); | |
3703 | kiocb->ki_flags |= IOCB_WAITQ; | |
c8d317aa | 3704 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3b2a4439 | 3705 | kiocb->ki_waitq = wait; |
3b2a4439 | 3706 | return true; |
bcf5a063 JA |
3707 | } |
3708 | ||
aeab9506 | 3709 | static inline int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) |
bcf5a063 | 3710 | { |
607b6fb8 | 3711 | if (likely(req->file->f_op->read_iter)) |
bcf5a063 | 3712 | return call_read_iter(req->file, &req->rw.kiocb, iter); |
2dd2111d | 3713 | else if (req->file->f_op->read) |
4017eb91 | 3714 | return loop_rw_iter(READ, req, iter); |
2dd2111d GH |
3715 | else |
3716 | return -EINVAL; | |
f67676d1 JA |
3717 | } |
3718 | ||
7db30437 ML |
3719 | static bool need_read_all(struct io_kiocb *req) |
3720 | { | |
3721 | return req->flags & REQ_F_ISREG || | |
3722 | S_ISBLK(file_inode(req->file)->i_mode); | |
3723 | } | |
3724 | ||
889fca73 | 3725 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 3726 | { |
607b6fb8 | 3727 | struct io_rw_state __s, *s = &__s; |
c88598a9 | 3728 | struct iovec *iovec; |
9adbd45d | 3729 | struct kiocb *kiocb = &req->rw.kiocb; |
45d189c6 | 3730 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
d886e185 | 3731 | struct io_async_rw *rw; |
cd658695 | 3732 | ssize_t ret, ret2; |
b4aec400 | 3733 | loff_t *ppos; |
ff6165b2 | 3734 | |
607b6fb8 PB |
3735 | if (!req_has_async_data(req)) { |
3736 | ret = io_import_iovec(READ, req, &iovec, s, issue_flags); | |
3737 | if (unlikely(ret < 0)) | |
3738 | return ret; | |
3739 | } else { | |
2be2eb02 JA |
3740 | /* |
3741 | * Safe and required to re-import if we're using provided | |
3742 | * buffers, as we dropped the selected one before retry. | |
3743 | */ | |
3744 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
3745 | ret = io_import_iovec(READ, req, &iovec, s, issue_flags); | |
3746 | if (unlikely(ret < 0)) | |
3747 | return ret; | |
3748 | } | |
3749 | ||
d886e185 | 3750 | rw = req->async_data; |
c88598a9 | 3751 | s = &rw->s; |
cd658695 JA |
3752 | /* |
3753 | * We come here from an earlier attempt, restore our state to | |
3754 | * match in case it doesn't. It's cheap enough that we don't | |
3755 | * need to make this conditional. | |
3756 | */ | |
c88598a9 | 3757 | iov_iter_restore(&s->iter, &s->iter_state); |
2846c481 | 3758 | iovec = NULL; |
2846c481 | 3759 | } |
c88598a9 | 3760 | req->result = iov_iter_count(&s->iter); |
2b188cc1 | 3761 | |
607b6fb8 PB |
3762 | if (force_nonblock) { |
3763 | /* If the file doesn't support async, just async punt */ | |
35645ac3 | 3764 | if (unlikely(!io_file_supports_nowait(req))) { |
607b6fb8 PB |
3765 | ret = io_setup_async_rw(req, iovec, s, true); |
3766 | return ret ?: -EAGAIN; | |
3767 | } | |
a88fc400 | 3768 | kiocb->ki_flags |= IOCB_NOWAIT; |
607b6fb8 PB |
3769 | } else { |
3770 | /* Ensure we clear previously set non-block flag */ | |
3771 | kiocb->ki_flags &= ~IOCB_NOWAIT; | |
6713e7a6 | 3772 | } |
9e645e11 | 3773 | |
b4aec400 | 3774 | ppos = io_kiocb_update_pos(req); |
d34e1e5b | 3775 | |
b4aec400 | 3776 | ret = rw_verify_area(READ, req->file, ppos, req->result); |
5ea5dd45 PB |
3777 | if (unlikely(ret)) { |
3778 | kfree(iovec); | |
3779 | return ret; | |
3780 | } | |
2b188cc1 | 3781 | |
c88598a9 | 3782 | ret = io_iter_do_read(req, &s->iter); |
32960613 | 3783 | |
230d50d4 | 3784 | if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) { |
6ad7f233 | 3785 | req->flags &= ~REQ_F_REISSUE; |
9af177ee JA |
3786 | /* if we can poll, just do that */ |
3787 | if (req->opcode == IORING_OP_READ && file_can_poll(req->file)) | |
3788 | return -EAGAIN; | |
eefdf30f JA |
3789 | /* IOPOLL retry should happen for io-wq threads */ |
3790 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
f91daf56 | 3791 | goto done; |
75c668cd PB |
3792 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3793 | if (req->flags & REQ_F_NOWAIT) | |
355afaeb | 3794 | goto done; |
f38c7e3a | 3795 | ret = 0; |
230d50d4 JA |
3796 | } else if (ret == -EIOCBQUEUED) { |
3797 | goto out_free; | |
f80a50a6 | 3798 | } else if (ret == req->result || ret <= 0 || !force_nonblock || |
7db30437 | 3799 | (req->flags & REQ_F_NOWAIT) || !need_read_all(req)) { |
7335e3bf | 3800 | /* read all, failed, already did sync or don't want to retry */ |
00d23d51 | 3801 | goto done; |
227c0c96 JA |
3802 | } |
3803 | ||
cd658695 JA |
3804 | /* |
3805 | * Don't depend on the iter state matching what was consumed, or being | |
3806 | * untouched in case of error. Restore it and we'll advance it | |
3807 | * manually if we need to. | |
3808 | */ | |
c88598a9 | 3809 | iov_iter_restore(&s->iter, &s->iter_state); |
cd658695 | 3810 | |
c88598a9 | 3811 | ret2 = io_setup_async_rw(req, iovec, s, true); |
6bf985dc PB |
3812 | if (ret2) |
3813 | return ret2; | |
3814 | ||
fe1cdd55 | 3815 | iovec = NULL; |
e8c2bc1f | 3816 | rw = req->async_data; |
c88598a9 | 3817 | s = &rw->s; |
cd658695 JA |
3818 | /* |
3819 | * Now use our persistent iterator and state, if we aren't already. | |
3820 | * We've restored and mapped the iter to match. | |
3821 | */ | |
227c0c96 | 3822 | |
b23df91b | 3823 | do { |
cd658695 JA |
3824 | /* |
3825 | * We end up here because of a partial read, either from | |
3826 | * above or inside this loop. Advance the iter by the bytes | |
3827 | * that were consumed. | |
3828 | */ | |
c88598a9 PB |
3829 | iov_iter_advance(&s->iter, ret); |
3830 | if (!iov_iter_count(&s->iter)) | |
cd658695 | 3831 | break; |
b23df91b | 3832 | rw->bytes_done += ret; |
c88598a9 | 3833 | iov_iter_save_state(&s->iter, &s->iter_state); |
cd658695 | 3834 | |
b23df91b PB |
3835 | /* if we can retry, do so with the callbacks armed */ |
3836 | if (!io_rw_should_retry(req)) { | |
3837 | kiocb->ki_flags &= ~IOCB_WAITQ; | |
3838 | return -EAGAIN; | |
3839 | } | |
3840 | ||
3841 | /* | |
3842 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If | |
3843 | * we get -EIOCBQUEUED, then we'll get a notification when the | |
3844 | * desired page gets unlocked. We can also get a partial read | |
3845 | * here, and if we do, then just retry at the new offset. | |
3846 | */ | |
c88598a9 | 3847 | ret = io_iter_do_read(req, &s->iter); |
b23df91b PB |
3848 | if (ret == -EIOCBQUEUED) |
3849 | return 0; | |
227c0c96 | 3850 | /* we got some bytes, but not all. retry. */ |
b5b0ecb7 | 3851 | kiocb->ki_flags &= ~IOCB_WAITQ; |
c88598a9 | 3852 | iov_iter_restore(&s->iter, &s->iter_state); |
cd658695 | 3853 | } while (ret > 0); |
227c0c96 | 3854 | done: |
2ea537ca | 3855 | kiocb_done(req, ret, issue_flags); |
fe1cdd55 PB |
3856 | out_free: |
3857 | /* it's faster to check here then delegate to kfree */ | |
3858 | if (iovec) | |
3859 | kfree(iovec); | |
5ea5dd45 | 3860 | return 0; |
2b188cc1 JA |
3861 | } |
3862 | ||
73debe68 | 3863 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3864 | { |
3529d8c2 JA |
3865 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
3866 | return -EBADF; | |
3884b83d | 3867 | req->rw.kiocb.ki_hint = ki_hint_validate(file_write_hint(req->file)); |
b9a6b8f9 | 3868 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3869 | } |
3870 | ||
889fca73 | 3871 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 3872 | { |
607b6fb8 | 3873 | struct io_rw_state __s, *s = &__s; |
c88598a9 | 3874 | struct iovec *iovec; |
9adbd45d | 3875 | struct kiocb *kiocb = &req->rw.kiocb; |
45d189c6 | 3876 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
cd658695 | 3877 | ssize_t ret, ret2; |
b4aec400 | 3878 | loff_t *ppos; |
2b188cc1 | 3879 | |
607b6fb8 | 3880 | if (!req_has_async_data(req)) { |
5e49c973 PB |
3881 | ret = io_import_iovec(WRITE, req, &iovec, s, issue_flags); |
3882 | if (unlikely(ret < 0)) | |
2846c481 | 3883 | return ret; |
607b6fb8 PB |
3884 | } else { |
3885 | struct io_async_rw *rw = req->async_data; | |
3886 | ||
3887 | s = &rw->s; | |
3888 | iov_iter_restore(&s->iter, &s->iter_state); | |
2846c481 | 3889 | iovec = NULL; |
2846c481 | 3890 | } |
c88598a9 | 3891 | req->result = iov_iter_count(&s->iter); |
2b188cc1 | 3892 | |
607b6fb8 PB |
3893 | if (force_nonblock) { |
3894 | /* If the file doesn't support async, just async punt */ | |
35645ac3 | 3895 | if (unlikely(!io_file_supports_nowait(req))) |
607b6fb8 | 3896 | goto copy_iov; |
fd6c2e4c | 3897 | |
607b6fb8 PB |
3898 | /* file path doesn't support NOWAIT for non-direct_IO */ |
3899 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
3900 | (req->flags & REQ_F_ISREG)) | |
3901 | goto copy_iov; | |
31b51510 | 3902 | |
607b6fb8 PB |
3903 | kiocb->ki_flags |= IOCB_NOWAIT; |
3904 | } else { | |
3905 | /* Ensure we clear previously set non-block flag */ | |
3906 | kiocb->ki_flags &= ~IOCB_NOWAIT; | |
3907 | } | |
31b51510 | 3908 | |
b4aec400 | 3909 | ppos = io_kiocb_update_pos(req); |
d34e1e5b | 3910 | |
b4aec400 | 3911 | ret = rw_verify_area(WRITE, req->file, ppos, req->result); |
fa15bafb PB |
3912 | if (unlikely(ret)) |
3913 | goto out_free; | |
4ed734b0 | 3914 | |
fa15bafb PB |
3915 | /* |
3916 | * Open-code file_start_write here to grab freeze protection, | |
3917 | * which will be released by another thread in | |
3918 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
3919 | * released so that it doesn't complain about the held lock when | |
3920 | * we return to userspace. | |
3921 | */ | |
3922 | if (req->flags & REQ_F_ISREG) { | |
8a3c84b6 | 3923 | sb_start_write(file_inode(req->file)->i_sb); |
fa15bafb PB |
3924 | __sb_writers_release(file_inode(req->file)->i_sb, |
3925 | SB_FREEZE_WRITE); | |
3926 | } | |
3927 | kiocb->ki_flags |= IOCB_WRITE; | |
4ed734b0 | 3928 | |
35645ac3 | 3929 | if (likely(req->file->f_op->write_iter)) |
c88598a9 | 3930 | ret2 = call_write_iter(req->file, kiocb, &s->iter); |
2dd2111d | 3931 | else if (req->file->f_op->write) |
c88598a9 | 3932 | ret2 = loop_rw_iter(WRITE, req, &s->iter); |
2dd2111d GH |
3933 | else |
3934 | ret2 = -EINVAL; | |
4ed734b0 | 3935 | |
6ad7f233 PB |
3936 | if (req->flags & REQ_F_REISSUE) { |
3937 | req->flags &= ~REQ_F_REISSUE; | |
230d50d4 | 3938 | ret2 = -EAGAIN; |
6ad7f233 | 3939 | } |
230d50d4 | 3940 | |
fa15bafb PB |
3941 | /* |
3942 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just | |
3943 | * retry them without IOCB_NOWAIT. | |
3944 | */ | |
3945 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) | |
3946 | ret2 = -EAGAIN; | |
75c668cd PB |
3947 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3948 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) | |
355afaeb | 3949 | goto done; |
fa15bafb | 3950 | if (!force_nonblock || ret2 != -EAGAIN) { |
eefdf30f | 3951 | /* IOPOLL retry should happen for io-wq threads */ |
b10841c9 | 3952 | if (ret2 == -EAGAIN && (req->ctx->flags & IORING_SETUP_IOPOLL)) |
eefdf30f | 3953 | goto copy_iov; |
355afaeb | 3954 | done: |
2ea537ca | 3955 | kiocb_done(req, ret2, issue_flags); |
fa15bafb | 3956 | } else { |
f67676d1 | 3957 | copy_iov: |
c88598a9 PB |
3958 | iov_iter_restore(&s->iter, &s->iter_state); |
3959 | ret = io_setup_async_rw(req, iovec, s, false); | |
6bf985dc | 3960 | return ret ?: -EAGAIN; |
2b188cc1 | 3961 | } |
31b51510 | 3962 | out_free: |
f261c168 | 3963 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 3964 | if (iovec) |
6f2cc166 | 3965 | kfree(iovec); |
2b188cc1 JA |
3966 | return ret; |
3967 | } | |
3968 | ||
80a261fd JA |
3969 | static int io_renameat_prep(struct io_kiocb *req, |
3970 | const struct io_uring_sqe *sqe) | |
3971 | { | |
3972 | struct io_rename *ren = &req->rename; | |
3973 | const char __user *oldf, *newf; | |
3974 | ||
ed7eb259 JA |
3975 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3976 | return -EINVAL; | |
26578cda | 3977 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
ed7eb259 | 3978 | return -EINVAL; |
80a261fd JA |
3979 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
3980 | return -EBADF; | |
3981 | ||
3982 | ren->old_dfd = READ_ONCE(sqe->fd); | |
3983 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3984 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3985 | ren->new_dfd = READ_ONCE(sqe->len); | |
3986 | ren->flags = READ_ONCE(sqe->rename_flags); | |
3987 | ||
3988 | ren->oldpath = getname(oldf); | |
3989 | if (IS_ERR(ren->oldpath)) | |
3990 | return PTR_ERR(ren->oldpath); | |
3991 | ||
3992 | ren->newpath = getname(newf); | |
3993 | if (IS_ERR(ren->newpath)) { | |
3994 | putname(ren->oldpath); | |
3995 | return PTR_ERR(ren->newpath); | |
3996 | } | |
3997 | ||
3998 | req->flags |= REQ_F_NEED_CLEANUP; | |
3999 | return 0; | |
4000 | } | |
4001 | ||
45d189c6 | 4002 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
80a261fd JA |
4003 | { |
4004 | struct io_rename *ren = &req->rename; | |
4005 | int ret; | |
4006 | ||
45d189c6 | 4007 | if (issue_flags & IO_URING_F_NONBLOCK) |
80a261fd JA |
4008 | return -EAGAIN; |
4009 | ||
4010 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, | |
4011 | ren->newpath, ren->flags); | |
4012 | ||
4013 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4014 | if (ret < 0) | |
93d2bcd2 | 4015 | req_set_fail(req); |
80a261fd JA |
4016 | io_req_complete(req, ret); |
4017 | return 0; | |
4018 | } | |
4019 | ||
14a1143b JA |
4020 | static int io_unlinkat_prep(struct io_kiocb *req, |
4021 | const struct io_uring_sqe *sqe) | |
4022 | { | |
4023 | struct io_unlink *un = &req->unlink; | |
4024 | const char __user *fname; | |
4025 | ||
22634bc5 JA |
4026 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4027 | return -EINVAL; | |
26578cda PB |
4028 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
4029 | sqe->splice_fd_in) | |
22634bc5 | 4030 | return -EINVAL; |
14a1143b JA |
4031 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
4032 | return -EBADF; | |
4033 | ||
4034 | un->dfd = READ_ONCE(sqe->fd); | |
4035 | ||
4036 | un->flags = READ_ONCE(sqe->unlink_flags); | |
4037 | if (un->flags & ~AT_REMOVEDIR) | |
4038 | return -EINVAL; | |
4039 | ||
4040 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4041 | un->filename = getname(fname); | |
4042 | if (IS_ERR(un->filename)) | |
4043 | return PTR_ERR(un->filename); | |
4044 | ||
4045 | req->flags |= REQ_F_NEED_CLEANUP; | |
4046 | return 0; | |
4047 | } | |
4048 | ||
45d189c6 | 4049 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
14a1143b JA |
4050 | { |
4051 | struct io_unlink *un = &req->unlink; | |
4052 | int ret; | |
4053 | ||
45d189c6 | 4054 | if (issue_flags & IO_URING_F_NONBLOCK) |
14a1143b JA |
4055 | return -EAGAIN; |
4056 | ||
4057 | if (un->flags & AT_REMOVEDIR) | |
4058 | ret = do_rmdir(un->dfd, un->filename); | |
4059 | else | |
4060 | ret = do_unlinkat(un->dfd, un->filename); | |
4061 | ||
4062 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4063 | if (ret < 0) | |
93d2bcd2 | 4064 | req_set_fail(req); |
14a1143b JA |
4065 | io_req_complete(req, ret); |
4066 | return 0; | |
4067 | } | |
4068 | ||
e34a02dc DK |
4069 | static int io_mkdirat_prep(struct io_kiocb *req, |
4070 | const struct io_uring_sqe *sqe) | |
4071 | { | |
4072 | struct io_mkdir *mkd = &req->mkdir; | |
4073 | const char __user *fname; | |
4074 | ||
4075 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
4076 | return -EINVAL; | |
4077 | if (sqe->ioprio || sqe->off || sqe->rw_flags || sqe->buf_index || | |
4078 | sqe->splice_fd_in) | |
4079 | return -EINVAL; | |
4080 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
4081 | return -EBADF; | |
4082 | ||
4083 | mkd->dfd = READ_ONCE(sqe->fd); | |
4084 | mkd->mode = READ_ONCE(sqe->len); | |
4085 | ||
4086 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4087 | mkd->filename = getname(fname); | |
4088 | if (IS_ERR(mkd->filename)) | |
4089 | return PTR_ERR(mkd->filename); | |
4090 | ||
4091 | req->flags |= REQ_F_NEED_CLEANUP; | |
4092 | return 0; | |
4093 | } | |
4094 | ||
04f34081 | 4095 | static int io_mkdirat(struct io_kiocb *req, unsigned int issue_flags) |
e34a02dc DK |
4096 | { |
4097 | struct io_mkdir *mkd = &req->mkdir; | |
4098 | int ret; | |
4099 | ||
4100 | if (issue_flags & IO_URING_F_NONBLOCK) | |
4101 | return -EAGAIN; | |
4102 | ||
4103 | ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode); | |
4104 | ||
4105 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4106 | if (ret < 0) | |
4107 | req_set_fail(req); | |
4108 | io_req_complete(req, ret); | |
4109 | return 0; | |
4110 | } | |
4111 | ||
7a8721f8 DK |
4112 | static int io_symlinkat_prep(struct io_kiocb *req, |
4113 | const struct io_uring_sqe *sqe) | |
4114 | { | |
4115 | struct io_symlink *sl = &req->symlink; | |
4116 | const char __user *oldpath, *newpath; | |
4117 | ||
4118 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
4119 | return -EINVAL; | |
4120 | if (sqe->ioprio || sqe->len || sqe->rw_flags || sqe->buf_index || | |
4121 | sqe->splice_fd_in) | |
4122 | return -EINVAL; | |
4123 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
4124 | return -EBADF; | |
4125 | ||
4126 | sl->new_dfd = READ_ONCE(sqe->fd); | |
4127 | oldpath = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4128 | newpath = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
4129 | ||
4130 | sl->oldpath = getname(oldpath); | |
4131 | if (IS_ERR(sl->oldpath)) | |
4132 | return PTR_ERR(sl->oldpath); | |
4133 | ||
4134 | sl->newpath = getname(newpath); | |
4135 | if (IS_ERR(sl->newpath)) { | |
4136 | putname(sl->oldpath); | |
4137 | return PTR_ERR(sl->newpath); | |
4138 | } | |
4139 | ||
4140 | req->flags |= REQ_F_NEED_CLEANUP; | |
4141 | return 0; | |
4142 | } | |
4143 | ||
04f34081 | 4144 | static int io_symlinkat(struct io_kiocb *req, unsigned int issue_flags) |
7a8721f8 DK |
4145 | { |
4146 | struct io_symlink *sl = &req->symlink; | |
4147 | int ret; | |
4148 | ||
4149 | if (issue_flags & IO_URING_F_NONBLOCK) | |
4150 | return -EAGAIN; | |
4151 | ||
4152 | ret = do_symlinkat(sl->oldpath, sl->new_dfd, sl->newpath); | |
4153 | ||
4154 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4155 | if (ret < 0) | |
4156 | req_set_fail(req); | |
4157 | io_req_complete(req, ret); | |
4158 | return 0; | |
4159 | } | |
4160 | ||
cf30da90 DK |
4161 | static int io_linkat_prep(struct io_kiocb *req, |
4162 | const struct io_uring_sqe *sqe) | |
4163 | { | |
4164 | struct io_hardlink *lnk = &req->hardlink; | |
4165 | const char __user *oldf, *newf; | |
4166 | ||
4167 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
4168 | return -EINVAL; | |
4169 | if (sqe->ioprio || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in) | |
4170 | return -EINVAL; | |
4171 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
4172 | return -EBADF; | |
4173 | ||
4174 | lnk->old_dfd = READ_ONCE(sqe->fd); | |
4175 | lnk->new_dfd = READ_ONCE(sqe->len); | |
4176 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4177 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
4178 | lnk->flags = READ_ONCE(sqe->hardlink_flags); | |
4179 | ||
4180 | lnk->oldpath = getname(oldf); | |
4181 | if (IS_ERR(lnk->oldpath)) | |
4182 | return PTR_ERR(lnk->oldpath); | |
4183 | ||
4184 | lnk->newpath = getname(newf); | |
4185 | if (IS_ERR(lnk->newpath)) { | |
4186 | putname(lnk->oldpath); | |
4187 | return PTR_ERR(lnk->newpath); | |
4188 | } | |
4189 | ||
4190 | req->flags |= REQ_F_NEED_CLEANUP; | |
4191 | return 0; | |
4192 | } | |
4193 | ||
04f34081 | 4194 | static int io_linkat(struct io_kiocb *req, unsigned int issue_flags) |
cf30da90 DK |
4195 | { |
4196 | struct io_hardlink *lnk = &req->hardlink; | |
4197 | int ret; | |
4198 | ||
4199 | if (issue_flags & IO_URING_F_NONBLOCK) | |
4200 | return -EAGAIN; | |
4201 | ||
4202 | ret = do_linkat(lnk->old_dfd, lnk->oldpath, lnk->new_dfd, | |
4203 | lnk->newpath, lnk->flags); | |
4204 | ||
4205 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4206 | if (ret < 0) | |
4207 | req_set_fail(req); | |
4208 | io_req_complete(req, ret); | |
4209 | return 0; | |
4210 | } | |
4211 | ||
36f4fa68 JA |
4212 | static int io_shutdown_prep(struct io_kiocb *req, |
4213 | const struct io_uring_sqe *sqe) | |
4214 | { | |
4215 | #if defined(CONFIG_NET) | |
4216 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
4217 | return -EINVAL; | |
26578cda PB |
4218 | if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || |
4219 | sqe->buf_index || sqe->splice_fd_in)) | |
36f4fa68 JA |
4220 | return -EINVAL; |
4221 | ||
4222 | req->shutdown.how = READ_ONCE(sqe->len); | |
4223 | return 0; | |
4224 | #else | |
4225 | return -EOPNOTSUPP; | |
4226 | #endif | |
4227 | } | |
4228 | ||
45d189c6 | 4229 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
36f4fa68 JA |
4230 | { |
4231 | #if defined(CONFIG_NET) | |
4232 | struct socket *sock; | |
4233 | int ret; | |
4234 | ||
45d189c6 | 4235 | if (issue_flags & IO_URING_F_NONBLOCK) |
36f4fa68 JA |
4236 | return -EAGAIN; |
4237 | ||
48aba79b | 4238 | sock = sock_from_file(req->file); |
36f4fa68 | 4239 | if (unlikely(!sock)) |
48aba79b | 4240 | return -ENOTSOCK; |
36f4fa68 JA |
4241 | |
4242 | ret = __sys_shutdown_sock(sock, req->shutdown.how); | |
a146468d | 4243 | if (ret < 0) |
93d2bcd2 | 4244 | req_set_fail(req); |
36f4fa68 JA |
4245 | io_req_complete(req, ret); |
4246 | return 0; | |
4247 | #else | |
4248 | return -EOPNOTSUPP; | |
4249 | #endif | |
4250 | } | |
4251 | ||
f2a8d5c7 PB |
4252 | static int __io_splice_prep(struct io_kiocb *req, |
4253 | const struct io_uring_sqe *sqe) | |
7d67af2c | 4254 | { |
fe7e3257 | 4255 | struct io_splice *sp = &req->splice; |
7d67af2c | 4256 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
7d67af2c | 4257 | |
3232dd02 PB |
4258 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4259 | return -EINVAL; | |
7d67af2c PB |
4260 | |
4261 | sp->file_in = NULL; | |
7d67af2c PB |
4262 | sp->len = READ_ONCE(sqe->len); |
4263 | sp->flags = READ_ONCE(sqe->splice_flags); | |
4264 | ||
4265 | if (unlikely(sp->flags & ~valid_flags)) | |
4266 | return -EINVAL; | |
4267 | ||
62906e89 | 4268 | sp->file_in = io_file_get(req->ctx, req, READ_ONCE(sqe->splice_fd_in), |
8371adf5 PB |
4269 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
4270 | if (!sp->file_in) | |
4271 | return -EBADF; | |
7d67af2c | 4272 | req->flags |= REQ_F_NEED_CLEANUP; |
7d67af2c PB |
4273 | return 0; |
4274 | } | |
4275 | ||
f2a8d5c7 PB |
4276 | static int io_tee_prep(struct io_kiocb *req, |
4277 | const struct io_uring_sqe *sqe) | |
4278 | { | |
4279 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) | |
4280 | return -EINVAL; | |
4281 | return __io_splice_prep(req, sqe); | |
4282 | } | |
4283 | ||
45d189c6 | 4284 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
f2a8d5c7 PB |
4285 | { |
4286 | struct io_splice *sp = &req->splice; | |
4287 | struct file *in = sp->file_in; | |
4288 | struct file *out = sp->file_out; | |
4289 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
4290 | long ret = 0; | |
4291 | ||
45d189c6 | 4292 | if (issue_flags & IO_URING_F_NONBLOCK) |
f2a8d5c7 PB |
4293 | return -EAGAIN; |
4294 | if (sp->len) | |
4295 | ret = do_tee(in, out, sp->len, flags); | |
4296 | ||
e1d767f0 PB |
4297 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
4298 | io_put_file(in); | |
f2a8d5c7 PB |
4299 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4300 | ||
f2a8d5c7 | 4301 | if (ret != sp->len) |
93d2bcd2 | 4302 | req_set_fail(req); |
e1e16097 | 4303 | io_req_complete(req, ret); |
f2a8d5c7 PB |
4304 | return 0; |
4305 | } | |
4306 | ||
4307 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4308 | { | |
fe7e3257 | 4309 | struct io_splice *sp = &req->splice; |
f2a8d5c7 PB |
4310 | |
4311 | sp->off_in = READ_ONCE(sqe->splice_off_in); | |
4312 | sp->off_out = READ_ONCE(sqe->off); | |
4313 | return __io_splice_prep(req, sqe); | |
4314 | } | |
4315 | ||
45d189c6 | 4316 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
7d67af2c PB |
4317 | { |
4318 | struct io_splice *sp = &req->splice; | |
4319 | struct file *in = sp->file_in; | |
4320 | struct file *out = sp->file_out; | |
4321 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
4322 | loff_t *poff_in, *poff_out; | |
c9687426 | 4323 | long ret = 0; |
7d67af2c | 4324 | |
45d189c6 | 4325 | if (issue_flags & IO_URING_F_NONBLOCK) |
2fb3e822 | 4326 | return -EAGAIN; |
7d67af2c PB |
4327 | |
4328 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; | |
4329 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; | |
c9687426 | 4330 | |
948a7749 | 4331 | if (sp->len) |
c9687426 | 4332 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
7d67af2c | 4333 | |
e1d767f0 PB |
4334 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
4335 | io_put_file(in); | |
7d67af2c PB |
4336 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4337 | ||
7d67af2c | 4338 | if (ret != sp->len) |
93d2bcd2 | 4339 | req_set_fail(req); |
e1e16097 | 4340 | io_req_complete(req, ret); |
7d67af2c PB |
4341 | return 0; |
4342 | } | |
4343 | ||
2b188cc1 JA |
4344 | /* |
4345 | * IORING_OP_NOP just posts a completion event, nothing else. | |
4346 | */ | |
889fca73 | 4347 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
4348 | { |
4349 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 4350 | |
def596e9 JA |
4351 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4352 | return -EINVAL; | |
4353 | ||
889fca73 | 4354 | __io_req_complete(req, issue_flags, 0, 0); |
2b188cc1 JA |
4355 | return 0; |
4356 | } | |
4357 | ||
4f57f06c JA |
4358 | static int io_msg_ring_prep(struct io_kiocb *req, |
4359 | const struct io_uring_sqe *sqe) | |
4360 | { | |
4361 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || | |
4362 | sqe->rw_flags || sqe->splice_fd_in || sqe->buf_index || | |
4363 | sqe->personality)) | |
4364 | return -EINVAL; | |
4365 | ||
4366 | if (req->file->f_op != &io_uring_fops) | |
4367 | return -EBADFD; | |
4368 | ||
4369 | req->msg.user_data = READ_ONCE(sqe->off); | |
4370 | req->msg.len = READ_ONCE(sqe->len); | |
4371 | return 0; | |
4372 | } | |
4373 | ||
4374 | static int io_msg_ring(struct io_kiocb *req, unsigned int issue_flags) | |
4375 | { | |
4376 | struct io_ring_ctx *target_ctx; | |
4377 | struct io_msg *msg = &req->msg; | |
4378 | int ret = -EOVERFLOW; | |
4379 | bool filled; | |
4380 | ||
4381 | target_ctx = req->file->private_data; | |
4382 | ||
4383 | spin_lock(&target_ctx->completion_lock); | |
4384 | filled = io_fill_cqe_aux(target_ctx, msg->user_data, msg->len, | |
4385 | IORING_CQE_F_MSG); | |
4386 | io_commit_cqring(target_ctx); | |
4387 | spin_unlock(&target_ctx->completion_lock); | |
4388 | ||
4389 | if (filled) { | |
4390 | io_cqring_ev_posted(target_ctx); | |
4391 | ret = 0; | |
4392 | } | |
4393 | ||
4394 | __io_req_complete(req, issue_flags, ret, 0); | |
4395 | return 0; | |
4396 | } | |
4397 | ||
1155c76a | 4398 | static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 4399 | { |
6b06314c | 4400 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 4401 | |
09bb8394 JA |
4402 | if (!req->file) |
4403 | return -EBADF; | |
c992fe29 | 4404 | |
6b06314c | 4405 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 4406 | return -EINVAL; |
26578cda PB |
4407 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
4408 | sqe->splice_fd_in)) | |
c992fe29 CH |
4409 | return -EINVAL; |
4410 | ||
8ed8d3c3 JA |
4411 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
4412 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
4413 | return -EINVAL; | |
4414 | ||
4415 | req->sync.off = READ_ONCE(sqe->off); | |
4416 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
4417 | return 0; |
4418 | } | |
4419 | ||
45d189c6 | 4420 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4421 | { |
8ed8d3c3 | 4422 | loff_t end = req->sync.off + req->sync.len; |
8ed8d3c3 JA |
4423 | int ret; |
4424 | ||
ac45abc0 | 4425 | /* fsync always requires a blocking context */ |
45d189c6 | 4426 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4427 | return -EAGAIN; |
4428 | ||
9adbd45d | 4429 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
4430 | end > 0 ? end : LLONG_MAX, |
4431 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
4432 | if (ret < 0) | |
93d2bcd2 | 4433 | req_set_fail(req); |
e1e16097 | 4434 | io_req_complete(req, ret); |
c992fe29 CH |
4435 | return 0; |
4436 | } | |
4437 | ||
d63d1b5e JA |
4438 | static int io_fallocate_prep(struct io_kiocb *req, |
4439 | const struct io_uring_sqe *sqe) | |
4440 | { | |
26578cda PB |
4441 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags || |
4442 | sqe->splice_fd_in) | |
d63d1b5e | 4443 | return -EINVAL; |
3232dd02 PB |
4444 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4445 | return -EINVAL; | |
d63d1b5e JA |
4446 | |
4447 | req->sync.off = READ_ONCE(sqe->off); | |
4448 | req->sync.len = READ_ONCE(sqe->addr); | |
4449 | req->sync.mode = READ_ONCE(sqe->len); | |
4450 | return 0; | |
4451 | } | |
4452 | ||
45d189c6 | 4453 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
5d17b4a4 | 4454 | { |
ac45abc0 PB |
4455 | int ret; |
4456 | ||
d63d1b5e | 4457 | /* fallocate always requiring blocking context */ |
45d189c6 | 4458 | if (issue_flags & IO_URING_F_NONBLOCK) |
5d17b4a4 | 4459 | return -EAGAIN; |
ac45abc0 PB |
4460 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
4461 | req->sync.len); | |
ac45abc0 | 4462 | if (ret < 0) |
93d2bcd2 | 4463 | req_set_fail(req); |
e1e16097 | 4464 | io_req_complete(req, ret); |
5d17b4a4 JA |
4465 | return 0; |
4466 | } | |
4467 | ||
ec65fea5 | 4468 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 4469 | { |
f8748881 | 4470 | const char __user *fname; |
15b71abe | 4471 | int ret; |
b7bb4f7d | 4472 | |
d3fddf6d PB |
4473 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4474 | return -EINVAL; | |
b9445598 | 4475 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
15b71abe | 4476 | return -EINVAL; |
ec65fea5 | 4477 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
cf3040ca | 4478 | return -EBADF; |
03b1230c | 4479 | |
ec65fea5 PB |
4480 | /* open.how should be already initialised */ |
4481 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
08a1d26e | 4482 | req->open.how.flags |= O_LARGEFILE; |
3529d8c2 | 4483 | |
25e72d10 PB |
4484 | req->open.dfd = READ_ONCE(sqe->fd); |
4485 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
f8748881 | 4486 | req->open.filename = getname(fname); |
15b71abe JA |
4487 | if (IS_ERR(req->open.filename)) { |
4488 | ret = PTR_ERR(req->open.filename); | |
4489 | req->open.filename = NULL; | |
4490 | return ret; | |
4491 | } | |
b9445598 PB |
4492 | |
4493 | req->open.file_slot = READ_ONCE(sqe->file_index); | |
4494 | if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC)) | |
4495 | return -EINVAL; | |
4496 | ||
4022e7af | 4497 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
8fef80bf | 4498 | req->flags |= REQ_F_NEED_CLEANUP; |
15b71abe | 4499 | return 0; |
03b1230c JA |
4500 | } |
4501 | ||
ec65fea5 PB |
4502 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4503 | { | |
d3fddf6d PB |
4504 | u64 mode = READ_ONCE(sqe->len); |
4505 | u64 flags = READ_ONCE(sqe->open_flags); | |
ec65fea5 | 4506 | |
ec65fea5 PB |
4507 | req->open.how = build_open_how(flags, mode); |
4508 | return __io_openat_prep(req, sqe); | |
4509 | } | |
4510 | ||
cebdb986 | 4511 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 4512 | { |
cebdb986 | 4513 | struct open_how __user *how; |
cebdb986 | 4514 | size_t len; |
0fa03c62 JA |
4515 | int ret; |
4516 | ||
cebdb986 JA |
4517 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4518 | len = READ_ONCE(sqe->len); | |
cebdb986 JA |
4519 | if (len < OPEN_HOW_SIZE_VER0) |
4520 | return -EINVAL; | |
3529d8c2 | 4521 | |
cebdb986 JA |
4522 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
4523 | len); | |
4524 | if (ret) | |
4525 | return ret; | |
3529d8c2 | 4526 | |
ec65fea5 | 4527 | return __io_openat_prep(req, sqe); |
cebdb986 JA |
4528 | } |
4529 | ||
45d189c6 | 4530 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
15b71abe JA |
4531 | { |
4532 | struct open_flags op; | |
15b71abe | 4533 | struct file *file; |
b9445598 PB |
4534 | bool resolve_nonblock, nonblock_set; |
4535 | bool fixed = !!req->open.file_slot; | |
15b71abe JA |
4536 | int ret; |
4537 | ||
cebdb986 | 4538 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
4539 | if (ret) |
4540 | goto err; | |
3a81fd02 JA |
4541 | nonblock_set = op.open_flag & O_NONBLOCK; |
4542 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; | |
45d189c6 | 4543 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3a81fd02 JA |
4544 | /* |
4545 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, | |
4546 | * it'll always -EAGAIN | |
4547 | */ | |
4548 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) | |
4549 | return -EAGAIN; | |
4550 | op.lookup_flags |= LOOKUP_CACHED; | |
4551 | op.open_flag |= O_NONBLOCK; | |
4552 | } | |
15b71abe | 4553 | |
b9445598 PB |
4554 | if (!fixed) { |
4555 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); | |
4556 | if (ret < 0) | |
4557 | goto err; | |
4558 | } | |
15b71abe JA |
4559 | |
4560 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
12dcb58a | 4561 | if (IS_ERR(file)) { |
944d1444 | 4562 | /* |
12dcb58a PB |
4563 | * We could hang on to this 'fd' on retrying, but seems like |
4564 | * marginal gain for something that is now known to be a slower | |
4565 | * path. So just put it, and we'll get a new one when we retry. | |
944d1444 | 4566 | */ |
b9445598 PB |
4567 | if (!fixed) |
4568 | put_unused_fd(ret); | |
3a81fd02 | 4569 | |
15b71abe | 4570 | ret = PTR_ERR(file); |
12dcb58a PB |
4571 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
4572 | if (ret == -EAGAIN && | |
4573 | (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK))) | |
4574 | return -EAGAIN; | |
4575 | goto err; | |
15b71abe | 4576 | } |
12dcb58a PB |
4577 | |
4578 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) | |
4579 | file->f_flags &= ~O_NONBLOCK; | |
4580 | fsnotify_open(file); | |
b9445598 PB |
4581 | |
4582 | if (!fixed) | |
4583 | fd_install(ret, file); | |
4584 | else | |
4585 | ret = io_install_fixed_file(req, file, issue_flags, | |
4586 | req->open.file_slot - 1); | |
15b71abe JA |
4587 | err: |
4588 | putname(req->open.filename); | |
8fef80bf | 4589 | req->flags &= ~REQ_F_NEED_CLEANUP; |
15b71abe | 4590 | if (ret < 0) |
93d2bcd2 | 4591 | req_set_fail(req); |
0bdf3398 | 4592 | __io_req_complete(req, issue_flags, ret, 0); |
15b71abe JA |
4593 | return 0; |
4594 | } | |
4595 | ||
45d189c6 | 4596 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
cebdb986 | 4597 | { |
e45cff58 | 4598 | return io_openat2(req, issue_flags); |
cebdb986 JA |
4599 | } |
4600 | ||
067524e9 JA |
4601 | static int io_remove_buffers_prep(struct io_kiocb *req, |
4602 | const struct io_uring_sqe *sqe) | |
4603 | { | |
4604 | struct io_provide_buf *p = &req->pbuf; | |
4605 | u64 tmp; | |
4606 | ||
26578cda PB |
4607 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off || |
4608 | sqe->splice_fd_in) | |
067524e9 JA |
4609 | return -EINVAL; |
4610 | ||
4611 | tmp = READ_ONCE(sqe->fd); | |
4612 | if (!tmp || tmp > USHRT_MAX) | |
4613 | return -EINVAL; | |
4614 | ||
4615 | memset(p, 0, sizeof(*p)); | |
4616 | p->nbufs = tmp; | |
4617 | p->bgid = READ_ONCE(sqe->buf_group); | |
4618 | return 0; | |
4619 | } | |
4620 | ||
4621 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, | |
4622 | int bgid, unsigned nbufs) | |
4623 | { | |
4624 | unsigned i = 0; | |
4625 | ||
4626 | /* shouldn't happen */ | |
4627 | if (!nbufs) | |
4628 | return 0; | |
4629 | ||
4630 | /* the head kbuf is the list itself */ | |
4631 | while (!list_empty(&buf->list)) { | |
4632 | struct io_buffer *nxt; | |
4633 | ||
4634 | nxt = list_first_entry(&buf->list, struct io_buffer, list); | |
4635 | list_del(&nxt->list); | |
067524e9 JA |
4636 | if (++i == nbufs) |
4637 | return i; | |
1d0254e6 | 4638 | cond_resched(); |
067524e9 JA |
4639 | } |
4640 | i++; | |
9e15c3a0 | 4641 | xa_erase(&ctx->io_buffers, bgid); |
067524e9 JA |
4642 | |
4643 | return i; | |
4644 | } | |
4645 | ||
889fca73 | 4646 | static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags) |
067524e9 JA |
4647 | { |
4648 | struct io_provide_buf *p = &req->pbuf; | |
4649 | struct io_ring_ctx *ctx = req->ctx; | |
4650 | struct io_buffer *head; | |
4651 | int ret = 0; | |
3b44b371 | 4652 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
067524e9 | 4653 | |
3b44b371 | 4654 | io_ring_submit_lock(ctx, needs_lock); |
067524e9 JA |
4655 | |
4656 | lockdep_assert_held(&ctx->uring_lock); | |
4657 | ||
4658 | ret = -ENOENT; | |
9e15c3a0 | 4659 | head = xa_load(&ctx->io_buffers, p->bgid); |
067524e9 JA |
4660 | if (head) |
4661 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); | |
067524e9 | 4662 | if (ret < 0) |
93d2bcd2 | 4663 | req_set_fail(req); |
067524e9 | 4664 | |
9fb8cb49 PB |
4665 | /* complete before unlock, IOPOLL may need the lock */ |
4666 | __io_req_complete(req, issue_flags, ret, 0); | |
3b44b371 | 4667 | io_ring_submit_unlock(ctx, needs_lock); |
067524e9 JA |
4668 | return 0; |
4669 | } | |
4670 | ||
ddf0322d JA |
4671 | static int io_provide_buffers_prep(struct io_kiocb *req, |
4672 | const struct io_uring_sqe *sqe) | |
4673 | { | |
38134ada | 4674 | unsigned long size, tmp_check; |
ddf0322d JA |
4675 | struct io_provide_buf *p = &req->pbuf; |
4676 | u64 tmp; | |
4677 | ||
26578cda | 4678 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
ddf0322d JA |
4679 | return -EINVAL; |
4680 | ||
4681 | tmp = READ_ONCE(sqe->fd); | |
4682 | if (!tmp || tmp > USHRT_MAX) | |
4683 | return -E2BIG; | |
4684 | p->nbufs = tmp; | |
4685 | p->addr = READ_ONCE(sqe->addr); | |
4686 | p->len = READ_ONCE(sqe->len); | |
4687 | ||
38134ada PB |
4688 | if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs, |
4689 | &size)) | |
4690 | return -EOVERFLOW; | |
4691 | if (check_add_overflow((unsigned long)p->addr, size, &tmp_check)) | |
4692 | return -EOVERFLOW; | |
4693 | ||
d81269fe PB |
4694 | size = (unsigned long)p->len * p->nbufs; |
4695 | if (!access_ok(u64_to_user_ptr(p->addr), size)) | |
ddf0322d JA |
4696 | return -EFAULT; |
4697 | ||
4698 | p->bgid = READ_ONCE(sqe->buf_group); | |
4699 | tmp = READ_ONCE(sqe->off); | |
4700 | if (tmp > USHRT_MAX) | |
4701 | return -E2BIG; | |
4702 | p->bid = tmp; | |
4703 | return 0; | |
4704 | } | |
4705 | ||
cc3cec83 JA |
4706 | static int io_refill_buffer_cache(struct io_ring_ctx *ctx) |
4707 | { | |
4708 | struct io_buffer *buf; | |
4709 | struct page *page; | |
4710 | int bufs_in_page; | |
4711 | ||
4712 | /* | |
4713 | * Completions that don't happen inline (eg not under uring_lock) will | |
4714 | * add to ->io_buffers_comp. If we don't have any free buffers, check | |
4715 | * the completion list and splice those entries first. | |
4716 | */ | |
4717 | if (!list_empty_careful(&ctx->io_buffers_comp)) { | |
4718 | spin_lock(&ctx->completion_lock); | |
4719 | if (!list_empty(&ctx->io_buffers_comp)) { | |
4720 | list_splice_init(&ctx->io_buffers_comp, | |
4721 | &ctx->io_buffers_cache); | |
4722 | spin_unlock(&ctx->completion_lock); | |
4723 | return 0; | |
4724 | } | |
4725 | spin_unlock(&ctx->completion_lock); | |
4726 | } | |
4727 | ||
4728 | /* | |
4729 | * No free buffers and no completion entries either. Allocate a new | |
4730 | * page worth of buffer entries and add those to our freelist. | |
4731 | */ | |
4732 | page = alloc_page(GFP_KERNEL_ACCOUNT); | |
4733 | if (!page) | |
4734 | return -ENOMEM; | |
4735 | ||
4736 | list_add(&page->lru, &ctx->io_buffers_pages); | |
4737 | ||
4738 | buf = page_address(page); | |
4739 | bufs_in_page = PAGE_SIZE / sizeof(*buf); | |
4740 | while (bufs_in_page) { | |
4741 | list_add_tail(&buf->list, &ctx->io_buffers_cache); | |
4742 | buf++; | |
4743 | bufs_in_page--; | |
4744 | } | |
4745 | ||
4746 | return 0; | |
4747 | } | |
4748 | ||
4749 | static int io_add_buffers(struct io_ring_ctx *ctx, struct io_provide_buf *pbuf, | |
4750 | struct io_buffer **head) | |
ddf0322d JA |
4751 | { |
4752 | struct io_buffer *buf; | |
4753 | u64 addr = pbuf->addr; | |
4754 | int i, bid = pbuf->bid; | |
4755 | ||
4756 | for (i = 0; i < pbuf->nbufs; i++) { | |
cc3cec83 JA |
4757 | if (list_empty(&ctx->io_buffers_cache) && |
4758 | io_refill_buffer_cache(ctx)) | |
ddf0322d | 4759 | break; |
cc3cec83 JA |
4760 | buf = list_first_entry(&ctx->io_buffers_cache, struct io_buffer, |
4761 | list); | |
4762 | list_del(&buf->list); | |
ddf0322d | 4763 | buf->addr = addr; |
d1f82808 | 4764 | buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT); |
ddf0322d JA |
4765 | buf->bid = bid; |
4766 | addr += pbuf->len; | |
4767 | bid++; | |
4768 | if (!*head) { | |
4769 | INIT_LIST_HEAD(&buf->list); | |
4770 | *head = buf; | |
4771 | } else { | |
4772 | list_add_tail(&buf->list, &(*head)->list); | |
4773 | } | |
f240762f | 4774 | cond_resched(); |
ddf0322d JA |
4775 | } |
4776 | ||
4777 | return i ? i : -ENOMEM; | |
4778 | } | |
4779 | ||
889fca73 | 4780 | static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags) |
ddf0322d JA |
4781 | { |
4782 | struct io_provide_buf *p = &req->pbuf; | |
4783 | struct io_ring_ctx *ctx = req->ctx; | |
4784 | struct io_buffer *head, *list; | |
4785 | int ret = 0; | |
3b44b371 | 4786 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
ddf0322d | 4787 | |
3b44b371 | 4788 | io_ring_submit_lock(ctx, needs_lock); |
ddf0322d JA |
4789 | |
4790 | lockdep_assert_held(&ctx->uring_lock); | |
4791 | ||
9e15c3a0 | 4792 | list = head = xa_load(&ctx->io_buffers, p->bgid); |
ddf0322d | 4793 | |
cc3cec83 | 4794 | ret = io_add_buffers(ctx, p, &head); |
9e15c3a0 JA |
4795 | if (ret >= 0 && !list) { |
4796 | ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL); | |
4797 | if (ret < 0) | |
067524e9 | 4798 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
ddf0322d | 4799 | } |
ddf0322d | 4800 | if (ret < 0) |
93d2bcd2 | 4801 | req_set_fail(req); |
9fb8cb49 PB |
4802 | /* complete before unlock, IOPOLL may need the lock */ |
4803 | __io_req_complete(req, issue_flags, ret, 0); | |
3b44b371 | 4804 | io_ring_submit_unlock(ctx, needs_lock); |
ddf0322d | 4805 | return 0; |
cebdb986 JA |
4806 | } |
4807 | ||
3e4827b0 JA |
4808 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
4809 | const struct io_uring_sqe *sqe) | |
4810 | { | |
4811 | #if defined(CONFIG_EPOLL) | |
26578cda | 4812 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
3e4827b0 | 4813 | return -EINVAL; |
2d74d042 | 4814 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4815 | return -EINVAL; |
3e4827b0 JA |
4816 | |
4817 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
4818 | req->epoll.op = READ_ONCE(sqe->len); | |
4819 | req->epoll.fd = READ_ONCE(sqe->off); | |
4820 | ||
4821 | if (ep_op_has_event(req->epoll.op)) { | |
4822 | struct epoll_event __user *ev; | |
4823 | ||
4824 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4825 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
4826 | return -EFAULT; | |
4827 | } | |
4828 | ||
4829 | return 0; | |
4830 | #else | |
4831 | return -EOPNOTSUPP; | |
4832 | #endif | |
4833 | } | |
4834 | ||
889fca73 | 4835 | static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags) |
3e4827b0 JA |
4836 | { |
4837 | #if defined(CONFIG_EPOLL) | |
4838 | struct io_epoll *ie = &req->epoll; | |
4839 | int ret; | |
45d189c6 | 4840 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
3e4827b0 JA |
4841 | |
4842 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
4843 | if (force_nonblock && ret == -EAGAIN) | |
4844 | return -EAGAIN; | |
4845 | ||
4846 | if (ret < 0) | |
93d2bcd2 | 4847 | req_set_fail(req); |
889fca73 | 4848 | __io_req_complete(req, issue_flags, ret, 0); |
3e4827b0 JA |
4849 | return 0; |
4850 | #else | |
4851 | return -EOPNOTSUPP; | |
4852 | #endif | |
4853 | } | |
4854 | ||
c1ca757b JA |
4855 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4856 | { | |
4857 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
26578cda | 4858 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in) |
c1ca757b | 4859 | return -EINVAL; |
3232dd02 PB |
4860 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4861 | return -EINVAL; | |
c1ca757b JA |
4862 | |
4863 | req->madvise.addr = READ_ONCE(sqe->addr); | |
4864 | req->madvise.len = READ_ONCE(sqe->len); | |
4865 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4866 | return 0; | |
4867 | #else | |
4868 | return -EOPNOTSUPP; | |
4869 | #endif | |
4870 | } | |
4871 | ||
45d189c6 | 4872 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
c1ca757b JA |
4873 | { |
4874 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4875 | struct io_madvise *ma = &req->madvise; | |
4876 | int ret; | |
4877 | ||
45d189c6 | 4878 | if (issue_flags & IO_URING_F_NONBLOCK) |
c1ca757b JA |
4879 | return -EAGAIN; |
4880 | ||
0726b01e | 4881 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
c1ca757b | 4882 | if (ret < 0) |
93d2bcd2 | 4883 | req_set_fail(req); |
e1e16097 | 4884 | io_req_complete(req, ret); |
c1ca757b JA |
4885 | return 0; |
4886 | #else | |
4887 | return -EOPNOTSUPP; | |
4888 | #endif | |
4889 | } | |
4890 | ||
4840e418 JA |
4891 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4892 | { | |
26578cda | 4893 | if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in) |
4840e418 | 4894 | return -EINVAL; |
3232dd02 PB |
4895 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4896 | return -EINVAL; | |
4840e418 JA |
4897 | |
4898 | req->fadvise.offset = READ_ONCE(sqe->off); | |
4899 | req->fadvise.len = READ_ONCE(sqe->len); | |
4900 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4901 | return 0; | |
4902 | } | |
4903 | ||
45d189c6 | 4904 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
4840e418 JA |
4905 | { |
4906 | struct io_fadvise *fa = &req->fadvise; | |
4907 | int ret; | |
4908 | ||
45d189c6 | 4909 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3e69426d JA |
4910 | switch (fa->advice) { |
4911 | case POSIX_FADV_NORMAL: | |
4912 | case POSIX_FADV_RANDOM: | |
4913 | case POSIX_FADV_SEQUENTIAL: | |
4914 | break; | |
4915 | default: | |
4916 | return -EAGAIN; | |
4917 | } | |
4918 | } | |
4840e418 JA |
4919 | |
4920 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
4921 | if (ret < 0) | |
93d2bcd2 | 4922 | req_set_fail(req); |
0bdf3398 | 4923 | __io_req_complete(req, issue_flags, ret, 0); |
4840e418 JA |
4924 | return 0; |
4925 | } | |
4926 | ||
eddc7ef5 JA |
4927 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4928 | { | |
2d74d042 | 4929 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4930 | return -EINVAL; |
26578cda | 4931 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
eddc7ef5 | 4932 | return -EINVAL; |
9c280f90 | 4933 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4934 | return -EBADF; |
eddc7ef5 | 4935 | |
1d9e1288 BM |
4936 | req->statx.dfd = READ_ONCE(sqe->fd); |
4937 | req->statx.mask = READ_ONCE(sqe->len); | |
e62753e4 | 4938 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
1d9e1288 BM |
4939 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4940 | req->statx.flags = READ_ONCE(sqe->statx_flags); | |
eddc7ef5 JA |
4941 | |
4942 | return 0; | |
4943 | } | |
4944 | ||
45d189c6 | 4945 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
eddc7ef5 | 4946 | { |
1d9e1288 | 4947 | struct io_statx *ctx = &req->statx; |
eddc7ef5 JA |
4948 | int ret; |
4949 | ||
59d70013 | 4950 | if (issue_flags & IO_URING_F_NONBLOCK) |
eddc7ef5 JA |
4951 | return -EAGAIN; |
4952 | ||
e62753e4 BM |
4953 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
4954 | ctx->buffer); | |
eddc7ef5 | 4955 | |
eddc7ef5 | 4956 | if (ret < 0) |
93d2bcd2 | 4957 | req_set_fail(req); |
e1e16097 | 4958 | io_req_complete(req, ret); |
eddc7ef5 JA |
4959 | return 0; |
4960 | } | |
4961 | ||
b5dba59e JA |
4962 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4963 | { | |
14587a46 | 4964 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4965 | return -EINVAL; |
b5dba59e | 4966 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
7df778be | 4967 | sqe->rw_flags || sqe->buf_index) |
b5dba59e | 4968 | return -EINVAL; |
9c280f90 | 4969 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4970 | return -EBADF; |
b5dba59e JA |
4971 | |
4972 | req->close.fd = READ_ONCE(sqe->fd); | |
7df778be PB |
4973 | req->close.file_slot = READ_ONCE(sqe->file_index); |
4974 | if (req->close.file_slot && req->close.fd) | |
4975 | return -EINVAL; | |
4976 | ||
b5dba59e | 4977 | return 0; |
b5dba59e JA |
4978 | } |
4979 | ||
889fca73 | 4980 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
b5dba59e | 4981 | { |
9eac1904 | 4982 | struct files_struct *files = current->files; |
3af73b28 | 4983 | struct io_close *close = &req->close; |
9eac1904 | 4984 | struct fdtable *fdt; |
a1fde923 PB |
4985 | struct file *file = NULL; |
4986 | int ret = -EBADF; | |
b5dba59e | 4987 | |
7df778be PB |
4988 | if (req->close.file_slot) { |
4989 | ret = io_close_fixed(req, issue_flags); | |
4990 | goto err; | |
4991 | } | |
4992 | ||
9eac1904 JA |
4993 | spin_lock(&files->file_lock); |
4994 | fdt = files_fdtable(files); | |
4995 | if (close->fd >= fdt->max_fds) { | |
4996 | spin_unlock(&files->file_lock); | |
4997 | goto err; | |
4998 | } | |
4999 | file = fdt->fd[close->fd]; | |
a1fde923 | 5000 | if (!file || file->f_op == &io_uring_fops) { |
9eac1904 JA |
5001 | spin_unlock(&files->file_lock); |
5002 | file = NULL; | |
5003 | goto err; | |
3af73b28 | 5004 | } |
b5dba59e JA |
5005 | |
5006 | /* if the file has a flush method, be safe and punt to async */ | |
45d189c6 | 5007 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
9eac1904 | 5008 | spin_unlock(&files->file_lock); |
0bf0eefd | 5009 | return -EAGAIN; |
a2100672 | 5010 | } |
b5dba59e | 5011 | |
9eac1904 JA |
5012 | ret = __close_fd_get_file(close->fd, &file); |
5013 | spin_unlock(&files->file_lock); | |
5014 | if (ret < 0) { | |
5015 | if (ret == -ENOENT) | |
5016 | ret = -EBADF; | |
5017 | goto err; | |
5018 | } | |
5019 | ||
3af73b28 | 5020 | /* No ->flush() or already async, safely close from here */ |
9eac1904 JA |
5021 | ret = filp_close(file, current->files); |
5022 | err: | |
3af73b28 | 5023 | if (ret < 0) |
93d2bcd2 | 5024 | req_set_fail(req); |
9eac1904 JA |
5025 | if (file) |
5026 | fput(file); | |
889fca73 | 5027 | __io_req_complete(req, issue_flags, ret, 0); |
1a417f4e | 5028 | return 0; |
b5dba59e JA |
5029 | } |
5030 | ||
1155c76a | 5031 | static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
5032 | { |
5033 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 | 5034 | |
5d17b4a4 JA |
5035 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
5036 | return -EINVAL; | |
26578cda PB |
5037 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
5038 | sqe->splice_fd_in)) | |
5d17b4a4 JA |
5039 | return -EINVAL; |
5040 | ||
8ed8d3c3 JA |
5041 | req->sync.off = READ_ONCE(sqe->off); |
5042 | req->sync.len = READ_ONCE(sqe->len); | |
5043 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
5044 | return 0; |
5045 | } | |
5046 | ||
45d189c6 | 5047 | static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 5048 | { |
8ed8d3c3 JA |
5049 | int ret; |
5050 | ||
ac45abc0 | 5051 | /* sync_file_range always requires a blocking context */ |
45d189c6 | 5052 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
5053 | return -EAGAIN; |
5054 | ||
9adbd45d | 5055 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
5056 | req->sync.flags); |
5057 | if (ret < 0) | |
93d2bcd2 | 5058 | req_set_fail(req); |
e1e16097 | 5059 | io_req_complete(req, ret); |
5d17b4a4 JA |
5060 | return 0; |
5061 | } | |
5062 | ||
469956e8 | 5063 | #if defined(CONFIG_NET) |
02d27d89 PB |
5064 | static int io_setup_async_msg(struct io_kiocb *req, |
5065 | struct io_async_msghdr *kmsg) | |
5066 | { | |
e8c2bc1f JA |
5067 | struct io_async_msghdr *async_msg = req->async_data; |
5068 | ||
5069 | if (async_msg) | |
02d27d89 | 5070 | return -EAGAIN; |
e8c2bc1f | 5071 | if (io_alloc_async_data(req)) { |
257e84a5 | 5072 | kfree(kmsg->free_iov); |
02d27d89 PB |
5073 | return -ENOMEM; |
5074 | } | |
e8c2bc1f | 5075 | async_msg = req->async_data; |
02d27d89 | 5076 | req->flags |= REQ_F_NEED_CLEANUP; |
e8c2bc1f | 5077 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
2a780802 | 5078 | async_msg->msg.msg_name = &async_msg->addr; |
257e84a5 PB |
5079 | /* if were using fast_iov, set it to the new one */ |
5080 | if (!async_msg->free_iov) | |
5081 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; | |
5082 | ||
02d27d89 PB |
5083 | return -EAGAIN; |
5084 | } | |
5085 | ||
2ae523ed PB |
5086 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
5087 | struct io_async_msghdr *iomsg) | |
5088 | { | |
2ae523ed | 5089 | iomsg->msg.msg_name = &iomsg->addr; |
257e84a5 | 5090 | iomsg->free_iov = iomsg->fast_iov; |
2ae523ed | 5091 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
257e84a5 | 5092 | req->sr_msg.msg_flags, &iomsg->free_iov); |
2ae523ed PB |
5093 | } |
5094 | ||
93642ef8 PB |
5095 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
5096 | { | |
5097 | int ret; | |
5098 | ||
93642ef8 PB |
5099 | ret = io_sendmsg_copy_hdr(req, req->async_data); |
5100 | if (!ret) | |
5101 | req->flags |= REQ_F_NEED_CLEANUP; | |
5102 | return ret; | |
5103 | } | |
5104 | ||
3529d8c2 | 5105 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 5106 | { |
e47293fd | 5107 | struct io_sr_msg *sr = &req->sr_msg; |
03b1230c | 5108 | |
d2b6f48b PB |
5109 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5110 | return -EINVAL; | |
5111 | ||
270a5940 | 5112 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
fddaface | 5113 | sr->len = READ_ONCE(sqe->len); |
04411806 PB |
5114 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
5115 | if (sr->msg_flags & MSG_DONTWAIT) | |
5116 | req->flags |= REQ_F_NOWAIT; | |
3529d8c2 | 5117 | |
d8768362 JA |
5118 | #ifdef CONFIG_COMPAT |
5119 | if (req->ctx->compat) | |
5120 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
5121 | #endif | |
93642ef8 | 5122 | return 0; |
03b1230c JA |
5123 | } |
5124 | ||
889fca73 | 5125 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 5126 | { |
6b754c8b | 5127 | struct io_async_msghdr iomsg, *kmsg; |
0fa03c62 | 5128 | struct socket *sock; |
7a7cacba | 5129 | unsigned flags; |
0031275d | 5130 | int min_ret = 0; |
0fa03c62 JA |
5131 | int ret; |
5132 | ||
dba4a925 | 5133 | sock = sock_from_file(req->file); |
7a7cacba | 5134 | if (unlikely(!sock)) |
dba4a925 | 5135 | return -ENOTSOCK; |
3529d8c2 | 5136 | |
d886e185 PB |
5137 | if (req_has_async_data(req)) { |
5138 | kmsg = req->async_data; | |
5139 | } else { | |
7a7cacba PB |
5140 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
5141 | if (ret) | |
5142 | return ret; | |
5143 | kmsg = &iomsg; | |
0fa03c62 | 5144 | } |
0fa03c62 | 5145 | |
04411806 PB |
5146 | flags = req->sr_msg.msg_flags; |
5147 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 5148 | flags |= MSG_DONTWAIT; |
0031275d SM |
5149 | if (flags & MSG_WAITALL) |
5150 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
5151 | ||
7a7cacba | 5152 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
0fa03c62 | 5153 | |
7297ce3d PB |
5154 | if (ret < min_ret) { |
5155 | if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK)) | |
5156 | return io_setup_async_msg(req, kmsg); | |
5157 | if (ret == -ERESTARTSYS) | |
5158 | ret = -EINTR; | |
5159 | req_set_fail(req); | |
5160 | } | |
257e84a5 PB |
5161 | /* fast path, check for non-NULL to avoid function call */ |
5162 | if (kmsg->free_iov) | |
5163 | kfree(kmsg->free_iov); | |
99bc4c38 | 5164 | req->flags &= ~REQ_F_NEED_CLEANUP; |
889fca73 | 5165 | __io_req_complete(req, issue_flags, ret, 0); |
5d17b4a4 | 5166 | return 0; |
03b1230c | 5167 | } |
aa1fa28f | 5168 | |
889fca73 | 5169 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 5170 | { |
7a7cacba PB |
5171 | struct io_sr_msg *sr = &req->sr_msg; |
5172 | struct msghdr msg; | |
5173 | struct iovec iov; | |
fddaface | 5174 | struct socket *sock; |
7a7cacba | 5175 | unsigned flags; |
0031275d | 5176 | int min_ret = 0; |
fddaface JA |
5177 | int ret; |
5178 | ||
dba4a925 | 5179 | sock = sock_from_file(req->file); |
7a7cacba | 5180 | if (unlikely(!sock)) |
dba4a925 | 5181 | return -ENOTSOCK; |
fddaface | 5182 | |
7a7cacba PB |
5183 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
5184 | if (unlikely(ret)) | |
14db8411 | 5185 | return ret; |
fddaface | 5186 | |
7a7cacba PB |
5187 | msg.msg_name = NULL; |
5188 | msg.msg_control = NULL; | |
5189 | msg.msg_controllen = 0; | |
5190 | msg.msg_namelen = 0; | |
fddaface | 5191 | |
04411806 PB |
5192 | flags = req->sr_msg.msg_flags; |
5193 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 5194 | flags |= MSG_DONTWAIT; |
0031275d SM |
5195 | if (flags & MSG_WAITALL) |
5196 | min_ret = iov_iter_count(&msg.msg_iter); | |
5197 | ||
7a7cacba PB |
5198 | msg.msg_flags = flags; |
5199 | ret = sock_sendmsg(sock, &msg); | |
7297ce3d PB |
5200 | if (ret < min_ret) { |
5201 | if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK)) | |
5202 | return -EAGAIN; | |
5203 | if (ret == -ERESTARTSYS) | |
5204 | ret = -EINTR; | |
93d2bcd2 | 5205 | req_set_fail(req); |
7297ce3d | 5206 | } |
889fca73 | 5207 | __io_req_complete(req, issue_flags, ret, 0); |
fddaface | 5208 | return 0; |
fddaface JA |
5209 | } |
5210 | ||
1400e697 PB |
5211 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
5212 | struct io_async_msghdr *iomsg) | |
52de1fe1 JA |
5213 | { |
5214 | struct io_sr_msg *sr = &req->sr_msg; | |
5215 | struct iovec __user *uiov; | |
5216 | size_t iov_len; | |
5217 | int ret; | |
5218 | ||
1400e697 PB |
5219 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
5220 | &iomsg->uaddr, &uiov, &iov_len); | |
52de1fe1 JA |
5221 | if (ret) |
5222 | return ret; | |
5223 | ||
5224 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
5225 | if (iov_len > 1) | |
5226 | return -EINVAL; | |
5476dfed | 5227 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
52de1fe1 | 5228 | return -EFAULT; |
5476dfed | 5229 | sr->len = iomsg->fast_iov[0].iov_len; |
257e84a5 | 5230 | iomsg->free_iov = NULL; |
52de1fe1 | 5231 | } else { |
257e84a5 | 5232 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 5233 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
257e84a5 | 5234 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
89cd35c5 | 5235 | false); |
52de1fe1 JA |
5236 | if (ret > 0) |
5237 | ret = 0; | |
5238 | } | |
5239 | ||
5240 | return ret; | |
5241 | } | |
5242 | ||
5243 | #ifdef CONFIG_COMPAT | |
5244 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, | |
1400e697 | 5245 | struct io_async_msghdr *iomsg) |
52de1fe1 | 5246 | { |
52de1fe1 JA |
5247 | struct io_sr_msg *sr = &req->sr_msg; |
5248 | struct compat_iovec __user *uiov; | |
5249 | compat_uptr_t ptr; | |
5250 | compat_size_t len; | |
5251 | int ret; | |
5252 | ||
4af3417a PB |
5253 | ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr, |
5254 | &ptr, &len); | |
52de1fe1 JA |
5255 | if (ret) |
5256 | return ret; | |
5257 | ||
5258 | uiov = compat_ptr(ptr); | |
5259 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
5260 | compat_ssize_t clen; | |
5261 | ||
5262 | if (len > 1) | |
5263 | return -EINVAL; | |
5264 | if (!access_ok(uiov, sizeof(*uiov))) | |
5265 | return -EFAULT; | |
5266 | if (__get_user(clen, &uiov->iov_len)) | |
5267 | return -EFAULT; | |
5268 | if (clen < 0) | |
5269 | return -EINVAL; | |
2d280bc8 | 5270 | sr->len = clen; |
257e84a5 | 5271 | iomsg->free_iov = NULL; |
52de1fe1 | 5272 | } else { |
257e84a5 | 5273 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 5274 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
257e84a5 | 5275 | UIO_FASTIOV, &iomsg->free_iov, |
89cd35c5 | 5276 | &iomsg->msg.msg_iter, true); |
52de1fe1 JA |
5277 | if (ret < 0) |
5278 | return ret; | |
5279 | } | |
5280 | ||
5281 | return 0; | |
5282 | } | |
5283 | #endif | |
5284 | ||
1400e697 PB |
5285 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
5286 | struct io_async_msghdr *iomsg) | |
52de1fe1 | 5287 | { |
1400e697 | 5288 | iomsg->msg.msg_name = &iomsg->addr; |
52de1fe1 JA |
5289 | |
5290 | #ifdef CONFIG_COMPAT | |
5291 | if (req->ctx->compat) | |
1400e697 | 5292 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
fddaface | 5293 | #endif |
52de1fe1 | 5294 | |
1400e697 | 5295 | return __io_recvmsg_copy_hdr(req, iomsg); |
52de1fe1 JA |
5296 | } |
5297 | ||
bcda7baa | 5298 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
51aac424 | 5299 | unsigned int issue_flags) |
bcda7baa JA |
5300 | { |
5301 | struct io_sr_msg *sr = &req->sr_msg; | |
bcda7baa | 5302 | |
51aac424 | 5303 | return io_buffer_select(req, &sr->len, sr->bgid, issue_flags); |
fddaface JA |
5304 | } |
5305 | ||
93642ef8 | 5306 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
aa1fa28f | 5307 | { |
99bc4c38 | 5308 | int ret; |
3529d8c2 | 5309 | |
93642ef8 PB |
5310 | ret = io_recvmsg_copy_hdr(req, req->async_data); |
5311 | if (!ret) | |
5312 | req->flags |= REQ_F_NEED_CLEANUP; | |
5313 | return ret; | |
5314 | } | |
5315 | ||
5316 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
5317 | { | |
5318 | struct io_sr_msg *sr = &req->sr_msg; | |
5319 | ||
d2b6f48b PB |
5320 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5321 | return -EINVAL; | |
5322 | ||
270a5940 | 5323 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
0b7b21e4 | 5324 | sr->len = READ_ONCE(sqe->len); |
bcda7baa | 5325 | sr->bgid = READ_ONCE(sqe->buf_group); |
04411806 PB |
5326 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
5327 | if (sr->msg_flags & MSG_DONTWAIT) | |
5328 | req->flags |= REQ_F_NOWAIT; | |
06b76d44 | 5329 | |
d8768362 JA |
5330 | #ifdef CONFIG_COMPAT |
5331 | if (req->ctx->compat) | |
5332 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
5333 | #endif | |
93642ef8 | 5334 | return 0; |
aa1fa28f JA |
5335 | } |
5336 | ||
889fca73 | 5337 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 5338 | { |
6b754c8b | 5339 | struct io_async_msghdr iomsg, *kmsg; |
03b1230c | 5340 | struct socket *sock; |
7fbb1b54 | 5341 | struct io_buffer *kbuf; |
7a7cacba | 5342 | unsigned flags; |
d1fd1c20 | 5343 | int ret, min_ret = 0; |
45d189c6 | 5344 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
03b1230c | 5345 | |
dba4a925 | 5346 | sock = sock_from_file(req->file); |
7a7cacba | 5347 | if (unlikely(!sock)) |
dba4a925 | 5348 | return -ENOTSOCK; |
3529d8c2 | 5349 | |
d886e185 PB |
5350 | if (req_has_async_data(req)) { |
5351 | kmsg = req->async_data; | |
5352 | } else { | |
7a7cacba PB |
5353 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
5354 | if (ret) | |
681fda8d | 5355 | return ret; |
7a7cacba PB |
5356 | kmsg = &iomsg; |
5357 | } | |
03b1230c | 5358 | |
bc02ef33 | 5359 | if (req->flags & REQ_F_BUFFER_SELECT) { |
51aac424 | 5360 | kbuf = io_recv_buffer_select(req, issue_flags); |
bc02ef33 | 5361 | if (IS_ERR(kbuf)) |
52de1fe1 | 5362 | return PTR_ERR(kbuf); |
7a7cacba | 5363 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
5476dfed PB |
5364 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
5365 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, | |
7a7cacba PB |
5366 | 1, req->sr_msg.len); |
5367 | } | |
52de1fe1 | 5368 | |
04411806 PB |
5369 | flags = req->sr_msg.msg_flags; |
5370 | if (force_nonblock) | |
7a7cacba | 5371 | flags |= MSG_DONTWAIT; |
0031275d SM |
5372 | if (flags & MSG_WAITALL) |
5373 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
5374 | ||
7a7cacba PB |
5375 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
5376 | kmsg->uaddr, flags); | |
7297ce3d PB |
5377 | if (ret < min_ret) { |
5378 | if (ret == -EAGAIN && force_nonblock) | |
5379 | return io_setup_async_msg(req, kmsg); | |
5380 | if (ret == -ERESTARTSYS) | |
5381 | ret = -EINTR; | |
5382 | req_set_fail(req); | |
5383 | } else if ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) { | |
5384 | req_set_fail(req); | |
5385 | } | |
03b1230c | 5386 | |
257e84a5 PB |
5387 | /* fast path, check for non-NULL to avoid function call */ |
5388 | if (kmsg->free_iov) | |
5389 | kfree(kmsg->free_iov); | |
99bc4c38 | 5390 | req->flags &= ~REQ_F_NEED_CLEANUP; |
cc3cec83 | 5391 | __io_req_complete(req, issue_flags, ret, io_put_kbuf(req, issue_flags)); |
03b1230c | 5392 | return 0; |
0fa03c62 | 5393 | } |
5d17b4a4 | 5394 | |
889fca73 | 5395 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 5396 | { |
6b754c8b | 5397 | struct io_buffer *kbuf; |
7a7cacba PB |
5398 | struct io_sr_msg *sr = &req->sr_msg; |
5399 | struct msghdr msg; | |
5400 | void __user *buf = sr->buf; | |
fddaface | 5401 | struct socket *sock; |
7a7cacba PB |
5402 | struct iovec iov; |
5403 | unsigned flags; | |
d1fd1c20 | 5404 | int ret, min_ret = 0; |
45d189c6 | 5405 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
fddaface | 5406 | |
dba4a925 | 5407 | sock = sock_from_file(req->file); |
7a7cacba | 5408 | if (unlikely(!sock)) |
dba4a925 | 5409 | return -ENOTSOCK; |
fddaface | 5410 | |
bc02ef33 | 5411 | if (req->flags & REQ_F_BUFFER_SELECT) { |
51aac424 | 5412 | kbuf = io_recv_buffer_select(req, issue_flags); |
bcda7baa JA |
5413 | if (IS_ERR(kbuf)) |
5414 | return PTR_ERR(kbuf); | |
7a7cacba | 5415 | buf = u64_to_user_ptr(kbuf->addr); |
bc02ef33 | 5416 | } |
bcda7baa | 5417 | |
7a7cacba | 5418 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
14c32eee PB |
5419 | if (unlikely(ret)) |
5420 | goto out_free; | |
fddaface | 5421 | |
7a7cacba PB |
5422 | msg.msg_name = NULL; |
5423 | msg.msg_control = NULL; | |
5424 | msg.msg_controllen = 0; | |
5425 | msg.msg_namelen = 0; | |
5426 | msg.msg_iocb = NULL; | |
5427 | msg.msg_flags = 0; | |
fddaface | 5428 | |
04411806 PB |
5429 | flags = req->sr_msg.msg_flags; |
5430 | if (force_nonblock) | |
7a7cacba | 5431 | flags |= MSG_DONTWAIT; |
0031275d SM |
5432 | if (flags & MSG_WAITALL) |
5433 | min_ret = iov_iter_count(&msg.msg_iter); | |
5434 | ||
7a7cacba | 5435 | ret = sock_recvmsg(sock, &msg, flags); |
7297ce3d PB |
5436 | if (ret < min_ret) { |
5437 | if (ret == -EAGAIN && force_nonblock) | |
5438 | return -EAGAIN; | |
5439 | if (ret == -ERESTARTSYS) | |
5440 | ret = -EINTR; | |
5441 | req_set_fail(req); | |
5442 | } else if ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) { | |
0d7c1153 | 5443 | out_free: |
93d2bcd2 | 5444 | req_set_fail(req); |
7297ce3d | 5445 | } |
cc3cec83 JA |
5446 | |
5447 | __io_req_complete(req, issue_flags, ret, io_put_kbuf(req, issue_flags)); | |
fddaface | 5448 | return 0; |
fddaface JA |
5449 | } |
5450 | ||
3529d8c2 | 5451 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 | 5452 | { |
8ed8d3c3 JA |
5453 | struct io_accept *accept = &req->accept; |
5454 | ||
14587a46 | 5455 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
17f2fe35 | 5456 | return -EINVAL; |
aaa4db12 | 5457 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
5458 | return -EINVAL; |
5459 | ||
d55e5f5b JA |
5460 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5461 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 5462 | accept->flags = READ_ONCE(sqe->accept_flags); |
09952e3e | 5463 | accept->nofile = rlimit(RLIMIT_NOFILE); |
a7083ad5 | 5464 | |
aaa4db12 PB |
5465 | accept->file_slot = READ_ONCE(sqe->file_index); |
5466 | if (accept->file_slot && ((req->open.how.flags & O_CLOEXEC) || | |
5467 | (accept->flags & SOCK_CLOEXEC))) | |
5468 | return -EINVAL; | |
a7083ad5 PB |
5469 | if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) |
5470 | return -EINVAL; | |
5471 | if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK)) | |
5472 | accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK; | |
8ed8d3c3 | 5473 | return 0; |
8ed8d3c3 | 5474 | } |
17f2fe35 | 5475 | |
889fca73 | 5476 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 JA |
5477 | { |
5478 | struct io_accept *accept = &req->accept; | |
45d189c6 | 5479 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ac45abc0 | 5480 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
aaa4db12 | 5481 | bool fixed = !!accept->file_slot; |
a7083ad5 PB |
5482 | struct file *file; |
5483 | int ret, fd; | |
8ed8d3c3 | 5484 | |
e697deed JX |
5485 | if (req->file->f_flags & O_NONBLOCK) |
5486 | req->flags |= REQ_F_NOWAIT; | |
5487 | ||
aaa4db12 PB |
5488 | if (!fixed) { |
5489 | fd = __get_unused_fd_flags(accept->flags, accept->nofile); | |
5490 | if (unlikely(fd < 0)) | |
5491 | return fd; | |
5492 | } | |
a7083ad5 PB |
5493 | file = do_accept(req->file, file_flags, accept->addr, accept->addr_len, |
5494 | accept->flags); | |
5495 | if (IS_ERR(file)) { | |
aaa4db12 PB |
5496 | if (!fixed) |
5497 | put_unused_fd(fd); | |
a7083ad5 PB |
5498 | ret = PTR_ERR(file); |
5499 | if (ret == -EAGAIN && force_nonblock) | |
5500 | return -EAGAIN; | |
ac45abc0 PB |
5501 | if (ret == -ERESTARTSYS) |
5502 | ret = -EINTR; | |
93d2bcd2 | 5503 | req_set_fail(req); |
aaa4db12 | 5504 | } else if (!fixed) { |
a7083ad5 PB |
5505 | fd_install(fd, file); |
5506 | ret = fd; | |
aaa4db12 PB |
5507 | } else { |
5508 | ret = io_install_fixed_file(req, file, issue_flags, | |
5509 | accept->file_slot - 1); | |
ac45abc0 | 5510 | } |
889fca73 | 5511 | __io_req_complete(req, issue_flags, ret, 0); |
17f2fe35 | 5512 | return 0; |
8ed8d3c3 JA |
5513 | } |
5514 | ||
93642ef8 PB |
5515 | static int io_connect_prep_async(struct io_kiocb *req) |
5516 | { | |
5517 | struct io_async_connect *io = req->async_data; | |
5518 | struct io_connect *conn = &req->connect; | |
5519 | ||
5520 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); | |
5521 | } | |
5522 | ||
3529d8c2 | 5523 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 | 5524 | { |
3529d8c2 | 5525 | struct io_connect *conn = &req->connect; |
f499a021 | 5526 | |
14587a46 | 5527 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3fbb51c1 | 5528 | return -EINVAL; |
26578cda PB |
5529 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags || |
5530 | sqe->splice_fd_in) | |
3fbb51c1 JA |
5531 | return -EINVAL; |
5532 | ||
3529d8c2 JA |
5533 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5534 | conn->addr_len = READ_ONCE(sqe->addr2); | |
93642ef8 | 5535 | return 0; |
f499a021 JA |
5536 | } |
5537 | ||
889fca73 | 5538 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
f8e85cf2 | 5539 | { |
e8c2bc1f | 5540 | struct io_async_connect __io, *io; |
f8e85cf2 | 5541 | unsigned file_flags; |
3fbb51c1 | 5542 | int ret; |
45d189c6 | 5543 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
f8e85cf2 | 5544 | |
d886e185 | 5545 | if (req_has_async_data(req)) { |
e8c2bc1f | 5546 | io = req->async_data; |
f499a021 | 5547 | } else { |
3529d8c2 JA |
5548 | ret = move_addr_to_kernel(req->connect.addr, |
5549 | req->connect.addr_len, | |
e8c2bc1f | 5550 | &__io.address); |
f499a021 JA |
5551 | if (ret) |
5552 | goto out; | |
5553 | io = &__io; | |
5554 | } | |
5555 | ||
3fbb51c1 JA |
5556 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
5557 | ||
e8c2bc1f | 5558 | ret = __sys_connect_file(req->file, &io->address, |
3fbb51c1 | 5559 | req->connect.addr_len, file_flags); |
87f80d62 | 5560 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
d886e185 | 5561 | if (req_has_async_data(req)) |
b7bb4f7d | 5562 | return -EAGAIN; |
e8c2bc1f | 5563 | if (io_alloc_async_data(req)) { |
f499a021 JA |
5564 | ret = -ENOMEM; |
5565 | goto out; | |
5566 | } | |
e8c2bc1f | 5567 | memcpy(req->async_data, &__io, sizeof(__io)); |
f8e85cf2 | 5568 | return -EAGAIN; |
f499a021 | 5569 | } |
f8e85cf2 JA |
5570 | if (ret == -ERESTARTSYS) |
5571 | ret = -EINTR; | |
f499a021 | 5572 | out: |
4e88d6e7 | 5573 | if (ret < 0) |
93d2bcd2 | 5574 | req_set_fail(req); |
889fca73 | 5575 | __io_req_complete(req, issue_flags, ret, 0); |
f8e85cf2 | 5576 | return 0; |
469956e8 Y |
5577 | } |
5578 | #else /* !CONFIG_NET */ | |
99a10081 JA |
5579 | #define IO_NETOP_FN(op) \ |
5580 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ | |
5581 | { \ | |
5582 | return -EOPNOTSUPP; \ | |
5583 | } | |
5584 | ||
5585 | #define IO_NETOP_PREP(op) \ | |
5586 | IO_NETOP_FN(op) \ | |
5587 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ | |
5588 | { \ | |
5589 | return -EOPNOTSUPP; \ | |
5590 | } \ | |
5591 | ||
5592 | #define IO_NETOP_PREP_ASYNC(op) \ | |
5593 | IO_NETOP_PREP(op) \ | |
5594 | static int io_##op##_prep_async(struct io_kiocb *req) \ | |
5595 | { \ | |
5596 | return -EOPNOTSUPP; \ | |
5597 | } | |
5598 | ||
5599 | IO_NETOP_PREP_ASYNC(sendmsg); | |
5600 | IO_NETOP_PREP_ASYNC(recvmsg); | |
5601 | IO_NETOP_PREP_ASYNC(connect); | |
5602 | IO_NETOP_PREP(accept); | |
5603 | IO_NETOP_FN(send); | |
5604 | IO_NETOP_FN(recv); | |
469956e8 | 5605 | #endif /* CONFIG_NET */ |
f8e85cf2 | 5606 | |
adc8682e OL |
5607 | #ifdef CONFIG_NET_RX_BUSY_POLL |
5608 | ||
5609 | #define NAPI_TIMEOUT (60 * SEC_CONVERSION) | |
5610 | ||
5611 | struct napi_entry { | |
5612 | struct list_head list; | |
5613 | unsigned int napi_id; | |
5614 | unsigned long timeout; | |
5615 | }; | |
5616 | ||
5617 | /* | |
5618 | * Add busy poll NAPI ID from sk. | |
5619 | */ | |
5620 | static void io_add_napi(struct file *file, struct io_ring_ctx *ctx) | |
5621 | { | |
5622 | unsigned int napi_id; | |
5623 | struct socket *sock; | |
5624 | struct sock *sk; | |
5625 | struct napi_entry *ne; | |
5626 | ||
5627 | if (!net_busy_loop_on()) | |
5628 | return; | |
5629 | ||
5630 | sock = sock_from_file(file); | |
5631 | if (!sock) | |
5632 | return; | |
5633 | ||
5634 | sk = sock->sk; | |
5635 | if (!sk) | |
5636 | return; | |
5637 | ||
5638 | napi_id = READ_ONCE(sk->sk_napi_id); | |
5639 | ||
5640 | /* Non-NAPI IDs can be rejected */ | |
5641 | if (napi_id < MIN_NAPI_ID) | |
5642 | return; | |
5643 | ||
5644 | spin_lock(&ctx->napi_lock); | |
5645 | list_for_each_entry(ne, &ctx->napi_list, list) { | |
5646 | if (ne->napi_id == napi_id) { | |
5647 | ne->timeout = jiffies + NAPI_TIMEOUT; | |
5648 | goto out; | |
5649 | } | |
5650 | } | |
5651 | ||
5652 | ne = kmalloc(sizeof(*ne), GFP_NOWAIT); | |
5653 | if (!ne) | |
5654 | goto out; | |
5655 | ||
5656 | ne->napi_id = napi_id; | |
5657 | ne->timeout = jiffies + NAPI_TIMEOUT; | |
5658 | list_add_tail(&ne->list, &ctx->napi_list); | |
5659 | out: | |
5660 | spin_unlock(&ctx->napi_lock); | |
5661 | } | |
5662 | ||
5663 | static inline void io_check_napi_entry_timeout(struct napi_entry *ne) | |
5664 | { | |
5665 | if (time_after(jiffies, ne->timeout)) { | |
5666 | list_del(&ne->list); | |
5667 | kfree(ne); | |
5668 | } | |
5669 | } | |
5670 | ||
5671 | /* | |
5672 | * Busy poll if globally on and supporting sockets found | |
5673 | */ | |
5674 | static bool io_napi_busy_loop(struct list_head *napi_list) | |
5675 | { | |
5676 | struct napi_entry *ne, *n; | |
5677 | ||
5678 | list_for_each_entry_safe(ne, n, napi_list, list) { | |
5679 | napi_busy_loop(ne->napi_id, NULL, NULL, true, | |
5680 | BUSY_POLL_BUDGET); | |
5681 | io_check_napi_entry_timeout(ne); | |
5682 | } | |
5683 | return !list_empty(napi_list); | |
5684 | } | |
5685 | ||
5686 | static void io_free_napi_list(struct io_ring_ctx *ctx) | |
5687 | { | |
5688 | spin_lock(&ctx->napi_lock); | |
5689 | while (!list_empty(&ctx->napi_list)) { | |
5690 | struct napi_entry *ne = | |
5691 | list_first_entry(&ctx->napi_list, struct napi_entry, | |
5692 | list); | |
5693 | ||
5694 | list_del(&ne->list); | |
5695 | kfree(ne); | |
5696 | } | |
5697 | spin_unlock(&ctx->napi_lock); | |
5698 | } | |
5699 | #else | |
5700 | static inline void io_add_napi(struct file *file, struct io_ring_ctx *ctx) | |
5701 | { | |
5702 | } | |
5703 | ||
5704 | static inline void io_free_napi_list(struct io_ring_ctx *ctx) | |
5705 | { | |
5706 | } | |
5707 | #endif /* CONFIG_NET_RX_BUSY_POLL */ | |
5708 | ||
d7718a9d JA |
5709 | struct io_poll_table { |
5710 | struct poll_table_struct pt; | |
5711 | struct io_kiocb *req; | |
68b11e8b | 5712 | int nr_entries; |
d7718a9d JA |
5713 | int error; |
5714 | }; | |
ce593a6c | 5715 | |
aa43477b PB |
5716 | #define IO_POLL_CANCEL_FLAG BIT(31) |
5717 | #define IO_POLL_REF_MASK ((1u << 20)-1) | |
6d816e08 | 5718 | |
aa43477b PB |
5719 | /* |
5720 | * If refs part of ->poll_refs (see IO_POLL_REF_MASK) is 0, it's free. We can | |
5721 | * bump it and acquire ownership. It's disallowed to modify requests while not | |
5722 | * owning it, that prevents from races for enqueueing task_work's and b/w | |
5723 | * arming poll and wakeups. | |
5724 | */ | |
5725 | static inline bool io_poll_get_ownership(struct io_kiocb *req) | |
5726 | { | |
5727 | return !(atomic_fetch_inc(&req->poll_refs) & IO_POLL_REF_MASK); | |
d7718a9d JA |
5728 | } |
5729 | ||
aa43477b | 5730 | static void io_poll_mark_cancelled(struct io_kiocb *req) |
74ce6ce4 | 5731 | { |
aa43477b | 5732 | atomic_or(IO_POLL_CANCEL_FLAG, &req->poll_refs); |
74ce6ce4 JA |
5733 | } |
5734 | ||
d4e7cd36 | 5735 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
18bceab1 | 5736 | { |
e8c2bc1f | 5737 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
d4e7cd36 | 5738 | if (req->opcode == IORING_OP_POLL_ADD) |
e8c2bc1f | 5739 | return req->async_data; |
d4e7cd36 JA |
5740 | return req->apoll->double_poll; |
5741 | } | |
5742 | ||
5743 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) | |
5744 | { | |
5745 | if (req->opcode == IORING_OP_POLL_ADD) | |
5746 | return &req->poll; | |
5747 | return &req->apoll->poll; | |
5748 | } | |
5749 | ||
5641897a | 5750 | static void io_poll_req_insert(struct io_kiocb *req) |
d4e7cd36 | 5751 | { |
5641897a PB |
5752 | struct io_ring_ctx *ctx = req->ctx; |
5753 | struct hlist_head *list; | |
18bceab1 | 5754 | |
5641897a PB |
5755 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
5756 | hlist_add_head(&req->hash_node, list); | |
18bceab1 JA |
5757 | } |
5758 | ||
5641897a PB |
5759 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, |
5760 | wait_queue_func_t wake_func) | |
18bceab1 | 5761 | { |
5641897a | 5762 | poll->head = NULL; |
5641897a PB |
5763 | #define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP) |
5764 | /* mask in events that we always want/need */ | |
5765 | poll->events = events | IO_POLL_UNMASK; | |
5766 | INIT_LIST_HEAD(&poll->wait.entry); | |
5767 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
18bceab1 JA |
5768 | } |
5769 | ||
aa43477b | 5770 | static inline void io_poll_remove_entry(struct io_poll_iocb *poll) |
18bceab1 | 5771 | { |
791f3465 | 5772 | struct wait_queue_head *head = smp_load_acquire(&poll->head); |
18bceab1 | 5773 | |
791f3465 PB |
5774 | if (head) { |
5775 | spin_lock_irq(&head->lock); | |
5776 | list_del_init(&poll->wait.entry); | |
5777 | poll->head = NULL; | |
5778 | spin_unlock_irq(&head->lock); | |
5779 | } | |
aa43477b | 5780 | } |
18bceab1 | 5781 | |
aa43477b PB |
5782 | static void io_poll_remove_entries(struct io_kiocb *req) |
5783 | { | |
5784 | struct io_poll_iocb *poll = io_poll_get_single(req); | |
5785 | struct io_poll_iocb *poll_double = io_poll_get_double(req); | |
18bceab1 | 5786 | |
791f3465 PB |
5787 | /* |
5788 | * While we hold the waitqueue lock and the waitqueue is nonempty, | |
5789 | * wake_up_pollfree() will wait for us. However, taking the waitqueue | |
5790 | * lock in the first place can race with the waitqueue being freed. | |
5791 | * | |
5792 | * We solve this as eventpoll does: by taking advantage of the fact that | |
5793 | * all users of wake_up_pollfree() will RCU-delay the actual free. If | |
5794 | * we enter rcu_read_lock() and see that the pointer to the queue is | |
5795 | * non-NULL, we can then lock it without the memory being freed out from | |
5796 | * under us. | |
5797 | * | |
5798 | * Keep holding rcu_read_lock() as long as we hold the queue lock, in | |
5799 | * case the caller deletes the entry from the queue, leaving it empty. | |
5800 | * In that case, only RCU prevents the queue memory from being freed. | |
5801 | */ | |
5802 | rcu_read_lock(); | |
5803 | io_poll_remove_entry(poll); | |
5804 | if (poll_double) | |
aa43477b | 5805 | io_poll_remove_entry(poll_double); |
791f3465 | 5806 | rcu_read_unlock(); |
18bceab1 JA |
5807 | } |
5808 | ||
aa43477b PB |
5809 | /* |
5810 | * All poll tw should go through this. Checks for poll events, manages | |
5811 | * references, does rewait, etc. | |
5812 | * | |
5813 | * Returns a negative error on failure. >0 when no action require, which is | |
5814 | * either spurious wakeup or multishot CQE is served. 0 when it's done with | |
5815 | * the request, then the mask is stored in req->result. | |
5816 | */ | |
5817 | static int io_poll_check_events(struct io_kiocb *req) | |
18bceab1 | 5818 | { |
74ce6ce4 | 5819 | struct io_ring_ctx *ctx = req->ctx; |
d4e7cd36 | 5820 | struct io_poll_iocb *poll = io_poll_get_single(req); |
aa43477b | 5821 | int v; |
18bceab1 | 5822 | |
316319e8 | 5823 | /* req->task == current here, checking PF_EXITING is safe */ |
e09ee510 | 5824 | if (unlikely(req->task->flags & PF_EXITING)) |
aa43477b | 5825 | io_poll_mark_cancelled(req); |
18bceab1 | 5826 | |
aa43477b PB |
5827 | do { |
5828 | v = atomic_read(&req->poll_refs); | |
74ce6ce4 | 5829 | |
aa43477b PB |
5830 | /* tw handler should be the owner, and so have some references */ |
5831 | if (WARN_ON_ONCE(!(v & IO_POLL_REF_MASK))) | |
5832 | return 0; | |
5833 | if (v & IO_POLL_CANCEL_FLAG) | |
5834 | return -ECANCELED; | |
8706e04e | 5835 | |
aa43477b PB |
5836 | if (!req->result) { |
5837 | struct poll_table_struct pt = { ._key = poll->events }; | |
18bceab1 | 5838 | |
aa43477b | 5839 | req->result = vfs_poll(req->file, &pt) & poll->events; |
c8b5e260 | 5840 | } |
74ce6ce4 | 5841 | |
aa43477b PB |
5842 | /* multishot, just fill an CQE and proceed */ |
5843 | if (req->result && !(poll->events & EPOLLONESHOT)) { | |
5844 | __poll_t mask = mangle_poll(req->result & poll->events); | |
5845 | bool filled; | |
18bceab1 | 5846 | |
aa43477b PB |
5847 | spin_lock(&ctx->completion_lock); |
5848 | filled = io_fill_cqe_aux(ctx, req->user_data, mask, | |
5849 | IORING_CQE_F_MORE); | |
5850 | io_commit_cqring(ctx); | |
5851 | spin_unlock(&ctx->completion_lock); | |
5852 | if (unlikely(!filled)) | |
5853 | return -ECANCELED; | |
5854 | io_cqring_ev_posted(ctx); | |
adc8682e | 5855 | io_add_napi(req->file, ctx); |
aa43477b PB |
5856 | } else if (req->result) { |
5857 | return 0; | |
5858 | } | |
18bceab1 | 5859 | |
aa43477b PB |
5860 | /* |
5861 | * Release all references, retry if someone tried to restart | |
5862 | * task_work while we were executing it. | |
5863 | */ | |
5864 | } while (atomic_sub_return(v & IO_POLL_REF_MASK, &req->poll_refs)); | |
18bceab1 | 5865 | |
18bceab1 JA |
5866 | return 1; |
5867 | } | |
5868 | ||
aa43477b | 5869 | static void io_poll_task_func(struct io_kiocb *req, bool *locked) |
18bceab1 | 5870 | { |
18bceab1 | 5871 | struct io_ring_ctx *ctx = req->ctx; |
aa43477b | 5872 | int ret; |
18bceab1 | 5873 | |
aa43477b PB |
5874 | ret = io_poll_check_events(req); |
5875 | if (ret > 0) | |
5876 | return; | |
5877 | ||
5878 | if (!ret) { | |
5879 | req->result = mangle_poll(req->result & req->poll.events); | |
e27414be | 5880 | } else { |
aa43477b PB |
5881 | req->result = ret; |
5882 | req_set_fail(req); | |
a62682f9 | 5883 | } |
aa43477b PB |
5884 | |
5885 | io_poll_remove_entries(req); | |
5886 | spin_lock(&ctx->completion_lock); | |
5887 | hash_del(&req->hash_node); | |
5888 | __io_req_complete_post(req, req->result, 0); | |
5889 | io_commit_cqring(ctx); | |
5890 | spin_unlock(&ctx->completion_lock); | |
5891 | io_cqring_ev_posted(ctx); | |
18bceab1 JA |
5892 | } |
5893 | ||
aa43477b | 5894 | static void io_apoll_task_func(struct io_kiocb *req, bool *locked) |
18bceab1 JA |
5895 | { |
5896 | struct io_ring_ctx *ctx = req->ctx; | |
aa43477b | 5897 | int ret; |
18bceab1 | 5898 | |
aa43477b PB |
5899 | ret = io_poll_check_events(req); |
5900 | if (ret > 0) | |
5901 | return; | |
18bceab1 | 5902 | |
aa43477b PB |
5903 | io_poll_remove_entries(req); |
5904 | spin_lock(&ctx->completion_lock); | |
5905 | hash_del(&req->hash_node); | |
5906 | spin_unlock(&ctx->completion_lock); | |
18bceab1 | 5907 | |
aa43477b PB |
5908 | if (!ret) |
5909 | io_req_task_submit(req, locked); | |
5910 | else | |
5911 | io_req_complete_failed(req, ret); | |
18bceab1 JA |
5912 | } |
5913 | ||
aa43477b PB |
5914 | static void __io_poll_execute(struct io_kiocb *req, int mask) |
5915 | { | |
5916 | req->result = mask; | |
5917 | if (req->opcode == IORING_OP_POLL_ADD) | |
5918 | req->io_task_work.func = io_poll_task_func; | |
5919 | else | |
5920 | req->io_task_work.func = io_apoll_task_func; | |
5921 | ||
502c87d6 | 5922 | trace_io_uring_task_add(req->ctx, req, req->user_data, req->opcode, mask); |
aa43477b PB |
5923 | io_req_task_work_add(req, false); |
5924 | } | |
5925 | ||
5926 | static inline void io_poll_execute(struct io_kiocb *req, int res) | |
5927 | { | |
5928 | if (io_poll_get_ownership(req)) | |
5929 | __io_poll_execute(req, res); | |
5930 | } | |
5931 | ||
5932 | static void io_poll_cancel_req(struct io_kiocb *req) | |
5933 | { | |
5934 | io_poll_mark_cancelled(req); | |
5935 | /* kick tw, which should complete the request */ | |
5936 | io_poll_execute(req, 0); | |
5937 | } | |
5938 | ||
5939 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, | |
5940 | void *key) | |
18bceab1 JA |
5941 | { |
5942 | struct io_kiocb *req = wait->private; | |
aa43477b PB |
5943 | struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb, |
5944 | wait); | |
18bceab1 JA |
5945 | __poll_t mask = key_to_poll(key); |
5946 | ||
791f3465 PB |
5947 | if (unlikely(mask & POLLFREE)) { |
5948 | io_poll_mark_cancelled(req); | |
5949 | /* we have to kick tw in case it's not already */ | |
5950 | io_poll_execute(req, 0); | |
5951 | ||
5952 | /* | |
5953 | * If the waitqueue is being freed early but someone is already | |
5954 | * holds ownership over it, we have to tear down the request as | |
5955 | * best we can. That means immediately removing the request from | |
5956 | * its waitqueue and preventing all further accesses to the | |
5957 | * waitqueue via the request. | |
5958 | */ | |
5959 | list_del_init(&poll->wait.entry); | |
5960 | ||
5961 | /* | |
5962 | * Careful: this *must* be the last step, since as soon | |
5963 | * as req->head is NULL'ed out, the request can be | |
5964 | * completed and freed, since aio_poll_complete_work() | |
5965 | * will no longer need to take the waitqueue lock. | |
5966 | */ | |
5967 | smp_store_release(&poll->head, NULL); | |
5968 | return 1; | |
5969 | } | |
5970 | ||
aa43477b | 5971 | /* for instances that support it check for an event match first */ |
18bceab1 JA |
5972 | if (mask && !(mask & poll->events)) |
5973 | return 0; | |
5974 | ||
eb0089d6 PB |
5975 | if (io_poll_get_ownership(req)) { |
5976 | /* optional, saves extra locking for removal in tw handler */ | |
5977 | if (mask && poll->events & EPOLLONESHOT) { | |
5978 | list_del_init(&poll->wait.entry); | |
5979 | poll->head = NULL; | |
5980 | } | |
aa43477b | 5981 | __io_poll_execute(req, mask); |
eb0089d6 | 5982 | } |
18bceab1 | 5983 | return 1; |
18bceab1 JA |
5984 | } |
5985 | ||
5986 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, | |
807abcb0 JA |
5987 | struct wait_queue_head *head, |
5988 | struct io_poll_iocb **poll_ptr) | |
18bceab1 JA |
5989 | { |
5990 | struct io_kiocb *req = pt->req; | |
5991 | ||
5992 | /* | |
68b11e8b PB |
5993 | * The file being polled uses multiple waitqueues for poll handling |
5994 | * (e.g. one for read, one for write). Setup a separate io_poll_iocb | |
5995 | * if this happens. | |
18bceab1 | 5996 | */ |
68b11e8b | 5997 | if (unlikely(pt->nr_entries)) { |
aa43477b | 5998 | struct io_poll_iocb *first = poll; |
58852d4d | 5999 | |
23a65db8 | 6000 | /* double add on the same waitqueue head, ignore */ |
aa43477b | 6001 | if (first->head == head) |
23a65db8 | 6002 | return; |
18bceab1 | 6003 | /* already have a 2nd entry, fail a third attempt */ |
807abcb0 | 6004 | if (*poll_ptr) { |
23a65db8 PB |
6005 | if ((*poll_ptr)->head == head) |
6006 | return; | |
18bceab1 JA |
6007 | pt->error = -EINVAL; |
6008 | return; | |
6009 | } | |
aa43477b | 6010 | |
18bceab1 JA |
6011 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
6012 | if (!poll) { | |
6013 | pt->error = -ENOMEM; | |
6014 | return; | |
6015 | } | |
aa43477b | 6016 | io_init_poll_iocb(poll, first->events, first->wait.func); |
807abcb0 | 6017 | *poll_ptr = poll; |
d886e185 PB |
6018 | if (req->opcode == IORING_OP_POLL_ADD) |
6019 | req->flags |= REQ_F_ASYNC_DATA; | |
18bceab1 JA |
6020 | } |
6021 | ||
68b11e8b | 6022 | pt->nr_entries++; |
18bceab1 | 6023 | poll->head = head; |
aa43477b | 6024 | poll->wait.private = req; |
a31eb4a2 JX |
6025 | |
6026 | if (poll->events & EPOLLEXCLUSIVE) | |
6027 | add_wait_queue_exclusive(head, &poll->wait); | |
6028 | else | |
6029 | add_wait_queue(head, &poll->wait); | |
18bceab1 JA |
6030 | } |
6031 | ||
aa43477b | 6032 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
18bceab1 JA |
6033 | struct poll_table_struct *p) |
6034 | { | |
6035 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
d7718a9d | 6036 | |
aa43477b PB |
6037 | __io_queue_proc(&pt->req->poll, pt, head, |
6038 | (struct io_poll_iocb **) &pt->req->async_data); | |
d7718a9d JA |
6039 | } |
6040 | ||
aa43477b PB |
6041 | static int __io_arm_poll_handler(struct io_kiocb *req, |
6042 | struct io_poll_iocb *poll, | |
6043 | struct io_poll_table *ipt, __poll_t mask) | |
d7718a9d JA |
6044 | { |
6045 | struct io_ring_ctx *ctx = req->ctx; | |
aa43477b | 6046 | int v; |
d7718a9d | 6047 | |
4d52f338 | 6048 | INIT_HLIST_NODE(&req->hash_node); |
aa43477b | 6049 | io_init_poll_iocb(poll, mask, io_poll_wake); |
b90cd197 | 6050 | poll->file = req->file; |
18bceab1 | 6051 | poll->wait.private = req; |
d7718a9d JA |
6052 | |
6053 | ipt->pt._key = mask; | |
6054 | ipt->req = req; | |
68b11e8b PB |
6055 | ipt->error = 0; |
6056 | ipt->nr_entries = 0; | |
d7718a9d | 6057 | |
aa43477b PB |
6058 | /* |
6059 | * Take the ownership to delay any tw execution up until we're done | |
6060 | * with poll arming. see io_poll_get_ownership(). | |
6061 | */ | |
6062 | atomic_set(&req->poll_refs, 1); | |
d7718a9d | 6063 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
aa43477b PB |
6064 | |
6065 | if (mask && (poll->events & EPOLLONESHOT)) { | |
6066 | io_poll_remove_entries(req); | |
6067 | /* no one else has access to the req, forget about the ref */ | |
6068 | return mask; | |
6069 | } | |
6070 | if (!mask && unlikely(ipt->error || !ipt->nr_entries)) { | |
6071 | io_poll_remove_entries(req); | |
6072 | if (!ipt->error) | |
6073 | ipt->error = -EINVAL; | |
6074 | return 0; | |
6075 | } | |
d7718a9d | 6076 | |
79ebeaee | 6077 | spin_lock(&ctx->completion_lock); |
aa43477b PB |
6078 | io_poll_req_insert(req); |
6079 | spin_unlock(&ctx->completion_lock); | |
6080 | ||
6081 | if (mask) { | |
6082 | /* can't multishot if failed, just queue the event we've got */ | |
6083 | if (unlikely(ipt->error || !ipt->nr_entries)) | |
6084 | poll->events |= EPOLLONESHOT; | |
6085 | __io_poll_execute(req, mask); | |
6086 | return 0; | |
d7718a9d | 6087 | } |
adc8682e | 6088 | io_add_napi(req->file, req->ctx); |
d7718a9d | 6089 | |
aa43477b PB |
6090 | /* |
6091 | * Release ownership. If someone tried to queue a tw while it was | |
6092 | * locked, kick it off for them. | |
6093 | */ | |
6094 | v = atomic_dec_return(&req->poll_refs); | |
6095 | if (unlikely(v & IO_POLL_REF_MASK)) | |
6096 | __io_poll_execute(req, 0); | |
6097 | return 0; | |
6098 | } | |
6099 | ||
6100 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
6101 | struct poll_table_struct *p) | |
6102 | { | |
6103 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
6104 | struct async_poll *apoll = pt->req->apoll; | |
6105 | ||
6106 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); | |
d7718a9d JA |
6107 | } |
6108 | ||
59b735ae OL |
6109 | enum { |
6110 | IO_APOLL_OK, | |
6111 | IO_APOLL_ABORTED, | |
6112 | IO_APOLL_READY | |
6113 | }; | |
6114 | ||
6115 | static int io_arm_poll_handler(struct io_kiocb *req) | |
d7718a9d JA |
6116 | { |
6117 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
6118 | struct io_ring_ctx *ctx = req->ctx; | |
6119 | struct async_poll *apoll; | |
6120 | struct io_poll_table ipt; | |
aa43477b PB |
6121 | __poll_t mask = EPOLLONESHOT | POLLERR | POLLPRI; |
6122 | int ret; | |
d7718a9d | 6123 | |
b2d9c3da PB |
6124 | if (!def->pollin && !def->pollout) |
6125 | return IO_APOLL_ABORTED; | |
658d0a40 PB |
6126 | if (!file_can_poll(req->file) || (req->flags & REQ_F_POLLED)) |
6127 | return IO_APOLL_ABORTED; | |
b2d9c3da PB |
6128 | |
6129 | if (def->pollin) { | |
b2d9c3da PB |
6130 | mask |= POLLIN | POLLRDNORM; |
6131 | ||
6132 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ | |
6133 | if ((req->opcode == IORING_OP_RECVMSG) && | |
6134 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) | |
6135 | mask &= ~POLLIN; | |
6136 | } else { | |
b2d9c3da PB |
6137 | mask |= POLLOUT | POLLWRNORM; |
6138 | } | |
6139 | ||
d7718a9d JA |
6140 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); |
6141 | if (unlikely(!apoll)) | |
59b735ae | 6142 | return IO_APOLL_ABORTED; |
807abcb0 | 6143 | apoll->double_poll = NULL; |
d7718a9d | 6144 | req->apoll = apoll; |
b2d9c3da | 6145 | req->flags |= REQ_F_POLLED; |
d7718a9d JA |
6146 | ipt.pt._qproc = io_async_queue_proc; |
6147 | ||
aa43477b | 6148 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask); |
41a5169c HX |
6149 | if (ret || ipt.error) |
6150 | return ret ? IO_APOLL_READY : IO_APOLL_ABORTED; | |
6151 | ||
502c87d6 | 6152 | trace_io_uring_poll_arm(ctx, req, req->user_data, req->opcode, |
236daeae | 6153 | mask, apoll->poll.events); |
59b735ae | 6154 | return IO_APOLL_OK; |
d7718a9d JA |
6155 | } |
6156 | ||
76e1b642 JA |
6157 | /* |
6158 | * Returns true if we found and killed one or more poll requests | |
6159 | */ | |
c072481d PB |
6160 | static __cold bool io_poll_remove_all(struct io_ring_ctx *ctx, |
6161 | struct task_struct *tsk, bool cancel_all) | |
221c5eb2 | 6162 | { |
78076bb6 | 6163 | struct hlist_node *tmp; |
221c5eb2 | 6164 | struct io_kiocb *req; |
aa43477b PB |
6165 | bool found = false; |
6166 | int i; | |
221c5eb2 | 6167 | |
79ebeaee | 6168 | spin_lock(&ctx->completion_lock); |
78076bb6 JA |
6169 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
6170 | struct hlist_head *list; | |
6171 | ||
6172 | list = &ctx->cancel_hash[i]; | |
f3606e3a | 6173 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
42a7b4ed | 6174 | if (io_match_task_safe(req, tsk, cancel_all)) { |
aa43477b PB |
6175 | io_poll_cancel_req(req); |
6176 | found = true; | |
6177 | } | |
f3606e3a | 6178 | } |
221c5eb2 | 6179 | } |
79ebeaee | 6180 | spin_unlock(&ctx->completion_lock); |
aa43477b | 6181 | return found; |
221c5eb2 JA |
6182 | } |
6183 | ||
9ba5fac8 PB |
6184 | static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr, |
6185 | bool poll_only) | |
e07785b0 | 6186 | __must_hold(&ctx->completion_lock) |
47f46768 | 6187 | { |
78076bb6 | 6188 | struct hlist_head *list; |
47f46768 JA |
6189 | struct io_kiocb *req; |
6190 | ||
78076bb6 JA |
6191 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
6192 | hlist_for_each_entry(req, list, hash_node) { | |
b41e9852 JA |
6193 | if (sqe_addr != req->user_data) |
6194 | continue; | |
9ba5fac8 PB |
6195 | if (poll_only && req->opcode != IORING_OP_POLL_ADD) |
6196 | continue; | |
b2cb805f | 6197 | return req; |
47f46768 | 6198 | } |
b2cb805f JA |
6199 | return NULL; |
6200 | } | |
6201 | ||
aa43477b PB |
6202 | static bool io_poll_disarm(struct io_kiocb *req) |
6203 | __must_hold(&ctx->completion_lock) | |
6204 | { | |
6205 | if (!io_poll_get_ownership(req)) | |
6206 | return false; | |
6207 | io_poll_remove_entries(req); | |
6208 | hash_del(&req->hash_node); | |
6209 | return true; | |
6210 | } | |
6211 | ||
9ba5fac8 PB |
6212 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr, |
6213 | bool poll_only) | |
e07785b0 | 6214 | __must_hold(&ctx->completion_lock) |
b2cb805f | 6215 | { |
aa43477b | 6216 | struct io_kiocb *req = io_poll_find(ctx, sqe_addr, poll_only); |
b2cb805f | 6217 | |
b2cb805f JA |
6218 | if (!req) |
6219 | return -ENOENT; | |
aa43477b PB |
6220 | io_poll_cancel_req(req); |
6221 | return 0; | |
47f46768 JA |
6222 | } |
6223 | ||
9096af3e PB |
6224 | static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe, |
6225 | unsigned int flags) | |
6226 | { | |
6227 | u32 events; | |
47f46768 | 6228 | |
9096af3e PB |
6229 | events = READ_ONCE(sqe->poll32_events); |
6230 | #ifdef __BIG_ENDIAN | |
6231 | events = swahw32(events); | |
6232 | #endif | |
6233 | if (!(flags & IORING_POLL_ADD_MULTI)) | |
6234 | events |= EPOLLONESHOT; | |
6235 | return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT)); | |
47f46768 JA |
6236 | } |
6237 | ||
c5de0036 | 6238 | static int io_poll_update_prep(struct io_kiocb *req, |
3529d8c2 | 6239 | const struct io_uring_sqe *sqe) |
0969e783 | 6240 | { |
c5de0036 PB |
6241 | struct io_poll_update *upd = &req->poll_update; |
6242 | u32 flags; | |
6243 | ||
0969e783 JA |
6244 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
6245 | return -EINVAL; | |
26578cda | 6246 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
c5de0036 PB |
6247 | return -EINVAL; |
6248 | flags = READ_ONCE(sqe->len); | |
6249 | if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA | | |
6250 | IORING_POLL_ADD_MULTI)) | |
6251 | return -EINVAL; | |
6252 | /* meaningless without update */ | |
6253 | if (flags == IORING_POLL_ADD_MULTI) | |
0969e783 JA |
6254 | return -EINVAL; |
6255 | ||
c5de0036 PB |
6256 | upd->old_user_data = READ_ONCE(sqe->addr); |
6257 | upd->update_events = flags & IORING_POLL_UPDATE_EVENTS; | |
6258 | upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA; | |
221c5eb2 | 6259 | |
c5de0036 PB |
6260 | upd->new_user_data = READ_ONCE(sqe->off); |
6261 | if (!upd->update_user_data && upd->new_user_data) | |
6262 | return -EINVAL; | |
6263 | if (upd->update_events) | |
6264 | upd->events = io_poll_parse_events(sqe, flags); | |
6265 | else if (sqe->poll32_events) | |
6266 | return -EINVAL; | |
221c5eb2 | 6267 | |
221c5eb2 JA |
6268 | return 0; |
6269 | } | |
6270 | ||
3529d8c2 | 6271 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
6272 | { |
6273 | struct io_poll_iocb *poll = &req->poll; | |
c5de0036 | 6274 | u32 flags; |
221c5eb2 JA |
6275 | |
6276 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
6277 | return -EINVAL; | |
c5de0036 | 6278 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr) |
88e41cf9 JA |
6279 | return -EINVAL; |
6280 | flags = READ_ONCE(sqe->len); | |
c5de0036 | 6281 | if (flags & ~IORING_POLL_ADD_MULTI) |
221c5eb2 | 6282 | return -EINVAL; |
04c76b41 PB |
6283 | if ((flags & IORING_POLL_ADD_MULTI) && (req->flags & REQ_F_CQE_SKIP)) |
6284 | return -EINVAL; | |
221c5eb2 | 6285 | |
48dcd38d | 6286 | io_req_set_refcount(req); |
c5de0036 | 6287 | poll->events = io_poll_parse_events(sqe, flags); |
0969e783 JA |
6288 | return 0; |
6289 | } | |
6290 | ||
61e98203 | 6291 | static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags) |
0969e783 JA |
6292 | { |
6293 | struct io_poll_iocb *poll = &req->poll; | |
0969e783 | 6294 | struct io_poll_table ipt; |
aa43477b | 6295 | int ret; |
0969e783 | 6296 | |
d7718a9d | 6297 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 6298 | |
aa43477b PB |
6299 | ret = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events); |
6300 | ret = ret ?: ipt.error; | |
6301 | if (ret) | |
6302 | __io_req_complete(req, issue_flags, ret, 0); | |
6303 | return 0; | |
221c5eb2 JA |
6304 | } |
6305 | ||
c5de0036 | 6306 | static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags) |
b69de288 JA |
6307 | { |
6308 | struct io_ring_ctx *ctx = req->ctx; | |
6309 | struct io_kiocb *preq; | |
2bbb146d | 6310 | int ret2, ret = 0; |
cc8e9ba7 | 6311 | bool locked; |
b69de288 | 6312 | |
79ebeaee | 6313 | spin_lock(&ctx->completion_lock); |
9ba5fac8 | 6314 | preq = io_poll_find(ctx, req->poll_update.old_user_data, true); |
aa43477b | 6315 | if (!preq || !io_poll_disarm(preq)) { |
79ebeaee | 6316 | spin_unlock(&ctx->completion_lock); |
aa43477b | 6317 | ret = preq ? -EALREADY : -ENOENT; |
2bbb146d | 6318 | goto out; |
b69de288 | 6319 | } |
79ebeaee | 6320 | spin_unlock(&ctx->completion_lock); |
cb3b200e | 6321 | |
2bbb146d PB |
6322 | if (req->poll_update.update_events || req->poll_update.update_user_data) { |
6323 | /* only mask one event flags, keep behavior flags */ | |
6324 | if (req->poll_update.update_events) { | |
6325 | preq->poll.events &= ~0xffff; | |
6326 | preq->poll.events |= req->poll_update.events & 0xffff; | |
6327 | preq->poll.events |= IO_POLL_UNMASK; | |
cb3b200e | 6328 | } |
2bbb146d PB |
6329 | if (req->poll_update.update_user_data) |
6330 | preq->user_data = req->poll_update.new_user_data; | |
b69de288 | 6331 | |
2bbb146d PB |
6332 | ret2 = io_poll_add(preq, issue_flags); |
6333 | /* successfully updated, don't complete poll request */ | |
6334 | if (!ret2) | |
6335 | goto out; | |
b69de288 | 6336 | } |
6224590d | 6337 | |
2bbb146d | 6338 | req_set_fail(preq); |
cc8e9ba7 PB |
6339 | preq->result = -ECANCELED; |
6340 | locked = !(issue_flags & IO_URING_F_UNLOCKED); | |
6341 | io_req_task_complete(preq, &locked); | |
2bbb146d PB |
6342 | out: |
6343 | if (ret < 0) | |
6224590d | 6344 | req_set_fail(req); |
2bbb146d | 6345 | /* complete update request, we're done with it */ |
cc8e9ba7 | 6346 | __io_req_complete(req, issue_flags, ret, 0); |
b69de288 | 6347 | return 0; |
89850fce JA |
6348 | } |
6349 | ||
5262f567 JA |
6350 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
6351 | { | |
ad8a48ac JA |
6352 | struct io_timeout_data *data = container_of(timer, |
6353 | struct io_timeout_data, timer); | |
6354 | struct io_kiocb *req = data->req; | |
6355 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
6356 | unsigned long flags; |
6357 | ||
89850fce | 6358 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
a71976f3 | 6359 | list_del_init(&req->timeout.list); |
01cec8c1 PB |
6360 | atomic_set(&req->ctx->cq_timeouts, |
6361 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
89850fce | 6362 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); |
01cec8c1 | 6363 | |
a90c8bf6 PB |
6364 | if (!(data->flags & IORING_TIMEOUT_ETIME_SUCCESS)) |
6365 | req_set_fail(req); | |
6366 | ||
6367 | req->result = -ETIME; | |
6368 | req->io_task_work.func = io_req_task_complete; | |
4813c377 | 6369 | io_req_task_work_add(req, false); |
5262f567 JA |
6370 | return HRTIMER_NORESTART; |
6371 | } | |
6372 | ||
fbd15848 PB |
6373 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
6374 | __u64 user_data) | |
89850fce | 6375 | __must_hold(&ctx->timeout_lock) |
f254ac04 | 6376 | { |
fbd15848 | 6377 | struct io_timeout_data *io; |
47f46768 | 6378 | struct io_kiocb *req; |
fd9c7bc5 | 6379 | bool found = false; |
f254ac04 | 6380 | |
135fcde8 | 6381 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
fd9c7bc5 PB |
6382 | found = user_data == req->user_data; |
6383 | if (found) | |
47f46768 | 6384 | break; |
47f46768 | 6385 | } |
fd9c7bc5 PB |
6386 | if (!found) |
6387 | return ERR_PTR(-ENOENT); | |
fbd15848 PB |
6388 | |
6389 | io = req->async_data; | |
fd9c7bc5 | 6390 | if (hrtimer_try_to_cancel(&io->timer) == -1) |
fbd15848 | 6391 | return ERR_PTR(-EALREADY); |
a71976f3 | 6392 | list_del_init(&req->timeout.list); |
fbd15848 PB |
6393 | return req; |
6394 | } | |
47f46768 | 6395 | |
fbd15848 | 6396 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
ec3c3d0f | 6397 | __must_hold(&ctx->completion_lock) |
89850fce | 6398 | __must_hold(&ctx->timeout_lock) |
fbd15848 PB |
6399 | { |
6400 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); | |
6401 | ||
6402 | if (IS_ERR(req)) | |
6403 | return PTR_ERR(req); | |
f254ac04 | 6404 | |
93d2bcd2 | 6405 | req_set_fail(req); |
913a571a | 6406 | io_fill_cqe_req(req, -ECANCELED, 0); |
91c2f697 | 6407 | io_put_req_deferred(req); |
f254ac04 JA |
6408 | return 0; |
6409 | } | |
6410 | ||
50c1df2b JA |
6411 | static clockid_t io_timeout_get_clock(struct io_timeout_data *data) |
6412 | { | |
6413 | switch (data->flags & IORING_TIMEOUT_CLOCK_MASK) { | |
6414 | case IORING_TIMEOUT_BOOTTIME: | |
6415 | return CLOCK_BOOTTIME; | |
6416 | case IORING_TIMEOUT_REALTIME: | |
6417 | return CLOCK_REALTIME; | |
6418 | default: | |
6419 | /* can't happen, vetted at prep time */ | |
6420 | WARN_ON_ONCE(1); | |
6421 | fallthrough; | |
6422 | case 0: | |
6423 | return CLOCK_MONOTONIC; | |
6424 | } | |
6425 | } | |
6426 | ||
f1042b6c PB |
6427 | static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
6428 | struct timespec64 *ts, enum hrtimer_mode mode) | |
6429 | __must_hold(&ctx->timeout_lock) | |
6430 | { | |
6431 | struct io_timeout_data *io; | |
6432 | struct io_kiocb *req; | |
6433 | bool found = false; | |
6434 | ||
6435 | list_for_each_entry(req, &ctx->ltimeout_list, timeout.list) { | |
6436 | found = user_data == req->user_data; | |
6437 | if (found) | |
6438 | break; | |
6439 | } | |
6440 | if (!found) | |
6441 | return -ENOENT; | |
6442 | ||
6443 | io = req->async_data; | |
6444 | if (hrtimer_try_to_cancel(&io->timer) == -1) | |
6445 | return -EALREADY; | |
6446 | hrtimer_init(&io->timer, io_timeout_get_clock(io), mode); | |
6447 | io->timer.function = io_link_timeout_fn; | |
6448 | hrtimer_start(&io->timer, timespec64_to_ktime(*ts), mode); | |
6449 | return 0; | |
6450 | } | |
6451 | ||
9c8e11b3 PB |
6452 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
6453 | struct timespec64 *ts, enum hrtimer_mode mode) | |
89850fce | 6454 | __must_hold(&ctx->timeout_lock) |
47f46768 | 6455 | { |
9c8e11b3 PB |
6456 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
6457 | struct io_timeout_data *data; | |
47f46768 | 6458 | |
9c8e11b3 PB |
6459 | if (IS_ERR(req)) |
6460 | return PTR_ERR(req); | |
47f46768 | 6461 | |
9c8e11b3 PB |
6462 | req->timeout.off = 0; /* noseq */ |
6463 | data = req->async_data; | |
6464 | list_add_tail(&req->timeout.list, &ctx->timeout_list); | |
50c1df2b | 6465 | hrtimer_init(&data->timer, io_timeout_get_clock(data), mode); |
9c8e11b3 PB |
6466 | data->timer.function = io_timeout_fn; |
6467 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); | |
6468 | return 0; | |
47f46768 JA |
6469 | } |
6470 | ||
3529d8c2 JA |
6471 | static int io_timeout_remove_prep(struct io_kiocb *req, |
6472 | const struct io_uring_sqe *sqe) | |
b29472ee | 6473 | { |
9c8e11b3 PB |
6474 | struct io_timeout_rem *tr = &req->timeout_rem; |
6475 | ||
b29472ee JA |
6476 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
6477 | return -EINVAL; | |
61710e43 DA |
6478 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6479 | return -EINVAL; | |
26578cda | 6480 | if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in) |
b29472ee JA |
6481 | return -EINVAL; |
6482 | ||
f1042b6c | 6483 | tr->ltimeout = false; |
9c8e11b3 PB |
6484 | tr->addr = READ_ONCE(sqe->addr); |
6485 | tr->flags = READ_ONCE(sqe->timeout_flags); | |
f1042b6c PB |
6486 | if (tr->flags & IORING_TIMEOUT_UPDATE_MASK) { |
6487 | if (hweight32(tr->flags & IORING_TIMEOUT_CLOCK_MASK) > 1) | |
6488 | return -EINVAL; | |
6489 | if (tr->flags & IORING_LINK_TIMEOUT_UPDATE) | |
6490 | tr->ltimeout = true; | |
6491 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE_MASK|IORING_TIMEOUT_ABS)) | |
9c8e11b3 PB |
6492 | return -EINVAL; |
6493 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) | |
6494 | return -EFAULT; | |
2087009c YB |
6495 | if (tr->ts.tv_sec < 0 || tr->ts.tv_nsec < 0) |
6496 | return -EINVAL; | |
9c8e11b3 PB |
6497 | } else if (tr->flags) { |
6498 | /* timeout removal doesn't support flags */ | |
b29472ee | 6499 | return -EINVAL; |
9c8e11b3 | 6500 | } |
b29472ee | 6501 | |
b29472ee JA |
6502 | return 0; |
6503 | } | |
6504 | ||
8662daec PB |
6505 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
6506 | { | |
6507 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS | |
6508 | : HRTIMER_MODE_REL; | |
6509 | } | |
6510 | ||
11365043 JA |
6511 | /* |
6512 | * Remove or update an existing timeout command | |
6513 | */ | |
61e98203 | 6514 | static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags) |
11365043 | 6515 | { |
9c8e11b3 | 6516 | struct io_timeout_rem *tr = &req->timeout_rem; |
11365043 | 6517 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 | 6518 | int ret; |
11365043 | 6519 | |
ec3c3d0f PB |
6520 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) { |
6521 | spin_lock(&ctx->completion_lock); | |
6522 | spin_lock_irq(&ctx->timeout_lock); | |
9c8e11b3 | 6523 | ret = io_timeout_cancel(ctx, tr->addr); |
ec3c3d0f PB |
6524 | spin_unlock_irq(&ctx->timeout_lock); |
6525 | spin_unlock(&ctx->completion_lock); | |
6526 | } else { | |
f1042b6c PB |
6527 | enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags); |
6528 | ||
ec3c3d0f | 6529 | spin_lock_irq(&ctx->timeout_lock); |
f1042b6c PB |
6530 | if (tr->ltimeout) |
6531 | ret = io_linked_timeout_update(ctx, tr->addr, &tr->ts, mode); | |
6532 | else | |
6533 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode); | |
ec3c3d0f PB |
6534 | spin_unlock_irq(&ctx->timeout_lock); |
6535 | } | |
11365043 | 6536 | |
4e88d6e7 | 6537 | if (ret < 0) |
93d2bcd2 | 6538 | req_set_fail(req); |
505657bc | 6539 | io_req_complete_post(req, ret, 0); |
11365043 | 6540 | return 0; |
5262f567 JA |
6541 | } |
6542 | ||
3529d8c2 | 6543 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 6544 | bool is_timeout_link) |
5262f567 | 6545 | { |
ad8a48ac | 6546 | struct io_timeout_data *data; |
a41525ab | 6547 | unsigned flags; |
56080b02 | 6548 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 6549 | |
ad8a48ac | 6550 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 6551 | return -EINVAL; |
26578cda PB |
6552 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1 || |
6553 | sqe->splice_fd_in) | |
a41525ab | 6554 | return -EINVAL; |
56080b02 | 6555 | if (off && is_timeout_link) |
2d28390a | 6556 | return -EINVAL; |
a41525ab | 6557 | flags = READ_ONCE(sqe->timeout_flags); |
6224590d PB |
6558 | if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK | |
6559 | IORING_TIMEOUT_ETIME_SUCCESS)) | |
50c1df2b JA |
6560 | return -EINVAL; |
6561 | /* more than one clock specified is invalid, obviously */ | |
6562 | if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1) | |
5262f567 | 6563 | return -EINVAL; |
bdf20073 | 6564 | |
ef9dd637 | 6565 | INIT_LIST_HEAD(&req->timeout.list); |
bfe68a22 | 6566 | req->timeout.off = off; |
f18ee4cf PB |
6567 | if (unlikely(off && !req->ctx->off_timeout_used)) |
6568 | req->ctx->off_timeout_used = true; | |
26a61679 | 6569 | |
d6a644a7 PB |
6570 | if (WARN_ON_ONCE(req_has_async_data(req))) |
6571 | return -EFAULT; | |
6572 | if (io_alloc_async_data(req)) | |
26a61679 JA |
6573 | return -ENOMEM; |
6574 | ||
e8c2bc1f | 6575 | data = req->async_data; |
ad8a48ac | 6576 | data->req = req; |
50c1df2b | 6577 | data->flags = flags; |
ad8a48ac JA |
6578 | |
6579 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
6580 | return -EFAULT; |
6581 | ||
f6223ff7 YB |
6582 | if (data->ts.tv_sec < 0 || data->ts.tv_nsec < 0) |
6583 | return -EINVAL; | |
6584 | ||
8662daec | 6585 | data->mode = io_translate_timeout_mode(flags); |
50c1df2b | 6586 | hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode); |
b97e736a PB |
6587 | |
6588 | if (is_timeout_link) { | |
6589 | struct io_submit_link *link = &req->ctx->submit_state.link; | |
6590 | ||
6591 | if (!link->head) | |
6592 | return -EINVAL; | |
6593 | if (link->last->opcode == IORING_OP_LINK_TIMEOUT) | |
6594 | return -EINVAL; | |
4d13d1a4 PB |
6595 | req->timeout.head = link->last; |
6596 | link->last->flags |= REQ_F_ARM_LTIMEOUT; | |
b97e736a | 6597 | } |
ad8a48ac JA |
6598 | return 0; |
6599 | } | |
6600 | ||
61e98203 | 6601 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
ad8a48ac | 6602 | { |
ad8a48ac | 6603 | struct io_ring_ctx *ctx = req->ctx; |
e8c2bc1f | 6604 | struct io_timeout_data *data = req->async_data; |
ad8a48ac | 6605 | struct list_head *entry; |
bfe68a22 | 6606 | u32 tail, off = req->timeout.off; |
ad8a48ac | 6607 | |
89850fce | 6608 | spin_lock_irq(&ctx->timeout_lock); |
93bd25bb | 6609 | |
5262f567 JA |
6610 | /* |
6611 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
6612 | * timeout event to be satisfied. If it isn't set, then this is |
6613 | * a pure timeout request, sequence isn't used. | |
5262f567 | 6614 | */ |
8eb7e2d0 | 6615 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
6616 | entry = ctx->timeout_list.prev; |
6617 | goto add; | |
6618 | } | |
5262f567 | 6619 | |
bfe68a22 PB |
6620 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
6621 | req->timeout.target_seq = tail + off; | |
5262f567 | 6622 | |
f010505b MDG |
6623 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
6624 | * This is safe because ->completion_lock is held, and submissions | |
6625 | * and completions are never mixed in the same ->completion_lock section. | |
6626 | */ | |
6627 | ctx->cq_last_tm_flush = tail; | |
6628 | ||
5262f567 JA |
6629 | /* |
6630 | * Insertion sort, ensuring the first entry in the list is always | |
6631 | * the one we need first. | |
6632 | */ | |
5262f567 | 6633 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
6634 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
6635 | timeout.list); | |
5262f567 | 6636 | |
8eb7e2d0 | 6637 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 6638 | continue; |
bfe68a22 PB |
6639 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
6640 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
6641 | break; |
6642 | } | |
93bd25bb | 6643 | add: |
135fcde8 | 6644 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
6645 | data->timer.function = io_timeout_fn; |
6646 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
89850fce | 6647 | spin_unlock_irq(&ctx->timeout_lock); |
5262f567 JA |
6648 | return 0; |
6649 | } | |
5262f567 | 6650 | |
f458dd84 PB |
6651 | struct io_cancel_data { |
6652 | struct io_ring_ctx *ctx; | |
6653 | u64 user_data; | |
6654 | }; | |
6655 | ||
62755e35 JA |
6656 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
6657 | { | |
6658 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
f458dd84 | 6659 | struct io_cancel_data *cd = data; |
62755e35 | 6660 | |
f458dd84 | 6661 | return req->ctx == cd->ctx && req->user_data == cd->user_data; |
62755e35 JA |
6662 | } |
6663 | ||
f458dd84 PB |
6664 | static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data, |
6665 | struct io_ring_ctx *ctx) | |
62755e35 | 6666 | { |
f458dd84 | 6667 | struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, }; |
62755e35 | 6668 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
6669 | int ret = 0; |
6670 | ||
f458dd84 | 6671 | if (!tctx || !tctx->io_wq) |
5aa75ed5 JA |
6672 | return -ENOENT; |
6673 | ||
f458dd84 | 6674 | cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false); |
62755e35 JA |
6675 | switch (cancel_ret) { |
6676 | case IO_WQ_CANCEL_OK: | |
6677 | ret = 0; | |
6678 | break; | |
6679 | case IO_WQ_CANCEL_RUNNING: | |
6680 | ret = -EALREADY; | |
6681 | break; | |
6682 | case IO_WQ_CANCEL_NOTFOUND: | |
6683 | ret = -ENOENT; | |
6684 | break; | |
6685 | } | |
6686 | ||
e977d6d3 JA |
6687 | return ret; |
6688 | } | |
6689 | ||
8cb01fac | 6690 | static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr) |
47f46768 | 6691 | { |
8cb01fac | 6692 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 JA |
6693 | int ret; |
6694 | ||
dadebc35 | 6695 | WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current); |
8cb01fac | 6696 | |
f458dd84 | 6697 | ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx); |
ccbf7261 JA |
6698 | /* |
6699 | * Fall-through even for -EALREADY, as we may have poll armed | |
6700 | * that need unarming. | |
6701 | */ | |
6702 | if (!ret) | |
6703 | return 0; | |
505657bc PB |
6704 | |
6705 | spin_lock(&ctx->completion_lock); | |
ccbf7261 JA |
6706 | ret = io_poll_cancel(ctx, sqe_addr, false); |
6707 | if (ret != -ENOENT) | |
6708 | goto out; | |
6709 | ||
79ebeaee | 6710 | spin_lock_irq(&ctx->timeout_lock); |
47f46768 | 6711 | ret = io_timeout_cancel(ctx, sqe_addr); |
79ebeaee | 6712 | spin_unlock_irq(&ctx->timeout_lock); |
505657bc PB |
6713 | out: |
6714 | spin_unlock(&ctx->completion_lock); | |
6715 | return ret; | |
47f46768 JA |
6716 | } |
6717 | ||
3529d8c2 JA |
6718 | static int io_async_cancel_prep(struct io_kiocb *req, |
6719 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 6720 | { |
fbf23849 | 6721 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 6722 | return -EINVAL; |
61710e43 DA |
6723 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6724 | return -EINVAL; | |
26578cda PB |
6725 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags || |
6726 | sqe->splice_fd_in) | |
e977d6d3 JA |
6727 | return -EINVAL; |
6728 | ||
fbf23849 JA |
6729 | req->cancel.addr = READ_ONCE(sqe->addr); |
6730 | return 0; | |
6731 | } | |
6732 | ||
61e98203 | 6733 | static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6734 | { |
6735 | struct io_ring_ctx *ctx = req->ctx; | |
58f99373 | 6736 | u64 sqe_addr = req->cancel.addr; |
3b44b371 | 6737 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
58f99373 PB |
6738 | struct io_tctx_node *node; |
6739 | int ret; | |
6740 | ||
8cb01fac | 6741 | ret = io_try_cancel_userdata(req, sqe_addr); |
58f99373 PB |
6742 | if (ret != -ENOENT) |
6743 | goto done; | |
58f99373 PB |
6744 | |
6745 | /* slow path, try all io-wq's */ | |
3b44b371 | 6746 | io_ring_submit_lock(ctx, needs_lock); |
58f99373 PB |
6747 | ret = -ENOENT; |
6748 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
6749 | struct io_uring_task *tctx = node->task->io_uring; | |
fbf23849 | 6750 | |
58f99373 PB |
6751 | ret = io_async_cancel_one(tctx, req->cancel.addr, ctx); |
6752 | if (ret != -ENOENT) | |
6753 | break; | |
6754 | } | |
3b44b371 | 6755 | io_ring_submit_unlock(ctx, needs_lock); |
58f99373 | 6756 | done: |
58f99373 | 6757 | if (ret < 0) |
93d2bcd2 | 6758 | req_set_fail(req); |
505657bc | 6759 | io_req_complete_post(req, ret, 0); |
5262f567 JA |
6760 | return 0; |
6761 | } | |
6762 | ||
269bbe5f | 6763 | static int io_rsrc_update_prep(struct io_kiocb *req, |
05f3fb3c JA |
6764 | const struct io_uring_sqe *sqe) |
6765 | { | |
61710e43 DA |
6766 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6767 | return -EINVAL; | |
26578cda | 6768 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
05f3fb3c JA |
6769 | return -EINVAL; |
6770 | ||
269bbe5f BM |
6771 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
6772 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); | |
6773 | if (!req->rsrc_update.nr_args) | |
05f3fb3c | 6774 | return -EINVAL; |
269bbe5f | 6775 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
05f3fb3c JA |
6776 | return 0; |
6777 | } | |
6778 | ||
889fca73 | 6779 | static int io_files_update(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6780 | { |
6781 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 6782 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
c3bdad02 | 6783 | struct io_uring_rsrc_update2 up; |
05f3fb3c | 6784 | int ret; |
fbf23849 | 6785 | |
269bbe5f BM |
6786 | up.offset = req->rsrc_update.offset; |
6787 | up.data = req->rsrc_update.arg; | |
c3bdad02 PB |
6788 | up.nr = 0; |
6789 | up.tags = 0; | |
615cee49 | 6790 | up.resv = 0; |
05f3fb3c | 6791 | |
3b44b371 | 6792 | io_ring_submit_lock(ctx, needs_lock); |
fdecb662 | 6793 | ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE, |
98f0b3b4 | 6794 | &up, req->rsrc_update.nr_args); |
3b44b371 | 6795 | io_ring_submit_unlock(ctx, needs_lock); |
05f3fb3c JA |
6796 | |
6797 | if (ret < 0) | |
93d2bcd2 | 6798 | req_set_fail(req); |
889fca73 | 6799 | __io_req_complete(req, issue_flags, ret, 0); |
5262f567 JA |
6800 | return 0; |
6801 | } | |
6802 | ||
bfe76559 | 6803 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 6804 | { |
d625c6ee | 6805 | switch (req->opcode) { |
e781573e | 6806 | case IORING_OP_NOP: |
bfe76559 | 6807 | return 0; |
f67676d1 JA |
6808 | case IORING_OP_READV: |
6809 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 6810 | case IORING_OP_READ: |
bfe76559 | 6811 | return io_read_prep(req, sqe); |
f67676d1 JA |
6812 | case IORING_OP_WRITEV: |
6813 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 6814 | case IORING_OP_WRITE: |
bfe76559 | 6815 | return io_write_prep(req, sqe); |
0969e783 | 6816 | case IORING_OP_POLL_ADD: |
bfe76559 | 6817 | return io_poll_add_prep(req, sqe); |
0969e783 | 6818 | case IORING_OP_POLL_REMOVE: |
c5de0036 | 6819 | return io_poll_update_prep(req, sqe); |
8ed8d3c3 | 6820 | case IORING_OP_FSYNC: |
1155c76a | 6821 | return io_fsync_prep(req, sqe); |
8ed8d3c3 | 6822 | case IORING_OP_SYNC_FILE_RANGE: |
1155c76a | 6823 | return io_sfr_prep(req, sqe); |
03b1230c | 6824 | case IORING_OP_SENDMSG: |
fddaface | 6825 | case IORING_OP_SEND: |
bfe76559 | 6826 | return io_sendmsg_prep(req, sqe); |
03b1230c | 6827 | case IORING_OP_RECVMSG: |
fddaface | 6828 | case IORING_OP_RECV: |
bfe76559 | 6829 | return io_recvmsg_prep(req, sqe); |
f499a021 | 6830 | case IORING_OP_CONNECT: |
bfe76559 | 6831 | return io_connect_prep(req, sqe); |
2d28390a | 6832 | case IORING_OP_TIMEOUT: |
bfe76559 | 6833 | return io_timeout_prep(req, sqe, false); |
b29472ee | 6834 | case IORING_OP_TIMEOUT_REMOVE: |
bfe76559 | 6835 | return io_timeout_remove_prep(req, sqe); |
fbf23849 | 6836 | case IORING_OP_ASYNC_CANCEL: |
bfe76559 | 6837 | return io_async_cancel_prep(req, sqe); |
2d28390a | 6838 | case IORING_OP_LINK_TIMEOUT: |
bfe76559 | 6839 | return io_timeout_prep(req, sqe, true); |
8ed8d3c3 | 6840 | case IORING_OP_ACCEPT: |
bfe76559 | 6841 | return io_accept_prep(req, sqe); |
d63d1b5e | 6842 | case IORING_OP_FALLOCATE: |
bfe76559 | 6843 | return io_fallocate_prep(req, sqe); |
15b71abe | 6844 | case IORING_OP_OPENAT: |
bfe76559 | 6845 | return io_openat_prep(req, sqe); |
b5dba59e | 6846 | case IORING_OP_CLOSE: |
bfe76559 | 6847 | return io_close_prep(req, sqe); |
05f3fb3c | 6848 | case IORING_OP_FILES_UPDATE: |
269bbe5f | 6849 | return io_rsrc_update_prep(req, sqe); |
eddc7ef5 | 6850 | case IORING_OP_STATX: |
bfe76559 | 6851 | return io_statx_prep(req, sqe); |
4840e418 | 6852 | case IORING_OP_FADVISE: |
bfe76559 | 6853 | return io_fadvise_prep(req, sqe); |
c1ca757b | 6854 | case IORING_OP_MADVISE: |
bfe76559 | 6855 | return io_madvise_prep(req, sqe); |
cebdb986 | 6856 | case IORING_OP_OPENAT2: |
bfe76559 | 6857 | return io_openat2_prep(req, sqe); |
3e4827b0 | 6858 | case IORING_OP_EPOLL_CTL: |
bfe76559 | 6859 | return io_epoll_ctl_prep(req, sqe); |
7d67af2c | 6860 | case IORING_OP_SPLICE: |
bfe76559 | 6861 | return io_splice_prep(req, sqe); |
ddf0322d | 6862 | case IORING_OP_PROVIDE_BUFFERS: |
bfe76559 | 6863 | return io_provide_buffers_prep(req, sqe); |
067524e9 | 6864 | case IORING_OP_REMOVE_BUFFERS: |
bfe76559 | 6865 | return io_remove_buffers_prep(req, sqe); |
f2a8d5c7 | 6866 | case IORING_OP_TEE: |
bfe76559 | 6867 | return io_tee_prep(req, sqe); |
36f4fa68 JA |
6868 | case IORING_OP_SHUTDOWN: |
6869 | return io_shutdown_prep(req, sqe); | |
80a261fd JA |
6870 | case IORING_OP_RENAMEAT: |
6871 | return io_renameat_prep(req, sqe); | |
14a1143b JA |
6872 | case IORING_OP_UNLINKAT: |
6873 | return io_unlinkat_prep(req, sqe); | |
e34a02dc DK |
6874 | case IORING_OP_MKDIRAT: |
6875 | return io_mkdirat_prep(req, sqe); | |
7a8721f8 DK |
6876 | case IORING_OP_SYMLINKAT: |
6877 | return io_symlinkat_prep(req, sqe); | |
cf30da90 DK |
6878 | case IORING_OP_LINKAT: |
6879 | return io_linkat_prep(req, sqe); | |
4f57f06c JA |
6880 | case IORING_OP_MSG_RING: |
6881 | return io_msg_ring_prep(req, sqe); | |
f67676d1 JA |
6882 | } |
6883 | ||
bfe76559 PB |
6884 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
6885 | req->opcode); | |
bd54b6fe | 6886 | return -EINVAL; |
bfe76559 PB |
6887 | } |
6888 | ||
93642ef8 | 6889 | static int io_req_prep_async(struct io_kiocb *req) |
bfe76559 | 6890 | { |
b7e298d2 PB |
6891 | if (!io_op_defs[req->opcode].needs_async_setup) |
6892 | return 0; | |
d886e185 | 6893 | if (WARN_ON_ONCE(req_has_async_data(req))) |
b7e298d2 PB |
6894 | return -EFAULT; |
6895 | if (io_alloc_async_data(req)) | |
6896 | return -EAGAIN; | |
6897 | ||
93642ef8 PB |
6898 | switch (req->opcode) { |
6899 | case IORING_OP_READV: | |
93642ef8 PB |
6900 | return io_rw_prep_async(req, READ); |
6901 | case IORING_OP_WRITEV: | |
93642ef8 PB |
6902 | return io_rw_prep_async(req, WRITE); |
6903 | case IORING_OP_SENDMSG: | |
93642ef8 PB |
6904 | return io_sendmsg_prep_async(req); |
6905 | case IORING_OP_RECVMSG: | |
93642ef8 PB |
6906 | return io_recvmsg_prep_async(req); |
6907 | case IORING_OP_CONNECT: | |
6908 | return io_connect_prep_async(req); | |
6909 | } | |
b7e298d2 PB |
6910 | printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n", |
6911 | req->opcode); | |
6912 | return -EFAULT; | |
f67676d1 JA |
6913 | } |
6914 | ||
9cf7c104 PB |
6915 | static u32 io_get_sequence(struct io_kiocb *req) |
6916 | { | |
a3dbdf54 | 6917 | u32 seq = req->ctx->cached_sq_head; |
9cf7c104 | 6918 | |
a3dbdf54 PB |
6919 | /* need original cached_sq_head, but it was increased for each req */ |
6920 | io_for_each_link(req, req) | |
6921 | seq--; | |
6922 | return seq; | |
9cf7c104 PB |
6923 | } |
6924 | ||
c072481d | 6925 | static __cold void io_drain_req(struct io_kiocb *req) |
de0617e4 | 6926 | { |
a197f664 | 6927 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 6928 | struct io_defer_entry *de; |
f67676d1 | 6929 | int ret; |
e0eb71dc | 6930 | u32 seq = io_get_sequence(req); |
3c19966d | 6931 | |
9d858b21 | 6932 | /* Still need defer if there is pending req in defer list. */ |
e302f104 | 6933 | spin_lock(&ctx->completion_lock); |
5e371265 | 6934 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) { |
e302f104 | 6935 | spin_unlock(&ctx->completion_lock); |
e0eb71dc | 6936 | queue: |
10c66904 | 6937 | ctx->drain_active = false; |
e0eb71dc PB |
6938 | io_req_task_queue(req); |
6939 | return; | |
10c66904 | 6940 | } |
e302f104 | 6941 | spin_unlock(&ctx->completion_lock); |
9cf7c104 | 6942 | |
b7e298d2 | 6943 | ret = io_req_prep_async(req); |
e0eb71dc PB |
6944 | if (ret) { |
6945 | fail: | |
6946 | io_req_complete_failed(req, ret); | |
6947 | return; | |
6948 | } | |
cbdcb435 | 6949 | io_prep_async_link(req); |
27dc8338 | 6950 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
76cc33d7 | 6951 | if (!de) { |
1b48773f | 6952 | ret = -ENOMEM; |
e0eb71dc | 6953 | goto fail; |
76cc33d7 | 6954 | } |
2d28390a | 6955 | |
79ebeaee | 6956 | spin_lock(&ctx->completion_lock); |
9cf7c104 | 6957 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
79ebeaee | 6958 | spin_unlock(&ctx->completion_lock); |
27dc8338 | 6959 | kfree(de); |
e0eb71dc | 6960 | goto queue; |
de0617e4 JA |
6961 | } |
6962 | ||
502c87d6 | 6963 | trace_io_uring_defer(ctx, req, req->user_data, req->opcode); |
27dc8338 | 6964 | de->req = req; |
9cf7c104 | 6965 | de->seq = seq; |
27dc8338 | 6966 | list_add_tail(&de->list, &ctx->defer_list); |
79ebeaee | 6967 | spin_unlock(&ctx->completion_lock); |
de0617e4 JA |
6968 | } |
6969 | ||
68fb8979 | 6970 | static void io_clean_op(struct io_kiocb *req) |
99bc4c38 | 6971 | { |
d1fd1c20 | 6972 | if (req->flags & REQ_F_BUFFER_SELECTED) |
cc3cec83 | 6973 | io_put_kbuf_comp(req); |
99bc4c38 | 6974 | |
0e1b6fe3 PB |
6975 | if (req->flags & REQ_F_NEED_CLEANUP) { |
6976 | switch (req->opcode) { | |
6977 | case IORING_OP_READV: | |
6978 | case IORING_OP_READ_FIXED: | |
6979 | case IORING_OP_READ: | |
6980 | case IORING_OP_WRITEV: | |
6981 | case IORING_OP_WRITE_FIXED: | |
e8c2bc1f JA |
6982 | case IORING_OP_WRITE: { |
6983 | struct io_async_rw *io = req->async_data; | |
1dacb4df PB |
6984 | |
6985 | kfree(io->free_iovec); | |
0e1b6fe3 | 6986 | break; |
e8c2bc1f | 6987 | } |
0e1b6fe3 | 6988 | case IORING_OP_RECVMSG: |
e8c2bc1f JA |
6989 | case IORING_OP_SENDMSG: { |
6990 | struct io_async_msghdr *io = req->async_data; | |
257e84a5 PB |
6991 | |
6992 | kfree(io->free_iov); | |
0e1b6fe3 | 6993 | break; |
e8c2bc1f | 6994 | } |
0e1b6fe3 PB |
6995 | case IORING_OP_SPLICE: |
6996 | case IORING_OP_TEE: | |
e1d767f0 PB |
6997 | if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED)) |
6998 | io_put_file(req->splice.file_in); | |
0e1b6fe3 | 6999 | break; |
f3cd4850 JA |
7000 | case IORING_OP_OPENAT: |
7001 | case IORING_OP_OPENAT2: | |
7002 | if (req->open.filename) | |
7003 | putname(req->open.filename); | |
7004 | break; | |
80a261fd JA |
7005 | case IORING_OP_RENAMEAT: |
7006 | putname(req->rename.oldpath); | |
7007 | putname(req->rename.newpath); | |
7008 | break; | |
14a1143b JA |
7009 | case IORING_OP_UNLINKAT: |
7010 | putname(req->unlink.filename); | |
7011 | break; | |
e34a02dc DK |
7012 | case IORING_OP_MKDIRAT: |
7013 | putname(req->mkdir.filename); | |
7014 | break; | |
7a8721f8 DK |
7015 | case IORING_OP_SYMLINKAT: |
7016 | putname(req->symlink.oldpath); | |
7017 | putname(req->symlink.newpath); | |
7018 | break; | |
cf30da90 DK |
7019 | case IORING_OP_LINKAT: |
7020 | putname(req->hardlink.oldpath); | |
7021 | putname(req->hardlink.newpath); | |
7022 | break; | |
0e1b6fe3 | 7023 | } |
99bc4c38 | 7024 | } |
75652a30 JA |
7025 | if ((req->flags & REQ_F_POLLED) && req->apoll) { |
7026 | kfree(req->apoll->double_poll); | |
7027 | kfree(req->apoll); | |
7028 | req->apoll = NULL; | |
7029 | } | |
3a0a6902 PB |
7030 | if (req->flags & REQ_F_INFLIGHT) { |
7031 | struct io_uring_task *tctx = req->task->io_uring; | |
7032 | ||
7033 | atomic_dec(&tctx->inflight_tracked); | |
3a0a6902 | 7034 | } |
c854357b | 7035 | if (req->flags & REQ_F_CREDS) |
b8e64b53 | 7036 | put_cred(req->creds); |
d886e185 PB |
7037 | if (req->flags & REQ_F_ASYNC_DATA) { |
7038 | kfree(req->async_data); | |
7039 | req->async_data = NULL; | |
7040 | } | |
c854357b | 7041 | req->flags &= ~IO_REQ_CLEAN_FLAGS; |
99bc4c38 PB |
7042 | } |
7043 | ||
889fca73 | 7044 | static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 7045 | { |
5730b27e | 7046 | const struct cred *creds = NULL; |
d625c6ee | 7047 | int ret; |
2b188cc1 | 7048 | |
6878b40e | 7049 | if (unlikely((req->flags & REQ_F_CREDS) && req->creds != current_cred())) |
c10d1f98 | 7050 | creds = override_creds(req->creds); |
5730b27e | 7051 | |
5bd2182d PM |
7052 | if (!io_op_defs[req->opcode].audit_skip) |
7053 | audit_uring_entry(req->opcode); | |
7054 | ||
d625c6ee | 7055 | switch (req->opcode) { |
2b188cc1 | 7056 | case IORING_OP_NOP: |
889fca73 | 7057 | ret = io_nop(req, issue_flags); |
2b188cc1 JA |
7058 | break; |
7059 | case IORING_OP_READV: | |
edafccee | 7060 | case IORING_OP_READ_FIXED: |
3a6820f2 | 7061 | case IORING_OP_READ: |
889fca73 | 7062 | ret = io_read(req, issue_flags); |
edafccee | 7063 | break; |
3529d8c2 | 7064 | case IORING_OP_WRITEV: |
edafccee | 7065 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 7066 | case IORING_OP_WRITE: |
889fca73 | 7067 | ret = io_write(req, issue_flags); |
2b188cc1 | 7068 | break; |
c992fe29 | 7069 | case IORING_OP_FSYNC: |
45d189c6 | 7070 | ret = io_fsync(req, issue_flags); |
c992fe29 | 7071 | break; |
221c5eb2 | 7072 | case IORING_OP_POLL_ADD: |
61e98203 | 7073 | ret = io_poll_add(req, issue_flags); |
221c5eb2 JA |
7074 | break; |
7075 | case IORING_OP_POLL_REMOVE: | |
c5de0036 | 7076 | ret = io_poll_update(req, issue_flags); |
221c5eb2 | 7077 | break; |
5d17b4a4 | 7078 | case IORING_OP_SYNC_FILE_RANGE: |
45d189c6 | 7079 | ret = io_sync_file_range(req, issue_flags); |
5d17b4a4 | 7080 | break; |
0fa03c62 | 7081 | case IORING_OP_SENDMSG: |
889fca73 | 7082 | ret = io_sendmsg(req, issue_flags); |
062d04d7 | 7083 | break; |
fddaface | 7084 | case IORING_OP_SEND: |
889fca73 | 7085 | ret = io_send(req, issue_flags); |
0fa03c62 | 7086 | break; |
aa1fa28f | 7087 | case IORING_OP_RECVMSG: |
889fca73 | 7088 | ret = io_recvmsg(req, issue_flags); |
062d04d7 | 7089 | break; |
fddaface | 7090 | case IORING_OP_RECV: |
889fca73 | 7091 | ret = io_recv(req, issue_flags); |
aa1fa28f | 7092 | break; |
5262f567 | 7093 | case IORING_OP_TIMEOUT: |
61e98203 | 7094 | ret = io_timeout(req, issue_flags); |
5262f567 | 7095 | break; |
11365043 | 7096 | case IORING_OP_TIMEOUT_REMOVE: |
61e98203 | 7097 | ret = io_timeout_remove(req, issue_flags); |
11365043 | 7098 | break; |
17f2fe35 | 7099 | case IORING_OP_ACCEPT: |
889fca73 | 7100 | ret = io_accept(req, issue_flags); |
17f2fe35 | 7101 | break; |
f8e85cf2 | 7102 | case IORING_OP_CONNECT: |
889fca73 | 7103 | ret = io_connect(req, issue_flags); |
f8e85cf2 | 7104 | break; |
62755e35 | 7105 | case IORING_OP_ASYNC_CANCEL: |
61e98203 | 7106 | ret = io_async_cancel(req, issue_flags); |
62755e35 | 7107 | break; |
d63d1b5e | 7108 | case IORING_OP_FALLOCATE: |
45d189c6 | 7109 | ret = io_fallocate(req, issue_flags); |
d63d1b5e | 7110 | break; |
15b71abe | 7111 | case IORING_OP_OPENAT: |
45d189c6 | 7112 | ret = io_openat(req, issue_flags); |
15b71abe | 7113 | break; |
b5dba59e | 7114 | case IORING_OP_CLOSE: |
889fca73 | 7115 | ret = io_close(req, issue_flags); |
b5dba59e | 7116 | break; |
05f3fb3c | 7117 | case IORING_OP_FILES_UPDATE: |
889fca73 | 7118 | ret = io_files_update(req, issue_flags); |
05f3fb3c | 7119 | break; |
eddc7ef5 | 7120 | case IORING_OP_STATX: |
45d189c6 | 7121 | ret = io_statx(req, issue_flags); |
eddc7ef5 | 7122 | break; |
4840e418 | 7123 | case IORING_OP_FADVISE: |
45d189c6 | 7124 | ret = io_fadvise(req, issue_flags); |
4840e418 | 7125 | break; |
c1ca757b | 7126 | case IORING_OP_MADVISE: |
45d189c6 | 7127 | ret = io_madvise(req, issue_flags); |
c1ca757b | 7128 | break; |
cebdb986 | 7129 | case IORING_OP_OPENAT2: |
45d189c6 | 7130 | ret = io_openat2(req, issue_flags); |
cebdb986 | 7131 | break; |
3e4827b0 | 7132 | case IORING_OP_EPOLL_CTL: |
889fca73 | 7133 | ret = io_epoll_ctl(req, issue_flags); |
3e4827b0 | 7134 | break; |
7d67af2c | 7135 | case IORING_OP_SPLICE: |
45d189c6 | 7136 | ret = io_splice(req, issue_flags); |
7d67af2c | 7137 | break; |
ddf0322d | 7138 | case IORING_OP_PROVIDE_BUFFERS: |
889fca73 | 7139 | ret = io_provide_buffers(req, issue_flags); |
ddf0322d | 7140 | break; |
067524e9 | 7141 | case IORING_OP_REMOVE_BUFFERS: |
889fca73 | 7142 | ret = io_remove_buffers(req, issue_flags); |
3e4827b0 | 7143 | break; |
f2a8d5c7 | 7144 | case IORING_OP_TEE: |
45d189c6 | 7145 | ret = io_tee(req, issue_flags); |
f2a8d5c7 | 7146 | break; |
36f4fa68 | 7147 | case IORING_OP_SHUTDOWN: |
45d189c6 | 7148 | ret = io_shutdown(req, issue_flags); |
36f4fa68 | 7149 | break; |
80a261fd | 7150 | case IORING_OP_RENAMEAT: |
45d189c6 | 7151 | ret = io_renameat(req, issue_flags); |
80a261fd | 7152 | break; |
14a1143b | 7153 | case IORING_OP_UNLINKAT: |
45d189c6 | 7154 | ret = io_unlinkat(req, issue_flags); |
14a1143b | 7155 | break; |
e34a02dc DK |
7156 | case IORING_OP_MKDIRAT: |
7157 | ret = io_mkdirat(req, issue_flags); | |
7158 | break; | |
7a8721f8 DK |
7159 | case IORING_OP_SYMLINKAT: |
7160 | ret = io_symlinkat(req, issue_flags); | |
7161 | break; | |
cf30da90 DK |
7162 | case IORING_OP_LINKAT: |
7163 | ret = io_linkat(req, issue_flags); | |
7164 | break; | |
4f57f06c JA |
7165 | case IORING_OP_MSG_RING: |
7166 | ret = io_msg_ring(req, issue_flags); | |
7167 | break; | |
2b188cc1 JA |
7168 | default: |
7169 | ret = -EINVAL; | |
7170 | break; | |
7171 | } | |
7172 | ||
5bd2182d PM |
7173 | if (!io_op_defs[req->opcode].audit_skip) |
7174 | audit_uring_exit(!ret, ret); | |
7175 | ||
5730b27e JA |
7176 | if (creds) |
7177 | revert_creds(creds); | |
def596e9 JA |
7178 | if (ret) |
7179 | return ret; | |
b532576e | 7180 | /* If the op doesn't have a file, we're not polling for it */ |
9983028e | 7181 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && req->file) |
9882131c | 7182 | io_iopoll_req_issued(req, issue_flags); |
def596e9 JA |
7183 | |
7184 | return 0; | |
2b188cc1 JA |
7185 | } |
7186 | ||
ebc11b6c PB |
7187 | static struct io_wq_work *io_wq_free_work(struct io_wq_work *work) |
7188 | { | |
7189 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
7190 | ||
7191 | req = io_put_req_find_next(req); | |
7192 | return req ? &req->work : NULL; | |
7193 | } | |
7194 | ||
5280f7e5 | 7195 | static void io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
7196 | { |
7197 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
d01905db PB |
7198 | unsigned int issue_flags = IO_URING_F_UNLOCKED; |
7199 | bool needs_poll = false; | |
6df1db6b | 7200 | struct io_kiocb *timeout; |
561fb04a | 7201 | int ret = 0; |
2b188cc1 | 7202 | |
48dcd38d PB |
7203 | /* one will be dropped by ->io_free_work() after returning to io-wq */ |
7204 | if (!(req->flags & REQ_F_REFCOUNT)) | |
7205 | __io_req_set_refcount(req, 2); | |
7206 | else | |
7207 | req_ref_get(req); | |
5d5901a3 | 7208 | |
6df1db6b PB |
7209 | timeout = io_prep_linked_timeout(req); |
7210 | if (timeout) | |
7211 | io_queue_linked_timeout(timeout); | |
d4c81f38 | 7212 | |
dadebc35 | 7213 | /* either cancelled or io-wq is dying, so don't touch tctx->iowq */ |
d01905db PB |
7214 | if (work->flags & IO_WQ_WORK_CANCEL) { |
7215 | io_req_task_queue_fail(req, -ECANCELED); | |
7216 | return; | |
7217 | } | |
31b51510 | 7218 | |
d01905db | 7219 | if (req->flags & REQ_F_FORCE_ASYNC) { |
afb7f56f PB |
7220 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
7221 | bool opcode_poll = def->pollin || def->pollout; | |
7222 | ||
7223 | if (opcode_poll && file_can_poll(req->file)) { | |
7224 | needs_poll = true; | |
d01905db | 7225 | issue_flags |= IO_URING_F_NONBLOCK; |
afb7f56f | 7226 | } |
561fb04a | 7227 | } |
31b51510 | 7228 | |
d01905db PB |
7229 | do { |
7230 | ret = io_issue_sqe(req, issue_flags); | |
7231 | if (ret != -EAGAIN) | |
7232 | break; | |
7233 | /* | |
7234 | * We can get EAGAIN for iopolled IO even though we're | |
7235 | * forcing a sync submission from here, since we can't | |
7236 | * wait for request slots on the block side. | |
7237 | */ | |
7238 | if (!needs_poll) { | |
7239 | cond_resched(); | |
7240 | continue; | |
90fa0288 HX |
7241 | } |
7242 | ||
d01905db PB |
7243 | if (io_arm_poll_handler(req) == IO_APOLL_OK) |
7244 | return; | |
7245 | /* aborted or ready, in either case retry blocking */ | |
7246 | needs_poll = false; | |
7247 | issue_flags &= ~IO_URING_F_NONBLOCK; | |
7248 | } while (1); | |
31b51510 | 7249 | |
a3df7698 | 7250 | /* avoid locking problems by failing it from a clean context */ |
5d5901a3 | 7251 | if (ret) |
a3df7698 | 7252 | io_req_task_queue_fail(req, ret); |
2b188cc1 JA |
7253 | } |
7254 | ||
aeca241b | 7255 | static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table, |
042b0d85 | 7256 | unsigned i) |
65e19f54 | 7257 | { |
042b0d85 | 7258 | return &table->files[i]; |
dafecf19 PB |
7259 | } |
7260 | ||
65e19f54 JA |
7261 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
7262 | int index) | |
7263 | { | |
aeca241b | 7264 | struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index); |
65e19f54 | 7265 | |
a04b0ac0 | 7266 | return (struct file *) (slot->file_ptr & FFS_MASK); |
65e19f54 JA |
7267 | } |
7268 | ||
a04b0ac0 | 7269 | static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file) |
9a321c98 PB |
7270 | { |
7271 | unsigned long file_ptr = (unsigned long) file; | |
7272 | ||
88459b50 | 7273 | file_ptr |= io_file_get_flags(file); |
a04b0ac0 | 7274 | file_slot->file_ptr = file_ptr; |
65e19f54 JA |
7275 | } |
7276 | ||
ac177053 PB |
7277 | static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx, |
7278 | struct io_kiocb *req, int fd) | |
09bb8394 | 7279 | { |
8da11c19 | 7280 | struct file *file; |
ac177053 | 7281 | unsigned long file_ptr; |
09bb8394 | 7282 | |
ac177053 PB |
7283 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
7284 | return NULL; | |
7285 | fd = array_index_nospec(fd, ctx->nr_user_files); | |
7286 | file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr; | |
7287 | file = (struct file *) (file_ptr & FFS_MASK); | |
7288 | file_ptr &= ~FFS_MASK; | |
7289 | /* mask in overlapping REQ_F and FFS bits */ | |
35645ac3 | 7290 | req->flags |= (file_ptr << REQ_F_SUPPORT_NOWAIT_BIT); |
a46be971 | 7291 | io_req_set_rsrc_node(req, ctx); |
ac177053 PB |
7292 | return file; |
7293 | } | |
d44f554e | 7294 | |
ac177053 | 7295 | static struct file *io_file_get_normal(struct io_ring_ctx *ctx, |
ac177053 PB |
7296 | struct io_kiocb *req, int fd) |
7297 | { | |
62906e89 | 7298 | struct file *file = fget(fd); |
ac177053 | 7299 | |
502c87d6 | 7300 | trace_io_uring_file_get(ctx, req, req->user_data, fd); |
09bb8394 | 7301 | |
ac177053 PB |
7302 | /* we don't allow fixed io_uring files */ |
7303 | if (file && unlikely(file->f_op == &io_uring_fops)) | |
7304 | io_req_track_inflight(req); | |
8371adf5 | 7305 | return file; |
09bb8394 JA |
7306 | } |
7307 | ||
ac177053 | 7308 | static inline struct file *io_file_get(struct io_ring_ctx *ctx, |
ac177053 PB |
7309 | struct io_kiocb *req, int fd, bool fixed) |
7310 | { | |
7311 | if (fixed) | |
7312 | return io_file_get_fixed(ctx, req, fd); | |
7313 | else | |
62906e89 | 7314 | return io_file_get_normal(ctx, req, fd); |
ac177053 PB |
7315 | } |
7316 | ||
f237c30a | 7317 | static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked) |
89b263f6 JA |
7318 | { |
7319 | struct io_kiocb *prev = req->timeout.prev; | |
617a8948 | 7320 | int ret = -ENOENT; |
89b263f6 JA |
7321 | |
7322 | if (prev) { | |
617a8948 PB |
7323 | if (!(req->task->flags & PF_EXITING)) |
7324 | ret = io_try_cancel_userdata(req, prev->user_data); | |
505657bc | 7325 | io_req_complete_post(req, ret ?: -ETIME, 0); |
89b263f6 | 7326 | io_put_req(prev); |
89b263f6 JA |
7327 | } else { |
7328 | io_req_complete_post(req, -ETIME, 0); | |
7329 | } | |
7330 | } | |
7331 | ||
2665abfd | 7332 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 7333 | { |
ad8a48ac JA |
7334 | struct io_timeout_data *data = container_of(timer, |
7335 | struct io_timeout_data, timer); | |
90cd7e42 | 7336 | struct io_kiocb *prev, *req = data->req; |
2665abfd | 7337 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 7338 | unsigned long flags; |
2665abfd | 7339 | |
89b263f6 | 7340 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
90cd7e42 PB |
7341 | prev = req->timeout.head; |
7342 | req->timeout.head = NULL; | |
2665abfd JA |
7343 | |
7344 | /* | |
7345 | * We don't expect the list to be empty, that will only happen if we | |
7346 | * race with the completion of the linked work. | |
7347 | */ | |
447c19f3 | 7348 | if (prev) { |
f2f87370 | 7349 | io_remove_next_linked(prev); |
447c19f3 PB |
7350 | if (!req_ref_inc_not_zero(prev)) |
7351 | prev = NULL; | |
7352 | } | |
ef9dd637 | 7353 | list_del(&req->timeout.list); |
89b263f6 JA |
7354 | req->timeout.prev = prev; |
7355 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); | |
2665abfd | 7356 | |
89b263f6 | 7357 | req->io_task_work.func = io_req_task_link_timeout; |
4813c377 | 7358 | io_req_task_work_add(req, false); |
2665abfd JA |
7359 | return HRTIMER_NORESTART; |
7360 | } | |
7361 | ||
de968c18 | 7362 | static void io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 7363 | { |
de968c18 PB |
7364 | struct io_ring_ctx *ctx = req->ctx; |
7365 | ||
89b263f6 | 7366 | spin_lock_irq(&ctx->timeout_lock); |
76a46e06 | 7367 | /* |
f2f87370 PB |
7368 | * If the back reference is NULL, then our linked request finished |
7369 | * before we got a chance to setup the timer | |
76a46e06 | 7370 | */ |
90cd7e42 | 7371 | if (req->timeout.head) { |
e8c2bc1f | 7372 | struct io_timeout_data *data = req->async_data; |
94ae5e77 | 7373 | |
ad8a48ac JA |
7374 | data->timer.function = io_link_timeout_fn; |
7375 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
7376 | data->mode); | |
ef9dd637 | 7377 | list_add_tail(&req->timeout.list, &ctx->ltimeout_list); |
2665abfd | 7378 | } |
89b263f6 | 7379 | spin_unlock_irq(&ctx->timeout_lock); |
2665abfd | 7380 | /* drop submission reference */ |
76a46e06 JA |
7381 | io_put_req(req); |
7382 | } | |
2665abfd | 7383 | |
d475a9a6 PB |
7384 | static void io_queue_sqe_arm_apoll(struct io_kiocb *req) |
7385 | __must_hold(&req->ctx->uring_lock) | |
7386 | { | |
7387 | struct io_kiocb *linked_timeout = io_prep_linked_timeout(req); | |
7388 | ||
7389 | switch (io_arm_poll_handler(req)) { | |
7390 | case IO_APOLL_READY: | |
d475a9a6 PB |
7391 | io_req_task_queue(req); |
7392 | break; | |
7393 | case IO_APOLL_ABORTED: | |
7394 | /* | |
7395 | * Queued up for async execution, worker will release | |
7396 | * submit reference when the iocb is actually submitted. | |
7397 | */ | |
7398 | io_queue_async_work(req, NULL); | |
7399 | break; | |
7400 | } | |
7401 | ||
7402 | if (linked_timeout) | |
7403 | io_queue_linked_timeout(linked_timeout); | |
7404 | } | |
7405 | ||
7406 | static inline void __io_queue_sqe(struct io_kiocb *req) | |
282cdc86 | 7407 | __must_hold(&req->ctx->uring_lock) |
2b188cc1 | 7408 | { |
906c6caa | 7409 | struct io_kiocb *linked_timeout; |
e0c5c576 | 7410 | int ret; |
2b188cc1 | 7411 | |
c5eef2b9 | 7412 | ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER); |
193155c8 | 7413 | |
fff4e40e PB |
7414 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
7415 | io_req_add_compl_list(req); | |
d9f9d284 | 7416 | return; |
fff4e40e | 7417 | } |
491381ce JA |
7418 | /* |
7419 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
7420 | * doesn't support non-blocking read/write attempts | |
7421 | */ | |
1840038e | 7422 | if (likely(!ret)) { |
906c6caa PB |
7423 | linked_timeout = io_prep_linked_timeout(req); |
7424 | if (linked_timeout) | |
7425 | io_queue_linked_timeout(linked_timeout); | |
1840038e | 7426 | } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
d475a9a6 | 7427 | io_queue_sqe_arm_apoll(req); |
0d63c148 | 7428 | } else { |
f41db273 | 7429 | io_req_complete_failed(req, ret); |
9e645e11 | 7430 | } |
2b188cc1 JA |
7431 | } |
7432 | ||
4652fe3f | 7433 | static void io_queue_sqe_fallback(struct io_kiocb *req) |
282cdc86 | 7434 | __must_hold(&req->ctx->uring_lock) |
4fe2c963 | 7435 | { |
4652fe3f | 7436 | if (req->flags & REQ_F_FAIL) { |
c6d3d9cb | 7437 | io_req_complete_fail_submit(req); |
e0eb71dc PB |
7438 | } else if (unlikely(req->ctx->drain_active)) { |
7439 | io_drain_req(req); | |
76cc33d7 PB |
7440 | } else { |
7441 | int ret = io_req_prep_async(req); | |
7442 | ||
7443 | if (unlikely(ret)) | |
7444 | io_req_complete_failed(req, ret); | |
7445 | else | |
f237c30a | 7446 | io_queue_async_work(req, NULL); |
ce35a47a | 7447 | } |
4fe2c963 JL |
7448 | } |
7449 | ||
4652fe3f PB |
7450 | static inline void io_queue_sqe(struct io_kiocb *req) |
7451 | __must_hold(&req->ctx->uring_lock) | |
7452 | { | |
7453 | if (likely(!(req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL)))) | |
7454 | __io_queue_sqe(req); | |
7455 | else | |
7456 | io_queue_sqe_fallback(req); | |
7457 | } | |
7458 | ||
b16fed66 PB |
7459 | /* |
7460 | * Check SQE restrictions (opcode and flags). | |
7461 | * | |
7462 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
7463 | */ | |
7464 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
7465 | struct io_kiocb *req, | |
7466 | unsigned int sqe_flags) | |
4fe2c963 | 7467 | { |
b16fed66 PB |
7468 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) |
7469 | return false; | |
7470 | ||
7471 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
7472 | ctx->restrictions.sqe_flags_required) | |
7473 | return false; | |
7474 | ||
7475 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
7476 | ctx->restrictions.sqe_flags_required)) | |
7477 | return false; | |
7478 | ||
7479 | return true; | |
4fe2c963 JL |
7480 | } |
7481 | ||
22b2ca31 PB |
7482 | static void io_init_req_drain(struct io_kiocb *req) |
7483 | { | |
7484 | struct io_ring_ctx *ctx = req->ctx; | |
7485 | struct io_kiocb *head = ctx->submit_state.link.head; | |
7486 | ||
7487 | ctx->drain_active = true; | |
7488 | if (head) { | |
7489 | /* | |
7490 | * If we need to drain a request in the middle of a link, drain | |
7491 | * the head request and the next request/link after the current | |
7492 | * link. Considering sequential execution of links, | |
b6c7db32 | 7493 | * REQ_F_IO_DRAIN will be maintained for every request of our |
22b2ca31 PB |
7494 | * link. |
7495 | */ | |
b6c7db32 | 7496 | head->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC; |
22b2ca31 PB |
7497 | ctx->drain_next = true; |
7498 | } | |
7499 | } | |
7500 | ||
b16fed66 PB |
7501 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
7502 | const struct io_uring_sqe *sqe) | |
282cdc86 | 7503 | __must_hold(&ctx->uring_lock) |
b16fed66 | 7504 | { |
b16fed66 | 7505 | unsigned int sqe_flags; |
fc0ae024 | 7506 | int personality; |
4a04d1d1 | 7507 | u8 opcode; |
b16fed66 | 7508 | |
864ea921 | 7509 | /* req is partially pre-initialised, see io_preinit_req() */ |
4a04d1d1 | 7510 | req->opcode = opcode = READ_ONCE(sqe->opcode); |
b16fed66 PB |
7511 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
7512 | req->flags = sqe_flags = READ_ONCE(sqe->flags); | |
7513 | req->user_data = READ_ONCE(sqe->user_data); | |
b16fed66 | 7514 | req->file = NULL; |
b16fed66 | 7515 | req->fixed_rsrc_refs = NULL; |
b16fed66 | 7516 | req->task = current; |
b16fed66 | 7517 | |
4a04d1d1 PB |
7518 | if (unlikely(opcode >= IORING_OP_LAST)) { |
7519 | req->opcode = 0; | |
b16fed66 | 7520 | return -EINVAL; |
4a04d1d1 | 7521 | } |
68fe256a PB |
7522 | if (unlikely(sqe_flags & ~SQE_COMMON_FLAGS)) { |
7523 | /* enforce forwards compatibility on users */ | |
7524 | if (sqe_flags & ~SQE_VALID_FLAGS) | |
7525 | return -EINVAL; | |
7526 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && | |
4a04d1d1 | 7527 | !io_op_defs[opcode].buffer_select) |
68fe256a | 7528 | return -EOPNOTSUPP; |
5562a8d7 PB |
7529 | if (sqe_flags & IOSQE_CQE_SKIP_SUCCESS) |
7530 | ctx->drain_disabled = true; | |
7531 | if (sqe_flags & IOSQE_IO_DRAIN) { | |
7532 | if (ctx->drain_disabled) | |
7533 | return -EOPNOTSUPP; | |
22b2ca31 | 7534 | io_init_req_drain(req); |
5562a8d7 | 7535 | } |
2a56a9bd PB |
7536 | } |
7537 | if (unlikely(ctx->restricted || ctx->drain_active || ctx->drain_next)) { | |
7538 | if (ctx->restricted && !io_check_restriction(ctx, req, sqe_flags)) | |
7539 | return -EACCES; | |
7540 | /* knock it to the slow queue path, will be drained there */ | |
7541 | if (ctx->drain_active) | |
7542 | req->flags |= REQ_F_FORCE_ASYNC; | |
7543 | /* if there is no link, we're at "next" request and need to drain */ | |
7544 | if (unlikely(ctx->drain_next) && !ctx->submit_state.link.head) { | |
7545 | ctx->drain_next = false; | |
7546 | ctx->drain_active = true; | |
b6c7db32 | 7547 | req->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC; |
2a56a9bd | 7548 | } |
68fe256a | 7549 | } |
b16fed66 | 7550 | |
4a04d1d1 | 7551 | if (io_op_defs[opcode].needs_file) { |
6d63416d PB |
7552 | struct io_submit_state *state = &ctx->submit_state; |
7553 | ||
7554 | /* | |
7555 | * Plug now if we have more than 2 IO left after this, and the | |
7556 | * target is potentially a read/write to block based storage. | |
7557 | */ | |
4a04d1d1 | 7558 | if (state->need_plug && io_op_defs[opcode].plug) { |
6d63416d PB |
7559 | state->plug_started = true; |
7560 | state->need_plug = false; | |
5ca7a8b3 | 7561 | blk_start_plug_nr_ios(&state->plug, state->submit_nr); |
6d63416d PB |
7562 | } |
7563 | ||
62906e89 | 7564 | req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd), |
ac177053 | 7565 | (sqe_flags & IOSQE_FIXED_FILE)); |
b16fed66 | 7566 | if (unlikely(!req->file)) |
fc0ae024 | 7567 | return -EBADF; |
b16fed66 | 7568 | } |
863e0560 | 7569 | |
003e8dcc JA |
7570 | personality = READ_ONCE(sqe->personality); |
7571 | if (personality) { | |
cdab10bf LT |
7572 | int ret; |
7573 | ||
c10d1f98 PB |
7574 | req->creds = xa_load(&ctx->personalities, personality); |
7575 | if (!req->creds) | |
003e8dcc | 7576 | return -EINVAL; |
c10d1f98 | 7577 | get_cred(req->creds); |
cdc1404a PM |
7578 | ret = security_uring_override_creds(req->creds); |
7579 | if (ret) { | |
7580 | put_cred(req->creds); | |
7581 | return ret; | |
7582 | } | |
b8e64b53 | 7583 | req->flags |= REQ_F_CREDS; |
003e8dcc | 7584 | } |
b16fed66 | 7585 | |
fc0ae024 | 7586 | return io_req_prep(req, sqe); |
b16fed66 PB |
7587 | } |
7588 | ||
a6b8cadc | 7589 | static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
a1ab7b35 | 7590 | const struct io_uring_sqe *sqe) |
282cdc86 | 7591 | __must_hold(&ctx->uring_lock) |
9e645e11 | 7592 | { |
a1ab7b35 | 7593 | struct io_submit_link *link = &ctx->submit_state.link; |
ef4ff581 | 7594 | int ret; |
9e645e11 | 7595 | |
a6b8cadc PB |
7596 | ret = io_init_req(ctx, req, sqe); |
7597 | if (unlikely(ret)) { | |
502c87d6 | 7598 | trace_io_uring_req_failed(sqe, ctx, req, ret); |
a87acfde | 7599 | |
a8295b98 | 7600 | /* fail even hard links since we don't submit */ |
de59bc10 | 7601 | if (link->head) { |
a8295b98 HX |
7602 | /* |
7603 | * we can judge a link req is failed or cancelled by if | |
7604 | * REQ_F_FAIL is set, but the head is an exception since | |
7605 | * it may be set REQ_F_FAIL because of other req's failure | |
7606 | * so let's leverage req->result to distinguish if a head | |
7607 | * is set REQ_F_FAIL because of its failure or other req's | |
7608 | * failure so that we can set the correct ret code for it. | |
7609 | * init result here to avoid affecting the normal path. | |
7610 | */ | |
7611 | if (!(link->head->flags & REQ_F_FAIL)) | |
7612 | req_fail_link_node(link->head, -ECANCELED); | |
7613 | } else if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { | |
7614 | /* | |
7615 | * the current req is a normal req, we should return | |
7616 | * error and thus break the submittion loop. | |
7617 | */ | |
7618 | io_req_complete_failed(req, ret); | |
7619 | return ret; | |
de59bc10 | 7620 | } |
a8295b98 | 7621 | req_fail_link_node(req, ret); |
a6b8cadc | 7622 | } |
441b8a78 | 7623 | |
be7053b7 | 7624 | /* don't need @sqe from now on */ |
502c87d6 | 7625 | trace_io_uring_submit_sqe(ctx, req, req->user_data, req->opcode, |
236daeae OL |
7626 | req->flags, true, |
7627 | ctx->flags & IORING_SETUP_SQPOLL); | |
a6b8cadc | 7628 | |
9e645e11 JA |
7629 | /* |
7630 | * If we already have a head request, queue this one for async | |
7631 | * submittal once the head completes. If we don't have a head but | |
7632 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
7633 | * submitted sync once the chain is complete. If none of those | |
7634 | * conditions are true (normal request), then just queue it. | |
7635 | */ | |
863e0560 PB |
7636 | if (link->head) { |
7637 | struct io_kiocb *head = link->head; | |
4e88d6e7 | 7638 | |
a8295b98 HX |
7639 | if (!(req->flags & REQ_F_FAIL)) { |
7640 | ret = io_req_prep_async(req); | |
7641 | if (unlikely(ret)) { | |
7642 | req_fail_link_node(req, ret); | |
7643 | if (!(head->flags & REQ_F_FAIL)) | |
7644 | req_fail_link_node(head, -ECANCELED); | |
7645 | } | |
7646 | } | |
9d76377f | 7647 | trace_io_uring_link(ctx, req, head); |
f2f87370 | 7648 | link->last->link = req; |
863e0560 | 7649 | link->last = req; |
32fe525b | 7650 | |
f15a3431 PB |
7651 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) |
7652 | return 0; | |
32fe525b | 7653 | /* last request of a link, enqueue the link */ |
f15a3431 PB |
7654 | link->head = NULL; |
7655 | req = head; | |
7656 | } else if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { | |
7657 | link->head = req; | |
7658 | link->last = req; | |
7659 | return 0; | |
9e645e11 | 7660 | } |
2e6e1fde | 7661 | |
f15a3431 | 7662 | io_queue_sqe(req); |
1d4240cc | 7663 | return 0; |
9e645e11 JA |
7664 | } |
7665 | ||
9a56a232 JA |
7666 | /* |
7667 | * Batched submission is done, ensure local IO is flushed out. | |
7668 | */ | |
553deffd | 7669 | static void io_submit_state_end(struct io_ring_ctx *ctx) |
9a56a232 | 7670 | { |
553deffd PB |
7671 | struct io_submit_state *state = &ctx->submit_state; |
7672 | ||
a1ab7b35 | 7673 | if (state->link.head) |
de59bc10 | 7674 | io_queue_sqe(state->link.head); |
553deffd | 7675 | /* flush only after queuing links as they can generate completions */ |
c450178d | 7676 | io_submit_flush_completions(ctx); |
27926b68 JA |
7677 | if (state->plug_started) |
7678 | blk_finish_plug(&state->plug); | |
9a56a232 JA |
7679 | } |
7680 | ||
7681 | /* | |
7682 | * Start submission side cache. | |
7683 | */ | |
7684 | static void io_submit_state_start(struct io_submit_state *state, | |
ba88ff11 | 7685 | unsigned int max_ios) |
9a56a232 | 7686 | { |
27926b68 | 7687 | state->plug_started = false; |
4b628aeb | 7688 | state->need_plug = max_ios > 2; |
5ca7a8b3 | 7689 | state->submit_nr = max_ios; |
a1ab7b35 PB |
7690 | /* set only head, no need to init link_last in advance */ |
7691 | state->link.head = NULL; | |
9a56a232 JA |
7692 | } |
7693 | ||
2b188cc1 JA |
7694 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
7695 | { | |
75b28aff | 7696 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 7697 | |
caf582c6 PB |
7698 | /* |
7699 | * Ensure any loads from the SQEs are done at this point, | |
7700 | * since once we write the new head, the application could | |
7701 | * write new data to them. | |
7702 | */ | |
7703 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
7704 | } |
7705 | ||
2b188cc1 | 7706 | /* |
dd9ae8a0 | 7707 | * Fetch an sqe, if one is available. Note this returns a pointer to memory |
2b188cc1 JA |
7708 | * that is mapped by userspace. This means that care needs to be taken to |
7709 | * ensure that reads are stable, as we cannot rely on userspace always | |
7710 | * being a good citizen. If members of the sqe are validated and then later | |
7711 | * used, it's important that those reads are done through READ_ONCE() to | |
7712 | * prevent a re-load down the line. | |
7713 | */ | |
709b302f | 7714 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 7715 | { |
ea5ab3b5 | 7716 | unsigned head, mask = ctx->sq_entries - 1; |
17d3aeb3 | 7717 | unsigned sq_idx = ctx->cached_sq_head++ & mask; |
2b188cc1 JA |
7718 | |
7719 | /* | |
7720 | * The cached sq head (or cq tail) serves two purposes: | |
7721 | * | |
7722 | * 1) allows us to batch the cost of updating the user visible | |
7723 | * head updates. | |
7724 | * 2) allows the kernel side to track the head on its own, even | |
7725 | * though the application is the one updating it. | |
7726 | */ | |
17d3aeb3 | 7727 | head = READ_ONCE(ctx->sq_array[sq_idx]); |
709b302f PB |
7728 | if (likely(head < ctx->sq_entries)) |
7729 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
7730 | |
7731 | /* drop invalid entries */ | |
15641e42 PB |
7732 | ctx->cq_extra--; |
7733 | WRITE_ONCE(ctx->rings->sq_dropped, | |
7734 | READ_ONCE(ctx->rings->sq_dropped) + 1); | |
709b302f PB |
7735 | return NULL; |
7736 | } | |
7737 | ||
0f212204 | 7738 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
282cdc86 | 7739 | __must_hold(&ctx->uring_lock) |
6c271ce2 | 7740 | { |
69629809 | 7741 | unsigned int entries = io_sqring_entries(ctx); |
46c4e16a | 7742 | int submitted = 0; |
6c271ce2 | 7743 | |
51d48dab | 7744 | if (unlikely(!entries)) |
69629809 | 7745 | return 0; |
ee7d46d9 | 7746 | /* make sure SQ entry isn't read before tail */ |
69629809 | 7747 | nr = min3(nr, ctx->sq_entries, entries); |
9a10867a | 7748 | io_get_task_refs(nr); |
6c271ce2 | 7749 | |
ba88ff11 | 7750 | io_submit_state_start(&ctx->submit_state, nr); |
69629809 | 7751 | do { |
3529d8c2 | 7752 | const struct io_uring_sqe *sqe; |
196be95c | 7753 | struct io_kiocb *req; |
fb5ccc98 | 7754 | |
a33ae9ce | 7755 | if (unlikely(!io_alloc_req_refill(ctx))) { |
196be95c PB |
7756 | if (!submitted) |
7757 | submitted = -EAGAIN; | |
fb5ccc98 | 7758 | break; |
196be95c | 7759 | } |
a33ae9ce | 7760 | req = io_alloc_req(ctx); |
4fccfcbb PB |
7761 | sqe = io_get_sqe(ctx); |
7762 | if (unlikely(!sqe)) { | |
c2b6c6bc | 7763 | wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list); |
4fccfcbb PB |
7764 | break; |
7765 | } | |
d3656344 JA |
7766 | /* will complete beyond this point, count as submitted */ |
7767 | submitted++; | |
a1ab7b35 | 7768 | if (io_submit_sqe(ctx, req, sqe)) |
196be95c | 7769 | break; |
69629809 | 7770 | } while (submitted < nr); |
6c271ce2 | 7771 | |
9466f437 PB |
7772 | if (unlikely(submitted != nr)) { |
7773 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; | |
d8a6df10 | 7774 | int unused = nr - ref_used; |
9466f437 | 7775 | |
09899b19 | 7776 | current->io_uring->cached_refs += unused; |
9466f437 | 7777 | } |
6c271ce2 | 7778 | |
553deffd | 7779 | io_submit_state_end(ctx); |
ae9428ca PB |
7780 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
7781 | io_commit_sqring(ctx); | |
7782 | ||
6c271ce2 JA |
7783 | return submitted; |
7784 | } | |
7785 | ||
e4b6d902 PB |
7786 | static inline bool io_sqd_events_pending(struct io_sq_data *sqd) |
7787 | { | |
7788 | return READ_ONCE(sqd->state); | |
7789 | } | |
7790 | ||
23b3628e XW |
7791 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
7792 | { | |
7793 | /* Tell userspace we may need a wakeup call */ | |
79ebeaee | 7794 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
7795 | WRITE_ONCE(ctx->rings->sq_flags, |
7796 | ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 7797 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
7798 | } |
7799 | ||
7800 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) | |
7801 | { | |
79ebeaee | 7802 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
7803 | WRITE_ONCE(ctx->rings->sq_flags, |
7804 | ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 7805 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
7806 | } |
7807 | ||
08369246 | 7808 | static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) |
6c271ce2 | 7809 | { |
c8d1ba58 | 7810 | unsigned int to_submit; |
bdcd3eab | 7811 | int ret = 0; |
6c271ce2 | 7812 | |
c8d1ba58 | 7813 | to_submit = io_sqring_entries(ctx); |
e95eee2d | 7814 | /* if we're handling multiple rings, cap submit size for fairness */ |
4ce8ad95 OL |
7815 | if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE) |
7816 | to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE; | |
e95eee2d | 7817 | |
5eef4e87 | 7818 | if (!wq_list_empty(&ctx->iopoll_list) || to_submit) { |
948e1947 PB |
7819 | const struct cred *creds = NULL; |
7820 | ||
7821 | if (ctx->sq_creds != current_cred()) | |
7822 | creds = override_creds(ctx->sq_creds); | |
a4c0b3de | 7823 | |
c8d1ba58 | 7824 | mutex_lock(&ctx->uring_lock); |
5eef4e87 | 7825 | if (!wq_list_empty(&ctx->iopoll_list)) |
5ba3c874 | 7826 | io_do_iopoll(ctx, true); |
906a3c6f | 7827 | |
3b763ba1 PB |
7828 | /* |
7829 | * Don't submit if refs are dying, good for io_uring_register(), | |
7830 | * but also it is relied upon by io_ring_exit_work() | |
7831 | */ | |
0298ef96 PB |
7832 | if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) && |
7833 | !(ctx->flags & IORING_SETUP_R_DISABLED)) | |
08369246 | 7834 | ret = io_submit_sqes(ctx, to_submit); |
c8d1ba58 | 7835 | mutex_unlock(&ctx->uring_lock); |
adc8682e OL |
7836 | #ifdef CONFIG_NET_RX_BUSY_POLL |
7837 | spin_lock(&ctx->napi_lock); | |
7838 | if (!list_empty(&ctx->napi_list) && | |
7839 | io_napi_busy_loop(&ctx->napi_list)) | |
7840 | ++ret; | |
7841 | spin_unlock(&ctx->napi_lock); | |
7842 | #endif | |
acfb381d PB |
7843 | if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait)) |
7844 | wake_up(&ctx->sqo_sq_wait); | |
948e1947 PB |
7845 | if (creds) |
7846 | revert_creds(creds); | |
acfb381d | 7847 | } |
6c271ce2 | 7848 | |
08369246 XW |
7849 | return ret; |
7850 | } | |
6c271ce2 | 7851 | |
c072481d | 7852 | static __cold void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
08369246 XW |
7853 | { |
7854 | struct io_ring_ctx *ctx; | |
7855 | unsigned sq_thread_idle = 0; | |
6c271ce2 | 7856 | |
c9dca27d PB |
7857 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7858 | sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle); | |
08369246 | 7859 | sqd->sq_thread_idle = sq_thread_idle; |
c8d1ba58 | 7860 | } |
6c271ce2 | 7861 | |
e4b6d902 PB |
7862 | static bool io_sqd_handle_event(struct io_sq_data *sqd) |
7863 | { | |
7864 | bool did_sig = false; | |
7865 | struct ksignal ksig; | |
7866 | ||
7867 | if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) || | |
7868 | signal_pending(current)) { | |
7869 | mutex_unlock(&sqd->lock); | |
7870 | if (signal_pending(current)) | |
7871 | did_sig = get_signal(&ksig); | |
7872 | cond_resched(); | |
7873 | mutex_lock(&sqd->lock); | |
7874 | } | |
e4b6d902 PB |
7875 | return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
7876 | } | |
7877 | ||
c8d1ba58 JA |
7878 | static int io_sq_thread(void *data) |
7879 | { | |
69fb2131 JA |
7880 | struct io_sq_data *sqd = data; |
7881 | struct io_ring_ctx *ctx; | |
a0d9205f | 7882 | unsigned long timeout = 0; |
37d1e2e3 | 7883 | char buf[TASK_COMM_LEN]; |
08369246 | 7884 | DEFINE_WAIT(wait); |
6c271ce2 | 7885 | |
696ee88a | 7886 | snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid); |
37d1e2e3 | 7887 | set_task_comm(current, buf); |
37d1e2e3 JA |
7888 | |
7889 | if (sqd->sq_cpu != -1) | |
7890 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); | |
7891 | else | |
7892 | set_cpus_allowed_ptr(current, cpu_online_mask); | |
7893 | current->flags |= PF_NO_SETAFFINITY; | |
7894 | ||
5bd2182d PM |
7895 | audit_alloc_kernel(current); |
7896 | ||
09a6f4ef | 7897 | mutex_lock(&sqd->lock); |
e4b6d902 | 7898 | while (1) { |
1a924a80 | 7899 | bool cap_entries, sqt_spin = false; |
c1edbf5f | 7900 | |
e4b6d902 PB |
7901 | if (io_sqd_events_pending(sqd) || signal_pending(current)) { |
7902 | if (io_sqd_handle_event(sqd)) | |
c7d95613 | 7903 | break; |
08369246 XW |
7904 | timeout = jiffies + sqd->sq_thread_idle; |
7905 | } | |
e4b6d902 | 7906 | |
e95eee2d | 7907 | cap_entries = !list_is_singular(&sqd->ctx_list); |
69fb2131 | 7908 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
948e1947 | 7909 | int ret = __io_sq_thread(ctx, cap_entries); |
7c30f36a | 7910 | |
5eef4e87 | 7911 | if (!sqt_spin && (ret > 0 || !wq_list_empty(&ctx->iopoll_list))) |
08369246 | 7912 | sqt_spin = true; |
69fb2131 | 7913 | } |
dd432ea5 PB |
7914 | if (io_run_task_work()) |
7915 | sqt_spin = true; | |
6c271ce2 | 7916 | |
08369246 | 7917 | if (sqt_spin || !time_after(jiffies, timeout)) { |
c8d1ba58 | 7918 | cond_resched(); |
08369246 XW |
7919 | if (sqt_spin) |
7920 | timeout = jiffies + sqd->sq_thread_idle; | |
7921 | continue; | |
7922 | } | |
7923 | ||
08369246 | 7924 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); |
dd432ea5 | 7925 | if (!io_sqd_events_pending(sqd) && !current->task_works) { |
1a924a80 PB |
7926 | bool needs_sched = true; |
7927 | ||
724cb4f9 | 7928 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
aaa9f0f4 PB |
7929 | io_ring_set_wakeup_flag(ctx); |
7930 | ||
724cb4f9 | 7931 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
5eef4e87 | 7932 | !wq_list_empty(&ctx->iopoll_list)) { |
724cb4f9 HX |
7933 | needs_sched = false; |
7934 | break; | |
7935 | } | |
7936 | if (io_sqring_entries(ctx)) { | |
7937 | needs_sched = false; | |
7938 | break; | |
7939 | } | |
7940 | } | |
7941 | ||
7942 | if (needs_sched) { | |
7943 | mutex_unlock(&sqd->lock); | |
7944 | schedule(); | |
7945 | mutex_lock(&sqd->lock); | |
7946 | } | |
69fb2131 JA |
7947 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7948 | io_ring_clear_wakeup_flag(ctx); | |
6c271ce2 | 7949 | } |
08369246 XW |
7950 | |
7951 | finish_wait(&sqd->wait, &wait); | |
7952 | timeout = jiffies + sqd->sq_thread_idle; | |
6c271ce2 | 7953 | } |
28cea78a | 7954 | |
78cc687b | 7955 | io_uring_cancel_generic(true, sqd); |
37d1e2e3 | 7956 | sqd->thread = NULL; |
05962f95 | 7957 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
5f3f26f9 | 7958 | io_ring_set_wakeup_flag(ctx); |
521d6a73 | 7959 | io_run_task_work(); |
734551df PB |
7960 | mutex_unlock(&sqd->lock); |
7961 | ||
5bd2182d PM |
7962 | audit_free(current); |
7963 | ||
37d1e2e3 JA |
7964 | complete(&sqd->exited); |
7965 | do_exit(0); | |
6c271ce2 JA |
7966 | } |
7967 | ||
bda52162 JA |
7968 | struct io_wait_queue { |
7969 | struct wait_queue_entry wq; | |
7970 | struct io_ring_ctx *ctx; | |
5fd46178 | 7971 | unsigned cq_tail; |
bda52162 | 7972 | unsigned nr_timeouts; |
adc8682e OL |
7973 | #ifdef CONFIG_NET_RX_BUSY_POLL |
7974 | unsigned busy_poll_to; | |
7975 | #endif | |
bda52162 JA |
7976 | }; |
7977 | ||
6c503150 | 7978 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
bda52162 JA |
7979 | { |
7980 | struct io_ring_ctx *ctx = iowq->ctx; | |
5fd46178 | 7981 | int dist = ctx->cached_cq_tail - (int) iowq->cq_tail; |
bda52162 JA |
7982 | |
7983 | /* | |
d195a66e | 7984 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
7985 | * started waiting. For timeouts, we always want to return to userspace, |
7986 | * regardless of event count. | |
7987 | */ | |
5fd46178 | 7988 | return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
bda52162 JA |
7989 | } |
7990 | ||
7991 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
7992 | int wake_flags, void *key) | |
7993 | { | |
7994 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
7995 | wq); | |
7996 | ||
6c503150 PB |
7997 | /* |
7998 | * Cannot safely flush overflowed CQEs from here, ensure we wake up | |
7999 | * the task, and the next invocation will do it. | |
8000 | */ | |
5ed7a37d | 8001 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->check_cq_overflow)) |
6c503150 PB |
8002 | return autoremove_wake_function(curr, mode, wake_flags, key); |
8003 | return -1; | |
bda52162 JA |
8004 | } |
8005 | ||
af9c1a44 JA |
8006 | static int io_run_task_work_sig(void) |
8007 | { | |
8008 | if (io_run_task_work()) | |
8009 | return 1; | |
0b8cfa97 | 8010 | if (test_thread_flag(TIF_NOTIFY_SIGNAL)) |
792ee0f6 | 8011 | return -ERESTARTSYS; |
c5020bc8 OL |
8012 | if (task_sigpending(current)) |
8013 | return -EINTR; | |
8014 | return 0; | |
af9c1a44 JA |
8015 | } |
8016 | ||
eeb60b9a PB |
8017 | /* when returns >0, the caller should retry */ |
8018 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, | |
8019 | struct io_wait_queue *iowq, | |
22833966 | 8020 | ktime_t timeout) |
eeb60b9a PB |
8021 | { |
8022 | int ret; | |
8023 | ||
8024 | /* make sure we run task_work before checking for signals */ | |
8025 | ret = io_run_task_work_sig(); | |
8026 | if (ret || io_should_wake(iowq)) | |
8027 | return ret; | |
8028 | /* let the caller flush overflows, retry */ | |
5ed7a37d | 8029 | if (test_bit(0, &ctx->check_cq_overflow)) |
eeb60b9a PB |
8030 | return 1; |
8031 | ||
22833966 JA |
8032 | if (!schedule_hrtimeout(&timeout, HRTIMER_MODE_ABS)) |
8033 | return -ETIME; | |
8034 | return 1; | |
eeb60b9a PB |
8035 | } |
8036 | ||
adc8682e OL |
8037 | #ifdef CONFIG_NET_RX_BUSY_POLL |
8038 | static void io_adjust_busy_loop_timeout(struct timespec64 *ts, | |
8039 | struct io_wait_queue *iowq) | |
8040 | { | |
8041 | unsigned busy_poll_to = READ_ONCE(sysctl_net_busy_poll); | |
8042 | struct timespec64 pollto = ns_to_timespec64(1000 * (s64)busy_poll_to); | |
8043 | ||
8044 | if (timespec64_compare(ts, &pollto) > 0) { | |
8045 | *ts = timespec64_sub(*ts, pollto); | |
8046 | iowq->busy_poll_to = busy_poll_to; | |
8047 | } else { | |
8048 | u64 to = timespec64_to_ns(ts); | |
8049 | ||
8050 | do_div(to, 1000); | |
8051 | iowq->busy_poll_to = to; | |
8052 | ts->tv_sec = 0; | |
8053 | ts->tv_nsec = 0; | |
8054 | } | |
8055 | } | |
8056 | ||
8057 | static inline bool io_busy_loop_timeout(unsigned long start_time, | |
8058 | unsigned long bp_usec) | |
8059 | { | |
8060 | if (bp_usec) { | |
8061 | unsigned long end_time = start_time + bp_usec; | |
8062 | unsigned long now = busy_loop_current_time(); | |
8063 | ||
8064 | return time_after(now, end_time); | |
8065 | } | |
8066 | return true; | |
8067 | } | |
8068 | ||
8069 | static bool io_busy_loop_end(void *p, unsigned long start_time) | |
8070 | { | |
8071 | struct io_wait_queue *iowq = p; | |
8072 | ||
8073 | return signal_pending(current) || | |
8074 | io_should_wake(iowq) || | |
8075 | io_busy_loop_timeout(start_time, iowq->busy_poll_to); | |
8076 | } | |
8077 | ||
8078 | static void io_blocking_napi_busy_loop(struct list_head *napi_list, | |
8079 | struct io_wait_queue *iowq) | |
8080 | { | |
8081 | unsigned long start_time = | |
8082 | list_is_singular(napi_list) ? 0 : | |
8083 | busy_loop_current_time(); | |
8084 | ||
8085 | do { | |
8086 | if (list_is_singular(napi_list)) { | |
8087 | struct napi_entry *ne = | |
8088 | list_first_entry(napi_list, | |
8089 | struct napi_entry, list); | |
8090 | ||
8091 | napi_busy_loop(ne->napi_id, io_busy_loop_end, iowq, | |
8092 | true, BUSY_POLL_BUDGET); | |
8093 | io_check_napi_entry_timeout(ne); | |
8094 | break; | |
8095 | } | |
8096 | } while (io_napi_busy_loop(napi_list) && | |
8097 | !io_busy_loop_end(iowq, start_time)); | |
8098 | } | |
8099 | ||
8100 | static void io_putback_napi_list(struct io_ring_ctx *ctx, | |
8101 | struct list_head *napi_list) | |
8102 | { | |
8103 | struct napi_entry *cne, *lne; | |
8104 | ||
8105 | spin_lock(&ctx->napi_lock); | |
8106 | list_for_each_entry(cne, &ctx->napi_list, list) | |
8107 | list_for_each_entry(lne, napi_list, list) | |
8108 | if (cne->napi_id == lne->napi_id) { | |
8109 | list_del(&lne->list); | |
8110 | kfree(lne); | |
8111 | break; | |
8112 | } | |
8113 | list_splice(napi_list, &ctx->napi_list); | |
8114 | spin_unlock(&ctx->napi_lock); | |
8115 | } | |
8116 | #endif /* CONFIG_NET_RX_BUSY_POLL */ | |
8117 | ||
2b188cc1 JA |
8118 | /* |
8119 | * Wait until events become available, if we don't already have some. The | |
8120 | * application must reap them itself, as they reside on the shared cq ring. | |
8121 | */ | |
8122 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
c73ebb68 HX |
8123 | const sigset_t __user *sig, size_t sigsz, |
8124 | struct __kernel_timespec __user *uts) | |
2b188cc1 | 8125 | { |
90291099 | 8126 | struct io_wait_queue iowq; |
75b28aff | 8127 | struct io_rings *rings = ctx->rings; |
22833966 | 8128 | ktime_t timeout = KTIME_MAX; |
c1d5a224 | 8129 | int ret; |
adc8682e OL |
8130 | #ifdef CONFIG_NET_RX_BUSY_POLL |
8131 | LIST_HEAD(local_napi_list); | |
8132 | #endif | |
2b188cc1 | 8133 | |
b41e9852 | 8134 | do { |
90f67366 | 8135 | io_cqring_overflow_flush(ctx); |
6c503150 | 8136 | if (io_cqring_events(ctx) >= min_events) |
b41e9852 | 8137 | return 0; |
4c6e277c | 8138 | if (!io_run_task_work()) |
b41e9852 | 8139 | break; |
b41e9852 | 8140 | } while (1); |
2b188cc1 JA |
8141 | |
8142 | if (sig) { | |
9e75ad5d AB |
8143 | #ifdef CONFIG_COMPAT |
8144 | if (in_compat_syscall()) | |
8145 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 8146 | sigsz); |
9e75ad5d AB |
8147 | else |
8148 | #endif | |
b772434b | 8149 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 8150 | |
2b188cc1 JA |
8151 | if (ret) |
8152 | return ret; | |
8153 | } | |
8154 | ||
adc8682e OL |
8155 | #ifdef CONFIG_NET_RX_BUSY_POLL |
8156 | iowq.busy_poll_to = 0; | |
8157 | if (!(ctx->flags & IORING_SETUP_SQPOLL)) { | |
8158 | spin_lock(&ctx->napi_lock); | |
8159 | list_splice_init(&ctx->napi_list, &local_napi_list); | |
8160 | spin_unlock(&ctx->napi_lock); | |
8161 | } | |
8162 | #endif | |
950e79dd OL |
8163 | if (uts) { |
8164 | struct timespec64 ts; | |
8165 | ||
8166 | if (get_timespec64(&ts, uts)) | |
8167 | return -EFAULT; | |
adc8682e OL |
8168 | #ifdef CONFIG_NET_RX_BUSY_POLL |
8169 | if (!list_empty(&local_napi_list)) | |
8170 | io_adjust_busy_loop_timeout(&ts, &iowq); | |
8171 | #endif | |
950e79dd OL |
8172 | timeout = ktime_add_ns(timespec64_to_ktime(ts), ktime_get_ns()); |
8173 | } | |
adc8682e OL |
8174 | #ifdef CONFIG_NET_RX_BUSY_POLL |
8175 | else if (!list_empty(&local_napi_list)) | |
8176 | iowq.busy_poll_to = READ_ONCE(sysctl_net_busy_poll); | |
8177 | #endif | |
950e79dd | 8178 | |
90291099 PB |
8179 | init_waitqueue_func_entry(&iowq.wq, io_wake_function); |
8180 | iowq.wq.private = current; | |
8181 | INIT_LIST_HEAD(&iowq.wq.entry); | |
8182 | iowq.ctx = ctx; | |
bda52162 | 8183 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
5fd46178 | 8184 | iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events; |
90291099 | 8185 | |
c826bd7a | 8186 | trace_io_uring_cqring_wait(ctx, min_events); |
adc8682e OL |
8187 | #ifdef CONFIG_NET_RX_BUSY_POLL |
8188 | if (iowq.busy_poll_to) | |
8189 | io_blocking_napi_busy_loop(&local_napi_list, &iowq); | |
8190 | if (!list_empty(&local_napi_list)) | |
8191 | io_putback_napi_list(ctx, &local_napi_list); | |
8192 | #endif | |
bda52162 | 8193 | do { |
ca0a2651 | 8194 | /* if we can't even flush overflow, don't wait for more */ |
90f67366 | 8195 | if (!io_cqring_overflow_flush(ctx)) { |
ca0a2651 JA |
8196 | ret = -EBUSY; |
8197 | break; | |
8198 | } | |
311997b3 | 8199 | prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq, |
bda52162 | 8200 | TASK_INTERRUPTIBLE); |
22833966 | 8201 | ret = io_cqring_wait_schedule(ctx, &iowq, timeout); |
311997b3 | 8202 | finish_wait(&ctx->cq_wait, &iowq.wq); |
ca0a2651 | 8203 | cond_resched(); |
eeb60b9a | 8204 | } while (ret > 0); |
bda52162 | 8205 | |
b7db41c9 | 8206 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 8207 | |
75b28aff | 8208 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
8209 | } |
8210 | ||
9123c8ff | 8211 | static void io_free_page_table(void **table, size_t size) |
05f3fb3c | 8212 | { |
9123c8ff | 8213 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); |
05f3fb3c | 8214 | |
846a4ef2 | 8215 | for (i = 0; i < nr_tables; i++) |
9123c8ff PB |
8216 | kfree(table[i]); |
8217 | kfree(table); | |
8218 | } | |
8219 | ||
c072481d | 8220 | static __cold void **io_alloc_page_table(size_t size) |
9123c8ff PB |
8221 | { |
8222 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); | |
8223 | size_t init_size = size; | |
8224 | void **table; | |
8225 | ||
0bea96f5 | 8226 | table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
8227 | if (!table) |
8228 | return NULL; | |
8229 | ||
8230 | for (i = 0; i < nr_tables; i++) { | |
27f6b318 | 8231 | unsigned int this_size = min_t(size_t, size, PAGE_SIZE); |
9123c8ff | 8232 | |
0bea96f5 | 8233 | table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
8234 | if (!table[i]) { |
8235 | io_free_page_table(table, init_size); | |
8236 | return NULL; | |
8237 | } | |
8238 | size -= this_size; | |
8239 | } | |
8240 | return table; | |
05f3fb3c JA |
8241 | } |
8242 | ||
28a9fe25 | 8243 | static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node) |
1642b445 | 8244 | { |
28a9fe25 PB |
8245 | percpu_ref_exit(&ref_node->refs); |
8246 | kfree(ref_node); | |
1642b445 PB |
8247 | } |
8248 | ||
c072481d | 8249 | static __cold void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
b9bd2bea PB |
8250 | { |
8251 | struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs); | |
8252 | struct io_ring_ctx *ctx = node->rsrc_data->ctx; | |
8253 | unsigned long flags; | |
8254 | bool first_add = false; | |
b36a2050 | 8255 | unsigned long delay = HZ; |
b9bd2bea PB |
8256 | |
8257 | spin_lock_irqsave(&ctx->rsrc_ref_lock, flags); | |
8258 | node->done = true; | |
8259 | ||
b36a2050 DY |
8260 | /* if we are mid-quiesce then do not delay */ |
8261 | if (node->rsrc_data->quiesce) | |
8262 | delay = 0; | |
8263 | ||
b9bd2bea PB |
8264 | while (!list_empty(&ctx->rsrc_ref_list)) { |
8265 | node = list_first_entry(&ctx->rsrc_ref_list, | |
8266 | struct io_rsrc_node, node); | |
8267 | /* recycle ref nodes in order */ | |
8268 | if (!node->done) | |
8269 | break; | |
8270 | list_del(&node->node); | |
8271 | first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist); | |
8272 | } | |
8273 | spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags); | |
8274 | ||
8275 | if (first_add) | |
b36a2050 | 8276 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, delay); |
b9bd2bea PB |
8277 | } |
8278 | ||
f6133fbd | 8279 | static struct io_rsrc_node *io_rsrc_node_alloc(void) |
b9bd2bea PB |
8280 | { |
8281 | struct io_rsrc_node *ref_node; | |
8282 | ||
8283 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); | |
8284 | if (!ref_node) | |
8285 | return NULL; | |
8286 | ||
8287 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, | |
8288 | 0, GFP_KERNEL)) { | |
8289 | kfree(ref_node); | |
8290 | return NULL; | |
8291 | } | |
8292 | INIT_LIST_HEAD(&ref_node->node); | |
8293 | INIT_LIST_HEAD(&ref_node->rsrc_list); | |
8294 | ref_node->done = false; | |
8295 | return ref_node; | |
8296 | } | |
8297 | ||
a7f0ed5a PB |
8298 | static void io_rsrc_node_switch(struct io_ring_ctx *ctx, |
8299 | struct io_rsrc_data *data_to_kill) | |
ab409402 | 8300 | __must_hold(&ctx->uring_lock) |
6b06314c | 8301 | { |
a7f0ed5a PB |
8302 | WARN_ON_ONCE(!ctx->rsrc_backup_node); |
8303 | WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node); | |
6b06314c | 8304 | |
ab409402 PB |
8305 | io_rsrc_refs_drop(ctx); |
8306 | ||
a7f0ed5a PB |
8307 | if (data_to_kill) { |
8308 | struct io_rsrc_node *rsrc_node = ctx->rsrc_node; | |
82fbcfa9 | 8309 | |
a7f0ed5a | 8310 | rsrc_node->rsrc_data = data_to_kill; |
4956b9ea | 8311 | spin_lock_irq(&ctx->rsrc_ref_lock); |
a7f0ed5a | 8312 | list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list); |
4956b9ea | 8313 | spin_unlock_irq(&ctx->rsrc_ref_lock); |
82fbcfa9 | 8314 | |
3e942498 | 8315 | atomic_inc(&data_to_kill->refs); |
a7f0ed5a PB |
8316 | percpu_ref_kill(&rsrc_node->refs); |
8317 | ctx->rsrc_node = NULL; | |
8318 | } | |
6b06314c | 8319 | |
a7f0ed5a PB |
8320 | if (!ctx->rsrc_node) { |
8321 | ctx->rsrc_node = ctx->rsrc_backup_node; | |
8322 | ctx->rsrc_backup_node = NULL; | |
8323 | } | |
8bad28d8 HX |
8324 | } |
8325 | ||
a7f0ed5a | 8326 | static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx) |
8dd03afe PB |
8327 | { |
8328 | if (ctx->rsrc_backup_node) | |
8329 | return 0; | |
f6133fbd | 8330 | ctx->rsrc_backup_node = io_rsrc_node_alloc(); |
8dd03afe | 8331 | return ctx->rsrc_backup_node ? 0 : -ENOMEM; |
8bad28d8 HX |
8332 | } |
8333 | ||
c072481d PB |
8334 | static __cold int io_rsrc_ref_quiesce(struct io_rsrc_data *data, |
8335 | struct io_ring_ctx *ctx) | |
8bad28d8 HX |
8336 | { |
8337 | int ret; | |
05589553 | 8338 | |
215c3902 | 8339 | /* As we may drop ->uring_lock, other task may have started quiesce */ |
8bad28d8 HX |
8340 | if (data->quiesce) |
8341 | return -ENXIO; | |
05589553 | 8342 | |
8bad28d8 | 8343 | data->quiesce = true; |
1ffc5422 | 8344 | do { |
a7f0ed5a | 8345 | ret = io_rsrc_node_switch_start(ctx); |
8dd03afe | 8346 | if (ret) |
f2303b1f | 8347 | break; |
a7f0ed5a | 8348 | io_rsrc_node_switch(ctx, data); |
f2303b1f | 8349 | |
3e942498 PB |
8350 | /* kill initial ref, already quiesced if zero */ |
8351 | if (atomic_dec_and_test(&data->refs)) | |
8352 | break; | |
c018db4a | 8353 | mutex_unlock(&ctx->uring_lock); |
8bad28d8 | 8354 | flush_delayed_work(&ctx->rsrc_put_work); |
1ffc5422 | 8355 | ret = wait_for_completion_interruptible(&data->done); |
c018db4a JA |
8356 | if (!ret) { |
8357 | mutex_lock(&ctx->uring_lock); | |
80912cef DY |
8358 | if (atomic_read(&data->refs) > 0) { |
8359 | /* | |
8360 | * it has been revived by another thread while | |
8361 | * we were unlocked | |
8362 | */ | |
8363 | mutex_unlock(&ctx->uring_lock); | |
8364 | } else { | |
8365 | break; | |
8366 | } | |
c018db4a | 8367 | } |
8bad28d8 | 8368 | |
3e942498 PB |
8369 | atomic_inc(&data->refs); |
8370 | /* wait for all works potentially completing data->done */ | |
8371 | flush_delayed_work(&ctx->rsrc_put_work); | |
cb5e1b81 | 8372 | reinit_completion(&data->done); |
8dd03afe | 8373 | |
1ffc5422 | 8374 | ret = io_run_task_work_sig(); |
8bad28d8 | 8375 | mutex_lock(&ctx->uring_lock); |
f2303b1f | 8376 | } while (ret >= 0); |
8bad28d8 | 8377 | data->quiesce = false; |
05f3fb3c | 8378 | |
8bad28d8 | 8379 | return ret; |
d7954b2b BM |
8380 | } |
8381 | ||
2d091d62 PB |
8382 | static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx) |
8383 | { | |
8384 | unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK; | |
8385 | unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT; | |
8386 | ||
8387 | return &data->tags[table_idx][off]; | |
8388 | } | |
8389 | ||
44b31f2f | 8390 | static void io_rsrc_data_free(struct io_rsrc_data *data) |
1ad555c6 | 8391 | { |
2d091d62 PB |
8392 | size_t size = data->nr * sizeof(data->tags[0][0]); |
8393 | ||
8394 | if (data->tags) | |
8395 | io_free_page_table((void **)data->tags, size); | |
44b31f2f PB |
8396 | kfree(data); |
8397 | } | |
8398 | ||
c072481d PB |
8399 | static __cold int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put, |
8400 | u64 __user *utags, unsigned nr, | |
8401 | struct io_rsrc_data **pdata) | |
1ad555c6 | 8402 | { |
b895c9a6 | 8403 | struct io_rsrc_data *data; |
2d091d62 | 8404 | int ret = -ENOMEM; |
d878c816 | 8405 | unsigned i; |
1ad555c6 BM |
8406 | |
8407 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
8408 | if (!data) | |
d878c816 | 8409 | return -ENOMEM; |
2d091d62 | 8410 | data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0])); |
b60c8dce | 8411 | if (!data->tags) { |
1ad555c6 | 8412 | kfree(data); |
d878c816 PB |
8413 | return -ENOMEM; |
8414 | } | |
2d091d62 PB |
8415 | |
8416 | data->nr = nr; | |
8417 | data->ctx = ctx; | |
8418 | data->do_put = do_put; | |
d878c816 | 8419 | if (utags) { |
2d091d62 | 8420 | ret = -EFAULT; |
d878c816 | 8421 | for (i = 0; i < nr; i++) { |
fdd1dc31 CIK |
8422 | u64 *tag_slot = io_get_tag_slot(data, i); |
8423 | ||
8424 | if (copy_from_user(tag_slot, &utags[i], | |
8425 | sizeof(*tag_slot))) | |
2d091d62 | 8426 | goto fail; |
d878c816 | 8427 | } |
1ad555c6 | 8428 | } |
b60c8dce | 8429 | |
3e942498 | 8430 | atomic_set(&data->refs, 1); |
1ad555c6 | 8431 | init_completion(&data->done); |
d878c816 PB |
8432 | *pdata = data; |
8433 | return 0; | |
2d091d62 PB |
8434 | fail: |
8435 | io_rsrc_data_free(data); | |
8436 | return ret; | |
1ad555c6 BM |
8437 | } |
8438 | ||
9123c8ff PB |
8439 | static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files) |
8440 | { | |
0bea96f5 PB |
8441 | table->files = kvcalloc(nr_files, sizeof(table->files[0]), |
8442 | GFP_KERNEL_ACCOUNT); | |
9123c8ff PB |
8443 | return !!table->files; |
8444 | } | |
8445 | ||
042b0d85 | 8446 | static void io_free_file_tables(struct io_file_table *table) |
9123c8ff | 8447 | { |
042b0d85 | 8448 | kvfree(table->files); |
9123c8ff PB |
8449 | table->files = NULL; |
8450 | } | |
8451 | ||
fff4db76 | 8452 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
1ad555c6 | 8453 | { |
fff4db76 PB |
8454 | #if defined(CONFIG_UNIX) |
8455 | if (ctx->ring_sock) { | |
8456 | struct sock *sock = ctx->ring_sock->sk; | |
8457 | struct sk_buff *skb; | |
8458 | ||
8459 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
8460 | kfree_skb(skb); | |
8461 | } | |
8462 | #else | |
8463 | int i; | |
8464 | ||
8465 | for (i = 0; i < ctx->nr_user_files; i++) { | |
8466 | struct file *file; | |
8467 | ||
8468 | file = io_file_from_index(ctx, i); | |
8469 | if (file) | |
8470 | fput(file); | |
8471 | } | |
8472 | #endif | |
042b0d85 | 8473 | io_free_file_tables(&ctx->file_table); |
44b31f2f | 8474 | io_rsrc_data_free(ctx->file_data); |
fff4db76 PB |
8475 | ctx->file_data = NULL; |
8476 | ctx->nr_user_files = 0; | |
1ad555c6 BM |
8477 | } |
8478 | ||
d7954b2b BM |
8479 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
8480 | { | |
d7954b2b BM |
8481 | int ret; |
8482 | ||
08480400 | 8483 | if (!ctx->file_data) |
d7954b2b | 8484 | return -ENXIO; |
08480400 PB |
8485 | ret = io_rsrc_ref_quiesce(ctx->file_data, ctx); |
8486 | if (!ret) | |
8487 | __io_sqe_files_unregister(ctx); | |
8488 | return ret; | |
6b06314c JA |
8489 | } |
8490 | ||
37d1e2e3 | 8491 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
09a6f4ef | 8492 | __releases(&sqd->lock) |
37d1e2e3 | 8493 | { |
521d6a73 PB |
8494 | WARN_ON_ONCE(sqd->thread == current); |
8495 | ||
9e138a48 PB |
8496 | /* |
8497 | * Do the dance but not conditional clear_bit() because it'd race with | |
8498 | * other threads incrementing park_pending and setting the bit. | |
8499 | */ | |
37d1e2e3 | 8500 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
9e138a48 PB |
8501 | if (atomic_dec_return(&sqd->park_pending)) |
8502 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); | |
09a6f4ef | 8503 | mutex_unlock(&sqd->lock); |
37d1e2e3 JA |
8504 | } |
8505 | ||
86e0d676 | 8506 | static void io_sq_thread_park(struct io_sq_data *sqd) |
09a6f4ef | 8507 | __acquires(&sqd->lock) |
37d1e2e3 | 8508 | { |
521d6a73 PB |
8509 | WARN_ON_ONCE(sqd->thread == current); |
8510 | ||
9e138a48 | 8511 | atomic_inc(&sqd->park_pending); |
86e0d676 | 8512 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
09a6f4ef | 8513 | mutex_lock(&sqd->lock); |
05962f95 | 8514 | if (sqd->thread) |
86e0d676 | 8515 | wake_up_process(sqd->thread); |
37d1e2e3 JA |
8516 | } |
8517 | ||
8518 | static void io_sq_thread_stop(struct io_sq_data *sqd) | |
8519 | { | |
521d6a73 | 8520 | WARN_ON_ONCE(sqd->thread == current); |
88885f66 | 8521 | WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)); |
521d6a73 | 8522 | |
05962f95 | 8523 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
88885f66 | 8524 | mutex_lock(&sqd->lock); |
e8f98f24 JA |
8525 | if (sqd->thread) |
8526 | wake_up_process(sqd->thread); | |
09a6f4ef | 8527 | mutex_unlock(&sqd->lock); |
05962f95 | 8528 | wait_for_completion(&sqd->exited); |
37d1e2e3 JA |
8529 | } |
8530 | ||
534ca6d6 | 8531 | static void io_put_sq_data(struct io_sq_data *sqd) |
6c271ce2 | 8532 | { |
534ca6d6 | 8533 | if (refcount_dec_and_test(&sqd->refs)) { |
9e138a48 PB |
8534 | WARN_ON_ONCE(atomic_read(&sqd->park_pending)); |
8535 | ||
37d1e2e3 JA |
8536 | io_sq_thread_stop(sqd); |
8537 | kfree(sqd); | |
8538 | } | |
8539 | } | |
8540 | ||
8541 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) | |
8542 | { | |
8543 | struct io_sq_data *sqd = ctx->sq_data; | |
8544 | ||
8545 | if (sqd) { | |
05962f95 | 8546 | io_sq_thread_park(sqd); |
521d6a73 | 8547 | list_del_init(&ctx->sqd_list); |
37d1e2e3 | 8548 | io_sqd_update_thread_idle(sqd); |
05962f95 | 8549 | io_sq_thread_unpark(sqd); |
37d1e2e3 JA |
8550 | |
8551 | io_put_sq_data(sqd); | |
8552 | ctx->sq_data = NULL; | |
534ca6d6 JA |
8553 | } |
8554 | } | |
8555 | ||
aa06165d JA |
8556 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
8557 | { | |
8558 | struct io_ring_ctx *ctx_attach; | |
8559 | struct io_sq_data *sqd; | |
8560 | struct fd f; | |
8561 | ||
8562 | f = fdget(p->wq_fd); | |
8563 | if (!f.file) | |
8564 | return ERR_PTR(-ENXIO); | |
8565 | if (f.file->f_op != &io_uring_fops) { | |
8566 | fdput(f); | |
8567 | return ERR_PTR(-EINVAL); | |
8568 | } | |
8569 | ||
8570 | ctx_attach = f.file->private_data; | |
8571 | sqd = ctx_attach->sq_data; | |
8572 | if (!sqd) { | |
8573 | fdput(f); | |
8574 | return ERR_PTR(-EINVAL); | |
8575 | } | |
5c2469e0 JA |
8576 | if (sqd->task_tgid != current->tgid) { |
8577 | fdput(f); | |
8578 | return ERR_PTR(-EPERM); | |
8579 | } | |
aa06165d JA |
8580 | |
8581 | refcount_inc(&sqd->refs); | |
8582 | fdput(f); | |
8583 | return sqd; | |
8584 | } | |
8585 | ||
26984fbf PB |
8586 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p, |
8587 | bool *attached) | |
534ca6d6 JA |
8588 | { |
8589 | struct io_sq_data *sqd; | |
8590 | ||
26984fbf | 8591 | *attached = false; |
5c2469e0 JA |
8592 | if (p->flags & IORING_SETUP_ATTACH_WQ) { |
8593 | sqd = io_attach_sq_data(p); | |
26984fbf PB |
8594 | if (!IS_ERR(sqd)) { |
8595 | *attached = true; | |
5c2469e0 | 8596 | return sqd; |
26984fbf | 8597 | } |
5c2469e0 JA |
8598 | /* fall through for EPERM case, setup new sqd/task */ |
8599 | if (PTR_ERR(sqd) != -EPERM) | |
8600 | return sqd; | |
8601 | } | |
aa06165d | 8602 | |
534ca6d6 JA |
8603 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
8604 | if (!sqd) | |
8605 | return ERR_PTR(-ENOMEM); | |
8606 | ||
9e138a48 | 8607 | atomic_set(&sqd->park_pending, 0); |
534ca6d6 | 8608 | refcount_set(&sqd->refs, 1); |
69fb2131 | 8609 | INIT_LIST_HEAD(&sqd->ctx_list); |
09a6f4ef | 8610 | mutex_init(&sqd->lock); |
534ca6d6 | 8611 | init_waitqueue_head(&sqd->wait); |
37d1e2e3 | 8612 | init_completion(&sqd->exited); |
534ca6d6 JA |
8613 | return sqd; |
8614 | } | |
8615 | ||
6b06314c | 8616 | #if defined(CONFIG_UNIX) |
6b06314c JA |
8617 | /* |
8618 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
8619 | * the io_uring can be safely unregistered on process exit, even if we have | |
8620 | * loops in the file referencing. | |
8621 | */ | |
8622 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
8623 | { | |
8624 | struct sock *sk = ctx->ring_sock->sk; | |
8625 | struct scm_fp_list *fpl; | |
8626 | struct sk_buff *skb; | |
08a45173 | 8627 | int i, nr_files; |
6b06314c | 8628 | |
6b06314c JA |
8629 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
8630 | if (!fpl) | |
8631 | return -ENOMEM; | |
8632 | ||
8633 | skb = alloc_skb(0, GFP_KERNEL); | |
8634 | if (!skb) { | |
8635 | kfree(fpl); | |
8636 | return -ENOMEM; | |
8637 | } | |
8638 | ||
8639 | skb->sk = sk; | |
6b06314c | 8640 | |
08a45173 | 8641 | nr_files = 0; |
62e398be | 8642 | fpl->user = get_uid(current_user()); |
6b06314c | 8643 | for (i = 0; i < nr; i++) { |
65e19f54 JA |
8644 | struct file *file = io_file_from_index(ctx, i + offset); |
8645 | ||
8646 | if (!file) | |
08a45173 | 8647 | continue; |
65e19f54 | 8648 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
8649 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
8650 | nr_files++; | |
6b06314c JA |
8651 | } |
8652 | ||
08a45173 JA |
8653 | if (nr_files) { |
8654 | fpl->max = SCM_MAX_FD; | |
8655 | fpl->count = nr_files; | |
8656 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 8657 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
8658 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
8659 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 8660 | |
08a45173 JA |
8661 | for (i = 0; i < nr_files; i++) |
8662 | fput(fpl->fp[i]); | |
8663 | } else { | |
8664 | kfree_skb(skb); | |
8665 | kfree(fpl); | |
8666 | } | |
6b06314c JA |
8667 | |
8668 | return 0; | |
8669 | } | |
8670 | ||
8671 | /* | |
8672 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
8673 | * causes regular reference counting to break down. We rely on the UNIX | |
8674 | * garbage collection to take care of this problem for us. | |
8675 | */ | |
8676 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
8677 | { | |
8678 | unsigned left, total; | |
8679 | int ret = 0; | |
8680 | ||
8681 | total = 0; | |
8682 | left = ctx->nr_user_files; | |
8683 | while (left) { | |
8684 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
8685 | |
8686 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
8687 | if (ret) | |
8688 | break; | |
8689 | left -= this_files; | |
8690 | total += this_files; | |
8691 | } | |
8692 | ||
8693 | if (!ret) | |
8694 | return 0; | |
8695 | ||
8696 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
8697 | struct file *file = io_file_from_index(ctx, total); |
8698 | ||
8699 | if (file) | |
8700 | fput(file); | |
6b06314c JA |
8701 | total++; |
8702 | } | |
8703 | ||
8704 | return ret; | |
8705 | } | |
8706 | #else | |
8707 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
8708 | { | |
8709 | return 0; | |
8710 | } | |
8711 | #endif | |
8712 | ||
47e90392 | 8713 | static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
05f3fb3c | 8714 | { |
50238531 | 8715 | struct file *file = prsrc->file; |
05f3fb3c JA |
8716 | #if defined(CONFIG_UNIX) |
8717 | struct sock *sock = ctx->ring_sock->sk; | |
8718 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
8719 | struct sk_buff *skb; | |
8720 | int i; | |
8721 | ||
8722 | __skb_queue_head_init(&list); | |
8723 | ||
8724 | /* | |
8725 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
8726 | * remove this entry and rearrange the file array. | |
8727 | */ | |
8728 | skb = skb_dequeue(head); | |
8729 | while (skb) { | |
8730 | struct scm_fp_list *fp; | |
8731 | ||
8732 | fp = UNIXCB(skb).fp; | |
8733 | for (i = 0; i < fp->count; i++) { | |
8734 | int left; | |
8735 | ||
8736 | if (fp->fp[i] != file) | |
8737 | continue; | |
8738 | ||
8739 | unix_notinflight(fp->user, fp->fp[i]); | |
8740 | left = fp->count - 1 - i; | |
8741 | if (left) { | |
8742 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
8743 | left * sizeof(struct file *)); | |
8744 | } | |
8745 | fp->count--; | |
8746 | if (!fp->count) { | |
8747 | kfree_skb(skb); | |
8748 | skb = NULL; | |
8749 | } else { | |
8750 | __skb_queue_tail(&list, skb); | |
8751 | } | |
8752 | fput(file); | |
8753 | file = NULL; | |
8754 | break; | |
8755 | } | |
8756 | ||
8757 | if (!file) | |
8758 | break; | |
8759 | ||
8760 | __skb_queue_tail(&list, skb); | |
8761 | ||
8762 | skb = skb_dequeue(head); | |
8763 | } | |
8764 | ||
8765 | if (skb_peek(&list)) { | |
8766 | spin_lock_irq(&head->lock); | |
8767 | while ((skb = __skb_dequeue(&list)) != NULL) | |
8768 | __skb_queue_tail(head, skb); | |
8769 | spin_unlock_irq(&head->lock); | |
8770 | } | |
8771 | #else | |
8772 | fput(file); | |
8773 | #endif | |
8774 | } | |
8775 | ||
b895c9a6 | 8776 | static void __io_rsrc_put_work(struct io_rsrc_node *ref_node) |
65e19f54 | 8777 | { |
b895c9a6 | 8778 | struct io_rsrc_data *rsrc_data = ref_node->rsrc_data; |
269bbe5f BM |
8779 | struct io_ring_ctx *ctx = rsrc_data->ctx; |
8780 | struct io_rsrc_put *prsrc, *tmp; | |
05589553 | 8781 | |
269bbe5f BM |
8782 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
8783 | list_del(&prsrc->list); | |
b60c8dce PB |
8784 | |
8785 | if (prsrc->tag) { | |
8786 | bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL; | |
b60c8dce PB |
8787 | |
8788 | io_ring_submit_lock(ctx, lock_ring); | |
79ebeaee | 8789 | spin_lock(&ctx->completion_lock); |
913a571a | 8790 | io_fill_cqe_aux(ctx, prsrc->tag, 0, 0); |
b60c8dce | 8791 | io_commit_cqring(ctx); |
79ebeaee | 8792 | spin_unlock(&ctx->completion_lock); |
b60c8dce PB |
8793 | io_cqring_ev_posted(ctx); |
8794 | io_ring_submit_unlock(ctx, lock_ring); | |
8795 | } | |
8796 | ||
40ae0ff7 | 8797 | rsrc_data->do_put(ctx, prsrc); |
269bbe5f | 8798 | kfree(prsrc); |
65e19f54 | 8799 | } |
05589553 | 8800 | |
28a9fe25 | 8801 | io_rsrc_node_destroy(ref_node); |
3e942498 PB |
8802 | if (atomic_dec_and_test(&rsrc_data->refs)) |
8803 | complete(&rsrc_data->done); | |
2faf852d | 8804 | } |
65e19f54 | 8805 | |
269bbe5f | 8806 | static void io_rsrc_put_work(struct work_struct *work) |
4a38aed2 JA |
8807 | { |
8808 | struct io_ring_ctx *ctx; | |
8809 | struct llist_node *node; | |
8810 | ||
269bbe5f BM |
8811 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
8812 | node = llist_del_all(&ctx->rsrc_put_llist); | |
4a38aed2 JA |
8813 | |
8814 | while (node) { | |
b895c9a6 | 8815 | struct io_rsrc_node *ref_node; |
4a38aed2 JA |
8816 | struct llist_node *next = node->next; |
8817 | ||
b895c9a6 | 8818 | ref_node = llist_entry(node, struct io_rsrc_node, llist); |
269bbe5f | 8819 | __io_rsrc_put_work(ref_node); |
4a38aed2 JA |
8820 | node = next; |
8821 | } | |
8822 | } | |
8823 | ||
6b06314c | 8824 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
792e3582 | 8825 | unsigned nr_args, u64 __user *tags) |
6b06314c JA |
8826 | { |
8827 | __s32 __user *fds = (__s32 __user *) arg; | |
05f3fb3c | 8828 | struct file *file; |
f3baed39 | 8829 | int fd, ret; |
846a4ef2 | 8830 | unsigned i; |
6b06314c | 8831 | |
05f3fb3c | 8832 | if (ctx->file_data) |
6b06314c JA |
8833 | return -EBUSY; |
8834 | if (!nr_args) | |
8835 | return -EINVAL; | |
8836 | if (nr_args > IORING_MAX_FIXED_FILES) | |
8837 | return -EMFILE; | |
3a1b8a4e PB |
8838 | if (nr_args > rlimit(RLIMIT_NOFILE)) |
8839 | return -EMFILE; | |
a7f0ed5a | 8840 | ret = io_rsrc_node_switch_start(ctx); |
f3baed39 PB |
8841 | if (ret) |
8842 | return ret; | |
d878c816 PB |
8843 | ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args, |
8844 | &ctx->file_data); | |
8845 | if (ret) | |
8846 | return ret; | |
6b06314c | 8847 | |
f3baed39 | 8848 | ret = -ENOMEM; |
aeca241b | 8849 | if (!io_alloc_file_tables(&ctx->file_table, nr_args)) |
1ad555c6 | 8850 | goto out_free; |
65e19f54 | 8851 | |
08a45173 | 8852 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
d878c816 | 8853 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
600cf3f8 PB |
8854 | ret = -EFAULT; |
8855 | goto out_fput; | |
8856 | } | |
08a45173 | 8857 | /* allow sparse sets */ |
792e3582 PB |
8858 | if (fd == -1) { |
8859 | ret = -EINVAL; | |
2d091d62 | 8860 | if (unlikely(*io_get_tag_slot(ctx->file_data, i))) |
792e3582 | 8861 | goto out_fput; |
08a45173 | 8862 | continue; |
792e3582 | 8863 | } |
6b06314c | 8864 | |
05f3fb3c | 8865 | file = fget(fd); |
6b06314c | 8866 | ret = -EBADF; |
792e3582 | 8867 | if (unlikely(!file)) |
600cf3f8 | 8868 | goto out_fput; |
05f3fb3c | 8869 | |
6b06314c JA |
8870 | /* |
8871 | * Don't allow io_uring instances to be registered. If UNIX | |
8872 | * isn't enabled, then this causes a reference cycle and this | |
8873 | * instance can never get freed. If UNIX is enabled we'll | |
8874 | * handle it just fine, but there's still no point in allowing | |
8875 | * a ring fd as it doesn't support regular read/write anyway. | |
8876 | */ | |
05f3fb3c JA |
8877 | if (file->f_op == &io_uring_fops) { |
8878 | fput(file); | |
600cf3f8 | 8879 | goto out_fput; |
6b06314c | 8880 | } |
aeca241b | 8881 | io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file); |
6b06314c JA |
8882 | } |
8883 | ||
6b06314c | 8884 | ret = io_sqe_files_scm(ctx); |
05589553 | 8885 | if (ret) { |
08480400 | 8886 | __io_sqe_files_unregister(ctx); |
05589553 XW |
8887 | return ret; |
8888 | } | |
6b06314c | 8889 | |
a7f0ed5a | 8890 | io_rsrc_node_switch(ctx, NULL); |
6b06314c | 8891 | return ret; |
600cf3f8 PB |
8892 | out_fput: |
8893 | for (i = 0; i < ctx->nr_user_files; i++) { | |
8894 | file = io_file_from_index(ctx, i); | |
8895 | if (file) | |
8896 | fput(file); | |
8897 | } | |
042b0d85 | 8898 | io_free_file_tables(&ctx->file_table); |
600cf3f8 | 8899 | ctx->nr_user_files = 0; |
600cf3f8 | 8900 | out_free: |
44b31f2f | 8901 | io_rsrc_data_free(ctx->file_data); |
55cbc256 | 8902 | ctx->file_data = NULL; |
6b06314c JA |
8903 | return ret; |
8904 | } | |
8905 | ||
c3a31e60 JA |
8906 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
8907 | int index) | |
8908 | { | |
8909 | #if defined(CONFIG_UNIX) | |
8910 | struct sock *sock = ctx->ring_sock->sk; | |
8911 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
8912 | struct sk_buff *skb; | |
8913 | ||
8914 | /* | |
8915 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
8916 | * file set. If there's no room, fall back to allocating a new skb | |
8917 | * and filling it in. | |
8918 | */ | |
8919 | spin_lock_irq(&head->lock); | |
8920 | skb = skb_peek(head); | |
8921 | if (skb) { | |
8922 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
8923 | ||
8924 | if (fpl->count < SCM_MAX_FD) { | |
8925 | __skb_unlink(skb, head); | |
8926 | spin_unlock_irq(&head->lock); | |
8927 | fpl->fp[fpl->count] = get_file(file); | |
8928 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
8929 | fpl->count++; | |
8930 | spin_lock_irq(&head->lock); | |
8931 | __skb_queue_head(head, skb); | |
8932 | } else { | |
8933 | skb = NULL; | |
8934 | } | |
8935 | } | |
8936 | spin_unlock_irq(&head->lock); | |
8937 | ||
8938 | if (skb) { | |
8939 | fput(file); | |
8940 | return 0; | |
8941 | } | |
8942 | ||
8943 | return __io_sqe_files_scm(ctx, 1, index); | |
8944 | #else | |
8945 | return 0; | |
8946 | #endif | |
8947 | } | |
8948 | ||
9c7b0ba8 PB |
8949 | static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, |
8950 | struct io_rsrc_node *node, void *rsrc) | |
8951 | { | |
8952 | struct io_rsrc_put *prsrc; | |
8953 | ||
8954 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); | |
8955 | if (!prsrc) | |
8956 | return -ENOMEM; | |
8957 | ||
8958 | prsrc->tag = *io_get_tag_slot(data, idx); | |
8959 | prsrc->rsrc = rsrc; | |
8960 | list_add(&prsrc->list, &node->rsrc_list); | |
8961 | return 0; | |
8962 | } | |
8963 | ||
b9445598 PB |
8964 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
8965 | unsigned int issue_flags, u32 slot_index) | |
8966 | { | |
8967 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 8968 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
9c7b0ba8 | 8969 | bool needs_switch = false; |
b9445598 PB |
8970 | struct io_fixed_file *file_slot; |
8971 | int ret = -EBADF; | |
8972 | ||
3b44b371 | 8973 | io_ring_submit_lock(ctx, needs_lock); |
b9445598 PB |
8974 | if (file->f_op == &io_uring_fops) |
8975 | goto err; | |
8976 | ret = -ENXIO; | |
8977 | if (!ctx->file_data) | |
8978 | goto err; | |
8979 | ret = -EINVAL; | |
8980 | if (slot_index >= ctx->nr_user_files) | |
8981 | goto err; | |
8982 | ||
8983 | slot_index = array_index_nospec(slot_index, ctx->nr_user_files); | |
8984 | file_slot = io_fixed_file_slot(&ctx->file_table, slot_index); | |
9c7b0ba8 PB |
8985 | |
8986 | if (file_slot->file_ptr) { | |
8987 | struct file *old_file; | |
8988 | ||
8989 | ret = io_rsrc_node_switch_start(ctx); | |
8990 | if (ret) | |
8991 | goto err; | |
8992 | ||
8993 | old_file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
8994 | ret = io_queue_rsrc_removal(ctx->file_data, slot_index, | |
8995 | ctx->rsrc_node, old_file); | |
8996 | if (ret) | |
8997 | goto err; | |
8998 | file_slot->file_ptr = 0; | |
8999 | needs_switch = true; | |
9000 | } | |
b9445598 PB |
9001 | |
9002 | *io_get_tag_slot(ctx->file_data, slot_index) = 0; | |
9003 | io_fixed_file_set(file_slot, file); | |
9004 | ret = io_sqe_file_register(ctx, file, slot_index); | |
9005 | if (ret) { | |
9006 | file_slot->file_ptr = 0; | |
9007 | goto err; | |
9008 | } | |
9009 | ||
9010 | ret = 0; | |
9011 | err: | |
9c7b0ba8 PB |
9012 | if (needs_switch) |
9013 | io_rsrc_node_switch(ctx, ctx->file_data); | |
3b44b371 | 9014 | io_ring_submit_unlock(ctx, needs_lock); |
b9445598 PB |
9015 | if (ret) |
9016 | fput(file); | |
9017 | return ret; | |
9018 | } | |
9019 | ||
7df778be PB |
9020 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags) |
9021 | { | |
9022 | unsigned int offset = req->close.file_slot - 1; | |
9023 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 9024 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
7df778be PB |
9025 | struct io_fixed_file *file_slot; |
9026 | struct file *file; | |
9027 | int ret, i; | |
9028 | ||
3b44b371 | 9029 | io_ring_submit_lock(ctx, needs_lock); |
7df778be PB |
9030 | ret = -ENXIO; |
9031 | if (unlikely(!ctx->file_data)) | |
9032 | goto out; | |
9033 | ret = -EINVAL; | |
9034 | if (offset >= ctx->nr_user_files) | |
9035 | goto out; | |
9036 | ret = io_rsrc_node_switch_start(ctx); | |
9037 | if (ret) | |
9038 | goto out; | |
9039 | ||
9040 | i = array_index_nospec(offset, ctx->nr_user_files); | |
9041 | file_slot = io_fixed_file_slot(&ctx->file_table, i); | |
9042 | ret = -EBADF; | |
9043 | if (!file_slot->file_ptr) | |
9044 | goto out; | |
9045 | ||
9046 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
9047 | ret = io_queue_rsrc_removal(ctx->file_data, offset, ctx->rsrc_node, file); | |
9048 | if (ret) | |
9049 | goto out; | |
9050 | ||
9051 | file_slot->file_ptr = 0; | |
9052 | io_rsrc_node_switch(ctx, ctx->file_data); | |
9053 | ret = 0; | |
9054 | out: | |
3b44b371 | 9055 | io_ring_submit_unlock(ctx, needs_lock); |
7df778be PB |
9056 | return ret; |
9057 | } | |
9058 | ||
05f3fb3c | 9059 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
c3bdad02 | 9060 | struct io_uring_rsrc_update2 *up, |
05f3fb3c JA |
9061 | unsigned nr_args) |
9062 | { | |
c3bdad02 | 9063 | u64 __user *tags = u64_to_user_ptr(up->tags); |
98f0b3b4 | 9064 | __s32 __user *fds = u64_to_user_ptr(up->data); |
b895c9a6 | 9065 | struct io_rsrc_data *data = ctx->file_data; |
a04b0ac0 PB |
9066 | struct io_fixed_file *file_slot; |
9067 | struct file *file; | |
98f0b3b4 PB |
9068 | int fd, i, err = 0; |
9069 | unsigned int done; | |
05589553 | 9070 | bool needs_switch = false; |
c3a31e60 | 9071 | |
98f0b3b4 PB |
9072 | if (!ctx->file_data) |
9073 | return -ENXIO; | |
9074 | if (up->offset + nr_args > ctx->nr_user_files) | |
c3a31e60 JA |
9075 | return -EINVAL; |
9076 | ||
67973b93 | 9077 | for (done = 0; done < nr_args; done++) { |
c3bdad02 PB |
9078 | u64 tag = 0; |
9079 | ||
9080 | if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) || | |
9081 | copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
c3a31e60 JA |
9082 | err = -EFAULT; |
9083 | break; | |
9084 | } | |
c3bdad02 PB |
9085 | if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) { |
9086 | err = -EINVAL; | |
9087 | break; | |
9088 | } | |
4e0377a1 | 9089 | if (fd == IORING_REGISTER_FILES_SKIP) |
9090 | continue; | |
9091 | ||
67973b93 | 9092 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
aeca241b | 9093 | file_slot = io_fixed_file_slot(&ctx->file_table, i); |
ea64ec02 | 9094 | |
a04b0ac0 PB |
9095 | if (file_slot->file_ptr) { |
9096 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
b60c8dce PB |
9097 | err = io_queue_rsrc_removal(data, up->offset + done, |
9098 | ctx->rsrc_node, file); | |
a5318d3c HD |
9099 | if (err) |
9100 | break; | |
a04b0ac0 | 9101 | file_slot->file_ptr = 0; |
05589553 | 9102 | needs_switch = true; |
c3a31e60 JA |
9103 | } |
9104 | if (fd != -1) { | |
c3a31e60 JA |
9105 | file = fget(fd); |
9106 | if (!file) { | |
9107 | err = -EBADF; | |
9108 | break; | |
9109 | } | |
9110 | /* | |
9111 | * Don't allow io_uring instances to be registered. If | |
9112 | * UNIX isn't enabled, then this causes a reference | |
9113 | * cycle and this instance can never get freed. If UNIX | |
9114 | * is enabled we'll handle it just fine, but there's | |
9115 | * still no point in allowing a ring fd as it doesn't | |
9116 | * support regular read/write anyway. | |
9117 | */ | |
9118 | if (file->f_op == &io_uring_fops) { | |
9119 | fput(file); | |
9120 | err = -EBADF; | |
9121 | break; | |
9122 | } | |
2d091d62 | 9123 | *io_get_tag_slot(data, up->offset + done) = tag; |
9a321c98 | 9124 | io_fixed_file_set(file_slot, file); |
c3a31e60 | 9125 | err = io_sqe_file_register(ctx, file, i); |
f3bd9dae | 9126 | if (err) { |
a04b0ac0 | 9127 | file_slot->file_ptr = 0; |
f3bd9dae | 9128 | fput(file); |
c3a31e60 | 9129 | break; |
f3bd9dae | 9130 | } |
c3a31e60 | 9131 | } |
05f3fb3c JA |
9132 | } |
9133 | ||
a7f0ed5a PB |
9134 | if (needs_switch) |
9135 | io_rsrc_node_switch(ctx, data); | |
c3a31e60 JA |
9136 | return done ? done : err; |
9137 | } | |
05589553 | 9138 | |
685fe7fe JA |
9139 | static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx, |
9140 | struct task_struct *task) | |
24369c2e | 9141 | { |
e941894e | 9142 | struct io_wq_hash *hash; |
24369c2e | 9143 | struct io_wq_data data; |
24369c2e | 9144 | unsigned int concurrency; |
24369c2e | 9145 | |
362a9e65 | 9146 | mutex_lock(&ctx->uring_lock); |
e941894e JA |
9147 | hash = ctx->hash_map; |
9148 | if (!hash) { | |
9149 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); | |
362a9e65 YY |
9150 | if (!hash) { |
9151 | mutex_unlock(&ctx->uring_lock); | |
e941894e | 9152 | return ERR_PTR(-ENOMEM); |
362a9e65 | 9153 | } |
e941894e JA |
9154 | refcount_set(&hash->refs, 1); |
9155 | init_waitqueue_head(&hash->wait); | |
9156 | ctx->hash_map = hash; | |
24369c2e | 9157 | } |
362a9e65 | 9158 | mutex_unlock(&ctx->uring_lock); |
24369c2e | 9159 | |
e941894e | 9160 | data.hash = hash; |
685fe7fe | 9161 | data.task = task; |
ebc11b6c | 9162 | data.free_work = io_wq_free_work; |
f5fa38c5 | 9163 | data.do_work = io_wq_submit_work; |
24369c2e | 9164 | |
d25e3a3d JA |
9165 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
9166 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
24369c2e | 9167 | |
5aa75ed5 | 9168 | return io_wq_create(concurrency, &data); |
24369c2e PB |
9169 | } |
9170 | ||
c072481d PB |
9171 | static __cold int io_uring_alloc_task_context(struct task_struct *task, |
9172 | struct io_ring_ctx *ctx) | |
0f212204 JA |
9173 | { |
9174 | struct io_uring_task *tctx; | |
d8a6df10 | 9175 | int ret; |
0f212204 | 9176 | |
09899b19 | 9177 | tctx = kzalloc(sizeof(*tctx), GFP_KERNEL); |
0f212204 JA |
9178 | if (unlikely(!tctx)) |
9179 | return -ENOMEM; | |
9180 | ||
e7a6c00d JA |
9181 | tctx->registered_rings = kcalloc(IO_RINGFD_REG_MAX, |
9182 | sizeof(struct file *), GFP_KERNEL); | |
9183 | if (unlikely(!tctx->registered_rings)) { | |
9184 | kfree(tctx); | |
9185 | return -ENOMEM; | |
9186 | } | |
9187 | ||
d8a6df10 JA |
9188 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
9189 | if (unlikely(ret)) { | |
e7a6c00d | 9190 | kfree(tctx->registered_rings); |
d8a6df10 JA |
9191 | kfree(tctx); |
9192 | return ret; | |
9193 | } | |
9194 | ||
685fe7fe | 9195 | tctx->io_wq = io_init_wq_offload(ctx, task); |
5aa75ed5 JA |
9196 | if (IS_ERR(tctx->io_wq)) { |
9197 | ret = PTR_ERR(tctx->io_wq); | |
9198 | percpu_counter_destroy(&tctx->inflight); | |
e7a6c00d | 9199 | kfree(tctx->registered_rings); |
5aa75ed5 JA |
9200 | kfree(tctx); |
9201 | return ret; | |
9202 | } | |
9203 | ||
0f212204 JA |
9204 | xa_init(&tctx->xa); |
9205 | init_waitqueue_head(&tctx->wait); | |
fdaf083c | 9206 | atomic_set(&tctx->in_idle, 0); |
b303fe2e | 9207 | atomic_set(&tctx->inflight_tracked, 0); |
0f212204 | 9208 | task->io_uring = tctx; |
7cbf1722 JA |
9209 | spin_lock_init(&tctx->task_lock); |
9210 | INIT_WQ_LIST(&tctx->task_list); | |
4813c377 | 9211 | INIT_WQ_LIST(&tctx->prior_task_list); |
7cbf1722 | 9212 | init_task_work(&tctx->task_work, tctx_task_work); |
0f212204 JA |
9213 | return 0; |
9214 | } | |
9215 | ||
9216 | void __io_uring_free(struct task_struct *tsk) | |
9217 | { | |
9218 | struct io_uring_task *tctx = tsk->io_uring; | |
9219 | ||
9220 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); | |
ef8eaa4e | 9221 | WARN_ON_ONCE(tctx->io_wq); |
09899b19 | 9222 | WARN_ON_ONCE(tctx->cached_refs); |
ef8eaa4e | 9223 | |
e7a6c00d | 9224 | kfree(tctx->registered_rings); |
d8a6df10 | 9225 | percpu_counter_destroy(&tctx->inflight); |
0f212204 JA |
9226 | kfree(tctx); |
9227 | tsk->io_uring = NULL; | |
9228 | } | |
9229 | ||
c072481d PB |
9230 | static __cold int io_sq_offload_create(struct io_ring_ctx *ctx, |
9231 | struct io_uring_params *p) | |
2b188cc1 JA |
9232 | { |
9233 | int ret; | |
9234 | ||
d25e3a3d JA |
9235 | /* Retain compatibility with failing for an invalid attach attempt */ |
9236 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == | |
9237 | IORING_SETUP_ATTACH_WQ) { | |
9238 | struct fd f; | |
9239 | ||
9240 | f = fdget(p->wq_fd); | |
9241 | if (!f.file) | |
9242 | return -ENXIO; | |
0cc936f7 JA |
9243 | if (f.file->f_op != &io_uring_fops) { |
9244 | fdput(f); | |
f2a48dd0 | 9245 | return -EINVAL; |
0cc936f7 JA |
9246 | } |
9247 | fdput(f); | |
d25e3a3d | 9248 | } |
6c271ce2 | 9249 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
46fe18b1 | 9250 | struct task_struct *tsk; |
534ca6d6 | 9251 | struct io_sq_data *sqd; |
26984fbf | 9252 | bool attached; |
534ca6d6 | 9253 | |
cdc1404a PM |
9254 | ret = security_uring_sqpoll(); |
9255 | if (ret) | |
9256 | return ret; | |
9257 | ||
26984fbf | 9258 | sqd = io_get_sq_data(p, &attached); |
534ca6d6 JA |
9259 | if (IS_ERR(sqd)) { |
9260 | ret = PTR_ERR(sqd); | |
9261 | goto err; | |
9262 | } | |
69fb2131 | 9263 | |
7c30f36a | 9264 | ctx->sq_creds = get_current_cred(); |
534ca6d6 | 9265 | ctx->sq_data = sqd; |
917257da JA |
9266 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
9267 | if (!ctx->sq_thread_idle) | |
9268 | ctx->sq_thread_idle = HZ; | |
9269 | ||
78d7f6ba | 9270 | io_sq_thread_park(sqd); |
de75a3d3 PB |
9271 | list_add(&ctx->sqd_list, &sqd->ctx_list); |
9272 | io_sqd_update_thread_idle(sqd); | |
26984fbf | 9273 | /* don't attach to a dying SQPOLL thread, would be racy */ |
f2a48dd0 | 9274 | ret = (attached && !sqd->thread) ? -ENXIO : 0; |
78d7f6ba PB |
9275 | io_sq_thread_unpark(sqd); |
9276 | ||
de75a3d3 PB |
9277 | if (ret < 0) |
9278 | goto err; | |
9279 | if (attached) | |
5aa75ed5 | 9280 | return 0; |
aa06165d | 9281 | |
6c271ce2 | 9282 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 9283 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 9284 | |
917257da | 9285 | ret = -EINVAL; |
f2a48dd0 | 9286 | if (cpu >= nr_cpu_ids || !cpu_online(cpu)) |
e8f98f24 | 9287 | goto err_sqpoll; |
37d1e2e3 | 9288 | sqd->sq_cpu = cpu; |
6c271ce2 | 9289 | } else { |
37d1e2e3 | 9290 | sqd->sq_cpu = -1; |
6c271ce2 | 9291 | } |
37d1e2e3 JA |
9292 | |
9293 | sqd->task_pid = current->pid; | |
5c2469e0 | 9294 | sqd->task_tgid = current->tgid; |
46fe18b1 JA |
9295 | tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE); |
9296 | if (IS_ERR(tsk)) { | |
9297 | ret = PTR_ERR(tsk); | |
e8f98f24 | 9298 | goto err_sqpoll; |
6c271ce2 | 9299 | } |
97a73a0f | 9300 | |
46fe18b1 | 9301 | sqd->thread = tsk; |
97a73a0f | 9302 | ret = io_uring_alloc_task_context(tsk, ctx); |
46fe18b1 | 9303 | wake_up_new_task(tsk); |
0f212204 JA |
9304 | if (ret) |
9305 | goto err; | |
6c271ce2 JA |
9306 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
9307 | /* Can't have SQ_AFF without SQPOLL */ | |
9308 | ret = -EINVAL; | |
9309 | goto err; | |
9310 | } | |
9311 | ||
2b188cc1 | 9312 | return 0; |
f2a48dd0 PB |
9313 | err_sqpoll: |
9314 | complete(&ctx->sq_data->exited); | |
2b188cc1 | 9315 | err: |
37d1e2e3 | 9316 | io_sq_thread_finish(ctx); |
2b188cc1 JA |
9317 | return ret; |
9318 | } | |
9319 | ||
a087e2b5 BM |
9320 | static inline void __io_unaccount_mem(struct user_struct *user, |
9321 | unsigned long nr_pages) | |
2b188cc1 JA |
9322 | { |
9323 | atomic_long_sub(nr_pages, &user->locked_vm); | |
9324 | } | |
9325 | ||
a087e2b5 BM |
9326 | static inline int __io_account_mem(struct user_struct *user, |
9327 | unsigned long nr_pages) | |
2b188cc1 JA |
9328 | { |
9329 | unsigned long page_limit, cur_pages, new_pages; | |
9330 | ||
9331 | /* Don't allow more pages than we can safely lock */ | |
9332 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
9333 | ||
9334 | do { | |
9335 | cur_pages = atomic_long_read(&user->locked_vm); | |
9336 | new_pages = cur_pages + nr_pages; | |
9337 | if (new_pages > page_limit) | |
9338 | return -ENOMEM; | |
9339 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
9340 | new_pages) != cur_pages); | |
9341 | ||
9342 | return 0; | |
9343 | } | |
9344 | ||
26bfa89e | 9345 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 9346 | { |
62e398be | 9347 | if (ctx->user) |
a087e2b5 | 9348 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 9349 | |
26bfa89e JA |
9350 | if (ctx->mm_account) |
9351 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
9352 | } |
9353 | ||
26bfa89e | 9354 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 9355 | { |
30975825 BM |
9356 | int ret; |
9357 | ||
62e398be | 9358 | if (ctx->user) { |
30975825 BM |
9359 | ret = __io_account_mem(ctx->user, nr_pages); |
9360 | if (ret) | |
9361 | return ret; | |
9362 | } | |
9363 | ||
26bfa89e JA |
9364 | if (ctx->mm_account) |
9365 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
9366 | |
9367 | return 0; | |
9368 | } | |
9369 | ||
2b188cc1 JA |
9370 | static void io_mem_free(void *ptr) |
9371 | { | |
52e04ef4 MR |
9372 | struct page *page; |
9373 | ||
9374 | if (!ptr) | |
9375 | return; | |
2b188cc1 | 9376 | |
52e04ef4 | 9377 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
9378 | if (put_page_testzero(page)) |
9379 | free_compound_page(page); | |
9380 | } | |
9381 | ||
9382 | static void *io_mem_alloc(size_t size) | |
9383 | { | |
0a3f1e0b | 9384 | gfp_t gfp = GFP_KERNEL_ACCOUNT | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP; |
2b188cc1 | 9385 | |
0a3f1e0b | 9386 | return (void *) __get_free_pages(gfp, get_order(size)); |
2b188cc1 JA |
9387 | } |
9388 | ||
75b28aff HV |
9389 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
9390 | size_t *sq_offset) | |
9391 | { | |
9392 | struct io_rings *rings; | |
9393 | size_t off, sq_array_size; | |
9394 | ||
9395 | off = struct_size(rings, cqes, cq_entries); | |
9396 | if (off == SIZE_MAX) | |
9397 | return SIZE_MAX; | |
9398 | ||
9399 | #ifdef CONFIG_SMP | |
9400 | off = ALIGN(off, SMP_CACHE_BYTES); | |
9401 | if (off == 0) | |
9402 | return SIZE_MAX; | |
9403 | #endif | |
9404 | ||
b36200f5 DV |
9405 | if (sq_offset) |
9406 | *sq_offset = off; | |
9407 | ||
75b28aff HV |
9408 | sq_array_size = array_size(sizeof(u32), sq_entries); |
9409 | if (sq_array_size == SIZE_MAX) | |
9410 | return SIZE_MAX; | |
9411 | ||
9412 | if (check_add_overflow(off, sq_array_size, &off)) | |
9413 | return SIZE_MAX; | |
9414 | ||
75b28aff HV |
9415 | return off; |
9416 | } | |
9417 | ||
41edf1a5 | 9418 | static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot) |
7f61a1e9 | 9419 | { |
41edf1a5 | 9420 | struct io_mapped_ubuf *imu = *slot; |
7f61a1e9 PB |
9421 | unsigned int i; |
9422 | ||
6224843d PB |
9423 | if (imu != ctx->dummy_ubuf) { |
9424 | for (i = 0; i < imu->nr_bvecs; i++) | |
9425 | unpin_user_page(imu->bvec[i].bv_page); | |
9426 | if (imu->acct_pages) | |
9427 | io_unaccount_mem(ctx, imu->acct_pages); | |
9428 | kvfree(imu); | |
9429 | } | |
41edf1a5 | 9430 | *slot = NULL; |
7f61a1e9 PB |
9431 | } |
9432 | ||
bd54b6fe | 9433 | static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
edafccee | 9434 | { |
634d00df PB |
9435 | io_buffer_unmap(ctx, &prsrc->buf); |
9436 | prsrc->buf = NULL; | |
bd54b6fe | 9437 | } |
edafccee | 9438 | |
bd54b6fe BM |
9439 | static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
9440 | { | |
9441 | unsigned int i; | |
edafccee | 9442 | |
7f61a1e9 PB |
9443 | for (i = 0; i < ctx->nr_user_bufs; i++) |
9444 | io_buffer_unmap(ctx, &ctx->user_bufs[i]); | |
edafccee | 9445 | kfree(ctx->user_bufs); |
bb6659cc | 9446 | io_rsrc_data_free(ctx->buf_data); |
edafccee | 9447 | ctx->user_bufs = NULL; |
bd54b6fe | 9448 | ctx->buf_data = NULL; |
edafccee | 9449 | ctx->nr_user_bufs = 0; |
bd54b6fe BM |
9450 | } |
9451 | ||
0a96bbe4 | 9452 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
edafccee | 9453 | { |
bd54b6fe | 9454 | int ret; |
edafccee | 9455 | |
bd54b6fe | 9456 | if (!ctx->buf_data) |
edafccee JA |
9457 | return -ENXIO; |
9458 | ||
bd54b6fe BM |
9459 | ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx); |
9460 | if (!ret) | |
9461 | __io_sqe_buffers_unregister(ctx); | |
9462 | return ret; | |
edafccee JA |
9463 | } |
9464 | ||
9465 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
9466 | void __user *arg, unsigned index) | |
9467 | { | |
9468 | struct iovec __user *src; | |
9469 | ||
9470 | #ifdef CONFIG_COMPAT | |
9471 | if (ctx->compat) { | |
9472 | struct compat_iovec __user *ciovs; | |
9473 | struct compat_iovec ciov; | |
9474 | ||
9475 | ciovs = (struct compat_iovec __user *) arg; | |
9476 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
9477 | return -EFAULT; | |
9478 | ||
d55e5f5b | 9479 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
9480 | dst->iov_len = ciov.iov_len; |
9481 | return 0; | |
9482 | } | |
9483 | #endif | |
9484 | src = (struct iovec __user *) arg; | |
9485 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
9486 | return -EFAULT; | |
9487 | return 0; | |
9488 | } | |
9489 | ||
de293938 JA |
9490 | /* |
9491 | * Not super efficient, but this is just a registration time. And we do cache | |
9492 | * the last compound head, so generally we'll only do a full search if we don't | |
9493 | * match that one. | |
9494 | * | |
9495 | * We check if the given compound head page has already been accounted, to | |
9496 | * avoid double accounting it. This allows us to account the full size of the | |
9497 | * page, not just the constituent pages of a huge page. | |
9498 | */ | |
9499 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, | |
9500 | int nr_pages, struct page *hpage) | |
9501 | { | |
9502 | int i, j; | |
9503 | ||
9504 | /* check current page array */ | |
9505 | for (i = 0; i < nr_pages; i++) { | |
9506 | if (!PageCompound(pages[i])) | |
9507 | continue; | |
9508 | if (compound_head(pages[i]) == hpage) | |
9509 | return true; | |
9510 | } | |
9511 | ||
9512 | /* check previously registered pages */ | |
9513 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
41edf1a5 | 9514 | struct io_mapped_ubuf *imu = ctx->user_bufs[i]; |
de293938 JA |
9515 | |
9516 | for (j = 0; j < imu->nr_bvecs; j++) { | |
9517 | if (!PageCompound(imu->bvec[j].bv_page)) | |
9518 | continue; | |
9519 | if (compound_head(imu->bvec[j].bv_page) == hpage) | |
9520 | return true; | |
9521 | } | |
9522 | } | |
9523 | ||
9524 | return false; | |
9525 | } | |
9526 | ||
9527 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, | |
9528 | int nr_pages, struct io_mapped_ubuf *imu, | |
9529 | struct page **last_hpage) | |
9530 | { | |
9531 | int i, ret; | |
9532 | ||
216e5835 | 9533 | imu->acct_pages = 0; |
de293938 JA |
9534 | for (i = 0; i < nr_pages; i++) { |
9535 | if (!PageCompound(pages[i])) { | |
9536 | imu->acct_pages++; | |
9537 | } else { | |
9538 | struct page *hpage; | |
9539 | ||
9540 | hpage = compound_head(pages[i]); | |
9541 | if (hpage == *last_hpage) | |
9542 | continue; | |
9543 | *last_hpage = hpage; | |
9544 | if (headpage_already_acct(ctx, pages, i, hpage)) | |
9545 | continue; | |
9546 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; | |
9547 | } | |
9548 | } | |
9549 | ||
9550 | if (!imu->acct_pages) | |
9551 | return 0; | |
9552 | ||
26bfa89e | 9553 | ret = io_account_mem(ctx, imu->acct_pages); |
de293938 JA |
9554 | if (ret) |
9555 | imu->acct_pages = 0; | |
9556 | return ret; | |
9557 | } | |
9558 | ||
0a96bbe4 | 9559 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
41edf1a5 | 9560 | struct io_mapped_ubuf **pimu, |
0a96bbe4 | 9561 | struct page **last_hpage) |
edafccee | 9562 | { |
41edf1a5 | 9563 | struct io_mapped_ubuf *imu = NULL; |
edafccee JA |
9564 | struct vm_area_struct **vmas = NULL; |
9565 | struct page **pages = NULL; | |
0a96bbe4 BM |
9566 | unsigned long off, start, end, ubuf; |
9567 | size_t size; | |
9568 | int ret, pret, nr_pages, i; | |
9569 | ||
6224843d PB |
9570 | if (!iov->iov_base) { |
9571 | *pimu = ctx->dummy_ubuf; | |
9572 | return 0; | |
9573 | } | |
9574 | ||
0a96bbe4 BM |
9575 | ubuf = (unsigned long) iov->iov_base; |
9576 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
9577 | start = ubuf >> PAGE_SHIFT; | |
9578 | nr_pages = end - start; | |
9579 | ||
41edf1a5 | 9580 | *pimu = NULL; |
0a96bbe4 BM |
9581 | ret = -ENOMEM; |
9582 | ||
9583 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); | |
9584 | if (!pages) | |
9585 | goto done; | |
9586 | ||
9587 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), | |
9588 | GFP_KERNEL); | |
9589 | if (!vmas) | |
9590 | goto done; | |
edafccee | 9591 | |
41edf1a5 | 9592 | imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL); |
a2b4198c | 9593 | if (!imu) |
0a96bbe4 BM |
9594 | goto done; |
9595 | ||
9596 | ret = 0; | |
9597 | mmap_read_lock(current->mm); | |
9598 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, | |
9599 | pages, vmas); | |
9600 | if (pret == nr_pages) { | |
9601 | /* don't support file backed memory */ | |
9602 | for (i = 0; i < nr_pages; i++) { | |
9603 | struct vm_area_struct *vma = vmas[i]; | |
9604 | ||
40dad765 PB |
9605 | if (vma_is_shmem(vma)) |
9606 | continue; | |
0a96bbe4 BM |
9607 | if (vma->vm_file && |
9608 | !is_file_hugepages(vma->vm_file)) { | |
9609 | ret = -EOPNOTSUPP; | |
9610 | break; | |
9611 | } | |
9612 | } | |
9613 | } else { | |
9614 | ret = pret < 0 ? pret : -EFAULT; | |
9615 | } | |
9616 | mmap_read_unlock(current->mm); | |
9617 | if (ret) { | |
9618 | /* | |
9619 | * if we did partial map, or found file backed vmas, | |
9620 | * release any pages we did get | |
9621 | */ | |
9622 | if (pret > 0) | |
9623 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
9624 | goto done; |
9625 | } | |
9626 | ||
9627 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); | |
9628 | if (ret) { | |
9629 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
9630 | goto done; |
9631 | } | |
9632 | ||
9633 | off = ubuf & ~PAGE_MASK; | |
9634 | size = iov->iov_len; | |
9635 | for (i = 0; i < nr_pages; i++) { | |
9636 | size_t vec_len; | |
9637 | ||
9638 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
9639 | imu->bvec[i].bv_page = pages[i]; | |
9640 | imu->bvec[i].bv_len = vec_len; | |
9641 | imu->bvec[i].bv_offset = off; | |
9642 | off = 0; | |
9643 | size -= vec_len; | |
9644 | } | |
9645 | /* store original address for later verification */ | |
9646 | imu->ubuf = ubuf; | |
4751f53d | 9647 | imu->ubuf_end = ubuf + iov->iov_len; |
0a96bbe4 | 9648 | imu->nr_bvecs = nr_pages; |
41edf1a5 | 9649 | *pimu = imu; |
0a96bbe4 BM |
9650 | ret = 0; |
9651 | done: | |
41edf1a5 PB |
9652 | if (ret) |
9653 | kvfree(imu); | |
0a96bbe4 BM |
9654 | kvfree(pages); |
9655 | kvfree(vmas); | |
9656 | return ret; | |
9657 | } | |
9658 | ||
2b358604 | 9659 | static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args) |
0a96bbe4 | 9660 | { |
87094465 PB |
9661 | ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL); |
9662 | return ctx->user_bufs ? 0 : -ENOMEM; | |
2b358604 | 9663 | } |
edafccee | 9664 | |
2b358604 BM |
9665 | static int io_buffer_validate(struct iovec *iov) |
9666 | { | |
50e96989 PB |
9667 | unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1); |
9668 | ||
2b358604 BM |
9669 | /* |
9670 | * Don't impose further limits on the size and buffer | |
9671 | * constraints here, we'll -EINVAL later when IO is | |
9672 | * submitted if they are wrong. | |
9673 | */ | |
6224843d PB |
9674 | if (!iov->iov_base) |
9675 | return iov->iov_len ? -EFAULT : 0; | |
9676 | if (!iov->iov_len) | |
2b358604 | 9677 | return -EFAULT; |
edafccee | 9678 | |
2b358604 BM |
9679 | /* arbitrary limit, but we need something */ |
9680 | if (iov->iov_len > SZ_1G) | |
9681 | return -EFAULT; | |
edafccee | 9682 | |
50e96989 PB |
9683 | if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp)) |
9684 | return -EOVERFLOW; | |
9685 | ||
2b358604 BM |
9686 | return 0; |
9687 | } | |
edafccee | 9688 | |
2b358604 | 9689 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
634d00df | 9690 | unsigned int nr_args, u64 __user *tags) |
2b358604 | 9691 | { |
bd54b6fe BM |
9692 | struct page *last_hpage = NULL; |
9693 | struct io_rsrc_data *data; | |
2b358604 BM |
9694 | int i, ret; |
9695 | struct iovec iov; | |
edafccee | 9696 | |
87094465 PB |
9697 | if (ctx->user_bufs) |
9698 | return -EBUSY; | |
489809e2 | 9699 | if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS) |
87094465 | 9700 | return -EINVAL; |
bd54b6fe | 9701 | ret = io_rsrc_node_switch_start(ctx); |
2b358604 BM |
9702 | if (ret) |
9703 | return ret; | |
d878c816 PB |
9704 | ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data); |
9705 | if (ret) | |
9706 | return ret; | |
bd54b6fe BM |
9707 | ret = io_buffers_map_alloc(ctx, nr_args); |
9708 | if (ret) { | |
bb6659cc | 9709 | io_rsrc_data_free(data); |
bd54b6fe BM |
9710 | return ret; |
9711 | } | |
edafccee | 9712 | |
87094465 | 9713 | for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) { |
edafccee JA |
9714 | ret = io_copy_iov(ctx, &iov, arg, i); |
9715 | if (ret) | |
0a96bbe4 | 9716 | break; |
2b358604 BM |
9717 | ret = io_buffer_validate(&iov); |
9718 | if (ret) | |
0a96bbe4 | 9719 | break; |
2d091d62 | 9720 | if (!iov.iov_base && *io_get_tag_slot(data, i)) { |
cf3770e7 CIK |
9721 | ret = -EINVAL; |
9722 | break; | |
9723 | } | |
edafccee | 9724 | |
41edf1a5 PB |
9725 | ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i], |
9726 | &last_hpage); | |
0a96bbe4 BM |
9727 | if (ret) |
9728 | break; | |
edafccee | 9729 | } |
0a96bbe4 | 9730 | |
bd54b6fe | 9731 | WARN_ON_ONCE(ctx->buf_data); |
0a96bbe4 | 9732 | |
bd54b6fe BM |
9733 | ctx->buf_data = data; |
9734 | if (ret) | |
9735 | __io_sqe_buffers_unregister(ctx); | |
9736 | else | |
9737 | io_rsrc_node_switch(ctx, NULL); | |
edafccee JA |
9738 | return ret; |
9739 | } | |
9740 | ||
634d00df PB |
9741 | static int __io_sqe_buffers_update(struct io_ring_ctx *ctx, |
9742 | struct io_uring_rsrc_update2 *up, | |
9743 | unsigned int nr_args) | |
9744 | { | |
9745 | u64 __user *tags = u64_to_user_ptr(up->tags); | |
9746 | struct iovec iov, __user *iovs = u64_to_user_ptr(up->data); | |
634d00df PB |
9747 | struct page *last_hpage = NULL; |
9748 | bool needs_switch = false; | |
9749 | __u32 done; | |
9750 | int i, err; | |
9751 | ||
9752 | if (!ctx->buf_data) | |
9753 | return -ENXIO; | |
9754 | if (up->offset + nr_args > ctx->nr_user_bufs) | |
9755 | return -EINVAL; | |
9756 | ||
9757 | for (done = 0; done < nr_args; done++) { | |
0b8c0e7c PB |
9758 | struct io_mapped_ubuf *imu; |
9759 | int offset = up->offset + done; | |
634d00df PB |
9760 | u64 tag = 0; |
9761 | ||
9762 | err = io_copy_iov(ctx, &iov, iovs, done); | |
9763 | if (err) | |
9764 | break; | |
9765 | if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) { | |
9766 | err = -EFAULT; | |
9767 | break; | |
9768 | } | |
0b8c0e7c PB |
9769 | err = io_buffer_validate(&iov); |
9770 | if (err) | |
9771 | break; | |
cf3770e7 CIK |
9772 | if (!iov.iov_base && tag) { |
9773 | err = -EINVAL; | |
9774 | break; | |
9775 | } | |
0b8c0e7c PB |
9776 | err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage); |
9777 | if (err) | |
9778 | break; | |
634d00df | 9779 | |
0b8c0e7c | 9780 | i = array_index_nospec(offset, ctx->nr_user_bufs); |
6224843d | 9781 | if (ctx->user_bufs[i] != ctx->dummy_ubuf) { |
0b8c0e7c PB |
9782 | err = io_queue_rsrc_removal(ctx->buf_data, offset, |
9783 | ctx->rsrc_node, ctx->user_bufs[i]); | |
9784 | if (unlikely(err)) { | |
9785 | io_buffer_unmap(ctx, &imu); | |
634d00df | 9786 | break; |
0b8c0e7c | 9787 | } |
634d00df PB |
9788 | ctx->user_bufs[i] = NULL; |
9789 | needs_switch = true; | |
9790 | } | |
9791 | ||
0b8c0e7c | 9792 | ctx->user_bufs[i] = imu; |
2d091d62 | 9793 | *io_get_tag_slot(ctx->buf_data, offset) = tag; |
634d00df PB |
9794 | } |
9795 | ||
9796 | if (needs_switch) | |
9797 | io_rsrc_node_switch(ctx, ctx->buf_data); | |
9798 | return done ? done : err; | |
9799 | } | |
9800 | ||
c75312dd UA |
9801 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg, |
9802 | unsigned int eventfd_async) | |
9b402849 | 9803 | { |
77bc59b4 | 9804 | struct io_ev_fd *ev_fd; |
9b402849 | 9805 | __s32 __user *fds = arg; |
f0a4e62b | 9806 | int fd; |
9b402849 | 9807 | |
77bc59b4 UA |
9808 | ev_fd = rcu_dereference_protected(ctx->io_ev_fd, |
9809 | lockdep_is_held(&ctx->uring_lock)); | |
9810 | if (ev_fd) | |
9b402849 JA |
9811 | return -EBUSY; |
9812 | ||
9813 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
9814 | return -EFAULT; | |
9815 | ||
77bc59b4 UA |
9816 | ev_fd = kmalloc(sizeof(*ev_fd), GFP_KERNEL); |
9817 | if (!ev_fd) | |
9818 | return -ENOMEM; | |
fe7e3257 | 9819 | |
77bc59b4 UA |
9820 | ev_fd->cq_ev_fd = eventfd_ctx_fdget(fd); |
9821 | if (IS_ERR(ev_fd->cq_ev_fd)) { | |
f0a4e62b | 9822 | int ret = PTR_ERR(ev_fd->cq_ev_fd); |
77bc59b4 | 9823 | kfree(ev_fd); |
9b402849 JA |
9824 | return ret; |
9825 | } | |
c75312dd | 9826 | ev_fd->eventfd_async = eventfd_async; |
9b402849 | 9827 | |
77bc59b4 | 9828 | rcu_assign_pointer(ctx->io_ev_fd, ev_fd); |
f0a4e62b | 9829 | return 0; |
77bc59b4 UA |
9830 | } |
9831 | ||
9832 | static void io_eventfd_put(struct rcu_head *rcu) | |
9833 | { | |
9834 | struct io_ev_fd *ev_fd = container_of(rcu, struct io_ev_fd, rcu); | |
9835 | ||
9836 | eventfd_ctx_put(ev_fd->cq_ev_fd); | |
9837 | kfree(ev_fd); | |
9b402849 JA |
9838 | } |
9839 | ||
9840 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
9841 | { | |
77bc59b4 UA |
9842 | struct io_ev_fd *ev_fd; |
9843 | ||
9844 | ev_fd = rcu_dereference_protected(ctx->io_ev_fd, | |
9845 | lockdep_is_held(&ctx->uring_lock)); | |
9846 | if (ev_fd) { | |
9847 | rcu_assign_pointer(ctx->io_ev_fd, NULL); | |
9848 | call_rcu(&ev_fd->rcu, io_eventfd_put); | |
9b402849 JA |
9849 | return 0; |
9850 | } | |
9851 | ||
9852 | return -ENXIO; | |
9853 | } | |
9854 | ||
5a2e745d JA |
9855 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
9856 | { | |
9e15c3a0 JA |
9857 | struct io_buffer *buf; |
9858 | unsigned long index; | |
9859 | ||
1d0254e6 | 9860 | xa_for_each(&ctx->io_buffers, index, buf) |
9e15c3a0 | 9861 | __io_remove_buffers(ctx, buf, index, -1U); |
cc3cec83 JA |
9862 | |
9863 | while (!list_empty(&ctx->io_buffers_pages)) { | |
9864 | struct page *page; | |
9865 | ||
9866 | page = list_first_entry(&ctx->io_buffers_pages, struct page, lru); | |
9867 | list_del_init(&page->lru); | |
9868 | __free_page(page); | |
9869 | } | |
5a2e745d JA |
9870 | } |
9871 | ||
4010fec4 | 9872 | static void io_req_caches_free(struct io_ring_ctx *ctx) |
2b188cc1 | 9873 | { |
cd0ca2e0 | 9874 | struct io_submit_state *state = &ctx->submit_state; |
37f0e767 | 9875 | int nr = 0; |
bf019da7 | 9876 | |
9a4fdbd8 | 9877 | mutex_lock(&ctx->uring_lock); |
cd0ca2e0 | 9878 | io_flush_cached_locked_reqs(ctx, state); |
9a4fdbd8 | 9879 | |
c2b6c6bc PB |
9880 | while (state->free_list.next) { |
9881 | struct io_wq_work_node *node; | |
9882 | struct io_kiocb *req; | |
9a4fdbd8 | 9883 | |
c2b6c6bc PB |
9884 | node = wq_stack_extract(&state->free_list); |
9885 | req = container_of(node, struct io_kiocb, comp_list); | |
9886 | kmem_cache_free(req_cachep, req); | |
37f0e767 | 9887 | nr++; |
c2b6c6bc | 9888 | } |
37f0e767 PB |
9889 | if (nr) |
9890 | percpu_ref_put_many(&ctx->refs, nr); | |
9a4fdbd8 JA |
9891 | mutex_unlock(&ctx->uring_lock); |
9892 | } | |
9893 | ||
43597aac | 9894 | static void io_wait_rsrc_data(struct io_rsrc_data *data) |
2b188cc1 | 9895 | { |
43597aac | 9896 | if (data && !atomic_dec_and_test(&data->refs)) |
bd54b6fe | 9897 | wait_for_completion(&data->done); |
bd54b6fe | 9898 | } |
04fc6c80 | 9899 | |
c072481d | 9900 | static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx) |
2b188cc1 | 9901 | { |
37d1e2e3 | 9902 | io_sq_thread_finish(ctx); |
2aede0e4 | 9903 | |
37d1e2e3 | 9904 | if (ctx->mm_account) { |
2aede0e4 JA |
9905 | mmdrop(ctx->mm_account); |
9906 | ctx->mm_account = NULL; | |
30975825 | 9907 | } |
def596e9 | 9908 | |
ab409402 | 9909 | io_rsrc_refs_drop(ctx); |
43597aac PB |
9910 | /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */ |
9911 | io_wait_rsrc_data(ctx->buf_data); | |
9912 | io_wait_rsrc_data(ctx->file_data); | |
9913 | ||
8bad28d8 | 9914 | mutex_lock(&ctx->uring_lock); |
43597aac | 9915 | if (ctx->buf_data) |
bd54b6fe | 9916 | __io_sqe_buffers_unregister(ctx); |
43597aac | 9917 | if (ctx->file_data) |
08480400 | 9918 | __io_sqe_files_unregister(ctx); |
c4ea060e PB |
9919 | if (ctx->rings) |
9920 | __io_cqring_overflow_flush(ctx, true); | |
9b402849 | 9921 | io_eventfd_unregister(ctx); |
77bc59b4 | 9922 | mutex_unlock(&ctx->uring_lock); |
5a2e745d | 9923 | io_destroy_buffers(ctx); |
07db298a PB |
9924 | if (ctx->sq_creds) |
9925 | put_cred(ctx->sq_creds); | |
def596e9 | 9926 | |
a7f0ed5a PB |
9927 | /* there are no registered resources left, nobody uses it */ |
9928 | if (ctx->rsrc_node) | |
9929 | io_rsrc_node_destroy(ctx->rsrc_node); | |
8dd03afe | 9930 | if (ctx->rsrc_backup_node) |
b895c9a6 | 9931 | io_rsrc_node_destroy(ctx->rsrc_backup_node); |
a7f0ed5a | 9932 | flush_delayed_work(&ctx->rsrc_put_work); |
756ab7c0 | 9933 | flush_delayed_work(&ctx->fallback_work); |
a7f0ed5a PB |
9934 | |
9935 | WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list)); | |
9936 | WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist)); | |
def596e9 | 9937 | |
2b188cc1 | 9938 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
9939 | if (ctx->ring_sock) { |
9940 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 9941 | sock_release(ctx->ring_sock); |
355e8d26 | 9942 | } |
2b188cc1 | 9943 | #endif |
ef9dd637 | 9944 | WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list)); |
2b188cc1 | 9945 | |
75b28aff | 9946 | io_mem_free(ctx->rings); |
2b188cc1 | 9947 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
9948 | |
9949 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 9950 | free_uid(ctx->user); |
4010fec4 | 9951 | io_req_caches_free(ctx); |
e941894e JA |
9952 | if (ctx->hash_map) |
9953 | io_wq_put_hash(ctx->hash_map); | |
adc8682e | 9954 | io_free_napi_list(ctx); |
78076bb6 | 9955 | kfree(ctx->cancel_hash); |
6224843d | 9956 | kfree(ctx->dummy_ubuf); |
2b188cc1 JA |
9957 | kfree(ctx); |
9958 | } | |
9959 | ||
9960 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
9961 | { | |
9962 | struct io_ring_ctx *ctx = file->private_data; | |
9963 | __poll_t mask = 0; | |
9964 | ||
d60aa65b | 9965 | poll_wait(file, &ctx->cq_wait, wait); |
4f7067c3 SB |
9966 | /* |
9967 | * synchronizes with barrier from wq_has_sleeper call in | |
9968 | * io_commit_cqring | |
9969 | */ | |
2b188cc1 | 9970 | smp_rmb(); |
90554200 | 9971 | if (!io_sqring_full(ctx)) |
2b188cc1 | 9972 | mask |= EPOLLOUT | EPOLLWRNORM; |
ed670c3f HX |
9973 | |
9974 | /* | |
9975 | * Don't flush cqring overflow list here, just do a simple check. | |
9976 | * Otherwise there could possible be ABBA deadlock: | |
9977 | * CPU0 CPU1 | |
9978 | * ---- ---- | |
9979 | * lock(&ctx->uring_lock); | |
9980 | * lock(&ep->mtx); | |
9981 | * lock(&ctx->uring_lock); | |
9982 | * lock(&ep->mtx); | |
9983 | * | |
9984 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this | |
9985 | * pushs them to do the flush. | |
9986 | */ | |
5ed7a37d | 9987 | if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow)) |
2b188cc1 JA |
9988 | mask |= EPOLLIN | EPOLLRDNORM; |
9989 | ||
9990 | return mask; | |
9991 | } | |
9992 | ||
0bead8cd | 9993 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
071698e1 | 9994 | { |
4379bf8b | 9995 | const struct cred *creds; |
071698e1 | 9996 | |
61cf9370 | 9997 | creds = xa_erase(&ctx->personalities, id); |
4379bf8b JA |
9998 | if (creds) { |
9999 | put_cred(creds); | |
0bead8cd | 10000 | return 0; |
1e6fa521 | 10001 | } |
0bead8cd YD |
10002 | |
10003 | return -EINVAL; | |
10004 | } | |
10005 | ||
d56d938b PB |
10006 | struct io_tctx_exit { |
10007 | struct callback_head task_work; | |
10008 | struct completion completion; | |
baf186c4 | 10009 | struct io_ring_ctx *ctx; |
d56d938b PB |
10010 | }; |
10011 | ||
c072481d | 10012 | static __cold void io_tctx_exit_cb(struct callback_head *cb) |
d56d938b PB |
10013 | { |
10014 | struct io_uring_task *tctx = current->io_uring; | |
10015 | struct io_tctx_exit *work; | |
10016 | ||
10017 | work = container_of(cb, struct io_tctx_exit, task_work); | |
10018 | /* | |
10019 | * When @in_idle, we're in cancellation and it's racy to remove the | |
10020 | * node. It'll be removed by the end of cancellation, just ignore it. | |
10021 | */ | |
10022 | if (!atomic_read(&tctx->in_idle)) | |
eef51daa | 10023 | io_uring_del_tctx_node((unsigned long)work->ctx); |
d56d938b PB |
10024 | complete(&work->completion); |
10025 | } | |
10026 | ||
c072481d | 10027 | static __cold bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
28090c13 PB |
10028 | { |
10029 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
10030 | ||
10031 | return req->ctx == data; | |
10032 | } | |
10033 | ||
c072481d | 10034 | static __cold void io_ring_exit_work(struct work_struct *work) |
85faa7b8 | 10035 | { |
d56d938b | 10036 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work); |
b5bb3a24 | 10037 | unsigned long timeout = jiffies + HZ * 60 * 5; |
58d3be2c | 10038 | unsigned long interval = HZ / 20; |
d56d938b PB |
10039 | struct io_tctx_exit exit; |
10040 | struct io_tctx_node *node; | |
10041 | int ret; | |
85faa7b8 | 10042 | |
56952e91 JA |
10043 | /* |
10044 | * If we're doing polled IO and end up having requests being | |
10045 | * submitted async (out-of-line), then completions can come in while | |
10046 | * we're waiting for refs to drop. We need to reap these manually, | |
10047 | * as nobody else will be looking for them. | |
10048 | */ | |
b2edc0a7 | 10049 | do { |
3dd0c97a | 10050 | io_uring_try_cancel_requests(ctx, NULL, true); |
28090c13 PB |
10051 | if (ctx->sq_data) { |
10052 | struct io_sq_data *sqd = ctx->sq_data; | |
10053 | struct task_struct *tsk; | |
10054 | ||
10055 | io_sq_thread_park(sqd); | |
10056 | tsk = sqd->thread; | |
10057 | if (tsk && tsk->io_uring && tsk->io_uring->io_wq) | |
10058 | io_wq_cancel_cb(tsk->io_uring->io_wq, | |
10059 | io_cancel_ctx_cb, ctx, true); | |
10060 | io_sq_thread_unpark(sqd); | |
10061 | } | |
b5bb3a24 | 10062 | |
37f0e767 PB |
10063 | io_req_caches_free(ctx); |
10064 | ||
58d3be2c PB |
10065 | if (WARN_ON_ONCE(time_after(jiffies, timeout))) { |
10066 | /* there is little hope left, don't run it too often */ | |
10067 | interval = HZ * 60; | |
10068 | } | |
10069 | } while (!wait_for_completion_timeout(&ctx->ref_comp, interval)); | |
d56d938b | 10070 | |
7f00651a PB |
10071 | init_completion(&exit.completion); |
10072 | init_task_work(&exit.task_work, io_tctx_exit_cb); | |
10073 | exit.ctx = ctx; | |
89b5066e PB |
10074 | /* |
10075 | * Some may use context even when all refs and requests have been put, | |
10076 | * and they are free to do so while still holding uring_lock or | |
5b0a6acc | 10077 | * completion_lock, see io_req_task_submit(). Apart from other work, |
89b5066e PB |
10078 | * this lock/unlock section also waits them to finish. |
10079 | */ | |
d56d938b PB |
10080 | mutex_lock(&ctx->uring_lock); |
10081 | while (!list_empty(&ctx->tctx_list)) { | |
b5bb3a24 PB |
10082 | WARN_ON_ONCE(time_after(jiffies, timeout)); |
10083 | ||
d56d938b PB |
10084 | node = list_first_entry(&ctx->tctx_list, struct io_tctx_node, |
10085 | ctx_node); | |
7f00651a PB |
10086 | /* don't spin on a single task if cancellation failed */ |
10087 | list_rotate_left(&ctx->tctx_list); | |
d56d938b PB |
10088 | ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL); |
10089 | if (WARN_ON_ONCE(ret)) | |
10090 | continue; | |
d56d938b PB |
10091 | |
10092 | mutex_unlock(&ctx->uring_lock); | |
10093 | wait_for_completion(&exit.completion); | |
d56d938b PB |
10094 | mutex_lock(&ctx->uring_lock); |
10095 | } | |
10096 | mutex_unlock(&ctx->uring_lock); | |
79ebeaee JA |
10097 | spin_lock(&ctx->completion_lock); |
10098 | spin_unlock(&ctx->completion_lock); | |
d56d938b | 10099 | |
85faa7b8 JA |
10100 | io_ring_ctx_free(ctx); |
10101 | } | |
10102 | ||
80c4cbdb | 10103 | /* Returns true if we found and killed one or more timeouts */ |
c072481d PB |
10104 | static __cold bool io_kill_timeouts(struct io_ring_ctx *ctx, |
10105 | struct task_struct *tsk, bool cancel_all) | |
80c4cbdb PB |
10106 | { |
10107 | struct io_kiocb *req, *tmp; | |
10108 | int canceled = 0; | |
10109 | ||
79ebeaee JA |
10110 | spin_lock(&ctx->completion_lock); |
10111 | spin_lock_irq(&ctx->timeout_lock); | |
80c4cbdb | 10112 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
3dd0c97a | 10113 | if (io_match_task(req, tsk, cancel_all)) { |
80c4cbdb PB |
10114 | io_kill_timeout(req, -ECANCELED); |
10115 | canceled++; | |
10116 | } | |
10117 | } | |
79ebeaee | 10118 | spin_unlock_irq(&ctx->timeout_lock); |
51520426 PB |
10119 | if (canceled != 0) |
10120 | io_commit_cqring(ctx); | |
79ebeaee | 10121 | spin_unlock(&ctx->completion_lock); |
80c4cbdb PB |
10122 | if (canceled != 0) |
10123 | io_cqring_ev_posted(ctx); | |
10124 | return canceled != 0; | |
10125 | } | |
10126 | ||
c072481d | 10127 | static __cold void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
2b188cc1 | 10128 | { |
61cf9370 MWO |
10129 | unsigned long index; |
10130 | struct creds *creds; | |
10131 | ||
2b188cc1 JA |
10132 | mutex_lock(&ctx->uring_lock); |
10133 | percpu_ref_kill(&ctx->refs); | |
634578f8 | 10134 | if (ctx->rings) |
6c2450ae | 10135 | __io_cqring_overflow_flush(ctx, true); |
61cf9370 MWO |
10136 | xa_for_each(&ctx->personalities, index, creds) |
10137 | io_unregister_personality(ctx, index); | |
2b188cc1 JA |
10138 | mutex_unlock(&ctx->uring_lock); |
10139 | ||
3dd0c97a PB |
10140 | io_kill_timeouts(ctx, NULL, true); |
10141 | io_poll_remove_all(ctx, NULL, true); | |
561fb04a | 10142 | |
15dff286 | 10143 | /* if we failed setting up the ctx, we might not have any rings */ |
b2edc0a7 | 10144 | io_iopoll_try_reap_events(ctx); |
309fc03a | 10145 | |
85faa7b8 | 10146 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
10147 | /* |
10148 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
10149 | * if we're exiting a ton of rings at the same time. It just adds | |
10150 | * noise and overhead, there's no discernable change in runtime | |
10151 | * over using system_wq. | |
10152 | */ | |
10153 | queue_work(system_unbound_wq, &ctx->exit_work); | |
2b188cc1 JA |
10154 | } |
10155 | ||
10156 | static int io_uring_release(struct inode *inode, struct file *file) | |
10157 | { | |
10158 | struct io_ring_ctx *ctx = file->private_data; | |
10159 | ||
10160 | file->private_data = NULL; | |
10161 | io_ring_ctx_wait_and_kill(ctx); | |
10162 | return 0; | |
10163 | } | |
10164 | ||
f6edbabb PB |
10165 | struct io_task_cancel { |
10166 | struct task_struct *task; | |
3dd0c97a | 10167 | bool all; |
f6edbabb | 10168 | }; |
f254ac04 | 10169 | |
f6edbabb | 10170 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
b711d4ea | 10171 | { |
9a472ef7 | 10172 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
f6edbabb | 10173 | struct io_task_cancel *cancel = data; |
9a472ef7 | 10174 | |
6af3f48b | 10175 | return io_match_task_safe(req, cancel->task, cancel->all); |
b711d4ea JA |
10176 | } |
10177 | ||
c072481d PB |
10178 | static __cold bool io_cancel_defer_files(struct io_ring_ctx *ctx, |
10179 | struct task_struct *task, | |
10180 | bool cancel_all) | |
b7ddce3c | 10181 | { |
e1915f76 | 10182 | struct io_defer_entry *de; |
b7ddce3c PB |
10183 | LIST_HEAD(list); |
10184 | ||
79ebeaee | 10185 | spin_lock(&ctx->completion_lock); |
b7ddce3c | 10186 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
6af3f48b | 10187 | if (io_match_task_safe(de->req, task, cancel_all)) { |
b7ddce3c PB |
10188 | list_cut_position(&list, &ctx->defer_list, &de->list); |
10189 | break; | |
10190 | } | |
10191 | } | |
79ebeaee | 10192 | spin_unlock(&ctx->completion_lock); |
e1915f76 PB |
10193 | if (list_empty(&list)) |
10194 | return false; | |
b7ddce3c PB |
10195 | |
10196 | while (!list_empty(&list)) { | |
10197 | de = list_first_entry(&list, struct io_defer_entry, list); | |
10198 | list_del_init(&de->list); | |
f41db273 | 10199 | io_req_complete_failed(de->req, -ECANCELED); |
b7ddce3c PB |
10200 | kfree(de); |
10201 | } | |
e1915f76 | 10202 | return true; |
b7ddce3c PB |
10203 | } |
10204 | ||
c072481d | 10205 | static __cold bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx) |
1b00764f PB |
10206 | { |
10207 | struct io_tctx_node *node; | |
10208 | enum io_wq_cancel cret; | |
10209 | bool ret = false; | |
10210 | ||
10211 | mutex_lock(&ctx->uring_lock); | |
10212 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
10213 | struct io_uring_task *tctx = node->task->io_uring; | |
10214 | ||
10215 | /* | |
10216 | * io_wq will stay alive while we hold uring_lock, because it's | |
10217 | * killed after ctx nodes, which requires to take the lock. | |
10218 | */ | |
10219 | if (!tctx || !tctx->io_wq) | |
10220 | continue; | |
10221 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true); | |
10222 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
10223 | } | |
10224 | mutex_unlock(&ctx->uring_lock); | |
10225 | ||
10226 | return ret; | |
10227 | } | |
10228 | ||
c072481d PB |
10229 | static __cold void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
10230 | struct task_struct *task, | |
10231 | bool cancel_all) | |
9936c7c2 | 10232 | { |
3dd0c97a | 10233 | struct io_task_cancel cancel = { .task = task, .all = cancel_all, }; |
1b00764f | 10234 | struct io_uring_task *tctx = task ? task->io_uring : NULL; |
9936c7c2 PB |
10235 | |
10236 | while (1) { | |
10237 | enum io_wq_cancel cret; | |
10238 | bool ret = false; | |
10239 | ||
1b00764f PB |
10240 | if (!task) { |
10241 | ret |= io_uring_try_cancel_iowq(ctx); | |
10242 | } else if (tctx && tctx->io_wq) { | |
10243 | /* | |
10244 | * Cancels requests of all rings, not only @ctx, but | |
10245 | * it's fine as the task is in exit/exec. | |
10246 | */ | |
5aa75ed5 | 10247 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, |
9936c7c2 PB |
10248 | &cancel, true); |
10249 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
10250 | } | |
10251 | ||
10252 | /* SQPOLL thread does its own polling */ | |
3dd0c97a | 10253 | if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) || |
d052d1d6 | 10254 | (ctx->sq_data && ctx->sq_data->thread == current)) { |
5eef4e87 | 10255 | while (!wq_list_empty(&ctx->iopoll_list)) { |
9936c7c2 PB |
10256 | io_iopoll_try_reap_events(ctx); |
10257 | ret = true; | |
10258 | } | |
10259 | } | |
10260 | ||
3dd0c97a PB |
10261 | ret |= io_cancel_defer_files(ctx, task, cancel_all); |
10262 | ret |= io_poll_remove_all(ctx, task, cancel_all); | |
10263 | ret |= io_kill_timeouts(ctx, task, cancel_all); | |
e5dc480d PB |
10264 | if (task) |
10265 | ret |= io_run_task_work(); | |
9936c7c2 PB |
10266 | if (!ret) |
10267 | break; | |
10268 | cond_resched(); | |
10269 | } | |
10270 | } | |
10271 | ||
eef51daa | 10272 | static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
0f212204 | 10273 | { |
236434c3 | 10274 | struct io_uring_task *tctx = current->io_uring; |
13bf43f5 | 10275 | struct io_tctx_node *node; |
a528b04e | 10276 | int ret; |
236434c3 MWO |
10277 | |
10278 | if (unlikely(!tctx)) { | |
5aa75ed5 | 10279 | ret = io_uring_alloc_task_context(current, ctx); |
0f212204 JA |
10280 | if (unlikely(ret)) |
10281 | return ret; | |
e139a1ec | 10282 | |
236434c3 | 10283 | tctx = current->io_uring; |
e139a1ec PB |
10284 | if (ctx->iowq_limits_set) { |
10285 | unsigned int limits[2] = { ctx->iowq_limits[0], | |
10286 | ctx->iowq_limits[1], }; | |
10287 | ||
10288 | ret = io_wq_max_workers(tctx->io_wq, limits); | |
10289 | if (ret) | |
10290 | return ret; | |
10291 | } | |
0f212204 | 10292 | } |
cf27f3b1 PB |
10293 | if (!xa_load(&tctx->xa, (unsigned long)ctx)) { |
10294 | node = kmalloc(sizeof(*node), GFP_KERNEL); | |
10295 | if (!node) | |
10296 | return -ENOMEM; | |
10297 | node->ctx = ctx; | |
10298 | node->task = current; | |
13bf43f5 | 10299 | |
cf27f3b1 PB |
10300 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx, |
10301 | node, GFP_KERNEL)); | |
10302 | if (ret) { | |
10303 | kfree(node); | |
10304 | return ret; | |
0f212204 | 10305 | } |
cf27f3b1 PB |
10306 | |
10307 | mutex_lock(&ctx->uring_lock); | |
10308 | list_add(&node->ctx_node, &ctx->tctx_list); | |
10309 | mutex_unlock(&ctx->uring_lock); | |
0f212204 | 10310 | } |
cf27f3b1 | 10311 | tctx->last = ctx; |
0f212204 JA |
10312 | return 0; |
10313 | } | |
10314 | ||
cf27f3b1 PB |
10315 | /* |
10316 | * Note that this task has used io_uring. We use it for cancelation purposes. | |
10317 | */ | |
eef51daa | 10318 | static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
cf27f3b1 PB |
10319 | { |
10320 | struct io_uring_task *tctx = current->io_uring; | |
10321 | ||
10322 | if (likely(tctx && tctx->last == ctx)) | |
10323 | return 0; | |
eef51daa | 10324 | return __io_uring_add_tctx_node(ctx); |
cf27f3b1 PB |
10325 | } |
10326 | ||
0f212204 JA |
10327 | /* |
10328 | * Remove this io_uring_file -> task mapping. | |
10329 | */ | |
c072481d | 10330 | static __cold void io_uring_del_tctx_node(unsigned long index) |
0f212204 JA |
10331 | { |
10332 | struct io_uring_task *tctx = current->io_uring; | |
13bf43f5 | 10333 | struct io_tctx_node *node; |
2941267b | 10334 | |
eebd2e37 PB |
10335 | if (!tctx) |
10336 | return; | |
13bf43f5 PB |
10337 | node = xa_erase(&tctx->xa, index); |
10338 | if (!node) | |
2941267b | 10339 | return; |
0f212204 | 10340 | |
13bf43f5 PB |
10341 | WARN_ON_ONCE(current != node->task); |
10342 | WARN_ON_ONCE(list_empty(&node->ctx_node)); | |
10343 | ||
10344 | mutex_lock(&node->ctx->uring_lock); | |
10345 | list_del(&node->ctx_node); | |
10346 | mutex_unlock(&node->ctx->uring_lock); | |
10347 | ||
baf186c4 | 10348 | if (tctx->last == node->ctx) |
0f212204 | 10349 | tctx->last = NULL; |
13bf43f5 | 10350 | kfree(node); |
0f212204 JA |
10351 | } |
10352 | ||
c072481d | 10353 | static __cold void io_uring_clean_tctx(struct io_uring_task *tctx) |
de7f1d9e | 10354 | { |
ba5ef6dc | 10355 | struct io_wq *wq = tctx->io_wq; |
13bf43f5 | 10356 | struct io_tctx_node *node; |
de7f1d9e PB |
10357 | unsigned long index; |
10358 | ||
8bab4c09 | 10359 | xa_for_each(&tctx->xa, index, node) { |
eef51daa | 10360 | io_uring_del_tctx_node(index); |
8bab4c09 JA |
10361 | cond_resched(); |
10362 | } | |
b16ef427 ME |
10363 | if (wq) { |
10364 | /* | |
f6f9b278 | 10365 | * Must be after io_uring_del_tctx_node() (removes nodes under |
b16ef427 ME |
10366 | * uring_lock) to avoid race with io_uring_try_cancel_iowq(). |
10367 | */ | |
ba5ef6dc | 10368 | io_wq_put_and_exit(wq); |
dadebc35 | 10369 | tctx->io_wq = NULL; |
b16ef427 | 10370 | } |
de7f1d9e PB |
10371 | } |
10372 | ||
3f48cf18 | 10373 | static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked) |
521d6a73 | 10374 | { |
3f48cf18 PB |
10375 | if (tracked) |
10376 | return atomic_read(&tctx->inflight_tracked); | |
521d6a73 PB |
10377 | return percpu_counter_sum(&tctx->inflight); |
10378 | } | |
10379 | ||
78cc687b PB |
10380 | /* |
10381 | * Find any io_uring ctx that this task has registered or done IO on, and cancel | |
78a78060 | 10382 | * requests. @sqd should be not-null IFF it's an SQPOLL thread cancellation. |
78cc687b | 10383 | */ |
c072481d PB |
10384 | static __cold void io_uring_cancel_generic(bool cancel_all, |
10385 | struct io_sq_data *sqd) | |
0e9ddb39 | 10386 | { |
521d6a73 | 10387 | struct io_uring_task *tctx = current->io_uring; |
734551df | 10388 | struct io_ring_ctx *ctx; |
0e9ddb39 PB |
10389 | s64 inflight; |
10390 | DEFINE_WAIT(wait); | |
fdaf083c | 10391 | |
78cc687b PB |
10392 | WARN_ON_ONCE(sqd && sqd->thread != current); |
10393 | ||
6d042ffb PO |
10394 | if (!current->io_uring) |
10395 | return; | |
17a91051 PB |
10396 | if (tctx->io_wq) |
10397 | io_wq_exit_start(tctx->io_wq); | |
10398 | ||
0e9ddb39 PB |
10399 | atomic_inc(&tctx->in_idle); |
10400 | do { | |
e9dbe221 | 10401 | io_uring_drop_tctx_refs(current); |
0e9ddb39 | 10402 | /* read completions before cancelations */ |
78cc687b | 10403 | inflight = tctx_inflight(tctx, !cancel_all); |
0e9ddb39 PB |
10404 | if (!inflight) |
10405 | break; | |
fdaf083c | 10406 | |
78cc687b PB |
10407 | if (!sqd) { |
10408 | struct io_tctx_node *node; | |
10409 | unsigned long index; | |
0f212204 | 10410 | |
78cc687b PB |
10411 | xa_for_each(&tctx->xa, index, node) { |
10412 | /* sqpoll task will cancel all its requests */ | |
10413 | if (node->ctx->sq_data) | |
10414 | continue; | |
10415 | io_uring_try_cancel_requests(node->ctx, current, | |
10416 | cancel_all); | |
10417 | } | |
10418 | } else { | |
10419 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) | |
10420 | io_uring_try_cancel_requests(ctx, current, | |
10421 | cancel_all); | |
10422 | } | |
17a91051 | 10423 | |
78a78060 JA |
10424 | prepare_to_wait(&tctx->wait, &wait, TASK_INTERRUPTIBLE); |
10425 | io_run_task_work(); | |
e9dbe221 | 10426 | io_uring_drop_tctx_refs(current); |
78a78060 | 10427 | |
0f212204 | 10428 | /* |
a1bb3cd5 PB |
10429 | * If we've seen completions, retry without waiting. This |
10430 | * avoids a race where a completion comes in before we did | |
10431 | * prepare_to_wait(). | |
0f212204 | 10432 | */ |
3dd0c97a | 10433 | if (inflight == tctx_inflight(tctx, !cancel_all)) |
a1bb3cd5 | 10434 | schedule(); |
f57555ed | 10435 | finish_wait(&tctx->wait, &wait); |
d8a6df10 | 10436 | } while (1); |
de7f1d9e | 10437 | |
8452d4a6 | 10438 | io_uring_clean_tctx(tctx); |
3dd0c97a | 10439 | if (cancel_all) { |
3cc7fdb9 PB |
10440 | /* |
10441 | * We shouldn't run task_works after cancel, so just leave | |
10442 | * ->in_idle set for normal exit. | |
10443 | */ | |
10444 | atomic_dec(&tctx->in_idle); | |
3f48cf18 PB |
10445 | /* for exec all current's requests should be gone, kill tctx */ |
10446 | __io_uring_free(current); | |
10447 | } | |
44e728b8 PB |
10448 | } |
10449 | ||
f552a27a | 10450 | void __io_uring_cancel(bool cancel_all) |
78cc687b | 10451 | { |
f552a27a | 10452 | io_uring_cancel_generic(cancel_all, NULL); |
78cc687b PB |
10453 | } |
10454 | ||
e7a6c00d JA |
10455 | void io_uring_unreg_ringfd(void) |
10456 | { | |
10457 | struct io_uring_task *tctx = current->io_uring; | |
10458 | int i; | |
10459 | ||
10460 | for (i = 0; i < IO_RINGFD_REG_MAX; i++) { | |
10461 | if (tctx->registered_rings[i]) { | |
10462 | fput(tctx->registered_rings[i]); | |
10463 | tctx->registered_rings[i] = NULL; | |
10464 | } | |
10465 | } | |
10466 | } | |
10467 | ||
10468 | static int io_ring_add_registered_fd(struct io_uring_task *tctx, int fd, | |
10469 | int start, int end) | |
10470 | { | |
10471 | struct file *file; | |
10472 | int offset; | |
10473 | ||
10474 | for (offset = start; offset < end; offset++) { | |
10475 | offset = array_index_nospec(offset, IO_RINGFD_REG_MAX); | |
10476 | if (tctx->registered_rings[offset]) | |
10477 | continue; | |
10478 | ||
10479 | file = fget(fd); | |
10480 | if (!file) { | |
10481 | return -EBADF; | |
10482 | } else if (file->f_op != &io_uring_fops) { | |
10483 | fput(file); | |
10484 | return -EOPNOTSUPP; | |
10485 | } | |
10486 | tctx->registered_rings[offset] = file; | |
10487 | return offset; | |
10488 | } | |
10489 | ||
10490 | return -EBUSY; | |
10491 | } | |
10492 | ||
10493 | /* | |
10494 | * Register a ring fd to avoid fdget/fdput for each io_uring_enter() | |
10495 | * invocation. User passes in an array of struct io_uring_rsrc_update | |
10496 | * with ->data set to the ring_fd, and ->offset given for the desired | |
10497 | * index. If no index is desired, application may set ->offset == -1U | |
10498 | * and we'll find an available index. Returns number of entries | |
10499 | * successfully processed, or < 0 on error if none were processed. | |
10500 | */ | |
10501 | static int io_ringfd_register(struct io_ring_ctx *ctx, void __user *__arg, | |
10502 | unsigned nr_args) | |
10503 | { | |
10504 | struct io_uring_rsrc_update __user *arg = __arg; | |
10505 | struct io_uring_rsrc_update reg; | |
10506 | struct io_uring_task *tctx; | |
10507 | int ret, i; | |
10508 | ||
10509 | if (!nr_args || nr_args > IO_RINGFD_REG_MAX) | |
10510 | return -EINVAL; | |
10511 | ||
10512 | mutex_unlock(&ctx->uring_lock); | |
10513 | ret = io_uring_add_tctx_node(ctx); | |
10514 | mutex_lock(&ctx->uring_lock); | |
10515 | if (ret) | |
10516 | return ret; | |
10517 | ||
10518 | tctx = current->io_uring; | |
10519 | for (i = 0; i < nr_args; i++) { | |
10520 | int start, end; | |
10521 | ||
10522 | if (copy_from_user(®, &arg[i], sizeof(reg))) { | |
10523 | ret = -EFAULT; | |
10524 | break; | |
10525 | } | |
10526 | ||
10527 | if (reg.offset == -1U) { | |
10528 | start = 0; | |
10529 | end = IO_RINGFD_REG_MAX; | |
10530 | } else { | |
10531 | if (reg.offset >= IO_RINGFD_REG_MAX) { | |
10532 | ret = -EINVAL; | |
10533 | break; | |
10534 | } | |
10535 | start = reg.offset; | |
10536 | end = start + 1; | |
10537 | } | |
10538 | ||
10539 | ret = io_ring_add_registered_fd(tctx, reg.data, start, end); | |
10540 | if (ret < 0) | |
10541 | break; | |
10542 | ||
10543 | reg.offset = ret; | |
10544 | if (copy_to_user(&arg[i], ®, sizeof(reg))) { | |
10545 | fput(tctx->registered_rings[reg.offset]); | |
10546 | tctx->registered_rings[reg.offset] = NULL; | |
10547 | ret = -EFAULT; | |
10548 | break; | |
10549 | } | |
10550 | } | |
10551 | ||
10552 | return i ? i : ret; | |
10553 | } | |
10554 | ||
10555 | static int io_ringfd_unregister(struct io_ring_ctx *ctx, void __user *__arg, | |
10556 | unsigned nr_args) | |
10557 | { | |
10558 | struct io_uring_rsrc_update __user *arg = __arg; | |
10559 | struct io_uring_task *tctx = current->io_uring; | |
10560 | struct io_uring_rsrc_update reg; | |
10561 | int ret = 0, i; | |
10562 | ||
10563 | if (!nr_args || nr_args > IO_RINGFD_REG_MAX) | |
10564 | return -EINVAL; | |
10565 | if (!tctx) | |
10566 | return 0; | |
10567 | ||
10568 | for (i = 0; i < nr_args; i++) { | |
10569 | if (copy_from_user(®, &arg[i], sizeof(reg))) { | |
10570 | ret = -EFAULT; | |
10571 | break; | |
10572 | } | |
10573 | if (reg.offset >= IO_RINGFD_REG_MAX) { | |
10574 | ret = -EINVAL; | |
10575 | break; | |
10576 | } | |
10577 | ||
10578 | reg.offset = array_index_nospec(reg.offset, IO_RINGFD_REG_MAX); | |
10579 | if (tctx->registered_rings[reg.offset]) { | |
10580 | fput(tctx->registered_rings[reg.offset]); | |
10581 | tctx->registered_rings[reg.offset] = NULL; | |
10582 | } | |
10583 | } | |
10584 | ||
10585 | return i ? i : ret; | |
10586 | } | |
10587 | ||
6c5c240e RP |
10588 | static void *io_uring_validate_mmap_request(struct file *file, |
10589 | loff_t pgoff, size_t sz) | |
2b188cc1 | 10590 | { |
2b188cc1 | 10591 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 10592 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
10593 | struct page *page; |
10594 | void *ptr; | |
10595 | ||
10596 | switch (offset) { | |
10597 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
10598 | case IORING_OFF_CQ_RING: |
10599 | ptr = ctx->rings; | |
2b188cc1 JA |
10600 | break; |
10601 | case IORING_OFF_SQES: | |
10602 | ptr = ctx->sq_sqes; | |
10603 | break; | |
2b188cc1 | 10604 | default: |
6c5c240e | 10605 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
10606 | } |
10607 | ||
10608 | page = virt_to_head_page(ptr); | |
a50b854e | 10609 | if (sz > page_size(page)) |
6c5c240e RP |
10610 | return ERR_PTR(-EINVAL); |
10611 | ||
10612 | return ptr; | |
10613 | } | |
10614 | ||
10615 | #ifdef CONFIG_MMU | |
10616 | ||
c072481d | 10617 | static __cold int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
6c5c240e RP |
10618 | { |
10619 | size_t sz = vma->vm_end - vma->vm_start; | |
10620 | unsigned long pfn; | |
10621 | void *ptr; | |
10622 | ||
10623 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
10624 | if (IS_ERR(ptr)) | |
10625 | return PTR_ERR(ptr); | |
2b188cc1 JA |
10626 | |
10627 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
10628 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
10629 | } | |
10630 | ||
6c5c240e RP |
10631 | #else /* !CONFIG_MMU */ |
10632 | ||
10633 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
10634 | { | |
10635 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
10636 | } | |
10637 | ||
10638 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
10639 | { | |
10640 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
10641 | } | |
10642 | ||
10643 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
10644 | unsigned long addr, unsigned long len, | |
10645 | unsigned long pgoff, unsigned long flags) | |
10646 | { | |
10647 | void *ptr; | |
10648 | ||
10649 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
10650 | if (IS_ERR(ptr)) | |
10651 | return PTR_ERR(ptr); | |
10652 | ||
10653 | return (unsigned long) ptr; | |
10654 | } | |
10655 | ||
10656 | #endif /* !CONFIG_MMU */ | |
10657 | ||
d9d05217 | 10658 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
90554200 JA |
10659 | { |
10660 | DEFINE_WAIT(wait); | |
10661 | ||
10662 | do { | |
10663 | if (!io_sqring_full(ctx)) | |
10664 | break; | |
90554200 JA |
10665 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
10666 | ||
10667 | if (!io_sqring_full(ctx)) | |
10668 | break; | |
90554200 JA |
10669 | schedule(); |
10670 | } while (!signal_pending(current)); | |
10671 | ||
10672 | finish_wait(&ctx->sqo_sq_wait, &wait); | |
5199328a | 10673 | return 0; |
90554200 JA |
10674 | } |
10675 | ||
c73ebb68 HX |
10676 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
10677 | struct __kernel_timespec __user **ts, | |
10678 | const sigset_t __user **sig) | |
10679 | { | |
10680 | struct io_uring_getevents_arg arg; | |
10681 | ||
10682 | /* | |
10683 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer | |
10684 | * is just a pointer to the sigset_t. | |
10685 | */ | |
10686 | if (!(flags & IORING_ENTER_EXT_ARG)) { | |
10687 | *sig = (const sigset_t __user *) argp; | |
10688 | *ts = NULL; | |
10689 | return 0; | |
10690 | } | |
10691 | ||
10692 | /* | |
10693 | * EXT_ARG is set - ensure we agree on the size of it and copy in our | |
10694 | * timespec and sigset_t pointers if good. | |
10695 | */ | |
10696 | if (*argsz != sizeof(arg)) | |
10697 | return -EINVAL; | |
10698 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
10699 | return -EFAULT; | |
10700 | *sig = u64_to_user_ptr(arg.sigmask); | |
10701 | *argsz = arg.sigmask_sz; | |
10702 | *ts = u64_to_user_ptr(arg.ts); | |
10703 | return 0; | |
10704 | } | |
10705 | ||
2b188cc1 | 10706 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
c73ebb68 HX |
10707 | u32, min_complete, u32, flags, const void __user *, argp, |
10708 | size_t, argsz) | |
2b188cc1 JA |
10709 | { |
10710 | struct io_ring_ctx *ctx; | |
2b188cc1 JA |
10711 | int submitted = 0; |
10712 | struct fd f; | |
33f993da | 10713 | long ret; |
2b188cc1 | 10714 | |
4c6e277c | 10715 | io_run_task_work(); |
b41e9852 | 10716 | |
33f993da | 10717 | if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
e7a6c00d JA |
10718 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG | |
10719 | IORING_ENTER_REGISTERED_RING))) | |
2b188cc1 JA |
10720 | return -EINVAL; |
10721 | ||
e7a6c00d JA |
10722 | /* |
10723 | * Ring fd has been registered via IORING_REGISTER_RING_FDS, we | |
10724 | * need only dereference our task private array to find it. | |
10725 | */ | |
10726 | if (flags & IORING_ENTER_REGISTERED_RING) { | |
10727 | struct io_uring_task *tctx = current->io_uring; | |
10728 | ||
10729 | if (!tctx || fd >= IO_RINGFD_REG_MAX) | |
10730 | return -EINVAL; | |
10731 | fd = array_index_nospec(fd, IO_RINGFD_REG_MAX); | |
10732 | f.file = tctx->registered_rings[fd]; | |
10733 | if (unlikely(!f.file)) | |
10734 | return -EBADF; | |
10735 | } else { | |
10736 | f = fdget(fd); | |
10737 | if (unlikely(!f.file)) | |
10738 | return -EBADF; | |
10739 | } | |
2b188cc1 JA |
10740 | |
10741 | ret = -EOPNOTSUPP; | |
33f993da | 10742 | if (unlikely(f.file->f_op != &io_uring_fops)) |
2b188cc1 JA |
10743 | goto out_fput; |
10744 | ||
10745 | ret = -ENXIO; | |
10746 | ctx = f.file->private_data; | |
33f993da | 10747 | if (unlikely(!percpu_ref_tryget(&ctx->refs))) |
2b188cc1 JA |
10748 | goto out_fput; |
10749 | ||
7e84e1c7 | 10750 | ret = -EBADFD; |
33f993da | 10751 | if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED)) |
7e84e1c7 SG |
10752 | goto out; |
10753 | ||
6c271ce2 JA |
10754 | /* |
10755 | * For SQ polling, the thread will do all submissions and completions. | |
10756 | * Just return the requested submit count, and wake the thread if | |
10757 | * we were asked to. | |
10758 | */ | |
b2a9eada | 10759 | ret = 0; |
6c271ce2 | 10760 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
90f67366 | 10761 | io_cqring_overflow_flush(ctx); |
89448c47 | 10762 | |
21f96522 JA |
10763 | if (unlikely(ctx->sq_data->thread == NULL)) { |
10764 | ret = -EOWNERDEAD; | |
04147488 | 10765 | goto out; |
21f96522 | 10766 | } |
6c271ce2 | 10767 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 10768 | wake_up(&ctx->sq_data->wait); |
d9d05217 PB |
10769 | if (flags & IORING_ENTER_SQ_WAIT) { |
10770 | ret = io_sqpoll_wait_sq(ctx); | |
10771 | if (ret) | |
10772 | goto out; | |
10773 | } | |
6c271ce2 | 10774 | submitted = to_submit; |
b2a9eada | 10775 | } else if (to_submit) { |
eef51daa | 10776 | ret = io_uring_add_tctx_node(ctx); |
0f212204 JA |
10777 | if (unlikely(ret)) |
10778 | goto out; | |
2b188cc1 | 10779 | mutex_lock(&ctx->uring_lock); |
0f212204 | 10780 | submitted = io_submit_sqes(ctx, to_submit); |
2b188cc1 | 10781 | mutex_unlock(&ctx->uring_lock); |
7c504e65 PB |
10782 | |
10783 | if (submitted != to_submit) | |
10784 | goto out; | |
2b188cc1 JA |
10785 | } |
10786 | if (flags & IORING_ENTER_GETEVENTS) { | |
c73ebb68 HX |
10787 | const sigset_t __user *sig; |
10788 | struct __kernel_timespec __user *ts; | |
10789 | ||
10790 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); | |
10791 | if (unlikely(ret)) | |
10792 | goto out; | |
10793 | ||
2b188cc1 JA |
10794 | min_complete = min(min_complete, ctx->cq_entries); |
10795 | ||
32b2244a XW |
10796 | /* |
10797 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
10798 | * space applications don't need to do io completion events | |
10799 | * polling again, they can rely on io_sq_thread to do polling | |
10800 | * work, which can reduce cpu usage and uring_lock contention. | |
10801 | */ | |
10802 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
10803 | !(ctx->flags & IORING_SETUP_SQPOLL)) { | |
7668b92a | 10804 | ret = io_iopoll_check(ctx, min_complete); |
def596e9 | 10805 | } else { |
c73ebb68 | 10806 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
def596e9 | 10807 | } |
2b188cc1 JA |
10808 | } |
10809 | ||
7c504e65 | 10810 | out: |
6805b32e | 10811 | percpu_ref_put(&ctx->refs); |
2b188cc1 | 10812 | out_fput: |
e7a6c00d JA |
10813 | if (!(flags & IORING_ENTER_REGISTERED_RING)) |
10814 | fdput(f); | |
2b188cc1 JA |
10815 | return submitted ? submitted : ret; |
10816 | } | |
10817 | ||
bebdb65e | 10818 | #ifdef CONFIG_PROC_FS |
c072481d | 10819 | static __cold int io_uring_show_cred(struct seq_file *m, unsigned int id, |
61cf9370 | 10820 | const struct cred *cred) |
87ce955b | 10821 | { |
87ce955b JA |
10822 | struct user_namespace *uns = seq_user_ns(m); |
10823 | struct group_info *gi; | |
10824 | kernel_cap_t cap; | |
10825 | unsigned __capi; | |
10826 | int g; | |
10827 | ||
10828 | seq_printf(m, "%5d\n", id); | |
10829 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
10830 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
10831 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
10832 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
10833 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
10834 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
10835 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
10836 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
10837 | seq_puts(m, "\n\tGroups:\t"); | |
10838 | gi = cred->group_info; | |
10839 | for (g = 0; g < gi->ngroups; g++) { | |
10840 | seq_put_decimal_ull(m, g ? " " : "", | |
10841 | from_kgid_munged(uns, gi->gid[g])); | |
10842 | } | |
10843 | seq_puts(m, "\n\tCapEff:\t"); | |
10844 | cap = cred->cap_effective; | |
10845 | CAP_FOR_EACH_U32(__capi) | |
10846 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
10847 | seq_putc(m, '\n'); | |
10848 | return 0; | |
10849 | } | |
10850 | ||
c072481d PB |
10851 | static __cold void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, |
10852 | struct seq_file *m) | |
87ce955b | 10853 | { |
dbbe9c64 | 10854 | struct io_sq_data *sq = NULL; |
83f84356 HX |
10855 | struct io_overflow_cqe *ocqe; |
10856 | struct io_rings *r = ctx->rings; | |
10857 | unsigned int sq_mask = ctx->sq_entries - 1, cq_mask = ctx->cq_entries - 1; | |
83f84356 HX |
10858 | unsigned int sq_head = READ_ONCE(r->sq.head); |
10859 | unsigned int sq_tail = READ_ONCE(r->sq.tail); | |
10860 | unsigned int cq_head = READ_ONCE(r->cq.head); | |
10861 | unsigned int cq_tail = READ_ONCE(r->cq.tail); | |
f75d1183 | 10862 | unsigned int sq_entries, cq_entries; |
fad8e0de | 10863 | bool has_lock; |
83f84356 HX |
10864 | unsigned int i; |
10865 | ||
10866 | /* | |
10867 | * we may get imprecise sqe and cqe info if uring is actively running | |
10868 | * since we get cached_sq_head and cached_cq_tail without uring_lock | |
10869 | * and sq_tail and cq_head are changed by userspace. But it's ok since | |
10870 | * we usually use these info when it is stuck. | |
10871 | */ | |
c0235652 | 10872 | seq_printf(m, "SqMask:\t0x%x\n", sq_mask); |
f75d1183 JA |
10873 | seq_printf(m, "SqHead:\t%u\n", sq_head); |
10874 | seq_printf(m, "SqTail:\t%u\n", sq_tail); | |
10875 | seq_printf(m, "CachedSqHead:\t%u\n", ctx->cached_sq_head); | |
10876 | seq_printf(m, "CqMask:\t0x%x\n", cq_mask); | |
10877 | seq_printf(m, "CqHead:\t%u\n", cq_head); | |
10878 | seq_printf(m, "CqTail:\t%u\n", cq_tail); | |
10879 | seq_printf(m, "CachedCqTail:\t%u\n", ctx->cached_cq_tail); | |
10880 | seq_printf(m, "SQEs:\t%u\n", sq_tail - ctx->cached_sq_head); | |
10881 | sq_entries = min(sq_tail - sq_head, ctx->sq_entries); | |
10882 | for (i = 0; i < sq_entries; i++) { | |
10883 | unsigned int entry = i + sq_head; | |
10884 | unsigned int sq_idx = READ_ONCE(ctx->sq_array[entry & sq_mask]); | |
a1957780 | 10885 | struct io_uring_sqe *sqe; |
f75d1183 JA |
10886 | |
10887 | if (sq_idx > sq_mask) | |
10888 | continue; | |
10889 | sqe = &ctx->sq_sqes[sq_idx]; | |
10890 | seq_printf(m, "%5u: opcode:%d, fd:%d, flags:%x, user_data:%llu\n", | |
10891 | sq_idx, sqe->opcode, sqe->fd, sqe->flags, | |
10892 | sqe->user_data); | |
83f84356 | 10893 | } |
f75d1183 JA |
10894 | seq_printf(m, "CQEs:\t%u\n", cq_tail - cq_head); |
10895 | cq_entries = min(cq_tail - cq_head, ctx->cq_entries); | |
10896 | for (i = 0; i < cq_entries; i++) { | |
10897 | unsigned int entry = i + cq_head; | |
10898 | struct io_uring_cqe *cqe = &r->cqes[entry & cq_mask]; | |
83f84356 HX |
10899 | |
10900 | seq_printf(m, "%5u: user_data:%llu, res:%d, flag:%x\n", | |
f75d1183 JA |
10901 | entry & cq_mask, cqe->user_data, cqe->res, |
10902 | cqe->flags); | |
83f84356 | 10903 | } |
87ce955b | 10904 | |
fad8e0de JA |
10905 | /* |
10906 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, | |
10907 | * since fdinfo case grabs it in the opposite direction of normal use | |
10908 | * cases. If we fail to get the lock, we just don't iterate any | |
10909 | * structures that could be going away outside the io_uring mutex. | |
10910 | */ | |
10911 | has_lock = mutex_trylock(&ctx->uring_lock); | |
10912 | ||
5f3f26f9 | 10913 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) { |
dbbe9c64 | 10914 | sq = ctx->sq_data; |
5f3f26f9 JA |
10915 | if (!sq->thread) |
10916 | sq = NULL; | |
10917 | } | |
dbbe9c64 JQ |
10918 | |
10919 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); | |
10920 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); | |
87ce955b | 10921 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
fad8e0de | 10922 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
7b29f92d | 10923 | struct file *f = io_file_from_index(ctx, i); |
87ce955b | 10924 | |
87ce955b JA |
10925 | if (f) |
10926 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
10927 | else | |
10928 | seq_printf(m, "%5u: <none>\n", i); | |
10929 | } | |
10930 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
fad8e0de | 10931 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
41edf1a5 | 10932 | struct io_mapped_ubuf *buf = ctx->user_bufs[i]; |
4751f53d | 10933 | unsigned int len = buf->ubuf_end - buf->ubuf; |
87ce955b | 10934 | |
4751f53d | 10935 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len); |
87ce955b | 10936 | } |
61cf9370 MWO |
10937 | if (has_lock && !xa_empty(&ctx->personalities)) { |
10938 | unsigned long index; | |
10939 | const struct cred *cred; | |
10940 | ||
87ce955b | 10941 | seq_printf(m, "Personalities:\n"); |
61cf9370 MWO |
10942 | xa_for_each(&ctx->personalities, index, cred) |
10943 | io_uring_show_cred(m, index, cred); | |
87ce955b | 10944 | } |
83f84356 HX |
10945 | if (has_lock) |
10946 | mutex_unlock(&ctx->uring_lock); | |
10947 | ||
10948 | seq_puts(m, "PollList:\n"); | |
79ebeaee | 10949 | spin_lock(&ctx->completion_lock); |
d7718a9d JA |
10950 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
10951 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
10952 | struct io_kiocb *req; | |
10953 | ||
10954 | hlist_for_each_entry(req, list, hash_node) | |
10955 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
10956 | req->task->task_works != NULL); | |
10957 | } | |
83f84356 HX |
10958 | |
10959 | seq_puts(m, "CqOverflowList:\n"); | |
10960 | list_for_each_entry(ocqe, &ctx->cq_overflow_list, list) { | |
10961 | struct io_uring_cqe *cqe = &ocqe->cqe; | |
10962 | ||
10963 | seq_printf(m, " user_data=%llu, res=%d, flags=%x\n", | |
10964 | cqe->user_data, cqe->res, cqe->flags); | |
10965 | ||
10966 | } | |
10967 | ||
79ebeaee | 10968 | spin_unlock(&ctx->completion_lock); |
87ce955b JA |
10969 | } |
10970 | ||
c072481d | 10971 | static __cold void io_uring_show_fdinfo(struct seq_file *m, struct file *f) |
87ce955b JA |
10972 | { |
10973 | struct io_ring_ctx *ctx = f->private_data; | |
10974 | ||
10975 | if (percpu_ref_tryget(&ctx->refs)) { | |
10976 | __io_uring_show_fdinfo(ctx, m); | |
10977 | percpu_ref_put(&ctx->refs); | |
10978 | } | |
10979 | } | |
bebdb65e | 10980 | #endif |
87ce955b | 10981 | |
2b188cc1 JA |
10982 | static const struct file_operations io_uring_fops = { |
10983 | .release = io_uring_release, | |
10984 | .mmap = io_uring_mmap, | |
6c5c240e RP |
10985 | #ifndef CONFIG_MMU |
10986 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
10987 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
10988 | #endif | |
2b188cc1 | 10989 | .poll = io_uring_poll, |
bebdb65e | 10990 | #ifdef CONFIG_PROC_FS |
87ce955b | 10991 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 10992 | #endif |
2b188cc1 JA |
10993 | }; |
10994 | ||
c072481d PB |
10995 | static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
10996 | struct io_uring_params *p) | |
2b188cc1 | 10997 | { |
75b28aff HV |
10998 | struct io_rings *rings; |
10999 | size_t size, sq_array_offset; | |
2b188cc1 | 11000 | |
bd740481 JA |
11001 | /* make sure these are sane, as we already accounted them */ |
11002 | ctx->sq_entries = p->sq_entries; | |
11003 | ctx->cq_entries = p->cq_entries; | |
11004 | ||
75b28aff HV |
11005 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
11006 | if (size == SIZE_MAX) | |
11007 | return -EOVERFLOW; | |
11008 | ||
11009 | rings = io_mem_alloc(size); | |
11010 | if (!rings) | |
2b188cc1 JA |
11011 | return -ENOMEM; |
11012 | ||
75b28aff HV |
11013 | ctx->rings = rings; |
11014 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
11015 | rings->sq_ring_mask = p->sq_entries - 1; | |
11016 | rings->cq_ring_mask = p->cq_entries - 1; | |
11017 | rings->sq_ring_entries = p->sq_entries; | |
11018 | rings->cq_ring_entries = p->cq_entries; | |
2b188cc1 JA |
11019 | |
11020 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
11021 | if (size == SIZE_MAX) { |
11022 | io_mem_free(ctx->rings); | |
11023 | ctx->rings = NULL; | |
2b188cc1 | 11024 | return -EOVERFLOW; |
eb065d30 | 11025 | } |
2b188cc1 JA |
11026 | |
11027 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
11028 | if (!ctx->sq_sqes) { |
11029 | io_mem_free(ctx->rings); | |
11030 | ctx->rings = NULL; | |
2b188cc1 | 11031 | return -ENOMEM; |
eb065d30 | 11032 | } |
2b188cc1 | 11033 | |
2b188cc1 JA |
11034 | return 0; |
11035 | } | |
11036 | ||
9faadcc8 PB |
11037 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
11038 | { | |
11039 | int ret, fd; | |
11040 | ||
11041 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
11042 | if (fd < 0) | |
11043 | return fd; | |
11044 | ||
eef51daa | 11045 | ret = io_uring_add_tctx_node(ctx); |
9faadcc8 PB |
11046 | if (ret) { |
11047 | put_unused_fd(fd); | |
11048 | return ret; | |
11049 | } | |
11050 | fd_install(fd, file); | |
11051 | return fd; | |
11052 | } | |
11053 | ||
2b188cc1 JA |
11054 | /* |
11055 | * Allocate an anonymous fd, this is what constitutes the application | |
11056 | * visible backing of an io_uring instance. The application mmaps this | |
11057 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
11058 | * we have to tie this fd to a socket for file garbage collection purposes. | |
11059 | */ | |
9faadcc8 | 11060 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
2b188cc1 JA |
11061 | { |
11062 | struct file *file; | |
9faadcc8 | 11063 | #if defined(CONFIG_UNIX) |
2b188cc1 JA |
11064 | int ret; |
11065 | ||
2b188cc1 JA |
11066 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
11067 | &ctx->ring_sock); | |
11068 | if (ret) | |
9faadcc8 | 11069 | return ERR_PTR(ret); |
2b188cc1 JA |
11070 | #endif |
11071 | ||
91a9ab7c PM |
11072 | file = anon_inode_getfile_secure("[io_uring]", &io_uring_fops, ctx, |
11073 | O_RDWR | O_CLOEXEC, NULL); | |
2b188cc1 | 11074 | #if defined(CONFIG_UNIX) |
9faadcc8 PB |
11075 | if (IS_ERR(file)) { |
11076 | sock_release(ctx->ring_sock); | |
11077 | ctx->ring_sock = NULL; | |
11078 | } else { | |
11079 | ctx->ring_sock->file = file; | |
0f212204 | 11080 | } |
2b188cc1 | 11081 | #endif |
9faadcc8 | 11082 | return file; |
2b188cc1 JA |
11083 | } |
11084 | ||
c072481d PB |
11085 | static __cold int io_uring_create(unsigned entries, struct io_uring_params *p, |
11086 | struct io_uring_params __user *params) | |
2b188cc1 | 11087 | { |
2b188cc1 | 11088 | struct io_ring_ctx *ctx; |
9faadcc8 | 11089 | struct file *file; |
2b188cc1 JA |
11090 | int ret; |
11091 | ||
8110c1a6 | 11092 | if (!entries) |
2b188cc1 | 11093 | return -EINVAL; |
8110c1a6 JA |
11094 | if (entries > IORING_MAX_ENTRIES) { |
11095 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
11096 | return -EINVAL; | |
11097 | entries = IORING_MAX_ENTRIES; | |
11098 | } | |
2b188cc1 JA |
11099 | |
11100 | /* | |
11101 | * Use twice as many entries for the CQ ring. It's possible for the | |
11102 | * application to drive a higher depth than the size of the SQ ring, | |
11103 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
11104 | * some flexibility in overcommitting a bit. If the application has |
11105 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
11106 | * of CQ ring entries manually. | |
2b188cc1 JA |
11107 | */ |
11108 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
11109 | if (p->flags & IORING_SETUP_CQSIZE) { |
11110 | /* | |
11111 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
11112 | * to a power-of-two, if it isn't already. We do NOT impose | |
11113 | * any cq vs sq ring sizing. | |
11114 | */ | |
eb2667b3 | 11115 | if (!p->cq_entries) |
33a107f0 | 11116 | return -EINVAL; |
8110c1a6 JA |
11117 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
11118 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
11119 | return -EINVAL; | |
11120 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
11121 | } | |
eb2667b3 JQ |
11122 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
11123 | if (p->cq_entries < p->sq_entries) | |
11124 | return -EINVAL; | |
33a107f0 JA |
11125 | } else { |
11126 | p->cq_entries = 2 * p->sq_entries; | |
11127 | } | |
2b188cc1 | 11128 | |
2b188cc1 | 11129 | ctx = io_ring_ctx_alloc(p); |
62e398be | 11130 | if (!ctx) |
2b188cc1 | 11131 | return -ENOMEM; |
2b188cc1 | 11132 | ctx->compat = in_compat_syscall(); |
62e398be JA |
11133 | if (!capable(CAP_IPC_LOCK)) |
11134 | ctx->user = get_uid(current_user()); | |
2aede0e4 JA |
11135 | |
11136 | /* | |
11137 | * This is just grabbed for accounting purposes. When a process exits, | |
11138 | * the mm is exited and dropped before the files, hence we need to hang | |
11139 | * on to this mm purely for the purposes of being able to unaccount | |
11140 | * memory (locked/pinned vm). It's not used for anything else. | |
11141 | */ | |
6b7898eb | 11142 | mmgrab(current->mm); |
2aede0e4 | 11143 | ctx->mm_account = current->mm; |
6b7898eb | 11144 | |
2b188cc1 JA |
11145 | ret = io_allocate_scq_urings(ctx, p); |
11146 | if (ret) | |
11147 | goto err; | |
11148 | ||
7e84e1c7 | 11149 | ret = io_sq_offload_create(ctx, p); |
2b188cc1 JA |
11150 | if (ret) |
11151 | goto err; | |
eae071c9 | 11152 | /* always set a rsrc node */ |
47b228ce PB |
11153 | ret = io_rsrc_node_switch_start(ctx); |
11154 | if (ret) | |
11155 | goto err; | |
eae071c9 | 11156 | io_rsrc_node_switch(ctx, NULL); |
2b188cc1 | 11157 | |
2b188cc1 | 11158 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
11159 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
11160 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
11161 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
11162 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
11163 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
11164 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
11165 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
11166 | |
11167 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
11168 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
11169 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
11170 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
11171 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
11172 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
11173 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 11174 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 11175 | |
7f13657d XW |
11176 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
11177 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 | 11178 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
c73ebb68 | 11179 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
9690557e | 11180 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS | |
04c76b41 | 11181 | IORING_FEAT_RSRC_TAGS | IORING_FEAT_CQE_SKIP; |
7f13657d XW |
11182 | |
11183 | if (copy_to_user(params, p, sizeof(*p))) { | |
11184 | ret = -EFAULT; | |
11185 | goto err; | |
11186 | } | |
d1719f70 | 11187 | |
9faadcc8 PB |
11188 | file = io_uring_get_file(ctx); |
11189 | if (IS_ERR(file)) { | |
11190 | ret = PTR_ERR(file); | |
11191 | goto err; | |
11192 | } | |
11193 | ||
044c1ab3 JA |
11194 | /* |
11195 | * Install ring fd as the very last thing, so we don't risk someone | |
11196 | * having closed it before we finish setup | |
11197 | */ | |
9faadcc8 PB |
11198 | ret = io_uring_install_fd(ctx, file); |
11199 | if (ret < 0) { | |
11200 | /* fput will clean it up */ | |
11201 | fput(file); | |
11202 | return ret; | |
11203 | } | |
044c1ab3 | 11204 | |
c826bd7a | 11205 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
11206 | return ret; |
11207 | err: | |
11208 | io_ring_ctx_wait_and_kill(ctx); | |
11209 | return ret; | |
11210 | } | |
11211 | ||
11212 | /* | |
11213 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
11214 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
11215 | * params structure passed in. | |
11216 | */ | |
11217 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
11218 | { | |
11219 | struct io_uring_params p; | |
2b188cc1 JA |
11220 | int i; |
11221 | ||
11222 | if (copy_from_user(&p, params, sizeof(p))) | |
11223 | return -EFAULT; | |
11224 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
11225 | if (p.resv[i]) | |
11226 | return -EINVAL; | |
11227 | } | |
11228 | ||
6c271ce2 | 11229 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 11230 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 SG |
11231 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
11232 | IORING_SETUP_R_DISABLED)) | |
2b188cc1 JA |
11233 | return -EINVAL; |
11234 | ||
7f13657d | 11235 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
11236 | } |
11237 | ||
11238 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
11239 | struct io_uring_params __user *, params) | |
11240 | { | |
11241 | return io_uring_setup(entries, params); | |
11242 | } | |
11243 | ||
c072481d PB |
11244 | static __cold int io_probe(struct io_ring_ctx *ctx, void __user *arg, |
11245 | unsigned nr_args) | |
66f4af93 JA |
11246 | { |
11247 | struct io_uring_probe *p; | |
11248 | size_t size; | |
11249 | int i, ret; | |
11250 | ||
11251 | size = struct_size(p, ops, nr_args); | |
11252 | if (size == SIZE_MAX) | |
11253 | return -EOVERFLOW; | |
11254 | p = kzalloc(size, GFP_KERNEL); | |
11255 | if (!p) | |
11256 | return -ENOMEM; | |
11257 | ||
11258 | ret = -EFAULT; | |
11259 | if (copy_from_user(p, arg, size)) | |
11260 | goto out; | |
11261 | ret = -EINVAL; | |
11262 | if (memchr_inv(p, 0, size)) | |
11263 | goto out; | |
11264 | ||
11265 | p->last_op = IORING_OP_LAST - 1; | |
11266 | if (nr_args > IORING_OP_LAST) | |
11267 | nr_args = IORING_OP_LAST; | |
11268 | ||
11269 | for (i = 0; i < nr_args; i++) { | |
11270 | p->ops[i].op = i; | |
11271 | if (!io_op_defs[i].not_supported) | |
11272 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
11273 | } | |
11274 | p->ops_len = i; | |
11275 | ||
11276 | ret = 0; | |
11277 | if (copy_to_user(arg, p, size)) | |
11278 | ret = -EFAULT; | |
11279 | out: | |
11280 | kfree(p); | |
11281 | return ret; | |
11282 | } | |
11283 | ||
071698e1 JA |
11284 | static int io_register_personality(struct io_ring_ctx *ctx) |
11285 | { | |
4379bf8b | 11286 | const struct cred *creds; |
61cf9370 | 11287 | u32 id; |
1e6fa521 | 11288 | int ret; |
071698e1 | 11289 | |
4379bf8b | 11290 | creds = get_current_cred(); |
1e6fa521 | 11291 | |
61cf9370 MWO |
11292 | ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds, |
11293 | XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL); | |
a30f895a JA |
11294 | if (ret < 0) { |
11295 | put_cred(creds); | |
11296 | return ret; | |
11297 | } | |
11298 | return id; | |
071698e1 JA |
11299 | } |
11300 | ||
c072481d PB |
11301 | static __cold int io_register_restrictions(struct io_ring_ctx *ctx, |
11302 | void __user *arg, unsigned int nr_args) | |
21b55dbc SG |
11303 | { |
11304 | struct io_uring_restriction *res; | |
11305 | size_t size; | |
11306 | int i, ret; | |
11307 | ||
7e84e1c7 SG |
11308 | /* Restrictions allowed only if rings started disabled */ |
11309 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
11310 | return -EBADFD; | |
11311 | ||
21b55dbc | 11312 | /* We allow only a single restrictions registration */ |
7e84e1c7 | 11313 | if (ctx->restrictions.registered) |
21b55dbc SG |
11314 | return -EBUSY; |
11315 | ||
11316 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) | |
11317 | return -EINVAL; | |
11318 | ||
11319 | size = array_size(nr_args, sizeof(*res)); | |
11320 | if (size == SIZE_MAX) | |
11321 | return -EOVERFLOW; | |
11322 | ||
11323 | res = memdup_user(arg, size); | |
11324 | if (IS_ERR(res)) | |
11325 | return PTR_ERR(res); | |
11326 | ||
11327 | ret = 0; | |
11328 | ||
11329 | for (i = 0; i < nr_args; i++) { | |
11330 | switch (res[i].opcode) { | |
11331 | case IORING_RESTRICTION_REGISTER_OP: | |
11332 | if (res[i].register_op >= IORING_REGISTER_LAST) { | |
11333 | ret = -EINVAL; | |
11334 | goto out; | |
11335 | } | |
11336 | ||
11337 | __set_bit(res[i].register_op, | |
11338 | ctx->restrictions.register_op); | |
11339 | break; | |
11340 | case IORING_RESTRICTION_SQE_OP: | |
11341 | if (res[i].sqe_op >= IORING_OP_LAST) { | |
11342 | ret = -EINVAL; | |
11343 | goto out; | |
11344 | } | |
11345 | ||
11346 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); | |
11347 | break; | |
11348 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: | |
11349 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; | |
11350 | break; | |
11351 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: | |
11352 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; | |
11353 | break; | |
11354 | default: | |
11355 | ret = -EINVAL; | |
11356 | goto out; | |
11357 | } | |
11358 | } | |
11359 | ||
11360 | out: | |
11361 | /* Reset all restrictions if an error happened */ | |
11362 | if (ret != 0) | |
11363 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); | |
11364 | else | |
7e84e1c7 | 11365 | ctx->restrictions.registered = true; |
21b55dbc SG |
11366 | |
11367 | kfree(res); | |
11368 | return ret; | |
11369 | } | |
11370 | ||
7e84e1c7 SG |
11371 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
11372 | { | |
11373 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
11374 | return -EBADFD; | |
11375 | ||
11376 | if (ctx->restrictions.registered) | |
11377 | ctx->restricted = 1; | |
11378 | ||
0298ef96 PB |
11379 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
11380 | if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait)) | |
11381 | wake_up(&ctx->sq_data->wait); | |
7e84e1c7 SG |
11382 | return 0; |
11383 | } | |
11384 | ||
fdecb662 | 11385 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 11386 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 PB |
11387 | unsigned nr_args) |
11388 | { | |
11389 | __u32 tmp; | |
11390 | int err; | |
11391 | ||
c3bdad02 PB |
11392 | if (up->resv) |
11393 | return -EINVAL; | |
98f0b3b4 PB |
11394 | if (check_add_overflow(up->offset, nr_args, &tmp)) |
11395 | return -EOVERFLOW; | |
11396 | err = io_rsrc_node_switch_start(ctx); | |
11397 | if (err) | |
11398 | return err; | |
11399 | ||
fdecb662 PB |
11400 | switch (type) { |
11401 | case IORING_RSRC_FILE: | |
98f0b3b4 | 11402 | return __io_sqe_files_update(ctx, up, nr_args); |
634d00df PB |
11403 | case IORING_RSRC_BUFFER: |
11404 | return __io_sqe_buffers_update(ctx, up, nr_args); | |
98f0b3b4 PB |
11405 | } |
11406 | return -EINVAL; | |
11407 | } | |
11408 | ||
c3bdad02 PB |
11409 | static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg, |
11410 | unsigned nr_args) | |
98f0b3b4 | 11411 | { |
c3bdad02 | 11412 | struct io_uring_rsrc_update2 up; |
98f0b3b4 PB |
11413 | |
11414 | if (!nr_args) | |
11415 | return -EINVAL; | |
c3bdad02 PB |
11416 | memset(&up, 0, sizeof(up)); |
11417 | if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update))) | |
11418 | return -EFAULT; | |
11419 | return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args); | |
11420 | } | |
11421 | ||
11422 | static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg, | |
992da01a | 11423 | unsigned size, unsigned type) |
c3bdad02 PB |
11424 | { |
11425 | struct io_uring_rsrc_update2 up; | |
11426 | ||
11427 | if (size != sizeof(up)) | |
11428 | return -EINVAL; | |
98f0b3b4 PB |
11429 | if (copy_from_user(&up, arg, sizeof(up))) |
11430 | return -EFAULT; | |
992da01a | 11431 | if (!up.nr || up.resv) |
98f0b3b4 | 11432 | return -EINVAL; |
992da01a | 11433 | return __io_register_rsrc_update(ctx, type, &up, up.nr); |
98f0b3b4 PB |
11434 | } |
11435 | ||
c072481d | 11436 | static __cold int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg, |
992da01a | 11437 | unsigned int size, unsigned int type) |
792e3582 PB |
11438 | { |
11439 | struct io_uring_rsrc_register rr; | |
11440 | ||
11441 | /* keep it extendible */ | |
11442 | if (size != sizeof(rr)) | |
11443 | return -EINVAL; | |
11444 | ||
11445 | memset(&rr, 0, sizeof(rr)); | |
11446 | if (copy_from_user(&rr, arg, size)) | |
11447 | return -EFAULT; | |
992da01a | 11448 | if (!rr.nr || rr.resv || rr.resv2) |
792e3582 PB |
11449 | return -EINVAL; |
11450 | ||
992da01a | 11451 | switch (type) { |
792e3582 PB |
11452 | case IORING_RSRC_FILE: |
11453 | return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data), | |
11454 | rr.nr, u64_to_user_ptr(rr.tags)); | |
634d00df PB |
11455 | case IORING_RSRC_BUFFER: |
11456 | return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data), | |
11457 | rr.nr, u64_to_user_ptr(rr.tags)); | |
792e3582 PB |
11458 | } |
11459 | return -EINVAL; | |
11460 | } | |
11461 | ||
c072481d PB |
11462 | static __cold int io_register_iowq_aff(struct io_ring_ctx *ctx, |
11463 | void __user *arg, unsigned len) | |
fe76421d JA |
11464 | { |
11465 | struct io_uring_task *tctx = current->io_uring; | |
11466 | cpumask_var_t new_mask; | |
11467 | int ret; | |
11468 | ||
11469 | if (!tctx || !tctx->io_wq) | |
11470 | return -EINVAL; | |
11471 | ||
11472 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) | |
11473 | return -ENOMEM; | |
11474 | ||
11475 | cpumask_clear(new_mask); | |
11476 | if (len > cpumask_size()) | |
11477 | len = cpumask_size(); | |
11478 | ||
11479 | if (copy_from_user(new_mask, arg, len)) { | |
11480 | free_cpumask_var(new_mask); | |
11481 | return -EFAULT; | |
11482 | } | |
11483 | ||
11484 | ret = io_wq_cpu_affinity(tctx->io_wq, new_mask); | |
11485 | free_cpumask_var(new_mask); | |
11486 | return ret; | |
11487 | } | |
11488 | ||
c072481d | 11489 | static __cold int io_unregister_iowq_aff(struct io_ring_ctx *ctx) |
fe76421d JA |
11490 | { |
11491 | struct io_uring_task *tctx = current->io_uring; | |
11492 | ||
11493 | if (!tctx || !tctx->io_wq) | |
11494 | return -EINVAL; | |
11495 | ||
11496 | return io_wq_cpu_affinity(tctx->io_wq, NULL); | |
11497 | } | |
11498 | ||
c072481d PB |
11499 | static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx, |
11500 | void __user *arg) | |
b22fa62a | 11501 | __must_hold(&ctx->uring_lock) |
2e480058 | 11502 | { |
b22fa62a | 11503 | struct io_tctx_node *node; |
fa84693b JA |
11504 | struct io_uring_task *tctx = NULL; |
11505 | struct io_sq_data *sqd = NULL; | |
2e480058 JA |
11506 | __u32 new_count[2]; |
11507 | int i, ret; | |
11508 | ||
2e480058 JA |
11509 | if (copy_from_user(new_count, arg, sizeof(new_count))) |
11510 | return -EFAULT; | |
11511 | for (i = 0; i < ARRAY_SIZE(new_count); i++) | |
11512 | if (new_count[i] > INT_MAX) | |
11513 | return -EINVAL; | |
11514 | ||
fa84693b JA |
11515 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
11516 | sqd = ctx->sq_data; | |
11517 | if (sqd) { | |
009ad9f0 JA |
11518 | /* |
11519 | * Observe the correct sqd->lock -> ctx->uring_lock | |
11520 | * ordering. Fine to drop uring_lock here, we hold | |
11521 | * a ref to the ctx. | |
11522 | */ | |
41d3a6bd | 11523 | refcount_inc(&sqd->refs); |
009ad9f0 | 11524 | mutex_unlock(&ctx->uring_lock); |
fa84693b | 11525 | mutex_lock(&sqd->lock); |
009ad9f0 | 11526 | mutex_lock(&ctx->uring_lock); |
41d3a6bd JA |
11527 | if (sqd->thread) |
11528 | tctx = sqd->thread->io_uring; | |
fa84693b JA |
11529 | } |
11530 | } else { | |
11531 | tctx = current->io_uring; | |
11532 | } | |
11533 | ||
e139a1ec | 11534 | BUILD_BUG_ON(sizeof(new_count) != sizeof(ctx->iowq_limits)); |
fa84693b | 11535 | |
bad119b9 PB |
11536 | for (i = 0; i < ARRAY_SIZE(new_count); i++) |
11537 | if (new_count[i]) | |
11538 | ctx->iowq_limits[i] = new_count[i]; | |
e139a1ec PB |
11539 | ctx->iowq_limits_set = true; |
11540 | ||
e139a1ec PB |
11541 | if (tctx && tctx->io_wq) { |
11542 | ret = io_wq_max_workers(tctx->io_wq, new_count); | |
11543 | if (ret) | |
11544 | goto err; | |
11545 | } else { | |
11546 | memset(new_count, 0, sizeof(new_count)); | |
11547 | } | |
fa84693b | 11548 | |
41d3a6bd | 11549 | if (sqd) { |
fa84693b | 11550 | mutex_unlock(&sqd->lock); |
41d3a6bd JA |
11551 | io_put_sq_data(sqd); |
11552 | } | |
2e480058 JA |
11553 | |
11554 | if (copy_to_user(arg, new_count, sizeof(new_count))) | |
11555 | return -EFAULT; | |
11556 | ||
b22fa62a PB |
11557 | /* that's it for SQPOLL, only the SQPOLL task creates requests */ |
11558 | if (sqd) | |
11559 | return 0; | |
11560 | ||
11561 | /* now propagate the restriction to all registered users */ | |
11562 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
11563 | struct io_uring_task *tctx = node->task->io_uring; | |
11564 | ||
11565 | if (WARN_ON_ONCE(!tctx->io_wq)) | |
11566 | continue; | |
11567 | ||
11568 | for (i = 0; i < ARRAY_SIZE(new_count); i++) | |
11569 | new_count[i] = ctx->iowq_limits[i]; | |
11570 | /* ignore errors, it always returns zero anyway */ | |
11571 | (void)io_wq_max_workers(tctx->io_wq, new_count); | |
11572 | } | |
2e480058 | 11573 | return 0; |
fa84693b | 11574 | err: |
41d3a6bd | 11575 | if (sqd) { |
fa84693b | 11576 | mutex_unlock(&sqd->lock); |
41d3a6bd JA |
11577 | io_put_sq_data(sqd); |
11578 | } | |
fa84693b | 11579 | return ret; |
2e480058 JA |
11580 | } |
11581 | ||
edafccee JA |
11582 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
11583 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
11584 | __releases(ctx->uring_lock) |
11585 | __acquires(ctx->uring_lock) | |
edafccee JA |
11586 | { |
11587 | int ret; | |
11588 | ||
35fa71a0 JA |
11589 | /* |
11590 | * We're inside the ring mutex, if the ref is already dying, then | |
11591 | * someone else killed the ctx or is already going through | |
11592 | * io_uring_register(). | |
11593 | */ | |
11594 | if (percpu_ref_is_dying(&ctx->refs)) | |
11595 | return -ENXIO; | |
11596 | ||
75c4021a PB |
11597 | if (ctx->restricted) { |
11598 | if (opcode >= IORING_REGISTER_LAST) | |
11599 | return -EINVAL; | |
11600 | opcode = array_index_nospec(opcode, IORING_REGISTER_LAST); | |
11601 | if (!test_bit(opcode, ctx->restrictions.register_op)) | |
11602 | return -EACCES; | |
11603 | } | |
11604 | ||
edafccee JA |
11605 | switch (opcode) { |
11606 | case IORING_REGISTER_BUFFERS: | |
634d00df | 11607 | ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL); |
edafccee JA |
11608 | break; |
11609 | case IORING_UNREGISTER_BUFFERS: | |
11610 | ret = -EINVAL; | |
11611 | if (arg || nr_args) | |
11612 | break; | |
0a96bbe4 | 11613 | ret = io_sqe_buffers_unregister(ctx); |
edafccee | 11614 | break; |
6b06314c | 11615 | case IORING_REGISTER_FILES: |
792e3582 | 11616 | ret = io_sqe_files_register(ctx, arg, nr_args, NULL); |
6b06314c JA |
11617 | break; |
11618 | case IORING_UNREGISTER_FILES: | |
11619 | ret = -EINVAL; | |
11620 | if (arg || nr_args) | |
11621 | break; | |
11622 | ret = io_sqe_files_unregister(ctx); | |
11623 | break; | |
c3a31e60 | 11624 | case IORING_REGISTER_FILES_UPDATE: |
c3bdad02 | 11625 | ret = io_register_files_update(ctx, arg, nr_args); |
c3a31e60 | 11626 | break; |
9b402849 JA |
11627 | case IORING_REGISTER_EVENTFD: |
11628 | ret = -EINVAL; | |
11629 | if (nr_args != 1) | |
11630 | break; | |
c75312dd UA |
11631 | ret = io_eventfd_register(ctx, arg, 0); |
11632 | break; | |
11633 | case IORING_REGISTER_EVENTFD_ASYNC: | |
11634 | ret = -EINVAL; | |
11635 | if (nr_args != 1) | |
f2842ab5 | 11636 | break; |
c75312dd | 11637 | ret = io_eventfd_register(ctx, arg, 1); |
9b402849 JA |
11638 | break; |
11639 | case IORING_UNREGISTER_EVENTFD: | |
11640 | ret = -EINVAL; | |
11641 | if (arg || nr_args) | |
11642 | break; | |
11643 | ret = io_eventfd_unregister(ctx); | |
11644 | break; | |
66f4af93 JA |
11645 | case IORING_REGISTER_PROBE: |
11646 | ret = -EINVAL; | |
11647 | if (!arg || nr_args > 256) | |
11648 | break; | |
11649 | ret = io_probe(ctx, arg, nr_args); | |
11650 | break; | |
071698e1 JA |
11651 | case IORING_REGISTER_PERSONALITY: |
11652 | ret = -EINVAL; | |
11653 | if (arg || nr_args) | |
11654 | break; | |
11655 | ret = io_register_personality(ctx); | |
11656 | break; | |
11657 | case IORING_UNREGISTER_PERSONALITY: | |
11658 | ret = -EINVAL; | |
11659 | if (arg) | |
11660 | break; | |
11661 | ret = io_unregister_personality(ctx, nr_args); | |
11662 | break; | |
7e84e1c7 SG |
11663 | case IORING_REGISTER_ENABLE_RINGS: |
11664 | ret = -EINVAL; | |
11665 | if (arg || nr_args) | |
11666 | break; | |
11667 | ret = io_register_enable_rings(ctx); | |
11668 | break; | |
21b55dbc SG |
11669 | case IORING_REGISTER_RESTRICTIONS: |
11670 | ret = io_register_restrictions(ctx, arg, nr_args); | |
11671 | break; | |
992da01a PB |
11672 | case IORING_REGISTER_FILES2: |
11673 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE); | |
11674 | break; | |
11675 | case IORING_REGISTER_FILES_UPDATE2: | |
11676 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
11677 | IORING_RSRC_FILE); | |
11678 | break; | |
11679 | case IORING_REGISTER_BUFFERS2: | |
11680 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER); | |
792e3582 | 11681 | break; |
992da01a PB |
11682 | case IORING_REGISTER_BUFFERS_UPDATE: |
11683 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
11684 | IORING_RSRC_BUFFER); | |
c3bdad02 | 11685 | break; |
fe76421d JA |
11686 | case IORING_REGISTER_IOWQ_AFF: |
11687 | ret = -EINVAL; | |
11688 | if (!arg || !nr_args) | |
11689 | break; | |
11690 | ret = io_register_iowq_aff(ctx, arg, nr_args); | |
11691 | break; | |
11692 | case IORING_UNREGISTER_IOWQ_AFF: | |
11693 | ret = -EINVAL; | |
11694 | if (arg || nr_args) | |
11695 | break; | |
11696 | ret = io_unregister_iowq_aff(ctx); | |
11697 | break; | |
2e480058 JA |
11698 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
11699 | ret = -EINVAL; | |
11700 | if (!arg || nr_args != 2) | |
11701 | break; | |
11702 | ret = io_register_iowq_max_workers(ctx, arg); | |
11703 | break; | |
e7a6c00d JA |
11704 | case IORING_REGISTER_RING_FDS: |
11705 | ret = io_ringfd_register(ctx, arg, nr_args); | |
11706 | break; | |
11707 | case IORING_UNREGISTER_RING_FDS: | |
11708 | ret = io_ringfd_unregister(ctx, arg, nr_args); | |
11709 | break; | |
edafccee JA |
11710 | default: |
11711 | ret = -EINVAL; | |
11712 | break; | |
11713 | } | |
11714 | ||
edafccee JA |
11715 | return ret; |
11716 | } | |
11717 | ||
11718 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
11719 | void __user *, arg, unsigned int, nr_args) | |
11720 | { | |
11721 | struct io_ring_ctx *ctx; | |
11722 | long ret = -EBADF; | |
11723 | struct fd f; | |
11724 | ||
11725 | f = fdget(fd); | |
11726 | if (!f.file) | |
11727 | return -EBADF; | |
11728 | ||
11729 | ret = -EOPNOTSUPP; | |
11730 | if (f.file->f_op != &io_uring_fops) | |
11731 | goto out_fput; | |
11732 | ||
11733 | ctx = f.file->private_data; | |
11734 | ||
b6c23dd5 PB |
11735 | io_run_task_work(); |
11736 | ||
edafccee JA |
11737 | mutex_lock(&ctx->uring_lock); |
11738 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
11739 | mutex_unlock(&ctx->uring_lock); | |
2757be22 | 11740 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, ret); |
edafccee JA |
11741 | out_fput: |
11742 | fdput(f); | |
11743 | return ret; | |
11744 | } | |
11745 | ||
2b188cc1 JA |
11746 | static int __init io_uring_init(void) |
11747 | { | |
d7f62e82 SM |
11748 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
11749 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
11750 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
11751 | } while (0) | |
11752 | ||
11753 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
11754 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
11755 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
11756 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
11757 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
11758 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
11759 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
11760 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
11761 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
11762 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 11763 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
11764 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
11765 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
11766 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
11767 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
11768 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
11769 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
11770 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
11771 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
11772 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
11773 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
11774 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
11775 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
11776 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
11777 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
11778 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 11779 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
11780 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
11781 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
16340eab | 11782 | BUILD_BUG_SQE_ELEM(40, __u16, buf_group); |
d7f62e82 | 11783 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
7d67af2c | 11784 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
b9445598 | 11785 | BUILD_BUG_SQE_ELEM(44, __u32, file_index); |
d7f62e82 | 11786 | |
b0d658ec PB |
11787 | BUILD_BUG_ON(sizeof(struct io_uring_files_update) != |
11788 | sizeof(struct io_uring_rsrc_update)); | |
11789 | BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) > | |
11790 | sizeof(struct io_uring_rsrc_update2)); | |
90499ad0 PB |
11791 | |
11792 | /* ->buf_index is u16 */ | |
11793 | BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16)); | |
11794 | ||
b0d658ec PB |
11795 | /* should fit into one byte */ |
11796 | BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8)); | |
68fe256a PB |
11797 | BUILD_BUG_ON(SQE_COMMON_FLAGS >= (1 << 8)); |
11798 | BUILD_BUG_ON((SQE_VALID_FLAGS | SQE_COMMON_FLAGS) != SQE_VALID_FLAGS); | |
b0d658ec | 11799 | |
d3656344 | 11800 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
32c2d33e | 11801 | BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof(int)); |
16340eab | 11802 | |
91f245d5 JA |
11803 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
11804 | SLAB_ACCOUNT); | |
2b188cc1 JA |
11805 | return 0; |
11806 | }; | |
11807 | __initcall(io_uring_init); |