Commit | Line | Data |
---|---|---|
2b188cc1 JA |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * Shared application/kernel submission and completion ring pairs, for | |
4 | * supporting fast/efficient IO. | |
5 | * | |
6 | * A note on the read/write ordering memory barriers that are matched between | |
1e84b97b SB |
7 | * the application and kernel side. |
8 | * | |
9 | * After the application reads the CQ ring tail, it must use an | |
10 | * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses | |
11 | * before writing the tail (using smp_load_acquire to read the tail will | |
12 | * do). It also needs a smp_mb() before updating CQ head (ordering the | |
13 | * entry load(s) with the head store), pairing with an implicit barrier | |
d068b506 | 14 | * through a control-dependency in io_get_cqe (smp_store_release to |
1e84b97b SB |
15 | * store head will do). Failure to do so could lead to reading invalid |
16 | * CQ entries. | |
17 | * | |
18 | * Likewise, the application must use an appropriate smp_wmb() before | |
19 | * writing the SQ tail (ordering SQ entry stores with the tail store), | |
20 | * which pairs with smp_load_acquire in io_get_sqring (smp_store_release | |
21 | * to store the tail will do). And it needs a barrier ordering the SQ | |
22 | * head load before writing new SQ entries (smp_load_acquire to read | |
23 | * head will do). | |
24 | * | |
25 | * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application | |
26 | * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after* | |
27 | * updating the SQ tail; a full memory barrier smp_mb() is needed | |
28 | * between. | |
2b188cc1 JA |
29 | * |
30 | * Also see the examples in the liburing library: | |
31 | * | |
32 | * git://git.kernel.dk/liburing | |
33 | * | |
34 | * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens | |
35 | * from data shared between the kernel and application. This is done both | |
36 | * for ordering purposes, but also to ensure that once a value is loaded from | |
37 | * data that the application could potentially modify, it remains stable. | |
38 | * | |
39 | * Copyright (C) 2018-2019 Jens Axboe | |
c992fe29 | 40 | * Copyright (c) 2018-2019 Christoph Hellwig |
2b188cc1 JA |
41 | */ |
42 | #include <linux/kernel.h> | |
43 | #include <linux/init.h> | |
44 | #include <linux/errno.h> | |
45 | #include <linux/syscalls.h> | |
46 | #include <linux/compat.h> | |
52de1fe1 | 47 | #include <net/compat.h> |
2b188cc1 JA |
48 | #include <linux/refcount.h> |
49 | #include <linux/uio.h> | |
6b47ee6e | 50 | #include <linux/bits.h> |
2b188cc1 JA |
51 | |
52 | #include <linux/sched/signal.h> | |
53 | #include <linux/fs.h> | |
54 | #include <linux/file.h> | |
55 | #include <linux/fdtable.h> | |
56 | #include <linux/mm.h> | |
57 | #include <linux/mman.h> | |
2b188cc1 JA |
58 | #include <linux/percpu.h> |
59 | #include <linux/slab.h> | |
edce22e1 | 60 | #include <linux/blk-mq.h> |
edafccee | 61 | #include <linux/bvec.h> |
2b188cc1 JA |
62 | #include <linux/net.h> |
63 | #include <net/sock.h> | |
64 | #include <net/af_unix.h> | |
6b06314c | 65 | #include <net/scm.h> |
2b188cc1 JA |
66 | #include <linux/anon_inodes.h> |
67 | #include <linux/sched/mm.h> | |
68 | #include <linux/uaccess.h> | |
69 | #include <linux/nospec.h> | |
edafccee JA |
70 | #include <linux/sizes.h> |
71 | #include <linux/hugetlb.h> | |
aa4c3967 | 72 | #include <linux/highmem.h> |
15b71abe JA |
73 | #include <linux/namei.h> |
74 | #include <linux/fsnotify.h> | |
4840e418 | 75 | #include <linux/fadvise.h> |
3e4827b0 | 76 | #include <linux/eventpoll.h> |
7d67af2c | 77 | #include <linux/splice.h> |
b41e9852 | 78 | #include <linux/task_work.h> |
bcf5a063 | 79 | #include <linux/pagemap.h> |
0f212204 | 80 | #include <linux/io_uring.h> |
5bd2182d | 81 | #include <linux/audit.h> |
cdc1404a | 82 | #include <linux/security.h> |
2b188cc1 | 83 | |
c826bd7a DD |
84 | #define CREATE_TRACE_POINTS |
85 | #include <trace/events/io_uring.h> | |
86 | ||
2b188cc1 JA |
87 | #include <uapi/linux/io_uring.h> |
88 | ||
89 | #include "internal.h" | |
561fb04a | 90 | #include "io-wq.h" |
2b188cc1 | 91 | |
5277deaa | 92 | #define IORING_MAX_ENTRIES 32768 |
33a107f0 | 93 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
4ce8ad95 | 94 | #define IORING_SQPOLL_CAP_ENTRIES_VALUE 8 |
65e19f54 | 95 | |
187f08c1 | 96 | /* only define max */ |
042b0d85 | 97 | #define IORING_MAX_FIXED_FILES (1U << 15) |
21b55dbc SG |
98 | #define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \ |
99 | IORING_REGISTER_LAST + IORING_OP_LAST) | |
2b188cc1 | 100 | |
187f08c1 | 101 | #define IO_RSRC_TAG_TABLE_SHIFT (PAGE_SHIFT - 3) |
2d091d62 PB |
102 | #define IO_RSRC_TAG_TABLE_MAX (1U << IO_RSRC_TAG_TABLE_SHIFT) |
103 | #define IO_RSRC_TAG_TABLE_MASK (IO_RSRC_TAG_TABLE_MAX - 1) | |
104 | ||
489809e2 PB |
105 | #define IORING_MAX_REG_BUFFERS (1U << 14) |
106 | ||
68fe256a PB |
107 | #define SQE_COMMON_FLAGS (IOSQE_FIXED_FILE | IOSQE_IO_LINK | \ |
108 | IOSQE_IO_HARDLINK | IOSQE_ASYNC) | |
109 | ||
5562a8d7 PB |
110 | #define SQE_VALID_FLAGS (SQE_COMMON_FLAGS | IOSQE_BUFFER_SELECT | \ |
111 | IOSQE_IO_DRAIN | IOSQE_CQE_SKIP_SUCCESS) | |
68fe256a | 112 | |
c854357b | 113 | #define IO_REQ_CLEAN_FLAGS (REQ_F_BUFFER_SELECTED | REQ_F_NEED_CLEANUP | \ |
d5361233 | 114 | REQ_F_POLLED | REQ_F_CREDS | REQ_F_ASYNC_DATA) |
b16fed66 | 115 | |
09899b19 PB |
116 | #define IO_TCTX_REFS_CACHE_NR (1U << 10) |
117 | ||
2b188cc1 JA |
118 | struct io_uring { |
119 | u32 head ____cacheline_aligned_in_smp; | |
120 | u32 tail ____cacheline_aligned_in_smp; | |
121 | }; | |
122 | ||
1e84b97b | 123 | /* |
75b28aff HV |
124 | * This data is shared with the application through the mmap at offsets |
125 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. | |
1e84b97b SB |
126 | * |
127 | * The offsets to the member fields are published through struct | |
128 | * io_sqring_offsets when calling io_uring_setup. | |
129 | */ | |
75b28aff | 130 | struct io_rings { |
1e84b97b SB |
131 | /* |
132 | * Head and tail offsets into the ring; the offsets need to be | |
133 | * masked to get valid indices. | |
134 | * | |
75b28aff HV |
135 | * The kernel controls head of the sq ring and the tail of the cq ring, |
136 | * and the application controls tail of the sq ring and the head of the | |
137 | * cq ring. | |
1e84b97b | 138 | */ |
75b28aff | 139 | struct io_uring sq, cq; |
1e84b97b | 140 | /* |
75b28aff | 141 | * Bitmasks to apply to head and tail offsets (constant, equals |
1e84b97b SB |
142 | * ring_entries - 1) |
143 | */ | |
75b28aff HV |
144 | u32 sq_ring_mask, cq_ring_mask; |
145 | /* Ring sizes (constant, power of 2) */ | |
146 | u32 sq_ring_entries, cq_ring_entries; | |
1e84b97b SB |
147 | /* |
148 | * Number of invalid entries dropped by the kernel due to | |
149 | * invalid index stored in array | |
150 | * | |
151 | * Written by the kernel, shouldn't be modified by the | |
152 | * application (i.e. get number of "new events" by comparing to | |
153 | * cached value). | |
154 | * | |
155 | * After a new SQ head value was read by the application this | |
156 | * counter includes all submissions that were dropped reaching | |
157 | * the new SQ head (and possibly more). | |
158 | */ | |
75b28aff | 159 | u32 sq_dropped; |
1e84b97b | 160 | /* |
0d9b5b3a | 161 | * Runtime SQ flags |
1e84b97b SB |
162 | * |
163 | * Written by the kernel, shouldn't be modified by the | |
164 | * application. | |
165 | * | |
166 | * The application needs a full memory barrier before checking | |
167 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. | |
168 | */ | |
75b28aff | 169 | u32 sq_flags; |
0d9b5b3a SG |
170 | /* |
171 | * Runtime CQ flags | |
172 | * | |
173 | * Written by the application, shouldn't be modified by the | |
174 | * kernel. | |
175 | */ | |
fe7e3257 | 176 | u32 cq_flags; |
1e84b97b SB |
177 | /* |
178 | * Number of completion events lost because the queue was full; | |
179 | * this should be avoided by the application by making sure | |
0b4295b5 | 180 | * there are not more requests pending than there is space in |
1e84b97b SB |
181 | * the completion queue. |
182 | * | |
183 | * Written by the kernel, shouldn't be modified by the | |
184 | * application (i.e. get number of "new events" by comparing to | |
185 | * cached value). | |
186 | * | |
187 | * As completion events come in out of order this counter is not | |
188 | * ordered with any other data. | |
189 | */ | |
75b28aff | 190 | u32 cq_overflow; |
1e84b97b SB |
191 | /* |
192 | * Ring buffer of completion events. | |
193 | * | |
194 | * The kernel writes completion events fresh every time they are | |
195 | * produced, so the application is allowed to modify pending | |
196 | * entries. | |
197 | */ | |
75b28aff | 198 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
2b188cc1 JA |
199 | }; |
200 | ||
45d189c6 | 201 | enum io_uring_cmd_flags { |
51aac424 | 202 | IO_URING_F_COMPLETE_DEFER = 1, |
3b44b371 | 203 | IO_URING_F_UNLOCKED = 2, |
51aac424 PB |
204 | /* int's last bit, sign checks are usually faster than a bit test */ |
205 | IO_URING_F_NONBLOCK = INT_MIN, | |
45d189c6 PB |
206 | }; |
207 | ||
edafccee JA |
208 | struct io_mapped_ubuf { |
209 | u64 ubuf; | |
4751f53d | 210 | u64 ubuf_end; |
edafccee | 211 | unsigned int nr_bvecs; |
de293938 | 212 | unsigned long acct_pages; |
41edf1a5 | 213 | struct bio_vec bvec[]; |
edafccee JA |
214 | }; |
215 | ||
50238531 BM |
216 | struct io_ring_ctx; |
217 | ||
6c2450ae PB |
218 | struct io_overflow_cqe { |
219 | struct io_uring_cqe cqe; | |
220 | struct list_head list; | |
221 | }; | |
222 | ||
a04b0ac0 PB |
223 | struct io_fixed_file { |
224 | /* file * with additional FFS_* flags */ | |
225 | unsigned long file_ptr; | |
226 | }; | |
227 | ||
269bbe5f BM |
228 | struct io_rsrc_put { |
229 | struct list_head list; | |
b60c8dce | 230 | u64 tag; |
50238531 BM |
231 | union { |
232 | void *rsrc; | |
233 | struct file *file; | |
bd54b6fe | 234 | struct io_mapped_ubuf *buf; |
50238531 | 235 | }; |
269bbe5f BM |
236 | }; |
237 | ||
aeca241b | 238 | struct io_file_table { |
042b0d85 | 239 | struct io_fixed_file *files; |
31b51510 JA |
240 | }; |
241 | ||
b895c9a6 | 242 | struct io_rsrc_node { |
05589553 XW |
243 | struct percpu_ref refs; |
244 | struct list_head node; | |
269bbe5f | 245 | struct list_head rsrc_list; |
b895c9a6 | 246 | struct io_rsrc_data *rsrc_data; |
4a38aed2 | 247 | struct llist_node llist; |
e297822b | 248 | bool done; |
05589553 XW |
249 | }; |
250 | ||
40ae0ff7 PB |
251 | typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc); |
252 | ||
b895c9a6 | 253 | struct io_rsrc_data { |
05f3fb3c JA |
254 | struct io_ring_ctx *ctx; |
255 | ||
2d091d62 PB |
256 | u64 **tags; |
257 | unsigned int nr; | |
40ae0ff7 | 258 | rsrc_put_fn *do_put; |
3e942498 | 259 | atomic_t refs; |
05f3fb3c | 260 | struct completion done; |
8bad28d8 | 261 | bool quiesce; |
05f3fb3c JA |
262 | }; |
263 | ||
dbc7d452 JA |
264 | struct io_buffer_list { |
265 | struct list_head list; | |
266 | struct list_head buf_list; | |
267 | __u16 bgid; | |
268 | }; | |
269 | ||
5a2e745d JA |
270 | struct io_buffer { |
271 | struct list_head list; | |
272 | __u64 addr; | |
d1f82808 | 273 | __u32 len; |
5a2e745d | 274 | __u16 bid; |
b1c62645 | 275 | __u16 bgid; |
5a2e745d JA |
276 | }; |
277 | ||
21b55dbc SG |
278 | struct io_restriction { |
279 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); | |
280 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); | |
281 | u8 sqe_flags_allowed; | |
282 | u8 sqe_flags_required; | |
7e84e1c7 | 283 | bool registered; |
21b55dbc SG |
284 | }; |
285 | ||
37d1e2e3 JA |
286 | enum { |
287 | IO_SQ_THREAD_SHOULD_STOP = 0, | |
288 | IO_SQ_THREAD_SHOULD_PARK, | |
289 | }; | |
290 | ||
534ca6d6 JA |
291 | struct io_sq_data { |
292 | refcount_t refs; | |
9e138a48 | 293 | atomic_t park_pending; |
09a6f4ef | 294 | struct mutex lock; |
69fb2131 JA |
295 | |
296 | /* ctx's that are using this sqd */ | |
297 | struct list_head ctx_list; | |
69fb2131 | 298 | |
534ca6d6 JA |
299 | struct task_struct *thread; |
300 | struct wait_queue_head wait; | |
08369246 XW |
301 | |
302 | unsigned sq_thread_idle; | |
37d1e2e3 JA |
303 | int sq_cpu; |
304 | pid_t task_pid; | |
5c2469e0 | 305 | pid_t task_tgid; |
37d1e2e3 JA |
306 | |
307 | unsigned long state; | |
37d1e2e3 | 308 | struct completion exited; |
534ca6d6 JA |
309 | }; |
310 | ||
6dd0be1e | 311 | #define IO_COMPL_BATCH 32 |
6ff119a6 | 312 | #define IO_REQ_CACHE_SIZE 32 |
bf019da7 | 313 | #define IO_REQ_ALLOC_BATCH 8 |
258b29a9 | 314 | |
a1ab7b35 PB |
315 | struct io_submit_link { |
316 | struct io_kiocb *head; | |
317 | struct io_kiocb *last; | |
318 | }; | |
319 | ||
258b29a9 | 320 | struct io_submit_state { |
5a158c6b PB |
321 | /* inline/task_work completion list, under ->uring_lock */ |
322 | struct io_wq_work_node free_list; | |
323 | /* batch completion logic */ | |
324 | struct io_wq_work_list compl_reqs; | |
a1ab7b35 | 325 | struct io_submit_link link; |
258b29a9 | 326 | |
258b29a9 | 327 | bool plug_started; |
4b628aeb | 328 | bool need_plug; |
3d4aeb9f | 329 | bool flush_cqes; |
5ca7a8b3 | 330 | unsigned short submit_nr; |
5a158c6b | 331 | struct blk_plug plug; |
258b29a9 PB |
332 | }; |
333 | ||
77bc59b4 UA |
334 | struct io_ev_fd { |
335 | struct eventfd_ctx *cq_ev_fd; | |
c75312dd | 336 | unsigned int eventfd_async: 1; |
77bc59b4 UA |
337 | struct rcu_head rcu; |
338 | }; | |
339 | ||
dbc7d452 JA |
340 | #define IO_BUFFERS_HASH_BITS 5 |
341 | ||
2b188cc1 | 342 | struct io_ring_ctx { |
b52ecf8c | 343 | /* const or read-mostly hot data */ |
2b188cc1 JA |
344 | struct { |
345 | struct percpu_ref refs; | |
2b188cc1 | 346 | |
b52ecf8c | 347 | struct io_rings *rings; |
2b188cc1 | 348 | unsigned int flags; |
e1d85334 | 349 | unsigned int compat: 1; |
e1d85334 | 350 | unsigned int drain_next: 1; |
21b55dbc | 351 | unsigned int restricted: 1; |
f18ee4cf | 352 | unsigned int off_timeout_used: 1; |
10c66904 | 353 | unsigned int drain_active: 1; |
5562a8d7 | 354 | unsigned int drain_disabled: 1; |
9aa8dfde | 355 | unsigned int has_evfd: 1; |
b52ecf8c | 356 | } ____cacheline_aligned_in_smp; |
2b188cc1 | 357 | |
7f1129d2 | 358 | /* submission data */ |
b52ecf8c | 359 | struct { |
0499e582 PB |
360 | struct mutex uring_lock; |
361 | ||
75b28aff HV |
362 | /* |
363 | * Ring buffer of indices into array of io_uring_sqe, which is | |
364 | * mmapped by the application using the IORING_OFF_SQES offset. | |
365 | * | |
366 | * This indirection could e.g. be used to assign fixed | |
367 | * io_uring_sqe entries to operations and only submit them to | |
368 | * the queue when needed. | |
369 | * | |
370 | * The kernel modifies neither the indices array nor the entries | |
371 | * array. | |
372 | */ | |
373 | u32 *sq_array; | |
c7af47cf | 374 | struct io_uring_sqe *sq_sqes; |
2b188cc1 JA |
375 | unsigned cached_sq_head; |
376 | unsigned sq_entries; | |
de0617e4 | 377 | struct list_head defer_list; |
7f1129d2 PB |
378 | |
379 | /* | |
380 | * Fixed resources fast path, should be accessed only under | |
381 | * uring_lock, and updated through io_uring_register(2) | |
382 | */ | |
383 | struct io_rsrc_node *rsrc_node; | |
ab409402 | 384 | int rsrc_cached_refs; |
7f1129d2 PB |
385 | struct io_file_table file_table; |
386 | unsigned nr_user_files; | |
387 | unsigned nr_user_bufs; | |
388 | struct io_mapped_ubuf **user_bufs; | |
389 | ||
390 | struct io_submit_state submit_state; | |
5262f567 | 391 | struct list_head timeout_list; |
ef9dd637 | 392 | struct list_head ltimeout_list; |
1d7bb1d5 | 393 | struct list_head cq_overflow_list; |
dbc7d452 | 394 | struct list_head *io_buffers; |
cc3cec83 | 395 | struct list_head io_buffers_cache; |
4d9237e3 | 396 | struct list_head apoll_cache; |
7f1129d2 PB |
397 | struct xarray personalities; |
398 | u32 pers_next; | |
399 | unsigned sq_thread_idle; | |
2b188cc1 JA |
400 | } ____cacheline_aligned_in_smp; |
401 | ||
d0acdee2 | 402 | /* IRQ completion list, under ->completion_lock */ |
c2b6c6bc | 403 | struct io_wq_work_list locked_free_list; |
d0acdee2 | 404 | unsigned int locked_free_nr; |
3c1a2ead | 405 | |
7c30f36a | 406 | const struct cred *sq_creds; /* cred used for __io_sq_thread() */ |
534ca6d6 JA |
407 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
408 | ||
90554200 | 409 | struct wait_queue_head sqo_sq_wait; |
69fb2131 | 410 | struct list_head sqd_list; |
75b28aff | 411 | |
5ed7a37d PB |
412 | unsigned long check_cq_overflow; |
413 | ||
206aefde JA |
414 | struct { |
415 | unsigned cached_cq_tail; | |
416 | unsigned cq_entries; | |
77bc59b4 | 417 | struct io_ev_fd __rcu *io_ev_fd; |
0499e582 PB |
418 | struct wait_queue_head cq_wait; |
419 | unsigned cq_extra; | |
420 | atomic_t cq_timeouts; | |
0499e582 | 421 | unsigned cq_last_tm_flush; |
206aefde | 422 | } ____cacheline_aligned_in_smp; |
2b188cc1 | 423 | |
2b188cc1 JA |
424 | struct { |
425 | spinlock_t completion_lock; | |
e94f141b | 426 | |
89850fce JA |
427 | spinlock_t timeout_lock; |
428 | ||
def596e9 | 429 | /* |
540e32a0 | 430 | * ->iopoll_list is protected by the ctx->uring_lock for |
def596e9 JA |
431 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
432 | * For SQPOLL, only the single threaded io_sq_thread() will | |
433 | * manipulate the list, hence no extra locking is needed there. | |
434 | */ | |
5eef4e87 | 435 | struct io_wq_work_list iopoll_list; |
78076bb6 JA |
436 | struct hlist_head *cancel_hash; |
437 | unsigned cancel_hash_bits; | |
915b3dde | 438 | bool poll_multi_queue; |
cc3cec83 JA |
439 | |
440 | struct list_head io_buffers_comp; | |
2b188cc1 | 441 | } ____cacheline_aligned_in_smp; |
85faa7b8 | 442 | |
21b55dbc | 443 | struct io_restriction restrictions; |
3c1a2ead | 444 | |
b13a8918 PB |
445 | /* slow path rsrc auxilary data, used by update/register */ |
446 | struct { | |
447 | struct io_rsrc_node *rsrc_backup_node; | |
448 | struct io_mapped_ubuf *dummy_ubuf; | |
449 | struct io_rsrc_data *file_data; | |
450 | struct io_rsrc_data *buf_data; | |
451 | ||
452 | struct delayed_work rsrc_put_work; | |
453 | struct llist_head rsrc_put_llist; | |
454 | struct list_head rsrc_ref_list; | |
455 | spinlock_t rsrc_ref_lock; | |
cc3cec83 JA |
456 | |
457 | struct list_head io_buffers_pages; | |
b13a8918 PB |
458 | }; |
459 | ||
3c1a2ead | 460 | /* Keep this last, we don't need it for the fast path */ |
b986af7e PB |
461 | struct { |
462 | #if defined(CONFIG_UNIX) | |
463 | struct socket *ring_sock; | |
464 | #endif | |
465 | /* hashed buffered write serialization */ | |
466 | struct io_wq_hash *hash_map; | |
467 | ||
468 | /* Only used for accounting purposes */ | |
469 | struct user_struct *user; | |
470 | struct mm_struct *mm_account; | |
471 | ||
472 | /* ctx exit and cancelation */ | |
9011bf9a PB |
473 | struct llist_head fallback_llist; |
474 | struct delayed_work fallback_work; | |
b986af7e PB |
475 | struct work_struct exit_work; |
476 | struct list_head tctx_list; | |
477 | struct completion ref_comp; | |
e139a1ec PB |
478 | u32 iowq_limits[2]; |
479 | bool iowq_limits_set; | |
b986af7e | 480 | }; |
2b188cc1 JA |
481 | }; |
482 | ||
e7a6c00d JA |
483 | /* |
484 | * Arbitrary limit, can be raised if need be | |
485 | */ | |
486 | #define IO_RINGFD_REG_MAX 16 | |
487 | ||
53e043b2 SM |
488 | struct io_uring_task { |
489 | /* submission side */ | |
09899b19 | 490 | int cached_refs; |
53e043b2 SM |
491 | struct xarray xa; |
492 | struct wait_queue_head wait; | |
ee53fb2b SM |
493 | const struct io_ring_ctx *last; |
494 | struct io_wq *io_wq; | |
53e043b2 SM |
495 | struct percpu_counter inflight; |
496 | atomic_t in_idle; | |
53e043b2 SM |
497 | |
498 | spinlock_t task_lock; | |
499 | struct io_wq_work_list task_list; | |
4813c377 | 500 | struct io_wq_work_list prior_task_list; |
53e043b2 | 501 | struct callback_head task_work; |
e7a6c00d | 502 | struct file **registered_rings; |
6294f368 | 503 | bool task_running; |
53e043b2 SM |
504 | }; |
505 | ||
09bb8394 JA |
506 | /* |
507 | * First field must be the file pointer in all the | |
508 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> | |
509 | */ | |
221c5eb2 JA |
510 | struct io_poll_iocb { |
511 | struct file *file; | |
018043be | 512 | struct wait_queue_head *head; |
221c5eb2 | 513 | __poll_t events; |
392edb45 | 514 | struct wait_queue_entry wait; |
221c5eb2 JA |
515 | }; |
516 | ||
9d805892 | 517 | struct io_poll_update { |
018043be | 518 | struct file *file; |
9d805892 PB |
519 | u64 old_user_data; |
520 | u64 new_user_data; | |
521 | __poll_t events; | |
b69de288 JA |
522 | bool update_events; |
523 | bool update_user_data; | |
018043be PB |
524 | }; |
525 | ||
b5dba59e JA |
526 | struct io_close { |
527 | struct file *file; | |
b5dba59e | 528 | int fd; |
7df778be | 529 | u32 file_slot; |
b5dba59e JA |
530 | }; |
531 | ||
ad8a48ac JA |
532 | struct io_timeout_data { |
533 | struct io_kiocb *req; | |
534 | struct hrtimer timer; | |
535 | struct timespec64 ts; | |
536 | enum hrtimer_mode mode; | |
50c1df2b | 537 | u32 flags; |
ad8a48ac JA |
538 | }; |
539 | ||
8ed8d3c3 JA |
540 | struct io_accept { |
541 | struct file *file; | |
542 | struct sockaddr __user *addr; | |
543 | int __user *addr_len; | |
544 | int flags; | |
aaa4db12 | 545 | u32 file_slot; |
09952e3e | 546 | unsigned long nofile; |
8ed8d3c3 JA |
547 | }; |
548 | ||
549 | struct io_sync { | |
550 | struct file *file; | |
551 | loff_t len; | |
552 | loff_t off; | |
553 | int flags; | |
d63d1b5e | 554 | int mode; |
8ed8d3c3 JA |
555 | }; |
556 | ||
fbf23849 JA |
557 | struct io_cancel { |
558 | struct file *file; | |
559 | u64 addr; | |
560 | }; | |
561 | ||
b29472ee JA |
562 | struct io_timeout { |
563 | struct file *file; | |
bfe68a22 PB |
564 | u32 off; |
565 | u32 target_seq; | |
135fcde8 | 566 | struct list_head list; |
90cd7e42 PB |
567 | /* head of the link, used by linked timeouts only */ |
568 | struct io_kiocb *head; | |
89b263f6 JA |
569 | /* for linked completions */ |
570 | struct io_kiocb *prev; | |
b29472ee JA |
571 | }; |
572 | ||
0bdf7a2d PB |
573 | struct io_timeout_rem { |
574 | struct file *file; | |
575 | u64 addr; | |
9c8e11b3 PB |
576 | |
577 | /* timeout update */ | |
578 | struct timespec64 ts; | |
579 | u32 flags; | |
f1042b6c | 580 | bool ltimeout; |
0bdf7a2d PB |
581 | }; |
582 | ||
9adbd45d JA |
583 | struct io_rw { |
584 | /* NOTE: kiocb has the file as the first member, so don't do it here */ | |
585 | struct kiocb kiocb; | |
586 | u64 addr; | |
584b0180 JA |
587 | u32 len; |
588 | u32 flags; | |
9adbd45d JA |
589 | }; |
590 | ||
3fbb51c1 JA |
591 | struct io_connect { |
592 | struct file *file; | |
593 | struct sockaddr __user *addr; | |
594 | int addr_len; | |
595 | }; | |
596 | ||
e47293fd JA |
597 | struct io_sr_msg { |
598 | struct file *file; | |
fddaface | 599 | union { |
4af3417a PB |
600 | struct compat_msghdr __user *umsg_compat; |
601 | struct user_msghdr __user *umsg; | |
602 | void __user *buf; | |
fddaface | 603 | }; |
e47293fd | 604 | int msg_flags; |
bcda7baa | 605 | int bgid; |
fddaface | 606 | size_t len; |
7ba89d2a | 607 | size_t done_io; |
e47293fd JA |
608 | }; |
609 | ||
15b71abe JA |
610 | struct io_open { |
611 | struct file *file; | |
612 | int dfd; | |
b9445598 | 613 | u32 file_slot; |
15b71abe | 614 | struct filename *filename; |
c12cedf2 | 615 | struct open_how how; |
4022e7af | 616 | unsigned long nofile; |
15b71abe JA |
617 | }; |
618 | ||
269bbe5f | 619 | struct io_rsrc_update { |
05f3fb3c JA |
620 | struct file *file; |
621 | u64 arg; | |
622 | u32 nr_args; | |
623 | u32 offset; | |
624 | }; | |
625 | ||
4840e418 JA |
626 | struct io_fadvise { |
627 | struct file *file; | |
628 | u64 offset; | |
629 | u32 len; | |
630 | u32 advice; | |
631 | }; | |
632 | ||
c1ca757b JA |
633 | struct io_madvise { |
634 | struct file *file; | |
635 | u64 addr; | |
636 | u32 len; | |
637 | u32 advice; | |
638 | }; | |
639 | ||
3e4827b0 JA |
640 | struct io_epoll { |
641 | struct file *file; | |
642 | int epfd; | |
643 | int op; | |
644 | int fd; | |
645 | struct epoll_event event; | |
e47293fd JA |
646 | }; |
647 | ||
7d67af2c PB |
648 | struct io_splice { |
649 | struct file *file_out; | |
7d67af2c PB |
650 | loff_t off_out; |
651 | loff_t off_in; | |
652 | u64 len; | |
a3e4bc23 | 653 | int splice_fd_in; |
7d67af2c PB |
654 | unsigned int flags; |
655 | }; | |
656 | ||
ddf0322d JA |
657 | struct io_provide_buf { |
658 | struct file *file; | |
659 | __u64 addr; | |
38134ada | 660 | __u32 len; |
ddf0322d JA |
661 | __u32 bgid; |
662 | __u16 nbufs; | |
663 | __u16 bid; | |
664 | }; | |
665 | ||
1d9e1288 BM |
666 | struct io_statx { |
667 | struct file *file; | |
668 | int dfd; | |
669 | unsigned int mask; | |
670 | unsigned int flags; | |
1b6fe6e0 | 671 | struct filename *filename; |
1d9e1288 BM |
672 | struct statx __user *buffer; |
673 | }; | |
674 | ||
36f4fa68 JA |
675 | struct io_shutdown { |
676 | struct file *file; | |
677 | int how; | |
678 | }; | |
679 | ||
80a261fd JA |
680 | struct io_rename { |
681 | struct file *file; | |
682 | int old_dfd; | |
683 | int new_dfd; | |
684 | struct filename *oldpath; | |
685 | struct filename *newpath; | |
686 | int flags; | |
687 | }; | |
688 | ||
14a1143b JA |
689 | struct io_unlink { |
690 | struct file *file; | |
691 | int dfd; | |
692 | int flags; | |
693 | struct filename *filename; | |
694 | }; | |
695 | ||
e34a02dc DK |
696 | struct io_mkdir { |
697 | struct file *file; | |
698 | int dfd; | |
699 | umode_t mode; | |
700 | struct filename *filename; | |
701 | }; | |
702 | ||
7a8721f8 DK |
703 | struct io_symlink { |
704 | struct file *file; | |
705 | int new_dfd; | |
706 | struct filename *oldpath; | |
707 | struct filename *newpath; | |
708 | }; | |
709 | ||
cf30da90 DK |
710 | struct io_hardlink { |
711 | struct file *file; | |
712 | int old_dfd; | |
713 | int new_dfd; | |
714 | struct filename *oldpath; | |
715 | struct filename *newpath; | |
716 | int flags; | |
717 | }; | |
718 | ||
4f57f06c JA |
719 | struct io_msg { |
720 | struct file *file; | |
721 | u64 user_data; | |
722 | u32 len; | |
723 | }; | |
724 | ||
f499a021 JA |
725 | struct io_async_connect { |
726 | struct sockaddr_storage address; | |
727 | }; | |
728 | ||
03b1230c JA |
729 | struct io_async_msghdr { |
730 | struct iovec fast_iov[UIO_FASTIOV]; | |
257e84a5 PB |
731 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
732 | struct iovec *free_iov; | |
03b1230c JA |
733 | struct sockaddr __user *uaddr; |
734 | struct msghdr msg; | |
b537916c | 735 | struct sockaddr_storage addr; |
03b1230c JA |
736 | }; |
737 | ||
538941e2 | 738 | struct io_rw_state { |
ff6165b2 | 739 | struct iov_iter iter; |
cd658695 | 740 | struct iov_iter_state iter_state; |
c88598a9 | 741 | struct iovec fast_iov[UIO_FASTIOV]; |
538941e2 PB |
742 | }; |
743 | ||
744 | struct io_async_rw { | |
745 | struct io_rw_state s; | |
746 | const struct iovec *free_iovec; | |
227c0c96 | 747 | size_t bytes_done; |
bcf5a063 | 748 | struct wait_page_queue wpq; |
f67676d1 JA |
749 | }; |
750 | ||
6b47ee6e PB |
751 | enum { |
752 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, | |
753 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, | |
754 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, | |
755 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, | |
756 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, | |
bcda7baa | 757 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
04c76b41 | 758 | REQ_F_CQE_SKIP_BIT = IOSQE_CQE_SKIP_SUCCESS_BIT, |
6b47ee6e | 759 | |
dddca226 | 760 | /* first byte is taken by user flags, shift it to not overlap */ |
93d2bcd2 | 761 | REQ_F_FAIL_BIT = 8, |
6b47ee6e PB |
762 | REQ_F_INFLIGHT_BIT, |
763 | REQ_F_CUR_POS_BIT, | |
764 | REQ_F_NOWAIT_BIT, | |
6b47ee6e | 765 | REQ_F_LINK_TIMEOUT_BIT, |
99bc4c38 | 766 | REQ_F_NEED_CLEANUP_BIT, |
d7718a9d | 767 | REQ_F_POLLED_BIT, |
bcda7baa | 768 | REQ_F_BUFFER_SELECTED_BIT, |
e342c807 | 769 | REQ_F_COMPLETE_INLINE_BIT, |
230d50d4 | 770 | REQ_F_REISSUE_BIT, |
b8e64b53 | 771 | REQ_F_CREDS_BIT, |
20e60a38 | 772 | REQ_F_REFCOUNT_BIT, |
4d13d1a4 | 773 | REQ_F_ARM_LTIMEOUT_BIT, |
d886e185 | 774 | REQ_F_ASYNC_DATA_BIT, |
04c76b41 | 775 | REQ_F_SKIP_LINK_CQES_BIT, |
91eac1c6 JA |
776 | REQ_F_SINGLE_POLL_BIT, |
777 | REQ_F_DOUBLE_POLL_BIT, | |
8a3e8ee5 | 778 | REQ_F_PARTIAL_IO_BIT, |
7b29f92d | 779 | /* keep async read/write and isreg together and in order */ |
35645ac3 | 780 | REQ_F_SUPPORT_NOWAIT_BIT, |
7b29f92d | 781 | REQ_F_ISREG_BIT, |
84557871 JA |
782 | |
783 | /* not a real bit, just to check we're not overflowing the space */ | |
784 | __REQ_F_LAST_BIT, | |
6b47ee6e PB |
785 | }; |
786 | ||
787 | enum { | |
788 | /* ctx owns file */ | |
789 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), | |
790 | /* drain existing IO first */ | |
791 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), | |
792 | /* linked sqes */ | |
793 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), | |
794 | /* doesn't sever on completion < 0 */ | |
795 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), | |
796 | /* IOSQE_ASYNC */ | |
797 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), | |
bcda7baa JA |
798 | /* IOSQE_BUFFER_SELECT */ |
799 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), | |
04c76b41 PB |
800 | /* IOSQE_CQE_SKIP_SUCCESS */ |
801 | REQ_F_CQE_SKIP = BIT(REQ_F_CQE_SKIP_BIT), | |
6b47ee6e | 802 | |
6b47ee6e | 803 | /* fail rest of links */ |
93d2bcd2 | 804 | REQ_F_FAIL = BIT(REQ_F_FAIL_BIT), |
b05a1bcd | 805 | /* on inflight list, should be cancelled and waited on exit reliably */ |
6b47ee6e PB |
806 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
807 | /* read/write uses file position */ | |
808 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), | |
809 | /* must not punt to workers */ | |
810 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), | |
900fad45 | 811 | /* has or had linked timeout */ |
6b47ee6e | 812 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
99bc4c38 PB |
813 | /* needs cleanup */ |
814 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), | |
d7718a9d JA |
815 | /* already went through poll handler */ |
816 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), | |
bcda7baa JA |
817 | /* buffer already selected */ |
818 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), | |
e342c807 PB |
819 | /* completion is deferred through io_comp_state */ |
820 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), | |
230d50d4 JA |
821 | /* caller should reissue async */ |
822 | REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT), | |
35645ac3 PB |
823 | /* supports async reads/writes */ |
824 | REQ_F_SUPPORT_NOWAIT = BIT(REQ_F_SUPPORT_NOWAIT_BIT), | |
7b29f92d JA |
825 | /* regular file */ |
826 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), | |
b8e64b53 PB |
827 | /* has creds assigned */ |
828 | REQ_F_CREDS = BIT(REQ_F_CREDS_BIT), | |
20e60a38 PB |
829 | /* skip refcounting if not set */ |
830 | REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT), | |
4d13d1a4 PB |
831 | /* there is a linked timeout that has to be armed */ |
832 | REQ_F_ARM_LTIMEOUT = BIT(REQ_F_ARM_LTIMEOUT_BIT), | |
d886e185 PB |
833 | /* ->async_data allocated */ |
834 | REQ_F_ASYNC_DATA = BIT(REQ_F_ASYNC_DATA_BIT), | |
04c76b41 PB |
835 | /* don't post CQEs while failing linked requests */ |
836 | REQ_F_SKIP_LINK_CQES = BIT(REQ_F_SKIP_LINK_CQES_BIT), | |
91eac1c6 JA |
837 | /* single poll may be active */ |
838 | REQ_F_SINGLE_POLL = BIT(REQ_F_SINGLE_POLL_BIT), | |
839 | /* double poll may active */ | |
840 | REQ_F_DOUBLE_POLL = BIT(REQ_F_DOUBLE_POLL_BIT), | |
8a3e8ee5 JA |
841 | /* request has already done partial IO */ |
842 | REQ_F_PARTIAL_IO = BIT(REQ_F_PARTIAL_IO_BIT), | |
d7718a9d JA |
843 | }; |
844 | ||
845 | struct async_poll { | |
846 | struct io_poll_iocb poll; | |
807abcb0 | 847 | struct io_poll_iocb *double_poll; |
6b47ee6e PB |
848 | }; |
849 | ||
f237c30a | 850 | typedef void (*io_req_tw_func_t)(struct io_kiocb *req, bool *locked); |
5b0a6acc | 851 | |
7cbf1722 | 852 | struct io_task_work { |
5b0a6acc PB |
853 | union { |
854 | struct io_wq_work_node node; | |
855 | struct llist_node fallback_node; | |
856 | }; | |
857 | io_req_tw_func_t func; | |
7cbf1722 JA |
858 | }; |
859 | ||
992da01a PB |
860 | enum { |
861 | IORING_RSRC_FILE = 0, | |
862 | IORING_RSRC_BUFFER = 1, | |
863 | }; | |
864 | ||
09bb8394 JA |
865 | /* |
866 | * NOTE! Each of the iocb union members has the file pointer | |
867 | * as the first entry in their struct definition. So you can | |
868 | * access the file pointer through any of the sub-structs, | |
63c36549 | 869 | * or directly as just 'file' in this struct. |
09bb8394 | 870 | */ |
2b188cc1 | 871 | struct io_kiocb { |
221c5eb2 | 872 | union { |
09bb8394 | 873 | struct file *file; |
9adbd45d | 874 | struct io_rw rw; |
221c5eb2 | 875 | struct io_poll_iocb poll; |
9d805892 | 876 | struct io_poll_update poll_update; |
8ed8d3c3 JA |
877 | struct io_accept accept; |
878 | struct io_sync sync; | |
fbf23849 | 879 | struct io_cancel cancel; |
b29472ee | 880 | struct io_timeout timeout; |
0bdf7a2d | 881 | struct io_timeout_rem timeout_rem; |
3fbb51c1 | 882 | struct io_connect connect; |
e47293fd | 883 | struct io_sr_msg sr_msg; |
15b71abe | 884 | struct io_open open; |
b5dba59e | 885 | struct io_close close; |
269bbe5f | 886 | struct io_rsrc_update rsrc_update; |
4840e418 | 887 | struct io_fadvise fadvise; |
c1ca757b | 888 | struct io_madvise madvise; |
3e4827b0 | 889 | struct io_epoll epoll; |
7d67af2c | 890 | struct io_splice splice; |
ddf0322d | 891 | struct io_provide_buf pbuf; |
1d9e1288 | 892 | struct io_statx statx; |
36f4fa68 | 893 | struct io_shutdown shutdown; |
80a261fd | 894 | struct io_rename rename; |
14a1143b | 895 | struct io_unlink unlink; |
e34a02dc | 896 | struct io_mkdir mkdir; |
7a8721f8 | 897 | struct io_symlink symlink; |
cf30da90 | 898 | struct io_hardlink hardlink; |
4f57f06c | 899 | struct io_msg msg; |
221c5eb2 | 900 | }; |
2b188cc1 | 901 | |
d625c6ee | 902 | u8 opcode; |
65a6543d XW |
903 | /* polled IO has completed */ |
904 | u8 iopoll_completed; | |
4f4eeba8 | 905 | u16 buf_index; |
d17e56eb PB |
906 | unsigned int flags; |
907 | ||
908 | u64 user_data; | |
9cf7c104 | 909 | u32 result; |
82733d16 JA |
910 | /* fd initially, then cflags for completion */ |
911 | union { | |
912 | u32 cflags; | |
913 | int fd; | |
914 | }; | |
4f4eeba8 | 915 | |
010e8e6b | 916 | struct io_ring_ctx *ctx; |
010e8e6b | 917 | struct task_struct *task; |
d7718a9d | 918 | |
269bbe5f | 919 | struct percpu_ref *fixed_rsrc_refs; |
d886e185 PB |
920 | /* store used ubuf, so we can prevent reloading */ |
921 | struct io_mapped_ubuf *imu; | |
fcb323cc | 922 | |
2804ecd8 JA |
923 | union { |
924 | /* used by request caches, completion batching and iopoll */ | |
925 | struct io_wq_work_node comp_list; | |
926 | /* cache ->apoll->events */ | |
927 | int apoll_events; | |
928 | }; | |
d17e56eb | 929 | atomic_t refs; |
521d61fc | 930 | atomic_t poll_refs; |
5b0a6acc | 931 | struct io_task_work io_task_work; |
010e8e6b PB |
932 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
933 | struct hlist_node hash_node; | |
7e3709d5 | 934 | /* internal polling, see IORING_FEAT_FAST_POLL */ |
010e8e6b | 935 | struct async_poll *apoll; |
d886e185 PB |
936 | /* opcode allocated if it needs to store data for async defer */ |
937 | void *async_data; | |
7e3709d5 | 938 | /* stores selected buf, valid IFF REQ_F_BUFFER_SELECTED is set */ |
30d51dd4 | 939 | struct io_buffer *kbuf; |
41cdcc22 | 940 | /* linked requests, IFF REQ_F_HARDLINK or REQ_F_LINK are set */ |
34d2bfe7 | 941 | struct io_kiocb *link; |
41cdcc22 | 942 | /* custom credentials, valid IFF REQ_F_CREDS is set */ |
521d61fc JA |
943 | const struct cred *creds; |
944 | struct io_wq_work work; | |
2b188cc1 | 945 | }; |
05589553 | 946 | |
13bf43f5 PB |
947 | struct io_tctx_node { |
948 | struct list_head ctx_node; | |
949 | struct task_struct *task; | |
13bf43f5 PB |
950 | struct io_ring_ctx *ctx; |
951 | }; | |
952 | ||
27dc8338 PB |
953 | struct io_defer_entry { |
954 | struct list_head list; | |
955 | struct io_kiocb *req; | |
9cf7c104 | 956 | u32 seq; |
2b188cc1 JA |
957 | }; |
958 | ||
d3656344 | 959 | struct io_op_def { |
d3656344 JA |
960 | /* needs req->file assigned */ |
961 | unsigned needs_file : 1; | |
6d63416d PB |
962 | /* should block plug */ |
963 | unsigned plug : 1; | |
d3656344 JA |
964 | /* hash wq insertion if file is a regular file */ |
965 | unsigned hash_reg_file : 1; | |
966 | /* unbound wq insertion if file is a non-regular file */ | |
967 | unsigned unbound_nonreg_file : 1; | |
8a72758c JA |
968 | /* set if opcode supports polled "wait" */ |
969 | unsigned pollin : 1; | |
970 | unsigned pollout : 1; | |
52dd8640 | 971 | unsigned poll_exclusive : 1; |
bcda7baa JA |
972 | /* op supports buffer selection */ |
973 | unsigned buffer_select : 1; | |
26f0505a PB |
974 | /* do prep async if is going to be punted */ |
975 | unsigned needs_async_setup : 1; | |
6d63416d PB |
976 | /* opcode is not supported by this kernel */ |
977 | unsigned not_supported : 1; | |
5bd2182d PM |
978 | /* skip auditing */ |
979 | unsigned audit_skip : 1; | |
e8c2bc1f JA |
980 | /* size of async data needed, if any */ |
981 | unsigned short async_size; | |
d3656344 JA |
982 | }; |
983 | ||
0918682b | 984 | static const struct io_op_def io_op_defs[] = { |
0463b6c5 PB |
985 | [IORING_OP_NOP] = {}, |
986 | [IORING_OP_READV] = { | |
d3656344 JA |
987 | .needs_file = 1, |
988 | .unbound_nonreg_file = 1, | |
8a72758c | 989 | .pollin = 1, |
4d954c25 | 990 | .buffer_select = 1, |
26f0505a | 991 | .needs_async_setup = 1, |
27926b68 | 992 | .plug = 1, |
5bd2182d | 993 | .audit_skip = 1, |
e8c2bc1f | 994 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 995 | }, |
0463b6c5 | 996 | [IORING_OP_WRITEV] = { |
d3656344 JA |
997 | .needs_file = 1, |
998 | .hash_reg_file = 1, | |
999 | .unbound_nonreg_file = 1, | |
8a72758c | 1000 | .pollout = 1, |
26f0505a | 1001 | .needs_async_setup = 1, |
27926b68 | 1002 | .plug = 1, |
5bd2182d | 1003 | .audit_skip = 1, |
e8c2bc1f | 1004 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 1005 | }, |
0463b6c5 | 1006 | [IORING_OP_FSYNC] = { |
d3656344 | 1007 | .needs_file = 1, |
5bd2182d | 1008 | .audit_skip = 1, |
d3656344 | 1009 | }, |
0463b6c5 | 1010 | [IORING_OP_READ_FIXED] = { |
d3656344 JA |
1011 | .needs_file = 1, |
1012 | .unbound_nonreg_file = 1, | |
8a72758c | 1013 | .pollin = 1, |
27926b68 | 1014 | .plug = 1, |
5bd2182d | 1015 | .audit_skip = 1, |
e8c2bc1f | 1016 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 1017 | }, |
0463b6c5 | 1018 | [IORING_OP_WRITE_FIXED] = { |
d3656344 JA |
1019 | .needs_file = 1, |
1020 | .hash_reg_file = 1, | |
1021 | .unbound_nonreg_file = 1, | |
8a72758c | 1022 | .pollout = 1, |
27926b68 | 1023 | .plug = 1, |
5bd2182d | 1024 | .audit_skip = 1, |
e8c2bc1f | 1025 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 1026 | }, |
0463b6c5 | 1027 | [IORING_OP_POLL_ADD] = { |
d3656344 JA |
1028 | .needs_file = 1, |
1029 | .unbound_nonreg_file = 1, | |
5bd2182d PM |
1030 | .audit_skip = 1, |
1031 | }, | |
1032 | [IORING_OP_POLL_REMOVE] = { | |
1033 | .audit_skip = 1, | |
d3656344 | 1034 | }, |
0463b6c5 | 1035 | [IORING_OP_SYNC_FILE_RANGE] = { |
d3656344 | 1036 | .needs_file = 1, |
5bd2182d | 1037 | .audit_skip = 1, |
d3656344 | 1038 | }, |
0463b6c5 | 1039 | [IORING_OP_SENDMSG] = { |
d3656344 JA |
1040 | .needs_file = 1, |
1041 | .unbound_nonreg_file = 1, | |
8a72758c | 1042 | .pollout = 1, |
26f0505a | 1043 | .needs_async_setup = 1, |
e8c2bc1f | 1044 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 1045 | }, |
0463b6c5 | 1046 | [IORING_OP_RECVMSG] = { |
d3656344 JA |
1047 | .needs_file = 1, |
1048 | .unbound_nonreg_file = 1, | |
8a72758c | 1049 | .pollin = 1, |
52de1fe1 | 1050 | .buffer_select = 1, |
26f0505a | 1051 | .needs_async_setup = 1, |
e8c2bc1f | 1052 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 1053 | }, |
0463b6c5 | 1054 | [IORING_OP_TIMEOUT] = { |
5bd2182d | 1055 | .audit_skip = 1, |
e8c2bc1f | 1056 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 1057 | }, |
9c8e11b3 PB |
1058 | [IORING_OP_TIMEOUT_REMOVE] = { |
1059 | /* used by timeout updates' prep() */ | |
5bd2182d | 1060 | .audit_skip = 1, |
9c8e11b3 | 1061 | }, |
0463b6c5 | 1062 | [IORING_OP_ACCEPT] = { |
d3656344 JA |
1063 | .needs_file = 1, |
1064 | .unbound_nonreg_file = 1, | |
8a72758c | 1065 | .pollin = 1, |
52dd8640 | 1066 | .poll_exclusive = 1, |
d3656344 | 1067 | }, |
5bd2182d PM |
1068 | [IORING_OP_ASYNC_CANCEL] = { |
1069 | .audit_skip = 1, | |
1070 | }, | |
0463b6c5 | 1071 | [IORING_OP_LINK_TIMEOUT] = { |
5bd2182d | 1072 | .audit_skip = 1, |
e8c2bc1f | 1073 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 1074 | }, |
0463b6c5 | 1075 | [IORING_OP_CONNECT] = { |
d3656344 JA |
1076 | .needs_file = 1, |
1077 | .unbound_nonreg_file = 1, | |
8a72758c | 1078 | .pollout = 1, |
26f0505a | 1079 | .needs_async_setup = 1, |
e8c2bc1f | 1080 | .async_size = sizeof(struct io_async_connect), |
d3656344 | 1081 | }, |
0463b6c5 | 1082 | [IORING_OP_FALLOCATE] = { |
d3656344 | 1083 | .needs_file = 1, |
d3656344 | 1084 | }, |
44526bed JA |
1085 | [IORING_OP_OPENAT] = {}, |
1086 | [IORING_OP_CLOSE] = {}, | |
5bd2182d PM |
1087 | [IORING_OP_FILES_UPDATE] = { |
1088 | .audit_skip = 1, | |
1089 | }, | |
1090 | [IORING_OP_STATX] = { | |
1091 | .audit_skip = 1, | |
1092 | }, | |
0463b6c5 | 1093 | [IORING_OP_READ] = { |
3a6820f2 JA |
1094 | .needs_file = 1, |
1095 | .unbound_nonreg_file = 1, | |
8a72758c | 1096 | .pollin = 1, |
bcda7baa | 1097 | .buffer_select = 1, |
27926b68 | 1098 | .plug = 1, |
5bd2182d | 1099 | .audit_skip = 1, |
e8c2bc1f | 1100 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 1101 | }, |
0463b6c5 | 1102 | [IORING_OP_WRITE] = { |
3a6820f2 | 1103 | .needs_file = 1, |
7b3188e7 | 1104 | .hash_reg_file = 1, |
3a6820f2 | 1105 | .unbound_nonreg_file = 1, |
8a72758c | 1106 | .pollout = 1, |
27926b68 | 1107 | .plug = 1, |
5bd2182d | 1108 | .audit_skip = 1, |
e8c2bc1f | 1109 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 1110 | }, |
0463b6c5 | 1111 | [IORING_OP_FADVISE] = { |
4840e418 | 1112 | .needs_file = 1, |
5bd2182d | 1113 | .audit_skip = 1, |
c1ca757b | 1114 | }, |
44526bed | 1115 | [IORING_OP_MADVISE] = {}, |
0463b6c5 | 1116 | [IORING_OP_SEND] = { |
fddaface JA |
1117 | .needs_file = 1, |
1118 | .unbound_nonreg_file = 1, | |
8a72758c | 1119 | .pollout = 1, |
5bd2182d | 1120 | .audit_skip = 1, |
fddaface | 1121 | }, |
0463b6c5 | 1122 | [IORING_OP_RECV] = { |
fddaface JA |
1123 | .needs_file = 1, |
1124 | .unbound_nonreg_file = 1, | |
8a72758c | 1125 | .pollin = 1, |
bcda7baa | 1126 | .buffer_select = 1, |
5bd2182d | 1127 | .audit_skip = 1, |
fddaface | 1128 | }, |
0463b6c5 | 1129 | [IORING_OP_OPENAT2] = { |
cebdb986 | 1130 | }, |
3e4827b0 JA |
1131 | [IORING_OP_EPOLL_CTL] = { |
1132 | .unbound_nonreg_file = 1, | |
5bd2182d | 1133 | .audit_skip = 1, |
3e4827b0 | 1134 | }, |
7d67af2c PB |
1135 | [IORING_OP_SPLICE] = { |
1136 | .needs_file = 1, | |
1137 | .hash_reg_file = 1, | |
1138 | .unbound_nonreg_file = 1, | |
5bd2182d PM |
1139 | .audit_skip = 1, |
1140 | }, | |
1141 | [IORING_OP_PROVIDE_BUFFERS] = { | |
1142 | .audit_skip = 1, | |
1143 | }, | |
1144 | [IORING_OP_REMOVE_BUFFERS] = { | |
1145 | .audit_skip = 1, | |
ddf0322d | 1146 | }, |
f2a8d5c7 PB |
1147 | [IORING_OP_TEE] = { |
1148 | .needs_file = 1, | |
1149 | .hash_reg_file = 1, | |
1150 | .unbound_nonreg_file = 1, | |
5bd2182d | 1151 | .audit_skip = 1, |
f2a8d5c7 | 1152 | }, |
36f4fa68 JA |
1153 | [IORING_OP_SHUTDOWN] = { |
1154 | .needs_file = 1, | |
1155 | }, | |
44526bed JA |
1156 | [IORING_OP_RENAMEAT] = {}, |
1157 | [IORING_OP_UNLINKAT] = {}, | |
e34a02dc | 1158 | [IORING_OP_MKDIRAT] = {}, |
7a8721f8 | 1159 | [IORING_OP_SYMLINKAT] = {}, |
cf30da90 | 1160 | [IORING_OP_LINKAT] = {}, |
4f57f06c JA |
1161 | [IORING_OP_MSG_RING] = { |
1162 | .needs_file = 1, | |
1163 | }, | |
d3656344 JA |
1164 | }; |
1165 | ||
0756a869 PB |
1166 | /* requests with any of those set should undergo io_disarm_next() */ |
1167 | #define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL) | |
1168 | ||
7a612350 | 1169 | static bool io_disarm_next(struct io_kiocb *req); |
eef51daa | 1170 | static void io_uring_del_tctx_node(unsigned long index); |
9936c7c2 PB |
1171 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
1172 | struct task_struct *task, | |
3dd0c97a | 1173 | bool cancel_all); |
78cc687b | 1174 | static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd); |
1ffc5422 | 1175 | |
913a571a PB |
1176 | static void io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags); |
1177 | ||
ec9c02ad | 1178 | static void io_put_req(struct io_kiocb *req); |
91c2f697 | 1179 | static void io_put_req_deferred(struct io_kiocb *req); |
c7dae4ba | 1180 | static void io_dismantle_req(struct io_kiocb *req); |
94ae5e77 | 1181 | static void io_queue_linked_timeout(struct io_kiocb *req); |
fdecb662 | 1182 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 1183 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 | 1184 | unsigned nr_args); |
68fb8979 | 1185 | static void io_clean_op(struct io_kiocb *req); |
5106dd6e JA |
1186 | static inline struct file *io_file_get_fixed(struct io_kiocb *req, int fd, |
1187 | unsigned issue_flags); | |
1188 | static inline struct file *io_file_get_normal(struct io_kiocb *req, int fd); | |
d5361233 JA |
1189 | static void io_drop_inflight_file(struct io_kiocb *req); |
1190 | static bool io_assign_file(struct io_kiocb *req, unsigned int issue_flags); | |
c5eef2b9 | 1191 | static void __io_queue_sqe(struct io_kiocb *req); |
269bbe5f | 1192 | static void io_rsrc_put_work(struct work_struct *work); |
de0617e4 | 1193 | |
907d1df3 | 1194 | static void io_req_task_queue(struct io_kiocb *req); |
c450178d | 1195 | static void __io_submit_flush_completions(struct io_ring_ctx *ctx); |
179ae0d1 | 1196 | static int io_req_prep_async(struct io_kiocb *req); |
de0617e4 | 1197 | |
b9445598 PB |
1198 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
1199 | unsigned int issue_flags, u32 slot_index); | |
7df778be PB |
1200 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags); |
1201 | ||
f1042b6c | 1202 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer); |
9aa8dfde | 1203 | static void io_eventfd_signal(struct io_ring_ctx *ctx); |
b9445598 | 1204 | |
2b188cc1 JA |
1205 | static struct kmem_cache *req_cachep; |
1206 | ||
0918682b | 1207 | static const struct file_operations io_uring_fops; |
2b188cc1 JA |
1208 | |
1209 | struct sock *io_uring_get_socket(struct file *file) | |
1210 | { | |
1211 | #if defined(CONFIG_UNIX) | |
1212 | if (file->f_op == &io_uring_fops) { | |
1213 | struct io_ring_ctx *ctx = file->private_data; | |
1214 | ||
1215 | return ctx->ring_sock->sk; | |
1216 | } | |
1217 | #endif | |
1218 | return NULL; | |
1219 | } | |
1220 | EXPORT_SYMBOL(io_uring_get_socket); | |
1221 | ||
f237c30a PB |
1222 | static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked) |
1223 | { | |
1224 | if (!*locked) { | |
1225 | mutex_lock(&ctx->uring_lock); | |
1226 | *locked = true; | |
1227 | } | |
1228 | } | |
1229 | ||
f2f87370 PB |
1230 | #define io_for_each_link(pos, head) \ |
1231 | for (pos = (head); pos; pos = pos->link) | |
1232 | ||
21c843d5 PB |
1233 | /* |
1234 | * Shamelessly stolen from the mm implementation of page reference checking, | |
1235 | * see commit f958d7b528b1 for details. | |
1236 | */ | |
1237 | #define req_ref_zero_or_close_to_overflow(req) \ | |
1238 | ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u) | |
1239 | ||
1240 | static inline bool req_ref_inc_not_zero(struct io_kiocb *req) | |
1241 | { | |
20e60a38 | 1242 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1243 | return atomic_inc_not_zero(&req->refs); |
1244 | } | |
1245 | ||
21c843d5 PB |
1246 | static inline bool req_ref_put_and_test(struct io_kiocb *req) |
1247 | { | |
20e60a38 PB |
1248 | if (likely(!(req->flags & REQ_F_REFCOUNT))) |
1249 | return true; | |
1250 | ||
21c843d5 PB |
1251 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1252 | return atomic_dec_and_test(&req->refs); | |
1253 | } | |
1254 | ||
21c843d5 PB |
1255 | static inline void req_ref_get(struct io_kiocb *req) |
1256 | { | |
20e60a38 | 1257 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1258 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1259 | atomic_inc(&req->refs); | |
1260 | } | |
1261 | ||
c450178d PB |
1262 | static inline void io_submit_flush_completions(struct io_ring_ctx *ctx) |
1263 | { | |
6f33b0bc | 1264 | if (!wq_list_empty(&ctx->submit_state.compl_reqs)) |
c450178d PB |
1265 | __io_submit_flush_completions(ctx); |
1266 | } | |
1267 | ||
48dcd38d | 1268 | static inline void __io_req_set_refcount(struct io_kiocb *req, int nr) |
20e60a38 PB |
1269 | { |
1270 | if (!(req->flags & REQ_F_REFCOUNT)) { | |
1271 | req->flags |= REQ_F_REFCOUNT; | |
48dcd38d | 1272 | atomic_set(&req->refs, nr); |
20e60a38 PB |
1273 | } |
1274 | } | |
1275 | ||
48dcd38d PB |
1276 | static inline void io_req_set_refcount(struct io_kiocb *req) |
1277 | { | |
1278 | __io_req_set_refcount(req, 1); | |
1279 | } | |
1280 | ||
ab409402 PB |
1281 | #define IO_RSRC_REF_BATCH 100 |
1282 | ||
1283 | static inline void io_req_put_rsrc_locked(struct io_kiocb *req, | |
1284 | struct io_ring_ctx *ctx) | |
1285 | __must_hold(&ctx->uring_lock) | |
36f72fe2 | 1286 | { |
ab409402 PB |
1287 | struct percpu_ref *ref = req->fixed_rsrc_refs; |
1288 | ||
1289 | if (ref) { | |
1290 | if (ref == &ctx->rsrc_node->refs) | |
1291 | ctx->rsrc_cached_refs++; | |
1292 | else | |
1293 | percpu_ref_put(ref); | |
1294 | } | |
1295 | } | |
1296 | ||
1297 | static inline void io_req_put_rsrc(struct io_kiocb *req, struct io_ring_ctx *ctx) | |
1298 | { | |
1299 | if (req->fixed_rsrc_refs) | |
1300 | percpu_ref_put(req->fixed_rsrc_refs); | |
1301 | } | |
1302 | ||
1303 | static __cold void io_rsrc_refs_drop(struct io_ring_ctx *ctx) | |
1304 | __must_hold(&ctx->uring_lock) | |
1305 | { | |
1306 | if (ctx->rsrc_cached_refs) { | |
1307 | percpu_ref_put_many(&ctx->rsrc_node->refs, ctx->rsrc_cached_refs); | |
1308 | ctx->rsrc_cached_refs = 0; | |
1309 | } | |
1310 | } | |
1311 | ||
1312 | static void io_rsrc_refs_refill(struct io_ring_ctx *ctx) | |
1313 | __must_hold(&ctx->uring_lock) | |
1314 | { | |
1315 | ctx->rsrc_cached_refs += IO_RSRC_REF_BATCH; | |
1316 | percpu_ref_get_many(&ctx->rsrc_node->refs, IO_RSRC_REF_BATCH); | |
1317 | } | |
36f72fe2 | 1318 | |
a46be971 | 1319 | static inline void io_req_set_rsrc_node(struct io_kiocb *req, |
5106dd6e JA |
1320 | struct io_ring_ctx *ctx, |
1321 | unsigned int issue_flags) | |
36f72fe2 | 1322 | { |
269bbe5f | 1323 | if (!req->fixed_rsrc_refs) { |
a7f0ed5a | 1324 | req->fixed_rsrc_refs = &ctx->rsrc_node->refs; |
5106dd6e JA |
1325 | |
1326 | if (!(issue_flags & IO_URING_F_UNLOCKED)) { | |
1327 | lockdep_assert_held(&ctx->uring_lock); | |
1328 | ctx->rsrc_cached_refs--; | |
1329 | if (unlikely(ctx->rsrc_cached_refs < 0)) | |
1330 | io_rsrc_refs_refill(ctx); | |
1331 | } else { | |
1332 | percpu_ref_get(req->fixed_rsrc_refs); | |
1333 | } | |
36f72fe2 PB |
1334 | } |
1335 | } | |
1336 | ||
cc3cec83 | 1337 | static unsigned int __io_put_kbuf(struct io_kiocb *req, struct list_head *list) |
3648e526 | 1338 | { |
d1fd1c20 | 1339 | struct io_buffer *kbuf = req->kbuf; |
3648e526 HX |
1340 | unsigned int cflags; |
1341 | ||
cc3cec83 | 1342 | cflags = IORING_CQE_F_BUFFER | (kbuf->bid << IORING_CQE_BUFFER_SHIFT); |
3648e526 | 1343 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
cc3cec83 | 1344 | list_add(&kbuf->list, list); |
d1fd1c20 | 1345 | req->kbuf = NULL; |
3648e526 HX |
1346 | return cflags; |
1347 | } | |
1348 | ||
cc3cec83 | 1349 | static inline unsigned int io_put_kbuf_comp(struct io_kiocb *req) |
3648e526 | 1350 | { |
8197b053 PB |
1351 | lockdep_assert_held(&req->ctx->completion_lock); |
1352 | ||
3648e526 HX |
1353 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) |
1354 | return 0; | |
cc3cec83 JA |
1355 | return __io_put_kbuf(req, &req->ctx->io_buffers_comp); |
1356 | } | |
1357 | ||
1358 | static inline unsigned int io_put_kbuf(struct io_kiocb *req, | |
1359 | unsigned issue_flags) | |
1360 | { | |
1361 | unsigned int cflags; | |
1362 | ||
1363 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) | |
1364 | return 0; | |
1365 | ||
1366 | /* | |
1367 | * We can add this buffer back to two lists: | |
1368 | * | |
1369 | * 1) The io_buffers_cache list. This one is protected by the | |
1370 | * ctx->uring_lock. If we already hold this lock, add back to this | |
1371 | * list as we can grab it from issue as well. | |
1372 | * 2) The io_buffers_comp list. This one is protected by the | |
1373 | * ctx->completion_lock. | |
1374 | * | |
1375 | * We migrate buffers from the comp_list to the issue cache list | |
1376 | * when we need one. | |
1377 | */ | |
1378 | if (issue_flags & IO_URING_F_UNLOCKED) { | |
1379 | struct io_ring_ctx *ctx = req->ctx; | |
1380 | ||
1381 | spin_lock(&ctx->completion_lock); | |
1382 | cflags = __io_put_kbuf(req, &ctx->io_buffers_comp); | |
1383 | spin_unlock(&ctx->completion_lock); | |
1384 | } else { | |
ab0ac095 PB |
1385 | lockdep_assert_held(&req->ctx->uring_lock); |
1386 | ||
cc3cec83 JA |
1387 | cflags = __io_put_kbuf(req, &req->ctx->io_buffers_cache); |
1388 | } | |
1389 | ||
1390 | return cflags; | |
3648e526 HX |
1391 | } |
1392 | ||
dbc7d452 JA |
1393 | static struct io_buffer_list *io_buffer_get_list(struct io_ring_ctx *ctx, |
1394 | unsigned int bgid) | |
1395 | { | |
1396 | struct list_head *hash_list; | |
1397 | struct io_buffer_list *bl; | |
1398 | ||
1399 | hash_list = &ctx->io_buffers[hash_32(bgid, IO_BUFFERS_HASH_BITS)]; | |
1400 | list_for_each_entry(bl, hash_list, list) | |
1401 | if (bl->bgid == bgid || bgid == -1U) | |
1402 | return bl; | |
1403 | ||
1404 | return NULL; | |
1405 | } | |
1406 | ||
4d55f238 | 1407 | static void io_kbuf_recycle(struct io_kiocb *req, unsigned issue_flags) |
b1c62645 JA |
1408 | { |
1409 | struct io_ring_ctx *ctx = req->ctx; | |
dbc7d452 JA |
1410 | struct io_buffer_list *bl; |
1411 | struct io_buffer *buf; | |
b1c62645 JA |
1412 | |
1413 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) | |
1414 | return; | |
8a3e8ee5 JA |
1415 | /* don't recycle if we already did IO to this buffer */ |
1416 | if (req->flags & REQ_F_PARTIAL_IO) | |
1417 | return; | |
b1c62645 | 1418 | |
4d55f238 JA |
1419 | if (issue_flags & IO_URING_F_UNLOCKED) |
1420 | mutex_lock(&ctx->uring_lock); | |
b1c62645 JA |
1421 | |
1422 | lockdep_assert_held(&ctx->uring_lock); | |
1423 | ||
1424 | buf = req->kbuf; | |
dbc7d452 JA |
1425 | bl = io_buffer_get_list(ctx, buf->bgid); |
1426 | list_add(&buf->list, &bl->buf_list); | |
b1c62645 JA |
1427 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
1428 | req->kbuf = NULL; | |
4d55f238 JA |
1429 | |
1430 | if (issue_flags & IO_URING_F_UNLOCKED) | |
1431 | mutex_unlock(&ctx->uring_lock); | |
b1c62645 JA |
1432 | } |
1433 | ||
3dd0c97a PB |
1434 | static bool io_match_task(struct io_kiocb *head, struct task_struct *task, |
1435 | bool cancel_all) | |
6af3f48b | 1436 | __must_hold(&req->ctx->timeout_lock) |
08d23634 | 1437 | { |
68207680 | 1438 | if (task && head->task != task) |
08d23634 | 1439 | return false; |
d5361233 | 1440 | return cancel_all; |
6af3f48b PB |
1441 | } |
1442 | ||
1443 | /* | |
1444 | * As io_match_task() but protected against racing with linked timeouts. | |
1445 | * User must not hold timeout_lock. | |
1446 | */ | |
1447 | static bool io_match_task_safe(struct io_kiocb *head, struct task_struct *task, | |
1448 | bool cancel_all) | |
1449 | { | |
6af3f48b PB |
1450 | if (task && head->task != task) |
1451 | return false; | |
d5361233 | 1452 | return cancel_all; |
6af3f48b PB |
1453 | } |
1454 | ||
d886e185 PB |
1455 | static inline bool req_has_async_data(struct io_kiocb *req) |
1456 | { | |
1457 | return req->flags & REQ_F_ASYNC_DATA; | |
1458 | } | |
1459 | ||
93d2bcd2 | 1460 | static inline void req_set_fail(struct io_kiocb *req) |
c40f6379 | 1461 | { |
93d2bcd2 | 1462 | req->flags |= REQ_F_FAIL; |
04c76b41 PB |
1463 | if (req->flags & REQ_F_CQE_SKIP) { |
1464 | req->flags &= ~REQ_F_CQE_SKIP; | |
1465 | req->flags |= REQ_F_SKIP_LINK_CQES; | |
1466 | } | |
c40f6379 | 1467 | } |
4a38aed2 | 1468 | |
a8295b98 HX |
1469 | static inline void req_fail_link_node(struct io_kiocb *req, int res) |
1470 | { | |
1471 | req_set_fail(req); | |
1472 | req->result = res; | |
1473 | } | |
1474 | ||
c072481d | 1475 | static __cold void io_ring_ctx_ref_free(struct percpu_ref *ref) |
2b188cc1 JA |
1476 | { |
1477 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
1478 | ||
0f158b4c | 1479 | complete(&ctx->ref_comp); |
2b188cc1 JA |
1480 | } |
1481 | ||
8eb7e2d0 PB |
1482 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
1483 | { | |
1484 | return !req->timeout.off; | |
1485 | } | |
1486 | ||
c072481d | 1487 | static __cold void io_fallback_req_func(struct work_struct *work) |
f56165e6 PB |
1488 | { |
1489 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, | |
1490 | fallback_work.work); | |
1491 | struct llist_node *node = llist_del_all(&ctx->fallback_llist); | |
1492 | struct io_kiocb *req, *tmp; | |
f237c30a | 1493 | bool locked = false; |
f56165e6 PB |
1494 | |
1495 | percpu_ref_get(&ctx->refs); | |
1496 | llist_for_each_entry_safe(req, tmp, node, io_task_work.fallback_node) | |
f237c30a | 1497 | req->io_task_work.func(req, &locked); |
5636c00d | 1498 | |
f237c30a | 1499 | if (locked) { |
c450178d | 1500 | io_submit_flush_completions(ctx); |
f237c30a PB |
1501 | mutex_unlock(&ctx->uring_lock); |
1502 | } | |
f56165e6 PB |
1503 | percpu_ref_put(&ctx->refs); |
1504 | } | |
1505 | ||
c072481d | 1506 | static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
2b188cc1 JA |
1507 | { |
1508 | struct io_ring_ctx *ctx; | |
dbc7d452 | 1509 | int i, hash_bits; |
2b188cc1 JA |
1510 | |
1511 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1512 | if (!ctx) | |
1513 | return NULL; | |
1514 | ||
78076bb6 JA |
1515 | /* |
1516 | * Use 5 bits less than the max cq entries, that should give us around | |
1517 | * 32 entries per hash list if totally full and uniformly spread. | |
1518 | */ | |
1519 | hash_bits = ilog2(p->cq_entries); | |
1520 | hash_bits -= 5; | |
1521 | if (hash_bits <= 0) | |
1522 | hash_bits = 1; | |
1523 | ctx->cancel_hash_bits = hash_bits; | |
1524 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
1525 | GFP_KERNEL); | |
1526 | if (!ctx->cancel_hash) | |
1527 | goto err; | |
1528 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
1529 | ||
6224843d PB |
1530 | ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL); |
1531 | if (!ctx->dummy_ubuf) | |
1532 | goto err; | |
1533 | /* set invalid range, so io_import_fixed() fails meeting it */ | |
1534 | ctx->dummy_ubuf->ubuf = -1UL; | |
1535 | ||
dbc7d452 JA |
1536 | ctx->io_buffers = kcalloc(1U << IO_BUFFERS_HASH_BITS, |
1537 | sizeof(struct list_head), GFP_KERNEL); | |
1538 | if (!ctx->io_buffers) | |
1539 | goto err; | |
1540 | for (i = 0; i < (1U << IO_BUFFERS_HASH_BITS); i++) | |
1541 | INIT_LIST_HEAD(&ctx->io_buffers[i]); | |
1542 | ||
21482896 | 1543 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
1544 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
1545 | goto err; | |
2b188cc1 JA |
1546 | |
1547 | ctx->flags = p->flags; | |
90554200 | 1548 | init_waitqueue_head(&ctx->sqo_sq_wait); |
69fb2131 | 1549 | INIT_LIST_HEAD(&ctx->sqd_list); |
1d7bb1d5 | 1550 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
cc3cec83 | 1551 | INIT_LIST_HEAD(&ctx->io_buffers_cache); |
4d9237e3 | 1552 | INIT_LIST_HEAD(&ctx->apoll_cache); |
0f158b4c | 1553 | init_completion(&ctx->ref_comp); |
61cf9370 | 1554 | xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1); |
2b188cc1 | 1555 | mutex_init(&ctx->uring_lock); |
311997b3 | 1556 | init_waitqueue_head(&ctx->cq_wait); |
2b188cc1 | 1557 | spin_lock_init(&ctx->completion_lock); |
89850fce | 1558 | spin_lock_init(&ctx->timeout_lock); |
5eef4e87 | 1559 | INIT_WQ_LIST(&ctx->iopoll_list); |
cc3cec83 JA |
1560 | INIT_LIST_HEAD(&ctx->io_buffers_pages); |
1561 | INIT_LIST_HEAD(&ctx->io_buffers_comp); | |
de0617e4 | 1562 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 1563 | INIT_LIST_HEAD(&ctx->timeout_list); |
ef9dd637 | 1564 | INIT_LIST_HEAD(&ctx->ltimeout_list); |
d67d2263 BM |
1565 | spin_lock_init(&ctx->rsrc_ref_lock); |
1566 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); | |
269bbe5f BM |
1567 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
1568 | init_llist_head(&ctx->rsrc_put_llist); | |
13bf43f5 | 1569 | INIT_LIST_HEAD(&ctx->tctx_list); |
c2b6c6bc PB |
1570 | ctx->submit_state.free_list.next = NULL; |
1571 | INIT_WQ_LIST(&ctx->locked_free_list); | |
9011bf9a | 1572 | INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func); |
6f33b0bc | 1573 | INIT_WQ_LIST(&ctx->submit_state.compl_reqs); |
2b188cc1 | 1574 | return ctx; |
206aefde | 1575 | err: |
6224843d | 1576 | kfree(ctx->dummy_ubuf); |
78076bb6 | 1577 | kfree(ctx->cancel_hash); |
dbc7d452 | 1578 | kfree(ctx->io_buffers); |
206aefde JA |
1579 | kfree(ctx); |
1580 | return NULL; | |
2b188cc1 JA |
1581 | } |
1582 | ||
8f6ed49a PB |
1583 | static void io_account_cq_overflow(struct io_ring_ctx *ctx) |
1584 | { | |
1585 | struct io_rings *r = ctx->rings; | |
1586 | ||
1587 | WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1); | |
1588 | ctx->cq_extra--; | |
1589 | } | |
1590 | ||
9cf7c104 | 1591 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
7adf4eaf | 1592 | { |
2bc9930e JA |
1593 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
1594 | struct io_ring_ctx *ctx = req->ctx; | |
a197f664 | 1595 | |
8f6ed49a | 1596 | return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail; |
2bc9930e | 1597 | } |
de0617e4 | 1598 | |
9d858b21 | 1599 | return false; |
de0617e4 JA |
1600 | } |
1601 | ||
35645ac3 PB |
1602 | #define FFS_NOWAIT 0x1UL |
1603 | #define FFS_ISREG 0x2UL | |
1604 | #define FFS_MASK ~(FFS_NOWAIT|FFS_ISREG) | |
c97d8a0f PB |
1605 | |
1606 | static inline bool io_req_ffs_set(struct io_kiocb *req) | |
1607 | { | |
35645ac3 | 1608 | return req->flags & REQ_F_FIXED_FILE; |
c97d8a0f PB |
1609 | } |
1610 | ||
fd08e530 PB |
1611 | static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req) |
1612 | { | |
906c6caa PB |
1613 | if (WARN_ON_ONCE(!req->link)) |
1614 | return NULL; | |
1615 | ||
4d13d1a4 PB |
1616 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
1617 | req->flags |= REQ_F_LINK_TIMEOUT; | |
fd08e530 PB |
1618 | |
1619 | /* linked timeouts should have two refs once prep'ed */ | |
48dcd38d | 1620 | io_req_set_refcount(req); |
4d13d1a4 PB |
1621 | __io_req_set_refcount(req->link, 2); |
1622 | return req->link; | |
fd08e530 PB |
1623 | } |
1624 | ||
1625 | static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) | |
1626 | { | |
4d13d1a4 | 1627 | if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT))) |
fd08e530 PB |
1628 | return NULL; |
1629 | return __io_prep_linked_timeout(req); | |
1630 | } | |
1631 | ||
1e6fa521 JA |
1632 | static void io_prep_async_work(struct io_kiocb *req) |
1633 | { | |
1634 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
1e6fa521 JA |
1635 | struct io_ring_ctx *ctx = req->ctx; |
1636 | ||
b8e64b53 PB |
1637 | if (!(req->flags & REQ_F_CREDS)) { |
1638 | req->flags |= REQ_F_CREDS; | |
c10d1f98 | 1639 | req->creds = get_current_cred(); |
b8e64b53 | 1640 | } |
003e8dcc | 1641 | |
e1d675df PB |
1642 | req->work.list.next = NULL; |
1643 | req->work.flags = 0; | |
feaadc4f PB |
1644 | if (req->flags & REQ_F_FORCE_ASYNC) |
1645 | req->work.flags |= IO_WQ_WORK_CONCURRENT; | |
1646 | ||
1e6fa521 JA |
1647 | if (req->flags & REQ_F_ISREG) { |
1648 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) | |
1649 | io_wq_hash_work(&req->work, file_inode(req->file)); | |
4b982bd0 | 1650 | } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) { |
1e6fa521 JA |
1651 | if (def->unbound_nonreg_file) |
1652 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1653 | } | |
561fb04a | 1654 | } |
cccf0ee8 | 1655 | |
cbdcb435 | 1656 | static void io_prep_async_link(struct io_kiocb *req) |
561fb04a | 1657 | { |
cbdcb435 | 1658 | struct io_kiocb *cur; |
54a91f3b | 1659 | |
44eff40a PB |
1660 | if (req->flags & REQ_F_LINK_TIMEOUT) { |
1661 | struct io_ring_ctx *ctx = req->ctx; | |
1662 | ||
674ee8e1 | 1663 | spin_lock_irq(&ctx->timeout_lock); |
44eff40a PB |
1664 | io_for_each_link(cur, req) |
1665 | io_prep_async_work(cur); | |
674ee8e1 | 1666 | spin_unlock_irq(&ctx->timeout_lock); |
44eff40a PB |
1667 | } else { |
1668 | io_for_each_link(cur, req) | |
1669 | io_prep_async_work(cur); | |
1670 | } | |
561fb04a JA |
1671 | } |
1672 | ||
fff4e40e PB |
1673 | static inline void io_req_add_compl_list(struct io_kiocb *req) |
1674 | { | |
3d4aeb9f | 1675 | struct io_ring_ctx *ctx = req->ctx; |
33ce2aff | 1676 | struct io_submit_state *state = &ctx->submit_state; |
fff4e40e | 1677 | |
3d4aeb9f PB |
1678 | if (!(req->flags & REQ_F_CQE_SKIP)) |
1679 | ctx->submit_state.flush_cqes = true; | |
fff4e40e PB |
1680 | wq_list_add_tail(&req->comp_list, &state->compl_reqs); |
1681 | } | |
1682 | ||
00169246 | 1683 | static void io_queue_async_work(struct io_kiocb *req, bool *dont_use) |
561fb04a | 1684 | { |
a197f664 | 1685 | struct io_ring_ctx *ctx = req->ctx; |
cbdcb435 | 1686 | struct io_kiocb *link = io_prep_linked_timeout(req); |
5aa75ed5 | 1687 | struct io_uring_task *tctx = req->task->io_uring; |
561fb04a | 1688 | |
3bfe6106 JA |
1689 | BUG_ON(!tctx); |
1690 | BUG_ON(!tctx->io_wq); | |
561fb04a | 1691 | |
cbdcb435 PB |
1692 | /* init ->work of the whole link before punting */ |
1693 | io_prep_async_link(req); | |
991468dc JA |
1694 | |
1695 | /* | |
1696 | * Not expected to happen, but if we do have a bug where this _can_ | |
1697 | * happen, catch it here and ensure the request is marked as | |
1698 | * canceled. That will make io-wq go through the usual work cancel | |
1699 | * procedure rather than attempt to run this request (or create a new | |
1700 | * worker for it). | |
1701 | */ | |
1702 | if (WARN_ON_ONCE(!same_thread_group(req->task, current))) | |
1703 | req->work.flags |= IO_WQ_WORK_CANCEL; | |
1704 | ||
502c87d6 SR |
1705 | trace_io_uring_queue_async_work(ctx, req, req->user_data, req->opcode, req->flags, |
1706 | &req->work, io_wq_is_hashed(&req->work)); | |
ebf93667 | 1707 | io_wq_enqueue(tctx->io_wq, &req->work); |
7271ef3a JA |
1708 | if (link) |
1709 | io_queue_linked_timeout(link); | |
cbdcb435 PB |
1710 | } |
1711 | ||
1ee4160c | 1712 | static void io_kill_timeout(struct io_kiocb *req, int status) |
8c855885 | 1713 | __must_hold(&req->ctx->completion_lock) |
89850fce | 1714 | __must_hold(&req->ctx->timeout_lock) |
5262f567 | 1715 | { |
e8c2bc1f | 1716 | struct io_timeout_data *io = req->async_data; |
5262f567 | 1717 | |
fd9c7bc5 | 1718 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
2ae2eb9d PB |
1719 | if (status) |
1720 | req_set_fail(req); | |
01cec8c1 PB |
1721 | atomic_set(&req->ctx->cq_timeouts, |
1722 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
135fcde8 | 1723 | list_del_init(&req->timeout.list); |
913a571a | 1724 | io_fill_cqe_req(req, status, 0); |
91c2f697 | 1725 | io_put_req_deferred(req); |
5262f567 JA |
1726 | } |
1727 | } | |
1728 | ||
c072481d | 1729 | static __cold void io_queue_deferred(struct io_ring_ctx *ctx) |
de0617e4 | 1730 | { |
441b8a78 | 1731 | while (!list_empty(&ctx->defer_list)) { |
27dc8338 PB |
1732 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
1733 | struct io_defer_entry, list); | |
de0617e4 | 1734 | |
9cf7c104 | 1735 | if (req_need_defer(de->req, de->seq)) |
04518945 | 1736 | break; |
27dc8338 | 1737 | list_del_init(&de->list); |
907d1df3 | 1738 | io_req_task_queue(de->req); |
27dc8338 | 1739 | kfree(de); |
441b8a78 | 1740 | } |
04518945 PB |
1741 | } |
1742 | ||
c072481d | 1743 | static __cold void io_flush_timeouts(struct io_ring_ctx *ctx) |
89850fce | 1744 | __must_hold(&ctx->completion_lock) |
de0617e4 | 1745 | { |
441b8a78 | 1746 | u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
e677edbc | 1747 | struct io_kiocb *req, *tmp; |
f010505b | 1748 | |
79ebeaee | 1749 | spin_lock_irq(&ctx->timeout_lock); |
e677edbc | 1750 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
f010505b | 1751 | u32 events_needed, events_got; |
de0617e4 | 1752 | |
8eb7e2d0 | 1753 | if (io_is_timeout_noseq(req)) |
360428f8 | 1754 | break; |
f010505b MDG |
1755 | |
1756 | /* | |
1757 | * Since seq can easily wrap around over time, subtract | |
1758 | * the last seq at which timeouts were flushed before comparing. | |
1759 | * Assuming not more than 2^31-1 events have happened since, | |
1760 | * these subtractions won't have wrapped, so we can check if | |
1761 | * target is in [last_seq, current_seq] by comparing the two. | |
1762 | */ | |
1763 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; | |
1764 | events_got = seq - ctx->cq_last_tm_flush; | |
1765 | if (events_got < events_needed) | |
360428f8 | 1766 | break; |
bfe68a22 | 1767 | |
1ee4160c | 1768 | io_kill_timeout(req, 0); |
f18ee4cf | 1769 | } |
f010505b | 1770 | ctx->cq_last_tm_flush = seq; |
79ebeaee | 1771 | spin_unlock_irq(&ctx->timeout_lock); |
360428f8 | 1772 | } |
5262f567 | 1773 | |
9333f6b4 PB |
1774 | static inline void io_commit_cqring(struct io_ring_ctx *ctx) |
1775 | { | |
1776 | /* order cqe stores with ring update */ | |
1777 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); | |
1778 | } | |
1779 | ||
9aa8dfde | 1780 | static void __io_commit_cqring_flush(struct io_ring_ctx *ctx) |
360428f8 | 1781 | { |
9aa8dfde PB |
1782 | if (ctx->off_timeout_used || ctx->drain_active) { |
1783 | spin_lock(&ctx->completion_lock); | |
1784 | if (ctx->off_timeout_used) | |
1785 | io_flush_timeouts(ctx); | |
1786 | if (ctx->drain_active) | |
1787 | io_queue_deferred(ctx); | |
1788 | io_commit_cqring(ctx); | |
1789 | spin_unlock(&ctx->completion_lock); | |
1790 | } | |
1791 | if (ctx->has_evfd) | |
1792 | io_eventfd_signal(ctx); | |
de0617e4 JA |
1793 | } |
1794 | ||
90554200 JA |
1795 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
1796 | { | |
1797 | struct io_rings *r = ctx->rings; | |
1798 | ||
a566c556 | 1799 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries; |
90554200 JA |
1800 | } |
1801 | ||
888aae2e PB |
1802 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
1803 | { | |
1804 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); | |
1805 | } | |
1806 | ||
d068b506 | 1807 | static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx) |
2b188cc1 | 1808 | { |
75b28aff | 1809 | struct io_rings *rings = ctx->rings; |
ea5ab3b5 | 1810 | unsigned tail, mask = ctx->cq_entries - 1; |
2b188cc1 | 1811 | |
115e12e5 SB |
1812 | /* |
1813 | * writes to the cq entry need to come after reading head; the | |
1814 | * control dependency is enough as we're using WRITE_ONCE to | |
1815 | * fill the cq entry | |
1816 | */ | |
a566c556 | 1817 | if (__io_cqring_events(ctx) == ctx->cq_entries) |
2b188cc1 JA |
1818 | return NULL; |
1819 | ||
888aae2e | 1820 | tail = ctx->cached_cq_tail++; |
ea5ab3b5 | 1821 | return &rings->cqes[tail & mask]; |
2b188cc1 JA |
1822 | } |
1823 | ||
77bc59b4 | 1824 | static void io_eventfd_signal(struct io_ring_ctx *ctx) |
f2842ab5 | 1825 | { |
77bc59b4 UA |
1826 | struct io_ev_fd *ev_fd; |
1827 | ||
77bc59b4 UA |
1828 | rcu_read_lock(); |
1829 | /* | |
1830 | * rcu_dereference ctx->io_ev_fd once and use it for both for checking | |
1831 | * and eventfd_signal | |
1832 | */ | |
1833 | ev_fd = rcu_dereference(ctx->io_ev_fd); | |
1834 | ||
1835 | /* | |
1836 | * Check again if ev_fd exists incase an io_eventfd_unregister call | |
1837 | * completed between the NULL check of ctx->io_ev_fd at the start of | |
1838 | * the function and rcu_read_lock. | |
1839 | */ | |
1840 | if (unlikely(!ev_fd)) | |
1841 | goto out; | |
7e55a19c | 1842 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
77bc59b4 UA |
1843 | goto out; |
1844 | ||
c75312dd | 1845 | if (!ev_fd->eventfd_async || io_wq_current_is_worker()) |
77bc59b4 | 1846 | eventfd_signal(ev_fd->cq_ev_fd, 1); |
77bc59b4 UA |
1847 | out: |
1848 | rcu_read_unlock(); | |
f2842ab5 JA |
1849 | } |
1850 | ||
9aa8dfde PB |
1851 | static inline void io_cqring_wake(struct io_ring_ctx *ctx) |
1852 | { | |
1853 | /* | |
1854 | * wake_up_all() may seem excessive, but io_wake_function() and | |
1855 | * io_should_wake() handle the termination of the loop and only | |
1856 | * wake as many waiters as we need to. | |
1857 | */ | |
1858 | if (wq_has_sleeper(&ctx->cq_wait)) | |
1859 | wake_up_all(&ctx->cq_wait); | |
1860 | } | |
1861 | ||
2c5d763c JA |
1862 | /* |
1863 | * This should only get called when at least one event has been posted. | |
1864 | * Some applications rely on the eventfd notification count only changing | |
1865 | * IFF a new CQE has been added to the CQ ring. There's no depedency on | |
1866 | * 1:1 relationship between how many times this function is called (and | |
1867 | * hence the eventfd count) and number of CQEs posted to the CQ ring. | |
1868 | */ | |
66fc25ca | 1869 | static inline void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
1d7bb1d5 | 1870 | { |
9aa8dfde PB |
1871 | if (unlikely(ctx->off_timeout_used || ctx->drain_active || |
1872 | ctx->has_evfd)) | |
9333f6b4 PB |
1873 | __io_commit_cqring_flush(ctx); |
1874 | ||
9aa8dfde | 1875 | io_cqring_wake(ctx); |
1d7bb1d5 JA |
1876 | } |
1877 | ||
80c18e4a PB |
1878 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
1879 | { | |
9aa8dfde PB |
1880 | if (unlikely(ctx->off_timeout_used || ctx->drain_active || |
1881 | ctx->has_evfd)) | |
9333f6b4 PB |
1882 | __io_commit_cqring_flush(ctx); |
1883 | ||
9aa8dfde PB |
1884 | if (ctx->flags & IORING_SETUP_SQPOLL) |
1885 | io_cqring_wake(ctx); | |
80c18e4a PB |
1886 | } |
1887 | ||
c4a2ed72 | 1888 | /* Returns true if there are no backlogged entries after the flush */ |
6c2450ae | 1889 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force) |
1d7bb1d5 | 1890 | { |
b18032bb | 1891 | bool all_flushed, posted; |
1d7bb1d5 | 1892 | |
a566c556 | 1893 | if (!force && __io_cqring_events(ctx) == ctx->cq_entries) |
e23de15f | 1894 | return false; |
1d7bb1d5 | 1895 | |
b18032bb | 1896 | posted = false; |
79ebeaee | 1897 | spin_lock(&ctx->completion_lock); |
6c2450ae | 1898 | while (!list_empty(&ctx->cq_overflow_list)) { |
d068b506 | 1899 | struct io_uring_cqe *cqe = io_get_cqe(ctx); |
6c2450ae | 1900 | struct io_overflow_cqe *ocqe; |
e6c8aa9a | 1901 | |
1d7bb1d5 JA |
1902 | if (!cqe && !force) |
1903 | break; | |
6c2450ae PB |
1904 | ocqe = list_first_entry(&ctx->cq_overflow_list, |
1905 | struct io_overflow_cqe, list); | |
1906 | if (cqe) | |
1907 | memcpy(cqe, &ocqe->cqe, sizeof(*cqe)); | |
1908 | else | |
8f6ed49a PB |
1909 | io_account_cq_overflow(ctx); |
1910 | ||
b18032bb | 1911 | posted = true; |
6c2450ae PB |
1912 | list_del(&ocqe->list); |
1913 | kfree(ocqe); | |
1d7bb1d5 JA |
1914 | } |
1915 | ||
09e88404 PB |
1916 | all_flushed = list_empty(&ctx->cq_overflow_list); |
1917 | if (all_flushed) { | |
5ed7a37d | 1918 | clear_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
1919 | WRITE_ONCE(ctx->rings->sq_flags, |
1920 | ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW); | |
09e88404 | 1921 | } |
46930143 | 1922 | |
b18032bb JA |
1923 | if (posted) |
1924 | io_commit_cqring(ctx); | |
79ebeaee | 1925 | spin_unlock(&ctx->completion_lock); |
b18032bb JA |
1926 | if (posted) |
1927 | io_cqring_ev_posted(ctx); | |
09e88404 | 1928 | return all_flushed; |
1d7bb1d5 JA |
1929 | } |
1930 | ||
90f67366 | 1931 | static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx) |
6c503150 | 1932 | { |
ca0a2651 JA |
1933 | bool ret = true; |
1934 | ||
5ed7a37d | 1935 | if (test_bit(0, &ctx->check_cq_overflow)) { |
6c503150 PB |
1936 | /* iopoll syncs against uring_lock, not completion_lock */ |
1937 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1938 | mutex_lock(&ctx->uring_lock); | |
90f67366 | 1939 | ret = __io_cqring_overflow_flush(ctx, false); |
6c503150 PB |
1940 | if (ctx->flags & IORING_SETUP_IOPOLL) |
1941 | mutex_unlock(&ctx->uring_lock); | |
1942 | } | |
ca0a2651 JA |
1943 | |
1944 | return ret; | |
6c503150 PB |
1945 | } |
1946 | ||
6a290a14 PB |
1947 | /* must to be called somewhat shortly after putting a request */ |
1948 | static inline void io_put_task(struct task_struct *task, int nr) | |
1949 | { | |
1950 | struct io_uring_task *tctx = task->io_uring; | |
1951 | ||
e98e49b2 PB |
1952 | if (likely(task == current)) { |
1953 | tctx->cached_refs += nr; | |
1954 | } else { | |
1955 | percpu_counter_sub(&tctx->inflight, nr); | |
1956 | if (unlikely(atomic_read(&tctx->in_idle))) | |
1957 | wake_up(&tctx->wait); | |
1958 | put_task_struct_many(task, nr); | |
1959 | } | |
6a290a14 PB |
1960 | } |
1961 | ||
9a10867a PB |
1962 | static void io_task_refs_refill(struct io_uring_task *tctx) |
1963 | { | |
1964 | unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR; | |
1965 | ||
1966 | percpu_counter_add(&tctx->inflight, refill); | |
1967 | refcount_add(refill, ¤t->usage); | |
1968 | tctx->cached_refs += refill; | |
1969 | } | |
1970 | ||
1971 | static inline void io_get_task_refs(int nr) | |
1972 | { | |
1973 | struct io_uring_task *tctx = current->io_uring; | |
1974 | ||
1975 | tctx->cached_refs -= nr; | |
1976 | if (unlikely(tctx->cached_refs < 0)) | |
1977 | io_task_refs_refill(tctx); | |
1978 | } | |
1979 | ||
3cc7fdb9 PB |
1980 | static __cold void io_uring_drop_tctx_refs(struct task_struct *task) |
1981 | { | |
1982 | struct io_uring_task *tctx = task->io_uring; | |
1983 | unsigned int refs = tctx->cached_refs; | |
1984 | ||
1985 | if (refs) { | |
1986 | tctx->cached_refs = 0; | |
1987 | percpu_counter_sub(&tctx->inflight, refs); | |
1988 | put_task_struct_many(task, refs); | |
1989 | } | |
1990 | } | |
1991 | ||
d4d19c19 | 1992 | static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data, |
54daa9b2 | 1993 | s32 res, u32 cflags) |
2b188cc1 | 1994 | { |
cce4b8b0 | 1995 | struct io_overflow_cqe *ocqe; |
2b188cc1 | 1996 | |
cce4b8b0 PB |
1997 | ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT); |
1998 | if (!ocqe) { | |
1999 | /* | |
2000 | * If we're in ring overflow flush mode, or in task cancel mode, | |
2001 | * or cannot allocate an overflow entry, then we need to drop it | |
2002 | * on the floor. | |
2003 | */ | |
8f6ed49a | 2004 | io_account_cq_overflow(ctx); |
cce4b8b0 | 2005 | return false; |
2b188cc1 | 2006 | } |
cce4b8b0 | 2007 | if (list_empty(&ctx->cq_overflow_list)) { |
5ed7a37d | 2008 | set_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
2009 | WRITE_ONCE(ctx->rings->sq_flags, |
2010 | ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW); | |
2011 | ||
cce4b8b0 | 2012 | } |
d4d19c19 | 2013 | ocqe->cqe.user_data = user_data; |
cce4b8b0 PB |
2014 | ocqe->cqe.res = res; |
2015 | ocqe->cqe.flags = cflags; | |
2016 | list_add_tail(&ocqe->list, &ctx->cq_overflow_list); | |
2017 | return true; | |
2b188cc1 JA |
2018 | } |
2019 | ||
ae4da189 | 2020 | static inline bool __io_fill_cqe(struct io_ring_ctx *ctx, u64 user_data, |
913a571a | 2021 | s32 res, u32 cflags) |
2b188cc1 JA |
2022 | { |
2023 | struct io_uring_cqe *cqe; | |
2024 | ||
2025 | /* | |
2026 | * If we can't get a cq entry, userspace overflowed the | |
2027 | * submission (by quite a lot). Increment the overflow count in | |
2028 | * the ring. | |
2029 | */ | |
d068b506 | 2030 | cqe = io_get_cqe(ctx); |
1d7bb1d5 | 2031 | if (likely(cqe)) { |
d4d19c19 | 2032 | WRITE_ONCE(cqe->user_data, user_data); |
2b188cc1 | 2033 | WRITE_ONCE(cqe->res, res); |
bcda7baa | 2034 | WRITE_ONCE(cqe->flags, cflags); |
8d13326e | 2035 | return true; |
2b188cc1 | 2036 | } |
d4d19c19 | 2037 | return io_cqring_event_overflow(ctx, user_data, res, cflags); |
2b188cc1 JA |
2038 | } |
2039 | ||
ae4da189 | 2040 | static inline bool __io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags) |
d5ec1dfa | 2041 | { |
502c87d6 | 2042 | trace_io_uring_complete(req->ctx, req, req->user_data, res, cflags); |
ae4da189 | 2043 | return __io_fill_cqe(req->ctx, req->user_data, res, cflags); |
d5ec1dfa SR |
2044 | } |
2045 | ||
913a571a | 2046 | static noinline void io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags) |
bcda7baa | 2047 | { |
04c76b41 | 2048 | if (!(req->flags & REQ_F_CQE_SKIP)) |
ae4da189 | 2049 | __io_fill_cqe_req(req, res, cflags); |
bcda7baa JA |
2050 | } |
2051 | ||
913a571a PB |
2052 | static noinline bool io_fill_cqe_aux(struct io_ring_ctx *ctx, u64 user_data, |
2053 | s32 res, u32 cflags) | |
bcda7baa | 2054 | { |
913a571a | 2055 | ctx->cq_extra++; |
502c87d6 | 2056 | trace_io_uring_complete(ctx, NULL, user_data, res, cflags); |
ae4da189 | 2057 | return __io_fill_cqe(ctx, user_data, res, cflags); |
bcda7baa JA |
2058 | } |
2059 | ||
a37fae8a HX |
2060 | static void __io_req_complete_post(struct io_kiocb *req, s32 res, |
2061 | u32 cflags) | |
2b188cc1 | 2062 | { |
78e19bbe | 2063 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 | 2064 | |
04c76b41 | 2065 | if (!(req->flags & REQ_F_CQE_SKIP)) |
ae4da189 | 2066 | __io_fill_cqe_req(req, res, cflags); |
c7dae4ba JA |
2067 | /* |
2068 | * If we're the last reference to this request, add to our locked | |
2069 | * free_list cache. | |
2070 | */ | |
de9b4cca | 2071 | if (req_ref_put_and_test(req)) { |
7a612350 | 2072 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
0756a869 | 2073 | if (req->flags & IO_DISARM_MASK) |
7a612350 PB |
2074 | io_disarm_next(req); |
2075 | if (req->link) { | |
2076 | io_req_task_queue(req->link); | |
2077 | req->link = NULL; | |
2078 | } | |
2079 | } | |
ab409402 | 2080 | io_req_put_rsrc(req, ctx); |
8197b053 PB |
2081 | /* |
2082 | * Selected buffer deallocation in io_clean_op() assumes that | |
2083 | * we don't hold ->completion_lock. Clean them here to avoid | |
2084 | * deadlocks. | |
2085 | */ | |
2086 | io_put_kbuf_comp(req); | |
c7dae4ba JA |
2087 | io_dismantle_req(req); |
2088 | io_put_task(req->task, 1); | |
c2b6c6bc | 2089 | wq_list_add_head(&req->comp_list, &ctx->locked_free_list); |
d0acdee2 | 2090 | ctx->locked_free_nr++; |
180f829f | 2091 | } |
a37fae8a HX |
2092 | } |
2093 | ||
2094 | static void io_req_complete_post(struct io_kiocb *req, s32 res, | |
2095 | u32 cflags) | |
2096 | { | |
2097 | struct io_ring_ctx *ctx = req->ctx; | |
2098 | ||
2099 | spin_lock(&ctx->completion_lock); | |
2100 | __io_req_complete_post(req, res, cflags); | |
7a612350 | 2101 | io_commit_cqring(ctx); |
79ebeaee | 2102 | spin_unlock(&ctx->completion_lock); |
a3f34907 | 2103 | io_cqring_ev_posted(ctx); |
4e3d9ff9 JA |
2104 | } |
2105 | ||
54daa9b2 PB |
2106 | static inline void io_req_complete_state(struct io_kiocb *req, s32 res, |
2107 | u32 cflags) | |
229a7b63 | 2108 | { |
a38d68db | 2109 | req->result = res; |
d17e56eb | 2110 | req->cflags = cflags; |
e342c807 | 2111 | req->flags |= REQ_F_COMPLETE_INLINE; |
e1e16097 JA |
2112 | } |
2113 | ||
889fca73 | 2114 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
54daa9b2 | 2115 | s32 res, u32 cflags) |
bcda7baa | 2116 | { |
889fca73 PB |
2117 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
2118 | io_req_complete_state(req, res, cflags); | |
a38d68db | 2119 | else |
c7dae4ba | 2120 | io_req_complete_post(req, res, cflags); |
bcda7baa JA |
2121 | } |
2122 | ||
54daa9b2 | 2123 | static inline void io_req_complete(struct io_kiocb *req, s32 res) |
0ddf92e8 | 2124 | { |
889fca73 | 2125 | __io_req_complete(req, 0, res, 0); |
0ddf92e8 JA |
2126 | } |
2127 | ||
54daa9b2 | 2128 | static void io_req_complete_failed(struct io_kiocb *req, s32 res) |
f41db273 | 2129 | { |
93d2bcd2 | 2130 | req_set_fail(req); |
ab0ac095 | 2131 | io_req_complete_post(req, res, io_put_kbuf(req, IO_URING_F_UNLOCKED)); |
f41db273 PB |
2132 | } |
2133 | ||
c6d3d9cb PB |
2134 | static void io_req_complete_fail_submit(struct io_kiocb *req) |
2135 | { | |
2136 | /* | |
2137 | * We don't submit, fail them all, for that replace hardlinks with | |
2138 | * normal links. Extra REQ_F_LINK is tolerated. | |
2139 | */ | |
2140 | req->flags &= ~REQ_F_HARDLINK; | |
2141 | req->flags |= REQ_F_LINK; | |
2142 | io_req_complete_failed(req, req->result); | |
2143 | } | |
2144 | ||
864ea921 PB |
2145 | /* |
2146 | * Don't initialise the fields below on every allocation, but do that in | |
2147 | * advance and keep them valid across allocations. | |
2148 | */ | |
2149 | static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx) | |
2150 | { | |
2151 | req->ctx = ctx; | |
2152 | req->link = NULL; | |
2153 | req->async_data = NULL; | |
2154 | /* not necessary, but safer to zero */ | |
2155 | req->result = 0; | |
2156 | } | |
2157 | ||
dac7a098 | 2158 | static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx, |
cd0ca2e0 | 2159 | struct io_submit_state *state) |
dac7a098 | 2160 | { |
79ebeaee | 2161 | spin_lock(&ctx->completion_lock); |
c2b6c6bc | 2162 | wq_list_splice(&ctx->locked_free_list, &state->free_list); |
d0acdee2 | 2163 | ctx->locked_free_nr = 0; |
79ebeaee | 2164 | spin_unlock(&ctx->completion_lock); |
dac7a098 PB |
2165 | } |
2166 | ||
dd78f492 | 2167 | /* Returns true IFF there are requests in the cache */ |
c7dae4ba | 2168 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
0ddf92e8 | 2169 | { |
c7dae4ba | 2170 | struct io_submit_state *state = &ctx->submit_state; |
0ddf92e8 | 2171 | |
c7dae4ba JA |
2172 | /* |
2173 | * If we have more than a batch's worth of requests in our IRQ side | |
2174 | * locked cache, grab the lock and move them over to our submission | |
2175 | * side cache. | |
2176 | */ | |
d0acdee2 | 2177 | if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH) |
cd0ca2e0 | 2178 | io_flush_cached_locked_reqs(ctx, state); |
c2b6c6bc | 2179 | return !!state->free_list.next; |
0ddf92e8 JA |
2180 | } |
2181 | ||
5d5901a3 PB |
2182 | /* |
2183 | * A request might get retired back into the request caches even before opcode | |
2184 | * handlers and io_issue_sqe() are done with it, e.g. inline completion path. | |
2185 | * Because of that, io_alloc_req() should be called only under ->uring_lock | |
2186 | * and with extra caution to not get a request that is still worked on. | |
2187 | */ | |
c072481d | 2188 | static __cold bool __io_alloc_req_refill(struct io_ring_ctx *ctx) |
5d5901a3 | 2189 | __must_hold(&ctx->uring_lock) |
2b188cc1 | 2190 | { |
e5d1bc0a | 2191 | struct io_submit_state *state = &ctx->submit_state; |
864ea921 | 2192 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
3ab665b7 PB |
2193 | void *reqs[IO_REQ_ALLOC_BATCH]; |
2194 | struct io_kiocb *req; | |
864ea921 | 2195 | int ret, i; |
e5d1bc0a | 2196 | |
c2b6c6bc | 2197 | if (likely(state->free_list.next || io_flush_cached_reqs(ctx))) |
a33ae9ce | 2198 | return true; |
e5d1bc0a | 2199 | |
3ab665b7 | 2200 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, ARRAY_SIZE(reqs), reqs); |
fd6fab2c | 2201 | |
864ea921 PB |
2202 | /* |
2203 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
2204 | * retry single alloc to be on the safe side. | |
2205 | */ | |
2206 | if (unlikely(ret <= 0)) { | |
3ab665b7 PB |
2207 | reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
2208 | if (!reqs[0]) | |
a33ae9ce | 2209 | return false; |
864ea921 | 2210 | ret = 1; |
2b188cc1 | 2211 | } |
864ea921 | 2212 | |
37f0e767 | 2213 | percpu_ref_get_many(&ctx->refs, ret); |
3ab665b7 PB |
2214 | for (i = 0; i < ret; i++) { |
2215 | req = reqs[i]; | |
2216 | ||
2217 | io_preinit_req(req, ctx); | |
c2b6c6bc | 2218 | wq_stack_add_head(&req->comp_list, &state->free_list); |
3ab665b7 | 2219 | } |
a33ae9ce PB |
2220 | return true; |
2221 | } | |
2222 | ||
2223 | static inline bool io_alloc_req_refill(struct io_ring_ctx *ctx) | |
2224 | { | |
2225 | if (unlikely(!ctx->submit_state.free_list.next)) | |
2226 | return __io_alloc_req_refill(ctx); | |
2227 | return true; | |
2228 | } | |
2229 | ||
2230 | static inline struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) | |
2231 | { | |
2232 | struct io_wq_work_node *node; | |
2233 | ||
2234 | node = wq_stack_extract(&ctx->submit_state.free_list); | |
c2b6c6bc | 2235 | return container_of(node, struct io_kiocb, comp_list); |
2b188cc1 JA |
2236 | } |
2237 | ||
e1d767f0 | 2238 | static inline void io_put_file(struct file *file) |
8da11c19 | 2239 | { |
e1d767f0 | 2240 | if (file) |
8da11c19 PB |
2241 | fput(file); |
2242 | } | |
2243 | ||
6b639522 | 2244 | static inline void io_dismantle_req(struct io_kiocb *req) |
2b188cc1 | 2245 | { |
094bae49 | 2246 | unsigned int flags = req->flags; |
929a3af9 | 2247 | |
867f8fa5 | 2248 | if (unlikely(flags & IO_REQ_CLEAN_FLAGS)) |
3a0a6902 | 2249 | io_clean_op(req); |
e1d767f0 PB |
2250 | if (!(flags & REQ_F_FIXED_FILE)) |
2251 | io_put_file(req->file); | |
e65ef56d JA |
2252 | } |
2253 | ||
c072481d | 2254 | static __cold void __io_free_req(struct io_kiocb *req) |
c6ca97b3 | 2255 | { |
51a4cc11 | 2256 | struct io_ring_ctx *ctx = req->ctx; |
c6ca97b3 | 2257 | |
ab409402 | 2258 | io_req_put_rsrc(req, ctx); |
216578e5 | 2259 | io_dismantle_req(req); |
7c660731 | 2260 | io_put_task(req->task, 1); |
c6ca97b3 | 2261 | |
79ebeaee | 2262 | spin_lock(&ctx->completion_lock); |
c2b6c6bc | 2263 | wq_list_add_head(&req->comp_list, &ctx->locked_free_list); |
c34b025f | 2264 | ctx->locked_free_nr++; |
79ebeaee | 2265 | spin_unlock(&ctx->completion_lock); |
e65ef56d JA |
2266 | } |
2267 | ||
f2f87370 PB |
2268 | static inline void io_remove_next_linked(struct io_kiocb *req) |
2269 | { | |
2270 | struct io_kiocb *nxt = req->link; | |
2271 | ||
2272 | req->link = nxt->link; | |
2273 | nxt->link = NULL; | |
2274 | } | |
2275 | ||
33cc89a9 PB |
2276 | static bool io_kill_linked_timeout(struct io_kiocb *req) |
2277 | __must_hold(&req->ctx->completion_lock) | |
89b263f6 | 2278 | __must_hold(&req->ctx->timeout_lock) |
2665abfd | 2279 | { |
33cc89a9 | 2280 | struct io_kiocb *link = req->link; |
f2f87370 | 2281 | |
b97e736a | 2282 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
c9abd7ad | 2283 | struct io_timeout_data *io = link->async_data; |
7c86ffee | 2284 | |
f2f87370 | 2285 | io_remove_next_linked(req); |
90cd7e42 | 2286 | link->timeout.head = NULL; |
fd9c7bc5 | 2287 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
ef9dd637 | 2288 | list_del(&link->timeout.list); |
04c76b41 | 2289 | /* leave REQ_F_CQE_SKIP to io_fill_cqe_req */ |
913a571a | 2290 | io_fill_cqe_req(link, -ECANCELED, 0); |
91c2f697 | 2291 | io_put_req_deferred(link); |
d4729fbd | 2292 | return true; |
c9abd7ad PB |
2293 | } |
2294 | } | |
d4729fbd | 2295 | return false; |
7c86ffee PB |
2296 | } |
2297 | ||
d148ca4b | 2298 | static void io_fail_links(struct io_kiocb *req) |
33cc89a9 | 2299 | __must_hold(&req->ctx->completion_lock) |
9e645e11 | 2300 | { |
33cc89a9 | 2301 | struct io_kiocb *nxt, *link = req->link; |
04c76b41 | 2302 | bool ignore_cqes = req->flags & REQ_F_SKIP_LINK_CQES; |
9e645e11 | 2303 | |
f2f87370 | 2304 | req->link = NULL; |
f2f87370 | 2305 | while (link) { |
a8295b98 HX |
2306 | long res = -ECANCELED; |
2307 | ||
2308 | if (link->flags & REQ_F_FAIL) | |
2309 | res = link->result; | |
2310 | ||
f2f87370 PB |
2311 | nxt = link->link; |
2312 | link->link = NULL; | |
2665abfd | 2313 | |
502c87d6 SR |
2314 | trace_io_uring_fail_link(req->ctx, req, req->user_data, |
2315 | req->opcode, link); | |
2316 | ||
04c76b41 PB |
2317 | if (!ignore_cqes) { |
2318 | link->flags &= ~REQ_F_CQE_SKIP; | |
2319 | io_fill_cqe_req(link, res, 0); | |
2320 | } | |
91c2f697 | 2321 | io_put_req_deferred(link); |
f2f87370 | 2322 | link = nxt; |
9e645e11 | 2323 | } |
33cc89a9 | 2324 | } |
9e645e11 | 2325 | |
33cc89a9 PB |
2326 | static bool io_disarm_next(struct io_kiocb *req) |
2327 | __must_hold(&req->ctx->completion_lock) | |
2328 | { | |
2329 | bool posted = false; | |
2330 | ||
0756a869 PB |
2331 | if (req->flags & REQ_F_ARM_LTIMEOUT) { |
2332 | struct io_kiocb *link = req->link; | |
2333 | ||
906c6caa | 2334 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
0756a869 PB |
2335 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
2336 | io_remove_next_linked(req); | |
04c76b41 | 2337 | /* leave REQ_F_CQE_SKIP to io_fill_cqe_req */ |
913a571a | 2338 | io_fill_cqe_req(link, -ECANCELED, 0); |
0756a869 PB |
2339 | io_put_req_deferred(link); |
2340 | posted = true; | |
2341 | } | |
2342 | } else if (req->flags & REQ_F_LINK_TIMEOUT) { | |
89b263f6 JA |
2343 | struct io_ring_ctx *ctx = req->ctx; |
2344 | ||
2345 | spin_lock_irq(&ctx->timeout_lock); | |
33cc89a9 | 2346 | posted = io_kill_linked_timeout(req); |
89b263f6 JA |
2347 | spin_unlock_irq(&ctx->timeout_lock); |
2348 | } | |
93d2bcd2 | 2349 | if (unlikely((req->flags & REQ_F_FAIL) && |
e4335ed3 | 2350 | !(req->flags & REQ_F_HARDLINK))) { |
33cc89a9 PB |
2351 | posted |= (req->link != NULL); |
2352 | io_fail_links(req); | |
2353 | } | |
2354 | return posted; | |
9e645e11 JA |
2355 | } |
2356 | ||
d81499bf PB |
2357 | static void __io_req_find_next_prep(struct io_kiocb *req) |
2358 | { | |
2359 | struct io_ring_ctx *ctx = req->ctx; | |
2360 | bool posted; | |
2361 | ||
2362 | spin_lock(&ctx->completion_lock); | |
2363 | posted = io_disarm_next(req); | |
2364 | if (posted) | |
33ce2aff | 2365 | io_commit_cqring(ctx); |
d81499bf PB |
2366 | spin_unlock(&ctx->completion_lock); |
2367 | if (posted) | |
2368 | io_cqring_ev_posted(ctx); | |
2369 | } | |
2370 | ||
2371 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) | |
c69f8dbe | 2372 | { |
33cc89a9 | 2373 | struct io_kiocb *nxt; |
944e58bf | 2374 | |
d81499bf PB |
2375 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
2376 | return NULL; | |
9e645e11 JA |
2377 | /* |
2378 | * If LINK is set, we have dependent requests in this chain. If we | |
2379 | * didn't fail this request, queue the first one up, moving any other | |
2380 | * dependencies to the next request. In case of failure, fail the rest | |
2381 | * of the chain. | |
2382 | */ | |
d81499bf PB |
2383 | if (unlikely(req->flags & IO_DISARM_MASK)) |
2384 | __io_req_find_next_prep(req); | |
33cc89a9 PB |
2385 | nxt = req->link; |
2386 | req->link = NULL; | |
2387 | return nxt; | |
4d7dd462 | 2388 | } |
9e645e11 | 2389 | |
f237c30a | 2390 | static void ctx_flush_and_put(struct io_ring_ctx *ctx, bool *locked) |
2c32395d PB |
2391 | { |
2392 | if (!ctx) | |
2393 | return; | |
f237c30a | 2394 | if (*locked) { |
c450178d | 2395 | io_submit_flush_completions(ctx); |
2c32395d | 2396 | mutex_unlock(&ctx->uring_lock); |
f237c30a | 2397 | *locked = false; |
2c32395d PB |
2398 | } |
2399 | percpu_ref_put(&ctx->refs); | |
2400 | } | |
2401 | ||
f28c240e HX |
2402 | static inline void ctx_commit_and_unlock(struct io_ring_ctx *ctx) |
2403 | { | |
2404 | io_commit_cqring(ctx); | |
2405 | spin_unlock(&ctx->completion_lock); | |
2406 | io_cqring_ev_posted(ctx); | |
2407 | } | |
2408 | ||
2409 | static void handle_prev_tw_list(struct io_wq_work_node *node, | |
2410 | struct io_ring_ctx **ctx, bool *uring_locked) | |
2411 | { | |
2412 | if (*ctx && !*uring_locked) | |
2413 | spin_lock(&(*ctx)->completion_lock); | |
2414 | ||
2415 | do { | |
2416 | struct io_wq_work_node *next = node->next; | |
2417 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2418 | io_task_work.node); | |
2419 | ||
34d2bfe7 JA |
2420 | prefetch(container_of(next, struct io_kiocb, io_task_work.node)); |
2421 | ||
f28c240e HX |
2422 | if (req->ctx != *ctx) { |
2423 | if (unlikely(!*uring_locked && *ctx)) | |
2424 | ctx_commit_and_unlock(*ctx); | |
2425 | ||
2426 | ctx_flush_and_put(*ctx, uring_locked); | |
2427 | *ctx = req->ctx; | |
2428 | /* if not contended, grab and improve batching */ | |
2429 | *uring_locked = mutex_trylock(&(*ctx)->uring_lock); | |
2430 | percpu_ref_get(&(*ctx)->refs); | |
2431 | if (unlikely(!*uring_locked)) | |
2432 | spin_lock(&(*ctx)->completion_lock); | |
2433 | } | |
2434 | if (likely(*uring_locked)) | |
2435 | req->io_task_work.func(req, uring_locked); | |
2436 | else | |
cc3cec83 JA |
2437 | __io_req_complete_post(req, req->result, |
2438 | io_put_kbuf_comp(req)); | |
f28c240e HX |
2439 | node = next; |
2440 | } while (node); | |
2441 | ||
2442 | if (unlikely(!*uring_locked)) | |
2443 | ctx_commit_and_unlock(*ctx); | |
2444 | } | |
2445 | ||
2446 | static void handle_tw_list(struct io_wq_work_node *node, | |
2447 | struct io_ring_ctx **ctx, bool *locked) | |
9f8d032a HX |
2448 | { |
2449 | do { | |
2450 | struct io_wq_work_node *next = node->next; | |
2451 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2452 | io_task_work.node); | |
2453 | ||
34d2bfe7 JA |
2454 | prefetch(container_of(next, struct io_kiocb, io_task_work.node)); |
2455 | ||
9f8d032a HX |
2456 | if (req->ctx != *ctx) { |
2457 | ctx_flush_and_put(*ctx, locked); | |
2458 | *ctx = req->ctx; | |
2459 | /* if not contended, grab and improve batching */ | |
2460 | *locked = mutex_trylock(&(*ctx)->uring_lock); | |
2461 | percpu_ref_get(&(*ctx)->refs); | |
2462 | } | |
2463 | req->io_task_work.func(req, locked); | |
2464 | node = next; | |
2465 | } while (node); | |
2466 | } | |
2467 | ||
7cbf1722 | 2468 | static void tctx_task_work(struct callback_head *cb) |
c40f6379 | 2469 | { |
f28c240e | 2470 | bool uring_locked = false; |
ebd0df2e | 2471 | struct io_ring_ctx *ctx = NULL; |
3f18407d PB |
2472 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, |
2473 | task_work); | |
c40f6379 | 2474 | |
16f72070 | 2475 | while (1) { |
f28c240e | 2476 | struct io_wq_work_node *node1, *node2; |
3f18407d PB |
2477 | |
2478 | spin_lock_irq(&tctx->task_lock); | |
f28c240e HX |
2479 | node1 = tctx->prior_task_list.first; |
2480 | node2 = tctx->task_list.first; | |
3f18407d | 2481 | INIT_WQ_LIST(&tctx->task_list); |
f28c240e HX |
2482 | INIT_WQ_LIST(&tctx->prior_task_list); |
2483 | if (!node2 && !node1) | |
6294f368 | 2484 | tctx->task_running = false; |
3f18407d | 2485 | spin_unlock_irq(&tctx->task_lock); |
f28c240e | 2486 | if (!node2 && !node1) |
6294f368 | 2487 | break; |
3f18407d | 2488 | |
f28c240e HX |
2489 | if (node1) |
2490 | handle_prev_tw_list(node1, &ctx, &uring_locked); | |
f28c240e HX |
2491 | if (node2) |
2492 | handle_tw_list(node2, &ctx, &uring_locked); | |
7cbf1722 | 2493 | cond_resched(); |
68ca8fc0 PB |
2494 | |
2495 | if (!tctx->task_list.first && | |
2496 | !tctx->prior_task_list.first && uring_locked) | |
2497 | io_submit_flush_completions(ctx); | |
3f18407d | 2498 | } |
ebd0df2e | 2499 | |
f28c240e | 2500 | ctx_flush_and_put(ctx, &uring_locked); |
3cc7fdb9 PB |
2501 | |
2502 | /* relaxed read is enough as only the task itself sets ->in_idle */ | |
2503 | if (unlikely(atomic_read(&tctx->in_idle))) | |
2504 | io_uring_drop_tctx_refs(current); | |
7cbf1722 JA |
2505 | } |
2506 | ||
4813c377 | 2507 | static void io_req_task_work_add(struct io_kiocb *req, bool priority) |
7cbf1722 | 2508 | { |
c15b79de | 2509 | struct task_struct *tsk = req->task; |
7cbf1722 | 2510 | struct io_uring_task *tctx = tsk->io_uring; |
c15b79de | 2511 | enum task_work_notify_mode notify; |
e09ee510 | 2512 | struct io_wq_work_node *node; |
0b81e80c | 2513 | unsigned long flags; |
6294f368 | 2514 | bool running; |
7cbf1722 JA |
2515 | |
2516 | WARN_ON_ONCE(!tctx); | |
2517 | ||
d5361233 JA |
2518 | io_drop_inflight_file(req); |
2519 | ||
0b81e80c | 2520 | spin_lock_irqsave(&tctx->task_lock, flags); |
4813c377 HX |
2521 | if (priority) |
2522 | wq_list_add_tail(&req->io_task_work.node, &tctx->prior_task_list); | |
2523 | else | |
2524 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); | |
6294f368 PB |
2525 | running = tctx->task_running; |
2526 | if (!running) | |
2527 | tctx->task_running = true; | |
0b81e80c | 2528 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
2529 | |
2530 | /* task_work already pending, we're done */ | |
6294f368 | 2531 | if (running) |
e09ee510 | 2532 | return; |
7cbf1722 | 2533 | |
c15b79de PB |
2534 | /* |
2535 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For | |
2536 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're | |
2537 | * processing task_work. There's no reliable way to tell if TWA_RESUME | |
2538 | * will do the job. | |
2539 | */ | |
2540 | notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL; | |
d97ec623 PB |
2541 | if (likely(!task_work_add(tsk, &tctx->task_work, notify))) { |
2542 | if (notify == TWA_NONE) | |
2543 | wake_up_process(tsk); | |
e09ee510 | 2544 | return; |
c15b79de | 2545 | } |
2215bed9 | 2546 | |
0b81e80c | 2547 | spin_lock_irqsave(&tctx->task_lock, flags); |
6294f368 | 2548 | tctx->task_running = false; |
4813c377 | 2549 | node = wq_list_merge(&tctx->prior_task_list, &tctx->task_list); |
0b81e80c | 2550 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 | 2551 | |
e09ee510 PB |
2552 | while (node) { |
2553 | req = container_of(node, struct io_kiocb, io_task_work.node); | |
2554 | node = node->next; | |
2555 | if (llist_add(&req->io_task_work.fallback_node, | |
2556 | &req->ctx->fallback_llist)) | |
2557 | schedule_delayed_work(&req->ctx->fallback_work, 1); | |
2558 | } | |
eab30c4d PB |
2559 | } |
2560 | ||
f237c30a | 2561 | static void io_req_task_cancel(struct io_kiocb *req, bool *locked) |
c40f6379 | 2562 | { |
87ceb6a6 | 2563 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 | 2564 | |
b18a1a45 | 2565 | /* not needed for normal modes, but SQPOLL depends on it */ |
f237c30a | 2566 | io_tw_lock(ctx, locked); |
2593553a | 2567 | io_req_complete_failed(req, req->result); |
c40f6379 JA |
2568 | } |
2569 | ||
f237c30a | 2570 | static void io_req_task_submit(struct io_kiocb *req, bool *locked) |
c40f6379 JA |
2571 | { |
2572 | struct io_ring_ctx *ctx = req->ctx; | |
2573 | ||
f237c30a | 2574 | io_tw_lock(ctx, locked); |
316319e8 | 2575 | /* req->task == current here, checking PF_EXITING is safe */ |
af066f31 | 2576 | if (likely(!(req->task->flags & PF_EXITING))) |
c5eef2b9 | 2577 | __io_queue_sqe(req); |
81b6d05c | 2578 | else |
2593553a | 2579 | io_req_complete_failed(req, -EFAULT); |
c40f6379 JA |
2580 | } |
2581 | ||
2c4b8eb6 | 2582 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
c40f6379 | 2583 | { |
2c4b8eb6 | 2584 | req->result = ret; |
5b0a6acc | 2585 | req->io_task_work.func = io_req_task_cancel; |
4813c377 | 2586 | io_req_task_work_add(req, false); |
c40f6379 JA |
2587 | } |
2588 | ||
2c4b8eb6 | 2589 | static void io_req_task_queue(struct io_kiocb *req) |
a3df7698 | 2590 | { |
5b0a6acc | 2591 | req->io_task_work.func = io_req_task_submit; |
4813c377 | 2592 | io_req_task_work_add(req, false); |
a3df7698 PB |
2593 | } |
2594 | ||
773af691 JA |
2595 | static void io_req_task_queue_reissue(struct io_kiocb *req) |
2596 | { | |
2597 | req->io_task_work.func = io_queue_async_work; | |
4813c377 | 2598 | io_req_task_work_add(req, false); |
773af691 JA |
2599 | } |
2600 | ||
f2f87370 | 2601 | static inline void io_queue_next(struct io_kiocb *req) |
c69f8dbe | 2602 | { |
9b5f7bd9 | 2603 | struct io_kiocb *nxt = io_req_find_next(req); |
944e58bf PB |
2604 | |
2605 | if (nxt) | |
906a8c3f | 2606 | io_req_task_queue(nxt); |
c69f8dbe JL |
2607 | } |
2608 | ||
c3524383 | 2609 | static void io_free_req(struct io_kiocb *req) |
7a743e22 | 2610 | { |
c3524383 PB |
2611 | io_queue_next(req); |
2612 | __io_free_req(req); | |
2613 | } | |
8766dd51 | 2614 | |
f237c30a PB |
2615 | static void io_free_req_work(struct io_kiocb *req, bool *locked) |
2616 | { | |
2617 | io_free_req(req); | |
2618 | } | |
2619 | ||
3aa83bfb | 2620 | static void io_free_batch_list(struct io_ring_ctx *ctx, |
1cce17ac | 2621 | struct io_wq_work_node *node) |
3aa83bfb | 2622 | __must_hold(&ctx->uring_lock) |
5af1d13e | 2623 | { |
d4b7a5ef | 2624 | struct task_struct *task = NULL; |
37f0e767 | 2625 | int task_refs = 0; |
5af1d13e | 2626 | |
3aa83bfb PB |
2627 | do { |
2628 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2629 | comp_list); | |
2d6500d4 | 2630 | |
def77acf | 2631 | if (unlikely(req->flags & REQ_F_REFCOUNT)) { |
c1e53a69 | 2632 | node = req->comp_list.next; |
def77acf PB |
2633 | if (!req_ref_put_and_test(req)) |
2634 | continue; | |
c1e53a69 | 2635 | } |
2d6500d4 | 2636 | |
ab409402 | 2637 | io_req_put_rsrc_locked(req, ctx); |
d4b7a5ef PB |
2638 | io_queue_next(req); |
2639 | io_dismantle_req(req); | |
5af1d13e | 2640 | |
d4b7a5ef PB |
2641 | if (req->task != task) { |
2642 | if (task) | |
2643 | io_put_task(task, task_refs); | |
2644 | task = req->task; | |
2645 | task_refs = 0; | |
2646 | } | |
2647 | task_refs++; | |
c1e53a69 | 2648 | node = req->comp_list.next; |
d4b7a5ef | 2649 | wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list); |
3aa83bfb | 2650 | } while (node); |
d4b7a5ef | 2651 | |
d4b7a5ef PB |
2652 | if (task) |
2653 | io_put_task(task, task_refs); | |
7a743e22 PB |
2654 | } |
2655 | ||
c450178d | 2656 | static void __io_submit_flush_completions(struct io_ring_ctx *ctx) |
a141dd89 | 2657 | __must_hold(&ctx->uring_lock) |
905c172f | 2658 | { |
6f33b0bc | 2659 | struct io_wq_work_node *node, *prev; |
cd0ca2e0 | 2660 | struct io_submit_state *state = &ctx->submit_state; |
905c172f | 2661 | |
3d4aeb9f PB |
2662 | if (state->flush_cqes) { |
2663 | spin_lock(&ctx->completion_lock); | |
2664 | wq_list_for_each(node, prev, &state->compl_reqs) { | |
2665 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
6f33b0bc | 2666 | comp_list); |
5182ed2e | 2667 | |
3d4aeb9f | 2668 | if (!(req->flags & REQ_F_CQE_SKIP)) |
ae4da189 | 2669 | __io_fill_cqe_req(req, req->result, req->cflags); |
4d9237e3 JA |
2670 | if ((req->flags & REQ_F_POLLED) && req->apoll) { |
2671 | struct async_poll *apoll = req->apoll; | |
2672 | ||
2673 | if (apoll->double_poll) | |
2674 | kfree(apoll->double_poll); | |
2675 | list_add(&apoll->poll.wait.entry, | |
2676 | &ctx->apoll_cache); | |
2677 | req->flags &= ~REQ_F_POLLED; | |
2678 | } | |
3d4aeb9f PB |
2679 | } |
2680 | ||
2681 | io_commit_cqring(ctx); | |
2682 | spin_unlock(&ctx->completion_lock); | |
2683 | io_cqring_ev_posted(ctx); | |
2684 | state->flush_cqes = false; | |
905c172f | 2685 | } |
5182ed2e | 2686 | |
1cce17ac | 2687 | io_free_batch_list(ctx, state->compl_reqs.first); |
6f33b0bc | 2688 | INIT_WQ_LIST(&state->compl_reqs); |
7a743e22 PB |
2689 | } |
2690 | ||
ba816ad6 JA |
2691 | /* |
2692 | * Drop reference to request, return next in chain (if there is one) if this | |
2693 | * was the last reference to this request. | |
2694 | */ | |
0d85035a | 2695 | static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
e65ef56d | 2696 | { |
9b5f7bd9 PB |
2697 | struct io_kiocb *nxt = NULL; |
2698 | ||
de9b4cca | 2699 | if (req_ref_put_and_test(req)) { |
9b5f7bd9 | 2700 | nxt = io_req_find_next(req); |
4d7dd462 | 2701 | __io_free_req(req); |
2a44f467 | 2702 | } |
9b5f7bd9 | 2703 | return nxt; |
2b188cc1 JA |
2704 | } |
2705 | ||
0d85035a | 2706 | static inline void io_put_req(struct io_kiocb *req) |
e65ef56d | 2707 | { |
de9b4cca | 2708 | if (req_ref_put_and_test(req)) |
e65ef56d | 2709 | io_free_req(req); |
2b188cc1 JA |
2710 | } |
2711 | ||
91c2f697 | 2712 | static inline void io_put_req_deferred(struct io_kiocb *req) |
216578e5 | 2713 | { |
91c2f697 | 2714 | if (req_ref_put_and_test(req)) { |
f237c30a | 2715 | req->io_task_work.func = io_free_req_work; |
4813c377 | 2716 | io_req_task_work_add(req, false); |
543af3a1 | 2717 | } |
216578e5 PB |
2718 | } |
2719 | ||
6c503150 | 2720 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
a3a0e43f JA |
2721 | { |
2722 | /* See comment at the top of this file */ | |
2723 | smp_rmb(); | |
e23de15f | 2724 | return __io_cqring_events(ctx); |
a3a0e43f JA |
2725 | } |
2726 | ||
fb5ccc98 PB |
2727 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
2728 | { | |
2729 | struct io_rings *rings = ctx->rings; | |
2730 | ||
2731 | /* make sure SQ entry isn't read before tail */ | |
2732 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
2733 | } | |
2734 | ||
4c6e277c JA |
2735 | static inline bool io_run_task_work(void) |
2736 | { | |
7f62d40d | 2737 | if (test_thread_flag(TIF_NOTIFY_SIGNAL) || task_work_pending(current)) { |
4c6e277c | 2738 | __set_current_state(TASK_RUNNING); |
7c5d8fa6 EB |
2739 | clear_notify_signal(); |
2740 | if (task_work_pending(current)) | |
2741 | task_work_run(); | |
4c6e277c JA |
2742 | return true; |
2743 | } | |
2744 | ||
2745 | return false; | |
bcda7baa JA |
2746 | } |
2747 | ||
5ba3c874 | 2748 | static int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin) |
def596e9 | 2749 | { |
5eef4e87 | 2750 | struct io_wq_work_node *pos, *start, *prev; |
d729cf9a | 2751 | unsigned int poll_flags = BLK_POLL_NOSLEEP; |
b688f11e | 2752 | DEFINE_IO_COMP_BATCH(iob); |
5ba3c874 | 2753 | int nr_events = 0; |
def596e9 JA |
2754 | |
2755 | /* | |
2756 | * Only spin for completions if we don't have multiple devices hanging | |
87a115fb | 2757 | * off our complete list. |
def596e9 | 2758 | */ |
87a115fb | 2759 | if (ctx->poll_multi_queue || force_nonspin) |
ef99b2d3 | 2760 | poll_flags |= BLK_POLL_ONESHOT; |
def596e9 | 2761 | |
5eef4e87 PB |
2762 | wq_list_for_each(pos, start, &ctx->iopoll_list) { |
2763 | struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); | |
9adbd45d | 2764 | struct kiocb *kiocb = &req->rw.kiocb; |
a2416e1e | 2765 | int ret; |
def596e9 JA |
2766 | |
2767 | /* | |
581f9810 BM |
2768 | * Move completed and retryable entries to our local lists. |
2769 | * If we find a request that requires polling, break out | |
2770 | * and complete those lists first, if we have entries there. | |
def596e9 | 2771 | */ |
e3f721e6 | 2772 | if (READ_ONCE(req->iopoll_completed)) |
def596e9 JA |
2773 | break; |
2774 | ||
b688f11e | 2775 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, &iob, poll_flags); |
a2416e1e PB |
2776 | if (unlikely(ret < 0)) |
2777 | return ret; | |
2778 | else if (ret) | |
ef99b2d3 | 2779 | poll_flags |= BLK_POLL_ONESHOT; |
def596e9 | 2780 | |
3aadc23e | 2781 | /* iopoll may have completed current req */ |
b688f11e JA |
2782 | if (!rq_list_empty(iob.req_list) || |
2783 | READ_ONCE(req->iopoll_completed)) | |
e3f721e6 | 2784 | break; |
def596e9 JA |
2785 | } |
2786 | ||
b688f11e JA |
2787 | if (!rq_list_empty(iob.req_list)) |
2788 | iob.complete(&iob); | |
5eef4e87 PB |
2789 | else if (!pos) |
2790 | return 0; | |
def596e9 | 2791 | |
5eef4e87 PB |
2792 | prev = start; |
2793 | wq_list_for_each_resume(pos, prev) { | |
2794 | struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); | |
2795 | ||
b3fa03fd PB |
2796 | /* order with io_complete_rw_iopoll(), e.g. ->result updates */ |
2797 | if (!smp_load_acquire(&req->iopoll_completed)) | |
e3f721e6 | 2798 | break; |
c0713540 | 2799 | nr_events++; |
83a13a41 PB |
2800 | if (unlikely(req->flags & REQ_F_CQE_SKIP)) |
2801 | continue; | |
ae4da189 | 2802 | __io_fill_cqe_req(req, req->result, io_put_kbuf(req, 0)); |
e3f721e6 | 2803 | } |
def596e9 | 2804 | |
f5ed3bcd PB |
2805 | if (unlikely(!nr_events)) |
2806 | return 0; | |
2807 | ||
2808 | io_commit_cqring(ctx); | |
2809 | io_cqring_ev_posted_iopoll(ctx); | |
1cce17ac | 2810 | pos = start ? start->next : ctx->iopoll_list.first; |
5eef4e87 | 2811 | wq_list_cut(&ctx->iopoll_list, prev, start); |
1cce17ac | 2812 | io_free_batch_list(ctx, pos); |
5ba3c874 | 2813 | return nr_events; |
def596e9 JA |
2814 | } |
2815 | ||
def596e9 JA |
2816 | /* |
2817 | * We can't just wait for polled events to come to us, we have to actively | |
2818 | * find and complete them. | |
2819 | */ | |
c072481d | 2820 | static __cold void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
def596e9 JA |
2821 | { |
2822 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
2823 | return; | |
2824 | ||
2825 | mutex_lock(&ctx->uring_lock); | |
5eef4e87 | 2826 | while (!wq_list_empty(&ctx->iopoll_list)) { |
b2edc0a7 | 2827 | /* let it sleep and repeat later if can't complete a request */ |
5ba3c874 | 2828 | if (io_do_iopoll(ctx, true) == 0) |
b2edc0a7 | 2829 | break; |
08f5439f JA |
2830 | /* |
2831 | * Ensure we allow local-to-the-cpu processing to take place, | |
2832 | * in this case we need to ensure that we reap all events. | |
3fcee5a6 | 2833 | * Also let task_work, etc. to progress by releasing the mutex |
08f5439f | 2834 | */ |
3fcee5a6 PB |
2835 | if (need_resched()) { |
2836 | mutex_unlock(&ctx->uring_lock); | |
2837 | cond_resched(); | |
2838 | mutex_lock(&ctx->uring_lock); | |
2839 | } | |
def596e9 JA |
2840 | } |
2841 | mutex_unlock(&ctx->uring_lock); | |
2842 | } | |
2843 | ||
7668b92a | 2844 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
def596e9 | 2845 | { |
7668b92a | 2846 | unsigned int nr_events = 0; |
e9979b36 | 2847 | int ret = 0; |
500f9fba | 2848 | |
c7849be9 XW |
2849 | /* |
2850 | * We disallow the app entering submit/complete with polling, but we | |
2851 | * still need to lock the ring to prevent racing with polled issue | |
2852 | * that got punted to a workqueue. | |
2853 | */ | |
2854 | mutex_lock(&ctx->uring_lock); | |
f39c8a5b PB |
2855 | /* |
2856 | * Don't enter poll loop if we already have events pending. | |
2857 | * If we do, we can potentially be spinning for commands that | |
2858 | * already triggered a CQE (eg in error). | |
2859 | */ | |
5ed7a37d | 2860 | if (test_bit(0, &ctx->check_cq_overflow)) |
f39c8a5b PB |
2861 | __io_cqring_overflow_flush(ctx, false); |
2862 | if (io_cqring_events(ctx)) | |
2863 | goto out; | |
def596e9 | 2864 | do { |
500f9fba JA |
2865 | /* |
2866 | * If a submit got punted to a workqueue, we can have the | |
2867 | * application entering polling for a command before it gets | |
2868 | * issued. That app will hold the uring_lock for the duration | |
2869 | * of the poll right here, so we need to take a breather every | |
2870 | * now and then to ensure that the issue has a chance to add | |
2871 | * the poll to the issued list. Otherwise we can spin here | |
2872 | * forever, while the workqueue is stuck trying to acquire the | |
2873 | * very same mutex. | |
2874 | */ | |
5eef4e87 | 2875 | if (wq_list_empty(&ctx->iopoll_list)) { |
8f487ef2 PB |
2876 | u32 tail = ctx->cached_cq_tail; |
2877 | ||
500f9fba | 2878 | mutex_unlock(&ctx->uring_lock); |
4c6e277c | 2879 | io_run_task_work(); |
500f9fba | 2880 | mutex_lock(&ctx->uring_lock); |
def596e9 | 2881 | |
8f487ef2 PB |
2882 | /* some requests don't go through iopoll_list */ |
2883 | if (tail != ctx->cached_cq_tail || | |
5eef4e87 | 2884 | wq_list_empty(&ctx->iopoll_list)) |
e9979b36 | 2885 | break; |
500f9fba | 2886 | } |
5ba3c874 PB |
2887 | ret = io_do_iopoll(ctx, !min); |
2888 | if (ret < 0) | |
2889 | break; | |
2890 | nr_events += ret; | |
2891 | ret = 0; | |
2892 | } while (nr_events < min && !need_resched()); | |
f39c8a5b | 2893 | out: |
500f9fba | 2894 | mutex_unlock(&ctx->uring_lock); |
def596e9 JA |
2895 | return ret; |
2896 | } | |
2897 | ||
491381ce | 2898 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 2899 | { |
491381ce JA |
2900 | /* |
2901 | * Tell lockdep we inherited freeze protection from submission | |
2902 | * thread. | |
2903 | */ | |
2904 | if (req->flags & REQ_F_ISREG) { | |
1c98679d | 2905 | struct super_block *sb = file_inode(req->file)->i_sb; |
2b188cc1 | 2906 | |
1c98679d PB |
2907 | __sb_writers_acquired(sb, SB_FREEZE_WRITE); |
2908 | sb_end_write(sb); | |
2b188cc1 JA |
2909 | } |
2910 | } | |
2911 | ||
b63534c4 | 2912 | #ifdef CONFIG_BLOCK |
dc2a6e9a | 2913 | static bool io_resubmit_prep(struct io_kiocb *req) |
b63534c4 | 2914 | { |
ab454438 | 2915 | struct io_async_rw *rw = req->async_data; |
b63534c4 | 2916 | |
d886e185 | 2917 | if (!req_has_async_data(req)) |
ab454438 | 2918 | return !io_req_prep_async(req); |
538941e2 | 2919 | iov_iter_restore(&rw->s.iter, &rw->s.iter_state); |
ab454438 | 2920 | return true; |
b63534c4 | 2921 | } |
b63534c4 | 2922 | |
3e6a0d3c | 2923 | static bool io_rw_should_reissue(struct io_kiocb *req) |
b63534c4 | 2924 | { |
355afaeb | 2925 | umode_t mode = file_inode(req->file)->i_mode; |
3e6a0d3c | 2926 | struct io_ring_ctx *ctx = req->ctx; |
b63534c4 | 2927 | |
355afaeb JA |
2928 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
2929 | return false; | |
3e6a0d3c JA |
2930 | if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() && |
2931 | !(ctx->flags & IORING_SETUP_IOPOLL))) | |
b63534c4 | 2932 | return false; |
7c977a58 JA |
2933 | /* |
2934 | * If ref is dying, we might be running poll reap from the exit work. | |
2935 | * Don't attempt to reissue from that path, just let it fail with | |
2936 | * -EAGAIN. | |
2937 | */ | |
3e6a0d3c JA |
2938 | if (percpu_ref_is_dying(&ctx->refs)) |
2939 | return false; | |
ef046888 JA |
2940 | /* |
2941 | * Play it safe and assume not safe to re-import and reissue if we're | |
2942 | * not in the original thread group (or in task context). | |
2943 | */ | |
2944 | if (!same_thread_group(req->task, current) || !in_task()) | |
2945 | return false; | |
3e6a0d3c JA |
2946 | return true; |
2947 | } | |
e82ad485 | 2948 | #else |
a1ff1e3f | 2949 | static bool io_resubmit_prep(struct io_kiocb *req) |
e82ad485 JA |
2950 | { |
2951 | return false; | |
2952 | } | |
e82ad485 | 2953 | static bool io_rw_should_reissue(struct io_kiocb *req) |
3e6a0d3c | 2954 | { |
b63534c4 JA |
2955 | return false; |
2956 | } | |
3e6a0d3c | 2957 | #endif |
b63534c4 | 2958 | |
8ef12efe | 2959 | static bool __io_complete_rw_common(struct io_kiocb *req, long res) |
a1d7c393 | 2960 | { |
f63cf519 | 2961 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) { |
b65c128f | 2962 | kiocb_end_write(req); |
f63cf519 JA |
2963 | fsnotify_modify(req->file); |
2964 | } else { | |
2965 | fsnotify_access(req->file); | |
2966 | } | |
258f3a7f | 2967 | if (unlikely(res != req->result)) { |
9532b99b PB |
2968 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && |
2969 | io_rw_should_reissue(req)) { | |
2970 | req->flags |= REQ_F_REISSUE; | |
8ef12efe | 2971 | return true; |
9532b99b | 2972 | } |
93d2bcd2 | 2973 | req_set_fail(req); |
8ef12efe | 2974 | req->result = res; |
9532b99b | 2975 | } |
8ef12efe JA |
2976 | return false; |
2977 | } | |
2978 | ||
cc8e9ba7 | 2979 | static inline void io_req_task_complete(struct io_kiocb *req, bool *locked) |
8ef12efe | 2980 | { |
54daa9b2 | 2981 | int res = req->result; |
126180b9 PB |
2982 | |
2983 | if (*locked) { | |
cc3cec83 | 2984 | io_req_complete_state(req, res, io_put_kbuf(req, 0)); |
fff4e40e | 2985 | io_req_add_compl_list(req); |
126180b9 | 2986 | } else { |
cc3cec83 JA |
2987 | io_req_complete_post(req, res, |
2988 | io_put_kbuf(req, IO_URING_F_UNLOCKED)); | |
126180b9 | 2989 | } |
8ef12efe JA |
2990 | } |
2991 | ||
00f6e68b | 2992 | static void __io_complete_rw(struct io_kiocb *req, long res, |
8ef12efe JA |
2993 | unsigned int issue_flags) |
2994 | { | |
2995 | if (__io_complete_rw_common(req, res)) | |
2996 | return; | |
cc3cec83 JA |
2997 | __io_req_complete(req, issue_flags, req->result, |
2998 | io_put_kbuf(req, issue_flags)); | |
ba816ad6 JA |
2999 | } |
3000 | ||
6b19b766 | 3001 | static void io_complete_rw(struct kiocb *kiocb, long res) |
ba816ad6 | 3002 | { |
9adbd45d | 3003 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 | 3004 | |
8ef12efe JA |
3005 | if (__io_complete_rw_common(req, res)) |
3006 | return; | |
3007 | req->result = res; | |
3008 | req->io_task_work.func = io_req_task_complete; | |
f28c240e | 3009 | io_req_task_work_add(req, !!(req->ctx->flags & IORING_SETUP_SQPOLL)); |
2b188cc1 JA |
3010 | } |
3011 | ||
6b19b766 | 3012 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res) |
def596e9 | 3013 | { |
9adbd45d | 3014 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 3015 | |
491381ce JA |
3016 | if (kiocb->ki_flags & IOCB_WRITE) |
3017 | kiocb_end_write(req); | |
9532b99b | 3018 | if (unlikely(res != req->result)) { |
b66ceaf3 PB |
3019 | if (res == -EAGAIN && io_rw_should_reissue(req)) { |
3020 | req->flags |= REQ_F_REISSUE; | |
3021 | return; | |
9532b99b | 3022 | } |
258f3a7f | 3023 | req->result = res; |
8c130827 | 3024 | } |
bbde017a | 3025 | |
b3fa03fd PB |
3026 | /* order with io_iopoll_complete() checking ->iopoll_completed */ |
3027 | smp_store_release(&req->iopoll_completed, 1); | |
def596e9 JA |
3028 | } |
3029 | ||
3030 | /* | |
3031 | * After the iocb has been issued, it's safe to be found on the poll list. | |
3032 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
f39c8a5b | 3033 | * find it from a io_do_iopoll() thread before the issuer is done |
def596e9 JA |
3034 | * accessing the kiocb cookie. |
3035 | */ | |
9882131c | 3036 | static void io_iopoll_req_issued(struct io_kiocb *req, unsigned int issue_flags) |
def596e9 JA |
3037 | { |
3038 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 3039 | const bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
cb3d8972 PB |
3040 | |
3041 | /* workqueue context doesn't hold uring_lock, grab it now */ | |
3b44b371 | 3042 | if (unlikely(needs_lock)) |
cb3d8972 | 3043 | mutex_lock(&ctx->uring_lock); |
def596e9 JA |
3044 | |
3045 | /* | |
3046 | * Track whether we have multiple files in our lists. This will impact | |
3047 | * how we do polling eventually, not spinning if we're on potentially | |
3048 | * different devices. | |
3049 | */ | |
5eef4e87 | 3050 | if (wq_list_empty(&ctx->iopoll_list)) { |
915b3dde HX |
3051 | ctx->poll_multi_queue = false; |
3052 | } else if (!ctx->poll_multi_queue) { | |
def596e9 JA |
3053 | struct io_kiocb *list_req; |
3054 | ||
5eef4e87 PB |
3055 | list_req = container_of(ctx->iopoll_list.first, struct io_kiocb, |
3056 | comp_list); | |
30da1b45 | 3057 | if (list_req->file != req->file) |
915b3dde | 3058 | ctx->poll_multi_queue = true; |
def596e9 JA |
3059 | } |
3060 | ||
3061 | /* | |
3062 | * For fast devices, IO may have already completed. If it has, add | |
3063 | * it to the front so we find it first. | |
3064 | */ | |
65a6543d | 3065 | if (READ_ONCE(req->iopoll_completed)) |
5eef4e87 | 3066 | wq_list_add_head(&req->comp_list, &ctx->iopoll_list); |
def596e9 | 3067 | else |
5eef4e87 | 3068 | wq_list_add_tail(&req->comp_list, &ctx->iopoll_list); |
bdcd3eab | 3069 | |
3b44b371 | 3070 | if (unlikely(needs_lock)) { |
cb3d8972 PB |
3071 | /* |
3072 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handle | |
3073 | * in sq thread task context or in io worker task context. If | |
3074 | * current task context is sq thread, we don't need to check | |
3075 | * whether should wake up sq thread. | |
3076 | */ | |
3077 | if ((ctx->flags & IORING_SETUP_SQPOLL) && | |
3078 | wq_has_sleeper(&ctx->sq_data->wait)) | |
3079 | wake_up(&ctx->sq_data->wait); | |
3080 | ||
3081 | mutex_unlock(&ctx->uring_lock); | |
3082 | } | |
def596e9 JA |
3083 | } |
3084 | ||
4503b767 JA |
3085 | static bool io_bdev_nowait(struct block_device *bdev) |
3086 | { | |
9ba0d0c8 | 3087 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
4503b767 JA |
3088 | } |
3089 | ||
2b188cc1 JA |
3090 | /* |
3091 | * If we tracked the file through the SCM inflight mechanism, we could support | |
3092 | * any file. For now, just ensure that anything potentially problematic is done | |
3093 | * inline. | |
3094 | */ | |
88459b50 | 3095 | static bool __io_file_supports_nowait(struct file *file, umode_t mode) |
2b188cc1 | 3096 | { |
4503b767 | 3097 | if (S_ISBLK(mode)) { |
4e7b5671 CH |
3098 | if (IS_ENABLED(CONFIG_BLOCK) && |
3099 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) | |
4503b767 JA |
3100 | return true; |
3101 | return false; | |
3102 | } | |
976517f1 | 3103 | if (S_ISSOCK(mode)) |
2b188cc1 | 3104 | return true; |
4503b767 | 3105 | if (S_ISREG(mode)) { |
4e7b5671 CH |
3106 | if (IS_ENABLED(CONFIG_BLOCK) && |
3107 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && | |
4503b767 JA |
3108 | file->f_op != &io_uring_fops) |
3109 | return true; | |
3110 | return false; | |
3111 | } | |
2b188cc1 | 3112 | |
c5b85625 JA |
3113 | /* any ->read/write should understand O_NONBLOCK */ |
3114 | if (file->f_flags & O_NONBLOCK) | |
3115 | return true; | |
35645ac3 | 3116 | return file->f_mode & FMODE_NOWAIT; |
2b188cc1 | 3117 | } |
c5b85625 | 3118 | |
88459b50 PB |
3119 | /* |
3120 | * If we tracked the file through the SCM inflight mechanism, we could support | |
3121 | * any file. For now, just ensure that anything potentially problematic is done | |
3122 | * inline. | |
3123 | */ | |
3124 | static unsigned int io_file_get_flags(struct file *file) | |
3125 | { | |
3126 | umode_t mode = file_inode(file)->i_mode; | |
3127 | unsigned int res = 0; | |
af197f50 | 3128 | |
88459b50 PB |
3129 | if (S_ISREG(mode)) |
3130 | res |= FFS_ISREG; | |
3131 | if (__io_file_supports_nowait(file, mode)) | |
3132 | res |= FFS_NOWAIT; | |
3133 | return res; | |
2b188cc1 JA |
3134 | } |
3135 | ||
35645ac3 | 3136 | static inline bool io_file_supports_nowait(struct io_kiocb *req) |
7b29f92d | 3137 | { |
88459b50 | 3138 | return req->flags & REQ_F_SUPPORT_NOWAIT; |
7b29f92d JA |
3139 | } |
3140 | ||
b9a6b8f9 | 3141 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2b188cc1 | 3142 | { |
9adbd45d | 3143 | struct kiocb *kiocb = &req->rw.kiocb; |
09bb8394 JA |
3144 | unsigned ioprio; |
3145 | int ret; | |
2b188cc1 | 3146 | |
2b188cc1 | 3147 | kiocb->ki_pos = READ_ONCE(sqe->off); |
9adbd45d | 3148 | |
fb27274a PB |
3149 | ioprio = READ_ONCE(sqe->ioprio); |
3150 | if (ioprio) { | |
3151 | ret = ioprio_check_cap(ioprio); | |
3152 | if (ret) | |
3153 | return ret; | |
3154 | ||
3155 | kiocb->ki_ioprio = ioprio; | |
3156 | } else { | |
3157 | kiocb->ki_ioprio = get_current_ioprio(); | |
eae071c9 PB |
3158 | } |
3159 | ||
578c0ee2 | 3160 | req->imu = NULL; |
3529d8c2 JA |
3161 | req->rw.addr = READ_ONCE(sqe->addr); |
3162 | req->rw.len = READ_ONCE(sqe->len); | |
584b0180 | 3163 | req->rw.flags = READ_ONCE(sqe->rw_flags); |
4f4eeba8 | 3164 | req->buf_index = READ_ONCE(sqe->buf_index); |
2b188cc1 | 3165 | return 0; |
2b188cc1 JA |
3166 | } |
3167 | ||
3168 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
3169 | { | |
3170 | switch (ret) { | |
3171 | case -EIOCBQUEUED: | |
3172 | break; | |
3173 | case -ERESTARTSYS: | |
3174 | case -ERESTARTNOINTR: | |
3175 | case -ERESTARTNOHAND: | |
3176 | case -ERESTART_RESTARTBLOCK: | |
3177 | /* | |
3178 | * We can't just restart the syscall, since previously | |
3179 | * submitted sqes may already be in progress. Just fail this | |
3180 | * IO with EINTR. | |
3181 | */ | |
3182 | ret = -EINTR; | |
df561f66 | 3183 | fallthrough; |
2b188cc1 | 3184 | default: |
6b19b766 | 3185 | kiocb->ki_complete(kiocb, ret); |
2b188cc1 JA |
3186 | } |
3187 | } | |
3188 | ||
b4aec400 | 3189 | static inline loff_t *io_kiocb_update_pos(struct io_kiocb *req) |
d34e1e5b DY |
3190 | { |
3191 | struct kiocb *kiocb = &req->rw.kiocb; | |
3192 | ||
6f83ab22 JA |
3193 | if (kiocb->ki_pos != -1) |
3194 | return &kiocb->ki_pos; | |
3195 | ||
3196 | if (!(req->file->f_mode & FMODE_STREAM)) { | |
3197 | req->flags |= REQ_F_CUR_POS; | |
3198 | kiocb->ki_pos = req->file->f_pos; | |
3199 | return &kiocb->ki_pos; | |
d34e1e5b | 3200 | } |
6f83ab22 JA |
3201 | |
3202 | kiocb->ki_pos = 0; | |
3203 | return NULL; | |
d34e1e5b DY |
3204 | } |
3205 | ||
2ea537ca | 3206 | static void kiocb_done(struct io_kiocb *req, ssize_t ret, |
889fca73 | 3207 | unsigned int issue_flags) |
ba816ad6 | 3208 | { |
e8c2bc1f | 3209 | struct io_async_rw *io = req->async_data; |
ba04291e | 3210 | |
227c0c96 | 3211 | /* add previously done IO, if any */ |
d886e185 | 3212 | if (req_has_async_data(req) && io->bytes_done > 0) { |
227c0c96 | 3213 | if (ret < 0) |
e8c2bc1f | 3214 | ret = io->bytes_done; |
227c0c96 | 3215 | else |
e8c2bc1f | 3216 | ret += io->bytes_done; |
227c0c96 JA |
3217 | } |
3218 | ||
ba04291e | 3219 | if (req->flags & REQ_F_CUR_POS) |
2ea537ca PB |
3220 | req->file->f_pos = req->rw.kiocb.ki_pos; |
3221 | if (ret >= 0 && (req->rw.kiocb.ki_complete == io_complete_rw)) | |
00f6e68b | 3222 | __io_complete_rw(req, ret, issue_flags); |
ba816ad6 | 3223 | else |
2ea537ca | 3224 | io_rw_done(&req->rw.kiocb, ret); |
97284637 | 3225 | |
b66ceaf3 | 3226 | if (req->flags & REQ_F_REISSUE) { |
97284637 | 3227 | req->flags &= ~REQ_F_REISSUE; |
b91ef187 | 3228 | if (io_resubmit_prep(req)) |
773af691 | 3229 | io_req_task_queue_reissue(req); |
b91ef187 PB |
3230 | else |
3231 | io_req_task_queue_fail(req, ret); | |
97284637 | 3232 | } |
ba816ad6 JA |
3233 | } |
3234 | ||
eae071c9 PB |
3235 | static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter, |
3236 | struct io_mapped_ubuf *imu) | |
edafccee | 3237 | { |
9adbd45d | 3238 | size_t len = req->rw.len; |
75769e3f | 3239 | u64 buf_end, buf_addr = req->rw.addr; |
edafccee | 3240 | size_t offset; |
edafccee | 3241 | |
75769e3f | 3242 | if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end))) |
edafccee JA |
3243 | return -EFAULT; |
3244 | /* not inside the mapped region */ | |
4751f53d | 3245 | if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end)) |
edafccee JA |
3246 | return -EFAULT; |
3247 | ||
3248 | /* | |
3249 | * May not be a start of buffer, set size appropriately | |
3250 | * and advance us to the beginning. | |
3251 | */ | |
3252 | offset = buf_addr - imu->ubuf; | |
3253 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
3254 | |
3255 | if (offset) { | |
3256 | /* | |
3257 | * Don't use iov_iter_advance() here, as it's really slow for | |
3258 | * using the latter parts of a big fixed buffer - it iterates | |
3259 | * over each segment manually. We can cheat a bit here, because | |
3260 | * we know that: | |
3261 | * | |
3262 | * 1) it's a BVEC iter, we set it up | |
3263 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
3264 | * first and last bvec | |
3265 | * | |
3266 | * So just find our index, and adjust the iterator afterwards. | |
3267 | * If the offset is within the first bvec (or the whole first | |
3268 | * bvec, just use iov_iter_advance(). This makes it easier | |
3269 | * since we can just skip the first segment, which may not | |
3270 | * be PAGE_SIZE aligned. | |
3271 | */ | |
3272 | const struct bio_vec *bvec = imu->bvec; | |
3273 | ||
3274 | if (offset <= bvec->bv_len) { | |
3275 | iov_iter_advance(iter, offset); | |
3276 | } else { | |
3277 | unsigned long seg_skip; | |
3278 | ||
3279 | /* skip first vec */ | |
3280 | offset -= bvec->bv_len; | |
3281 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
3282 | ||
3283 | iter->bvec = bvec + seg_skip; | |
3284 | iter->nr_segs -= seg_skip; | |
99c79f66 | 3285 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 3286 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
3287 | } |
3288 | } | |
3289 | ||
847595de | 3290 | return 0; |
edafccee JA |
3291 | } |
3292 | ||
5106dd6e JA |
3293 | static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter, |
3294 | unsigned int issue_flags) | |
eae071c9 | 3295 | { |
eae071c9 PB |
3296 | struct io_mapped_ubuf *imu = req->imu; |
3297 | u16 index, buf_index = req->buf_index; | |
3298 | ||
3299 | if (likely(!imu)) { | |
578c0ee2 PB |
3300 | struct io_ring_ctx *ctx = req->ctx; |
3301 | ||
eae071c9 PB |
3302 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
3303 | return -EFAULT; | |
5106dd6e | 3304 | io_req_set_rsrc_node(req, ctx, issue_flags); |
eae071c9 PB |
3305 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
3306 | imu = READ_ONCE(ctx->user_bufs[index]); | |
3307 | req->imu = imu; | |
3308 | } | |
3309 | return __io_import_fixed(req, rw, iter, imu); | |
3310 | } | |
3311 | ||
bcda7baa JA |
3312 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
3313 | { | |
3314 | if (needs_lock) | |
3315 | mutex_unlock(&ctx->uring_lock); | |
3316 | } | |
3317 | ||
3318 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) | |
3319 | { | |
3320 | /* | |
3321 | * "Normal" inline submissions always hold the uring_lock, since we | |
3322 | * grab it from the system call. Same is true for the SQPOLL offload. | |
3323 | * The only exception is when we've detached the request and issue it | |
3324 | * from an async worker thread, grab the lock for that case. | |
3325 | */ | |
3326 | if (needs_lock) | |
3327 | mutex_lock(&ctx->uring_lock); | |
3328 | } | |
3329 | ||
dbc7d452 JA |
3330 | static void io_buffer_add_list(struct io_ring_ctx *ctx, |
3331 | struct io_buffer_list *bl, unsigned int bgid) | |
3332 | { | |
3333 | struct list_head *list; | |
3334 | ||
3335 | list = &ctx->io_buffers[hash_32(bgid, IO_BUFFERS_HASH_BITS)]; | |
3336 | INIT_LIST_HEAD(&bl->buf_list); | |
3337 | bl->bgid = bgid; | |
3338 | list_add(&bl->list, list); | |
3339 | } | |
3340 | ||
bcda7baa | 3341 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, |
51aac424 | 3342 | int bgid, unsigned int issue_flags) |
bcda7baa | 3343 | { |
30d51dd4 | 3344 | struct io_buffer *kbuf = req->kbuf; |
3b44b371 | 3345 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
dbc7d452 JA |
3346 | struct io_ring_ctx *ctx = req->ctx; |
3347 | struct io_buffer_list *bl; | |
bcda7baa JA |
3348 | |
3349 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
3350 | return kbuf; | |
3351 | ||
dbc7d452 | 3352 | io_ring_submit_lock(ctx, needs_lock); |
bcda7baa | 3353 | |
dbc7d452 | 3354 | lockdep_assert_held(&ctx->uring_lock); |
bcda7baa | 3355 | |
dbc7d452 JA |
3356 | bl = io_buffer_get_list(ctx, bgid); |
3357 | if (bl && !list_empty(&bl->buf_list)) { | |
3358 | kbuf = list_first_entry(&bl->buf_list, struct io_buffer, list); | |
3359 | list_del(&kbuf->list); | |
bcda7baa JA |
3360 | if (*len > kbuf->len) |
3361 | *len = kbuf->len; | |
30d51dd4 PB |
3362 | req->flags |= REQ_F_BUFFER_SELECTED; |
3363 | req->kbuf = kbuf; | |
bcda7baa JA |
3364 | } else { |
3365 | kbuf = ERR_PTR(-ENOBUFS); | |
3366 | } | |
3367 | ||
3368 | io_ring_submit_unlock(req->ctx, needs_lock); | |
bcda7baa JA |
3369 | return kbuf; |
3370 | } | |
3371 | ||
4d954c25 | 3372 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
51aac424 | 3373 | unsigned int issue_flags) |
4d954c25 JA |
3374 | { |
3375 | struct io_buffer *kbuf; | |
4f4eeba8 | 3376 | u16 bgid; |
4d954c25 | 3377 | |
4f4eeba8 | 3378 | bgid = req->buf_index; |
51aac424 | 3379 | kbuf = io_buffer_select(req, len, bgid, issue_flags); |
4d954c25 JA |
3380 | if (IS_ERR(kbuf)) |
3381 | return kbuf; | |
4d954c25 JA |
3382 | return u64_to_user_ptr(kbuf->addr); |
3383 | } | |
3384 | ||
3385 | #ifdef CONFIG_COMPAT | |
3386 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, | |
51aac424 | 3387 | unsigned int issue_flags) |
4d954c25 JA |
3388 | { |
3389 | struct compat_iovec __user *uiov; | |
3390 | compat_ssize_t clen; | |
3391 | void __user *buf; | |
3392 | ssize_t len; | |
3393 | ||
3394 | uiov = u64_to_user_ptr(req->rw.addr); | |
3395 | if (!access_ok(uiov, sizeof(*uiov))) | |
3396 | return -EFAULT; | |
3397 | if (__get_user(clen, &uiov->iov_len)) | |
3398 | return -EFAULT; | |
3399 | if (clen < 0) | |
3400 | return -EINVAL; | |
3401 | ||
3402 | len = clen; | |
51aac424 | 3403 | buf = io_rw_buffer_select(req, &len, issue_flags); |
4d954c25 JA |
3404 | if (IS_ERR(buf)) |
3405 | return PTR_ERR(buf); | |
3406 | iov[0].iov_base = buf; | |
3407 | iov[0].iov_len = (compat_size_t) len; | |
3408 | return 0; | |
3409 | } | |
3410 | #endif | |
3411 | ||
3412 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
51aac424 | 3413 | unsigned int issue_flags) |
4d954c25 JA |
3414 | { |
3415 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); | |
3416 | void __user *buf; | |
3417 | ssize_t len; | |
3418 | ||
3419 | if (copy_from_user(iov, uiov, sizeof(*uiov))) | |
3420 | return -EFAULT; | |
3421 | ||
3422 | len = iov[0].iov_len; | |
3423 | if (len < 0) | |
3424 | return -EINVAL; | |
51aac424 | 3425 | buf = io_rw_buffer_select(req, &len, issue_flags); |
4d954c25 JA |
3426 | if (IS_ERR(buf)) |
3427 | return PTR_ERR(buf); | |
3428 | iov[0].iov_base = buf; | |
3429 | iov[0].iov_len = len; | |
3430 | return 0; | |
3431 | } | |
3432 | ||
3433 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
51aac424 | 3434 | unsigned int issue_flags) |
4d954c25 | 3435 | { |
dddb3e26 | 3436 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
30d51dd4 | 3437 | struct io_buffer *kbuf = req->kbuf; |
dddb3e26 | 3438 | |
dddb3e26 JA |
3439 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
3440 | iov[0].iov_len = kbuf->len; | |
4d954c25 | 3441 | return 0; |
dddb3e26 | 3442 | } |
dd201662 | 3443 | if (req->rw.len != 1) |
4d954c25 JA |
3444 | return -EINVAL; |
3445 | ||
3446 | #ifdef CONFIG_COMPAT | |
3447 | if (req->ctx->compat) | |
51aac424 | 3448 | return io_compat_import(req, iov, issue_flags); |
4d954c25 JA |
3449 | #endif |
3450 | ||
51aac424 | 3451 | return __io_iov_buffer_select(req, iov, issue_flags); |
4d954c25 JA |
3452 | } |
3453 | ||
caa8fe6e PB |
3454 | static struct iovec *__io_import_iovec(int rw, struct io_kiocb *req, |
3455 | struct io_rw_state *s, | |
3456 | unsigned int issue_flags) | |
2b188cc1 | 3457 | { |
5e49c973 | 3458 | struct iov_iter *iter = &s->iter; |
847595de | 3459 | u8 opcode = req->opcode; |
caa8fe6e | 3460 | struct iovec *iovec; |
d1d681b0 PB |
3461 | void __user *buf; |
3462 | size_t sqe_len; | |
4d954c25 | 3463 | ssize_t ret; |
edafccee | 3464 | |
f3251183 | 3465 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
5106dd6e | 3466 | ret = io_import_fixed(req, rw, iter, issue_flags); |
f3251183 PB |
3467 | if (ret) |
3468 | return ERR_PTR(ret); | |
3469 | return NULL; | |
3470 | } | |
2b188cc1 | 3471 | |
bcda7baa | 3472 | /* buffer index only valid with fixed read/write, or buffer select */ |
d1d681b0 | 3473 | if (unlikely(req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))) |
caa8fe6e | 3474 | return ERR_PTR(-EINVAL); |
9adbd45d | 3475 | |
d1d681b0 PB |
3476 | buf = u64_to_user_ptr(req->rw.addr); |
3477 | sqe_len = req->rw.len; | |
9adbd45d | 3478 | |
3a6820f2 | 3479 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
bcda7baa | 3480 | if (req->flags & REQ_F_BUFFER_SELECT) { |
51aac424 | 3481 | buf = io_rw_buffer_select(req, &sqe_len, issue_flags); |
867a23ea | 3482 | if (IS_ERR(buf)) |
898df244 | 3483 | return ERR_CAST(buf); |
3f9d6441 | 3484 | req->rw.len = sqe_len; |
bcda7baa JA |
3485 | } |
3486 | ||
5e49c973 | 3487 | ret = import_single_range(rw, buf, sqe_len, s->fast_iov, iter); |
f3251183 PB |
3488 | if (ret) |
3489 | return ERR_PTR(ret); | |
3490 | return NULL; | |
3a6820f2 JA |
3491 | } |
3492 | ||
caa8fe6e | 3493 | iovec = s->fast_iov; |
4d954c25 | 3494 | if (req->flags & REQ_F_BUFFER_SELECT) { |
caa8fe6e | 3495 | ret = io_iov_buffer_select(req, iovec, issue_flags); |
f3251183 PB |
3496 | if (ret) |
3497 | return ERR_PTR(ret); | |
3498 | iov_iter_init(iter, rw, iovec, 1, iovec->iov_len); | |
3499 | return NULL; | |
4d954c25 JA |
3500 | } |
3501 | ||
caa8fe6e | 3502 | ret = __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, &iovec, iter, |
89cd35c5 | 3503 | req->ctx->compat); |
caa8fe6e PB |
3504 | if (unlikely(ret < 0)) |
3505 | return ERR_PTR(ret); | |
3506 | return iovec; | |
2b188cc1 JA |
3507 | } |
3508 | ||
5e49c973 PB |
3509 | static inline int io_import_iovec(int rw, struct io_kiocb *req, |
3510 | struct iovec **iovec, struct io_rw_state *s, | |
3511 | unsigned int issue_flags) | |
3512 | { | |
caa8fe6e PB |
3513 | *iovec = __io_import_iovec(rw, req, s, issue_flags); |
3514 | if (unlikely(IS_ERR(*iovec))) | |
3515 | return PTR_ERR(*iovec); | |
5e49c973 | 3516 | |
5e49c973 | 3517 | iov_iter_save_state(&s->iter, &s->iter_state); |
caa8fe6e | 3518 | return 0; |
2b188cc1 JA |
3519 | } |
3520 | ||
0fef9483 JA |
3521 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
3522 | { | |
5b09e37e | 3523 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
0fef9483 JA |
3524 | } |
3525 | ||
31b51510 | 3526 | /* |
32960613 JA |
3527 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
3528 | * by looping over ->read() or ->write() manually. | |
31b51510 | 3529 | */ |
4017eb91 | 3530 | static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) |
32960613 | 3531 | { |
4017eb91 JA |
3532 | struct kiocb *kiocb = &req->rw.kiocb; |
3533 | struct file *file = req->file; | |
32960613 | 3534 | ssize_t ret = 0; |
af9c45ec | 3535 | loff_t *ppos; |
32960613 JA |
3536 | |
3537 | /* | |
3538 | * Don't support polled IO through this interface, and we can't | |
3539 | * support non-blocking either. For the latter, this just causes | |
3540 | * the kiocb to be handled from an async context. | |
3541 | */ | |
3542 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3543 | return -EOPNOTSUPP; | |
35645ac3 PB |
3544 | if ((kiocb->ki_flags & IOCB_NOWAIT) && |
3545 | !(kiocb->ki_filp->f_flags & O_NONBLOCK)) | |
32960613 JA |
3546 | return -EAGAIN; |
3547 | ||
af9c45ec DY |
3548 | ppos = io_kiocb_ppos(kiocb); |
3549 | ||
32960613 | 3550 | while (iov_iter_count(iter)) { |
311ae9e1 | 3551 | struct iovec iovec; |
32960613 JA |
3552 | ssize_t nr; |
3553 | ||
311ae9e1 PB |
3554 | if (!iov_iter_is_bvec(iter)) { |
3555 | iovec = iov_iter_iovec(iter); | |
3556 | } else { | |
4017eb91 JA |
3557 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
3558 | iovec.iov_len = req->rw.len; | |
311ae9e1 PB |
3559 | } |
3560 | ||
32960613 JA |
3561 | if (rw == READ) { |
3562 | nr = file->f_op->read(file, iovec.iov_base, | |
af9c45ec | 3563 | iovec.iov_len, ppos); |
32960613 JA |
3564 | } else { |
3565 | nr = file->f_op->write(file, iovec.iov_base, | |
af9c45ec | 3566 | iovec.iov_len, ppos); |
32960613 JA |
3567 | } |
3568 | ||
3569 | if (nr < 0) { | |
3570 | if (!ret) | |
3571 | ret = nr; | |
3572 | break; | |
3573 | } | |
5e929367 | 3574 | ret += nr; |
16c8d2df JA |
3575 | if (!iov_iter_is_bvec(iter)) { |
3576 | iov_iter_advance(iter, nr); | |
3577 | } else { | |
16c8d2df | 3578 | req->rw.addr += nr; |
5e929367 JA |
3579 | req->rw.len -= nr; |
3580 | if (!req->rw.len) | |
3581 | break; | |
16c8d2df | 3582 | } |
32960613 JA |
3583 | if (nr != iovec.iov_len) |
3584 | break; | |
32960613 JA |
3585 | } |
3586 | ||
3587 | return ret; | |
3588 | } | |
3589 | ||
ff6165b2 JA |
3590 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
3591 | const struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 3592 | { |
e8c2bc1f | 3593 | struct io_async_rw *rw = req->async_data; |
b64e3444 | 3594 | |
538941e2 | 3595 | memcpy(&rw->s.iter, iter, sizeof(*iter)); |
afb87658 | 3596 | rw->free_iovec = iovec; |
227c0c96 | 3597 | rw->bytes_done = 0; |
ff6165b2 | 3598 | /* can only be fixed buffers, no need to do anything */ |
9c3a205c | 3599 | if (iov_iter_is_bvec(iter)) |
ff6165b2 | 3600 | return; |
b64e3444 | 3601 | if (!iovec) { |
ff6165b2 JA |
3602 | unsigned iov_off = 0; |
3603 | ||
538941e2 | 3604 | rw->s.iter.iov = rw->s.fast_iov; |
ff6165b2 JA |
3605 | if (iter->iov != fast_iov) { |
3606 | iov_off = iter->iov - fast_iov; | |
538941e2 | 3607 | rw->s.iter.iov += iov_off; |
ff6165b2 | 3608 | } |
538941e2 PB |
3609 | if (rw->s.fast_iov != fast_iov) |
3610 | memcpy(rw->s.fast_iov + iov_off, fast_iov + iov_off, | |
45097dae | 3611 | sizeof(struct iovec) * iter->nr_segs); |
99bc4c38 PB |
3612 | } else { |
3613 | req->flags |= REQ_F_NEED_CLEANUP; | |
f67676d1 JA |
3614 | } |
3615 | } | |
3616 | ||
8d4af685 | 3617 | static inline bool io_alloc_async_data(struct io_kiocb *req) |
3d9932a8 | 3618 | { |
e8c2bc1f JA |
3619 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
3620 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); | |
d886e185 PB |
3621 | if (req->async_data) { |
3622 | req->flags |= REQ_F_ASYNC_DATA; | |
3623 | return false; | |
3624 | } | |
3625 | return true; | |
3d9932a8 XW |
3626 | } |
3627 | ||
ff6165b2 | 3628 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
c88598a9 | 3629 | struct io_rw_state *s, bool force) |
b7bb4f7d | 3630 | { |
26f0505a | 3631 | if (!force && !io_op_defs[req->opcode].needs_async_setup) |
74566df3 | 3632 | return 0; |
d886e185 | 3633 | if (!req_has_async_data(req)) { |
cd658695 JA |
3634 | struct io_async_rw *iorw; |
3635 | ||
6cb78689 | 3636 | if (io_alloc_async_data(req)) { |
6bf985dc | 3637 | kfree(iovec); |
5d204bcf | 3638 | return -ENOMEM; |
6bf985dc | 3639 | } |
b7bb4f7d | 3640 | |
c88598a9 | 3641 | io_req_map_rw(req, iovec, s->fast_iov, &s->iter); |
cd658695 JA |
3642 | iorw = req->async_data; |
3643 | /* we've copied and mapped the iter, ensure state is saved */ | |
538941e2 | 3644 | iov_iter_save_state(&iorw->s.iter, &iorw->s.iter_state); |
5d204bcf | 3645 | } |
b7bb4f7d | 3646 | return 0; |
f67676d1 JA |
3647 | } |
3648 | ||
73debe68 | 3649 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw) |
c3e330a4 | 3650 | { |
e8c2bc1f | 3651 | struct io_async_rw *iorw = req->async_data; |
5e49c973 | 3652 | struct iovec *iov; |
847595de | 3653 | int ret; |
c3e330a4 | 3654 | |
51aac424 | 3655 | /* submission path, ->uring_lock should already be taken */ |
3b44b371 | 3656 | ret = io_import_iovec(rw, req, &iov, &iorw->s, 0); |
c3e330a4 PB |
3657 | if (unlikely(ret < 0)) |
3658 | return ret; | |
3659 | ||
ab0b196c PB |
3660 | iorw->bytes_done = 0; |
3661 | iorw->free_iovec = iov; | |
3662 | if (iov) | |
3663 | req->flags |= REQ_F_NEED_CLEANUP; | |
c3e330a4 PB |
3664 | return 0; |
3665 | } | |
3666 | ||
c1dd91d1 | 3667 | /* |
ffdc8dab | 3668 | * This is our waitqueue callback handler, registered through __folio_lock_async() |
c1dd91d1 JA |
3669 | * when we initially tried to do the IO with the iocb armed our waitqueue. |
3670 | * This gets called when the page is unlocked, and we generally expect that to | |
3671 | * happen when the page IO is completed and the page is now uptodate. This will | |
3672 | * queue a task_work based retry of the operation, attempting to copy the data | |
3673 | * again. If the latter fails because the page was NOT uptodate, then we will | |
3674 | * do a thread based blocking retry of the operation. That's the unexpected | |
3675 | * slow path. | |
3676 | */ | |
bcf5a063 JA |
3677 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
3678 | int sync, void *arg) | |
3679 | { | |
3680 | struct wait_page_queue *wpq; | |
3681 | struct io_kiocb *req = wait->private; | |
bcf5a063 | 3682 | struct wait_page_key *key = arg; |
bcf5a063 JA |
3683 | |
3684 | wpq = container_of(wait, struct wait_page_queue, wait); | |
3685 | ||
cdc8fcb4 LT |
3686 | if (!wake_page_match(wpq, key)) |
3687 | return 0; | |
3688 | ||
c8d317aa | 3689 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
bcf5a063 | 3690 | list_del_init(&wait->entry); |
921b9054 | 3691 | io_req_task_queue(req); |
bcf5a063 JA |
3692 | return 1; |
3693 | } | |
3694 | ||
c1dd91d1 JA |
3695 | /* |
3696 | * This controls whether a given IO request should be armed for async page | |
3697 | * based retry. If we return false here, the request is handed to the async | |
3698 | * worker threads for retry. If we're doing buffered reads on a regular file, | |
3699 | * we prepare a private wait_page_queue entry and retry the operation. This | |
3700 | * will either succeed because the page is now uptodate and unlocked, or it | |
3701 | * will register a callback when the page is unlocked at IO completion. Through | |
3702 | * that callback, io_uring uses task_work to setup a retry of the operation. | |
3703 | * That retry will attempt the buffered read again. The retry will generally | |
3704 | * succeed, or in rare cases where it fails, we then fall back to using the | |
3705 | * async worker threads for a blocking retry. | |
3706 | */ | |
227c0c96 | 3707 | static bool io_rw_should_retry(struct io_kiocb *req) |
f67676d1 | 3708 | { |
e8c2bc1f JA |
3709 | struct io_async_rw *rw = req->async_data; |
3710 | struct wait_page_queue *wait = &rw->wpq; | |
bcf5a063 | 3711 | struct kiocb *kiocb = &req->rw.kiocb; |
f67676d1 | 3712 | |
bcf5a063 JA |
3713 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
3714 | if (req->flags & REQ_F_NOWAIT) | |
3715 | return false; | |
f67676d1 | 3716 | |
227c0c96 | 3717 | /* Only for buffered IO */ |
3b2a4439 | 3718 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
bcf5a063 | 3719 | return false; |
3b2a4439 | 3720 | |
bcf5a063 JA |
3721 | /* |
3722 | * just use poll if we can, and don't attempt if the fs doesn't | |
3723 | * support callback based unlocks | |
3724 | */ | |
3725 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) | |
3726 | return false; | |
f67676d1 | 3727 | |
3b2a4439 JA |
3728 | wait->wait.func = io_async_buf_func; |
3729 | wait->wait.private = req; | |
3730 | wait->wait.flags = 0; | |
3731 | INIT_LIST_HEAD(&wait->wait.entry); | |
3732 | kiocb->ki_flags |= IOCB_WAITQ; | |
c8d317aa | 3733 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3b2a4439 | 3734 | kiocb->ki_waitq = wait; |
3b2a4439 | 3735 | return true; |
bcf5a063 JA |
3736 | } |
3737 | ||
aeab9506 | 3738 | static inline int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) |
bcf5a063 | 3739 | { |
607b6fb8 | 3740 | if (likely(req->file->f_op->read_iter)) |
bcf5a063 | 3741 | return call_read_iter(req->file, &req->rw.kiocb, iter); |
2dd2111d | 3742 | else if (req->file->f_op->read) |
4017eb91 | 3743 | return loop_rw_iter(READ, req, iter); |
2dd2111d GH |
3744 | else |
3745 | return -EINVAL; | |
f67676d1 JA |
3746 | } |
3747 | ||
7db30437 ML |
3748 | static bool need_read_all(struct io_kiocb *req) |
3749 | { | |
3750 | return req->flags & REQ_F_ISREG || | |
3751 | S_ISBLK(file_inode(req->file)->i_mode); | |
3752 | } | |
3753 | ||
584b0180 JA |
3754 | static int io_rw_init_file(struct io_kiocb *req, fmode_t mode) |
3755 | { | |
3756 | struct kiocb *kiocb = &req->rw.kiocb; | |
3757 | struct io_ring_ctx *ctx = req->ctx; | |
3758 | struct file *file = req->file; | |
3759 | int ret; | |
3760 | ||
3761 | if (unlikely(!file || !(file->f_mode & mode))) | |
3762 | return -EBADF; | |
3763 | ||
3764 | if (!io_req_ffs_set(req)) | |
3765 | req->flags |= io_file_get_flags(file) << REQ_F_SUPPORT_NOWAIT_BIT; | |
3766 | ||
3767 | kiocb->ki_flags = iocb_flags(file); | |
3768 | ret = kiocb_set_rw_flags(kiocb, req->rw.flags); | |
3769 | if (unlikely(ret)) | |
3770 | return ret; | |
3771 | ||
3772 | /* | |
3773 | * If the file is marked O_NONBLOCK, still allow retry for it if it | |
3774 | * supports async. Otherwise it's impossible to use O_NONBLOCK files | |
3775 | * reliably. If not, or it IOCB_NOWAIT is set, don't retry. | |
3776 | */ | |
3777 | if ((kiocb->ki_flags & IOCB_NOWAIT) || | |
3778 | ((file->f_flags & O_NONBLOCK) && !io_file_supports_nowait(req))) | |
3779 | req->flags |= REQ_F_NOWAIT; | |
3780 | ||
3781 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
3782 | if (!(kiocb->ki_flags & IOCB_DIRECT) || !file->f_op->iopoll) | |
3783 | return -EOPNOTSUPP; | |
3784 | ||
3785 | kiocb->ki_flags |= IOCB_HIPRI | IOCB_ALLOC_CACHE; | |
3786 | kiocb->ki_complete = io_complete_rw_iopoll; | |
3787 | req->iopoll_completed = 0; | |
3788 | } else { | |
3789 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3790 | return -EINVAL; | |
3791 | kiocb->ki_complete = io_complete_rw; | |
3792 | } | |
3793 | ||
3794 | return 0; | |
3795 | } | |
3796 | ||
889fca73 | 3797 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 3798 | { |
607b6fb8 | 3799 | struct io_rw_state __s, *s = &__s; |
c88598a9 | 3800 | struct iovec *iovec; |
9adbd45d | 3801 | struct kiocb *kiocb = &req->rw.kiocb; |
45d189c6 | 3802 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
d886e185 | 3803 | struct io_async_rw *rw; |
cd658695 | 3804 | ssize_t ret, ret2; |
b4aec400 | 3805 | loff_t *ppos; |
ff6165b2 | 3806 | |
607b6fb8 PB |
3807 | if (!req_has_async_data(req)) { |
3808 | ret = io_import_iovec(READ, req, &iovec, s, issue_flags); | |
3809 | if (unlikely(ret < 0)) | |
3810 | return ret; | |
3811 | } else { | |
2be2eb02 JA |
3812 | /* |
3813 | * Safe and required to re-import if we're using provided | |
3814 | * buffers, as we dropped the selected one before retry. | |
3815 | */ | |
3816 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
3817 | ret = io_import_iovec(READ, req, &iovec, s, issue_flags); | |
3818 | if (unlikely(ret < 0)) | |
3819 | return ret; | |
3820 | } | |
3821 | ||
d886e185 | 3822 | rw = req->async_data; |
c88598a9 | 3823 | s = &rw->s; |
cd658695 JA |
3824 | /* |
3825 | * We come here from an earlier attempt, restore our state to | |
3826 | * match in case it doesn't. It's cheap enough that we don't | |
3827 | * need to make this conditional. | |
3828 | */ | |
c88598a9 | 3829 | iov_iter_restore(&s->iter, &s->iter_state); |
2846c481 | 3830 | iovec = NULL; |
2846c481 | 3831 | } |
584b0180 | 3832 | ret = io_rw_init_file(req, FMODE_READ); |
323b190b JA |
3833 | if (unlikely(ret)) { |
3834 | kfree(iovec); | |
584b0180 | 3835 | return ret; |
323b190b | 3836 | } |
c88598a9 | 3837 | req->result = iov_iter_count(&s->iter); |
2b188cc1 | 3838 | |
607b6fb8 PB |
3839 | if (force_nonblock) { |
3840 | /* If the file doesn't support async, just async punt */ | |
35645ac3 | 3841 | if (unlikely(!io_file_supports_nowait(req))) { |
607b6fb8 PB |
3842 | ret = io_setup_async_rw(req, iovec, s, true); |
3843 | return ret ?: -EAGAIN; | |
3844 | } | |
a88fc400 | 3845 | kiocb->ki_flags |= IOCB_NOWAIT; |
607b6fb8 PB |
3846 | } else { |
3847 | /* Ensure we clear previously set non-block flag */ | |
3848 | kiocb->ki_flags &= ~IOCB_NOWAIT; | |
6713e7a6 | 3849 | } |
9e645e11 | 3850 | |
b4aec400 | 3851 | ppos = io_kiocb_update_pos(req); |
d34e1e5b | 3852 | |
b4aec400 | 3853 | ret = rw_verify_area(READ, req->file, ppos, req->result); |
5ea5dd45 PB |
3854 | if (unlikely(ret)) { |
3855 | kfree(iovec); | |
3856 | return ret; | |
3857 | } | |
2b188cc1 | 3858 | |
c88598a9 | 3859 | ret = io_iter_do_read(req, &s->iter); |
32960613 | 3860 | |
230d50d4 | 3861 | if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) { |
6ad7f233 | 3862 | req->flags &= ~REQ_F_REISSUE; |
9af177ee JA |
3863 | /* if we can poll, just do that */ |
3864 | if (req->opcode == IORING_OP_READ && file_can_poll(req->file)) | |
3865 | return -EAGAIN; | |
eefdf30f JA |
3866 | /* IOPOLL retry should happen for io-wq threads */ |
3867 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
f91daf56 | 3868 | goto done; |
75c668cd PB |
3869 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3870 | if (req->flags & REQ_F_NOWAIT) | |
355afaeb | 3871 | goto done; |
f38c7e3a | 3872 | ret = 0; |
230d50d4 JA |
3873 | } else if (ret == -EIOCBQUEUED) { |
3874 | goto out_free; | |
f80a50a6 | 3875 | } else if (ret == req->result || ret <= 0 || !force_nonblock || |
7db30437 | 3876 | (req->flags & REQ_F_NOWAIT) || !need_read_all(req)) { |
7335e3bf | 3877 | /* read all, failed, already did sync or don't want to retry */ |
00d23d51 | 3878 | goto done; |
227c0c96 JA |
3879 | } |
3880 | ||
cd658695 JA |
3881 | /* |
3882 | * Don't depend on the iter state matching what was consumed, or being | |
3883 | * untouched in case of error. Restore it and we'll advance it | |
3884 | * manually if we need to. | |
3885 | */ | |
c88598a9 | 3886 | iov_iter_restore(&s->iter, &s->iter_state); |
cd658695 | 3887 | |
c88598a9 | 3888 | ret2 = io_setup_async_rw(req, iovec, s, true); |
6bf985dc PB |
3889 | if (ret2) |
3890 | return ret2; | |
3891 | ||
fe1cdd55 | 3892 | iovec = NULL; |
e8c2bc1f | 3893 | rw = req->async_data; |
c88598a9 | 3894 | s = &rw->s; |
cd658695 JA |
3895 | /* |
3896 | * Now use our persistent iterator and state, if we aren't already. | |
3897 | * We've restored and mapped the iter to match. | |
3898 | */ | |
227c0c96 | 3899 | |
b23df91b | 3900 | do { |
cd658695 JA |
3901 | /* |
3902 | * We end up here because of a partial read, either from | |
3903 | * above or inside this loop. Advance the iter by the bytes | |
3904 | * that were consumed. | |
3905 | */ | |
c88598a9 PB |
3906 | iov_iter_advance(&s->iter, ret); |
3907 | if (!iov_iter_count(&s->iter)) | |
cd658695 | 3908 | break; |
b23df91b | 3909 | rw->bytes_done += ret; |
c88598a9 | 3910 | iov_iter_save_state(&s->iter, &s->iter_state); |
cd658695 | 3911 | |
b23df91b PB |
3912 | /* if we can retry, do so with the callbacks armed */ |
3913 | if (!io_rw_should_retry(req)) { | |
3914 | kiocb->ki_flags &= ~IOCB_WAITQ; | |
3915 | return -EAGAIN; | |
3916 | } | |
3917 | ||
3918 | /* | |
3919 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If | |
3920 | * we get -EIOCBQUEUED, then we'll get a notification when the | |
3921 | * desired page gets unlocked. We can also get a partial read | |
3922 | * here, and if we do, then just retry at the new offset. | |
3923 | */ | |
c88598a9 | 3924 | ret = io_iter_do_read(req, &s->iter); |
b23df91b PB |
3925 | if (ret == -EIOCBQUEUED) |
3926 | return 0; | |
227c0c96 | 3927 | /* we got some bytes, but not all. retry. */ |
b5b0ecb7 | 3928 | kiocb->ki_flags &= ~IOCB_WAITQ; |
c88598a9 | 3929 | iov_iter_restore(&s->iter, &s->iter_state); |
cd658695 | 3930 | } while (ret > 0); |
227c0c96 | 3931 | done: |
2ea537ca | 3932 | kiocb_done(req, ret, issue_flags); |
fe1cdd55 PB |
3933 | out_free: |
3934 | /* it's faster to check here then delegate to kfree */ | |
3935 | if (iovec) | |
3936 | kfree(iovec); | |
5ea5dd45 | 3937 | return 0; |
2b188cc1 JA |
3938 | } |
3939 | ||
889fca73 | 3940 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 3941 | { |
607b6fb8 | 3942 | struct io_rw_state __s, *s = &__s; |
c88598a9 | 3943 | struct iovec *iovec; |
9adbd45d | 3944 | struct kiocb *kiocb = &req->rw.kiocb; |
45d189c6 | 3945 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
cd658695 | 3946 | ssize_t ret, ret2; |
b4aec400 | 3947 | loff_t *ppos; |
2b188cc1 | 3948 | |
607b6fb8 | 3949 | if (!req_has_async_data(req)) { |
5e49c973 PB |
3950 | ret = io_import_iovec(WRITE, req, &iovec, s, issue_flags); |
3951 | if (unlikely(ret < 0)) | |
2846c481 | 3952 | return ret; |
607b6fb8 PB |
3953 | } else { |
3954 | struct io_async_rw *rw = req->async_data; | |
3955 | ||
3956 | s = &rw->s; | |
3957 | iov_iter_restore(&s->iter, &s->iter_state); | |
2846c481 | 3958 | iovec = NULL; |
2846c481 | 3959 | } |
584b0180 | 3960 | ret = io_rw_init_file(req, FMODE_WRITE); |
323b190b JA |
3961 | if (unlikely(ret)) { |
3962 | kfree(iovec); | |
584b0180 | 3963 | return ret; |
323b190b | 3964 | } |
c88598a9 | 3965 | req->result = iov_iter_count(&s->iter); |
2b188cc1 | 3966 | |
607b6fb8 PB |
3967 | if (force_nonblock) { |
3968 | /* If the file doesn't support async, just async punt */ | |
35645ac3 | 3969 | if (unlikely(!io_file_supports_nowait(req))) |
607b6fb8 | 3970 | goto copy_iov; |
fd6c2e4c | 3971 | |
607b6fb8 PB |
3972 | /* file path doesn't support NOWAIT for non-direct_IO */ |
3973 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
3974 | (req->flags & REQ_F_ISREG)) | |
3975 | goto copy_iov; | |
31b51510 | 3976 | |
607b6fb8 PB |
3977 | kiocb->ki_flags |= IOCB_NOWAIT; |
3978 | } else { | |
3979 | /* Ensure we clear previously set non-block flag */ | |
3980 | kiocb->ki_flags &= ~IOCB_NOWAIT; | |
3981 | } | |
31b51510 | 3982 | |
b4aec400 | 3983 | ppos = io_kiocb_update_pos(req); |
d34e1e5b | 3984 | |
b4aec400 | 3985 | ret = rw_verify_area(WRITE, req->file, ppos, req->result); |
fa15bafb PB |
3986 | if (unlikely(ret)) |
3987 | goto out_free; | |
4ed734b0 | 3988 | |
fa15bafb PB |
3989 | /* |
3990 | * Open-code file_start_write here to grab freeze protection, | |
3991 | * which will be released by another thread in | |
3992 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
3993 | * released so that it doesn't complain about the held lock when | |
3994 | * we return to userspace. | |
3995 | */ | |
3996 | if (req->flags & REQ_F_ISREG) { | |
8a3c84b6 | 3997 | sb_start_write(file_inode(req->file)->i_sb); |
fa15bafb PB |
3998 | __sb_writers_release(file_inode(req->file)->i_sb, |
3999 | SB_FREEZE_WRITE); | |
4000 | } | |
4001 | kiocb->ki_flags |= IOCB_WRITE; | |
4ed734b0 | 4002 | |
35645ac3 | 4003 | if (likely(req->file->f_op->write_iter)) |
c88598a9 | 4004 | ret2 = call_write_iter(req->file, kiocb, &s->iter); |
2dd2111d | 4005 | else if (req->file->f_op->write) |
c88598a9 | 4006 | ret2 = loop_rw_iter(WRITE, req, &s->iter); |
2dd2111d GH |
4007 | else |
4008 | ret2 = -EINVAL; | |
4ed734b0 | 4009 | |
6ad7f233 PB |
4010 | if (req->flags & REQ_F_REISSUE) { |
4011 | req->flags &= ~REQ_F_REISSUE; | |
230d50d4 | 4012 | ret2 = -EAGAIN; |
6ad7f233 | 4013 | } |
230d50d4 | 4014 | |
fa15bafb PB |
4015 | /* |
4016 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just | |
4017 | * retry them without IOCB_NOWAIT. | |
4018 | */ | |
4019 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) | |
4020 | ret2 = -EAGAIN; | |
75c668cd PB |
4021 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
4022 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) | |
355afaeb | 4023 | goto done; |
fa15bafb | 4024 | if (!force_nonblock || ret2 != -EAGAIN) { |
eefdf30f | 4025 | /* IOPOLL retry should happen for io-wq threads */ |
b10841c9 | 4026 | if (ret2 == -EAGAIN && (req->ctx->flags & IORING_SETUP_IOPOLL)) |
eefdf30f | 4027 | goto copy_iov; |
355afaeb | 4028 | done: |
2ea537ca | 4029 | kiocb_done(req, ret2, issue_flags); |
fa15bafb | 4030 | } else { |
f67676d1 | 4031 | copy_iov: |
c88598a9 PB |
4032 | iov_iter_restore(&s->iter, &s->iter_state); |
4033 | ret = io_setup_async_rw(req, iovec, s, false); | |
6bf985dc | 4034 | return ret ?: -EAGAIN; |
2b188cc1 | 4035 | } |
31b51510 | 4036 | out_free: |
f261c168 | 4037 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 4038 | if (iovec) |
6f2cc166 | 4039 | kfree(iovec); |
2b188cc1 JA |
4040 | return ret; |
4041 | } | |
4042 | ||
80a261fd JA |
4043 | static int io_renameat_prep(struct io_kiocb *req, |
4044 | const struct io_uring_sqe *sqe) | |
4045 | { | |
4046 | struct io_rename *ren = &req->rename; | |
4047 | const char __user *oldf, *newf; | |
4048 | ||
ed7eb259 JA |
4049 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4050 | return -EINVAL; | |
26578cda | 4051 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
ed7eb259 | 4052 | return -EINVAL; |
80a261fd JA |
4053 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
4054 | return -EBADF; | |
4055 | ||
4056 | ren->old_dfd = READ_ONCE(sqe->fd); | |
4057 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4058 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
4059 | ren->new_dfd = READ_ONCE(sqe->len); | |
4060 | ren->flags = READ_ONCE(sqe->rename_flags); | |
4061 | ||
4062 | ren->oldpath = getname(oldf); | |
4063 | if (IS_ERR(ren->oldpath)) | |
4064 | return PTR_ERR(ren->oldpath); | |
4065 | ||
4066 | ren->newpath = getname(newf); | |
4067 | if (IS_ERR(ren->newpath)) { | |
4068 | putname(ren->oldpath); | |
4069 | return PTR_ERR(ren->newpath); | |
4070 | } | |
4071 | ||
4072 | req->flags |= REQ_F_NEED_CLEANUP; | |
4073 | return 0; | |
4074 | } | |
4075 | ||
45d189c6 | 4076 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
80a261fd JA |
4077 | { |
4078 | struct io_rename *ren = &req->rename; | |
4079 | int ret; | |
4080 | ||
45d189c6 | 4081 | if (issue_flags & IO_URING_F_NONBLOCK) |
80a261fd JA |
4082 | return -EAGAIN; |
4083 | ||
4084 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, | |
4085 | ren->newpath, ren->flags); | |
4086 | ||
4087 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4088 | if (ret < 0) | |
93d2bcd2 | 4089 | req_set_fail(req); |
80a261fd JA |
4090 | io_req_complete(req, ret); |
4091 | return 0; | |
4092 | } | |
4093 | ||
14a1143b JA |
4094 | static int io_unlinkat_prep(struct io_kiocb *req, |
4095 | const struct io_uring_sqe *sqe) | |
4096 | { | |
4097 | struct io_unlink *un = &req->unlink; | |
4098 | const char __user *fname; | |
4099 | ||
22634bc5 JA |
4100 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4101 | return -EINVAL; | |
26578cda PB |
4102 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
4103 | sqe->splice_fd_in) | |
22634bc5 | 4104 | return -EINVAL; |
14a1143b JA |
4105 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
4106 | return -EBADF; | |
4107 | ||
4108 | un->dfd = READ_ONCE(sqe->fd); | |
4109 | ||
4110 | un->flags = READ_ONCE(sqe->unlink_flags); | |
4111 | if (un->flags & ~AT_REMOVEDIR) | |
4112 | return -EINVAL; | |
4113 | ||
4114 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4115 | un->filename = getname(fname); | |
4116 | if (IS_ERR(un->filename)) | |
4117 | return PTR_ERR(un->filename); | |
4118 | ||
4119 | req->flags |= REQ_F_NEED_CLEANUP; | |
4120 | return 0; | |
4121 | } | |
4122 | ||
45d189c6 | 4123 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
14a1143b JA |
4124 | { |
4125 | struct io_unlink *un = &req->unlink; | |
4126 | int ret; | |
4127 | ||
45d189c6 | 4128 | if (issue_flags & IO_URING_F_NONBLOCK) |
14a1143b JA |
4129 | return -EAGAIN; |
4130 | ||
4131 | if (un->flags & AT_REMOVEDIR) | |
4132 | ret = do_rmdir(un->dfd, un->filename); | |
4133 | else | |
4134 | ret = do_unlinkat(un->dfd, un->filename); | |
4135 | ||
4136 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4137 | if (ret < 0) | |
93d2bcd2 | 4138 | req_set_fail(req); |
14a1143b JA |
4139 | io_req_complete(req, ret); |
4140 | return 0; | |
4141 | } | |
4142 | ||
e34a02dc DK |
4143 | static int io_mkdirat_prep(struct io_kiocb *req, |
4144 | const struct io_uring_sqe *sqe) | |
4145 | { | |
4146 | struct io_mkdir *mkd = &req->mkdir; | |
4147 | const char __user *fname; | |
4148 | ||
4149 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
4150 | return -EINVAL; | |
4151 | if (sqe->ioprio || sqe->off || sqe->rw_flags || sqe->buf_index || | |
4152 | sqe->splice_fd_in) | |
4153 | return -EINVAL; | |
4154 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
4155 | return -EBADF; | |
4156 | ||
4157 | mkd->dfd = READ_ONCE(sqe->fd); | |
4158 | mkd->mode = READ_ONCE(sqe->len); | |
4159 | ||
4160 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4161 | mkd->filename = getname(fname); | |
4162 | if (IS_ERR(mkd->filename)) | |
4163 | return PTR_ERR(mkd->filename); | |
4164 | ||
4165 | req->flags |= REQ_F_NEED_CLEANUP; | |
4166 | return 0; | |
4167 | } | |
4168 | ||
04f34081 | 4169 | static int io_mkdirat(struct io_kiocb *req, unsigned int issue_flags) |
e34a02dc DK |
4170 | { |
4171 | struct io_mkdir *mkd = &req->mkdir; | |
4172 | int ret; | |
4173 | ||
4174 | if (issue_flags & IO_URING_F_NONBLOCK) | |
4175 | return -EAGAIN; | |
4176 | ||
4177 | ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode); | |
4178 | ||
4179 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4180 | if (ret < 0) | |
4181 | req_set_fail(req); | |
4182 | io_req_complete(req, ret); | |
4183 | return 0; | |
4184 | } | |
4185 | ||
7a8721f8 DK |
4186 | static int io_symlinkat_prep(struct io_kiocb *req, |
4187 | const struct io_uring_sqe *sqe) | |
4188 | { | |
4189 | struct io_symlink *sl = &req->symlink; | |
4190 | const char __user *oldpath, *newpath; | |
4191 | ||
4192 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
4193 | return -EINVAL; | |
4194 | if (sqe->ioprio || sqe->len || sqe->rw_flags || sqe->buf_index || | |
4195 | sqe->splice_fd_in) | |
4196 | return -EINVAL; | |
4197 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
4198 | return -EBADF; | |
4199 | ||
4200 | sl->new_dfd = READ_ONCE(sqe->fd); | |
4201 | oldpath = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4202 | newpath = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
4203 | ||
4204 | sl->oldpath = getname(oldpath); | |
4205 | if (IS_ERR(sl->oldpath)) | |
4206 | return PTR_ERR(sl->oldpath); | |
4207 | ||
4208 | sl->newpath = getname(newpath); | |
4209 | if (IS_ERR(sl->newpath)) { | |
4210 | putname(sl->oldpath); | |
4211 | return PTR_ERR(sl->newpath); | |
4212 | } | |
4213 | ||
4214 | req->flags |= REQ_F_NEED_CLEANUP; | |
4215 | return 0; | |
4216 | } | |
4217 | ||
04f34081 | 4218 | static int io_symlinkat(struct io_kiocb *req, unsigned int issue_flags) |
7a8721f8 DK |
4219 | { |
4220 | struct io_symlink *sl = &req->symlink; | |
4221 | int ret; | |
4222 | ||
4223 | if (issue_flags & IO_URING_F_NONBLOCK) | |
4224 | return -EAGAIN; | |
4225 | ||
4226 | ret = do_symlinkat(sl->oldpath, sl->new_dfd, sl->newpath); | |
4227 | ||
4228 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4229 | if (ret < 0) | |
4230 | req_set_fail(req); | |
4231 | io_req_complete(req, ret); | |
4232 | return 0; | |
4233 | } | |
4234 | ||
cf30da90 DK |
4235 | static int io_linkat_prep(struct io_kiocb *req, |
4236 | const struct io_uring_sqe *sqe) | |
4237 | { | |
4238 | struct io_hardlink *lnk = &req->hardlink; | |
4239 | const char __user *oldf, *newf; | |
4240 | ||
4241 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
4242 | return -EINVAL; | |
4243 | if (sqe->ioprio || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in) | |
4244 | return -EINVAL; | |
4245 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
4246 | return -EBADF; | |
4247 | ||
4248 | lnk->old_dfd = READ_ONCE(sqe->fd); | |
4249 | lnk->new_dfd = READ_ONCE(sqe->len); | |
4250 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4251 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
4252 | lnk->flags = READ_ONCE(sqe->hardlink_flags); | |
4253 | ||
4254 | lnk->oldpath = getname(oldf); | |
4255 | if (IS_ERR(lnk->oldpath)) | |
4256 | return PTR_ERR(lnk->oldpath); | |
4257 | ||
4258 | lnk->newpath = getname(newf); | |
4259 | if (IS_ERR(lnk->newpath)) { | |
4260 | putname(lnk->oldpath); | |
4261 | return PTR_ERR(lnk->newpath); | |
4262 | } | |
4263 | ||
4264 | req->flags |= REQ_F_NEED_CLEANUP; | |
4265 | return 0; | |
4266 | } | |
4267 | ||
04f34081 | 4268 | static int io_linkat(struct io_kiocb *req, unsigned int issue_flags) |
cf30da90 DK |
4269 | { |
4270 | struct io_hardlink *lnk = &req->hardlink; | |
4271 | int ret; | |
4272 | ||
4273 | if (issue_flags & IO_URING_F_NONBLOCK) | |
4274 | return -EAGAIN; | |
4275 | ||
4276 | ret = do_linkat(lnk->old_dfd, lnk->oldpath, lnk->new_dfd, | |
4277 | lnk->newpath, lnk->flags); | |
4278 | ||
4279 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4280 | if (ret < 0) | |
4281 | req_set_fail(req); | |
4282 | io_req_complete(req, ret); | |
4283 | return 0; | |
4284 | } | |
4285 | ||
36f4fa68 JA |
4286 | static int io_shutdown_prep(struct io_kiocb *req, |
4287 | const struct io_uring_sqe *sqe) | |
4288 | { | |
4289 | #if defined(CONFIG_NET) | |
4290 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
4291 | return -EINVAL; | |
26578cda PB |
4292 | if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || |
4293 | sqe->buf_index || sqe->splice_fd_in)) | |
36f4fa68 JA |
4294 | return -EINVAL; |
4295 | ||
4296 | req->shutdown.how = READ_ONCE(sqe->len); | |
4297 | return 0; | |
4298 | #else | |
4299 | return -EOPNOTSUPP; | |
4300 | #endif | |
4301 | } | |
4302 | ||
45d189c6 | 4303 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
36f4fa68 JA |
4304 | { |
4305 | #if defined(CONFIG_NET) | |
4306 | struct socket *sock; | |
4307 | int ret; | |
4308 | ||
45d189c6 | 4309 | if (issue_flags & IO_URING_F_NONBLOCK) |
36f4fa68 JA |
4310 | return -EAGAIN; |
4311 | ||
48aba79b | 4312 | sock = sock_from_file(req->file); |
36f4fa68 | 4313 | if (unlikely(!sock)) |
48aba79b | 4314 | return -ENOTSOCK; |
36f4fa68 JA |
4315 | |
4316 | ret = __sys_shutdown_sock(sock, req->shutdown.how); | |
a146468d | 4317 | if (ret < 0) |
93d2bcd2 | 4318 | req_set_fail(req); |
36f4fa68 JA |
4319 | io_req_complete(req, ret); |
4320 | return 0; | |
4321 | #else | |
4322 | return -EOPNOTSUPP; | |
4323 | #endif | |
4324 | } | |
4325 | ||
f2a8d5c7 PB |
4326 | static int __io_splice_prep(struct io_kiocb *req, |
4327 | const struct io_uring_sqe *sqe) | |
7d67af2c | 4328 | { |
fe7e3257 | 4329 | struct io_splice *sp = &req->splice; |
7d67af2c | 4330 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
7d67af2c | 4331 | |
3232dd02 PB |
4332 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4333 | return -EINVAL; | |
7d67af2c | 4334 | |
7d67af2c PB |
4335 | sp->len = READ_ONCE(sqe->len); |
4336 | sp->flags = READ_ONCE(sqe->splice_flags); | |
7d67af2c PB |
4337 | if (unlikely(sp->flags & ~valid_flags)) |
4338 | return -EINVAL; | |
a3e4bc23 | 4339 | sp->splice_fd_in = READ_ONCE(sqe->splice_fd_in); |
7d67af2c PB |
4340 | return 0; |
4341 | } | |
4342 | ||
f2a8d5c7 PB |
4343 | static int io_tee_prep(struct io_kiocb *req, |
4344 | const struct io_uring_sqe *sqe) | |
4345 | { | |
4346 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) | |
4347 | return -EINVAL; | |
4348 | return __io_splice_prep(req, sqe); | |
4349 | } | |
4350 | ||
45d189c6 | 4351 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
f2a8d5c7 PB |
4352 | { |
4353 | struct io_splice *sp = &req->splice; | |
f2a8d5c7 PB |
4354 | struct file *out = sp->file_out; |
4355 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
a3e4bc23 | 4356 | struct file *in; |
f2a8d5c7 PB |
4357 | long ret = 0; |
4358 | ||
45d189c6 | 4359 | if (issue_flags & IO_URING_F_NONBLOCK) |
f2a8d5c7 | 4360 | return -EAGAIN; |
a3e4bc23 | 4361 | |
5106dd6e | 4362 | if (sp->flags & SPLICE_F_FD_IN_FIXED) |
e9419766 | 4363 | in = io_file_get_fixed(req, sp->splice_fd_in, issue_flags); |
5106dd6e JA |
4364 | else |
4365 | in = io_file_get_normal(req, sp->splice_fd_in); | |
a3e4bc23 JA |
4366 | if (!in) { |
4367 | ret = -EBADF; | |
4368 | goto done; | |
4369 | } | |
4370 | ||
f2a8d5c7 PB |
4371 | if (sp->len) |
4372 | ret = do_tee(in, out, sp->len, flags); | |
4373 | ||
e1d767f0 PB |
4374 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
4375 | io_put_file(in); | |
a3e4bc23 | 4376 | done: |
f2a8d5c7 | 4377 | if (ret != sp->len) |
93d2bcd2 | 4378 | req_set_fail(req); |
e1e16097 | 4379 | io_req_complete(req, ret); |
f2a8d5c7 PB |
4380 | return 0; |
4381 | } | |
4382 | ||
4383 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4384 | { | |
fe7e3257 | 4385 | struct io_splice *sp = &req->splice; |
f2a8d5c7 PB |
4386 | |
4387 | sp->off_in = READ_ONCE(sqe->splice_off_in); | |
4388 | sp->off_out = READ_ONCE(sqe->off); | |
4389 | return __io_splice_prep(req, sqe); | |
4390 | } | |
4391 | ||
45d189c6 | 4392 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
7d67af2c PB |
4393 | { |
4394 | struct io_splice *sp = &req->splice; | |
7d67af2c PB |
4395 | struct file *out = sp->file_out; |
4396 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
4397 | loff_t *poff_in, *poff_out; | |
a3e4bc23 | 4398 | struct file *in; |
c9687426 | 4399 | long ret = 0; |
7d67af2c | 4400 | |
45d189c6 | 4401 | if (issue_flags & IO_URING_F_NONBLOCK) |
2fb3e822 | 4402 | return -EAGAIN; |
7d67af2c | 4403 | |
5106dd6e | 4404 | if (sp->flags & SPLICE_F_FD_IN_FIXED) |
e9419766 | 4405 | in = io_file_get_fixed(req, sp->splice_fd_in, issue_flags); |
5106dd6e JA |
4406 | else |
4407 | in = io_file_get_normal(req, sp->splice_fd_in); | |
a3e4bc23 JA |
4408 | if (!in) { |
4409 | ret = -EBADF; | |
4410 | goto done; | |
4411 | } | |
4412 | ||
7d67af2c PB |
4413 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; |
4414 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; | |
c9687426 | 4415 | |
948a7749 | 4416 | if (sp->len) |
c9687426 | 4417 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
7d67af2c | 4418 | |
e1d767f0 PB |
4419 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
4420 | io_put_file(in); | |
a3e4bc23 | 4421 | done: |
7d67af2c | 4422 | if (ret != sp->len) |
93d2bcd2 | 4423 | req_set_fail(req); |
e1e16097 | 4424 | io_req_complete(req, ret); |
7d67af2c PB |
4425 | return 0; |
4426 | } | |
4427 | ||
2b188cc1 JA |
4428 | /* |
4429 | * IORING_OP_NOP just posts a completion event, nothing else. | |
4430 | */ | |
889fca73 | 4431 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
4432 | { |
4433 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 4434 | |
def596e9 JA |
4435 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4436 | return -EINVAL; | |
4437 | ||
889fca73 | 4438 | __io_req_complete(req, issue_flags, 0, 0); |
2b188cc1 JA |
4439 | return 0; |
4440 | } | |
4441 | ||
4f57f06c JA |
4442 | static int io_msg_ring_prep(struct io_kiocb *req, |
4443 | const struct io_uring_sqe *sqe) | |
4444 | { | |
f3b6a41e JA |
4445 | if (unlikely(sqe->addr || sqe->ioprio || sqe->rw_flags || |
4446 | sqe->splice_fd_in || sqe->buf_index || sqe->personality)) | |
4f57f06c JA |
4447 | return -EINVAL; |
4448 | ||
4f57f06c JA |
4449 | req->msg.user_data = READ_ONCE(sqe->off); |
4450 | req->msg.len = READ_ONCE(sqe->len); | |
4451 | return 0; | |
4452 | } | |
4453 | ||
4454 | static int io_msg_ring(struct io_kiocb *req, unsigned int issue_flags) | |
4455 | { | |
4456 | struct io_ring_ctx *target_ctx; | |
4457 | struct io_msg *msg = &req->msg; | |
4f57f06c | 4458 | bool filled; |
3f1d52ab | 4459 | int ret; |
4f57f06c | 4460 | |
3f1d52ab JA |
4461 | ret = -EBADFD; |
4462 | if (req->file->f_op != &io_uring_fops) | |
4463 | goto done; | |
4f57f06c | 4464 | |
3f1d52ab | 4465 | ret = -EOVERFLOW; |
4f57f06c JA |
4466 | target_ctx = req->file->private_data; |
4467 | ||
4468 | spin_lock(&target_ctx->completion_lock); | |
7ef66d18 | 4469 | filled = io_fill_cqe_aux(target_ctx, msg->user_data, msg->len, 0); |
4f57f06c JA |
4470 | io_commit_cqring(target_ctx); |
4471 | spin_unlock(&target_ctx->completion_lock); | |
4472 | ||
4473 | if (filled) { | |
4474 | io_cqring_ev_posted(target_ctx); | |
4475 | ret = 0; | |
4476 | } | |
4477 | ||
3f1d52ab | 4478 | done: |
9666d420 JA |
4479 | if (ret < 0) |
4480 | req_set_fail(req); | |
4f57f06c JA |
4481 | __io_req_complete(req, issue_flags, ret, 0); |
4482 | return 0; | |
4483 | } | |
4484 | ||
1155c76a | 4485 | static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 4486 | { |
6b06314c | 4487 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 4488 | |
6b06314c | 4489 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 4490 | return -EINVAL; |
26578cda PB |
4491 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
4492 | sqe->splice_fd_in)) | |
c992fe29 CH |
4493 | return -EINVAL; |
4494 | ||
8ed8d3c3 JA |
4495 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
4496 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
4497 | return -EINVAL; | |
4498 | ||
4499 | req->sync.off = READ_ONCE(sqe->off); | |
4500 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
4501 | return 0; |
4502 | } | |
4503 | ||
45d189c6 | 4504 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4505 | { |
8ed8d3c3 | 4506 | loff_t end = req->sync.off + req->sync.len; |
8ed8d3c3 JA |
4507 | int ret; |
4508 | ||
ac45abc0 | 4509 | /* fsync always requires a blocking context */ |
45d189c6 | 4510 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4511 | return -EAGAIN; |
4512 | ||
9adbd45d | 4513 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
4514 | end > 0 ? end : LLONG_MAX, |
4515 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
4516 | if (ret < 0) | |
93d2bcd2 | 4517 | req_set_fail(req); |
e1e16097 | 4518 | io_req_complete(req, ret); |
c992fe29 CH |
4519 | return 0; |
4520 | } | |
4521 | ||
d63d1b5e JA |
4522 | static int io_fallocate_prep(struct io_kiocb *req, |
4523 | const struct io_uring_sqe *sqe) | |
4524 | { | |
26578cda PB |
4525 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags || |
4526 | sqe->splice_fd_in) | |
d63d1b5e | 4527 | return -EINVAL; |
3232dd02 PB |
4528 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4529 | return -EINVAL; | |
d63d1b5e JA |
4530 | |
4531 | req->sync.off = READ_ONCE(sqe->off); | |
4532 | req->sync.len = READ_ONCE(sqe->addr); | |
4533 | req->sync.mode = READ_ONCE(sqe->len); | |
4534 | return 0; | |
4535 | } | |
4536 | ||
45d189c6 | 4537 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
5d17b4a4 | 4538 | { |
ac45abc0 PB |
4539 | int ret; |
4540 | ||
d63d1b5e | 4541 | /* fallocate always requiring blocking context */ |
45d189c6 | 4542 | if (issue_flags & IO_URING_F_NONBLOCK) |
5d17b4a4 | 4543 | return -EAGAIN; |
ac45abc0 PB |
4544 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
4545 | req->sync.len); | |
ac45abc0 | 4546 | if (ret < 0) |
93d2bcd2 | 4547 | req_set_fail(req); |
f63cf519 JA |
4548 | else |
4549 | fsnotify_modify(req->file); | |
e1e16097 | 4550 | io_req_complete(req, ret); |
5d17b4a4 JA |
4551 | return 0; |
4552 | } | |
4553 | ||
ec65fea5 | 4554 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 4555 | { |
f8748881 | 4556 | const char __user *fname; |
15b71abe | 4557 | int ret; |
b7bb4f7d | 4558 | |
d3fddf6d PB |
4559 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4560 | return -EINVAL; | |
b9445598 | 4561 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
15b71abe | 4562 | return -EINVAL; |
ec65fea5 | 4563 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
cf3040ca | 4564 | return -EBADF; |
03b1230c | 4565 | |
ec65fea5 PB |
4566 | /* open.how should be already initialised */ |
4567 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
08a1d26e | 4568 | req->open.how.flags |= O_LARGEFILE; |
3529d8c2 | 4569 | |
25e72d10 PB |
4570 | req->open.dfd = READ_ONCE(sqe->fd); |
4571 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
f8748881 | 4572 | req->open.filename = getname(fname); |
15b71abe JA |
4573 | if (IS_ERR(req->open.filename)) { |
4574 | ret = PTR_ERR(req->open.filename); | |
4575 | req->open.filename = NULL; | |
4576 | return ret; | |
4577 | } | |
b9445598 PB |
4578 | |
4579 | req->open.file_slot = READ_ONCE(sqe->file_index); | |
4580 | if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC)) | |
4581 | return -EINVAL; | |
4582 | ||
4022e7af | 4583 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
8fef80bf | 4584 | req->flags |= REQ_F_NEED_CLEANUP; |
15b71abe | 4585 | return 0; |
03b1230c JA |
4586 | } |
4587 | ||
ec65fea5 PB |
4588 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4589 | { | |
d3fddf6d PB |
4590 | u64 mode = READ_ONCE(sqe->len); |
4591 | u64 flags = READ_ONCE(sqe->open_flags); | |
ec65fea5 | 4592 | |
ec65fea5 PB |
4593 | req->open.how = build_open_how(flags, mode); |
4594 | return __io_openat_prep(req, sqe); | |
4595 | } | |
4596 | ||
cebdb986 | 4597 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 4598 | { |
cebdb986 | 4599 | struct open_how __user *how; |
cebdb986 | 4600 | size_t len; |
0fa03c62 JA |
4601 | int ret; |
4602 | ||
cebdb986 JA |
4603 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4604 | len = READ_ONCE(sqe->len); | |
cebdb986 JA |
4605 | if (len < OPEN_HOW_SIZE_VER0) |
4606 | return -EINVAL; | |
3529d8c2 | 4607 | |
cebdb986 JA |
4608 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
4609 | len); | |
4610 | if (ret) | |
4611 | return ret; | |
3529d8c2 | 4612 | |
ec65fea5 | 4613 | return __io_openat_prep(req, sqe); |
cebdb986 JA |
4614 | } |
4615 | ||
45d189c6 | 4616 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
15b71abe JA |
4617 | { |
4618 | struct open_flags op; | |
15b71abe | 4619 | struct file *file; |
b9445598 PB |
4620 | bool resolve_nonblock, nonblock_set; |
4621 | bool fixed = !!req->open.file_slot; | |
15b71abe JA |
4622 | int ret; |
4623 | ||
cebdb986 | 4624 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
4625 | if (ret) |
4626 | goto err; | |
3a81fd02 JA |
4627 | nonblock_set = op.open_flag & O_NONBLOCK; |
4628 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; | |
45d189c6 | 4629 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3a81fd02 JA |
4630 | /* |
4631 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, | |
4632 | * it'll always -EAGAIN | |
4633 | */ | |
4634 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) | |
4635 | return -EAGAIN; | |
4636 | op.lookup_flags |= LOOKUP_CACHED; | |
4637 | op.open_flag |= O_NONBLOCK; | |
4638 | } | |
15b71abe | 4639 | |
b9445598 PB |
4640 | if (!fixed) { |
4641 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); | |
4642 | if (ret < 0) | |
4643 | goto err; | |
4644 | } | |
15b71abe JA |
4645 | |
4646 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
12dcb58a | 4647 | if (IS_ERR(file)) { |
944d1444 | 4648 | /* |
12dcb58a PB |
4649 | * We could hang on to this 'fd' on retrying, but seems like |
4650 | * marginal gain for something that is now known to be a slower | |
4651 | * path. So just put it, and we'll get a new one when we retry. | |
944d1444 | 4652 | */ |
b9445598 PB |
4653 | if (!fixed) |
4654 | put_unused_fd(ret); | |
3a81fd02 | 4655 | |
15b71abe | 4656 | ret = PTR_ERR(file); |
12dcb58a PB |
4657 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
4658 | if (ret == -EAGAIN && | |
4659 | (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK))) | |
4660 | return -EAGAIN; | |
4661 | goto err; | |
15b71abe | 4662 | } |
12dcb58a PB |
4663 | |
4664 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) | |
4665 | file->f_flags &= ~O_NONBLOCK; | |
4666 | fsnotify_open(file); | |
b9445598 PB |
4667 | |
4668 | if (!fixed) | |
4669 | fd_install(ret, file); | |
4670 | else | |
4671 | ret = io_install_fixed_file(req, file, issue_flags, | |
4672 | req->open.file_slot - 1); | |
15b71abe JA |
4673 | err: |
4674 | putname(req->open.filename); | |
8fef80bf | 4675 | req->flags &= ~REQ_F_NEED_CLEANUP; |
15b71abe | 4676 | if (ret < 0) |
93d2bcd2 | 4677 | req_set_fail(req); |
0bdf3398 | 4678 | __io_req_complete(req, issue_flags, ret, 0); |
15b71abe JA |
4679 | return 0; |
4680 | } | |
4681 | ||
45d189c6 | 4682 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
cebdb986 | 4683 | { |
e45cff58 | 4684 | return io_openat2(req, issue_flags); |
cebdb986 JA |
4685 | } |
4686 | ||
067524e9 JA |
4687 | static int io_remove_buffers_prep(struct io_kiocb *req, |
4688 | const struct io_uring_sqe *sqe) | |
4689 | { | |
4690 | struct io_provide_buf *p = &req->pbuf; | |
4691 | u64 tmp; | |
4692 | ||
26578cda PB |
4693 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off || |
4694 | sqe->splice_fd_in) | |
067524e9 JA |
4695 | return -EINVAL; |
4696 | ||
4697 | tmp = READ_ONCE(sqe->fd); | |
4698 | if (!tmp || tmp > USHRT_MAX) | |
4699 | return -EINVAL; | |
4700 | ||
4701 | memset(p, 0, sizeof(*p)); | |
4702 | p->nbufs = tmp; | |
4703 | p->bgid = READ_ONCE(sqe->buf_group); | |
4704 | return 0; | |
4705 | } | |
4706 | ||
dbc7d452 JA |
4707 | static int __io_remove_buffers(struct io_ring_ctx *ctx, |
4708 | struct io_buffer_list *bl, unsigned nbufs) | |
067524e9 JA |
4709 | { |
4710 | unsigned i = 0; | |
4711 | ||
4712 | /* shouldn't happen */ | |
4713 | if (!nbufs) | |
4714 | return 0; | |
4715 | ||
4716 | /* the head kbuf is the list itself */ | |
dbc7d452 | 4717 | while (!list_empty(&bl->buf_list)) { |
067524e9 JA |
4718 | struct io_buffer *nxt; |
4719 | ||
dbc7d452 | 4720 | nxt = list_first_entry(&bl->buf_list, struct io_buffer, list); |
067524e9 | 4721 | list_del(&nxt->list); |
067524e9 JA |
4722 | if (++i == nbufs) |
4723 | return i; | |
1d0254e6 | 4724 | cond_resched(); |
067524e9 JA |
4725 | } |
4726 | i++; | |
067524e9 JA |
4727 | |
4728 | return i; | |
4729 | } | |
4730 | ||
889fca73 | 4731 | static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags) |
067524e9 JA |
4732 | { |
4733 | struct io_provide_buf *p = &req->pbuf; | |
4734 | struct io_ring_ctx *ctx = req->ctx; | |
dbc7d452 | 4735 | struct io_buffer_list *bl; |
067524e9 | 4736 | int ret = 0; |
3b44b371 | 4737 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
067524e9 | 4738 | |
3b44b371 | 4739 | io_ring_submit_lock(ctx, needs_lock); |
067524e9 JA |
4740 | |
4741 | lockdep_assert_held(&ctx->uring_lock); | |
4742 | ||
4743 | ret = -ENOENT; | |
dbc7d452 JA |
4744 | bl = io_buffer_get_list(ctx, p->bgid); |
4745 | if (bl) | |
4746 | ret = __io_remove_buffers(ctx, bl, p->nbufs); | |
067524e9 | 4747 | if (ret < 0) |
93d2bcd2 | 4748 | req_set_fail(req); |
067524e9 | 4749 | |
9fb8cb49 PB |
4750 | /* complete before unlock, IOPOLL may need the lock */ |
4751 | __io_req_complete(req, issue_flags, ret, 0); | |
3b44b371 | 4752 | io_ring_submit_unlock(ctx, needs_lock); |
067524e9 JA |
4753 | return 0; |
4754 | } | |
4755 | ||
ddf0322d JA |
4756 | static int io_provide_buffers_prep(struct io_kiocb *req, |
4757 | const struct io_uring_sqe *sqe) | |
4758 | { | |
38134ada | 4759 | unsigned long size, tmp_check; |
ddf0322d JA |
4760 | struct io_provide_buf *p = &req->pbuf; |
4761 | u64 tmp; | |
4762 | ||
26578cda | 4763 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
ddf0322d JA |
4764 | return -EINVAL; |
4765 | ||
4766 | tmp = READ_ONCE(sqe->fd); | |
4767 | if (!tmp || tmp > USHRT_MAX) | |
4768 | return -E2BIG; | |
4769 | p->nbufs = tmp; | |
4770 | p->addr = READ_ONCE(sqe->addr); | |
4771 | p->len = READ_ONCE(sqe->len); | |
4772 | ||
38134ada PB |
4773 | if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs, |
4774 | &size)) | |
4775 | return -EOVERFLOW; | |
4776 | if (check_add_overflow((unsigned long)p->addr, size, &tmp_check)) | |
4777 | return -EOVERFLOW; | |
4778 | ||
d81269fe PB |
4779 | size = (unsigned long)p->len * p->nbufs; |
4780 | if (!access_ok(u64_to_user_ptr(p->addr), size)) | |
ddf0322d JA |
4781 | return -EFAULT; |
4782 | ||
4783 | p->bgid = READ_ONCE(sqe->buf_group); | |
4784 | tmp = READ_ONCE(sqe->off); | |
4785 | if (tmp > USHRT_MAX) | |
4786 | return -E2BIG; | |
4787 | p->bid = tmp; | |
4788 | return 0; | |
4789 | } | |
4790 | ||
cc3cec83 JA |
4791 | static int io_refill_buffer_cache(struct io_ring_ctx *ctx) |
4792 | { | |
4793 | struct io_buffer *buf; | |
4794 | struct page *page; | |
4795 | int bufs_in_page; | |
4796 | ||
4797 | /* | |
4798 | * Completions that don't happen inline (eg not under uring_lock) will | |
4799 | * add to ->io_buffers_comp. If we don't have any free buffers, check | |
4800 | * the completion list and splice those entries first. | |
4801 | */ | |
4802 | if (!list_empty_careful(&ctx->io_buffers_comp)) { | |
4803 | spin_lock(&ctx->completion_lock); | |
4804 | if (!list_empty(&ctx->io_buffers_comp)) { | |
4805 | list_splice_init(&ctx->io_buffers_comp, | |
4806 | &ctx->io_buffers_cache); | |
4807 | spin_unlock(&ctx->completion_lock); | |
4808 | return 0; | |
4809 | } | |
4810 | spin_unlock(&ctx->completion_lock); | |
4811 | } | |
4812 | ||
4813 | /* | |
4814 | * No free buffers and no completion entries either. Allocate a new | |
4815 | * page worth of buffer entries and add those to our freelist. | |
4816 | */ | |
4817 | page = alloc_page(GFP_KERNEL_ACCOUNT); | |
4818 | if (!page) | |
4819 | return -ENOMEM; | |
4820 | ||
4821 | list_add(&page->lru, &ctx->io_buffers_pages); | |
4822 | ||
4823 | buf = page_address(page); | |
4824 | bufs_in_page = PAGE_SIZE / sizeof(*buf); | |
4825 | while (bufs_in_page) { | |
4826 | list_add_tail(&buf->list, &ctx->io_buffers_cache); | |
4827 | buf++; | |
4828 | bufs_in_page--; | |
4829 | } | |
4830 | ||
4831 | return 0; | |
4832 | } | |
4833 | ||
4834 | static int io_add_buffers(struct io_ring_ctx *ctx, struct io_provide_buf *pbuf, | |
dbc7d452 | 4835 | struct io_buffer_list *bl) |
ddf0322d JA |
4836 | { |
4837 | struct io_buffer *buf; | |
4838 | u64 addr = pbuf->addr; | |
4839 | int i, bid = pbuf->bid; | |
4840 | ||
4841 | for (i = 0; i < pbuf->nbufs; i++) { | |
cc3cec83 JA |
4842 | if (list_empty(&ctx->io_buffers_cache) && |
4843 | io_refill_buffer_cache(ctx)) | |
ddf0322d | 4844 | break; |
cc3cec83 JA |
4845 | buf = list_first_entry(&ctx->io_buffers_cache, struct io_buffer, |
4846 | list); | |
dbc7d452 | 4847 | list_move_tail(&buf->list, &bl->buf_list); |
ddf0322d | 4848 | buf->addr = addr; |
d1f82808 | 4849 | buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT); |
ddf0322d | 4850 | buf->bid = bid; |
b1c62645 | 4851 | buf->bgid = pbuf->bgid; |
ddf0322d JA |
4852 | addr += pbuf->len; |
4853 | bid++; | |
f240762f | 4854 | cond_resched(); |
ddf0322d JA |
4855 | } |
4856 | ||
dbc7d452 | 4857 | return i ? 0 : -ENOMEM; |
ddf0322d JA |
4858 | } |
4859 | ||
889fca73 | 4860 | static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags) |
ddf0322d JA |
4861 | { |
4862 | struct io_provide_buf *p = &req->pbuf; | |
4863 | struct io_ring_ctx *ctx = req->ctx; | |
dbc7d452 | 4864 | struct io_buffer_list *bl; |
ddf0322d | 4865 | int ret = 0; |
3b44b371 | 4866 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
ddf0322d | 4867 | |
3b44b371 | 4868 | io_ring_submit_lock(ctx, needs_lock); |
ddf0322d JA |
4869 | |
4870 | lockdep_assert_held(&ctx->uring_lock); | |
4871 | ||
dbc7d452 JA |
4872 | bl = io_buffer_get_list(ctx, p->bgid); |
4873 | if (unlikely(!bl)) { | |
4874 | bl = kmalloc(sizeof(*bl), GFP_KERNEL); | |
4875 | if (!bl) { | |
4876 | ret = -ENOMEM; | |
4877 | goto err; | |
4878 | } | |
4879 | io_buffer_add_list(ctx, bl, p->bgid); | |
ddf0322d | 4880 | } |
dbc7d452 JA |
4881 | |
4882 | ret = io_add_buffers(ctx, p, bl); | |
4883 | err: | |
ddf0322d | 4884 | if (ret < 0) |
93d2bcd2 | 4885 | req_set_fail(req); |
9fb8cb49 PB |
4886 | /* complete before unlock, IOPOLL may need the lock */ |
4887 | __io_req_complete(req, issue_flags, ret, 0); | |
3b44b371 | 4888 | io_ring_submit_unlock(ctx, needs_lock); |
ddf0322d | 4889 | return 0; |
cebdb986 JA |
4890 | } |
4891 | ||
3e4827b0 JA |
4892 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
4893 | const struct io_uring_sqe *sqe) | |
4894 | { | |
4895 | #if defined(CONFIG_EPOLL) | |
26578cda | 4896 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
3e4827b0 | 4897 | return -EINVAL; |
2d74d042 | 4898 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4899 | return -EINVAL; |
3e4827b0 JA |
4900 | |
4901 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
4902 | req->epoll.op = READ_ONCE(sqe->len); | |
4903 | req->epoll.fd = READ_ONCE(sqe->off); | |
4904 | ||
4905 | if (ep_op_has_event(req->epoll.op)) { | |
4906 | struct epoll_event __user *ev; | |
4907 | ||
4908 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4909 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
4910 | return -EFAULT; | |
4911 | } | |
4912 | ||
4913 | return 0; | |
4914 | #else | |
4915 | return -EOPNOTSUPP; | |
4916 | #endif | |
4917 | } | |
4918 | ||
889fca73 | 4919 | static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags) |
3e4827b0 JA |
4920 | { |
4921 | #if defined(CONFIG_EPOLL) | |
4922 | struct io_epoll *ie = &req->epoll; | |
4923 | int ret; | |
45d189c6 | 4924 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
3e4827b0 JA |
4925 | |
4926 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
4927 | if (force_nonblock && ret == -EAGAIN) | |
4928 | return -EAGAIN; | |
4929 | ||
4930 | if (ret < 0) | |
93d2bcd2 | 4931 | req_set_fail(req); |
889fca73 | 4932 | __io_req_complete(req, issue_flags, ret, 0); |
3e4827b0 JA |
4933 | return 0; |
4934 | #else | |
4935 | return -EOPNOTSUPP; | |
4936 | #endif | |
4937 | } | |
4938 | ||
c1ca757b JA |
4939 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4940 | { | |
4941 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
26578cda | 4942 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in) |
c1ca757b | 4943 | return -EINVAL; |
3232dd02 PB |
4944 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4945 | return -EINVAL; | |
c1ca757b JA |
4946 | |
4947 | req->madvise.addr = READ_ONCE(sqe->addr); | |
4948 | req->madvise.len = READ_ONCE(sqe->len); | |
4949 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4950 | return 0; | |
4951 | #else | |
4952 | return -EOPNOTSUPP; | |
4953 | #endif | |
4954 | } | |
4955 | ||
45d189c6 | 4956 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
c1ca757b JA |
4957 | { |
4958 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4959 | struct io_madvise *ma = &req->madvise; | |
4960 | int ret; | |
4961 | ||
45d189c6 | 4962 | if (issue_flags & IO_URING_F_NONBLOCK) |
c1ca757b JA |
4963 | return -EAGAIN; |
4964 | ||
0726b01e | 4965 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
c1ca757b | 4966 | if (ret < 0) |
93d2bcd2 | 4967 | req_set_fail(req); |
e1e16097 | 4968 | io_req_complete(req, ret); |
c1ca757b JA |
4969 | return 0; |
4970 | #else | |
4971 | return -EOPNOTSUPP; | |
4972 | #endif | |
4973 | } | |
4974 | ||
4840e418 JA |
4975 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4976 | { | |
26578cda | 4977 | if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in) |
4840e418 | 4978 | return -EINVAL; |
3232dd02 PB |
4979 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4980 | return -EINVAL; | |
4840e418 JA |
4981 | |
4982 | req->fadvise.offset = READ_ONCE(sqe->off); | |
4983 | req->fadvise.len = READ_ONCE(sqe->len); | |
4984 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4985 | return 0; | |
4986 | } | |
4987 | ||
45d189c6 | 4988 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
4840e418 JA |
4989 | { |
4990 | struct io_fadvise *fa = &req->fadvise; | |
4991 | int ret; | |
4992 | ||
45d189c6 | 4993 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3e69426d JA |
4994 | switch (fa->advice) { |
4995 | case POSIX_FADV_NORMAL: | |
4996 | case POSIX_FADV_RANDOM: | |
4997 | case POSIX_FADV_SEQUENTIAL: | |
4998 | break; | |
4999 | default: | |
5000 | return -EAGAIN; | |
5001 | } | |
5002 | } | |
4840e418 JA |
5003 | |
5004 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
5005 | if (ret < 0) | |
93d2bcd2 | 5006 | req_set_fail(req); |
0bdf3398 | 5007 | __io_req_complete(req, issue_flags, ret, 0); |
4840e418 JA |
5008 | return 0; |
5009 | } | |
5010 | ||
eddc7ef5 JA |
5011 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5012 | { | |
1b6fe6e0 SR |
5013 | const char __user *path; |
5014 | ||
2d74d042 | 5015 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 5016 | return -EINVAL; |
26578cda | 5017 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
eddc7ef5 | 5018 | return -EINVAL; |
9c280f90 | 5019 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 5020 | return -EBADF; |
eddc7ef5 | 5021 | |
1d9e1288 BM |
5022 | req->statx.dfd = READ_ONCE(sqe->fd); |
5023 | req->statx.mask = READ_ONCE(sqe->len); | |
1b6fe6e0 | 5024 | path = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
1d9e1288 BM |
5025 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
5026 | req->statx.flags = READ_ONCE(sqe->statx_flags); | |
eddc7ef5 | 5027 | |
1b6fe6e0 SR |
5028 | req->statx.filename = getname_flags(path, |
5029 | getname_statx_lookup_flags(req->statx.flags), | |
5030 | NULL); | |
5031 | ||
5032 | if (IS_ERR(req->statx.filename)) { | |
5033 | int ret = PTR_ERR(req->statx.filename); | |
5034 | ||
5035 | req->statx.filename = NULL; | |
5036 | return ret; | |
5037 | } | |
5038 | ||
5039 | req->flags |= REQ_F_NEED_CLEANUP; | |
eddc7ef5 JA |
5040 | return 0; |
5041 | } | |
5042 | ||
45d189c6 | 5043 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
eddc7ef5 | 5044 | { |
1d9e1288 | 5045 | struct io_statx *ctx = &req->statx; |
eddc7ef5 JA |
5046 | int ret; |
5047 | ||
59d70013 | 5048 | if (issue_flags & IO_URING_F_NONBLOCK) |
eddc7ef5 JA |
5049 | return -EAGAIN; |
5050 | ||
e62753e4 BM |
5051 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
5052 | ctx->buffer); | |
eddc7ef5 | 5053 | |
eddc7ef5 | 5054 | if (ret < 0) |
93d2bcd2 | 5055 | req_set_fail(req); |
e1e16097 | 5056 | io_req_complete(req, ret); |
eddc7ef5 JA |
5057 | return 0; |
5058 | } | |
5059 | ||
b5dba59e JA |
5060 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5061 | { | |
14587a46 | 5062 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 5063 | return -EINVAL; |
b5dba59e | 5064 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
7df778be | 5065 | sqe->rw_flags || sqe->buf_index) |
b5dba59e | 5066 | return -EINVAL; |
9c280f90 | 5067 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 5068 | return -EBADF; |
b5dba59e JA |
5069 | |
5070 | req->close.fd = READ_ONCE(sqe->fd); | |
7df778be PB |
5071 | req->close.file_slot = READ_ONCE(sqe->file_index); |
5072 | if (req->close.file_slot && req->close.fd) | |
5073 | return -EINVAL; | |
5074 | ||
b5dba59e | 5075 | return 0; |
b5dba59e JA |
5076 | } |
5077 | ||
889fca73 | 5078 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
b5dba59e | 5079 | { |
9eac1904 | 5080 | struct files_struct *files = current->files; |
3af73b28 | 5081 | struct io_close *close = &req->close; |
9eac1904 | 5082 | struct fdtable *fdt; |
a1fde923 PB |
5083 | struct file *file = NULL; |
5084 | int ret = -EBADF; | |
b5dba59e | 5085 | |
7df778be PB |
5086 | if (req->close.file_slot) { |
5087 | ret = io_close_fixed(req, issue_flags); | |
5088 | goto err; | |
5089 | } | |
5090 | ||
9eac1904 JA |
5091 | spin_lock(&files->file_lock); |
5092 | fdt = files_fdtable(files); | |
5093 | if (close->fd >= fdt->max_fds) { | |
5094 | spin_unlock(&files->file_lock); | |
5095 | goto err; | |
5096 | } | |
5097 | file = fdt->fd[close->fd]; | |
a1fde923 | 5098 | if (!file || file->f_op == &io_uring_fops) { |
9eac1904 JA |
5099 | spin_unlock(&files->file_lock); |
5100 | file = NULL; | |
5101 | goto err; | |
3af73b28 | 5102 | } |
b5dba59e JA |
5103 | |
5104 | /* if the file has a flush method, be safe and punt to async */ | |
45d189c6 | 5105 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
9eac1904 | 5106 | spin_unlock(&files->file_lock); |
0bf0eefd | 5107 | return -EAGAIN; |
a2100672 | 5108 | } |
b5dba59e | 5109 | |
9eac1904 JA |
5110 | ret = __close_fd_get_file(close->fd, &file); |
5111 | spin_unlock(&files->file_lock); | |
5112 | if (ret < 0) { | |
5113 | if (ret == -ENOENT) | |
5114 | ret = -EBADF; | |
5115 | goto err; | |
5116 | } | |
5117 | ||
3af73b28 | 5118 | /* No ->flush() or already async, safely close from here */ |
9eac1904 JA |
5119 | ret = filp_close(file, current->files); |
5120 | err: | |
3af73b28 | 5121 | if (ret < 0) |
93d2bcd2 | 5122 | req_set_fail(req); |
9eac1904 JA |
5123 | if (file) |
5124 | fput(file); | |
889fca73 | 5125 | __io_req_complete(req, issue_flags, ret, 0); |
1a417f4e | 5126 | return 0; |
b5dba59e JA |
5127 | } |
5128 | ||
1155c76a | 5129 | static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
5130 | { |
5131 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 | 5132 | |
5d17b4a4 JA |
5133 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
5134 | return -EINVAL; | |
26578cda PB |
5135 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
5136 | sqe->splice_fd_in)) | |
5d17b4a4 JA |
5137 | return -EINVAL; |
5138 | ||
8ed8d3c3 JA |
5139 | req->sync.off = READ_ONCE(sqe->off); |
5140 | req->sync.len = READ_ONCE(sqe->len); | |
5141 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
5142 | return 0; |
5143 | } | |
5144 | ||
45d189c6 | 5145 | static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 5146 | { |
8ed8d3c3 JA |
5147 | int ret; |
5148 | ||
ac45abc0 | 5149 | /* sync_file_range always requires a blocking context */ |
45d189c6 | 5150 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
5151 | return -EAGAIN; |
5152 | ||
9adbd45d | 5153 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
5154 | req->sync.flags); |
5155 | if (ret < 0) | |
93d2bcd2 | 5156 | req_set_fail(req); |
e1e16097 | 5157 | io_req_complete(req, ret); |
5d17b4a4 JA |
5158 | return 0; |
5159 | } | |
5160 | ||
469956e8 | 5161 | #if defined(CONFIG_NET) |
02d27d89 PB |
5162 | static int io_setup_async_msg(struct io_kiocb *req, |
5163 | struct io_async_msghdr *kmsg) | |
5164 | { | |
e8c2bc1f JA |
5165 | struct io_async_msghdr *async_msg = req->async_data; |
5166 | ||
5167 | if (async_msg) | |
02d27d89 | 5168 | return -EAGAIN; |
e8c2bc1f | 5169 | if (io_alloc_async_data(req)) { |
257e84a5 | 5170 | kfree(kmsg->free_iov); |
02d27d89 PB |
5171 | return -ENOMEM; |
5172 | } | |
e8c2bc1f | 5173 | async_msg = req->async_data; |
02d27d89 | 5174 | req->flags |= REQ_F_NEED_CLEANUP; |
e8c2bc1f | 5175 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
2a780802 | 5176 | async_msg->msg.msg_name = &async_msg->addr; |
257e84a5 PB |
5177 | /* if were using fast_iov, set it to the new one */ |
5178 | if (!async_msg->free_iov) | |
5179 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; | |
5180 | ||
02d27d89 PB |
5181 | return -EAGAIN; |
5182 | } | |
5183 | ||
2ae523ed PB |
5184 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
5185 | struct io_async_msghdr *iomsg) | |
5186 | { | |
2ae523ed | 5187 | iomsg->msg.msg_name = &iomsg->addr; |
257e84a5 | 5188 | iomsg->free_iov = iomsg->fast_iov; |
2ae523ed | 5189 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
257e84a5 | 5190 | req->sr_msg.msg_flags, &iomsg->free_iov); |
2ae523ed PB |
5191 | } |
5192 | ||
93642ef8 PB |
5193 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
5194 | { | |
5195 | int ret; | |
5196 | ||
93642ef8 PB |
5197 | ret = io_sendmsg_copy_hdr(req, req->async_data); |
5198 | if (!ret) | |
5199 | req->flags |= REQ_F_NEED_CLEANUP; | |
5200 | return ret; | |
5201 | } | |
5202 | ||
3529d8c2 | 5203 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 5204 | { |
e47293fd | 5205 | struct io_sr_msg *sr = &req->sr_msg; |
03b1230c | 5206 | |
d2b6f48b PB |
5207 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5208 | return -EINVAL; | |
5209 | ||
270a5940 | 5210 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
fddaface | 5211 | sr->len = READ_ONCE(sqe->len); |
04411806 PB |
5212 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
5213 | if (sr->msg_flags & MSG_DONTWAIT) | |
5214 | req->flags |= REQ_F_NOWAIT; | |
3529d8c2 | 5215 | |
d8768362 JA |
5216 | #ifdef CONFIG_COMPAT |
5217 | if (req->ctx->compat) | |
5218 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
5219 | #endif | |
93642ef8 | 5220 | return 0; |
03b1230c JA |
5221 | } |
5222 | ||
889fca73 | 5223 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 5224 | { |
6b754c8b | 5225 | struct io_async_msghdr iomsg, *kmsg; |
0fa03c62 | 5226 | struct socket *sock; |
7a7cacba | 5227 | unsigned flags; |
0031275d | 5228 | int min_ret = 0; |
0fa03c62 JA |
5229 | int ret; |
5230 | ||
dba4a925 | 5231 | sock = sock_from_file(req->file); |
7a7cacba | 5232 | if (unlikely(!sock)) |
dba4a925 | 5233 | return -ENOTSOCK; |
3529d8c2 | 5234 | |
d886e185 PB |
5235 | if (req_has_async_data(req)) { |
5236 | kmsg = req->async_data; | |
5237 | } else { | |
7a7cacba PB |
5238 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
5239 | if (ret) | |
5240 | return ret; | |
5241 | kmsg = &iomsg; | |
0fa03c62 | 5242 | } |
0fa03c62 | 5243 | |
04411806 PB |
5244 | flags = req->sr_msg.msg_flags; |
5245 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 5246 | flags |= MSG_DONTWAIT; |
0031275d SM |
5247 | if (flags & MSG_WAITALL) |
5248 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
5249 | ||
7a7cacba | 5250 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
0fa03c62 | 5251 | |
7297ce3d PB |
5252 | if (ret < min_ret) { |
5253 | if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK)) | |
5254 | return io_setup_async_msg(req, kmsg); | |
5255 | if (ret == -ERESTARTSYS) | |
5256 | ret = -EINTR; | |
5257 | req_set_fail(req); | |
5258 | } | |
257e84a5 PB |
5259 | /* fast path, check for non-NULL to avoid function call */ |
5260 | if (kmsg->free_iov) | |
5261 | kfree(kmsg->free_iov); | |
99bc4c38 | 5262 | req->flags &= ~REQ_F_NEED_CLEANUP; |
889fca73 | 5263 | __io_req_complete(req, issue_flags, ret, 0); |
5d17b4a4 | 5264 | return 0; |
03b1230c | 5265 | } |
aa1fa28f | 5266 | |
889fca73 | 5267 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 5268 | { |
7a7cacba PB |
5269 | struct io_sr_msg *sr = &req->sr_msg; |
5270 | struct msghdr msg; | |
5271 | struct iovec iov; | |
fddaface | 5272 | struct socket *sock; |
7a7cacba | 5273 | unsigned flags; |
0031275d | 5274 | int min_ret = 0; |
fddaface JA |
5275 | int ret; |
5276 | ||
dba4a925 | 5277 | sock = sock_from_file(req->file); |
7a7cacba | 5278 | if (unlikely(!sock)) |
dba4a925 | 5279 | return -ENOTSOCK; |
fddaface | 5280 | |
7a7cacba PB |
5281 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
5282 | if (unlikely(ret)) | |
14db8411 | 5283 | return ret; |
fddaface | 5284 | |
7a7cacba PB |
5285 | msg.msg_name = NULL; |
5286 | msg.msg_control = NULL; | |
5287 | msg.msg_controllen = 0; | |
5288 | msg.msg_namelen = 0; | |
fddaface | 5289 | |
04411806 PB |
5290 | flags = req->sr_msg.msg_flags; |
5291 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 5292 | flags |= MSG_DONTWAIT; |
0031275d SM |
5293 | if (flags & MSG_WAITALL) |
5294 | min_ret = iov_iter_count(&msg.msg_iter); | |
5295 | ||
7a7cacba PB |
5296 | msg.msg_flags = flags; |
5297 | ret = sock_sendmsg(sock, &msg); | |
7297ce3d PB |
5298 | if (ret < min_ret) { |
5299 | if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK)) | |
5300 | return -EAGAIN; | |
5301 | if (ret == -ERESTARTSYS) | |
5302 | ret = -EINTR; | |
93d2bcd2 | 5303 | req_set_fail(req); |
7297ce3d | 5304 | } |
889fca73 | 5305 | __io_req_complete(req, issue_flags, ret, 0); |
fddaface | 5306 | return 0; |
fddaface JA |
5307 | } |
5308 | ||
1400e697 PB |
5309 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
5310 | struct io_async_msghdr *iomsg) | |
52de1fe1 JA |
5311 | { |
5312 | struct io_sr_msg *sr = &req->sr_msg; | |
5313 | struct iovec __user *uiov; | |
5314 | size_t iov_len; | |
5315 | int ret; | |
5316 | ||
1400e697 PB |
5317 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
5318 | &iomsg->uaddr, &uiov, &iov_len); | |
52de1fe1 JA |
5319 | if (ret) |
5320 | return ret; | |
5321 | ||
5322 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
5323 | if (iov_len > 1) | |
5324 | return -EINVAL; | |
5476dfed | 5325 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
52de1fe1 | 5326 | return -EFAULT; |
5476dfed | 5327 | sr->len = iomsg->fast_iov[0].iov_len; |
257e84a5 | 5328 | iomsg->free_iov = NULL; |
52de1fe1 | 5329 | } else { |
257e84a5 | 5330 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 5331 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
257e84a5 | 5332 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
89cd35c5 | 5333 | false); |
52de1fe1 JA |
5334 | if (ret > 0) |
5335 | ret = 0; | |
5336 | } | |
5337 | ||
5338 | return ret; | |
5339 | } | |
5340 | ||
5341 | #ifdef CONFIG_COMPAT | |
5342 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, | |
1400e697 | 5343 | struct io_async_msghdr *iomsg) |
52de1fe1 | 5344 | { |
52de1fe1 JA |
5345 | struct io_sr_msg *sr = &req->sr_msg; |
5346 | struct compat_iovec __user *uiov; | |
5347 | compat_uptr_t ptr; | |
5348 | compat_size_t len; | |
5349 | int ret; | |
5350 | ||
4af3417a PB |
5351 | ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr, |
5352 | &ptr, &len); | |
52de1fe1 JA |
5353 | if (ret) |
5354 | return ret; | |
5355 | ||
5356 | uiov = compat_ptr(ptr); | |
5357 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
5358 | compat_ssize_t clen; | |
5359 | ||
5360 | if (len > 1) | |
5361 | return -EINVAL; | |
5362 | if (!access_ok(uiov, sizeof(*uiov))) | |
5363 | return -EFAULT; | |
5364 | if (__get_user(clen, &uiov->iov_len)) | |
5365 | return -EFAULT; | |
5366 | if (clen < 0) | |
5367 | return -EINVAL; | |
2d280bc8 | 5368 | sr->len = clen; |
257e84a5 | 5369 | iomsg->free_iov = NULL; |
52de1fe1 | 5370 | } else { |
257e84a5 | 5371 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 5372 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
257e84a5 | 5373 | UIO_FASTIOV, &iomsg->free_iov, |
89cd35c5 | 5374 | &iomsg->msg.msg_iter, true); |
52de1fe1 JA |
5375 | if (ret < 0) |
5376 | return ret; | |
5377 | } | |
5378 | ||
5379 | return 0; | |
5380 | } | |
5381 | #endif | |
5382 | ||
1400e697 PB |
5383 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
5384 | struct io_async_msghdr *iomsg) | |
52de1fe1 | 5385 | { |
1400e697 | 5386 | iomsg->msg.msg_name = &iomsg->addr; |
52de1fe1 JA |
5387 | |
5388 | #ifdef CONFIG_COMPAT | |
5389 | if (req->ctx->compat) | |
1400e697 | 5390 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
fddaface | 5391 | #endif |
52de1fe1 | 5392 | |
1400e697 | 5393 | return __io_recvmsg_copy_hdr(req, iomsg); |
52de1fe1 JA |
5394 | } |
5395 | ||
bcda7baa | 5396 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
51aac424 | 5397 | unsigned int issue_flags) |
bcda7baa JA |
5398 | { |
5399 | struct io_sr_msg *sr = &req->sr_msg; | |
bcda7baa | 5400 | |
51aac424 | 5401 | return io_buffer_select(req, &sr->len, sr->bgid, issue_flags); |
fddaface JA |
5402 | } |
5403 | ||
93642ef8 | 5404 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
aa1fa28f | 5405 | { |
99bc4c38 | 5406 | int ret; |
3529d8c2 | 5407 | |
93642ef8 PB |
5408 | ret = io_recvmsg_copy_hdr(req, req->async_data); |
5409 | if (!ret) | |
5410 | req->flags |= REQ_F_NEED_CLEANUP; | |
5411 | return ret; | |
5412 | } | |
5413 | ||
5414 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
5415 | { | |
5416 | struct io_sr_msg *sr = &req->sr_msg; | |
5417 | ||
d2b6f48b PB |
5418 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5419 | return -EINVAL; | |
5420 | ||
270a5940 | 5421 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
0b7b21e4 | 5422 | sr->len = READ_ONCE(sqe->len); |
bcda7baa | 5423 | sr->bgid = READ_ONCE(sqe->buf_group); |
04411806 PB |
5424 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
5425 | if (sr->msg_flags & MSG_DONTWAIT) | |
5426 | req->flags |= REQ_F_NOWAIT; | |
06b76d44 | 5427 | |
d8768362 JA |
5428 | #ifdef CONFIG_COMPAT |
5429 | if (req->ctx->compat) | |
5430 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
5431 | #endif | |
7ba89d2a | 5432 | sr->done_io = 0; |
93642ef8 | 5433 | return 0; |
aa1fa28f JA |
5434 | } |
5435 | ||
7ba89d2a JA |
5436 | static bool io_net_retry(struct socket *sock, int flags) |
5437 | { | |
5438 | if (!(flags & MSG_WAITALL)) | |
5439 | return false; | |
5440 | return sock->type == SOCK_STREAM || sock->type == SOCK_SEQPACKET; | |
5441 | } | |
5442 | ||
889fca73 | 5443 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 5444 | { |
6b754c8b | 5445 | struct io_async_msghdr iomsg, *kmsg; |
7ba89d2a | 5446 | struct io_sr_msg *sr = &req->sr_msg; |
03b1230c | 5447 | struct socket *sock; |
7fbb1b54 | 5448 | struct io_buffer *kbuf; |
7a7cacba | 5449 | unsigned flags; |
d1fd1c20 | 5450 | int ret, min_ret = 0; |
45d189c6 | 5451 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
03b1230c | 5452 | |
dba4a925 | 5453 | sock = sock_from_file(req->file); |
7a7cacba | 5454 | if (unlikely(!sock)) |
dba4a925 | 5455 | return -ENOTSOCK; |
3529d8c2 | 5456 | |
d886e185 PB |
5457 | if (req_has_async_data(req)) { |
5458 | kmsg = req->async_data; | |
5459 | } else { | |
7a7cacba PB |
5460 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
5461 | if (ret) | |
681fda8d | 5462 | return ret; |
7a7cacba PB |
5463 | kmsg = &iomsg; |
5464 | } | |
03b1230c | 5465 | |
bc02ef33 | 5466 | if (req->flags & REQ_F_BUFFER_SELECT) { |
51aac424 | 5467 | kbuf = io_recv_buffer_select(req, issue_flags); |
bc02ef33 | 5468 | if (IS_ERR(kbuf)) |
52de1fe1 | 5469 | return PTR_ERR(kbuf); |
7a7cacba | 5470 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
5476dfed PB |
5471 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
5472 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, | |
7a7cacba PB |
5473 | 1, req->sr_msg.len); |
5474 | } | |
52de1fe1 | 5475 | |
04411806 PB |
5476 | flags = req->sr_msg.msg_flags; |
5477 | if (force_nonblock) | |
7a7cacba | 5478 | flags |= MSG_DONTWAIT; |
0031275d SM |
5479 | if (flags & MSG_WAITALL) |
5480 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
5481 | ||
7a7cacba PB |
5482 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
5483 | kmsg->uaddr, flags); | |
7297ce3d PB |
5484 | if (ret < min_ret) { |
5485 | if (ret == -EAGAIN && force_nonblock) | |
5486 | return io_setup_async_msg(req, kmsg); | |
5487 | if (ret == -ERESTARTSYS) | |
5488 | ret = -EINTR; | |
7ba89d2a JA |
5489 | if (ret > 0 && io_net_retry(sock, flags)) { |
5490 | sr->done_io += ret; | |
8a3e8ee5 | 5491 | req->flags |= REQ_F_PARTIAL_IO; |
7ba89d2a JA |
5492 | return io_setup_async_msg(req, kmsg); |
5493 | } | |
7297ce3d PB |
5494 | req_set_fail(req); |
5495 | } else if ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) { | |
5496 | req_set_fail(req); | |
5497 | } | |
03b1230c | 5498 | |
257e84a5 PB |
5499 | /* fast path, check for non-NULL to avoid function call */ |
5500 | if (kmsg->free_iov) | |
5501 | kfree(kmsg->free_iov); | |
99bc4c38 | 5502 | req->flags &= ~REQ_F_NEED_CLEANUP; |
7ba89d2a JA |
5503 | if (ret >= 0) |
5504 | ret += sr->done_io; | |
5505 | else if (sr->done_io) | |
5506 | ret = sr->done_io; | |
cc3cec83 | 5507 | __io_req_complete(req, issue_flags, ret, io_put_kbuf(req, issue_flags)); |
03b1230c | 5508 | return 0; |
0fa03c62 | 5509 | } |
5d17b4a4 | 5510 | |
889fca73 | 5511 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 5512 | { |
6b754c8b | 5513 | struct io_buffer *kbuf; |
7a7cacba PB |
5514 | struct io_sr_msg *sr = &req->sr_msg; |
5515 | struct msghdr msg; | |
5516 | void __user *buf = sr->buf; | |
fddaface | 5517 | struct socket *sock; |
7a7cacba PB |
5518 | struct iovec iov; |
5519 | unsigned flags; | |
d1fd1c20 | 5520 | int ret, min_ret = 0; |
45d189c6 | 5521 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
fddaface | 5522 | |
dba4a925 | 5523 | sock = sock_from_file(req->file); |
7a7cacba | 5524 | if (unlikely(!sock)) |
dba4a925 | 5525 | return -ENOTSOCK; |
fddaface | 5526 | |
bc02ef33 | 5527 | if (req->flags & REQ_F_BUFFER_SELECT) { |
51aac424 | 5528 | kbuf = io_recv_buffer_select(req, issue_flags); |
bcda7baa JA |
5529 | if (IS_ERR(kbuf)) |
5530 | return PTR_ERR(kbuf); | |
7a7cacba | 5531 | buf = u64_to_user_ptr(kbuf->addr); |
bc02ef33 | 5532 | } |
bcda7baa | 5533 | |
7a7cacba | 5534 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
14c32eee PB |
5535 | if (unlikely(ret)) |
5536 | goto out_free; | |
fddaface | 5537 | |
7a7cacba PB |
5538 | msg.msg_name = NULL; |
5539 | msg.msg_control = NULL; | |
5540 | msg.msg_controllen = 0; | |
5541 | msg.msg_namelen = 0; | |
5542 | msg.msg_iocb = NULL; | |
5543 | msg.msg_flags = 0; | |
fddaface | 5544 | |
04411806 PB |
5545 | flags = req->sr_msg.msg_flags; |
5546 | if (force_nonblock) | |
7a7cacba | 5547 | flags |= MSG_DONTWAIT; |
0031275d SM |
5548 | if (flags & MSG_WAITALL) |
5549 | min_ret = iov_iter_count(&msg.msg_iter); | |
5550 | ||
7a7cacba | 5551 | ret = sock_recvmsg(sock, &msg, flags); |
7297ce3d PB |
5552 | if (ret < min_ret) { |
5553 | if (ret == -EAGAIN && force_nonblock) | |
5554 | return -EAGAIN; | |
5555 | if (ret == -ERESTARTSYS) | |
5556 | ret = -EINTR; | |
7ba89d2a JA |
5557 | if (ret > 0 && io_net_retry(sock, flags)) { |
5558 | sr->len -= ret; | |
5559 | sr->buf += ret; | |
5560 | sr->done_io += ret; | |
8a3e8ee5 | 5561 | req->flags |= REQ_F_PARTIAL_IO; |
7ba89d2a JA |
5562 | return -EAGAIN; |
5563 | } | |
7297ce3d PB |
5564 | req_set_fail(req); |
5565 | } else if ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) { | |
0d7c1153 | 5566 | out_free: |
93d2bcd2 | 5567 | req_set_fail(req); |
7297ce3d | 5568 | } |
cc3cec83 | 5569 | |
7ba89d2a JA |
5570 | if (ret >= 0) |
5571 | ret += sr->done_io; | |
5572 | else if (sr->done_io) | |
5573 | ret = sr->done_io; | |
cc3cec83 | 5574 | __io_req_complete(req, issue_flags, ret, io_put_kbuf(req, issue_flags)); |
fddaface | 5575 | return 0; |
fddaface JA |
5576 | } |
5577 | ||
3529d8c2 | 5578 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 | 5579 | { |
8ed8d3c3 JA |
5580 | struct io_accept *accept = &req->accept; |
5581 | ||
14587a46 | 5582 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
17f2fe35 | 5583 | return -EINVAL; |
aaa4db12 | 5584 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
5585 | return -EINVAL; |
5586 | ||
d55e5f5b JA |
5587 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5588 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 5589 | accept->flags = READ_ONCE(sqe->accept_flags); |
09952e3e | 5590 | accept->nofile = rlimit(RLIMIT_NOFILE); |
a7083ad5 | 5591 | |
aaa4db12 | 5592 | accept->file_slot = READ_ONCE(sqe->file_index); |
adf3a9e9 | 5593 | if (accept->file_slot && (accept->flags & SOCK_CLOEXEC)) |
aaa4db12 | 5594 | return -EINVAL; |
a7083ad5 PB |
5595 | if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) |
5596 | return -EINVAL; | |
5597 | if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK)) | |
5598 | accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK; | |
8ed8d3c3 | 5599 | return 0; |
8ed8d3c3 | 5600 | } |
17f2fe35 | 5601 | |
889fca73 | 5602 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 JA |
5603 | { |
5604 | struct io_accept *accept = &req->accept; | |
45d189c6 | 5605 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ac45abc0 | 5606 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
aaa4db12 | 5607 | bool fixed = !!accept->file_slot; |
a7083ad5 PB |
5608 | struct file *file; |
5609 | int ret, fd; | |
8ed8d3c3 | 5610 | |
aaa4db12 PB |
5611 | if (!fixed) { |
5612 | fd = __get_unused_fd_flags(accept->flags, accept->nofile); | |
5613 | if (unlikely(fd < 0)) | |
5614 | return fd; | |
5615 | } | |
a7083ad5 PB |
5616 | file = do_accept(req->file, file_flags, accept->addr, accept->addr_len, |
5617 | accept->flags); | |
5618 | if (IS_ERR(file)) { | |
aaa4db12 PB |
5619 | if (!fixed) |
5620 | put_unused_fd(fd); | |
a7083ad5 PB |
5621 | ret = PTR_ERR(file); |
5622 | if (ret == -EAGAIN && force_nonblock) | |
5623 | return -EAGAIN; | |
ac45abc0 PB |
5624 | if (ret == -ERESTARTSYS) |
5625 | ret = -EINTR; | |
93d2bcd2 | 5626 | req_set_fail(req); |
aaa4db12 | 5627 | } else if (!fixed) { |
a7083ad5 PB |
5628 | fd_install(fd, file); |
5629 | ret = fd; | |
aaa4db12 PB |
5630 | } else { |
5631 | ret = io_install_fixed_file(req, file, issue_flags, | |
5632 | accept->file_slot - 1); | |
ac45abc0 | 5633 | } |
889fca73 | 5634 | __io_req_complete(req, issue_flags, ret, 0); |
17f2fe35 | 5635 | return 0; |
8ed8d3c3 JA |
5636 | } |
5637 | ||
93642ef8 PB |
5638 | static int io_connect_prep_async(struct io_kiocb *req) |
5639 | { | |
5640 | struct io_async_connect *io = req->async_data; | |
5641 | struct io_connect *conn = &req->connect; | |
5642 | ||
5643 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); | |
5644 | } | |
5645 | ||
3529d8c2 | 5646 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 | 5647 | { |
3529d8c2 | 5648 | struct io_connect *conn = &req->connect; |
f499a021 | 5649 | |
14587a46 | 5650 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3fbb51c1 | 5651 | return -EINVAL; |
26578cda PB |
5652 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags || |
5653 | sqe->splice_fd_in) | |
3fbb51c1 JA |
5654 | return -EINVAL; |
5655 | ||
3529d8c2 JA |
5656 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5657 | conn->addr_len = READ_ONCE(sqe->addr2); | |
93642ef8 | 5658 | return 0; |
f499a021 JA |
5659 | } |
5660 | ||
889fca73 | 5661 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
f8e85cf2 | 5662 | { |
e8c2bc1f | 5663 | struct io_async_connect __io, *io; |
f8e85cf2 | 5664 | unsigned file_flags; |
3fbb51c1 | 5665 | int ret; |
45d189c6 | 5666 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
f8e85cf2 | 5667 | |
d886e185 | 5668 | if (req_has_async_data(req)) { |
e8c2bc1f | 5669 | io = req->async_data; |
f499a021 | 5670 | } else { |
3529d8c2 JA |
5671 | ret = move_addr_to_kernel(req->connect.addr, |
5672 | req->connect.addr_len, | |
e8c2bc1f | 5673 | &__io.address); |
f499a021 JA |
5674 | if (ret) |
5675 | goto out; | |
5676 | io = &__io; | |
5677 | } | |
5678 | ||
3fbb51c1 JA |
5679 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
5680 | ||
e8c2bc1f | 5681 | ret = __sys_connect_file(req->file, &io->address, |
3fbb51c1 | 5682 | req->connect.addr_len, file_flags); |
87f80d62 | 5683 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
d886e185 | 5684 | if (req_has_async_data(req)) |
b7bb4f7d | 5685 | return -EAGAIN; |
e8c2bc1f | 5686 | if (io_alloc_async_data(req)) { |
f499a021 JA |
5687 | ret = -ENOMEM; |
5688 | goto out; | |
5689 | } | |
e8c2bc1f | 5690 | memcpy(req->async_data, &__io, sizeof(__io)); |
f8e85cf2 | 5691 | return -EAGAIN; |
f499a021 | 5692 | } |
f8e85cf2 JA |
5693 | if (ret == -ERESTARTSYS) |
5694 | ret = -EINTR; | |
f499a021 | 5695 | out: |
4e88d6e7 | 5696 | if (ret < 0) |
93d2bcd2 | 5697 | req_set_fail(req); |
889fca73 | 5698 | __io_req_complete(req, issue_flags, ret, 0); |
f8e85cf2 | 5699 | return 0; |
469956e8 Y |
5700 | } |
5701 | #else /* !CONFIG_NET */ | |
99a10081 JA |
5702 | #define IO_NETOP_FN(op) \ |
5703 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ | |
5704 | { \ | |
5705 | return -EOPNOTSUPP; \ | |
5706 | } | |
5707 | ||
5708 | #define IO_NETOP_PREP(op) \ | |
5709 | IO_NETOP_FN(op) \ | |
5710 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ | |
5711 | { \ | |
5712 | return -EOPNOTSUPP; \ | |
5713 | } \ | |
5714 | ||
5715 | #define IO_NETOP_PREP_ASYNC(op) \ | |
5716 | IO_NETOP_PREP(op) \ | |
5717 | static int io_##op##_prep_async(struct io_kiocb *req) \ | |
5718 | { \ | |
5719 | return -EOPNOTSUPP; \ | |
5720 | } | |
5721 | ||
5722 | IO_NETOP_PREP_ASYNC(sendmsg); | |
5723 | IO_NETOP_PREP_ASYNC(recvmsg); | |
5724 | IO_NETOP_PREP_ASYNC(connect); | |
5725 | IO_NETOP_PREP(accept); | |
5726 | IO_NETOP_FN(send); | |
5727 | IO_NETOP_FN(recv); | |
469956e8 | 5728 | #endif /* CONFIG_NET */ |
f8e85cf2 | 5729 | |
d7718a9d JA |
5730 | struct io_poll_table { |
5731 | struct poll_table_struct pt; | |
5732 | struct io_kiocb *req; | |
68b11e8b | 5733 | int nr_entries; |
d7718a9d JA |
5734 | int error; |
5735 | }; | |
ce593a6c | 5736 | |
aa43477b | 5737 | #define IO_POLL_CANCEL_FLAG BIT(31) |
e2c0cb7c | 5738 | #define IO_POLL_REF_MASK GENMASK(30, 0) |
6d816e08 | 5739 | |
aa43477b PB |
5740 | /* |
5741 | * If refs part of ->poll_refs (see IO_POLL_REF_MASK) is 0, it's free. We can | |
5742 | * bump it and acquire ownership. It's disallowed to modify requests while not | |
5743 | * owning it, that prevents from races for enqueueing task_work's and b/w | |
5744 | * arming poll and wakeups. | |
5745 | */ | |
5746 | static inline bool io_poll_get_ownership(struct io_kiocb *req) | |
5747 | { | |
5748 | return !(atomic_fetch_inc(&req->poll_refs) & IO_POLL_REF_MASK); | |
d7718a9d JA |
5749 | } |
5750 | ||
aa43477b | 5751 | static void io_poll_mark_cancelled(struct io_kiocb *req) |
74ce6ce4 | 5752 | { |
aa43477b | 5753 | atomic_or(IO_POLL_CANCEL_FLAG, &req->poll_refs); |
74ce6ce4 JA |
5754 | } |
5755 | ||
d4e7cd36 | 5756 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
18bceab1 | 5757 | { |
e8c2bc1f | 5758 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
d4e7cd36 | 5759 | if (req->opcode == IORING_OP_POLL_ADD) |
e8c2bc1f | 5760 | return req->async_data; |
d4e7cd36 JA |
5761 | return req->apoll->double_poll; |
5762 | } | |
5763 | ||
5764 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) | |
5765 | { | |
5766 | if (req->opcode == IORING_OP_POLL_ADD) | |
5767 | return &req->poll; | |
5768 | return &req->apoll->poll; | |
5769 | } | |
5770 | ||
5641897a | 5771 | static void io_poll_req_insert(struct io_kiocb *req) |
d4e7cd36 | 5772 | { |
5641897a PB |
5773 | struct io_ring_ctx *ctx = req->ctx; |
5774 | struct hlist_head *list; | |
18bceab1 | 5775 | |
5641897a PB |
5776 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
5777 | hlist_add_head(&req->hash_node, list); | |
18bceab1 JA |
5778 | } |
5779 | ||
5641897a PB |
5780 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, |
5781 | wait_queue_func_t wake_func) | |
18bceab1 | 5782 | { |
5641897a | 5783 | poll->head = NULL; |
5641897a PB |
5784 | #define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP) |
5785 | /* mask in events that we always want/need */ | |
5786 | poll->events = events | IO_POLL_UNMASK; | |
5787 | INIT_LIST_HEAD(&poll->wait.entry); | |
5788 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
18bceab1 JA |
5789 | } |
5790 | ||
aa43477b | 5791 | static inline void io_poll_remove_entry(struct io_poll_iocb *poll) |
18bceab1 | 5792 | { |
791f3465 | 5793 | struct wait_queue_head *head = smp_load_acquire(&poll->head); |
18bceab1 | 5794 | |
791f3465 PB |
5795 | if (head) { |
5796 | spin_lock_irq(&head->lock); | |
5797 | list_del_init(&poll->wait.entry); | |
5798 | poll->head = NULL; | |
5799 | spin_unlock_irq(&head->lock); | |
5800 | } | |
aa43477b | 5801 | } |
18bceab1 | 5802 | |
aa43477b PB |
5803 | static void io_poll_remove_entries(struct io_kiocb *req) |
5804 | { | |
91eac1c6 JA |
5805 | /* |
5806 | * Nothing to do if neither of those flags are set. Avoid dipping | |
5807 | * into the poll/apoll/double cachelines if we can. | |
5808 | */ | |
5809 | if (!(req->flags & (REQ_F_SINGLE_POLL | REQ_F_DOUBLE_POLL))) | |
5810 | return; | |
18bceab1 | 5811 | |
791f3465 PB |
5812 | /* |
5813 | * While we hold the waitqueue lock and the waitqueue is nonempty, | |
5814 | * wake_up_pollfree() will wait for us. However, taking the waitqueue | |
5815 | * lock in the first place can race with the waitqueue being freed. | |
5816 | * | |
5817 | * We solve this as eventpoll does: by taking advantage of the fact that | |
5818 | * all users of wake_up_pollfree() will RCU-delay the actual free. If | |
5819 | * we enter rcu_read_lock() and see that the pointer to the queue is | |
5820 | * non-NULL, we can then lock it without the memory being freed out from | |
5821 | * under us. | |
5822 | * | |
5823 | * Keep holding rcu_read_lock() as long as we hold the queue lock, in | |
5824 | * case the caller deletes the entry from the queue, leaving it empty. | |
5825 | * In that case, only RCU prevents the queue memory from being freed. | |
5826 | */ | |
5827 | rcu_read_lock(); | |
91eac1c6 JA |
5828 | if (req->flags & REQ_F_SINGLE_POLL) |
5829 | io_poll_remove_entry(io_poll_get_single(req)); | |
5830 | if (req->flags & REQ_F_DOUBLE_POLL) | |
5831 | io_poll_remove_entry(io_poll_get_double(req)); | |
791f3465 | 5832 | rcu_read_unlock(); |
18bceab1 JA |
5833 | } |
5834 | ||
aa43477b PB |
5835 | /* |
5836 | * All poll tw should go through this. Checks for poll events, manages | |
5837 | * references, does rewait, etc. | |
5838 | * | |
5839 | * Returns a negative error on failure. >0 when no action require, which is | |
5840 | * either spurious wakeup or multishot CQE is served. 0 when it's done with | |
5841 | * the request, then the mask is stored in req->result. | |
5842 | */ | |
5106dd6e | 5843 | static int io_poll_check_events(struct io_kiocb *req, bool locked) |
18bceab1 | 5844 | { |
74ce6ce4 | 5845 | struct io_ring_ctx *ctx = req->ctx; |
aa43477b | 5846 | int v; |
18bceab1 | 5847 | |
316319e8 | 5848 | /* req->task == current here, checking PF_EXITING is safe */ |
e09ee510 | 5849 | if (unlikely(req->task->flags & PF_EXITING)) |
aa43477b | 5850 | io_poll_mark_cancelled(req); |
18bceab1 | 5851 | |
aa43477b PB |
5852 | do { |
5853 | v = atomic_read(&req->poll_refs); | |
74ce6ce4 | 5854 | |
aa43477b PB |
5855 | /* tw handler should be the owner, and so have some references */ |
5856 | if (WARN_ON_ONCE(!(v & IO_POLL_REF_MASK))) | |
5857 | return 0; | |
5858 | if (v & IO_POLL_CANCEL_FLAG) | |
5859 | return -ECANCELED; | |
8706e04e | 5860 | |
aa43477b | 5861 | if (!req->result) { |
2804ecd8 | 5862 | struct poll_table_struct pt = { ._key = req->apoll_events }; |
cce64ef0 | 5863 | unsigned flags = locked ? 0 : IO_URING_F_UNLOCKED; |
18bceab1 | 5864 | |
cce64ef0 | 5865 | if (unlikely(!io_assign_file(req, flags))) |
7179c3ce PB |
5866 | return -EBADF; |
5867 | req->result = vfs_poll(req->file, &pt) & req->apoll_events; | |
c8b5e260 | 5868 | } |
74ce6ce4 | 5869 | |
aa43477b | 5870 | /* multishot, just fill an CQE and proceed */ |
2804ecd8 JA |
5871 | if (req->result && !(req->apoll_events & EPOLLONESHOT)) { |
5872 | __poll_t mask = mangle_poll(req->result & req->apoll_events); | |
aa43477b | 5873 | bool filled; |
18bceab1 | 5874 | |
aa43477b PB |
5875 | spin_lock(&ctx->completion_lock); |
5876 | filled = io_fill_cqe_aux(ctx, req->user_data, mask, | |
5877 | IORING_CQE_F_MORE); | |
5878 | io_commit_cqring(ctx); | |
5879 | spin_unlock(&ctx->completion_lock); | |
5880 | if (unlikely(!filled)) | |
5881 | return -ECANCELED; | |
5882 | io_cqring_ev_posted(ctx); | |
5883 | } else if (req->result) { | |
5884 | return 0; | |
5885 | } | |
18bceab1 | 5886 | |
aa43477b PB |
5887 | /* |
5888 | * Release all references, retry if someone tried to restart | |
5889 | * task_work while we were executing it. | |
5890 | */ | |
5891 | } while (atomic_sub_return(v & IO_POLL_REF_MASK, &req->poll_refs)); | |
18bceab1 | 5892 | |
18bceab1 JA |
5893 | return 1; |
5894 | } | |
5895 | ||
aa43477b | 5896 | static void io_poll_task_func(struct io_kiocb *req, bool *locked) |
18bceab1 | 5897 | { |
18bceab1 | 5898 | struct io_ring_ctx *ctx = req->ctx; |
aa43477b | 5899 | int ret; |
18bceab1 | 5900 | |
5106dd6e | 5901 | ret = io_poll_check_events(req, *locked); |
aa43477b PB |
5902 | if (ret > 0) |
5903 | return; | |
5904 | ||
5905 | if (!ret) { | |
5906 | req->result = mangle_poll(req->result & req->poll.events); | |
e27414be | 5907 | } else { |
aa43477b PB |
5908 | req->result = ret; |
5909 | req_set_fail(req); | |
a62682f9 | 5910 | } |
aa43477b PB |
5911 | |
5912 | io_poll_remove_entries(req); | |
5913 | spin_lock(&ctx->completion_lock); | |
5914 | hash_del(&req->hash_node); | |
5915 | __io_req_complete_post(req, req->result, 0); | |
5916 | io_commit_cqring(ctx); | |
5917 | spin_unlock(&ctx->completion_lock); | |
5918 | io_cqring_ev_posted(ctx); | |
18bceab1 JA |
5919 | } |
5920 | ||
aa43477b | 5921 | static void io_apoll_task_func(struct io_kiocb *req, bool *locked) |
18bceab1 JA |
5922 | { |
5923 | struct io_ring_ctx *ctx = req->ctx; | |
aa43477b | 5924 | int ret; |
18bceab1 | 5925 | |
5106dd6e | 5926 | ret = io_poll_check_events(req, *locked); |
aa43477b PB |
5927 | if (ret > 0) |
5928 | return; | |
18bceab1 | 5929 | |
aa43477b PB |
5930 | io_poll_remove_entries(req); |
5931 | spin_lock(&ctx->completion_lock); | |
5932 | hash_del(&req->hash_node); | |
5933 | spin_unlock(&ctx->completion_lock); | |
18bceab1 | 5934 | |
aa43477b PB |
5935 | if (!ret) |
5936 | io_req_task_submit(req, locked); | |
5937 | else | |
5938 | io_req_complete_failed(req, ret); | |
18bceab1 JA |
5939 | } |
5940 | ||
81459350 | 5941 | static void __io_poll_execute(struct io_kiocb *req, int mask, int events) |
aa43477b PB |
5942 | { |
5943 | req->result = mask; | |
81459350 JA |
5944 | /* |
5945 | * This is useful for poll that is armed on behalf of another | |
5946 | * request, and where the wakeup path could be on a different | |
5947 | * CPU. We want to avoid pulling in req->apoll->events for that | |
5948 | * case. | |
5949 | */ | |
2804ecd8 | 5950 | req->apoll_events = events; |
aa43477b PB |
5951 | if (req->opcode == IORING_OP_POLL_ADD) |
5952 | req->io_task_work.func = io_poll_task_func; | |
5953 | else | |
5954 | req->io_task_work.func = io_apoll_task_func; | |
5955 | ||
502c87d6 | 5956 | trace_io_uring_task_add(req->ctx, req, req->user_data, req->opcode, mask); |
aa43477b PB |
5957 | io_req_task_work_add(req, false); |
5958 | } | |
5959 | ||
81459350 | 5960 | static inline void io_poll_execute(struct io_kiocb *req, int res, int events) |
aa43477b PB |
5961 | { |
5962 | if (io_poll_get_ownership(req)) | |
81459350 | 5963 | __io_poll_execute(req, res, events); |
aa43477b PB |
5964 | } |
5965 | ||
5966 | static void io_poll_cancel_req(struct io_kiocb *req) | |
5967 | { | |
5968 | io_poll_mark_cancelled(req); | |
5969 | /* kick tw, which should complete the request */ | |
81459350 | 5970 | io_poll_execute(req, 0, 0); |
aa43477b PB |
5971 | } |
5972 | ||
d89a4fac JA |
5973 | #define wqe_to_req(wait) ((void *)((unsigned long) (wait)->private & ~1)) |
5974 | #define wqe_is_double(wait) ((unsigned long) (wait)->private & 1) | |
5975 | ||
aa43477b PB |
5976 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
5977 | void *key) | |
18bceab1 | 5978 | { |
d89a4fac | 5979 | struct io_kiocb *req = wqe_to_req(wait); |
aa43477b PB |
5980 | struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb, |
5981 | wait); | |
18bceab1 JA |
5982 | __poll_t mask = key_to_poll(key); |
5983 | ||
791f3465 PB |
5984 | if (unlikely(mask & POLLFREE)) { |
5985 | io_poll_mark_cancelled(req); | |
5986 | /* we have to kick tw in case it's not already */ | |
81459350 | 5987 | io_poll_execute(req, 0, poll->events); |
791f3465 PB |
5988 | |
5989 | /* | |
5990 | * If the waitqueue is being freed early but someone is already | |
5991 | * holds ownership over it, we have to tear down the request as | |
5992 | * best we can. That means immediately removing the request from | |
5993 | * its waitqueue and preventing all further accesses to the | |
5994 | * waitqueue via the request. | |
5995 | */ | |
5996 | list_del_init(&poll->wait.entry); | |
5997 | ||
5998 | /* | |
5999 | * Careful: this *must* be the last step, since as soon | |
6000 | * as req->head is NULL'ed out, the request can be | |
6001 | * completed and freed, since aio_poll_complete_work() | |
6002 | * will no longer need to take the waitqueue lock. | |
6003 | */ | |
6004 | smp_store_release(&poll->head, NULL); | |
6005 | return 1; | |
6006 | } | |
6007 | ||
aa43477b | 6008 | /* for instances that support it check for an event match first */ |
18bceab1 JA |
6009 | if (mask && !(mask & poll->events)) |
6010 | return 0; | |
6011 | ||
eb0089d6 PB |
6012 | if (io_poll_get_ownership(req)) { |
6013 | /* optional, saves extra locking for removal in tw handler */ | |
6014 | if (mask && poll->events & EPOLLONESHOT) { | |
6015 | list_del_init(&poll->wait.entry); | |
6016 | poll->head = NULL; | |
d89a4fac JA |
6017 | if (wqe_is_double(wait)) |
6018 | req->flags &= ~REQ_F_DOUBLE_POLL; | |
6019 | else | |
6020 | req->flags &= ~REQ_F_SINGLE_POLL; | |
eb0089d6 | 6021 | } |
81459350 | 6022 | __io_poll_execute(req, mask, poll->events); |
eb0089d6 | 6023 | } |
18bceab1 | 6024 | return 1; |
18bceab1 JA |
6025 | } |
6026 | ||
6027 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, | |
807abcb0 JA |
6028 | struct wait_queue_head *head, |
6029 | struct io_poll_iocb **poll_ptr) | |
18bceab1 JA |
6030 | { |
6031 | struct io_kiocb *req = pt->req; | |
d89a4fac | 6032 | unsigned long wqe_private = (unsigned long) req; |
18bceab1 JA |
6033 | |
6034 | /* | |
68b11e8b PB |
6035 | * The file being polled uses multiple waitqueues for poll handling |
6036 | * (e.g. one for read, one for write). Setup a separate io_poll_iocb | |
6037 | * if this happens. | |
18bceab1 | 6038 | */ |
68b11e8b | 6039 | if (unlikely(pt->nr_entries)) { |
aa43477b | 6040 | struct io_poll_iocb *first = poll; |
58852d4d | 6041 | |
23a65db8 | 6042 | /* double add on the same waitqueue head, ignore */ |
aa43477b | 6043 | if (first->head == head) |
23a65db8 | 6044 | return; |
18bceab1 | 6045 | /* already have a 2nd entry, fail a third attempt */ |
807abcb0 | 6046 | if (*poll_ptr) { |
23a65db8 PB |
6047 | if ((*poll_ptr)->head == head) |
6048 | return; | |
18bceab1 JA |
6049 | pt->error = -EINVAL; |
6050 | return; | |
6051 | } | |
aa43477b | 6052 | |
18bceab1 JA |
6053 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
6054 | if (!poll) { | |
6055 | pt->error = -ENOMEM; | |
6056 | return; | |
6057 | } | |
d89a4fac JA |
6058 | /* mark as double wq entry */ |
6059 | wqe_private |= 1; | |
91eac1c6 | 6060 | req->flags |= REQ_F_DOUBLE_POLL; |
aa43477b | 6061 | io_init_poll_iocb(poll, first->events, first->wait.func); |
807abcb0 | 6062 | *poll_ptr = poll; |
d886e185 PB |
6063 | if (req->opcode == IORING_OP_POLL_ADD) |
6064 | req->flags |= REQ_F_ASYNC_DATA; | |
18bceab1 JA |
6065 | } |
6066 | ||
91eac1c6 | 6067 | req->flags |= REQ_F_SINGLE_POLL; |
68b11e8b | 6068 | pt->nr_entries++; |
18bceab1 | 6069 | poll->head = head; |
d89a4fac | 6070 | poll->wait.private = (void *) wqe_private; |
a31eb4a2 JX |
6071 | |
6072 | if (poll->events & EPOLLEXCLUSIVE) | |
6073 | add_wait_queue_exclusive(head, &poll->wait); | |
6074 | else | |
6075 | add_wait_queue(head, &poll->wait); | |
18bceab1 JA |
6076 | } |
6077 | ||
aa43477b | 6078 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
18bceab1 JA |
6079 | struct poll_table_struct *p) |
6080 | { | |
6081 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
d7718a9d | 6082 | |
aa43477b PB |
6083 | __io_queue_proc(&pt->req->poll, pt, head, |
6084 | (struct io_poll_iocb **) &pt->req->async_data); | |
d7718a9d JA |
6085 | } |
6086 | ||
aa43477b PB |
6087 | static int __io_arm_poll_handler(struct io_kiocb *req, |
6088 | struct io_poll_iocb *poll, | |
6089 | struct io_poll_table *ipt, __poll_t mask) | |
d7718a9d JA |
6090 | { |
6091 | struct io_ring_ctx *ctx = req->ctx; | |
aa43477b | 6092 | int v; |
d7718a9d | 6093 | |
4d52f338 | 6094 | INIT_HLIST_NODE(&req->hash_node); |
aa43477b | 6095 | io_init_poll_iocb(poll, mask, io_poll_wake); |
b90cd197 | 6096 | poll->file = req->file; |
d7718a9d JA |
6097 | |
6098 | ipt->pt._key = mask; | |
6099 | ipt->req = req; | |
68b11e8b PB |
6100 | ipt->error = 0; |
6101 | ipt->nr_entries = 0; | |
d7718a9d | 6102 | |
aa43477b PB |
6103 | /* |
6104 | * Take the ownership to delay any tw execution up until we're done | |
6105 | * with poll arming. see io_poll_get_ownership(). | |
6106 | */ | |
6107 | atomic_set(&req->poll_refs, 1); | |
d7718a9d | 6108 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
aa43477b PB |
6109 | |
6110 | if (mask && (poll->events & EPOLLONESHOT)) { | |
6111 | io_poll_remove_entries(req); | |
6112 | /* no one else has access to the req, forget about the ref */ | |
6113 | return mask; | |
6114 | } | |
6115 | if (!mask && unlikely(ipt->error || !ipt->nr_entries)) { | |
6116 | io_poll_remove_entries(req); | |
6117 | if (!ipt->error) | |
6118 | ipt->error = -EINVAL; | |
6119 | return 0; | |
6120 | } | |
d7718a9d | 6121 | |
79ebeaee | 6122 | spin_lock(&ctx->completion_lock); |
aa43477b PB |
6123 | io_poll_req_insert(req); |
6124 | spin_unlock(&ctx->completion_lock); | |
6125 | ||
6126 | if (mask) { | |
6127 | /* can't multishot if failed, just queue the event we've got */ | |
6128 | if (unlikely(ipt->error || !ipt->nr_entries)) | |
6129 | poll->events |= EPOLLONESHOT; | |
81459350 | 6130 | __io_poll_execute(req, mask, poll->events); |
aa43477b | 6131 | return 0; |
d7718a9d JA |
6132 | } |
6133 | ||
aa43477b PB |
6134 | /* |
6135 | * Release ownership. If someone tried to queue a tw while it was | |
6136 | * locked, kick it off for them. | |
6137 | */ | |
6138 | v = atomic_dec_return(&req->poll_refs); | |
6139 | if (unlikely(v & IO_POLL_REF_MASK)) | |
81459350 | 6140 | __io_poll_execute(req, 0, poll->events); |
aa43477b PB |
6141 | return 0; |
6142 | } | |
6143 | ||
6144 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
6145 | struct poll_table_struct *p) | |
6146 | { | |
6147 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
6148 | struct async_poll *apoll = pt->req->apoll; | |
6149 | ||
6150 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); | |
d7718a9d JA |
6151 | } |
6152 | ||
59b735ae OL |
6153 | enum { |
6154 | IO_APOLL_OK, | |
6155 | IO_APOLL_ABORTED, | |
6156 | IO_APOLL_READY | |
6157 | }; | |
6158 | ||
4d9237e3 | 6159 | static int io_arm_poll_handler(struct io_kiocb *req, unsigned issue_flags) |
d7718a9d JA |
6160 | { |
6161 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
6162 | struct io_ring_ctx *ctx = req->ctx; | |
6163 | struct async_poll *apoll; | |
6164 | struct io_poll_table ipt; | |
aa43477b PB |
6165 | __poll_t mask = EPOLLONESHOT | POLLERR | POLLPRI; |
6166 | int ret; | |
d7718a9d | 6167 | |
b2d9c3da PB |
6168 | if (!def->pollin && !def->pollout) |
6169 | return IO_APOLL_ABORTED; | |
658d0a40 PB |
6170 | if (!file_can_poll(req->file) || (req->flags & REQ_F_POLLED)) |
6171 | return IO_APOLL_ABORTED; | |
b2d9c3da PB |
6172 | |
6173 | if (def->pollin) { | |
b2d9c3da PB |
6174 | mask |= POLLIN | POLLRDNORM; |
6175 | ||
6176 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ | |
6177 | if ((req->opcode == IORING_OP_RECVMSG) && | |
6178 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) | |
6179 | mask &= ~POLLIN; | |
6180 | } else { | |
b2d9c3da PB |
6181 | mask |= POLLOUT | POLLWRNORM; |
6182 | } | |
52dd8640 DY |
6183 | if (def->poll_exclusive) |
6184 | mask |= EPOLLEXCLUSIVE; | |
4d9237e3 JA |
6185 | if (!(issue_flags & IO_URING_F_UNLOCKED) && |
6186 | !list_empty(&ctx->apoll_cache)) { | |
6187 | apoll = list_first_entry(&ctx->apoll_cache, struct async_poll, | |
6188 | poll.wait.entry); | |
6189 | list_del_init(&apoll->poll.wait.entry); | |
6190 | } else { | |
6191 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); | |
6192 | if (unlikely(!apoll)) | |
6193 | return IO_APOLL_ABORTED; | |
6194 | } | |
807abcb0 | 6195 | apoll->double_poll = NULL; |
d7718a9d | 6196 | req->apoll = apoll; |
b2d9c3da | 6197 | req->flags |= REQ_F_POLLED; |
d7718a9d JA |
6198 | ipt.pt._qproc = io_async_queue_proc; |
6199 | ||
4d55f238 | 6200 | io_kbuf_recycle(req, issue_flags); |
abdad709 | 6201 | |
aa43477b | 6202 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask); |
41a5169c HX |
6203 | if (ret || ipt.error) |
6204 | return ret ? IO_APOLL_READY : IO_APOLL_ABORTED; | |
6205 | ||
502c87d6 | 6206 | trace_io_uring_poll_arm(ctx, req, req->user_data, req->opcode, |
236daeae | 6207 | mask, apoll->poll.events); |
59b735ae | 6208 | return IO_APOLL_OK; |
d7718a9d JA |
6209 | } |
6210 | ||
76e1b642 JA |
6211 | /* |
6212 | * Returns true if we found and killed one or more poll requests | |
6213 | */ | |
c072481d PB |
6214 | static __cold bool io_poll_remove_all(struct io_ring_ctx *ctx, |
6215 | struct task_struct *tsk, bool cancel_all) | |
221c5eb2 | 6216 | { |
78076bb6 | 6217 | struct hlist_node *tmp; |
221c5eb2 | 6218 | struct io_kiocb *req; |
aa43477b PB |
6219 | bool found = false; |
6220 | int i; | |
221c5eb2 | 6221 | |
79ebeaee | 6222 | spin_lock(&ctx->completion_lock); |
78076bb6 JA |
6223 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
6224 | struct hlist_head *list; | |
6225 | ||
6226 | list = &ctx->cancel_hash[i]; | |
f3606e3a | 6227 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
42a7b4ed | 6228 | if (io_match_task_safe(req, tsk, cancel_all)) { |
61bc84c4 | 6229 | hlist_del_init(&req->hash_node); |
aa43477b PB |
6230 | io_poll_cancel_req(req); |
6231 | found = true; | |
6232 | } | |
f3606e3a | 6233 | } |
221c5eb2 | 6234 | } |
79ebeaee | 6235 | spin_unlock(&ctx->completion_lock); |
aa43477b | 6236 | return found; |
221c5eb2 JA |
6237 | } |
6238 | ||
9ba5fac8 PB |
6239 | static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr, |
6240 | bool poll_only) | |
e07785b0 | 6241 | __must_hold(&ctx->completion_lock) |
47f46768 | 6242 | { |
78076bb6 | 6243 | struct hlist_head *list; |
47f46768 JA |
6244 | struct io_kiocb *req; |
6245 | ||
78076bb6 JA |
6246 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
6247 | hlist_for_each_entry(req, list, hash_node) { | |
b41e9852 JA |
6248 | if (sqe_addr != req->user_data) |
6249 | continue; | |
9ba5fac8 PB |
6250 | if (poll_only && req->opcode != IORING_OP_POLL_ADD) |
6251 | continue; | |
b2cb805f | 6252 | return req; |
47f46768 | 6253 | } |
b2cb805f JA |
6254 | return NULL; |
6255 | } | |
6256 | ||
aa43477b PB |
6257 | static bool io_poll_disarm(struct io_kiocb *req) |
6258 | __must_hold(&ctx->completion_lock) | |
6259 | { | |
6260 | if (!io_poll_get_ownership(req)) | |
6261 | return false; | |
6262 | io_poll_remove_entries(req); | |
6263 | hash_del(&req->hash_node); | |
6264 | return true; | |
6265 | } | |
6266 | ||
9ba5fac8 PB |
6267 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr, |
6268 | bool poll_only) | |
e07785b0 | 6269 | __must_hold(&ctx->completion_lock) |
b2cb805f | 6270 | { |
aa43477b | 6271 | struct io_kiocb *req = io_poll_find(ctx, sqe_addr, poll_only); |
b2cb805f | 6272 | |
b2cb805f JA |
6273 | if (!req) |
6274 | return -ENOENT; | |
aa43477b PB |
6275 | io_poll_cancel_req(req); |
6276 | return 0; | |
47f46768 JA |
6277 | } |
6278 | ||
9096af3e PB |
6279 | static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe, |
6280 | unsigned int flags) | |
6281 | { | |
6282 | u32 events; | |
47f46768 | 6283 | |
9096af3e PB |
6284 | events = READ_ONCE(sqe->poll32_events); |
6285 | #ifdef __BIG_ENDIAN | |
6286 | events = swahw32(events); | |
6287 | #endif | |
6288 | if (!(flags & IORING_POLL_ADD_MULTI)) | |
6289 | events |= EPOLLONESHOT; | |
6290 | return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT)); | |
47f46768 JA |
6291 | } |
6292 | ||
c5de0036 | 6293 | static int io_poll_update_prep(struct io_kiocb *req, |
3529d8c2 | 6294 | const struct io_uring_sqe *sqe) |
0969e783 | 6295 | { |
c5de0036 PB |
6296 | struct io_poll_update *upd = &req->poll_update; |
6297 | u32 flags; | |
6298 | ||
0969e783 JA |
6299 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
6300 | return -EINVAL; | |
26578cda | 6301 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
c5de0036 PB |
6302 | return -EINVAL; |
6303 | flags = READ_ONCE(sqe->len); | |
6304 | if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA | | |
6305 | IORING_POLL_ADD_MULTI)) | |
6306 | return -EINVAL; | |
6307 | /* meaningless without update */ | |
6308 | if (flags == IORING_POLL_ADD_MULTI) | |
0969e783 JA |
6309 | return -EINVAL; |
6310 | ||
c5de0036 PB |
6311 | upd->old_user_data = READ_ONCE(sqe->addr); |
6312 | upd->update_events = flags & IORING_POLL_UPDATE_EVENTS; | |
6313 | upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA; | |
221c5eb2 | 6314 | |
c5de0036 PB |
6315 | upd->new_user_data = READ_ONCE(sqe->off); |
6316 | if (!upd->update_user_data && upd->new_user_data) | |
6317 | return -EINVAL; | |
6318 | if (upd->update_events) | |
6319 | upd->events = io_poll_parse_events(sqe, flags); | |
6320 | else if (sqe->poll32_events) | |
6321 | return -EINVAL; | |
221c5eb2 | 6322 | |
221c5eb2 JA |
6323 | return 0; |
6324 | } | |
6325 | ||
3529d8c2 | 6326 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
6327 | { |
6328 | struct io_poll_iocb *poll = &req->poll; | |
c5de0036 | 6329 | u32 flags; |
221c5eb2 JA |
6330 | |
6331 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
6332 | return -EINVAL; | |
c5de0036 | 6333 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr) |
88e41cf9 JA |
6334 | return -EINVAL; |
6335 | flags = READ_ONCE(sqe->len); | |
c5de0036 | 6336 | if (flags & ~IORING_POLL_ADD_MULTI) |
221c5eb2 | 6337 | return -EINVAL; |
04c76b41 PB |
6338 | if ((flags & IORING_POLL_ADD_MULTI) && (req->flags & REQ_F_CQE_SKIP)) |
6339 | return -EINVAL; | |
221c5eb2 | 6340 | |
48dcd38d | 6341 | io_req_set_refcount(req); |
2804ecd8 | 6342 | req->apoll_events = poll->events = io_poll_parse_events(sqe, flags); |
0969e783 JA |
6343 | return 0; |
6344 | } | |
6345 | ||
61e98203 | 6346 | static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags) |
0969e783 JA |
6347 | { |
6348 | struct io_poll_iocb *poll = &req->poll; | |
0969e783 | 6349 | struct io_poll_table ipt; |
aa43477b | 6350 | int ret; |
0969e783 | 6351 | |
d7718a9d | 6352 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 6353 | |
aa43477b PB |
6354 | ret = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events); |
6355 | ret = ret ?: ipt.error; | |
6356 | if (ret) | |
6357 | __io_req_complete(req, issue_flags, ret, 0); | |
6358 | return 0; | |
221c5eb2 JA |
6359 | } |
6360 | ||
c5de0036 | 6361 | static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags) |
b69de288 JA |
6362 | { |
6363 | struct io_ring_ctx *ctx = req->ctx; | |
6364 | struct io_kiocb *preq; | |
2bbb146d | 6365 | int ret2, ret = 0; |
cc8e9ba7 | 6366 | bool locked; |
b69de288 | 6367 | |
79ebeaee | 6368 | spin_lock(&ctx->completion_lock); |
9ba5fac8 | 6369 | preq = io_poll_find(ctx, req->poll_update.old_user_data, true); |
aa43477b | 6370 | if (!preq || !io_poll_disarm(preq)) { |
79ebeaee | 6371 | spin_unlock(&ctx->completion_lock); |
aa43477b | 6372 | ret = preq ? -EALREADY : -ENOENT; |
2bbb146d | 6373 | goto out; |
b69de288 | 6374 | } |
79ebeaee | 6375 | spin_unlock(&ctx->completion_lock); |
cb3b200e | 6376 | |
2bbb146d PB |
6377 | if (req->poll_update.update_events || req->poll_update.update_user_data) { |
6378 | /* only mask one event flags, keep behavior flags */ | |
6379 | if (req->poll_update.update_events) { | |
6380 | preq->poll.events &= ~0xffff; | |
6381 | preq->poll.events |= req->poll_update.events & 0xffff; | |
6382 | preq->poll.events |= IO_POLL_UNMASK; | |
cb3b200e | 6383 | } |
2bbb146d PB |
6384 | if (req->poll_update.update_user_data) |
6385 | preq->user_data = req->poll_update.new_user_data; | |
b69de288 | 6386 | |
2bbb146d PB |
6387 | ret2 = io_poll_add(preq, issue_flags); |
6388 | /* successfully updated, don't complete poll request */ | |
6389 | if (!ret2) | |
6390 | goto out; | |
b69de288 | 6391 | } |
6224590d | 6392 | |
2bbb146d | 6393 | req_set_fail(preq); |
cc8e9ba7 PB |
6394 | preq->result = -ECANCELED; |
6395 | locked = !(issue_flags & IO_URING_F_UNLOCKED); | |
6396 | io_req_task_complete(preq, &locked); | |
2bbb146d PB |
6397 | out: |
6398 | if (ret < 0) | |
6224590d | 6399 | req_set_fail(req); |
2bbb146d | 6400 | /* complete update request, we're done with it */ |
cc8e9ba7 | 6401 | __io_req_complete(req, issue_flags, ret, 0); |
b69de288 | 6402 | return 0; |
89850fce JA |
6403 | } |
6404 | ||
5262f567 JA |
6405 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
6406 | { | |
ad8a48ac JA |
6407 | struct io_timeout_data *data = container_of(timer, |
6408 | struct io_timeout_data, timer); | |
6409 | struct io_kiocb *req = data->req; | |
6410 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
6411 | unsigned long flags; |
6412 | ||
89850fce | 6413 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
a71976f3 | 6414 | list_del_init(&req->timeout.list); |
01cec8c1 PB |
6415 | atomic_set(&req->ctx->cq_timeouts, |
6416 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
89850fce | 6417 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); |
01cec8c1 | 6418 | |
a90c8bf6 PB |
6419 | if (!(data->flags & IORING_TIMEOUT_ETIME_SUCCESS)) |
6420 | req_set_fail(req); | |
6421 | ||
6422 | req->result = -ETIME; | |
6423 | req->io_task_work.func = io_req_task_complete; | |
4813c377 | 6424 | io_req_task_work_add(req, false); |
5262f567 JA |
6425 | return HRTIMER_NORESTART; |
6426 | } | |
6427 | ||
fbd15848 PB |
6428 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
6429 | __u64 user_data) | |
89850fce | 6430 | __must_hold(&ctx->timeout_lock) |
f254ac04 | 6431 | { |
fbd15848 | 6432 | struct io_timeout_data *io; |
47f46768 | 6433 | struct io_kiocb *req; |
fd9c7bc5 | 6434 | bool found = false; |
f254ac04 | 6435 | |
135fcde8 | 6436 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
fd9c7bc5 PB |
6437 | found = user_data == req->user_data; |
6438 | if (found) | |
47f46768 | 6439 | break; |
47f46768 | 6440 | } |
fd9c7bc5 PB |
6441 | if (!found) |
6442 | return ERR_PTR(-ENOENT); | |
fbd15848 PB |
6443 | |
6444 | io = req->async_data; | |
fd9c7bc5 | 6445 | if (hrtimer_try_to_cancel(&io->timer) == -1) |
fbd15848 | 6446 | return ERR_PTR(-EALREADY); |
a71976f3 | 6447 | list_del_init(&req->timeout.list); |
fbd15848 PB |
6448 | return req; |
6449 | } | |
47f46768 | 6450 | |
fbd15848 | 6451 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
ec3c3d0f | 6452 | __must_hold(&ctx->completion_lock) |
89850fce | 6453 | __must_hold(&ctx->timeout_lock) |
fbd15848 PB |
6454 | { |
6455 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); | |
6456 | ||
6457 | if (IS_ERR(req)) | |
6458 | return PTR_ERR(req); | |
6695490d | 6459 | io_req_task_queue_fail(req, -ECANCELED); |
f254ac04 JA |
6460 | return 0; |
6461 | } | |
6462 | ||
50c1df2b JA |
6463 | static clockid_t io_timeout_get_clock(struct io_timeout_data *data) |
6464 | { | |
6465 | switch (data->flags & IORING_TIMEOUT_CLOCK_MASK) { | |
6466 | case IORING_TIMEOUT_BOOTTIME: | |
6467 | return CLOCK_BOOTTIME; | |
6468 | case IORING_TIMEOUT_REALTIME: | |
6469 | return CLOCK_REALTIME; | |
6470 | default: | |
6471 | /* can't happen, vetted at prep time */ | |
6472 | WARN_ON_ONCE(1); | |
6473 | fallthrough; | |
6474 | case 0: | |
6475 | return CLOCK_MONOTONIC; | |
6476 | } | |
6477 | } | |
6478 | ||
f1042b6c PB |
6479 | static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
6480 | struct timespec64 *ts, enum hrtimer_mode mode) | |
6481 | __must_hold(&ctx->timeout_lock) | |
6482 | { | |
6483 | struct io_timeout_data *io; | |
6484 | struct io_kiocb *req; | |
6485 | bool found = false; | |
6486 | ||
6487 | list_for_each_entry(req, &ctx->ltimeout_list, timeout.list) { | |
6488 | found = user_data == req->user_data; | |
6489 | if (found) | |
6490 | break; | |
6491 | } | |
6492 | if (!found) | |
6493 | return -ENOENT; | |
6494 | ||
6495 | io = req->async_data; | |
6496 | if (hrtimer_try_to_cancel(&io->timer) == -1) | |
6497 | return -EALREADY; | |
6498 | hrtimer_init(&io->timer, io_timeout_get_clock(io), mode); | |
6499 | io->timer.function = io_link_timeout_fn; | |
6500 | hrtimer_start(&io->timer, timespec64_to_ktime(*ts), mode); | |
6501 | return 0; | |
6502 | } | |
6503 | ||
9c8e11b3 PB |
6504 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
6505 | struct timespec64 *ts, enum hrtimer_mode mode) | |
89850fce | 6506 | __must_hold(&ctx->timeout_lock) |
47f46768 | 6507 | { |
9c8e11b3 PB |
6508 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
6509 | struct io_timeout_data *data; | |
47f46768 | 6510 | |
9c8e11b3 PB |
6511 | if (IS_ERR(req)) |
6512 | return PTR_ERR(req); | |
47f46768 | 6513 | |
9c8e11b3 PB |
6514 | req->timeout.off = 0; /* noseq */ |
6515 | data = req->async_data; | |
6516 | list_add_tail(&req->timeout.list, &ctx->timeout_list); | |
50c1df2b | 6517 | hrtimer_init(&data->timer, io_timeout_get_clock(data), mode); |
9c8e11b3 PB |
6518 | data->timer.function = io_timeout_fn; |
6519 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); | |
6520 | return 0; | |
47f46768 JA |
6521 | } |
6522 | ||
3529d8c2 JA |
6523 | static int io_timeout_remove_prep(struct io_kiocb *req, |
6524 | const struct io_uring_sqe *sqe) | |
b29472ee | 6525 | { |
9c8e11b3 PB |
6526 | struct io_timeout_rem *tr = &req->timeout_rem; |
6527 | ||
b29472ee JA |
6528 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
6529 | return -EINVAL; | |
61710e43 DA |
6530 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6531 | return -EINVAL; | |
26578cda | 6532 | if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in) |
b29472ee JA |
6533 | return -EINVAL; |
6534 | ||
f1042b6c | 6535 | tr->ltimeout = false; |
9c8e11b3 PB |
6536 | tr->addr = READ_ONCE(sqe->addr); |
6537 | tr->flags = READ_ONCE(sqe->timeout_flags); | |
f1042b6c PB |
6538 | if (tr->flags & IORING_TIMEOUT_UPDATE_MASK) { |
6539 | if (hweight32(tr->flags & IORING_TIMEOUT_CLOCK_MASK) > 1) | |
6540 | return -EINVAL; | |
6541 | if (tr->flags & IORING_LINK_TIMEOUT_UPDATE) | |
6542 | tr->ltimeout = true; | |
6543 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE_MASK|IORING_TIMEOUT_ABS)) | |
9c8e11b3 PB |
6544 | return -EINVAL; |
6545 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) | |
6546 | return -EFAULT; | |
2087009c YB |
6547 | if (tr->ts.tv_sec < 0 || tr->ts.tv_nsec < 0) |
6548 | return -EINVAL; | |
9c8e11b3 PB |
6549 | } else if (tr->flags) { |
6550 | /* timeout removal doesn't support flags */ | |
b29472ee | 6551 | return -EINVAL; |
9c8e11b3 | 6552 | } |
b29472ee | 6553 | |
b29472ee JA |
6554 | return 0; |
6555 | } | |
6556 | ||
8662daec PB |
6557 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
6558 | { | |
6559 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS | |
6560 | : HRTIMER_MODE_REL; | |
6561 | } | |
6562 | ||
11365043 JA |
6563 | /* |
6564 | * Remove or update an existing timeout command | |
6565 | */ | |
61e98203 | 6566 | static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags) |
11365043 | 6567 | { |
9c8e11b3 | 6568 | struct io_timeout_rem *tr = &req->timeout_rem; |
11365043 | 6569 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 | 6570 | int ret; |
11365043 | 6571 | |
ec3c3d0f PB |
6572 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) { |
6573 | spin_lock(&ctx->completion_lock); | |
6574 | spin_lock_irq(&ctx->timeout_lock); | |
9c8e11b3 | 6575 | ret = io_timeout_cancel(ctx, tr->addr); |
ec3c3d0f PB |
6576 | spin_unlock_irq(&ctx->timeout_lock); |
6577 | spin_unlock(&ctx->completion_lock); | |
6578 | } else { | |
f1042b6c PB |
6579 | enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags); |
6580 | ||
ec3c3d0f | 6581 | spin_lock_irq(&ctx->timeout_lock); |
f1042b6c PB |
6582 | if (tr->ltimeout) |
6583 | ret = io_linked_timeout_update(ctx, tr->addr, &tr->ts, mode); | |
6584 | else | |
6585 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode); | |
ec3c3d0f PB |
6586 | spin_unlock_irq(&ctx->timeout_lock); |
6587 | } | |
11365043 | 6588 | |
4e88d6e7 | 6589 | if (ret < 0) |
93d2bcd2 | 6590 | req_set_fail(req); |
505657bc | 6591 | io_req_complete_post(req, ret, 0); |
11365043 | 6592 | return 0; |
5262f567 JA |
6593 | } |
6594 | ||
3529d8c2 | 6595 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 6596 | bool is_timeout_link) |
5262f567 | 6597 | { |
ad8a48ac | 6598 | struct io_timeout_data *data; |
a41525ab | 6599 | unsigned flags; |
56080b02 | 6600 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 6601 | |
ad8a48ac | 6602 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 6603 | return -EINVAL; |
26578cda PB |
6604 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1 || |
6605 | sqe->splice_fd_in) | |
a41525ab | 6606 | return -EINVAL; |
56080b02 | 6607 | if (off && is_timeout_link) |
2d28390a | 6608 | return -EINVAL; |
a41525ab | 6609 | flags = READ_ONCE(sqe->timeout_flags); |
6224590d PB |
6610 | if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK | |
6611 | IORING_TIMEOUT_ETIME_SUCCESS)) | |
50c1df2b JA |
6612 | return -EINVAL; |
6613 | /* more than one clock specified is invalid, obviously */ | |
6614 | if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1) | |
5262f567 | 6615 | return -EINVAL; |
bdf20073 | 6616 | |
ef9dd637 | 6617 | INIT_LIST_HEAD(&req->timeout.list); |
bfe68a22 | 6618 | req->timeout.off = off; |
f18ee4cf PB |
6619 | if (unlikely(off && !req->ctx->off_timeout_used)) |
6620 | req->ctx->off_timeout_used = true; | |
26a61679 | 6621 | |
d6a644a7 PB |
6622 | if (WARN_ON_ONCE(req_has_async_data(req))) |
6623 | return -EFAULT; | |
6624 | if (io_alloc_async_data(req)) | |
26a61679 JA |
6625 | return -ENOMEM; |
6626 | ||
e8c2bc1f | 6627 | data = req->async_data; |
ad8a48ac | 6628 | data->req = req; |
50c1df2b | 6629 | data->flags = flags; |
ad8a48ac JA |
6630 | |
6631 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
6632 | return -EFAULT; |
6633 | ||
f6223ff7 YB |
6634 | if (data->ts.tv_sec < 0 || data->ts.tv_nsec < 0) |
6635 | return -EINVAL; | |
6636 | ||
e677edbc | 6637 | INIT_LIST_HEAD(&req->timeout.list); |
8662daec | 6638 | data->mode = io_translate_timeout_mode(flags); |
50c1df2b | 6639 | hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode); |
b97e736a PB |
6640 | |
6641 | if (is_timeout_link) { | |
6642 | struct io_submit_link *link = &req->ctx->submit_state.link; | |
6643 | ||
6644 | if (!link->head) | |
6645 | return -EINVAL; | |
6646 | if (link->last->opcode == IORING_OP_LINK_TIMEOUT) | |
6647 | return -EINVAL; | |
4d13d1a4 PB |
6648 | req->timeout.head = link->last; |
6649 | link->last->flags |= REQ_F_ARM_LTIMEOUT; | |
b97e736a | 6650 | } |
ad8a48ac JA |
6651 | return 0; |
6652 | } | |
6653 | ||
61e98203 | 6654 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
ad8a48ac | 6655 | { |
ad8a48ac | 6656 | struct io_ring_ctx *ctx = req->ctx; |
e8c2bc1f | 6657 | struct io_timeout_data *data = req->async_data; |
ad8a48ac | 6658 | struct list_head *entry; |
bfe68a22 | 6659 | u32 tail, off = req->timeout.off; |
ad8a48ac | 6660 | |
89850fce | 6661 | spin_lock_irq(&ctx->timeout_lock); |
93bd25bb | 6662 | |
5262f567 JA |
6663 | /* |
6664 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
6665 | * timeout event to be satisfied. If it isn't set, then this is |
6666 | * a pure timeout request, sequence isn't used. | |
5262f567 | 6667 | */ |
8eb7e2d0 | 6668 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
6669 | entry = ctx->timeout_list.prev; |
6670 | goto add; | |
6671 | } | |
5262f567 | 6672 | |
bfe68a22 PB |
6673 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
6674 | req->timeout.target_seq = tail + off; | |
5262f567 | 6675 | |
f010505b MDG |
6676 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
6677 | * This is safe because ->completion_lock is held, and submissions | |
6678 | * and completions are never mixed in the same ->completion_lock section. | |
6679 | */ | |
6680 | ctx->cq_last_tm_flush = tail; | |
6681 | ||
5262f567 JA |
6682 | /* |
6683 | * Insertion sort, ensuring the first entry in the list is always | |
6684 | * the one we need first. | |
6685 | */ | |
5262f567 | 6686 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
6687 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
6688 | timeout.list); | |
5262f567 | 6689 | |
8eb7e2d0 | 6690 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 6691 | continue; |
bfe68a22 PB |
6692 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
6693 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
6694 | break; |
6695 | } | |
93bd25bb | 6696 | add: |
135fcde8 | 6697 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
6698 | data->timer.function = io_timeout_fn; |
6699 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
89850fce | 6700 | spin_unlock_irq(&ctx->timeout_lock); |
5262f567 JA |
6701 | return 0; |
6702 | } | |
5262f567 | 6703 | |
f458dd84 PB |
6704 | struct io_cancel_data { |
6705 | struct io_ring_ctx *ctx; | |
6706 | u64 user_data; | |
6707 | }; | |
6708 | ||
62755e35 JA |
6709 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
6710 | { | |
6711 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
f458dd84 | 6712 | struct io_cancel_data *cd = data; |
62755e35 | 6713 | |
f458dd84 | 6714 | return req->ctx == cd->ctx && req->user_data == cd->user_data; |
62755e35 JA |
6715 | } |
6716 | ||
f458dd84 PB |
6717 | static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data, |
6718 | struct io_ring_ctx *ctx) | |
62755e35 | 6719 | { |
f458dd84 | 6720 | struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, }; |
62755e35 | 6721 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
6722 | int ret = 0; |
6723 | ||
f458dd84 | 6724 | if (!tctx || !tctx->io_wq) |
5aa75ed5 JA |
6725 | return -ENOENT; |
6726 | ||
f458dd84 | 6727 | cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false); |
62755e35 JA |
6728 | switch (cancel_ret) { |
6729 | case IO_WQ_CANCEL_OK: | |
6730 | ret = 0; | |
6731 | break; | |
6732 | case IO_WQ_CANCEL_RUNNING: | |
6733 | ret = -EALREADY; | |
6734 | break; | |
6735 | case IO_WQ_CANCEL_NOTFOUND: | |
6736 | ret = -ENOENT; | |
6737 | break; | |
6738 | } | |
6739 | ||
e977d6d3 JA |
6740 | return ret; |
6741 | } | |
6742 | ||
8cb01fac | 6743 | static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr) |
47f46768 | 6744 | { |
8cb01fac | 6745 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 JA |
6746 | int ret; |
6747 | ||
dadebc35 | 6748 | WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current); |
8cb01fac | 6749 | |
f458dd84 | 6750 | ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx); |
ccbf7261 JA |
6751 | /* |
6752 | * Fall-through even for -EALREADY, as we may have poll armed | |
6753 | * that need unarming. | |
6754 | */ | |
6755 | if (!ret) | |
6756 | return 0; | |
505657bc PB |
6757 | |
6758 | spin_lock(&ctx->completion_lock); | |
ccbf7261 JA |
6759 | ret = io_poll_cancel(ctx, sqe_addr, false); |
6760 | if (ret != -ENOENT) | |
6761 | goto out; | |
6762 | ||
79ebeaee | 6763 | spin_lock_irq(&ctx->timeout_lock); |
47f46768 | 6764 | ret = io_timeout_cancel(ctx, sqe_addr); |
79ebeaee | 6765 | spin_unlock_irq(&ctx->timeout_lock); |
505657bc PB |
6766 | out: |
6767 | spin_unlock(&ctx->completion_lock); | |
6768 | return ret; | |
47f46768 JA |
6769 | } |
6770 | ||
3529d8c2 JA |
6771 | static int io_async_cancel_prep(struct io_kiocb *req, |
6772 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 6773 | { |
fbf23849 | 6774 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 6775 | return -EINVAL; |
61710e43 DA |
6776 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6777 | return -EINVAL; | |
26578cda PB |
6778 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags || |
6779 | sqe->splice_fd_in) | |
e977d6d3 JA |
6780 | return -EINVAL; |
6781 | ||
fbf23849 JA |
6782 | req->cancel.addr = READ_ONCE(sqe->addr); |
6783 | return 0; | |
6784 | } | |
6785 | ||
61e98203 | 6786 | static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6787 | { |
6788 | struct io_ring_ctx *ctx = req->ctx; | |
58f99373 | 6789 | u64 sqe_addr = req->cancel.addr; |
3b44b371 | 6790 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
58f99373 PB |
6791 | struct io_tctx_node *node; |
6792 | int ret; | |
6793 | ||
8cb01fac | 6794 | ret = io_try_cancel_userdata(req, sqe_addr); |
58f99373 PB |
6795 | if (ret != -ENOENT) |
6796 | goto done; | |
58f99373 PB |
6797 | |
6798 | /* slow path, try all io-wq's */ | |
3b44b371 | 6799 | io_ring_submit_lock(ctx, needs_lock); |
58f99373 PB |
6800 | ret = -ENOENT; |
6801 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
6802 | struct io_uring_task *tctx = node->task->io_uring; | |
fbf23849 | 6803 | |
58f99373 PB |
6804 | ret = io_async_cancel_one(tctx, req->cancel.addr, ctx); |
6805 | if (ret != -ENOENT) | |
6806 | break; | |
6807 | } | |
3b44b371 | 6808 | io_ring_submit_unlock(ctx, needs_lock); |
58f99373 | 6809 | done: |
58f99373 | 6810 | if (ret < 0) |
93d2bcd2 | 6811 | req_set_fail(req); |
505657bc | 6812 | io_req_complete_post(req, ret, 0); |
5262f567 JA |
6813 | return 0; |
6814 | } | |
6815 | ||
269bbe5f | 6816 | static int io_rsrc_update_prep(struct io_kiocb *req, |
05f3fb3c JA |
6817 | const struct io_uring_sqe *sqe) |
6818 | { | |
61710e43 DA |
6819 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6820 | return -EINVAL; | |
26578cda | 6821 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
05f3fb3c JA |
6822 | return -EINVAL; |
6823 | ||
269bbe5f BM |
6824 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
6825 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); | |
6826 | if (!req->rsrc_update.nr_args) | |
05f3fb3c | 6827 | return -EINVAL; |
269bbe5f | 6828 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
05f3fb3c JA |
6829 | return 0; |
6830 | } | |
6831 | ||
889fca73 | 6832 | static int io_files_update(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6833 | { |
6834 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 6835 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
c3bdad02 | 6836 | struct io_uring_rsrc_update2 up; |
05f3fb3c | 6837 | int ret; |
fbf23849 | 6838 | |
269bbe5f BM |
6839 | up.offset = req->rsrc_update.offset; |
6840 | up.data = req->rsrc_update.arg; | |
c3bdad02 PB |
6841 | up.nr = 0; |
6842 | up.tags = 0; | |
615cee49 | 6843 | up.resv = 0; |
d8a3ba9c | 6844 | up.resv2 = 0; |
05f3fb3c | 6845 | |
3b44b371 | 6846 | io_ring_submit_lock(ctx, needs_lock); |
fdecb662 | 6847 | ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE, |
98f0b3b4 | 6848 | &up, req->rsrc_update.nr_args); |
3b44b371 | 6849 | io_ring_submit_unlock(ctx, needs_lock); |
05f3fb3c JA |
6850 | |
6851 | if (ret < 0) | |
93d2bcd2 | 6852 | req_set_fail(req); |
889fca73 | 6853 | __io_req_complete(req, issue_flags, ret, 0); |
5262f567 JA |
6854 | return 0; |
6855 | } | |
6856 | ||
bfe76559 | 6857 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 6858 | { |
d625c6ee | 6859 | switch (req->opcode) { |
e781573e | 6860 | case IORING_OP_NOP: |
bfe76559 | 6861 | return 0; |
f67676d1 JA |
6862 | case IORING_OP_READV: |
6863 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 6864 | case IORING_OP_READ: |
f67676d1 JA |
6865 | case IORING_OP_WRITEV: |
6866 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 6867 | case IORING_OP_WRITE: |
584b0180 | 6868 | return io_prep_rw(req, sqe); |
0969e783 | 6869 | case IORING_OP_POLL_ADD: |
bfe76559 | 6870 | return io_poll_add_prep(req, sqe); |
0969e783 | 6871 | case IORING_OP_POLL_REMOVE: |
c5de0036 | 6872 | return io_poll_update_prep(req, sqe); |
8ed8d3c3 | 6873 | case IORING_OP_FSYNC: |
1155c76a | 6874 | return io_fsync_prep(req, sqe); |
8ed8d3c3 | 6875 | case IORING_OP_SYNC_FILE_RANGE: |
1155c76a | 6876 | return io_sfr_prep(req, sqe); |
03b1230c | 6877 | case IORING_OP_SENDMSG: |
fddaface | 6878 | case IORING_OP_SEND: |
bfe76559 | 6879 | return io_sendmsg_prep(req, sqe); |
03b1230c | 6880 | case IORING_OP_RECVMSG: |
fddaface | 6881 | case IORING_OP_RECV: |
bfe76559 | 6882 | return io_recvmsg_prep(req, sqe); |
f499a021 | 6883 | case IORING_OP_CONNECT: |
bfe76559 | 6884 | return io_connect_prep(req, sqe); |
2d28390a | 6885 | case IORING_OP_TIMEOUT: |
bfe76559 | 6886 | return io_timeout_prep(req, sqe, false); |
b29472ee | 6887 | case IORING_OP_TIMEOUT_REMOVE: |
bfe76559 | 6888 | return io_timeout_remove_prep(req, sqe); |
fbf23849 | 6889 | case IORING_OP_ASYNC_CANCEL: |
bfe76559 | 6890 | return io_async_cancel_prep(req, sqe); |
2d28390a | 6891 | case IORING_OP_LINK_TIMEOUT: |
bfe76559 | 6892 | return io_timeout_prep(req, sqe, true); |
8ed8d3c3 | 6893 | case IORING_OP_ACCEPT: |
bfe76559 | 6894 | return io_accept_prep(req, sqe); |
d63d1b5e | 6895 | case IORING_OP_FALLOCATE: |
bfe76559 | 6896 | return io_fallocate_prep(req, sqe); |
15b71abe | 6897 | case IORING_OP_OPENAT: |
bfe76559 | 6898 | return io_openat_prep(req, sqe); |
b5dba59e | 6899 | case IORING_OP_CLOSE: |
bfe76559 | 6900 | return io_close_prep(req, sqe); |
05f3fb3c | 6901 | case IORING_OP_FILES_UPDATE: |
269bbe5f | 6902 | return io_rsrc_update_prep(req, sqe); |
eddc7ef5 | 6903 | case IORING_OP_STATX: |
bfe76559 | 6904 | return io_statx_prep(req, sqe); |
4840e418 | 6905 | case IORING_OP_FADVISE: |
bfe76559 | 6906 | return io_fadvise_prep(req, sqe); |
c1ca757b | 6907 | case IORING_OP_MADVISE: |
bfe76559 | 6908 | return io_madvise_prep(req, sqe); |
cebdb986 | 6909 | case IORING_OP_OPENAT2: |
bfe76559 | 6910 | return io_openat2_prep(req, sqe); |
3e4827b0 | 6911 | case IORING_OP_EPOLL_CTL: |
bfe76559 | 6912 | return io_epoll_ctl_prep(req, sqe); |
7d67af2c | 6913 | case IORING_OP_SPLICE: |
bfe76559 | 6914 | return io_splice_prep(req, sqe); |
ddf0322d | 6915 | case IORING_OP_PROVIDE_BUFFERS: |
bfe76559 | 6916 | return io_provide_buffers_prep(req, sqe); |
067524e9 | 6917 | case IORING_OP_REMOVE_BUFFERS: |
bfe76559 | 6918 | return io_remove_buffers_prep(req, sqe); |
f2a8d5c7 | 6919 | case IORING_OP_TEE: |
bfe76559 | 6920 | return io_tee_prep(req, sqe); |
36f4fa68 JA |
6921 | case IORING_OP_SHUTDOWN: |
6922 | return io_shutdown_prep(req, sqe); | |
80a261fd JA |
6923 | case IORING_OP_RENAMEAT: |
6924 | return io_renameat_prep(req, sqe); | |
14a1143b JA |
6925 | case IORING_OP_UNLINKAT: |
6926 | return io_unlinkat_prep(req, sqe); | |
e34a02dc DK |
6927 | case IORING_OP_MKDIRAT: |
6928 | return io_mkdirat_prep(req, sqe); | |
7a8721f8 DK |
6929 | case IORING_OP_SYMLINKAT: |
6930 | return io_symlinkat_prep(req, sqe); | |
cf30da90 DK |
6931 | case IORING_OP_LINKAT: |
6932 | return io_linkat_prep(req, sqe); | |
4f57f06c JA |
6933 | case IORING_OP_MSG_RING: |
6934 | return io_msg_ring_prep(req, sqe); | |
f67676d1 JA |
6935 | } |
6936 | ||
bfe76559 PB |
6937 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
6938 | req->opcode); | |
bd54b6fe | 6939 | return -EINVAL; |
bfe76559 PB |
6940 | } |
6941 | ||
93642ef8 | 6942 | static int io_req_prep_async(struct io_kiocb *req) |
bfe76559 | 6943 | { |
b7e298d2 PB |
6944 | if (!io_op_defs[req->opcode].needs_async_setup) |
6945 | return 0; | |
d886e185 | 6946 | if (WARN_ON_ONCE(req_has_async_data(req))) |
b7e298d2 PB |
6947 | return -EFAULT; |
6948 | if (io_alloc_async_data(req)) | |
6949 | return -EAGAIN; | |
6950 | ||
93642ef8 PB |
6951 | switch (req->opcode) { |
6952 | case IORING_OP_READV: | |
93642ef8 PB |
6953 | return io_rw_prep_async(req, READ); |
6954 | case IORING_OP_WRITEV: | |
93642ef8 PB |
6955 | return io_rw_prep_async(req, WRITE); |
6956 | case IORING_OP_SENDMSG: | |
93642ef8 PB |
6957 | return io_sendmsg_prep_async(req); |
6958 | case IORING_OP_RECVMSG: | |
93642ef8 PB |
6959 | return io_recvmsg_prep_async(req); |
6960 | case IORING_OP_CONNECT: | |
6961 | return io_connect_prep_async(req); | |
6962 | } | |
b7e298d2 PB |
6963 | printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n", |
6964 | req->opcode); | |
6965 | return -EFAULT; | |
f67676d1 JA |
6966 | } |
6967 | ||
9cf7c104 PB |
6968 | static u32 io_get_sequence(struct io_kiocb *req) |
6969 | { | |
a3dbdf54 | 6970 | u32 seq = req->ctx->cached_sq_head; |
9cf7c104 | 6971 | |
a3dbdf54 PB |
6972 | /* need original cached_sq_head, but it was increased for each req */ |
6973 | io_for_each_link(req, req) | |
6974 | seq--; | |
6975 | return seq; | |
9cf7c104 PB |
6976 | } |
6977 | ||
c072481d | 6978 | static __cold void io_drain_req(struct io_kiocb *req) |
de0617e4 | 6979 | { |
a197f664 | 6980 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 6981 | struct io_defer_entry *de; |
f67676d1 | 6982 | int ret; |
e0eb71dc | 6983 | u32 seq = io_get_sequence(req); |
3c19966d | 6984 | |
9d858b21 | 6985 | /* Still need defer if there is pending req in defer list. */ |
e302f104 | 6986 | spin_lock(&ctx->completion_lock); |
5e371265 | 6987 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) { |
e302f104 | 6988 | spin_unlock(&ctx->completion_lock); |
e0eb71dc | 6989 | queue: |
10c66904 | 6990 | ctx->drain_active = false; |
e0eb71dc PB |
6991 | io_req_task_queue(req); |
6992 | return; | |
10c66904 | 6993 | } |
e302f104 | 6994 | spin_unlock(&ctx->completion_lock); |
9cf7c104 | 6995 | |
b7e298d2 | 6996 | ret = io_req_prep_async(req); |
e0eb71dc PB |
6997 | if (ret) { |
6998 | fail: | |
6999 | io_req_complete_failed(req, ret); | |
7000 | return; | |
7001 | } | |
cbdcb435 | 7002 | io_prep_async_link(req); |
27dc8338 | 7003 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
76cc33d7 | 7004 | if (!de) { |
1b48773f | 7005 | ret = -ENOMEM; |
e0eb71dc | 7006 | goto fail; |
76cc33d7 | 7007 | } |
2d28390a | 7008 | |
79ebeaee | 7009 | spin_lock(&ctx->completion_lock); |
9cf7c104 | 7010 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
79ebeaee | 7011 | spin_unlock(&ctx->completion_lock); |
27dc8338 | 7012 | kfree(de); |
e0eb71dc | 7013 | goto queue; |
de0617e4 JA |
7014 | } |
7015 | ||
502c87d6 | 7016 | trace_io_uring_defer(ctx, req, req->user_data, req->opcode); |
27dc8338 | 7017 | de->req = req; |
9cf7c104 | 7018 | de->seq = seq; |
27dc8338 | 7019 | list_add_tail(&de->list, &ctx->defer_list); |
79ebeaee | 7020 | spin_unlock(&ctx->completion_lock); |
de0617e4 JA |
7021 | } |
7022 | ||
68fb8979 | 7023 | static void io_clean_op(struct io_kiocb *req) |
99bc4c38 | 7024 | { |
8197b053 PB |
7025 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
7026 | spin_lock(&req->ctx->completion_lock); | |
cc3cec83 | 7027 | io_put_kbuf_comp(req); |
8197b053 PB |
7028 | spin_unlock(&req->ctx->completion_lock); |
7029 | } | |
99bc4c38 | 7030 | |
0e1b6fe3 PB |
7031 | if (req->flags & REQ_F_NEED_CLEANUP) { |
7032 | switch (req->opcode) { | |
7033 | case IORING_OP_READV: | |
7034 | case IORING_OP_READ_FIXED: | |
7035 | case IORING_OP_READ: | |
7036 | case IORING_OP_WRITEV: | |
7037 | case IORING_OP_WRITE_FIXED: | |
e8c2bc1f JA |
7038 | case IORING_OP_WRITE: { |
7039 | struct io_async_rw *io = req->async_data; | |
1dacb4df PB |
7040 | |
7041 | kfree(io->free_iovec); | |
0e1b6fe3 | 7042 | break; |
e8c2bc1f | 7043 | } |
0e1b6fe3 | 7044 | case IORING_OP_RECVMSG: |
e8c2bc1f JA |
7045 | case IORING_OP_SENDMSG: { |
7046 | struct io_async_msghdr *io = req->async_data; | |
257e84a5 PB |
7047 | |
7048 | kfree(io->free_iov); | |
0e1b6fe3 | 7049 | break; |
e8c2bc1f | 7050 | } |
f3cd4850 JA |
7051 | case IORING_OP_OPENAT: |
7052 | case IORING_OP_OPENAT2: | |
7053 | if (req->open.filename) | |
7054 | putname(req->open.filename); | |
7055 | break; | |
80a261fd JA |
7056 | case IORING_OP_RENAMEAT: |
7057 | putname(req->rename.oldpath); | |
7058 | putname(req->rename.newpath); | |
7059 | break; | |
14a1143b JA |
7060 | case IORING_OP_UNLINKAT: |
7061 | putname(req->unlink.filename); | |
7062 | break; | |
e34a02dc DK |
7063 | case IORING_OP_MKDIRAT: |
7064 | putname(req->mkdir.filename); | |
7065 | break; | |
7a8721f8 DK |
7066 | case IORING_OP_SYMLINKAT: |
7067 | putname(req->symlink.oldpath); | |
7068 | putname(req->symlink.newpath); | |
7069 | break; | |
cf30da90 DK |
7070 | case IORING_OP_LINKAT: |
7071 | putname(req->hardlink.oldpath); | |
7072 | putname(req->hardlink.newpath); | |
7073 | break; | |
1b6fe6e0 SR |
7074 | case IORING_OP_STATX: |
7075 | if (req->statx.filename) | |
7076 | putname(req->statx.filename); | |
7077 | break; | |
0e1b6fe3 | 7078 | } |
99bc4c38 | 7079 | } |
75652a30 JA |
7080 | if ((req->flags & REQ_F_POLLED) && req->apoll) { |
7081 | kfree(req->apoll->double_poll); | |
7082 | kfree(req->apoll); | |
7083 | req->apoll = NULL; | |
7084 | } | |
c854357b | 7085 | if (req->flags & REQ_F_CREDS) |
b8e64b53 | 7086 | put_cred(req->creds); |
d886e185 PB |
7087 | if (req->flags & REQ_F_ASYNC_DATA) { |
7088 | kfree(req->async_data); | |
7089 | req->async_data = NULL; | |
7090 | } | |
c854357b | 7091 | req->flags &= ~IO_REQ_CLEAN_FLAGS; |
99bc4c38 PB |
7092 | } |
7093 | ||
6bf9c47a JA |
7094 | static bool io_assign_file(struct io_kiocb *req, unsigned int issue_flags) |
7095 | { | |
7096 | if (req->file || !io_op_defs[req->opcode].needs_file) | |
7097 | return true; | |
7098 | ||
7099 | if (req->flags & REQ_F_FIXED_FILE) | |
82733d16 | 7100 | req->file = io_file_get_fixed(req, req->fd, issue_flags); |
6bf9c47a | 7101 | else |
82733d16 | 7102 | req->file = io_file_get_normal(req, req->fd); |
6bf9c47a JA |
7103 | if (req->file) |
7104 | return true; | |
7105 | ||
7106 | req_set_fail(req); | |
7107 | req->result = -EBADF; | |
7108 | return false; | |
7109 | } | |
7110 | ||
889fca73 | 7111 | static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 7112 | { |
5730b27e | 7113 | const struct cred *creds = NULL; |
d625c6ee | 7114 | int ret; |
2b188cc1 | 7115 | |
70152140 JA |
7116 | if (unlikely(!io_assign_file(req, issue_flags))) |
7117 | return -EBADF; | |
7118 | ||
6878b40e | 7119 | if (unlikely((req->flags & REQ_F_CREDS) && req->creds != current_cred())) |
c10d1f98 | 7120 | creds = override_creds(req->creds); |
5730b27e | 7121 | |
5bd2182d PM |
7122 | if (!io_op_defs[req->opcode].audit_skip) |
7123 | audit_uring_entry(req->opcode); | |
7124 | ||
d625c6ee | 7125 | switch (req->opcode) { |
2b188cc1 | 7126 | case IORING_OP_NOP: |
889fca73 | 7127 | ret = io_nop(req, issue_flags); |
2b188cc1 JA |
7128 | break; |
7129 | case IORING_OP_READV: | |
edafccee | 7130 | case IORING_OP_READ_FIXED: |
3a6820f2 | 7131 | case IORING_OP_READ: |
889fca73 | 7132 | ret = io_read(req, issue_flags); |
edafccee | 7133 | break; |
3529d8c2 | 7134 | case IORING_OP_WRITEV: |
edafccee | 7135 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 7136 | case IORING_OP_WRITE: |
889fca73 | 7137 | ret = io_write(req, issue_flags); |
2b188cc1 | 7138 | break; |
c992fe29 | 7139 | case IORING_OP_FSYNC: |
45d189c6 | 7140 | ret = io_fsync(req, issue_flags); |
c992fe29 | 7141 | break; |
221c5eb2 | 7142 | case IORING_OP_POLL_ADD: |
61e98203 | 7143 | ret = io_poll_add(req, issue_flags); |
221c5eb2 JA |
7144 | break; |
7145 | case IORING_OP_POLL_REMOVE: | |
c5de0036 | 7146 | ret = io_poll_update(req, issue_flags); |
221c5eb2 | 7147 | break; |
5d17b4a4 | 7148 | case IORING_OP_SYNC_FILE_RANGE: |
45d189c6 | 7149 | ret = io_sync_file_range(req, issue_flags); |
5d17b4a4 | 7150 | break; |
0fa03c62 | 7151 | case IORING_OP_SENDMSG: |
889fca73 | 7152 | ret = io_sendmsg(req, issue_flags); |
062d04d7 | 7153 | break; |
fddaface | 7154 | case IORING_OP_SEND: |
889fca73 | 7155 | ret = io_send(req, issue_flags); |
0fa03c62 | 7156 | break; |
aa1fa28f | 7157 | case IORING_OP_RECVMSG: |
889fca73 | 7158 | ret = io_recvmsg(req, issue_flags); |
062d04d7 | 7159 | break; |
fddaface | 7160 | case IORING_OP_RECV: |
889fca73 | 7161 | ret = io_recv(req, issue_flags); |
aa1fa28f | 7162 | break; |
5262f567 | 7163 | case IORING_OP_TIMEOUT: |
61e98203 | 7164 | ret = io_timeout(req, issue_flags); |
5262f567 | 7165 | break; |
11365043 | 7166 | case IORING_OP_TIMEOUT_REMOVE: |
61e98203 | 7167 | ret = io_timeout_remove(req, issue_flags); |
11365043 | 7168 | break; |
17f2fe35 | 7169 | case IORING_OP_ACCEPT: |
889fca73 | 7170 | ret = io_accept(req, issue_flags); |
17f2fe35 | 7171 | break; |
f8e85cf2 | 7172 | case IORING_OP_CONNECT: |
889fca73 | 7173 | ret = io_connect(req, issue_flags); |
f8e85cf2 | 7174 | break; |
62755e35 | 7175 | case IORING_OP_ASYNC_CANCEL: |
61e98203 | 7176 | ret = io_async_cancel(req, issue_flags); |
62755e35 | 7177 | break; |
d63d1b5e | 7178 | case IORING_OP_FALLOCATE: |
45d189c6 | 7179 | ret = io_fallocate(req, issue_flags); |
d63d1b5e | 7180 | break; |
15b71abe | 7181 | case IORING_OP_OPENAT: |
45d189c6 | 7182 | ret = io_openat(req, issue_flags); |
15b71abe | 7183 | break; |
b5dba59e | 7184 | case IORING_OP_CLOSE: |
889fca73 | 7185 | ret = io_close(req, issue_flags); |
b5dba59e | 7186 | break; |
05f3fb3c | 7187 | case IORING_OP_FILES_UPDATE: |
889fca73 | 7188 | ret = io_files_update(req, issue_flags); |
05f3fb3c | 7189 | break; |
eddc7ef5 | 7190 | case IORING_OP_STATX: |
45d189c6 | 7191 | ret = io_statx(req, issue_flags); |
eddc7ef5 | 7192 | break; |
4840e418 | 7193 | case IORING_OP_FADVISE: |
45d189c6 | 7194 | ret = io_fadvise(req, issue_flags); |
4840e418 | 7195 | break; |
c1ca757b | 7196 | case IORING_OP_MADVISE: |
45d189c6 | 7197 | ret = io_madvise(req, issue_flags); |
c1ca757b | 7198 | break; |
cebdb986 | 7199 | case IORING_OP_OPENAT2: |
45d189c6 | 7200 | ret = io_openat2(req, issue_flags); |
cebdb986 | 7201 | break; |
3e4827b0 | 7202 | case IORING_OP_EPOLL_CTL: |
889fca73 | 7203 | ret = io_epoll_ctl(req, issue_flags); |
3e4827b0 | 7204 | break; |
7d67af2c | 7205 | case IORING_OP_SPLICE: |
45d189c6 | 7206 | ret = io_splice(req, issue_flags); |
7d67af2c | 7207 | break; |
ddf0322d | 7208 | case IORING_OP_PROVIDE_BUFFERS: |
889fca73 | 7209 | ret = io_provide_buffers(req, issue_flags); |
ddf0322d | 7210 | break; |
067524e9 | 7211 | case IORING_OP_REMOVE_BUFFERS: |
889fca73 | 7212 | ret = io_remove_buffers(req, issue_flags); |
3e4827b0 | 7213 | break; |
f2a8d5c7 | 7214 | case IORING_OP_TEE: |
45d189c6 | 7215 | ret = io_tee(req, issue_flags); |
f2a8d5c7 | 7216 | break; |
36f4fa68 | 7217 | case IORING_OP_SHUTDOWN: |
45d189c6 | 7218 | ret = io_shutdown(req, issue_flags); |
36f4fa68 | 7219 | break; |
80a261fd | 7220 | case IORING_OP_RENAMEAT: |
45d189c6 | 7221 | ret = io_renameat(req, issue_flags); |
80a261fd | 7222 | break; |
14a1143b | 7223 | case IORING_OP_UNLINKAT: |
45d189c6 | 7224 | ret = io_unlinkat(req, issue_flags); |
14a1143b | 7225 | break; |
e34a02dc DK |
7226 | case IORING_OP_MKDIRAT: |
7227 | ret = io_mkdirat(req, issue_flags); | |
7228 | break; | |
7a8721f8 DK |
7229 | case IORING_OP_SYMLINKAT: |
7230 | ret = io_symlinkat(req, issue_flags); | |
7231 | break; | |
cf30da90 DK |
7232 | case IORING_OP_LINKAT: |
7233 | ret = io_linkat(req, issue_flags); | |
7234 | break; | |
4f57f06c JA |
7235 | case IORING_OP_MSG_RING: |
7236 | ret = io_msg_ring(req, issue_flags); | |
7237 | break; | |
2b188cc1 JA |
7238 | default: |
7239 | ret = -EINVAL; | |
7240 | break; | |
7241 | } | |
7242 | ||
5bd2182d PM |
7243 | if (!io_op_defs[req->opcode].audit_skip) |
7244 | audit_uring_exit(!ret, ret); | |
7245 | ||
5730b27e JA |
7246 | if (creds) |
7247 | revert_creds(creds); | |
def596e9 JA |
7248 | if (ret) |
7249 | return ret; | |
b532576e | 7250 | /* If the op doesn't have a file, we're not polling for it */ |
9983028e | 7251 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && req->file) |
9882131c | 7252 | io_iopoll_req_issued(req, issue_flags); |
def596e9 JA |
7253 | |
7254 | return 0; | |
2b188cc1 JA |
7255 | } |
7256 | ||
ebc11b6c PB |
7257 | static struct io_wq_work *io_wq_free_work(struct io_wq_work *work) |
7258 | { | |
7259 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
7260 | ||
7261 | req = io_put_req_find_next(req); | |
7262 | return req ? &req->work : NULL; | |
7263 | } | |
7264 | ||
5280f7e5 | 7265 | static void io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
7266 | { |
7267 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6bf9c47a | 7268 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
d01905db PB |
7269 | unsigned int issue_flags = IO_URING_F_UNLOCKED; |
7270 | bool needs_poll = false; | |
6df1db6b | 7271 | struct io_kiocb *timeout; |
6bf9c47a | 7272 | int ret = 0, err = -ECANCELED; |
2b188cc1 | 7273 | |
48dcd38d PB |
7274 | /* one will be dropped by ->io_free_work() after returning to io-wq */ |
7275 | if (!(req->flags & REQ_F_REFCOUNT)) | |
7276 | __io_req_set_refcount(req, 2); | |
7277 | else | |
7278 | req_ref_get(req); | |
5d5901a3 | 7279 | |
6df1db6b PB |
7280 | timeout = io_prep_linked_timeout(req); |
7281 | if (timeout) | |
7282 | io_queue_linked_timeout(timeout); | |
d4c81f38 | 7283 | |
6bf9c47a | 7284 | |
dadebc35 | 7285 | /* either cancelled or io-wq is dying, so don't touch tctx->iowq */ |
d01905db | 7286 | if (work->flags & IO_WQ_WORK_CANCEL) { |
0f8da75b | 7287 | fail: |
6bf9c47a | 7288 | io_req_task_queue_fail(req, err); |
d01905db PB |
7289 | return; |
7290 | } | |
0f8da75b PB |
7291 | if (!io_assign_file(req, issue_flags)) { |
7292 | err = -EBADF; | |
7293 | work->flags |= IO_WQ_WORK_CANCEL; | |
7294 | goto fail; | |
7295 | } | |
31b51510 | 7296 | |
d01905db | 7297 | if (req->flags & REQ_F_FORCE_ASYNC) { |
afb7f56f PB |
7298 | bool opcode_poll = def->pollin || def->pollout; |
7299 | ||
7300 | if (opcode_poll && file_can_poll(req->file)) { | |
7301 | needs_poll = true; | |
d01905db | 7302 | issue_flags |= IO_URING_F_NONBLOCK; |
afb7f56f | 7303 | } |
561fb04a | 7304 | } |
31b51510 | 7305 | |
d01905db PB |
7306 | do { |
7307 | ret = io_issue_sqe(req, issue_flags); | |
7308 | if (ret != -EAGAIN) | |
7309 | break; | |
7310 | /* | |
7311 | * We can get EAGAIN for iopolled IO even though we're | |
7312 | * forcing a sync submission from here, since we can't | |
7313 | * wait for request slots on the block side. | |
7314 | */ | |
7315 | if (!needs_poll) { | |
7316 | cond_resched(); | |
7317 | continue; | |
90fa0288 HX |
7318 | } |
7319 | ||
4d9237e3 | 7320 | if (io_arm_poll_handler(req, issue_flags) == IO_APOLL_OK) |
d01905db PB |
7321 | return; |
7322 | /* aborted or ready, in either case retry blocking */ | |
7323 | needs_poll = false; | |
7324 | issue_flags &= ~IO_URING_F_NONBLOCK; | |
7325 | } while (1); | |
31b51510 | 7326 | |
a3df7698 | 7327 | /* avoid locking problems by failing it from a clean context */ |
5d5901a3 | 7328 | if (ret) |
a3df7698 | 7329 | io_req_task_queue_fail(req, ret); |
2b188cc1 JA |
7330 | } |
7331 | ||
aeca241b | 7332 | static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table, |
042b0d85 | 7333 | unsigned i) |
65e19f54 | 7334 | { |
042b0d85 | 7335 | return &table->files[i]; |
dafecf19 PB |
7336 | } |
7337 | ||
65e19f54 JA |
7338 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
7339 | int index) | |
7340 | { | |
aeca241b | 7341 | struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index); |
65e19f54 | 7342 | |
a04b0ac0 | 7343 | return (struct file *) (slot->file_ptr & FFS_MASK); |
65e19f54 JA |
7344 | } |
7345 | ||
a04b0ac0 | 7346 | static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file) |
9a321c98 PB |
7347 | { |
7348 | unsigned long file_ptr = (unsigned long) file; | |
7349 | ||
88459b50 | 7350 | file_ptr |= io_file_get_flags(file); |
a04b0ac0 | 7351 | file_slot->file_ptr = file_ptr; |
65e19f54 JA |
7352 | } |
7353 | ||
5106dd6e JA |
7354 | static inline struct file *io_file_get_fixed(struct io_kiocb *req, int fd, |
7355 | unsigned int issue_flags) | |
09bb8394 | 7356 | { |
5106dd6e JA |
7357 | struct io_ring_ctx *ctx = req->ctx; |
7358 | struct file *file = NULL; | |
ac177053 | 7359 | unsigned long file_ptr; |
09bb8394 | 7360 | |
5106dd6e JA |
7361 | if (issue_flags & IO_URING_F_UNLOCKED) |
7362 | mutex_lock(&ctx->uring_lock); | |
7363 | ||
ac177053 | 7364 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
5106dd6e | 7365 | goto out; |
ac177053 PB |
7366 | fd = array_index_nospec(fd, ctx->nr_user_files); |
7367 | file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr; | |
7368 | file = (struct file *) (file_ptr & FFS_MASK); | |
7369 | file_ptr &= ~FFS_MASK; | |
7370 | /* mask in overlapping REQ_F and FFS bits */ | |
35645ac3 | 7371 | req->flags |= (file_ptr << REQ_F_SUPPORT_NOWAIT_BIT); |
5106dd6e JA |
7372 | io_req_set_rsrc_node(req, ctx, 0); |
7373 | out: | |
7374 | if (issue_flags & IO_URING_F_UNLOCKED) | |
7375 | mutex_unlock(&ctx->uring_lock); | |
ac177053 PB |
7376 | return file; |
7377 | } | |
d44f554e | 7378 | |
d5361233 JA |
7379 | /* |
7380 | * Drop the file for requeue operations. Only used of req->file is the | |
7381 | * io_uring descriptor itself. | |
7382 | */ | |
7383 | static void io_drop_inflight_file(struct io_kiocb *req) | |
7384 | { | |
7385 | if (unlikely(req->flags & REQ_F_INFLIGHT)) { | |
7386 | fput(req->file); | |
7387 | req->file = NULL; | |
7388 | req->flags &= ~REQ_F_INFLIGHT; | |
7389 | } | |
7390 | } | |
7391 | ||
5106dd6e | 7392 | static struct file *io_file_get_normal(struct io_kiocb *req, int fd) |
ac177053 | 7393 | { |
62906e89 | 7394 | struct file *file = fget(fd); |
ac177053 | 7395 | |
5106dd6e | 7396 | trace_io_uring_file_get(req->ctx, req, req->user_data, fd); |
09bb8394 | 7397 | |
ac177053 | 7398 | /* we don't allow fixed io_uring files */ |
d5361233 JA |
7399 | if (file && file->f_op == &io_uring_fops) |
7400 | req->flags |= REQ_F_INFLIGHT; | |
8371adf5 | 7401 | return file; |
09bb8394 JA |
7402 | } |
7403 | ||
f237c30a | 7404 | static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked) |
89b263f6 JA |
7405 | { |
7406 | struct io_kiocb *prev = req->timeout.prev; | |
617a8948 | 7407 | int ret = -ENOENT; |
89b263f6 JA |
7408 | |
7409 | if (prev) { | |
617a8948 PB |
7410 | if (!(req->task->flags & PF_EXITING)) |
7411 | ret = io_try_cancel_userdata(req, prev->user_data); | |
505657bc | 7412 | io_req_complete_post(req, ret ?: -ETIME, 0); |
89b263f6 | 7413 | io_put_req(prev); |
89b263f6 JA |
7414 | } else { |
7415 | io_req_complete_post(req, -ETIME, 0); | |
7416 | } | |
7417 | } | |
7418 | ||
2665abfd | 7419 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 7420 | { |
ad8a48ac JA |
7421 | struct io_timeout_data *data = container_of(timer, |
7422 | struct io_timeout_data, timer); | |
90cd7e42 | 7423 | struct io_kiocb *prev, *req = data->req; |
2665abfd | 7424 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 7425 | unsigned long flags; |
2665abfd | 7426 | |
89b263f6 | 7427 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
90cd7e42 PB |
7428 | prev = req->timeout.head; |
7429 | req->timeout.head = NULL; | |
2665abfd JA |
7430 | |
7431 | /* | |
7432 | * We don't expect the list to be empty, that will only happen if we | |
7433 | * race with the completion of the linked work. | |
7434 | */ | |
447c19f3 | 7435 | if (prev) { |
f2f87370 | 7436 | io_remove_next_linked(prev); |
447c19f3 PB |
7437 | if (!req_ref_inc_not_zero(prev)) |
7438 | prev = NULL; | |
7439 | } | |
ef9dd637 | 7440 | list_del(&req->timeout.list); |
89b263f6 JA |
7441 | req->timeout.prev = prev; |
7442 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); | |
2665abfd | 7443 | |
89b263f6 | 7444 | req->io_task_work.func = io_req_task_link_timeout; |
4813c377 | 7445 | io_req_task_work_add(req, false); |
2665abfd JA |
7446 | return HRTIMER_NORESTART; |
7447 | } | |
7448 | ||
de968c18 | 7449 | static void io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 7450 | { |
de968c18 PB |
7451 | struct io_ring_ctx *ctx = req->ctx; |
7452 | ||
89b263f6 | 7453 | spin_lock_irq(&ctx->timeout_lock); |
76a46e06 | 7454 | /* |
f2f87370 PB |
7455 | * If the back reference is NULL, then our linked request finished |
7456 | * before we got a chance to setup the timer | |
76a46e06 | 7457 | */ |
90cd7e42 | 7458 | if (req->timeout.head) { |
e8c2bc1f | 7459 | struct io_timeout_data *data = req->async_data; |
94ae5e77 | 7460 | |
ad8a48ac JA |
7461 | data->timer.function = io_link_timeout_fn; |
7462 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
7463 | data->mode); | |
ef9dd637 | 7464 | list_add_tail(&req->timeout.list, &ctx->ltimeout_list); |
2665abfd | 7465 | } |
89b263f6 | 7466 | spin_unlock_irq(&ctx->timeout_lock); |
2665abfd | 7467 | /* drop submission reference */ |
76a46e06 JA |
7468 | io_put_req(req); |
7469 | } | |
2665abfd | 7470 | |
d475a9a6 PB |
7471 | static void io_queue_sqe_arm_apoll(struct io_kiocb *req) |
7472 | __must_hold(&req->ctx->uring_lock) | |
7473 | { | |
7474 | struct io_kiocb *linked_timeout = io_prep_linked_timeout(req); | |
7475 | ||
4d9237e3 | 7476 | switch (io_arm_poll_handler(req, 0)) { |
d475a9a6 | 7477 | case IO_APOLL_READY: |
d475a9a6 PB |
7478 | io_req_task_queue(req); |
7479 | break; | |
7480 | case IO_APOLL_ABORTED: | |
7481 | /* | |
7482 | * Queued up for async execution, worker will release | |
7483 | * submit reference when the iocb is actually submitted. | |
7484 | */ | |
7485 | io_queue_async_work(req, NULL); | |
7486 | break; | |
b1c62645 | 7487 | case IO_APOLL_OK: |
b1c62645 | 7488 | break; |
d475a9a6 PB |
7489 | } |
7490 | ||
7491 | if (linked_timeout) | |
7492 | io_queue_linked_timeout(linked_timeout); | |
7493 | } | |
7494 | ||
7495 | static inline void __io_queue_sqe(struct io_kiocb *req) | |
282cdc86 | 7496 | __must_hold(&req->ctx->uring_lock) |
2b188cc1 | 7497 | { |
906c6caa | 7498 | struct io_kiocb *linked_timeout; |
e0c5c576 | 7499 | int ret; |
2b188cc1 | 7500 | |
c5eef2b9 | 7501 | ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER); |
193155c8 | 7502 | |
fff4e40e PB |
7503 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
7504 | io_req_add_compl_list(req); | |
d9f9d284 | 7505 | return; |
fff4e40e | 7506 | } |
491381ce JA |
7507 | /* |
7508 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
7509 | * doesn't support non-blocking read/write attempts | |
7510 | */ | |
1840038e | 7511 | if (likely(!ret)) { |
906c6caa PB |
7512 | linked_timeout = io_prep_linked_timeout(req); |
7513 | if (linked_timeout) | |
7514 | io_queue_linked_timeout(linked_timeout); | |
1840038e | 7515 | } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
d475a9a6 | 7516 | io_queue_sqe_arm_apoll(req); |
0d63c148 | 7517 | } else { |
f41db273 | 7518 | io_req_complete_failed(req, ret); |
9e645e11 | 7519 | } |
2b188cc1 JA |
7520 | } |
7521 | ||
4652fe3f | 7522 | static void io_queue_sqe_fallback(struct io_kiocb *req) |
282cdc86 | 7523 | __must_hold(&req->ctx->uring_lock) |
4fe2c963 | 7524 | { |
4652fe3f | 7525 | if (req->flags & REQ_F_FAIL) { |
c6d3d9cb | 7526 | io_req_complete_fail_submit(req); |
e0eb71dc PB |
7527 | } else if (unlikely(req->ctx->drain_active)) { |
7528 | io_drain_req(req); | |
76cc33d7 PB |
7529 | } else { |
7530 | int ret = io_req_prep_async(req); | |
7531 | ||
7532 | if (unlikely(ret)) | |
7533 | io_req_complete_failed(req, ret); | |
7534 | else | |
f237c30a | 7535 | io_queue_async_work(req, NULL); |
ce35a47a | 7536 | } |
4fe2c963 JL |
7537 | } |
7538 | ||
4652fe3f PB |
7539 | static inline void io_queue_sqe(struct io_kiocb *req) |
7540 | __must_hold(&req->ctx->uring_lock) | |
7541 | { | |
7542 | if (likely(!(req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL)))) | |
7543 | __io_queue_sqe(req); | |
7544 | else | |
7545 | io_queue_sqe_fallback(req); | |
7546 | } | |
7547 | ||
b16fed66 PB |
7548 | /* |
7549 | * Check SQE restrictions (opcode and flags). | |
7550 | * | |
7551 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
7552 | */ | |
7553 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
7554 | struct io_kiocb *req, | |
7555 | unsigned int sqe_flags) | |
4fe2c963 | 7556 | { |
b16fed66 PB |
7557 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) |
7558 | return false; | |
7559 | ||
7560 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
7561 | ctx->restrictions.sqe_flags_required) | |
7562 | return false; | |
7563 | ||
7564 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
7565 | ctx->restrictions.sqe_flags_required)) | |
7566 | return false; | |
7567 | ||
7568 | return true; | |
4fe2c963 JL |
7569 | } |
7570 | ||
22b2ca31 PB |
7571 | static void io_init_req_drain(struct io_kiocb *req) |
7572 | { | |
7573 | struct io_ring_ctx *ctx = req->ctx; | |
7574 | struct io_kiocb *head = ctx->submit_state.link.head; | |
7575 | ||
7576 | ctx->drain_active = true; | |
7577 | if (head) { | |
7578 | /* | |
7579 | * If we need to drain a request in the middle of a link, drain | |
7580 | * the head request and the next request/link after the current | |
7581 | * link. Considering sequential execution of links, | |
b6c7db32 | 7582 | * REQ_F_IO_DRAIN will be maintained for every request of our |
22b2ca31 PB |
7583 | * link. |
7584 | */ | |
b6c7db32 | 7585 | head->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC; |
22b2ca31 PB |
7586 | ctx->drain_next = true; |
7587 | } | |
7588 | } | |
7589 | ||
b16fed66 PB |
7590 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
7591 | const struct io_uring_sqe *sqe) | |
282cdc86 | 7592 | __must_hold(&ctx->uring_lock) |
b16fed66 | 7593 | { |
b16fed66 | 7594 | unsigned int sqe_flags; |
fc0ae024 | 7595 | int personality; |
4a04d1d1 | 7596 | u8 opcode; |
b16fed66 | 7597 | |
864ea921 | 7598 | /* req is partially pre-initialised, see io_preinit_req() */ |
4a04d1d1 | 7599 | req->opcode = opcode = READ_ONCE(sqe->opcode); |
b16fed66 PB |
7600 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
7601 | req->flags = sqe_flags = READ_ONCE(sqe->flags); | |
7602 | req->user_data = READ_ONCE(sqe->user_data); | |
b16fed66 | 7603 | req->file = NULL; |
b16fed66 | 7604 | req->fixed_rsrc_refs = NULL; |
b16fed66 | 7605 | req->task = current; |
b16fed66 | 7606 | |
4a04d1d1 PB |
7607 | if (unlikely(opcode >= IORING_OP_LAST)) { |
7608 | req->opcode = 0; | |
b16fed66 | 7609 | return -EINVAL; |
4a04d1d1 | 7610 | } |
68fe256a PB |
7611 | if (unlikely(sqe_flags & ~SQE_COMMON_FLAGS)) { |
7612 | /* enforce forwards compatibility on users */ | |
7613 | if (sqe_flags & ~SQE_VALID_FLAGS) | |
7614 | return -EINVAL; | |
7615 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && | |
4a04d1d1 | 7616 | !io_op_defs[opcode].buffer_select) |
68fe256a | 7617 | return -EOPNOTSUPP; |
5562a8d7 PB |
7618 | if (sqe_flags & IOSQE_CQE_SKIP_SUCCESS) |
7619 | ctx->drain_disabled = true; | |
7620 | if (sqe_flags & IOSQE_IO_DRAIN) { | |
7621 | if (ctx->drain_disabled) | |
7622 | return -EOPNOTSUPP; | |
22b2ca31 | 7623 | io_init_req_drain(req); |
5562a8d7 | 7624 | } |
2a56a9bd PB |
7625 | } |
7626 | if (unlikely(ctx->restricted || ctx->drain_active || ctx->drain_next)) { | |
7627 | if (ctx->restricted && !io_check_restriction(ctx, req, sqe_flags)) | |
7628 | return -EACCES; | |
7629 | /* knock it to the slow queue path, will be drained there */ | |
7630 | if (ctx->drain_active) | |
7631 | req->flags |= REQ_F_FORCE_ASYNC; | |
7632 | /* if there is no link, we're at "next" request and need to drain */ | |
7633 | if (unlikely(ctx->drain_next) && !ctx->submit_state.link.head) { | |
7634 | ctx->drain_next = false; | |
7635 | ctx->drain_active = true; | |
b6c7db32 | 7636 | req->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC; |
2a56a9bd | 7637 | } |
68fe256a | 7638 | } |
b16fed66 | 7639 | |
4a04d1d1 | 7640 | if (io_op_defs[opcode].needs_file) { |
6d63416d PB |
7641 | struct io_submit_state *state = &ctx->submit_state; |
7642 | ||
82733d16 | 7643 | req->fd = READ_ONCE(sqe->fd); |
6bf9c47a | 7644 | |
6d63416d PB |
7645 | /* |
7646 | * Plug now if we have more than 2 IO left after this, and the | |
7647 | * target is potentially a read/write to block based storage. | |
7648 | */ | |
4a04d1d1 | 7649 | if (state->need_plug && io_op_defs[opcode].plug) { |
6d63416d PB |
7650 | state->plug_started = true; |
7651 | state->need_plug = false; | |
5ca7a8b3 | 7652 | blk_start_plug_nr_ios(&state->plug, state->submit_nr); |
6d63416d | 7653 | } |
b16fed66 | 7654 | } |
863e0560 | 7655 | |
003e8dcc JA |
7656 | personality = READ_ONCE(sqe->personality); |
7657 | if (personality) { | |
cdab10bf LT |
7658 | int ret; |
7659 | ||
c10d1f98 PB |
7660 | req->creds = xa_load(&ctx->personalities, personality); |
7661 | if (!req->creds) | |
003e8dcc | 7662 | return -EINVAL; |
c10d1f98 | 7663 | get_cred(req->creds); |
cdc1404a PM |
7664 | ret = security_uring_override_creds(req->creds); |
7665 | if (ret) { | |
7666 | put_cred(req->creds); | |
7667 | return ret; | |
7668 | } | |
b8e64b53 | 7669 | req->flags |= REQ_F_CREDS; |
003e8dcc | 7670 | } |
b16fed66 | 7671 | |
fc0ae024 | 7672 | return io_req_prep(req, sqe); |
b16fed66 PB |
7673 | } |
7674 | ||
a6b8cadc | 7675 | static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
a1ab7b35 | 7676 | const struct io_uring_sqe *sqe) |
282cdc86 | 7677 | __must_hold(&ctx->uring_lock) |
9e645e11 | 7678 | { |
a1ab7b35 | 7679 | struct io_submit_link *link = &ctx->submit_state.link; |
ef4ff581 | 7680 | int ret; |
9e645e11 | 7681 | |
a6b8cadc PB |
7682 | ret = io_init_req(ctx, req, sqe); |
7683 | if (unlikely(ret)) { | |
502c87d6 | 7684 | trace_io_uring_req_failed(sqe, ctx, req, ret); |
a87acfde | 7685 | |
a8295b98 | 7686 | /* fail even hard links since we don't submit */ |
de59bc10 | 7687 | if (link->head) { |
a8295b98 HX |
7688 | /* |
7689 | * we can judge a link req is failed or cancelled by if | |
7690 | * REQ_F_FAIL is set, but the head is an exception since | |
7691 | * it may be set REQ_F_FAIL because of other req's failure | |
7692 | * so let's leverage req->result to distinguish if a head | |
7693 | * is set REQ_F_FAIL because of its failure or other req's | |
7694 | * failure so that we can set the correct ret code for it. | |
7695 | * init result here to avoid affecting the normal path. | |
7696 | */ | |
7697 | if (!(link->head->flags & REQ_F_FAIL)) | |
7698 | req_fail_link_node(link->head, -ECANCELED); | |
7699 | } else if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { | |
7700 | /* | |
7701 | * the current req is a normal req, we should return | |
7702 | * error and thus break the submittion loop. | |
7703 | */ | |
7704 | io_req_complete_failed(req, ret); | |
7705 | return ret; | |
de59bc10 | 7706 | } |
a8295b98 | 7707 | req_fail_link_node(req, ret); |
a6b8cadc | 7708 | } |
441b8a78 | 7709 | |
be7053b7 | 7710 | /* don't need @sqe from now on */ |
502c87d6 | 7711 | trace_io_uring_submit_sqe(ctx, req, req->user_data, req->opcode, |
236daeae OL |
7712 | req->flags, true, |
7713 | ctx->flags & IORING_SETUP_SQPOLL); | |
a6b8cadc | 7714 | |
9e645e11 JA |
7715 | /* |
7716 | * If we already have a head request, queue this one for async | |
7717 | * submittal once the head completes. If we don't have a head but | |
7718 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
7719 | * submitted sync once the chain is complete. If none of those | |
7720 | * conditions are true (normal request), then just queue it. | |
7721 | */ | |
863e0560 PB |
7722 | if (link->head) { |
7723 | struct io_kiocb *head = link->head; | |
4e88d6e7 | 7724 | |
a8295b98 HX |
7725 | if (!(req->flags & REQ_F_FAIL)) { |
7726 | ret = io_req_prep_async(req); | |
7727 | if (unlikely(ret)) { | |
7728 | req_fail_link_node(req, ret); | |
7729 | if (!(head->flags & REQ_F_FAIL)) | |
7730 | req_fail_link_node(head, -ECANCELED); | |
7731 | } | |
7732 | } | |
9d76377f | 7733 | trace_io_uring_link(ctx, req, head); |
f2f87370 | 7734 | link->last->link = req; |
863e0560 | 7735 | link->last = req; |
32fe525b | 7736 | |
f15a3431 PB |
7737 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) |
7738 | return 0; | |
32fe525b | 7739 | /* last request of a link, enqueue the link */ |
f15a3431 PB |
7740 | link->head = NULL; |
7741 | req = head; | |
7742 | } else if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { | |
7743 | link->head = req; | |
7744 | link->last = req; | |
7745 | return 0; | |
9e645e11 | 7746 | } |
2e6e1fde | 7747 | |
f15a3431 | 7748 | io_queue_sqe(req); |
1d4240cc | 7749 | return 0; |
9e645e11 JA |
7750 | } |
7751 | ||
9a56a232 JA |
7752 | /* |
7753 | * Batched submission is done, ensure local IO is flushed out. | |
7754 | */ | |
553deffd | 7755 | static void io_submit_state_end(struct io_ring_ctx *ctx) |
9a56a232 | 7756 | { |
553deffd PB |
7757 | struct io_submit_state *state = &ctx->submit_state; |
7758 | ||
a1ab7b35 | 7759 | if (state->link.head) |
de59bc10 | 7760 | io_queue_sqe(state->link.head); |
553deffd | 7761 | /* flush only after queuing links as they can generate completions */ |
c450178d | 7762 | io_submit_flush_completions(ctx); |
27926b68 JA |
7763 | if (state->plug_started) |
7764 | blk_finish_plug(&state->plug); | |
9a56a232 JA |
7765 | } |
7766 | ||
7767 | /* | |
7768 | * Start submission side cache. | |
7769 | */ | |
7770 | static void io_submit_state_start(struct io_submit_state *state, | |
ba88ff11 | 7771 | unsigned int max_ios) |
9a56a232 | 7772 | { |
27926b68 | 7773 | state->plug_started = false; |
4b628aeb | 7774 | state->need_plug = max_ios > 2; |
5ca7a8b3 | 7775 | state->submit_nr = max_ios; |
a1ab7b35 PB |
7776 | /* set only head, no need to init link_last in advance */ |
7777 | state->link.head = NULL; | |
9a56a232 JA |
7778 | } |
7779 | ||
2b188cc1 JA |
7780 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
7781 | { | |
75b28aff | 7782 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 7783 | |
caf582c6 PB |
7784 | /* |
7785 | * Ensure any loads from the SQEs are done at this point, | |
7786 | * since once we write the new head, the application could | |
7787 | * write new data to them. | |
7788 | */ | |
7789 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
7790 | } |
7791 | ||
2b188cc1 | 7792 | /* |
dd9ae8a0 | 7793 | * Fetch an sqe, if one is available. Note this returns a pointer to memory |
2b188cc1 JA |
7794 | * that is mapped by userspace. This means that care needs to be taken to |
7795 | * ensure that reads are stable, as we cannot rely on userspace always | |
7796 | * being a good citizen. If members of the sqe are validated and then later | |
7797 | * used, it's important that those reads are done through READ_ONCE() to | |
7798 | * prevent a re-load down the line. | |
7799 | */ | |
709b302f | 7800 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 7801 | { |
ea5ab3b5 | 7802 | unsigned head, mask = ctx->sq_entries - 1; |
17d3aeb3 | 7803 | unsigned sq_idx = ctx->cached_sq_head++ & mask; |
2b188cc1 JA |
7804 | |
7805 | /* | |
7806 | * The cached sq head (or cq tail) serves two purposes: | |
7807 | * | |
7808 | * 1) allows us to batch the cost of updating the user visible | |
7809 | * head updates. | |
7810 | * 2) allows the kernel side to track the head on its own, even | |
7811 | * though the application is the one updating it. | |
7812 | */ | |
17d3aeb3 | 7813 | head = READ_ONCE(ctx->sq_array[sq_idx]); |
709b302f PB |
7814 | if (likely(head < ctx->sq_entries)) |
7815 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
7816 | |
7817 | /* drop invalid entries */ | |
15641e42 PB |
7818 | ctx->cq_extra--; |
7819 | WRITE_ONCE(ctx->rings->sq_dropped, | |
7820 | READ_ONCE(ctx->rings->sq_dropped) + 1); | |
709b302f PB |
7821 | return NULL; |
7822 | } | |
7823 | ||
0f212204 | 7824 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
282cdc86 | 7825 | __must_hold(&ctx->uring_lock) |
6c271ce2 | 7826 | { |
69629809 | 7827 | unsigned int entries = io_sqring_entries(ctx); |
46c4e16a | 7828 | int submitted = 0; |
6c271ce2 | 7829 | |
51d48dab | 7830 | if (unlikely(!entries)) |
69629809 | 7831 | return 0; |
ee7d46d9 | 7832 | /* make sure SQ entry isn't read before tail */ |
69629809 | 7833 | nr = min3(nr, ctx->sq_entries, entries); |
9a10867a | 7834 | io_get_task_refs(nr); |
6c271ce2 | 7835 | |
ba88ff11 | 7836 | io_submit_state_start(&ctx->submit_state, nr); |
69629809 | 7837 | do { |
3529d8c2 | 7838 | const struct io_uring_sqe *sqe; |
196be95c | 7839 | struct io_kiocb *req; |
fb5ccc98 | 7840 | |
a33ae9ce | 7841 | if (unlikely(!io_alloc_req_refill(ctx))) { |
196be95c PB |
7842 | if (!submitted) |
7843 | submitted = -EAGAIN; | |
fb5ccc98 | 7844 | break; |
196be95c | 7845 | } |
a33ae9ce | 7846 | req = io_alloc_req(ctx); |
4fccfcbb PB |
7847 | sqe = io_get_sqe(ctx); |
7848 | if (unlikely(!sqe)) { | |
c2b6c6bc | 7849 | wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list); |
4fccfcbb PB |
7850 | break; |
7851 | } | |
d3656344 JA |
7852 | /* will complete beyond this point, count as submitted */ |
7853 | submitted++; | |
bcbb7bf6 JA |
7854 | if (io_submit_sqe(ctx, req, sqe)) { |
7855 | /* | |
7856 | * Continue submitting even for sqe failure if the | |
7857 | * ring was setup with IORING_SETUP_SUBMIT_ALL | |
7858 | */ | |
7859 | if (!(ctx->flags & IORING_SETUP_SUBMIT_ALL)) | |
7860 | break; | |
7861 | } | |
69629809 | 7862 | } while (submitted < nr); |
6c271ce2 | 7863 | |
9466f437 PB |
7864 | if (unlikely(submitted != nr)) { |
7865 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; | |
d8a6df10 | 7866 | int unused = nr - ref_used; |
9466f437 | 7867 | |
09899b19 | 7868 | current->io_uring->cached_refs += unused; |
9466f437 | 7869 | } |
6c271ce2 | 7870 | |
553deffd | 7871 | io_submit_state_end(ctx); |
ae9428ca PB |
7872 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
7873 | io_commit_sqring(ctx); | |
7874 | ||
6c271ce2 JA |
7875 | return submitted; |
7876 | } | |
7877 | ||
e4b6d902 PB |
7878 | static inline bool io_sqd_events_pending(struct io_sq_data *sqd) |
7879 | { | |
7880 | return READ_ONCE(sqd->state); | |
7881 | } | |
7882 | ||
23b3628e XW |
7883 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
7884 | { | |
7885 | /* Tell userspace we may need a wakeup call */ | |
79ebeaee | 7886 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
7887 | WRITE_ONCE(ctx->rings->sq_flags, |
7888 | ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 7889 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
7890 | } |
7891 | ||
7892 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) | |
7893 | { | |
79ebeaee | 7894 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
7895 | WRITE_ONCE(ctx->rings->sq_flags, |
7896 | ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 7897 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
7898 | } |
7899 | ||
08369246 | 7900 | static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) |
6c271ce2 | 7901 | { |
c8d1ba58 | 7902 | unsigned int to_submit; |
bdcd3eab | 7903 | int ret = 0; |
6c271ce2 | 7904 | |
c8d1ba58 | 7905 | to_submit = io_sqring_entries(ctx); |
e95eee2d | 7906 | /* if we're handling multiple rings, cap submit size for fairness */ |
4ce8ad95 OL |
7907 | if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE) |
7908 | to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE; | |
e95eee2d | 7909 | |
5eef4e87 | 7910 | if (!wq_list_empty(&ctx->iopoll_list) || to_submit) { |
948e1947 PB |
7911 | const struct cred *creds = NULL; |
7912 | ||
7913 | if (ctx->sq_creds != current_cred()) | |
7914 | creds = override_creds(ctx->sq_creds); | |
a4c0b3de | 7915 | |
c8d1ba58 | 7916 | mutex_lock(&ctx->uring_lock); |
5eef4e87 | 7917 | if (!wq_list_empty(&ctx->iopoll_list)) |
5ba3c874 | 7918 | io_do_iopoll(ctx, true); |
906a3c6f | 7919 | |
3b763ba1 PB |
7920 | /* |
7921 | * Don't submit if refs are dying, good for io_uring_register(), | |
7922 | * but also it is relied upon by io_ring_exit_work() | |
7923 | */ | |
0298ef96 PB |
7924 | if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) && |
7925 | !(ctx->flags & IORING_SETUP_R_DISABLED)) | |
08369246 | 7926 | ret = io_submit_sqes(ctx, to_submit); |
c8d1ba58 | 7927 | mutex_unlock(&ctx->uring_lock); |
cb318216 | 7928 | |
acfb381d PB |
7929 | if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait)) |
7930 | wake_up(&ctx->sqo_sq_wait); | |
948e1947 PB |
7931 | if (creds) |
7932 | revert_creds(creds); | |
acfb381d | 7933 | } |
6c271ce2 | 7934 | |
08369246 XW |
7935 | return ret; |
7936 | } | |
6c271ce2 | 7937 | |
c072481d | 7938 | static __cold void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
08369246 XW |
7939 | { |
7940 | struct io_ring_ctx *ctx; | |
7941 | unsigned sq_thread_idle = 0; | |
6c271ce2 | 7942 | |
c9dca27d PB |
7943 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7944 | sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle); | |
08369246 | 7945 | sqd->sq_thread_idle = sq_thread_idle; |
c8d1ba58 | 7946 | } |
6c271ce2 | 7947 | |
e4b6d902 PB |
7948 | static bool io_sqd_handle_event(struct io_sq_data *sqd) |
7949 | { | |
7950 | bool did_sig = false; | |
7951 | struct ksignal ksig; | |
7952 | ||
7953 | if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) || | |
7954 | signal_pending(current)) { | |
7955 | mutex_unlock(&sqd->lock); | |
7956 | if (signal_pending(current)) | |
7957 | did_sig = get_signal(&ksig); | |
7958 | cond_resched(); | |
7959 | mutex_lock(&sqd->lock); | |
7960 | } | |
e4b6d902 PB |
7961 | return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
7962 | } | |
7963 | ||
c8d1ba58 JA |
7964 | static int io_sq_thread(void *data) |
7965 | { | |
69fb2131 JA |
7966 | struct io_sq_data *sqd = data; |
7967 | struct io_ring_ctx *ctx; | |
a0d9205f | 7968 | unsigned long timeout = 0; |
37d1e2e3 | 7969 | char buf[TASK_COMM_LEN]; |
08369246 | 7970 | DEFINE_WAIT(wait); |
6c271ce2 | 7971 | |
696ee88a | 7972 | snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid); |
37d1e2e3 | 7973 | set_task_comm(current, buf); |
37d1e2e3 JA |
7974 | |
7975 | if (sqd->sq_cpu != -1) | |
7976 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); | |
7977 | else | |
7978 | set_cpus_allowed_ptr(current, cpu_online_mask); | |
7979 | current->flags |= PF_NO_SETAFFINITY; | |
7980 | ||
5bd2182d PM |
7981 | audit_alloc_kernel(current); |
7982 | ||
09a6f4ef | 7983 | mutex_lock(&sqd->lock); |
e4b6d902 | 7984 | while (1) { |
1a924a80 | 7985 | bool cap_entries, sqt_spin = false; |
c1edbf5f | 7986 | |
e4b6d902 PB |
7987 | if (io_sqd_events_pending(sqd) || signal_pending(current)) { |
7988 | if (io_sqd_handle_event(sqd)) | |
c7d95613 | 7989 | break; |
08369246 XW |
7990 | timeout = jiffies + sqd->sq_thread_idle; |
7991 | } | |
e4b6d902 | 7992 | |
e95eee2d | 7993 | cap_entries = !list_is_singular(&sqd->ctx_list); |
69fb2131 | 7994 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
948e1947 | 7995 | int ret = __io_sq_thread(ctx, cap_entries); |
7c30f36a | 7996 | |
5eef4e87 | 7997 | if (!sqt_spin && (ret > 0 || !wq_list_empty(&ctx->iopoll_list))) |
08369246 | 7998 | sqt_spin = true; |
69fb2131 | 7999 | } |
dd432ea5 PB |
8000 | if (io_run_task_work()) |
8001 | sqt_spin = true; | |
6c271ce2 | 8002 | |
08369246 | 8003 | if (sqt_spin || !time_after(jiffies, timeout)) { |
c8d1ba58 | 8004 | cond_resched(); |
08369246 XW |
8005 | if (sqt_spin) |
8006 | timeout = jiffies + sqd->sq_thread_idle; | |
8007 | continue; | |
8008 | } | |
8009 | ||
08369246 | 8010 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); |
7f62d40d | 8011 | if (!io_sqd_events_pending(sqd) && !task_work_pending(current)) { |
1a924a80 PB |
8012 | bool needs_sched = true; |
8013 | ||
724cb4f9 | 8014 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
aaa9f0f4 PB |
8015 | io_ring_set_wakeup_flag(ctx); |
8016 | ||
724cb4f9 | 8017 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
5eef4e87 | 8018 | !wq_list_empty(&ctx->iopoll_list)) { |
724cb4f9 HX |
8019 | needs_sched = false; |
8020 | break; | |
8021 | } | |
649bb75d AK |
8022 | |
8023 | /* | |
8024 | * Ensure the store of the wakeup flag is not | |
8025 | * reordered with the load of the SQ tail | |
8026 | */ | |
8027 | smp_mb(); | |
8028 | ||
724cb4f9 HX |
8029 | if (io_sqring_entries(ctx)) { |
8030 | needs_sched = false; | |
8031 | break; | |
8032 | } | |
8033 | } | |
8034 | ||
8035 | if (needs_sched) { | |
8036 | mutex_unlock(&sqd->lock); | |
8037 | schedule(); | |
8038 | mutex_lock(&sqd->lock); | |
8039 | } | |
69fb2131 JA |
8040 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
8041 | io_ring_clear_wakeup_flag(ctx); | |
6c271ce2 | 8042 | } |
08369246 XW |
8043 | |
8044 | finish_wait(&sqd->wait, &wait); | |
8045 | timeout = jiffies + sqd->sq_thread_idle; | |
6c271ce2 | 8046 | } |
28cea78a | 8047 | |
78cc687b | 8048 | io_uring_cancel_generic(true, sqd); |
37d1e2e3 | 8049 | sqd->thread = NULL; |
05962f95 | 8050 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
5f3f26f9 | 8051 | io_ring_set_wakeup_flag(ctx); |
521d6a73 | 8052 | io_run_task_work(); |
734551df PB |
8053 | mutex_unlock(&sqd->lock); |
8054 | ||
5bd2182d PM |
8055 | audit_free(current); |
8056 | ||
37d1e2e3 JA |
8057 | complete(&sqd->exited); |
8058 | do_exit(0); | |
6c271ce2 JA |
8059 | } |
8060 | ||
bda52162 JA |
8061 | struct io_wait_queue { |
8062 | struct wait_queue_entry wq; | |
8063 | struct io_ring_ctx *ctx; | |
5fd46178 | 8064 | unsigned cq_tail; |
bda52162 JA |
8065 | unsigned nr_timeouts; |
8066 | }; | |
8067 | ||
6c503150 | 8068 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
bda52162 JA |
8069 | { |
8070 | struct io_ring_ctx *ctx = iowq->ctx; | |
5fd46178 | 8071 | int dist = ctx->cached_cq_tail - (int) iowq->cq_tail; |
bda52162 JA |
8072 | |
8073 | /* | |
d195a66e | 8074 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
8075 | * started waiting. For timeouts, we always want to return to userspace, |
8076 | * regardless of event count. | |
8077 | */ | |
5fd46178 | 8078 | return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
bda52162 JA |
8079 | } |
8080 | ||
8081 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
8082 | int wake_flags, void *key) | |
8083 | { | |
8084 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
8085 | wq); | |
8086 | ||
6c503150 PB |
8087 | /* |
8088 | * Cannot safely flush overflowed CQEs from here, ensure we wake up | |
8089 | * the task, and the next invocation will do it. | |
8090 | */ | |
5ed7a37d | 8091 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->check_cq_overflow)) |
6c503150 PB |
8092 | return autoremove_wake_function(curr, mode, wake_flags, key); |
8093 | return -1; | |
bda52162 JA |
8094 | } |
8095 | ||
af9c1a44 JA |
8096 | static int io_run_task_work_sig(void) |
8097 | { | |
8098 | if (io_run_task_work()) | |
8099 | return 1; | |
0b8cfa97 | 8100 | if (test_thread_flag(TIF_NOTIFY_SIGNAL)) |
792ee0f6 | 8101 | return -ERESTARTSYS; |
c5020bc8 OL |
8102 | if (task_sigpending(current)) |
8103 | return -EINTR; | |
8104 | return 0; | |
af9c1a44 JA |
8105 | } |
8106 | ||
eeb60b9a PB |
8107 | /* when returns >0, the caller should retry */ |
8108 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, | |
8109 | struct io_wait_queue *iowq, | |
22833966 | 8110 | ktime_t timeout) |
eeb60b9a PB |
8111 | { |
8112 | int ret; | |
8113 | ||
8114 | /* make sure we run task_work before checking for signals */ | |
8115 | ret = io_run_task_work_sig(); | |
8116 | if (ret || io_should_wake(iowq)) | |
8117 | return ret; | |
8118 | /* let the caller flush overflows, retry */ | |
5ed7a37d | 8119 | if (test_bit(0, &ctx->check_cq_overflow)) |
eeb60b9a PB |
8120 | return 1; |
8121 | ||
22833966 JA |
8122 | if (!schedule_hrtimeout(&timeout, HRTIMER_MODE_ABS)) |
8123 | return -ETIME; | |
8124 | return 1; | |
eeb60b9a PB |
8125 | } |
8126 | ||
2b188cc1 JA |
8127 | /* |
8128 | * Wait until events become available, if we don't already have some. The | |
8129 | * application must reap them itself, as they reside on the shared cq ring. | |
8130 | */ | |
8131 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
c73ebb68 HX |
8132 | const sigset_t __user *sig, size_t sigsz, |
8133 | struct __kernel_timespec __user *uts) | |
2b188cc1 | 8134 | { |
90291099 | 8135 | struct io_wait_queue iowq; |
75b28aff | 8136 | struct io_rings *rings = ctx->rings; |
22833966 | 8137 | ktime_t timeout = KTIME_MAX; |
c1d5a224 | 8138 | int ret; |
2b188cc1 | 8139 | |
b41e9852 | 8140 | do { |
90f67366 | 8141 | io_cqring_overflow_flush(ctx); |
6c503150 | 8142 | if (io_cqring_events(ctx) >= min_events) |
b41e9852 | 8143 | return 0; |
4c6e277c | 8144 | if (!io_run_task_work()) |
b41e9852 | 8145 | break; |
b41e9852 | 8146 | } while (1); |
2b188cc1 JA |
8147 | |
8148 | if (sig) { | |
9e75ad5d AB |
8149 | #ifdef CONFIG_COMPAT |
8150 | if (in_compat_syscall()) | |
8151 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 8152 | sigsz); |
9e75ad5d AB |
8153 | else |
8154 | #endif | |
b772434b | 8155 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 8156 | |
2b188cc1 JA |
8157 | if (ret) |
8158 | return ret; | |
8159 | } | |
8160 | ||
950e79dd OL |
8161 | if (uts) { |
8162 | struct timespec64 ts; | |
8163 | ||
8164 | if (get_timespec64(&ts, uts)) | |
8165 | return -EFAULT; | |
8166 | timeout = ktime_add_ns(timespec64_to_ktime(ts), ktime_get_ns()); | |
8167 | } | |
8168 | ||
90291099 PB |
8169 | init_waitqueue_func_entry(&iowq.wq, io_wake_function); |
8170 | iowq.wq.private = current; | |
8171 | INIT_LIST_HEAD(&iowq.wq.entry); | |
8172 | iowq.ctx = ctx; | |
bda52162 | 8173 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
5fd46178 | 8174 | iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events; |
90291099 | 8175 | |
c826bd7a | 8176 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 | 8177 | do { |
ca0a2651 | 8178 | /* if we can't even flush overflow, don't wait for more */ |
90f67366 | 8179 | if (!io_cqring_overflow_flush(ctx)) { |
ca0a2651 JA |
8180 | ret = -EBUSY; |
8181 | break; | |
8182 | } | |
311997b3 | 8183 | prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq, |
bda52162 | 8184 | TASK_INTERRUPTIBLE); |
22833966 | 8185 | ret = io_cqring_wait_schedule(ctx, &iowq, timeout); |
311997b3 | 8186 | finish_wait(&ctx->cq_wait, &iowq.wq); |
ca0a2651 | 8187 | cond_resched(); |
eeb60b9a | 8188 | } while (ret > 0); |
bda52162 | 8189 | |
b7db41c9 | 8190 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 8191 | |
75b28aff | 8192 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
8193 | } |
8194 | ||
9123c8ff | 8195 | static void io_free_page_table(void **table, size_t size) |
05f3fb3c | 8196 | { |
9123c8ff | 8197 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); |
05f3fb3c | 8198 | |
846a4ef2 | 8199 | for (i = 0; i < nr_tables; i++) |
9123c8ff PB |
8200 | kfree(table[i]); |
8201 | kfree(table); | |
8202 | } | |
8203 | ||
c072481d | 8204 | static __cold void **io_alloc_page_table(size_t size) |
9123c8ff PB |
8205 | { |
8206 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); | |
8207 | size_t init_size = size; | |
8208 | void **table; | |
8209 | ||
0bea96f5 | 8210 | table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
8211 | if (!table) |
8212 | return NULL; | |
8213 | ||
8214 | for (i = 0; i < nr_tables; i++) { | |
27f6b318 | 8215 | unsigned int this_size = min_t(size_t, size, PAGE_SIZE); |
9123c8ff | 8216 | |
0bea96f5 | 8217 | table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
8218 | if (!table[i]) { |
8219 | io_free_page_table(table, init_size); | |
8220 | return NULL; | |
8221 | } | |
8222 | size -= this_size; | |
8223 | } | |
8224 | return table; | |
05f3fb3c JA |
8225 | } |
8226 | ||
28a9fe25 | 8227 | static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node) |
1642b445 | 8228 | { |
28a9fe25 PB |
8229 | percpu_ref_exit(&ref_node->refs); |
8230 | kfree(ref_node); | |
1642b445 PB |
8231 | } |
8232 | ||
c072481d | 8233 | static __cold void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
b9bd2bea PB |
8234 | { |
8235 | struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs); | |
8236 | struct io_ring_ctx *ctx = node->rsrc_data->ctx; | |
8237 | unsigned long flags; | |
8238 | bool first_add = false; | |
b36a2050 | 8239 | unsigned long delay = HZ; |
b9bd2bea PB |
8240 | |
8241 | spin_lock_irqsave(&ctx->rsrc_ref_lock, flags); | |
8242 | node->done = true; | |
8243 | ||
b36a2050 DY |
8244 | /* if we are mid-quiesce then do not delay */ |
8245 | if (node->rsrc_data->quiesce) | |
8246 | delay = 0; | |
8247 | ||
b9bd2bea PB |
8248 | while (!list_empty(&ctx->rsrc_ref_list)) { |
8249 | node = list_first_entry(&ctx->rsrc_ref_list, | |
8250 | struct io_rsrc_node, node); | |
8251 | /* recycle ref nodes in order */ | |
8252 | if (!node->done) | |
8253 | break; | |
8254 | list_del(&node->node); | |
8255 | first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist); | |
8256 | } | |
8257 | spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags); | |
8258 | ||
8259 | if (first_add) | |
b36a2050 | 8260 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, delay); |
b9bd2bea PB |
8261 | } |
8262 | ||
f6133fbd | 8263 | static struct io_rsrc_node *io_rsrc_node_alloc(void) |
b9bd2bea PB |
8264 | { |
8265 | struct io_rsrc_node *ref_node; | |
8266 | ||
8267 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); | |
8268 | if (!ref_node) | |
8269 | return NULL; | |
8270 | ||
8271 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, | |
8272 | 0, GFP_KERNEL)) { | |
8273 | kfree(ref_node); | |
8274 | return NULL; | |
8275 | } | |
8276 | INIT_LIST_HEAD(&ref_node->node); | |
8277 | INIT_LIST_HEAD(&ref_node->rsrc_list); | |
8278 | ref_node->done = false; | |
8279 | return ref_node; | |
8280 | } | |
8281 | ||
a7f0ed5a PB |
8282 | static void io_rsrc_node_switch(struct io_ring_ctx *ctx, |
8283 | struct io_rsrc_data *data_to_kill) | |
ab409402 | 8284 | __must_hold(&ctx->uring_lock) |
6b06314c | 8285 | { |
a7f0ed5a PB |
8286 | WARN_ON_ONCE(!ctx->rsrc_backup_node); |
8287 | WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node); | |
6b06314c | 8288 | |
ab409402 PB |
8289 | io_rsrc_refs_drop(ctx); |
8290 | ||
a7f0ed5a PB |
8291 | if (data_to_kill) { |
8292 | struct io_rsrc_node *rsrc_node = ctx->rsrc_node; | |
82fbcfa9 | 8293 | |
a7f0ed5a | 8294 | rsrc_node->rsrc_data = data_to_kill; |
4956b9ea | 8295 | spin_lock_irq(&ctx->rsrc_ref_lock); |
a7f0ed5a | 8296 | list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list); |
4956b9ea | 8297 | spin_unlock_irq(&ctx->rsrc_ref_lock); |
82fbcfa9 | 8298 | |
3e942498 | 8299 | atomic_inc(&data_to_kill->refs); |
a7f0ed5a PB |
8300 | percpu_ref_kill(&rsrc_node->refs); |
8301 | ctx->rsrc_node = NULL; | |
8302 | } | |
6b06314c | 8303 | |
a7f0ed5a PB |
8304 | if (!ctx->rsrc_node) { |
8305 | ctx->rsrc_node = ctx->rsrc_backup_node; | |
8306 | ctx->rsrc_backup_node = NULL; | |
8307 | } | |
8bad28d8 HX |
8308 | } |
8309 | ||
a7f0ed5a | 8310 | static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx) |
8dd03afe PB |
8311 | { |
8312 | if (ctx->rsrc_backup_node) | |
8313 | return 0; | |
f6133fbd | 8314 | ctx->rsrc_backup_node = io_rsrc_node_alloc(); |
8dd03afe | 8315 | return ctx->rsrc_backup_node ? 0 : -ENOMEM; |
8bad28d8 HX |
8316 | } |
8317 | ||
c072481d PB |
8318 | static __cold int io_rsrc_ref_quiesce(struct io_rsrc_data *data, |
8319 | struct io_ring_ctx *ctx) | |
8bad28d8 HX |
8320 | { |
8321 | int ret; | |
05589553 | 8322 | |
215c3902 | 8323 | /* As we may drop ->uring_lock, other task may have started quiesce */ |
8bad28d8 HX |
8324 | if (data->quiesce) |
8325 | return -ENXIO; | |
05589553 | 8326 | |
8bad28d8 | 8327 | data->quiesce = true; |
1ffc5422 | 8328 | do { |
a7f0ed5a | 8329 | ret = io_rsrc_node_switch_start(ctx); |
8dd03afe | 8330 | if (ret) |
f2303b1f | 8331 | break; |
a7f0ed5a | 8332 | io_rsrc_node_switch(ctx, data); |
f2303b1f | 8333 | |
3e942498 PB |
8334 | /* kill initial ref, already quiesced if zero */ |
8335 | if (atomic_dec_and_test(&data->refs)) | |
8336 | break; | |
c018db4a | 8337 | mutex_unlock(&ctx->uring_lock); |
8bad28d8 | 8338 | flush_delayed_work(&ctx->rsrc_put_work); |
1ffc5422 | 8339 | ret = wait_for_completion_interruptible(&data->done); |
c018db4a JA |
8340 | if (!ret) { |
8341 | mutex_lock(&ctx->uring_lock); | |
80912cef DY |
8342 | if (atomic_read(&data->refs) > 0) { |
8343 | /* | |
8344 | * it has been revived by another thread while | |
8345 | * we were unlocked | |
8346 | */ | |
8347 | mutex_unlock(&ctx->uring_lock); | |
8348 | } else { | |
8349 | break; | |
8350 | } | |
c018db4a | 8351 | } |
8bad28d8 | 8352 | |
3e942498 PB |
8353 | atomic_inc(&data->refs); |
8354 | /* wait for all works potentially completing data->done */ | |
8355 | flush_delayed_work(&ctx->rsrc_put_work); | |
cb5e1b81 | 8356 | reinit_completion(&data->done); |
8dd03afe | 8357 | |
1ffc5422 | 8358 | ret = io_run_task_work_sig(); |
8bad28d8 | 8359 | mutex_lock(&ctx->uring_lock); |
f2303b1f | 8360 | } while (ret >= 0); |
8bad28d8 | 8361 | data->quiesce = false; |
05f3fb3c | 8362 | |
8bad28d8 | 8363 | return ret; |
d7954b2b BM |
8364 | } |
8365 | ||
2d091d62 PB |
8366 | static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx) |
8367 | { | |
8368 | unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK; | |
8369 | unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT; | |
8370 | ||
8371 | return &data->tags[table_idx][off]; | |
8372 | } | |
8373 | ||
44b31f2f | 8374 | static void io_rsrc_data_free(struct io_rsrc_data *data) |
1ad555c6 | 8375 | { |
2d091d62 PB |
8376 | size_t size = data->nr * sizeof(data->tags[0][0]); |
8377 | ||
8378 | if (data->tags) | |
8379 | io_free_page_table((void **)data->tags, size); | |
44b31f2f PB |
8380 | kfree(data); |
8381 | } | |
8382 | ||
c072481d PB |
8383 | static __cold int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put, |
8384 | u64 __user *utags, unsigned nr, | |
8385 | struct io_rsrc_data **pdata) | |
1ad555c6 | 8386 | { |
b895c9a6 | 8387 | struct io_rsrc_data *data; |
2d091d62 | 8388 | int ret = -ENOMEM; |
d878c816 | 8389 | unsigned i; |
1ad555c6 BM |
8390 | |
8391 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
8392 | if (!data) | |
d878c816 | 8393 | return -ENOMEM; |
2d091d62 | 8394 | data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0])); |
b60c8dce | 8395 | if (!data->tags) { |
1ad555c6 | 8396 | kfree(data); |
d878c816 PB |
8397 | return -ENOMEM; |
8398 | } | |
2d091d62 PB |
8399 | |
8400 | data->nr = nr; | |
8401 | data->ctx = ctx; | |
8402 | data->do_put = do_put; | |
d878c816 | 8403 | if (utags) { |
2d091d62 | 8404 | ret = -EFAULT; |
d878c816 | 8405 | for (i = 0; i < nr; i++) { |
fdd1dc31 CIK |
8406 | u64 *tag_slot = io_get_tag_slot(data, i); |
8407 | ||
8408 | if (copy_from_user(tag_slot, &utags[i], | |
8409 | sizeof(*tag_slot))) | |
2d091d62 | 8410 | goto fail; |
d878c816 | 8411 | } |
1ad555c6 | 8412 | } |
b60c8dce | 8413 | |
3e942498 | 8414 | atomic_set(&data->refs, 1); |
1ad555c6 | 8415 | init_completion(&data->done); |
d878c816 PB |
8416 | *pdata = data; |
8417 | return 0; | |
2d091d62 PB |
8418 | fail: |
8419 | io_rsrc_data_free(data); | |
8420 | return ret; | |
1ad555c6 BM |
8421 | } |
8422 | ||
9123c8ff PB |
8423 | static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files) |
8424 | { | |
0bea96f5 PB |
8425 | table->files = kvcalloc(nr_files, sizeof(table->files[0]), |
8426 | GFP_KERNEL_ACCOUNT); | |
9123c8ff PB |
8427 | return !!table->files; |
8428 | } | |
8429 | ||
042b0d85 | 8430 | static void io_free_file_tables(struct io_file_table *table) |
9123c8ff | 8431 | { |
042b0d85 | 8432 | kvfree(table->files); |
9123c8ff PB |
8433 | table->files = NULL; |
8434 | } | |
8435 | ||
fff4db76 | 8436 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
1ad555c6 | 8437 | { |
fff4db76 PB |
8438 | #if defined(CONFIG_UNIX) |
8439 | if (ctx->ring_sock) { | |
8440 | struct sock *sock = ctx->ring_sock->sk; | |
8441 | struct sk_buff *skb; | |
8442 | ||
8443 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
8444 | kfree_skb(skb); | |
8445 | } | |
8446 | #else | |
8447 | int i; | |
8448 | ||
8449 | for (i = 0; i < ctx->nr_user_files; i++) { | |
8450 | struct file *file; | |
8451 | ||
8452 | file = io_file_from_index(ctx, i); | |
8453 | if (file) | |
8454 | fput(file); | |
8455 | } | |
8456 | #endif | |
042b0d85 | 8457 | io_free_file_tables(&ctx->file_table); |
44b31f2f | 8458 | io_rsrc_data_free(ctx->file_data); |
fff4db76 PB |
8459 | ctx->file_data = NULL; |
8460 | ctx->nr_user_files = 0; | |
1ad555c6 BM |
8461 | } |
8462 | ||
d7954b2b BM |
8463 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
8464 | { | |
d7954b2b BM |
8465 | int ret; |
8466 | ||
08480400 | 8467 | if (!ctx->file_data) |
d7954b2b | 8468 | return -ENXIO; |
08480400 PB |
8469 | ret = io_rsrc_ref_quiesce(ctx->file_data, ctx); |
8470 | if (!ret) | |
8471 | __io_sqe_files_unregister(ctx); | |
8472 | return ret; | |
6b06314c JA |
8473 | } |
8474 | ||
37d1e2e3 | 8475 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
09a6f4ef | 8476 | __releases(&sqd->lock) |
37d1e2e3 | 8477 | { |
521d6a73 PB |
8478 | WARN_ON_ONCE(sqd->thread == current); |
8479 | ||
9e138a48 PB |
8480 | /* |
8481 | * Do the dance but not conditional clear_bit() because it'd race with | |
8482 | * other threads incrementing park_pending and setting the bit. | |
8483 | */ | |
37d1e2e3 | 8484 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
9e138a48 PB |
8485 | if (atomic_dec_return(&sqd->park_pending)) |
8486 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); | |
09a6f4ef | 8487 | mutex_unlock(&sqd->lock); |
37d1e2e3 JA |
8488 | } |
8489 | ||
86e0d676 | 8490 | static void io_sq_thread_park(struct io_sq_data *sqd) |
09a6f4ef | 8491 | __acquires(&sqd->lock) |
37d1e2e3 | 8492 | { |
521d6a73 PB |
8493 | WARN_ON_ONCE(sqd->thread == current); |
8494 | ||
9e138a48 | 8495 | atomic_inc(&sqd->park_pending); |
86e0d676 | 8496 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
09a6f4ef | 8497 | mutex_lock(&sqd->lock); |
05962f95 | 8498 | if (sqd->thread) |
86e0d676 | 8499 | wake_up_process(sqd->thread); |
37d1e2e3 JA |
8500 | } |
8501 | ||
8502 | static void io_sq_thread_stop(struct io_sq_data *sqd) | |
8503 | { | |
521d6a73 | 8504 | WARN_ON_ONCE(sqd->thread == current); |
88885f66 | 8505 | WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)); |
521d6a73 | 8506 | |
05962f95 | 8507 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
88885f66 | 8508 | mutex_lock(&sqd->lock); |
e8f98f24 JA |
8509 | if (sqd->thread) |
8510 | wake_up_process(sqd->thread); | |
09a6f4ef | 8511 | mutex_unlock(&sqd->lock); |
05962f95 | 8512 | wait_for_completion(&sqd->exited); |
37d1e2e3 JA |
8513 | } |
8514 | ||
534ca6d6 | 8515 | static void io_put_sq_data(struct io_sq_data *sqd) |
6c271ce2 | 8516 | { |
534ca6d6 | 8517 | if (refcount_dec_and_test(&sqd->refs)) { |
9e138a48 PB |
8518 | WARN_ON_ONCE(atomic_read(&sqd->park_pending)); |
8519 | ||
37d1e2e3 JA |
8520 | io_sq_thread_stop(sqd); |
8521 | kfree(sqd); | |
8522 | } | |
8523 | } | |
8524 | ||
8525 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) | |
8526 | { | |
8527 | struct io_sq_data *sqd = ctx->sq_data; | |
8528 | ||
8529 | if (sqd) { | |
05962f95 | 8530 | io_sq_thread_park(sqd); |
521d6a73 | 8531 | list_del_init(&ctx->sqd_list); |
37d1e2e3 | 8532 | io_sqd_update_thread_idle(sqd); |
05962f95 | 8533 | io_sq_thread_unpark(sqd); |
37d1e2e3 JA |
8534 | |
8535 | io_put_sq_data(sqd); | |
8536 | ctx->sq_data = NULL; | |
534ca6d6 JA |
8537 | } |
8538 | } | |
8539 | ||
aa06165d JA |
8540 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
8541 | { | |
8542 | struct io_ring_ctx *ctx_attach; | |
8543 | struct io_sq_data *sqd; | |
8544 | struct fd f; | |
8545 | ||
8546 | f = fdget(p->wq_fd); | |
8547 | if (!f.file) | |
8548 | return ERR_PTR(-ENXIO); | |
8549 | if (f.file->f_op != &io_uring_fops) { | |
8550 | fdput(f); | |
8551 | return ERR_PTR(-EINVAL); | |
8552 | } | |
8553 | ||
8554 | ctx_attach = f.file->private_data; | |
8555 | sqd = ctx_attach->sq_data; | |
8556 | if (!sqd) { | |
8557 | fdput(f); | |
8558 | return ERR_PTR(-EINVAL); | |
8559 | } | |
5c2469e0 JA |
8560 | if (sqd->task_tgid != current->tgid) { |
8561 | fdput(f); | |
8562 | return ERR_PTR(-EPERM); | |
8563 | } | |
aa06165d JA |
8564 | |
8565 | refcount_inc(&sqd->refs); | |
8566 | fdput(f); | |
8567 | return sqd; | |
8568 | } | |
8569 | ||
26984fbf PB |
8570 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p, |
8571 | bool *attached) | |
534ca6d6 JA |
8572 | { |
8573 | struct io_sq_data *sqd; | |
8574 | ||
26984fbf | 8575 | *attached = false; |
5c2469e0 JA |
8576 | if (p->flags & IORING_SETUP_ATTACH_WQ) { |
8577 | sqd = io_attach_sq_data(p); | |
26984fbf PB |
8578 | if (!IS_ERR(sqd)) { |
8579 | *attached = true; | |
5c2469e0 | 8580 | return sqd; |
26984fbf | 8581 | } |
5c2469e0 JA |
8582 | /* fall through for EPERM case, setup new sqd/task */ |
8583 | if (PTR_ERR(sqd) != -EPERM) | |
8584 | return sqd; | |
8585 | } | |
aa06165d | 8586 | |
534ca6d6 JA |
8587 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
8588 | if (!sqd) | |
8589 | return ERR_PTR(-ENOMEM); | |
8590 | ||
9e138a48 | 8591 | atomic_set(&sqd->park_pending, 0); |
534ca6d6 | 8592 | refcount_set(&sqd->refs, 1); |
69fb2131 | 8593 | INIT_LIST_HEAD(&sqd->ctx_list); |
09a6f4ef | 8594 | mutex_init(&sqd->lock); |
534ca6d6 | 8595 | init_waitqueue_head(&sqd->wait); |
37d1e2e3 | 8596 | init_completion(&sqd->exited); |
534ca6d6 JA |
8597 | return sqd; |
8598 | } | |
8599 | ||
6b06314c | 8600 | #if defined(CONFIG_UNIX) |
6b06314c JA |
8601 | /* |
8602 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
8603 | * the io_uring can be safely unregistered on process exit, even if we have | |
8604 | * loops in the file referencing. | |
8605 | */ | |
8606 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
8607 | { | |
8608 | struct sock *sk = ctx->ring_sock->sk; | |
8609 | struct scm_fp_list *fpl; | |
8610 | struct sk_buff *skb; | |
08a45173 | 8611 | int i, nr_files; |
6b06314c | 8612 | |
6b06314c JA |
8613 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
8614 | if (!fpl) | |
8615 | return -ENOMEM; | |
8616 | ||
8617 | skb = alloc_skb(0, GFP_KERNEL); | |
8618 | if (!skb) { | |
8619 | kfree(fpl); | |
8620 | return -ENOMEM; | |
8621 | } | |
8622 | ||
8623 | skb->sk = sk; | |
6b06314c | 8624 | |
08a45173 | 8625 | nr_files = 0; |
62e398be | 8626 | fpl->user = get_uid(current_user()); |
6b06314c | 8627 | for (i = 0; i < nr; i++) { |
65e19f54 JA |
8628 | struct file *file = io_file_from_index(ctx, i + offset); |
8629 | ||
8630 | if (!file) | |
08a45173 | 8631 | continue; |
65e19f54 | 8632 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
8633 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
8634 | nr_files++; | |
6b06314c JA |
8635 | } |
8636 | ||
08a45173 JA |
8637 | if (nr_files) { |
8638 | fpl->max = SCM_MAX_FD; | |
8639 | fpl->count = nr_files; | |
8640 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 8641 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
8642 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
8643 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 8644 | |
a07211e3 PB |
8645 | for (i = 0; i < nr; i++) { |
8646 | struct file *file = io_file_from_index(ctx, i + offset); | |
8647 | ||
8648 | if (file) | |
8649 | fput(file); | |
8650 | } | |
08a45173 JA |
8651 | } else { |
8652 | kfree_skb(skb); | |
c86d18f4 | 8653 | free_uid(fpl->user); |
08a45173 JA |
8654 | kfree(fpl); |
8655 | } | |
6b06314c JA |
8656 | |
8657 | return 0; | |
8658 | } | |
8659 | ||
8660 | /* | |
8661 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
8662 | * causes regular reference counting to break down. We rely on the UNIX | |
8663 | * garbage collection to take care of this problem for us. | |
8664 | */ | |
8665 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
8666 | { | |
8667 | unsigned left, total; | |
8668 | int ret = 0; | |
8669 | ||
8670 | total = 0; | |
8671 | left = ctx->nr_user_files; | |
8672 | while (left) { | |
8673 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
8674 | |
8675 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
8676 | if (ret) | |
8677 | break; | |
8678 | left -= this_files; | |
8679 | total += this_files; | |
8680 | } | |
8681 | ||
8682 | if (!ret) | |
8683 | return 0; | |
8684 | ||
8685 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
8686 | struct file *file = io_file_from_index(ctx, total); |
8687 | ||
8688 | if (file) | |
8689 | fput(file); | |
6b06314c JA |
8690 | total++; |
8691 | } | |
8692 | ||
8693 | return ret; | |
8694 | } | |
8695 | #else | |
8696 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
8697 | { | |
8698 | return 0; | |
8699 | } | |
8700 | #endif | |
8701 | ||
47e90392 | 8702 | static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
05f3fb3c | 8703 | { |
50238531 | 8704 | struct file *file = prsrc->file; |
05f3fb3c JA |
8705 | #if defined(CONFIG_UNIX) |
8706 | struct sock *sock = ctx->ring_sock->sk; | |
8707 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
8708 | struct sk_buff *skb; | |
8709 | int i; | |
8710 | ||
8711 | __skb_queue_head_init(&list); | |
8712 | ||
8713 | /* | |
8714 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
8715 | * remove this entry and rearrange the file array. | |
8716 | */ | |
8717 | skb = skb_dequeue(head); | |
8718 | while (skb) { | |
8719 | struct scm_fp_list *fp; | |
8720 | ||
8721 | fp = UNIXCB(skb).fp; | |
8722 | for (i = 0; i < fp->count; i++) { | |
8723 | int left; | |
8724 | ||
8725 | if (fp->fp[i] != file) | |
8726 | continue; | |
8727 | ||
8728 | unix_notinflight(fp->user, fp->fp[i]); | |
8729 | left = fp->count - 1 - i; | |
8730 | if (left) { | |
8731 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
8732 | left * sizeof(struct file *)); | |
8733 | } | |
8734 | fp->count--; | |
8735 | if (!fp->count) { | |
8736 | kfree_skb(skb); | |
8737 | skb = NULL; | |
8738 | } else { | |
8739 | __skb_queue_tail(&list, skb); | |
8740 | } | |
8741 | fput(file); | |
8742 | file = NULL; | |
8743 | break; | |
8744 | } | |
8745 | ||
8746 | if (!file) | |
8747 | break; | |
8748 | ||
8749 | __skb_queue_tail(&list, skb); | |
8750 | ||
8751 | skb = skb_dequeue(head); | |
8752 | } | |
8753 | ||
8754 | if (skb_peek(&list)) { | |
8755 | spin_lock_irq(&head->lock); | |
8756 | while ((skb = __skb_dequeue(&list)) != NULL) | |
8757 | __skb_queue_tail(head, skb); | |
8758 | spin_unlock_irq(&head->lock); | |
8759 | } | |
8760 | #else | |
8761 | fput(file); | |
8762 | #endif | |
8763 | } | |
8764 | ||
b895c9a6 | 8765 | static void __io_rsrc_put_work(struct io_rsrc_node *ref_node) |
65e19f54 | 8766 | { |
b895c9a6 | 8767 | struct io_rsrc_data *rsrc_data = ref_node->rsrc_data; |
269bbe5f BM |
8768 | struct io_ring_ctx *ctx = rsrc_data->ctx; |
8769 | struct io_rsrc_put *prsrc, *tmp; | |
05589553 | 8770 | |
269bbe5f BM |
8771 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
8772 | list_del(&prsrc->list); | |
b60c8dce PB |
8773 | |
8774 | if (prsrc->tag) { | |
8775 | bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL; | |
b60c8dce PB |
8776 | |
8777 | io_ring_submit_lock(ctx, lock_ring); | |
79ebeaee | 8778 | spin_lock(&ctx->completion_lock); |
913a571a | 8779 | io_fill_cqe_aux(ctx, prsrc->tag, 0, 0); |
b60c8dce | 8780 | io_commit_cqring(ctx); |
79ebeaee | 8781 | spin_unlock(&ctx->completion_lock); |
b60c8dce PB |
8782 | io_cqring_ev_posted(ctx); |
8783 | io_ring_submit_unlock(ctx, lock_ring); | |
8784 | } | |
8785 | ||
40ae0ff7 | 8786 | rsrc_data->do_put(ctx, prsrc); |
269bbe5f | 8787 | kfree(prsrc); |
65e19f54 | 8788 | } |
05589553 | 8789 | |
28a9fe25 | 8790 | io_rsrc_node_destroy(ref_node); |
3e942498 PB |
8791 | if (atomic_dec_and_test(&rsrc_data->refs)) |
8792 | complete(&rsrc_data->done); | |
2faf852d | 8793 | } |
65e19f54 | 8794 | |
269bbe5f | 8795 | static void io_rsrc_put_work(struct work_struct *work) |
4a38aed2 JA |
8796 | { |
8797 | struct io_ring_ctx *ctx; | |
8798 | struct llist_node *node; | |
8799 | ||
269bbe5f BM |
8800 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
8801 | node = llist_del_all(&ctx->rsrc_put_llist); | |
4a38aed2 JA |
8802 | |
8803 | while (node) { | |
b895c9a6 | 8804 | struct io_rsrc_node *ref_node; |
4a38aed2 JA |
8805 | struct llist_node *next = node->next; |
8806 | ||
b895c9a6 | 8807 | ref_node = llist_entry(node, struct io_rsrc_node, llist); |
269bbe5f | 8808 | __io_rsrc_put_work(ref_node); |
4a38aed2 JA |
8809 | node = next; |
8810 | } | |
8811 | } | |
8812 | ||
6b06314c | 8813 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
792e3582 | 8814 | unsigned nr_args, u64 __user *tags) |
6b06314c JA |
8815 | { |
8816 | __s32 __user *fds = (__s32 __user *) arg; | |
05f3fb3c | 8817 | struct file *file; |
f3baed39 | 8818 | int fd, ret; |
846a4ef2 | 8819 | unsigned i; |
6b06314c | 8820 | |
05f3fb3c | 8821 | if (ctx->file_data) |
6b06314c JA |
8822 | return -EBUSY; |
8823 | if (!nr_args) | |
8824 | return -EINVAL; | |
8825 | if (nr_args > IORING_MAX_FIXED_FILES) | |
8826 | return -EMFILE; | |
3a1b8a4e PB |
8827 | if (nr_args > rlimit(RLIMIT_NOFILE)) |
8828 | return -EMFILE; | |
a7f0ed5a | 8829 | ret = io_rsrc_node_switch_start(ctx); |
f3baed39 PB |
8830 | if (ret) |
8831 | return ret; | |
d878c816 PB |
8832 | ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args, |
8833 | &ctx->file_data); | |
8834 | if (ret) | |
8835 | return ret; | |
6b06314c | 8836 | |
f3baed39 | 8837 | ret = -ENOMEM; |
aeca241b | 8838 | if (!io_alloc_file_tables(&ctx->file_table, nr_args)) |
1ad555c6 | 8839 | goto out_free; |
65e19f54 | 8840 | |
08a45173 | 8841 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
d878c816 | 8842 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
600cf3f8 PB |
8843 | ret = -EFAULT; |
8844 | goto out_fput; | |
8845 | } | |
08a45173 | 8846 | /* allow sparse sets */ |
792e3582 PB |
8847 | if (fd == -1) { |
8848 | ret = -EINVAL; | |
2d091d62 | 8849 | if (unlikely(*io_get_tag_slot(ctx->file_data, i))) |
792e3582 | 8850 | goto out_fput; |
08a45173 | 8851 | continue; |
792e3582 | 8852 | } |
6b06314c | 8853 | |
05f3fb3c | 8854 | file = fget(fd); |
6b06314c | 8855 | ret = -EBADF; |
792e3582 | 8856 | if (unlikely(!file)) |
600cf3f8 | 8857 | goto out_fput; |
05f3fb3c | 8858 | |
6b06314c JA |
8859 | /* |
8860 | * Don't allow io_uring instances to be registered. If UNIX | |
8861 | * isn't enabled, then this causes a reference cycle and this | |
8862 | * instance can never get freed. If UNIX is enabled we'll | |
8863 | * handle it just fine, but there's still no point in allowing | |
8864 | * a ring fd as it doesn't support regular read/write anyway. | |
8865 | */ | |
05f3fb3c JA |
8866 | if (file->f_op == &io_uring_fops) { |
8867 | fput(file); | |
600cf3f8 | 8868 | goto out_fput; |
6b06314c | 8869 | } |
aeca241b | 8870 | io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file); |
6b06314c JA |
8871 | } |
8872 | ||
6b06314c | 8873 | ret = io_sqe_files_scm(ctx); |
05589553 | 8874 | if (ret) { |
08480400 | 8875 | __io_sqe_files_unregister(ctx); |
05589553 XW |
8876 | return ret; |
8877 | } | |
6b06314c | 8878 | |
a7f0ed5a | 8879 | io_rsrc_node_switch(ctx, NULL); |
6b06314c | 8880 | return ret; |
600cf3f8 PB |
8881 | out_fput: |
8882 | for (i = 0; i < ctx->nr_user_files; i++) { | |
8883 | file = io_file_from_index(ctx, i); | |
8884 | if (file) | |
8885 | fput(file); | |
8886 | } | |
042b0d85 | 8887 | io_free_file_tables(&ctx->file_table); |
600cf3f8 | 8888 | ctx->nr_user_files = 0; |
600cf3f8 | 8889 | out_free: |
44b31f2f | 8890 | io_rsrc_data_free(ctx->file_data); |
55cbc256 | 8891 | ctx->file_data = NULL; |
6b06314c JA |
8892 | return ret; |
8893 | } | |
8894 | ||
c3a31e60 JA |
8895 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
8896 | int index) | |
8897 | { | |
8898 | #if defined(CONFIG_UNIX) | |
8899 | struct sock *sock = ctx->ring_sock->sk; | |
8900 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
8901 | struct sk_buff *skb; | |
8902 | ||
8903 | /* | |
8904 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
8905 | * file set. If there's no room, fall back to allocating a new skb | |
8906 | * and filling it in. | |
8907 | */ | |
8908 | spin_lock_irq(&head->lock); | |
8909 | skb = skb_peek(head); | |
8910 | if (skb) { | |
8911 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
8912 | ||
8913 | if (fpl->count < SCM_MAX_FD) { | |
8914 | __skb_unlink(skb, head); | |
8915 | spin_unlock_irq(&head->lock); | |
8916 | fpl->fp[fpl->count] = get_file(file); | |
8917 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
8918 | fpl->count++; | |
8919 | spin_lock_irq(&head->lock); | |
8920 | __skb_queue_head(head, skb); | |
8921 | } else { | |
8922 | skb = NULL; | |
8923 | } | |
8924 | } | |
8925 | spin_unlock_irq(&head->lock); | |
8926 | ||
8927 | if (skb) { | |
8928 | fput(file); | |
8929 | return 0; | |
8930 | } | |
8931 | ||
8932 | return __io_sqe_files_scm(ctx, 1, index); | |
8933 | #else | |
8934 | return 0; | |
8935 | #endif | |
8936 | } | |
8937 | ||
9c7b0ba8 PB |
8938 | static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, |
8939 | struct io_rsrc_node *node, void *rsrc) | |
8940 | { | |
8f0a2480 | 8941 | u64 *tag_slot = io_get_tag_slot(data, idx); |
9c7b0ba8 PB |
8942 | struct io_rsrc_put *prsrc; |
8943 | ||
8944 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); | |
8945 | if (!prsrc) | |
8946 | return -ENOMEM; | |
8947 | ||
8f0a2480 PB |
8948 | prsrc->tag = *tag_slot; |
8949 | *tag_slot = 0; | |
9c7b0ba8 PB |
8950 | prsrc->rsrc = rsrc; |
8951 | list_add(&prsrc->list, &node->rsrc_list); | |
8952 | return 0; | |
8953 | } | |
8954 | ||
b9445598 PB |
8955 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
8956 | unsigned int issue_flags, u32 slot_index) | |
8957 | { | |
8958 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 8959 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
9c7b0ba8 | 8960 | bool needs_switch = false; |
b9445598 PB |
8961 | struct io_fixed_file *file_slot; |
8962 | int ret = -EBADF; | |
8963 | ||
3b44b371 | 8964 | io_ring_submit_lock(ctx, needs_lock); |
b9445598 PB |
8965 | if (file->f_op == &io_uring_fops) |
8966 | goto err; | |
8967 | ret = -ENXIO; | |
8968 | if (!ctx->file_data) | |
8969 | goto err; | |
8970 | ret = -EINVAL; | |
8971 | if (slot_index >= ctx->nr_user_files) | |
8972 | goto err; | |
8973 | ||
8974 | slot_index = array_index_nospec(slot_index, ctx->nr_user_files); | |
8975 | file_slot = io_fixed_file_slot(&ctx->file_table, slot_index); | |
9c7b0ba8 PB |
8976 | |
8977 | if (file_slot->file_ptr) { | |
8978 | struct file *old_file; | |
8979 | ||
8980 | ret = io_rsrc_node_switch_start(ctx); | |
8981 | if (ret) | |
8982 | goto err; | |
8983 | ||
8984 | old_file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
8985 | ret = io_queue_rsrc_removal(ctx->file_data, slot_index, | |
8986 | ctx->rsrc_node, old_file); | |
8987 | if (ret) | |
8988 | goto err; | |
8989 | file_slot->file_ptr = 0; | |
8990 | needs_switch = true; | |
8991 | } | |
b9445598 PB |
8992 | |
8993 | *io_get_tag_slot(ctx->file_data, slot_index) = 0; | |
8994 | io_fixed_file_set(file_slot, file); | |
8995 | ret = io_sqe_file_register(ctx, file, slot_index); | |
8996 | if (ret) { | |
8997 | file_slot->file_ptr = 0; | |
8998 | goto err; | |
8999 | } | |
9000 | ||
9001 | ret = 0; | |
9002 | err: | |
9c7b0ba8 PB |
9003 | if (needs_switch) |
9004 | io_rsrc_node_switch(ctx, ctx->file_data); | |
3b44b371 | 9005 | io_ring_submit_unlock(ctx, needs_lock); |
b9445598 PB |
9006 | if (ret) |
9007 | fput(file); | |
9008 | return ret; | |
9009 | } | |
9010 | ||
7df778be PB |
9011 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags) |
9012 | { | |
9013 | unsigned int offset = req->close.file_slot - 1; | |
9014 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 9015 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
7df778be PB |
9016 | struct io_fixed_file *file_slot; |
9017 | struct file *file; | |
4cdd158b | 9018 | int ret; |
7df778be | 9019 | |
3b44b371 | 9020 | io_ring_submit_lock(ctx, needs_lock); |
7df778be PB |
9021 | ret = -ENXIO; |
9022 | if (unlikely(!ctx->file_data)) | |
9023 | goto out; | |
9024 | ret = -EINVAL; | |
9025 | if (offset >= ctx->nr_user_files) | |
9026 | goto out; | |
9027 | ret = io_rsrc_node_switch_start(ctx); | |
9028 | if (ret) | |
9029 | goto out; | |
9030 | ||
4cdd158b PB |
9031 | offset = array_index_nospec(offset, ctx->nr_user_files); |
9032 | file_slot = io_fixed_file_slot(&ctx->file_table, offset); | |
7df778be PB |
9033 | ret = -EBADF; |
9034 | if (!file_slot->file_ptr) | |
9035 | goto out; | |
9036 | ||
9037 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
9038 | ret = io_queue_rsrc_removal(ctx->file_data, offset, ctx->rsrc_node, file); | |
9039 | if (ret) | |
9040 | goto out; | |
9041 | ||
9042 | file_slot->file_ptr = 0; | |
9043 | io_rsrc_node_switch(ctx, ctx->file_data); | |
9044 | ret = 0; | |
9045 | out: | |
3b44b371 | 9046 | io_ring_submit_unlock(ctx, needs_lock); |
7df778be PB |
9047 | return ret; |
9048 | } | |
9049 | ||
05f3fb3c | 9050 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
c3bdad02 | 9051 | struct io_uring_rsrc_update2 *up, |
05f3fb3c JA |
9052 | unsigned nr_args) |
9053 | { | |
c3bdad02 | 9054 | u64 __user *tags = u64_to_user_ptr(up->tags); |
98f0b3b4 | 9055 | __s32 __user *fds = u64_to_user_ptr(up->data); |
b895c9a6 | 9056 | struct io_rsrc_data *data = ctx->file_data; |
a04b0ac0 PB |
9057 | struct io_fixed_file *file_slot; |
9058 | struct file *file; | |
98f0b3b4 PB |
9059 | int fd, i, err = 0; |
9060 | unsigned int done; | |
05589553 | 9061 | bool needs_switch = false; |
c3a31e60 | 9062 | |
98f0b3b4 PB |
9063 | if (!ctx->file_data) |
9064 | return -ENXIO; | |
9065 | if (up->offset + nr_args > ctx->nr_user_files) | |
c3a31e60 JA |
9066 | return -EINVAL; |
9067 | ||
67973b93 | 9068 | for (done = 0; done < nr_args; done++) { |
c3bdad02 PB |
9069 | u64 tag = 0; |
9070 | ||
9071 | if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) || | |
9072 | copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
c3a31e60 JA |
9073 | err = -EFAULT; |
9074 | break; | |
9075 | } | |
c3bdad02 PB |
9076 | if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) { |
9077 | err = -EINVAL; | |
9078 | break; | |
9079 | } | |
4e0377a1 | 9080 | if (fd == IORING_REGISTER_FILES_SKIP) |
9081 | continue; | |
9082 | ||
67973b93 | 9083 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
aeca241b | 9084 | file_slot = io_fixed_file_slot(&ctx->file_table, i); |
ea64ec02 | 9085 | |
a04b0ac0 PB |
9086 | if (file_slot->file_ptr) { |
9087 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
4cdd158b | 9088 | err = io_queue_rsrc_removal(data, i, ctx->rsrc_node, file); |
a5318d3c HD |
9089 | if (err) |
9090 | break; | |
a04b0ac0 | 9091 | file_slot->file_ptr = 0; |
05589553 | 9092 | needs_switch = true; |
c3a31e60 JA |
9093 | } |
9094 | if (fd != -1) { | |
c3a31e60 JA |
9095 | file = fget(fd); |
9096 | if (!file) { | |
9097 | err = -EBADF; | |
9098 | break; | |
9099 | } | |
9100 | /* | |
9101 | * Don't allow io_uring instances to be registered. If | |
9102 | * UNIX isn't enabled, then this causes a reference | |
9103 | * cycle and this instance can never get freed. If UNIX | |
9104 | * is enabled we'll handle it just fine, but there's | |
9105 | * still no point in allowing a ring fd as it doesn't | |
9106 | * support regular read/write anyway. | |
9107 | */ | |
9108 | if (file->f_op == &io_uring_fops) { | |
9109 | fput(file); | |
9110 | err = -EBADF; | |
9111 | break; | |
9112 | } | |
34bb7718 | 9113 | *io_get_tag_slot(data, i) = tag; |
9a321c98 | 9114 | io_fixed_file_set(file_slot, file); |
c3a31e60 | 9115 | err = io_sqe_file_register(ctx, file, i); |
f3bd9dae | 9116 | if (err) { |
a04b0ac0 | 9117 | file_slot->file_ptr = 0; |
f3bd9dae | 9118 | fput(file); |
c3a31e60 | 9119 | break; |
f3bd9dae | 9120 | } |
c3a31e60 | 9121 | } |
05f3fb3c JA |
9122 | } |
9123 | ||
a7f0ed5a PB |
9124 | if (needs_switch) |
9125 | io_rsrc_node_switch(ctx, data); | |
c3a31e60 JA |
9126 | return done ? done : err; |
9127 | } | |
05589553 | 9128 | |
685fe7fe JA |
9129 | static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx, |
9130 | struct task_struct *task) | |
24369c2e | 9131 | { |
e941894e | 9132 | struct io_wq_hash *hash; |
24369c2e | 9133 | struct io_wq_data data; |
24369c2e | 9134 | unsigned int concurrency; |
24369c2e | 9135 | |
362a9e65 | 9136 | mutex_lock(&ctx->uring_lock); |
e941894e JA |
9137 | hash = ctx->hash_map; |
9138 | if (!hash) { | |
9139 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); | |
362a9e65 YY |
9140 | if (!hash) { |
9141 | mutex_unlock(&ctx->uring_lock); | |
e941894e | 9142 | return ERR_PTR(-ENOMEM); |
362a9e65 | 9143 | } |
e941894e JA |
9144 | refcount_set(&hash->refs, 1); |
9145 | init_waitqueue_head(&hash->wait); | |
9146 | ctx->hash_map = hash; | |
24369c2e | 9147 | } |
362a9e65 | 9148 | mutex_unlock(&ctx->uring_lock); |
24369c2e | 9149 | |
e941894e | 9150 | data.hash = hash; |
685fe7fe | 9151 | data.task = task; |
ebc11b6c | 9152 | data.free_work = io_wq_free_work; |
f5fa38c5 | 9153 | data.do_work = io_wq_submit_work; |
24369c2e | 9154 | |
d25e3a3d JA |
9155 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
9156 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
24369c2e | 9157 | |
5aa75ed5 | 9158 | return io_wq_create(concurrency, &data); |
24369c2e PB |
9159 | } |
9160 | ||
c072481d PB |
9161 | static __cold int io_uring_alloc_task_context(struct task_struct *task, |
9162 | struct io_ring_ctx *ctx) | |
0f212204 JA |
9163 | { |
9164 | struct io_uring_task *tctx; | |
d8a6df10 | 9165 | int ret; |
0f212204 | 9166 | |
09899b19 | 9167 | tctx = kzalloc(sizeof(*tctx), GFP_KERNEL); |
0f212204 JA |
9168 | if (unlikely(!tctx)) |
9169 | return -ENOMEM; | |
9170 | ||
e7a6c00d JA |
9171 | tctx->registered_rings = kcalloc(IO_RINGFD_REG_MAX, |
9172 | sizeof(struct file *), GFP_KERNEL); | |
9173 | if (unlikely(!tctx->registered_rings)) { | |
9174 | kfree(tctx); | |
9175 | return -ENOMEM; | |
9176 | } | |
9177 | ||
d8a6df10 JA |
9178 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
9179 | if (unlikely(ret)) { | |
e7a6c00d | 9180 | kfree(tctx->registered_rings); |
d8a6df10 JA |
9181 | kfree(tctx); |
9182 | return ret; | |
9183 | } | |
9184 | ||
685fe7fe | 9185 | tctx->io_wq = io_init_wq_offload(ctx, task); |
5aa75ed5 JA |
9186 | if (IS_ERR(tctx->io_wq)) { |
9187 | ret = PTR_ERR(tctx->io_wq); | |
9188 | percpu_counter_destroy(&tctx->inflight); | |
e7a6c00d | 9189 | kfree(tctx->registered_rings); |
5aa75ed5 JA |
9190 | kfree(tctx); |
9191 | return ret; | |
9192 | } | |
9193 | ||
0f212204 JA |
9194 | xa_init(&tctx->xa); |
9195 | init_waitqueue_head(&tctx->wait); | |
fdaf083c | 9196 | atomic_set(&tctx->in_idle, 0); |
0f212204 | 9197 | task->io_uring = tctx; |
7cbf1722 JA |
9198 | spin_lock_init(&tctx->task_lock); |
9199 | INIT_WQ_LIST(&tctx->task_list); | |
4813c377 | 9200 | INIT_WQ_LIST(&tctx->prior_task_list); |
7cbf1722 | 9201 | init_task_work(&tctx->task_work, tctx_task_work); |
0f212204 JA |
9202 | return 0; |
9203 | } | |
9204 | ||
9205 | void __io_uring_free(struct task_struct *tsk) | |
9206 | { | |
9207 | struct io_uring_task *tctx = tsk->io_uring; | |
9208 | ||
9209 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); | |
ef8eaa4e | 9210 | WARN_ON_ONCE(tctx->io_wq); |
09899b19 | 9211 | WARN_ON_ONCE(tctx->cached_refs); |
ef8eaa4e | 9212 | |
e7a6c00d | 9213 | kfree(tctx->registered_rings); |
d8a6df10 | 9214 | percpu_counter_destroy(&tctx->inflight); |
0f212204 JA |
9215 | kfree(tctx); |
9216 | tsk->io_uring = NULL; | |
9217 | } | |
9218 | ||
c072481d PB |
9219 | static __cold int io_sq_offload_create(struct io_ring_ctx *ctx, |
9220 | struct io_uring_params *p) | |
2b188cc1 JA |
9221 | { |
9222 | int ret; | |
9223 | ||
d25e3a3d JA |
9224 | /* Retain compatibility with failing for an invalid attach attempt */ |
9225 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == | |
9226 | IORING_SETUP_ATTACH_WQ) { | |
9227 | struct fd f; | |
9228 | ||
9229 | f = fdget(p->wq_fd); | |
9230 | if (!f.file) | |
9231 | return -ENXIO; | |
0cc936f7 JA |
9232 | if (f.file->f_op != &io_uring_fops) { |
9233 | fdput(f); | |
f2a48dd0 | 9234 | return -EINVAL; |
0cc936f7 JA |
9235 | } |
9236 | fdput(f); | |
d25e3a3d | 9237 | } |
6c271ce2 | 9238 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
46fe18b1 | 9239 | struct task_struct *tsk; |
534ca6d6 | 9240 | struct io_sq_data *sqd; |
26984fbf | 9241 | bool attached; |
534ca6d6 | 9242 | |
cdc1404a PM |
9243 | ret = security_uring_sqpoll(); |
9244 | if (ret) | |
9245 | return ret; | |
9246 | ||
26984fbf | 9247 | sqd = io_get_sq_data(p, &attached); |
534ca6d6 JA |
9248 | if (IS_ERR(sqd)) { |
9249 | ret = PTR_ERR(sqd); | |
9250 | goto err; | |
9251 | } | |
69fb2131 | 9252 | |
7c30f36a | 9253 | ctx->sq_creds = get_current_cred(); |
534ca6d6 | 9254 | ctx->sq_data = sqd; |
917257da JA |
9255 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
9256 | if (!ctx->sq_thread_idle) | |
9257 | ctx->sq_thread_idle = HZ; | |
9258 | ||
78d7f6ba | 9259 | io_sq_thread_park(sqd); |
de75a3d3 PB |
9260 | list_add(&ctx->sqd_list, &sqd->ctx_list); |
9261 | io_sqd_update_thread_idle(sqd); | |
26984fbf | 9262 | /* don't attach to a dying SQPOLL thread, would be racy */ |
f2a48dd0 | 9263 | ret = (attached && !sqd->thread) ? -ENXIO : 0; |
78d7f6ba PB |
9264 | io_sq_thread_unpark(sqd); |
9265 | ||
de75a3d3 PB |
9266 | if (ret < 0) |
9267 | goto err; | |
9268 | if (attached) | |
5aa75ed5 | 9269 | return 0; |
aa06165d | 9270 | |
6c271ce2 | 9271 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 9272 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 9273 | |
917257da | 9274 | ret = -EINVAL; |
f2a48dd0 | 9275 | if (cpu >= nr_cpu_ids || !cpu_online(cpu)) |
e8f98f24 | 9276 | goto err_sqpoll; |
37d1e2e3 | 9277 | sqd->sq_cpu = cpu; |
6c271ce2 | 9278 | } else { |
37d1e2e3 | 9279 | sqd->sq_cpu = -1; |
6c271ce2 | 9280 | } |
37d1e2e3 JA |
9281 | |
9282 | sqd->task_pid = current->pid; | |
5c2469e0 | 9283 | sqd->task_tgid = current->tgid; |
46fe18b1 JA |
9284 | tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE); |
9285 | if (IS_ERR(tsk)) { | |
9286 | ret = PTR_ERR(tsk); | |
e8f98f24 | 9287 | goto err_sqpoll; |
6c271ce2 | 9288 | } |
97a73a0f | 9289 | |
46fe18b1 | 9290 | sqd->thread = tsk; |
97a73a0f | 9291 | ret = io_uring_alloc_task_context(tsk, ctx); |
46fe18b1 | 9292 | wake_up_new_task(tsk); |
0f212204 JA |
9293 | if (ret) |
9294 | goto err; | |
6c271ce2 JA |
9295 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
9296 | /* Can't have SQ_AFF without SQPOLL */ | |
9297 | ret = -EINVAL; | |
9298 | goto err; | |
9299 | } | |
9300 | ||
2b188cc1 | 9301 | return 0; |
f2a48dd0 PB |
9302 | err_sqpoll: |
9303 | complete(&ctx->sq_data->exited); | |
2b188cc1 | 9304 | err: |
37d1e2e3 | 9305 | io_sq_thread_finish(ctx); |
2b188cc1 JA |
9306 | return ret; |
9307 | } | |
9308 | ||
a087e2b5 BM |
9309 | static inline void __io_unaccount_mem(struct user_struct *user, |
9310 | unsigned long nr_pages) | |
2b188cc1 JA |
9311 | { |
9312 | atomic_long_sub(nr_pages, &user->locked_vm); | |
9313 | } | |
9314 | ||
a087e2b5 BM |
9315 | static inline int __io_account_mem(struct user_struct *user, |
9316 | unsigned long nr_pages) | |
2b188cc1 JA |
9317 | { |
9318 | unsigned long page_limit, cur_pages, new_pages; | |
9319 | ||
9320 | /* Don't allow more pages than we can safely lock */ | |
9321 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
9322 | ||
9323 | do { | |
9324 | cur_pages = atomic_long_read(&user->locked_vm); | |
9325 | new_pages = cur_pages + nr_pages; | |
9326 | if (new_pages > page_limit) | |
9327 | return -ENOMEM; | |
9328 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
9329 | new_pages) != cur_pages); | |
9330 | ||
9331 | return 0; | |
9332 | } | |
9333 | ||
26bfa89e | 9334 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 9335 | { |
62e398be | 9336 | if (ctx->user) |
a087e2b5 | 9337 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 9338 | |
26bfa89e JA |
9339 | if (ctx->mm_account) |
9340 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
9341 | } |
9342 | ||
26bfa89e | 9343 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 9344 | { |
30975825 BM |
9345 | int ret; |
9346 | ||
62e398be | 9347 | if (ctx->user) { |
30975825 BM |
9348 | ret = __io_account_mem(ctx->user, nr_pages); |
9349 | if (ret) | |
9350 | return ret; | |
9351 | } | |
9352 | ||
26bfa89e JA |
9353 | if (ctx->mm_account) |
9354 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
9355 | |
9356 | return 0; | |
9357 | } | |
9358 | ||
2b188cc1 JA |
9359 | static void io_mem_free(void *ptr) |
9360 | { | |
52e04ef4 MR |
9361 | struct page *page; |
9362 | ||
9363 | if (!ptr) | |
9364 | return; | |
2b188cc1 | 9365 | |
52e04ef4 | 9366 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
9367 | if (put_page_testzero(page)) |
9368 | free_compound_page(page); | |
9369 | } | |
9370 | ||
9371 | static void *io_mem_alloc(size_t size) | |
9372 | { | |
0a3f1e0b | 9373 | gfp_t gfp = GFP_KERNEL_ACCOUNT | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP; |
2b188cc1 | 9374 | |
0a3f1e0b | 9375 | return (void *) __get_free_pages(gfp, get_order(size)); |
2b188cc1 JA |
9376 | } |
9377 | ||
75b28aff HV |
9378 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
9379 | size_t *sq_offset) | |
9380 | { | |
9381 | struct io_rings *rings; | |
9382 | size_t off, sq_array_size; | |
9383 | ||
9384 | off = struct_size(rings, cqes, cq_entries); | |
9385 | if (off == SIZE_MAX) | |
9386 | return SIZE_MAX; | |
9387 | ||
9388 | #ifdef CONFIG_SMP | |
9389 | off = ALIGN(off, SMP_CACHE_BYTES); | |
9390 | if (off == 0) | |
9391 | return SIZE_MAX; | |
9392 | #endif | |
9393 | ||
b36200f5 DV |
9394 | if (sq_offset) |
9395 | *sq_offset = off; | |
9396 | ||
75b28aff HV |
9397 | sq_array_size = array_size(sizeof(u32), sq_entries); |
9398 | if (sq_array_size == SIZE_MAX) | |
9399 | return SIZE_MAX; | |
9400 | ||
9401 | if (check_add_overflow(off, sq_array_size, &off)) | |
9402 | return SIZE_MAX; | |
9403 | ||
75b28aff HV |
9404 | return off; |
9405 | } | |
9406 | ||
41edf1a5 | 9407 | static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot) |
7f61a1e9 | 9408 | { |
41edf1a5 | 9409 | struct io_mapped_ubuf *imu = *slot; |
7f61a1e9 PB |
9410 | unsigned int i; |
9411 | ||
6224843d PB |
9412 | if (imu != ctx->dummy_ubuf) { |
9413 | for (i = 0; i < imu->nr_bvecs; i++) | |
9414 | unpin_user_page(imu->bvec[i].bv_page); | |
9415 | if (imu->acct_pages) | |
9416 | io_unaccount_mem(ctx, imu->acct_pages); | |
9417 | kvfree(imu); | |
9418 | } | |
41edf1a5 | 9419 | *slot = NULL; |
7f61a1e9 PB |
9420 | } |
9421 | ||
bd54b6fe | 9422 | static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
edafccee | 9423 | { |
634d00df PB |
9424 | io_buffer_unmap(ctx, &prsrc->buf); |
9425 | prsrc->buf = NULL; | |
bd54b6fe | 9426 | } |
edafccee | 9427 | |
bd54b6fe BM |
9428 | static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
9429 | { | |
9430 | unsigned int i; | |
edafccee | 9431 | |
7f61a1e9 PB |
9432 | for (i = 0; i < ctx->nr_user_bufs; i++) |
9433 | io_buffer_unmap(ctx, &ctx->user_bufs[i]); | |
edafccee | 9434 | kfree(ctx->user_bufs); |
bb6659cc | 9435 | io_rsrc_data_free(ctx->buf_data); |
edafccee | 9436 | ctx->user_bufs = NULL; |
bd54b6fe | 9437 | ctx->buf_data = NULL; |
edafccee | 9438 | ctx->nr_user_bufs = 0; |
bd54b6fe BM |
9439 | } |
9440 | ||
0a96bbe4 | 9441 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
edafccee | 9442 | { |
bd54b6fe | 9443 | int ret; |
edafccee | 9444 | |
bd54b6fe | 9445 | if (!ctx->buf_data) |
edafccee JA |
9446 | return -ENXIO; |
9447 | ||
bd54b6fe BM |
9448 | ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx); |
9449 | if (!ret) | |
9450 | __io_sqe_buffers_unregister(ctx); | |
9451 | return ret; | |
edafccee JA |
9452 | } |
9453 | ||
9454 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
9455 | void __user *arg, unsigned index) | |
9456 | { | |
9457 | struct iovec __user *src; | |
9458 | ||
9459 | #ifdef CONFIG_COMPAT | |
9460 | if (ctx->compat) { | |
9461 | struct compat_iovec __user *ciovs; | |
9462 | struct compat_iovec ciov; | |
9463 | ||
9464 | ciovs = (struct compat_iovec __user *) arg; | |
9465 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
9466 | return -EFAULT; | |
9467 | ||
d55e5f5b | 9468 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
9469 | dst->iov_len = ciov.iov_len; |
9470 | return 0; | |
9471 | } | |
9472 | #endif | |
9473 | src = (struct iovec __user *) arg; | |
9474 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
9475 | return -EFAULT; | |
9476 | return 0; | |
9477 | } | |
9478 | ||
de293938 JA |
9479 | /* |
9480 | * Not super efficient, but this is just a registration time. And we do cache | |
9481 | * the last compound head, so generally we'll only do a full search if we don't | |
9482 | * match that one. | |
9483 | * | |
9484 | * We check if the given compound head page has already been accounted, to | |
9485 | * avoid double accounting it. This allows us to account the full size of the | |
9486 | * page, not just the constituent pages of a huge page. | |
9487 | */ | |
9488 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, | |
9489 | int nr_pages, struct page *hpage) | |
9490 | { | |
9491 | int i, j; | |
9492 | ||
9493 | /* check current page array */ | |
9494 | for (i = 0; i < nr_pages; i++) { | |
9495 | if (!PageCompound(pages[i])) | |
9496 | continue; | |
9497 | if (compound_head(pages[i]) == hpage) | |
9498 | return true; | |
9499 | } | |
9500 | ||
9501 | /* check previously registered pages */ | |
9502 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
41edf1a5 | 9503 | struct io_mapped_ubuf *imu = ctx->user_bufs[i]; |
de293938 JA |
9504 | |
9505 | for (j = 0; j < imu->nr_bvecs; j++) { | |
9506 | if (!PageCompound(imu->bvec[j].bv_page)) | |
9507 | continue; | |
9508 | if (compound_head(imu->bvec[j].bv_page) == hpage) | |
9509 | return true; | |
9510 | } | |
9511 | } | |
9512 | ||
9513 | return false; | |
9514 | } | |
9515 | ||
9516 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, | |
9517 | int nr_pages, struct io_mapped_ubuf *imu, | |
9518 | struct page **last_hpage) | |
9519 | { | |
9520 | int i, ret; | |
9521 | ||
216e5835 | 9522 | imu->acct_pages = 0; |
de293938 JA |
9523 | for (i = 0; i < nr_pages; i++) { |
9524 | if (!PageCompound(pages[i])) { | |
9525 | imu->acct_pages++; | |
9526 | } else { | |
9527 | struct page *hpage; | |
9528 | ||
9529 | hpage = compound_head(pages[i]); | |
9530 | if (hpage == *last_hpage) | |
9531 | continue; | |
9532 | *last_hpage = hpage; | |
9533 | if (headpage_already_acct(ctx, pages, i, hpage)) | |
9534 | continue; | |
9535 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; | |
9536 | } | |
9537 | } | |
9538 | ||
9539 | if (!imu->acct_pages) | |
9540 | return 0; | |
9541 | ||
26bfa89e | 9542 | ret = io_account_mem(ctx, imu->acct_pages); |
de293938 JA |
9543 | if (ret) |
9544 | imu->acct_pages = 0; | |
9545 | return ret; | |
9546 | } | |
9547 | ||
0a96bbe4 | 9548 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
41edf1a5 | 9549 | struct io_mapped_ubuf **pimu, |
0a96bbe4 | 9550 | struct page **last_hpage) |
edafccee | 9551 | { |
41edf1a5 | 9552 | struct io_mapped_ubuf *imu = NULL; |
edafccee JA |
9553 | struct vm_area_struct **vmas = NULL; |
9554 | struct page **pages = NULL; | |
0a96bbe4 BM |
9555 | unsigned long off, start, end, ubuf; |
9556 | size_t size; | |
9557 | int ret, pret, nr_pages, i; | |
9558 | ||
6224843d PB |
9559 | if (!iov->iov_base) { |
9560 | *pimu = ctx->dummy_ubuf; | |
9561 | return 0; | |
9562 | } | |
9563 | ||
0a96bbe4 BM |
9564 | ubuf = (unsigned long) iov->iov_base; |
9565 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
9566 | start = ubuf >> PAGE_SHIFT; | |
9567 | nr_pages = end - start; | |
9568 | ||
41edf1a5 | 9569 | *pimu = NULL; |
0a96bbe4 BM |
9570 | ret = -ENOMEM; |
9571 | ||
9572 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); | |
9573 | if (!pages) | |
9574 | goto done; | |
9575 | ||
9576 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), | |
9577 | GFP_KERNEL); | |
9578 | if (!vmas) | |
9579 | goto done; | |
edafccee | 9580 | |
41edf1a5 | 9581 | imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL); |
a2b4198c | 9582 | if (!imu) |
0a96bbe4 BM |
9583 | goto done; |
9584 | ||
9585 | ret = 0; | |
9586 | mmap_read_lock(current->mm); | |
9587 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, | |
9588 | pages, vmas); | |
9589 | if (pret == nr_pages) { | |
9590 | /* don't support file backed memory */ | |
9591 | for (i = 0; i < nr_pages; i++) { | |
9592 | struct vm_area_struct *vma = vmas[i]; | |
9593 | ||
40dad765 PB |
9594 | if (vma_is_shmem(vma)) |
9595 | continue; | |
0a96bbe4 BM |
9596 | if (vma->vm_file && |
9597 | !is_file_hugepages(vma->vm_file)) { | |
9598 | ret = -EOPNOTSUPP; | |
9599 | break; | |
9600 | } | |
9601 | } | |
9602 | } else { | |
9603 | ret = pret < 0 ? pret : -EFAULT; | |
9604 | } | |
9605 | mmap_read_unlock(current->mm); | |
9606 | if (ret) { | |
9607 | /* | |
9608 | * if we did partial map, or found file backed vmas, | |
9609 | * release any pages we did get | |
9610 | */ | |
9611 | if (pret > 0) | |
9612 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
9613 | goto done; |
9614 | } | |
9615 | ||
9616 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); | |
9617 | if (ret) { | |
9618 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
9619 | goto done; |
9620 | } | |
9621 | ||
9622 | off = ubuf & ~PAGE_MASK; | |
9623 | size = iov->iov_len; | |
9624 | for (i = 0; i < nr_pages; i++) { | |
9625 | size_t vec_len; | |
9626 | ||
9627 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
9628 | imu->bvec[i].bv_page = pages[i]; | |
9629 | imu->bvec[i].bv_len = vec_len; | |
9630 | imu->bvec[i].bv_offset = off; | |
9631 | off = 0; | |
9632 | size -= vec_len; | |
9633 | } | |
9634 | /* store original address for later verification */ | |
9635 | imu->ubuf = ubuf; | |
4751f53d | 9636 | imu->ubuf_end = ubuf + iov->iov_len; |
0a96bbe4 | 9637 | imu->nr_bvecs = nr_pages; |
41edf1a5 | 9638 | *pimu = imu; |
0a96bbe4 BM |
9639 | ret = 0; |
9640 | done: | |
41edf1a5 PB |
9641 | if (ret) |
9642 | kvfree(imu); | |
0a96bbe4 BM |
9643 | kvfree(pages); |
9644 | kvfree(vmas); | |
9645 | return ret; | |
9646 | } | |
9647 | ||
2b358604 | 9648 | static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args) |
0a96bbe4 | 9649 | { |
87094465 PB |
9650 | ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL); |
9651 | return ctx->user_bufs ? 0 : -ENOMEM; | |
2b358604 | 9652 | } |
edafccee | 9653 | |
2b358604 BM |
9654 | static int io_buffer_validate(struct iovec *iov) |
9655 | { | |
50e96989 PB |
9656 | unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1); |
9657 | ||
2b358604 BM |
9658 | /* |
9659 | * Don't impose further limits on the size and buffer | |
9660 | * constraints here, we'll -EINVAL later when IO is | |
9661 | * submitted if they are wrong. | |
9662 | */ | |
6224843d PB |
9663 | if (!iov->iov_base) |
9664 | return iov->iov_len ? -EFAULT : 0; | |
9665 | if (!iov->iov_len) | |
2b358604 | 9666 | return -EFAULT; |
edafccee | 9667 | |
2b358604 BM |
9668 | /* arbitrary limit, but we need something */ |
9669 | if (iov->iov_len > SZ_1G) | |
9670 | return -EFAULT; | |
edafccee | 9671 | |
50e96989 PB |
9672 | if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp)) |
9673 | return -EOVERFLOW; | |
9674 | ||
2b358604 BM |
9675 | return 0; |
9676 | } | |
edafccee | 9677 | |
2b358604 | 9678 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
634d00df | 9679 | unsigned int nr_args, u64 __user *tags) |
2b358604 | 9680 | { |
bd54b6fe BM |
9681 | struct page *last_hpage = NULL; |
9682 | struct io_rsrc_data *data; | |
2b358604 BM |
9683 | int i, ret; |
9684 | struct iovec iov; | |
edafccee | 9685 | |
87094465 PB |
9686 | if (ctx->user_bufs) |
9687 | return -EBUSY; | |
489809e2 | 9688 | if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS) |
87094465 | 9689 | return -EINVAL; |
bd54b6fe | 9690 | ret = io_rsrc_node_switch_start(ctx); |
2b358604 BM |
9691 | if (ret) |
9692 | return ret; | |
d878c816 PB |
9693 | ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data); |
9694 | if (ret) | |
9695 | return ret; | |
bd54b6fe BM |
9696 | ret = io_buffers_map_alloc(ctx, nr_args); |
9697 | if (ret) { | |
bb6659cc | 9698 | io_rsrc_data_free(data); |
bd54b6fe BM |
9699 | return ret; |
9700 | } | |
edafccee | 9701 | |
87094465 | 9702 | for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) { |
edafccee JA |
9703 | ret = io_copy_iov(ctx, &iov, arg, i); |
9704 | if (ret) | |
0a96bbe4 | 9705 | break; |
2b358604 BM |
9706 | ret = io_buffer_validate(&iov); |
9707 | if (ret) | |
0a96bbe4 | 9708 | break; |
2d091d62 | 9709 | if (!iov.iov_base && *io_get_tag_slot(data, i)) { |
cf3770e7 CIK |
9710 | ret = -EINVAL; |
9711 | break; | |
9712 | } | |
edafccee | 9713 | |
41edf1a5 PB |
9714 | ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i], |
9715 | &last_hpage); | |
0a96bbe4 BM |
9716 | if (ret) |
9717 | break; | |
edafccee | 9718 | } |
0a96bbe4 | 9719 | |
bd54b6fe | 9720 | WARN_ON_ONCE(ctx->buf_data); |
0a96bbe4 | 9721 | |
bd54b6fe BM |
9722 | ctx->buf_data = data; |
9723 | if (ret) | |
9724 | __io_sqe_buffers_unregister(ctx); | |
9725 | else | |
9726 | io_rsrc_node_switch(ctx, NULL); | |
edafccee JA |
9727 | return ret; |
9728 | } | |
9729 | ||
634d00df PB |
9730 | static int __io_sqe_buffers_update(struct io_ring_ctx *ctx, |
9731 | struct io_uring_rsrc_update2 *up, | |
9732 | unsigned int nr_args) | |
9733 | { | |
9734 | u64 __user *tags = u64_to_user_ptr(up->tags); | |
9735 | struct iovec iov, __user *iovs = u64_to_user_ptr(up->data); | |
634d00df PB |
9736 | struct page *last_hpage = NULL; |
9737 | bool needs_switch = false; | |
9738 | __u32 done; | |
9739 | int i, err; | |
9740 | ||
9741 | if (!ctx->buf_data) | |
9742 | return -ENXIO; | |
9743 | if (up->offset + nr_args > ctx->nr_user_bufs) | |
9744 | return -EINVAL; | |
9745 | ||
9746 | for (done = 0; done < nr_args; done++) { | |
0b8c0e7c PB |
9747 | struct io_mapped_ubuf *imu; |
9748 | int offset = up->offset + done; | |
634d00df PB |
9749 | u64 tag = 0; |
9750 | ||
9751 | err = io_copy_iov(ctx, &iov, iovs, done); | |
9752 | if (err) | |
9753 | break; | |
9754 | if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) { | |
9755 | err = -EFAULT; | |
9756 | break; | |
9757 | } | |
0b8c0e7c PB |
9758 | err = io_buffer_validate(&iov); |
9759 | if (err) | |
9760 | break; | |
cf3770e7 CIK |
9761 | if (!iov.iov_base && tag) { |
9762 | err = -EINVAL; | |
9763 | break; | |
9764 | } | |
0b8c0e7c PB |
9765 | err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage); |
9766 | if (err) | |
9767 | break; | |
634d00df | 9768 | |
0b8c0e7c | 9769 | i = array_index_nospec(offset, ctx->nr_user_bufs); |
6224843d | 9770 | if (ctx->user_bufs[i] != ctx->dummy_ubuf) { |
4cdd158b | 9771 | err = io_queue_rsrc_removal(ctx->buf_data, i, |
0b8c0e7c PB |
9772 | ctx->rsrc_node, ctx->user_bufs[i]); |
9773 | if (unlikely(err)) { | |
9774 | io_buffer_unmap(ctx, &imu); | |
634d00df | 9775 | break; |
0b8c0e7c | 9776 | } |
634d00df PB |
9777 | ctx->user_bufs[i] = NULL; |
9778 | needs_switch = true; | |
9779 | } | |
9780 | ||
0b8c0e7c | 9781 | ctx->user_bufs[i] = imu; |
2d091d62 | 9782 | *io_get_tag_slot(ctx->buf_data, offset) = tag; |
634d00df PB |
9783 | } |
9784 | ||
9785 | if (needs_switch) | |
9786 | io_rsrc_node_switch(ctx, ctx->buf_data); | |
9787 | return done ? done : err; | |
9788 | } | |
9789 | ||
c75312dd UA |
9790 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg, |
9791 | unsigned int eventfd_async) | |
9b402849 | 9792 | { |
77bc59b4 | 9793 | struct io_ev_fd *ev_fd; |
9b402849 | 9794 | __s32 __user *fds = arg; |
f0a4e62b | 9795 | int fd; |
9b402849 | 9796 | |
77bc59b4 UA |
9797 | ev_fd = rcu_dereference_protected(ctx->io_ev_fd, |
9798 | lockdep_is_held(&ctx->uring_lock)); | |
9799 | if (ev_fd) | |
9b402849 JA |
9800 | return -EBUSY; |
9801 | ||
9802 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
9803 | return -EFAULT; | |
9804 | ||
77bc59b4 UA |
9805 | ev_fd = kmalloc(sizeof(*ev_fd), GFP_KERNEL); |
9806 | if (!ev_fd) | |
9807 | return -ENOMEM; | |
fe7e3257 | 9808 | |
77bc59b4 UA |
9809 | ev_fd->cq_ev_fd = eventfd_ctx_fdget(fd); |
9810 | if (IS_ERR(ev_fd->cq_ev_fd)) { | |
f0a4e62b | 9811 | int ret = PTR_ERR(ev_fd->cq_ev_fd); |
77bc59b4 | 9812 | kfree(ev_fd); |
9b402849 JA |
9813 | return ret; |
9814 | } | |
c75312dd | 9815 | ev_fd->eventfd_async = eventfd_async; |
9aa8dfde | 9816 | ctx->has_evfd = true; |
77bc59b4 | 9817 | rcu_assign_pointer(ctx->io_ev_fd, ev_fd); |
f0a4e62b | 9818 | return 0; |
77bc59b4 UA |
9819 | } |
9820 | ||
9821 | static void io_eventfd_put(struct rcu_head *rcu) | |
9822 | { | |
9823 | struct io_ev_fd *ev_fd = container_of(rcu, struct io_ev_fd, rcu); | |
9824 | ||
9825 | eventfd_ctx_put(ev_fd->cq_ev_fd); | |
9826 | kfree(ev_fd); | |
9b402849 JA |
9827 | } |
9828 | ||
9829 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
9830 | { | |
77bc59b4 UA |
9831 | struct io_ev_fd *ev_fd; |
9832 | ||
9833 | ev_fd = rcu_dereference_protected(ctx->io_ev_fd, | |
9834 | lockdep_is_held(&ctx->uring_lock)); | |
9835 | if (ev_fd) { | |
9aa8dfde | 9836 | ctx->has_evfd = false; |
77bc59b4 UA |
9837 | rcu_assign_pointer(ctx->io_ev_fd, NULL); |
9838 | call_rcu(&ev_fd->rcu, io_eventfd_put); | |
9b402849 JA |
9839 | return 0; |
9840 | } | |
9841 | ||
9842 | return -ENXIO; | |
9843 | } | |
9844 | ||
5a2e745d JA |
9845 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
9846 | { | |
dbc7d452 JA |
9847 | int i; |
9848 | ||
9849 | for (i = 0; i < (1U << IO_BUFFERS_HASH_BITS); i++) { | |
9850 | struct list_head *list = &ctx->io_buffers[i]; | |
9e15c3a0 | 9851 | |
dbc7d452 JA |
9852 | while (!list_empty(list)) { |
9853 | struct io_buffer_list *bl; | |
9854 | ||
9855 | bl = list_first_entry(list, struct io_buffer_list, list); | |
9856 | __io_remove_buffers(ctx, bl, -1U); | |
9857 | list_del(&bl->list); | |
9858 | kfree(bl); | |
9859 | } | |
9860 | } | |
cc3cec83 JA |
9861 | |
9862 | while (!list_empty(&ctx->io_buffers_pages)) { | |
9863 | struct page *page; | |
9864 | ||
9865 | page = list_first_entry(&ctx->io_buffers_pages, struct page, lru); | |
9866 | list_del_init(&page->lru); | |
9867 | __free_page(page); | |
9868 | } | |
5a2e745d JA |
9869 | } |
9870 | ||
4010fec4 | 9871 | static void io_req_caches_free(struct io_ring_ctx *ctx) |
2b188cc1 | 9872 | { |
cd0ca2e0 | 9873 | struct io_submit_state *state = &ctx->submit_state; |
37f0e767 | 9874 | int nr = 0; |
bf019da7 | 9875 | |
9a4fdbd8 | 9876 | mutex_lock(&ctx->uring_lock); |
cd0ca2e0 | 9877 | io_flush_cached_locked_reqs(ctx, state); |
9a4fdbd8 | 9878 | |
c2b6c6bc PB |
9879 | while (state->free_list.next) { |
9880 | struct io_wq_work_node *node; | |
9881 | struct io_kiocb *req; | |
9a4fdbd8 | 9882 | |
c2b6c6bc PB |
9883 | node = wq_stack_extract(&state->free_list); |
9884 | req = container_of(node, struct io_kiocb, comp_list); | |
9885 | kmem_cache_free(req_cachep, req); | |
37f0e767 | 9886 | nr++; |
c2b6c6bc | 9887 | } |
37f0e767 PB |
9888 | if (nr) |
9889 | percpu_ref_put_many(&ctx->refs, nr); | |
9a4fdbd8 JA |
9890 | mutex_unlock(&ctx->uring_lock); |
9891 | } | |
9892 | ||
43597aac | 9893 | static void io_wait_rsrc_data(struct io_rsrc_data *data) |
2b188cc1 | 9894 | { |
43597aac | 9895 | if (data && !atomic_dec_and_test(&data->refs)) |
bd54b6fe | 9896 | wait_for_completion(&data->done); |
bd54b6fe | 9897 | } |
04fc6c80 | 9898 | |
4d9237e3 JA |
9899 | static void io_flush_apoll_cache(struct io_ring_ctx *ctx) |
9900 | { | |
9901 | struct async_poll *apoll; | |
9902 | ||
9903 | while (!list_empty(&ctx->apoll_cache)) { | |
9904 | apoll = list_first_entry(&ctx->apoll_cache, struct async_poll, | |
9905 | poll.wait.entry); | |
9906 | list_del(&apoll->poll.wait.entry); | |
9907 | kfree(apoll); | |
9908 | } | |
9909 | } | |
9910 | ||
c072481d | 9911 | static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx) |
2b188cc1 | 9912 | { |
37d1e2e3 | 9913 | io_sq_thread_finish(ctx); |
2aede0e4 | 9914 | |
37d1e2e3 | 9915 | if (ctx->mm_account) { |
2aede0e4 JA |
9916 | mmdrop(ctx->mm_account); |
9917 | ctx->mm_account = NULL; | |
30975825 | 9918 | } |
def596e9 | 9919 | |
ab409402 | 9920 | io_rsrc_refs_drop(ctx); |
43597aac PB |
9921 | /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */ |
9922 | io_wait_rsrc_data(ctx->buf_data); | |
9923 | io_wait_rsrc_data(ctx->file_data); | |
9924 | ||
8bad28d8 | 9925 | mutex_lock(&ctx->uring_lock); |
43597aac | 9926 | if (ctx->buf_data) |
bd54b6fe | 9927 | __io_sqe_buffers_unregister(ctx); |
43597aac | 9928 | if (ctx->file_data) |
08480400 | 9929 | __io_sqe_files_unregister(ctx); |
c4ea060e PB |
9930 | if (ctx->rings) |
9931 | __io_cqring_overflow_flush(ctx, true); | |
9b402849 | 9932 | io_eventfd_unregister(ctx); |
4d9237e3 | 9933 | io_flush_apoll_cache(ctx); |
77bc59b4 | 9934 | mutex_unlock(&ctx->uring_lock); |
5a2e745d | 9935 | io_destroy_buffers(ctx); |
07db298a PB |
9936 | if (ctx->sq_creds) |
9937 | put_cred(ctx->sq_creds); | |
def596e9 | 9938 | |
a7f0ed5a PB |
9939 | /* there are no registered resources left, nobody uses it */ |
9940 | if (ctx->rsrc_node) | |
9941 | io_rsrc_node_destroy(ctx->rsrc_node); | |
8dd03afe | 9942 | if (ctx->rsrc_backup_node) |
b895c9a6 | 9943 | io_rsrc_node_destroy(ctx->rsrc_backup_node); |
a7f0ed5a | 9944 | flush_delayed_work(&ctx->rsrc_put_work); |
756ab7c0 | 9945 | flush_delayed_work(&ctx->fallback_work); |
a7f0ed5a PB |
9946 | |
9947 | WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list)); | |
9948 | WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist)); | |
def596e9 | 9949 | |
2b188cc1 | 9950 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
9951 | if (ctx->ring_sock) { |
9952 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 9953 | sock_release(ctx->ring_sock); |
355e8d26 | 9954 | } |
2b188cc1 | 9955 | #endif |
ef9dd637 | 9956 | WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list)); |
2b188cc1 | 9957 | |
75b28aff | 9958 | io_mem_free(ctx->rings); |
2b188cc1 | 9959 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
9960 | |
9961 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 9962 | free_uid(ctx->user); |
4010fec4 | 9963 | io_req_caches_free(ctx); |
e941894e JA |
9964 | if (ctx->hash_map) |
9965 | io_wq_put_hash(ctx->hash_map); | |
78076bb6 | 9966 | kfree(ctx->cancel_hash); |
6224843d | 9967 | kfree(ctx->dummy_ubuf); |
dbc7d452 | 9968 | kfree(ctx->io_buffers); |
2b188cc1 JA |
9969 | kfree(ctx); |
9970 | } | |
9971 | ||
9972 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
9973 | { | |
9974 | struct io_ring_ctx *ctx = file->private_data; | |
9975 | __poll_t mask = 0; | |
9976 | ||
d60aa65b | 9977 | poll_wait(file, &ctx->cq_wait, wait); |
4f7067c3 SB |
9978 | /* |
9979 | * synchronizes with barrier from wq_has_sleeper call in | |
9980 | * io_commit_cqring | |
9981 | */ | |
2b188cc1 | 9982 | smp_rmb(); |
90554200 | 9983 | if (!io_sqring_full(ctx)) |
2b188cc1 | 9984 | mask |= EPOLLOUT | EPOLLWRNORM; |
ed670c3f HX |
9985 | |
9986 | /* | |
9987 | * Don't flush cqring overflow list here, just do a simple check. | |
9988 | * Otherwise there could possible be ABBA deadlock: | |
9989 | * CPU0 CPU1 | |
9990 | * ---- ---- | |
9991 | * lock(&ctx->uring_lock); | |
9992 | * lock(&ep->mtx); | |
9993 | * lock(&ctx->uring_lock); | |
9994 | * lock(&ep->mtx); | |
9995 | * | |
9996 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this | |
9997 | * pushs them to do the flush. | |
9998 | */ | |
5ed7a37d | 9999 | if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow)) |
2b188cc1 JA |
10000 | mask |= EPOLLIN | EPOLLRDNORM; |
10001 | ||
10002 | return mask; | |
10003 | } | |
10004 | ||
0bead8cd | 10005 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
071698e1 | 10006 | { |
4379bf8b | 10007 | const struct cred *creds; |
071698e1 | 10008 | |
61cf9370 | 10009 | creds = xa_erase(&ctx->personalities, id); |
4379bf8b JA |
10010 | if (creds) { |
10011 | put_cred(creds); | |
0bead8cd | 10012 | return 0; |
1e6fa521 | 10013 | } |
0bead8cd YD |
10014 | |
10015 | return -EINVAL; | |
10016 | } | |
10017 | ||
d56d938b PB |
10018 | struct io_tctx_exit { |
10019 | struct callback_head task_work; | |
10020 | struct completion completion; | |
baf186c4 | 10021 | struct io_ring_ctx *ctx; |
d56d938b PB |
10022 | }; |
10023 | ||
c072481d | 10024 | static __cold void io_tctx_exit_cb(struct callback_head *cb) |
d56d938b PB |
10025 | { |
10026 | struct io_uring_task *tctx = current->io_uring; | |
10027 | struct io_tctx_exit *work; | |
10028 | ||
10029 | work = container_of(cb, struct io_tctx_exit, task_work); | |
10030 | /* | |
10031 | * When @in_idle, we're in cancellation and it's racy to remove the | |
10032 | * node. It'll be removed by the end of cancellation, just ignore it. | |
10033 | */ | |
10034 | if (!atomic_read(&tctx->in_idle)) | |
eef51daa | 10035 | io_uring_del_tctx_node((unsigned long)work->ctx); |
d56d938b PB |
10036 | complete(&work->completion); |
10037 | } | |
10038 | ||
c072481d | 10039 | static __cold bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
28090c13 PB |
10040 | { |
10041 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
10042 | ||
10043 | return req->ctx == data; | |
10044 | } | |
10045 | ||
c072481d | 10046 | static __cold void io_ring_exit_work(struct work_struct *work) |
85faa7b8 | 10047 | { |
d56d938b | 10048 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work); |
b5bb3a24 | 10049 | unsigned long timeout = jiffies + HZ * 60 * 5; |
58d3be2c | 10050 | unsigned long interval = HZ / 20; |
d56d938b PB |
10051 | struct io_tctx_exit exit; |
10052 | struct io_tctx_node *node; | |
10053 | int ret; | |
85faa7b8 | 10054 | |
56952e91 JA |
10055 | /* |
10056 | * If we're doing polled IO and end up having requests being | |
10057 | * submitted async (out-of-line), then completions can come in while | |
10058 | * we're waiting for refs to drop. We need to reap these manually, | |
10059 | * as nobody else will be looking for them. | |
10060 | */ | |
b2edc0a7 | 10061 | do { |
3dd0c97a | 10062 | io_uring_try_cancel_requests(ctx, NULL, true); |
28090c13 PB |
10063 | if (ctx->sq_data) { |
10064 | struct io_sq_data *sqd = ctx->sq_data; | |
10065 | struct task_struct *tsk; | |
10066 | ||
10067 | io_sq_thread_park(sqd); | |
10068 | tsk = sqd->thread; | |
10069 | if (tsk && tsk->io_uring && tsk->io_uring->io_wq) | |
10070 | io_wq_cancel_cb(tsk->io_uring->io_wq, | |
10071 | io_cancel_ctx_cb, ctx, true); | |
10072 | io_sq_thread_unpark(sqd); | |
10073 | } | |
b5bb3a24 | 10074 | |
37f0e767 PB |
10075 | io_req_caches_free(ctx); |
10076 | ||
58d3be2c PB |
10077 | if (WARN_ON_ONCE(time_after(jiffies, timeout))) { |
10078 | /* there is little hope left, don't run it too often */ | |
10079 | interval = HZ * 60; | |
10080 | } | |
10081 | } while (!wait_for_completion_timeout(&ctx->ref_comp, interval)); | |
d56d938b | 10082 | |
7f00651a PB |
10083 | init_completion(&exit.completion); |
10084 | init_task_work(&exit.task_work, io_tctx_exit_cb); | |
10085 | exit.ctx = ctx; | |
89b5066e PB |
10086 | /* |
10087 | * Some may use context even when all refs and requests have been put, | |
10088 | * and they are free to do so while still holding uring_lock or | |
5b0a6acc | 10089 | * completion_lock, see io_req_task_submit(). Apart from other work, |
89b5066e PB |
10090 | * this lock/unlock section also waits them to finish. |
10091 | */ | |
d56d938b PB |
10092 | mutex_lock(&ctx->uring_lock); |
10093 | while (!list_empty(&ctx->tctx_list)) { | |
b5bb3a24 PB |
10094 | WARN_ON_ONCE(time_after(jiffies, timeout)); |
10095 | ||
d56d938b PB |
10096 | node = list_first_entry(&ctx->tctx_list, struct io_tctx_node, |
10097 | ctx_node); | |
7f00651a PB |
10098 | /* don't spin on a single task if cancellation failed */ |
10099 | list_rotate_left(&ctx->tctx_list); | |
d56d938b PB |
10100 | ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL); |
10101 | if (WARN_ON_ONCE(ret)) | |
10102 | continue; | |
d56d938b PB |
10103 | |
10104 | mutex_unlock(&ctx->uring_lock); | |
10105 | wait_for_completion(&exit.completion); | |
d56d938b PB |
10106 | mutex_lock(&ctx->uring_lock); |
10107 | } | |
10108 | mutex_unlock(&ctx->uring_lock); | |
79ebeaee JA |
10109 | spin_lock(&ctx->completion_lock); |
10110 | spin_unlock(&ctx->completion_lock); | |
d56d938b | 10111 | |
85faa7b8 JA |
10112 | io_ring_ctx_free(ctx); |
10113 | } | |
10114 | ||
80c4cbdb | 10115 | /* Returns true if we found and killed one or more timeouts */ |
c072481d PB |
10116 | static __cold bool io_kill_timeouts(struct io_ring_ctx *ctx, |
10117 | struct task_struct *tsk, bool cancel_all) | |
80c4cbdb PB |
10118 | { |
10119 | struct io_kiocb *req, *tmp; | |
10120 | int canceled = 0; | |
10121 | ||
79ebeaee JA |
10122 | spin_lock(&ctx->completion_lock); |
10123 | spin_lock_irq(&ctx->timeout_lock); | |
80c4cbdb | 10124 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
3dd0c97a | 10125 | if (io_match_task(req, tsk, cancel_all)) { |
80c4cbdb PB |
10126 | io_kill_timeout(req, -ECANCELED); |
10127 | canceled++; | |
10128 | } | |
10129 | } | |
79ebeaee | 10130 | spin_unlock_irq(&ctx->timeout_lock); |
51520426 PB |
10131 | if (canceled != 0) |
10132 | io_commit_cqring(ctx); | |
79ebeaee | 10133 | spin_unlock(&ctx->completion_lock); |
80c4cbdb PB |
10134 | if (canceled != 0) |
10135 | io_cqring_ev_posted(ctx); | |
10136 | return canceled != 0; | |
10137 | } | |
10138 | ||
c072481d | 10139 | static __cold void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
2b188cc1 | 10140 | { |
61cf9370 MWO |
10141 | unsigned long index; |
10142 | struct creds *creds; | |
10143 | ||
2b188cc1 JA |
10144 | mutex_lock(&ctx->uring_lock); |
10145 | percpu_ref_kill(&ctx->refs); | |
634578f8 | 10146 | if (ctx->rings) |
6c2450ae | 10147 | __io_cqring_overflow_flush(ctx, true); |
61cf9370 MWO |
10148 | xa_for_each(&ctx->personalities, index, creds) |
10149 | io_unregister_personality(ctx, index); | |
2b188cc1 JA |
10150 | mutex_unlock(&ctx->uring_lock); |
10151 | ||
3dd0c97a PB |
10152 | io_kill_timeouts(ctx, NULL, true); |
10153 | io_poll_remove_all(ctx, NULL, true); | |
561fb04a | 10154 | |
15dff286 | 10155 | /* if we failed setting up the ctx, we might not have any rings */ |
b2edc0a7 | 10156 | io_iopoll_try_reap_events(ctx); |
309fc03a | 10157 | |
85faa7b8 | 10158 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
10159 | /* |
10160 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
10161 | * if we're exiting a ton of rings at the same time. It just adds | |
10162 | * noise and overhead, there's no discernable change in runtime | |
10163 | * over using system_wq. | |
10164 | */ | |
10165 | queue_work(system_unbound_wq, &ctx->exit_work); | |
2b188cc1 JA |
10166 | } |
10167 | ||
10168 | static int io_uring_release(struct inode *inode, struct file *file) | |
10169 | { | |
10170 | struct io_ring_ctx *ctx = file->private_data; | |
10171 | ||
10172 | file->private_data = NULL; | |
10173 | io_ring_ctx_wait_and_kill(ctx); | |
10174 | return 0; | |
10175 | } | |
10176 | ||
f6edbabb PB |
10177 | struct io_task_cancel { |
10178 | struct task_struct *task; | |
3dd0c97a | 10179 | bool all; |
f6edbabb | 10180 | }; |
f254ac04 | 10181 | |
f6edbabb | 10182 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
b711d4ea | 10183 | { |
9a472ef7 | 10184 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
f6edbabb | 10185 | struct io_task_cancel *cancel = data; |
9a472ef7 | 10186 | |
6af3f48b | 10187 | return io_match_task_safe(req, cancel->task, cancel->all); |
b711d4ea JA |
10188 | } |
10189 | ||
c072481d PB |
10190 | static __cold bool io_cancel_defer_files(struct io_ring_ctx *ctx, |
10191 | struct task_struct *task, | |
10192 | bool cancel_all) | |
b7ddce3c | 10193 | { |
e1915f76 | 10194 | struct io_defer_entry *de; |
b7ddce3c PB |
10195 | LIST_HEAD(list); |
10196 | ||
79ebeaee | 10197 | spin_lock(&ctx->completion_lock); |
b7ddce3c | 10198 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
6af3f48b | 10199 | if (io_match_task_safe(de->req, task, cancel_all)) { |
b7ddce3c PB |
10200 | list_cut_position(&list, &ctx->defer_list, &de->list); |
10201 | break; | |
10202 | } | |
10203 | } | |
79ebeaee | 10204 | spin_unlock(&ctx->completion_lock); |
e1915f76 PB |
10205 | if (list_empty(&list)) |
10206 | return false; | |
b7ddce3c PB |
10207 | |
10208 | while (!list_empty(&list)) { | |
10209 | de = list_first_entry(&list, struct io_defer_entry, list); | |
10210 | list_del_init(&de->list); | |
f41db273 | 10211 | io_req_complete_failed(de->req, -ECANCELED); |
b7ddce3c PB |
10212 | kfree(de); |
10213 | } | |
e1915f76 | 10214 | return true; |
b7ddce3c PB |
10215 | } |
10216 | ||
c072481d | 10217 | static __cold bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx) |
1b00764f PB |
10218 | { |
10219 | struct io_tctx_node *node; | |
10220 | enum io_wq_cancel cret; | |
10221 | bool ret = false; | |
10222 | ||
10223 | mutex_lock(&ctx->uring_lock); | |
10224 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
10225 | struct io_uring_task *tctx = node->task->io_uring; | |
10226 | ||
10227 | /* | |
10228 | * io_wq will stay alive while we hold uring_lock, because it's | |
10229 | * killed after ctx nodes, which requires to take the lock. | |
10230 | */ | |
10231 | if (!tctx || !tctx->io_wq) | |
10232 | continue; | |
10233 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true); | |
10234 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
10235 | } | |
10236 | mutex_unlock(&ctx->uring_lock); | |
10237 | ||
10238 | return ret; | |
10239 | } | |
10240 | ||
c072481d PB |
10241 | static __cold void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
10242 | struct task_struct *task, | |
10243 | bool cancel_all) | |
9936c7c2 | 10244 | { |
3dd0c97a | 10245 | struct io_task_cancel cancel = { .task = task, .all = cancel_all, }; |
1b00764f | 10246 | struct io_uring_task *tctx = task ? task->io_uring : NULL; |
9936c7c2 PB |
10247 | |
10248 | while (1) { | |
10249 | enum io_wq_cancel cret; | |
10250 | bool ret = false; | |
10251 | ||
1b00764f PB |
10252 | if (!task) { |
10253 | ret |= io_uring_try_cancel_iowq(ctx); | |
10254 | } else if (tctx && tctx->io_wq) { | |
10255 | /* | |
10256 | * Cancels requests of all rings, not only @ctx, but | |
10257 | * it's fine as the task is in exit/exec. | |
10258 | */ | |
5aa75ed5 | 10259 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, |
9936c7c2 PB |
10260 | &cancel, true); |
10261 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
10262 | } | |
10263 | ||
10264 | /* SQPOLL thread does its own polling */ | |
3dd0c97a | 10265 | if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) || |
d052d1d6 | 10266 | (ctx->sq_data && ctx->sq_data->thread == current)) { |
5eef4e87 | 10267 | while (!wq_list_empty(&ctx->iopoll_list)) { |
9936c7c2 PB |
10268 | io_iopoll_try_reap_events(ctx); |
10269 | ret = true; | |
10270 | } | |
10271 | } | |
10272 | ||
3dd0c97a PB |
10273 | ret |= io_cancel_defer_files(ctx, task, cancel_all); |
10274 | ret |= io_poll_remove_all(ctx, task, cancel_all); | |
10275 | ret |= io_kill_timeouts(ctx, task, cancel_all); | |
e5dc480d PB |
10276 | if (task) |
10277 | ret |= io_run_task_work(); | |
9936c7c2 PB |
10278 | if (!ret) |
10279 | break; | |
10280 | cond_resched(); | |
10281 | } | |
10282 | } | |
10283 | ||
eef51daa | 10284 | static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
0f212204 | 10285 | { |
236434c3 | 10286 | struct io_uring_task *tctx = current->io_uring; |
13bf43f5 | 10287 | struct io_tctx_node *node; |
a528b04e | 10288 | int ret; |
236434c3 MWO |
10289 | |
10290 | if (unlikely(!tctx)) { | |
5aa75ed5 | 10291 | ret = io_uring_alloc_task_context(current, ctx); |
0f212204 JA |
10292 | if (unlikely(ret)) |
10293 | return ret; | |
e139a1ec | 10294 | |
236434c3 | 10295 | tctx = current->io_uring; |
e139a1ec PB |
10296 | if (ctx->iowq_limits_set) { |
10297 | unsigned int limits[2] = { ctx->iowq_limits[0], | |
10298 | ctx->iowq_limits[1], }; | |
10299 | ||
10300 | ret = io_wq_max_workers(tctx->io_wq, limits); | |
10301 | if (ret) | |
10302 | return ret; | |
10303 | } | |
0f212204 | 10304 | } |
cf27f3b1 PB |
10305 | if (!xa_load(&tctx->xa, (unsigned long)ctx)) { |
10306 | node = kmalloc(sizeof(*node), GFP_KERNEL); | |
10307 | if (!node) | |
10308 | return -ENOMEM; | |
10309 | node->ctx = ctx; | |
10310 | node->task = current; | |
13bf43f5 | 10311 | |
cf27f3b1 PB |
10312 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx, |
10313 | node, GFP_KERNEL)); | |
10314 | if (ret) { | |
10315 | kfree(node); | |
10316 | return ret; | |
0f212204 | 10317 | } |
cf27f3b1 PB |
10318 | |
10319 | mutex_lock(&ctx->uring_lock); | |
10320 | list_add(&node->ctx_node, &ctx->tctx_list); | |
10321 | mutex_unlock(&ctx->uring_lock); | |
0f212204 | 10322 | } |
cf27f3b1 | 10323 | tctx->last = ctx; |
0f212204 JA |
10324 | return 0; |
10325 | } | |
10326 | ||
cf27f3b1 PB |
10327 | /* |
10328 | * Note that this task has used io_uring. We use it for cancelation purposes. | |
10329 | */ | |
eef51daa | 10330 | static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
cf27f3b1 PB |
10331 | { |
10332 | struct io_uring_task *tctx = current->io_uring; | |
10333 | ||
10334 | if (likely(tctx && tctx->last == ctx)) | |
10335 | return 0; | |
eef51daa | 10336 | return __io_uring_add_tctx_node(ctx); |
cf27f3b1 PB |
10337 | } |
10338 | ||
0f212204 JA |
10339 | /* |
10340 | * Remove this io_uring_file -> task mapping. | |
10341 | */ | |
c072481d | 10342 | static __cold void io_uring_del_tctx_node(unsigned long index) |
0f212204 JA |
10343 | { |
10344 | struct io_uring_task *tctx = current->io_uring; | |
13bf43f5 | 10345 | struct io_tctx_node *node; |
2941267b | 10346 | |
eebd2e37 PB |
10347 | if (!tctx) |
10348 | return; | |
13bf43f5 PB |
10349 | node = xa_erase(&tctx->xa, index); |
10350 | if (!node) | |
2941267b | 10351 | return; |
0f212204 | 10352 | |
13bf43f5 PB |
10353 | WARN_ON_ONCE(current != node->task); |
10354 | WARN_ON_ONCE(list_empty(&node->ctx_node)); | |
10355 | ||
10356 | mutex_lock(&node->ctx->uring_lock); | |
10357 | list_del(&node->ctx_node); | |
10358 | mutex_unlock(&node->ctx->uring_lock); | |
10359 | ||
baf186c4 | 10360 | if (tctx->last == node->ctx) |
0f212204 | 10361 | tctx->last = NULL; |
13bf43f5 | 10362 | kfree(node); |
0f212204 JA |
10363 | } |
10364 | ||
c072481d | 10365 | static __cold void io_uring_clean_tctx(struct io_uring_task *tctx) |
de7f1d9e | 10366 | { |
ba5ef6dc | 10367 | struct io_wq *wq = tctx->io_wq; |
13bf43f5 | 10368 | struct io_tctx_node *node; |
de7f1d9e PB |
10369 | unsigned long index; |
10370 | ||
8bab4c09 | 10371 | xa_for_each(&tctx->xa, index, node) { |
eef51daa | 10372 | io_uring_del_tctx_node(index); |
8bab4c09 JA |
10373 | cond_resched(); |
10374 | } | |
b16ef427 ME |
10375 | if (wq) { |
10376 | /* | |
f6f9b278 | 10377 | * Must be after io_uring_del_tctx_node() (removes nodes under |
b16ef427 ME |
10378 | * uring_lock) to avoid race with io_uring_try_cancel_iowq(). |
10379 | */ | |
ba5ef6dc | 10380 | io_wq_put_and_exit(wq); |
dadebc35 | 10381 | tctx->io_wq = NULL; |
b16ef427 | 10382 | } |
de7f1d9e PB |
10383 | } |
10384 | ||
3f48cf18 | 10385 | static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked) |
521d6a73 | 10386 | { |
3f48cf18 | 10387 | if (tracked) |
d5361233 | 10388 | return 0; |
521d6a73 PB |
10389 | return percpu_counter_sum(&tctx->inflight); |
10390 | } | |
10391 | ||
78cc687b PB |
10392 | /* |
10393 | * Find any io_uring ctx that this task has registered or done IO on, and cancel | |
78a78060 | 10394 | * requests. @sqd should be not-null IFF it's an SQPOLL thread cancellation. |
78cc687b | 10395 | */ |
c072481d PB |
10396 | static __cold void io_uring_cancel_generic(bool cancel_all, |
10397 | struct io_sq_data *sqd) | |
0e9ddb39 | 10398 | { |
521d6a73 | 10399 | struct io_uring_task *tctx = current->io_uring; |
734551df | 10400 | struct io_ring_ctx *ctx; |
0e9ddb39 PB |
10401 | s64 inflight; |
10402 | DEFINE_WAIT(wait); | |
fdaf083c | 10403 | |
78cc687b PB |
10404 | WARN_ON_ONCE(sqd && sqd->thread != current); |
10405 | ||
6d042ffb PO |
10406 | if (!current->io_uring) |
10407 | return; | |
17a91051 PB |
10408 | if (tctx->io_wq) |
10409 | io_wq_exit_start(tctx->io_wq); | |
10410 | ||
0e9ddb39 PB |
10411 | atomic_inc(&tctx->in_idle); |
10412 | do { | |
e9dbe221 | 10413 | io_uring_drop_tctx_refs(current); |
0e9ddb39 | 10414 | /* read completions before cancelations */ |
78cc687b | 10415 | inflight = tctx_inflight(tctx, !cancel_all); |
0e9ddb39 PB |
10416 | if (!inflight) |
10417 | break; | |
fdaf083c | 10418 | |
78cc687b PB |
10419 | if (!sqd) { |
10420 | struct io_tctx_node *node; | |
10421 | unsigned long index; | |
0f212204 | 10422 | |
78cc687b PB |
10423 | xa_for_each(&tctx->xa, index, node) { |
10424 | /* sqpoll task will cancel all its requests */ | |
10425 | if (node->ctx->sq_data) | |
10426 | continue; | |
10427 | io_uring_try_cancel_requests(node->ctx, current, | |
10428 | cancel_all); | |
10429 | } | |
10430 | } else { | |
10431 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) | |
10432 | io_uring_try_cancel_requests(ctx, current, | |
10433 | cancel_all); | |
10434 | } | |
17a91051 | 10435 | |
78a78060 JA |
10436 | prepare_to_wait(&tctx->wait, &wait, TASK_INTERRUPTIBLE); |
10437 | io_run_task_work(); | |
e9dbe221 | 10438 | io_uring_drop_tctx_refs(current); |
78a78060 | 10439 | |
0f212204 | 10440 | /* |
a1bb3cd5 PB |
10441 | * If we've seen completions, retry without waiting. This |
10442 | * avoids a race where a completion comes in before we did | |
10443 | * prepare_to_wait(). | |
0f212204 | 10444 | */ |
3dd0c97a | 10445 | if (inflight == tctx_inflight(tctx, !cancel_all)) |
a1bb3cd5 | 10446 | schedule(); |
f57555ed | 10447 | finish_wait(&tctx->wait, &wait); |
d8a6df10 | 10448 | } while (1); |
de7f1d9e | 10449 | |
8452d4a6 | 10450 | io_uring_clean_tctx(tctx); |
3dd0c97a | 10451 | if (cancel_all) { |
3cc7fdb9 PB |
10452 | /* |
10453 | * We shouldn't run task_works after cancel, so just leave | |
10454 | * ->in_idle set for normal exit. | |
10455 | */ | |
10456 | atomic_dec(&tctx->in_idle); | |
3f48cf18 PB |
10457 | /* for exec all current's requests should be gone, kill tctx */ |
10458 | __io_uring_free(current); | |
10459 | } | |
44e728b8 PB |
10460 | } |
10461 | ||
f552a27a | 10462 | void __io_uring_cancel(bool cancel_all) |
78cc687b | 10463 | { |
f552a27a | 10464 | io_uring_cancel_generic(cancel_all, NULL); |
78cc687b PB |
10465 | } |
10466 | ||
e7a6c00d JA |
10467 | void io_uring_unreg_ringfd(void) |
10468 | { | |
10469 | struct io_uring_task *tctx = current->io_uring; | |
10470 | int i; | |
10471 | ||
10472 | for (i = 0; i < IO_RINGFD_REG_MAX; i++) { | |
10473 | if (tctx->registered_rings[i]) { | |
10474 | fput(tctx->registered_rings[i]); | |
10475 | tctx->registered_rings[i] = NULL; | |
10476 | } | |
10477 | } | |
10478 | } | |
10479 | ||
10480 | static int io_ring_add_registered_fd(struct io_uring_task *tctx, int fd, | |
10481 | int start, int end) | |
10482 | { | |
10483 | struct file *file; | |
10484 | int offset; | |
10485 | ||
10486 | for (offset = start; offset < end; offset++) { | |
10487 | offset = array_index_nospec(offset, IO_RINGFD_REG_MAX); | |
10488 | if (tctx->registered_rings[offset]) | |
10489 | continue; | |
10490 | ||
10491 | file = fget(fd); | |
10492 | if (!file) { | |
10493 | return -EBADF; | |
10494 | } else if (file->f_op != &io_uring_fops) { | |
10495 | fput(file); | |
10496 | return -EOPNOTSUPP; | |
10497 | } | |
10498 | tctx->registered_rings[offset] = file; | |
10499 | return offset; | |
10500 | } | |
10501 | ||
10502 | return -EBUSY; | |
10503 | } | |
10504 | ||
10505 | /* | |
10506 | * Register a ring fd to avoid fdget/fdput for each io_uring_enter() | |
10507 | * invocation. User passes in an array of struct io_uring_rsrc_update | |
10508 | * with ->data set to the ring_fd, and ->offset given for the desired | |
10509 | * index. If no index is desired, application may set ->offset == -1U | |
10510 | * and we'll find an available index. Returns number of entries | |
10511 | * successfully processed, or < 0 on error if none were processed. | |
10512 | */ | |
10513 | static int io_ringfd_register(struct io_ring_ctx *ctx, void __user *__arg, | |
10514 | unsigned nr_args) | |
10515 | { | |
10516 | struct io_uring_rsrc_update __user *arg = __arg; | |
10517 | struct io_uring_rsrc_update reg; | |
10518 | struct io_uring_task *tctx; | |
10519 | int ret, i; | |
10520 | ||
10521 | if (!nr_args || nr_args > IO_RINGFD_REG_MAX) | |
10522 | return -EINVAL; | |
10523 | ||
10524 | mutex_unlock(&ctx->uring_lock); | |
10525 | ret = io_uring_add_tctx_node(ctx); | |
10526 | mutex_lock(&ctx->uring_lock); | |
10527 | if (ret) | |
10528 | return ret; | |
10529 | ||
10530 | tctx = current->io_uring; | |
10531 | for (i = 0; i < nr_args; i++) { | |
10532 | int start, end; | |
10533 | ||
10534 | if (copy_from_user(®, &arg[i], sizeof(reg))) { | |
10535 | ret = -EFAULT; | |
10536 | break; | |
10537 | } | |
10538 | ||
6fb53cf8 DY |
10539 | if (reg.resv) { |
10540 | ret = -EINVAL; | |
10541 | break; | |
10542 | } | |
10543 | ||
e7a6c00d JA |
10544 | if (reg.offset == -1U) { |
10545 | start = 0; | |
10546 | end = IO_RINGFD_REG_MAX; | |
10547 | } else { | |
10548 | if (reg.offset >= IO_RINGFD_REG_MAX) { | |
10549 | ret = -EINVAL; | |
10550 | break; | |
10551 | } | |
10552 | start = reg.offset; | |
10553 | end = start + 1; | |
10554 | } | |
10555 | ||
10556 | ret = io_ring_add_registered_fd(tctx, reg.data, start, end); | |
10557 | if (ret < 0) | |
10558 | break; | |
10559 | ||
10560 | reg.offset = ret; | |
10561 | if (copy_to_user(&arg[i], ®, sizeof(reg))) { | |
10562 | fput(tctx->registered_rings[reg.offset]); | |
10563 | tctx->registered_rings[reg.offset] = NULL; | |
10564 | ret = -EFAULT; | |
10565 | break; | |
10566 | } | |
10567 | } | |
10568 | ||
10569 | return i ? i : ret; | |
10570 | } | |
10571 | ||
10572 | static int io_ringfd_unregister(struct io_ring_ctx *ctx, void __user *__arg, | |
10573 | unsigned nr_args) | |
10574 | { | |
10575 | struct io_uring_rsrc_update __user *arg = __arg; | |
10576 | struct io_uring_task *tctx = current->io_uring; | |
10577 | struct io_uring_rsrc_update reg; | |
10578 | int ret = 0, i; | |
10579 | ||
10580 | if (!nr_args || nr_args > IO_RINGFD_REG_MAX) | |
10581 | return -EINVAL; | |
10582 | if (!tctx) | |
10583 | return 0; | |
10584 | ||
10585 | for (i = 0; i < nr_args; i++) { | |
10586 | if (copy_from_user(®, &arg[i], sizeof(reg))) { | |
10587 | ret = -EFAULT; | |
10588 | break; | |
10589 | } | |
6fb53cf8 | 10590 | if (reg.resv || reg.offset >= IO_RINGFD_REG_MAX) { |
e7a6c00d JA |
10591 | ret = -EINVAL; |
10592 | break; | |
10593 | } | |
10594 | ||
10595 | reg.offset = array_index_nospec(reg.offset, IO_RINGFD_REG_MAX); | |
10596 | if (tctx->registered_rings[reg.offset]) { | |
10597 | fput(tctx->registered_rings[reg.offset]); | |
10598 | tctx->registered_rings[reg.offset] = NULL; | |
10599 | } | |
10600 | } | |
10601 | ||
10602 | return i ? i : ret; | |
10603 | } | |
10604 | ||
6c5c240e RP |
10605 | static void *io_uring_validate_mmap_request(struct file *file, |
10606 | loff_t pgoff, size_t sz) | |
2b188cc1 | 10607 | { |
2b188cc1 | 10608 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 10609 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
10610 | struct page *page; |
10611 | void *ptr; | |
10612 | ||
10613 | switch (offset) { | |
10614 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
10615 | case IORING_OFF_CQ_RING: |
10616 | ptr = ctx->rings; | |
2b188cc1 JA |
10617 | break; |
10618 | case IORING_OFF_SQES: | |
10619 | ptr = ctx->sq_sqes; | |
10620 | break; | |
2b188cc1 | 10621 | default: |
6c5c240e | 10622 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
10623 | } |
10624 | ||
10625 | page = virt_to_head_page(ptr); | |
a50b854e | 10626 | if (sz > page_size(page)) |
6c5c240e RP |
10627 | return ERR_PTR(-EINVAL); |
10628 | ||
10629 | return ptr; | |
10630 | } | |
10631 | ||
10632 | #ifdef CONFIG_MMU | |
10633 | ||
c072481d | 10634 | static __cold int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
6c5c240e RP |
10635 | { |
10636 | size_t sz = vma->vm_end - vma->vm_start; | |
10637 | unsigned long pfn; | |
10638 | void *ptr; | |
10639 | ||
10640 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
10641 | if (IS_ERR(ptr)) | |
10642 | return PTR_ERR(ptr); | |
2b188cc1 JA |
10643 | |
10644 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
10645 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
10646 | } | |
10647 | ||
6c5c240e RP |
10648 | #else /* !CONFIG_MMU */ |
10649 | ||
10650 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
10651 | { | |
10652 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
10653 | } | |
10654 | ||
10655 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
10656 | { | |
10657 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
10658 | } | |
10659 | ||
10660 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
10661 | unsigned long addr, unsigned long len, | |
10662 | unsigned long pgoff, unsigned long flags) | |
10663 | { | |
10664 | void *ptr; | |
10665 | ||
10666 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
10667 | if (IS_ERR(ptr)) | |
10668 | return PTR_ERR(ptr); | |
10669 | ||
10670 | return (unsigned long) ptr; | |
10671 | } | |
10672 | ||
10673 | #endif /* !CONFIG_MMU */ | |
10674 | ||
d9d05217 | 10675 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
90554200 JA |
10676 | { |
10677 | DEFINE_WAIT(wait); | |
10678 | ||
10679 | do { | |
10680 | if (!io_sqring_full(ctx)) | |
10681 | break; | |
90554200 JA |
10682 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
10683 | ||
10684 | if (!io_sqring_full(ctx)) | |
10685 | break; | |
90554200 JA |
10686 | schedule(); |
10687 | } while (!signal_pending(current)); | |
10688 | ||
10689 | finish_wait(&ctx->sqo_sq_wait, &wait); | |
5199328a | 10690 | return 0; |
90554200 JA |
10691 | } |
10692 | ||
c73ebb68 HX |
10693 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
10694 | struct __kernel_timespec __user **ts, | |
10695 | const sigset_t __user **sig) | |
10696 | { | |
10697 | struct io_uring_getevents_arg arg; | |
10698 | ||
10699 | /* | |
10700 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer | |
10701 | * is just a pointer to the sigset_t. | |
10702 | */ | |
10703 | if (!(flags & IORING_ENTER_EXT_ARG)) { | |
10704 | *sig = (const sigset_t __user *) argp; | |
10705 | *ts = NULL; | |
10706 | return 0; | |
10707 | } | |
10708 | ||
10709 | /* | |
10710 | * EXT_ARG is set - ensure we agree on the size of it and copy in our | |
10711 | * timespec and sigset_t pointers if good. | |
10712 | */ | |
10713 | if (*argsz != sizeof(arg)) | |
10714 | return -EINVAL; | |
10715 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
10716 | return -EFAULT; | |
d2347b96 DY |
10717 | if (arg.pad) |
10718 | return -EINVAL; | |
c73ebb68 HX |
10719 | *sig = u64_to_user_ptr(arg.sigmask); |
10720 | *argsz = arg.sigmask_sz; | |
10721 | *ts = u64_to_user_ptr(arg.ts); | |
10722 | return 0; | |
10723 | } | |
10724 | ||
2b188cc1 | 10725 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
c73ebb68 HX |
10726 | u32, min_complete, u32, flags, const void __user *, argp, |
10727 | size_t, argsz) | |
2b188cc1 JA |
10728 | { |
10729 | struct io_ring_ctx *ctx; | |
2b188cc1 JA |
10730 | int submitted = 0; |
10731 | struct fd f; | |
33f993da | 10732 | long ret; |
2b188cc1 | 10733 | |
4c6e277c | 10734 | io_run_task_work(); |
b41e9852 | 10735 | |
33f993da | 10736 | if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
e7a6c00d JA |
10737 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG | |
10738 | IORING_ENTER_REGISTERED_RING))) | |
2b188cc1 JA |
10739 | return -EINVAL; |
10740 | ||
e7a6c00d JA |
10741 | /* |
10742 | * Ring fd has been registered via IORING_REGISTER_RING_FDS, we | |
10743 | * need only dereference our task private array to find it. | |
10744 | */ | |
10745 | if (flags & IORING_ENTER_REGISTERED_RING) { | |
10746 | struct io_uring_task *tctx = current->io_uring; | |
10747 | ||
10748 | if (!tctx || fd >= IO_RINGFD_REG_MAX) | |
10749 | return -EINVAL; | |
10750 | fd = array_index_nospec(fd, IO_RINGFD_REG_MAX); | |
10751 | f.file = tctx->registered_rings[fd]; | |
10752 | if (unlikely(!f.file)) | |
10753 | return -EBADF; | |
10754 | } else { | |
10755 | f = fdget(fd); | |
10756 | if (unlikely(!f.file)) | |
10757 | return -EBADF; | |
10758 | } | |
2b188cc1 JA |
10759 | |
10760 | ret = -EOPNOTSUPP; | |
33f993da | 10761 | if (unlikely(f.file->f_op != &io_uring_fops)) |
2b188cc1 JA |
10762 | goto out_fput; |
10763 | ||
10764 | ret = -ENXIO; | |
10765 | ctx = f.file->private_data; | |
33f993da | 10766 | if (unlikely(!percpu_ref_tryget(&ctx->refs))) |
2b188cc1 JA |
10767 | goto out_fput; |
10768 | ||
7e84e1c7 | 10769 | ret = -EBADFD; |
33f993da | 10770 | if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED)) |
7e84e1c7 SG |
10771 | goto out; |
10772 | ||
6c271ce2 JA |
10773 | /* |
10774 | * For SQ polling, the thread will do all submissions and completions. | |
10775 | * Just return the requested submit count, and wake the thread if | |
10776 | * we were asked to. | |
10777 | */ | |
b2a9eada | 10778 | ret = 0; |
6c271ce2 | 10779 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
90f67366 | 10780 | io_cqring_overflow_flush(ctx); |
89448c47 | 10781 | |
21f96522 JA |
10782 | if (unlikely(ctx->sq_data->thread == NULL)) { |
10783 | ret = -EOWNERDEAD; | |
04147488 | 10784 | goto out; |
21f96522 | 10785 | } |
6c271ce2 | 10786 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 10787 | wake_up(&ctx->sq_data->wait); |
d9d05217 PB |
10788 | if (flags & IORING_ENTER_SQ_WAIT) { |
10789 | ret = io_sqpoll_wait_sq(ctx); | |
10790 | if (ret) | |
10791 | goto out; | |
10792 | } | |
6c271ce2 | 10793 | submitted = to_submit; |
b2a9eada | 10794 | } else if (to_submit) { |
eef51daa | 10795 | ret = io_uring_add_tctx_node(ctx); |
0f212204 JA |
10796 | if (unlikely(ret)) |
10797 | goto out; | |
2b188cc1 | 10798 | mutex_lock(&ctx->uring_lock); |
0f212204 | 10799 | submitted = io_submit_sqes(ctx, to_submit); |
2b188cc1 | 10800 | mutex_unlock(&ctx->uring_lock); |
7c504e65 PB |
10801 | |
10802 | if (submitted != to_submit) | |
10803 | goto out; | |
2b188cc1 JA |
10804 | } |
10805 | if (flags & IORING_ENTER_GETEVENTS) { | |
c73ebb68 HX |
10806 | const sigset_t __user *sig; |
10807 | struct __kernel_timespec __user *ts; | |
10808 | ||
10809 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); | |
10810 | if (unlikely(ret)) | |
10811 | goto out; | |
10812 | ||
2b188cc1 JA |
10813 | min_complete = min(min_complete, ctx->cq_entries); |
10814 | ||
32b2244a XW |
10815 | /* |
10816 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
10817 | * space applications don't need to do io completion events | |
10818 | * polling again, they can rely on io_sq_thread to do polling | |
10819 | * work, which can reduce cpu usage and uring_lock contention. | |
10820 | */ | |
10821 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
10822 | !(ctx->flags & IORING_SETUP_SQPOLL)) { | |
7668b92a | 10823 | ret = io_iopoll_check(ctx, min_complete); |
def596e9 | 10824 | } else { |
c73ebb68 | 10825 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
def596e9 | 10826 | } |
2b188cc1 JA |
10827 | } |
10828 | ||
7c504e65 | 10829 | out: |
6805b32e | 10830 | percpu_ref_put(&ctx->refs); |
2b188cc1 | 10831 | out_fput: |
e7a6c00d JA |
10832 | if (!(flags & IORING_ENTER_REGISTERED_RING)) |
10833 | fdput(f); | |
2b188cc1 JA |
10834 | return submitted ? submitted : ret; |
10835 | } | |
10836 | ||
bebdb65e | 10837 | #ifdef CONFIG_PROC_FS |
c072481d | 10838 | static __cold int io_uring_show_cred(struct seq_file *m, unsigned int id, |
61cf9370 | 10839 | const struct cred *cred) |
87ce955b | 10840 | { |
87ce955b JA |
10841 | struct user_namespace *uns = seq_user_ns(m); |
10842 | struct group_info *gi; | |
10843 | kernel_cap_t cap; | |
10844 | unsigned __capi; | |
10845 | int g; | |
10846 | ||
10847 | seq_printf(m, "%5d\n", id); | |
10848 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
10849 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
10850 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
10851 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
10852 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
10853 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
10854 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
10855 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
10856 | seq_puts(m, "\n\tGroups:\t"); | |
10857 | gi = cred->group_info; | |
10858 | for (g = 0; g < gi->ngroups; g++) { | |
10859 | seq_put_decimal_ull(m, g ? " " : "", | |
10860 | from_kgid_munged(uns, gi->gid[g])); | |
10861 | } | |
10862 | seq_puts(m, "\n\tCapEff:\t"); | |
10863 | cap = cred->cap_effective; | |
10864 | CAP_FOR_EACH_U32(__capi) | |
10865 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
10866 | seq_putc(m, '\n'); | |
10867 | return 0; | |
10868 | } | |
10869 | ||
c072481d PB |
10870 | static __cold void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, |
10871 | struct seq_file *m) | |
87ce955b | 10872 | { |
dbbe9c64 | 10873 | struct io_sq_data *sq = NULL; |
83f84356 HX |
10874 | struct io_overflow_cqe *ocqe; |
10875 | struct io_rings *r = ctx->rings; | |
10876 | unsigned int sq_mask = ctx->sq_entries - 1, cq_mask = ctx->cq_entries - 1; | |
83f84356 HX |
10877 | unsigned int sq_head = READ_ONCE(r->sq.head); |
10878 | unsigned int sq_tail = READ_ONCE(r->sq.tail); | |
10879 | unsigned int cq_head = READ_ONCE(r->cq.head); | |
10880 | unsigned int cq_tail = READ_ONCE(r->cq.tail); | |
f75d1183 | 10881 | unsigned int sq_entries, cq_entries; |
fad8e0de | 10882 | bool has_lock; |
83f84356 HX |
10883 | unsigned int i; |
10884 | ||
10885 | /* | |
10886 | * we may get imprecise sqe and cqe info if uring is actively running | |
10887 | * since we get cached_sq_head and cached_cq_tail without uring_lock | |
10888 | * and sq_tail and cq_head are changed by userspace. But it's ok since | |
10889 | * we usually use these info when it is stuck. | |
10890 | */ | |
c0235652 | 10891 | seq_printf(m, "SqMask:\t0x%x\n", sq_mask); |
f75d1183 JA |
10892 | seq_printf(m, "SqHead:\t%u\n", sq_head); |
10893 | seq_printf(m, "SqTail:\t%u\n", sq_tail); | |
10894 | seq_printf(m, "CachedSqHead:\t%u\n", ctx->cached_sq_head); | |
10895 | seq_printf(m, "CqMask:\t0x%x\n", cq_mask); | |
10896 | seq_printf(m, "CqHead:\t%u\n", cq_head); | |
10897 | seq_printf(m, "CqTail:\t%u\n", cq_tail); | |
10898 | seq_printf(m, "CachedCqTail:\t%u\n", ctx->cached_cq_tail); | |
10899 | seq_printf(m, "SQEs:\t%u\n", sq_tail - ctx->cached_sq_head); | |
10900 | sq_entries = min(sq_tail - sq_head, ctx->sq_entries); | |
10901 | for (i = 0; i < sq_entries; i++) { | |
10902 | unsigned int entry = i + sq_head; | |
10903 | unsigned int sq_idx = READ_ONCE(ctx->sq_array[entry & sq_mask]); | |
a1957780 | 10904 | struct io_uring_sqe *sqe; |
f75d1183 JA |
10905 | |
10906 | if (sq_idx > sq_mask) | |
10907 | continue; | |
10908 | sqe = &ctx->sq_sqes[sq_idx]; | |
10909 | seq_printf(m, "%5u: opcode:%d, fd:%d, flags:%x, user_data:%llu\n", | |
10910 | sq_idx, sqe->opcode, sqe->fd, sqe->flags, | |
10911 | sqe->user_data); | |
83f84356 | 10912 | } |
f75d1183 JA |
10913 | seq_printf(m, "CQEs:\t%u\n", cq_tail - cq_head); |
10914 | cq_entries = min(cq_tail - cq_head, ctx->cq_entries); | |
10915 | for (i = 0; i < cq_entries; i++) { | |
10916 | unsigned int entry = i + cq_head; | |
10917 | struct io_uring_cqe *cqe = &r->cqes[entry & cq_mask]; | |
83f84356 HX |
10918 | |
10919 | seq_printf(m, "%5u: user_data:%llu, res:%d, flag:%x\n", | |
f75d1183 JA |
10920 | entry & cq_mask, cqe->user_data, cqe->res, |
10921 | cqe->flags); | |
83f84356 | 10922 | } |
87ce955b | 10923 | |
fad8e0de JA |
10924 | /* |
10925 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, | |
10926 | * since fdinfo case grabs it in the opposite direction of normal use | |
10927 | * cases. If we fail to get the lock, we just don't iterate any | |
10928 | * structures that could be going away outside the io_uring mutex. | |
10929 | */ | |
10930 | has_lock = mutex_trylock(&ctx->uring_lock); | |
10931 | ||
5f3f26f9 | 10932 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) { |
dbbe9c64 | 10933 | sq = ctx->sq_data; |
5f3f26f9 JA |
10934 | if (!sq->thread) |
10935 | sq = NULL; | |
10936 | } | |
dbbe9c64 JQ |
10937 | |
10938 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); | |
10939 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); | |
87ce955b | 10940 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
fad8e0de | 10941 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
7b29f92d | 10942 | struct file *f = io_file_from_index(ctx, i); |
87ce955b | 10943 | |
87ce955b JA |
10944 | if (f) |
10945 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
10946 | else | |
10947 | seq_printf(m, "%5u: <none>\n", i); | |
10948 | } | |
10949 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
fad8e0de | 10950 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
41edf1a5 | 10951 | struct io_mapped_ubuf *buf = ctx->user_bufs[i]; |
4751f53d | 10952 | unsigned int len = buf->ubuf_end - buf->ubuf; |
87ce955b | 10953 | |
4751f53d | 10954 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len); |
87ce955b | 10955 | } |
61cf9370 MWO |
10956 | if (has_lock && !xa_empty(&ctx->personalities)) { |
10957 | unsigned long index; | |
10958 | const struct cred *cred; | |
10959 | ||
87ce955b | 10960 | seq_printf(m, "Personalities:\n"); |
61cf9370 MWO |
10961 | xa_for_each(&ctx->personalities, index, cred) |
10962 | io_uring_show_cred(m, index, cred); | |
87ce955b | 10963 | } |
83f84356 HX |
10964 | if (has_lock) |
10965 | mutex_unlock(&ctx->uring_lock); | |
10966 | ||
10967 | seq_puts(m, "PollList:\n"); | |
79ebeaee | 10968 | spin_lock(&ctx->completion_lock); |
d7718a9d JA |
10969 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
10970 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
10971 | struct io_kiocb *req; | |
10972 | ||
10973 | hlist_for_each_entry(req, list, hash_node) | |
10974 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
7f62d40d | 10975 | task_work_pending(req->task)); |
d7718a9d | 10976 | } |
83f84356 HX |
10977 | |
10978 | seq_puts(m, "CqOverflowList:\n"); | |
10979 | list_for_each_entry(ocqe, &ctx->cq_overflow_list, list) { | |
10980 | struct io_uring_cqe *cqe = &ocqe->cqe; | |
10981 | ||
10982 | seq_printf(m, " user_data=%llu, res=%d, flags=%x\n", | |
10983 | cqe->user_data, cqe->res, cqe->flags); | |
10984 | ||
10985 | } | |
10986 | ||
79ebeaee | 10987 | spin_unlock(&ctx->completion_lock); |
87ce955b JA |
10988 | } |
10989 | ||
c072481d | 10990 | static __cold void io_uring_show_fdinfo(struct seq_file *m, struct file *f) |
87ce955b JA |
10991 | { |
10992 | struct io_ring_ctx *ctx = f->private_data; | |
10993 | ||
10994 | if (percpu_ref_tryget(&ctx->refs)) { | |
10995 | __io_uring_show_fdinfo(ctx, m); | |
10996 | percpu_ref_put(&ctx->refs); | |
10997 | } | |
10998 | } | |
bebdb65e | 10999 | #endif |
87ce955b | 11000 | |
2b188cc1 JA |
11001 | static const struct file_operations io_uring_fops = { |
11002 | .release = io_uring_release, | |
11003 | .mmap = io_uring_mmap, | |
6c5c240e RP |
11004 | #ifndef CONFIG_MMU |
11005 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
11006 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
11007 | #endif | |
2b188cc1 | 11008 | .poll = io_uring_poll, |
bebdb65e | 11009 | #ifdef CONFIG_PROC_FS |
87ce955b | 11010 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 11011 | #endif |
2b188cc1 JA |
11012 | }; |
11013 | ||
c072481d PB |
11014 | static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
11015 | struct io_uring_params *p) | |
2b188cc1 | 11016 | { |
75b28aff HV |
11017 | struct io_rings *rings; |
11018 | size_t size, sq_array_offset; | |
2b188cc1 | 11019 | |
bd740481 JA |
11020 | /* make sure these are sane, as we already accounted them */ |
11021 | ctx->sq_entries = p->sq_entries; | |
11022 | ctx->cq_entries = p->cq_entries; | |
11023 | ||
75b28aff HV |
11024 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
11025 | if (size == SIZE_MAX) | |
11026 | return -EOVERFLOW; | |
11027 | ||
11028 | rings = io_mem_alloc(size); | |
11029 | if (!rings) | |
2b188cc1 JA |
11030 | return -ENOMEM; |
11031 | ||
75b28aff HV |
11032 | ctx->rings = rings; |
11033 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
11034 | rings->sq_ring_mask = p->sq_entries - 1; | |
11035 | rings->cq_ring_mask = p->cq_entries - 1; | |
11036 | rings->sq_ring_entries = p->sq_entries; | |
11037 | rings->cq_ring_entries = p->cq_entries; | |
2b188cc1 JA |
11038 | |
11039 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
11040 | if (size == SIZE_MAX) { |
11041 | io_mem_free(ctx->rings); | |
11042 | ctx->rings = NULL; | |
2b188cc1 | 11043 | return -EOVERFLOW; |
eb065d30 | 11044 | } |
2b188cc1 JA |
11045 | |
11046 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
11047 | if (!ctx->sq_sqes) { |
11048 | io_mem_free(ctx->rings); | |
11049 | ctx->rings = NULL; | |
2b188cc1 | 11050 | return -ENOMEM; |
eb065d30 | 11051 | } |
2b188cc1 | 11052 | |
2b188cc1 JA |
11053 | return 0; |
11054 | } | |
11055 | ||
9faadcc8 PB |
11056 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
11057 | { | |
11058 | int ret, fd; | |
11059 | ||
11060 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
11061 | if (fd < 0) | |
11062 | return fd; | |
11063 | ||
eef51daa | 11064 | ret = io_uring_add_tctx_node(ctx); |
9faadcc8 PB |
11065 | if (ret) { |
11066 | put_unused_fd(fd); | |
11067 | return ret; | |
11068 | } | |
11069 | fd_install(fd, file); | |
11070 | return fd; | |
11071 | } | |
11072 | ||
2b188cc1 JA |
11073 | /* |
11074 | * Allocate an anonymous fd, this is what constitutes the application | |
11075 | * visible backing of an io_uring instance. The application mmaps this | |
11076 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
11077 | * we have to tie this fd to a socket for file garbage collection purposes. | |
11078 | */ | |
9faadcc8 | 11079 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
2b188cc1 JA |
11080 | { |
11081 | struct file *file; | |
9faadcc8 | 11082 | #if defined(CONFIG_UNIX) |
2b188cc1 JA |
11083 | int ret; |
11084 | ||
2b188cc1 JA |
11085 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
11086 | &ctx->ring_sock); | |
11087 | if (ret) | |
9faadcc8 | 11088 | return ERR_PTR(ret); |
2b188cc1 JA |
11089 | #endif |
11090 | ||
91a9ab7c PM |
11091 | file = anon_inode_getfile_secure("[io_uring]", &io_uring_fops, ctx, |
11092 | O_RDWR | O_CLOEXEC, NULL); | |
2b188cc1 | 11093 | #if defined(CONFIG_UNIX) |
9faadcc8 PB |
11094 | if (IS_ERR(file)) { |
11095 | sock_release(ctx->ring_sock); | |
11096 | ctx->ring_sock = NULL; | |
11097 | } else { | |
11098 | ctx->ring_sock->file = file; | |
0f212204 | 11099 | } |
2b188cc1 | 11100 | #endif |
9faadcc8 | 11101 | return file; |
2b188cc1 JA |
11102 | } |
11103 | ||
c072481d PB |
11104 | static __cold int io_uring_create(unsigned entries, struct io_uring_params *p, |
11105 | struct io_uring_params __user *params) | |
2b188cc1 | 11106 | { |
2b188cc1 | 11107 | struct io_ring_ctx *ctx; |
9faadcc8 | 11108 | struct file *file; |
2b188cc1 JA |
11109 | int ret; |
11110 | ||
8110c1a6 | 11111 | if (!entries) |
2b188cc1 | 11112 | return -EINVAL; |
8110c1a6 JA |
11113 | if (entries > IORING_MAX_ENTRIES) { |
11114 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
11115 | return -EINVAL; | |
11116 | entries = IORING_MAX_ENTRIES; | |
11117 | } | |
2b188cc1 JA |
11118 | |
11119 | /* | |
11120 | * Use twice as many entries for the CQ ring. It's possible for the | |
11121 | * application to drive a higher depth than the size of the SQ ring, | |
11122 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
11123 | * some flexibility in overcommitting a bit. If the application has |
11124 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
11125 | * of CQ ring entries manually. | |
2b188cc1 JA |
11126 | */ |
11127 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
11128 | if (p->flags & IORING_SETUP_CQSIZE) { |
11129 | /* | |
11130 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
11131 | * to a power-of-two, if it isn't already. We do NOT impose | |
11132 | * any cq vs sq ring sizing. | |
11133 | */ | |
eb2667b3 | 11134 | if (!p->cq_entries) |
33a107f0 | 11135 | return -EINVAL; |
8110c1a6 JA |
11136 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
11137 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
11138 | return -EINVAL; | |
11139 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
11140 | } | |
eb2667b3 JQ |
11141 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
11142 | if (p->cq_entries < p->sq_entries) | |
11143 | return -EINVAL; | |
33a107f0 JA |
11144 | } else { |
11145 | p->cq_entries = 2 * p->sq_entries; | |
11146 | } | |
2b188cc1 | 11147 | |
2b188cc1 | 11148 | ctx = io_ring_ctx_alloc(p); |
62e398be | 11149 | if (!ctx) |
2b188cc1 | 11150 | return -ENOMEM; |
2b188cc1 | 11151 | ctx->compat = in_compat_syscall(); |
62e398be JA |
11152 | if (!capable(CAP_IPC_LOCK)) |
11153 | ctx->user = get_uid(current_user()); | |
2aede0e4 JA |
11154 | |
11155 | /* | |
11156 | * This is just grabbed for accounting purposes. When a process exits, | |
11157 | * the mm is exited and dropped before the files, hence we need to hang | |
11158 | * on to this mm purely for the purposes of being able to unaccount | |
11159 | * memory (locked/pinned vm). It's not used for anything else. | |
11160 | */ | |
6b7898eb | 11161 | mmgrab(current->mm); |
2aede0e4 | 11162 | ctx->mm_account = current->mm; |
6b7898eb | 11163 | |
2b188cc1 JA |
11164 | ret = io_allocate_scq_urings(ctx, p); |
11165 | if (ret) | |
11166 | goto err; | |
11167 | ||
7e84e1c7 | 11168 | ret = io_sq_offload_create(ctx, p); |
2b188cc1 JA |
11169 | if (ret) |
11170 | goto err; | |
eae071c9 | 11171 | /* always set a rsrc node */ |
47b228ce PB |
11172 | ret = io_rsrc_node_switch_start(ctx); |
11173 | if (ret) | |
11174 | goto err; | |
eae071c9 | 11175 | io_rsrc_node_switch(ctx, NULL); |
2b188cc1 | 11176 | |
2b188cc1 | 11177 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
11178 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
11179 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
11180 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
11181 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
11182 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
11183 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
11184 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
11185 | |
11186 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
11187 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
11188 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
11189 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
11190 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
11191 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
11192 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 11193 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 11194 | |
7f13657d XW |
11195 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
11196 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 | 11197 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
c73ebb68 | 11198 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
9690557e | 11199 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS | |
c4212f3e JA |
11200 | IORING_FEAT_RSRC_TAGS | IORING_FEAT_CQE_SKIP | |
11201 | IORING_FEAT_LINKED_FILE; | |
7f13657d XW |
11202 | |
11203 | if (copy_to_user(params, p, sizeof(*p))) { | |
11204 | ret = -EFAULT; | |
11205 | goto err; | |
11206 | } | |
d1719f70 | 11207 | |
9faadcc8 PB |
11208 | file = io_uring_get_file(ctx); |
11209 | if (IS_ERR(file)) { | |
11210 | ret = PTR_ERR(file); | |
11211 | goto err; | |
11212 | } | |
11213 | ||
044c1ab3 JA |
11214 | /* |
11215 | * Install ring fd as the very last thing, so we don't risk someone | |
11216 | * having closed it before we finish setup | |
11217 | */ | |
9faadcc8 PB |
11218 | ret = io_uring_install_fd(ctx, file); |
11219 | if (ret < 0) { | |
11220 | /* fput will clean it up */ | |
11221 | fput(file); | |
11222 | return ret; | |
11223 | } | |
044c1ab3 | 11224 | |
c826bd7a | 11225 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
11226 | return ret; |
11227 | err: | |
11228 | io_ring_ctx_wait_and_kill(ctx); | |
11229 | return ret; | |
11230 | } | |
11231 | ||
11232 | /* | |
11233 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
11234 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
11235 | * params structure passed in. | |
11236 | */ | |
11237 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
11238 | { | |
11239 | struct io_uring_params p; | |
2b188cc1 JA |
11240 | int i; |
11241 | ||
11242 | if (copy_from_user(&p, params, sizeof(p))) | |
11243 | return -EFAULT; | |
11244 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
11245 | if (p.resv[i]) | |
11246 | return -EINVAL; | |
11247 | } | |
11248 | ||
6c271ce2 | 11249 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 11250 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 | 11251 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
bcbb7bf6 | 11252 | IORING_SETUP_R_DISABLED | IORING_SETUP_SUBMIT_ALL)) |
2b188cc1 JA |
11253 | return -EINVAL; |
11254 | ||
7f13657d | 11255 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
11256 | } |
11257 | ||
11258 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
11259 | struct io_uring_params __user *, params) | |
11260 | { | |
11261 | return io_uring_setup(entries, params); | |
11262 | } | |
11263 | ||
c072481d PB |
11264 | static __cold int io_probe(struct io_ring_ctx *ctx, void __user *arg, |
11265 | unsigned nr_args) | |
66f4af93 JA |
11266 | { |
11267 | struct io_uring_probe *p; | |
11268 | size_t size; | |
11269 | int i, ret; | |
11270 | ||
11271 | size = struct_size(p, ops, nr_args); | |
11272 | if (size == SIZE_MAX) | |
11273 | return -EOVERFLOW; | |
11274 | p = kzalloc(size, GFP_KERNEL); | |
11275 | if (!p) | |
11276 | return -ENOMEM; | |
11277 | ||
11278 | ret = -EFAULT; | |
11279 | if (copy_from_user(p, arg, size)) | |
11280 | goto out; | |
11281 | ret = -EINVAL; | |
11282 | if (memchr_inv(p, 0, size)) | |
11283 | goto out; | |
11284 | ||
11285 | p->last_op = IORING_OP_LAST - 1; | |
11286 | if (nr_args > IORING_OP_LAST) | |
11287 | nr_args = IORING_OP_LAST; | |
11288 | ||
11289 | for (i = 0; i < nr_args; i++) { | |
11290 | p->ops[i].op = i; | |
11291 | if (!io_op_defs[i].not_supported) | |
11292 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
11293 | } | |
11294 | p->ops_len = i; | |
11295 | ||
11296 | ret = 0; | |
11297 | if (copy_to_user(arg, p, size)) | |
11298 | ret = -EFAULT; | |
11299 | out: | |
11300 | kfree(p); | |
11301 | return ret; | |
11302 | } | |
11303 | ||
071698e1 JA |
11304 | static int io_register_personality(struct io_ring_ctx *ctx) |
11305 | { | |
4379bf8b | 11306 | const struct cred *creds; |
61cf9370 | 11307 | u32 id; |
1e6fa521 | 11308 | int ret; |
071698e1 | 11309 | |
4379bf8b | 11310 | creds = get_current_cred(); |
1e6fa521 | 11311 | |
61cf9370 MWO |
11312 | ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds, |
11313 | XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL); | |
a30f895a JA |
11314 | if (ret < 0) { |
11315 | put_cred(creds); | |
11316 | return ret; | |
11317 | } | |
11318 | return id; | |
071698e1 JA |
11319 | } |
11320 | ||
c072481d PB |
11321 | static __cold int io_register_restrictions(struct io_ring_ctx *ctx, |
11322 | void __user *arg, unsigned int nr_args) | |
21b55dbc SG |
11323 | { |
11324 | struct io_uring_restriction *res; | |
11325 | size_t size; | |
11326 | int i, ret; | |
11327 | ||
7e84e1c7 SG |
11328 | /* Restrictions allowed only if rings started disabled */ |
11329 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
11330 | return -EBADFD; | |
11331 | ||
21b55dbc | 11332 | /* We allow only a single restrictions registration */ |
7e84e1c7 | 11333 | if (ctx->restrictions.registered) |
21b55dbc SG |
11334 | return -EBUSY; |
11335 | ||
11336 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) | |
11337 | return -EINVAL; | |
11338 | ||
11339 | size = array_size(nr_args, sizeof(*res)); | |
11340 | if (size == SIZE_MAX) | |
11341 | return -EOVERFLOW; | |
11342 | ||
11343 | res = memdup_user(arg, size); | |
11344 | if (IS_ERR(res)) | |
11345 | return PTR_ERR(res); | |
11346 | ||
11347 | ret = 0; | |
11348 | ||
11349 | for (i = 0; i < nr_args; i++) { | |
11350 | switch (res[i].opcode) { | |
11351 | case IORING_RESTRICTION_REGISTER_OP: | |
11352 | if (res[i].register_op >= IORING_REGISTER_LAST) { | |
11353 | ret = -EINVAL; | |
11354 | goto out; | |
11355 | } | |
11356 | ||
11357 | __set_bit(res[i].register_op, | |
11358 | ctx->restrictions.register_op); | |
11359 | break; | |
11360 | case IORING_RESTRICTION_SQE_OP: | |
11361 | if (res[i].sqe_op >= IORING_OP_LAST) { | |
11362 | ret = -EINVAL; | |
11363 | goto out; | |
11364 | } | |
11365 | ||
11366 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); | |
11367 | break; | |
11368 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: | |
11369 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; | |
11370 | break; | |
11371 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: | |
11372 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; | |
11373 | break; | |
11374 | default: | |
11375 | ret = -EINVAL; | |
11376 | goto out; | |
11377 | } | |
11378 | } | |
11379 | ||
11380 | out: | |
11381 | /* Reset all restrictions if an error happened */ | |
11382 | if (ret != 0) | |
11383 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); | |
11384 | else | |
7e84e1c7 | 11385 | ctx->restrictions.registered = true; |
21b55dbc SG |
11386 | |
11387 | kfree(res); | |
11388 | return ret; | |
11389 | } | |
11390 | ||
7e84e1c7 SG |
11391 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
11392 | { | |
11393 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
11394 | return -EBADFD; | |
11395 | ||
11396 | if (ctx->restrictions.registered) | |
11397 | ctx->restricted = 1; | |
11398 | ||
0298ef96 PB |
11399 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
11400 | if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait)) | |
11401 | wake_up(&ctx->sq_data->wait); | |
7e84e1c7 SG |
11402 | return 0; |
11403 | } | |
11404 | ||
fdecb662 | 11405 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 11406 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 PB |
11407 | unsigned nr_args) |
11408 | { | |
11409 | __u32 tmp; | |
11410 | int err; | |
11411 | ||
11412 | if (check_add_overflow(up->offset, nr_args, &tmp)) | |
11413 | return -EOVERFLOW; | |
11414 | err = io_rsrc_node_switch_start(ctx); | |
11415 | if (err) | |
11416 | return err; | |
11417 | ||
fdecb662 PB |
11418 | switch (type) { |
11419 | case IORING_RSRC_FILE: | |
98f0b3b4 | 11420 | return __io_sqe_files_update(ctx, up, nr_args); |
634d00df PB |
11421 | case IORING_RSRC_BUFFER: |
11422 | return __io_sqe_buffers_update(ctx, up, nr_args); | |
98f0b3b4 PB |
11423 | } |
11424 | return -EINVAL; | |
11425 | } | |
11426 | ||
c3bdad02 PB |
11427 | static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg, |
11428 | unsigned nr_args) | |
98f0b3b4 | 11429 | { |
c3bdad02 | 11430 | struct io_uring_rsrc_update2 up; |
98f0b3b4 PB |
11431 | |
11432 | if (!nr_args) | |
11433 | return -EINVAL; | |
c3bdad02 PB |
11434 | memset(&up, 0, sizeof(up)); |
11435 | if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update))) | |
11436 | return -EFAULT; | |
d8a3ba9c | 11437 | if (up.resv || up.resv2) |
565c5e61 | 11438 | return -EINVAL; |
c3bdad02 PB |
11439 | return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args); |
11440 | } | |
11441 | ||
11442 | static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg, | |
992da01a | 11443 | unsigned size, unsigned type) |
c3bdad02 PB |
11444 | { |
11445 | struct io_uring_rsrc_update2 up; | |
11446 | ||
11447 | if (size != sizeof(up)) | |
11448 | return -EINVAL; | |
98f0b3b4 PB |
11449 | if (copy_from_user(&up, arg, sizeof(up))) |
11450 | return -EFAULT; | |
d8a3ba9c | 11451 | if (!up.nr || up.resv || up.resv2) |
98f0b3b4 | 11452 | return -EINVAL; |
992da01a | 11453 | return __io_register_rsrc_update(ctx, type, &up, up.nr); |
98f0b3b4 PB |
11454 | } |
11455 | ||
c072481d | 11456 | static __cold int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg, |
992da01a | 11457 | unsigned int size, unsigned int type) |
792e3582 PB |
11458 | { |
11459 | struct io_uring_rsrc_register rr; | |
11460 | ||
11461 | /* keep it extendible */ | |
11462 | if (size != sizeof(rr)) | |
11463 | return -EINVAL; | |
11464 | ||
11465 | memset(&rr, 0, sizeof(rr)); | |
11466 | if (copy_from_user(&rr, arg, size)) | |
11467 | return -EFAULT; | |
992da01a | 11468 | if (!rr.nr || rr.resv || rr.resv2) |
792e3582 PB |
11469 | return -EINVAL; |
11470 | ||
992da01a | 11471 | switch (type) { |
792e3582 PB |
11472 | case IORING_RSRC_FILE: |
11473 | return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data), | |
11474 | rr.nr, u64_to_user_ptr(rr.tags)); | |
634d00df PB |
11475 | case IORING_RSRC_BUFFER: |
11476 | return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data), | |
11477 | rr.nr, u64_to_user_ptr(rr.tags)); | |
792e3582 PB |
11478 | } |
11479 | return -EINVAL; | |
11480 | } | |
11481 | ||
c072481d PB |
11482 | static __cold int io_register_iowq_aff(struct io_ring_ctx *ctx, |
11483 | void __user *arg, unsigned len) | |
fe76421d JA |
11484 | { |
11485 | struct io_uring_task *tctx = current->io_uring; | |
11486 | cpumask_var_t new_mask; | |
11487 | int ret; | |
11488 | ||
11489 | if (!tctx || !tctx->io_wq) | |
11490 | return -EINVAL; | |
11491 | ||
11492 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) | |
11493 | return -ENOMEM; | |
11494 | ||
11495 | cpumask_clear(new_mask); | |
11496 | if (len > cpumask_size()) | |
11497 | len = cpumask_size(); | |
11498 | ||
0f5e4b83 ES |
11499 | if (in_compat_syscall()) { |
11500 | ret = compat_get_bitmap(cpumask_bits(new_mask), | |
11501 | (const compat_ulong_t __user *)arg, | |
11502 | len * 8 /* CHAR_BIT */); | |
11503 | } else { | |
11504 | ret = copy_from_user(new_mask, arg, len); | |
11505 | } | |
11506 | ||
11507 | if (ret) { | |
fe76421d JA |
11508 | free_cpumask_var(new_mask); |
11509 | return -EFAULT; | |
11510 | } | |
11511 | ||
11512 | ret = io_wq_cpu_affinity(tctx->io_wq, new_mask); | |
11513 | free_cpumask_var(new_mask); | |
11514 | return ret; | |
11515 | } | |
11516 | ||
c072481d | 11517 | static __cold int io_unregister_iowq_aff(struct io_ring_ctx *ctx) |
fe76421d JA |
11518 | { |
11519 | struct io_uring_task *tctx = current->io_uring; | |
11520 | ||
11521 | if (!tctx || !tctx->io_wq) | |
11522 | return -EINVAL; | |
11523 | ||
11524 | return io_wq_cpu_affinity(tctx->io_wq, NULL); | |
11525 | } | |
11526 | ||
c072481d PB |
11527 | static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx, |
11528 | void __user *arg) | |
b22fa62a | 11529 | __must_hold(&ctx->uring_lock) |
2e480058 | 11530 | { |
b22fa62a | 11531 | struct io_tctx_node *node; |
fa84693b JA |
11532 | struct io_uring_task *tctx = NULL; |
11533 | struct io_sq_data *sqd = NULL; | |
2e480058 JA |
11534 | __u32 new_count[2]; |
11535 | int i, ret; | |
11536 | ||
2e480058 JA |
11537 | if (copy_from_user(new_count, arg, sizeof(new_count))) |
11538 | return -EFAULT; | |
11539 | for (i = 0; i < ARRAY_SIZE(new_count); i++) | |
11540 | if (new_count[i] > INT_MAX) | |
11541 | return -EINVAL; | |
11542 | ||
fa84693b JA |
11543 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
11544 | sqd = ctx->sq_data; | |
11545 | if (sqd) { | |
009ad9f0 JA |
11546 | /* |
11547 | * Observe the correct sqd->lock -> ctx->uring_lock | |
11548 | * ordering. Fine to drop uring_lock here, we hold | |
11549 | * a ref to the ctx. | |
11550 | */ | |
41d3a6bd | 11551 | refcount_inc(&sqd->refs); |
009ad9f0 | 11552 | mutex_unlock(&ctx->uring_lock); |
fa84693b | 11553 | mutex_lock(&sqd->lock); |
009ad9f0 | 11554 | mutex_lock(&ctx->uring_lock); |
41d3a6bd JA |
11555 | if (sqd->thread) |
11556 | tctx = sqd->thread->io_uring; | |
fa84693b JA |
11557 | } |
11558 | } else { | |
11559 | tctx = current->io_uring; | |
11560 | } | |
11561 | ||
e139a1ec | 11562 | BUILD_BUG_ON(sizeof(new_count) != sizeof(ctx->iowq_limits)); |
fa84693b | 11563 | |
bad119b9 PB |
11564 | for (i = 0; i < ARRAY_SIZE(new_count); i++) |
11565 | if (new_count[i]) | |
11566 | ctx->iowq_limits[i] = new_count[i]; | |
e139a1ec PB |
11567 | ctx->iowq_limits_set = true; |
11568 | ||
e139a1ec PB |
11569 | if (tctx && tctx->io_wq) { |
11570 | ret = io_wq_max_workers(tctx->io_wq, new_count); | |
11571 | if (ret) | |
11572 | goto err; | |
11573 | } else { | |
11574 | memset(new_count, 0, sizeof(new_count)); | |
11575 | } | |
fa84693b | 11576 | |
41d3a6bd | 11577 | if (sqd) { |
fa84693b | 11578 | mutex_unlock(&sqd->lock); |
41d3a6bd JA |
11579 | io_put_sq_data(sqd); |
11580 | } | |
2e480058 JA |
11581 | |
11582 | if (copy_to_user(arg, new_count, sizeof(new_count))) | |
11583 | return -EFAULT; | |
11584 | ||
b22fa62a PB |
11585 | /* that's it for SQPOLL, only the SQPOLL task creates requests */ |
11586 | if (sqd) | |
11587 | return 0; | |
11588 | ||
11589 | /* now propagate the restriction to all registered users */ | |
11590 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
11591 | struct io_uring_task *tctx = node->task->io_uring; | |
11592 | ||
11593 | if (WARN_ON_ONCE(!tctx->io_wq)) | |
11594 | continue; | |
11595 | ||
11596 | for (i = 0; i < ARRAY_SIZE(new_count); i++) | |
11597 | new_count[i] = ctx->iowq_limits[i]; | |
11598 | /* ignore errors, it always returns zero anyway */ | |
11599 | (void)io_wq_max_workers(tctx->io_wq, new_count); | |
11600 | } | |
2e480058 | 11601 | return 0; |
fa84693b | 11602 | err: |
41d3a6bd | 11603 | if (sqd) { |
fa84693b | 11604 | mutex_unlock(&sqd->lock); |
41d3a6bd JA |
11605 | io_put_sq_data(sqd); |
11606 | } | |
fa84693b | 11607 | return ret; |
2e480058 JA |
11608 | } |
11609 | ||
edafccee JA |
11610 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
11611 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
11612 | __releases(ctx->uring_lock) |
11613 | __acquires(ctx->uring_lock) | |
edafccee JA |
11614 | { |
11615 | int ret; | |
11616 | ||
35fa71a0 JA |
11617 | /* |
11618 | * We're inside the ring mutex, if the ref is already dying, then | |
11619 | * someone else killed the ctx or is already going through | |
11620 | * io_uring_register(). | |
11621 | */ | |
11622 | if (percpu_ref_is_dying(&ctx->refs)) | |
11623 | return -ENXIO; | |
11624 | ||
75c4021a PB |
11625 | if (ctx->restricted) { |
11626 | if (opcode >= IORING_REGISTER_LAST) | |
11627 | return -EINVAL; | |
11628 | opcode = array_index_nospec(opcode, IORING_REGISTER_LAST); | |
11629 | if (!test_bit(opcode, ctx->restrictions.register_op)) | |
11630 | return -EACCES; | |
11631 | } | |
11632 | ||
edafccee JA |
11633 | switch (opcode) { |
11634 | case IORING_REGISTER_BUFFERS: | |
634d00df | 11635 | ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL); |
edafccee JA |
11636 | break; |
11637 | case IORING_UNREGISTER_BUFFERS: | |
11638 | ret = -EINVAL; | |
11639 | if (arg || nr_args) | |
11640 | break; | |
0a96bbe4 | 11641 | ret = io_sqe_buffers_unregister(ctx); |
edafccee | 11642 | break; |
6b06314c | 11643 | case IORING_REGISTER_FILES: |
792e3582 | 11644 | ret = io_sqe_files_register(ctx, arg, nr_args, NULL); |
6b06314c JA |
11645 | break; |
11646 | case IORING_UNREGISTER_FILES: | |
11647 | ret = -EINVAL; | |
11648 | if (arg || nr_args) | |
11649 | break; | |
11650 | ret = io_sqe_files_unregister(ctx); | |
11651 | break; | |
c3a31e60 | 11652 | case IORING_REGISTER_FILES_UPDATE: |
c3bdad02 | 11653 | ret = io_register_files_update(ctx, arg, nr_args); |
c3a31e60 | 11654 | break; |
9b402849 JA |
11655 | case IORING_REGISTER_EVENTFD: |
11656 | ret = -EINVAL; | |
11657 | if (nr_args != 1) | |
11658 | break; | |
c75312dd UA |
11659 | ret = io_eventfd_register(ctx, arg, 0); |
11660 | break; | |
11661 | case IORING_REGISTER_EVENTFD_ASYNC: | |
11662 | ret = -EINVAL; | |
11663 | if (nr_args != 1) | |
f2842ab5 | 11664 | break; |
c75312dd | 11665 | ret = io_eventfd_register(ctx, arg, 1); |
9b402849 JA |
11666 | break; |
11667 | case IORING_UNREGISTER_EVENTFD: | |
11668 | ret = -EINVAL; | |
11669 | if (arg || nr_args) | |
11670 | break; | |
11671 | ret = io_eventfd_unregister(ctx); | |
11672 | break; | |
66f4af93 JA |
11673 | case IORING_REGISTER_PROBE: |
11674 | ret = -EINVAL; | |
11675 | if (!arg || nr_args > 256) | |
11676 | break; | |
11677 | ret = io_probe(ctx, arg, nr_args); | |
11678 | break; | |
071698e1 JA |
11679 | case IORING_REGISTER_PERSONALITY: |
11680 | ret = -EINVAL; | |
11681 | if (arg || nr_args) | |
11682 | break; | |
11683 | ret = io_register_personality(ctx); | |
11684 | break; | |
11685 | case IORING_UNREGISTER_PERSONALITY: | |
11686 | ret = -EINVAL; | |
11687 | if (arg) | |
11688 | break; | |
11689 | ret = io_unregister_personality(ctx, nr_args); | |
11690 | break; | |
7e84e1c7 SG |
11691 | case IORING_REGISTER_ENABLE_RINGS: |
11692 | ret = -EINVAL; | |
11693 | if (arg || nr_args) | |
11694 | break; | |
11695 | ret = io_register_enable_rings(ctx); | |
11696 | break; | |
21b55dbc SG |
11697 | case IORING_REGISTER_RESTRICTIONS: |
11698 | ret = io_register_restrictions(ctx, arg, nr_args); | |
11699 | break; | |
992da01a PB |
11700 | case IORING_REGISTER_FILES2: |
11701 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE); | |
11702 | break; | |
11703 | case IORING_REGISTER_FILES_UPDATE2: | |
11704 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
11705 | IORING_RSRC_FILE); | |
11706 | break; | |
11707 | case IORING_REGISTER_BUFFERS2: | |
11708 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER); | |
792e3582 | 11709 | break; |
992da01a PB |
11710 | case IORING_REGISTER_BUFFERS_UPDATE: |
11711 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
11712 | IORING_RSRC_BUFFER); | |
c3bdad02 | 11713 | break; |
fe76421d JA |
11714 | case IORING_REGISTER_IOWQ_AFF: |
11715 | ret = -EINVAL; | |
11716 | if (!arg || !nr_args) | |
11717 | break; | |
11718 | ret = io_register_iowq_aff(ctx, arg, nr_args); | |
11719 | break; | |
11720 | case IORING_UNREGISTER_IOWQ_AFF: | |
11721 | ret = -EINVAL; | |
11722 | if (arg || nr_args) | |
11723 | break; | |
11724 | ret = io_unregister_iowq_aff(ctx); | |
11725 | break; | |
2e480058 JA |
11726 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
11727 | ret = -EINVAL; | |
11728 | if (!arg || nr_args != 2) | |
11729 | break; | |
11730 | ret = io_register_iowq_max_workers(ctx, arg); | |
11731 | break; | |
e7a6c00d JA |
11732 | case IORING_REGISTER_RING_FDS: |
11733 | ret = io_ringfd_register(ctx, arg, nr_args); | |
11734 | break; | |
11735 | case IORING_UNREGISTER_RING_FDS: | |
11736 | ret = io_ringfd_unregister(ctx, arg, nr_args); | |
11737 | break; | |
edafccee JA |
11738 | default: |
11739 | ret = -EINVAL; | |
11740 | break; | |
11741 | } | |
11742 | ||
edafccee JA |
11743 | return ret; |
11744 | } | |
11745 | ||
11746 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
11747 | void __user *, arg, unsigned int, nr_args) | |
11748 | { | |
11749 | struct io_ring_ctx *ctx; | |
11750 | long ret = -EBADF; | |
11751 | struct fd f; | |
11752 | ||
11753 | f = fdget(fd); | |
11754 | if (!f.file) | |
11755 | return -EBADF; | |
11756 | ||
11757 | ret = -EOPNOTSUPP; | |
11758 | if (f.file->f_op != &io_uring_fops) | |
11759 | goto out_fput; | |
11760 | ||
11761 | ctx = f.file->private_data; | |
11762 | ||
b6c23dd5 PB |
11763 | io_run_task_work(); |
11764 | ||
edafccee JA |
11765 | mutex_lock(&ctx->uring_lock); |
11766 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
11767 | mutex_unlock(&ctx->uring_lock); | |
2757be22 | 11768 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, ret); |
edafccee JA |
11769 | out_fput: |
11770 | fdput(f); | |
11771 | return ret; | |
11772 | } | |
11773 | ||
2b188cc1 JA |
11774 | static int __init io_uring_init(void) |
11775 | { | |
d7f62e82 SM |
11776 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
11777 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
11778 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
11779 | } while (0) | |
11780 | ||
11781 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
11782 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
11783 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
11784 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
11785 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
11786 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
11787 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
11788 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
11789 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
11790 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 11791 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
11792 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
11793 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
11794 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
11795 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
11796 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
11797 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
11798 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
11799 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
11800 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
11801 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
11802 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
11803 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
11804 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
11805 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
11806 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 11807 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
11808 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
11809 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
16340eab | 11810 | BUILD_BUG_SQE_ELEM(40, __u16, buf_group); |
d7f62e82 | 11811 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
7d67af2c | 11812 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
b9445598 | 11813 | BUILD_BUG_SQE_ELEM(44, __u32, file_index); |
d7f62e82 | 11814 | |
b0d658ec PB |
11815 | BUILD_BUG_ON(sizeof(struct io_uring_files_update) != |
11816 | sizeof(struct io_uring_rsrc_update)); | |
11817 | BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) > | |
11818 | sizeof(struct io_uring_rsrc_update2)); | |
90499ad0 PB |
11819 | |
11820 | /* ->buf_index is u16 */ | |
11821 | BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16)); | |
11822 | ||
b0d658ec PB |
11823 | /* should fit into one byte */ |
11824 | BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8)); | |
68fe256a PB |
11825 | BUILD_BUG_ON(SQE_COMMON_FLAGS >= (1 << 8)); |
11826 | BUILD_BUG_ON((SQE_VALID_FLAGS | SQE_COMMON_FLAGS) != SQE_VALID_FLAGS); | |
b0d658ec | 11827 | |
d3656344 | 11828 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
32c2d33e | 11829 | BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof(int)); |
16340eab | 11830 | |
91f245d5 JA |
11831 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
11832 | SLAB_ACCOUNT); | |
2b188cc1 JA |
11833 | return 0; |
11834 | }; | |
11835 | __initcall(io_uring_init); |